2
0
Files
cal/calcom/packages/ui/components/editor/nodes/VariableNode.ts
2024-08-09 00:39:27 +02:00

54 lines
1.2 KiB
TypeScript

import type { EditorConfig, LexicalNode, NodeKey, SerializedTextNode } from "lexical";
import { $applyNodeReplacement, TextNode } from "lexical";
export class VariableNode extends TextNode {
static getType(): string {
return "variable";
}
static clone(node: VariableNode): VariableNode {
return new VariableNode(node.__text, node.__key);
}
constructor(text: string, key?: NodeKey) {
super(text, key);
}
createDOM(config: EditorConfig): HTMLElement {
const dom = super.createDOM(config);
dom.className = "bg-info";
dom.setAttribute("data-lexical-variable", "true");
return dom;
}
exportJSON(): SerializedTextNode {
return {
...super.exportJSON(),
type: "variable",
version: 1,
};
}
isTextEntity(): true {
return true;
}
canInsertTextBefore(): boolean {
return false;
}
canInsertTextAfter(): boolean {
return false;
}
}
export function $createVariableNode(text = ""): VariableNode {
const node = new VariableNode(text);
node.setMode("segmented").toggleDirectionless();
return $applyNodeReplacement(node);
}
export function $isVariableNode(node: LexicalNode | null | undefined): node is VariableNode {
return node instanceof VariableNode;
}