Update emoji.ts

This commit is contained in:
enneaa 2025-04-02 22:10:49 +08:00 committed by GitHub
parent 8aa2ee4a82
commit d0e299a2e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -25,23 +25,14 @@ function toCodePoint(unicodeSurrogates: string) {
return r.join("-") return r.join("-")
} }
type EmojiMap = { const twemoji = (code: string) =>
codePointToName: Record<string, string> `https://cdnjs.cloudflare.com/ajax/libs/twemoji/15.1.0/svg/${code.toLowerCase()}.svg`
nameToBase64: Record<string, string> const emojiCache: Record<string, Promise<any>> = {}
}
export function loadEmoji(code: string) {
let emojimap: EmojiMap | undefined = undefined const type = "twemoji"
export async function loadEmoji(code: string) { const key = type + ":" + code
if (!emojimap) { if (key in emojiCache) return emojiCache[key]
const data = await import("./emojimap.json")
emojimap = data return (emojiCache[key] = fetch(twemoji(code)).then((r) => r.text()))
}
const name = emojimap.codePointToName[`U+${code.toUpperCase()}`]
if (!name) throw new Error(`codepoint ${code} not found in map`)
const b64 = emojimap.nameToBase64[name]
if (!b64) throw new Error(`name ${name} not found in map`)
return b64
} }