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'
|
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,
|
|
|
|
faSmileBeam
|
|
|
|
} 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,
|
|
|
|
faSmileBeam
|
|
|
|
)
|
2019-08-12 10:01:38 -07:00
|
|
|
|
2019-11-08 11:25:13 -08:00
|
|
|
// At widest, approximately 20 emoji are visible in a row,
|
|
|
|
// loading 3 rows, could be overkill for narrow picker
|
|
|
|
const LOAD_EMOJI_BY = 60
|
|
|
|
|
|
|
|
// When to start loading new batch emoji, in pixels
|
|
|
|
const LOAD_EMOJI_MARGIN = 64
|
2019-10-07 13:46:40 -07: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
|
|
|
}
|
|
|
|
|
2021-08-14 18:50:58 -07:00
|
|
|
const packOf = emoji => (emoji.tags.filter(k => k.startsWith('pack:'))[0] || '').slice(5)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
},
|
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,
|
2019-11-08 10:12:01 -08:00
|
|
|
customEmojiBufferSlice: LOAD_EMOJI_BY,
|
2019-10-07 13:46:40 -07:00
|
|
|
customEmojiTimeout: null,
|
2021-08-14 20:37:00 -07:00
|
|
|
customEmojiLoadAllConfirmed: false,
|
|
|
|
groupLoadedCount: {}
|
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')),
|
2019-10-07 10:43:23 -07:00
|
|
|
Checkbox
|
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)
|
|
|
|
this.scrolledGroup(target)
|
2021-08-14 20:37:00 -07:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.triggerLoadMore(target)
|
|
|
|
})
|
2019-11-08 11:25:13 -08:00
|
|
|
},
|
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
|
2021-08-14 20:37:00 -07:00
|
|
|
this.loadEmoji(key)
|
2019-08-12 10:01:38 -07:00
|
|
|
})
|
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
|
|
|
},
|
|
|
|
triggerLoadMore (target) {
|
2021-08-14 20:37:00 -07:00
|
|
|
Object.keys(this.allCustomGroups)
|
|
|
|
.map(groupId => {
|
|
|
|
const ref = this.$refs[`group-end-${groupId}`][0]
|
|
|
|
if (!ref) return undefined
|
2019-11-08 11:25:13 -08:00
|
|
|
|
2021-08-14 20:37:00 -07:00
|
|
|
const bottom = ref.offsetTop + ref.offsetHeight
|
2019-11-08 11:25:13 -08:00
|
|
|
|
2021-08-14 20:37:00 -07:00
|
|
|
const group = this.$refs[`group-${groupId}`][0]
|
|
|
|
const top = group.offsetTop
|
|
|
|
|
|
|
|
const scrollerBottom = target.scrollTop + target.clientHeight
|
|
|
|
const scrollerTop = target.scrollTop
|
|
|
|
const scrollerMax = target.scrollHeight
|
|
|
|
|
|
|
|
// Loads more emoji when they come into view
|
|
|
|
const approachingBottom = bottom - scrollerBottom < LOAD_EMOJI_MARGIN
|
|
|
|
// Always load when at the very top in case there's no scroll space yet
|
|
|
|
const atTop = scrollerTop < top + target.clientHeight / 2 && top < scrollerBottom
|
|
|
|
// Don't load when looking at unicode category or at the very bottom
|
|
|
|
const bottomAboveViewport = bottom < scrollerTop || scrollerBottom === scrollerMax
|
|
|
|
if (!bottomAboveViewport && (approachingBottom || atTop)) {
|
|
|
|
return groupId
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
})
|
|
|
|
.filter(k => k)
|
|
|
|
.map(k => {
|
|
|
|
this.loadEmoji(k)
|
|
|
|
})
|
2019-11-08 11:25:13 -08:00
|
|
|
},
|
|
|
|
scrolledGroup (target) {
|
|
|
|
const top = target.scrollTop + 5
|
2019-08-12 10:01:38 -07:00
|
|
|
this.$nextTick(() => {
|
2021-08-14 20:37:00 -07:00
|
|
|
this.allEmojiGroups.forEach(group => {
|
2019-08-12 10:01:38 -07:00
|
|
|
const ref = this.$refs['group-' + group.id]
|
2021-04-25 04:51:00 -07:00
|
|
|
if (ref.offsetTop <= top) {
|
2019-08-12 10:01:38 -07:00
|
|
|
this.activeGroup = group.id
|
|
|
|
}
|
|
|
|
})
|
2019-04-05 11:51:25 -07:00
|
|
|
})
|
2019-08-12 04:20:08 -07:00
|
|
|
},
|
2021-08-14 20:37:00 -07:00
|
|
|
loadEmoji (loadGroup) {
|
|
|
|
if (!this.allCustomGroups[loadGroup]) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const allLoaded = this.loadedCount[loadGroup] >= this.allCustomGroups[loadGroup].emojis.length
|
2019-10-03 10:16:01 -07:00
|
|
|
|
2019-11-08 11:25:13 -08:00
|
|
|
if (allLoaded) {
|
2019-10-07 13:46:40 -07:00
|
|
|
return
|
2019-10-03 10:16:01 -07:00
|
|
|
}
|
2019-10-07 13:46:40 -07:00
|
|
|
|
2021-08-14 20:37:00 -07:00
|
|
|
this.groupLoadedCount = {
|
|
|
|
...this.groupLoadedCount,
|
|
|
|
[loadGroup]: this.loadedCount[loadGroup] + LOAD_EMOJI_BY
|
|
|
|
}
|
2019-10-07 13:46:40 -07:00
|
|
|
},
|
2019-10-09 12:33:15 -07:00
|
|
|
startEmojiLoad (forceUpdate = false) {
|
2019-11-08 13:57:20 -08:00
|
|
|
if (!forceUpdate) {
|
|
|
|
this.keyword = ''
|
|
|
|
}
|
2019-11-08 11:44:36 -08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.$refs['emoji-groups'].scrollTop = 0
|
|
|
|
})
|
2019-10-09 12:33:15 -07:00
|
|
|
const bufferSize = this.customEmojiBuffer.length
|
|
|
|
const bufferPrefilledAll = bufferSize === this.filteredEmoji.length
|
2019-11-08 11:25:13 -08:00
|
|
|
if (bufferPrefilledAll && !forceUpdate) {
|
2019-10-09 12:33:15 -07:00
|
|
|
return
|
|
|
|
}
|
2019-11-08 10:12:01 -08:00
|
|
|
this.customEmojiBufferSlice = LOAD_EMOJI_BY
|
2019-10-03 10:16:01 -07: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
|
|
|
},
|
|
|
|
limitedEmojis (list, groupId) {
|
|
|
|
return list.slice(0, this.loadedCount[groupId])
|
2019-08-12 10:01:38 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
keyword () {
|
2019-10-09 12:33:15 -07:00
|
|
|
this.customEmojiLoadAllConfirmed = false
|
2019-11-08 11:25:13 -08:00
|
|
|
this.onScroll()
|
2019-10-09 12:33:15 -07:00
|
|
|
this.startEmojiLoad(true)
|
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 18:50:58 -07:00
|
|
|
allEmojis () {
|
|
|
|
return this.$store.state.instance.customEmoji || []
|
|
|
|
},
|
2019-10-07 13:46:40 -07:00
|
|
|
filteredEmoji () {
|
|
|
|
return filterByKeyword(
|
2021-08-14 18:50:58 -07:00
|
|
|
this.allEmojis,
|
2022-06-12 04:38:12 -07:00
|
|
|
trim(this.keyword)
|
2019-10-07 13:46:40 -07:00
|
|
|
)
|
|
|
|
},
|
2019-11-08 10:12:01 -08:00
|
|
|
customEmojiBuffer () {
|
|
|
|
return this.filteredEmoji.slice(0, this.customEmojiBufferSlice)
|
|
|
|
},
|
2021-08-14 18:10:24 -07:00
|
|
|
groupedCustomEmojis () {
|
|
|
|
return this.customEmojiBuffer.reduce((res, emoji) => {
|
|
|
|
const pack = packOf(emoji)
|
|
|
|
if (!res[pack]) {
|
|
|
|
res[pack] = {
|
|
|
|
id: `custom-${pack}`,
|
|
|
|
text: pack,
|
|
|
|
/// FIXME
|
|
|
|
// icon: 'smile-beam',
|
|
|
|
image: emoji.imageUrl,
|
|
|
|
emojis: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res[pack].emojis.push(emoji)
|
|
|
|
return res
|
|
|
|
}, {})
|
|
|
|
},
|
2021-08-14 20:37:00 -07:00
|
|
|
allCustomGroups () {
|
|
|
|
return this.filteredEmoji
|
2021-08-14 18:50:58 -07:00
|
|
|
.reduce((res, emoji) => {
|
|
|
|
const packName = packOf(emoji)
|
|
|
|
const packId = `custom-${packName}`
|
2021-08-14 20:37:00 -07:00
|
|
|
if (!res[packId]) {
|
|
|
|
res[packId] = ({
|
2021-08-14 18:50:58 -07:00
|
|
|
id: packId,
|
|
|
|
text: packName,
|
2021-08-14 20:37:00 -07:00
|
|
|
image: emoji.imageUrl,
|
|
|
|
emojis: []
|
2021-08-14 18:50:58 -07:00
|
|
|
})
|
|
|
|
}
|
2021-08-14 20:37:00 -07:00
|
|
|
res[packId].emojis.push(emoji)
|
2021-08-14 18:50:58 -07:00
|
|
|
return res
|
2021-08-14 20:37:00 -07:00
|
|
|
}, {})
|
|
|
|
},
|
|
|
|
sensibleInitialAmountForAGroup () {
|
|
|
|
const groupCount = Object.keys(this.allCustomGroups).length
|
|
|
|
return Math.max(Math.floor(LOAD_EMOJI_BY / Math.max(groupCount, 1)), 1)
|
|
|
|
},
|
|
|
|
allEmojiGroups () {
|
|
|
|
const standardEmojis = this.$store.state.instance.emoji || []
|
|
|
|
return Object.entries(this.allCustomGroups)
|
|
|
|
.map(([_, v]) => v)
|
2021-08-14 18:50:58 -07:00
|
|
|
.concat({
|
|
|
|
id: 'standard',
|
|
|
|
text: this.$t('emoji.unicode'),
|
2021-08-14 20:37:00 -07:00
|
|
|
icon: 'box-open',
|
|
|
|
emojis: filterByKeyword(standardEmojis, this.keyword)
|
2021-08-14 18:50:58 -07:00
|
|
|
})
|
|
|
|
},
|
2019-03-29 09:48:52 -07:00
|
|
|
emojis () {
|
|
|
|
const standardEmojis = this.$store.state.instance.emoji || []
|
2021-08-14 18:10:24 -07:00
|
|
|
// const customEmojis = this.customEmojiBuffer
|
2019-10-03 10:16:01 -07:00
|
|
|
|
2019-08-12 10:01:38 -07:00
|
|
|
return [
|
2021-08-14 18:10:24 -07:00
|
|
|
...Object
|
|
|
|
.keys(this.groupedCustomEmojis)
|
|
|
|
.map(k => this.groupedCustomEmojis[k]),
|
2019-08-12 10:01:38 -07:00
|
|
|
{
|
|
|
|
id: 'standard',
|
|
|
|
text: this.$t('emoji.unicode'),
|
2020-10-28 13:52:20 -07:00
|
|
|
icon: 'box-open',
|
2022-06-12 04:38:12 -07:00
|
|
|
emojis: filterByKeyword(standardEmojis, trim(this.keyword))
|
2019-03-29 09:48:52 -07:00
|
|
|
}
|
2019-08-12 10:01:38 -07:00
|
|
|
]
|
|
|
|
},
|
2021-08-14 20:37:00 -07:00
|
|
|
loadedCount () {
|
|
|
|
return Object.keys(this.allCustomGroups)
|
|
|
|
.reduce((res, groupId) => {
|
|
|
|
res[groupId] = this.groupLoadedCount[groupId] || this.sensibleInitialAmountForAGroup
|
|
|
|
return res
|
|
|
|
}, {})
|
|
|
|
},
|
2021-08-14 18:23:45 -07:00
|
|
|
lastNonUnicodeGroupId () {
|
|
|
|
return this.emojis[this.emojis.length - 2].id
|
|
|
|
},
|
2019-08-12 10:01:38 -07:00
|
|
|
emojisView () {
|
|
|
|
return this.emojis.filter(value => value.emojis.length > 0)
|
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
|