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

69 lines
1.8 KiB
TypeScript
Raw Normal View History

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