7 import {EditorConfig} from "lexical/LexicalEditor";
8 import {HeadingNode, HeadingTagType, SerializedHeadingNode} from "@lexical/rich-text";
10 CommonBlockAlignment, commonPropertiesDifferent, deserializeCommonBlockNode,
11 SerializedCommonBlockNode,
12 setCommonBlockPropsFromElement,
13 updateElementWithCommonBlockProps
17 export type SerializedCustomHeadingNode = Spread<SerializedCommonBlockNode, SerializedHeadingNode>
19 export class CustomHeadingNode extends HeadingNode {
21 __alignment: CommonBlockAlignment = '';
25 return 'custom-heading';
29 const self = this.getWritable();
34 const self = this.getLatest();
38 setAlignment(alignment: CommonBlockAlignment) {
39 const self = this.getWritable();
40 self.__alignment = alignment;
43 getAlignment(): CommonBlockAlignment {
44 const self = this.getLatest();
45 return self.__alignment;
48 setInset(size: number) {
49 const self = this.getWritable();
54 const self = this.getLatest();
58 static clone(node: CustomHeadingNode) {
59 const newNode = new CustomHeadingNode(node.__tag, node.__key);
60 newNode.__alignment = node.__alignment;
61 newNode.__inset = node.__inset;
65 createDOM(config: EditorConfig): HTMLElement {
66 const dom = super.createDOM(config);
67 updateElementWithCommonBlockProps(dom, this);
71 updateDOM(prevNode: CustomHeadingNode, dom: HTMLElement): boolean {
72 return super.updateDOM(prevNode, dom)
73 || commonPropertiesDifferent(prevNode, this);
76 exportJSON(): SerializedCustomHeadingNode {
78 ...super.exportJSON(),
79 type: 'custom-heading',
82 alignment: this.__alignment,
87 static importJSON(serializedNode: SerializedCustomHeadingNode): CustomHeadingNode {
88 const node = $createCustomHeadingNode(serializedNode.tag);
89 deserializeCommonBlockNode(serializedNode, node);
93 static importDOM(): DOMConversionMap | null {
95 h1: (node: Node) => ({
96 conversion: $convertHeadingElement,
99 h2: (node: Node) => ({
100 conversion: $convertHeadingElement,
103 h3: (node: Node) => ({
104 conversion: $convertHeadingElement,
107 h4: (node: Node) => ({
108 conversion: $convertHeadingElement,
111 h5: (node: Node) => ({
112 conversion: $convertHeadingElement,
115 h6: (node: Node) => ({
116 conversion: $convertHeadingElement,
123 function $convertHeadingElement(element: HTMLElement): DOMConversionOutput {
124 const nodeName = element.nodeName.toLowerCase();
134 node = $createCustomHeadingNode(nodeName);
135 setCommonBlockPropsFromElement(element, node);
140 export function $createCustomHeadingNode(tag: HeadingTagType) {
141 return new CustomHeadingNode(tag);
144 export function $isCustomHeadingNode(node: LexicalNode | null | undefined): node is CustomHeadingNode {
145 return node instanceof CustomHeadingNode;