@ekz/lexical-utils 0.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1066 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ 'use strict';
10
+
11
+ var lexical = require('@ekz/lexical');
12
+ var lexicalSelection = require('@ekz/lexical-selection');
13
+
14
+ /**
15
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
16
+ *
17
+ * This source code is licensed under the MIT license found in the
18
+ * LICENSE file in the root directory of this source tree.
19
+ *
20
+ */
21
+
22
+ // Do not require this module directly! Use normal `invariant` calls.
23
+
24
+ function formatDevErrorMessage(message) {
25
+ throw new Error(message);
26
+ }
27
+
28
+ /**
29
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
30
+ *
31
+ * This source code is licensed under the MIT license found in the
32
+ * LICENSE file in the root directory of this source tree.
33
+ *
34
+ */
35
+
36
+ const CAN_USE_DOM$1 = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
37
+
38
+ /**
39
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
40
+ *
41
+ * This source code is licensed under the MIT license found in the
42
+ * LICENSE file in the root directory of this source tree.
43
+ *
44
+ */
45
+
46
+ const documentMode = CAN_USE_DOM$1 && 'documentMode' in document ? document.documentMode : null;
47
+ const IS_APPLE$1 = CAN_USE_DOM$1 && /Mac|iPod|iPhone|iPad/.test(navigator.platform);
48
+ const IS_FIREFOX$1 = CAN_USE_DOM$1 && /^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
49
+ const CAN_USE_BEFORE_INPUT$1 = CAN_USE_DOM$1 && 'InputEvent' in window && !documentMode ? 'getTargetRanges' in new window.InputEvent('input') : false;
50
+ const IS_SAFARI$1 = CAN_USE_DOM$1 && /Version\/[\d.]+.*Safari/.test(navigator.userAgent);
51
+ const IS_IOS$1 = CAN_USE_DOM$1 && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
52
+ const IS_ANDROID$1 = CAN_USE_DOM$1 && /Android/.test(navigator.userAgent);
53
+
54
+ // Keep these in case we need to use them in the future.
55
+ // export const IS_WINDOWS: boolean = CAN_USE_DOM && /Win/.test(navigator.platform);
56
+ const IS_CHROME$1 = CAN_USE_DOM$1 && /^(?=.*Chrome).*/i.test(navigator.userAgent);
57
+ // export const canUseTextInputEvent: boolean = CAN_USE_DOM && 'TextEvent' in window && !documentMode;
58
+
59
+ const IS_ANDROID_CHROME$1 = CAN_USE_DOM$1 && IS_ANDROID$1 && IS_CHROME$1;
60
+ const IS_APPLE_WEBKIT$1 = CAN_USE_DOM$1 && /AppleWebKit\/[\d.]+/.test(navigator.userAgent) && IS_APPLE$1 && !IS_CHROME$1;
61
+
62
+ /**
63
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
64
+ *
65
+ * This source code is licensed under the MIT license found in the
66
+ * LICENSE file in the root directory of this source tree.
67
+ *
68
+ */
69
+
70
+ function px(value) {
71
+ return `${value}px`;
72
+ }
73
+
74
+ const mutationObserverConfig = {
75
+ attributes: true,
76
+ characterData: true,
77
+ childList: true,
78
+ subtree: true
79
+ };
80
+ function prependDOMNode(parent, node) {
81
+ parent.insertBefore(node, parent.firstChild);
82
+ }
83
+
84
+ /**
85
+ * Place one or multiple newly created Nodes at the passed Range's position.
86
+ * Multiple nodes will only be created when the Range spans multiple lines (aka
87
+ * client rects).
88
+ *
89
+ * This function can come particularly useful to highlight particular parts of
90
+ * the text without interfering with the EditorState, that will often replicate
91
+ * the state across collab and clipboard.
92
+ *
93
+ * This function accounts for DOM updates which can modify the passed Range.
94
+ * Hence, the function return to remove the listener.
95
+ */
96
+ function mlcPositionNodeOnRange(editor, range, onReposition) {
97
+ let rootDOMNode = null;
98
+ let parentDOMNode = null;
99
+ let observer = null;
100
+ let lastNodes = [];
101
+ const wrapperNode = document.createElement('div');
102
+ wrapperNode.style.position = 'relative';
103
+ function position() {
104
+ if (!(rootDOMNode !== null)) {
105
+ formatDevErrorMessage(`Unexpected null rootDOMNode`);
106
+ }
107
+ if (!(parentDOMNode !== null)) {
108
+ formatDevErrorMessage(`Unexpected null parentDOMNode`);
109
+ }
110
+ const {
111
+ left: parentLeft,
112
+ top: parentTop
113
+ } = parentDOMNode.getBoundingClientRect();
114
+ const rects = lexicalSelection.createRectsFromDOMRange(editor, range);
115
+ if (!wrapperNode.isConnected) {
116
+ prependDOMNode(parentDOMNode, wrapperNode);
117
+ }
118
+ let hasRepositioned = false;
119
+ for (let i = 0; i < rects.length; i++) {
120
+ const rect = rects[i];
121
+ // Try to reuse the previously created Node when possible, no need to
122
+ // remove/create on the most common case reposition case
123
+ const rectNode = lastNodes[i] || document.createElement('div');
124
+ const rectNodeStyle = rectNode.style;
125
+ if (rectNodeStyle.position !== 'absolute') {
126
+ rectNodeStyle.position = 'absolute';
127
+ hasRepositioned = true;
128
+ }
129
+ const left = px(rect.left - parentLeft);
130
+ if (rectNodeStyle.left !== left) {
131
+ rectNodeStyle.left = left;
132
+ hasRepositioned = true;
133
+ }
134
+ const top = px(rect.top - parentTop);
135
+ if (rectNodeStyle.top !== top) {
136
+ rectNode.style.top = top;
137
+ hasRepositioned = true;
138
+ }
139
+ const width = px(rect.width);
140
+ if (rectNodeStyle.width !== width) {
141
+ rectNode.style.width = width;
142
+ hasRepositioned = true;
143
+ }
144
+ const height = px(rect.height);
145
+ if (rectNodeStyle.height !== height) {
146
+ rectNode.style.height = height;
147
+ hasRepositioned = true;
148
+ }
149
+ if (rectNode.parentNode !== wrapperNode) {
150
+ wrapperNode.append(rectNode);
151
+ hasRepositioned = true;
152
+ }
153
+ lastNodes[i] = rectNode;
154
+ }
155
+ while (lastNodes.length > rects.length) {
156
+ lastNodes.pop();
157
+ }
158
+ if (hasRepositioned) {
159
+ onReposition(lastNodes);
160
+ }
161
+ }
162
+ function stop() {
163
+ parentDOMNode = null;
164
+ rootDOMNode = null;
165
+ if (observer !== null) {
166
+ observer.disconnect();
167
+ }
168
+ observer = null;
169
+ wrapperNode.remove();
170
+ for (const node of lastNodes) {
171
+ node.remove();
172
+ }
173
+ lastNodes = [];
174
+ }
175
+ function restart() {
176
+ const currentRootDOMNode = editor.getRootElement();
177
+ if (currentRootDOMNode === null) {
178
+ return stop();
179
+ }
180
+ const currentParentDOMNode = currentRootDOMNode.parentElement;
181
+ if (!lexical.isHTMLElement(currentParentDOMNode)) {
182
+ return stop();
183
+ }
184
+ stop();
185
+ rootDOMNode = currentRootDOMNode;
186
+ parentDOMNode = currentParentDOMNode;
187
+ observer = new MutationObserver(mutations => {
188
+ const nextRootDOMNode = editor.getRootElement();
189
+ const nextParentDOMNode = nextRootDOMNode && nextRootDOMNode.parentElement;
190
+ if (nextRootDOMNode !== rootDOMNode || nextParentDOMNode !== parentDOMNode) {
191
+ return restart();
192
+ }
193
+ for (const mutation of mutations) {
194
+ if (!wrapperNode.contains(mutation.target)) {
195
+ // TODO throttle
196
+ return position();
197
+ }
198
+ }
199
+ });
200
+ observer.observe(currentParentDOMNode, mutationObserverConfig);
201
+ position();
202
+ }
203
+ const removeRootListener = editor.registerRootListener(restart);
204
+ return () => {
205
+ removeRootListener();
206
+ stop();
207
+ };
208
+ }
209
+
210
+ /**
211
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
212
+ *
213
+ * This source code is licensed under the MIT license found in the
214
+ * LICENSE file in the root directory of this source tree.
215
+ *
216
+ */
217
+
218
+ function $getOrderedSelectionPoints(selection) {
219
+ const points = selection.getStartEndPoints();
220
+ return selection.isBackward() ? [points[1], points[0]] : points;
221
+ }
222
+ function rangeTargetFromPoint(point, node, dom) {
223
+ if (point.type === 'text' || !lexical.$isElementNode(node)) {
224
+ const textDOM = lexical.getDOMTextNode(dom) || dom;
225
+ return [textDOM, point.offset];
226
+ } else {
227
+ const slot = node.getDOMSlot(dom);
228
+ return [slot.element, slot.getFirstChildOffset() + point.offset];
229
+ }
230
+ }
231
+ function rangeFromPoints(editor, start, startNode, startDOM, end, endNode, endDOM) {
232
+ const editorDocument = editor._window ? editor._window.document : document;
233
+ const range = editorDocument.createRange();
234
+ range.setStart(...rangeTargetFromPoint(start, startNode, startDOM));
235
+ range.setEnd(...rangeTargetFromPoint(end, endNode, endDOM));
236
+ return range;
237
+ }
238
+ /**
239
+ * Place one or multiple newly created Nodes at the current selection. Multiple
240
+ * nodes will only be created when the selection spans multiple lines (aka
241
+ * client rects).
242
+ *
243
+ * This function can come useful when you want to show the selection but the
244
+ * editor has been focused away.
245
+ */
246
+ function markSelection(editor, onReposition) {
247
+ let previousAnchorNode = null;
248
+ let previousAnchorNodeDOM = null;
249
+ let previousAnchorOffset = null;
250
+ let previousFocusNode = null;
251
+ let previousFocusNodeDOM = null;
252
+ let previousFocusOffset = null;
253
+ let removeRangeListener = () => {};
254
+ function compute(editorState) {
255
+ editorState.read(() => {
256
+ const selection = lexical.$getSelection();
257
+ if (!lexical.$isRangeSelection(selection)) {
258
+ // TODO
259
+ previousAnchorNode = null;
260
+ previousAnchorOffset = null;
261
+ previousFocusNode = null;
262
+ previousFocusOffset = null;
263
+ removeRangeListener();
264
+ removeRangeListener = () => {};
265
+ return;
266
+ }
267
+ const [start, end] = $getOrderedSelectionPoints(selection);
268
+ const currentStartNode = start.getNode();
269
+ const currentStartNodeKey = currentStartNode.getKey();
270
+ const currentStartOffset = start.offset;
271
+ const currentEndNode = end.getNode();
272
+ const currentEndNodeKey = currentEndNode.getKey();
273
+ const currentEndOffset = end.offset;
274
+ const currentStartNodeDOM = editor.getElementByKey(currentStartNodeKey);
275
+ const currentEndNodeDOM = editor.getElementByKey(currentEndNodeKey);
276
+ const differentStartDOM = previousAnchorNode === null || currentStartNodeDOM !== previousAnchorNodeDOM || currentStartOffset !== previousAnchorOffset || currentStartNodeKey !== previousAnchorNode.getKey();
277
+ const differentEndDOM = previousFocusNode === null || currentEndNodeDOM !== previousFocusNodeDOM || currentEndOffset !== previousFocusOffset || currentEndNodeKey !== previousFocusNode.getKey();
278
+ if ((differentStartDOM || differentEndDOM) && currentStartNodeDOM !== null && currentEndNodeDOM !== null) {
279
+ const range = rangeFromPoints(editor, start, currentStartNode, currentStartNodeDOM, end, currentEndNode, currentEndNodeDOM);
280
+ removeRangeListener();
281
+ removeRangeListener = mlcPositionNodeOnRange(editor, range, domNodes => {
282
+ if (onReposition === undefined) {
283
+ for (const domNode of domNodes) {
284
+ const domNodeStyle = domNode.style;
285
+ if (domNodeStyle.background !== 'Highlight') {
286
+ domNodeStyle.background = 'Highlight';
287
+ }
288
+ if (domNodeStyle.color !== 'HighlightText') {
289
+ domNodeStyle.color = 'HighlightText';
290
+ }
291
+ if (domNodeStyle.marginTop !== px(-1.5)) {
292
+ domNodeStyle.marginTop = px(-1.5);
293
+ }
294
+ if (domNodeStyle.paddingTop !== px(4)) {
295
+ domNodeStyle.paddingTop = px(4);
296
+ }
297
+ if (domNodeStyle.paddingBottom !== px(0)) {
298
+ domNodeStyle.paddingBottom = px(0);
299
+ }
300
+ }
301
+ } else {
302
+ onReposition(domNodes);
303
+ }
304
+ });
305
+ }
306
+ previousAnchorNode = currentStartNode;
307
+ previousAnchorNodeDOM = currentStartNodeDOM;
308
+ previousAnchorOffset = currentStartOffset;
309
+ previousFocusNode = currentEndNode;
310
+ previousFocusNodeDOM = currentEndNodeDOM;
311
+ previousFocusOffset = currentEndOffset;
312
+ });
313
+ }
314
+ compute(editor.getEditorState());
315
+ return lexical.mergeRegister(editor.registerUpdateListener(({
316
+ editorState
317
+ }) => compute(editorState)), () => {
318
+ removeRangeListener();
319
+ });
320
+ }
321
+
322
+ /**
323
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
324
+ *
325
+ * This source code is licensed under the MIT license found in the
326
+ * LICENSE file in the root directory of this source tree.
327
+ *
328
+ */
329
+
330
+ function selectionAlwaysOnDisplay(editor, onReposition) {
331
+ let removeSelectionMark = null;
332
+ const onSelectionChange = () => {
333
+ const editorRootElement = editor.getRootElement();
334
+ if (!editorRootElement) {
335
+ return;
336
+ }
337
+
338
+ // Get selection from the proper context (shadow DOM or document)
339
+ let domSelection = null;
340
+ let current = editorRootElement;
341
+ while (current) {
342
+ if (lexical.isDocumentFragment(current.nodeType)) {
343
+ const shadowRoot = current;
344
+
345
+ // Try modern getComposedRanges API first
346
+ if ('getComposedRanges' in Selection.prototype) {
347
+ const globalSelection = window.getSelection();
348
+ if (globalSelection) {
349
+ const ranges = globalSelection.getComposedRanges({
350
+ shadowRoots: [shadowRoot]
351
+ });
352
+ if (ranges.length > 0) {
353
+ // Use the global selection with composed ranges context
354
+ domSelection = globalSelection;
355
+ }
356
+ }
357
+ }
358
+ break;
359
+ }
360
+ current = current.parentNode;
361
+ }
362
+ if (!domSelection) {
363
+ domSelection = getSelection();
364
+ }
365
+ const domAnchorNode = domSelection && domSelection.anchorNode;
366
+ const isSelectionInsideEditor = domAnchorNode !== null && editorRootElement !== null && editorRootElement.contains(domAnchorNode);
367
+ if (isSelectionInsideEditor) {
368
+ if (removeSelectionMark !== null) {
369
+ removeSelectionMark();
370
+ removeSelectionMark = null;
371
+ }
372
+ } else {
373
+ if (removeSelectionMark === null) {
374
+ removeSelectionMark = markSelection(editor, onReposition);
375
+ }
376
+ }
377
+ };
378
+
379
+ // Get the proper document context for event listeners
380
+ const editorRootElement = editor.getRootElement();
381
+ let targetDocument = document;
382
+ if (editorRootElement) {
383
+ let current = editorRootElement;
384
+ while (current) {
385
+ if (lexical.isDocumentFragment(current.nodeType)) {
386
+ targetDocument = current.ownerDocument || document;
387
+ break;
388
+ }
389
+ current = current.parentNode;
390
+ }
391
+ targetDocument = editorRootElement.ownerDocument || document;
392
+ }
393
+ targetDocument.addEventListener('selectionchange', onSelectionChange);
394
+ return () => {
395
+ if (removeSelectionMark !== null) {
396
+ removeSelectionMark();
397
+ }
398
+ targetDocument.removeEventListener('selectionchange', onSelectionChange);
399
+ };
400
+ }
401
+
402
+ // Hotfix to export these with inlined types #5918
403
+ const CAN_USE_BEFORE_INPUT = CAN_USE_BEFORE_INPUT$1;
404
+ const CAN_USE_DOM = CAN_USE_DOM$1;
405
+ const IS_ANDROID = IS_ANDROID$1;
406
+ const IS_ANDROID_CHROME = IS_ANDROID_CHROME$1;
407
+ const IS_APPLE = IS_APPLE$1;
408
+ const IS_APPLE_WEBKIT = IS_APPLE_WEBKIT$1;
409
+ const IS_CHROME = IS_CHROME$1;
410
+ const IS_FIREFOX = IS_FIREFOX$1;
411
+ const IS_IOS = IS_IOS$1;
412
+ const IS_SAFARI = IS_SAFARI$1;
413
+
414
+ /**
415
+ * Returns true if the file type matches the types passed within the acceptableMimeTypes array, false otherwise.
416
+ * The types passed must be strings and are CASE-SENSITIVE.
417
+ * eg. if file is of type 'text' and acceptableMimeTypes = ['TEXT', 'IMAGE'] the function will return false.
418
+ * @param file - The file you want to type check.
419
+ * @param acceptableMimeTypes - An array of strings of types which the file is checked against.
420
+ * @returns true if the file is an acceptable mime type, false otherwise.
421
+ */
422
+ function isMimeType(file, acceptableMimeTypes) {
423
+ for (const acceptableType of acceptableMimeTypes) {
424
+ if (file.type.startsWith(acceptableType)) {
425
+ return true;
426
+ }
427
+ }
428
+ return false;
429
+ }
430
+
431
+ /**
432
+ * Lexical File Reader with:
433
+ * 1. MIME type support
434
+ * 2. batched results (HistoryPlugin compatibility)
435
+ * 3. Order aware (respects the order when multiple Files are passed)
436
+ *
437
+ * const filesResult = await mediaFileReader(files, ['image/']);
438
+ * filesResult.forEach(file => editor.dispatchCommand('INSERT_IMAGE', \\{
439
+ * src: file.result,
440
+ * \\}));
441
+ */
442
+ function mediaFileReader(files, acceptableMimeTypes) {
443
+ const filesIterator = files[Symbol.iterator]();
444
+ return new Promise((resolve, reject) => {
445
+ const processed = [];
446
+ const handleNextFile = () => {
447
+ const {
448
+ done,
449
+ value: file
450
+ } = filesIterator.next();
451
+ if (done) {
452
+ return resolve(processed);
453
+ }
454
+ const fileReader = new FileReader();
455
+ fileReader.addEventListener('error', reject);
456
+ fileReader.addEventListener('load', () => {
457
+ const result = fileReader.result;
458
+ if (typeof result === 'string') {
459
+ processed.push({
460
+ file,
461
+ result
462
+ });
463
+ }
464
+ handleNextFile();
465
+ });
466
+ if (isMimeType(file, acceptableMimeTypes)) {
467
+ fileReader.readAsDataURL(file);
468
+ } else {
469
+ handleNextFile();
470
+ }
471
+ };
472
+ handleNextFile();
473
+ });
474
+ }
475
+ /**
476
+ * "Depth-First Search" starts at the root/top node of a tree and goes as far as it can down a branch end
477
+ * before backtracking and finding a new path. Consider solving a maze by hugging either wall, moving down a
478
+ * branch until you hit a dead-end (leaf) and backtracking to find the nearest branching path and repeat.
479
+ * It will then return all the nodes found in the search in an array of objects.
480
+ * Preorder traversal is used, meaning that nodes are listed in the order of when they are FIRST encountered.
481
+ * @param startNode - The node to start the search (inclusive), if omitted, it will start at the root node.
482
+ * @param endNode - The node to end the search (inclusive), if omitted, it will find all descendants of the startingNode. If endNode
483
+ * is an ElementNode, it will stop before visiting any of its children.
484
+ * @returns An array of objects of all the nodes found by the search, including their depth into the tree.
485
+ * \\{depth: number, node: LexicalNode\\} It will always return at least 1 node (the start node).
486
+ */
487
+ function $dfs(startNode, endNode) {
488
+ return Array.from($dfsIterator(startNode, endNode));
489
+ }
490
+
491
+ /**
492
+ * Get the adjacent caret in the same direction
493
+ *
494
+ * @param caret A caret or null
495
+ * @returns `caret.getAdjacentCaret()` or `null`
496
+ */
497
+ function $getAdjacentCaret(caret) {
498
+ return caret ? caret.getAdjacentCaret() : null;
499
+ }
500
+
501
+ /**
502
+ * $dfs iterator (right to left). Tree traversal is done on the fly as new values are requested with O(1) memory.
503
+ * @param startNode - The node to start the search, if omitted, it will start at the root node.
504
+ * @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
505
+ * @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node).
506
+ */
507
+ function $reverseDfs(startNode, endNode) {
508
+ return Array.from($reverseDfsIterator(startNode, endNode));
509
+ }
510
+
511
+ /**
512
+ * $dfs iterator (left to right). Tree traversal is done on the fly as new values are requested with O(1) memory.
513
+ * Preorder traversal is used, meaning that nodes are iterated over in the order of when they are FIRST encountered.
514
+ * @param startNode - The node to start the search (inclusive), if omitted, it will start at the root node.
515
+ * @param endNode - The node to end the search (inclusive), if omitted, it will find all descendants of the startingNode.
516
+ * If endNode is an ElementNode, the iterator will end as soon as it reaches the endNode (no children will be visited).
517
+ * @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node).
518
+ */
519
+ function $dfsIterator(startNode, endNode) {
520
+ return $dfsCaretIterator('next', startNode, endNode);
521
+ }
522
+ function $getEndCaret(startNode, direction) {
523
+ const rval = lexical.$getAdjacentSiblingOrParentSiblingCaret(lexical.$getSiblingCaret(startNode, direction));
524
+ return rval && rval[0];
525
+ }
526
+ function $dfsCaretIterator(direction, startNode, endNode) {
527
+ const root = lexical.$getRoot();
528
+ const start = startNode || root;
529
+ const startCaret = lexical.$isElementNode(start) ? lexical.$getChildCaret(start, direction) : lexical.$getSiblingCaret(start, direction);
530
+ const startDepth = $getDepth(start);
531
+ const endCaret = endNode ? lexical.$getAdjacentChildCaret(lexical.$getChildCaretOrSelf(lexical.$getSiblingCaret(endNode, direction))) || $getEndCaret(endNode, direction) : $getEndCaret(start, direction);
532
+ let depth = startDepth;
533
+ return lexical.makeStepwiseIterator({
534
+ hasNext: state => state !== null,
535
+ initial: startCaret,
536
+ map: state => ({
537
+ depth,
538
+ node: state.origin
539
+ }),
540
+ step: state => {
541
+ if (state.isSameNodeCaret(endCaret)) {
542
+ return null;
543
+ }
544
+ if (lexical.$isChildCaret(state)) {
545
+ depth++;
546
+ }
547
+ const rval = lexical.$getAdjacentSiblingOrParentSiblingCaret(state);
548
+ if (!rval || rval[0].isSameNodeCaret(endCaret)) {
549
+ return null;
550
+ }
551
+ depth += rval[1];
552
+ return rval[0];
553
+ }
554
+ });
555
+ }
556
+
557
+ /**
558
+ * Returns the Node sibling when this exists, otherwise the closest parent sibling. For example
559
+ * R -> P -> T1, T2
560
+ * -> P2
561
+ * returns T2 for node T1, P2 for node T2, and null for node P2.
562
+ * @param node LexicalNode.
563
+ * @returns An array (tuple) containing the found Lexical node and the depth difference, or null, if this node doesn't exist.
564
+ */
565
+ function $getNextSiblingOrParentSibling(node) {
566
+ const rval = lexical.$getAdjacentSiblingOrParentSiblingCaret(lexical.$getSiblingCaret(node, 'next'));
567
+ return rval && [rval[0].origin, rval[1]];
568
+ }
569
+ function $getDepth(node) {
570
+ let depth = -1;
571
+ for (let innerNode = node; innerNode !== null; innerNode = innerNode.getParent()) {
572
+ depth++;
573
+ }
574
+ return depth;
575
+ }
576
+
577
+ /**
578
+ * Performs a right-to-left preorder tree traversal.
579
+ * From the starting node it goes to the rightmost child, than backtracks to parent and finds new rightmost path.
580
+ * It will return the next node in traversal sequence after the startingNode.
581
+ * The traversal is similar to $dfs functions above, but the nodes are visited right-to-left, not left-to-right.
582
+ * @param startingNode - The node to start the search.
583
+ * @returns The next node in pre-order right to left traversal sequence or `null`, if the node does not exist
584
+ */
585
+ function $getNextRightPreorderNode(startingNode) {
586
+ const startCaret = lexical.$getChildCaretOrSelf(lexical.$getSiblingCaret(startingNode, 'previous'));
587
+ const next = lexical.$getAdjacentSiblingOrParentSiblingCaret(startCaret, 'root');
588
+ return next && next[0].origin;
589
+ }
590
+
591
+ /**
592
+ * $dfs iterator (right to left). Tree traversal is done on the fly as new values are requested with O(1) memory.
593
+ * @param startNode - The node to start the search, if omitted, it will start at the root node.
594
+ * @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
595
+ * @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node).
596
+ */
597
+ function $reverseDfsIterator(startNode, endNode) {
598
+ return $dfsCaretIterator('previous', startNode, endNode);
599
+ }
600
+
601
+ /**
602
+ * Takes a node and traverses up its ancestors (toward the root node)
603
+ * in order to find a specific type of node.
604
+ * @param node - the node to begin searching.
605
+ * @param klass - an instance of the type of node to look for.
606
+ * @returns the node of type klass that was passed, or null if none exist.
607
+ */
608
+ function $getNearestNodeOfType(node, klass) {
609
+ let parent = node;
610
+ while (parent != null) {
611
+ if (parent instanceof klass) {
612
+ return parent;
613
+ }
614
+ parent = parent.getParent();
615
+ }
616
+ return null;
617
+ }
618
+
619
+ /**
620
+ * Returns the element node of the nearest ancestor, otherwise throws an error.
621
+ * @param startNode - The starting node of the search
622
+ * @returns The ancestor node found
623
+ */
624
+ function $getNearestBlockElementAncestorOrThrow(startNode) {
625
+ const blockNode = lexical.$findMatchingParent(startNode, node => lexical.$isElementNode(node) && !node.isInline());
626
+ if (!lexical.$isElementNode(blockNode)) {
627
+ {
628
+ formatDevErrorMessage(`Expected node ${startNode.__key} to have closest block element node.`);
629
+ }
630
+ }
631
+ return blockNode;
632
+ }
633
+ /**
634
+ * Attempts to resolve nested element nodes of the same type into a single node of that type.
635
+ * It is generally used for marks/commenting
636
+ * @param editor - The lexical editor
637
+ * @param targetNode - The target for the nested element to be extracted from.
638
+ * @param cloneNode - See {@link $createMarkNode}
639
+ * @param handleOverlap - Handles any overlap between the node to extract and the targetNode
640
+ * @returns The lexical editor
641
+ */
642
+ function registerNestedElementResolver(editor, targetNode, cloneNode, handleOverlap) {
643
+ const $isTargetNode = node => {
644
+ return node instanceof targetNode;
645
+ };
646
+ const $findMatch = node => {
647
+ // First validate we don't have any children that are of the target,
648
+ // as we need to handle them first.
649
+ const children = node.getChildren();
650
+ for (let i = 0; i < children.length; i++) {
651
+ const child = children[i];
652
+ if ($isTargetNode(child)) {
653
+ return null;
654
+ }
655
+ }
656
+ let parentNode = node;
657
+ let childNode = node;
658
+ while (parentNode !== null) {
659
+ childNode = parentNode;
660
+ parentNode = parentNode.getParent();
661
+ if ($isTargetNode(parentNode)) {
662
+ return {
663
+ child: childNode,
664
+ parent: parentNode
665
+ };
666
+ }
667
+ }
668
+ return null;
669
+ };
670
+ const $elementNodeTransform = node => {
671
+ const match = $findMatch(node);
672
+ if (match !== null) {
673
+ const {
674
+ child,
675
+ parent
676
+ } = match;
677
+
678
+ // Simple path, we can move child out and siblings into a new parent.
679
+
680
+ if (child.is(node)) {
681
+ handleOverlap(parent, node);
682
+ const nextSiblings = child.getNextSiblings();
683
+ const nextSiblingsLength = nextSiblings.length;
684
+ parent.insertAfter(child);
685
+ if (nextSiblingsLength !== 0) {
686
+ const newParent = cloneNode(parent);
687
+ child.insertAfter(newParent);
688
+ for (let i = 0; i < nextSiblingsLength; i++) {
689
+ newParent.append(nextSiblings[i]);
690
+ }
691
+ }
692
+ if (!parent.canBeEmpty() && parent.getChildrenSize() === 0) {
693
+ parent.remove();
694
+ }
695
+ }
696
+ }
697
+ };
698
+ return editor.registerNodeTransform(targetNode, $elementNodeTransform);
699
+ }
700
+
701
+ /**
702
+ * Clones the editor and marks it as dirty to be reconciled. If there was a selection,
703
+ * it would be set back to its previous state, or null otherwise.
704
+ * @param editor - The lexical editor
705
+ * @param editorState - The editor's state
706
+ */
707
+ function $restoreEditorState(editor, editorState) {
708
+ const FULL_RECONCILE = 2;
709
+ const nodeMap = new Map();
710
+ const activeEditorState = editor._pendingEditorState;
711
+ for (const [key, node] of editorState._nodeMap) {
712
+ nodeMap.set(key, lexical.$cloneWithProperties(node));
713
+ }
714
+ if (activeEditorState) {
715
+ activeEditorState._nodeMap = nodeMap;
716
+ }
717
+ editor._dirtyType = FULL_RECONCILE;
718
+ const selection = editorState._selection;
719
+ lexical.$setSelection(selection === null ? null : selection.clone());
720
+ }
721
+
722
+ /**
723
+ * If the selected insertion area is the root/shadow root node (see {@link lexical!$isRootOrShadowRoot}),
724
+ * the node will be appended there, otherwise, it will be inserted before the insertion area.
725
+ * If there is no selection where the node is to be inserted, it will be appended after any current nodes
726
+ * within the tree, as a child of the root node. A paragraph will then be added after the inserted node and selected.
727
+ * @param node - The node to be inserted
728
+ * @returns The node after its insertion
729
+ */
730
+ function $insertNodeToNearestRoot(node) {
731
+ const selection = lexical.$getSelection() || lexical.$getPreviousSelection();
732
+ let initialCaret;
733
+ if (lexical.$isRangeSelection(selection)) {
734
+ initialCaret = lexical.$caretFromPoint(selection.focus, 'next');
735
+ } else {
736
+ if (selection != null) {
737
+ const nodes = selection.getNodes();
738
+ const lastNode = nodes[nodes.length - 1];
739
+ if (lastNode) {
740
+ initialCaret = lexical.$getSiblingCaret(lastNode, 'next');
741
+ }
742
+ }
743
+ initialCaret = initialCaret || lexical.$getChildCaret(lexical.$getRoot(), 'previous').getFlipped().insert(lexical.$createParagraphNode());
744
+ }
745
+ const insertCaret = $insertNodeToNearestRootAtCaret(node, initialCaret);
746
+ const adjacent = lexical.$getAdjacentChildCaret(insertCaret);
747
+ const selectionCaret = lexical.$isChildCaret(adjacent) ? lexical.$normalizeCaret(adjacent) : insertCaret;
748
+ lexical.$setSelectionFromCaretRange(lexical.$getCollapsedCaretRange(selectionCaret));
749
+ return node.getLatest();
750
+ }
751
+
752
+ /**
753
+ * If the insertion caret is the root/shadow root node (see {@link lexical!$isRootOrShadowRoot}),
754
+ * the node will be inserted there, otherwise the parent nodes will be split according to the
755
+ * given options.
756
+ * @param node - The node to be inserted
757
+ * @param caret - The location to insert or split from
758
+ * @returns The node after its insertion
759
+ */
760
+ function $insertNodeToNearestRootAtCaret(node, caret, options) {
761
+ let insertCaret = lexical.$getCaretInDirection(caret, 'next');
762
+ for (let nextCaret = insertCaret; nextCaret; nextCaret = lexical.$splitAtPointCaretNext(nextCaret, options)) {
763
+ insertCaret = nextCaret;
764
+ }
765
+ if (!!lexical.$isTextPointCaret(insertCaret)) {
766
+ formatDevErrorMessage(`$insertNodeToNearestRootAtCaret: An unattached TextNode can not be split`);
767
+ }
768
+ insertCaret.insert(node.isInline() ? lexical.$createParagraphNode().append(node) : node);
769
+ return lexical.$getCaretInDirection(lexical.$getSiblingCaret(node.getLatest(), 'next'), caret.direction);
770
+ }
771
+
772
+ /**
773
+ * Wraps the node into another node created from a createElementNode function, eg. $createParagraphNode
774
+ * @param node - Node to be wrapped.
775
+ * @param createElementNode - Creates a new lexical element to wrap the to-be-wrapped node and returns it.
776
+ * @returns A new lexical element with the previous node appended within (as a child, including its children).
777
+ */
778
+ function $wrapNodeInElement(node, createElementNode) {
779
+ const elementNode = createElementNode();
780
+ node.replace(elementNode);
781
+ elementNode.append(node);
782
+ return elementNode;
783
+ }
784
+
785
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
786
+
787
+ /**
788
+ * @param object = The instance of the type
789
+ * @param objectClass = The class of the type
790
+ * @returns Whether the object is has the same Klass of the objectClass, ignoring the difference across window (e.g. different iframes)
791
+ */
792
+ function objectKlassEquals(object, objectClass) {
793
+ return object !== null ? Object.getPrototypeOf(object).constructor.name === objectClass.name : false;
794
+ }
795
+
796
+ /**
797
+ * @deprecated Use Array filter or flatMap
798
+ *
799
+ * Filter the nodes
800
+ * @param nodes Array of nodes that needs to be filtered
801
+ * @param filterFn A filter function that returns node if the current node satisfies the condition otherwise null
802
+ * @returns Array of filtered nodes
803
+ */
804
+
805
+ function $filter(nodes, filterFn) {
806
+ const result = [];
807
+ for (let i = 0; i < nodes.length; i++) {
808
+ const node = filterFn(nodes[i]);
809
+ if (node !== null) {
810
+ result.push(node);
811
+ }
812
+ }
813
+ return result;
814
+ }
815
+ /**
816
+ * Appends the node before the first child of the parent node
817
+ * @param parent A parent node
818
+ * @param node Node that needs to be appended
819
+ */
820
+ function $insertFirst(parent, node) {
821
+ lexical.$getChildCaret(parent, 'next').insert(node);
822
+ }
823
+ let NEEDS_MANUAL_ZOOM = IS_FIREFOX || !CAN_USE_DOM ? false : undefined;
824
+ function needsManualZoom() {
825
+ if (NEEDS_MANUAL_ZOOM === undefined) {
826
+ // If the browser implements standardized CSS zoom, then the client rect
827
+ // will be wider after zoom is applied
828
+ // https://chromestatus.com/feature/5198254868529152
829
+ // https://github.com/facebook/lexical/issues/6863
830
+ const div = document.createElement('div');
831
+ div.style.cssText = 'position: absolute; opacity: 0; width: 100px; left: -1000px;';
832
+ document.body.appendChild(div);
833
+ const noZoom = div.getBoundingClientRect();
834
+ div.style.setProperty('zoom', '2');
835
+ NEEDS_MANUAL_ZOOM = div.getBoundingClientRect().width === noZoom.width;
836
+ document.body.removeChild(div);
837
+ }
838
+ return NEEDS_MANUAL_ZOOM;
839
+ }
840
+
841
+ /**
842
+ * Calculates the zoom level of an element as a result of using
843
+ * css zoom property. For browsers that implement standardized CSS
844
+ * zoom (Firefox, Chrome >= 128), this will always return 1.
845
+ * @param element
846
+ * @param useManualZoom - If true, always use zoom level will be calculated manually, otherwise it will be calculated on as needed basis.
847
+ */
848
+ function calculateZoomLevel(element, useManualZoom = false) {
849
+ let zoom = 1;
850
+ if (needsManualZoom() || useManualZoom) {
851
+ while (element) {
852
+ zoom *= Number(window.getComputedStyle(element).getPropertyValue('zoom'));
853
+ element = element.parentElement;
854
+ }
855
+ }
856
+ return zoom;
857
+ }
858
+
859
+ /**
860
+ * Checks if the editor is a nested editor created by LexicalNestedComposer
861
+ */
862
+ function $isEditorIsNestedEditor(editor) {
863
+ return editor._parentEditor !== null;
864
+ }
865
+
866
+ /**
867
+ * A depth first last-to-first traversal of root that stops at each node that matches
868
+ * $predicate and ensures that its parent is root. This is typically used to discard
869
+ * invalid or unsupported wrapping nodes. For example, a TableNode must only have
870
+ * TableRowNode as children, but an importer might add invalid nodes based on
871
+ * caption, tbody, thead, etc. and this will unwrap and discard those.
872
+ *
873
+ * @param root The root to start the traversal
874
+ * @param $predicate Should return true for nodes that are permitted to be children of root
875
+ * @returns true if this unwrapped or removed any nodes
876
+ */
877
+ function $unwrapAndFilterDescendants(root, $predicate) {
878
+ return $unwrapAndFilterDescendantsImpl(root, $predicate, null);
879
+ }
880
+ function $unwrapAndFilterDescendantsImpl(root, $predicate, $onSuccess) {
881
+ let didMutate = false;
882
+ for (const node of $lastToFirstIterator(root)) {
883
+ if ($predicate(node)) {
884
+ if ($onSuccess !== null) {
885
+ $onSuccess(node);
886
+ }
887
+ continue;
888
+ }
889
+ didMutate = true;
890
+ if (lexical.$isElementNode(node)) {
891
+ $unwrapAndFilterDescendantsImpl(node, $predicate, $onSuccess || (child => node.insertAfter(child)));
892
+ }
893
+ node.remove();
894
+ }
895
+ return didMutate;
896
+ }
897
+
898
+ /**
899
+ * A depth first traversal of the children array that stops at and collects
900
+ * each node that `$predicate` matches. This is typically used to discard
901
+ * invalid or unsupported wrapping nodes on a children array in the `after`
902
+ * of an {@link lexical!DOMConversionOutput}. For example, a TableNode must only have
903
+ * TableRowNode as children, but an importer might add invalid nodes based on
904
+ * caption, tbody, thead, etc. and this will unwrap and discard those.
905
+ *
906
+ * This function is read-only and performs no mutation operations, which makes
907
+ * it suitable for import and export purposes but likely not for any in-place
908
+ * mutation. You should use {@link $unwrapAndFilterDescendants} for in-place
909
+ * mutations such as node transforms.
910
+ *
911
+ * @param children The children to traverse
912
+ * @param $predicate Should return true for nodes that are permitted to be children of root
913
+ * @returns The children or their descendants that match $predicate
914
+ */
915
+
916
+ function $descendantsMatching(children, $predicate) {
917
+ const result = [];
918
+ const stack = Array.from(children).reverse();
919
+ for (let child = stack.pop(); child !== undefined; child = stack.pop()) {
920
+ if ($predicate(child)) {
921
+ result.push(child);
922
+ } else if (lexical.$isElementNode(child)) {
923
+ for (const grandchild of $lastToFirstIterator(child)) {
924
+ stack.push(grandchild);
925
+ }
926
+ }
927
+ }
928
+ return result;
929
+ }
930
+
931
+ /**
932
+ * Return an iterator that yields each child of node from first to last, taking
933
+ * care to preserve the next sibling before yielding the value in case the caller
934
+ * removes the yielded node.
935
+ *
936
+ * @param node The node whose children to iterate
937
+ * @returns An iterator of the node's children
938
+ */
939
+ function $firstToLastIterator(node) {
940
+ return $childIterator(lexical.$getChildCaret(node, 'next'));
941
+ }
942
+
943
+ /**
944
+ * Return an iterator that yields each child of node from last to first, taking
945
+ * care to preserve the previous sibling before yielding the value in case the caller
946
+ * removes the yielded node.
947
+ *
948
+ * @param node The node whose children to iterate
949
+ * @returns An iterator of the node's children
950
+ */
951
+ function $lastToFirstIterator(node) {
952
+ return $childIterator(lexical.$getChildCaret(node, 'previous'));
953
+ }
954
+ function $childIterator(startCaret) {
955
+ const seen = new Set() ;
956
+ return lexical.makeStepwiseIterator({
957
+ hasNext: lexical.$isSiblingCaret,
958
+ initial: startCaret.getAdjacentCaret(),
959
+ map: caret => {
960
+ const origin = caret.origin.getLatest();
961
+ if (seen !== null) {
962
+ const key = origin.getKey();
963
+ if (!!seen.has(key)) {
964
+ formatDevErrorMessage(`$childIterator: Cycle detected, node with key ${String(key)} has already been traversed`);
965
+ }
966
+ seen.add(key);
967
+ }
968
+ return origin;
969
+ },
970
+ step: caret => caret.getAdjacentCaret()
971
+ });
972
+ }
973
+
974
+ /**
975
+ * Replace this node with its children
976
+ *
977
+ * @param node The ElementNode to unwrap and remove
978
+ */
979
+ function $unwrapNode(node) {
980
+ lexical.$rewindSiblingCaret(lexical.$getSiblingCaret(node, 'next')).splice(1, node.getChildren());
981
+ }
982
+
983
+ /**
984
+ * A wrapper that creates bound functions and methods for the
985
+ * StateConfig to save some boilerplate when defining methods
986
+ * or exporting only the accessors from your modules rather
987
+ * than exposing the StateConfig directly.
988
+ */
989
+
990
+ /**
991
+ * EXPERIMENTAL
992
+ *
993
+ * A convenience interface for working with {@link $getState} and
994
+ * {@link $setState}.
995
+ *
996
+ * @param stateConfig The stateConfig to wrap with convenience functionality
997
+ * @returns a StateWrapper
998
+ */
999
+ function makeStateWrapper(stateConfig) {
1000
+ const $get = node => lexical.$getState(node, stateConfig);
1001
+ const $set = (node, valueOrUpdater) => lexical.$setState(node, stateConfig, valueOrUpdater);
1002
+ return {
1003
+ $get,
1004
+ $set,
1005
+ accessors: [$get, $set],
1006
+ makeGetterMethod: () => function $getter() {
1007
+ return $get(this);
1008
+ },
1009
+ makeSetterMethod: () => function $setter(valueOrUpdater) {
1010
+ return $set(this, valueOrUpdater);
1011
+ },
1012
+ stateConfig
1013
+ };
1014
+ }
1015
+
1016
+ exports.$findMatchingParent = lexical.$findMatchingParent;
1017
+ exports.$getAdjacentSiblingOrParentSiblingCaret = lexical.$getAdjacentSiblingOrParentSiblingCaret;
1018
+ exports.$splitNode = lexical.$splitNode;
1019
+ exports.addClassNamesToElement = lexical.addClassNamesToElement;
1020
+ exports.isBlockDomNode = lexical.isBlockDomNode;
1021
+ exports.isHTMLAnchorElement = lexical.isHTMLAnchorElement;
1022
+ exports.isHTMLElement = lexical.isHTMLElement;
1023
+ exports.isInlineDomNode = lexical.isInlineDomNode;
1024
+ exports.mergeRegister = lexical.mergeRegister;
1025
+ exports.removeClassNamesFromElement = lexical.removeClassNamesFromElement;
1026
+ exports.$descendantsMatching = $descendantsMatching;
1027
+ exports.$dfs = $dfs;
1028
+ exports.$dfsIterator = $dfsIterator;
1029
+ exports.$filter = $filter;
1030
+ exports.$firstToLastIterator = $firstToLastIterator;
1031
+ exports.$getAdjacentCaret = $getAdjacentCaret;
1032
+ exports.$getDepth = $getDepth;
1033
+ exports.$getNearestBlockElementAncestorOrThrow = $getNearestBlockElementAncestorOrThrow;
1034
+ exports.$getNearestNodeOfType = $getNearestNodeOfType;
1035
+ exports.$getNextRightPreorderNode = $getNextRightPreorderNode;
1036
+ exports.$getNextSiblingOrParentSibling = $getNextSiblingOrParentSibling;
1037
+ exports.$insertFirst = $insertFirst;
1038
+ exports.$insertNodeToNearestRoot = $insertNodeToNearestRoot;
1039
+ exports.$insertNodeToNearestRootAtCaret = $insertNodeToNearestRootAtCaret;
1040
+ exports.$isEditorIsNestedEditor = $isEditorIsNestedEditor;
1041
+ exports.$lastToFirstIterator = $lastToFirstIterator;
1042
+ exports.$restoreEditorState = $restoreEditorState;
1043
+ exports.$reverseDfs = $reverseDfs;
1044
+ exports.$reverseDfsIterator = $reverseDfsIterator;
1045
+ exports.$unwrapAndFilterDescendants = $unwrapAndFilterDescendants;
1046
+ exports.$unwrapNode = $unwrapNode;
1047
+ exports.$wrapNodeInElement = $wrapNodeInElement;
1048
+ exports.CAN_USE_BEFORE_INPUT = CAN_USE_BEFORE_INPUT;
1049
+ exports.CAN_USE_DOM = CAN_USE_DOM;
1050
+ exports.IS_ANDROID = IS_ANDROID;
1051
+ exports.IS_ANDROID_CHROME = IS_ANDROID_CHROME;
1052
+ exports.IS_APPLE = IS_APPLE;
1053
+ exports.IS_APPLE_WEBKIT = IS_APPLE_WEBKIT;
1054
+ exports.IS_CHROME = IS_CHROME;
1055
+ exports.IS_FIREFOX = IS_FIREFOX;
1056
+ exports.IS_IOS = IS_IOS;
1057
+ exports.IS_SAFARI = IS_SAFARI;
1058
+ exports.calculateZoomLevel = calculateZoomLevel;
1059
+ exports.isMimeType = isMimeType;
1060
+ exports.makeStateWrapper = makeStateWrapper;
1061
+ exports.markSelection = markSelection;
1062
+ exports.mediaFileReader = mediaFileReader;
1063
+ exports.objectKlassEquals = objectKlassEquals;
1064
+ exports.positionNodeOnRange = mlcPositionNodeOnRange;
1065
+ exports.registerNestedElementResolver = registerNestedElementResolver;
1066
+ exports.selectionAlwaysOnDisplay = selectionAlwaysOnDisplay;