@lexical/selection 0.12.6 → 0.13.1

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.
@@ -240,14 +240,16 @@ function $cloneWithProperties(node) {
240
240
  * @returns The updated TextNode.
241
241
  */
242
242
  function $sliceSelectedTextNodeContent(selection, textNode) {
243
- if (textNode.isSelected(selection) && !textNode.isSegmented() && !textNode.isToken() && lexical.$INTERNAL_isPointSelection(selection)) {
244
- const anchorNode = selection.anchor.getNode();
245
- const focusNode = selection.focus.getNode();
243
+ const anchorAndFocus = selection.getStartEndPoints();
244
+ if (textNode.isSelected(selection) && !textNode.isSegmented() && !textNode.isToken() && anchorAndFocus !== null) {
245
+ const [anchor, focus] = anchorAndFocus;
246
+ const isBackward = selection.isBackward();
247
+ const anchorNode = anchor.getNode();
248
+ const focusNode = focus.getNode();
246
249
  const isAnchor = textNode.is(anchorNode);
247
250
  const isFocus = textNode.is(focusNode);
248
251
  if (isAnchor || isFocus) {
249
- const isBackward = selection.isBackward();
250
- const [anchorOffset, focusOffset] = selection.getCharacterOffsets();
252
+ const [anchorOffset, focusOffset] = lexical.$getCharacterOffsets(selection);
251
253
  const isSame = anchorNode.is(focusNode);
252
254
  const isFirst = textNode.is(isBackward ? focusNode : anchorNode);
253
255
  const isLast = textNode.is(isBackward ? anchorNode : focusNode);
@@ -412,7 +414,9 @@ function $addNodeStyle(node) {
412
414
  function $patchStyle(target, patch) {
413
415
  const prevStyles = getStyleObjectFromCSS('getStyle' in target ? target.getStyle() : target.style);
414
416
  const newStyles = Object.entries(patch).reduce((styles, [key, value]) => {
415
- if (value === null) {
417
+ if (value instanceof Function) {
418
+ styles[key] = value(prevStyles[key]);
419
+ } else if (value === null) {
416
420
  delete styles[key];
417
421
  } else {
418
422
  styles[key] = value;
@@ -431,26 +435,16 @@ function $patchStyle(target, patch) {
431
435
  * Will update partially selected TextNodes by splitting the TextNode and applying
432
436
  * the styles to the appropriate one.
433
437
  * @param selection - The selected node(s) to update.
434
- * @param patch - The patch to apply, which can include multiple styles. { CSSProperty: value }
438
+ * @param patch - The patch to apply, which can include multiple styles. { CSSProperty: value }. Can also accept a function that returns the new property value.
435
439
  */
436
440
  function $patchStyleText(selection, patch) {
437
441
  const selectedNodes = selection.getNodes();
438
442
  const selectedNodesLength = selectedNodes.length;
439
- if (!lexical.$isRangeSelection(selection)) {
440
- const cellSelection = lexical.$createRangeSelection();
441
- const cellSelectionAnchor = cellSelection.anchor;
442
- const cellSelectionFocus = cellSelection.focus;
443
- for (let i = 0; i < selectedNodesLength; i++) {
444
- const node = selectedNodes[i];
445
- if (lexical.DEPRECATED_$isGridCellNode(node)) {
446
- cellSelectionAnchor.set(node.getKey(), 0, 'element');
447
- cellSelectionFocus.set(node.getKey(), node.getChildrenSize(), 'element');
448
- $patchStyleText(lexical.$normalizeSelection__EXPERIMENTAL(cellSelection), patch);
449
- }
450
- }
451
- lexical.$setSelection(selection);
443
+ const anchorAndFocus = selection.getStartEndPoints();
444
+ if (anchorAndFocus === null) {
452
445
  return;
453
446
  }
447
+ const [anchor, focus] = anchorAndFocus;
454
448
  const lastIndex = selectedNodesLength - 1;
455
449
  let firstNode = selectedNodes[0];
456
450
  let lastNode = selectedNodes[lastIndex];
@@ -458,8 +452,6 @@ function $patchStyleText(selection, patch) {
458
452
  $patchStyle(selection, patch);
459
453
  return;
460
454
  }
461
- const anchor = selection.anchor;
462
- const focus = selection.focus;
463
455
  const firstNodeText = firstNode.getTextContent();
464
456
  const firstNodeTextLength = firstNodeText.length;
465
457
  const focusOffset = focus.offset;
@@ -485,7 +477,7 @@ function $patchStyleText(selection, patch) {
485
477
 
486
478
  // This is the case where we only selected a single node
487
479
  if (selectedNodes.length === 1) {
488
- if (lexical.$isTextNode(firstNode)) {
480
+ if (lexical.$isTextNode(firstNode) && firstNode.canHaveFormat()) {
489
481
  startOffset = startType === 'element' ? 0 : anchorOffset > focusOffset ? focusOffset : anchorOffset;
490
482
  endOffset = endType === 'element' ? firstNodeTextLength : anchorOffset > focusOffset ? anchorOffset : focusOffset;
491
483
 
@@ -508,7 +500,7 @@ function $patchStyleText(selection, patch) {
508
500
  }
509
501
  } // multiple nodes selected.
510
502
  } else {
511
- if (lexical.$isTextNode(firstNode) && startOffset < firstNode.getTextContentSize()) {
503
+ if (lexical.$isTextNode(firstNode) && startOffset < firstNode.getTextContentSize() && firstNode.canHaveFormat()) {
512
504
  if (startOffset !== 0) {
513
505
  // the entire first node isn't selected, so split it
514
506
  firstNode = firstNode.splitText(startOffset)[1];
@@ -517,7 +509,7 @@ function $patchStyleText(selection, patch) {
517
509
  }
518
510
  $patchStyle(firstNode, patch);
519
511
  }
520
- if (lexical.$isTextNode(lastNode)) {
512
+ if (lexical.$isTextNode(lastNode) && lastNode.canHaveFormat()) {
521
513
  const lastNodeText = lastNode.getTextContent();
522
514
  const lastNodeTextLength = lastNodeText.length;
523
515
 
@@ -533,7 +525,7 @@ function $patchStyleText(selection, patch) {
533
525
  if (endOffset !== lastNodeTextLength) {
534
526
  [lastNode] = lastNode.splitText(endOffset);
535
527
  }
536
- if (endOffset !== 0) {
528
+ if (endOffset !== 0 || endType === 'element') {
537
529
  $patchStyle(lastNode, patch);
538
530
  }
539
531
  }
@@ -542,7 +534,7 @@ function $patchStyleText(selection, patch) {
542
534
  for (let i = 1; i < lastIndex; i++) {
543
535
  const selectedNode = selectedNodes[i];
544
536
  const selectedNodeKey = selectedNode.getKey();
545
- if (lexical.$isTextNode(selectedNode) && selectedNodeKey !== firstNode.getKey() && selectedNodeKey !== lastNode.getKey() && !selectedNode.isToken()) {
537
+ if (lexical.$isTextNode(selectedNode) && selectedNode.canHaveFormat() && selectedNodeKey !== firstNode.getKey() && selectedNodeKey !== lastNode.getKey() && !selectedNode.isToken()) {
546
538
  $patchStyle(selectedNode, patch);
547
539
  }
548
540
  }
@@ -563,7 +555,12 @@ function $patchStyleText(selection, patch) {
563
555
  * @param createElement - The function that creates the node. eg. $createParagraphNode.
564
556
  */
565
557
  function $setBlocksType(selection, createElement) {
566
- if (selection.anchor.key === 'root') {
558
+ if (selection === null) {
559
+ return;
560
+ }
561
+ const anchorAndFocus = selection.getStartEndPoints();
562
+ const anchor = anchorAndFocus ? anchorAndFocus[0] : null;
563
+ if (anchor !== null && anchor.key === 'root') {
567
564
  const element = createElement();
568
565
  const root = lexical.$getRoot();
569
566
  const firstChild = root.getFirstChild();
@@ -575,7 +572,7 @@ function $setBlocksType(selection, createElement) {
575
572
  return;
576
573
  }
577
574
  const nodes = selection.getNodes();
578
- const firstSelectedBlock = $getAncestor(selection.anchor.getNode(), INTERNAL_$isBlock);
575
+ const firstSelectedBlock = anchor !== null ? $getAncestor(anchor.getNode(), INTERNAL_$isBlock) : false;
579
576
  if (firstSelectedBlock && nodes.indexOf(firstSelectedBlock) === -1) {
580
577
  nodes.push(firstSelectedBlock);
581
578
  }
@@ -616,10 +613,11 @@ function $removeParentEmptyElements(startingNode) {
616
613
  * @param wrappingElement - An element to append the wrapped selection and its children to.
617
614
  */
618
615
  function $wrapNodes(selection, createElement, wrappingElement = null) {
616
+ const anchorAndFocus = selection.getStartEndPoints();
617
+ const anchor = anchorAndFocus ? anchorAndFocus[0] : null;
619
618
  const nodes = selection.getNodes();
620
619
  const nodesLength = nodes.length;
621
- const anchor = selection.anchor;
622
- if (nodesLength === 0 || nodesLength === 1 && anchor.type === 'element' && anchor.getNode().getChildrenSize() === 0) {
620
+ if (anchor !== null && (nodesLength === 0 || nodesLength === 1 && anchor.type === 'element' && anchor.getNode().getChildrenSize() === 0)) {
623
621
  const target = anchor.type === 'text' ? anchor.getNode().getParentOrThrow() : anchor.getNode();
624
622
  const children = target.getChildren();
625
623
  let element = createElement();
@@ -954,8 +952,8 @@ function $getSelectionStyleValueForProperty(selection, styleProperty, defaultVal
954
952
  * Please do not use it as it may change in the future.
955
953
  */
956
954
  function INTERNAL_$isBlock(node) {
957
- if (lexical.$isDecoratorNode(node) && !node.isInline()) {
958
- return true;
955
+ if (lexical.$isDecoratorNode(node)) {
956
+ return false;
959
957
  }
960
958
  if (!lexical.$isElementNode(node) || lexical.$isRootOrShadowRoot(node)) {
961
959
  return false;
@@ -11,9 +11,9 @@ import type {
11
11
  LexicalEditor,
12
12
  LexicalNode,
13
13
  NodeKey,
14
+ BaseSelection,
14
15
  NodeSelection,
15
16
  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: INTERNAL_PointSelection,
24
+ selection: BaseSelection,
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: INTERNAL_PointSelection,
48
+ selection: BaseSelection,
49
49
  createElement: () => ElementNode,
50
50
  wrappingElement?: ElementNode,
51
51
  ): void;
@@ -4,28 +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");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 [e,c]=d.split(/:([^]+)/);e&&c&&(b[e.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((e,[c,f])=>{null===f?delete e[c]:e[c]=f;return e},{...d});d=A(b);a.setStyle(d);u.set(d,b)}
9
- function C(a,b){var d=a.getNodes(),e=d.length;if(k.$isRangeSelection(a)){--e;var c=d[0],f=d[e];if(a.isCollapsed()&&k.$isRangeSelection(a))B(a,b);else{var h=a.anchor,g=a.focus,l=c.getTextContent().length,n=g.offset,q=h.offset,p=h.isBefore(g),m=p?q:n;a=p?n:q;var r=p?h.type:g.type,t=p?g.type:h.type;g=p?g.key:h.key;k.$isTextNode(c)&&m===l&&(p=c.getNextSibling(),k.$isTextNode(p)&&(m=q=0,c=p));if(1===d.length)k.$isTextNode(c)&&(m="element"===r?0:q>n?n:q,a="element"===t?l:q>n?q:n,m!==a&&(0===m&&a===l?(B(c,
10
- b),c.select(m,a)):(d=c.splitText(m,a),d=0===m?d[0]:d[1],B(d,b),d.select(0,a-m))));else for(k.$isTextNode(c)&&m<c.getTextContentSize()&&(0!==m&&(c=c.splitText(m)[1],m=0,h.set(c.getKey(),m,"text")),B(c,b)),k.$isTextNode(f)&&(m=f.getTextContent().length,f.__key!==g&&0!==a&&(a=m),a!==m&&([f]=f.splitText(a)),0!==a&&B(f,b)),a=1;a<e;a++)m=d[a],h=m.getKey(),k.$isTextNode(m)&&h!==c.getKey()&&h!==f.getKey()&&!m.isToken()&&B(m,b)}}else{c=k.$createRangeSelection();f=c.anchor;m=c.focus;for(h=0;h<e;h++)l=d[h],
11
- k.DEPRECATED_$isGridCellNode(l)&&(f.set(l.getKey(),0,"element"),m.set(l.getKey(),l.getChildrenSize(),"element"),C(k.$normalizeSelection__EXPERIMENTAL(c),b));k.$setSelection(a)}}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,e,c=null){if(0!==b.length){var f=b[0],h=new Map,g=[];f=k.$isElementNode(f)?f:f.getParentOrThrow();f.isInline()&&(f=f.getParentOrThrow());for(var l=!1;null!==f;){var n=f.getPreviousSibling();if(null!==n){f=n;l=!0;break}f=f.getParentOrThrow();if(k.$isRootOrShadowRoot(f))break}n=new Set;for(var q=0;q<d;q++){var p=b[q];k.$isElementNode(p)&&0===p.getChildrenSize()&&n.add(p.getKey())}var m=new Set;for(q=0;q<d;q++){p=b[q];var r=p.getParent();null!==r&&r.isInline()&&(r=r.getParent());if(null!==
13
- r&&k.$isLeafNode(p)&&!m.has(p.getKey())){if(p=r.getKey(),void 0===h.get(p)){let t=e();t.setFormat(r.getFormatType());t.setIndent(r.getIndent());g.push(t);h.set(p,t);r.getChildren().forEach(x=>{t.append(x);m.add(x.getKey());k.$isElementNode(x)&&x.getChildrenKeys().forEach(I=>m.add(I))});D(r)}}else if(n.has(p.getKey())){if(!k.$isElementNode(p))throw Error("Expected node in emptyElements to be an ElementNode");r=e();r.setFormat(p.getFormatType());r.setIndent(p.getIndent());g.push(r);p.remove(!0)}}if(null!==
14
- c)for(b=0;b<g.length;b++)c.append(g[b]);b=null;if(k.$isRootOrShadowRoot(f))if(l)if(null!==c)f.insertAfter(c);else for(c=g.length-1;0<=c;c--)f.insertAfter(g[c]);else if(l=f.getFirstChild(),k.$isElementNode(l)&&(f=l),null===l)if(c)f.append(c);else for(c=0;c<g.length;c++)l=g[c],f.append(l),b=l;else if(null!==c)l.insertBefore(c);else for(f=0;f<g.length;f++)c=g[f],l.insertBefore(c),b=c;else if(c)f.insertAfter(c);else for(c=g.length-1;0<=c;c--)l=g[c],f.insertAfter(l),b=l;g=k.$getPreviousSelection();k.$isRangeSelection(g)&&
15
- g.anchor.getNode().isAttached()&&g.focus.getNode().isAttached()?k.$setSelection(g.clone()):null!==b?b.selectEnd():a.dirty=!0}}function F(a,b,d,e){a.modify(b?"extend":"move",d,e)}function G(a){a=a.anchor.getNode();return"rtl"===(k.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}
16
- 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}exports.$addNodeStyle=function(a){a=a.getStyle();let b=y(a);u.set(a,b)};
7
+ 'use strict';var k=require("lexical");let v=new Map;function w(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function x(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 c of a)if(""!==c){let [e,d]=c.split(/:([^]+)/);e&&d&&(b[e.trim()]=d.trim())}return b}function z(a){let b=v.get(a);void 0===b&&(b=y(a),v.set(a,b));return b}
8
+ function A(a){let b="";for(let c in a)c&&(b+=`${c}: ${a[c]};`);return b}function B(a,b){let c=z("getStyle"in a?a.getStyle():a.style);b=Object.entries(b).reduce((d,[g,h])=>{h instanceof Function?d[g]=h(c[g]):null===h?delete d[g]:d[g]=h;return d},{...c});let e=A(b);a.setStyle(e);v.set(e,b)}function C(a){for(;null!==a&&!k.$isRootOrShadowRoot(a);){let b=a.getLatest(),c=a.getParent();0===b.getChildrenSize()&&a.remove(!0);a=c}}
9
+ function D(a,b,c,e,d=null){if(0!==b.length){var g=b[0],h=new Map,f=[];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<c;p++){var q=b[p];k.$isElementNode(q)&&0===q.getChildrenSize()&&m.add(q.getKey())}var n=new Set;for(p=0;p<c;p++){q=b[p];var r=q.getParent();null!==r&&r.isInline()&&(r=r.getParent());if(null!==
10
+ r&&k.$isLeafNode(q)&&!n.has(q.getKey())){if(q=r.getKey(),void 0===h.get(q)){let t=e();t.setFormat(r.getFormatType());t.setIndent(r.getIndent());f.push(t);h.set(q,t);r.getChildren().forEach(u=>{t.append(u);n.add(u.getKey());k.$isElementNode(u)&&u.getChildrenKeys().forEach(H=>n.add(H))});C(r)}}else if(m.has(q.getKey())){if(!k.$isElementNode(q))throw Error("Expected node in emptyElements to be an ElementNode");r=e();r.setFormat(q.getFormatType());r.setIndent(q.getIndent());f.push(r);q.remove(!0)}}if(null!==
11
+ d)for(b=0;b<f.length;b++)d.append(f[b]);b=null;if(k.$isRootOrShadowRoot(g))if(l)if(null!==d)g.insertAfter(d);else for(d=f.length-1;0<=d;d--)g.insertAfter(f[d]);else if(l=g.getFirstChild(),k.$isElementNode(l)&&(g=l),null===l)if(d)g.append(d);else for(d=0;d<f.length;d++)l=f[d],g.append(l),b=l;else if(null!==d)l.insertBefore(d);else for(g=0;g<f.length;g++)d=f[g],l.insertBefore(d),b=d;else if(d)g.insertAfter(d);else for(d=f.length-1;0<=d;d--)l=f[d],g.insertAfter(l),b=l;f=k.$getPreviousSelection();k.$isRangeSelection(f)&&
12
+ f.anchor.getNode().isAttached()&&f.focus.getNode().isAttached()?k.$setSelection(f.clone()):null!==b?b.selectEnd():a.dirty=!0}}function E(a,b,c,e){a.modify(b?"extend":"move",c,e)}function F(a){a=a.anchor.getNode();return"rtl"===(k.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}
13
+ function G(a){if(k.$isDecoratorNode(a)||!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}exports.$addNodeStyle=function(a){a=a.getStyle();let b=y(a);v.set(a,b)};
17
14
  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};
18
- exports.$getSelectionStyleValueForProperty=function(a,b,d=""){let e=null,c=a.getNodes();var f=a.anchor,h=a.focus,g=a.isBackward();let l=g?h.offset:f.offset;f=g?h.getNode():f.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 n=c[a];if((0===a||0!==l||!n.is(f))&&k.$isTextNode(n))if(h=b,g=d,n=n.getStyle(),n=z(n),h=null!==n?n[h]||g:g,null===e)e=h;else if(e!==h){e="";break}}return null===e?d:e};
19
- exports.$isAtNodeEnd=function(a){if("text"===a.type)return a.offset===a.getNode().getTextContentSize();let b=a.getNode();if(!k.$isElementNode(b))throw Error("isAtNodeEnd: node must be a TextNode or ElementNode");return a.offset===b.getChildrenSize()};exports.$isParentElementRTL=G;exports.$moveCaretSelection=F;exports.$moveCharacter=function(a,b,d){let e=G(a);F(a,b,d?!e:e,"character")};exports.$patchStyleText=C;
20
- exports.$selectAll=function(a){let b=a.anchor;a=a.focus;var d=b.getNode().getTopLevelElementOrThrow().getParentOrThrow();let e=d.getFirstDescendant();d=d.getLastDescendant();let c="element",f="element",h=0;k.$isTextNode(e)?c="text":k.$isElementNode(e)||null===e||(e=e.getParentOrThrow());k.$isTextNode(d)?(f="text",h=d.getTextContentSize()):k.$isElementNode(d)||null===d||(d=d.getParentOrThrow());e&&d&&(b.set(e.getKey(),0,c),a.set(d.getKey(),h,f))};
21
- 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 e=d[a];if(!H(e))continue;if(!k.$isElementNode(e))throw Error("Expected block node to be an ElementNode");let c=b();c.setFormat(e.getFormatType());c.setIndent(e.getIndent());e.replace(c,
22
- !0)}}};exports.$shouldOverrideDefaultCharacterSelection=function(a,b){a=k.$getAdjacentNode(a.focus,b);return k.$isDecoratorNode(a)&&!a.isIsolated()||k.$isElementNode(a)&&!a.isInline()&&!a.canBeEmpty()};
23
- exports.$sliceSelectedTextNodeContent=function(a,b){if(b.isSelected(a)&&!b.isSegmented()&&!b.isToken()&&k.$INTERNAL_isPointSelection(a)){var d=a.anchor.getNode(),e=a.focus.getNode(),c=b.is(d),f=b.is(e);if(c||f){c=a.isBackward();let [h,g]=a.getCharacterOffsets();a=d.is(e);f=b.is(c?e:d);e=b.is(c?d:e);d=0;let l=void 0;a?(d=h>g?g:h,l=h>g?h:g):f?(d=c?g:h,l=void 0):e&&(c=c?h:g,d=0,l=c);b.__text=b.__text.slice(d,l)}}return b};
24
- exports.$wrapNodes=function(a,b,d=null){var e=a.getNodes();let c=e.length;var f=a.anchor;if(0===c||1===c&&"element"===f.type&&0===f.getNode().getChildrenSize()){a="text"===f.type?f.getNode().getParentOrThrow():f.getNode();e=a.getChildren();let g=b();g.setFormat(a.getFormatType());g.setIndent(a.getIndent());e.forEach(l=>g.append(l));d&&(g=d.append(g));a.replace(g)}else{f=null;var h=[];for(let g=0;g<c;g++){let l=e[g];k.$isRootOrShadowRoot(l)?(E(a,h,h.length,b,d),h=[],f=l):null===f||null!==f&&k.$hasAncestor(l,
25
- f)?h.push(l):(E(a,h,h.length,b,d),h=[l])}E(a,h,h.length,b,d)}};
26
- exports.createDOMRange=function(a,b,d,e,c){let f=b.getKey(),h=e.getKey(),g=document.createRange(),l=a.getElementByKey(f);a=a.getElementByKey(h);k.$isTextNode(b)&&(l=v(l));k.$isTextNode(e)&&(a=v(a));if(void 0===b||void 0===e||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{g.setStart(l,d),g.setEnd(a,c)}catch(n){return null}!g.collapsed||d===c&&f===h||(g.setStart(a,c),g.setEnd(l,
27
- d));return g};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 e=b.length;b.sort((f,h)=>{let g=f.top-h.top;return 3>=Math.abs(g)?f.left-h.left:g});let c;for(let f=0;f<e;f++){let h=b[f],g=h.width+d===a.width;c&&c.top<=h.top&&c.top+c.height>h.top&&c.left+c.width>h.left||g?(b.splice(f--,1),e--):c=h}return b};
15
+ exports.$getSelectionStyleValueForProperty=function(a,b,c=""){let e=null,d=a.getNodes();var g=a.anchor,h=a.focus,f=a.isBackward();let l=f?h.offset:g.offset;g=f?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<d.length;a++){var m=d[a];if((0===a||0!==l||!m.is(g))&&k.$isTextNode(m))if(h=b,f=c,m=m.getStyle(),m=z(m),h=null!==m?m[h]||f:f,null===e)e=h;else if(e!==h){e="";break}}return null===e?c:e};
16
+ exports.$isAtNodeEnd=function(a){if("text"===a.type)return a.offset===a.getNode().getTextContentSize();let b=a.getNode();if(!k.$isElementNode(b))throw Error("isAtNodeEnd: node must be a TextNode or ElementNode");return a.offset===b.getChildrenSize()};exports.$isParentElementRTL=F;exports.$moveCaretSelection=E;exports.$moveCharacter=function(a,b,c){let e=F(a);E(a,b,c?!e:e,"character")};
17
+ exports.$patchStyleText=function(a,b){var c=a.getNodes(),e=c.length,d=a.getStartEndPoints();if(null!==d){var [g,h]=d;--e;d=c[0];var f=c[e];if(a.isCollapsed()&&k.$isRangeSelection(a))B(a,b);else{var l=d.getTextContent().length,m=h.offset,p=g.offset,q=g.isBefore(h),n=q?p:m;a=q?m:p;var r=q?g.type:h.type,t=q?h.type:g.type;q=q?h.key:g.key;if(k.$isTextNode(d)&&n===l){let u=d.getNextSibling();k.$isTextNode(u)&&(n=p=0,d=u)}if(1===c.length)k.$isTextNode(d)&&d.canHaveFormat()&&(n="element"===r?0:p>m?m:p,a=
18
+ "element"===t?l:p>m?p:m,n!==a&&(0===n&&a===l?(B(d,b),d.select(n,a)):(c=d.splitText(n,a),c=0===n?c[0]:c[1],B(c,b),c.select(0,a-n))));else for(k.$isTextNode(d)&&n<d.getTextContentSize()&&d.canHaveFormat()&&(0!==n&&(d=d.splitText(n)[1],n=0,g.set(d.getKey(),n,"text")),B(d,b)),k.$isTextNode(f)&&f.canHaveFormat()&&(n=f.getTextContent().length,f.__key!==q&&0!==a&&(a=n),a!==n&&([f]=f.splitText(a)),0===a&&"element"!==t||B(f,b)),a=1;a<e;a++)n=c[a],t=n.getKey(),k.$isTextNode(n)&&n.canHaveFormat()&&t!==d.getKey()&&
19
+ t!==f.getKey()&&!n.isToken()&&B(n,b)}}};exports.$selectAll=function(a){let b=a.anchor;a=a.focus;var c=b.getNode().getTopLevelElementOrThrow().getParentOrThrow();let e=c.getFirstDescendant();c=c.getLastDescendant();let d="element",g="element",h=0;k.$isTextNode(e)?d="text":k.$isElementNode(e)||null===e||(e=e.getParentOrThrow());k.$isTextNode(c)?(g="text",h=c.getTextContentSize()):k.$isElementNode(c)||null===c||(c=c.getParentOrThrow());e&&c&&(b.set(e.getKey(),0,d),a.set(c.getKey(),h,g))};
20
+ exports.$setBlocksType=function(a,b){if(null!==a){var c=a.getStartEndPoints();c=c?c[0]:null;if(null!==c&&"root"===c.key)b=b(),a=k.$getRoot(),(c=a.getFirstChild())?c.replace(b,!0):a.append(b);else{a=a.getNodes();if(null!==c){for(c=c.getNode();null!==c&&null!==c.getParent()&&!G(c);)c=c.getParentOrThrow();c=G(c)?c:null}else c=!1;c&&-1===a.indexOf(c)&&a.push(c);for(c=0;c<a.length;c++){let e=a[c];if(!G(e))continue;if(!k.$isElementNode(e))throw Error("Expected block node to be an ElementNode");let d=b();
21
+ d.setFormat(e.getFormatType());d.setIndent(e.getIndent());e.replace(d,!0)}}}};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){var c=a.getStartEndPoints();if(b.isSelected(a)&&!b.isSegmented()&&!b.isToken()&&null!==c){let [f,l]=c;c=a.isBackward();var e=f.getNode(),d=l.getNode(),g=b.is(e),h=b.is(d);if(g||h){let [m,p]=k.$getCharacterOffsets(a);a=e.is(d);g=b.is(c?d:e);d=b.is(c?e:d);e=0;h=void 0;a?(e=m>p?p:m,h=m>p?m:p):g?(e=c?p:m,h=void 0):d&&(c=c?m:p,e=0,h=c);b.__text=b.__text.slice(e,h)}}return b};
23
+ exports.$wrapNodes=function(a,b,c=null){var e=a.getStartEndPoints(),d=e?e[0]:null;e=a.getNodes();let g=e.length;if(null!==d&&(0===g||1===g&&"element"===d.type&&0===d.getNode().getChildrenSize())){a="text"===d.type?d.getNode().getParentOrThrow():d.getNode();e=a.getChildren();let f=b();f.setFormat(a.getFormatType());f.setIndent(a.getIndent());e.forEach(l=>f.append(l));c&&(f=c.append(f));a.replace(f)}else{d=null;var h=[];for(let f=0;f<g;f++){let l=e[f];k.$isRootOrShadowRoot(l)?(D(a,h,h.length,b,c),h=
24
+ [],d=l):null===d||null!==d&&k.$hasAncestor(l,d)?h.push(l):(D(a,h,h.length,b,c),h=[l])}D(a,h,h.length,b,c)}};
25
+ exports.createDOMRange=function(a,b,c,e,d){let g=b.getKey(),h=e.getKey(),f=document.createRange(),l=a.getElementByKey(g);a=a.getElementByKey(h);k.$isTextNode(b)&&(l=w(l));k.$isTextNode(e)&&(a=w(a));if(void 0===b||void 0===e||null===l||null===a)return null;"BR"===l.nodeName&&([l,c]=x(l));"BR"===a.nodeName&&([a,d]=x(a));b=l.firstChild;l===a&&null!=b&&"BR"===b.nodeName&&0===c&&0===d&&(d=1);try{f.setStart(l,c),f.setEnd(a,d)}catch(m){return null}!f.collapsed||c===d&&g===h||(f.setStart(a,d),f.setEnd(l,
26
+ c));return f};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 e=b.length;b.sort((g,h)=>{let f=g.top-h.top;return 3>=Math.abs(f)?g.left-h.left:f});let d;for(let g=0;g<e;g++){let h=b[g],f=h.width+c===a.width;d&&d.top<=h.top&&d.top+d.height>h.top&&d.left+d.width>h.left||f?(b.splice(g--,1),e--):d=h}return b};
28
27
  exports.getStyleObjectFromCSS=z;
29
- exports.trimTextContentFromAnchor=function(a,b,d){let e=b.getNode();if(k.$isElementNode(e)){var c=e.getDescendantByIndex(b.offset);null!==c&&(e=c)}for(;0<d&&null!==e;){k.$isElementNode(e)&&(c=e.getLastDescendant(),null!==c&&(e=c));var f=e.getPreviousSibling(),h=0;if(null===f){c=e.getParentOrThrow();for(var g=c.getPreviousSibling();null===g;){c=c.getParent();if(null===c){f=null;break}g=c.getPreviousSibling()}null!==c&&(h=c.isInline()?0:2,f=g)}g=e.getTextContent();""===g&&k.$isElementNode(e)&&!e.isInline()&&
30
- (g="\n\n");c=g.length;if(!k.$isTextNode(e)||d>=c)g=e.getParent(),e.remove(),null==g||0!==g.getChildrenSize()||k.$isRootNode(g)||g.remove(),d-=c+h,e=f;else{let l=e.getKey();h=a.getEditorState().read(()=>{const q=k.$getNodeByKey(l);return k.$isTextNode(q)&&q.isSimpleText()?q.getTextContent():null});f=c-d;let n=g.slice(0,f);null!==h&&h!==g?(d=k.$getPreviousSelection(),c=e,e.isSimpleText()?e.setTextContent(h):(c=k.$createTextNode(h),e.replace(c)),k.$isRangeSelection(d)&&d.isCollapsed()&&(d=d.anchor.offset,
31
- c.select(d,d))):e.isSimpleText()?(h=b.key===l,g=b.offset,g<d&&(g=c),d=h?g-d:0,c=h?g:f,h&&0===d?([d]=e.splitText(d,c),d.remove()):([,d]=e.splitText(d,c),d.remove())):(d=k.$createTextNode(n),e.replace(d));d=0}}}
28
+ exports.trimTextContentFromAnchor=function(a,b,c){let e=b.getNode();if(k.$isElementNode(e)){var d=e.getDescendantByIndex(b.offset);null!==d&&(e=d)}for(;0<c&&null!==e;){k.$isElementNode(e)&&(d=e.getLastDescendant(),null!==d&&(e=d));var g=e.getPreviousSibling(),h=0;if(null===g){d=e.getParentOrThrow();for(var f=d.getPreviousSibling();null===f;){d=d.getParent();if(null===d){g=null;break}f=d.getPreviousSibling()}null!==d&&(h=d.isInline()?0:2,g=f)}f=e.getTextContent();""===f&&k.$isElementNode(e)&&!e.isInline()&&
29
+ (f="\n\n");d=f.length;if(!k.$isTextNode(e)||c>=d)f=e.getParent(),e.remove(),null==f||0!==f.getChildrenSize()||k.$isRootNode(f)||f.remove(),c-=d+h,e=g;else{let l=e.getKey();h=a.getEditorState().read(()=>{const p=k.$getNodeByKey(l);return k.$isTextNode(p)&&p.isSimpleText()?p.getTextContent():null});g=d-c;let m=f.slice(0,g);null!==h&&h!==f?(c=k.$getPreviousSelection(),d=e,e.isSimpleText()?e.setTextContent(h):(d=k.$createTextNode(h),e.replace(d)),k.$isRangeSelection(c)&&c.isCollapsed()&&(c=c.anchor.offset,
30
+ d.select(c,c))):e.isSimpleText()?(h=b.key===l,f=b.offset,f<c&&(f=d),c=h?f-c:0,d=h?f:g,h&&0===c?([c]=e.splitText(c,d),c.remove()):([,c]=e.splitText(c,d),c.remove())):(c=k.$createTextNode(m),e.replace(c));c=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 { BaseSelection, INTERNAL_PointSelection, LexicalEditor, LexicalNode, Point, TextNode } from 'lexical';
8
+ import { BaseSelection, 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.
@@ -45,6 +45,6 @@ export declare function $addNodeStyle(node: TextNode): void;
45
45
  * Will update partially selected TextNodes by splitting the TextNode and applying
46
46
  * the styles to the appropriate one.
47
47
  * @param selection - The selected node(s) to update.
48
- * @param patch - The patch to apply, which can include multiple styles. { CSSProperty: value }
48
+ * @param patch - The patch to apply, which can include multiple styles. { CSSProperty: value }. Can also accept a function that returns the new property value.
49
49
  */
50
- export declare function $patchStyleText(selection: INTERNAL_PointSelection, patch: Record<string, string | null>): void;
50
+ export declare function $patchStyleText(selection: BaseSelection, patch: Record<string, string | null | ((currentStyleValue: string | null) => string)>): void;
package/package.json CHANGED
@@ -9,10 +9,10 @@
9
9
  "selection"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.12.6",
12
+ "version": "0.13.1",
13
13
  "main": "LexicalSelection.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.12.6"
15
+ "lexical": "0.13.1"
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 { DecoratorNode, ElementNode, INTERNAL_PointSelection, LexicalNode, RangeSelection } from 'lexical';
8
+ import type { BaseSelection, ElementNode, 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: INTERNAL_PointSelection, createElement: () => ElementNode): void;
14
+ export declare function $setBlocksType(selection: BaseSelection | null, 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: INTERNAL_PointSelection, creat
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: INTERNAL_PointSelection, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
22
+ export declare function $wrapNodes(selection: BaseSelection, 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: INTERNAL_PointSelection, createEle
29
29
  * @param wrappingElement - An element to wrap all the nodes into.
30
30
  * @returns
31
31
  */
32
- export declare function $wrapNodesImpl(selection: INTERNAL_PointSelection, nodes: LexicalNode[], nodesLength: number, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
32
+ export declare function $wrapNodesImpl(selection: BaseSelection, 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.
@@ -76,5 +76,5 @@ export declare function $getSelectionStyleValueForProperty(selection: RangeSelec
76
76
  * This function is for internal use of the library.
77
77
  * Please do not use it as it may change in the future.
78
78
  */
79
- export declare function INTERNAL_$isBlock(node: LexicalNode): node is ElementNode | DecoratorNode<unknown>;
79
+ export declare function INTERNAL_$isBlock(node: LexicalNode): node is ElementNode;
80
80
  export declare function $getAncestor<NodeType extends LexicalNode = LexicalNode>(node: LexicalNode, predicate: (ancestor: LexicalNode) => ancestor is NodeType): NodeType | null;