Fix emoji reactivity bug

This commit is contained in:
Rūdolfs Ošiņš
2025-02-07 09:30:47 +01:00
committed by Sebastian Martinez
parent f08255a194
commit 8babf383d3
2 changed files with 20 additions and 3 deletions
+3 -3
View File
@@ -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);
}
}}>
<span use:twemoji={{ exclude: ["21a9"] }}>{emoji}</span>
<span>{@html emojiToTwemoji(emoji, ["21a9"])}</span>
<span>{authors.length}</span>
</div>
{:else}
<div class="reaction txt-tiny" style="padding: 2px 4px;">
<span use:twemoji={{ exclude: ["21a9"] }}>{emoji}</span>
<span>{@html emojiToTwemoji(emoji, ["21a9"])}</span>
<span>{authors.length}</span>
</div>
{/if}
+17
View File
@@ -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: <span use:twemoji>👍</span>
export function twemoji(
node: HTMLElement,
{ exclude }: { exclude: string[] } = { exclude: [] },
@@ -121,6 +125,19 @@ export function twemoji(
});
}
// Converts a single emoji character to an <img> tag using Twemoji SVG assets.
// This function is useful in reactive contexts where Svelte actions won't
// re-run automatically.
//
// Usage: <span>{@html emojiToTwemoji("👍")}</span>
export function emojiToTwemoji(emoji: string, exclude?: string[]) {
const filename = emoji.codePointAt(0)?.toString(16);
if (!filename || exclude?.includes(filename)) {
return "";
}
return `<img alt="${emoji}" src="/twemoji/${filename}.svg" class="txt-emoji">`;
}
export function scrollIntoView(id: string, options?: ScrollIntoViewOptions) {
const lineElement = document.getElementById(id);
if (lineElement) {