]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/nodes/index.ts
Lexical: Merged custom table node code
[bookstack] / resources / js / wysiwyg / nodes / index.ts
1 import {CalloutNode} from './callout';
2 import {
3     ElementNode,
4     KlassConstructor,
5     LexicalNode,
6     LexicalNodeReplacement, NodeMutation,
7     ParagraphNode
8 } from "lexical";
9 import {LinkNode} from "@lexical/link";
10 import {ImageNode} from "./image";
11 import {DetailsNode, SummaryNode} from "./details";
12 import {ListItemNode, ListNode} from "@lexical/list";
13 import {TableCellNode, TableNode, TableRowNode} from "@lexical/table";
14 import {HorizontalRuleNode} from "./horizontal-rule";
15 import {CodeBlockNode} from "./code-block";
16 import {DiagramNode} from "./diagram";
17 import {EditorUiContext} from "../ui/framework/core";
18 import {MediaNode} from "./media";
19 import {HeadingNode} from "@lexical/rich-text/LexicalHeadingNode";
20 import {QuoteNode} from "@lexical/rich-text/LexicalQuoteNode";
21
22 /**
23  * Load the nodes for lexical.
24  */
25 export function getNodesForPageEditor(): (KlassConstructor<typeof LexicalNode> | LexicalNodeReplacement)[] {
26     return [
27         CalloutNode,
28         HeadingNode,
29         QuoteNode,
30         ListNode,
31         ListItemNode,
32         TableNode,
33         TableRowNode,
34         TableCellNode,
35         ImageNode, // TODO - Alignment
36         HorizontalRuleNode,
37         DetailsNode, SummaryNode,
38         CodeBlockNode,
39         DiagramNode,
40         MediaNode, // TODO - Alignment
41         ParagraphNode,
42         LinkNode,
43     ];
44 }
45
46 export function registerCommonNodeMutationListeners(context: EditorUiContext): void {
47     const decorated = [ImageNode, CodeBlockNode, DiagramNode];
48
49     const decorationDestroyListener = (mutations: Map<string, NodeMutation>): void => {
50         for (let [nodeKey, mutation] of mutations) {
51             if (mutation === "destroyed") {
52                 const decorator = context.manager.getDecoratorByNodeKey(nodeKey);
53                 if (decorator) {
54                     decorator.destroy(context);
55                 }
56             }
57         }
58     };
59
60     for (let decoratedNode of decorated) {
61         // Have to pass a unique function here since they are stored by lexical keyed on listener function.
62         context.editor.registerMutationListener(decoratedNode, (mutations) => decorationDestroyListener(mutations));
63     }
64 }
65
66 export type LexicalNodeMatcher = (node: LexicalNode|null|undefined) => boolean;
67 export type LexicalElementNodeCreator = () => ElementNode;