6 ParagraphNode, SerializedParagraphNode, Spread,
8 import {EditorConfig} from "lexical/LexicalEditor";
10 CommonBlockAlignment, commonPropertiesDifferent,
11 SerializedCommonBlockNode,
12 setCommonBlockPropsFromElement,
13 updateElementWithCommonBlockProps
16 export type SerializedCustomParagraphNode = Spread<SerializedCommonBlockNode, SerializedParagraphNode>
18 export class CustomParagraphNode extends ParagraphNode {
20 __alignment: CommonBlockAlignment = '';
23 return 'custom-paragraph';
27 const self = this.getWritable();
32 const self = this.getLatest();
36 setAlignment(alignment: CommonBlockAlignment) {
37 const self = this.getWritable();
38 self.__alignment = alignment;
41 getAlignment(): CommonBlockAlignment {
42 const self = this.getLatest();
43 return self.__alignment;
46 static clone(node: CustomParagraphNode): CustomParagraphNode {
47 const newNode = new CustomParagraphNode(node.__key);
48 newNode.__id = node.__id;
49 newNode.__alignment = node.__alignment;
53 createDOM(config: EditorConfig): HTMLElement {
54 const dom = super.createDOM(config);
55 updateElementWithCommonBlockProps(dom, this);
59 updateDOM(prevNode: CustomParagraphNode, dom: HTMLElement, config: EditorConfig): boolean {
60 return super.updateDOM(prevNode, dom, config)
61 || commonPropertiesDifferent(prevNode, this);
64 exportJSON(): SerializedCustomParagraphNode {
66 ...super.exportJSON(),
67 type: 'custom-paragraph',
70 alignment: this.__alignment,
74 static importJSON(serializedNode: SerializedCustomParagraphNode): CustomParagraphNode {
75 const node = $createCustomParagraphNode();
76 node.setId(serializedNode.id);
77 node.setAlignment(serializedNode.alignment);
81 static importDOM(): DOMConversionMap|null {
83 p(node: HTMLElement): DOMConversion|null {
85 conversion: (element: HTMLElement): DOMConversionOutput|null => {
86 const node = $createCustomParagraphNode();
87 if (element.style.textIndent) {
88 const indent = parseInt(element.style.textIndent, 10) / 20;
90 node.setIndent(indent);
94 setCommonBlockPropsFromElement(element, node);
105 export function $createCustomParagraphNode(): CustomParagraphNode {
106 return new CustomParagraphNode();
109 export function $isCustomParagraphNode(node: LexicalNode | null | undefined): node is CustomParagraphNode {
110 return node instanceof CustomParagraphNode;