]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/nodes/horizontal-rule.ts
Move settings category layouts into their own view folder
[bookstack] / resources / js / wysiwyg / nodes / horizontal-rule.ts
1 import {
2     DOMConversion,
3     DOMConversionMap, DOMConversionOutput,
4     ElementNode,
5     LexicalEditor,
6     LexicalNode,
7     SerializedElementNode, Spread,
8 } from 'lexical';
9 import type {EditorConfig} from "lexical/LexicalEditor";
10
11 export type SerializedHorizontalRuleNode = Spread<{
12     id: string;
13 }, SerializedElementNode>
14
15 export class HorizontalRuleNode extends ElementNode {
16     __id: string = '';
17
18     static getType() {
19         return 'horizontal-rule';
20     }
21
22     setId(id: string) {
23         const self = this.getWritable();
24         self.__id = id;
25     }
26
27     getId(): string {
28         const self = this.getLatest();
29         return self.__id;
30     }
31
32     static clone(node: HorizontalRuleNode): HorizontalRuleNode {
33         const newNode = new HorizontalRuleNode(node.__key);
34         newNode.__id = node.__id;
35         return newNode;
36     }
37
38     createDOM(_config: EditorConfig, _editor: LexicalEditor): HTMLElement {
39         const el = document.createElement('hr');
40         if (this.__id) {
41             el.setAttribute('id', this.__id);
42         }
43
44         return el;
45     }
46
47     updateDOM(prevNode: HorizontalRuleNode, dom: HTMLElement) {
48         return prevNode.__id !== this.__id;
49     }
50
51     static importDOM(): DOMConversionMap|null {
52         return {
53             hr(node: HTMLElement): DOMConversion|null {
54                 return {
55                     conversion: (element: HTMLElement): DOMConversionOutput|null => {
56                         const node = new HorizontalRuleNode();
57                         if (element.id) {
58                             node.setId(element.id);
59                         }
60
61                         return {node};
62                     },
63                     priority: 3,
64                 };
65             },
66         };
67     }
68
69     exportJSON(): SerializedHorizontalRuleNode {
70         return {
71             ...super.exportJSON(),
72             type: 'horizontal-rule',
73             version: 1,
74             id: this.__id,
75         };
76     }
77
78     static importJSON(serializedNode: SerializedHorizontalRuleNode): HorizontalRuleNode {
79         const node = $createHorizontalRuleNode();
80         node.setId(serializedNode.id);
81         return node;
82     }
83
84 }
85
86 export function $createHorizontalRuleNode(): HorizontalRuleNode {
87     return new HorizontalRuleNode();
88 }
89
90 export function $isHorizontalRuleNode(node: LexicalNode | null | undefined): node is HorizontalRuleNode {
91     return node instanceof HorizontalRuleNode;
92 }