From 8babf383d3dd5aca1953b9058dfe169eb6113f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Thu, 6 Feb 2025 15:13:12 -0600 Subject: [PATCH] Fix emoji reactivity bug --- src/components/Reactions.svelte | 6 +++--- src/lib/utils.ts | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/components/Reactions.svelte b/src/components/Reactions.svelte index 95e51f8bd..9ef2ba152 100644 --- a/src/components/Reactions.svelte +++ b/src/components/Reactions.svelte @@ -2,7 +2,7 @@ import type { Author } from "@bindings/cob/Author"; import type { Reaction } from "@bindings/cob/Reaction"; - import { twemoji } from "@app/lib/utils"; + import { emojiToTwemoji } from "@app/lib/utils"; interface Props { reactions: Reaction[]; @@ -44,12 +44,12 @@ await handleReaction(authors, emoji); } }}> - {emoji} + {@html emojiToTwemoji(emoji, ["21a9"])} {authors.length} {:else}
- {emoji} + {@html emojiToTwemoji(emoji, ["21a9"])} {authors.length}
{/if} diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 24bf347d7..10c8616b4 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -102,6 +102,10 @@ export const formatTimestamp = ( return new Date(timestamp).toUTCString(); }; +// Svelte action for replacing emojis within an element with Twemoji SVGs. +// This action is non-reactive; it only runs when the element is mounted. +// +// Usage: 👍 export function twemoji( node: HTMLElement, { exclude }: { exclude: string[] } = { exclude: [] }, @@ -121,6 +125,19 @@ export function twemoji( }); } +// Converts a single emoji character to an tag using Twemoji SVG assets. +// This function is useful in reactive contexts where Svelte actions won't +// re-run automatically. +// +// Usage: {@html emojiToTwemoji("👍")} +export function emojiToTwemoji(emoji: string, exclude?: string[]) { + const filename = emoji.codePointAt(0)?.toString(16); + if (!filename || exclude?.includes(filename)) { + return ""; + } + return `${emoji}`; +} + export function scrollIntoView(id: string, options?: ScrollIntoViewOptions) { const lineElement = document.getElementById(id); if (lineElement) {