@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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import { $getRoot, $isElementNode, $cloneWithProperties, $setSelection, $getSelection, $getPreviousSelection, $isRangeSelection, $
|
|
3
|
+
import { $getRoot, $isElementNode, $getChildCaret, $getSiblingCaret, $getAdjacentChildCaret, $getChildCaretOrSelf, makeStepwiseIterator, $isChildCaret, $cloneWithProperties, $setSelection, $getSelection, $getPreviousSelection, $isRangeSelection, $caretFromPoint, $createParagraphNode, $normalizeCaret, $setSelectionFromCaretRange, $getCollapsedCaretRange, $getCaretInDirection, $splitAtPointCaretNext, $isTextPointCaret, $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
|
*
|
|
@@ -92,7 +106,7 @@ function normalizeClassNames(...classNames) {
|
|
|
92
106
|
* In this case, useEffect is returning the function returned by mergeRegister as a cleanup
|
|
93
107
|
* function to be executed after either the useEffect runs again (due to one of its dependencies
|
|
94
108
|
* updating) or the component it resides in unmounts.
|
|
95
|
-
* Note the functions don't
|
|
109
|
+
* Note the functions don't necessarily need to be in an array as all arguments
|
|
96
110
|
* are considered to be the func argument and spread from there.
|
|
97
111
|
* The order of cleanup is the reverse of the argument order. Generally it is
|
|
98
112
|
* expected that the first "acquire" will be "released" last (LIFO order),
|
|
@@ -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,40 @@ function positionNodeOnRange(editor, range, onReposition) {
|
|
|
258
280
|
*
|
|
259
281
|
*/
|
|
260
282
|
|
|
283
|
+
function $getOrderedSelectionPoints(selection) {
|
|
284
|
+
const points = selection.getStartEndPoints();
|
|
285
|
+
return selection.isBackward() ? [points[1], points[0]] : points;
|
|
286
|
+
}
|
|
287
|
+
function rangeTargetFromPoint(point, node, dom) {
|
|
288
|
+
if (point.type === 'text' || !$isElementNode(node)) {
|
|
289
|
+
const textDOM = getDOMTextNode(dom) || dom;
|
|
290
|
+
return [textDOM, point.offset];
|
|
291
|
+
} else {
|
|
292
|
+
const slot = node.getDOMSlot(dom);
|
|
293
|
+
return [slot.element, slot.getFirstChildOffset() + point.offset];
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
function rangeFromPoints(editor, start, startNode, startDOM, end, endNode, endDOM) {
|
|
297
|
+
const editorDocument = editor._window ? editor._window.document : document;
|
|
298
|
+
const range = editorDocument.createRange();
|
|
299
|
+
range.setStart(...rangeTargetFromPoint(start, startNode, startDOM));
|
|
300
|
+
range.setEnd(...rangeTargetFromPoint(end, endNode, endDOM));
|
|
301
|
+
return range;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Place one or multiple newly created Nodes at the current selection. Multiple
|
|
305
|
+
* nodes will only be created when the selection spans multiple lines (aka
|
|
306
|
+
* client rects).
|
|
307
|
+
*
|
|
308
|
+
* This function can come useful when you want to show the selection but the
|
|
309
|
+
* editor has been focused away.
|
|
310
|
+
*/
|
|
261
311
|
function markSelection(editor, onReposition) {
|
|
262
312
|
let previousAnchorNode = null;
|
|
313
|
+
let previousAnchorNodeDOM = null;
|
|
263
314
|
let previousAnchorOffset = null;
|
|
264
315
|
let previousFocusNode = null;
|
|
316
|
+
let previousFocusNodeDOM = null;
|
|
265
317
|
let previousFocusOffset = null;
|
|
266
318
|
let removeRangeListener = () => {};
|
|
267
319
|
function compute(editorState) {
|
|
@@ -277,53 +329,22 @@ function markSelection(editor, onReposition) {
|
|
|
277
329
|
removeRangeListener = () => {};
|
|
278
330
|
return;
|
|
279
331
|
}
|
|
280
|
-
const
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
const
|
|
285
|
-
const
|
|
286
|
-
const
|
|
287
|
-
const
|
|
288
|
-
const
|
|
289
|
-
const
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
const focusHTMLElement = editor.getElementByKey(focus.getNode().getKey());
|
|
297
|
-
// TODO handle selection beyond the common TextNode
|
|
298
|
-
if (anchorHTMLElement !== null && focusHTMLElement !== null && anchorHTMLElement.tagName === 'SPAN' && focusHTMLElement.tagName === 'SPAN') {
|
|
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 => {
|
|
332
|
+
const [start, end] = $getOrderedSelectionPoints(selection);
|
|
333
|
+
const currentStartNode = start.getNode();
|
|
334
|
+
const currentStartNodeKey = currentStartNode.getKey();
|
|
335
|
+
const currentStartOffset = start.offset;
|
|
336
|
+
const currentEndNode = end.getNode();
|
|
337
|
+
const currentEndNodeKey = currentEndNode.getKey();
|
|
338
|
+
const currentEndOffset = end.offset;
|
|
339
|
+
const currentStartNodeDOM = editor.getElementByKey(currentStartNodeKey);
|
|
340
|
+
const currentEndNodeDOM = editor.getElementByKey(currentEndNodeKey);
|
|
341
|
+
const differentStartDOM = previousAnchorNode === null || currentStartNodeDOM !== previousAnchorNodeDOM || currentStartOffset !== previousAnchorOffset || currentStartNodeKey !== previousAnchorNode.getKey();
|
|
342
|
+
const differentEndDOM = previousFocusNode === null || currentEndNodeDOM !== previousFocusNodeDOM || currentEndOffset !== previousFocusOffset || currentEndNodeKey !== previousFocusNode.getKey();
|
|
343
|
+
if ((differentStartDOM || differentEndDOM) && currentStartNodeDOM !== null && currentEndNodeDOM !== null) {
|
|
344
|
+
const range = rangeFromPoints(editor, start, currentStartNode, currentStartNodeDOM, end, currentEndNode, currentEndNodeDOM);
|
|
345
|
+
removeRangeListener();
|
|
346
|
+
removeRangeListener = mlcPositionNodeOnRange(editor, range, domNodes => {
|
|
347
|
+
if (onReposition === undefined) {
|
|
327
348
|
for (const domNode of domNodes) {
|
|
328
349
|
const domNodeStyle = domNode.style;
|
|
329
350
|
if (domNodeStyle.background !== 'Highlight') {
|
|
@@ -332,12 +353,6 @@ function markSelection(editor, onReposition) {
|
|
|
332
353
|
if (domNodeStyle.color !== 'HighlightText') {
|
|
333
354
|
domNodeStyle.color = 'HighlightText';
|
|
334
355
|
}
|
|
335
|
-
if (domNodeStyle.zIndex !== '-1') {
|
|
336
|
-
domNodeStyle.zIndex = '-1';
|
|
337
|
-
}
|
|
338
|
-
if (domNodeStyle.pointerEvents !== 'none') {
|
|
339
|
-
domNodeStyle.pointerEvents = 'none';
|
|
340
|
-
}
|
|
341
356
|
if (domNodeStyle.marginTop !== px(-1.5)) {
|
|
342
357
|
domNodeStyle.marginTop = px(-1.5);
|
|
343
358
|
}
|
|
@@ -348,22 +363,23 @@ function markSelection(editor, onReposition) {
|
|
|
348
363
|
domNodeStyle.paddingBottom = px(0);
|
|
349
364
|
}
|
|
350
365
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
}
|
|
366
|
+
} else {
|
|
367
|
+
onReposition(domNodes);
|
|
368
|
+
}
|
|
369
|
+
});
|
|
356
370
|
}
|
|
357
|
-
previousAnchorNode =
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
371
|
+
previousAnchorNode = currentStartNode;
|
|
372
|
+
previousAnchorNodeDOM = currentStartNodeDOM;
|
|
373
|
+
previousAnchorOffset = currentStartOffset;
|
|
374
|
+
previousFocusNode = currentEndNode;
|
|
375
|
+
previousFocusNodeDOM = currentEndNodeDOM;
|
|
376
|
+
previousFocusOffset = currentEndOffset;
|
|
361
377
|
});
|
|
362
378
|
}
|
|
363
379
|
compute(editor.getEditorState());
|
|
364
380
|
return mergeRegister(editor.registerUpdateListener(({
|
|
365
381
|
editorState
|
|
366
|
-
}) => compute(editorState)),
|
|
382
|
+
}) => compute(editorState)), () => {
|
|
367
383
|
removeRangeListener();
|
|
368
384
|
});
|
|
369
385
|
}
|
|
@@ -376,6 +392,33 @@ function markSelection(editor, onReposition) {
|
|
|
376
392
|
*
|
|
377
393
|
*/
|
|
378
394
|
|
|
395
|
+
function selectionAlwaysOnDisplay(editor) {
|
|
396
|
+
let removeSelectionMark = null;
|
|
397
|
+
const onSelectionChange = () => {
|
|
398
|
+
const domSelection = getSelection();
|
|
399
|
+
const domAnchorNode = domSelection && domSelection.anchorNode;
|
|
400
|
+
const editorRootElement = editor.getRootElement();
|
|
401
|
+
const isSelectionInsideEditor = domAnchorNode !== null && editorRootElement !== null && editorRootElement.contains(domAnchorNode);
|
|
402
|
+
if (isSelectionInsideEditor) {
|
|
403
|
+
if (removeSelectionMark !== null) {
|
|
404
|
+
removeSelectionMark();
|
|
405
|
+
removeSelectionMark = null;
|
|
406
|
+
}
|
|
407
|
+
} else {
|
|
408
|
+
if (removeSelectionMark === null) {
|
|
409
|
+
removeSelectionMark = markSelection(editor);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
document.addEventListener('selectionchange', onSelectionChange);
|
|
414
|
+
return () => {
|
|
415
|
+
if (removeSelectionMark !== null) {
|
|
416
|
+
removeSelectionMark();
|
|
417
|
+
}
|
|
418
|
+
document.removeEventListener('selectionchange', onSelectionChange);
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
|
|
379
422
|
// Hotfix to export these with inlined types #5918
|
|
380
423
|
const CAN_USE_BEFORE_INPUT = CAN_USE_BEFORE_INPUT$1;
|
|
381
424
|
const CAN_USE_DOM = CAN_USE_DOM$1;
|
|
@@ -387,6 +430,7 @@ const IS_CHROME = IS_CHROME$1;
|
|
|
387
430
|
const IS_FIREFOX = IS_FIREFOX$1;
|
|
388
431
|
const IS_IOS = IS_IOS$1;
|
|
389
432
|
const IS_SAFARI = IS_SAFARI$1;
|
|
433
|
+
|
|
390
434
|
/**
|
|
391
435
|
* Takes an HTML element and adds the classNames passed within an array,
|
|
392
436
|
* ignoring any non-string types. A space can be used to add multiple classes
|
|
@@ -478,57 +522,99 @@ function mediaFileReader(files, acceptableMimeTypes) {
|
|
|
478
522
|
handleNextFile();
|
|
479
523
|
});
|
|
480
524
|
}
|
|
481
|
-
|
|
482
525
|
/**
|
|
483
526
|
* "Depth-First Search" starts at the root/top node of a tree and goes as far as it can down a branch end
|
|
484
527
|
* before backtracking and finding a new path. Consider solving a maze by hugging either wall, moving down a
|
|
485
528
|
* branch until you hit a dead-end (leaf) and backtracking to find the nearest branching path and repeat.
|
|
486
529
|
* It will then return all the nodes found in the search in an array of objects.
|
|
487
|
-
* @param
|
|
488
|
-
* @param
|
|
530
|
+
* @param startNode - The node to start the search, if omitted, it will start at the root node.
|
|
531
|
+
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
|
|
489
532
|
* @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
|
-
|
|
533
|
+
* \\{depth: number, node: LexicalNode\\} It will always return at least 1 node (the start node).
|
|
534
|
+
*/
|
|
535
|
+
function $dfs(startNode, endNode) {
|
|
536
|
+
return Array.from($dfsIterator(startNode, endNode));
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Get the adjacent caret in the same direction
|
|
541
|
+
*
|
|
542
|
+
* @param caret A caret or null
|
|
543
|
+
* @returns `caret.getAdjacentCaret()` or `null`
|
|
544
|
+
*/
|
|
545
|
+
function $getAdjacentCaret(caret) {
|
|
546
|
+
return caret ? caret.getAdjacentCaret() : null;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* $dfs iterator (right to left). Tree traversal is done on the fly as new values are requested with O(1) memory.
|
|
551
|
+
* @param startNode - The node to start the search, if omitted, it will start at the root node.
|
|
552
|
+
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
|
|
553
|
+
* @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node).
|
|
554
|
+
*/
|
|
555
|
+
function $reverseDfs(startNode, endNode) {
|
|
556
|
+
return Array.from($reverseDfsIterator(startNode, endNode));
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* $dfs iterator (left to right). Tree traversal is done on the fly as new values are requested with O(1) memory.
|
|
561
|
+
* @param startNode - The node to start the search, if omitted, it will start at the root node.
|
|
562
|
+
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
|
|
563
|
+
* @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node).
|
|
564
|
+
*/
|
|
565
|
+
function $dfsIterator(startNode, endNode) {
|
|
566
|
+
return $dfsCaretIterator('next', startNode, endNode);
|
|
567
|
+
}
|
|
568
|
+
function $getEndCaret(startNode, direction) {
|
|
569
|
+
const rval = $getAdjacentSiblingOrParentSiblingCaret($getSiblingCaret(startNode, direction));
|
|
570
|
+
return rval && rval[0];
|
|
571
|
+
}
|
|
572
|
+
function $dfsCaretIterator(direction, startNode, endNode) {
|
|
573
|
+
const root = $getRoot();
|
|
574
|
+
const start = startNode || root;
|
|
575
|
+
const startCaret = $isElementNode(start) ? $getChildCaret(start, direction) : $getSiblingCaret(start, direction);
|
|
576
|
+
const startDepth = $getDepth(start);
|
|
577
|
+
const endCaret = endNode ? $getAdjacentChildCaret($getChildCaretOrSelf($getSiblingCaret(endNode, direction))) : $getEndCaret(start, direction);
|
|
578
|
+
let depth = startDepth;
|
|
579
|
+
return makeStepwiseIterator({
|
|
580
|
+
hasNext: state => state !== null,
|
|
581
|
+
initial: startCaret,
|
|
582
|
+
map: state => ({
|
|
500
583
|
depth,
|
|
501
|
-
node
|
|
502
|
-
})
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
} else {
|
|
507
|
-
// Find immediate sibling or nearest parent sibling
|
|
508
|
-
let sibling = null;
|
|
509
|
-
while (sibling === null && node !== null) {
|
|
510
|
-
sibling = node.getNextSibling();
|
|
511
|
-
if (sibling === null) {
|
|
512
|
-
node = node.getParent();
|
|
513
|
-
depth--;
|
|
514
|
-
} else {
|
|
515
|
-
node = sibling;
|
|
516
|
-
}
|
|
584
|
+
node: state.origin
|
|
585
|
+
}),
|
|
586
|
+
step: state => {
|
|
587
|
+
if (state.isSameNodeCaret(endCaret)) {
|
|
588
|
+
return null;
|
|
517
589
|
}
|
|
590
|
+
if ($isChildCaret(state)) {
|
|
591
|
+
depth++;
|
|
592
|
+
}
|
|
593
|
+
const rval = $getAdjacentSiblingOrParentSiblingCaret(state);
|
|
594
|
+
if (!rval || rval[0].isSameNodeCaret(endCaret)) {
|
|
595
|
+
return null;
|
|
596
|
+
}
|
|
597
|
+
depth += rval[1];
|
|
598
|
+
return rval[0];
|
|
518
599
|
}
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Returns the Node sibling when this exists, otherwise the closest parent sibling. For example
|
|
605
|
+
* R -> P -> T1, T2
|
|
606
|
+
* -> P2
|
|
607
|
+
* returns T2 for node T1, P2 for node T2, and null for node P2.
|
|
608
|
+
* @param node LexicalNode.
|
|
609
|
+
* @returns An array (tuple) containing the found Lexical node and the depth difference, or null, if this node doesn't exist.
|
|
610
|
+
*/
|
|
611
|
+
function $getNextSiblingOrParentSibling(node) {
|
|
612
|
+
const rval = $getAdjacentSiblingOrParentSiblingCaret($getSiblingCaret(node, 'next'));
|
|
613
|
+
return rval && [rval[0].origin, rval[1]];
|
|
527
614
|
}
|
|
528
615
|
function $getDepth(node) {
|
|
529
|
-
let
|
|
530
|
-
let
|
|
531
|
-
while ((innerNode = innerNode.getParent()) !== null) {
|
|
616
|
+
let depth = -1;
|
|
617
|
+
for (let innerNode = node; innerNode !== null; innerNode = innerNode.getParent()) {
|
|
532
618
|
depth++;
|
|
533
619
|
}
|
|
534
620
|
return depth;
|
|
@@ -536,28 +622,26 @@ function $getDepth(node) {
|
|
|
536
622
|
|
|
537
623
|
/**
|
|
538
624
|
* Performs a right-to-left preorder tree traversal.
|
|
539
|
-
* From the starting node it goes to the rightmost child, than backtracks to
|
|
625
|
+
* From the starting node it goes to the rightmost child, than backtracks to parent and finds new rightmost path.
|
|
540
626
|
* It will return the next node in traversal sequence after the startingNode.
|
|
541
627
|
* The traversal is similar to $dfs functions above, but the nodes are visited right-to-left, not left-to-right.
|
|
542
628
|
* @param startingNode - The node to start the search.
|
|
543
629
|
* @returns The next node in pre-order right to left traversal sequence or `null`, if the node does not exist
|
|
544
630
|
*/
|
|
545
631
|
function $getNextRightPreorderNode(startingNode) {
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
}
|
|
560
|
-
return node;
|
|
632
|
+
const startCaret = $getChildCaretOrSelf($getSiblingCaret(startingNode, 'previous'));
|
|
633
|
+
const next = $getAdjacentSiblingOrParentSiblingCaret(startCaret, 'root');
|
|
634
|
+
return next && next[0].origin;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* $dfs iterator (right to left). Tree traversal is done on the fly as new values are requested with O(1) memory.
|
|
639
|
+
* @param startNode - The node to start the search, if omitted, it will start at the root node.
|
|
640
|
+
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode.
|
|
641
|
+
* @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node).
|
|
642
|
+
*/
|
|
643
|
+
function $reverseDfsIterator(startNode, endNode) {
|
|
644
|
+
return $dfsCaretIterator('previous', startNode, endNode);
|
|
561
645
|
}
|
|
562
646
|
|
|
563
647
|
/**
|
|
@@ -587,7 +671,7 @@ function $getNearestBlockElementAncestorOrThrow(startNode) {
|
|
|
587
671
|
const blockNode = $findMatchingParent(startNode, node => $isElementNode(node) && !node.isInline());
|
|
588
672
|
if (!$isElementNode(blockNode)) {
|
|
589
673
|
{
|
|
590
|
-
|
|
674
|
+
formatDevErrorMessage(`Expected node ${startNode.__key} to have closest block element node.`);
|
|
591
675
|
}
|
|
592
676
|
}
|
|
593
677
|
return blockNode;
|
|
@@ -704,59 +788,52 @@ function $restoreEditorState(editor, editorState) {
|
|
|
704
788
|
* If the selected insertion area is the root/shadow root node (see {@link lexical!$isRootOrShadowRoot}),
|
|
705
789
|
* the node will be appended there, otherwise, it will be inserted before the insertion area.
|
|
706
790
|
* If there is no selection where the node is to be inserted, it will be appended after any current nodes
|
|
707
|
-
* within the tree, as a child of the root node. A paragraph
|
|
791
|
+
* within the tree, as a child of the root node. A paragraph will then be added after the inserted node and selected.
|
|
708
792
|
* @param node - The node to be inserted
|
|
709
793
|
* @returns The node after its insertion
|
|
710
794
|
*/
|
|
711
795
|
function $insertNodeToNearestRoot(node) {
|
|
712
796
|
const selection = $getSelection() || $getPreviousSelection();
|
|
797
|
+
let initialCaret;
|
|
713
798
|
if ($isRangeSelection(selection)) {
|
|
714
|
-
|
|
715
|
-
focus
|
|
716
|
-
} = selection;
|
|
717
|
-
const focusNode = focus.getNode();
|
|
718
|
-
const focusOffset = focus.offset;
|
|
719
|
-
if ($isRootOrShadowRoot(focusNode)) {
|
|
720
|
-
const focusChild = focusNode.getChildAtIndex(focusOffset);
|
|
721
|
-
if (focusChild == null) {
|
|
722
|
-
focusNode.append(node);
|
|
723
|
-
} else {
|
|
724
|
-
focusChild.insertBefore(node);
|
|
725
|
-
}
|
|
726
|
-
node.selectNext();
|
|
727
|
-
} else {
|
|
728
|
-
let splitNode;
|
|
729
|
-
let splitOffset;
|
|
730
|
-
if ($isTextNode(focusNode)) {
|
|
731
|
-
splitNode = focusNode.getParentOrThrow();
|
|
732
|
-
splitOffset = focusNode.getIndexWithinParent();
|
|
733
|
-
if (focusOffset > 0) {
|
|
734
|
-
splitOffset += 1;
|
|
735
|
-
focusNode.splitText(focusOffset);
|
|
736
|
-
}
|
|
737
|
-
} else {
|
|
738
|
-
splitNode = focusNode;
|
|
739
|
-
splitOffset = focusOffset;
|
|
740
|
-
}
|
|
741
|
-
const [, rightTree] = $splitNode(splitNode, splitOffset);
|
|
742
|
-
rightTree.insertBefore(node);
|
|
743
|
-
rightTree.selectStart();
|
|
744
|
-
}
|
|
799
|
+
initialCaret = $caretFromPoint(selection.focus, 'next');
|
|
745
800
|
} else {
|
|
746
801
|
if (selection != null) {
|
|
747
802
|
const nodes = selection.getNodes();
|
|
748
|
-
nodes[nodes.length - 1]
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
803
|
+
const lastNode = nodes[nodes.length - 1];
|
|
804
|
+
if (lastNode) {
|
|
805
|
+
initialCaret = $getSiblingCaret(lastNode, 'next');
|
|
806
|
+
}
|
|
752
807
|
}
|
|
753
|
-
|
|
754
|
-
node.insertAfter(paragraphNode);
|
|
755
|
-
paragraphNode.select();
|
|
808
|
+
initialCaret = initialCaret || $getChildCaret($getRoot(), 'previous').getFlipped().insert($createParagraphNode());
|
|
756
809
|
}
|
|
810
|
+
const insertCaret = $insertNodeToNearestRootAtCaret(node, initialCaret);
|
|
811
|
+
const adjacent = $getAdjacentChildCaret(insertCaret);
|
|
812
|
+
const selectionCaret = $isChildCaret(adjacent) ? $normalizeCaret(adjacent) : insertCaret;
|
|
813
|
+
$setSelectionFromCaretRange($getCollapsedCaretRange(selectionCaret));
|
|
757
814
|
return node.getLatest();
|
|
758
815
|
}
|
|
759
816
|
|
|
817
|
+
/**
|
|
818
|
+
* If the insertion caret is the root/shadow root node (see {@link lexical!$isRootOrShadowRoot}),
|
|
819
|
+
* the node will be inserted there, otherwise the parent nodes will be split according to the
|
|
820
|
+
* given options.
|
|
821
|
+
* @param node - The node to be inserted
|
|
822
|
+
* @param caret - The location to insert or split from
|
|
823
|
+
* @returns The node after its insertion
|
|
824
|
+
*/
|
|
825
|
+
function $insertNodeToNearestRootAtCaret(node, caret, options) {
|
|
826
|
+
let insertCaret = $getCaretInDirection(caret, 'next');
|
|
827
|
+
for (let nextCaret = insertCaret; nextCaret; nextCaret = $splitAtPointCaretNext(nextCaret, options)) {
|
|
828
|
+
insertCaret = nextCaret;
|
|
829
|
+
}
|
|
830
|
+
if (!!$isTextPointCaret(insertCaret)) {
|
|
831
|
+
formatDevErrorMessage(`$insertNodeToNearestRootAtCaret: An unattached TextNode can not be split`);
|
|
832
|
+
}
|
|
833
|
+
insertCaret.insert(node.isInline() ? $createParagraphNode().append(node) : node);
|
|
834
|
+
return $getCaretInDirection($getSiblingCaret(node.getLatest(), 'next'), caret.direction);
|
|
835
|
+
}
|
|
836
|
+
|
|
760
837
|
/**
|
|
761
838
|
* Wraps the node into another node created from a createElementNode function, eg. $createParagraphNode
|
|
762
839
|
* @param node - Node to be wrapped.
|
|
@@ -775,7 +852,7 @@ function $wrapNodeInElement(node, createElementNode) {
|
|
|
775
852
|
/**
|
|
776
853
|
* @param object = The instance of the type
|
|
777
854
|
* @param objectClass = The class of the type
|
|
778
|
-
* @returns Whether the object is has the same Klass of the objectClass, ignoring the difference across window (e.g. different
|
|
855
|
+
* @returns Whether the object is has the same Klass of the objectClass, ignoring the difference across window (e.g. different iframes)
|
|
779
856
|
*/
|
|
780
857
|
function objectKlassEquals(object, objectClass) {
|
|
781
858
|
return object !== null ? Object.getPrototypeOf(object).constructor.name === objectClass.name : false;
|
|
@@ -804,27 +881,39 @@ function $filter(nodes, filterFn) {
|
|
|
804
881
|
* @param node Node that needs to be appended
|
|
805
882
|
*/
|
|
806
883
|
function $insertFirst(parent, node) {
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
884
|
+
$getChildCaret(parent, 'next').insert(node);
|
|
885
|
+
}
|
|
886
|
+
let NEEDS_MANUAL_ZOOM = IS_FIREFOX || !CAN_USE_DOM ? false : undefined;
|
|
887
|
+
function needsManualZoom() {
|
|
888
|
+
if (NEEDS_MANUAL_ZOOM === undefined) {
|
|
889
|
+
// If the browser implements standardized CSS zoom, then the client rect
|
|
890
|
+
// will be wider after zoom is applied
|
|
891
|
+
// https://chromestatus.com/feature/5198254868529152
|
|
892
|
+
// https://github.com/facebook/lexical/issues/6863
|
|
893
|
+
const div = document.createElement('div');
|
|
894
|
+
div.style.cssText = 'position: absolute; opacity: 0; width: 100px; left: -1000px;';
|
|
895
|
+
document.body.appendChild(div);
|
|
896
|
+
const noZoom = div.getBoundingClientRect();
|
|
897
|
+
div.style.setProperty('zoom', '2');
|
|
898
|
+
NEEDS_MANUAL_ZOOM = div.getBoundingClientRect().width === noZoom.width;
|
|
899
|
+
document.body.removeChild(div);
|
|
812
900
|
}
|
|
901
|
+
return NEEDS_MANUAL_ZOOM;
|
|
813
902
|
}
|
|
814
903
|
|
|
815
904
|
/**
|
|
816
905
|
* Calculates the zoom level of an element as a result of using
|
|
817
|
-
* css zoom property.
|
|
906
|
+
* css zoom property. For browsers that implement standardized CSS
|
|
907
|
+
* zoom (Firefox, Chrome >= 128), this will always return 1.
|
|
818
908
|
* @param element
|
|
819
909
|
*/
|
|
820
910
|
function calculateZoomLevel(element) {
|
|
821
|
-
if (IS_FIREFOX) {
|
|
822
|
-
return 1;
|
|
823
|
-
}
|
|
824
911
|
let zoom = 1;
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
912
|
+
if (needsManualZoom()) {
|
|
913
|
+
while (element) {
|
|
914
|
+
zoom *= Number(window.getComputedStyle(element).getPropertyValue('zoom'));
|
|
915
|
+
element = element.parentElement;
|
|
916
|
+
}
|
|
828
917
|
}
|
|
829
918
|
return zoom;
|
|
830
919
|
}
|
|
@@ -836,4 +925,178 @@ function $isEditorIsNestedEditor(editor) {
|
|
|
836
925
|
return editor._parentEditor !== null;
|
|
837
926
|
}
|
|
838
927
|
|
|
839
|
-
|
|
928
|
+
/**
|
|
929
|
+
* A depth first last-to-first traversal of root that stops at each node that matches
|
|
930
|
+
* $predicate and ensures that its parent is root. This is typically used to discard
|
|
931
|
+
* invalid or unsupported wrapping nodes. For example, a TableNode must only have
|
|
932
|
+
* TableRowNode as children, but an importer might add invalid nodes based on
|
|
933
|
+
* caption, tbody, thead, etc. and this will unwrap and discard those.
|
|
934
|
+
*
|
|
935
|
+
* @param root The root to start the traversal
|
|
936
|
+
* @param $predicate Should return true for nodes that are permitted to be children of root
|
|
937
|
+
* @returns true if this unwrapped or removed any nodes
|
|
938
|
+
*/
|
|
939
|
+
function $unwrapAndFilterDescendants(root, $predicate) {
|
|
940
|
+
return $unwrapAndFilterDescendantsImpl(root, $predicate, null);
|
|
941
|
+
}
|
|
942
|
+
function $unwrapAndFilterDescendantsImpl(root, $predicate, $onSuccess) {
|
|
943
|
+
let didMutate = false;
|
|
944
|
+
for (const node of $lastToFirstIterator(root)) {
|
|
945
|
+
if ($predicate(node)) {
|
|
946
|
+
if ($onSuccess !== null) {
|
|
947
|
+
$onSuccess(node);
|
|
948
|
+
}
|
|
949
|
+
continue;
|
|
950
|
+
}
|
|
951
|
+
didMutate = true;
|
|
952
|
+
if ($isElementNode(node)) {
|
|
953
|
+
$unwrapAndFilterDescendantsImpl(node, $predicate, $onSuccess || (child => node.insertAfter(child)));
|
|
954
|
+
}
|
|
955
|
+
node.remove();
|
|
956
|
+
}
|
|
957
|
+
return didMutate;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/**
|
|
961
|
+
* A depth first traversal of the children array that stops at and collects
|
|
962
|
+
* each node that `$predicate` matches. This is typically used to discard
|
|
963
|
+
* invalid or unsupported wrapping nodes on a children array in the `after`
|
|
964
|
+
* of an {@link lexical!DOMConversionOutput}. For example, a TableNode must only have
|
|
965
|
+
* TableRowNode as children, but an importer might add invalid nodes based on
|
|
966
|
+
* caption, tbody, thead, etc. and this will unwrap and discard those.
|
|
967
|
+
*
|
|
968
|
+
* This function is read-only and performs no mutation operations, which makes
|
|
969
|
+
* it suitable for import and export purposes but likely not for any in-place
|
|
970
|
+
* mutation. You should use {@link $unwrapAndFilterDescendants} for in-place
|
|
971
|
+
* mutations such as node transforms.
|
|
972
|
+
*
|
|
973
|
+
* @param children The children to traverse
|
|
974
|
+
* @param $predicate Should return true for nodes that are permitted to be children of root
|
|
975
|
+
* @returns The children or their descendants that match $predicate
|
|
976
|
+
*/
|
|
977
|
+
|
|
978
|
+
function $descendantsMatching(children, $predicate) {
|
|
979
|
+
const result = [];
|
|
980
|
+
const stack = Array.from(children).reverse();
|
|
981
|
+
for (let child = stack.pop(); child !== undefined; child = stack.pop()) {
|
|
982
|
+
if ($predicate(child)) {
|
|
983
|
+
result.push(child);
|
|
984
|
+
} else if ($isElementNode(child)) {
|
|
985
|
+
for (const grandchild of $lastToFirstIterator(child)) {
|
|
986
|
+
stack.push(grandchild);
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
return result;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
/**
|
|
994
|
+
* Return an iterator that yields each child of node from first to last, taking
|
|
995
|
+
* care to preserve the next sibling before yielding the value in case the caller
|
|
996
|
+
* removes the yielded node.
|
|
997
|
+
*
|
|
998
|
+
* @param node The node whose children to iterate
|
|
999
|
+
* @returns An iterator of the node's children
|
|
1000
|
+
*/
|
|
1001
|
+
function $firstToLastIterator(node) {
|
|
1002
|
+
return $childIterator($getChildCaret(node, 'next'));
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
/**
|
|
1006
|
+
* Return an iterator that yields each child of node from last to first, taking
|
|
1007
|
+
* care to preserve the previous sibling before yielding the value in case the caller
|
|
1008
|
+
* removes the yielded node.
|
|
1009
|
+
*
|
|
1010
|
+
* @param node The node whose children to iterate
|
|
1011
|
+
* @returns An iterator of the node's children
|
|
1012
|
+
*/
|
|
1013
|
+
function $lastToFirstIterator(node) {
|
|
1014
|
+
return $childIterator($getChildCaret(node, 'previous'));
|
|
1015
|
+
}
|
|
1016
|
+
function $childIterator(startCaret) {
|
|
1017
|
+
const seen = new Set() ;
|
|
1018
|
+
return makeStepwiseIterator({
|
|
1019
|
+
hasNext: $isSiblingCaret,
|
|
1020
|
+
initial: startCaret.getAdjacentCaret(),
|
|
1021
|
+
map: caret => {
|
|
1022
|
+
const origin = caret.origin.getLatest();
|
|
1023
|
+
if (seen !== null) {
|
|
1024
|
+
const key = origin.getKey();
|
|
1025
|
+
if (!!seen.has(key)) {
|
|
1026
|
+
formatDevErrorMessage(`$childIterator: Cycle detected, node with key ${String(key)} has already been traversed`);
|
|
1027
|
+
}
|
|
1028
|
+
seen.add(key);
|
|
1029
|
+
}
|
|
1030
|
+
return origin;
|
|
1031
|
+
},
|
|
1032
|
+
step: caret => caret.getAdjacentCaret()
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
/**
|
|
1037
|
+
* Replace this node with its children
|
|
1038
|
+
*
|
|
1039
|
+
* @param node The ElementNode to unwrap and remove
|
|
1040
|
+
*/
|
|
1041
|
+
function $unwrapNode(node) {
|
|
1042
|
+
$rewindSiblingCaret($getSiblingCaret(node, 'next')).splice(1, node.getChildren());
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
/**
|
|
1046
|
+
* Returns the Node sibling when this exists, otherwise the closest parent sibling. For example
|
|
1047
|
+
* R -> P -> T1, T2
|
|
1048
|
+
* -> P2
|
|
1049
|
+
* returns T2 for node T1, P2 for node T2, and null for node P2.
|
|
1050
|
+
* @param node LexicalNode.
|
|
1051
|
+
* @returns An array (tuple) containing the found Lexical node and the depth difference, or null, if this node doesn't exist.
|
|
1052
|
+
*/
|
|
1053
|
+
function $getAdjacentSiblingOrParentSiblingCaret(startCaret, rootMode = 'root') {
|
|
1054
|
+
let depthDiff = 0;
|
|
1055
|
+
let caret = startCaret;
|
|
1056
|
+
let nextCaret = $getAdjacentChildCaret(caret);
|
|
1057
|
+
while (nextCaret === null) {
|
|
1058
|
+
depthDiff--;
|
|
1059
|
+
nextCaret = caret.getParentCaret(rootMode);
|
|
1060
|
+
if (!nextCaret) {
|
|
1061
|
+
return null;
|
|
1062
|
+
}
|
|
1063
|
+
caret = nextCaret;
|
|
1064
|
+
nextCaret = $getAdjacentChildCaret(caret);
|
|
1065
|
+
}
|
|
1066
|
+
return nextCaret && [nextCaret, depthDiff];
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
/**
|
|
1070
|
+
* A wrapper that creates bound functions and methods for the
|
|
1071
|
+
* StateConfig to save some boilerplate when defining methods
|
|
1072
|
+
* or exporting only the accessors from your modules rather
|
|
1073
|
+
* than exposing the StateConfig directly.
|
|
1074
|
+
*/
|
|
1075
|
+
|
|
1076
|
+
/**
|
|
1077
|
+
* EXPERIMENTAL
|
|
1078
|
+
*
|
|
1079
|
+
* A convenience interface for working with {@link $getState} and
|
|
1080
|
+
* {@link $setState}.
|
|
1081
|
+
*
|
|
1082
|
+
* @param stateConfig The stateConfig to wrap with convenience functionality
|
|
1083
|
+
* @returns a StateWrapper
|
|
1084
|
+
*/
|
|
1085
|
+
function makeStateWrapper(stateConfig) {
|
|
1086
|
+
const $get = node => $getState(node, stateConfig);
|
|
1087
|
+
const $set = (node, valueOrUpdater) => $setState(node, stateConfig, valueOrUpdater);
|
|
1088
|
+
return {
|
|
1089
|
+
$get,
|
|
1090
|
+
$set,
|
|
1091
|
+
accessors: [$get, $set],
|
|
1092
|
+
makeGetterMethod: () => function $getter() {
|
|
1093
|
+
return $get(this);
|
|
1094
|
+
},
|
|
1095
|
+
makeSetterMethod: () => function $setter(valueOrUpdater) {
|
|
1096
|
+
return $set(this, valueOrUpdater);
|
|
1097
|
+
},
|
|
1098
|
+
stateConfig
|
|
1099
|
+
};
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
export { $descendantsMatching, $dfs, $dfsIterator, $filter, $findMatchingParent, $firstToLastIterator, $getAdjacentCaret, $getAdjacentSiblingOrParentSiblingCaret, $getDepth, $getNearestBlockElementAncestorOrThrow, $getNearestNodeOfType, $getNextRightPreorderNode, $getNextSiblingOrParentSibling, $insertFirst, $insertNodeToNearestRoot, $insertNodeToNearestRootAtCaret, $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 };
|