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'
|
2021-10-08 05:02:16 +00:00
|
|
|
import lozad from 'lozad'
|
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'
|
2022-07-08 03:10:06 +00:00
|
|
|
import { debounce, trim } 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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
},
|
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,
|
|
|
|
// 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: {},
|
|
|
|
filteredEmojiGroups: []
|
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 }
|
|
|
|
},
|
|
|
|
setEmojiRef (name) {
|
|
|
|
return el => { this.emojiRefs[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
|
|
|
},
|
2019-11-08 19:25:13 +00:00
|
|
|
onScroll (e) {
|
|
|
|
const target = (e && e.target) || this.$refs['emoji-groups']
|
|
|
|
this.updateScrolledClass(target)
|
2021-10-08 05:11:32 +00:00
|
|
|
this.scrolledGroup(target)
|
|
|
|
},
|
|
|
|
scrolledGroup (target) {
|
|
|
|
const top = target.scrollTop + 5
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.allEmojiGroups.forEach(group => {
|
2022-04-07 01:29:50 +00:00
|
|
|
const ref = this.groupRefs['group-' + group.id]
|
|
|
|
if (ref && ref.offsetTop <= top) {
|
2021-10-08 05:11:32 +00:00
|
|
|
this.activeGroup = group.id
|
|
|
|
}
|
|
|
|
})
|
2022-01-08 07:17:59 +00:00
|
|
|
this.scrollHeader()
|
2021-10-08 05:11:32 +00:00
|
|
|
})
|
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)
|
|
|
|
}
|
|
|
|
},
|
2019-04-05 18:51:25 +00:00
|
|
|
highlight (key) {
|
2022-04-07 01:29:50 +00:00
|
|
|
const ref = this.groupRefs['group-' + key]
|
2021-04-25 11:51:00 +00:00
|
|
|
const top = ref.offsetTop
|
2019-08-12 17:01:38 +00:00
|
|
|
this.setShowStickers(false)
|
2019-04-05 18:51:25 +00:00
|
|
|
this.activeGroup = key
|
2019-08-12 17:01:38 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.$refs['emoji-groups'].scrollTop = top + 1
|
|
|
|
})
|
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
|
|
|
},
|
|
|
|
initializeLazyLoad () {
|
|
|
|
this.destroyLazyLoad()
|
2022-01-08 06:35:16 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.$lozad = lozad('.still-image.emoji-picker-emoji', {
|
|
|
|
load: el => {
|
2022-04-07 01:29:50 +00:00
|
|
|
const name = el.getAttribute('data-emoji-name')
|
|
|
|
const vn = this.emojiRefs[name]
|
2022-01-08 06:35:16 +00:00
|
|
|
if (!vn) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vn.loadLazy()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.$lozad.observe()
|
|
|
|
})
|
2021-10-08 05:02:16 +00:00
|
|
|
},
|
2021-10-08 19:25:13 +00:00
|
|
|
waitForDomAndInitializeLazyLoad () {
|
2021-10-08 19:09:24 +00:00
|
|
|
this.$nextTick(() => this.initializeLazyLoad())
|
|
|
|
},
|
2021-10-08 05:02:16 +00:00
|
|
|
destroyLazyLoad () {
|
|
|
|
if (this.$lozad) {
|
|
|
|
if (this.$lozad.observer) {
|
|
|
|
this.$lozad.observer.disconnect()
|
|
|
|
}
|
|
|
|
if (this.$lozad.mutationObserver) {
|
|
|
|
this.$lozad.mutationObserver.disconnect()
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 19:47:39 +00:00
|
|
|
},
|
|
|
|
onShowing () {
|
|
|
|
const oldContentLoaded = this.contentLoaded
|
|
|
|
this.contentLoaded = true
|
|
|
|
this.waitForDomAndInitializeLazyLoad()
|
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)
|
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 () {
|
2021-10-08 19:09:24 +00:00
|
|
|
this.waitForDomAndInitializeLazyLoad()
|
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
|
|
|
},
|
|
|
|
destroyed () {
|
|
|
|
this.destroyLazyLoad()
|
|
|
|
},
|
2019-03-29 15:49:32 +00:00
|
|
|
computed: {
|
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 () {
|
2021-08-15 04:43:35 +00:00
|
|
|
return this.$store.getters.groupedCustomEmojis
|
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.waitForDomAndInitializeLazyLoad()
|
|
|
|
this.filteredEmojiGroups = this.getFilteredEmojiGroups()
|
|
|
|
}, 500)
|
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
|
|
|
|
}
|
2019-03-29 15:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-28 10:56:08 +00:00
|
|
|
export default EmojiPicker
|