@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.
Files changed (76) hide show
  1. package/{src/editor/Lexical.module.css → css/_lexical.css} +8 -4
  2. package/css/styles.css +24 -0
  3. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +62 -20
  4. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
  5. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +11 -11
  6. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +643 -245
  7. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +1 -1
  8. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +29 -22
  9. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +13 -6
  10. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +1 -1
  11. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
  12. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +242 -141
  13. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +1 -1
  14. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +11 -10
  15. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +594 -255
  16. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +1 -1
  17. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +23 -16
  18. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +102 -53
  19. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
  20. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +10 -10
  21. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +275 -194
  22. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +5 -1
  23. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +28 -18
  24. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +2293 -878
  25. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +1 -1
  26. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +53 -40
  27. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +471 -193
  28. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +13 -1
  29. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +47 -32
  30. package/dist/cjs/node_modules/prismjs/prism.js +1 -1
  31. package/dist/cjs/packages/lexical/src/components/StickyComponent.js +1 -1
  32. package/dist/cjs/packages/lexical/src/editor/LexicalEditor.js +30 -5
  33. package/dist/cjs/packages/lexical/src/hooks/useFormatWatcher.js +86 -83
  34. package/dist/cjs/packages/lexical/src/plugins/ToolbarPlugin.js +25 -17
  35. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +62 -20
  36. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
  37. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +3 -3
  38. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +637 -246
  39. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +1 -1
  40. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +4 -4
  41. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +14 -7
  42. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +1 -1
  43. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
  44. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +244 -144
  45. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +1 -1
  46. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +4 -4
  47. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +590 -257
  48. package/dist/esm/node_modules/@lexical/list/LexicalList.js +1 -1
  49. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +5 -4
  50. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +104 -55
  51. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
  52. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +4 -4
  53. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +267 -196
  54. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +4 -2
  55. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +4 -4
  56. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +2283 -881
  57. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +1 -1
  58. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +4 -4
  59. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +457 -194
  60. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +8 -2
  61. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +3 -3
  62. package/dist/esm/node_modules/prismjs/prism.js +1 -1
  63. package/dist/esm/packages/lexical/src/components/StickyComponent.js +1 -1
  64. package/dist/esm/packages/lexical/src/editor/LexicalEditor.js +30 -5
  65. package/dist/esm/packages/lexical/src/hooks/useFormatWatcher.js +88 -85
  66. package/dist/esm/packages/lexical/src/plugins/ToolbarPlugin.js +25 -17
  67. package/dist/types/editor/LexicalEditor.d.ts +25 -0
  68. package/dist/types/hooks/useFormatWatcher.d.ts +2 -2
  69. package/dist/types/nodes/StickyNode.d.ts +0 -1
  70. package/package.json +14 -13
  71. package/src/components/StickyComponent.tsx +2 -2
  72. package/src/editor/LexicalEditor.tsx +57 -26
  73. package/src/hooks/useFormatWatcher.ts +35 -31
  74. package/src/plugins/ToolbarPlugin.tsx +27 -17
  75. package/dist/cjs/editor/Lexical.module.css +0 -1
  76. package/dist/esm/editor/Lexical.module.css +0 -1
@@ -11,6 +11,20 @@ var lexical = require('lexical');
11
11
  */
12
12
 
13
13
 
14
+ /**
15
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
16
+ *
17
+ * This source code is licensed under the MIT license found in the
18
+ * LICENSE file in the root directory of this source tree.
19
+ *
20
+ */
21
+
22
+ // Do not require this module directly! Use normal `invariant` calls.
23
+
24
+ function formatDevErrorMessage(message) {
25
+ throw new Error(message);
26
+ }
27
+
14
28
  /**
15
29
  * Copyright (c) Meta Platforms, Inc. and affiliates.
16
30
  *
@@ -146,6 +160,9 @@ function createRectsFromDOMRange(editor, range) {
146
160
  */
