only emojify actual emojis

votekiss
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,9 +1,13 @@
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)));
if (!emojiList.includes(name)) {
nodes.push(document.createTextNode(match));
} else {
nodes.push( nodes.push(
Object.assign(new Image(), { Object.assign(new Image(), {
src: `/emojis/${name}.png`, src: `/emojis/${name}.png`,
@ -11,6 +15,7 @@ export function emojify(text) {
alt: name, 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)));