CustomParagraphNode.ts (1555B)
1 import { $applyNodeReplacement, $getRoot, DOMExportOutput, LexicalEditor, ParagraphNode, SerializedElementNode, SerializedParagraphNode } from "lexical"; 2 3 export class CustomParagraphNode extends ParagraphNode { 4 static getType(): string { 5 return "custom-paragraph"; 6 } 7 8 static clone(node: CustomParagraphNode): CustomParagraphNode { 9 return new CustomParagraphNode(node.__key); 10 } 11 12 exportDOM(editor: LexicalEditor): DOMExportOutput { 13 const { element } = super.exportDOM(editor); 14 15 if (this.isEmpty()) { 16 const root = $getRoot(); 17 let foundFurtherText = false; 18 for (const child of root.getChildren()) { 19 if (child === this) { 20 continue; 21 } 22 if (child instanceof CustomParagraphNode || child instanceof ParagraphNode) { 23 if (!child.isEmpty()) { 24 foundFurtherText = true; 25 } 26 } 27 } 28 if (!foundFurtherText) { 29 return { element: null }; 30 } 31 } 32 33 return { element }; 34 } 35 36 exportJSON(): SerializedElementNode { 37 const node = super.exportJSON(); 38 node.type = "custom-paragraph"; 39 return node; 40 } 41 42 static importJSON(node: SerializedParagraphNode): CustomParagraphNode { 43 return super.importJSON(node); 44 } 45 } 46 47 export function $createCustomParagraphNode(): CustomParagraphNode { 48 return $applyNodeReplacement(new CustomParagraphNode()); 49 }