97 lines
3.0 KiB
TypeScript
97 lines
3.0 KiB
TypeScript
// Emoji support for watch chat and reactions (item 16). A curated Unicode set
|
|
// across categories (dependency-free — no multi-MB emoji-data bundle), plus
|
|
// admin-managed custom image emojis served at /emoji/<name> and referenced by
|
|
// :name: shortcodes. Recently-used emoji are remembered locally.
|
|
|
|
import { Store } from './store';
|
|
import { rpc } from './rpc';
|
|
|
|
export interface CustomEmoji {
|
|
name: string;
|
|
mime: string;
|
|
}
|
|
|
|
export const emojiStore = new Store<{
|
|
custom: CustomEmoji[];
|
|
recent: string[]; // unicode chars or :shortcode:
|
|
}>({
|
|
custom: [],
|
|
recent: JSON.parse(localStorage.getItem('echoion.recentEmoji') || '[]'),
|
|
});
|
|
|
|
export async function loadCustomEmojis() {
|
|
try {
|
|
const res = await rpc.call<{ emojis?: CustomEmoji[] }>(
|
|
'GetCustomEmojis'
|
|
);
|
|
emojiStore.set({ custom: (res && res.emojis) || [] });
|
|
} catch {
|
|
/* not connected / not authed — leave empty */
|
|
}
|
|
}
|
|
|
|
export function rememberEmoji(token: string) {
|
|
const recent = [
|
|
token,
|
|
...emojiStore.get().recent.filter((r) => r !== token),
|
|
].slice(0, 24);
|
|
emojiStore.set({ recent });
|
|
localStorage.setItem('echoion.recentEmoji', JSON.stringify(recent));
|
|
}
|
|
|
|
// Curated categories of common Unicode emoji.
|
|
export const EMOJI_CATEGORIES: Array<{ label: string; emoji: string[] }> = [
|
|
{
|
|
label: 'Smileys',
|
|
emoji: [
|
|
'😀', '😁', '😂', '🤣', '😊', '😇', '🙂', '😉', '😍', '🥰',
|
|
'😘', '😜', '🤪', '😎', '🤩', '🥳', '😏', '😒', '😔', '😞',
|
|
'😢', '😭', '😤', '😠', '😡', '🤬', '🤯', '😳', '🥵', '🥶',
|
|
'😱', '😨', '🤗', '🤔', '🤫', '🤥', '😴', '🤤', '😷', '🤒',
|
|
],
|
|
},
|
|
{
|
|
label: 'Gestures',
|
|
emoji: [
|
|
'👍', '👎', '👌', '✌️', '🤞', '🤟', '🤘', '👊', '✊', '🙌',
|
|
'👏', '🙏', '💪', '👀', '👋', '🤙', '💅', '🖖', '🫶', '🤝',
|
|
],
|
|
},
|
|
{
|
|
label: 'Hearts',
|
|
emoji: [
|
|
'❤️', '🧡', '💛', '💚', '💙', '💜', '🖤', '🤍', '💔', '❣️',
|
|
'💕', '💞', '💓', '💗', '💖', '💘', '💝', '✨', '💯', '🔥',
|
|
],
|
|
},
|
|
{
|
|
label: 'Fun',
|
|
emoji: [
|
|
'🎉', '🎊', '🥂', '🍻', '🍕', '🍿', '🎮', '🎧', '🎵', '🎸',
|
|
'⚡', '🌟', '💥', '👑', '🚀', '🏆', '🎯', '💎', '🌈', '☀️',
|
|
],
|
|
},
|
|
];
|
|
|
|
// Render a chat message: plain text with :name: custom-emoji shortcodes
|
|
// replaced by <img> tags. Returns React nodes (text is auto-escaped by React).
|
|
export function renderChatText(text: string): (string | EmojiToken)[] {
|
|
const out: (string | EmojiToken)[] = [];
|
|
const re = /:([a-z0-9_]{1,32}):/g;
|
|
const custom = new Set(emojiStore.get().custom.map((c) => c.name));
|
|
let last = 0;
|
|
let m: RegExpExecArray | null;
|
|
while ((m = re.exec(text)) !== null) {
|
|
if (!custom.has(m[1])) continue;
|
|
if (m.index > last) out.push(text.slice(last, m.index));
|
|
out.push({ emojiName: m[1] });
|
|
last = m.index + m[0].length;
|
|
}
|
|
if (last < text.length) out.push(text.slice(last));
|
|
return out;
|
|
}
|
|
|
|
export interface EmojiToken {
|
|
emojiName: string;
|
|
}
|