pleroma-fe/src/components/emoji_picker/emoji_picker.js

431 lines
11 KiB
JavaScript
Raw Normal View History

import { library } from '@fortawesome/fontawesome-svg-core'
import {
2026-01-06 16:23:17 +02:00
faBasketballBall,
2020-10-28 22:52:20 +02:00
faBoxOpen,
2022-01-08 17:14:23 -05:00
faBus,
faCode,
2026-01-06 16:22:52 +02:00
faFlag,
2026-01-06 16:23:17 +02:00
faIceCream,
faLightbulb,
faPaw,
faSmile,
faSmileBeam,
faStickyNote,
faUser,
} from '@fortawesome/free-solid-svg-icons'
2026-01-06 16:23:17 +02:00
import { chunk, debounce, trim } from 'lodash'
import Popover from 'src/components/popover/popover.vue'
import { defineAsyncComponent } from 'vue'
import { ensureFinalFallback } from '../../i18n/languages.js'
import Checkbox from '../checkbox/checkbox.vue'
import StillImage from '../still-image/still-image.vue'
library.add(
2020-10-28 22:52:20 +02:00
faBoxOpen,
faStickyNote,
2022-01-08 17:14:23 -05:00
faSmileBeam,
faSmile,
faUser,
faPaw,
faIceCream,
faBus,
faBasketballBall,
faLightbulb,
faCode,
2026-01-06 16:22:52 +02:00
faFlag,
)
2022-01-08 17:14:23 -05:00
const UNICODE_EMOJI_GROUP_ICON = {
'smileys-and-emotion': 'smile',
'people-and-body': 'user',
'animals-and-nature': 'paw',
'food-and-drink': 'ice-cream',
'travel-and-places': 'bus',
2022-08-01 11:03:52 -04:00
activities: 'basketball-ball',
objects: 'lightbulb',
symbols: 'code',
2026-01-06 16:22:52 +02:00
flags: 'flag',
2022-01-08 17:14:23 -05:00
}
2022-09-20 21:50:40 -04:00
const maybeLocalizedKeywords = (emoji, languages, nameLocalizer) => {
const res = [emoji.displayText, nameLocalizer(emoji)]
if (emoji.annotations) {
2026-01-06 16:22:52 +02:00
languages.forEach((lang) => {
const keywords = emoji.annotations[lang]?.keywords || []
const name = emoji.annotations[lang]?.name
2026-01-06 16:22:52 +02:00
res.push(...keywords.concat([name]).filter((k) => k))
})
}
return res
}
2022-09-20 21:50:40 -04:00
const filterByKeyword = (list, keyword = '', languages, nameLocalizer) => {
if (keyword === '') return list
2020-09-18 11:07:38 +02:00
const keywordLowercase = keyword.toLowerCase()
2022-07-31 12:35:48 +03:00
const orderedEmojiList = []
for (const emoji of list) {
2022-09-20 21:50:40 -04:00
const indices = maybeLocalizedKeywords(emoji, languages, nameLocalizer)
2026-01-06 16:22:52 +02:00
.map((k) => k.toLowerCase().indexOf(keywordLowercase))
.filter((k) => k > -1)
const indexOfKeyword = indices.length ? Math.min(...indices) : -1
2020-09-21 17:42:17 +02:00
if (indexOfKeyword > -1) {
2020-09-21 18:13:31 +02:00
if (!Array.isArray(orderedEmojiList[indexOfKeyword])) {
orderedEmojiList[indexOfKeyword] = []
2020-09-21 18:10:55 +02:00
}
2020-09-21 18:13:31 +02:00
orderedEmojiList[indexOfKeyword].push(emoji)
}
}
2020-09-21 18:10:55 +02:00
return orderedEmojiList.flat()
2019-03-29 12:48:52 -04:00
}
2023-01-02 12:40:03 -05:00
const getOffset = (elem) => {
const style = elem.style.transform
const res = /translateY\((\d+)px\)/.exec(style)
2026-01-06 16:22:52 +02:00
if (!res) {
return 0
}
2023-01-02 12:40:03 -05:00
return res[1]
}
2026-01-06 16:22:52 +02:00
const toHeaderId = (id) => {
2023-01-02 13:25:59 -05:00
return id.replace(/^row-\d+-/, '')
}
2019-07-28 13:56:08 +03:00
const EmojiPicker = {
props: {
2019-09-12 20:36:43 +03:00
enableStickerPicker: {
required: false,
type: Boolean,
2026-01-06 16:22:52 +02:00
default: true,
},
hideCustomEmoji: {
required: false,
type: Boolean,
2026-01-06 16:22:52 +02:00
default: false,
},
},
inject: {
popoversZLayer: {
2026-01-06 16:22:52 +02:00
default: '',
},
},
2026-01-06 16:22:52 +02:00
data() {
2019-03-29 11:49:32 -04:00
return {
keyword: '',
activeGroup: 'custom',
showingStickers: false,
groupsScrolledClass: 'scrolled-top',
2019-10-03 20:16:01 +03:00
keepOpen: false,
customEmojiTimeout: null,
hideCustomEmojiInPicker: false,
// Lazy-load only after the first time `showing` becomes true.
2022-04-06 21:29:50 -04:00
contentLoaded: false,
groupRefs: {},
emojiRefs: {},
2023-01-02 13:42:09 -05:00
filteredEmojiGroups: [],
2024-06-25 23:30:08 +03:00
emojiSize: 0,
2026-01-06 16:22:52 +02:00
width: 0,
2019-03-29 11:49:32 -04:00
}
},
components: {
2026-01-06 16:22:52 +02:00
StickerPicker: defineAsyncComponent(
() => import('../sticker_picker/sticker_picker.vue'),
),
Checkbox,
StillImage,
2026-01-06 16:22:52 +02:00
Popover,
},
2019-03-29 11:49:32 -04:00
methods: {
2026-01-06 16:22:52 +02:00
groupScroll(e) {
2024-12-23 23:15:35 +02:00
e.currentTarget.scrollLeft += e.deltaY + e.deltaX
},
2026-01-06 16:22:52 +02:00
updateEmojiSize() {
2024-06-25 23:30:08 +03:00
const css = window.getComputedStyle(this.$refs.popover.$el)
2025-08-15 09:41:08 +03:00
const fontSize = css.getPropertyValue('font-size') || '1rem'
2024-12-28 16:48:33 +02:00
const emojiSize = css.getPropertyValue('--emojiSize') || '2.2rem'
2024-12-24 11:56:00 +02:00
2025-08-14 12:24:09 +03:00
const fontSizeUnit = fontSize.replace(/[0-9,.]+/, '').trim()
2024-12-28 16:48:33 +02:00
const fontSizeValue = Number(fontSize.replace(/[^0-9,.]+/, ''))
2024-12-24 11:56:00 +02:00
2025-08-14 12:24:09 +03:00
const emojiSizeUnit = emojiSize.replace(/[0-9,.]+/, '').trim()
2024-12-28 16:48:33 +02:00
const emojiSizeValue = Number(emojiSize.replace(/[^0-9,.]+/, ''))
2024-12-24 11:56:00 +02:00
let fontSizeMultiplier
if (fontSizeUnit.endsWith('em')) {
fontSizeMultiplier = fontSizeValue
} else {
fontSizeMultiplier = fontSizeValue / 14
}
2024-06-25 23:30:08 +03:00
let emojiSizeReal
if (emojiSizeUnit.endsWith('em')) {
2024-12-24 11:56:00 +02:00
emojiSizeReal = emojiSizeValue * fontSizeMultiplier * 14
2024-06-25 23:30:08 +03:00
} else {
emojiSizeReal = emojiSizeValue
}
2026-01-06 16:22:52 +02:00
const fullEmojiSize = emojiSizeReal + 2 * 0.2 * fontSizeMultiplier * 14
2024-06-25 23:30:08 +03:00
this.emojiSize = fullEmojiSize
},
2026-01-06 16:22:52 +02:00
showPicker() {
this.$refs.popover.showPopover()
2024-08-13 23:54:18 +03:00
this.$nextTick(() => {
this.onShowing()
})
},
2026-01-06 16:22:52 +02:00
hidePicker() {
this.$refs.popover.hidePopover()
},
2026-01-06 16:22:52 +02:00
setAnchorEl(el) {
2022-10-10 00:37:59 +03:00
this.$refs.popover.setAnchorEl(el)
},
2026-01-06 16:22:52 +02:00
setGroupRef(name) {
return (el) => {
this.groupRefs[name] = el
}
2022-04-06 21:29:50 -04:00
},
2026-01-06 16:22:52 +02:00
onPopoverShown() {
this.$emit('show')
},
2026-01-06 16:22:52 +02:00
onPopoverClosed() {
this.$emit('close')
},
2026-01-06 16:22:52 +02:00
onStickerUploaded(e) {
this.$emit('sticker-uploaded', e)
},
2026-01-06 16:22:52 +02:00
onStickerUploadFailed(e) {
this.$emit('sticker-upload-failed', e)
},
2026-01-06 16:22:52 +02:00
onEmoji(emoji) {
const value = emoji.imageUrl
? `:${emoji.displayText}:`
: emoji.replacement
2022-10-10 00:33:58 +03:00
if (!this.keepOpen) {
this.$refs.popover.hidePopover()
}
2026-01-06 16:22:52 +02:00
this.$emit('emoji', {
insertion: value,
insertionUrl: emoji.imageUrl,
keepOpen: this.keepOpen,
})
},
2026-01-06 16:22:52 +02:00
onScroll(startIndex, endIndex, visibleStartIndex, visibleEndIndex) {
2023-01-02 12:40:03 -05:00
const target = this.$refs['emoji-groups'].$el
2023-01-02 13:25:59 -05:00
this.scrolledGroup(target, visibleStartIndex, visibleEndIndex)
},
2026-01-06 16:22:52 +02:00
scrolledGroup(target, start, end) {
2023-01-02 12:40:03 -05:00
const top = target.scrollTop + 5
this.$nextTick(() => {
2026-01-06 16:22:52 +02:00
this.emojiItems.slice(start, end + 1).forEach((group) => {
2023-01-02 13:25:59 -05:00
const headerId = toHeaderId(group.id)
2023-01-02 12:40:03 -05:00
const ref = this.groupRefs['group-' + group.id]
2026-01-06 16:22:52 +02:00
if (!ref) {
return
}
2023-01-02 12:40:03 -05:00
const elem = ref.$el.parentElement
2026-01-06 16:22:52 +02:00
if (!elem) {
return
}
2023-01-02 12:40:03 -05:00
if (elem && getOffset(elem) <= top) {
2023-01-02 13:25:59 -05:00
this.activeGroup = headerId
2023-01-02 12:40:03 -05:00
}
})
this.scrollHeader()
})
},
2026-01-06 16:22:52 +02:00
scrollHeader() {
// Scroll the active tab's header into view
2022-04-06 21:29:50 -04:00
const headerRef = this.groupRefs['group-header-' + this.activeGroup]
const left = headerRef.offsetLeft
const right = left + headerRef.offsetWidth
const headerCont = this.$refs.header
const currentScroll = headerCont.scrollLeft
const currentScrollRight = currentScroll + headerCont.clientWidth
2026-01-06 16:22:52 +02:00
const setScroll = (s) => {
headerCont.scrollLeft = s
}
const margin = 7 // .emoji-tabs-item: padding
if (left - margin < currentScroll) {
setScroll(left - margin)
} else if (right + margin > currentScrollRight) {
setScroll(right + margin - headerCont.clientWidth)
}
},
2026-01-06 16:22:52 +02:00
highlight(groupId) {
this.setShowStickers(false)
2026-01-06 16:22:52 +02:00
const indexInList = this.emojiItems.findIndex((k) => k.id === groupId)
2023-01-02 13:25:59 -05:00
this.$refs['emoji-groups'].scrollToItem(indexInList)
},
2026-01-06 16:22:52 +02:00
updateScrolledClass(target) {
if (target.scrollTop <= 5) {
this.groupsScrolledClass = 'scrolled-top'
} else if (target.scrollTop >= target.scrollTopMax - 5) {
this.groupsScrolledClass = 'scrolled-bottom'
} else {
this.groupsScrolledClass = 'scrolled-middle'
}
},
2026-01-06 16:22:52 +02:00
toggleStickers() {
this.showingStickers = !this.showingStickers
},
2026-01-06 16:22:52 +02:00
setShowStickers(value) {
this.showingStickers = value
},
2026-01-06 16:22:52 +02:00
filterByKeyword(list, keyword) {
return filterByKeyword(
list,
keyword,
this.languages,
this.maybeLocalizedEmojiName,
)
},
2026-01-06 16:22:52 +02:00
onShowing() {
const oldContentLoaded = this.contentLoaded
2024-06-26 02:15:32 +03:00
this.updateEmojiSize()
2023-01-02 13:42:09 -05:00
this.recalculateItemPerRow()
this.$nextTick(() => {
this.$refs.search.focus()
})
this.contentLoaded = true
this.filteredEmojiGroups = this.getFilteredEmojiGroups()
if (!oldContentLoaded) {
this.$nextTick(() => {
if (this.defaultGroup) {
this.highlight(this.defaultGroup)
}
})
}
},
2026-01-06 16:22:52 +02:00
getFilteredEmojiGroups() {
return this.allEmojiGroups
2026-01-06 16:22:52 +02:00
.map((group) => ({
...group,
2026-01-06 16:22:52 +02:00
emojis: this.filterByKeyword(group.emojis, trim(this.keyword)),
}))
2026-01-06 16:22:52 +02:00
.filter((group) => group.emojis.length > 0)
2023-01-02 13:42:09 -05:00
},
2026-01-06 16:22:52 +02:00
recalculateItemPerRow() {
2023-01-02 13:42:09 -05:00
this.$nextTick(() => {
if (!this.$refs['emoji-groups']) {
return
}
2023-01-06 13:14:38 -05:00
this.width = this.$refs['emoji-groups'].$el.clientWidth
2023-01-02 13:42:09 -05:00
})
2026-01-06 16:22:52 +02:00
},
},
watch: {
2026-01-06 16:22:52 +02:00
keyword() {
this.onScroll()
this.debouncedHandleKeywordChange()
},
2026-01-06 16:22:52 +02:00
allCustomGroups() {
this.filteredEmojiGroups = this.getFilteredEmojiGroups()
2026-01-06 16:22:52 +02:00
},
},
2019-03-29 11:49:32 -04:00
computed: {
2026-01-06 16:22:52 +02:00
minItemSize() {
2024-06-26 02:15:32 +03:00
return this.emojiSize
2023-01-02 13:25:59 -05:00
},
2024-06-25 23:30:08 +03:00
// used to watch it
2026-01-06 16:22:52 +02:00
fontSize() {
2024-06-25 23:30:08 +03:00
this.$nextTick(() => {
this.updateEmojiSize()
})
return this.$store.getters.mergedConfig.fontSize
},
2026-01-06 16:22:52 +02:00
emojiHeight() {
2024-06-25 23:30:08 +03:00
return this.emojiSize
2023-01-02 13:25:59 -05:00
},
2026-01-06 16:22:52 +02:00
itemPerRow() {
2024-06-26 02:15:32 +03:00
return this.width ? Math.floor(this.width / this.emojiSize) : 6
2022-12-24 13:48:36 -05:00
},
2026-01-06 16:22:52 +02:00
activeGroupView() {
return this.showingStickers ? '' : this.activeGroup
},
2026-01-06 16:22:52 +02:00
stickersAvailable() {
if (this.$store.state.instance.stickers) {
return this.$store.state.instance.stickers.length > 0
}
return 0
},
2026-01-06 16:22:52 +02:00
allCustomGroups() {
if (this.hideCustomEmoji || this.hideCustomEmojiInPicker) {
return {}
}
const emojis = this.$store.getters.groupedCustomEmojis
if (emojis.unpacked) {
emojis.unpacked.text = this.$t('emoji.unpacked')
}
return emojis
},
2026-01-06 16:22:52 +02:00
defaultGroup() {
return Object.keys(this.allCustomGroups)[0]
},
2026-01-06 16:22:52 +02:00
unicodeEmojiGroups() {
return this.$store.getters.standardEmojiGroupList.map((group) => ({
id: `standard-${group.id}`,
text: this.$t(`emoji.unicode_groups.${group.id}`),
2022-01-08 17:14:23 -05:00
icon: UNICODE_EMOJI_GROUP_ICON[group.id],
2026-01-06 16:22:52 +02:00
emojis: group.emojis,
}))
},
2026-01-06 16:22:52 +02:00
allEmojiGroups() {
return Object.entries(this.allCustomGroups)
2025-02-04 15:23:21 +02:00
.map(([, v]) => v)
.concat(this.unicodeEmojiGroups)
},
2026-01-06 16:22:52 +02:00
stickerPickerEnabled() {
2019-09-12 20:36:43 +03:00
return (this.$store.state.instance.stickers || []).length !== 0
},
2026-01-06 16:22:52 +02:00
debouncedHandleKeywordChange() {
return debounce(() => {
this.filteredEmojiGroups = this.getFilteredEmojiGroups()
}, 500)
2022-09-20 20:44:52 -04:00
},
2026-01-06 16:22:52 +02:00
emojiItems() {
return this.filteredEmojiGroups
.map((group) =>
chunk(group.emojis, this.itemPerRow).map((items, index) => ({
2023-01-02 13:25:59 -05:00
...group,
id: index === 0 ? group.id : `row-${index}-${group.id}`,
emojis: items,
2026-01-06 16:22:52 +02:00
isFirstRow: index === 0,
})),
)
2023-01-02 13:25:59 -05:00
.reduce((a, c) => a.concat(c), [])
},
2026-01-06 16:22:52 +02:00
languages() {
return ensureFinalFallback(
this.$store.getters.mergedConfig.interfaceLanguage,
)
2022-09-20 20:44:52 -04:00
},
2026-01-06 16:22:52 +02:00
maybeLocalizedEmojiName() {
return (emoji) => {
2022-09-20 20:44:52 -04:00
if (!emoji.annotations) {
return emoji.displayText
}
2022-09-20 21:50:40 -04:00
if (emoji.displayTextI18n) {
return this.$t(emoji.displayTextI18n.key, emoji.displayTextI18n.args)
}
2022-09-20 20:44:52 -04:00
for (const lang of this.languages) {
if (emoji.annotations[lang]?.name) {
return emoji.annotations[lang].name
}
}
return emoji.displayText
}
},
2026-01-06 16:22:52 +02:00
isInModal() {
return this.popoversZLayer === 'modals'
2026-01-06 16:22:52 +02:00
},
},
2019-03-29 11:49:32 -04:00
}
2019-07-28 13:56:08 +03:00
export default EmojiPicker