7 import {EditorConfig} from "lexical/LexicalEditor";
8 import {HeadingNode, HeadingTagType, SerializedHeadingNode} from "@lexical/rich-text";
10 CommonBlockAlignment, commonPropertiesDifferent,
11 SerializedCommonBlockNode,
12 setCommonBlockPropsFromElement,
13 updateElementWithCommonBlockProps
17 export type SerializedCustomHeadingNode = Spread<SerializedCommonBlockNode, SerializedHeadingNode>
19 export class CustomHeadingNode extends HeadingNode {
21 __alignment: CommonBlockAlignment = '';
24 return 'custom-heading';
28 const self = this.getWritable();
33 const self = this.getLatest();
37 setAlignment(alignment: CommonBlockAlignment) {
38 const self = this.getWritable();
39 self.__alignment = alignment;
42 getAlignment(): CommonBlockAlignment {
43 const self = this.getLatest();
44 return self.__alignment;
47 static clone(node: CustomHeadingNode) {
48 const newNode = new CustomHeadingNode(node.__tag, node.__key);
49 newNode.__alignment = node.__alignment;
53 createDOM(config: EditorConfig): HTMLElement {
54 const dom = super.createDOM(config);
55 updateElementWithCommonBlockProps(dom, this);
59 updateDOM(prevNode: CustomHeadingNode, dom: HTMLElement): boolean {
60 return super.updateDOM(prevNode, dom)
61 || commonPropertiesDifferent(prevNode, this);
64 exportJSON(): SerializedCustomHeadingNode {
66 ...super.exportJSON(),
67 type: 'custom-heading',
70 alignment: this.__alignment,
74 static importJSON(serializedNode: SerializedCustomHeadingNode): CustomHeadingNode {
75 const node = $createCustomHeadingNode(serializedNode.tag);
76 node.setId(serializedNode.id);
77 node.setAlignment(serializedNode.alignment);
81 static importDOM(): DOMConversionMap | null {
83 h1: (node: Node) => ({
84 conversion: $convertHeadingElement,
87 h2: (node: Node) => ({
88 conversion: $convertHeadingElement,
91 h3: (node: Node) => ({
92 conversion: $convertHeadingElement,
95 h4: (node: Node) => ({
96 conversion: $convertHeadingElement,
99 h5: (node: Node) => ({
100 conversion: $convertHeadingElement,
103 h6: (node: Node) => ({
104 conversion: $convertHeadingElement,
111 function $convertHeadingElement(element: HTMLElement): DOMConversionOutput {
112 const nodeName = element.nodeName.toLowerCase();
122 node = $createCustomHeadingNode(nodeName);
123 setCommonBlockPropsFromElement(element, node);
128 export function $createCustomHeadingNode(tag: HeadingTagType) {
129 return new CustomHeadingNode(tag);
132 export function $isCustomHeadingNode(node: LexicalNode | null | undefined): node is CustomHeadingNode {
133 return node instanceof CustomHeadingNode;