]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/nodes.ts
Tests: Updated comment test to account for new editor usage
[bookstack] / resources / js / wysiwyg / nodes.ts
1 import {CalloutNode} from '@lexical/rich-text/LexicalCalloutNode';
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 "@lexical/rich-text/LexicalImageNode";
11 import {DetailsNode} from "@lexical/rich-text/LexicalDetailsNode";
12 import {ListItemNode, ListNode} from "@lexical/list";
13 import {TableCellNode, TableNode, TableRowNode} from "@lexical/table";
14 import {HorizontalRuleNode} from "@lexical/rich-text/LexicalHorizontalRuleNode";
15 import {CodeBlockNode} from "@lexical/rich-text/LexicalCodeBlockNode";
16 import {DiagramNode} from "@lexical/rich-text/LexicalDiagramNode";
17 import {EditorUiContext} from "./ui/framework/core";
18 import {MediaNode} from "@lexical/rich-text/LexicalMediaNode";
19 import {HeadingNode} from "@lexical/rich-text/LexicalHeadingNode";
20 import {QuoteNode} from "@lexical/rich-text/LexicalQuoteNode";
21 import {CaptionNode} from "@lexical/table/LexicalCaptionNode";
22
23 export function getNodesForPageEditor(): (KlassConstructor<typeof LexicalNode> | LexicalNodeReplacement)[] {
24     return [
25         CalloutNode,
26         HeadingNode,
27         QuoteNode,
28         ListNode,
29         ListItemNode,
30         TableNode,
31         TableRowNode,
32         TableCellNode,
33         CaptionNode,
34         ImageNode, // TODO - Alignment
35         HorizontalRuleNode,
36         DetailsNode,
37         CodeBlockNode,
38         DiagramNode,
39         MediaNode, // TODO - Alignment
40         ParagraphNode,
41         LinkNode,
42     ];
43 }
44
45 export function getNodesForBasicEditor(): (KlassConstructor<typeof LexicalNode> | LexicalNodeReplacement)[] {
46     return [
47         ListNode,
48         ListItemNode,
49         ParagraphNode,
50         LinkNode,
51     ];
52 }
53
54 export function registerCommonNodeMutationListeners(context: EditorUiContext): void {
55     const decorated = [ImageNode, CodeBlockNode, DiagramNode];
56
57     const decorationDestroyListener = (mutations: Map<string, NodeMutation>): void => {
58         for (let [nodeKey, mutation] of mutations) {
59             if (mutation === "destroyed") {
60                 const decorator = context.manager.getDecoratorByNodeKey(nodeKey);
61                 if (decorator) {
62                     decorator.teardown();
63                 }
64             }
65         }
66     };
67
68     for (let decoratedNode of decorated) {
69         // Have to pass a unique function here since they are stored by lexical keyed on listener function.
70         context.editor.registerMutationListener(decoratedNode, (mutations) => decorationDestroyListener(mutations));
71     }
72 }
73
74 export type LexicalNodeMatcher = (node: LexicalNode|null|undefined) => boolean;
75 export type LexicalElementNodeCreator = () => ElementNode;