only emojify actual emojis

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}`; }:${seconds < 10 ? "0" + seconds : seconds}`;
}; };
export const logEventToChat = (event) => { export const logEventToChat = async (event) => {
if (checkDebounce(event)) { if (checkDebounce(event)) {
return; return;
} }
@ -313,7 +313,7 @@ export const logEventToChat = (event) => {
case "ChatMessage": { case "ChatMessage": {
const messageContent = document.createElement("span"); const messageContent = document.createElement("span");
messageContent.classList.add("message-content"); messageContent.classList.add("message-content");
messageContent.append(...emojify(event.data)); messageContent.append(...(await emojify(event.data)));
printChatMessage( printChatMessage(
"chat-message", "chat-message",
event.user, 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 last = 0;
let nodes = []; let nodes = [];
text.replace(/:([^\s:]+):/g, (match, name, index) => { text.replace(/:([^\s:]+):/g, (match, name, index) => {
if (last <= index) if (last <= index)
nodes.push(document.createTextNode(text.slice(last, index))); nodes.push(document.createTextNode(text.slice(last, index)));
nodes.push( if (!emojiList.includes(name)) {
Object.assign(new Image(), { nodes.push(document.createTextNode(match));
src: `/emojis/${name}.png`, } else {
className: "emoji", nodes.push(
alt: name, Object.assign(new Image(), {
}) src: `/emojis/${name}.png`,
); className: "emoji",
alt: name,
})
);
}
last = index + match.length; last = index + match.length;
}); });
if (last < text.length) nodes.push(document.createTextNode(text.slice(last))); if (last < text.length) nodes.push(document.createTextNode(text.slice(last)));