]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/utils/selection.ts
Lexical: Added some level of img/media alignment
[bookstack] / resources / js / wysiwyg / utils / selection.ts
1 import {
2     $createNodeSelection,
3     $createParagraphNode,
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 {$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 $selectionContainsNode(selection: BaseSelection | null, node: LexicalNode): boolean {
110     if (!selection) {
111         return false;
112     }
113
114     const key = node.getKey();
115     for (const node of selection.getNodes()) {
116         if (node.getKey() === key) {
117             return true;
118         }
119     }
120
121     return false;
122 }
123
124 export function $selectionContainsAlignment(selection: BaseSelection | null, alignment: CommonBlockAlignment): boolean {
125     const nodes = $getBlockElementNodesInSelection(selection);
126     for (const node of nodes) {
127         if (nodeHasAlignment(node) && node.getAlignment() === alignment) {
128             return true;
129         }
130     }
131
132     return false;
133 }
134
135 export function $getBlockElementNodesInSelection(selection: BaseSelection | null): ElementNode[] {
136     if (!selection) {
137         return [];
138     }
139
140     const blockNodes: Map<string, ElementNode> = new Map();
141     for (const node of selection.getNodes()) {
142         const blockElement = $findMatchingParent(node, (node) => {
143             return $isElementNode(node) && !node.isInline();
144         }) as ElementNode | null;
145
146         if (blockElement) {
147             blockNodes.set(blockElement.getKey(), blockElement);
148         }
149     }
150
151     return Array.from(blockNodes.values());
152 }
153
154 export function $getDecoratorNodesInSelection(selection: BaseSelection | null): DecoratorNode<any>[] {
155     if (!selection) {
156         return [];
157     }
158
159     return selection.getNodes().filter(node => $isDecoratorNode(node));
160 }