only emojify actual emojis

main
easrng 2022-02-15 19:42:16 -05:00
parent e6699e05dd
commit e9a1b762e7
2 changed files with 15 additions and 10 deletions

View File

@ -286,7 +286,7 @@ const formatTime = (ms) => {
}:${seconds < 10 ? "0" + seconds : seconds}`;
};
export const logEventToChat = (event) => {
export const logEventToChat = async (event) => {
if (checkDebounce(event)) {
return;
}
@ -313,7 +313,7 @@ export const logEventToChat = (event) => {
case "ChatMessage": {
const messageContent = document.createElement("span");
messageContent.classList.add("message-content");
messageContent.append(...emojify(event.data));
messageContent.append(...(await emojify(event.data)));
printChatMessage(
"chat-message",
event.user,

View File

@ -1,16 +1,21 @@
export function emojify(text) {
export async function emojify(text) {
const emojiList = await emojis;
let last = 0;
let nodes = [];
text.replace(/:([^\s:]+):/g, (match, name, index) => {
if (last <= index)
nodes.push(document.createTextNode(text.slice(last, index)));
nodes.push(
Object.assign(new Image(), {
src: `/emojis/${name}.png`,
className: "emoji",
alt: name,
})
);
if (!emojiList.includes(name)) {
nodes.push(document.createTextNode(match));
} else {
nodes.push(
Object.assign(new Image(), {
src: `/emojis/${name}.png`,
className: "emoji",
alt: name,
})
);
}
last = index + match.length;
});
if (last < text.length) nodes.push(document.createTextNode(text.slice(last)));