signal-desktop/ts/quill/emoji/blot.tsx

54 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2020 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import type Parchment from 'parchment';
import Quill from 'quill';
import { emojiToImage } from '../../components/emoji/lib';
const Embed: typeof Parchment.Embed = Quill.import('blots/embed');
// the DOM structure of this EmojiBlot should match the other emoji implementations:
// ts/components/conversation/Emojify.tsx
// ts/components/emoji/Emoji.tsx
2023-12-18 23:22:46 +00:00
export type EmojiBlotValue = Readonly<{
value: string;
source?: string;
}>;
export class EmojiBlot extends Embed {
static override blotName = 'emoji';
static override tagName = 'img';
static override className = 'emoji-blot';
2023-12-18 23:22:46 +00:00
static override create({ value: emoji, source }: EmojiBlotValue): Node {
const node = super.create(undefined) as HTMLElement;
node.dataset.emoji = emoji;
2023-12-18 23:22:46 +00:00
node.dataset.source = source;
const image = emojiToImage(emoji);
node.setAttribute('src', image || '');
node.setAttribute('data-emoji', emoji);
2023-12-18 23:22:46 +00:00
node.setAttribute('data-source', source || '');
node.setAttribute('title', emoji);
node.setAttribute('aria-label', emoji);
return node;
}
2023-12-18 23:22:46 +00:00
static override value(node: HTMLElement): EmojiBlotValue | undefined {
const { emoji, source } = node.dataset;
if (emoji === undefined) {
throw new Error(
`Failed to make EmojiBlot with emoji: ${emoji}, source: ${source}`
);
}
return { value: emoji, source };
}
}