2020-11-03 01:19:52 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import Parchment from 'parchment';
|
|
|
|
import Quill from 'quill';
|
|
|
|
import { render } from 'react-dom';
|
|
|
|
import { Emojify } from '../../components/conversation/Emojify';
|
2020-11-05 21:18:42 +00:00
|
|
|
import { MentionBlotValue } from '../util';
|
2020-11-03 01:19:52 +00:00
|
|
|
|
|
|
|
const Embed: typeof Parchment.Embed = Quill.import('blots/embed');
|
|
|
|
|
|
|
|
export class MentionBlot extends Embed {
|
|
|
|
static blotName = 'mention';
|
|
|
|
|
|
|
|
static className = 'mention-blot';
|
|
|
|
|
|
|
|
static tagName = 'span';
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
static 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 value(node: HTMLElement): MentionBlotValue {
|
|
|
|
const { uuid, title } = node.dataset;
|
2020-11-05 21:18:42 +00:00
|
|
|
if (uuid === undefined || title === undefined) {
|
|
|
|
throw new Error(
|
|
|
|
`Failed to make MentionBlot with uuid: ${uuid} and title: ${title}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:19:52 +00:00
|
|
|
return {
|
|
|
|
uuid,
|
|
|
|
title,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
static buildSpan(mention: MentionBlotValue, node: HTMLElement): void {
|
|
|
|
node.setAttribute('data-uuid', mention.uuid || '');
|
|
|
|
node.setAttribute('data-title', mention.title || '');
|
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);
|
|
|
|
}
|
|
|
|
}
|