]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/utils/selection.ts
Lexical: Added single node enter handling
[bookstack] / resources / js / wysiwyg / utils / selection.ts
1 import {
2     $createNodeSelection,
3     $createParagraphNode, $createRangeSelection,
4     $getRoot,
5     $getSelection, $isDecoratorNode,
6     $isElementNode,
7     $isTextNode,
8     $setSelection,
9     BaseSelection, DecoratorNode,
10     ElementFormatType,
11     ElementNode, LexicalEditor,
12     LexicalNode,
13     TextFormatType
14 } from "lexical";
15 import {$findMatchingParent, $getNearestBlockElementAncestorOrThrow} from "@lexical/utils";
16 import {LexicalElementNodeCreator, LexicalNodeMatcher} from "../nodes";
17 import {$setBlocksType} from "@lexical/selection";
18
19 import {$getNearestNodeBlockParent, $getParentOfType, nodeHasAlignment} from "./nodes";
20 import {$createCustomParagraphNode} from "../nodes/custom-paragraph";
21 import {CommonBlockAlignment} from "../nodes/_common";
22
23 const lastSelectionByEditor = new WeakMap<LexicalEditor, BaseSelection|null>;
24
25 export function getLastSelection(editor: LexicalEditor): BaseSelection|null {
26     return lastSelectionByEditor.get(editor) || null;
27 }
28
29 export function setLastSelection(editor: LexicalEditor, selection: BaseSelection|null): void {
30     lastSelectionByEditor.set(editor, selection);
31 }
32
33 export function $selectionContainsNodeType(selection: BaseSelection | null, matcher: LexicalNodeMatcher): boolean {
34     return $getNodeFromSelection(selection, matcher) !== null;
35 }
36
37 export function $getNodeFromSelection(selection: BaseSelection | null, matcher: LexicalNodeMatcher): LexicalNode | null {
38     if (!selection) {
39         return null;
40     }
41
42     for (const node of selection.getNodes()) {
43         if (matcher(node)) {
44             return node;
45         }
46
47         const matchedParent = $getParentOfType(node, matcher);
48         if (matchedParent) {
49             return matchedParent;
50         }
51     }
52
53     return null;
54 }
55
56 export function $selectionContainsTextFormat(selection: BaseSelection | null, format: TextFormatType): boolean {
57     if (!selection) {
58         return false;
59     }
60
61     for (const node of selection.getNodes()) {
62         if ($isTextNode(node) && node.hasFormat(format)) {
63             return true;
64         }
65     }
66
67     return false;
68 }
69
70 export function $toggleSelectionBlockNodeType(matcher: LexicalNodeMatcher, creator: LexicalElementNodeCreator) {
71     const selection = $getSelection();
72     const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
73     if (selection && matcher(blockElement)) {
74         $setBlocksType(selection, $createCustomParagraphNode);
75     } else {
76         $setBlocksType(selection, creator);
77     }
78 }
79
80 export function $insertNewBlockNodeAtSelection(node: LexicalNode, insertAfter: boolean = true) {
81     $insertNewBlockNodesAtSelection([node], insertAfter);
82 }
83
84 export function $insertNewBlockNodesAtSelection(nodes: LexicalNode[], insertAfter: boolean = true) {
85     const selection = $getSelection();
86     const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
87
88     if (blockElement) {
89         if (insertAfter) {
90             for (let i = nodes.length - 1; i >= 0; i--) {
91                 blockElement.insertAfter(nodes[i]);
92             }
93         } else {
94             for (const node of nodes) {
95                 blockElement.insertBefore(node);
96             }
97         }
98     } else {
99         $getRoot().append(...nodes);
100     }
101 }
102
103 export function $selectSingleNode(node: LexicalNode) {
104     const nodeSelection = $createNodeSelection();
105     nodeSelection.add(node.getKey());
106     $setSelection(nodeSelection);
107 }
108
109 export function $toggleSelection(editor: LexicalEditor) {
110     const lastSelection = getLastSelection(editor);
111
112     if (lastSelection) {
113         window.requestAnimationFrame(() => {
114             editor.update(() => {
115                 $setSelection(lastSelection.clone());
116             })
117         });
118     }
119 }
120
121 export function $selectionContainsNode(selection: BaseSelection | null, node: LexicalNode): boolean {
122     if (!selection) {
123         return false;
124     }
125
126     const key = node.getKey();
127     for (const node of selection.getNodes()) {
128         if (node.getKey() === key) {
129             return true;
130         }
131     }
132
133     return false;
134 }
135
136 export function $selectionContainsAlignment(selection: BaseSelection | null, alignment: CommonBlockAlignment): boolean {
137
138     const nodes = [
139         ...(selection?.getNodes() || []),
140         ...$getBlockElementNodesInSelection(selection)
141     ];
142     for (const node of nodes) {
143         if (nodeHasAlignment(node) && node.getAlignment() === alignment) {
144             return true;
145         }
146     }
147
148     return false;
149 }
150
151 export function $getBlockElementNodesInSelection(selection: BaseSelection | null): ElementNode[] {
152     if (!selection) {
153         return [];
154     }
155
156     const blockNodes: Map<string, ElementNode> = new Map();
157     for (const node of selection.getNodes()) {
158         const blockElement = $getNearestNodeBlockParent(node);
159         if ($isElementNode(blockElement)) {
160             blockNodes.set(blockElement.getKey(), blockElement);
161         }
162     }
163
164     return Array.from(blockNodes.values());
165 }
166
167 export function $getDecoratorNodesInSelection(selection: BaseSelection | null): DecoratorNode<any>[] {
168     if (!selection) {
169         return [];
170     }
171
172     return selection.getNodes().filter(node => $isDecoratorNode(node));
173 }