akkoma-fe/src/components/emoji_picker/emoji_picker.js

152 lines
4.0 KiB
JavaScript
Raw Normal View History

2021-04-25 11:51:00 +00:00
import { defineAsyncComponent } from 'vue'
import Checkbox from '../checkbox/checkbox.vue'
import EmojiGrid from '../emoji_grid/emoji_grid.vue'
import { library } from '@fortawesome/fontawesome-svg-core'
import {
2020-10-28 20:52:20 +00:00
faBoxOpen,
faStickyNote,
faSmileBeam
} from '@fortawesome/free-solid-svg-icons'
import { trim, escapeRegExp, startCase } from 'lodash'
library.add(
2020-10-28 20:52:20 +00:00
faBoxOpen,
faStickyNote,
faSmileBeam
)
2019-07-28 10:56:08 +00:00
const EmojiPicker = {
props: {
2019-09-12 17:36:43 +00:00
enableStickerPicker: {
required: false,
type: Boolean,
default: false
},
showKeepOpen: {
required: false,
type: Boolean,
default: false
}
},
2019-03-29 15:49:32 +00:00
data () {
return {
keyword: '',
activeGroup: 'standard',
showingStickers: false,
keepOpen: false
2019-03-29 15:49:32 +00:00
}
},
components: {
2021-04-25 11:51:00 +00:00
StickerPicker: defineAsyncComponent(() => import('../sticker_picker/sticker_picker.vue')),
Checkbox,
EmojiGrid
},
2019-03-29 15:49:32 +00:00
methods: {
onStickerUploaded (e) {
this.$emit('sticker-uploaded', e)
},
onStickerUploadFailed (e) {
this.$emit('sticker-upload-failed', e)
},
onEmoji (emoji) {
const value = emoji.imageUrl ? `:${emoji.displayText}:` : emoji.replacement
this.$emit('emoji', { insertion: value, keepOpen: this.keepOpen })
},
onWheel (e) {
e.preventDefault()
this.$refs['emoji-tabs'].scrollBy(e.deltaY, 0)
},
highlight (key) {
this.setShowStickers(false)
this.activeGroup = key
if (this.keyword.length) {
this.$refs.emojiGrid.scrollToItem(key)
}
},
onActiveGroup (group) {
this.activeGroup = group
2019-10-03 17:16:01 +00:00
},
toggleStickers () {
this.showingStickers = !this.showingStickers
},
setShowStickers (value) {
this.showingStickers = value
},
filterByKeyword (list) {
if (this.keyword === '') return list
const regex = new RegExp(escapeRegExp(trim(this.keyword)), 'i')
return list.filter(emoji => {
return (regex.test(emoji.displayText) || (!emoji.imageUrl && emoji.replacement === this.keyword))
})
}
},
2019-03-29 15:49:32 +00:00
computed: {
activeGroupView () {
return this.showingStickers ? '' : this.activeGroup
},
stickersAvailable () {
if (this.$store.state.instance.stickers) {
return this.$store.state.instance.stickers.length > 0
}
return 0
},
filteredEmoji () {
return this.filterByKeyword(
this.$store.state.instance.customEmoji || []
)
},
2019-03-29 16:48:52 +00:00
emojis () {
const standardEmojis = this.$store.state.instance.emoji || []
const customEmojis = this.sortedEmoji
const emojiPacks = []
customEmojis.forEach((pack, id) => {
emojiPacks.push({
id: id.replace(/^pack:/, ''),
text: startCase(id.replace(/^pack:/, '')),
first: pack[0],
emojis: this.filterByKeyword(pack)
})
})
return [
{
id: 'standard',
text: this.$t('emoji.unicode'),
first: {
imageUrl: '',
replacement: '🥴'
},
emojis: this.filterByKeyword(standardEmojis)
}
].concat(emojiPacks)
},
sortedEmoji () {
const customEmojis = this.$store.state.instance.customEmoji || []
const sortedEmojiGroups = new Map()
customEmojis.forEach((emoji) => {
if (!sortedEmojiGroups.has(emoji.tags[0])) {
sortedEmojiGroups.set(emoji.tags[0], [emoji])
} else {
sortedEmojiGroups.get(emoji.tags[0]).push(emoji)
2019-03-29 16:48:52 +00:00
}
})
return new Map([...sortedEmojiGroups.entries()].sort())
},
emojisView () {
if (this.keyword === '') {
return this.emojis.filter(pack => {
return pack.id === this.activeGroup
})
} else {
return this.emojis.filter(pack => {
return pack.emojis.length > 0
})
}
2019-09-12 17:36:43 +00:00
},
stickerPickerEnabled () {
return (this.$store.state.instance.stickers || []).length !== 0 && this.enableStickerPicker
2019-03-29 15:49:32 +00:00
}
}
}
2019-07-28 10:56:08 +00:00
export default EmojiPicker