2019-10-03 10:16:01 -07:00
|
|
|
import { set } from 'vue'
|
2019-08-12 10:01:38 -07:00
|
|
|
|
2019-10-07 13:46:40 -07:00
|
|
|
const LOAD_EMOJI_BY = 50
|
|
|
|
const LOAD_EMOJI_INTERVAL = 100
|
|
|
|
const LOAD_EMOJI_SANE_AMOUNT = 500
|
|
|
|
|
2019-03-29 09:48:52 -07:00
|
|
|
const filterByKeyword = (list, keyword = '') => {
|
2019-07-28 06:07:01 -07:00
|
|
|
return list.filter(x => x.displayText.includes(keyword))
|
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
|
|
|
|
}
|
|
|
|
},
|
2019-03-29 08:49:32 -07:00
|
|
|
data () {
|
|
|
|
return {
|
2019-09-08 05:51:17 -07:00
|
|
|
labelKey: String(Math.random() * 100000),
|
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-10-09 12:33:15 -07:00
|
|
|
customEmojiBuffer: (this.$store.state.instance.customEmoji || [])
|
|
|
|
.slice(0, LOAD_EMOJI_BY),
|
2019-10-07 13:46:40 -07:00
|
|
|
customEmojiTimeout: null,
|
2019-10-09 12:33:15 -07:00
|
|
|
customEmojiCounter: LOAD_EMOJI_BY,
|
2019-10-07 13:46:40 -07:00
|
|
|
customEmojiLoadAllConfirmed: false
|
2019-03-29 08:49:32 -07:00
|
|
|
}
|
|
|
|
},
|
2019-08-12 10:01:38 -07:00
|
|
|
components: {
|
|
|
|
StickerPicker: () => import('../sticker_picker/sticker_picker.vue')
|
|
|
|
},
|
2019-03-29 08:49:32 -07:00
|
|
|
methods: {
|
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
|
|
|
},
|
|
|
|
highlight (key) {
|
|
|
|
const ref = this.$refs['group-' + key]
|
|
|
|
const top = ref[0].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
|
|
|
},
|
|
|
|
scrolledGroup (e) {
|
2019-08-12 10:01:38 -07:00
|
|
|
const target = (e && e.target) || this.$refs['emoji-groups']
|
|
|
|
const top = target.scrollTop + 5
|
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-08-12 10:01:38 -07:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.emojisView.forEach(group => {
|
|
|
|
const ref = this.$refs['group-' + group.id]
|
|
|
|
if (ref[0].offsetTop <= top) {
|
|
|
|
this.activeGroup = group.id
|
|
|
|
}
|
|
|
|
})
|
2019-04-05 11:51:25 -07:00
|
|
|
})
|
2019-08-12 04:20:08 -07:00
|
|
|
},
|
2019-10-07 13:46:40 -07:00
|
|
|
loadEmojiInsane () {
|
|
|
|
this.customEmojiLoadAllConfirmed = true
|
|
|
|
this.continueEmojiLoad()
|
|
|
|
},
|
|
|
|
loadEmoji () {
|
|
|
|
const allLoaded = this.customEmojiBuffer.length === this.filteredEmoji.length
|
|
|
|
const saneLoaded = this.customEmojiBuffer.length >= LOAD_EMOJI_SANE_AMOUNT &&
|
|
|
|
!this.customEmojiLoadAllConfirmed
|
2019-10-03 10:16:01 -07:00
|
|
|
|
2019-10-07 13:46:40 -07:00
|
|
|
if (allLoaded || saneLoaded) {
|
|
|
|
return
|
2019-10-03 10:16:01 -07:00
|
|
|
}
|
2019-10-07 13:46:40 -07:00
|
|
|
|
|
|
|
this.customEmojiBuffer.push(
|
|
|
|
...this.filteredEmoji.slice(
|
|
|
|
this.customEmojiCounter,
|
|
|
|
this.customEmojiCounter + LOAD_EMOJI_BY
|
|
|
|
)
|
2019-10-03 10:16:01 -07:00
|
|
|
)
|
2019-10-07 13:46:40 -07:00
|
|
|
this.customEmojiTimeout = window.setTimeout(this.loadEmoji, LOAD_EMOJI_INTERVAL)
|
|
|
|
this.customEmojiCounter += LOAD_EMOJI_BY
|
|
|
|
},
|
2019-10-09 12:33:15 -07:00
|
|
|
startEmojiLoad (forceUpdate = false) {
|
|
|
|
const bufferSize = this.customEmojiBuffer.length
|
|
|
|
const bufferPrefilledSane = bufferSize === LOAD_EMOJI_SANE_AMOUNT && !this.customEmojiLoadAllConfirmed
|
|
|
|
const bufferPrefilledAll = bufferSize === this.filteredEmoji.length
|
|
|
|
if (!forceUpdate || bufferPrefilledSane || bufferPrefilledAll) {
|
|
|
|
return
|
|
|
|
}
|
2019-10-07 13:46:40 -07:00
|
|
|
if (this.customEmojiTimeout) {
|
|
|
|
window.clearTimeout(this.customEmojiTimeout)
|
|
|
|
}
|
|
|
|
|
2019-10-09 12:33:15 -07:00
|
|
|
set(
|
|
|
|
this,
|
|
|
|
'customEmojiBuffer',
|
|
|
|
this.filteredEmoji.slice(0, LOAD_EMOJI_BY)
|
|
|
|
)
|
|
|
|
this.customEmojiCounter = LOAD_EMOJI_BY
|
2019-10-07 13:46:40 -07:00
|
|
|
this.customEmojiTimeout = window.setTimeout(this.loadEmoji, LOAD_EMOJI_INTERVAL)
|
|
|
|
},
|
|
|
|
continueEmojiLoad () {
|
|
|
|
this.customEmojiTimeout = window.setTimeout(this.loadEmoji, LOAD_EMOJI_INTERVAL)
|
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
|
|
|
|
},
|
|
|
|
onStickerUploaded (e) {
|
|
|
|
this.$emit('sticker-uploaded', e)
|
|
|
|
},
|
|
|
|
onStickerUploadFailed (e) {
|
|
|
|
this.$emit('sticker-upload-failed', e)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
keyword () {
|
2019-10-09 12:33:15 -07:00
|
|
|
this.customEmojiLoadAllConfirmed = false
|
2019-08-12 10:01:38 -07:00
|
|
|
this.scrolledGroup()
|
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
|
|
|
|
},
|
2019-10-07 13:46:40 -07:00
|
|
|
saneAmount () {
|
|
|
|
// for UI
|
|
|
|
return LOAD_EMOJI_SANE_AMOUNT
|
|
|
|
},
|
|
|
|
filteredEmoji () {
|
|
|
|
return filterByKeyword(
|
|
|
|
this.$store.state.instance.customEmoji || [],
|
|
|
|
this.keyword
|
|
|
|
)
|
|
|
|
},
|
|
|
|
askForSanity () {
|
|
|
|
return this.customEmojiBuffer.length >= LOAD_EMOJI_SANE_AMOUNT &&
|
|
|
|
!this.customEmojiLoadAllConfirmed
|
|
|
|
},
|
2019-03-29 09:48:52 -07:00
|
|
|
emojis () {
|
|
|
|
const standardEmojis = this.$store.state.instance.emoji || []
|
2019-10-03 10:16:01 -07:00
|
|
|
const customEmojis = this.customEmojiBuffer
|
|
|
|
|
2019-08-12 10:01:38 -07:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
id: 'custom',
|
|
|
|
text: this.$t('emoji.custom'),
|
|
|
|
icon: 'icon-smile',
|
2019-10-03 10:16:01 -07:00
|
|
|
emojis: customEmojis
|
2019-07-28 06:07:01 -07:00
|
|
|
},
|
2019-08-12 10:01:38 -07:00
|
|
|
{
|
|
|
|
id: 'standard',
|
|
|
|
text: this.$t('emoji.unicode'),
|
|
|
|
icon: 'icon-picture',
|
2019-07-28 06:07:01 -07:00
|
|
|
emojis: filterByKeyword(standardEmojis, this.keyword)
|
2019-03-29 09:48:52 -07:00
|
|
|
}
|
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
|