@lexical/selection 0.4.1 → 0.5.0

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.
@@ -201,7 +201,7 @@ function $cloneContentsImpl(selection) {
201
201
  nodeMap: Array.from(nodeMap.entries()),
202
202
  range
203
203
  };
204
- } else if (lexical.$isGridSelection(selection)) {
204
+ } else if (lexical.DEPRECATED_$isGridSelection(selection)) {
205
205
  const nodeMap = selection.getNodes().map(node => {
206
206
  const nodeKey = node.getKey();
207
207
  const clone = $cloneWithProperties(node);
@@ -219,7 +219,14 @@ function $cloneContentsImpl(selection) {
219
219
  }
220
220
 
221
221
  function getStyleObjectFromCSS(css) {
222
- return cssToStyles.get(css) || null;
222
+ let value = cssToStyles.get(css);
223
+
224
+ if (value === undefined) {
225
+ value = getStyleObjectFromRawCSS(css);
226
+ cssToStyles.set(css, value);
227
+ }
228
+
229
+ return value;
223
230
  }
224
231
 
225
232
  function getStyleObjectFromRawCSS(css) {
@@ -451,7 +458,7 @@ function $selectAll(selection) {
451
458
  function $removeParentEmptyElements(startingNode) {
452
459
  let node = startingNode;
453
460
 
454
- while (node !== null && !lexical.$isRootNode(node)) {
461
+ while (node !== null && !lexical.$isRootOrShadowRoot(node)) {
455
462
  const latest = node.getLatest();
456
463
  const parentNode = node.getParent();
457
464
 
@@ -462,8 +469,19 @@ function $removeParentEmptyElements(startingNode) {
462
469
  node = parentNode;
463
470
  }
464
471
  }
472
+ /**
473
+ * Attempts to wrap all nodes in the Selection in ElementNodes returned from createElement.
474
+ * If wrappingElement is provided, all of the wrapped leaves are appended to the wrappingElement.
475
+ * It attempts to append the resulting sub-tree to the nearest safe insertion target.
476
+ *
477
+ * @param selection
478
+ * @param createElement
479
+ * @param wrappingElement
480
+ * @returns
481
+ */
465
482
 
466
- function $wrapLeafNodesInElements(selection, createElement, wrappingElement) {
483
+
484
+ function $wrapNodes(selection, createElement, wrappingElement = null) {
467
485
  const nodes = selection.getNodes();
468
486
  const nodesLength = nodes.length;
469
487
  const anchor = selection.anchor;
@@ -484,6 +502,34 @@ function $wrapLeafNodesInElements(selection, createElement, wrappingElement) {
484
502
  return;
485
503
  }
486
504
 
505
+ let topLevelNode = null;
506
+ let descendants = [];
507
+
508
+ for (let i = 0; i < nodesLength; i++) {
509
+ const node = nodes[i]; // Determine whether wrapping has to be broken down into multiple chunks. This can happen if the
510
+ // user selected multiple Root-like nodes that have to be treated separately as if they are
511
+ // their own branch. I.e. you don't want to wrap a whole table, but rather the contents of each
512
+ // of each of the cell nodes.
513
+
514
+ if (lexical.$isRootOrShadowRoot(node)) {
515
+ $wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
516
+ descendants = [];
517
+ topLevelNode = node;
518
+ } else if (topLevelNode === null || topLevelNode !== null && lexical.$hasAncestor(node, topLevelNode)) {
519
+ descendants.push(node);
520
+ } else {
521
+ $wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
522
+ descendants = [node];
523
+ }
524
+ }
525
+
526
+ $wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
527
+ }
528
+ function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingElement = null) {
529
+ if (nodes.length === 0) {
530
+ return;
531
+ }
532
+
487
533
  const firstNode = nodes[0];
488
534
  const elementMapping = new Map();
489
535
  const elements = []; // The below logic is to find the right target for us to
@@ -497,17 +543,20 @@ function $wrapLeafNodesInElements(selection, createElement, wrappingElement) {
497
543
  target = target.getParentOrThrow();
498
544
  }
499
545
 
546
+ let targetIsPrevSibling = false;
547
+
500
548
  while (target !== null) {
501
549
  const prevSibling = target.getPreviousSibling();
502
550
 
503
551
  if (prevSibling !== null) {
504
552
  target = prevSibling;
553
+ targetIsPrevSibling = true;
505
554
  break;
506
555
  }
507
556
 
508
557
  target = target.getParentOrThrow();
509
558
 
510
- if (lexical.$isRootNode(target)) {
559
+ if (lexical.$isRootOrShadowRoot(target)) {
511
560
  break;
512
561
  }
513
562
  }
@@ -556,42 +605,53 @@ function $wrapLeafNodesInElements(selection, createElement, wrappingElement) {
556
605
  targetElement.setFormat(node.getFormatType());
557
606
  targetElement.setIndent(node.getIndent());
558
607
  elements.push(targetElement);
559
- node.remove();
608
+ node.remove(true);
560
609
  }
561
610
  }
562
611
 
563
- if (wrappingElement) {
612
+ if (wrappingElement !== null) {
564
613
  for (let i = 0; i < elements.length; i++) {
565
614
  const element = elements[i];
566
615
  wrappingElement.append(element);
567
616
  }
568
- } // If our target is the root, let's see if we can re-adjust
617
+ } // If our target is Root-like, let's see if we can re-adjust
569
618
  // so that the target is the first child instead.
570
619
 
571
620
 
572
- if (lexical.$isRootNode(target)) {
573
- const firstChild = target.getFirstChild();
574
-
575
- if (lexical.$isElementNode(firstChild)) {
576
- target = firstChild;
577
- }
578
-
579
- if (firstChild === null) {
580
- if (wrappingElement) {
581
- target.append(wrappingElement);
621
+ if (lexical.$isRootOrShadowRoot(target)) {
622
+ if (targetIsPrevSibling) {
623
+ if (wrappingElement !== null) {
624
+ target.insertAfter(wrappingElement);
582
625
  } else {
583
- for (let i = 0; i < elements.length; i++) {
626
+ for (let i = elements.length - 1; i >= 0; i--) {
584
627
  const element = elements[i];
585
- target.append(element);
628
+ target.insertAfter(element);
586
629
  }
587
630
  }
588
631
  } else {
589
- if (wrappingElement) {
590
- firstChild.insertBefore(wrappingElement);
632
+ const firstChild = target.getFirstChild();
633
+
634
+ if (lexical.$isElementNode(firstChild)) {
635
+ target = firstChild;
636
+ }
637
+
638
+ if (firstChild === null) {
639
+ if (wrappingElement) {
640
+ target.append(wrappingElement);
641
+ } else {
642
+ for (let i = 0; i < elements.length; i++) {
643
+ const element = elements[i];
644
+ target.append(element);
645
+ }
646
+ }
591
647
  } else {
592
- for (let i = 0; i < elements.length; i++) {
593
- const element = elements[i];
594
- firstChild.insertBefore(element);
648
+ if (wrappingElement !== null) {
649
+ firstChild.insertBefore(wrappingElement);
650
+ } else {
651
+ for (let i = 0; i < elements.length; i++) {
652
+ const element = elements[i];
653
+ firstChild.insertBefore(element);
654
+ }
595
655
  }
596
656
  }
597
657
  }
@@ -864,7 +924,7 @@ function trimTextContentFromAnchor(editor, anchor, delCount) {
864
924
  }
865
925
  }
866
926
  function $sliceSelectedTextNodeContent(selection, textNode) {
867
- if (textNode.isSelected() && !textNode.isSegmented() && !textNode.isToken() && (lexical.$isRangeSelection(selection) || lexical.$isGridSelection(selection))) {
927
+ if (textNode.isSelected() && !textNode.isSegmented() && !textNode.isToken() && (lexical.$isRangeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection))) {
868
928
  const anchorNode = selection.anchor.getNode();
869
929
  const focusNode = selection.focus.getNode();
870
930
  const isAnchor = textNode.is(anchorNode);
@@ -912,7 +972,8 @@ exports.$patchStyleText = $patchStyleText;
912
972
  exports.$selectAll = $selectAll;
913
973
  exports.$shouldOverrideDefaultCharacterSelection = $shouldOverrideDefaultCharacterSelection;
914
974
  exports.$sliceSelectedTextNodeContent = $sliceSelectedTextNodeContent;
915
- exports.$wrapLeafNodesInElements = $wrapLeafNodesInElements;
975
+ exports.$wrapNodes = $wrapNodes;
976
+ exports.$wrapNodesImpl = $wrapNodesImpl;
916
977
  exports.createDOMRange = createDOMRange;
917
978
  exports.createRectsFromDOMRange = createRectsFromDOMRange;
918
979
  exports.getStyleObjectFromCSS = getStyleObjectFromCSS;
@@ -25,7 +25,7 @@ declare export function $cloneContents<T: LexicalNode>(
25
25
  declare export function $cloneWithProperties<T: LexicalNode>(node: T): T;
26
26
  declare export function getStyleObjectFromCSS(css: string): {
27
27
  [string]: string,
28
- } | null;
28
+ };
29
29
  declare export function $patchStyleText(
30
30
  selection: RangeSelection | GridSelection,
31
31
  patch: {
@@ -50,7 +50,7 @@ declare export function $moveCharacter(
50
50
  isBackward: boolean,
51
51
  ): void;
52
52
  declare export function $selectAll(selection: RangeSelection): void;
53
- declare export function $wrapLeafNodesInElements(
53
+ declare export function $wrapNodes(
54
54
  selection: RangeSelection,
55
55
  createElement: () => ElementNode,
56
56
  wrappingElement?: ElementNode,
@@ -4,26 +4,28 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- 'use strict';var l=require("lexical");let t=new Map;function u(a){a=a.getLatest();let c=a.constructor.clone(a);c.__parent=a.__parent;l.$isElementNode(a)&&l.$isElementNode(c)?(c.__children=Array.from(a.__children),c.__format=a.__format,c.__indent=a.__indent,c.__dir=a.__dir):l.$isTextNode(a)&&l.$isTextNode(c)&&(c.__format=a.__format,c.__style=a.__style,c.__mode=a.__mode,c.__detail=a.__detail);return c}
8
- function w(a,c,b,d,f,g){for(var e=c;null!==a;){for(c=a.getParent();null!==c&&c.excludeFromCopy("clone");)c=c.getParent();if(null===c)break;if(!l.$isElementNode(a)||!a.excludeFromCopy("clone")){let h=a.getKey(),k=g.get(h),m=void 0===k;m&&(k=u(a),g.set(h,k));!l.$isTextNode(k)||k.isSegmented()||k.isToken()?l.$isElementNode(k)&&(k.__children=k.__children.slice(d?e:0,d?void 0:(e||0)+1)):k.__text=k.__text.slice(d?e:0,d?b:e);if(l.$isRootNode(c)){m&&f.push(h);break}}e=g.get(c.getKey());e=l.$isElementNode(e)?
9
- e.__children.indexOf(a.getKey()):a.getIndexWithinParent();a=c}}
10
- function x(a){if(l.$isRangeSelection(a)){var c=a.anchor,b=a.focus;let [k,m]=a.getCharacterOffsets();a=a.getNodes();if(0===a.length)return{nodeMap:[],range:[]};let n=a.length;var d=a[0],f=d.getParent();if(null!==f&&(!f.canBeEmpty()||l.$isRootNode(f))){var g=f.__children;if(g.length===n){var e=!0;for(var h=0;h<g.length;h++)if(g[h]!==a[h].__key){e=!1;break}e&&(n++,a.push(f))}}f=a[n-1];c=c.isBefore(b);b=new Map;g=[];e=l.$isTextNode(d)&&1===n;w(d,c?k:m,e?c?m:k:void 0,!0,g,b);for(d=0;d<n;d++){h=a[d];let r=
11
- h.getKey();if(!(b.has(r)||l.$isElementNode(h)&&h.excludeFromCopy("clone"))){let p=u(h);l.$isRootNode(h.getParent())&&g.push(h.getKey());"root"!==r&&b.set(r,p)}}w(f,e?void 0:c?m:k,void 0,!1,g,b);return{nodeMap:Array.from(b.entries()),range:g}}if(l.$isGridSelection(a))return{nodeMap:a.getNodes().map(k=>{const m=k.getKey();k=u(k);return[m,k]}),range:[a.gridKey]};throw Error("Minified Lexical error #1; visit https://lexical.dev/docs/error?code=1 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");
12
- }function y(a){return t.get(a)||null}function z(a,c){var b=y(a.getStyle());c=b?{...b,...c}:c;b="";for(d in c)d&&(b+=`${d}: ${c[d]};`);var d=b;a.setStyle(d);t.set(d,c)}function A(a,c,b,d){a.modify(c?"extend":"move",b,d)}function B(a){a=a.anchor.getNode();return"rtl"===(l.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}function C(a){for(;null!==a&&!l.$isRootNode(a);){let c=a.getLatest(),b=a.getParent();0===c.__children.length&&a.remove(!0);a=b}}
13
- function D(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function E(a){let c=a.parentNode;if(null==c)throw Error("Should never happen");return[c,Array.from(c.childNodes).indexOf(a)]}exports.$addNodeStyle=function(a){a=a.getStyle();let c={},b=a.split(";");for(let d of b)if(""!==d){let [f,g]=d.split(/:([^]+)/);c[f.trim()]=g.trim()}t.set(a,c)};exports.$cloneContents=function(a){return x(a)};exports.$cloneWithProperties=u;
14
- exports.$getSelectionStyleValueForProperty=function(a,c,b=""){let d=null,f=a.getNodes();var g=a.anchor,e=a.focus,h=a.isBackward();a=h?e.offset:g.offset;g=h?e.getNode():g.getNode();for(e=0;e<f.length;e++){var k=f[e];if((0===e||0!==a||!k.is(g))&&l.$isTextNode(k)){h=c;var m=b;k=k.getStyle();k=y(k);h=null!==k?k[h]||m:m;if(null===d)d=h;else if(d!==h){d="";break}}}return null===d?b:d};exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};
15
- exports.$isParentElementRTL=B;exports.$moveCaretSelection=A;exports.$moveCharacter=function(a,c,b){let d=B(a);A(a,c,b?!d:d,"character")};
16
- exports.$patchStyleText=function(a,c){var b=a.getNodes();let d=b.length-1,f=b[0],g=b[d];if(!a.isCollapsed()){var e=a.anchor,h=a.focus;a=f.getTextContent().length;var k=h.offset,m=e.offset;e=(h=e.isBefore(h))?m:k;h=h?k:m;if(e===f.getTextContentSize()){let n=f.getNextSibling();l.$isTextNode(n)&&(e=m=0,f=n)}if(f.is(g))l.$isTextNode(f)&&(e=m>k?k:m,h=m>k?m:k,e!==h&&(0===e&&h===a?(z(f,c),f.select(e,h)):(b=f.splitText(e,h),b=0===e?b[0]:b[1],z(b,c),b.select(0,h-e))));else for(l.$isTextNode(f)&&(0!==e&&(f=
17
- f.splitText(e)[1]),z(f,c)),l.$isTextNode(g)&&(a=g.getTextContent().length,h!==a&&([g]=g.splitText(h)),0!==h&&z(g,c)),a=1;a<d;a++)k=b[a],m=k.getKey(),l.$isTextNode(k)&&m!==f.getKey()&&m!==g.getKey()&&!k.isToken()&&z(k,c)}};
18
- exports.$selectAll=function(a){let c=a.anchor;a=a.focus;var b=c.getNode().getTopLevelElementOrThrow().getParentOrThrow();let d=b.getFirstDescendant();b=b.getLastDescendant();let f="element",g="element",e=0;l.$isTextNode(d)?f="text":l.$isElementNode(d)||null===d||(d=d.getParentOrThrow());l.$isTextNode(b)?(g="text",e=b.getTextContentSize()):l.$isElementNode(b)||null===b||(b=b.getParentOrThrow());d&&b&&(c.set(d.getKey(),0,f),a.set(b.getKey(),e,g))};
19
- exports.$shouldOverrideDefaultCharacterSelection=function(a,c){a=l.$getDecoratorNode(a.focus,c);return l.$isDecoratorNode(a)&&!a.isIsolated()};
20
- exports.$sliceSelectedTextNodeContent=function(a,c){if(c.isSelected()&&!c.isSegmented()&&!c.isToken()&&(l.$isRangeSelection(a)||l.$isGridSelection(a))){var b=a.anchor.getNode(),d=a.focus.getNode(),f=c.is(b),g=c.is(d);if(f||g){f=a.isBackward();let [e,h]=a.getCharacterOffsets();a=b.is(d);g=c.is(f?d:b);d=c.is(f?b:d);b=0;let k=void 0;a?(b=e>h?h:e,k=e>h?e:h):g?(b=f?h:e,k=void 0):d&&(f=f?e:h,b=0,k=f);c.__text=c.__text.slice(b,k)}}return c};
21
- exports.$wrapLeafNodesInElements=function(a,c,b){let d=a.getNodes(),f=d.length;var g=a.anchor;if(0===f||1===f&&"element"===g.type&&0===g.getNode().getChildrenSize()){a="text"===g.type?g.getNode().getParentOrThrow():g.getNode();g=a.getChildren();let q=c();q.setFormat(a.getFormatType());q.setIndent(a.getIndent());g.forEach(v=>q.append(v));b&&(q=b.append(q));a.replace(q)}else{var e=d[0],h=new Map;g=[];e=l.$isElementNode(e)?e:e.getParentOrThrow();for(e.isInline()&&(e=e.getParentOrThrow());null!==e;){var k=
22
- e.getPreviousSibling();if(null!==k){e=k;break}e=e.getParentOrThrow();if(l.$isRootNode(e))break}k=new Set;for(var m=0;m<f;m++){var n=d[m];l.$isElementNode(n)&&0===n.getChildrenSize()&&k.add(n.getKey())}var r=new Set;for(m=0;m<f;m++){n=d[m];var p=n.getParent();null!==p&&p.isInline()&&(p=p.getParent());if(null!==p&&l.$isLeafNode(n)&&!r.has(n.getKey())){if(n=p.getKey(),void 0===h.get(n)){let q=c();q.setFormat(p.getFormatType());q.setIndent(p.getIndent());g.push(q);h.set(n,q);p.getChildren().forEach(v=>
23
- {q.append(v);r.add(v.getKey())});C(p)}}else k.has(n.getKey())&&(p=c(),p.setFormat(n.getFormatType()),p.setIndent(n.getIndent()),g.push(p),n.remove())}if(b)for(c=0;c<g.length;c++)b.append(g[c]);if(l.$isRootNode(e))if(c=e.getFirstChild(),l.$isElementNode(c)&&(e=c),null===c)if(b)e.append(b);else for(b=0;b<g.length;b++)e.append(g[b]);else if(b)c.insertBefore(b);else for(b=0;b<g.length;b++)c.insertBefore(g[b]);else if(b)e.insertAfter(b);else for(b=g.length-1;0<=b;b--)e.insertAfter(g[b]);b=l.$getPreviousSelection();
24
- l.$isRangeSelection(b)&&b.anchor.getNode().isAttached()&&b.focus.getNode().isAttached()?l.$setSelection(b.clone()):a.dirty=!0}};
25
- exports.createDOMRange=function(a,c,b,d,f){let g=c.getKey(),e=d.getKey(),h=document.createRange(),k=a.getElementByKey(g);a=a.getElementByKey(e);l.$isTextNode(c)&&(k=D(k));l.$isTextNode(d)&&(a=D(a));if(void 0===c||void 0===d||null===k||null===a)return null;"BR"===k.nodeName&&([k,b]=E(k));"BR"===a.nodeName&&([a,f]=E(a));c=k.firstChild;k===a&&null!=c&&"BR"===c.nodeName&&0===b&&0===f&&(f=1);try{h.setStart(k,b),h.setEnd(a,f)}catch(m){return null}!h.collapsed||b===f&&g===e||(h.setStart(a,f),h.setEnd(k,
26
- b));return h};exports.createRectsFromDOMRange=function(a,c){var b=a.getRootElement();if(null===b)return[];a=b.getBoundingClientRect();b=getComputedStyle(b);b=parseFloat(b.paddingLeft)+parseFloat(b.paddingRight);c=Array.from(c.getClientRects());let d=c.length,f;for(let g=0;g<d;g++){let e=c[g],h=e.width+b===a.width;f&&f.top===e.top&&f.left===e.left&&f.width===e.width&&f.height===e.height||h?(c.splice(g--,1),d--):f=e}return c};exports.getStyleObjectFromCSS=y;
27
- exports.trimTextContentFromAnchor=function(a,c,b){let d=c.getNode();if(l.$isElementNode(d)){var f=d.getDescendantByIndex(c.offset);null!==f&&(d=f)}for(;0<b&&null!==d;){var g=d.getPreviousSibling(),e=0;if(null===g){f=d.getParentOrThrow();for(var h=f.getPreviousSibling();null===h;){f=f.getParent();if(null===f){g=null;break}h=f.getPreviousSibling()}null!==f&&(e=f.isInline()?0:2,g=l.$isElementNode(h)?h.getLastDescendant():h)}let k=d.getTextContent();""===k&&l.$isElementNode(d)&&!d.isInline()&&(k="\n\n");
28
- f=k.length;h=f-b;let m=k.slice(0,h);if(!l.$isTextNode(d)||b>=f)h=d.getParent(),d.remove(),null!=h&&0===h.getChildrenSize()&&h.remove(),b-=f+e,d=g;else{let n=d.getKey();g=a.getEditorState().read(()=>{const r=l.$getNodeByKey(n);return l.$isTextNode(r)&&r.isSimpleText()?r.getTextContent():null});null!==g&&g!==k?(b=l.$getPreviousSelection(),f=d,d.isSimpleText()?d.setTextContent(g):(f=l.$createTextNode(g),d.replace(f)),l.$isRangeSelection(b)&&b.isCollapsed()&&(b=b.anchor.offset,f.select(b,b))):d.isSimpleText()?
29
- (g=c.key===n,e=c.offset,e<b&&(e=f),b=g?e-b:0,f=g?e:h,g&&0===b?([b]=d.splitText(b,f),b.remove()):([,b]=d.splitText(b,f),b.remove())):(b=l.$createTextNode(m),d.replace(b));b=0}}}
7
+ 'use strict';var l=require("lexical");let r=new Map;function u(a){a=a.getLatest();let b=a.constructor.clone(a);b.__parent=a.__parent;l.$isElementNode(a)&&l.$isElementNode(b)?(b.__children=Array.from(a.__children),b.__format=a.__format,b.__indent=a.__indent,b.__dir=a.__dir):l.$isTextNode(a)&&l.$isTextNode(b)&&(b.__format=a.__format,b.__style=a.__style,b.__mode=a.__mode,b.__detail=a.__detail);return b}
8
+ function v(a,b,c,g,d,f){for(var h=b;null!==a;){for(b=a.getParent();null!==b&&b.excludeFromCopy("clone");)b=b.getParent();if(null===b)break;if(!l.$isElementNode(a)||!a.excludeFromCopy("clone")){let e=a.getKey(),k=f.get(e),m=void 0===k;m&&(k=u(a),f.set(e,k));!l.$isTextNode(k)||k.isSegmented()||k.isToken()?l.$isElementNode(k)&&(k.__children=k.__children.slice(g?h:0,g?void 0:(h||0)+1)):k.__text=k.__text.slice(g?h:0,g?c:h);if(l.$isRootNode(b)){m&&d.push(e);break}}h=f.get(b.getKey());h=l.$isElementNode(h)?
9
+ h.__children.indexOf(a.getKey()):a.getIndexWithinParent();a=b}}
10
+ function x(a){if(l.$isRangeSelection(a)){var b=a.anchor,c=a.focus;let [k,m]=a.getCharacterOffsets();a=a.getNodes();if(0===a.length)return{nodeMap:[],range:[]};let p=a.length;var g=a[0],d=g.getParent();if(null!==d&&(!d.canBeEmpty()||l.$isRootNode(d))){var f=d.__children;if(f.length===p){var h=!0;for(var e=0;e<f.length;e++)if(f[e]!==a[e].__key){h=!1;break}h&&(p++,a.push(d))}}d=a[p-1];b=b.isBefore(c);c=new Map;f=[];h=l.$isTextNode(g)&&1===p;v(g,b?k:m,h?b?m:k:void 0,!0,f,c);for(g=0;g<p;g++){e=a[g];let n=
11
+ e.getKey();if(!(c.has(n)||l.$isElementNode(e)&&e.excludeFromCopy("clone"))){let w=u(e);l.$isRootNode(e.getParent())&&f.push(e.getKey());"root"!==n&&c.set(n,w)}}v(d,h?void 0:b?m:k,void 0,!1,f,c);return{nodeMap:Array.from(c.entries()),range:f}}if(l.DEPRECATED_$isGridSelection(a))return{nodeMap:a.getNodes().map(k=>{const m=k.getKey();k=u(k);return[m,k]}),range:[a.gridKey]};throw Error("Minified Lexical error #1; visit https://lexical.dev/docs/error?code=1 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");
12
+ }function y(a){let b=r.get(a);void 0===b&&(b=z(a),r.set(a,b));return b}function z(a){let b={};a=a.split(";");for(let c of a)if(""!==c){let [g,d]=c.split(/:([^]+)/);b[g.trim()]=d.trim()}return b}function A(a,b){var c=y(a.getStyle());b=c?{...c,...b}:b;c="";for(g in b)g&&(c+=`${g}: ${b[g]};`);var g=c;a.setStyle(g);r.set(g,b)}function B(a,b,c,g){a.modify(b?"extend":"move",c,g)}function C(a){a=a.anchor.getNode();return"rtl"===(l.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}
13
+ function E(a){for(;null!==a&&!l.$isRootOrShadowRoot(a);){let b=a.getLatest(),c=a.getParent();0===b.__children.length&&a.remove(!0);a=c}}
14
+ function F(a,b,c,g,d=null){if(0!==b.length){var f=b[0],h=new Map,e=[];f=l.$isElementNode(f)?f:f.getParentOrThrow();f.isInline()&&(f=f.getParentOrThrow());for(var k=!1;null!==f;){var m=f.getPreviousSibling();if(null!==m){f=m;k=!0;break}f=f.getParentOrThrow();if(l.$isRootOrShadowRoot(f))break}m=new Set;for(var p=0;p<c;p++){var n=b[p];l.$isElementNode(n)&&0===n.getChildrenSize()&&m.add(n.getKey())}var w=new Set;for(p=0;p<c;p++){n=b[p];var q=n.getParent();null!==q&&q.isInline()&&(q=q.getParent());if(null!==
15
+ q&&l.$isLeafNode(n)&&!w.has(n.getKey())){if(n=q.getKey(),void 0===h.get(n)){let t=g();t.setFormat(q.getFormatType());t.setIndent(q.getIndent());e.push(t);h.set(n,t);q.getChildren().forEach(D=>{t.append(D);w.add(D.getKey())});E(q)}}else m.has(n.getKey())&&(q=g(),q.setFormat(n.getFormatType()),q.setIndent(n.getIndent()),e.push(q),n.remove(!0))}if(null!==d)for(b=0;b<e.length;b++)d.append(e[b]);if(l.$isRootOrShadowRoot(f))if(k)if(null!==d)f.insertAfter(d);else for(d=e.length-1;0<=d;d--)f.insertAfter(e[d]);
16
+ else if(b=f.getFirstChild(),l.$isElementNode(b)&&(f=b),null===b)if(d)f.append(d);else for(d=0;d<e.length;d++)f.append(e[d]);else if(null!==d)b.insertBefore(d);else for(f=0;f<e.length;f++)b.insertBefore(e[f]);else if(d)f.insertAfter(d);else for(d=e.length-1;0<=d;d--)f.insertAfter(e[d]);e=l.$getPreviousSelection();l.$isRangeSelection(e)&&e.anchor.getNode().isAttached()&&e.focus.getNode().isAttached()?l.$setSelection(e.clone()):a.dirty=!0}}
17
+ function G(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function H(a){let b=a.parentNode;if(null==b)throw Error("Should never happen");return[b,Array.from(b.childNodes).indexOf(a)]}exports.$addNodeStyle=function(a){a=a.getStyle();let b=z(a);r.set(a,b)};exports.$cloneContents=function(a){return x(a)};exports.$cloneWithProperties=u;
18
+ exports.$getSelectionStyleValueForProperty=function(a,b,c=""){let g=null,d=a.getNodes();var f=a.anchor,h=a.focus,e=a.isBackward();a=e?h.offset:f.offset;f=e?h.getNode():f.getNode();for(h=0;h<d.length;h++){var k=d[h];if((0===h||0!==a||!k.is(f))&&l.$isTextNode(k)){e=b;var m=c;k=k.getStyle();k=y(k);e=null!==k?k[e]||m:m;if(null===g)g=e;else if(g!==e){g="";break}}}return null===g?c:g};exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};
19
+ exports.$isParentElementRTL=C;exports.$moveCaretSelection=B;exports.$moveCharacter=function(a,b,c){let g=C(a);B(a,b,c?!g:g,"character")};
20
+ exports.$patchStyleText=function(a,b){var c=a.getNodes();let g=c.length-1,d=c[0],f=c[g];if(!a.isCollapsed()){var h=a.anchor,e=a.focus;a=d.getTextContent().length;var k=e.offset,m=h.offset;h=(e=h.isBefore(e))?m:k;e=e?k:m;if(h===d.getTextContentSize()){let p=d.getNextSibling();l.$isTextNode(p)&&(h=m=0,d=p)}if(d.is(f))l.$isTextNode(d)&&(h=m>k?k:m,e=m>k?m:k,h!==e&&(0===h&&e===a?(A(d,b),d.select(h,e)):(c=d.splitText(h,e),c=0===h?c[0]:c[1],A(c,b),c.select(0,e-h))));else for(l.$isTextNode(d)&&(0!==h&&(d=
21
+ d.splitText(h)[1]),A(d,b)),l.$isTextNode(f)&&(a=f.getTextContent().length,e!==a&&([f]=f.splitText(e)),0!==e&&A(f,b)),a=1;a<g;a++)k=c[a],m=k.getKey(),l.$isTextNode(k)&&m!==d.getKey()&&m!==f.getKey()&&!k.isToken()&&A(k,b)}};
22
+ exports.$selectAll=function(a){let b=a.anchor;a=a.focus;var c=b.getNode().getTopLevelElementOrThrow().getParentOrThrow();let g=c.getFirstDescendant();c=c.getLastDescendant();let d="element",f="element",h=0;l.$isTextNode(g)?d="text":l.$isElementNode(g)||null===g||(g=g.getParentOrThrow());l.$isTextNode(c)?(f="text",h=c.getTextContentSize()):l.$isElementNode(c)||null===c||(c=c.getParentOrThrow());g&&c&&(b.set(g.getKey(),0,d),a.set(c.getKey(),h,f))};
23
+ exports.$shouldOverrideDefaultCharacterSelection=function(a,b){a=l.$getDecoratorNode(a.focus,b);return l.$isDecoratorNode(a)&&!a.isIsolated()};
24
+ exports.$sliceSelectedTextNodeContent=function(a,b){if(b.isSelected()&&!b.isSegmented()&&!b.isToken()&&(l.$isRangeSelection(a)||l.DEPRECATED_$isGridSelection(a))){var c=a.anchor.getNode(),g=a.focus.getNode(),d=b.is(c),f=b.is(g);if(d||f){d=a.isBackward();let [h,e]=a.getCharacterOffsets();a=c.is(g);f=b.is(d?g:c);g=b.is(d?c:g);c=0;let k=void 0;a?(c=h>e?e:h,k=h>e?h:e):f?(c=d?e:h,k=void 0):g&&(d=d?h:e,c=0,k=d);b.__text=b.__text.slice(c,k)}}return b};
25
+ exports.$wrapNodes=function(a,b,c=null){var g=a.getNodes();let d=g.length;var f=a.anchor;if(0===d||1===d&&"element"===f.type&&0===f.getNode().getChildrenSize()){a="text"===f.type?f.getNode().getParentOrThrow():f.getNode();g=a.getChildren();let e=b();e.setFormat(a.getFormatType());e.setIndent(a.getIndent());g.forEach(k=>e.append(k));c&&(e=c.append(e));a.replace(e)}else{f=null;var h=[];for(let e=0;e<d;e++){let k=g[e];l.$isRootOrShadowRoot(k)?(F(a,h,h.length,b,c),h=[],f=k):null===f||null!==f&&l.$hasAncestor(k,
26
+ f)?h.push(k):(F(a,h,h.length,b,c),h=[k])}F(a,h,h.length,b,c)}};exports.$wrapNodesImpl=F;
27
+ exports.createDOMRange=function(a,b,c,g,d){let f=b.getKey(),h=g.getKey(),e=document.createRange(),k=a.getElementByKey(f);a=a.getElementByKey(h);l.$isTextNode(b)&&(k=G(k));l.$isTextNode(g)&&(a=G(a));if(void 0===b||void 0===g||null===k||null===a)return null;"BR"===k.nodeName&&([k,c]=H(k));"BR"===a.nodeName&&([a,d]=H(a));b=k.firstChild;k===a&&null!=b&&"BR"===b.nodeName&&0===c&&0===d&&(d=1);try{e.setStart(k,c),e.setEnd(a,d)}catch(m){return null}!e.collapsed||c===d&&f===h||(e.setStart(a,d),e.setEnd(k,
28
+ c));return e};exports.createRectsFromDOMRange=function(a,b){var c=a.getRootElement();if(null===c)return[];a=c.getBoundingClientRect();c=getComputedStyle(c);c=parseFloat(c.paddingLeft)+parseFloat(c.paddingRight);b=Array.from(b.getClientRects());let g=b.length,d;for(let f=0;f<g;f++){let h=b[f],e=h.width+c===a.width;d&&d.top===h.top&&d.left===h.left&&d.width===h.width&&d.height===h.height||e?(b.splice(f--,1),g--):d=h}return b};exports.getStyleObjectFromCSS=y;
29
+ exports.trimTextContentFromAnchor=function(a,b,c){let g=b.getNode();if(l.$isElementNode(g)){var d=g.getDescendantByIndex(b.offset);null!==d&&(g=d)}for(;0<c&&null!==g;){var f=g.getPreviousSibling(),h=0;if(null===f){d=g.getParentOrThrow();for(var e=d.getPreviousSibling();null===e;){d=d.getParent();if(null===d){f=null;break}e=d.getPreviousSibling()}null!==d&&(h=d.isInline()?0:2,f=l.$isElementNode(e)?e.getLastDescendant():e)}let k=g.getTextContent();""===k&&l.$isElementNode(g)&&!g.isInline()&&(k="\n\n");
30
+ d=k.length;e=d-c;let m=k.slice(0,e);if(!l.$isTextNode(g)||c>=d)e=g.getParent(),g.remove(),null!=e&&0===e.getChildrenSize()&&e.remove(),c-=d+h,g=f;else{let p=g.getKey();f=a.getEditorState().read(()=>{const n=l.$getNodeByKey(p);return l.$isTextNode(n)&&n.isSimpleText()?n.getTextContent():null});null!==f&&f!==k?(c=l.$getPreviousSelection(),d=g,g.isSimpleText()?g.setTextContent(f):(d=l.$createTextNode(f),g.replace(d)),l.$isRangeSelection(c)&&c.isCollapsed()&&(c=c.anchor.offset,d.select(c,c))):g.isSimpleText()?
31
+ (f=b.key===p,h=b.offset,h<c&&(h=d),c=f?h-c:0,d=f?h:e,f&&0===c?([c]=g.splitText(c,d),c.remove()):([,c]=g.splitText(c,d),c.remove())):(c=l.$createTextNode(m),g.replace(c));c=0}}}
package/README.md CHANGED
@@ -95,12 +95,12 @@ Expands the current Selection to cover all of the content in the editor.
95
95
  export function $selectAll(selection: RangeSelection): void;
96
96
  ```
97
97
 
98
- #### `$wrapLeafNodesInElements`
98
+ #### `$wrapNodes`
99
99
 
100
- Attempts to wrap all leaf nodes in the Selection in ElementNodes returned from createElement. If wrappingElement is provided, all of the wrapped leaves are appended to the wrappingElement. It attempts to append the resulting sub-tree to the nearest safe insertion target.
100
+ Attempts to wrap all nodes in the Selection in ElementNodes returned from createElement. If wrappingElement is provided, all of the wrapped leaves are appended to the wrappingElement. It attempts to append the resulting sub-tree to the nearest safe insertion target.
101
101
 
102
102
  ```ts
103
- export function $wrapLeafNodesInElements(
103
+ export function $wrapNodes(
104
104
  selection: RangeSelection,
105
105
  createElement: () => ElementNode,
106
106
  wrappingElement?: ElementNode,
package/index.d.ts CHANGED
@@ -6,13 +6,13 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  *
8
8
  */
9
- import type { ElementNode, GridSelection, LexicalEditor, LexicalNode, NodeKey, NodeSelection, Point, RangeSelection, TextNode } from 'lexical';
9
+ import { ElementNode, GridSelection, LexicalEditor, LexicalNode, NodeKey, NodeSelection, Point, RangeSelection, TextNode } from 'lexical';
10
10
  export declare function $cloneWithProperties<T extends LexicalNode>(node: T): T;
11
11
  export declare function $cloneContents(selection: RangeSelection | NodeSelection | GridSelection): {
12
12
  nodeMap: Array<[NodeKey, LexicalNode]>;
13
13
  range: Array<NodeKey>;
14
14
  };
15
- export declare function getStyleObjectFromCSS(css: string): Record<string, string> | null;
15
+ export declare function getStyleObjectFromCSS(css: string): Record<string, string>;
16
16
  export declare function $addNodeStyle(node: TextNode): void;
17
17
  export declare function $patchStyleText(selection: RangeSelection | GridSelection, patch: Record<string, string>): void;
18
18
  export declare function $getSelectionStyleValueForProperty(selection: RangeSelection, styleProperty: string, defaultValue?: string): string;
@@ -20,7 +20,18 @@ export declare function $moveCaretSelection(selection: RangeSelection, isHolding
20
20
  export declare function $isParentElementRTL(selection: RangeSelection): boolean;
21
21
  export declare function $moveCharacter(selection: RangeSelection, isHoldingShift: boolean, isBackward: boolean): void;
22
22
  export declare function $selectAll(selection: RangeSelection): void;
23
- export declare function $wrapLeafNodesInElements(selection: RangeSelection, createElement: () => ElementNode, wrappingElement?: ElementNode): void;
23
+ /**
24
+ * Attempts to wrap all nodes in the Selection in ElementNodes returned from createElement.
25
+ * If wrappingElement is provided, all of the wrapped leaves are appended to the wrappingElement.
26
+ * It attempts to append the resulting sub-tree to the nearest safe insertion target.
27
+ *
28
+ * @param selection
29
+ * @param createElement
30
+ * @param wrappingElement
31
+ * @returns
32
+ */
33
+ export declare function $wrapNodes(selection: RangeSelection, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
34
+ export declare function $wrapNodesImpl(selection: RangeSelection, nodes: LexicalNode[], nodesLength: number, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
24
35
  export declare function $isAtNodeEnd(point: Point): boolean;
25
36
  export declare function $shouldOverrideDefaultCharacterSelection(selection: RangeSelection, isBackward: boolean): boolean;
26
37
  export declare function createDOMRange(editor: LexicalEditor, anchorNode: LexicalNode, _anchorOffset: number, focusNode: LexicalNode, _focusOffset: number): Range | null;
package/package.json CHANGED
@@ -9,10 +9,10 @@
9
9
  "selection"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.4.1",
12
+ "version": "0.5.0",
13
13
  "main": "LexicalSelection.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.4.1"
15
+ "lexical": "0.5.0"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",