@lexical/selection 0.12.3 → 0.12.5

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.
@@ -7,7 +7,6 @@
7
7
  'use strict';
8
8
 
9
9
  var lexical = require('lexical');
10
- var utils = require('@lexical/utils');
11
10
 
12
11
  /**
13
12
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -147,7 +146,9 @@ function getStyleObjectFromRawCSS(css) {
147
146
  for (const style of styles) {
148
147
  if (style !== '') {
149
148
  const [key, value] = style.split(/:([^]+)/); // split on first colon
150
- styleObject[key.trim()] = value.trim();
149
+ if (key && value) {
150
+ styleObject[key.trim()] = value.trim();
151
+ }
151
152
  }
152
153
  }
153
154
  return styleObject;
@@ -239,7 +240,7 @@ function $cloneWithProperties(node) {
239
240
  * @returns The updated TextNode.
240
241
  */
241
242
  function $sliceSelectedTextNodeContent(selection, textNode) {
242
- if (textNode.isSelected() && !textNode.isSegmented() && !textNode.isToken() && (lexical.$isRangeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection))) {
243
+ if (textNode.isSelected() && !textNode.isSegmented() && !textNode.isToken() && lexical.$INTERNAL_isPointSelection(selection)) {
243
244
  const anchorNode = selection.anchor.getNode();
244
245
  const focusNode = selection.focus.getNode();
245
246
  const isAnchor = textNode.is(anchorNode);
@@ -449,7 +450,7 @@ function $patchStyleText(selection, patch) {
449
450
  const lastIndex = selectedNodesLength - 1;
450
451
  let firstNode = selectedNodes[0];
451
452
  let lastNode = selectedNodes[lastIndex];
452
- if (selection.isCollapsed()) {
453
+ if (selection.isCollapsed() && lexical.$isRangeSelection(selection)) {
453
454
  $patchStyle(selection, patch);
454
455
  return;
455
456
  }
@@ -570,13 +571,13 @@ function $setBlocksType(selection, createElement) {
570
571
  return;
571
572
  }
572
573
  const nodes = selection.getNodes();
573
- const firstSelectedBlock = utils.$getAncestor(selection.anchor.getNode(), utils.INTERNAL_$isBlock);
574
+ const firstSelectedBlock = $getAncestor(selection.anchor.getNode(), INTERNAL_$isBlock);
574
575
  if (firstSelectedBlock && nodes.indexOf(firstSelectedBlock) === -1) {
575
576
  nodes.push(firstSelectedBlock);
576
577
  }
577
578
  for (let i = 0; i < nodes.length; i++) {
578
579
  const node = nodes[i];
579
- if (!utils.INTERNAL_$isBlock(node)) {
580
+ if (!INTERNAL_$isBlock(node)) {
580
581
  continue;
581
582
  }
582
583
  const targetElement = createElement();
@@ -938,6 +939,29 @@ function $getSelectionStyleValueForProperty(selection, styleProperty, defaultVal
938
939
  return styleValue === null ? defaultValue : styleValue;
939
940
  }
940
941
 
942
+ /**
943
+ * This function is for internal use of the library.
944
+ * Please do not use it as it may change in the future.
945
+ */
946
+ function INTERNAL_$isBlock(node) {
947
+ if (lexical.$isDecoratorNode(node) && !node.isInline()) {
948
+ return true;
949
+ }
950
+ if (!lexical.$isElementNode(node) || lexical.$isRootOrShadowRoot(node)) {
951
+ return false;
952
+ }
953
+ const firstChild = node.getFirstChild();
954
+ const isLeafElement = firstChild === null || lexical.$isLineBreakNode(firstChild) || lexical.$isTextNode(firstChild) || firstChild.isInline();
955
+ return !node.isInline() && node.canBeEmpty() !== false && isLeafElement;
956
+ }
957
+ function $getAncestor(node, predicate) {
958
+ let parent = node;
959
+ while (parent !== null && parent.getParent() !== null && !predicate(parent)) {
960
+ parent = parent.getParentOrThrow();
961
+ }
962
+ return predicate(parent) ? parent : null;
963
+ }
964
+
941
965
  exports.$addNodeStyle = $addNodeStyle;
942
966
  exports.$cloneWithProperties = $cloneWithProperties;
943
967
  exports.$getSelectionStyleValueForProperty = $getSelectionStyleValueForProperty;
@@ -8,12 +8,12 @@
8
8
  */
9
9
  import type {
10
10
  ElementNode,
11
- GridSelection,
12
11
  LexicalEditor,
13
12
  LexicalNode,
14
13
  NodeKey,
15
14
  NodeSelection,
16
15
  Point,
16
+ INTERNAL_PointSelection,
17
17
  RangeSelection,
18
18
  } from 'lexical';
19
19
  declare export function $cloneWithProperties<T: LexicalNode>(node: T): T;
@@ -21,7 +21,7 @@ declare export function getStyleObjectFromCSS(css: string): {
21
21
  [string]: string,
22
22
  };
23
23
  declare export function $patchStyleText(
24
- selection: RangeSelection | GridSelection,
24
+ selection: INTERNAL_PointSelection,
25
25
  patch: {
26
26
  [string]: string | null,
27
27
  },
@@ -45,7 +45,7 @@ declare export function $moveCharacter(
45
45
  ): void;
46
46
  declare export function $selectAll(selection: RangeSelection): void;
47
47
  declare export function $wrapNodes(
48
- selection: RangeSelection | GridSelection,
48
+ selection: INTERNAL_PointSelection,
49
49
  createElement: () => ElementNode,
50
50
  wrappingElement?: ElementNode,
51
51
  ): void;
@@ -4,27 +4,27 @@
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 k=require("lexical"),u=require("@lexical/utils");let v=new Map;function w(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function y(a){let b=a.parentNode;if(null==b)throw Error("Should never happen");return[b,Array.from(b.childNodes).indexOf(a)]}function z(a){let b={};a=a.split(";");for(let d of a)if(""!==d){let [f,c]=d.split(/:([^]+)/);b[f.trim()]=c.trim()}return b}function A(a){let b=v.get(a);void 0===b&&(b=z(a),v.set(a,b));return b}
8
- function B(a){let b="";for(let d in a)d&&(b+=`${d}: ${a[d]};`);return b}function C(a,b){var d=A("getStyle"in a?a.getStyle():a.style);b=Object.entries(b).reduce((f,[c,g])=>{null===g?delete f[c]:f[c]=g;return f},{...d});d=B(b);a.setStyle(d);v.set(d,b)}
9
- function D(a,b){var d=a.getNodes(),f=d.length;if(k.DEPRECATED_$isGridSelection(a)){var c=k.$createRangeSelection(),g=c.anchor,h=c.focus;for(var e=0;e<f;e++){var l=d[e];k.DEPRECATED_$isGridCellNode(l)&&(g.set(l.getKey(),0,"element"),h.set(l.getKey(),l.getChildrenSize(),"element"),D(k.$normalizeSelection__EXPERIMENTAL(c),b))}k.$setSelection(a)}else if(--f,c=d[0],g=d[f],a.isCollapsed())C(a,b);else{e=a.anchor;var m=a.focus;l=c.getTextContent().length;var p=m.offset,n=e.offset,r=e.isBefore(m);h=r?n:p;
10
- a=r?p:n;var q=r?e.type:m.type,t=r?m.type:e.type;m=r?m.key:e.key;k.$isTextNode(c)&&h===l&&(r=c.getNextSibling(),k.$isTextNode(r)&&(h=n=0,c=r));if(1===d.length)k.$isTextNode(c)&&(h="element"===q?0:n>p?p:n,a="element"===t?l:n>p?n:p,h!==a&&(0===h&&a===l?(C(c,b),c.select(h,a)):(d=c.splitText(h,a),d=0===h?d[0]:d[1],C(d,b),d.select(0,a-h))));else for(k.$isTextNode(c)&&h<c.getTextContentSize()&&(0!==h&&(c=c.splitText(h)[1],h=0,e.set(c.getKey(),h,"text")),C(c,b)),k.$isTextNode(g)&&(h=g.getTextContent().length,
11
- g.__key!==m&&0!==a&&(a=h),a!==h&&([g]=g.splitText(a)),0!==a&&C(g,b)),a=1;a<f;a++)h=d[a],e=h.getKey(),k.$isTextNode(h)&&e!==c.getKey()&&e!==g.getKey()&&!h.isToken()&&C(h,b)}}function E(a){for(;null!==a&&!k.$isRootOrShadowRoot(a);){let b=a.getLatest(),d=a.getParent();0===b.getChildrenSize()&&a.remove(!0);a=d}}
12
- function F(a,b,d,f,c=null){if(0!==b.length){var g=b[0],h=new Map,e=[];g=k.$isElementNode(g)?g:g.getParentOrThrow();g.isInline()&&(g=g.getParentOrThrow());for(var l=!1;null!==g;){var m=g.getPreviousSibling();if(null!==m){g=m;l=!0;break}g=g.getParentOrThrow();if(k.$isRootOrShadowRoot(g))break}m=new Set;for(var p=0;p<d;p++){var n=b[p];k.$isElementNode(n)&&0===n.getChildrenSize()&&m.add(n.getKey())}var r=new Set;for(p=0;p<d;p++){n=b[p];var q=n.getParent();null!==q&&q.isInline()&&(q=q.getParent());if(null!==
13
- q&&k.$isLeafNode(n)&&!r.has(n.getKey())){if(n=q.getKey(),void 0===h.get(n)){let t=f();t.setFormat(q.getFormatType());t.setIndent(q.getIndent());e.push(t);h.set(n,t);q.getChildren().forEach(x=>{t.append(x);r.add(x.getKey());k.$isElementNode(x)&&x.getChildrenKeys().forEach(I=>r.add(I))});E(q)}}else m.has(n.getKey())&&(q=f(),q.setFormat(n.getFormatType()),q.setIndent(n.getIndent()),e.push(q),n.remove(!0))}if(null!==c)for(b=0;b<e.length;b++)c.append(e[b]);b=null;if(k.$isRootOrShadowRoot(g))if(l)if(null!==
7
+ 'use strict';var k=require("lexical");let u=new Map;function v(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function w(a){let b=a.parentNode;if(null==b)throw Error("Should never happen");return[b,Array.from(b.childNodes).indexOf(a)]}function y(a){let b={};a=a.split(";");for(let d of a)if(""!==d){let [f,c]=d.split(/:([^]+)/);f&&c&&(b[f.trim()]=c.trim())}return b}function z(a){let b=u.get(a);void 0===b&&(b=y(a),u.set(a,b));return b}
8
+ function A(a){let b="";for(let d in a)d&&(b+=`${d}: ${a[d]};`);return b}function B(a,b){var d=z("getStyle"in a?a.getStyle():a.style);b=Object.entries(b).reduce((f,[c,g])=>{null===g?delete f[c]:f[c]=g;return f},{...d});d=A(b);a.setStyle(d);u.set(d,b)}
9
+ function C(a,b){var d=a.getNodes(),f=d.length;if(k.DEPRECATED_$isGridSelection(a)){var c=k.$createRangeSelection(),g=c.anchor,h=c.focus;for(var e=0;e<f;e++){var l=d[e];k.DEPRECATED_$isGridCellNode(l)&&(g.set(l.getKey(),0,"element"),h.set(l.getKey(),l.getChildrenSize(),"element"),C(k.$normalizeSelection__EXPERIMENTAL(c),b))}k.$setSelection(a)}else if(--f,c=d[0],g=d[f],a.isCollapsed()&&k.$isRangeSelection(a))B(a,b);else{e=a.anchor;var m=a.focus;l=c.getTextContent().length;var p=m.offset,n=e.offset,
10
+ r=e.isBefore(m);h=r?n:p;a=r?p:n;var q=r?e.type:m.type,t=r?m.type:e.type;m=r?m.key:e.key;k.$isTextNode(c)&&h===l&&(r=c.getNextSibling(),k.$isTextNode(r)&&(h=n=0,c=r));if(1===d.length)k.$isTextNode(c)&&(h="element"===q?0:n>p?p:n,a="element"===t?l:n>p?n:p,h!==a&&(0===h&&a===l?(B(c,b),c.select(h,a)):(d=c.splitText(h,a),d=0===h?d[0]:d[1],B(d,b),d.select(0,a-h))));else for(k.$isTextNode(c)&&h<c.getTextContentSize()&&(0!==h&&(c=c.splitText(h)[1],h=0,e.set(c.getKey(),h,"text")),B(c,b)),k.$isTextNode(g)&&
11
+ (h=g.getTextContent().length,g.__key!==m&&0!==a&&(a=h),a!==h&&([g]=g.splitText(a)),0!==a&&B(g,b)),a=1;a<f;a++)h=d[a],e=h.getKey(),k.$isTextNode(h)&&e!==c.getKey()&&e!==g.getKey()&&!h.isToken()&&B(h,b)}}function D(a){for(;null!==a&&!k.$isRootOrShadowRoot(a);){let b=a.getLatest(),d=a.getParent();0===b.getChildrenSize()&&a.remove(!0);a=d}}
12
+ function E(a,b,d,f,c=null){if(0!==b.length){var g=b[0],h=new Map,e=[];g=k.$isElementNode(g)?g:g.getParentOrThrow();g.isInline()&&(g=g.getParentOrThrow());for(var l=!1;null!==g;){var m=g.getPreviousSibling();if(null!==m){g=m;l=!0;break}g=g.getParentOrThrow();if(k.$isRootOrShadowRoot(g))break}m=new Set;for(var p=0;p<d;p++){var n=b[p];k.$isElementNode(n)&&0===n.getChildrenSize()&&m.add(n.getKey())}var r=new Set;for(p=0;p<d;p++){n=b[p];var q=n.getParent();null!==q&&q.isInline()&&(q=q.getParent());if(null!==
13
+ q&&k.$isLeafNode(n)&&!r.has(n.getKey())){if(n=q.getKey(),void 0===h.get(n)){let t=f();t.setFormat(q.getFormatType());t.setIndent(q.getIndent());e.push(t);h.set(n,t);q.getChildren().forEach(x=>{t.append(x);r.add(x.getKey());k.$isElementNode(x)&&x.getChildrenKeys().forEach(I=>r.add(I))});D(q)}}else m.has(n.getKey())&&(q=f(),q.setFormat(n.getFormatType()),q.setIndent(n.getIndent()),e.push(q),n.remove(!0))}if(null!==c)for(b=0;b<e.length;b++)c.append(e[b]);b=null;if(k.$isRootOrShadowRoot(g))if(l)if(null!==
14
14
  c)g.insertAfter(c);else for(c=e.length-1;0<=c;c--)g.insertAfter(e[c]);else if(l=g.getFirstChild(),k.$isElementNode(l)&&(g=l),null===l)if(c)g.append(c);else for(c=0;c<e.length;c++)l=e[c],g.append(l),b=l;else if(null!==c)l.insertBefore(c);else for(g=0;g<e.length;g++)c=e[g],l.insertBefore(c),b=c;else if(c)g.insertAfter(c);else for(c=e.length-1;0<=c;c--)l=e[c],g.insertAfter(l),b=l;e=k.$getPreviousSelection();k.$isRangeSelection(e)&&e.anchor.getNode().isAttached()&&e.focus.getNode().isAttached()?k.$setSelection(e.clone()):
15
- null!==b?b.selectEnd():a.dirty=!0}}function G(a,b,d,f){a.modify(b?"extend":"move",d,f)}function H(a){a=a.anchor.getNode();return"rtl"===(k.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}exports.$addNodeStyle=function(a){a=a.getStyle();let b=z(a);v.set(a,b)};
16
- exports.$cloneWithProperties=function(a){let b=a.constructor.clone(a);b.__parent=a.__parent;b.__next=a.__next;b.__prev=a.__prev;if(k.$isElementNode(a)&&k.$isElementNode(b))return b.__first=a.__first,b.__last=a.__last,b.__size=a.__size,b.__format=a.__format,b.__indent=a.__indent,b.__dir=a.__dir,b;k.$isTextNode(a)&&k.$isTextNode(b)&&(b.__format=a.__format,b.__style=a.__style,b.__mode=a.__mode,b.__detail=a.__detail);return b};
17
- exports.$getSelectionStyleValueForProperty=function(a,b,d=""){let f=null,c=a.getNodes();var g=a.anchor,h=a.focus,e=a.isBackward();let l=e?h.offset:g.offset;g=e?h.getNode():g.getNode();if(a.isCollapsed()&&""!==a.style&&(a=A(a.style),null!==a&&b in a))return a[b];for(a=0;a<c.length;a++){var m=c[a];if((0===a||0!==l||!m.is(g))&&k.$isTextNode(m))if(h=b,e=d,m=m.getStyle(),m=A(m),h=null!==m?m[h]||e:e,null===f)f=h;else if(f!==h){f="";break}}return null===f?d:f};
18
- exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};exports.$isParentElementRTL=H;exports.$moveCaretSelection=G;exports.$moveCharacter=function(a,b,d){let f=H(a);G(a,b,d?!f:f,"character")};exports.$patchStyleText=D;
15
+ null!==b?b.selectEnd():a.dirty=!0}}function F(a,b,d,f){a.modify(b?"extend":"move",d,f)}function G(a){a=a.anchor.getNode();return"rtl"===(k.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}function H(a){if(k.$isDecoratorNode(a)&&!a.isInline())return!0;if(!k.$isElementNode(a)||k.$isRootOrShadowRoot(a))return!1;var b=a.getFirstChild();b=null===b||k.$isLineBreakNode(b)||k.$isTextNode(b)||b.isInline();return!a.isInline()&&!1!==a.canBeEmpty()&&b}
16
+ exports.$addNodeStyle=function(a){a=a.getStyle();let b=y(a);u.set(a,b)};exports.$cloneWithProperties=function(a){let b=a.constructor.clone(a);b.__parent=a.__parent;b.__next=a.__next;b.__prev=a.__prev;if(k.$isElementNode(a)&&k.$isElementNode(b))return b.__first=a.__first,b.__last=a.__last,b.__size=a.__size,b.__format=a.__format,b.__indent=a.__indent,b.__dir=a.__dir,b;k.$isTextNode(a)&&k.$isTextNode(b)&&(b.__format=a.__format,b.__style=a.__style,b.__mode=a.__mode,b.__detail=a.__detail);return b};
17
+ exports.$getSelectionStyleValueForProperty=function(a,b,d=""){let f=null,c=a.getNodes();var g=a.anchor,h=a.focus,e=a.isBackward();let l=e?h.offset:g.offset;g=e?h.getNode():g.getNode();if(a.isCollapsed()&&""!==a.style&&(a=z(a.style),null!==a&&b in a))return a[b];for(a=0;a<c.length;a++){var m=c[a];if((0===a||0!==l||!m.is(g))&&k.$isTextNode(m))if(h=b,e=d,m=m.getStyle(),m=z(m),h=null!==m?m[h]||e:e,null===f)f=h;else if(f!==h){f="";break}}return null===f?d:f};
18
+ exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};exports.$isParentElementRTL=G;exports.$moveCaretSelection=F;exports.$moveCharacter=function(a,b,d){let f=G(a);F(a,b,d?!f:f,"character")};exports.$patchStyleText=C;
19
19
  exports.$selectAll=function(a){let b=a.anchor;a=a.focus;var d=b.getNode().getTopLevelElementOrThrow().getParentOrThrow();let f=d.getFirstDescendant();d=d.getLastDescendant();let c="element",g="element",h=0;k.$isTextNode(f)?c="text":k.$isElementNode(f)||null===f||(f=f.getParentOrThrow());k.$isTextNode(d)?(g="text",h=d.getTextContentSize()):k.$isElementNode(d)||null===d||(d=d.getParentOrThrow());f&&d&&(b.set(f.getKey(),0,c),a.set(d.getKey(),h,g))};
20
- exports.$setBlocksType=function(a,b){if("root"===a.anchor.key){b=b();var d=k.$getRoot();(a=d.getFirstChild())?a.replace(b,!0):d.append(b)}else for(d=a.getNodes(),(a=u.$getAncestor(a.anchor.getNode(),u.INTERNAL_$isBlock))&&-1===d.indexOf(a)&&d.push(a),a=0;a<d.length;a++){let f=d[a];if(!u.INTERNAL_$isBlock(f))continue;let c=b();c.setFormat(f.getFormatType());c.setIndent(f.getIndent());f.replace(c,!0)}};
20
+ exports.$setBlocksType=function(a,b){if("root"===a.anchor.key){b=b();var d=k.$getRoot();(a=d.getFirstChild())?a.replace(b,!0):d.append(b)}else{d=a.getNodes();for(a=a.anchor.getNode();null!==a&&null!==a.getParent()&&!H(a);)a=a.getParentOrThrow();(a=H(a)?a:null)&&-1===d.indexOf(a)&&d.push(a);for(a=0;a<d.length;a++){let f=d[a];if(!H(f))continue;let c=b();c.setFormat(f.getFormatType());c.setIndent(f.getIndent());f.replace(c,!0)}}};
21
21
  exports.$shouldOverrideDefaultCharacterSelection=function(a,b){a=k.$getAdjacentNode(a.focus,b);return k.$isDecoratorNode(a)&&!a.isIsolated()||k.$isElementNode(a)&&!a.isInline()&&!a.canBeEmpty()};
22
- exports.$sliceSelectedTextNodeContent=function(a,b){if(b.isSelected()&&!b.isSegmented()&&!b.isToken()&&(k.$isRangeSelection(a)||k.DEPRECATED_$isGridSelection(a))){var d=a.anchor.getNode(),f=a.focus.getNode(),c=b.is(d),g=b.is(f);if(c||g){c=a.isBackward();let [h,e]=a.getCharacterOffsets();a=d.is(f);g=b.is(c?f:d);f=b.is(c?d:f);d=0;let l=void 0;a?(d=h>e?e:h,l=h>e?h:e):g?(d=c?e:h,l=void 0):f&&(c=c?h:e,d=0,l=c);b.__text=b.__text.slice(d,l)}}return b};
23
- exports.$wrapNodes=function(a,b,d=null){var f=a.getNodes();let c=f.length;var g=a.anchor;if(0===c||1===c&&"element"===g.type&&0===g.getNode().getChildrenSize()){a="text"===g.type?g.getNode().getParentOrThrow():g.getNode();f=a.getChildren();let e=b();e.setFormat(a.getFormatType());e.setIndent(a.getIndent());f.forEach(l=>e.append(l));d&&(e=d.append(e));a.replace(e)}else{g=null;var h=[];for(let e=0;e<c;e++){let l=f[e];k.$isRootOrShadowRoot(l)?(F(a,h,h.length,b,d),h=[],g=l):null===g||null!==g&&k.$hasAncestor(l,
24
- g)?h.push(l):(F(a,h,h.length,b,d),h=[l])}F(a,h,h.length,b,d)}};
25
- exports.createDOMRange=function(a,b,d,f,c){let g=b.getKey(),h=f.getKey(),e=document.createRange(),l=a.getElementByKey(g);a=a.getElementByKey(h);k.$isTextNode(b)&&(l=w(l));k.$isTextNode(f)&&(a=w(a));if(void 0===b||void 0===f||null===l||null===a)return null;"BR"===l.nodeName&&([l,d]=y(l));"BR"===a.nodeName&&([a,c]=y(a));b=l.firstChild;l===a&&null!=b&&"BR"===b.nodeName&&0===d&&0===c&&(c=1);try{e.setStart(l,d),e.setEnd(a,c)}catch(m){return null}!e.collapsed||d===c&&g===h||(e.setStart(a,c),e.setEnd(l,
22
+ exports.$sliceSelectedTextNodeContent=function(a,b){if(b.isSelected()&&!b.isSegmented()&&!b.isToken()&&k.$INTERNAL_isPointSelection(a)){var d=a.anchor.getNode(),f=a.focus.getNode(),c=b.is(d),g=b.is(f);if(c||g){c=a.isBackward();let [h,e]=a.getCharacterOffsets();a=d.is(f);g=b.is(c?f:d);f=b.is(c?d:f);d=0;let l=void 0;a?(d=h>e?e:h,l=h>e?h:e):g?(d=c?e:h,l=void 0):f&&(c=c?h:e,d=0,l=c);b.__text=b.__text.slice(d,l)}}return b};
23
+ exports.$wrapNodes=function(a,b,d=null){var f=a.getNodes();let c=f.length;var g=a.anchor;if(0===c||1===c&&"element"===g.type&&0===g.getNode().getChildrenSize()){a="text"===g.type?g.getNode().getParentOrThrow():g.getNode();f=a.getChildren();let e=b();e.setFormat(a.getFormatType());e.setIndent(a.getIndent());f.forEach(l=>e.append(l));d&&(e=d.append(e));a.replace(e)}else{g=null;var h=[];for(let e=0;e<c;e++){let l=f[e];k.$isRootOrShadowRoot(l)?(E(a,h,h.length,b,d),h=[],g=l):null===g||null!==g&&k.$hasAncestor(l,
24
+ g)?h.push(l):(E(a,h,h.length,b,d),h=[l])}E(a,h,h.length,b,d)}};
25
+ exports.createDOMRange=function(a,b,d,f,c){let g=b.getKey(),h=f.getKey(),e=document.createRange(),l=a.getElementByKey(g);a=a.getElementByKey(h);k.$isTextNode(b)&&(l=v(l));k.$isTextNode(f)&&(a=v(a));if(void 0===b||void 0===f||null===l||null===a)return null;"BR"===l.nodeName&&([l,d]=w(l));"BR"===a.nodeName&&([a,c]=w(a));b=l.firstChild;l===a&&null!=b&&"BR"===b.nodeName&&0===d&&0===c&&(c=1);try{e.setStart(l,d),e.setEnd(a,c)}catch(m){return null}!e.collapsed||d===c&&g===h||(e.setStart(a,c),e.setEnd(l,
26
26
  d));return e};exports.createRectsFromDOMRange=function(a,b){var d=a.getRootElement();if(null===d)return[];a=d.getBoundingClientRect();d=getComputedStyle(d);d=parseFloat(d.paddingLeft)+parseFloat(d.paddingRight);b=Array.from(b.getClientRects());let f=b.length;b.sort((g,h)=>{let e=g.top-h.top;return 3>=Math.abs(e)?g.left-h.left:e});let c;for(let g=0;g<f;g++){let h=b[g],e=h.width+d===a.width;c&&c.top<=h.top&&c.top+c.height>h.top&&c.left+c.width>h.left||e?(b.splice(g--,1),f--):c=h}return b};
27
- exports.getStyleObjectFromCSS=A;
27
+ exports.getStyleObjectFromCSS=z;
28
28
  exports.trimTextContentFromAnchor=function(a,b,d){let f=b.getNode();if(k.$isElementNode(f)){var c=f.getDescendantByIndex(b.offset);null!==c&&(f=c)}for(;0<d&&null!==f;){k.$isElementNode(f)&&(c=f.getLastDescendant(),null!==c&&(f=c));var g=f.getPreviousSibling(),h=0;if(null===g){c=f.getParentOrThrow();for(var e=c.getPreviousSibling();null===e;){c=c.getParent();if(null===c){g=null;break}e=c.getPreviousSibling()}null!==c&&(h=c.isInline()?0:2,g=e)}e=f.getTextContent();""===e&&k.$isElementNode(f)&&!f.isInline()&&
29
29
  (e="\n\n");c=e.length;if(!k.$isTextNode(f)||d>=c)e=f.getParent(),f.remove(),null==e||0!==e.getChildrenSize()||k.$isRootNode(e)||e.remove(),d-=c+h,f=g;else{let l=f.getKey();h=a.getEditorState().read(()=>{const p=k.$getNodeByKey(l);return k.$isTextNode(p)&&p.isSimpleText()?p.getTextContent():null});g=c-d;let m=e.slice(0,g);null!==h&&h!==e?(d=k.$getPreviousSelection(),c=f,f.isSimpleText()?f.setTextContent(h):(c=k.$createTextNode(h),f.replace(c)),k.$isRangeSelection(d)&&d.isCollapsed()&&(d=d.anchor.offset,
30
30
  c.select(d,d))):f.isSimpleText()?(h=b.key===l,e=b.offset,e<d&&(e=c),d=h?e-d:0,c=h?e:g,h&&0===d?([d]=f.splitText(d,c),d.remove()):([,d]=f.splitText(d,c),d.remove())):(d=k.$createTextNode(m),f.replace(d));d=0}}}
package/lexical-node.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
7
  */
8
- import { GridSelection, LexicalEditor, LexicalNode, NodeSelection, Point, RangeSelection, TextNode } from 'lexical';
8
+ import { BaseSelection, INTERNAL_PointSelection, LexicalEditor, LexicalNode, Point, TextNode } from 'lexical';
9
9
  /**
10
10
  * Returns a copy of a node, but generates a new key for the copy.
11
11
  * @param node - The node to be cloned.
@@ -19,7 +19,7 @@ export declare function $cloneWithProperties<T extends LexicalNode>(node: T): T;
19
19
  * @param textNode - The TextNode to be edited.
20
20
  * @returns The updated TextNode.
21
21
  */
22
- export declare function $sliceSelectedTextNodeContent(selection: RangeSelection | GridSelection | NodeSelection, textNode: TextNode): LexicalNode;
22
+ export declare function $sliceSelectedTextNodeContent(selection: BaseSelection, textNode: TextNode): LexicalNode;
23
23
  /**
24
24
  * Determines if the current selection is at the end of the node.
25
25
  * @param point - The point of the selection to test.
@@ -47,4 +47,4 @@ export declare function $addNodeStyle(node: TextNode): void;
47
47
  * @param selection - The selected node(s) to update.
48
48
  * @param patch - The patch to apply, which can include multiple styles. { CSSProperty: value }
49
49
  */
50
- export declare function $patchStyleText(selection: RangeSelection | GridSelection, patch: Record<string, string | null>): void;
50
+ export declare function $patchStyleText(selection: INTERNAL_PointSelection, patch: Record<string, string | null>): void;
package/package.json CHANGED
@@ -9,10 +9,10 @@
9
9
  "selection"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.12.3",
12
+ "version": "0.12.5",
13
13
  "main": "LexicalSelection.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.12.3"
15
+ "lexical": "0.12.5"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",
@@ -5,13 +5,13 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
7
  */
8
- import type { ElementNode, GridSelection, LexicalNode, RangeSelection } from 'lexical';
8
+ import type { DecoratorNode, ElementNode, INTERNAL_PointSelection, LexicalNode, RangeSelection } from 'lexical';
9
9
  /**
10
10
  * Converts all nodes in the selection that are of one block type to another.
11
11
  * @param selection - The selected blocks to be converted.
12
12
  * @param createElement - The function that creates the node. eg. $createParagraphNode.
13
13
  */
14
- export declare function $setBlocksType(selection: RangeSelection | GridSelection, createElement: () => ElementNode): void;
14
+ export declare function $setBlocksType(selection: INTERNAL_PointSelection, createElement: () => ElementNode): void;
15
15
  /**
16
16
  * @deprecated
17
17
  * Wraps all nodes in the selection into another node of the type returned by createElement.
@@ -19,7 +19,7 @@ export declare function $setBlocksType(selection: RangeSelection | GridSelection
19
19
  * @param createElement - A function that creates the wrapping ElementNode. eg. $createParagraphNode.
20
20
  * @param wrappingElement - An element to append the wrapped selection and its children to.
21
21
  */
22
- export declare function $wrapNodes(selection: RangeSelection | GridSelection, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
22
+ export declare function $wrapNodes(selection: INTERNAL_PointSelection, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
23
23
  /**
24
24
  * Wraps each node into a new ElementNode.
25
25
  * @param selection - The selection of nodes to wrap.
@@ -29,7 +29,7 @@ export declare function $wrapNodes(selection: RangeSelection | GridSelection, cr
29
29
  * @param wrappingElement - An element to wrap all the nodes into.
30
30
  * @returns
31
31
  */
32
- export declare function $wrapNodesImpl(selection: RangeSelection | GridSelection, nodes: LexicalNode[], nodesLength: number, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
32
+ export declare function $wrapNodesImpl(selection: INTERNAL_PointSelection, nodes: LexicalNode[], nodesLength: number, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
33
33
  /**
34
34
  * Determines if the default character selection should be overridden. Used with DecoratorNodes
35
35
  * @param selection - The selection whose default character selection may need to be overridden.
@@ -72,3 +72,9 @@ export declare function $selectAll(selection: RangeSelection): void;
72
72
  * @returns The value of the property for the selected TextNodes.
73
73
  */
74
74
  export declare function $getSelectionStyleValueForProperty(selection: RangeSelection, styleProperty: string, defaultValue?: string): string;
75
+ /**
76
+ * This function is for internal use of the library.
77
+ * Please do not use it as it may change in the future.
78
+ */
79
+ export declare function INTERNAL_$isBlock(node: LexicalNode): node is ElementNode | DecoratorNode<unknown>;
80
+ export declare function $getAncestor<NodeType extends LexicalNode = LexicalNode>(node: LexicalNode, predicate: (ancestor: LexicalNode) => ancestor is NodeType): NodeType | null;