]> BookStack Code Mirror - bookstack/blobdiff - resources/js/wysiwyg/nodes/custom-table.ts
ExportFormatter: Add book description and check for empty book and chapter descriptio...
[bookstack] / resources / js / wysiwyg / nodes / custom-table.ts
index 1d95b789625e1b0ab48204cae062959fd7b8001a..c25c06c65151012f338d4c8cfb493f82856c5afa 100644 (file)
@@ -4,17 +4,24 @@ import {EditorConfig} from "lexical/LexicalEditor";
 
 import {el, extractStyleMapFromElement, StyleMap} from "../utils/dom";
 import {getTableColumnWidths} from "../utils/tables";
-
-export type SerializedCustomTableNode = Spread<{
-    id: string;
+import {
+    CommonBlockAlignment, deserializeCommonBlockNode,
+    SerializedCommonBlockNode,
+    setCommonBlockPropsFromElement,
+    updateElementWithCommonBlockProps
+} from "./_common";
+
+export type SerializedCustomTableNode = Spread<Spread<{
     colWidths: string[];
     styles: Record<string, string>,
-}, SerializedTableNode>
+}, SerializedTableNode>, SerializedCommonBlockNode>
 
 export class CustomTableNode extends TableNode {
     __id: string = '';
     __colWidths: string[] = [];
     __styles: StyleMap = new Map;
+    __alignment: CommonBlockAlignment = '';
+    __inset: number = 0;
 
     static getType() {
         return 'custom-table';
@@ -30,6 +37,26 @@ export class CustomTableNode extends TableNode {
         return self.__id;
     }
 
+    setAlignment(alignment: CommonBlockAlignment) {
+        const self = this.getWritable();
+        self.__alignment = alignment;
+    }
+
+    getAlignment(): CommonBlockAlignment {
+        const self = this.getLatest();
+        return self.__alignment;
+    }
+
+    setInset(size: number) {
+        const self = this.getWritable();
+        self.__inset = size;
+    }
+
+    getInset(): number {
+        const self = this.getLatest();
+        return self.__inset;
+    }
+
     setColWidths(widths: string[]) {
         const self = this.getWritable();
         self.__colWidths = widths;
@@ -55,15 +82,14 @@ export class CustomTableNode extends TableNode {
         newNode.__id = node.__id;
         newNode.__colWidths = node.__colWidths;
         newNode.__styles = new Map(node.__styles);
+        newNode.__alignment = node.__alignment;
+        newNode.__inset = node.__inset;
         return newNode;
     }
 
     createDOM(config: EditorConfig): HTMLElement {
         const dom = super.createDOM(config);
-        const id = this.getId();
-        if (id) {
-            dom.setAttribute('id', id);
-        }
+        updateElementWithCommonBlockProps(dom, this);
 
         const colWidths = this.getColWidths();
         if (colWidths.length > 0) {
@@ -97,12 +123,14 @@ export class CustomTableNode extends TableNode {
             id: this.__id,
             colWidths: this.__colWidths,
             styles: Object.fromEntries(this.__styles),
+            alignment: this.__alignment,
+            inset: this.__inset,
         };
     }
 
     static importJSON(serializedNode: SerializedCustomTableNode): CustomTableNode {
         const node = $createCustomTableNode();
-        node.setId(serializedNode.id);
+        deserializeCommonBlockNode(serializedNode, node);
         node.setColWidths(serializedNode.colWidths);
         node.setStyles(new Map(Object.entries(serializedNode.styles)));
         return node;
@@ -114,10 +142,7 @@ export class CustomTableNode extends TableNode {
                 return {
                     conversion: (element: HTMLElement): DOMConversionOutput|null => {
                         const node = $createCustomTableNode();
-
-                        if (element.id) {
-                            node.setId(element.id);
-                        }
+                        setCommonBlockPropsFromElement(element, node);
 
                         const colWidths = getTableColumnWidths(element as HTMLTableElement);
                         node.setColWidths(colWidths);