watch-party/frontend/lib/emojis.mjs

30 lines
928 B
JavaScript
Raw Normal View History

2022-02-15 16:42:16 -08:00
export async function emojify(text) {
const emojiList = await emojis;
2022-02-15 16:30:22 -08:00
let last = 0;
let nodes = [];
text.replace(/:([^\s:]+):/g, (match, name, index) => {
if (last <= index)
nodes.push(document.createTextNode(text.slice(last, index)));
2022-02-15 16:42:16 -08:00
if (!emojiList.includes(name)) {
nodes.push(document.createTextNode(match));
} else {
nodes.push(
Object.assign(new Image(), {
src: `/emojis/${name}.png`,
className: "emoji",
alt: name,
})
);
}
2022-02-15 16:30:22 -08:00
last = index + match.length;
});
if (last < text.length) nodes.push(document.createTextNode(text.slice(last)));
return nodes;
}
2022-02-16 07:30:47 -08:00
export const emojis = Promise.all([
fetch("/emojis")
.then((e) => e.json())
.then((e) => e.map((e) => [e.slice(0, -4), ":"+e.slice(0, -4)+":"])),
fetch('/emojis/unicode.json')
.then((e) => e.json())
]).then(e=>e.flat(1));