import i18n from '@/locales/config'; import { BeginId } from '@/pages/flow/constant'; import { DecoratorNode, LexicalNode, NodeKey } from 'lexical'; import { ReactNode } from 'react'; const prefix = BeginId + '@'; export class VariableNode extends DecoratorNode { __value: string; __label: string; static getType(): string { return 'variable'; } static clone(node: VariableNode): VariableNode { return new VariableNode(node.__value, node.__label, node.__key); } constructor(value: string, label: string, key?: NodeKey) { super(key); this.__value = value; this.__label = label; } createDOM(): HTMLElement { const dom = document.createElement('span'); dom.className = 'mr-1'; return dom; } updateDOM(): false { return false; } decorate(): ReactNode { let content: ReactNode = ( {this.__label} ); if (this.__value.startsWith(prefix)) { content = (
{i18n.t(`flow.begin`)} / {content}
); } return (
{content}
); } getTextContent(): string { return `{${this.__value}}`; } } export function $createVariableNode( value: string, label: string, ): VariableNode { return new VariableNode(value, label); } export function $isVariableNode( node: LexicalNode | null | undefined, ): node is VariableNode { return node instanceof VariableNode; }