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