2019-10-07 17:43:23 +00:00
|
|
|
import Checkbox from '../checkbox/checkbox.vue'
|
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,
|
|
|
|
faSmileBeam
|
|
|
|
} from '@fortawesome/free-solid-svg-icons'
|
|
|
|
|
|
|
|
library.add(
|
2020-10-28 20:52:20 +00:00
|
|
|
faBoxOpen,
|
2020-10-19 16:38:49 +00:00
|
|
|
faStickyNote,
|
|
|
|
faSmileBeam
|
|
|
|
)
|
2019-08-12 17:01:38 +00:00
|
|
|
|
2019-11-08 19:25:13 +00: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 20:46:40 +00:00
|
|
|
|
2019-03-29 16:48:52 +00:00
|
|
|
const filterByKeyword = (list, keyword = '') => {
|
2020-09-21 15:29:36 +00:00
|
|
|
if (keyword === '') return list
|
|
|
|
|
2020-09-18 09:07:38 +00:00
|
|
|
const keywordLowercase = keyword.toLowerCase()
|
2020-09-21 16:10:55 +00:00
|
|
|
let orderedEmojiList = []
|
2020-09-21 15:29:36 +00:00
|
|
|
for (const emoji of list) {
|
2020-09-21 15:42:17 +00:00
|
|
|
const indexOfKeyword = emoji.displayText.toLowerCase().indexOf(keywordLowercase)
|
|
|
|
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,
|
2019-11-08 18:12:01 +00:00
|
|
|
customEmojiBufferSlice: LOAD_EMOJI_BY,
|
2019-10-07 20:46:40 +00:00
|
|
|
customEmojiTimeout: null,
|
|
|
|
customEmojiLoadAllConfirmed: false
|
2019-03-29 15:49:32 +00:00
|
|
|
}
|
|
|
|
},
|
2019-08-12 17:01:38 +00:00
|
|
|
components: {
|
2019-10-07 17:43:23 +00:00
|
|
|
StickerPicker: () => import('../sticker_picker/sticker_picker.vue'),
|
|
|
|
Checkbox
|
2019-08-12 17:01:38 +00:00
|
|
|
},
|
2019-03-29 15:49:32 +00:00
|
|
|
methods: {
|
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
|
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)
|
|
|
|
this.scrolledGroup(target)
|
|
|
|
this.triggerLoadMore(target)
|
|
|
|
},
|
2019-04-05 18:51:25 +00:00
|
|
|
highlight (key) {
|
|
|
|
const ref = this.$refs['group-' + key]
|
|
|
|
const top = ref[0].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
|
|
|
},
|
|
|
|
triggerLoadMore (target) {
|
|
|
|
const ref = this.$refs['group-end-custom'][0]
|
2019-11-08 21:57:20 +00:00
|
|
|
if (!ref) return
|
2019-11-08 19:25:13 +00:00
|
|
|
const bottom = ref.offsetTop + ref.offsetHeight
|
|
|
|
|
|
|
|
const scrollerBottom = target.scrollTop + target.clientHeight
|
|
|
|
const scrollerTop = target.scrollTop
|
2019-11-08 19:28:51 +00:00
|
|
|
const scrollerMax = target.scrollHeight
|
2019-11-08 19:25:13 +00:00
|
|
|
|
|
|
|
// 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 < 5
|
2019-11-08 19:28:51 +00:00
|
|
|
// Don't load when looking at unicode category or at the very bottom
|
|
|
|
const bottomAboveViewport = bottom < scrollerTop || scrollerBottom === scrollerMax
|
2019-11-08 19:25:13 +00:00
|
|
|
if (!bottomAboveViewport && (approachingBottom || atTop)) {
|
|
|
|
this.loadEmoji()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
scrolledGroup (target) {
|
|
|
|
const top = target.scrollTop + 5
|
2019-08-12 17:01:38 +00: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 18:51:25 +00:00
|
|
|
})
|
2019-08-12 11:20:08 +00:00
|
|
|
},
|
2019-10-07 20:46:40 +00:00
|
|
|
loadEmoji () {
|
|
|
|
const allLoaded = this.customEmojiBuffer.length === this.filteredEmoji.length
|
2019-10-03 17:16:01 +00:00
|
|
|
|
2019-11-08 19:25:13 +00:00
|
|
|
if (allLoaded) {
|
2019-10-07 20:46:40 +00:00
|
|
|
return
|
2019-10-03 17:16:01 +00:00
|
|
|
}
|
2019-10-07 20:46:40 +00:00
|
|
|
|
2019-11-08 18:12:01 +00:00
|
|
|
this.customEmojiBufferSlice += LOAD_EMOJI_BY
|
2019-10-07 20:46:40 +00:00
|
|
|
},
|
2019-10-09 19:33:15 +00:00
|
|
|
startEmojiLoad (forceUpdate = false) {
|
2019-11-08 21:57:20 +00:00
|
|
|
if (!forceUpdate) {
|
|
|
|
this.keyword = ''
|
|
|
|
}
|
2019-11-08 19:44:36 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.$refs['emoji-groups'].scrollTop = 0
|
|
|
|
})
|
2019-10-09 19:33:15 +00:00
|
|
|
const bufferSize = this.customEmojiBuffer.length
|
|
|
|
const bufferPrefilledAll = bufferSize === this.filteredEmoji.length
|
2019-11-08 19:25:13 +00:00
|
|
|
if (bufferPrefilledAll && !forceUpdate) {
|
2019-10-09 19:33:15 +00:00
|
|
|
return
|
|
|
|
}
|
2019-11-08 18:12:01 +00:00
|
|
|
this.customEmojiBufferSlice = LOAD_EMOJI_BY
|
2019-10-03 17:16:01 +00:00
|
|
|
},
|
2019-08-12 17:01:38 +00:00
|
|
|
toggleStickers () {
|
|
|
|
this.showingStickers = !this.showingStickers
|
|
|
|
},
|
|
|
|
setShowStickers (value) {
|
|
|
|
this.showingStickers = value
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
keyword () {
|
2019-10-09 19:33:15 +00:00
|
|
|
this.customEmojiLoadAllConfirmed = false
|
2019-11-08 19:25:13 +00:00
|
|
|
this.onScroll()
|
2019-10-09 19:33:15 +00:00
|
|
|
this.startEmojiLoad(true)
|
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
|
|
|
|
},
|
2019-10-07 20:46:40 +00:00
|
|
|
filteredEmoji () {
|
|
|
|
return filterByKeyword(
|
|
|
|
this.$store.state.instance.customEmoji || [],
|
|
|
|
this.keyword
|
|
|
|
)
|
|
|
|
},
|
2019-11-08 18:12:01 +00:00
|
|
|
customEmojiBuffer () {
|
|
|
|
return this.filteredEmoji.slice(0, this.customEmojiBufferSlice)
|
|
|
|
},
|
2019-03-29 16:48:52 +00:00
|
|
|
emojis () {
|
|
|
|
const standardEmojis = this.$store.state.instance.emoji || []
|
2019-10-03 17:16:01 +00:00
|
|
|
const customEmojis = this.customEmojiBuffer
|
|
|
|
|
2019-08-12 17:01:38 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
id: 'custom',
|
|
|
|
text: this.$t('emoji.custom'),
|
2020-10-19 16:38:49 +00:00
|
|
|
icon: 'smile-beam',
|
2019-10-03 17:16:01 +00:00
|
|
|
emojis: customEmojis
|
2019-07-28 13:07:01 +00:00
|
|
|
},
|
2019-08-12 17:01:38 +00:00
|
|
|
{
|
|
|
|
id: 'standard',
|
|
|
|
text: this.$t('emoji.unicode'),
|
2020-10-28 20:52:20 +00:00
|
|
|
icon: 'box-open',
|
2019-07-28 13:07:01 +00:00
|
|
|
emojis: filterByKeyword(standardEmojis, this.keyword)
|
2019-03-29 16:48:52 +00:00
|
|
|
}
|
2019-08-12 17:01:38 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
emojisView () {
|
|
|
|
return this.emojis.filter(value => value.emojis.length > 0)
|
2019-09-12 17:36:43 +00:00
|
|
|
},
|
|
|
|
stickerPickerEnabled () {
|
|
|
|
return (this.$store.state.instance.stickers || []).length !== 0
|
2019-03-29 15:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-28 10:56:08 +00:00
|
|
|
export default EmojiPicker
|