watch-party/frontend/lib/emojis.mjs

35 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-02-16 00:42:16 +00:00
export async function emojify(text) {
const emojiList = await emojis;
2022-02-16 00:30:22 +00: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-16 15:48:34 +00:00
let emoji = emojiList.find((e) => e[0] == name);
if (!emoji) {
2022-02-16 00:42:16 +00:00
nodes.push(document.createTextNode(match));
} else {
2022-02-16 15:48:34 +00:00
if (emoji[1][0] !== ":") {
nodes.push(document.createTextNode(emoji[1]));
} else {
nodes.push(
Object.assign(new Image(), {
src: `/emojis/${name}.png`,
className: "emoji",
alt: name,
})
);
}
2022-02-16 00:42:16 +00:00
}
2022-02-16 00:30:22 +00:00
last = index + match.length;
});
if (last < text.length) nodes.push(document.createTextNode(text.slice(last)));
return nodes;
}
2022-02-16 15:30:47 +00:00
export const emojis = Promise.all([
fetch("/emojis")
.then((e) => e.json())
2022-02-16 15:48:34 +00:00
.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));