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