@lexical/selection 0.2.2 → 0.2.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.
@@ -30,10 +30,12 @@ function $cloneWithProperties(node) {
30
30
  clone.__indent = latest.__indent;
31
31
  clone.__dir = latest.__dir;
32
32
  } else if (lexical.$isTextNode(latest) && lexical.$isTextNode(clone)) {
33
+ const marks = latest.__marks;
33
34
  clone.__format = latest.__format;
34
35
  clone.__style = latest.__style;
35
36
  clone.__mode = latest.__mode;
36
37
  clone.__detail = latest.__detail;
38
+ clone.__marks = marks === null ? null : Array.from(marks);
37
39
  } // $FlowFixMe
38
40
 
39
41
 
@@ -60,7 +62,7 @@ function $getParentAvoidingExcludedElements(node) {
60
62
  return parent;
61
63
  }
62
64
 
63
- function $copyLeafNodeBranchToRoot(leaf, startingOffset, isLeftSide, range, nodeMap) {
65
+ function $copyLeafNodeBranchToRoot(leaf, startingOffset, endingOffset, isLeftSide, range, nodeMap) {
64
66
  let node = leaf;
65
67
  let offset = startingOffset;
66
68
 
@@ -82,9 +84,9 @@ function $copyLeafNodeBranchToRoot(leaf, startingOffset, isLeftSide, range, node
82
84
  }
83
85
 
84
86
  if (lexical.$isTextNode(clone) && !clone.isSegmented() && !clone.isToken()) {
85
- clone.__text = clone.__text.slice(isLeftSide ? offset : 0, isLeftSide ? undefined : offset);
87
+ clone.__text = clone.__text.slice(isLeftSide ? offset : 0, isLeftSide ? endingOffset : offset);
86
88
  } else if (lexical.$isElementNode(clone)) {
87
- clone.__children = clone.__children.slice(isLeftSide ? offset : 0, isLeftSide ? undefined : offset + 1);
89
+ clone.__children = clone.__children.slice(isLeftSide ? offset : 0, isLeftSide ? undefined : (offset || 0) + 1);
88
90
  }
89
91
 
90
92
  if (lexical.$isRootNode(parent)) {
@@ -140,25 +142,7 @@ function $cloneContentsImpl(selection) {
140
142
  if (lexical.$isRangeSelection(selection)) {
141
143
  const anchor = selection.anchor;
142
144
  const focus = selection.focus;
143
- const anchorOffset = anchor.getCharacterOffset();
144
- const focusOffset = focus.getCharacterOffset();
145
- const anchorNode = anchor.getNode();
146
- const focusNode = focus.getNode();
147
- const anchorNodeParent = anchorNode.getParentOrThrow(); // Handle a single text node extraction
148
-
149
- if (anchorNode === focusNode && lexical.$isTextNode(anchorNode) && (anchorNodeParent.canBeEmpty() || anchorNodeParent.getChildrenSize() > 1)) {
150
- const clonedFirstNode = $cloneWithProperties(anchorNode);
151
- const isBefore = focusOffset > anchorOffset;
152
- const startOffset = isBefore ? anchorOffset : focusOffset;
153
- const endOffset = isBefore ? focusOffset : anchorOffset;
154
- clonedFirstNode.__text = clonedFirstNode.__text.slice(startOffset, endOffset);
155
- const key = clonedFirstNode.getKey();
156
- return {
157
- nodeMap: [[key, clonedFirstNode]],
158
- range: [key]
159
- };
160
- }
161
-
145
+ const [anchorOffset, focusOffset] = selection.getCharacterOffsets();
162
146
  const nodes = selection.getNodes();
163
147
 
164
148
  if (nodes.length === 0) {
@@ -199,9 +183,10 @@ function $cloneContentsImpl(selection) {
199
183
  const lastNode = nodes[nodesLength - 1];
200
184
  const isBefore = anchor.isBefore(focus);
201
185
  const nodeMap = new Map();
202
- const range = []; // Do first node to root
186
+ const range = [];
187
+ const isOnlyText = lexical.$isTextNode(firstNode) && nodesLength === 1; // Do first node to root
203
188
 
204
- $copyLeafNodeBranchToRoot(firstNode, isBefore ? anchorOffset : focusOffset, true, range, nodeMap); // Copy all nodes between
189
+ $copyLeafNodeBranchToRoot(firstNode, isBefore ? anchorOffset : focusOffset, isOnlyText ? isBefore ? focusOffset : anchorOffset : undefined, true, range, nodeMap); // Copy all nodes between
205
190
 
206
191
  for (let i = 0; i < nodesLength; i++) {
207
192
  const node = nodes[i];
@@ -214,12 +199,14 @@ function $cloneContentsImpl(selection) {
214
199
  range.push(node.getKey());
215
200
  }
216
201
 
217
- nodeMap.set(key, clone);
202
+ if (key !== 'root') {
203
+ nodeMap.set(key, clone);
204
+ }
218
205
  }
219
206
  } // Do last node to root
220
207
 
221
208
 
222
- $copyLeafNodeBranchToRoot(lastNode, isBefore ? focusOffset : anchorOffset, false, range, nodeMap);
209
+ $copyLeafNodeBranchToRoot(lastNode, isOnlyText ? undefined : isBefore ? focusOffset : anchorOffset, undefined, false, range, nodeMap);
223
210
  return {
224
211
  nodeMap: Array.from(nodeMap.entries()),
225
212
  range
@@ -459,7 +446,7 @@ function $removeParentEmptyElements(startingNode) {
459
446
  const parentNode = node.getParent();
460
447
 
461
448
  if (latest.__children.length === 0) {
462
- node.remove();
449
+ node.remove(true);
463
450
  }
464
451
 
465
452
  node = parentNode;
@@ -469,9 +456,9 @@ function $removeParentEmptyElements(startingNode) {
469
456
  function $wrapLeafNodesInElements(selection, createElement, wrappingElement) {
470
457
  const nodes = selection.getNodes();
471
458
  const nodesLength = nodes.length;
459
+ const anchor = selection.anchor;
472
460
 
473
- if (nodesLength === 0) {
474
- const anchor = selection.anchor;
461
+ if (nodesLength === 0 || nodesLength === 1 && anchor.type === 'element' && anchor.getNode().getChildrenSize() === 0) {
475
462
  const target = anchor.type === 'text' ? anchor.getNode().getParentOrThrow() : anchor.getNode();
476
463
  const children = target.getChildren();
477
464
  let element = createElement();
@@ -4,19 +4,18 @@
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
- var l=require("lexical");const t=new Map;function u(a){a=a.getLatest();const 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,g,h){for(var e=c;null!==a;){for(c=a.getParent();null!==c&&c.excludeFromCopy();)c=c.getParent();if(null===c)break;if(!l.$isElementNode(a)||!a.excludeFromCopy()){const d=a.getKey();let f=h.get(d);const k=void 0===f;k&&(f=u(a),h.set(d,f));!l.$isTextNode(f)||f.isSegmented()||f.isToken()?l.$isElementNode(f)&&(f.__children=f.__children.slice(b?e:0,b?void 0:e+1)):f.__text=f.__text.slice(b?e:0,b?void 0:e);if(l.$isRootNode(c)){k&&g.push(d);break}}e=h.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,g=c.getCharacterOffset();const n=b.getCharacterOffset();var h=c.getNode(),e=b.getNode(),d=h.getParentOrThrow();if(h===e&&l.$isTextNode(h)&&(d.canBeEmpty()||1<d.getChildrenSize()))return a=u(h),h=n>g,a.__text=a.__text.slice(h?g:n,h?n:g),g=a.getKey(),{nodeMap:[[g,a]],range:[g]};a=a.getNodes();if(0===a.length)return{nodeMap:[],range:[]};h=a.length;e=a[0];d=e.getParent();if(null!==d&&(!d.canBeEmpty()||l.$isRootNode(d))){var f=d.__children;
11
- if(f.length===h){var k=!0;for(var m=0;m<f.length;m++)if(f[m]!==a[m].__key){k=!1;break}k&&(h++,a.push(d))}}d=a[h-1];c=c.isBefore(b);b=new Map;f=[];w(e,c?g:n,!0,f,b);for(e=0;e<h;e++)if(k=a[e],m=k.getKey(),!(b.has(m)||l.$isElementNode(k)&&k.excludeFromCopy())){const r=u(k);l.$isRootNode(k.getParent())&&f.push(k.getKey());b.set(m,r)}w(d,c?n:g,!1,f,b);return{nodeMap:Array.from(b.entries()),range:f}}if(l.$isGridSelection(a))return{nodeMap:a.getNodes().map(n=>{const r=n.getKey();n=u(n);return[r,n]}),range:[a.gridKey]};
12
- throw Error("Minified Lexical error #68; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");}function y(a){return t.get(a)||null}function z(a,c){var b=y(a.getStyle());c=b?{...b,...c}:c;b="";for(g in c)g&&(b+=`${g}: ${c[g]};`);var g=b;a.setStyle(g);t.set(g,c)}function A(a,c,b,g){a.modify(c?"extend":"move",b,g)}function B(a){a=a.anchor.getNode();return"rtl"===(l.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}
13
- function C(a){for(;null!==a&&!l.$isRootNode(a);){const c=a.getLatest(),b=a.getParent();0===c.__children.length&&a.remove();a=b}}exports.$cloneContents=function(a){return x(a)};
14
- exports.$getSelectionStyleValueForProperty=function(a,c,b=""){let g=null;const h=a.getNodes();var e=a.anchor,d=a.focus,f=a.isBackward();a=f?d.offset:e.offset;e=f?d.getNode():e.getNode();for(d=0;d<h.length;d++){var k=h[d];if((0===d||0!==a||!k.is(e))&&l.$isTextNode(k)){f=c;var m=b;k=k.getStyle();k=y(k);f=null!==k?k[f]||m:m;if(null===g)g=f;else if(g!==f){g="";break}}}return null===g?b:g};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){const g=B(a);A(a,c,b?!g:g,"character")};
16
- exports.$patchStyleText=function(a,c){var b=a.getNodes();const g=b.length-1;let h=b[0],e=b[g];if(!a.isCollapsed()){var d=a.anchor,f=a.focus;a=h.getTextContent().length;var k=f.offset,m=d.offset;d=(f=d.isBefore(f))?m:k;f=f?k:m;if(d===h.getTextContentSize()){const n=h.getNextSibling();l.$isTextNode(n)&&(d=m=0,h=n)}if(h.is(e))l.$isTextNode(h)&&(d=m>k?k:m,f=m>k?m:k,d!==f&&(0===d&&f===a?(z(h,c),h.select(d,f)):(b=h.splitText(d,f),b=0===d?b[0]:b[1],z(b,c),b.select(0,f-d))));else for(l.$isTextNode(h)&&(0!==
17
- d&&([,h]=h.splitText(d)),z(h,c)),l.$isTextNode(e)&&(a=e.getTextContent().length,f!==a&&([e]=e.splitText(f)),0!==f&&z(e,c)),a=1;a<g;a++)k=b[a],m=k.getKey(),l.$isTextNode(k)&&m!==h.getKey()&&m!==e.getKey()&&!k.isToken()&&z(k,c)}};
18
- exports.$selectAll=function(a){const c=a.anchor;a=a.focus;var b=c.getNode().getTopLevelElementOrThrow().getParentOrThrow();let g=b.getFirstDescendant();b=b.getLastDescendant();let h="element",e="element",d=0;l.$isTextNode(g)?h="text":l.$isElementNode(g)||null===g||(g=g.getParentOrThrow());l.$isTextNode(b)?(e="text",d=b.getTextContentSize()):l.$isElementNode(b)||null===b||(b=b.getParentOrThrow(),d=b.getChildrenSize());g&&b&&(c.set(g.getKey(),0,h),a.set(b.getKey(),d,e))};
19
- exports.$shouldOverrideDefaultCharacterSelection=function(a,c){a=l.$getDecoratorNode(a.focus,c);return l.$isDecoratorNode(a)&&!a.isIsolated()};
20
- exports.$wrapLeafNodesInElements=function(a,c,b){const g=a.getNodes(),h=g.length;if(0===h){a=a.anchor;a="text"===a.type?a.getNode().getParentOrThrow():a.getNode();var e=a.getChildren();let p=c();e.forEach(v=>p.append(v));b&&(p=b.append(p));a.replace(p)}else{var d=g[0],f=new Map;e=[];d=l.$isElementNode(d)?d:d.getParentOrThrow();for(d.isInline()&&(d=d.getParentOrThrow());null!==d;){var k=d.getPreviousSibling();if(null!==k){d=k;break}d=d.getParentOrThrow();if(l.$isRootNode(d))break}k=new Set;for(var m=
21
- 0;m<h;m++){var n=g[m];l.$isElementNode(n)&&0===n.getChildrenSize()&&k.add(n.getKey())}var r=new Set;for(m=0;m<h;m++){var q=g[m];n=q.getParent();null!==n&&n.isInline()&&(n=n.getParent());if(null!==n&&l.$isLeafNode(q)&&!r.has(q.getKey())){if(q=n.getKey(),void 0===f.get(q)){const p=c();e.push(p);f.set(q,p);n.getChildren().forEach(v=>{p.append(v);r.add(v.getKey())});C(n)}}else k.has(q.getKey())&&(e.push(c()),q.remove())}if(b)for(c=0;c<e.length;c++)b.append(e[c]);if(l.$isRootNode(d))if(c=d.getFirstChild(),
22
- l.$isElementNode(c)&&(d=c),null===c)if(b)d.append(b);else for(b=0;b<e.length;b++)d.append(e[b]);else if(b)c.insertBefore(b);else for(b=0;b<e.length;b++)c.insertBefore(e[b]);else if(b)d.insertAfter(b);else for(b=e.length-1;0<=b;b--)d.insertAfter(e[b]);b=l.$getPreviousSelection();l.$isRangeSelection(b)&&b.anchor.getNode().isAttached()&&b.focus.getNode().isAttached()?(a=b.clone(),a.dirty=!0,l.$setSelection(a)):a.dirty=!0}};exports.getStyleObjectFromCSS=y;
7
+ var k=require("lexical");const t=new Map;function v(a){a=a.getLatest();const c=a.constructor.clone(a);c.__parent=a.__parent;if(k.$isElementNode(a)&&k.$isElementNode(c))c.__children=Array.from(a.__children),c.__format=a.__format,c.__indent=a.__indent,c.__dir=a.__dir;else if(k.$isTextNode(a)&&k.$isTextNode(c)){const b=a.__marks;c.__format=a.__format;c.__style=a.__style;c.__mode=a.__mode;c.__detail=a.__detail;c.__marks=null===b?null:Array.from(b)}return c}
8
+ function w(a,c,b,e,l,f){for(var d=c;null!==a;){for(c=a.getParent();null!==c&&c.excludeFromCopy();)c=c.getParent();if(null===c)break;if(!k.$isElementNode(a)||!a.excludeFromCopy()){const h=a.getKey();let g=f.get(h);const m=void 0===g;m&&(g=v(a),f.set(h,g));!k.$isTextNode(g)||g.isSegmented()||g.isToken()?k.$isElementNode(g)&&(g.__children=g.__children.slice(e?d:0,e?void 0:(d||0)+1)):g.__text=g.__text.slice(e?d:0,e?b:d);if(k.$isRootNode(c)){m&&l.push(h);break}}d=f.get(c.getKey());d=k.$isElementNode(d)?
9
+ d.__children.indexOf(a.getKey()):a.getIndexWithinParent();a=c}}
10
+ function x(a){if(k.$isRangeSelection(a)){var c=a.anchor,b=a.focus;const [g,m]=a.getCharacterOffsets();a=a.getNodes();if(0===a.length)return{nodeMap:[],range:[]};let n=a.length;var e=a[0],l=e.getParent();if(null!==l&&(!l.canBeEmpty()||k.$isRootNode(l))){var f=l.__children;if(f.length===n){var d=!0;for(var h=0;h<f.length;h++)if(f[h]!==a[h].__key){d=!1;break}d&&(n++,a.push(l))}}l=a[n-1];c=c.isBefore(b);b=new Map;f=[];d=k.$isTextNode(e)&&1===n;w(e,c?g:m,d?c?m:g:void 0,!0,f,b);for(e=0;e<n;e++){h=a[e];
11
+ const r=h.getKey();if(!(b.has(r)||k.$isElementNode(h)&&h.excludeFromCopy())){const p=v(h);k.$isRootNode(h.getParent())&&f.push(h.getKey());"root"!==r&&b.set(r,p)}}w(l,d?void 0:c?m:g,void 0,!1,f,b);return{nodeMap:Array.from(b.entries()),range:f}}if(k.$isGridSelection(a))return{nodeMap:a.getNodes().map(g=>{const m=g.getKey();g=v(g);return[m,g]}),range:[a.gridKey]};throw Error("Minified Lexical error #68; see codes.json 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(e in c)e&&(b+=`${e}: ${c[e]};`);var e=b;a.setStyle(e);t.set(e,c)}function A(a,c,b,e){a.modify(c?"extend":"move",b,e)}function B(a){a=a.anchor.getNode();return"rtl"===(k.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}function C(a){for(;null!==a&&!k.$isRootNode(a);){const c=a.getLatest(),b=a.getParent();0===c.__children.length&&a.remove(!0);a=b}}exports.$cloneContents=function(a){return x(a)};
13
+ exports.$getSelectionStyleValueForProperty=function(a,c,b=""){let e=null;const l=a.getNodes();var f=a.anchor,d=a.focus,h=a.isBackward();a=h?d.offset:f.offset;f=h?d.getNode():f.getNode();for(d=0;d<l.length;d++){var g=l[d];if((0===d||0!==a||!g.is(f))&&k.$isTextNode(g)){h=c;var m=b;g=g.getStyle();g=y(g);h=null!==g?g[h]||m:m;if(null===e)e=h;else if(e!==h){e="";break}}}return null===e?b:e};exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};
14
+ exports.$isParentElementRTL=B;exports.$moveCaretSelection=A;exports.$moveCharacter=function(a,c,b){const e=B(a);A(a,c,b?!e:e,"character")};
15
+ exports.$patchStyleText=function(a,c){var b=a.getNodes();const e=b.length-1;let l=b[0],f=b[e];if(!a.isCollapsed()){var d=a.anchor,h=a.focus;a=l.getTextContent().length;var g=h.offset,m=d.offset;d=(h=d.isBefore(h))?m:g;h=h?g:m;if(d===l.getTextContentSize()){const n=l.getNextSibling();k.$isTextNode(n)&&(d=m=0,l=n)}if(l.is(f))k.$isTextNode(l)&&(d=m>g?g:m,h=m>g?m:g,d!==h&&(0===d&&h===a?(z(l,c),l.select(d,h)):(b=l.splitText(d,h),b=0===d?b[0]:b[1],z(b,c),b.select(0,h-d))));else for(k.$isTextNode(l)&&(0!==
16
+ d&&([,l]=l.splitText(d)),z(l,c)),k.$isTextNode(f)&&(a=f.getTextContent().length,h!==a&&([f]=f.splitText(h)),0!==h&&z(f,c)),a=1;a<e;a++)g=b[a],m=g.getKey(),k.$isTextNode(g)&&m!==l.getKey()&&m!==f.getKey()&&!g.isToken()&&z(g,c)}};
17
+ exports.$selectAll=function(a){const c=a.anchor;a=a.focus;var b=c.getNode().getTopLevelElementOrThrow().getParentOrThrow();let e=b.getFirstDescendant();b=b.getLastDescendant();let l="element",f="element",d=0;k.$isTextNode(e)?l="text":k.$isElementNode(e)||null===e||(e=e.getParentOrThrow());k.$isTextNode(b)?(f="text",d=b.getTextContentSize()):k.$isElementNode(b)||null===b||(b=b.getParentOrThrow(),d=b.getChildrenSize());e&&b&&(c.set(e.getKey(),0,l),a.set(b.getKey(),d,f))};
18
+ exports.$shouldOverrideDefaultCharacterSelection=function(a,c){a=k.$getDecoratorNode(a.focus,c);return k.$isDecoratorNode(a)&&!a.isIsolated()};
19
+ exports.$wrapLeafNodesInElements=function(a,c,b){const e=a.getNodes(),l=e.length;var f=a.anchor;if(0===l||1===l&&"element"===f.type&&0===f.getNode().getChildrenSize()){a="text"===f.type?f.getNode().getParentOrThrow():f.getNode();f=a.getChildren();let q=c();f.forEach(u=>q.append(u));b&&(q=b.append(q));a.replace(q)}else{var d=e[0],h=new Map;f=[];d=k.$isElementNode(d)?d:d.getParentOrThrow();for(d.isInline()&&(d=d.getParentOrThrow());null!==d;){var g=d.getPreviousSibling();if(null!==g){d=g;break}d=d.getParentOrThrow();
20
+ if(k.$isRootNode(d))break}g=new Set;for(var m=0;m<l;m++){var n=e[m];k.$isElementNode(n)&&0===n.getChildrenSize()&&g.add(n.getKey())}var r=new Set;for(m=0;m<l;m++){var p=e[m];n=p.getParent();null!==n&&n.isInline()&&(n=n.getParent());if(null!==n&&k.$isLeafNode(p)&&!r.has(p.getKey())){if(p=n.getKey(),void 0===h.get(p)){const q=c();f.push(q);h.set(p,q);n.getChildren().forEach(u=>{q.append(u);r.add(u.getKey())});C(n)}}else g.has(p.getKey())&&(f.push(c()),p.remove())}if(b)for(c=0;c<f.length;c++)b.append(f[c]);
21
+ if(k.$isRootNode(d))if(c=d.getFirstChild(),k.$isElementNode(c)&&(d=c),null===c)if(b)d.append(b);else for(b=0;b<f.length;b++)d.append(f[b]);else if(b)c.insertBefore(b);else for(b=0;b<f.length;b++)c.insertBefore(f[b]);else if(b)d.insertAfter(b);else for(b=f.length-1;0<=b;b--)d.insertAfter(f[b]);b=k.$getPreviousSelection();k.$isRangeSelection(b)&&b.anchor.getNode().isAttached()&&b.focus.getNode().isAttached()?(a=b.clone(),a.dirty=!0,k.$setSelection(a)):a.dirty=!0}};exports.getStyleObjectFromCSS=y;
package/package.json CHANGED
@@ -9,10 +9,10 @@
9
9
  "selection"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.2.2",
12
+ "version": "0.2.5",
13
13
  "main": "LexicalSelection.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.2.2"
15
+ "lexical": "0.2.5"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",