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
|
|
|
|
|
2020-11-10 17:51:24 +00:00
|
|
|
/* 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-08-10 16:43:33 +00:00
|
|
|
import { normalizeAci } from '../../types/ServiceId';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MentionBlotValue } from '../util';
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-10 17:51:24 +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 {
|
2021-11-12 23:44:20 +00:00
|
|
|
static override blotName = 'mention';
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2021-11-12 23:44:20 +00:00
|
|
|
static override className = 'mention-blot';
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2021-11-12 23:44:20 +00:00
|
|
|
static override tagName = 'span';
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2021-11-12 23:44:20 +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;
|
|
|
|
}
|
|
|
|
|
2021-11-12 23:44:20 +00:00
|
|
|
static override value(node: HTMLElement): MentionBlotValue {
|
2023-08-16 20:54:39 +00:00
|
|
|
const { aci, title } = node.dataset;
|
|
|
|
if (aci === undefined || title === undefined) {
|
2020-11-05 21:18:42 +00:00
|
|
|
throw new Error(
|
2023-08-16 20:54:39 +00:00
|
|
|
`Failed to make MentionBlot with aci: ${aci}, title: ${title}`
|
2020-11-05 21:18:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
static buildSpan(mention: MentionBlotValue, node: HTMLElement): void {
|
2023-08-16 20:54:39 +00:00
|
|
|
node.setAttribute('data-aci', mention.aci || '');
|
2020-11-05 21:18:42 +00:00
|
|
|
node.setAttribute('data-title', mention.title || '');
|
2021-09-01 20:42:12 +00:00
|
|
|
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>
|
|
|
|
@
|
2020-11-05 21:18:42 +00:00
|
|
|
<Emojify text={mention.title} />
|
2020-11-03 01:19:52 +00:00
|
|
|
</bdi>
|
|
|
|
</span>,
|
|
|
|
mentionSpan
|
|
|
|
);
|
|
|
|
|
|
|
|
node.appendChild(mentionSpan);
|
|
|
|
}
|
|
|
|
}
|