2021-04-25 04:51:00 -07:00
|
|
|
import { defineAsyncComponent } from 'vue'
|
2019-10-07 10:43:23 -07:00
|
|
|
import Checkbox from '../checkbox/checkbox.vue'
|
2021-10-08 10:17:47 -07:00
|
|
|
import StillImage from '../still-image/still-image.vue'
|
2021-10-07 22:02:16 -07:00
|
|
|
import lozad from 'lozad'
|
2020-10-19 09:38:49 -07:00
|
|
|
import { library } from '@fortawesome/fontawesome-svg-core'
|
|
|
|
import {
|
2020-10-28 13:52:20 -07:00
|
|
|
faBoxOpen,
|
2020-10-19 09:38:49 -07:00
|
|
|
faStickyNote,
|
2022-01-08 14:14:23 -08:00
|
|
|
faSmileBeam,
|
|
|
|
faSmile,
|
|
|
|
faUser,
|
|
|
|
faPaw,
|
|
|
|
faIceCream,
|
|
|
|
faBus,
|
|
|
|
faBasketballBall,
|
|
|
|
faLightbulb,
|
|
|
|
faCode,
|
|
|
|
faFlag
|
2020-10-19 09:38:49 -07:00
|
|
|
} from '@fortawesome/free-solid-svg-icons'
|
2022-06-12 04:38:12 -07:00
|
|
|
import { trim } from 'lodash'
|
2020-10-19 09:38:49 -07:00
|
|
|
|
|
|
|
library.add(
|
2020-10-28 13:52:20 -07:00
|
|
|
faBoxOpen,
|
2020-10-19 09:38:49 -07:00
|
|
|
faStickyNote,
|
2022-01-08 14:14:23 -08:00
|
|
|
faSmileBeam,
|
|
|
|
faSmile,
|
|
|
|
faUser,
|
|
|
|
faPaw,
|
|
|
|
faIceCream,
|
|
|
|
faBus,
|
|
|
|
faBasketballBall,
|
|
|
|
faLightbulb,
|
|
|
|
faCode,
|
|
|
|
faFlag
|
2020-10-19 09:38:49 -07:00
|
|
|
)
|
2019-08-12 10:01:38 -07:00
|
|
|
|
2022-01-08 14:14:23 -08: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',
|
|
|
|
'activities': 'basketball-ball',
|
|
|
|
'objects': 'lightbulb',
|
|
|
|
'symbols': 'code',
|
2022-01-08 14:17:32 -08:00
|
|
|
'flags': 'flag'
|
2022-01-08 14:14:23 -08:00
|
|
|
}
|
|
|
|
|
2019-03-29 09:48:52 -07:00
|
|
|
const filterByKeyword = (list, keyword = '') => {
|
2020-09-21 08:29:36 -07:00
|
|
|
if (keyword === '') return list
|
|
|
|
|
2020-09-18 02:07:38 -07:00
|
|
|
const keywordLowercase = keyword.toLowerCase()
|
2022-07-31 02:35:48 -07:00
|
|
|
const orderedEmojiList = []
|
2020-09-21 08:29:36 -07:00
|
|
|
for (const emoji of list) {
|
2020-09-21 08:42:17 -07:00
|
|
|
const indexOfKeyword = emoji.displayText.toLowerCase().indexOf(keywordLowercase)
|
|
|
|
if (indexOfKeyword > -1) {
|
2020-09-21 09:13:31 -07:00
|
|
|
if (!Array.isArray(orderedEmojiList[indexOfKeyword])) {
|
|
|
|
orderedEmojiList[indexOfKeyword] = []
|
2020-09-21 09:10:55 -07:00
|
|
|
}
|
2020-09-21 09:13:31 -07:00
|
|
|
orderedEmojiList[indexOfKeyword].push(emoji)
|
2020-09-21 08:29:36 -07:00
|
|
|
}
|
|
|
|
}
|
2020-09-21 09:10:55 -07:00
|
|
|
return orderedEmojiList.flat()
|
2019-03-29 09:48:52 -07:00
|
|
|
}
|
|
|
|
|
2019-07-28 03:56:08 -07:00
|
|
|
const EmojiPicker = {
|
2019-08-12 10:01:38 -07:00
|
|
|
props: {
|
2019-09-12 10:36:43 -07:00
|
|
|
enableStickerPicker: {
|
2019-08-12 10:01:38 -07:00
|
|
|
required: false,
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2021-10-08 12:09:24 -07:00
|
|
|
},
|
|
|
|
showing: {
|
|
|
|
required: true,
|
|
|
|
type: Boolean
|
2019-08-12 10:01:38 -07:00
|
|
|
}
|
|
|
|
},
|
2019-03-29 08:49:32 -07:00
|
|
|
data () {
|
|
|
|
return {
|
2019-04-05 11:51:25 -07:00
|
|
|
keyword: '',
|
2019-08-12 10:01:38 -07:00
|
|
|
activeGroup: 'custom',
|
2019-09-08 05:51:17 -07:00
|
|
|
showingStickers: false,
|
2019-09-14 15:16:54 -07:00
|
|
|
groupsScrolledClass: 'scrolled-top',
|
2019-10-03 10:16:01 -07:00
|
|
|
keepOpen: false,
|
2021-10-08 12:09:24 -07:00
|
|
|
customEmojiTimeout: null,
|
|
|
|
// Lazy-load only after the first time `showing` becomes true.
|
|
|
|
contentLoaded: false
|
2019-03-29 08:49:32 -07:00
|
|
|
}
|
|
|
|
},
|
2019-08-12 10:01:38 -07:00
|
|
|
components: {
|
2021-04-25 04:51:00 -07:00
|
|
|
StickerPicker: defineAsyncComponent(() => import('../sticker_picker/sticker_picker.vue')),
|
2021-10-08 10:17:47 -07:00
|
|
|
Checkbox,
|
|
|
|
StillImage
|
2019-08-12 10:01:38 -07:00
|
|
|
},
|
2019-03-29 08:49:32 -07:00
|
|
|
methods: {
|
2019-11-08 11:25:13 -08:00
|
|
|
onStickerUploaded (e) {
|
|
|
|
this.$emit('sticker-uploaded', e)
|
|
|
|
},
|
|
|
|
onStickerUploadFailed (e) {
|
|
|
|
this.$emit('sticker-upload-failed', e)
|
|
|
|
},
|
2019-03-29 12:56:50 -07:00
|
|
|
onEmoji (emoji) {
|
2019-07-28 06:07:01 -07:00
|
|
|
const value = emoji.imageUrl ? `:${emoji.displayText}:` : emoji.replacement
|
2019-09-23 10:29:01 -07:00
|
|
|
this.$emit('emoji', { insertion: value, keepOpen: this.keepOpen })
|
2019-04-05 11:51:25 -07:00
|
|
|
},
|
2019-11-08 11:25:13 -08:00
|
|
|
onScroll (e) {
|
|
|
|
const target = (e && e.target) || this.$refs['emoji-groups']
|
|
|
|
this.updateScrolledClass(target)
|
2021-10-07 22:11:32 -07:00
|
|
|
this.scrolledGroup(target)
|
|
|
|
},
|
|
|
|
scrolledGroup (target) {
|
|
|
|
const top = target.scrollTop + 5
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.allEmojiGroups.forEach(group => {
|
|
|
|
const ref = this.$refs['group-' + group.id]
|
|
|
|
if (ref[0].offsetTop <= top) {
|
|
|
|
this.activeGroup = group.id
|
|
|
|
}
|
|
|
|
})
|
2022-01-07 23:17:59 -08:00
|
|
|
this.scrollHeader()
|
2021-10-07 22:11:32 -07:00
|
|
|
})
|
2019-11-08 11:25:13 -08:00
|
|
|
},
|
2022-01-07 23:17:59 -08:00
|
|
|
scrollHeader () {
|
|
|
|
// Scroll the active tab's header into view
|
|
|
|
const headerRef = this.$refs['group-header-' + this.activeGroup][0]
|
|
|
|
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 11:51:25 -07:00
|
|
|
highlight (key) {
|
|
|
|
const ref = this.$refs['group-' + key]
|
2021-04-25 04:51:00 -07:00
|
|
|
const top = ref.offsetTop
|
2019-08-12 10:01:38 -07:00
|
|
|
this.setShowStickers(false)
|
2019-04-05 11:51:25 -07:00
|
|
|
this.activeGroup = key
|
2019-08-12 10:01:38 -07:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.$refs['emoji-groups'].scrollTop = top + 1
|
|
|
|
})
|
2019-04-05 11:51:25 -07:00
|
|
|
},
|
2019-11-08 11:25:13 -08:00
|
|
|
updateScrolledClass (target) {
|
2019-09-14 15:16:54 -07: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 11:25:13 -08:00
|
|
|
},
|
2019-08-12 10:01:38 -07:00
|
|
|
toggleStickers () {
|
|
|
|
this.showingStickers = !this.showingStickers
|
|
|
|
},
|
|
|
|
setShowStickers (value) {
|
|
|
|
this.showingStickers = value
|
2021-08-14 20:37:00 -07:00
|
|
|
},
|
2021-08-14 21:43:35 -07:00
|
|
|
filterByKeyword (list, keyword) {
|
|
|
|
return filterByKeyword(list, keyword)
|
2021-10-07 22:02:16 -07:00
|
|
|
},
|
|
|
|
initializeLazyLoad () {
|
|
|
|
this.destroyLazyLoad()
|
2022-01-07 22:35:16 -08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.$lozad = lozad('.still-image.emoji-picker-emoji', {
|
|
|
|
load: el => {
|
|
|
|
const vn = el.__vue__
|
|
|
|
if (!vn) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vn.loadLazy()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.$lozad.observe()
|
|
|
|
})
|
2021-10-07 22:02:16 -07:00
|
|
|
},
|
2021-10-08 12:25:13 -07:00
|
|
|
waitForDomAndInitializeLazyLoad () {
|
2021-10-08 12:09:24 -07:00
|
|
|
this.$nextTick(() => this.initializeLazyLoad())
|
|
|
|
},
|
2021-10-07 22:02:16 -07:00
|
|
|
destroyLazyLoad () {
|
|
|
|
if (this.$lozad) {
|
|
|
|
if (this.$lozad.observer) {
|
|
|
|
this.$lozad.observer.disconnect()
|
|
|
|
}
|
|
|
|
if (this.$lozad.mutationObserver) {
|
|
|
|
this.$lozad.mutationObserver.disconnect()
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 12:47:39 -07:00
|
|
|
},
|
|
|
|
onShowing () {
|
|
|
|
const oldContentLoaded = this.contentLoaded
|
|
|
|
this.contentLoaded = true
|
|
|
|
this.waitForDomAndInitializeLazyLoad()
|
|
|
|
if (!oldContentLoaded) {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.defaultGroup) {
|
|
|
|
this.highlight(this.defaultGroup)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-08-12 10:01:38 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
keyword () {
|
2019-11-08 11:25:13 -08:00
|
|
|
this.onScroll()
|
2021-10-08 12:09:24 -07:00
|
|
|
this.waitForDomAndInitializeLazyLoad()
|
2021-10-07 22:02:16 -07:00
|
|
|
},
|
|
|
|
allCustomGroups () {
|
2021-10-08 12:09:24 -07:00
|
|
|
this.waitForDomAndInitializeLazyLoad()
|
|
|
|
},
|
|
|
|
showing (val) {
|
|
|
|
if (val) {
|
2021-10-08 12:47:39 -07:00
|
|
|
this.onShowing()
|
2021-10-08 12:09:24 -07:00
|
|
|
}
|
2019-03-29 08:49:32 -07:00
|
|
|
}
|
|
|
|
},
|
2021-10-07 22:02:16 -07:00
|
|
|
mounted () {
|
2021-10-08 12:47:39 -07:00
|
|
|
if (this.showing) {
|
|
|
|
this.onShowing()
|
2021-10-07 22:11:32 -07:00
|
|
|
}
|
2021-10-07 22:02:16 -07:00
|
|
|
},
|
|
|
|
destroyed () {
|
|
|
|
this.destroyLazyLoad()
|
|
|
|
},
|
2019-03-29 08:49:32 -07:00
|
|
|
computed: {
|
2019-08-12 10:01:38 -07: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-14 20:37:00 -07:00
|
|
|
allCustomGroups () {
|
2021-08-14 21:43:35 -07:00
|
|
|
return this.$store.getters.groupedCustomEmojis
|
2021-08-14 20:37:00 -07:00
|
|
|
},
|
2021-10-07 22:11:32 -07:00
|
|
|
defaultGroup () {
|
|
|
|
return Object.keys(this.allCustomGroups)[0]
|
|
|
|
},
|
2022-01-08 13:55:00 -08:00
|
|
|
unicodeEmojiGroups () {
|
|
|
|
return this.$store.getters.standardEmojiGroupList.map(group => ({
|
|
|
|
id: `standard-${group.id}`,
|
|
|
|
text: this.$t(`emoji.unicode_groups.${group.id}`),
|
2022-01-08 14:14:23 -08:00
|
|
|
icon: UNICODE_EMOJI_GROUP_ICON[group.id],
|
2022-01-08 13:55:00 -08:00
|
|
|
emojis: group.emojis
|
|
|
|
}))
|
|
|
|
},
|
2021-08-14 20:37:00 -07:00
|
|
|
allEmojiGroups () {
|
|
|
|
return Object.entries(this.allCustomGroups)
|
|
|
|
.map(([_, v]) => v)
|
2022-01-08 13:55:00 -08:00
|
|
|
.concat(this.unicodeEmojiGroups)
|
2021-08-14 18:50:58 -07:00
|
|
|
},
|
2021-08-14 21:43:35 -07:00
|
|
|
filteredEmojiGroups () {
|
|
|
|
return this.allEmojiGroups
|
|
|
|
.map(group => ({
|
|
|
|
...group,
|
|
|
|
emojis: filterByKeyword(group.emojis, this.keyword)
|
|
|
|
}))
|
|
|
|
.filter(group => group.emojis.length > 0)
|
2019-08-12 10:01:38 -07:00
|
|
|
},
|
2019-09-12 10:36:43 -07:00
|
|
|
stickerPickerEnabled () {
|
|
|
|
return (this.$store.state.instance.stickers || []).length !== 0
|
2019-03-29 08:49:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-28 03:56:08 -07:00
|
|
|
export default EmojiPicker
|