UNPKG

@yuntijs/ui

Version:

☁️ Yunti UI - an open-source UI component library for building Cloud Native web apps

32 lines 1.57 kB
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; import { mergeRegister } from '@lexical/utils'; import { $insertNodes, COMMAND_PRIORITY_EDITOR } from 'lexical'; import { memo, useEffect } from 'react'; import { $createMentionNode, MentionNode } from "./node"; import { CLEAR_HIDE_MENU_TIMEOUT, DELETE_MENTION_COMMAND, INSERT_MENTION_COMMAND } from "./utils"; export * from "./node"; export * from "./replacement"; export * from "./utils"; export var MentionNodePlugin = /*#__PURE__*/memo(function (_ref) { var onInsert = _ref.onInsert, onDelete = _ref.onDelete; var _useLexicalComposerCo = useLexicalComposerContext(), _useLexicalComposerCo2 = _slicedToArray(_useLexicalComposerCo, 1), editor = _useLexicalComposerCo2[0]; useEffect(function () { if (!editor.hasNodes([MentionNode])) throw new Error('MentionsNodePlugin: MentionNode not registered on editor'); return mergeRegister(editor.registerCommand(INSERT_MENTION_COMMAND, function (variable) { editor.dispatchCommand(CLEAR_HIDE_MENU_TIMEOUT, null); var mentionNode = $createMentionNode(variable); $insertNodes([mentionNode]); if (onInsert) onInsert(); return true; }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(DELETE_MENTION_COMMAND, function () { if (onDelete) onDelete(); return true; }, COMMAND_PRIORITY_EDITOR)); }, [editor, onInsert, onDelete]); return null; }); MentionNodePlugin.displayName = 'MentionNodePlugin';