]> BookStack Code Mirror - bookstack/blobdiff - resources/js/wysiwyg/nodes/details.ts
System CLI: Updated to 126de5599c state
[bookstack] / resources / js / wysiwyg / nodes / details.ts
index 8071d5e8fd98017368fba2c0ef97cdc42bec87cd..de87696f34570b3bfefa637ef352e398de0d1edd 100644 (file)
@@ -4,28 +4,57 @@ import {
     ElementNode,
     LexicalEditor,
     LexicalNode,
-    SerializedElementNode,
+    SerializedElementNode, Spread,
+    EditorConfig,
 } from 'lexical';
-import type {EditorConfig} from "lexical/LexicalEditor";
 
 import {el} from "../utils/dom";
+import {extractDirectionFromElement} from "./_common";
+
+export type SerializedDetailsNode = Spread<{
+    id: string;
+}, SerializedElementNode>
 
 export class DetailsNode extends ElementNode {
+    __id: string = '';
 
     static getType() {
         return 'details';
     }
 
-    static clone(node: DetailsNode) {
-        return new DetailsNode(node.__key);
+    setId(id: string) {
+        const self = this.getWritable();
+        self.__id = id;
+    }
+
+    getId(): string {
+        const self = this.getLatest();
+        return self.__id;
+    }
+
+    static clone(node: DetailsNode): DetailsNode {
+        const newNode =  new DetailsNode(node.__key);
+        newNode.__id = node.__id;
+        newNode.__dir = node.__dir;
+        return newNode;
     }
 
     createDOM(_config: EditorConfig, _editor: LexicalEditor) {
-        return el('details');
+        const el = document.createElement('details');
+        if (this.__id) {
+            el.setAttribute('id', this.__id);
+        }
+
+        if (this.__dir) {
+            el.setAttribute('dir', this.__dir);
+        }
+
+        return el;
     }
 
     updateDOM(prevNode: DetailsNode, dom: HTMLElement) {
-        return false;
+        return prevNode.__id !== this.__id
+        || prevNode.__dir !== this.__dir;
     }
 
     static importDOM(): DOMConversionMap|null {
@@ -33,9 +62,16 @@ export class DetailsNode extends ElementNode {
             details(node: HTMLElement): DOMConversion|null {
                 return {
                     conversion: (element: HTMLElement): DOMConversionOutput|null => {
-                        return {
-                            node: new DetailsNode(),
-                        };
+                        const node = new DetailsNode();
+                        if (element.id) {
+                            node.setId(element.id);
+                        }
+
+                        if (element.dir) {
+                            node.setDirection(extractDirectionFromElement(element));
+                        }
+
+                        return {node};
                     },
                     priority: 3,
                 };
@@ -43,16 +79,20 @@ export class DetailsNode extends ElementNode {
         };
     }
 
-    exportJSON(): SerializedElementNode {
+    exportJSON(): SerializedDetailsNode {
         return {
             ...super.exportJSON(),
             type: 'details',
             version: 1,
+            id: this.__id,
         };
     }
 
-    static importJSON(serializedNode: SerializedElementNode): DetailsNode {
-        return $createDetailsNode();
+    static importJSON(serializedNode: SerializedDetailsNode): DetailsNode {
+        const node = $createDetailsNode();
+        node.setId(serializedNode.id);
+        node.setDirection(serializedNode.direction);
+        return node;
     }
 
 }
@@ -61,7 +101,7 @@ export function $createDetailsNode() {
     return new DetailsNode();
 }
 
-export function $isDetailsNode(node: LexicalNode | null | undefined) {
+export function $isDetailsNode(node: LexicalNode | null | undefined): node is DetailsNode {
     return node instanceof DetailsNode;
 }
 
@@ -106,16 +146,16 @@ export class SummaryNode extends ElementNode {
         };
     }
 
-    static importJSON(serializedNode: SerializedElementNode): DetailsNode {
+    static importJSON(serializedNode: SerializedElementNode): SummaryNode {
         return $createSummaryNode();
     }
 
 }
 
-export function $createSummaryNode() {
+export function $createSummaryNode(): SummaryNode {
     return new SummaryNode();
 }
 
-export function $isSummaryNode(node: LexicalNode | null | undefined) {
+export function $isSummaryNode(node: LexicalNode | null | undefined): node is SummaryNode {
     return node instanceof SummaryNode;
 }