@react-fabric/lexical 1.0.0-beta.46 → 1.0.0-beta.48
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.
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +29 -19
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +9 -9
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +142 -75
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +1 -1
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +25 -21
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +10 -3
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +1 -1
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +147 -103
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +1 -1
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +10 -10
- package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +254 -178
- package/dist/cjs/node_modules/@lexical/list/LexicalList.js +1 -1
- package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +19 -16
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +53 -29
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +10 -10
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +176 -194
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +1 -1
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +25 -18
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +1925 -793
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +1 -1
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +48 -40
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +426 -144
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +11 -1
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +46 -32
- package/dist/cjs/node_modules/prismjs/prism.js +1 -1
- package/dist/cjs/packages/lexical/src/editor/LexicalEditor.js +1 -1
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +29 -19
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +139 -76
- package/dist/esm/node_modules/@lexical/code/LexicalCode.js +1 -1
- package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +11 -4
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +1 -1
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +149 -105
- package/dist/esm/node_modules/@lexical/link/LexicalLink.js +1 -1
- package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +253 -180
- package/dist/esm/node_modules/@lexical/list/LexicalList.js +1 -1
- package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +54 -30
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +171 -196
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +1 -1
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +1920 -796
- package/dist/esm/node_modules/@lexical/table/LexicalTable.js +1 -1
- package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +413 -145
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +7 -2
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +3 -3
- package/dist/esm/node_modules/prismjs/prism.js +1 -1
- package/dist/esm/packages/lexical/src/editor/LexicalEditor.js +1 -1
- package/package.json +7 -8
- package/src/components/StickyComponent.tsx +1 -1
- package/src/editor/LexicalEditor.tsx +25 -21
- package/src/plugins/ToolbarPlugin.tsx +20 -4
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import { $getRoot, $isElementNode, $cloneWithProperties, $setSelection, $getSelection, $getPreviousSelection, $isRangeSelection, $isRootOrShadowRoot, $isTextNode, $splitNode, $createParagraphNode,
|
|
3
|
+
import { $getRoot, $isElementNode, $getChildCaret, $getSiblingCaret, $getAdjacentChildCaret, $getChildCaretOrSelf, makeStepwiseIterator, $isChildCaret, $cloneWithProperties, $setSelection, $getSelection, $getPreviousSelection, $isRangeSelection, $isRootOrShadowRoot, $getChildCaretAtIndex, $isTextNode, $splitNode, $createParagraphNode, $isSiblingCaret, $rewindSiblingCaret, isHTMLElement, $getState, $setState, getDOMTextNode } from 'lexical';
|
|
4
4
|
export { $splitNode, isBlockDomNode, isHTMLAnchorElement, isHTMLElement, isInlineDomNode } from 'lexical';
|
|
5
5
|
import { createRectsFromDOMRange } from '../selection/LexicalSelection.js';
|
|
6
6
|
|
|
@@ -13,6 +13,20 @@ import { createRectsFromDOMRange } from '../selection/LexicalSelection.js';
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
18
|
+
*
|
|
19
|
+
* This source code is licensed under the MIT license found in the
|
|
20
|
+
* LICENSE file in the root directory of this source tree.
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
// Do not require this module directly! Use normal `invariant` calls.
|
|
25
|
+
|
|
26
|
+
function formatDevErrorMessage(message) {
|
|
27
|
+
throw new Error(message);
|
|
28
|
+
}
|
|
29
|
+
|
|
16
30
|
/**
|
|
17
31
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
18
32
|
*
|
|
@@ -122,41 +136,49 @@ function px(value) {
|
|
|
122
136
|
return `${value}px`;
|
|
123
137
|
}
|
|
124
138
|
|
|
125
|
-
/**
|
|
126
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
127
|
-
*
|
|
128
|
-
* This source code is licensed under the MIT license found in the
|
|
129
|
-
* LICENSE file in the root directory of this source tree.
|
|
130
|
-
*
|
|
131
|
-
*/
|
|
132
|
-
|
|
133
139
|
const mutationObserverConfig = {
|
|
134
140
|
attributes: true,
|
|
135
141
|
characterData: true,
|
|
136
142
|
childList: true,
|
|
137
143
|
subtree: true
|
|
138
144
|
};
|
|
139
|
-
function
|
|
145
|
+
function prependDOMNode(parent, node) {
|
|
146
|
+
parent.insertBefore(node, parent.firstChild);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Place one or multiple newly created Nodes at the passed Range's position.
|
|
151
|
+
* Multiple nodes will only be created when the Range spans multiple lines (aka
|
|
152
|
+
* client rects).
|
|
153
|
+
*
|
|
154
|
+
* This function can come particularly useful to highlight particular parts of
|
|
155
|
+
* the text without interfering with the EditorState, that will often replicate
|
|
156
|
+
* the state across collab and clipboard.
|
|
157
|
+
*
|
|
158
|
+
* This function accounts for DOM updates which can modify the passed Range.
|
|
159
|
+
* Hence, the function return to remove the listener.
|
|
160
|
+
*/
|
|
161
|
+
function mlcPositionNodeOnRange(editor, range, onReposition) {
|
|
140
162
|
let rootDOMNode = null;
|
|
141
163
|
let parentDOMNode = null;
|
|
142
164
|
let observer = null;
|
|
143
165
|
let lastNodes = [];
|
|
144
166
|
const wrapperNode = document.createElement('div');
|
|
167
|
+
wrapperNode.style.position = 'relative';
|
|
145
168
|
function position() {
|
|
146
169
|
if (!(rootDOMNode !== null)) {
|
|
147
|
-
|
|
170
|
+
formatDevErrorMessage(`Unexpected null rootDOMNode`);
|
|
148
171
|
}
|
|
149
172
|
if (!(parentDOMNode !== null)) {
|
|
150
|
-
|
|
173
|
+
formatDevErrorMessage(`Unexpected null parentDOMNode`);
|
|
151
174
|
}
|
|
152
175
|
const {
|
|
153
|
-
left:
|
|
154
|
-
top:
|
|
155
|
-
} =
|
|
156
|
-
const parentDOMNode_ = parentDOMNode;
|
|
176
|
+
left: parentLeft,
|
|
177
|
+
top: parentTop
|
|
178
|
+
} = parentDOMNode.getBoundingClientRect();
|
|
157
179
|
const rects = createRectsFromDOMRange(editor, range);
|
|
158
180
|
if (!wrapperNode.isConnected) {
|
|
159
|
-
|
|
181
|
+
prependDOMNode(parentDOMNode, wrapperNode);
|
|
160
182
|
}
|
|
161
183
|
let hasRepositioned = false;
|
|
162
184
|
for (let i = 0; i < rects.length; i++) {
|
|
@@ -169,12 +191,12 @@ function positionNodeOnRange(editor, range, onReposition) {
|
|
|
169
191
|
rectNodeStyle.position = 'absolute';
|
|
170
192
|
hasRepositioned = true;
|
|
171
193
|
}
|
|
172
|
-
const left = px(rect.left -
|
|
194
|
+
const left = px(rect.left - parentLeft);
|
|
173
195
|
if (rectNodeStyle.left !== left) {
|
|
174
196
|
rectNodeStyle.left = left;
|
|
175
197
|
hasRepositioned = true;
|
|
176
198
|
}
|
|
177
|
-
const top = px(rect.top -
|
|
199
|
+
const top = px(rect.top - parentTop);
|
|
178
200
|
if (rectNodeStyle.top !== top) {
|
|
179
201
|
rectNode.style.top = top;
|
|
180
202
|
hasRepositioned = true;
|
|
@@ -221,7 +243,7 @@ function positionNodeOnRange(editor, range, onReposition) {
|
|
|
221
243
|
return stop();
|
|
222
244
|
}
|
|
223
245
|
const currentParentDOMNode = currentRootDOMNode.parentElement;
|
|
224
|
-
if (!(currentParentDOMNode
|
|
246
|
+
if (!isHTMLElement(currentParentDOMNode)) {
|
|
225
247
|
return stop();
|
|
226
248
|
}
|
|
227
249
|
stop();
|
|
@@ -258,10 +280,41 @@ function positionNodeOnRange(editor, range, onReposition) {
|
|
|
258
280
|
*
|
|
259
281
|
*/
|
|
260
282
|
|
|
283
|
+
function rangeTargetFromPoint(point, node, dom) {
|
|
284
|
+
if (point.type === 'text' || !$isElementNode(node)) {
|
|
285
|
+
const textDOM = getDOMTextNode(dom) || dom;
|
|
286
|
+
return [textDOM, point.offset];
|
|
287
|
+
} else {
|
|
288
|
+
const slot = node.getDOMSlot(dom);
|
|
289
|
+
return [slot.element, slot.getFirstChildOffset() + point.offset];
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
function rangeFromPoints(editor, anchor, anchorNode, anchorDOM, focus, focusNode, focusDOM) {
|
|
293
|
+
const editorDocument = editor._window ? editor._window.document : document;
|
|
294
|
+
const range = editorDocument.createRange();
|
|
295
|
+
if (focusNode.isBefore(anchorNode)) {
|
|
296
|
+
range.setStart(...rangeTargetFromPoint(focus, focusNode, focusDOM));
|
|
297
|
+
range.setEnd(...rangeTargetFromPoint(anchor, anchorNode, anchorDOM));
|
|
298
|
+
} else {
|
|
299
|
+
range.setStart(...rangeTargetFromPoint(anchor, anchorNode, anchorDOM));
|
|
300
|
+
range.setEnd(...rangeTargetFromPoint(focus, focusNode, focusDOM));
|
|
301
|
+
}
|
|
302
|
+
return range;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Place one or multiple newly created Nodes at the current selection. Multiple
|
|
306
|
+
* nodes will only be created when the selection spans multiple lines (aka
|
|
307
|
+
* client rects).
|
|
308
|
+
*
|
|
309
|
+
* This function can come useful when you want to show the selection but the
|
|
310
|
+
* editor has been focused away.
|
|
311
|
+
*/
|
|
261
312
|
function markSelection(editor, onReposition) {
|
|
262
313
|
let previousAnchorNode = null;
|
|
314
|
+
let previousAnchorNodeDOM = null;
|
|
263
315
|
let previousAnchorOffset = null;
|
|
264
316
|
let previousFocusNode = null;
|
|
317
|
+
let previousFocusNodeDOM = null;
|
|
265
318
|
let previousFocusOffset = null;
|
|
266
319
|
let removeRangeListener = () => {};
|
|
267
320
|
function compute(editorState) {
|
|
@@ -289,41 +342,13 @@ function markSelection(editor, onReposition) {
|
|
|
289
342
|
const currentFocusOffset = focus.offset;
|
|
290
343
|
const currentAnchorNodeDOM = editor.getElementByKey(currentAnchorNodeKey);
|
|
291
344
|
const currentFocusNodeDOM = editor.getElementByKey(currentFocusNodeKey);
|
|
292
|
-
const differentAnchorDOM = previousAnchorNode === null || currentAnchorNodeDOM
|
|
293
|
-
const differentFocusDOM = previousFocusNode === null || currentFocusNodeDOM
|
|
294
|
-
if (differentAnchorDOM || differentFocusDOM) {
|
|
295
|
-
const
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
const range = document.createRange();
|
|
300
|
-
let firstHTMLElement;
|
|
301
|
-
let firstOffset;
|
|
302
|
-
let lastHTMLElement;
|
|
303
|
-
let lastOffset;
|
|
304
|
-
if (focus.isBefore(anchor)) {
|
|
305
|
-
firstHTMLElement = focusHTMLElement;
|
|
306
|
-
firstOffset = focus.offset;
|
|
307
|
-
lastHTMLElement = anchorHTMLElement;
|
|
308
|
-
lastOffset = anchor.offset;
|
|
309
|
-
} else {
|
|
310
|
-
firstHTMLElement = anchorHTMLElement;
|
|
311
|
-
firstOffset = anchor.offset;
|
|
312
|
-
lastHTMLElement = focusHTMLElement;
|
|
313
|
-
lastOffset = focus.offset;
|
|
314
|
-
}
|
|
315
|
-
const firstTextNode = firstHTMLElement.firstChild;
|
|
316
|
-
if (!(firstTextNode !== null)) {
|
|
317
|
-
throw Error(`Expected text node to be first child of span`);
|
|
318
|
-
}
|
|
319
|
-
const lastTextNode = lastHTMLElement.firstChild;
|
|
320
|
-
if (!(lastTextNode !== null)) {
|
|
321
|
-
throw Error(`Expected text node to be first child of span`);
|
|
322
|
-
}
|
|
323
|
-
range.setStart(firstTextNode, firstOffset);
|
|
324
|
-
range.setEnd(lastTextNode, lastOffset);
|
|
325
|
-
removeRangeListener();
|
|
326
|
-
removeRangeListener = positionNodeOnRange(editor, range, domNodes => {
|
|
345
|
+
const differentAnchorDOM = previousAnchorNode === null || currentAnchorNodeDOM !== previousAnchorNodeDOM || currentAnchorOffset !== previousAnchorOffset || currentAnchorNodeKey !== previousAnchorNode.getKey();
|
|
346
|
+
const differentFocusDOM = previousFocusNode === null || currentFocusNodeDOM !== previousFocusNodeDOM || currentFocusOffset !== previousFocusOffset || currentFocusNodeKey !== previousFocusNode.getKey();
|
|
347
|
+
if ((differentAnchorDOM || differentFocusDOM) && currentAnchorNodeDOM !== null && currentFocusNodeDOM !== null) {
|
|
348
|
+
const range = rangeFromPoints(editor, anchor, currentAnchorNode, currentAnchorNodeDOM, focus, currentFocusNode, currentFocusNodeDOM);
|
|
349
|
+
removeRangeListener();
|
|
350
|
+
removeRangeListener = mlcPositionNodeOnRange(editor, range, domNodes => {
|
|
351
|
+
if (onReposition === undefined) {
|
|
327
352
|
for (const domNode of domNodes) {
|
|
328
353
|
const domNodeStyle = domNode.style;
|
|
329
354
|
if (domNodeStyle.background !== 'Highlight') {
|
|
@@ -332,12 +357,6 @@ function markSelection(editor, onReposition) {
|
|
|
332
357
|
if (domNodeStyle.color !== 'HighlightText') {
|
|
333
358
|
domNodeStyle.color = 'HighlightText';
|
|
334
359
|
}
|
|
335
|
-
if (domNodeStyle.zIndex !== '-1') {
|
|
336
|
-
domNodeStyle.zIndex = '-1';
|
|
337
|
-
}
|
|
338
|
-
if (domNodeStyle.pointerEvents !== 'none') {
|
|
339
|
-
domNodeStyle.pointerEvents = 'none';
|
|
340
|
-
}
|
|
341
360
|
if (domNodeStyle.marginTop !== px(-1.5)) {
|
|
342
361
|
domNodeStyle.marginTop = px(-1.5);
|
|
343
362
|
}
|
|
@@ -348,22 +367,23 @@ function markSelection(editor, onReposition) {
|
|
|
348
367
|
domNodeStyle.paddingBottom = px(0);
|
|
349
368
|
}
|
|
350
369
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
}
|
|
370
|
+
} else {
|
|
371
|
+
onReposition(domNodes);
|
|
372
|
+
}
|
|
373
|
+
});
|
|
356
374
|
}
|
|
357
375
|
previousAnchorNode = currentAnchorNode;
|
|
376
|
+
previousAnchorNodeDOM = currentAnchorNodeDOM;
|
|
358
377
|
previousAnchorOffset = currentAnchorOffset;
|
|
359
378
|
previousFocusNode = currentFocusNode;
|
|
379
|
+
previousFocusNodeDOM = currentFocusNodeDOM;
|
|
360
380
|
previousFocusOffset = currentFocusOffset;
|
|
361
381
|
});
|
|
362
382
|
}
|
|
363
383
|
compute(editor.getEditorState());
|
|
364
384
|
return mergeRegister(editor.registerUpdateListener(({
|
|
365
385
|
editorState
|
|
366
|
-
}) => compute(editorState)),
|
|
386
|
+
}) => compute(editorState)), () => {
|
|
367
387
|
removeRangeListener();
|
|
368
388
|
});
|
|
369
389
|
}
|
|
@@ -376,6 +396,33 @@ function markSelection(editor, onReposition) {
|
|
|
376
396
|
*
|
|
377
397
|
*/
|
|
378
398
|
|
|
399
|
+
function selectionAlwaysOnDisplay(editor) {
|
|
400
|
+
let removeSelectionMark = null;
|
|
401
|
+
const onSelectionChange = () => {
|
|
402
|
+
const domSelection = getSelection();
|
|
403
|
+
const domAnchorNode = domSelection && domSelection.anchorNode;
|
|
404
|
+
const editorRootElement = editor.getRootElement();
|
|
405
|
+
const isSelectionInsideEditor = domAnchorNode !== null && editorRootElement !== null && editorRootElement.contains(domAnchorNode);
|
|
406
|
+
if (isSelectionInsideEditor) {
|
|
407
|
+
if (removeSelectionMark !== null) {
|
|
408
|
+
removeSelectionMark();
|
|
409
|
+
removeSelectionMark = null;
|
|
410
|
+
}
|
|
411
|
+
} else {
|
|
412
|
+
if (removeSelectionMark === null) {
|
|
413
|
+
removeSelectionMark = markSelection(editor);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
document.addEventListener('selectionchange', onSelectionChange);
|
|
418
|
+
return () => {
|
|
419
|
+
if (removeSelectionMark !== null) {
|
|
420
|
+
removeSelectionMark();
|
|
421
|
+
}
|
|
422
|
+
document.removeEventListener('selectionchange', onSelectionChange);
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
379
426
|
// Hotfix to export these with inlined types #5918
|
|
380
427
|
const CAN_USE_BEFORE_INPUT = CAN_USE_BEFORE_INPUT$1;
|
|
381
428
|
const CAN_USE_DOM = CAN_USE_DOM$1;
|
|
@@ -387,6 +434,7 @@ const IS_CHROME = IS_CHROME$1;
|
|
|
387
434
|
const IS_FIREFOX = IS_FIREFOX$1;
|
|
388
435
|
const IS_IOS = IS_IOS$1;
|
|
389
436
|
const IS_SAFARI = IS_SAFARI$1;
|
|
437
|
+
|
|
390
438
|
/**
|
|
391
439
|
* Takes an HTML element and adds the classNames passed within an array,
|
|
392
440
|
* ignoring any non-string types. A space can be used to add multiple classes
|
|
@@ -478,57 +526,99 @@ function mediaFileReader(files, acceptableMimeTypes) {
|
|
|
478
526
|
handleNextFile();
|
|
479
527
|
});
|
|
480
528
|
}
|
|
481
|
-
|
|
482
529
|
/**
|
|
483
530
|
* "Depth-First Search" starts at the root/top node of a tree and goes as far as it can down a branch end
|
|
484
531
|
* before backtracking and finding a new path. Consider solving a maze by hugging either wall, moving down a
|
|
485
532
|
* branch until you hit a dead-end (leaf) and backtracking to find the nearest branching path and repeat.
|
|
486
533
|
* It will then return all the nodes found in the search in an array of objects.
|
|
487
|
-
* @param
|
|
488
|
-
* @param
|
|
534
|
+
* @param startNode - The node to start the search, if omitted, it will start at the root node.
|
|
535
|
+
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
|
|
489
536
|
* @returns An array of objects of all the nodes found by the search, including their depth into the tree.
|
|
490
|
-
* \\{depth: number, node: LexicalNode\\} It will always return at least 1 node (the
|
|
491
|
-
*/
|
|
492
|
-
function $dfs(
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
537
|
+
* \\{depth: number, node: LexicalNode\\} It will always return at least 1 node (the start node).
|
|
538
|
+
*/
|
|
539
|
+
function $dfs(startNode, endNode) {
|
|
540
|
+
return Array.from($dfsIterator(startNode, endNode));
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Get the adjacent caret in the same direction
|
|
545
|
+
*
|
|
546
|
+
* @param caret A caret or null
|
|
547
|
+
* @returns `caret.getAdjacentCaret()` or `null`
|
|
548
|
+
*/
|
|
549
|
+
function $getAdjacentCaret(caret) {
|
|
550
|
+
return caret ? caret.getAdjacentCaret() : null;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* $dfs iterator (right to left). Tree traversal is done on the fly as new values are requested with O(1) memory.
|
|
555
|
+
* @param startNode - The node to start the search, if omitted, it will start at the root node.
|
|
556
|
+
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
|
|
557
|
+
* @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node).
|
|
558
|
+
*/
|
|
559
|
+
function $reverseDfs(startNode, endNode) {
|
|
560
|
+
return Array.from($reverseDfsIterator(startNode, endNode));
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* $dfs iterator (left to right). Tree traversal is done on the fly as new values are requested with O(1) memory.
|
|
565
|
+
* @param startNode - The node to start the search, if omitted, it will start at the root node.
|
|
566
|
+
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
|
|
567
|
+
* @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node).
|
|
568
|
+
*/
|
|
569
|
+
function $dfsIterator(startNode, endNode) {
|
|
570
|
+
return $dfsCaretIterator('next', startNode, endNode);
|
|
571
|
+
}
|
|
572
|
+
function $getEndCaret(startNode, direction) {
|
|
573
|
+
const rval = $getAdjacentSiblingOrParentSiblingCaret($getSiblingCaret(startNode, direction));
|
|
574
|
+
return rval && rval[0];
|
|
575
|
+
}
|
|
576
|
+
function $dfsCaretIterator(direction, startNode, endNode) {
|
|
577
|
+
const root = $getRoot();
|
|
578
|
+
const start = startNode || root;
|
|
579
|
+
const startCaret = $isElementNode(start) ? $getChildCaret(start, direction) : $getSiblingCaret(start, direction);
|
|
580
|
+
const startDepth = $getDepth(start);
|
|
581
|
+
const endCaret = endNode ? $getAdjacentChildCaret($getChildCaretOrSelf($getSiblingCaret(endNode, direction))) : $getEndCaret(start, direction);
|
|
582
|
+
let depth = startDepth;
|
|
583
|
+
return makeStepwiseIterator({
|
|
584
|
+
hasNext: state => state !== null,
|
|
585
|
+
initial: startCaret,
|
|
586
|
+
map: state => ({
|
|
500
587
|
depth,
|
|
501
|
-
node
|
|
502
|
-
})
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
depth--;
|
|
514
|
-
} else {
|
|
515
|
-
node = sibling;
|
|
516
|
-
}
|
|
588
|
+
node: state.origin
|
|
589
|
+
}),
|
|
590
|
+
step: state => {
|
|
591
|
+
if (state.isSameNodeCaret(endCaret)) {
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
if ($isChildCaret(state)) {
|
|
595
|
+
depth++;
|
|
596
|
+
}
|
|
597
|
+
const rval = $getAdjacentSiblingOrParentSiblingCaret(state);
|
|
598
|
+
if (!rval || rval[0].isSameNodeCaret(endCaret)) {
|
|
599
|
+
return null;
|
|
517
600
|
}
|
|
601
|
+
depth += rval[1];
|
|
602
|
+
return rval[0];
|
|
518
603
|
}
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* Returns the Node sibling when this exists, otherwise the closest parent sibling. For example
|
|
609
|
+
* R -> P -> T1, T2
|
|
610
|
+
* -> P2
|
|
611
|
+
* returns T2 for node T1, P2 for node T2, and null for node P2.
|
|
612
|
+
* @param node LexicalNode.
|
|
613
|
+
* @returns An array (tuple) containing the found Lexical node and the depth difference, or null, if this node doesn't exist.
|
|
614
|
+
*/
|
|
615
|
+
function $getNextSiblingOrParentSibling(node) {
|
|
616
|
+
const rval = $getAdjacentSiblingOrParentSiblingCaret($getSiblingCaret(node, 'next'));
|
|
617
|
+
return rval && [rval[0].origin, rval[1]];
|
|
527
618
|
}
|
|
528
619
|
function $getDepth(node) {
|
|
529
|
-
let
|
|
530
|
-
let
|
|
531
|
-
while ((innerNode = innerNode.getParent()) !== null) {
|
|
620
|
+
let depth = -1;
|
|
621
|
+
for (let innerNode = node; innerNode !== null; innerNode = innerNode.getParent()) {
|
|
532
622
|
depth++;
|
|
533
623
|
}
|
|
534
624
|
return depth;
|
|
@@ -536,28 +626,26 @@ function $getDepth(node) {
|
|
|
536
626
|
|
|
537
627
|
/**
|
|
538
628
|
* Performs a right-to-left preorder tree traversal.
|
|
539
|
-
* From the starting node it goes to the rightmost child, than backtracks to
|
|
629
|
+
* From the starting node it goes to the rightmost child, than backtracks to parent and finds new rightmost path.
|
|
540
630
|
* It will return the next node in traversal sequence after the startingNode.
|
|
541
631
|
* The traversal is similar to $dfs functions above, but the nodes are visited right-to-left, not left-to-right.
|
|
542
632
|
* @param startingNode - The node to start the search.
|
|
543
633
|
* @returns The next node in pre-order right to left traversal sequence or `null`, if the node does not exist
|
|
544
634
|
*/
|
|
545
635
|
function $getNextRightPreorderNode(startingNode) {
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
}
|
|
560
|
-
return node;
|
|
636
|
+
const startCaret = $getChildCaretOrSelf($getSiblingCaret(startingNode, 'previous'));
|
|
637
|
+
const next = $getAdjacentSiblingOrParentSiblingCaret(startCaret, 'root');
|
|
638
|
+
return next && next[0].origin;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* $dfs iterator (right to left). Tree traversal is done on the fly as new values are requested with O(1) memory.
|
|
643
|
+
* @param startNode - The node to start the search, if omitted, it will start at the root node.
|
|
644
|
+
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
|
|
645
|
+
* @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node).
|
|
646
|
+
*/
|
|
647
|
+
function $reverseDfsIterator(startNode, endNode) {
|
|
648
|
+
return $dfsCaretIterator('previous', startNode, endNode);
|
|
561
649
|
}
|
|
562
650
|
|
|
563
651
|
/**
|
|
@@ -587,7 +675,7 @@ function $getNearestBlockElementAncestorOrThrow(startNode) {
|
|
|
587
675
|
const blockNode = $findMatchingParent(startNode, node => $isElementNode(node) && !node.isInline());
|
|
588
676
|
if (!$isElementNode(blockNode)) {
|
|
589
677
|
{
|
|
590
|
-
|
|
678
|
+
formatDevErrorMessage(`Expected node ${startNode.__key} to have closest block element node.`);
|
|
591
679
|
}
|
|
592
680
|
}
|
|
593
681
|
return blockNode;
|
|
@@ -717,12 +805,7 @@ function $insertNodeToNearestRoot(node) {
|
|
|
717
805
|
const focusNode = focus.getNode();
|
|
718
806
|
const focusOffset = focus.offset;
|
|
719
807
|
if ($isRootOrShadowRoot(focusNode)) {
|
|
720
|
-
|
|
721
|
-
if (focusChild == null) {
|
|
722
|
-
focusNode.append(node);
|
|
723
|
-
} else {
|
|
724
|
-
focusChild.insertBefore(node);
|
|
725
|
-
}
|
|
808
|
+
$getChildCaretAtIndex(focusNode, focusOffset, 'next').insert(node);
|
|
726
809
|
node.selectNext();
|
|
727
810
|
} else {
|
|
728
811
|
let splitNode;
|
|
@@ -747,8 +830,7 @@ function $insertNodeToNearestRoot(node) {
|
|
|
747
830
|
const nodes = selection.getNodes();
|
|
748
831
|
nodes[nodes.length - 1].getTopLevelElementOrThrow().insertAfter(node);
|
|
749
832
|
} else {
|
|
750
|
-
|
|
751
|
-
root.append(node);
|
|
833
|
+
$getRoot().append(node);
|
|
752
834
|
}
|
|
753
835
|
const paragraphNode = $createParagraphNode();
|
|
754
836
|
node.insertAfter(paragraphNode);
|
|
@@ -804,27 +886,39 @@ function $filter(nodes, filterFn) {
|
|
|
804
886
|
* @param node Node that needs to be appended
|
|
805
887
|
*/
|
|
806
888
|
function $insertFirst(parent, node) {
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
889
|
+
$getChildCaret(parent, 'next').insert(node);
|
|
890
|
+
}
|
|
891
|
+
let NEEDS_MANUAL_ZOOM = IS_FIREFOX || !CAN_USE_DOM ? false : undefined;
|
|
892
|
+
function needsManualZoom() {
|
|
893
|
+
if (NEEDS_MANUAL_ZOOM === undefined) {
|
|
894
|
+
// If the browser implements standardized CSS zoom, then the client rect
|
|
895
|
+
// will be wider after zoom is applied
|
|
896
|
+
// https://chromestatus.com/feature/5198254868529152
|
|
897
|
+
// https://github.com/facebook/lexical/issues/6863
|
|
898
|
+
const div = document.createElement('div');
|
|
899
|
+
div.style.cssText = 'position: absolute; opacity: 0; width: 100px; left: -1000px;';
|
|
900
|
+
document.body.appendChild(div);
|
|
901
|
+
const noZoom = div.getBoundingClientRect();
|
|
902
|
+
div.style.setProperty('zoom', '2');
|
|
903
|
+
NEEDS_MANUAL_ZOOM = div.getBoundingClientRect().width === noZoom.width;
|
|
904
|
+
document.body.removeChild(div);
|
|
812
905
|
}
|
|
906
|
+
return NEEDS_MANUAL_ZOOM;
|
|
813
907
|
}
|
|
814
908
|
|
|
815
909
|
/**
|
|
816
910
|
* Calculates the zoom level of an element as a result of using
|
|
817
|
-
* css zoom property.
|
|
911
|
+
* css zoom property. For browsers that implement standardized CSS
|
|
912
|
+
* zoom (Firefox, Chrome >= 128), this will always return 1.
|
|
818
913
|
* @param element
|
|
819
914
|
*/
|
|
820
915
|
function calculateZoomLevel(element) {
|
|
821
|
-
if (IS_FIREFOX) {
|
|
822
|
-
return 1;
|
|
823
|
-
}
|
|
824
916
|
let zoom = 1;
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
917
|
+
if (needsManualZoom()) {
|
|
918
|
+
while (element) {
|
|
919
|
+
zoom *= Number(window.getComputedStyle(element).getPropertyValue('zoom'));
|
|
920
|
+
element = element.parentElement;
|
|
921
|
+
}
|
|
828
922
|
}
|
|
829
923
|
return zoom;
|
|
830
924
|
}
|
|
@@ -836,4 +930,178 @@ function $isEditorIsNestedEditor(editor) {
|
|
|
836
930
|
return editor._parentEditor !== null;
|
|
837
931
|
}
|
|
838
932
|
|
|
839
|
-
|
|
933
|
+
/**
|
|
934
|
+
* A depth first last-to-first traversal of root that stops at each node that matches
|
|
935
|
+
* $predicate and ensures that its parent is root. This is typically used to discard
|
|
936
|
+
* invalid or unsupported wrapping nodes. For example, a TableNode must only have
|
|
937
|
+
* TableRowNode as children, but an importer might add invalid nodes based on
|
|
938
|
+
* caption, tbody, thead, etc. and this will unwrap and discard those.
|
|
939
|
+
*
|
|
940
|
+
* @param root The root to start the traversal
|
|
941
|
+
* @param $predicate Should return true for nodes that are permitted to be children of root
|
|
942
|
+
* @returns true if this unwrapped or removed any nodes
|
|
943
|
+
*/
|
|
944
|
+
function $unwrapAndFilterDescendants(root, $predicate) {
|
|
945
|
+
return $unwrapAndFilterDescendantsImpl(root, $predicate, null);
|
|
946
|
+
}
|
|
947
|
+
function $unwrapAndFilterDescendantsImpl(root, $predicate, $onSuccess) {
|
|
948
|
+
let didMutate = false;
|
|
949
|
+
for (const node of $lastToFirstIterator(root)) {
|
|
950
|
+
if ($predicate(node)) {
|
|
951
|
+
if ($onSuccess !== null) {
|
|
952
|
+
$onSuccess(node);
|
|
953
|
+
}
|
|
954
|
+
continue;
|
|
955
|
+
}
|
|
956
|
+
didMutate = true;
|
|
957
|
+
if ($isElementNode(node)) {
|
|
958
|
+
$unwrapAndFilterDescendantsImpl(node, $predicate, $onSuccess || (child => node.insertAfter(child)));
|
|
959
|
+
}
|
|
960
|
+
node.remove();
|
|
961
|
+
}
|
|
962
|
+
return didMutate;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/**
|
|
966
|
+
* A depth first traversal of the children array that stops at and collects
|
|
967
|
+
* each node that `$predicate` matches. This is typically used to discard
|
|
968
|
+
* invalid or unsupported wrapping nodes on a children array in the `after`
|
|
969
|
+
* of an {@link lexical!DOMConversionOutput}. For example, a TableNode must only have
|
|
970
|
+
* TableRowNode as children, but an importer might add invalid nodes based on
|
|
971
|
+
* caption, tbody, thead, etc. and this will unwrap and discard those.
|
|
972
|
+
*
|
|
973
|
+
* This function is read-only and performs no mutation operations, which makes
|
|
974
|
+
* it suitable for import and export purposes but likely not for any in-place
|
|
975
|
+
* mutation. You should use {@link $unwrapAndFilterDescendants} for in-place
|
|
976
|
+
* mutations such as node transforms.
|
|
977
|
+
*
|
|
978
|
+
* @param children The children to traverse
|
|
979
|
+
* @param $predicate Should return true for nodes that are permitted to be children of root
|
|
980
|
+
* @returns The children or their descendants that match $predicate
|
|
981
|
+
*/
|
|
982
|
+
|
|
983
|
+
function $descendantsMatching(children, $predicate) {
|
|
984
|
+
const result = [];
|
|
985
|
+
const stack = Array.from(children).reverse();
|
|
986
|
+
for (let child = stack.pop(); child !== undefined; child = stack.pop()) {
|
|
987
|
+
if ($predicate(child)) {
|
|
988
|
+
result.push(child);
|
|
989
|
+
} else if ($isElementNode(child)) {
|
|
990
|
+
for (const grandchild of $lastToFirstIterator(child)) {
|
|
991
|
+
stack.push(grandchild);
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
return result;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* Return an iterator that yields each child of node from first to last, taking
|
|
1000
|
+
* care to preserve the next sibling before yielding the value in case the caller
|
|
1001
|
+
* removes the yielded node.
|
|
1002
|
+
*
|
|
1003
|
+
* @param node The node whose children to iterate
|
|
1004
|
+
* @returns An iterator of the node's children
|
|
1005
|
+
*/
|
|
1006
|
+
function $firstToLastIterator(node) {
|
|
1007
|
+
return $childIterator($getChildCaret(node, 'next'));
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
* Return an iterator that yields each child of node from last to first, taking
|
|
1012
|
+
* care to preserve the previous sibling before yielding the value in case the caller
|
|
1013
|
+
* removes the yielded node.
|
|
1014
|
+
*
|
|
1015
|
+
* @param node The node whose children to iterate
|
|
1016
|
+
* @returns An iterator of the node's children
|
|
1017
|
+
*/
|
|
1018
|
+
function $lastToFirstIterator(node) {
|
|
1019
|
+
return $childIterator($getChildCaret(node, 'previous'));
|
|
1020
|
+
}
|
|
1021
|
+
function $childIterator(startCaret) {
|
|
1022
|
+
const seen = new Set() ;
|
|
1023
|
+
return makeStepwiseIterator({
|
|
1024
|
+
hasNext: $isSiblingCaret,
|
|
1025
|
+
initial: startCaret.getAdjacentCaret(),
|
|
1026
|
+
map: caret => {
|
|
1027
|
+
const origin = caret.origin.getLatest();
|
|
1028
|
+
if (seen !== null) {
|
|
1029
|
+
const key = origin.getKey();
|
|
1030
|
+
if (!!seen.has(key)) {
|
|
1031
|
+
formatDevErrorMessage(`$childIterator: Cycle detected, node with key ${String(key)} has already been traversed`);
|
|
1032
|
+
}
|
|
1033
|
+
seen.add(key);
|
|
1034
|
+
}
|
|
1035
|
+
return origin;
|
|
1036
|
+
},
|
|
1037
|
+
step: caret => caret.getAdjacentCaret()
|
|
1038
|
+
});
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
/**
|
|
1042
|
+
* Replace this node with its children
|
|
1043
|
+
*
|
|
1044
|
+
* @param node The ElementNode to unwrap and remove
|
|
1045
|
+
*/
|
|
1046
|
+
function $unwrapNode(node) {
|
|
1047
|
+
$rewindSiblingCaret($getSiblingCaret(node, 'next')).splice(1, node.getChildren());
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
/**
|
|
1051
|
+
* Returns the Node sibling when this exists, otherwise the closest parent sibling. For example
|
|
1052
|
+
* R -> P -> T1, T2
|
|
1053
|
+
* -> P2
|
|
1054
|
+
* returns T2 for node T1, P2 for node T2, and null for node P2.
|
|
1055
|
+
* @param node LexicalNode.
|
|
1056
|
+
* @returns An array (tuple) containing the found Lexical node and the depth difference, or null, if this node doesn't exist.
|
|
1057
|
+
*/
|
|
1058
|
+
function $getAdjacentSiblingOrParentSiblingCaret(startCaret, rootMode = 'root') {
|
|
1059
|
+
let depthDiff = 0;
|
|
1060
|
+
let caret = startCaret;
|
|
1061
|
+
let nextCaret = $getAdjacentChildCaret(caret);
|
|
1062
|
+
while (nextCaret === null) {
|
|
1063
|
+
depthDiff--;
|
|
1064
|
+
nextCaret = caret.getParentCaret(rootMode);
|
|
1065
|
+
if (!nextCaret) {
|
|
1066
|
+
return null;
|
|
1067
|
+
}
|
|
1068
|
+
caret = nextCaret;
|
|
1069
|
+
nextCaret = $getAdjacentChildCaret(caret);
|
|
1070
|
+
}
|
|
1071
|
+
return nextCaret && [nextCaret, depthDiff];
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/**
|
|
1075
|
+
* A wrapper that creates bound functions and methods for the
|
|
1076
|
+
* StateConfig to save some boilerplate when defining methods
|
|
1077
|
+
* or exporting only the accessors from your modules rather
|
|
1078
|
+
* than exposing the StateConfig directly.
|
|
1079
|
+
*/
|
|
1080
|
+
|
|
1081
|
+
/**
|
|
1082
|
+
* EXPERIMENTAL
|
|
1083
|
+
*
|
|
1084
|
+
* A convenience interface for working with {@link $getState} and
|
|
1085
|
+
* {@link $setState}.
|
|
1086
|
+
*
|
|
1087
|
+
* @param stateConfig The stateConfig to wrap with convenience functionality
|
|
1088
|
+
* @returns a StateWrapper
|
|
1089
|
+
*/
|
|
1090
|
+
function makeStateWrapper(stateConfig) {
|
|
1091
|
+
const $get = node => $getState(node, stateConfig);
|
|
1092
|
+
const $set = (node, valueOrUpdater) => $setState(node, stateConfig, valueOrUpdater);
|
|
1093
|
+
return {
|
|
1094
|
+
$get,
|
|
1095
|
+
$set,
|
|
1096
|
+
accessors: [$get, $set],
|
|
1097
|
+
makeGetterMethod: () => function $getter() {
|
|
1098
|
+
return $get(this);
|
|
1099
|
+
},
|
|
1100
|
+
makeSetterMethod: () => function $setter(valueOrUpdater) {
|
|
1101
|
+
return $set(this, valueOrUpdater);
|
|
1102
|
+
},
|
|
1103
|
+
stateConfig
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
export { $descendantsMatching, $dfs, $dfsIterator, $filter, $findMatchingParent, $firstToLastIterator, $getAdjacentCaret, $getAdjacentSiblingOrParentSiblingCaret, $getDepth, $getNearestBlockElementAncestorOrThrow, $getNearestNodeOfType, $getNextRightPreorderNode, $getNextSiblingOrParentSibling, $insertFirst, $insertNodeToNearestRoot, $isEditorIsNestedEditor, $lastToFirstIterator, $restoreEditorState, $reverseDfs, $reverseDfsIterator, $unwrapAndFilterDescendants, $unwrapNode, $wrapNodeInElement, CAN_USE_BEFORE_INPUT, CAN_USE_DOM, IS_ANDROID, IS_ANDROID_CHROME, IS_APPLE, IS_APPLE_WEBKIT, IS_CHROME, IS_FIREFOX, IS_IOS, IS_SAFARI, addClassNamesToElement, calculateZoomLevel, isMimeType, makeStateWrapper, markSelection, mediaFileReader, mergeRegister, objectKlassEquals, mlcPositionNodeOnRange as positionNodeOnRange, registerNestedElementResolver, removeClassNamesFromElement, selectionAlwaysOnDisplay };
|