@lexical/utils 0.6.3 → 0.6.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.
@@ -297,16 +297,31 @@ function unstable_internalCreateNodeFromParse(parsedNode, parsedNodeMap, editor,
297
297
 
298
298
  if (lexical.$isElementNode(node)) {
299
299
  const children = parsedNode.__children;
300
+ let prevNode = null;
300
301
 
301
302
  for (let i = 0; i < children.length; i++) {
302
303
  const childKey = children[i];
304
+
305
+ if (i === 0) {
306
+ node.__first = childKey;
307
+ } else if (i === children.length - 1) {
308
+ node.__last = childKey;
309
+ }
310
+
303
311
  const parsedChild = parsedNodeMap.get(childKey);
304
312
 
305
313
  if (parsedChild !== undefined) {
306
314
  const child = unstable_internalCreateNodeFromParse(parsedChild, parsedNodeMap, editor, key, activeEditorState);
307
315
  const newChildKey = child.__key;
308
316
 
317
+ if (prevNode !== null) {
318
+ child.__prev = prevNode.__key;
319
+ prevNode.__next = newChildKey;
320
+ }
321
+
309
322
  node.__children.push(newChildKey);
323
+
324
+ prevNode = child;
310
325
  }
311
326
  }
312
327
 
@@ -387,25 +402,38 @@ function $insertNodeToNearestRoot(node) {
387
402
  } = selection;
388
403
  const focusNode = focus.getNode();
389
404
  const focusOffset = focus.offset;
390
- let splitNode;
391
- let splitOffset;
392
405
 
393
- if (lexical.$isTextNode(focusNode)) {
394
- splitNode = focusNode.getParentOrThrow();
395
- splitOffset = focusNode.getIndexWithinParent();
406
+ if (lexical.$isRootOrShadowRoot(focusNode)) {
407
+ const focusChild = focusNode.getChildAtIndex(focusOffset);
396
408
 
397
- if (focusOffset > 0) {
398
- splitOffset += 1;
399
- focusNode.splitText(focusOffset);
409
+ if (focusChild == null) {
410
+ focusNode.append(node);
411
+ } else {
412
+ focusChild.insertBefore(node);
400
413
  }
414
+
415
+ node.selectNext();
401
416
  } else {
402
- splitNode = focusNode;
403
- splitOffset = focusOffset;
404
- }
417
+ let splitNode;
418
+ let splitOffset;
405
419
 
406
- const [, rightTree] = $splitNode(splitNode, splitOffset);
407
- rightTree.insertBefore(node);
408
- rightTree.selectStart();
420
+ if (lexical.$isTextNode(focusNode)) {
421
+ splitNode = focusNode.getParentOrThrow();
422
+ splitOffset = focusNode.getIndexWithinParent();
423
+
424
+ if (focusOffset > 0) {
425
+ splitOffset += 1;
426
+ focusNode.splitText(focusOffset);
427
+ }
428
+ } else {
429
+ splitNode = focusNode;
430
+ splitOffset = focusOffset;
431
+ }
432
+
433
+ const [, rightTree] = $splitNode(splitNode, splitOffset);
434
+ rightTree.insertBefore(node);
435
+ rightTree.selectStart();
436
+ }
409
437
  } else {
410
438
  if (lexical.$isNodeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection)) {
411
439
  const nodes = selection.getNodes();
@@ -435,6 +463,10 @@ function $splitNode(node, offset) {
435
463
  startNode = node;
436
464
  }
437
465
 
466
+ if (!!lexical.$isRootOrShadowRoot(node)) {
467
+ throw Error(`Can not call $splitNode() on root element`);
468
+ }
469
+
438
470
  const recurse = currentNode => {
439
471
  const parent = currentNode.getParentOrThrow();
440
472
  const isParentRoot = lexical.$isRootOrShadowRoot(parent); // The node we start split from (leaf) is moved, but its recursive
@@ -79,4 +79,7 @@ declare export function $wrapNodeInElement(
79
79
  createElementNode: () => ElementNode,
80
80
  ): ElementNode;
81
81
 
82
- declare export function $splitNode<T: LexicalNode>(node: T): T;
82
+ declare export function $splitNode(
83
+ node: ElementNode,
84
+ offset: number,
85
+ ): [ElementNode | null, ElementNode];
@@ -4,15 +4,16 @@
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");function p(a){throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?code=${a} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function q(a,b){for(let d of b)if(a.type.startsWith(d))return!0;return!1}function r(a,b){for(;a!==l.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null}
8
- function t(a,b,d,g,f){var c=d._nodes.get(a.__type);void 0===c&&p(5);for(var e in a){var k=a[e];if(null!=k&&"object"===typeof k&&(k=k.editorState,null!=k)){var h=l.createEditor({namespace:k.namespace});h._nodes=d._nodes;h._parentEditor=d._parentEditor;h._pendingEditorState=u(h,k);a[e]=h}}c=c.klass;e=a.__key;a.__key=void 0;c=c.clone(a);a.__key=e;e=c.__key;f._nodeMap.set(e,c);c.__parent=g;if(l.$isElementNode(c)){g=a.__children;for(k=0;k<g.length;k++)h=b.get(g[k]),void 0!==h&&(h=t(h,b,d,e,f).__key,c.__children.push(h));
9
- c.__indent=a.__indent;c.__format=a.__format;c.__dir=a.__dir}else l.$isTextNode(c)&&(c.__format=a.__format,c.__style=a.__style,c.__mode=a.__mode,c.__detail=a.__detail);return c}
10
- function v(a,b){let d=b._editorState.constructor,g=new Map,f=new d(g),c=new Map(a._nodeMap),e=c.get("root");a=b._updating;try{b._updating=!1,b.update(()=>{let k=b._dirtyElements,h=b._dirtyLeaves,m=b._dirtyType;b._dirtyElements=new Map;b._dirtyLeaves=new Set;b._dirtyType=0;try{t(e,c,b,null,f)}finally{b._dirtyElements=k,b._dirtyLeaves=h,b._dirtyType=m}})}finally{b._updating=a}f._readOnly=!0;return f}function u(a,b){b="string"===typeof b?JSON.parse(b):b;return v(b,a)}
11
- function w(a,b){let d=a.getChildAtIndex(b);null==d&&(d=a);let g=e=>{const k=e.getParentOrThrow(),h=l.$isRootOrShadowRoot(k),m=e!==d||h?l.$copyNode(e):e;if(h)return e.insertAfter(m),[e,m,m];const [n,x,y]=g(k);e=e.getNextSiblings();y.append(m,...e);return[n,x,m]},[f,c]=g(d);return[f,c]}
12
- exports.$dfs=function(a,b){let d=[];a=(a||l.$getRoot()).getLatest();b=b||(l.$isElementNode(a)?a.getLastDescendant():a);for(var g=a,f=0;null!==(g=g.getParent());)f++;for(g=f;null!==a&&!a.is(b);)if(d.push({depth:g,node:a}),l.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),g++;else for(f=null;null===f&&null!==a;)f=a.getNextSibling(),null===f?(a=a.getParent(),g--):a=f;null!==a&&a.is(b)&&d.push({depth:g,node:a});return d};exports.$findMatchingParent=r;
13
- exports.$getNearestBlockElementAncestorOrThrow=function(a){a=r(a,b=>l.$isElementNode(b)&&!b.isInline());l.$isElementNode(a)||p(4);return a};exports.$getNearestNodeOfType=function(a,b){for(;null!=a;){if(a instanceof b)return a;a=a.getParent()}return null};
14
- exports.$insertNodeToNearestRoot=function(a){var b=l.$getSelection();if(l.$isRangeSelection(b)){var {focus:d}=b;b=d.getNode();d=d.offset;let g,f;l.$isTextNode(b)?(g=b.getParentOrThrow(),f=b.getIndexWithinParent(),0<d&&(f+=1,b.splitText(d))):(g=b,f=d);[,b]=w(g,f);b.insertBefore(a);b.selectStart()}else l.$isNodeSelection(b)||l.DEPRECATED_$isGridSelection(b)?(b=b.getNodes(),b[b.length-1].getTopLevelElementOrThrow().insertAfter(a)):l.$getRoot().append(a),b=l.$createParagraphNode(),a.insertAfter(b),b.select();
15
- return a.getLatest()};exports.$restoreEditorState=function(a,b){let d=new Map(b._nodeMap),g=a._pendingEditorState;g&&(g._nodeMap=d);a._dirtyType=2;a=b._selection;l.$setSelection(null===a?null:a.clone())};exports.$splitNode=w;exports.$wrapNodeInElement=function(a,b){b=b();a.replace(b);b.append(a);return b};exports.addClassNamesToElement=function(a,...b){b.forEach(d=>{"string"===typeof d&&(d=d.split(" ").filter(g=>""!==g),a.classList.add(...d))})};exports.isMimeType=q;
16
- exports.mediaFileReader=function(a,b){let d=a[Symbol.iterator]();return new Promise((g,f)=>{let c=[],e=()=>{const {done:k,value:h}=d.next();if(k)return g(c);const m=new FileReader;m.addEventListener("error",f);m.addEventListener("load",()=>{const n=m.result;"string"===typeof n&&c.push({file:h,result:n});e()});q(h,b)?m.readAsDataURL(h):e()};e()})};exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}};
17
- exports.registerNestedElementResolver=function(a,b,d,g){return a.registerNodeTransform(b,f=>{a:{var c=f.getChildren();for(var e=0;e<c.length;e++)if(c[e]instanceof b){c=null;break a}for(c=f;null!==c;)if(e=c,c=c.getParent(),c instanceof b){c={child:e,parent:c};break a}c=null}if(null!==c){const {child:k,parent:h}=c;if(k.is(f)){g(h,f);f=k.getNextSiblings();c=f.length;h.insertAfter(k);if(0!==c){e=d(h);k.insertAfter(e);for(let m=0;m<c;m++)e.append(f[m])}h.canBeEmpty()||0!==h.getChildrenSize()||h.remove()}}})};
7
+ 'use strict';var m=require("lexical");function p(a){throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?code=${a} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function q(a,b){for(let d of b)if(a.type.startsWith(d))return!0;return!1}function r(a,b){for(;a!==m.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null}
8
+ function t(a,b,d,f,g){var c=d._nodes.get(a.__type);void 0===c&&p(5);for(var e in a){var l=a[e];if(null!=l&&"object"===typeof l&&(l=l.editorState,null!=l)){var h=m.createEditor({namespace:l.namespace});h._nodes=d._nodes;h._parentEditor=d._parentEditor;h._pendingEditorState=u(h,l);a[e]=h}}c=c.klass;e=a.__key;a.__key=void 0;c=c.clone(a);a.__key=e;e=c.__key;g._nodeMap.set(e,c);c.__parent=f;if(m.$isElementNode(c)){f=a.__children;l=null;for(h=0;h<f.length;h++){var k=f[h];0===h?c.__first=k:h===f.length-
9
+ 1&&(c.__last=k);k=b.get(k);if(void 0!==k){k=t(k,b,d,e,g);let n=k.__key;null!==l&&(k.__prev=l.__key,l.__next=n);c.__children.push(n);l=k}}c.__indent=a.__indent;c.__format=a.__format;c.__dir=a.__dir}else m.$isTextNode(c)&&(c.__format=a.__format,c.__style=a.__style,c.__mode=a.__mode,c.__detail=a.__detail);return c}
10
+ function v(a,b){let d=b._editorState.constructor,f=new Map,g=new d(f),c=new Map(a._nodeMap),e=c.get("root");a=b._updating;try{b._updating=!1,b.update(()=>{let l=b._dirtyElements,h=b._dirtyLeaves,k=b._dirtyType;b._dirtyElements=new Map;b._dirtyLeaves=new Set;b._dirtyType=0;try{t(e,c,b,null,g)}finally{b._dirtyElements=l,b._dirtyLeaves=h,b._dirtyType=k}})}finally{b._updating=a}g._readOnly=!0;return g}function u(a,b){b="string"===typeof b?JSON.parse(b):b;return v(b,a)}
11
+ function w(a,b){let d=a.getChildAtIndex(b);null==d&&(d=a);if(m.$isRootOrShadowRoot(a))throw Error("Can not call $splitNode() on root element");let f=e=>{const l=e.getParentOrThrow(),h=m.$isRootOrShadowRoot(l),k=e!==d||h?m.$copyNode(e):e;if(h)return e.insertAfter(k),[e,k,k];const [n,x,y]=f(l);e=e.getNextSiblings();y.append(k,...e);return[n,x,k]},[g,c]=f(d);return[g,c]}
12
+ exports.$dfs=function(a,b){let d=[];a=(a||m.$getRoot()).getLatest();b=b||(m.$isElementNode(a)?a.getLastDescendant():a);for(var f=a,g=0;null!==(f=f.getParent());)g++;for(f=g;null!==a&&!a.is(b);)if(d.push({depth:f,node:a}),m.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),f++;else for(g=null;null===g&&null!==a;)g=a.getNextSibling(),null===g?(a=a.getParent(),f--):a=g;null!==a&&a.is(b)&&d.push({depth:f,node:a});return d};exports.$findMatchingParent=r;
13
+ exports.$getNearestBlockElementAncestorOrThrow=function(a){a=r(a,b=>m.$isElementNode(b)&&!b.isInline());m.$isElementNode(a)||p(4);return a};exports.$getNearestNodeOfType=function(a,b){for(;null!=a;){if(a instanceof b)return a;a=a.getParent()}return null};
14
+ exports.$insertNodeToNearestRoot=function(a){var b=m.$getSelection();if(m.$isRangeSelection(b)){var {focus:d}=b;b=d.getNode();d=d.offset;if(m.$isRootOrShadowRoot(b))d=b.getChildAtIndex(d),null==d?b.append(a):d.insertBefore(a),a.selectNext();else{let f,g;m.$isTextNode(b)?(f=b.getParentOrThrow(),g=b.getIndexWithinParent(),0<d&&(g+=1,b.splitText(d))):(f=b,g=d);[,b]=w(f,g);b.insertBefore(a);b.selectStart()}}else m.$isNodeSelection(b)||m.DEPRECATED_$isGridSelection(b)?(b=b.getNodes(),b[b.length-1].getTopLevelElementOrThrow().insertAfter(a)):
15
+ m.$getRoot().append(a),b=m.$createParagraphNode(),a.insertAfter(b),b.select();return a.getLatest()};exports.$restoreEditorState=function(a,b){let d=new Map(b._nodeMap),f=a._pendingEditorState;f&&(f._nodeMap=d);a._dirtyType=2;a=b._selection;m.$setSelection(null===a?null:a.clone())};exports.$splitNode=w;exports.$wrapNodeInElement=function(a,b){b=b();a.replace(b);b.append(a);return b};
16
+ exports.addClassNamesToElement=function(a,...b){b.forEach(d=>{"string"===typeof d&&(d=d.split(" ").filter(f=>""!==f),a.classList.add(...d))})};exports.isMimeType=q;
17
+ exports.mediaFileReader=function(a,b){let d=a[Symbol.iterator]();return new Promise((f,g)=>{let c=[],e=()=>{const {done:l,value:h}=d.next();if(l)return f(c);const k=new FileReader;k.addEventListener("error",g);k.addEventListener("load",()=>{const n=k.result;"string"===typeof n&&c.push({file:h,result:n});e()});q(h,b)?k.readAsDataURL(h):e()};e()})};exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}};
18
+ exports.registerNestedElementResolver=function(a,b,d,f){return a.registerNodeTransform(b,g=>{a:{var c=g.getChildren();for(var e=0;e<c.length;e++)if(c[e]instanceof b){c=null;break a}for(c=g;null!==c;)if(e=c,c=c.getParent(),c instanceof b){c={child:e,parent:c};break a}c=null}if(null!==c){const {child:l,parent:h}=c;if(l.is(g)){f(h,g);g=l.getNextSiblings();c=g.length;h.insertAfter(l);if(0!==c){e=d(h);l.insertAfter(e);for(let k=0;k<c;k++)e.append(g[k])}h.canBeEmpty()||0!==h.getChildrenSize()||h.remove()}}})};
18
19
  exports.removeClassNamesFromElement=function(a,...b){b.forEach(d=>{"string"===typeof d&&a.classList.remove(...d.split(" "))})};exports.unstable_convertLegacyJSONEditorState=u
package/package.json CHANGED
@@ -8,14 +8,14 @@
8
8
  "utils"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.6.3",
11
+ "version": "0.6.5",
12
12
  "main": "LexicalUtils.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.6.3"
14
+ "lexical": "0.6.5"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/list": "0.6.3",
18
- "@lexical/table": "0.6.3"
17
+ "@lexical/list": "0.6.5",
18
+ "@lexical/table": "0.6.5"
19
19
  },
20
20
  "repository": {
21
21
  "type": "git",