@lexical/utils 0.6.1-next.0 → 0.6.3
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 +64 -10
- package/LexicalUtils.js.flow +2 -0
- package/LexicalUtils.prod.js +11 -10
- package/index.d.ts +1 -0
- package/package.json +4 -4
package/LexicalUtils.dev.js
CHANGED
|
@@ -382,19 +382,44 @@ function $insertNodeToNearestRoot(node) {
|
|
|
382
382
|
const selection = lexical.$getSelection();
|
|
383
383
|
|
|
384
384
|
if (lexical.$isRangeSelection(selection)) {
|
|
385
|
-
const
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
const
|
|
389
|
-
|
|
385
|
+
const {
|
|
386
|
+
focus
|
|
387
|
+
} = selection;
|
|
388
|
+
const focusNode = focus.getNode();
|
|
389
|
+
const focusOffset = focus.offset;
|
|
390
|
+
let splitNode;
|
|
391
|
+
let splitOffset;
|
|
392
|
+
|
|
393
|
+
if (lexical.$isTextNode(focusNode)) {
|
|
394
|
+
splitNode = focusNode.getParentOrThrow();
|
|
395
|
+
splitOffset = focusNode.getIndexWithinParent();
|
|
396
|
+
|
|
397
|
+
if (focusOffset > 0) {
|
|
398
|
+
splitOffset += 1;
|
|
399
|
+
focusNode.splitText(focusOffset);
|
|
400
|
+
}
|
|
401
|
+
} else {
|
|
402
|
+
splitNode = focusNode;
|
|
403
|
+
splitOffset = focusOffset;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
const [, rightTree] = $splitNode(splitNode, splitOffset);
|
|
407
|
+
rightTree.insertBefore(node);
|
|
408
|
+
rightTree.selectStart();
|
|
390
409
|
} else {
|
|
391
|
-
|
|
392
|
-
|
|
410
|
+
if (lexical.$isNodeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection)) {
|
|
411
|
+
const nodes = selection.getNodes();
|
|
412
|
+
nodes[nodes.length - 1].getTopLevelElementOrThrow().insertAfter(node);
|
|
413
|
+
} else {
|
|
414
|
+
const root = lexical.$getRoot();
|
|
415
|
+
root.append(node);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const paragraphNode = lexical.$createParagraphNode();
|
|
419
|
+
node.insertAfter(paragraphNode);
|
|
420
|
+
paragraphNode.select();
|
|
393
421
|
}
|
|
394
422
|
|
|
395
|
-
const paragraphNode = lexical.$createParagraphNode();
|
|
396
|
-
node.insertAfter(paragraphNode);
|
|
397
|
-
paragraphNode.select();
|
|
398
423
|
return node.getLatest();
|
|
399
424
|
}
|
|
400
425
|
function $wrapNodeInElement(node, createElementNode) {
|
|
@@ -403,6 +428,34 @@ function $wrapNodeInElement(node, createElementNode) {
|
|
|
403
428
|
elementNode.append(node);
|
|
404
429
|
return elementNode;
|
|
405
430
|
}
|
|
431
|
+
function $splitNode(node, offset) {
|
|
432
|
+
let startNode = node.getChildAtIndex(offset);
|
|
433
|
+
|
|
434
|
+
if (startNode == null) {
|
|
435
|
+
startNode = node;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const recurse = currentNode => {
|
|
439
|
+
const parent = currentNode.getParentOrThrow();
|
|
440
|
+
const isParentRoot = lexical.$isRootOrShadowRoot(parent); // The node we start split from (leaf) is moved, but its recursive
|
|
441
|
+
// parents are copied to create separate tree
|
|
442
|
+
|
|
443
|
+
const nodeToMove = currentNode === startNode && !isParentRoot ? currentNode : lexical.$copyNode(currentNode);
|
|
444
|
+
|
|
445
|
+
if (isParentRoot) {
|
|
446
|
+
currentNode.insertAfter(nodeToMove);
|
|
447
|
+
return [currentNode, nodeToMove, nodeToMove];
|
|
448
|
+
} else {
|
|
449
|
+
const [leftTree, rightTree, newParent] = recurse(parent);
|
|
450
|
+
const nextSiblings = currentNode.getNextSiblings();
|
|
451
|
+
newParent.append(nodeToMove, ...nextSiblings);
|
|
452
|
+
return [leftTree, rightTree, nodeToMove];
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
const [leftTree, rightTree] = recurse(startNode);
|
|
457
|
+
return [leftTree, rightTree];
|
|
458
|
+
}
|
|
406
459
|
|
|
407
460
|
exports.$dfs = $dfs;
|
|
408
461
|
exports.$findMatchingParent = $findMatchingParent;
|
|
@@ -410,6 +463,7 @@ exports.$getNearestBlockElementAncestorOrThrow = $getNearestBlockElementAncestor
|
|
|
410
463
|
exports.$getNearestNodeOfType = $getNearestNodeOfType;
|
|
411
464
|
exports.$insertNodeToNearestRoot = $insertNodeToNearestRoot;
|
|
412
465
|
exports.$restoreEditorState = $restoreEditorState;
|
|
466
|
+
exports.$splitNode = $splitNode;
|
|
413
467
|
exports.$wrapNodeInElement = $wrapNodeInElement;
|
|
414
468
|
exports.addClassNamesToElement = addClassNamesToElement;
|
|
415
469
|
exports.isMimeType = isMimeType;
|
package/LexicalUtils.js.flow
CHANGED
package/LexicalUtils.prod.js
CHANGED
|
@@ -4,14 +4,15 @@
|
|
|
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
|
|
8
|
-
function
|
|
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
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,
|
|
11
|
-
|
|
12
|
-
exports.$
|
|
13
|
-
exports.$
|
|
14
|
-
exports.$
|
|
15
|
-
exports
|
|
16
|
-
exports.
|
|
17
|
-
exports.
|
|
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()}}})};
|
|
18
|
+
exports.removeClassNamesFromElement=function(a,...b){b.forEach(d=>{"string"===typeof d&&a.classList.remove(...d.split(" "))})};exports.unstable_convertLegacyJSONEditorState=u
|
package/index.d.ts
CHANGED
|
@@ -42,4 +42,5 @@ export declare function unstable_convertLegacyJSONEditorState(editor: LexicalEdi
|
|
|
42
42
|
export declare function $restoreEditorState(editor: LexicalEditor, editorState: EditorState): void;
|
|
43
43
|
export declare function $insertNodeToNearestRoot<T extends LexicalNode>(node: T): T;
|
|
44
44
|
export declare function $wrapNodeInElement(node: LexicalNode, createElementNode: () => ElementNode): ElementNode;
|
|
45
|
+
export declare function $splitNode(node: ElementNode, offset: number): [ElementNode | null, ElementNode];
|
|
45
46
|
export {};
|
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.3",
|
|
12
12
|
"main": "LexicalUtils.js",
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"lexical": "0.6.
|
|
14
|
+
"lexical": "0.6.3"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/list": "0.6.
|
|
18
|
-
"@lexical/table": "0.6.
|
|
17
|
+
"@lexical/list": "0.6.3",
|
|
18
|
+
"@lexical/table": "0.6.3"
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|