2023-01-16 13:22:26 +03:00
|
|
|
<script setup lang="ts">
|
|
|
|
import { getEmojiMatchesInText } from '@iconify/utils/lib/emoji/replace/find'
|
2023-01-16 13:21:38 +03:00
|
|
|
import { emojiFilename, emojiPrefix, emojiRegEx } from '~~/config/emojis'
|
2023-08-02 13:28:18 +03:00
|
|
|
import type { CommandHandler } from '~/composables/command'
|
2024-09-30 11:11:56 +03:00
|
|
|
import type { CustomEmoji, Emoji } from '~/composables/tiptap/suggestion'
|
|
|
|
import { isCustomEmoji } from '~/composables/tiptap/suggestion'
|
2023-01-16 13:22:26 +03:00
|
|
|
|
|
|
|
const { items, command } = defineProps<{
|
|
|
|
items: (CustomEmoji | Emoji)[]
|
2023-08-02 13:28:18 +03:00
|
|
|
command: CommandHandler<any>
|
2023-01-16 13:22:26 +03:00
|
|
|
isPending?: boolean
|
|
|
|
}>()
|
|
|
|
|
|
|
|
const emojis = computed(() => {
|
2024-02-24 19:46:14 +03:00
|
|
|
if (import.meta.server)
|
2023-02-06 12:34:50 +03:00
|
|
|
return []
|
|
|
|
|
2023-01-16 13:22:26 +03:00
|
|
|
return items.map((item: CustomEmoji | Emoji) => {
|
|
|
|
if (isCustomEmoji(item)) {
|
|
|
|
return {
|
|
|
|
title: item.shortcode,
|
|
|
|
src: item.url,
|
|
|
|
emoji: item,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const skin = item.skins.find(skin => skin.native !== undefined)
|
|
|
|
const match = getEmojiMatchesInText(emojiRegEx, skin!.native)[0]
|
|
|
|
const file = emojiFilename(match)
|
|
|
|
|
|
|
|
return {
|
|
|
|
title: item.id,
|
|
|
|
src: `/emojis/${emojiPrefix}/${file.filename}`,
|
|
|
|
emoji: item,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const selectedIndex = ref(0)
|
2023-01-16 13:22:26 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
watch(() => items, () => {
|
|
|
|
selectedIndex.value = 0
|
2023-01-16 13:22:26 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
function onKeyDown(event: KeyboardEvent) {
|
2023-02-03 17:21:29 +03:00
|
|
|
if (items.length === 0)
|
|
|
|
return false
|
|
|
|
|
2023-01-16 13:22:26 +03:00
|
|
|
if (event.key === 'ArrowUp') {
|
2024-02-21 18:20:08 +03:00
|
|
|
selectedIndex.value = ((selectedIndex.value + items.length) - 1) % items.length
|
2023-01-16 13:22:26 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
else if (event.key === 'ArrowDown') {
|
2024-02-21 18:20:08 +03:00
|
|
|
selectedIndex.value = (selectedIndex.value + 1) % items.length
|
2023-01-16 13:22:26 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
else if (event.key === 'Enter') {
|
2024-02-21 18:20:08 +03:00
|
|
|
selectItem(selectedIndex.value)
|
2023-01-16 13:22:26 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectItem(index: number) {
|
|
|
|
const emoji = emojis.value[index]
|
|
|
|
if (emoji)
|
|
|
|
command(emoji)
|
|
|
|
}
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
onKeyDown,
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2023-01-16 13:21:38 +03:00
|
|
|
<div
|
|
|
|
v-if="isPending || items.length"
|
|
|
|
relative bg-base text-base shadow border="~ base rounded"
|
|
|
|
text-sm py-2 overflow-x-hidden overflow-y-auto max-h-100
|
|
|
|
min-w-40 max-w-50
|
|
|
|
>
|
2023-01-16 13:22:26 +03:00
|
|
|
<template v-if="isPending">
|
|
|
|
<div flex gap-1 items-center p2 animate-pulse>
|
|
|
|
<div i-ri:loader-2-line animate-spin />
|
2023-02-15 22:15:11 +03:00
|
|
|
<span>{{ $t('common.fetching') }}</span>
|
2023-01-16 13:22:26 +03:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<template v-if="items.length">
|
|
|
|
<CommonScrollIntoView
|
|
|
|
v-for="(item, index) in emojis" :key="index"
|
|
|
|
:active="index === selectedIndex"
|
|
|
|
as="button"
|
|
|
|
:class="index === selectedIndex ? 'bg-active' : 'text-secondary'"
|
|
|
|
block m0 w-full text-left px2 py1
|
|
|
|
@click="selectItem(index)"
|
|
|
|
>
|
|
|
|
<SearchEmojiInfo :emoji="item" />
|
|
|
|
</CommonScrollIntoView>
|
|
|
|
</template>
|
|
|
|
</div>
|
|
|
|
<div v-else />
|
|
|
|
</template>
|