147
161
  function getStyleObjectFromRawCSS(css) {
148
162
  const styleObject = {};
163
+ if (!css) {
164
+ return styleObject;
165
+ }
149
166
  const styles = css.split(';');
150
167
  for (const style of styles) {
151
168
  if (style !== '') {
@@ -192,12 +209,42 @@ function getCSSFromStyleObject(styles) {
192
209
  }
193
210
 
194
211
  /**
195
- * Copyright (c) Meta Platforms, Inc. and affiliates.
196
- *
197
- * This source code is licensed under the MIT license found in the
198
- * LICENSE file in the root directory of this source tree.
199
- *
212
+ * Gets the computed DOM styles of the element.
213
+ * @param node - The node to check the styles for.
214
+ * @returns the computed styles of the element or null if there is no DOM element or no default view for the document.
200
215
  */
216
+ function $getComputedStyleForElement(element) {
217
+ const editor = lexical.$getEditor();
218
+ const domElement = editor.getElementByKey(element.getKey());
219
+ if (domElement === null) {
220
+ return null;
221
+ }
222
+ const view = domElement.ownerDocument.defaultView;
223
+ if (view === null) {
224
+ return null;
225
+ }
226
+ return view.getComputedStyle(domElement);
227
+ }
228
+
229
+ /**
230
+ * Gets the computed DOM styles of the parent of the node.
231
+ * @param node - The node to check its parent's styles for.
232
+ * @returns the computed styles of the node or null if there is no DOM element or no default view for the document.
233
+ */
234
+ function $getComputedStyleForParent(node) {
235
+ const parent = lexical.$isRootNode(node) ? node : node.getParentOrThrow();
236
+ return $getComputedStyleForElement(parent);
237
+ }
238
+
239
+ /**
240
+ * Determines whether a node's parent is RTL.
241
+ * @param node - The node to check whether it is RTL.
242
+ * @returns whether the node is RTL.
243
+ */
244
+ function $isParentRTL(node) {
245
+ const styles = $getComputedStyleForParent(node);
246
+ return styles !== null && styles.direction === 'rtl';
247
+ }
201
248
 
202
249
  /**
203
250
  * Generally used to append text content to HTML and JSON. Grabs the text content and "slices"
@@ -208,7 +255,7 @@ function getCSSFromStyleObject(styles) {
208
255
  */
209
256
  function $sliceSelectedTextNodeContent(selection, textNode) {
210
257
  const anchorAndFocus = selection.getStartEndPoints();
211
- if (textNode.isSelected(selection) && !textNode.isSegmented() && !textNode.isToken() && anchorAndFocus !== null) {
258
+ if (textNode.isSelected(selection) && !lexical.$isTokenOrSegmented(textNode) && anchorAndFocus !== null) {
212
259
  const [anchor, focus] = anchorAndFocus;
213
260
  const isBackward = selection.isBackward();
214
261
  const anchorNode = anchor.getNode();
@@ -234,8 +281,11 @@ function $sliceSelectedTextNodeContent(selection, textNode) {
234
281
  startOffset = 0;
235
282
  endOffset = offset;
236
283
  }
284
+
285
+ // NOTE: This mutates __text directly because the primary use case is to
286
+ // modify a $cloneWithProperties node that should never be added
287
+ // to the EditorState so we must not call getWritable via setTextContent
237
288
  textNode.__text = textNode.__text.slice(startOffset, endOffset);
238
- return textNode;
239
289
  }
240
290
  }
241
291
  return textNode;
@@ -252,7 +302,7 @@ function $isAtNodeEnd(point) {
252
302
  }
253
303
  const node = point.getNode();
254
304
  if (!lexical.$isElementNode(node)) {
255
- throw Error(`isAtNodeEnd: node must be a TextNode or ElementNode`);
305
+ formatDevErrorMessage(`isAtNodeEnd: node must be a TextNode or ElementNode`);
256
306
  }
257
307
  return point.offset === node.getChildrenSize();
258
308
  }
@@ -378,8 +428,19 @@ function $addNodeStyle(node) {
378
428
  const styles = getStyleObjectFromRawCSS(CSSText);
379
429
  CSS_TO_STYLES.set(CSSText, styles);
380
430
  }
431
+
432
+ /**
433
+ * Applies the provided styles to the given TextNode, ElementNode, or
434
+ * collapsed RangeSelection.
435
+ *
436
+ * @param target - The TextNode, ElementNode, or collapsed RangeSelection to apply the styles to
437
+ * @param patch - The patch to apply, which can include multiple styles. \\{CSSProperty: value\\} . Can also accept a function that returns the new property value.
438
+ */
381
439
  function $patchStyle(target, patch) {
382
- const prevStyles = getStyleObjectFromCSS('getStyle' in target ? target.getStyle() : target.style);
440
+ if (!(lexical.$isRangeSelection(target) ? target.isCollapsed() : lexical.$isTextNode(target) || lexical.$isElementNode(target))) {
441
+ formatDevErrorMessage(`$patchStyle must only be called with a TextNode, ElementNode, or collapsed RangeSelection`);
442
+ }
443
+ const prevStyles = getStyleObjectFromCSS(lexical.$isRangeSelection(target) ? target.style : lexical.$isTextNode(target) ? target.getStyle() : target.getTextStyle());
383
444
  const newStyles = Object.entries(patch).reduce((styles, [key, value]) => {
384
445
  if (typeof value === 'function') {
385
446
  styles[key] = value(prevStyles[key], target);
@@ -391,9 +452,13 @@ function $patchStyle(target, patch) {
391
452
  return styles;
392
453
  }, {
393
454
  ...prevStyles
394
- } || {});
455
+ });
395
456
  const newCSSText = getCSSFromStyleObject(newStyles);
396
- target.setStyle(newCSSText);
457
+ if (lexical.$isRangeSelection(target) || lexical.$isTextNode(target)) {
458
+ target.setStyle(newCSSText);
459
+ } else {
460
+ target.setTextStyle(newCSSText);
461
+ }
397
462
  CSS_TO_STYLES.set(newCSSText, newStyles);
398
463
  }
399
464
 
@@ -405,161 +470,152 @@ function $patchStyle(target, patch) {
405
470
  * @param patch - The patch to apply, which can include multiple styles. \\{CSSProperty: value\\} . Can also accept a function that returns the new property value.
406
471
  */
407
472
  function $patchStyleText(selection, patch) {
408
- const selectedNodes = selection.getNodes();
409
- const selectedNodesLength = selectedNodes.length;
410
- const anchorAndFocus = selection.getStartEndPoints();
411
- if (anchorAndFocus === null) {
412
- return;
413
- }
414
- const [anchor, focus] = anchorAndFocus;
415
- const lastIndex = selectedNodesLength - 1;
416
- let firstNode = selectedNodes[0];
417
- let lastNode = selectedNodes[lastIndex];
418
- if (selection.isCollapsed() && lexical.$isRangeSelection(selection)) {
473
+ if (lexical.$isRangeSelection(selection) && selection.isCollapsed()) {
419
474
  $patchStyle(selection, patch);
475
+ const emptyNode = selection.anchor.getNode();
476
+ if (lexical.$isElementNode(emptyNode) && emptyNode.isEmpty()) {
477
+ $patchStyle(emptyNode, patch);
478
+ }
479
+ }
480
+ $forEachSelectedTextNode(textNode => {
481
+ $patchStyle(textNode, patch);
482
+ });
483
+ }
484
+ function $forEachSelectedTextNode(fn) {
485
+ const selection = lexical.$getSelection();
486
+ if (!selection) {
420
487
  return;
421
488
  }
422
- const firstNodeText = firstNode.getTextContent();
423
- const firstNodeTextLength = firstNodeText.length;
424
- const focusOffset = focus.offset;
425
- let anchorOffset = anchor.offset;
426
- const isBefore = anchor.isBefore(focus);
427
- let startOffset = isBefore ? anchorOffset : focusOffset;
428
- let endOffset = isBefore ? focusOffset : anchorOffset;
429
- const startType = isBefore ? anchor.type : focus.type;
430
- const endType = isBefore ? focus.type : anchor.type;
431
- const endKey = isBefore ? focus.key : anchor.key;
432
-
433
- // This is the case where the user only selected the very end of the
434
- // first node so we don't want to include it in the formatting change.
435
- if (lexical.$isTextNode(firstNode) && startOffset === firstNodeTextLength) {
436
- const nextSibling = firstNode.getNextSibling();
437
- if (lexical.$isTextNode(nextSibling)) {
438
- // we basically make the second node the firstNode, changing offsets accordingly
439
- anchorOffset = 0;
440
- startOffset = 0;
441
- firstNode = nextSibling;
442
- }
443
- }
444
-
445
- // This is the case where we only selected a single node
446
- if (selectedNodes.length === 1) {
447
- if (lexical.$isTextNode(firstNode) && firstNode.canHaveFormat()) {
448
- startOffset = startType === 'element' ? 0 : anchorOffset > focusOffset ? focusOffset : anchorOffset;
449
- endOffset = endType === 'element' ? firstNodeTextLength : anchorOffset > focusOffset ? anchorOffset : focusOffset;
450
-
451
- // No actual text is selected, so do nothing.
452
- if (startOffset === endOffset) {
453
- return;
454
- }
455
-
456
- // The entire node is selected or a token/segment, so just format it
457
- if (lexical.$isTokenOrSegmented(firstNode) || startOffset === 0 && endOffset === firstNodeTextLength) {
458
- $patchStyle(firstNode, patch);
459
- firstNode.select(startOffset, endOffset);
460
- } else {
461
- // The node is partially selected, so split it into two nodes
462
- // and style the selected one.
463
- const splitNodes = firstNode.splitText(startOffset, endOffset);
464
- const replacement = startOffset === 0 ? splitNodes[0] : splitNodes[1];
465
- $patchStyle(replacement, patch);
466
- replacement.select(0, endOffset - startOffset);
467
- }
468
- } // multiple nodes selected.
469
- } else {
470
- if (lexical.$isTextNode(firstNode) && startOffset < firstNode.getTextContentSize() && firstNode.canHaveFormat()) {
471
- if (startOffset !== 0 && !lexical.$isTokenOrSegmented(firstNode)) {
472
- // the entire first node isn't selected and it isn't a token or segmented, so split it
473
- firstNode = firstNode.splitText(startOffset)[1];
474
- startOffset = 0;
475
- if (isBefore) {
476
- anchor.set(firstNode.getKey(), startOffset, 'text');
477
- } else {
478
- focus.set(firstNode.getKey(), startOffset, 'text');
479
- }
480
- }
481
- $patchStyle(firstNode, patch);
482
- }
483
- if (lexical.$isTextNode(lastNode) && lastNode.canHaveFormat()) {
484
- const lastNodeText = lastNode.getTextContent();
485
- const lastNodeTextLength = lastNodeText.length;
486
-
487
- // The last node might not actually be the end node
488
- //
489
- // If not, assume the last node is fully-selected unless the end offset is
490
- // zero.
491
- if (lastNode.__key !== endKey && endOffset !== 0) {
492
- endOffset = lastNodeTextLength;
493
- }
494
-
495
- // if the entire last node isn't selected and it isn't a token or segmented, split it
496
- if (endOffset !== lastNodeTextLength && !lexical.$isTokenOrSegmented(lastNode)) {
497
- [lastNode] = lastNode.splitText(endOffset);
498
- }
499
- if (endOffset !== 0 || endType === 'element') {
500
- $patchStyle(lastNode, patch);
489
+ const slicedTextNodes = new Map();
490
+ const getSliceIndices = node => slicedTextNodes.get(node.getKey()) || [0, node.getTextContentSize()];
491
+ if (lexical.$isRangeSelection(selection)) {
492
+ for (const slice of lexical.$caretRangeFromSelection(selection).getTextSlices()) {
493
+ if (slice) {
494
+ slicedTextNodes.set(slice.caret.origin.getKey(), slice.getSliceIndices());
501
495
  }
502
496
  }
497
+ }
498
+ const selectedNodes = selection.getNodes();
499
+ for (const selectedNode of selectedNodes) {
500
+ if (!(lexical.$isTextNode(selectedNode) && selectedNode.canHaveFormat())) {
501
+ continue;
502
+ }
503
+ const [startOffset, endOffset] = getSliceIndices(selectedNode);
504
+ // No actual text is selected, so do nothing.
505
+ if (endOffset === startOffset) {
506
+ continue;
507
+ }
503
508
 
504
- // style all the text nodes in between
505
- for (let i = 1; i < lastIndex; i++) {
506
- const selectedNode = selectedNodes[i];
507
- const selectedNodeKey = selectedNode.getKey();
508
- if (lexical.$isTextNode(selectedNode) && selectedNode.canHaveFormat() && selectedNodeKey !== firstNode.getKey() && selectedNodeKey !== lastNode.getKey() && !selectedNode.isToken()) {
509
- $patchStyle(selectedNode, patch);
510
- }
509
+ // The entire node is selected or a token/segment, so just format it
510
+ if (lexical.$isTokenOrSegmented(selectedNode) || startOffset === 0 && endOffset === selectedNode.getTextContentSize()) {
511
+ fn(selectedNode);
512
+ } else {
513
+ // The node is partially selected, so split it into two or three nodes
514
+ // and style the selected one.
515
+ const splitNodes = selectedNode.splitText(startOffset, endOffset);
516
+ const replacement = splitNodes[startOffset === 0 ? 0 : 1];
517
+ fn(replacement);
511
518
  }
512
519
  }
520
+ // Prior to NodeCaret #7046 this would have been a side-effect
521
+ // so we do this for test compatibility.
522
+ // TODO: we may want to consider simplifying by removing this
523
+ if (lexical.$isRangeSelection(selection) && selection.anchor.type === 'text' && selection.focus.type === 'text' && selection.anchor.key === selection.focus.key) {
524
+ $ensureForwardRangeSelection(selection);
525
+ }
513
526
  }
514
527
 
515
528
  /**
516
- * Copyright (c) Meta Platforms, Inc. and affiliates.
517
- *
518
- * This source code is licensed under the MIT license found in the
519
- * LICENSE file in the root directory of this source tree.
529
+ * Ensure that the given RangeSelection is not backwards. If it
530
+ * is backwards, then the anchor and focus points will be swapped
531
+ * in-place. Ensuring that the selection is a writable RangeSelection
532
+ * is the responsibility of the caller (e.g. in a read-only context
533
+ * you will want to clone $getSelection() before using this).
520
534
  *
535
+ * @param selection a writable RangeSelection
521
536
  */
537
+ function $ensureForwardRangeSelection(selection) {
538
+ if (selection.isBackward()) {
539
+ const {
540
+ anchor,
541
+ focus
542
+ } = selection;
543
+ // stash for the in-place swap
544
+ const {
545
+ key,
546
+ offset,
547
+ type
548
+ } = anchor;
549
+ anchor.set(focus.key, focus.offset, focus.type);
550
+ focus.set(key, offset, type);
551
+ }
552
+ }
522
553
 
554
+ function $copyBlockFormatIndent(srcNode, destNode) {
555
+ const format = srcNode.getFormatType();
556
+ const indent = srcNode.getIndent();
557
+ if (format !== destNode.getFormatType()) {
558
+ destNode.setFormat(format);
559
+ }
560
+ if (indent !== destNode.getIndent()) {
561
+ destNode.setIndent(indent);
562
+ }
563
+ }
523
564
 
524
565
  /**
525
566
  * Converts all nodes in the selection that are of one block type to another.
526
567
  * @param selection - The selected blocks to be converted.
527
- * @param createElement - The function that creates the node. eg. $createParagraphNode.
568
+ * @param $createElement - The function that creates the node. eg. $createParagraphNode.
569
+ * @param $afterCreateElement - The function that updates the new node based on the previous one ($copyBlockFormatIndent by default)
528
570
  */
529
- function $setBlocksType(selection, createElement) {
571
+ function $setBlocksType(selection, $createElement, $afterCreateElement = $copyBlockFormatIndent) {
530
572
  if (selection === null) {
531
573
  return;
532
574
  }
575
+ // Selections tend to not include their containing blocks so we effectively
576
+ // expand it here
533
577
  const anchorAndFocus = selection.getStartEndPoints();
534
- const anchor = anchorAndFocus ? anchorAndFocus[0] : null;
535
- if (anchor !== null && anchor.key === 'root') {
536
- const element = createElement();
537
- const root = lexical.$getRoot();
538
- const firstChild = root.getFirstChild();
539
- if (firstChild) {
540
- firstChild.replace(element, true);
541
- } else {
542
- root.append(element);
578
+ const blockMap = new Map();
579
+ let newSelection = null;
580
+ if (anchorAndFocus) {
581
+ const [anchor, focus] = anchorAndFocus;
582
+ newSelection = lexical.$createRangeSelection();
583
+ newSelection.anchor.set(anchor.key, anchor.offset, anchor.type);
584
+ newSelection.focus.set(focus.key, focus.offset, focus.type);
585
+ const anchorBlock = $getAncestor(anchor.getNode(), lexical.INTERNAL_$isBlock);
586
+ const focusBlock = $getAncestor(focus.getNode(), lexical.INTERNAL_$isBlock);
587
+ if (lexical.$isElementNode(anchorBlock)) {
588
+ blockMap.set(anchorBlock.getKey(), anchorBlock);
589
+ }
590
+ if (lexical.$isElementNode(focusBlock)) {
591
+ blockMap.set(focusBlock.getKey(), focusBlock);
543
592
  }
544
- return;
545
- }
546
- const nodes = selection.getNodes();
547
- const firstSelectedBlock = anchor !== null ? $getAncestor(anchor.getNode(), INTERNAL_$isBlock) : false;
548
- if (firstSelectedBlock && nodes.indexOf(firstSelectedBlock) === -1) {
549
- nodes.push(firstSelectedBlock);
550
593
  }
551
- for (let i = 0; i < nodes.length; i++) {
552
- const node = nodes[i];
553
- if (!INTERNAL_$isBlock(node)) {
554
- continue;
594
+ for (const node of selection.getNodes()) {
595
+ if (lexical.$isElementNode(node) && lexical.INTERNAL_$isBlock(node)) {
596
+ blockMap.set(node.getKey(), node);
597
+ } else if (anchorAndFocus === null) {
598
+ const ancestorBlock = $getAncestor(node, lexical.INTERNAL_$isBlock);
599
+ if (lexical.$isElementNode(ancestorBlock)) {
600
+ blockMap.set(ancestorBlock.getKey(), ancestorBlock);
601
+ }
555
602
  }
556
- if (!lexical.$isElementNode(node)) {
557
- throw Error(`Expected block node to be an ElementNode`);
603
+ }
604
+ for (const [key, prevNode] of blockMap) {
605
+ const element = $createElement();
606
+ $afterCreateElement(prevNode, element);
607
+ prevNode.replace(element, true);
608
+ if (newSelection) {
609
+ if (key === newSelection.anchor.key) {
610
+ newSelection.anchor.set(element.getKey(), newSelection.anchor.offset, newSelection.anchor.type);
611
+ }
612
+ if (key === newSelection.focus.key) {
613
+ newSelection.focus.set(element.getKey(), newSelection.focus.offset, newSelection.focus.type);
614
+ }
558
615
  }
559
- const targetElement = createElement();
560
- targetElement.setFormat(node.getFormatType());
561
- targetElement.setIndent(node.getIndent());
562
- node.replace(targetElement, true);
616
+ }
617
+ if (newSelection && selection.is(lexical.$getSelection())) {
618
+ lexical.$setSelection(newSelection);
563
619
  }
564
620
  }
565
621
  function isPointAttached(point) {
@@ -578,7 +634,7 @@ function $removeParentEmptyElements(startingNode) {
578
634
  }
579
635
 
580
636
  /**
581
- * @deprecated
637
+ * @deprecated In favor of $setBlockTypes
582
638
  * Wraps all nodes in the selection into another node of the type returned by createElement.
583
639
  * @param selection - The selection of nodes to be wrapped.
584
640
  * @param createElement - A function that creates the wrapping ElementNode. eg. $createParagraphNode.
@@ -703,7 +759,7 @@ function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingEl
703
759
  }
704
760
  } else if (emptyElements.has(node.getKey())) {
705
761
  if (!lexical.$isElementNode(node)) {
706
- throw Error(`Expected node in emptyElements to be an ElementNode`);
762
+ formatDevErrorMessage(`Expected node in emptyElements to be an ElementNode`);
707
763
  }
708
764
  const targetElement = createElement();
709
765
  targetElement.setFormat(node.getFormatType());
@@ -780,6 +836,29 @@ function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingEl
780
836
  }
781
837
  }
782
838
 
839
+ /**
840
+ * Tests if the selection's parent element has vertical writing mode.
841
+ * @param selection - The selection whose parent to test.
842
+ * @returns true if the selection's parent has vertical writing mode (writing-mode: vertical-rl), false otherwise.
843
+ */
844
+ function $isEditorVerticalOrientation(selection) {
845
+ const computedStyle = $getComputedStyle(selection);
846
+ return computedStyle !== null && computedStyle.writingMode === 'vertical-rl';
847
+ }
848
+
849
+ /**
850
+ * Gets the computed DOM styles of the parent of the selection's anchor node.
851
+ * @param selection - The selection to check the styles for.
852
+ * @returns the computed styles of the node or null if there is no DOM element or no default view for the document.
853
+ */
854
+ function $getComputedStyle(selection) {
855
+ const anchorNode = selection.anchor.getNode();
856
+ if (lexical.$isElementNode(anchorNode)) {
857
+ return $getComputedStyleForElement(anchorNode);
858
+ }
859
+ return $getComputedStyleForParent(anchorNode);
860
+ }
861
+
783
862
  /**
784
863
  * Determines if the default character selection should be overridden. Used with DecoratorNodes
785
864
  * @param selection - The selection whose default character selection may need to be overridden.
@@ -787,8 +866,30 @@ function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingEl
787
866
  * @returns true if it should be overridden, false if not.
788
867
  */
789
868
  function $shouldOverrideDefaultCharacterSelection(selection, isBackward) {
790
- const possibleNode = lexical.$getAdjacentNode(selection.focus, isBackward);
791
- return lexical.$isDecoratorNode(possibleNode) && !possibleNode.isIsolated() || lexical.$isElementNode(possibleNode) && !possibleNode.isInline() && !possibleNode.canBeEmpty();
869
+ const isVertical = $isEditorVerticalOrientation(selection);
870
+
871
+ // In vertical writing mode, we adjust the direction for correct caret movement
872
+ let adjustedIsBackward = isVertical ? !isBackward : isBackward;
873
+
874
+ // In right-to-left writing mode, we invert the direction for correct caret movement
875
+ if ($isParentElementRTL(selection)) {
876
+ adjustedIsBackward = !adjustedIsBackward;
877
+ }
878
+ const focusCaret = lexical.$caretFromPoint(selection.focus, adjustedIsBackward ? 'previous' : 'next');
879
+ if (lexical.$isExtendableTextPointCaret(focusCaret)) {
880
+ return false;
881
+ }
882
+ for (const nextCaret of lexical.$extendCaretToRange(focusCaret)) {
883
+ if (lexical.$isChildCaret(nextCaret)) {
884
+ return !nextCaret.origin.isInline();
885
+ } else if (lexical.$isElementNode(nextCaret.origin)) {
886
+ continue;
887
+ } else if (lexical.$isDecoratorNode(nextCaret.origin)) {
888
+ return true;
889
+ }
890
+ break;
891
+ }
892
+ return false;
792
893
  }
793
894
 
794
895
  /**
@@ -808,9 +909,8 @@ function $moveCaretSelection(selection, isHoldingShift, isBackward, granularity)
808
909
  * @returns true if the selections' parent element has a direction of 'rtl' (right to left), false otherwise.
809
910
  */
810
911
  function $isParentElementRTL(selection) {
811
- const anchorNode = selection.anchor.getNode();
812
- const parent = lexical.$isRootNode(anchorNode) ? anchorNode : anchorNode.getParentOrThrow();
813
- return parent.getDirection() === 'rtl';
912
+ const computedStyle = $getComputedStyle(selection);
913
+ return computedStyle !== null && computedStyle.direction === 'rtl';
814
914
  }
815
915
 
816
916
  /**
@@ -821,39 +921,26 @@ function $isParentElementRTL(selection) {
821
921
  */
822
922
  function $moveCharacter(selection, isHoldingShift, isBackward) {
823
923
  const isRTL = $isParentElementRTL(selection);
824
- $moveCaretSelection(selection, isHoldingShift, isBackward ? !isRTL : isRTL, 'character');
825
- }
924
+ const isVertical = $isEditorVerticalOrientation(selection);
826
925
 
827
- /**
828
- * Expands the current Selection to cover all of the content in the editor.
829
- * @param selection - The current selection.
830
- */
831
- function $selectAll(selection) {
832
- const anchor = selection.anchor;
833
- const focus = selection.focus;
834
- const anchorNode = anchor.getNode();
835
- const topParent = anchorNode.getTopLevelElementOrThrow();
836
- const root = topParent.getParentOrThrow();
837
- let firstNode = root.getFirstDescendant();
838
- let lastNode = root.getLastDescendant();
839
- let firstType = 'element';
840
- let lastType = 'element';
841
- let lastOffset = 0;
842
- if (lexical.$isTextNode(firstNode)) {
843
- firstType = 'text';
844
- } else if (!lexical.$isElementNode(firstNode) && firstNode !== null) {
845
- firstNode = firstNode.getParentOrThrow();
846
- }
847
- if (lexical.$isTextNode(lastNode)) {
848
- lastType = 'text';
849
- lastOffset = lastNode.getTextContentSize();
850
- } else if (!lexical.$isElementNode(lastNode) && lastNode !== null) {
851
- lastNode = lastNode.getParentOrThrow();
852
- }
853
- if (firstNode && lastNode) {
854
- anchor.set(firstNode.getKey(), 0, firstType);
855
- focus.set(lastNode.getKey(), lastOffset, lastType);
926
+ // In vertical-rl writing mode, arrow key directions need to be flipped
927
+ // to match the visual flow of text (top to bottom, right to left)
928
+ let adjustedIsBackward;
929
+ if (isVertical) {
930
+ // In vertical-rl mode, we need to completely invert the direction
931
+ // Left arrow (backward) should move down (forward)
932
+ // Right arrow (forward) should move up (backward)
933
+ adjustedIsBackward = !isBackward;
934
+ } else if (isRTL) {
935
+ // In horizontal RTL mode, use the standard RTL behavior
936
+ adjustedIsBackward = !isBackward;
937
+ } else {
938
+ // Standard LTR horizontal text
939
+ adjustedIsBackward = isBackward;
856
940
  }
941
+
942
+ // Apply the direction adjustment to move the caret
943
+ $moveCaretSelection(selection, isHoldingShift, adjustedIsBackward, 'character');
857
944
  }
858
945
 
859
946
  /**
@@ -918,22 +1005,6 @@ function $getSelectionStyleValueForProperty(selection, styleProperty, defaultVal
918
1005
  }
919
1006
  return styleValue === null ? defaultValue : styleValue;
920
1007
  }
921
-
922
- /**
923
- * This function is for internal use of the library.
924
- * Please do not use it as it may change in the future.
925
- */
926
- function INTERNAL_$isBlock(node) {
927
- if (lexical.$isDecoratorNode(node)) {
928
- return false;
929
- }
930
- if (!lexical.$isElementNode(node) || lexical.$isRootOrShadowRoot(node)) {
931
- return false;
932
- }
933
- const firstChild = node.getFirstChild();
934
- const isLeafElement = firstChild === null || lexical.$isLineBreakNode(firstChild) || lexical.$isTextNode(firstChild) || firstChild.isInline();
935
- return !node.isInline() && node.canBeEmpty() !== false && isLeafElement;
936
- }
937
1008
  function $getAncestor(node, predicate) {
938
1009
  let parent = node;
939
1010
  while (parent !== null && parent.getParent() !== null && !predicate(parent)) {
@@ -957,14 +1028,23 @@ Object.defineProperty(exports, "$cloneWithProperties", {
957
1028
  enumerable: true,
958
1029
  get: function () { return lexical.$cloneWithProperties; }
959
1030
  });
1031
+ Object.defineProperty(exports, "$selectAll", {
1032
+ enumerable: true,
1033
+ get: function () { return lexical.$selectAll; }
1034
+ });
960
1035
  exports.$addNodeStyle = $addNodeStyle;
1036
+ exports.$copyBlockFormatIndent = $copyBlockFormatIndent;
1037
+ exports.$ensureForwardRangeSelection = $ensureForwardRangeSelection;
1038
+ exports.$forEachSelectedTextNode = $forEachSelectedTextNode;
1039
+ exports.$getComputedStyleForElement = $getComputedStyleForElement;
1040
+ exports.$getComputedStyleForParent = $getComputedStyleForParent;
961
1041
  exports.$getSelectionStyleValueForProperty = $getSelectionStyleValueForProperty;
962
1042
  exports.$isAtNodeEnd = $isAtNodeEnd;
963
1043
  exports.$isParentElementRTL = $isParentElementRTL;
1044
+ exports.$isParentRTL = $isParentRTL;
964
1045
  exports.$moveCaretSelection = $moveCaretSelection;
965
1046
  exports.$moveCharacter = $moveCharacter;
966
1047
  exports.$patchStyleText = $patchStyleText;
967
- exports.$selectAll = $selectAll;
968
1048
  exports.$setBlocksType = $setBlocksType;
969
1049
  exports.$shouldOverrideDefaultCharacterSelection = $shouldOverrideDefaultCharacterSelection;
970
1050
  exports.$sliceSelectedTextNodeContent = $sliceSelectedTextNodeContent;
@@ -972,5 +1052,6 @@ exports.$trimTextContentFromAnchor = $trimTextContentFromAnchor;
972
1052
  exports.$wrapNodes = $wrapNodes;
973
1053
  exports.createDOMRange = createDOMRange;
974
1054
  exports.createRectsFromDOMRange = createRectsFromDOMRange;
1055
+ exports.getCSSFromStyleObject = getCSSFromStyleObject;
975
1056
  exports.getStyleObjectFromCSS = getStyleObjectFromCSS;
976
1057
  exports.trimTextContentFromAnchor = trimTextContentFromAnchor;
@@ -11,25 +11,29 @@ var LexicalSelection_prod = require('./LexicalSelection.prod.js');
11
11
  *
12
12
  */
13
13
 
14
- const mod = process.env.NODE_ENV === 'development' ? LexicalSelection_dev : LexicalSelection_prod;
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
28
  exports.$addNodeStyle = $addNodeStyle;
27
29
  exports.$getSelectionStyleValueForProperty = $getSelectionStyleValueForProperty;
28
30
  exports.$isAtNodeEnd = $isAtNodeEnd;
29
31
  exports.$isParentElementRTL = $isParentElementRTL;
32
+ exports.$isParentRTL = $isParentRTL;
30
33
  exports.$moveCharacter = $moveCharacter;
31
34
  exports.$patchStyleText = $patchStyleText;
32
35
  exports.$setBlocksType = $setBlocksType;
33
36
  exports.$shouldOverrideDefaultCharacterSelection = $shouldOverrideDefaultCharacterSelection;
34
37
  exports.$sliceSelectedTextNodeContent = $sliceSelectedTextNodeContent;
35
38
  exports.createRectsFromDOMRange = createRectsFromDOMRange;
39
+ exports.getStyleObjectFromCSS = getStyleObjectFromCSS;