@lexical/utils 0.3.5 → 0.3.8

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.
@@ -73,10 +73,10 @@ function $dfs(startingNode, endingNode) {
73
73
  }
74
74
 
75
75
  function $getDepth(node) {
76
- let node_ = node;
76
+ let innerNode = node;
77
77
  let depth = 0;
78
78
 
79
- while ((node_ = node_.getParent()) !== null) {
79
+ while ((innerNode = innerNode.getParent()) !== null) {
80
80
  depth++;
81
81
  }
82
82
 
@@ -94,7 +94,7 @@ function $getNearestNodeOfType(node, klass) {
94
94
  parent = parent.getParent();
95
95
  }
96
96
 
97
- return parent;
97
+ return null;
98
98
  }
99
99
  function $getNearestBlockElementAncestorOrThrow(startNode) {
100
100
  const blockNode = $findMatchingParent(startNode, node => lexical.$isElementNode(node) && !node.isInline());
@@ -229,8 +229,7 @@ function unstable_internalCreateNodeFromParse(parsedNode, parsedNodeMap, editor,
229
229
  const parsedKey = parsedNode.__key; // We set the parsedKey to undefined before calling clone() so that
230
230
  // we get a new random key assigned.
231
231
 
232
- parsedNode.__key = undefined; // @ts-expect-error TODO Replace Class utility type with InstanceType
233
-
232
+ parsedNode.__key = undefined;
234
233
  const node = NodeKlass.clone(parsedNode);
235
234
  parsedNode.__key = parsedKey;
236
235
  const key = node.__key;
@@ -314,16 +313,40 @@ function $restoreEditorState(editor, editorState) {
314
313
  const FULL_RECONCILE = 2;
315
314
  const nodeMap = new Map(editorState._nodeMap);
316
315
  const activeEditorState = editor._pendingEditorState;
317
- activeEditorState._nodeMap = nodeMap;
316
+
317
+ if (activeEditorState) {
318
+ activeEditorState._nodeMap = nodeMap;
319
+ }
320
+
318
321
  editor._dirtyType = FULL_RECONCILE;
319
322
  const selection = editorState._selection;
320
323
  lexical.$setSelection(selection === null ? null : selection.clone());
321
324
  }
325
+ function $insertBlockNode(node) {
326
+ const selection = lexical.$getSelection();
327
+
328
+ if (lexical.$isRangeSelection(selection)) {
329
+ const focusNode = selection.focus.getNode();
330
+ focusNode.getTopLevelElementOrThrow().insertAfter(node);
331
+ } else if (lexical.$isNodeSelection(selection) || lexical.$isGridSelection(selection)) {
332
+ const nodes = selection.getNodes();
333
+ nodes[nodes.length - 1].getTopLevelElementOrThrow().insertAfter(node);
334
+ } else {
335
+ const root = lexical.$getRoot();
336
+ root.append(node);
337
+ }
338
+
339
+ const paragraphNode = lexical.$createParagraphNode();
340
+ node.insertAfter(paragraphNode);
341
+ paragraphNode.select();
342
+ return node.getLatest();
343
+ }
322
344
 
323
345
  exports.$dfs = $dfs;
324
346
  exports.$findMatchingParent = $findMatchingParent;
325
347
  exports.$getNearestBlockElementAncestorOrThrow = $getNearestBlockElementAncestorOrThrow;
326
348
  exports.$getNearestNodeOfType = $getNearestNodeOfType;
349
+ exports.$insertBlockNode = $insertBlockNode;
327
350
  exports.$restoreEditorState = $restoreEditorState;
328
351
  exports.addClassNamesToElement = addClassNamesToElement;
329
352
  exports.mergeRegister = mergeRegister;
@@ -63,3 +63,5 @@ declare export function $restoreEditorState(
63
63
  editor: LexicalEditor,
64
64
  editorState: EditorState,
65
65
  ): void;
66
+
67
+ declare export function $insertBlockNode<T: LexicalNode>(node: T): T;
@@ -4,12 +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 l=require("lexical");function n(a){throw Error(`Minified Lexical error #${a}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function p(a,b){for(;a!==l.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null}
8
- function q(a,b,e,k,d){var c=e._nodes.get(a.__type);void 0===c&&n(5);for(var g in a){var h=a[g];if(null!=h&&"object"===typeof h&&(h=h.editorState,null!=h)){var f=l.createEditor({namespace:h.namespace});f._nodes=e._nodes;f._parentEditor=e._parentEditor;f._pendingEditorState=r(f,h);a[g]=f}}c=c.klass;g=a.__key;a.__key=void 0;c=c.clone(a);a.__key=g;g=c.__key;d._nodeMap.set(g,c);c.__parent=k;if(l.$isElementNode(c)){k=a.__children;for(h=0;h<k.length;h++)f=b.get(k[h]),void 0!==f&&(f=q(f,b,e,g,d).__key,c.__children.push(f));
7
+ 'use strict';var l=require("lexical");function n(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 p(a,b){for(;a!==l.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null}
8
+ function q(a,b,e,f,d){var c=e._nodes.get(a.__type);void 0===c&&n(5);for(var h in a){var k=a[h];if(null!=k&&"object"===typeof k&&(k=k.editorState,null!=k)){var g=l.createEditor({namespace:k.namespace});g._nodes=e._nodes;g._parentEditor=e._parentEditor;g._pendingEditorState=r(g,k);a[h]=g}}c=c.klass;h=a.__key;a.__key=void 0;c=c.clone(a);a.__key=h;h=c.__key;d._nodeMap.set(h,c);c.__parent=f;if(l.$isElementNode(c)){f=a.__children;for(k=0;k<f.length;k++)g=b.get(f[k]),void 0!==g&&(g=q(g,b,e,h,d).__key,c.__children.push(g));
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 t(a,b){let e=b._editorState.constructor,k=new Map,d=new e(k),c=new Map(a._nodeMap),g=c.get("root");a=b._updating;try{b._updating=!1,b.update(()=>{let h=b._dirtyElements,f=b._dirtyLeaves,m=b._dirtyType;b._dirtyElements=new Map;b._dirtyLeaves=new Set;b._dirtyType=0;try{q(g,c,b,null,d)}finally{b._dirtyElements=h,b._dirtyLeaves=f,b._dirtyType=m}})}finally{b._updating=a}d._readOnly=!0;return d}function r(a,b){b="string"===typeof b?JSON.parse(b):b;return t(b,a)}
11
- exports.$dfs=function(a,b){let e=[];a=(a||l.$getRoot()).getLatest();b=b||(l.$isElementNode(a)?a.getLastDescendant():a);for(var k=a,d=0;null!==(k=k.getParent());)d++;for(k=d;null!==a&&!a.is(b);)if(e.push({depth:k,node:a}),l.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),k++;else for(d=null;null===d&&null!==a;)d=a.getNextSibling(),null===d?(a=a.getParent(),k--):a=d;null!==a&&a.is(b)&&e.push({depth:k,node:a});return e};exports.$findMatchingParent=p;
12
- exports.$getNearestBlockElementAncestorOrThrow=function(a){a=p(a,b=>l.$isElementNode(b)&&!b.isInline());l.$isElementNode(a)||n(4);return a};exports.$getNearestNodeOfType=function(a,b){for(;null!=a&&!(a instanceof b);)a=a.getParent();return a};exports.$restoreEditorState=function(a,b){let e=new Map(b._nodeMap);a._pendingEditorState._nodeMap=e;a._dirtyType=2;a=b._selection;l.$setSelection(null===a?null:a.clone())};exports.addClassNamesToElement=function(a,...b){b.forEach(e=>{"string"===typeof e&&a.classList.add(...e.split(" "))})};
13
- exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}};
14
- exports.registerNestedElementResolver=function(a,b,e,k){return a.registerNodeTransform(b,d=>{a:{var c=d.getChildren();for(var g=0;g<c.length;g++)if(c[g]instanceof b){c=null;break a}for(c=d;null!==c;)if(g=c,c=c.getParent(),c instanceof b){c={child:g,parent:c};break a}c=null}if(null!==c){const {child:h,parent:f}=c;if(h.is(d)){k(f,d);d=h.getNextSiblings();c=d.length;f.insertAfter(h);if(0!==c){g=e(f);h.insertAfter(g);for(let m=0;m<c;m++)g.append(d[m])}f.canBeEmpty()||0!==f.getChildrenSize()||f.remove()}}})};
10
+ function t(a,b){let e=b._editorState.constructor,f=new Map,d=new e(f),c=new Map(a._nodeMap),h=c.get("root");a=b._updating;try{b._updating=!1,b.update(()=>{let k=b._dirtyElements,g=b._dirtyLeaves,m=b._dirtyType;b._dirtyElements=new Map;b._dirtyLeaves=new Set;b._dirtyType=0;try{q(h,c,b,null,d)}finally{b._dirtyElements=k,b._dirtyLeaves=g,b._dirtyType=m}})}finally{b._updating=a}d._readOnly=!0;return d}function r(a,b){b="string"===typeof b?JSON.parse(b):b;return t(b,a)}
11
+ exports.$dfs=function(a,b){let e=[];a=(a||l.$getRoot()).getLatest();b=b||(l.$isElementNode(a)?a.getLastDescendant():a);for(var f=a,d=0;null!==(f=f.getParent());)d++;for(f=d;null!==a&&!a.is(b);)if(e.push({depth:f,node:a}),l.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),f++;else for(d=null;null===d&&null!==a;)d=a.getNextSibling(),null===d?(a=a.getParent(),f--):a=d;null!==a&&a.is(b)&&e.push({depth:f,node:a});return e};exports.$findMatchingParent=p;
12
+ exports.$getNearestBlockElementAncestorOrThrow=function(a){a=p(a,b=>l.$isElementNode(b)&&!b.isInline());l.$isElementNode(a)||n(4);return a};exports.$getNearestNodeOfType=function(a,b){for(;null!=a;){if(a instanceof b)return a;a=a.getParent()}return null};
13
+ exports.$insertBlockNode=function(a){var b=l.$getSelection();l.$isRangeSelection(b)?b.focus.getNode().getTopLevelElementOrThrow().insertAfter(a):l.$isNodeSelection(b)||l.$isGridSelection(b)?(b=b.getNodes(),b[b.length-1].getTopLevelElementOrThrow().insertAfter(a)):l.$getRoot().append(a);b=l.$createParagraphNode();a.insertAfter(b);b.select();return a.getLatest()};
14
+ exports.$restoreEditorState=function(a,b){let e=new Map(b._nodeMap),f=a._pendingEditorState;f&&(f._nodeMap=e);a._dirtyType=2;a=b._selection;l.$setSelection(null===a?null:a.clone())};exports.addClassNamesToElement=function(a,...b){b.forEach(e=>{"string"===typeof e&&a.classList.add(...e.split(" "))})};exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}};
15
+ exports.registerNestedElementResolver=function(a,b,e,f){return a.registerNodeTransform(b,d=>{a:{var c=d.getChildren();for(var h=0;h<c.length;h++)if(c[h]instanceof b){c=null;break a}for(c=d;null!==c;)if(h=c,c=c.getParent(),c instanceof b){c={child:h,parent:c};break a}c=null}if(null!==c){const {child:k,parent:g}=c;if(k.is(d)){f(g,d);d=k.getNextSiblings();c=d.length;g.insertAfter(k);if(0!==c){h=e(g);k.insertAfter(h);for(let m=0;m<c;m++)h.append(d[m])}g.canBeEmpty()||0!==g.getChildrenSize()||g.remove()}}})};
15
16
  exports.removeClassNamesFromElement=function(a,...b){b.forEach(e=>{"string"===typeof e&&a.classList.remove(...e.split(" "))})};exports.unstable_convertLegacyJSONEditorState=r
package/index.d.ts CHANGED
@@ -5,16 +5,15 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
7
  */
8
- import type { EditorState, ElementNode, LexicalEditor, LexicalNode } from 'lexical';
9
- import type { Klass } from 'shared/types';
8
+ import { EditorState, ElementNode, Klass, LexicalEditor, LexicalNode } from 'lexical';
10
9
  export declare type DFSNode = Readonly<{
11
10
  depth: number;
12
11
  node: LexicalNode;
13
12
  }>;
14
13
  export declare function addClassNamesToElement(element: HTMLElement, ...classNames: Array<typeof undefined | boolean | null | string>): void;
15
- export declare function removeClassNamesFromElement(element: HTMLElement, ...classNames: Array<string>): void;
14
+ export declare function removeClassNamesFromElement(element: HTMLElement, ...classNames: Array<typeof undefined | boolean | null | string>): void;
16
15
  export declare function $dfs(startingNode?: LexicalNode, endingNode?: LexicalNode): Array<DFSNode>;
17
- export declare function $getNearestNodeOfType<T extends ElementNode>(node: LexicalNode, klass: Klass<T>): T | LexicalNode;
16
+ export declare function $getNearestNodeOfType<T extends ElementNode>(node: LexicalNode, klass: Klass<T>): T | null;
18
17
  export declare function $getNearestBlockElementAncestorOrThrow(startNode: LexicalNode): ElementNode;
19
18
  export declare type DOMNodeToLexicalConversion = (element: Node) => LexicalNode;
20
19
  export declare type DOMNodeToLexicalConversionMap = Record<string, DOMNodeToLexicalConversion>;
@@ -24,4 +23,5 @@ export declare function mergeRegister(...func: Array<Func>): () => void;
24
23
  export declare function registerNestedElementResolver<N extends ElementNode>(editor: LexicalEditor, targetNode: Klass<N>, cloneNode: (from: N) => N, handleOverlap: (from: N, to: N) => void): () => void;
25
24
  export declare function unstable_convertLegacyJSONEditorState(editor: LexicalEditor, maybeStringifiedEditorState: string): EditorState;
26
25
  export declare function $restoreEditorState(editor: LexicalEditor, editorState: EditorState): void;
26
+ export declare function $insertBlockNode<T extends LexicalNode>(node: T): T;
27
27
  export {};
package/package.json CHANGED
@@ -8,21 +8,18 @@
8
8
  "utils"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.3.5",
11
+ "version": "0.3.8",
12
12
  "main": "LexicalUtils.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.3.5"
14
+ "lexical": "0.3.8"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/list": "0.3.5",
18
- "@lexical/table": "0.3.5"
17
+ "@lexical/list": "0.3.8",
18
+ "@lexical/table": "0.3.8"
19
19
  },
20
20
  "repository": {
21
21
  "type": "git",
22
22
  "url": "https://github.com/facebook/lexical",
23
23
  "directory": "packages/lexical-utils"
24
- },
25
- "devDependencies": {
26
- "utility-types": "^3.10.0"
27
24
  }
28
25
  }