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

61 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-01-03 11:55:46 -08:00
// Copyright 2020 Signal Messenger, LLC
2020-11-02 17:19:52 -08:00
// SPDX-License-Identifier: AGPL-3.0-only
import React from 'react';
2025-01-28 07:31:42 -10:00
import EmbedBlot from '@signalapp/quill-cjs/blots/embed';
2020-11-02 17:19:52 -08:00
import { render } from 'react-dom';
2025-01-28 07:31:42 -10:00
2020-11-02 17:19:52 -08:00
import { Emojify } from '../../components/conversation/Emojify';
2023-09-14 13:04:48 -04:00
import { normalizeAci } from '../../util/normalizeAci';
import type { MentionBlotValue } from '../util';
2020-11-02 17:19:52 -08:00
2025-01-28 07:31:42 -10:00
export class MentionBlot extends EmbedBlot {
static override blotName = 'mention';
2020-11-02 17:19:52 -08:00
static override className = 'mention-blot';
2020-11-02 17:19:52 -08:00
static override tagName = 'span';
2020-11-02 17:19:52 -08:00
static override create(value: MentionBlotValue): Node {
2020-11-02 17:19:52 -08:00
const node = super.create(undefined) as HTMLElement;
MentionBlot.buildSpan(value, node);
return node;
}
static override value(node: HTMLElement): MentionBlotValue {
2023-08-16 22:54:39 +02:00
const { aci, title } = node.dataset;
if (aci === undefined || title === undefined) {
throw new Error(
2023-08-16 22:54:39 +02:00
`Failed to make MentionBlot with aci: ${aci}, title: ${title}`
);
}
2020-11-02 17:19:52 -08:00
return {
2023-08-16 22:54:39 +02:00
aci: normalizeAci(aci, 'quill mention blot'),
2020-11-02 17:19:52 -08:00
title,
};
}
static buildSpan(mention: MentionBlotValue, node: HTMLElement): void {
2023-08-16 22:54:39 +02:00
node.setAttribute('data-aci', mention.aci || '');
node.setAttribute('data-title', mention.title || '');
node.setAttribute('contenteditable', 'false');
2020-11-02 17:19:52 -08:00
const mentionSpan = document.createElement('span');
render(
<span className="module-composition-input__at-mention">
<bdi>
@
<Emojify text={mention.title} />
2020-11-02 17:19:52 -08:00
</bdi>
</span>,
mentionSpan
);
node.appendChild(mentionSpan);
}
}