3 DOMConversionOutput, ElementFormatType,
7 import {EditorConfig} from "lexical/LexicalEditor";
8 import {HeadingNode, HeadingTagType, SerializedHeadingNode} from "@lexical/rich-text";
11 export type SerializedCustomHeadingNode = Spread<{
13 }, SerializedHeadingNode>
15 export class CustomHeadingNode extends HeadingNode {
19 return 'custom-heading';
23 const self = this.getWritable();
28 const self = this.getLatest();
32 static clone(node: CustomHeadingNode) {
33 return new CustomHeadingNode(node.__tag, node.__key);
36 createDOM(config: EditorConfig): HTMLElement {
37 const dom = super.createDOM(config);
39 dom.setAttribute('id', this.__id);
45 exportJSON(): SerializedCustomHeadingNode {
47 ...super.exportJSON(),
48 type: 'custom-heading',
54 static importJSON(serializedNode: SerializedCustomHeadingNode): CustomHeadingNode {
55 const node = $createCustomHeadingNode(serializedNode.tag);
56 node.setId(serializedNode.id);
60 static importDOM(): DOMConversionMap | null {
62 h1: (node: Node) => ({
63 conversion: $convertHeadingElement,
66 h2: (node: Node) => ({
67 conversion: $convertHeadingElement,
70 h3: (node: Node) => ({
71 conversion: $convertHeadingElement,
74 h4: (node: Node) => ({
75 conversion: $convertHeadingElement,
78 h5: (node: Node) => ({
79 conversion: $convertHeadingElement,
82 h6: (node: Node) => ({
83 conversion: $convertHeadingElement,
90 function $convertHeadingElement(element: HTMLElement): DOMConversionOutput {
91 const nodeName = element.nodeName.toLowerCase();
101 node = $createCustomHeadingNode(nodeName);
102 if (element.style !== null) {
103 node.setFormat(element.style.textAlign as ElementFormatType);
106 node.setId(element.id);
112 export function $createCustomHeadingNode(tag: HeadingTagType) {
113 return new CustomHeadingNode(tag);
116 export function $isCustomHeadingNode(node: LexicalNode | null | undefined): node is CustomHeadingNode {
117 return node instanceof CustomHeadingNode;