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

358 行
9.7 KiB
JavaScript
Raw 通常表示 履歴

2021-04-25 20:51:00 +09:00
import { defineAsyncComponent } from 'vue'
import Checkbox from '../checkbox/checkbox.vue'
import Popover from 'src/components/popover/popover.vue'
import StillImage from '../still-image/still-image.vue'
2022-09-21 09:44:52 +09:00
import { ensureFinalFallback } from '../../i18n/languages.js'
import { library } from '@fortawesome/fontawesome-svg-core'
import {
2020-10-29 05:52:20 +09:00
faBoxOpen,
faStickyNote,
2022-01-09 07:14:23 +09:00
faSmileBeam,
faSmile,
faUser,
faPaw,
faIceCream,
faBus,
faBasketballBall,
faLightbulb,
faCode,
faFlag
} from '@fortawesome/free-solid-svg-icons'
2023-01-03 03:25:59 +09:00
import { debounce, trim, chunk } from 'lodash'
library.add(
2020-10-29 05:52:20 +09:00
faBoxOpen,
faStickyNote,
2022-01-09 07:14:23 +09:00
faSmileBeam,
faSmile,
faUser,
faPaw,
faIceCream,
faBus,
faBasketballBall,
faLightbulb,
faCode,
faFlag
)
2022-01-09 07:14:23 +09: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',
2022-08-02 00:03:52 +09:00
activities: 'basketball-ball',
objects: 'lightbulb',
symbols: 'code',
flags: 'flag'
2022-01-09 07:14:23 +09:00
}
2022-09-21 10:50:40 +09:00
const maybeLocalizedKeywords = (emoji, languages, nameLocalizer) => {
const res = [emoji.displayText, nameLocalizer(emoji)]
if (emoji.annotations) {
languages.forEach(lang => {
const keywords = emoji.annotations[lang]?.keywords || []
const name = emoji.annotations[lang]?.name
res.push(...(keywords.concat([name]).filter(k => k)))
})
}
return res
}
2022-09-21 10:50:40 +09:00
const filterByKeyword = (list, keyword = '', languages, nameLocalizer) => {
if (keyword === '') return list
2020-09-18 18:07:38 +09:00
const keywordLowercase = keyword.toLowerCase()
2022-07-31 18:35:48 +09:00
const orderedEmojiList = []
for (const emoji of list) {
2022-09-21 10:50:40 +09:00
const indices = maybeLocalizedKeywords(emoji, languages, nameLocalizer)
.map(k => k.toLowerCase().indexOf(keywordLowercase))
.filter(k => k > -1)
const indexOfKeyword = indices.length ? Math.min(...indices) : -1
2020-09-22 00:42:17 +09:00
if (indexOfKeyword > -1) {
2020-09-22 01:13:31 +09:00
if (!Array.isArray(orderedEmojiList[indexOfKeyword])) {
orderedEmojiList[indexOfKeyword] = []
2020-09-22 01:10:55 +09:00
}
2020-09-22 01:13:31 +09:00
orderedEmojiList[indexOfKeyword].push(emoji)
}
}
2020-09-22 01:10:55 +09:00
return orderedEmojiList.flat()
2019-03-30 01:48:52 +09:00
}
2023-01-03 02:40:03 +09:00
const getOffset = (elem) => {
const style = elem.style.transform
const res = /translateY\((\d+)px\)/.exec(style)
if (!res) { return 0 }
return res[1]
}
2023-01-03 03:25:59 +09:00
const toHeaderId = id => {
return id.replace(/^row-\d+-/, '')
}
2019-07-28 19:56:08 +09:00
const EmojiPicker = {
props: {
2019-09-13 02:36:43 +09:00
enableStickerPicker: {
required: false,
type: Boolean,
default: false
},
hideCustomEmoji: {
required: false,
type: Boolean,
default: false
}
},
2019-03-30 00:49:32 +09:00
data () {
return {
keyword: '',
activeGroup: 'custom',
showingStickers: false,
groupsScrolledClass: 'scrolled-top',
2019-10-04 02:16:01 +09:00
keepOpen: false,
customEmojiTimeout: null,
// Lazy-load only after the first time `showing` becomes true.
2022-04-07 10:29:50 +09:00
contentLoaded: false,
groupRefs: {},
emojiRefs: {},
2023-01-03 03:42:09 +09:00
filteredEmojiGroups: [],
width: 0
2019-03-30 00:49:32 +09:00
}
},
components: {
2021-04-25 20:51:00 +09:00
StickerPicker: defineAsyncComponent(() => import('../sticker_picker/sticker_picker.vue')),
Checkbox,
StillImage,
Popover
},
2019-03-30 00:49:32 +09:00
methods: {
showPicker () {
this.$refs.popover.showPopover()
this.onShowing()
},
hidePicker () {
this.$refs.popover.hidePopover()
},
2022-10-10 06:37:59 +09:00
setAnchorEl (el) {
this.$refs.popover.setAnchorEl(el)
},
2022-04-07 10:29:50 +09:00
setGroupRef (name) {
return el => { this.groupRefs[name] = el }
},
onPopoverShown () {
this.$emit('show')
},
onPopoverClosed () {
this.$emit('close')
},
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
2022-10-10 06:33:58 +09:00
if (!this.keepOpen) {
this.$refs.popover.hidePopover()
}
this.$emit('emoji', { insertion: value, keepOpen: this.keepOpen })
},
2022-12-25 03:48:36 +09:00
onScroll (startIndex, endIndex, visibleStartIndex, visibleEndIndex) {
2023-01-03 02:40:03 +09:00
const target = this.$refs['emoji-groups'].$el
2023-01-03 03:25:59 +09:00
this.scrolledGroup(target, visibleStartIndex, visibleEndIndex)
},
2023-01-03 03:25:59 +09:00
scrolledGroup (target, start, end) {
2023-01-03 02:40:03 +09:00
const top = target.scrollTop + 5
this.$nextTick(() => {
2023-01-03 03:25:59 +09:00
this.emojiItems.slice(start, end + 1).forEach(group => {
const headerId = toHeaderId(group.id)
2023-01-03 02:40:03 +09:00
const ref = this.groupRefs['group-' + group.id]
if (!ref) { return }
const elem = ref.$el.parentElement
if (!elem) { return }
if (elem && getOffset(elem) <= top) {
2023-01-03 03:25:59 +09:00
this.activeGroup = headerId
2023-01-03 02:40:03 +09:00
}
})
this.scrollHeader()
})
},
scrollHeader () {
// Scroll the active tab's header into view
2022-04-07 10:29:50 +09:00
const headerRef = this.groupRefs['group-header-' + this.activeGroup]
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)
}
},
2023-01-03 04:01:56 +09:00
highlight (groupId) {
this.setShowStickers(false)
2023-01-03 04:01:56 +09:00
const indexInList = this.emojiItems.findIndex(k => k.id === groupId)
2023-01-03 03:25:59 +09:00
this.$refs['emoji-groups'].scrollToItem(indexInList)
},
updateScrolledClass (target) {
if (target.scrollTop <= 5) {
this.groupsScrolledClass = 'scrolled-top'
} else if (target.scrollTop >= target.scrollTopMax - 5) {
this.groupsScrolledClass = 'scrolled-bottom'
} else {
this.groupsScrolledClass = 'scrolled-middle'
}
},
toggleStickers () {
this.showingStickers = !this.showingStickers
},
setShowStickers (value) {
this.showingStickers = value
},
2021-08-15 13:43:35 +09:00
filterByKeyword (list, keyword) {
2022-09-21 10:50:40 +09:00
return filterByKeyword(list, keyword, this.languages, this.maybeLocalizedEmojiName)
},
onShowing () {
const oldContentLoaded = this.contentLoaded
2023-01-03 03:42:09 +09:00
this.recalculateItemPerRow()
this.$nextTick(() => {
this.$refs.search.focus()
})
this.contentLoaded = true
this.filteredEmojiGroups = this.getFilteredEmojiGroups()
if (!oldContentLoaded) {
this.$nextTick(() => {
if (this.defaultGroup) {
this.highlight(this.defaultGroup)
}
})
}
},
getFilteredEmojiGroups () {
return this.allEmojiGroups
.map(group => ({
...group,
emojis: this.filterByKeyword(group.emojis, trim(this.keyword))
}))
.filter(group => group.emojis.length > 0)
2023-01-03 03:42:09 +09:00
},
recalculateItemPerRow () {
this.$nextTick(() => {
if (!this.$refs['emoji-groups']) {
return
}
2023-01-07 03:14:38 +09:00
this.width = this.$refs['emoji-groups'].$el.clientWidth
2023-01-03 03:42:09 +09:00
})
}
},
watch: {
keyword () {
this.onScroll()
this.debouncedHandleKeywordChange()
},
allCustomGroups () {
this.filteredEmojiGroups = this.getFilteredEmojiGroups()
}
},
2019-03-30 00:49:32 +09:00
computed: {
2022-12-25 03:48:36 +09:00
minItemSize () {
2023-01-03 03:25:59 +09:00
return this.emojiHeight
},
emojiHeight () {
return 32 + 4
},
emojiWidth () {
return 32 + 4
},
itemPerRow () {
2023-01-03 03:42:09 +09:00
return this.width ? Math.floor(this.width / this.emojiWidth - 1) : 6
2022-12-25 03:48:36 +09:00
},
activeGroupView () {
return this.showingStickers ? '' : this.activeGroup
},
stickersAvailable () {
if (this.$store.state.instance.stickers) {
return this.$store.state.instance.stickers.length > 0
}
return 0
},
allCustomGroups () {
if (this.hideCustomEmoji) {
return {}
}
const emojis = this.$store.getters.groupedCustomEmojis
if (emojis.unpacked) {
emojis.unpacked.text = this.$t('emoji.unpacked')
}
return emojis
},
defaultGroup () {
return Object.keys(this.allCustomGroups)[0]
},
unicodeEmojiGroups () {
return this.$store.getters.standardEmojiGroupList.map(group => ({
id: `standard-${group.id}`,
text: this.$t(`emoji.unicode_groups.${group.id}`),
2022-01-09 07:14:23 +09:00
icon: UNICODE_EMOJI_GROUP_ICON[group.id],
emojis: group.emojis
}))
},
allEmojiGroups () {
return Object.entries(this.allCustomGroups)
.map(([_, v]) => v)
.concat(this.unicodeEmojiGroups)
},
2019-09-13 02:36:43 +09:00
stickerPickerEnabled () {
return (this.$store.state.instance.stickers || []).length !== 0
},
debouncedHandleKeywordChange () {
return debounce(() => {
this.filteredEmojiGroups = this.getFilteredEmojiGroups()
}, 500)
2022-09-21 09:44:52 +09:00
},
2023-01-03 03:25:59 +09:00
emojiItems () {
return this.filteredEmojiGroups.map(group =>
chunk(group.emojis, this.itemPerRow)
.map((items, index) => ({
...group,
id: index === 0 ? group.id : `row-${index}-${group.id}`,
emojis: items,
isFirstRow: index === 0
})))
.reduce((a, c) => a.concat(c), [])
},
2022-09-21 09:44:52 +09:00
languages () {
return ensureFinalFallback(this.$store.getters.mergedConfig.interfaceLanguage)
},
maybeLocalizedEmojiName () {
return emoji => {
if (!emoji.annotations) {
return emoji.displayText
}
2022-09-21 10:50:40 +09:00
if (emoji.displayTextI18n) {
return this.$t(emoji.displayTextI18n.key, emoji.displayTextI18n.args)
}
2022-09-21 09:44:52 +09:00
for (const lang of this.languages) {
if (emoji.annotations[lang]?.name) {
return emoji.annotations[lang].name
}
}
return emoji.displayText
}
2019-03-30 00:49:32 +09:00
}
}
}
2019-07-28 19:56:08 +09:00
export default EmojiPicker