]> BookStack Code Mirror - bookstack/blobdiff - resources/js/wysiwyg/nodes/index.ts
Lexical: Merged list nodes
[bookstack] / resources / js / wysiwyg / nodes / index.ts
index ffe1b027fe5d543391bb7370353b23b6efce6e92..7e0ce9daf2bf437fe44019a8f335077825b7350d 100644 (file)
@@ -1,25 +1,94 @@
-import {HeadingNode, QuoteNode} from '@lexical/rich-text';
 import {CalloutNode} from './callout';
-import {ElementNode, KlassConstructor, LexicalNode, LexicalNodeReplacement, ParagraphNode} from "lexical";
-import {CustomParagraphNode} from "./custom-paragraph";
+import {
+    ElementNode,
+    KlassConstructor,
+    LexicalNode,
+    LexicalNodeReplacement, NodeMutation,
+    ParagraphNode
+} from "lexical";
+import {LinkNode} from "@lexical/link";
+import {ImageNode} from "./image";
+import {DetailsNode, SummaryNode} from "./details";
+import {ListItemNode, ListNode} from "@lexical/list";
+import {TableCellNode, TableNode, TableRowNode} from "@lexical/table";
+import {CustomTableNode} from "./custom-table";
+import {HorizontalRuleNode} from "./horizontal-rule";
+import {CodeBlockNode} from "./code-block";
+import {DiagramNode} from "./diagram";
+import {EditorUiContext} from "../ui/framework/core";
+import {MediaNode} from "./media";
+import {CustomTableCellNode} from "./custom-table-cell";
+import {CustomTableRowNode} from "./custom-table-row";
+import {HeadingNode} from "@lexical/rich-text/LexicalHeadingNode";
+import {QuoteNode} from "@lexical/rich-text/LexicalQuoteNode";
 
 /**
  * Load the nodes for lexical.
  */
 export function getNodesForPageEditor(): (KlassConstructor<typeof LexicalNode> | LexicalNodeReplacement)[] {
     return [
-        CalloutNode, // Todo - Create custom
-        HeadingNode, // Todo - Create custom
-        QuoteNode, // Todo - Create custom
-        CustomParagraphNode,
+        CalloutNode,
+        HeadingNode,
+        QuoteNode,
+        ListNode,
+        ListItemNode,
+        CustomTableNode,
+        CustomTableRowNode,
+        CustomTableCellNode,
+        ImageNode, // TODO - Alignment
+        HorizontalRuleNode,
+        DetailsNode, SummaryNode,
+        CodeBlockNode,
+        DiagramNode,
+        MediaNode, // TODO - Alignment
+        ParagraphNode,
+        LinkNode,
         {
-            replace: ParagraphNode,
-            with: (node: ParagraphNode) => {
-                return new CustomParagraphNode();
+            replace: TableNode,
+            with(node: TableNode) {
+                return new CustomTableNode();
             }
-        }
+        },
+        {
+            replace: TableRowNode,
+            with(node: TableRowNode) {
+                return new CustomTableRowNode();
+            }
+        },
+        {
+            replace: TableCellNode,
+            with: (node: TableCellNode) => {
+                const cell = new CustomTableCellNode(
+                    node.__headerState,
+                    node.__colSpan,
+                    node.__width,
+                );
+                cell.__rowSpan = node.__rowSpan;
+                return cell;
+            }
+        },
     ];
 }
 
+export function registerCommonNodeMutationListeners(context: EditorUiContext): void {
+    const decorated = [ImageNode, CodeBlockNode, DiagramNode];
+
+    const decorationDestroyListener = (mutations: Map<string, NodeMutation>): void => {
+        for (let [nodeKey, mutation] of mutations) {
+            if (mutation === "destroyed") {
+                const decorator = context.manager.getDecoratorByNodeKey(nodeKey);
+                if (decorator) {
+                    decorator.destroy(context);
+                }
+            }
+        }
+    };
+
+    for (let decoratedNode of decorated) {
+        // Have to pass a unique function here since they are stored by lexical keyed on listener function.
+        context.editor.registerMutationListener(decoratedNode, (mutations) => decorationDestroyListener(mutations));
+    }
+}
+
 export type LexicalNodeMatcher = (node: LexicalNode|null|undefined) => boolean;
 export type LexicalElementNodeCreator = () => ElementNode;
\ No newline at end of file