@lexical/utils 0.6.4 → 0.7.0
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 +0 -116
- package/LexicalUtils.prod.js +10 -13
- package/index.d.ts +0 -1
- package/package.json +4 -4
package/LexicalUtils.dev.js
CHANGED
|
@@ -249,121 +249,6 @@ function registerNestedElementResolver(editor, targetNode, cloneNode, handleOver
|
|
|
249
249
|
};
|
|
250
250
|
|
|
251
251
|
return editor.registerNodeTransform(targetNode, elementNodeTransform);
|
|
252
|
-
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
253
|
-
|
|
254
|
-
function unstable_internalCreateNodeFromParse(parsedNode, parsedNodeMap, editor, parentKey, activeEditorState) {
|
|
255
|
-
const nodeType = parsedNode.__type;
|
|
256
|
-
|
|
257
|
-
const registeredNode = editor._nodes.get(nodeType);
|
|
258
|
-
|
|
259
|
-
if (registeredNode === undefined) {
|
|
260
|
-
{
|
|
261
|
-
throw Error(`createNodeFromParse: type "${nodeType}" + not found`);
|
|
262
|
-
}
|
|
263
|
-
} // Check for properties that are editors
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
for (const property in parsedNode) {
|
|
267
|
-
const value = parsedNode[property];
|
|
268
|
-
|
|
269
|
-
if (value != null && typeof value === 'object') {
|
|
270
|
-
const parsedEditorState = value.editorState;
|
|
271
|
-
|
|
272
|
-
if (parsedEditorState != null) {
|
|
273
|
-
const nestedEditor = lexical.createEditor({
|
|
274
|
-
namespace: parsedEditorState.namespace
|
|
275
|
-
});
|
|
276
|
-
nestedEditor._nodes = editor._nodes;
|
|
277
|
-
nestedEditor._parentEditor = editor._parentEditor;
|
|
278
|
-
nestedEditor._pendingEditorState = unstable_convertLegacyJSONEditorState(nestedEditor, parsedEditorState);
|
|
279
|
-
parsedNode[property] = nestedEditor;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const NodeKlass = registeredNode.klass;
|
|
285
|
-
const parsedKey = parsedNode.__key; // We set the parsedKey to undefined before calling clone() so that
|
|
286
|
-
// we get a new random key assigned.
|
|
287
|
-
|
|
288
|
-
parsedNode.__key = undefined;
|
|
289
|
-
const node = NodeKlass.clone(parsedNode);
|
|
290
|
-
parsedNode.__key = parsedKey;
|
|
291
|
-
const key = node.__key;
|
|
292
|
-
|
|
293
|
-
activeEditorState._nodeMap.set(key, node);
|
|
294
|
-
|
|
295
|
-
node.__parent = parentKey; // We will need to recursively handle the children in the case
|
|
296
|
-
// of a ElementNode.
|
|
297
|
-
|
|
298
|
-
if (lexical.$isElementNode(node)) {
|
|
299
|
-
const children = parsedNode.__children;
|
|
300
|
-
|
|
301
|
-
for (let i = 0; i < children.length; i++) {
|
|
302
|
-
const childKey = children[i];
|
|
303
|
-
const parsedChild = parsedNodeMap.get(childKey);
|
|
304
|
-
|
|
305
|
-
if (parsedChild !== undefined) {
|
|
306
|
-
const child = unstable_internalCreateNodeFromParse(parsedChild, parsedNodeMap, editor, key, activeEditorState);
|
|
307
|
-
const newChildKey = child.__key;
|
|
308
|
-
|
|
309
|
-
node.__children.push(newChildKey);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
node.__indent = parsedNode.__indent;
|
|
314
|
-
node.__format = parsedNode.__format;
|
|
315
|
-
node.__dir = parsedNode.__dir;
|
|
316
|
-
} else if (lexical.$isTextNode(node)) {
|
|
317
|
-
node.__format = parsedNode.__format;
|
|
318
|
-
node.__style = parsedNode.__style;
|
|
319
|
-
node.__mode = parsedNode.__mode;
|
|
320
|
-
node.__detail = parsedNode.__detail;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
return node;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
function unstable_parseEditorState(parsedEditorState, editor) {
|
|
327
|
-
// This is hacky, do not do this!
|
|
328
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
329
|
-
const EditorStateClass = editor._editorState.constructor;
|
|
330
|
-
const nodeMap = new Map();
|
|
331
|
-
const editorState = new EditorStateClass(nodeMap);
|
|
332
|
-
const parsedNodeMap = new Map(parsedEditorState._nodeMap); // root always exists in Map
|
|
333
|
-
|
|
334
|
-
const parsedRoot = parsedNodeMap.get('root');
|
|
335
|
-
const isUpdating = editor._updating;
|
|
336
|
-
|
|
337
|
-
try {
|
|
338
|
-
editor._updating = false;
|
|
339
|
-
editor.update(() => {
|
|
340
|
-
const dirtyElements = editor._dirtyElements;
|
|
341
|
-
const dirtyLeaves = editor._dirtyLeaves;
|
|
342
|
-
const dirtyType = editor._dirtyType;
|
|
343
|
-
editor._dirtyElements = new Map();
|
|
344
|
-
editor._dirtyLeaves = new Set();
|
|
345
|
-
editor._dirtyType = 0;
|
|
346
|
-
|
|
347
|
-
try {
|
|
348
|
-
unstable_internalCreateNodeFromParse(parsedRoot, parsedNodeMap, editor, null, editorState);
|
|
349
|
-
} finally {
|
|
350
|
-
editor._dirtyElements = dirtyElements;
|
|
351
|
-
editor._dirtyLeaves = dirtyLeaves;
|
|
352
|
-
editor._dirtyType = dirtyType;
|
|
353
|
-
}
|
|
354
|
-
});
|
|
355
|
-
} finally {
|
|
356
|
-
editor._updating = isUpdating;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
editorState._readOnly = true;
|
|
360
|
-
return editorState;
|
|
361
|
-
} // TODO: remove this function in version 0.4
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
function unstable_convertLegacyJSONEditorState(editor, maybeStringifiedEditorState) {
|
|
365
|
-
const parsedEditorState = typeof maybeStringifiedEditorState === 'string' ? JSON.parse(maybeStringifiedEditorState) : maybeStringifiedEditorState;
|
|
366
|
-
return unstable_parseEditorState(parsedEditorState, editor);
|
|
367
252
|
}
|
|
368
253
|
function $restoreEditorState(editor, editorState) {
|
|
369
254
|
const FULL_RECONCILE = 2;
|
|
@@ -488,4 +373,3 @@ exports.mediaFileReader = mediaFileReader;
|
|
|
488
373
|
exports.mergeRegister = mergeRegister;
|
|
489
374
|
exports.registerNestedElementResolver = registerNestedElementResolver;
|
|
490
375
|
exports.removeClassNamesFromElement = removeClassNamesFromElement;
|
|
491
|
-
exports.unstable_convertLegacyJSONEditorState = unstable_convertLegacyJSONEditorState;
|
package/LexicalUtils.prod.js
CHANGED
|
@@ -4,16 +4,13 @@
|
|
|
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
|
|
8
|
-
function
|
|
9
|
-
c.
|
|
10
|
-
function
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
exports
|
|
14
|
-
exports
|
|
15
|
-
|
|
16
|
-
exports.
|
|
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())}};
|
|
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()}}})};
|
|
19
|
-
exports.removeClassNamesFromElement=function(a,...b){b.forEach(d=>{"string"===typeof d&&a.classList.remove(...d.split(" "))})};exports.unstable_convertLegacyJSONEditorState=u
|
|
7
|
+
'use strict';var h=require("lexical");function p(a,b){for(let c of b)if(a.type.startsWith(c))return!0;return!1}function q(a,b){for(;a!==h.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null}
|
|
8
|
+
function r(a,b){let c=a.getChildAtIndex(b);null==c&&(c=a);if(h.$isRootOrShadowRoot(a))throw Error("Can not call $splitNode() on root element");let e=f=>{const m=f.getParentOrThrow(),l=h.$isRootOrShadowRoot(m),k=f!==c||l?h.$copyNode(f):f;if(l)return f.insertAfter(k),[f,k,k];const [n,t,u]=e(m);f=f.getNextSiblings();u.append(k,...f);return[n,t,k]},[d,g]=e(c);return[d,g]}
|
|
9
|
+
exports.$dfs=function(a,b){let c=[];a=(a||h.$getRoot()).getLatest();b=b||(h.$isElementNode(a)?a.getLastDescendant():a);for(var e=a,d=0;null!==(e=e.getParent());)d++;for(e=d;null!==a&&!a.is(b);)if(c.push({depth:e,node:a}),h.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),e++;else for(d=null;null===d&&null!==a;)d=a.getNextSibling(),null===d?(a=a.getParent(),e--):a=d;null!==a&&a.is(b)&&c.push({depth:e,node:a});return c};exports.$findMatchingParent=q;
|
|
10
|
+
exports.$getNearestBlockElementAncestorOrThrow=function(a){a=q(a,b=>h.$isElementNode(b)&&!b.isInline());if(!h.$isElementNode(a))throw Error("Minified Lexical error #4; visit https://lexical.dev/docs/error?code=4 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");return a};exports.$getNearestNodeOfType=function(a,b){for(;null!=a;){if(a instanceof b)return a;a=a.getParent()}return null};
|
|
11
|
+
exports.$insertNodeToNearestRoot=function(a){var b=h.$getSelection();if(h.$isRangeSelection(b)){var {focus:c}=b;b=c.getNode();c=c.offset;if(h.$isRootOrShadowRoot(b))c=b.getChildAtIndex(c),null==c?b.append(a):c.insertBefore(a),a.selectNext();else{let e,d;h.$isTextNode(b)?(e=b.getParentOrThrow(),d=b.getIndexWithinParent(),0<c&&(d+=1,b.splitText(c))):(e=b,d=c);[,b]=r(e,d);b.insertBefore(a);b.selectStart()}}else h.$isNodeSelection(b)||h.DEPRECATED_$isGridSelection(b)?(b=b.getNodes(),b[b.length-1].getTopLevelElementOrThrow().insertAfter(a)):
|
|
12
|
+
h.$getRoot().append(a),b=h.$createParagraphNode(),a.insertAfter(b),b.select();return a.getLatest()};exports.$restoreEditorState=function(a,b){let c=new Map(b._nodeMap),e=a._pendingEditorState;e&&(e._nodeMap=c);a._dirtyType=2;a=b._selection;h.$setSelection(null===a?null:a.clone())};exports.$splitNode=r;exports.$wrapNodeInElement=function(a,b){b=b();a.replace(b);b.append(a);return b};
|
|
13
|
+
exports.addClassNamesToElement=function(a,...b){b.forEach(c=>{"string"===typeof c&&(c=c.split(" ").filter(e=>""!==e),a.classList.add(...c))})};exports.isMimeType=p;
|
|
14
|
+
exports.mediaFileReader=function(a,b){let c=a[Symbol.iterator]();return new Promise((e,d)=>{let g=[],f=()=>{const {done:m,value:l}=c.next();if(m)return e(g);const k=new FileReader;k.addEventListener("error",d);k.addEventListener("load",()=>{const n=k.result;"string"===typeof n&&g.push({file:l,result:n});f()});p(l,b)?k.readAsDataURL(l):f()};f()})};exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}};
|
|
15
|
+
exports.registerNestedElementResolver=function(a,b,c,e){return a.registerNodeTransform(b,d=>{a:{var g=d.getChildren();for(var f=0;f<g.length;f++)if(g[f]instanceof b){g=null;break a}for(g=d;null!==g;)if(f=g,g=g.getParent(),g instanceof b){g={child:f,parent:g};break a}g=null}if(null!==g){const {child:m,parent:l}=g;if(m.is(d)){e(l,d);d=m.getNextSiblings();g=d.length;l.insertAfter(m);if(0!==g){f=c(l);m.insertAfter(f);for(let k=0;k<g;k++)f.append(d[k])}l.canBeEmpty()||0!==l.getChildrenSize()||l.remove()}}})};
|
|
16
|
+
exports.removeClassNamesFromElement=function(a,...b){b.forEach(c=>{"string"===typeof c&&a.classList.remove(...c.split(" "))})}
|
package/index.d.ts
CHANGED
|
@@ -38,7 +38,6 @@ export declare function $findMatchingParent(startingNode: LexicalNode, findFn: (
|
|
|
38
38
|
declare type Func = () => void;
|
|
39
39
|
export declare function mergeRegister(...func: Array<Func>): () => void;
|
|
40
40
|
export declare function registerNestedElementResolver<N extends ElementNode>(editor: LexicalEditor, targetNode: Klass<N>, cloneNode: (from: N) => N, handleOverlap: (from: N, to: N) => void): () => void;
|
|
41
|
-
export declare function unstable_convertLegacyJSONEditorState(editor: LexicalEditor, maybeStringifiedEditorState: string): EditorState;
|
|
42
41
|
export declare function $restoreEditorState(editor: LexicalEditor, editorState: EditorState): void;
|
|
43
42
|
export declare function $insertNodeToNearestRoot<T extends LexicalNode>(node: T): T;
|
|
44
43
|
export declare function $wrapNodeInElement(node: LexicalNode, createElementNode: () => ElementNode): ElementNode;
|
package/package.json
CHANGED
|
@@ -8,14 +8,14 @@
|
|
|
8
8
|
"utils"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.
|
|
11
|
+
"version": "0.7.0",
|
|
12
12
|
"main": "LexicalUtils.js",
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"lexical": "0.
|
|
14
|
+
"lexical": "0.7.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/list": "0.
|
|
18
|
-
"@lexical/table": "0.
|
|
17
|
+
"@lexical/list": "0.7.0",
|
|
18
|
+
"@lexical/table": "0.7.0"
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|