@react-fabric/lexical 1.0.0-beta.6 → 1.0.0-beta.60
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/{src/editor/Lexical.module.css → css/_lexical.css} +8 -4
- package/css/styles.css +24 -0
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +62 -20
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +11 -11
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +643 -245
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +1 -1
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +29 -22
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +13 -6
- 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 +242 -141
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +1 -1
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +11 -10
- package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +594 -255
- package/dist/cjs/node_modules/@lexical/list/LexicalList.js +1 -1
- package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +23 -16
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +102 -53
- 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 +275 -194
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +5 -1
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +28 -18
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +2293 -878
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +1 -1
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +53 -40
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +471 -193
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +13 -1
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +47 -32
- package/dist/cjs/node_modules/prismjs/prism.js +1 -1
- package/dist/cjs/packages/lexical/src/components/StickyComponent.js +1 -1
- package/dist/cjs/packages/lexical/src/editor/LexicalEditor.js +30 -5
- package/dist/cjs/packages/lexical/src/hooks/useFormatWatcher.js +86 -83
- package/dist/cjs/packages/lexical/src/plugins/ToolbarPlugin.js +25 -17
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +62 -20
- 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 +637 -246
- 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 +14 -7
- 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 +244 -144
- 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 +590 -257
- package/dist/esm/node_modules/@lexical/list/LexicalList.js +1 -1
- package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +5 -4
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +104 -55
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +267 -196
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +4 -2
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +2283 -881
- 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 +457 -194
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +8 -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/components/StickyComponent.js +1 -1
- package/dist/esm/packages/lexical/src/editor/LexicalEditor.js +30 -5
- package/dist/esm/packages/lexical/src/hooks/useFormatWatcher.js +88 -85
- package/dist/esm/packages/lexical/src/plugins/ToolbarPlugin.js +25 -17
- package/dist/types/editor/LexicalEditor.d.ts +25 -0
- package/dist/types/hooks/useFormatWatcher.d.ts +2 -2
- package/dist/types/nodes/StickyNode.d.ts +0 -1
- package/package.json +14 -13
- package/src/components/StickyComponent.tsx +2 -2
- package/src/editor/LexicalEditor.tsx +57 -26
- package/src/hooks/useFormatWatcher.ts +35 -31
- package/src/plugins/ToolbarPlugin.tsx +27 -17
- package/dist/cjs/editor/Lexical.module.css +0 -1
- package/dist/esm/editor/Lexical.module.css +0 -1
|
@@ -12,6 +12,20 @@ var LexicalSelection = require('../selection/LexicalSelection.js');
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
17
|
+
*
|
|
18
|
+
* This source code is licensed under the MIT license found in the
|
|
19
|
+
* LICENSE file in the root directory of this source tree.
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
// Do not require this module directly! Use normal `invariant` calls.
|
|
24
|
+
|
|
25
|
+
function formatDevErrorMessage(message) {
|
|
26
|
+
throw new Error(message);
|
|
27
|
+
}
|
|
28
|
+
|
|
15
29
|
/**
|
|
16
30
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
17
31
|
*
|
|
@@ -91,7 +105,7 @@ function normalizeClassNames(...classNames) {
|
|
|
91
105
|
* In this case, useEffect is returning the function returned by mergeRegister as a cleanup
|
|
92
106
|
* function to be executed after either the useEffect runs again (due to one of its dependencies
|
|
93
107
|
* updating) or the component it resides in unmounts.
|
|
94
|
-
* Note the functions don't
|
|
108
|
+
* Note the functions don't necessarily need to be in an array as all arguments
|
|
95
109
|
* are considered to be the func argument and spread from there.
|
|
96
110
|
* The order of cleanup is the reverse of the argument order. Generally it is
|
|
97
111
|
* expected that the first "acquire" will be "released" last (LIFO order),
|
|
@@ -121,41 +135,49 @@ function px(value) {
|
|
|
121
135
|
return `${value}px`;
|
|
122
136
|
}
|
|
123
137
|
|
|
124
|
-
/**
|
|
125
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
126
|
-
*
|
|
127
|
-
* This source code is licensed under the MIT license found in the
|
|
128
|
-
* LICENSE file in the root directory of this source tree.
|
|
129
|
-
*
|
|
130
|
-
*/
|
|
131
|
-
|
|
132
138
|
const mutationObserverConfig = {
|
|
133
139
|
attributes: true,
|
|
134
140
|
characterData: true,
|
|
135
141
|
childList: true,
|
|
136
142
|
subtree: true
|
|
137
143
|
};
|
|
138
|
-
function
|
|
144
|
+
function prependDOMNode(parent, node) {
|
|
145
|
+
parent.insertBefore(node, parent.firstChild);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Place one or multiple newly created Nodes at the passed Range's position.
|
|
150
|
+
* Multiple nodes will only be created when the Range spans multiple lines (aka
|
|
151
|
+
* client rects).
|
|
152
|
+
*
|
|
153
|
+
* This function can come particularly useful to highlight particular parts of
|
|
154
|
+
* the text without interfering with the EditorState, that will often replicate
|
|
155
|
+
* the state across collab and clipboard.
|
|
156
|
+
*
|
|
157
|
+
* This function accounts for DOM updates which can modify the passed Range.
|
|
158
|
+
* Hence, the function return to remove the listener.
|
|
159
|
+
*/
|
|
160
|
+
function mlcPositionNodeOnRange(editor, range, onReposition) {
|
|
139
161
|
let rootDOMNode = null;
|
|
140
162
|
let parentDOMNode = null;
|
|
141
163
|
let observer = null;
|
|
142
164
|
let lastNodes = [];
|
|
143
165
|
const wrapperNode = document.createElement('div');
|
|
166
|
+
wrapperNode.style.position = 'relative';
|
|
144
167
|
function position() {
|
|
145
168
|
if (!(rootDOMNode !== null)) {
|
|
146
|
-
|
|
169
|
+
formatDevErrorMessage(`Unexpected null rootDOMNode`);
|
|
147
170
|
}
|
|
148
171
|
if (!(parentDOMNode !== null)) {
|
|
149
|
-
|
|
172
|
+
formatDevErrorMessage(`Unexpected null parentDOMNode`);
|
|
150
173
|
}
|
|
151
174
|
const {
|
|
152
|
-
left:
|
|
153
|
-
top:
|
|
154
|
-
} =
|
|
155
|
-
const parentDOMNode_ = parentDOMNode;
|
|
175
|
+
left: parentLeft,
|
|
176
|
+
top: parentTop
|
|
177
|
+
} = parentDOMNode.getBoundingClientRect();
|
|
156
178
|
const rects = LexicalSelection.createRectsFromDOMRange(editor, range);
|
|
157
179
|
if (!wrapperNode.isConnected) {
|
|
158
|
-
|
|
180
|
+
prependDOMNode(parentDOMNode, wrapperNode);
|
|
159
181
|
}
|
|
160
182
|
let hasRepositioned = false;
|
|
161
183
|
for (let i = 0; i < rects.length; i++) {
|
|
@@ -168,12 +190,12 @@ function positionNodeOnRange(editor, range, onReposition) {
|
|
|
168
190
|
rectNodeStyle.position = 'absolute';
|
|
169
191
|
hasRepositioned = true;
|
|
170
192
|
}
|
|
171
|
-
const left = px(rect.left -
|
|
193
|
+
const left = px(rect.left - parentLeft);
|
|
172
194
|
if (rectNodeStyle.left !== left) {
|
|
173
195
|
rectNodeStyle.left = left;
|
|
174
196
|
hasRepositioned = true;
|
|
175
197
|
}
|
|
176
|
-
const top = px(rect.top -
|
|
198
|
+
const top = px(rect.top - parentTop);
|
|
177
199
|
if (rectNodeStyle.top !== top) {
|
|
178
200
|
rectNode.style.top = top;
|
|
179
201
|
hasRepositioned = true;
|
|
@@ -220,7 +242,7 @@ function positionNodeOnRange(editor, range, onReposition) {
|
|
|
220
242
|
return stop();
|
|
221
243
|
}
|
|
222
244
|
const currentParentDOMNode = currentRootDOMNode.parentElement;
|
|
223
|
-
if (!(currentParentDOMNode
|
|
245
|
+
if (!lexical.isHTMLElement(currentParentDOMNode)) {
|
|
224
246
|
return stop();
|
|
225
247
|
}
|
|
226
248
|
stop();
|
|
@@ -257,10 +279,40 @@ function positionNodeOnRange(editor, range, onReposition) {
|
|
|
257
279
|
*
|
|
258
280
|
*/
|
|
259
281
|
|
|
282
|
+
function $getOrderedSelectionPoints(selection) {
|
|
283
|
+
const points = selection.getStartEndPoints();
|
|
284
|
+
return selection.isBackward() ? [points[1], points[0]] : points;
|
|
285
|
+
}
|
|
286
|
+
function rangeTargetFromPoint(point, node, dom) {
|
|
287
|
+
if (point.type === 'text' || !lexical.$isElementNode(node)) {
|
|
288
|
+
const textDOM = lexical.getDOMTextNode(dom) || dom;
|
|
289
|
+
return [textDOM, point.offset];
|
|
290
|
+
} else {
|
|
291
|
+
const slot = node.getDOMSlot(dom);
|
|
292
|
+
return [slot.element, slot.getFirstChildOffset() + point.offset];
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
function rangeFromPoints(editor, start, startNode, startDOM, end, endNode, endDOM) {
|
|
296
|
+
const editorDocument = editor._window ? editor._window.document : document;
|
|
297
|
+
const range = editorDocument.createRange();
|
|
298
|
+
range.setStart(...rangeTargetFromPoint(start, startNode, startDOM));
|
|
299
|
+
range.setEnd(...rangeTargetFromPoint(end, endNode, endDOM));
|
|
300
|
+
return range;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Place one or multiple newly created Nodes at the current selection. Multiple
|
|
304
|
+
* nodes will only be created when the selection spans multiple lines (aka
|
|
305
|
+
* client rects).
|
|
306
|
+
*
|
|
307
|
+
* This function can come useful when you want to show the selection but the
|
|
308
|
+
* editor has been focused away.
|
|
309
|
+
*/
|
|
260
310
|
function markSelection(editor, onReposition) {
|
|
261
311
|
let previousAnchorNode = null;
|
|
312
|
+
let previousAnchorNodeDOM = null;
|
|
262
313
|
let previousAnchorOffset = null;
|
|
263
314
|
let previousFocusNode = null;
|
|
315
|
+
let previousFocusNodeDOM = null;
|
|
264
316
|
let previousFocusOffset = null;
|
|
265
317
|
let removeRangeListener = () => {};
|
|
266
318
|
function compute(editorState) {
|
|
@@ -276,53 +328,22 @@ function markSelection(editor, onReposition) {
|
|
|
276
328
|
removeRangeListener = () => {};
|
|
277
329
|
return;
|
|
278
330
|
}
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
const
|
|
284
|
-
const
|
|
285
|
-
const
|
|
286
|
-
const
|
|
287
|
-
const
|
|
288
|
-
const
|
|
289
|
-
const
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
const focusHTMLElement = editor.getElementByKey(focus.getNode().getKey());
|
|
296
|
-
// TODO handle selection beyond the common TextNode
|
|
297
|
-
if (anchorHTMLElement !== null && focusHTMLElement !== null && anchorHTMLElement.tagName === 'SPAN' && focusHTMLElement.tagName === 'SPAN') {
|
|
298
|
-
const range = document.createRange();
|
|
299
|
-
let firstHTMLElement;
|
|
300
|
-
let firstOffset;
|
|
301
|
-
let lastHTMLElement;
|
|
302
|
-
let lastOffset;
|
|
303
|
-
if (focus.isBefore(anchor)) {
|
|
304
|
-
firstHTMLElement = focusHTMLElement;
|
|
305
|
-
firstOffset = focus.offset;
|
|
306
|
-
lastHTMLElement = anchorHTMLElement;
|
|
307
|
-
lastOffset = anchor.offset;
|
|
308
|
-
} else {
|
|
309
|
-
firstHTMLElement = anchorHTMLElement;
|
|
310
|
-
firstOffset = anchor.offset;
|
|
311
|
-
lastHTMLElement = focusHTMLElement;
|
|
312
|
-
lastOffset = focus.offset;
|
|
313
|
-
}
|
|
314
|
-
const firstTextNode = firstHTMLElement.firstChild;
|
|
315
|
-
if (!(firstTextNode !== null)) {
|
|
316
|
-
throw Error(`Expected text node to be first child of span`);
|
|
317
|
-
}
|
|
318
|
-
const lastTextNode = lastHTMLElement.firstChild;
|
|
319
|
-
if (!(lastTextNode !== null)) {
|
|
320
|
-
throw Error(`Expected text node to be first child of span`);
|
|
321
|
-
}
|
|
322
|
-
range.setStart(firstTextNode, firstOffset);
|
|
323
|
-
range.setEnd(lastTextNode, lastOffset);
|
|
324
|
-
removeRangeListener();
|
|
325
|
-
removeRangeListener = positionNodeOnRange(editor, range, domNodes => {
|
|
331
|
+
const [start, end] = $getOrderedSelectionPoints(selection);
|
|
332
|
+
const currentStartNode = start.getNode();
|
|
333
|
+
const currentStartNodeKey = currentStartNode.getKey();
|
|
334
|
+
const currentStartOffset = start.offset;
|
|
335
|
+
const currentEndNode = end.getNode();
|
|
336
|
+
const currentEndNodeKey = currentEndNode.getKey();
|
|
337
|
+
const currentEndOffset = end.offset;
|
|
338
|
+
const currentStartNodeDOM = editor.getElementByKey(currentStartNodeKey);
|
|
339
|
+
const currentEndNodeDOM = editor.getElementByKey(currentEndNodeKey);
|
|
340
|
+
const differentStartDOM = previousAnchorNode === null || currentStartNodeDOM !== previousAnchorNodeDOM || currentStartOffset !== previousAnchorOffset || currentStartNodeKey !== previousAnchorNode.getKey();
|
|
341
|
+
const differentEndDOM = previousFocusNode === null || currentEndNodeDOM !== previousFocusNodeDOM || currentEndOffset !== previousFocusOffset || currentEndNodeKey !== previousFocusNode.getKey();
|
|
342
|
+
if ((differentStartDOM || differentEndDOM) && currentStartNodeDOM !== null && currentEndNodeDOM !== null) {
|
|
343
|
+
const range = rangeFromPoints(editor, start, currentStartNode, currentStartNodeDOM, end, currentEndNode, currentEndNodeDOM);
|
|
344
|
+
removeRangeListener();
|
|
345
|
+
removeRangeListener = mlcPositionNodeOnRange(editor, range, domNodes => {
|
|
346
|
+
if (onReposition === undefined) {
|
|
326
347
|
for (const domNode of domNodes) {
|
|
327
348
|
const domNodeStyle = domNode.style;
|
|
328
349
|
if (domNodeStyle.background !== 'Highlight') {
|
|
@@ -331,12 +352,6 @@ function markSelection(editor, onReposition) {
|
|
|
331
352
|
if (domNodeStyle.color !== 'HighlightText') {
|
|
332
353
|
domNodeStyle.color = 'HighlightText';
|
|
333
354
|
}
|
|
334
|
-
if (domNodeStyle.zIndex !== '-1') {
|
|
335
|
-
domNodeStyle.zIndex = '-1';
|
|
336
|
-
}
|
|
337
|
-
if (domNodeStyle.pointerEvents !== 'none') {
|
|
338
|
-
domNodeStyle.pointerEvents = 'none';
|
|
339
|
-
}
|
|
340
355
|
if (domNodeStyle.marginTop !== px(-1.5)) {
|
|
341
356
|
domNodeStyle.marginTop = px(-1.5);
|
|
342
357
|
}
|
|
@@ -347,22 +362,23 @@ function markSelection(editor, onReposition) {
|
|
|
347
362
|
domNodeStyle.paddingBottom = px(0);
|
|
348
363
|
}
|
|
349
364
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
}
|
|
365
|
+
} else {
|
|
366
|
+
onReposition(domNodes);
|
|
367
|
+
}
|
|
368
|
+
});
|
|
355
369
|
}
|
|
356
|
-
previousAnchorNode =
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
370
|
+
previousAnchorNode = currentStartNode;
|
|
371
|
+
previousAnchorNodeDOM = currentStartNodeDOM;
|
|
372
|
+
previousAnchorOffset = currentStartOffset;
|
|
373
|
+
previousFocusNode = currentEndNode;
|
|
374
|
+
previousFocusNodeDOM = currentEndNodeDOM;
|
|
375
|
+
previousFocusOffset = currentEndOffset;
|
|
360
376
|
});
|
|
361
377
|
}
|
|
362
378
|
compute(editor.getEditorState());
|
|
363
379
|
return mergeRegister(editor.registerUpdateListener(({
|
|
364
380
|
editorState
|
|
365
|
-
}) => compute(editorState)),
|
|
381
|
+
}) => compute(editorState)), () => {
|
|
366
382
|
removeRangeListener();
|
|
367
383
|
});
|
|
368
384
|
}
|
|
@@ -375,6 +391,33 @@ function markSelection(editor, onReposition) {
|
|
|
375
391
|
*
|
|
376
392
|
*/
|
|
377
393
|
|
|
394
|
+
function selectionAlwaysOnDisplay(editor) {
|
|
395
|
+
let removeSelectionMark = null;
|
|
396
|
+
const onSelectionChange = () => {
|
|
397
|
+
const domSelection = getSelection();
|
|
398
|
+
const domAnchorNode = domSelection && domSelection.anchorNode;
|
|
399
|
+
const editorRootElement = editor.getRootElement();
|
|
400
|
+
const isSelectionInsideEditor = domAnchorNode !== null && editorRootElement !== null && editorRootElement.contains(domAnchorNode);
|
|
401
|
+
if (isSelectionInsideEditor) {
|
|
402
|
+
if (removeSelectionMark !== null) {
|
|
403
|
+
removeSelectionMark();
|
|
404
|
+
removeSelectionMark = null;
|
|
405
|
+
}
|
|
406
|
+
} else {
|
|
407
|
+
if (removeSelectionMark === null) {
|
|
408
|
+
removeSelectionMark = markSelection(editor);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
document.addEventListener('selectionchange', onSelectionChange);
|
|
413
|
+
return () => {
|
|
414
|
+
if (removeSelectionMark !== null) {
|
|
415
|
+
removeSelectionMark();
|
|
416
|
+
}
|
|
417
|
+
document.removeEventListener('selectionchange', onSelectionChange);
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
|
|
378
421
|
// Hotfix to export these with inlined types #5918
|
|
379
422
|
const CAN_USE_BEFORE_INPUT = CAN_USE_BEFORE_INPUT$1;
|
|
380
423
|
const CAN_USE_DOM = CAN_USE_DOM$1;
|
|
@@ -386,6 +429,7 @@ const IS_CHROME = IS_CHROME$1;
|
|
|
386
429
|
const IS_FIREFOX = IS_FIREFOX$1;
|
|
387
430
|
const IS_IOS = IS_IOS$1;
|
|
388
431
|
const IS_SAFARI = IS_SAFARI$1;
|
|
432
|
+
|
|
389
433
|
/**
|
|
390
434
|
* Takes an HTML element and adds the classNames passed within an array,
|
|
391
435
|
* ignoring any non-string types. A space can be used to add multiple classes
|
|
@@ -477,57 +521,99 @@ function mediaFileReader(files, acceptableMimeTypes) {
|
|
|
477
521
|
handleNextFile();
|
|
478
522
|
});
|
|
479
523
|
}
|
|
480
|
-
|
|
481
524
|
/**
|
|
482
525
|
* "Depth-First Search" starts at the root/top node of a tree and goes as far as it can down a branch end
|
|
483
526
|
* before backtracking and finding a new path. Consider solving a maze by hugging either wall, moving down a
|
|
484
527
|
* branch until you hit a dead-end (leaf) and backtracking to find the nearest branching path and repeat.
|
|
485
528
|
* It will then return all the nodes found in the search in an array of objects.
|
|
486
|
-
* @param
|
|
487
|
-
* @param
|
|
529
|
+
* @param startNode - The node to start the search, if omitted, it will start at the root node.
|
|
530
|
+
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
|
|
488
531
|
* @returns An array of objects of all the nodes found by the search, including their depth into the tree.
|
|
489
|
-
* \\{depth: number, node: LexicalNode\\} It will always return at least 1 node (the
|
|
490
|
-
*/
|
|
491
|
-
function $dfs(
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
532
|
+
* \\{depth: number, node: LexicalNode\\} It will always return at least 1 node (the start node).
|
|
533
|
+
*/
|
|
534
|
+
function $dfs(startNode, endNode) {
|
|
535
|
+
return Array.from($dfsIterator(startNode, endNode));
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Get the adjacent caret in the same direction
|
|
540
|
+
*
|
|
541
|
+
* @param caret A caret or null
|
|
542
|
+
* @returns `caret.getAdjacentCaret()` or `null`
|
|
543
|
+
*/
|
|
544
|
+
function $getAdjacentCaret(caret) {
|
|
545
|
+
return caret ? caret.getAdjacentCaret() : null;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* $dfs iterator (right to left). Tree traversal is done on the fly as new values are requested with O(1) memory.
|
|
550
|
+
* @param startNode - The node to start the search, if omitted, it will start at the root node.
|
|
551
|
+
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
|
|
552
|
+
* @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node).
|
|
553
|
+
*/
|
|
554
|
+
function $reverseDfs(startNode, endNode) {
|
|
555
|
+
return Array.from($reverseDfsIterator(startNode, endNode));
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* $dfs iterator (left to right). Tree traversal is done on the fly as new values are requested with O(1) memory.
|
|
560
|
+
* @param startNode - The node to start the search, if omitted, it will start at the root node.
|
|
561
|
+
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
|
|
562
|
+
* @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node).
|
|
563
|
+
*/
|
|
564
|
+
function $dfsIterator(startNode, endNode) {
|
|
565
|
+
return $dfsCaretIterator('next', startNode, endNode);
|
|
566
|
+
}
|
|
567
|
+
function $getEndCaret(startNode, direction) {
|
|
568
|
+
const rval = $getAdjacentSiblingOrParentSiblingCaret(lexical.$getSiblingCaret(startNode, direction));
|
|
569
|
+
return rval && rval[0];
|
|
570
|
+
}
|
|
571
|
+
function $dfsCaretIterator(direction, startNode, endNode) {
|
|
572
|
+
const root = lexical.$getRoot();
|
|
573
|
+
const start = startNode || root;
|
|
574
|
+
const startCaret = lexical.$isElementNode(start) ? lexical.$getChildCaret(start, direction) : lexical.$getSiblingCaret(start, direction);
|
|
575
|
+
const startDepth = $getDepth(start);
|
|
576
|
+
const endCaret = endNode ? lexical.$getAdjacentChildCaret(lexical.$getChildCaretOrSelf(lexical.$getSiblingCaret(endNode, direction))) : $getEndCaret(start, direction);
|
|
577
|
+
let depth = startDepth;
|
|
578
|
+
return lexical.makeStepwiseIterator({
|
|
579
|
+
hasNext: state => state !== null,
|
|
580
|
+
initial: startCaret,
|
|
581
|
+
map: state => ({
|
|
499
582
|
depth,
|
|
500
|
-
node
|
|
501
|
-
})
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
} else {
|
|
506
|
-
// Find immediate sibling or nearest parent sibling
|
|
507
|
-
let sibling = null;
|
|
508
|
-
while (sibling === null && node !== null) {
|
|
509
|
-
sibling = node.getNextSibling();
|
|
510
|
-
if (sibling === null) {
|
|
511
|
-
node = node.getParent();
|
|
512
|
-
depth--;
|
|
513
|
-
} else {
|
|
514
|
-
node = sibling;
|
|
515
|
-
}
|
|
583
|
+
node: state.origin
|
|
584
|
+
}),
|
|
585
|
+
step: state => {
|
|
586
|
+
if (state.isSameNodeCaret(endCaret)) {
|
|
587
|
+
return null;
|
|
516
588
|
}
|
|
589
|
+
if (lexical.$isChildCaret(state)) {
|
|
590
|
+
depth++;
|
|
591
|
+
}
|
|
592
|
+
const rval = $getAdjacentSiblingOrParentSiblingCaret(state);
|
|
593
|
+
if (!rval || rval[0].isSameNodeCaret(endCaret)) {
|
|
594
|
+
return null;
|
|
595
|
+
}
|
|
596
|
+
depth += rval[1];
|
|
597
|
+
return rval[0];
|
|
517
598
|
}
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Returns the Node sibling when this exists, otherwise the closest parent sibling. For example
|
|
604
|
+
* R -> P -> T1, T2
|
|
605
|
+
* -> P2
|
|
606
|
+
* returns T2 for node T1, P2 for node T2, and null for node P2.
|
|
607
|
+
* @param node LexicalNode.
|
|
608
|
+
* @returns An array (tuple) containing the found Lexical node and the depth difference, or null, if this node doesn't exist.
|
|
609
|
+
*/
|
|
610
|
+
function $getNextSiblingOrParentSibling(node) {
|
|
611
|
+
const rval = $getAdjacentSiblingOrParentSiblingCaret(lexical.$getSiblingCaret(node, 'next'));
|
|
612
|
+
return rval && [rval[0].origin, rval[1]];
|
|
526
613
|
}
|
|
527
614
|
function $getDepth(node) {
|
|
528
|
-
let
|
|
529
|
-
let
|
|
530
|
-
while ((innerNode = innerNode.getParent()) !== null) {
|
|
615
|
+
let depth = -1;
|
|
616
|
+
for (let innerNode = node; innerNode !== null; innerNode = innerNode.getParent()) {
|
|
531
617
|
depth++;
|
|
532
618
|
}
|
|
533
619
|
return depth;
|
|
@@ -535,28 +621,26 @@ function $getDepth(node) {
|
|
|
535
621
|
|
|
536
622
|
/**
|
|
537
623
|
* Performs a right-to-left preorder tree traversal.
|
|
538
|
-
* From the starting node it goes to the rightmost child, than backtracks to
|
|
624
|
+
* From the starting node it goes to the rightmost child, than backtracks to parent and finds new rightmost path.
|
|
539
625
|
* It will return the next node in traversal sequence after the startingNode.
|
|
540
626
|
* The traversal is similar to $dfs functions above, but the nodes are visited right-to-left, not left-to-right.
|
|
541
627
|
* @param startingNode - The node to start the search.
|
|
542
628
|
* @returns The next node in pre-order right to left traversal sequence or `null`, if the node does not exist
|
|
543
629
|
*/
|
|
544
630
|
function $getNextRightPreorderNode(startingNode) {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
}
|
|
559
|
-
return node;
|
|
631
|
+
const startCaret = lexical.$getChildCaretOrSelf(lexical.$getSiblingCaret(startingNode, 'previous'));
|
|
632
|
+
const next = $getAdjacentSiblingOrParentSiblingCaret(startCaret, 'root');
|
|
633
|
+
return next && next[0].origin;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* $dfs iterator (right to left). Tree traversal is done on the fly as new values are requested with O(1) memory.
|
|
638
|
+
* @param startNode - The node to start the search, if omitted, it will start at the root node.
|
|
639
|
+
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
|
|
640
|
+
* @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node).
|
|
641
|
+
*/
|
|
642
|
+
function $reverseDfsIterator(startNode, endNode) {
|
|
643
|
+
return $dfsCaretIterator('previous', startNode, endNode);
|
|
560
644
|
}
|
|
561
645
|
|
|
562
646
|
/**
|
|
@@ -586,7 +670,7 @@ function $getNearestBlockElementAncestorOrThrow(startNode) {
|
|
|
586
670
|
const blockNode = $findMatchingParent(startNode, node => lexical.$isElementNode(node) && !node.isInline());
|
|
587
671
|
if (!lexical.$isElementNode(blockNode)) {
|
|
588
672
|
{
|
|
589
|
-
|
|
673
|
+
formatDevErrorMessage(`Expected node ${startNode.__key} to have closest block element node.`);
|
|
590
674
|
}
|
|
591
675
|
}
|
|
592
676
|
return blockNode;
|
|
@@ -703,59 +787,52 @@ function $restoreEditorState(editor, editorState) {
|
|
|
703
787
|
* If the selected insertion area is the root/shadow root node (see {@link lexical!$isRootOrShadowRoot}),
|
|
704
788
|
* the node will be appended there, otherwise, it will be inserted before the insertion area.
|
|
705
789
|
* If there is no selection where the node is to be inserted, it will be appended after any current nodes
|
|
706
|
-
* within the tree, as a child of the root node. A paragraph
|
|
790
|
+
* within the tree, as a child of the root node. A paragraph will then be added after the inserted node and selected.
|
|
707
791
|
* @param node - The node to be inserted
|
|
708
792
|
* @returns The node after its insertion
|
|
709
793
|
*/
|
|
710
794
|
function $insertNodeToNearestRoot(node) {
|
|
711
795
|
const selection = lexical.$getSelection() || lexical.$getPreviousSelection();
|
|
796
|
+
let initialCaret;
|
|
712
797
|
if (lexical.$isRangeSelection(selection)) {
|
|
713
|
-
|
|
714
|
-
focus
|
|
715
|
-
} = selection;
|
|
716
|
-
const focusNode = focus.getNode();
|
|
717
|
-
const focusOffset = focus.offset;
|
|
718
|
-
if (lexical.$isRootOrShadowRoot(focusNode)) {
|
|
719
|
-
const focusChild = focusNode.getChildAtIndex(focusOffset);
|
|
720
|
-
if (focusChild == null) {
|
|
721
|
-
focusNode.append(node);
|
|
722
|
-
} else {
|
|
723
|
-
focusChild.insertBefore(node);
|
|
724
|
-
}
|
|
725
|
-
node.selectNext();
|
|
726
|
-
} else {
|
|
727
|
-
let splitNode;
|
|
728
|
-
let splitOffset;
|
|
729
|
-
if (lexical.$isTextNode(focusNode)) {
|
|
730
|
-
splitNode = focusNode.getParentOrThrow();
|
|
731
|
-
splitOffset = focusNode.getIndexWithinParent();
|
|
732
|
-
if (focusOffset > 0) {
|
|
733
|
-
splitOffset += 1;
|
|
734
|
-
focusNode.splitText(focusOffset);
|
|
735
|
-
}
|
|
736
|
-
} else {
|
|
737
|
-
splitNode = focusNode;
|
|
738
|
-
splitOffset = focusOffset;
|
|
739
|
-
}
|
|
740
|
-
const [, rightTree] = lexical.$splitNode(splitNode, splitOffset);
|
|
741
|
-
rightTree.insertBefore(node);
|
|
742
|
-
rightTree.selectStart();
|
|
743
|
-
}
|
|
798
|
+
initialCaret = lexical.$caretFromPoint(selection.focus, 'next');
|
|
744
799
|
} else {
|
|
745
800
|
if (selection != null) {
|
|
746
801
|
const nodes = selection.getNodes();
|
|
747
|
-
nodes[nodes.length - 1]
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
802
|
+
const lastNode = nodes[nodes.length - 1];
|
|
803
|
+
if (lastNode) {
|
|
804
|
+
initialCaret = lexical.$getSiblingCaret(lastNode, 'next');
|
|
805
|
+
}
|
|
751
806
|
}
|
|
752
|
-
|
|
753
|
-
node.insertAfter(paragraphNode);
|
|
754
|
-
paragraphNode.select();
|
|
807
|
+
initialCaret = initialCaret || lexical.$getChildCaret(lexical.$getRoot(), 'previous').getFlipped().insert(lexical.$createParagraphNode());
|
|
755
808
|
}
|
|
809
|
+
const insertCaret = $insertNodeToNearestRootAtCaret(node, initialCaret);
|
|
810
|
+
const adjacent = lexical.$getAdjacentChildCaret(insertCaret);
|
|
811
|
+
const selectionCaret = lexical.$isChildCaret(adjacent) ? lexical.$normalizeCaret(adjacent) : insertCaret;
|
|
812
|
+
lexical.$setSelectionFromCaretRange(lexical.$getCollapsedCaretRange(selectionCaret));
|
|
756
813
|
return node.getLatest();
|
|
757
814
|
}
|
|
758
815
|
|
|
816
|
+
/**
|
|
817
|
+
* If the insertion caret is the root/shadow root node (see {@link lexical!$isRootOrShadowRoot}),
|
|
818
|
+
* the node will be inserted there, otherwise the parent nodes will be split according to the
|
|
819
|
+
* given options.
|
|
820
|
+
* @param node - The node to be inserted
|
|
821
|
+
* @param caret - The location to insert or split from
|
|
822
|
+
* @returns The node after its insertion
|
|
823
|
+
*/
|
|
824
|
+
function $insertNodeToNearestRootAtCaret(node, caret, options) {
|
|
825
|
+
let insertCaret = lexical.$getCaretInDirection(caret, 'next');
|
|
826
|
+
for (let nextCaret = insertCaret; nextCaret; nextCaret = lexical.$splitAtPointCaretNext(nextCaret, options)) {
|
|
827
|
+
insertCaret = nextCaret;
|
|
828
|
+
}
|
|
829
|
+
if (!!lexical.$isTextPointCaret(insertCaret)) {
|
|
830
|
+
formatDevErrorMessage(`$insertNodeToNearestRootAtCaret: An unattached TextNode can not be split`);
|
|
831
|
+
}
|
|
832
|
+
insertCaret.insert(node.isInline() ? lexical.$createParagraphNode().append(node) : node);
|
|
833
|
+
return lexical.$getCaretInDirection(lexical.$getSiblingCaret(node.getLatest(), 'next'), caret.direction);
|
|
834
|
+
}
|
|
835
|
+
|
|
759
836
|
/**
|
|
760
837
|
* Wraps the node into another node created from a createElementNode function, eg. $createParagraphNode
|
|
761
838
|
* @param node - Node to be wrapped.
|
|
@@ -774,7 +851,7 @@ function $wrapNodeInElement(node, createElementNode) {
|
|
|
774
851
|
/**
|
|
775
852
|
* @param object = The instance of the type
|
|
776
853
|
* @param objectClass = The class of the type
|
|
777
|
-
* @returns Whether the object is has the same Klass of the objectClass, ignoring the difference across window (e.g. different
|
|
854
|
+
* @returns Whether the object is has the same Klass of the objectClass, ignoring the difference across window (e.g. different iframes)
|
|
778
855
|
*/
|
|
779
856
|
function objectKlassEquals(object, objectClass) {
|
|
780
857
|
return object !== null ? Object.getPrototypeOf(object).constructor.name === objectClass.name : false;
|
|
@@ -803,27 +880,39 @@ function $filter(nodes, filterFn) {
|
|
|
803
880
|
* @param node Node that needs to be appended
|
|
804
881
|
*/
|
|
805
882
|
function $insertFirst(parent, node) {
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
883
|
+
lexical.$getChildCaret(parent, 'next').insert(node);
|
|
884
|
+
}
|
|
885
|
+
let NEEDS_MANUAL_ZOOM = IS_FIREFOX || !CAN_USE_DOM ? false : undefined;
|
|
886
|
+
function needsManualZoom() {
|
|
887
|
+
if (NEEDS_MANUAL_ZOOM === undefined) {
|
|
888
|
+
// If the browser implements standardized CSS zoom, then the client rect
|
|
889
|
+
// will be wider after zoom is applied
|
|
890
|
+
// https://chromestatus.com/feature/5198254868529152
|
|
891
|
+
// https://github.com/facebook/lexical/issues/6863
|
|
892
|
+
const div = document.createElement('div');
|
|
893
|
+
div.style.cssText = 'position: absolute; opacity: 0; width: 100px; left: -1000px;';
|
|
894
|
+
document.body.appendChild(div);
|
|
895
|
+
const noZoom = div.getBoundingClientRect();
|
|
896
|
+
div.style.setProperty('zoom', '2');
|
|
897
|
+
NEEDS_MANUAL_ZOOM = div.getBoundingClientRect().width === noZoom.width;
|
|
898
|
+
document.body.removeChild(div);
|
|
811
899
|
}
|
|
900
|
+
return NEEDS_MANUAL_ZOOM;
|
|
812
901
|
}
|
|
813
902
|
|
|
814
903
|
/**
|
|
815
904
|
* Calculates the zoom level of an element as a result of using
|
|
816
|
-
* css zoom property.
|
|
905
|
+
* css zoom property. For browsers that implement standardized CSS
|
|
906
|
+
* zoom (Firefox, Chrome >= 128), this will always return 1.
|
|
817
907
|
* @param element
|
|
818
908
|
*/
|
|
819
909
|
function calculateZoomLevel(element) {
|
|
820
|
-
if (IS_FIREFOX) {
|
|
821
|
-
return 1;
|
|
822
|
-
}
|
|
823
910
|
let zoom = 1;
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
911
|
+
if (needsManualZoom()) {
|
|
912
|
+
while (element) {
|
|
913
|
+
zoom *= Number(window.getComputedStyle(element).getPropertyValue('zoom'));
|
|
914
|
+
element = element.parentElement;
|
|
915
|
+
}
|
|
827
916
|
}
|
|
828
917
|
return zoom;
|
|
829
918
|
}
|
|
@@ -835,6 +924,180 @@ function $isEditorIsNestedEditor(editor) {
|
|
|
835
924
|
return editor._parentEditor !== null;
|
|
836
925
|
}
|
|
837
926
|
|
|
927
|
+
/**
|
|
928
|
+
* A depth first last-to-first traversal of root that stops at each node that matches
|
|
929
|
+
* $predicate and ensures that its parent is root. This is typically used to discard
|
|
930
|
+
* invalid or unsupported wrapping nodes. For example, a TableNode must only have
|
|
931
|
+
* TableRowNode as children, but an importer might add invalid nodes based on
|
|
932
|
+
* caption, tbody, thead, etc. and this will unwrap and discard those.
|
|
933
|
+
*
|
|
934
|
+
* @param root The root to start the traversal
|
|
935
|
+
* @param $predicate Should return true for nodes that are permitted to be children of root
|
|
936
|
+
* @returns true if this unwrapped or removed any nodes
|
|
937
|
+
*/
|
|
938
|
+
function $unwrapAndFilterDescendants(root, $predicate) {
|
|
939
|
+
return $unwrapAndFilterDescendantsImpl(root, $predicate, null);
|
|
940
|
+
}
|
|
941
|
+
function $unwrapAndFilterDescendantsImpl(root, $predicate, $onSuccess) {
|
|
942
|
+
let didMutate = false;
|
|
943
|
+
for (const node of $lastToFirstIterator(root)) {
|
|
944
|
+
if ($predicate(node)) {
|
|
945
|
+
if ($onSuccess !== null) {
|
|
946
|
+
$onSuccess(node);
|
|
947
|
+
}
|
|
948
|
+
continue;
|
|
949
|
+
}
|
|
950
|
+
didMutate = true;
|
|
951
|
+
if (lexical.$isElementNode(node)) {
|
|
952
|
+
$unwrapAndFilterDescendantsImpl(node, $predicate, $onSuccess || (child => node.insertAfter(child)));
|
|
953
|
+
}
|
|
954
|
+
node.remove();
|
|
955
|
+
}
|
|
956
|
+
return didMutate;
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
/**
|
|
960
|
+
* A depth first traversal of the children array that stops at and collects
|
|
961
|
+
* each node that `$predicate` matches. This is typically used to discard
|
|
962
|
+
* invalid or unsupported wrapping nodes on a children array in the `after`
|
|
963
|
+
* of an {@link lexical!DOMConversionOutput}. For example, a TableNode must only have
|
|
964
|
+
* TableRowNode as children, but an importer might add invalid nodes based on
|
|
965
|
+
* caption, tbody, thead, etc. and this will unwrap and discard those.
|
|
966
|
+
*
|
|
967
|
+
* This function is read-only and performs no mutation operations, which makes
|
|
968
|
+
* it suitable for import and export purposes but likely not for any in-place
|
|
969
|
+
* mutation. You should use {@link $unwrapAndFilterDescendants} for in-place
|
|
970
|
+
* mutations such as node transforms.
|
|
971
|
+
*
|
|
972
|
+
* @param children The children to traverse
|
|
973
|
+
* @param $predicate Should return true for nodes that are permitted to be children of root
|
|
974
|
+
* @returns The children or their descendants that match $predicate
|
|
975
|
+
*/
|
|
976
|
+
|
|
977
|
+
function $descendantsMatching(children, $predicate) {
|
|
978
|
+
const result = [];
|
|
979
|
+
const stack = Array.from(children).reverse();
|
|
980
|
+
for (let child = stack.pop(); child !== undefined; child = stack.pop()) {
|
|
981
|
+
if ($predicate(child)) {
|
|
982
|
+
result.push(child);
|
|
983
|
+
} else if (lexical.$isElementNode(child)) {
|
|
984
|
+
for (const grandchild of $lastToFirstIterator(child)) {
|
|
985
|
+
stack.push(grandchild);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
return result;
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/**
|
|
993
|
+
* Return an iterator that yields each child of node from first to last, taking
|
|
994
|
+
* care to preserve the next sibling before yielding the value in case the caller
|
|
995
|
+
* removes the yielded node.
|
|
996
|
+
*
|
|
997
|
+
* @param node The node whose children to iterate
|
|
998
|
+
* @returns An iterator of the node's children
|
|
999
|
+
*/
|
|
1000
|
+
function $firstToLastIterator(node) {
|
|
1001
|
+
return $childIterator(lexical.$getChildCaret(node, 'next'));
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
1005
|
+
* Return an iterator that yields each child of node from last to first, taking
|
|
1006
|
+
* care to preserve the previous sibling before yielding the value in case the caller
|
|
1007
|
+
* removes the yielded node.
|
|
1008
|
+
*
|
|
1009
|
+
* @param node The node whose children to iterate
|
|
1010
|
+
* @returns An iterator of the node's children
|
|
1011
|
+
*/
|
|
1012
|
+
function $lastToFirstIterator(node) {
|
|
1013
|
+
return $childIterator(lexical.$getChildCaret(node, 'previous'));
|
|
1014
|
+
}
|
|
1015
|
+
function $childIterator(startCaret) {
|
|
1016
|
+
const seen = new Set() ;
|
|
1017
|
+
return lexical.makeStepwiseIterator({
|
|
1018
|
+
hasNext: lexical.$isSiblingCaret,
|
|
1019
|
+
initial: startCaret.getAdjacentCaret(),
|
|
1020
|
+
map: caret => {
|
|
1021
|
+
const origin = caret.origin.getLatest();
|
|
1022
|
+
if (seen !== null) {
|
|
1023
|
+
const key = origin.getKey();
|
|
1024
|
+
if (!!seen.has(key)) {
|
|
1025
|
+
formatDevErrorMessage(`$childIterator: Cycle detected, node with key ${String(key)} has already been traversed`);
|
|
1026
|
+
}
|
|
1027
|
+
seen.add(key);
|
|
1028
|
+
}
|
|
1029
|
+
return origin;
|
|
1030
|
+
},
|
|
1031
|
+
step: caret => caret.getAdjacentCaret()
|
|
1032
|
+
});
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
/**
|
|
1036
|
+
* Replace this node with its children
|
|
1037
|
+
*
|
|
1038
|
+
* @param node The ElementNode to unwrap and remove
|
|
1039
|
+
*/
|
|
1040
|
+
function $unwrapNode(node) {
|
|
1041
|
+
lexical.$rewindSiblingCaret(lexical.$getSiblingCaret(node, 'next')).splice(1, node.getChildren());
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
/**
|
|
1045
|
+
* Returns the Node sibling when this exists, otherwise the closest parent sibling. For example
|
|
1046
|
+
* R -> P -> T1, T2
|
|
1047
|
+
* -> P2
|
|
1048
|
+
* returns T2 for node T1, P2 for node T2, and null for node P2.
|
|
1049
|
+
* @param node LexicalNode.
|
|
1050
|
+
* @returns An array (tuple) containing the found Lexical node and the depth difference, or null, if this node doesn't exist.
|
|
1051
|
+
*/
|
|
1052
|
+
function $getAdjacentSiblingOrParentSiblingCaret(startCaret, rootMode = 'root') {
|
|
1053
|
+
let depthDiff = 0;
|
|
1054
|
+
let caret = startCaret;
|
|
1055
|
+
let nextCaret = lexical.$getAdjacentChildCaret(caret);
|
|
1056
|
+
while (nextCaret === null) {
|
|
1057
|
+
depthDiff--;
|
|
1058
|
+
nextCaret = caret.getParentCaret(rootMode);
|
|
1059
|
+
if (!nextCaret) {
|
|
1060
|
+
return null;
|
|
1061
|
+
}
|
|
1062
|
+
caret = nextCaret;
|
|
1063
|
+
nextCaret = lexical.$getAdjacentChildCaret(caret);
|
|
1064
|
+
}
|
|
1065
|
+
return nextCaret && [nextCaret, depthDiff];
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
/**
|
|
1069
|
+
* A wrapper that creates bound functions and methods for the
|
|
1070
|
+
* StateConfig to save some boilerplate when defining methods
|
|
1071
|
+
* or exporting only the accessors from your modules rather
|
|
1072
|
+
* than exposing the StateConfig directly.
|
|
1073
|
+
*/
|
|
1074
|
+
|
|
1075
|
+
/**
|
|
1076
|
+
* EXPERIMENTAL
|
|
1077
|
+
*
|
|
1078
|
+
* A convenience interface for working with {@link $getState} and
|
|
1079
|
+
* {@link $setState}.
|
|
1080
|
+
*
|
|
1081
|
+
* @param stateConfig The stateConfig to wrap with convenience functionality
|
|
1082
|
+
* @returns a StateWrapper
|
|
1083
|
+
*/
|
|
1084
|
+
function makeStateWrapper(stateConfig) {
|
|
1085
|
+
const $get = node => lexical.$getState(node, stateConfig);
|
|
1086
|
+
const $set = (node, valueOrUpdater) => lexical.$setState(node, stateConfig, valueOrUpdater);
|
|
1087
|
+
return {
|
|
1088
|
+
$get,
|
|
1089
|
+
$set,
|
|
1090
|
+
accessors: [$get, $set],
|
|
1091
|
+
makeGetterMethod: () => function $getter() {
|
|
1092
|
+
return $get(this);
|
|
1093
|
+
},
|
|
1094
|
+
makeSetterMethod: () => function $setter(valueOrUpdater) {
|
|
1095
|
+
return $set(this, valueOrUpdater);
|
|
1096
|
+
},
|
|
1097
|
+
stateConfig
|
|
1098
|
+
};
|
|
1099
|
+
}
|
|
1100
|
+
|
|
838
1101
|
Object.defineProperty(exports, "$splitNode", {
|
|
839
1102
|
enumerable: true,
|
|
840
1103
|
get: function () { return lexical.$splitNode; }
|
|
@@ -855,16 +1118,29 @@ Object.defineProperty(exports, "isInlineDomNode", {
|
|
|
855
1118
|
enumerable: true,
|
|
856
1119
|
get: function () { return lexical.isInlineDomNode; }
|
|
857
1120
|
});
|
|
1121
|
+
exports.$descendantsMatching = $descendantsMatching;
|
|
858
1122
|
exports.$dfs = $dfs;
|
|
1123
|
+
exports.$dfsIterator = $dfsIterator;
|
|
859
1124
|
exports.$filter = $filter;
|
|
860
1125
|
exports.$findMatchingParent = $findMatchingParent;
|
|
1126
|
+
exports.$firstToLastIterator = $firstToLastIterator;
|
|
1127
|
+
exports.$getAdjacentCaret = $getAdjacentCaret;
|
|
1128
|
+
exports.$getAdjacentSiblingOrParentSiblingCaret = $getAdjacentSiblingOrParentSiblingCaret;
|
|
1129
|
+
exports.$getDepth = $getDepth;
|
|
861
1130
|
exports.$getNearestBlockElementAncestorOrThrow = $getNearestBlockElementAncestorOrThrow;
|
|
862
1131
|
exports.$getNearestNodeOfType = $getNearestNodeOfType;
|
|
863
1132
|
exports.$getNextRightPreorderNode = $getNextRightPreorderNode;
|
|
1133
|
+
exports.$getNextSiblingOrParentSibling = $getNextSiblingOrParentSibling;
|
|
864
1134
|
exports.$insertFirst = $insertFirst;
|
|
865
1135
|
exports.$insertNodeToNearestRoot = $insertNodeToNearestRoot;
|
|
1136
|
+
exports.$insertNodeToNearestRootAtCaret = $insertNodeToNearestRootAtCaret;
|
|
866
1137
|
exports.$isEditorIsNestedEditor = $isEditorIsNestedEditor;
|
|
1138
|
+
exports.$lastToFirstIterator = $lastToFirstIterator;
|
|
867
1139
|
exports.$restoreEditorState = $restoreEditorState;
|
|
1140
|
+
exports.$reverseDfs = $reverseDfs;
|
|
1141
|
+
exports.$reverseDfsIterator = $reverseDfsIterator;
|
|
1142
|
+
exports.$unwrapAndFilterDescendants = $unwrapAndFilterDescendants;
|
|
1143
|
+
exports.$unwrapNode = $unwrapNode;
|
|
868
1144
|
exports.$wrapNodeInElement = $wrapNodeInElement;
|
|
869
1145
|
exports.CAN_USE_BEFORE_INPUT = CAN_USE_BEFORE_INPUT;
|
|
870
1146
|
exports.CAN_USE_DOM = CAN_USE_DOM;
|
|
@@ -879,10 +1155,12 @@ exports.IS_SAFARI = IS_SAFARI;
|
|
|
879
1155
|
exports.addClassNamesToElement = addClassNamesToElement;
|
|
880
1156
|
exports.calculateZoomLevel = calculateZoomLevel;
|
|
881
1157
|
exports.isMimeType = isMimeType;
|
|
1158
|
+
exports.makeStateWrapper = makeStateWrapper;
|
|
882
1159
|
exports.markSelection = markSelection;
|
|
883
1160
|
exports.mediaFileReader = mediaFileReader;
|
|
884
1161
|
exports.mergeRegister = mergeRegister;
|
|
885
1162
|
exports.objectKlassEquals = objectKlassEquals;
|
|
886
|
-
exports.positionNodeOnRange =
|
|
1163
|
+
exports.positionNodeOnRange = mlcPositionNodeOnRange;
|
|
887
1164
|
exports.registerNestedElementResolver = registerNestedElementResolver;
|
|
888
1165
|
exports.removeClassNamesFromElement = removeClassNamesFromElement;
|
|
1166
|
+
exports.selectionAlwaysOnDisplay = selectionAlwaysOnDisplay;
|