@lobehub/editor
Version:
A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.
20 lines • 915 B
JavaScript
import { $wrapNodeInElement } from '@lexical/utils';
import { $createParagraphNode, $insertNodes, $isRootOrShadowRoot, COMMAND_PRIORITY_HIGH, createCommand } from 'lexical';
import { $createMentionNode } from "../node/MentionNode";
export var INSERT_MENTION_COMMAND = createCommand('INSERT_MENTION_COMMAND');
export function registerMentionCommand(editor) {
return editor.registerCommand(INSERT_MENTION_COMMAND, function (payload) {
var metadata = payload.metadata,
label = payload.label;
editor.update(function () {
var mentionNode = $createMentionNode(label, metadata);
$insertNodes([mentionNode]);
// Ensure mention is inside a paragraph when inserted at root
if ($isRootOrShadowRoot(mentionNode.getParentOrThrow())) {
$wrapNodeInElement(mentionNode, $createParagraphNode).selectEnd();
}
});
return true;
}, COMMAND_PRIORITY_HIGH // Priority
);
}