chore: Update emoji picker in widget (#14741)
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<script setup>
|
||||
import { ref, computed, onMounted } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { Virtualizer } from 'virtua/vue';
|
||||
|
||||
import {
|
||||
buildEmojiSections,
|
||||
getEmojiTint,
|
||||
getRecentEmojis,
|
||||
addRecentEmoji,
|
||||
} from 'shared/components/emoji/pickerHelper';
|
||||
|
||||
const emit = defineEmits(['select']);
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
const emojiSearch = ref('');
|
||||
const recentEmojis = ref([]);
|
||||
const searchInput = ref(null);
|
||||
|
||||
onMounted(() => {
|
||||
recentEmojis.value = getRecentEmojis();
|
||||
searchInput.value?.focus();
|
||||
});
|
||||
|
||||
const emojiSections = computed(() =>
|
||||
buildEmojiSections(
|
||||
emojiSearch.value,
|
||||
recentEmojis.value,
|
||||
t('EMOJI_ICON_PICKER.FREQUENTLY_USED')
|
||||
)
|
||||
);
|
||||
|
||||
// Tints an emoji button with a light shade of the emoji's own color on hover.
|
||||
const applyEmojiTint = (event, emoji) => {
|
||||
event.currentTarget.style.setProperty('--ep-tint', getEmojiTint(emoji));
|
||||
};
|
||||
|
||||
const selectEmoji = emoji => {
|
||||
recentEmojis.value = addRecentEmoji(emoji);
|
||||
emit('select', { type: 'emoji', value: emoji.emoji, emoji: emoji.emoji });
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
role="dialog"
|
||||
class="absolute z-20 flex flex-col overflow-hidden shadow-xl w-[22rem] bg-n-surface-2 backdrop-blur-[100px] rounded-2xl outline outline-1 outline-n-weak dark:outline-n-strong/50"
|
||||
>
|
||||
<div class="flex flex-col gap-1.5 pt-2">
|
||||
<div class="relative px-2">
|
||||
<span
|
||||
class="absolute z-10 -translate-y-1/2 i-lucide-search size-4 text-n-slate-10 top-1/2 start-5"
|
||||
/>
|
||||
<input
|
||||
ref="searchInput"
|
||||
v-model="emojiSearch"
|
||||
type="text"
|
||||
class="block w-full h-10 text-sm reset-base outline outline-1 outline-offset-[-1px] outline-n-weak focus:outline-n-brand border-none rounded-lg !ps-9 !pe-3 !py-2.5 bg-transparent text-n-slate-12 placeholder:text-n-slate-10"
|
||||
:placeholder="t('EMOJI_ICON_PICKER.SEARCH_EMOJI')"
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
v-if="emojiSections.length"
|
||||
class="h-60 overflow-y-auto px-2 pb-2 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
|
||||
>
|
||||
<Virtualizer v-slot="{ item: section }" :data="emojiSections">
|
||||
<h5
|
||||
class="px-1 pt-2 pb-1 m-0 text-xs font-medium tracking-wide uppercase text-n-slate-10"
|
||||
>
|
||||
{{ section.name }}
|
||||
</h5>
|
||||
<div class="grid grid-cols-10 gap-0.5">
|
||||
<button
|
||||
v-for="emoji in section.emojis"
|
||||
:key="`${section.name}-${emoji.slug}`"
|
||||
type="button"
|
||||
:title="emoji.name"
|
||||
class="flex items-center justify-center !p-0 w-full max-w-[2rem] text-xl transition-colors rounded-lg aspect-square hover:bg-[var(--ep-tint)] active:enabled:scale-[0.97]"
|
||||
@mouseenter="applyEmojiTint($event, emoji.emoji)"
|
||||
@click="selectEmoji(emoji)"
|
||||
>
|
||||
{{ emoji.emoji }}
|
||||
</button>
|
||||
</div>
|
||||
</Virtualizer>
|
||||
</div>
|
||||
<div
|
||||
v-else
|
||||
class="flex flex-col items-center justify-center gap-2 h-60 text-n-slate-10"
|
||||
>
|
||||
<span class="i-lucide-smile-plus size-7" />
|
||||
<span class="text-sm font-medium">
|
||||
{{ t('EMOJI_ICON_PICKER.NO_EMOJI') }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,93 @@
|
||||
import emojiGroups from 'shared/components/emoji/emojisGroup.json';
|
||||
|
||||
// Recently used emojis persisted in localStorage.
|
||||
const RECENT_EMOJI_KEY = 'emoji-icon-picker.recent-emojis';
|
||||
const MAX_RECENT_EMOJIS = 16;
|
||||
|
||||
const matchesSearch = (emoji, term) =>
|
||||
emoji.slug.replaceAll('_', ' ').includes(term) ||
|
||||
emoji.name.toLowerCase().includes(term);
|
||||
|
||||
// Emoji sections for the search term; prepends "Frequently used" when idle.
|
||||
export const buildEmojiSections = (search, recentEmojis, frequentLabel) => {
|
||||
const term = search.trim().toLowerCase();
|
||||
if (term) {
|
||||
return emojiGroups
|
||||
.map(({ name, emojis }) => ({
|
||||
name,
|
||||
emojis: emojis.filter(e => matchesSearch(e, term)),
|
||||
}))
|
||||
.filter(group => group.emojis.length);
|
||||
}
|
||||
return [
|
||||
...(recentEmojis.length
|
||||
? [{ name: frequentLabel, emojis: recentEmojis }]
|
||||
: []),
|
||||
...emojiGroups,
|
||||
];
|
||||
};
|
||||
|
||||
// Samples an emoji's average color (via canvas) as a translucent hover tint. Cached per emoji.
|
||||
const emojiTintCache = new Map();
|
||||
const NEUTRAL_TINT = 'rgb(var(--slate-9) / 0.12)';
|
||||
|
||||
const sampleEmojiTint = emoji => {
|
||||
const canvas = Object.assign(document.createElement('canvas'), {
|
||||
width: 16,
|
||||
height: 16,
|
||||
});
|
||||
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
||||
ctx.font = '14px serif';
|
||||
ctx.textAlign = 'center';
|
||||
ctx.textBaseline = 'middle';
|
||||
ctx.fillText(emoji, 8, 9);
|
||||
|
||||
const { data } = ctx.getImageData(0, 0, 16, 16);
|
||||
const pixels = Array.from(
|
||||
{ length: data.length / 4 },
|
||||
(_, i) => i * 4
|
||||
).filter(i => data[i + 3] > 16);
|
||||
if (!pixels.length) return NEUTRAL_TINT;
|
||||
|
||||
const avg = offset =>
|
||||
Math.round(
|
||||
pixels.reduce((sum, i) => sum + data[i + offset], 0) / pixels.length
|
||||
);
|
||||
|
||||
return `rgba(${avg(0)}, ${avg(1)}, ${avg(2)}, 0.16)`;
|
||||
};
|
||||
|
||||
export const getEmojiTint = emoji => {
|
||||
if (!emojiTintCache.has(emoji)) {
|
||||
let tint = NEUTRAL_TINT;
|
||||
try {
|
||||
tint = sampleEmojiTint(emoji);
|
||||
} catch {
|
||||
/* canvas unavailable */
|
||||
}
|
||||
emojiTintCache.set(emoji, tint);
|
||||
}
|
||||
return emojiTintCache.get(emoji);
|
||||
};
|
||||
|
||||
export const getRecentEmojis = () => {
|
||||
try {
|
||||
const stored = JSON.parse(localStorage.getItem(RECENT_EMOJI_KEY) ?? '[]');
|
||||
return Array.isArray(stored) ? stored : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
export const addRecentEmoji = emoji => {
|
||||
const updated = [
|
||||
emoji,
|
||||
...getRecentEmojis().filter(item => item.slug !== emoji.slug),
|
||||
].slice(0, MAX_RECENT_EMOJIS);
|
||||
try {
|
||||
localStorage.setItem(RECENT_EMOJI_KEY, JSON.stringify(updated));
|
||||
} catch {
|
||||
/* private mode; recents are best-effort */
|
||||
}
|
||||
return updated;
|
||||
};
|
||||
Reference in New Issue
Block a user