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 const newNode = new CustomHeadingNode(node.__tag, node.__key);
34 newNode.__id = node.__id;
38 createDOM(config: EditorConfig): HTMLElement {
39 const dom = super.createDOM(config);
41 dom.setAttribute('id', this.__id);
47 exportJSON(): SerializedCustomHeadingNode {
49 ...super.exportJSON(),
50 type: 'custom-heading',
56 static importJSON(serializedNode: SerializedCustomHeadingNode): CustomHeadingNode {
57 const node = $createCustomHeadingNode(serializedNode.tag);
58 node.setId(serializedNode.id);
62 static importDOM(): DOMConversionMap | null {
64 h1: (node: Node) => ({
65 conversion: $convertHeadingElement,
68 h2: (node: Node) => ({
69 conversion: $convertHeadingElement,
72 h3: (node: Node) => ({
73 conversion: $convertHeadingElement,
76 h4: (node: Node) => ({
77 conversion: $convertHeadingElement,
80 h5: (node: Node) => ({
81 conversion: $convertHeadingElement,
84 h6: (node: Node) => ({
85 conversion: $convertHeadingElement,
92 function $convertHeadingElement(element: HTMLElement): DOMConversionOutput {
93 const nodeName = element.nodeName.toLowerCase();
103 node = $createCustomHeadingNode(nodeName);
104 if (element.style !== null) {
105 node.setFormat(element.style.textAlign as ElementFormatType);
108 node.setId(element.id);
114 export function $createCustomHeadingNode(tag: HeadingTagType) {
115 return new CustomHeadingNode(tag);
118 export function $isCustomHeadingNode(node: LexicalNode | null | undefined): node is CustomHeadingNode {
119 return node instanceof CustomHeadingNode;