@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,7 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import { $isTextNode, $
|
|
4
|
-
export { $cloneWithProperties } from 'lexical';
|
|
3
|
+
import { $isTextNode, $getEditor, $isRootNode, $isTokenOrSegmented, $getCharacterOffsets, $isElementNode, $getNodeByKey, $getPreviousSelection, $createTextNode, $isRangeSelection, $getSelection, $caretRangeFromSelection, $createRangeSelection, INTERNAL_$isBlock, $setSelection, $isRootOrShadowRoot, $hasAncestor, $isLeafNode, $caretFromPoint, $isExtendableTextPointCaret, $extendCaretToRange, $isChildCaret, $isDecoratorNode } from 'lexical';
|
|
4
|
+
export { $cloneWithProperties, $selectAll } from 'lexical';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
@@ -12,6 +12,20 @@ export { $cloneWithProperties } from 'lexical';
|
|
|
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
|
*
|
|
@@ -147,6 +161,9 @@ function createRectsFromDOMRange(editor, range) {
|
|
|
147
161
|
*/
|
|
148
162
|
function getStyleObjectFromRawCSS(css) {
|
|
149
163
|
const styleObject = {};
|
|
164
|
+
if (!css) {
|
|
165
|
+
return styleObject;
|
|
166
|
+
}
|
|
150
167
|
const styles = css.split(';');
|
|
151
168
|
for (const style of styles) {
|
|
152
169
|
if (style !== '') {
|
|
@@ -193,12 +210,42 @@ function getCSSFromStyleObject(styles) {
|
|
|
193
210
|
}
|
|
194
211
|
|
|
195
212
|
/**
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
|
|
200
|
-
|
|
213
|
+
* Gets the computed DOM styles of the element.
|
|
214
|
+
* @param node - The node to check the styles for.
|
|
215
|
+
* @returns the computed styles of the element or null if there is no DOM element or no default view for the document.
|
|
216
|
+
*/
|
|
217
|
+
function $getComputedStyleForElement(element) {
|
|
218
|
+
const editor = $getEditor();
|
|
219
|
+
const domElement = editor.getElementByKey(element.getKey());
|
|
220
|
+
if (domElement === null) {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
const view = domElement.ownerDocument.defaultView;
|
|
224
|
+
if (view === null) {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
return view.getComputedStyle(domElement);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Gets the computed DOM styles of the parent of the node.
|
|
232
|
+
* @param node - The node to check its parent's styles for.
|
|
233
|
+
* @returns the computed styles of the node or null if there is no DOM element or no default view for the document.
|
|
234
|
+
*/
|
|
235
|
+
function $getComputedStyleForParent(node) {
|
|
236
|
+
const parent = $isRootNode(node) ? node : node.getParentOrThrow();
|
|
237
|
+
return $getComputedStyleForElement(parent);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Determines whether a node's parent is RTL.
|
|
242
|
+
* @param node - The node to check whether it is RTL.
|
|
243
|
+
* @returns whether the node is RTL.
|
|
201
244
|
*/
|
|
245
|
+
function $isParentRTL(node) {
|
|
246
|
+
const styles = $getComputedStyleForParent(node);
|
|
247
|
+
return styles !== null && styles.direction === 'rtl';
|
|
248
|
+
}
|
|
202
249
|
|
|
203
250
|
/**
|
|
204
251
|
* Generally used to append text content to HTML and JSON. Grabs the text content and "slices"
|
|
@@ -209,7 +256,7 @@ function getCSSFromStyleObject(styles) {
|
|
|
209
256
|
*/
|
|
210
257
|
function $sliceSelectedTextNodeContent(selection, textNode) {
|
|
211
258
|
const anchorAndFocus = selection.getStartEndPoints();
|
|
212
|
-
if (textNode.isSelected(selection) &&
|
|
259
|
+
if (textNode.isSelected(selection) && !$isTokenOrSegmented(textNode) && anchorAndFocus !== null) {
|
|
213
260
|
const [anchor, focus] = anchorAndFocus;
|
|
214
261
|
const isBackward = selection.isBackward();
|
|
215
262
|
const anchorNode = anchor.getNode();
|
|
@@ -235,8 +282,11 @@ function $sliceSelectedTextNodeContent(selection, textNode) {
|
|
|
235
282
|
startOffset = 0;
|
|
236
283
|
endOffset = offset;
|
|
237
284
|
}
|
|
285
|
+
|
|
286
|
+
// NOTE: This mutates __text directly because the primary use case is to
|
|
287
|
+
// modify a $cloneWithProperties node that should never be added
|
|
288
|
+
// to the EditorState so we must not call getWritable via setTextContent
|
|
238
289
|
textNode.__text = textNode.__text.slice(startOffset, endOffset);
|
|
239
|
-
return textNode;
|
|
240
290
|
}
|
|
241
291
|
}
|
|
242
292
|
return textNode;
|
|
@@ -253,7 +303,7 @@ function $isAtNodeEnd(point) {
|
|
|
253
303
|
}
|
|
254
304
|
const node = point.getNode();
|
|
255
305
|
if (!$isElementNode(node)) {
|
|
256
|
-
|
|
306
|
+
formatDevErrorMessage(`isAtNodeEnd: node must be a TextNode or ElementNode`);
|
|
257
307
|
}
|
|
258
308
|
return point.offset === node.getChildrenSize();
|
|
259
309
|
}
|
|
@@ -379,8 +429,19 @@ function $addNodeStyle(node) {
|
|
|
379
429
|
const styles = getStyleObjectFromRawCSS(CSSText);
|
|
380
430
|
CSS_TO_STYLES.set(CSSText, styles);
|
|
381
431
|
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Applies the provided styles to the given TextNode, ElementNode, or
|
|
435
|
+
* collapsed RangeSelection.
|
|
436
|
+
*
|
|
437
|
+
* @param target - The TextNode, ElementNode, or collapsed RangeSelection to apply the styles to
|
|
438
|
+
* @param patch - The patch to apply, which can include multiple styles. \\{CSSProperty: value\\} . Can also accept a function that returns the new property value.
|
|
439
|
+
*/
|
|
382
440
|
function $patchStyle(target, patch) {
|
|
383
|
-
|
|
441
|
+
if (!($isRangeSelection(target) ? target.isCollapsed() : $isTextNode(target) || $isElementNode(target))) {
|
|
442
|
+
formatDevErrorMessage(`$patchStyle must only be called with a TextNode, ElementNode, or collapsed RangeSelection`);
|
|
443
|
+
}
|
|
444
|
+
const prevStyles = getStyleObjectFromCSS($isRangeSelection(target) ? target.style : $isTextNode(target) ? target.getStyle() : target.getTextStyle());
|
|
384
445
|
const newStyles = Object.entries(patch).reduce((styles, [key, value]) => {
|
|
385
446
|
if (typeof value === 'function') {
|
|
386
447
|
styles[key] = value(prevStyles[key], target);
|
|
@@ -392,9 +453,13 @@ function $patchStyle(target, patch) {
|
|
|
392
453
|
return styles;
|
|
393
454
|
}, {
|
|
394
455
|
...prevStyles
|
|
395
|
-
}
|
|
456
|
+
});
|
|
396
457
|
const newCSSText = getCSSFromStyleObject(newStyles);
|
|
397
|
-
target
|
|
458
|
+
if ($isRangeSelection(target) || $isTextNode(target)) {
|
|
459
|
+
target.setStyle(newCSSText);
|
|
460
|
+
} else {
|
|
461
|
+
target.setTextStyle(newCSSText);
|
|
462
|
+
}
|
|
398
463
|
CSS_TO_STYLES.set(newCSSText, newStyles);
|
|
399
464
|
}
|
|
400
465
|
|
|
@@ -406,161 +471,152 @@ function $patchStyle(target, patch) {
|
|
|
406
471
|
* @param patch - The patch to apply, which can include multiple styles. \\{CSSProperty: value\\} . Can also accept a function that returns the new property value.
|
|
407
472
|
*/
|
|
408
473
|
function $patchStyleText(selection, patch) {
|
|
409
|
-
|
|
410
|
-
const selectedNodesLength = selectedNodes.length;
|
|
411
|
-
const anchorAndFocus = selection.getStartEndPoints();
|
|
412
|
-
if (anchorAndFocus === null) {
|
|
413
|
-
return;
|
|
414
|
-
}
|
|
415
|
-
const [anchor, focus] = anchorAndFocus;
|
|
416
|
-
const lastIndex = selectedNodesLength - 1;
|
|
417
|
-
let firstNode = selectedNodes[0];
|
|
418
|
-
let lastNode = selectedNodes[lastIndex];
|
|
419
|
-
if (selection.isCollapsed() && $isRangeSelection(selection)) {
|
|
474
|
+
if ($isRangeSelection(selection) && selection.isCollapsed()) {
|
|
420
475
|
$patchStyle(selection, patch);
|
|
476
|
+
const emptyNode = selection.anchor.getNode();
|
|
477
|
+
if ($isElementNode(emptyNode) && emptyNode.isEmpty()) {
|
|
478
|
+
$patchStyle(emptyNode, patch);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
$forEachSelectedTextNode(textNode => {
|
|
482
|
+
$patchStyle(textNode, patch);
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
function $forEachSelectedTextNode(fn) {
|
|
486
|
+
const selection = $getSelection();
|
|
487
|
+
if (!selection) {
|
|
421
488
|
return;
|
|
422
489
|
}
|
|
423
|
-
const
|
|
424
|
-
const
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
let endOffset = isBefore ? focusOffset : anchorOffset;
|
|
430
|
-
const startType = isBefore ? anchor.type : focus.type;
|
|
431
|
-
const endType = isBefore ? focus.type : anchor.type;
|
|
432
|
-
const endKey = isBefore ? focus.key : anchor.key;
|
|
433
|
-
|
|
434
|
-
// This is the case where the user only selected the very end of the
|
|
435
|
-
// first node so we don't want to include it in the formatting change.
|
|
436
|
-
if ($isTextNode(firstNode) && startOffset === firstNodeTextLength) {
|
|
437
|
-
const nextSibling = firstNode.getNextSibling();
|
|
438
|
-
if ($isTextNode(nextSibling)) {
|
|
439
|
-
// we basically make the second node the firstNode, changing offsets accordingly
|
|
440
|
-
anchorOffset = 0;
|
|
441
|
-
startOffset = 0;
|
|
442
|
-
firstNode = nextSibling;
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
// This is the case where we only selected a single node
|
|
447
|
-
if (selectedNodes.length === 1) {
|
|
448
|
-
if ($isTextNode(firstNode) && firstNode.canHaveFormat()) {
|
|
449
|
-
startOffset = startType === 'element' ? 0 : anchorOffset > focusOffset ? focusOffset : anchorOffset;
|
|
450
|
-
endOffset = endType === 'element' ? firstNodeTextLength : anchorOffset > focusOffset ? anchorOffset : focusOffset;
|
|
451
|
-
|
|
452
|
-
// No actual text is selected, so do nothing.
|
|
453
|
-
if (startOffset === endOffset) {
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
// The entire node is selected or a token/segment, so just format it
|
|
458
|
-
if ($isTokenOrSegmented(firstNode) || startOffset === 0 && endOffset === firstNodeTextLength) {
|
|
459
|
-
$patchStyle(firstNode, patch);
|
|
460
|
-
firstNode.select(startOffset, endOffset);
|
|
461
|
-
} else {
|
|
462
|
-
// The node is partially selected, so split it into two nodes
|
|
463
|
-
// and style the selected one.
|
|
464
|
-
const splitNodes = firstNode.splitText(startOffset, endOffset);
|
|
465
|
-
const replacement = startOffset === 0 ? splitNodes[0] : splitNodes[1];
|
|
466
|
-
$patchStyle(replacement, patch);
|
|
467
|
-
replacement.select(0, endOffset - startOffset);
|
|
468
|
-
}
|
|
469
|
-
} // multiple nodes selected.
|
|
470
|
-
} else {
|
|
471
|
-
if ($isTextNode(firstNode) && startOffset < firstNode.getTextContentSize() && firstNode.canHaveFormat()) {
|
|
472
|
-
if (startOffset !== 0 && !$isTokenOrSegmented(firstNode)) {
|
|
473
|
-
// the entire first node isn't selected and it isn't a token or segmented, so split it
|
|
474
|
-
firstNode = firstNode.splitText(startOffset)[1];
|
|
475
|
-
startOffset = 0;
|
|
476
|
-
if (isBefore) {
|
|
477
|
-
anchor.set(firstNode.getKey(), startOffset, 'text');
|
|
478
|
-
} else {
|
|
479
|
-
focus.set(firstNode.getKey(), startOffset, 'text');
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
$patchStyle(firstNode, patch);
|
|
483
|
-
}
|
|
484
|
-
if ($isTextNode(lastNode) && lastNode.canHaveFormat()) {
|
|
485
|
-
const lastNodeText = lastNode.getTextContent();
|
|
486
|
-
const lastNodeTextLength = lastNodeText.length;
|
|
487
|
-
|
|
488
|
-
// The last node might not actually be the end node
|
|
489
|
-
//
|
|
490
|
-
// If not, assume the last node is fully-selected unless the end offset is
|
|
491
|
-
// zero.
|
|
492
|
-
if (lastNode.__key !== endKey && endOffset !== 0) {
|
|
493
|
-
endOffset = lastNodeTextLength;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
// if the entire last node isn't selected and it isn't a token or segmented, split it
|
|
497
|
-
if (endOffset !== lastNodeTextLength && !$isTokenOrSegmented(lastNode)) {
|
|
498
|
-
[lastNode] = lastNode.splitText(endOffset);
|
|
499
|
-
}
|
|
500
|
-
if (endOffset !== 0 || endType === 'element') {
|
|
501
|
-
$patchStyle(lastNode, patch);
|
|
490
|
+
const slicedTextNodes = new Map();
|
|
491
|
+
const getSliceIndices = node => slicedTextNodes.get(node.getKey()) || [0, node.getTextContentSize()];
|
|
492
|
+
if ($isRangeSelection(selection)) {
|
|
493
|
+
for (const slice of $caretRangeFromSelection(selection).getTextSlices()) {
|
|
494
|
+
if (slice) {
|
|
495
|
+
slicedTextNodes.set(slice.caret.origin.getKey(), slice.getSliceIndices());
|
|
502
496
|
}
|
|
503
497
|
}
|
|
498
|
+
}
|
|
499
|
+
const selectedNodes = selection.getNodes();
|
|
500
|
+
for (const selectedNode of selectedNodes) {
|
|
501
|
+
if (!($isTextNode(selectedNode) && selectedNode.canHaveFormat())) {
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
const [startOffset, endOffset] = getSliceIndices(selectedNode);
|
|
505
|
+
// No actual text is selected, so do nothing.
|
|
506
|
+
if (endOffset === startOffset) {
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
504
509
|
|
|
505
|
-
//
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
510
|
+
// The entire node is selected or a token/segment, so just format it
|
|
511
|
+
if ($isTokenOrSegmented(selectedNode) || startOffset === 0 && endOffset === selectedNode.getTextContentSize()) {
|
|
512
|
+
fn(selectedNode);
|
|
513
|
+
} else {
|
|
514
|
+
// The node is partially selected, so split it into two or three nodes
|
|
515
|
+
// and style the selected one.
|
|
516
|
+
const splitNodes = selectedNode.splitText(startOffset, endOffset);
|
|
517
|
+
const replacement = splitNodes[startOffset === 0 ? 0 : 1];
|
|
518
|
+
fn(replacement);
|
|
512
519
|
}
|
|
513
520
|
}
|
|
521
|
+
// Prior to NodeCaret #7046 this would have been a side-effect
|
|
522
|
+
// so we do this for test compatibility.
|
|
523
|
+
// TODO: we may want to consider simplifying by removing this
|
|
524
|
+
if ($isRangeSelection(selection) && selection.anchor.type === 'text' && selection.focus.type === 'text' && selection.anchor.key === selection.focus.key) {
|
|
525
|
+
$ensureForwardRangeSelection(selection);
|
|
526
|
+
}
|
|
514
527
|
}
|
|
515
528
|
|
|
516
529
|
/**
|
|
517
|
-
*
|
|
518
|
-
*
|
|
519
|
-
*
|
|
520
|
-
*
|
|
530
|
+
* Ensure that the given RangeSelection is not backwards. If it
|
|
531
|
+
* is backwards, then the anchor and focus points will be swapped
|
|
532
|
+
* in-place. Ensuring that the selection is a writable RangeSelection
|
|
533
|
+
* is the responsibility of the caller (e.g. in a read-only context
|
|
534
|
+
* you will want to clone $getSelection() before using this).
|
|
521
535
|
*
|
|
536
|
+
* @param selection a writable RangeSelection
|
|
522
537
|
*/
|
|
538
|
+
function $ensureForwardRangeSelection(selection) {
|
|
539
|
+
if (selection.isBackward()) {
|
|
540
|
+
const {
|
|
541
|
+
anchor,
|
|
542
|
+
focus
|
|
543
|
+
} = selection;
|
|
544
|
+
// stash for the in-place swap
|
|
545
|
+
const {
|
|
546
|
+
key,
|
|
547
|
+
offset,
|
|
548
|
+
type
|
|
549
|
+
} = anchor;
|
|
550
|
+
anchor.set(focus.key, focus.offset, focus.type);
|
|
551
|
+
focus.set(key, offset, type);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
523
554
|
|
|
555
|
+
function $copyBlockFormatIndent(srcNode, destNode) {
|
|
556
|
+
const format = srcNode.getFormatType();
|
|
557
|
+
const indent = srcNode.getIndent();
|
|
558
|
+
if (format !== destNode.getFormatType()) {
|
|
559
|
+
destNode.setFormat(format);
|
|
560
|
+
}
|
|
561
|
+
if (indent !== destNode.getIndent()) {
|
|
562
|
+
destNode.setIndent(indent);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
524
565
|
|
|
525
566
|
/**
|
|
526
567
|
* Converts all nodes in the selection that are of one block type to another.
|
|
527
568
|
* @param selection - The selected blocks to be converted.
|
|
528
|
-
* @param createElement - The function that creates the node. eg. $createParagraphNode.
|
|
569
|
+
* @param $createElement - The function that creates the node. eg. $createParagraphNode.
|
|
570
|
+
* @param $afterCreateElement - The function that updates the new node based on the previous one ($copyBlockFormatIndent by default)
|
|
529
571
|
*/
|
|
530
|
-
function $setBlocksType(selection, createElement) {
|
|
572
|
+
function $setBlocksType(selection, $createElement, $afterCreateElement = $copyBlockFormatIndent) {
|
|
531
573
|
if (selection === null) {
|
|
532
574
|
return;
|
|
533
575
|
}
|
|
576
|
+
// Selections tend to not include their containing blocks so we effectively
|
|
577
|
+
// expand it here
|
|
534
578
|
const anchorAndFocus = selection.getStartEndPoints();
|
|
535
|
-
const
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
const
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
579
|
+
const blockMap = new Map();
|
|
580
|
+
let newSelection = null;
|
|
581
|
+
if (anchorAndFocus) {
|
|
582
|
+
const [anchor, focus] = anchorAndFocus;
|
|
583
|
+
newSelection = $createRangeSelection();
|
|
584
|
+
newSelection.anchor.set(anchor.key, anchor.offset, anchor.type);
|
|
585
|
+
newSelection.focus.set(focus.key, focus.offset, focus.type);
|
|
586
|
+
const anchorBlock = $getAncestor(anchor.getNode(), INTERNAL_$isBlock);
|
|
587
|
+
const focusBlock = $getAncestor(focus.getNode(), INTERNAL_$isBlock);
|
|
588
|
+
if ($isElementNode(anchorBlock)) {
|
|
589
|
+
blockMap.set(anchorBlock.getKey(), anchorBlock);
|
|
590
|
+
}
|
|
591
|
+
if ($isElementNode(focusBlock)) {
|
|
592
|
+
blockMap.set(focusBlock.getKey(), focusBlock);
|
|
544
593
|
}
|
|
545
|
-
return;
|
|
546
|
-
}
|
|
547
|
-
const nodes = selection.getNodes();
|
|
548
|
-
const firstSelectedBlock = anchor !== null ? $getAncestor(anchor.getNode(), INTERNAL_$isBlock) : false;
|
|
549
|
-
if (firstSelectedBlock && nodes.indexOf(firstSelectedBlock) === -1) {
|
|
550
|
-
nodes.push(firstSelectedBlock);
|
|
551
594
|
}
|
|
552
|
-
for (
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
595
|
+
for (const node of selection.getNodes()) {
|
|
596
|
+
if ($isElementNode(node) && INTERNAL_$isBlock(node)) {
|
|
597
|
+
blockMap.set(node.getKey(), node);
|
|
598
|
+
} else if (anchorAndFocus === null) {
|
|
599
|
+
const ancestorBlock = $getAncestor(node, INTERNAL_$isBlock);
|
|
600
|
+
if ($isElementNode(ancestorBlock)) {
|
|
601
|
+
blockMap.set(ancestorBlock.getKey(), ancestorBlock);
|
|
602
|
+
}
|
|
556
603
|
}
|
|
557
|
-
|
|
558
|
-
|
|
604
|
+
}
|
|
605
|
+
for (const [key, prevNode] of blockMap) {
|
|
606
|
+
const element = $createElement();
|
|
607
|
+
$afterCreateElement(prevNode, element);
|
|
608
|
+
prevNode.replace(element, true);
|
|
609
|
+
if (newSelection) {
|
|
610
|
+
if (key === newSelection.anchor.key) {
|
|
611
|
+
newSelection.anchor.set(element.getKey(), newSelection.anchor.offset, newSelection.anchor.type);
|
|
612
|
+
}
|
|
613
|
+
if (key === newSelection.focus.key) {
|
|
614
|
+
newSelection.focus.set(element.getKey(), newSelection.focus.offset, newSelection.focus.type);
|
|
615
|
+
}
|
|
559
616
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
node.replace(targetElement, true);
|
|
617
|
+
}
|
|
618
|
+
if (newSelection && selection.is($getSelection())) {
|
|
619
|
+
$setSelection(newSelection);
|
|
564
620
|
}
|
|
565
621
|
}
|
|
566
622
|
function isPointAttached(point) {
|
|
@@ -579,7 +635,7 @@ function $removeParentEmptyElements(startingNode) {
|
|
|
579
635
|
}
|
|
580
636
|
|
|
581
637
|
/**
|
|
582
|
-
* @deprecated
|
|
638
|
+
* @deprecated In favor of $setBlockTypes
|
|
583
639
|
* Wraps all nodes in the selection into another node of the type returned by createElement.
|
|
584
640
|
* @param selection - The selection of nodes to be wrapped.
|
|
585
641
|
* @param createElement - A function that creates the wrapping ElementNode. eg. $createParagraphNode.
|
|
@@ -704,7 +760,7 @@ function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingEl
|
|
|
704
760
|
}
|
|
705
761
|
} else if (emptyElements.has(node.getKey())) {
|
|
706
762
|
if (!$isElementNode(node)) {
|
|
707
|
-
|
|
763
|
+
formatDevErrorMessage(`Expected node in emptyElements to be an ElementNode`);
|
|
708
764
|
}
|
|
709
765
|
const targetElement = createElement();
|
|
710
766
|
targetElement.setFormat(node.getFormatType());
|
|
@@ -781,6 +837,29 @@ function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingEl
|
|
|
781
837
|
}
|
|
782
838
|
}
|
|
783
839
|
|
|
840
|
+
/**
|
|
841
|
+
* Tests if the selection's parent element has vertical writing mode.
|
|
842
|
+
* @param selection - The selection whose parent to test.
|
|
843
|
+
* @returns true if the selection's parent has vertical writing mode (writing-mode: vertical-rl), false otherwise.
|
|
844
|
+
*/
|
|
845
|
+
function $isEditorVerticalOrientation(selection) {
|
|
846
|
+
const computedStyle = $getComputedStyle(selection);
|
|
847
|
+
return computedStyle !== null && computedStyle.writingMode === 'vertical-rl';
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
/**
|
|
851
|
+
* Gets the computed DOM styles of the parent of the selection's anchor node.
|
|
852
|
+
* @param selection - The selection to check the styles for.
|
|
853
|
+
* @returns the computed styles of the node or null if there is no DOM element or no default view for the document.
|
|
854
|
+
*/
|
|
855
|
+
function $getComputedStyle(selection) {
|
|
856
|
+
const anchorNode = selection.anchor.getNode();
|
|
857
|
+
if ($isElementNode(anchorNode)) {
|
|
858
|
+
return $getComputedStyleForElement(anchorNode);
|
|
859
|
+
}
|
|
860
|
+
return $getComputedStyleForParent(anchorNode);
|
|
861
|
+
}
|
|
862
|
+
|
|
784
863
|
/**
|
|
785
864
|
* Determines if the default character selection should be overridden. Used with DecoratorNodes
|
|
786
865
|
* @param selection - The selection whose default character selection may need to be overridden.
|
|
@@ -788,8 +867,30 @@ function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingEl
|
|
|
788
867
|
* @returns true if it should be overridden, false if not.
|
|
789
868
|
*/
|
|
790
869
|
function $shouldOverrideDefaultCharacterSelection(selection, isBackward) {
|
|
791
|
-
const
|
|
792
|
-
|
|
870
|
+
const isVertical = $isEditorVerticalOrientation(selection);
|
|
871
|
+
|
|
872
|
+
// In vertical writing mode, we adjust the direction for correct caret movement
|
|
873
|
+
let adjustedIsBackward = isVertical ? !isBackward : isBackward;
|
|
874
|
+
|
|
875
|
+
// In right-to-left writing mode, we invert the direction for correct caret movement
|
|
876
|
+
if ($isParentElementRTL(selection)) {
|
|
877
|
+
adjustedIsBackward = !adjustedIsBackward;
|
|
878
|
+
}
|
|
879
|
+
const focusCaret = $caretFromPoint(selection.focus, adjustedIsBackward ? 'previous' : 'next');
|
|
880
|
+
if ($isExtendableTextPointCaret(focusCaret)) {
|
|
881
|
+
return false;
|
|
882
|
+
}
|
|
883
|
+
for (const nextCaret of $extendCaretToRange(focusCaret)) {
|
|
884
|
+
if ($isChildCaret(nextCaret)) {
|
|
885
|
+
return !nextCaret.origin.isInline();
|
|
886
|
+
} else if ($isElementNode(nextCaret.origin)) {
|
|
887
|
+
continue;
|
|
888
|
+
} else if ($isDecoratorNode(nextCaret.origin)) {
|
|
889
|
+
return true;
|
|
890
|
+
}
|
|
891
|
+
break;
|
|
892
|
+
}
|
|
893
|
+
return false;
|
|
793
894
|
}
|
|
794
895
|
|
|
795
896
|
/**
|
|
@@ -809,9 +910,8 @@ function $moveCaretSelection(selection, isHoldingShift, isBackward, granularity)
|
|
|
809
910
|
* @returns true if the selections' parent element has a direction of 'rtl' (right to left), false otherwise.
|
|
810
911
|
*/
|
|
811
912
|
function $isParentElementRTL(selection) {
|
|
812
|
-
const
|
|
813
|
-
|
|
814
|
-
return parent.getDirection() === 'rtl';
|
|
913
|
+
const computedStyle = $getComputedStyle(selection);
|
|
914
|
+
return computedStyle !== null && computedStyle.direction === 'rtl';
|
|
815
915
|
}
|
|
816
916
|
|
|
817
917
|
/**
|
|
@@ -822,39 +922,26 @@ function $isParentElementRTL(selection) {
|
|
|
822
922
|
*/
|
|
823
923
|
function $moveCharacter(selection, isHoldingShift, isBackward) {
|
|
824
924
|
const isRTL = $isParentElementRTL(selection);
|
|
825
|
-
$
|
|
826
|
-
}
|
|
925
|
+
const isVertical = $isEditorVerticalOrientation(selection);
|
|
827
926
|
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
let lastOffset = 0;
|
|
843
|
-
if ($isTextNode(firstNode)) {
|
|
844
|
-
firstType = 'text';
|
|
845
|
-
} else if (!$isElementNode(firstNode) && firstNode !== null) {
|
|
846
|
-
firstNode = firstNode.getParentOrThrow();
|
|
847
|
-
}
|
|
848
|
-
if ($isTextNode(lastNode)) {
|
|
849
|
-
lastType = 'text';
|
|
850
|
-
lastOffset = lastNode.getTextContentSize();
|
|
851
|
-
} else if (!$isElementNode(lastNode) && lastNode !== null) {
|
|
852
|
-
lastNode = lastNode.getParentOrThrow();
|
|
853
|
-
}
|
|
854
|
-
if (firstNode && lastNode) {
|
|
855
|
-
anchor.set(firstNode.getKey(), 0, firstType);
|
|
856
|
-
focus.set(lastNode.getKey(), lastOffset, lastType);
|
|
927
|
+
// In vertical-rl writing mode, arrow key directions need to be flipped
|
|
928
|
+
// to match the visual flow of text (top to bottom, right to left)
|
|
929
|
+
let adjustedIsBackward;
|
|
930
|
+
if (isVertical) {
|
|
931
|
+
// In vertical-rl mode, we need to completely invert the direction
|
|
932
|
+
// Left arrow (backward) should move down (forward)
|
|
933
|
+
// Right arrow (forward) should move up (backward)
|
|
934
|
+
adjustedIsBackward = !isBackward;
|
|
935
|
+
} else if (isRTL) {
|
|
936
|
+
// In horizontal RTL mode, use the standard RTL behavior
|
|
937
|
+
adjustedIsBackward = !isBackward;
|
|
938
|
+
} else {
|
|
939
|
+
// Standard LTR horizontal text
|
|
940
|
+
adjustedIsBackward = isBackward;
|
|
857
941
|
}
|
|
942
|
+
|
|
943
|
+
// Apply the direction adjustment to move the caret
|
|
944
|
+
$moveCaretSelection(selection, isHoldingShift, adjustedIsBackward, 'character');
|
|
858
945
|
}
|
|
859
946
|
|
|
860
947
|
/**
|
|
@@ -919,22 +1006,6 @@ function $getSelectionStyleValueForProperty(selection, styleProperty, defaultVal
|
|
|
919
1006
|
}
|
|
920
1007
|
return styleValue === null ? defaultValue : styleValue;
|
|
921
1008
|
}
|
|
922
|
-
|
|
923
|
-
/**
|
|
924
|
-
* This function is for internal use of the library.
|
|
925
|
-
* Please do not use it as it may change in the future.
|
|
926
|
-
*/
|
|
927
|
-
function INTERNAL_$isBlock(node) {
|
|
928
|
-
if ($isDecoratorNode(node)) {
|
|
929
|
-
return false;
|
|
930
|
-
}
|
|
931
|
-
if (!$isElementNode(node) || $isRootOrShadowRoot(node)) {
|
|
932
|
-
return false;
|
|
933
|
-
}
|
|
934
|
-
const firstChild = node.getFirstChild();
|
|
935
|
-
const isLeafElement = firstChild === null || $isLineBreakNode(firstChild) || $isTextNode(firstChild) || firstChild.isInline();
|
|
936
|
-
return !node.isInline() && node.canBeEmpty() !== false && isLeafElement;
|
|
937
|
-
}
|
|
938
1009
|
function $getAncestor(node, predicate) {
|
|
939
1010
|
let parent = node;
|
|
940
1011
|
while (parent !== null && parent.getParent() !== null && !predicate(parent)) {
|
|
@@ -954,4 +1025,4 @@ function $getAncestor(node, predicate) {
|
|
|
954
1025
|
/** @deprecated renamed to {@link $trimTextContentFromAnchor} by @lexical/eslint-plugin rules-of-lexical */
|
|
955
1026
|
const trimTextContentFromAnchor = $trimTextContentFromAnchor;
|
|
956
1027
|
|
|
957
|
-
export { $addNodeStyle, $getSelectionStyleValueForProperty, $isAtNodeEnd, $isParentElementRTL, $moveCaretSelection, $moveCharacter, $patchStyleText, $
|
|
1028
|
+
export { $addNodeStyle, $copyBlockFormatIndent, $ensureForwardRangeSelection, $forEachSelectedTextNode, $getComputedStyleForElement, $getComputedStyleForParent, $getSelectionStyleValueForProperty, $isAtNodeEnd, $isParentElementRTL, $isParentRTL, $moveCaretSelection, $moveCharacter, $patchStyleText, $setBlocksType, $shouldOverrideDefaultCharacterSelection, $sliceSelectedTextNodeContent, $trimTextContentFromAnchor, $wrapNodes, createDOMRange, createRectsFromDOMRange, getCSSFromStyleObject, getStyleObjectFromCSS, trimTextContentFromAnchor };
|
|
@@ -11,16 +11,18 @@ import * as LexicalSelection_prod from './LexicalSelection.prod.js';
|
|
|
11
11
|
*
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
const mod = process.env.NODE_ENV
|
|
14
|
+
const mod = process.env.NODE_ENV !== 'production' ? LexicalSelection_dev : LexicalSelection_prod;
|
|
15
15
|
const $addNodeStyle = mod.$addNodeStyle;
|
|
16
16
|
const $getSelectionStyleValueForProperty = mod.$getSelectionStyleValueForProperty;
|
|
17
17
|
const $isAtNodeEnd = mod.$isAtNodeEnd;
|
|
18
18
|
const $isParentElementRTL = mod.$isParentElementRTL;
|
|
19
|
+
const $isParentRTL = mod.$isParentRTL;
|
|
19
20
|
const $moveCharacter = mod.$moveCharacter;
|
|
20
21
|
const $patchStyleText = mod.$patchStyleText;
|
|
21
22
|
const $setBlocksType = mod.$setBlocksType;
|
|
22
23
|
const $shouldOverrideDefaultCharacterSelection = mod.$shouldOverrideDefaultCharacterSelection;
|
|
23
24
|
const $sliceSelectedTextNodeContent = mod.$sliceSelectedTextNodeContent;
|
|
24
25
|
const createRectsFromDOMRange = mod.createRectsFromDOMRange;
|
|
26
|
+
const getStyleObjectFromCSS = mod.getStyleObjectFromCSS;
|
|
25
27
|
|
|
26
|
-
export { $addNodeStyle, $getSelectionStyleValueForProperty, $isAtNodeEnd, $isParentElementRTL, $moveCharacter, $patchStyleText, $setBlocksType, $shouldOverrideDefaultCharacterSelection, $sliceSelectedTextNodeContent, createRectsFromDOMRange };
|
|
28
|
+
export { $addNodeStyle, $getSelectionStyleValueForProperty, $isAtNodeEnd, $isParentElementRTL, $isParentRTL, $moveCharacter, $patchStyleText, $setBlocksType, $shouldOverrideDefaultCharacterSelection, $sliceSelectedTextNodeContent, createRectsFromDOMRange, getStyleObjectFromCSS };
|