2023-03-10 15:17:54 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { addClassNamesToElement } from '@lexical/utils';
|
|
|
|
import { $applyNodeReplacement, TextNode } from 'lexical';
|
|
|
|
|
|
|
|
import type {
|
|
|
|
EditorConfig,
|
|
|
|
LexicalNode,
|
|
|
|
NodeKey,
|
|
|
|
SerializedTextNode,
|
|
|
|
} from 'lexical';
|
|
|
|
|
2023-04-01 04:40:15 -07:00
|
|
|
class MentionNode extends TextNode {
|
2023-03-10 15:17:54 -08:00
|
|
|
|
|
|
|
static getType(): string {
|
|
|
|
return 'mention';
|
|
|
|
}
|
|
|
|
|
|
|
|
static clone(node: MentionNode): MentionNode {
|
|
|
|
return new MentionNode(node.__text, node.__key);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(text: string, key?: NodeKey) {
|
|
|
|
super(text, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
createDOM(config: EditorConfig): HTMLElement {
|
|
|
|
const element = super.createDOM(config);
|
|
|
|
addClassNamesToElement(element, config.theme.mention);
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
static importJSON(serializedNode: SerializedTextNode): MentionNode {
|
|
|
|
const node = $createMentionNode(serializedNode.text);
|
|
|
|
node.setFormat(serializedNode.format);
|
|
|
|
node.setDetail(serializedNode.detail);
|
|
|
|
node.setMode(serializedNode.mode);
|
|
|
|
node.setStyle(serializedNode.style);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
exportJSON(): SerializedTextNode {
|
|
|
|
return {
|
|
|
|
...super.exportJSON(),
|
|
|
|
type: 'mention',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
canInsertTextBefore(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
isTextEntity(): true {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-01 04:40:15 -07:00
|
|
|
const $createMentionNode = (text = ''): MentionNode => $applyNodeReplacement(new MentionNode(text));
|
2023-03-10 15:17:54 -08:00
|
|
|
|
2023-04-01 04:40:15 -07:00
|
|
|
const $isMentionNode = (
|
2023-03-10 15:17:54 -08:00
|
|
|
node: LexicalNode | null | undefined,
|
2023-04-01 04:40:15 -07:00
|
|
|
): node is MentionNode => node instanceof MentionNode;
|
|
|
|
|
|
|
|
export { MentionNode, $createMentionNode, $isMentionNode };
|