@lexical/utils 0.6.2 → 0.6.4
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.
- package/LexicalUtils.dev.js +31 -14
- package/LexicalUtils.js.flow +4 -1
- package/LexicalUtils.prod.js +4 -3
- package/package.json +4 -4
package/LexicalUtils.dev.js
CHANGED
|
@@ -387,25 +387,38 @@ function $insertNodeToNearestRoot(node) {
|
|
|
387
387
|
} = selection;
|
|
388
388
|
const focusNode = focus.getNode();
|
|
389
389
|
const focusOffset = focus.offset;
|
|
390
|
-
let splitNode;
|
|
391
|
-
let splitOffset;
|
|
392
390
|
|
|
393
|
-
if (lexical.$
|
|
394
|
-
|
|
395
|
-
splitOffset = focusNode.getIndexWithinParent();
|
|
391
|
+
if (lexical.$isRootOrShadowRoot(focusNode)) {
|
|
392
|
+
const focusChild = focusNode.getChildAtIndex(focusOffset);
|
|
396
393
|
|
|
397
|
-
if (
|
|
398
|
-
|
|
399
|
-
|
|
394
|
+
if (focusChild == null) {
|
|
395
|
+
focusNode.append(node);
|
|
396
|
+
} else {
|
|
397
|
+
focusChild.insertBefore(node);
|
|
400
398
|
}
|
|
399
|
+
|
|
400
|
+
node.selectNext();
|
|
401
401
|
} else {
|
|
402
|
-
splitNode
|
|
403
|
-
splitOffset
|
|
404
|
-
}
|
|
402
|
+
let splitNode;
|
|
403
|
+
let splitOffset;
|
|
405
404
|
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
405
|
+
if (lexical.$isTextNode(focusNode)) {
|
|
406
|
+
splitNode = focusNode.getParentOrThrow();
|
|
407
|
+
splitOffset = focusNode.getIndexWithinParent();
|
|
408
|
+
|
|
409
|
+
if (focusOffset > 0) {
|
|
410
|
+
splitOffset += 1;
|
|
411
|
+
focusNode.splitText(focusOffset);
|
|
412
|
+
}
|
|
413
|
+
} else {
|
|
414
|
+
splitNode = focusNode;
|
|
415
|
+
splitOffset = focusOffset;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const [, rightTree] = $splitNode(splitNode, splitOffset);
|
|
419
|
+
rightTree.insertBefore(node);
|
|
420
|
+
rightTree.selectStart();
|
|
421
|
+
}
|
|
409
422
|
} else {
|
|
410
423
|
if (lexical.$isNodeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection)) {
|
|
411
424
|
const nodes = selection.getNodes();
|
|
@@ -435,6 +448,10 @@ function $splitNode(node, offset) {
|
|
|
435
448
|
startNode = node;
|
|
436
449
|
}
|
|
437
450
|
|
|
451
|
+
if (!!lexical.$isRootOrShadowRoot(node)) {
|
|
452
|
+
throw Error(`Can not call $splitNode() on root element`);
|
|
453
|
+
}
|
|
454
|
+
|
|
438
455
|
const recurse = currentNode => {
|
|
439
456
|
const parent = currentNode.getParentOrThrow();
|
|
440
457
|
const isParentRoot = lexical.$isRootOrShadowRoot(parent); // The node we start split from (leaf) is moved, but its recursive
|
package/LexicalUtils.js.flow
CHANGED
|
@@ -79,4 +79,7 @@ declare export function $wrapNodeInElement(
|
|
|
79
79
|
createElementNode: () => ElementNode,
|
|
80
80
|
): ElementNode;
|
|
81
81
|
|
|
82
|
-
declare export function $splitNode
|
|
82
|
+
declare export function $splitNode(
|
|
83
|
+
node: ElementNode,
|
|
84
|
+
offset: number,
|
|
85
|
+
): [ElementNode | null, ElementNode];
|
package/LexicalUtils.prod.js
CHANGED
|
@@ -8,11 +8,12 @@
|
|
|
8
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
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
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]}
|
|
11
|
+
function w(a,b){let d=a.getChildAtIndex(b);null==d&&(d=a);if(l.$isRootOrShadowRoot(a))throw Error("Can not call $splitNode() on root element");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
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
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)):
|
|
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};
|
|
14
|
+
exports.$insertNodeToNearestRoot=function(a){var b=l.$getSelection();if(l.$isRangeSelection(b)){var {focus:d}=b;b=d.getNode();d=d.offset;if(l.$isRootOrShadowRoot(b))d=b.getChildAtIndex(d),null==d?b.append(a):d.insertBefore(a),a.selectNext();else{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)):
|
|
15
|
+
l.$getRoot().append(a),b=l.$createParagraphNode(),a.insertAfter(b),b.select();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};
|
|
16
|
+
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
17
|
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
18
|
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()}}})};
|
|
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.
|
|
11
|
+
"version": "0.6.4",
|
|
12
12
|
"main": "LexicalUtils.js",
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"lexical": "0.6.
|
|
14
|
+
"lexical": "0.6.4"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/list": "0.6.
|
|
18
|
-
"@lexical/table": "0.6.
|
|
17
|
+
"@lexical/list": "0.6.4",
|
|
18
|
+
"@lexical/table": "0.6.4"
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|