@lexical/utils 0.11.1 → 0.11.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.
@@ -385,7 +385,7 @@ function $restoreEditorState(editor, editorState) {
385
385
  */
386
386
 
387
387
  function $insertNodeToNearestRoot(node) {
388
- const selection = lexical.$getSelection();
388
+ const selection = lexical.$getSelection() || lexical.$getPreviousSelection();
389
389
 
390
390
  if (lexical.$isRangeSelection(selection)) {
391
391
  const {
@@ -470,6 +470,15 @@ function isHTMLAnchorElement(x) {
470
470
  function isHTMLElement(x) {
471
471
  // @ts-ignore-next-line - strict check on nodeType here should filter out non-Element EventTarget implementors
472
472
  return x.nodeType === 1;
473
+ } // eslint-disable-next-line @typescript-eslint/no-explicit-any
474
+
475
+ /**
476
+ * @param object = The instance of the type
477
+ * @param objectClass = The class of the type
478
+ * @returns Whether the object is has the same Klass of the objectClass, ignoring the difference across window (e.g. different iframs)
479
+ */
480
+ function objectKlassEquals(object, objectClass) {
481
+ return object !== null ? Object.getPrototypeOf(object).constructor.name === objectClass.name : false;
473
482
  }
474
483
 
475
484
  exports.$splitNode = lexical.$splitNode;
@@ -486,5 +495,6 @@ exports.isHTMLElement = isHTMLElement;
486
495
  exports.isMimeType = isMimeType;
487
496
  exports.mediaFileReader = mediaFileReader;
488
497
  exports.mergeRegister = mergeRegister;
498
+ exports.objectKlassEquals = objectKlassEquals;
489
499
  exports.registerNestedElementResolver = registerNestedElementResolver;
490
500
  exports.removeClassNamesFromElement = removeClassNamesFromElement;
@@ -4,12 +4,14 @@
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 g=require("@lexical/selection"),n=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!==n.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null}function t(a){return 1===a.nodeType}exports.$splitNode=n.$splitNode;
8
- exports.$dfs=function(a,b){let c=[];a=(a||n.$getRoot()).getLatest();b=b||(n.$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(c.push({depth:f,node:a}),n.$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)&&c.push({depth:f,node:a});return c};exports.$findMatchingParent=q;
9
- exports.$getNearestBlockElementAncestorOrThrow=function(a){a=q(a,b=>n.$isElementNode(b)&&!b.isInline());if(!n.$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};
10
- exports.$insertNodeToNearestRoot=function(a){var b=n.$getSelection();if(n.$isRangeSelection(b)){var {focus:c}=b;b=c.getNode();c=c.offset;if(n.$isRootOrShadowRoot(b))c=b.getChildAtIndex(c),null==c?b.append(a):c.insertBefore(a),a.selectNext();else{let f,d;n.$isTextNode(b)?(f=b.getParentOrThrow(),d=b.getIndexWithinParent(),0<c&&(d+=1,b.splitText(c))):(f=b,d=c);[,b]=n.$splitNode(f,d);b.insertBefore(a);b.selectStart()}}else n.$isNodeSelection(b)||n.DEPRECATED_$isGridSelection(b)?(b=b.getNodes(),b[b.length-
11
- 1].getTopLevelElementOrThrow().insertAfter(a)):n.$getRoot().append(a),b=n.$createParagraphNode(),a.insertAfter(b),b.select();return a.getLatest()};exports.$restoreEditorState=function(a,b){let c=new Map,f=a._pendingEditorState;for(let [d,e]of b._nodeMap){let h=g.$cloneWithProperties(e);n.$isTextNode(h)&&(h.__text=e.__text);c.set(d,h)}f&&(f._nodeMap=c);a._dirtyType=2;a=b._selection;n.$setSelection(null===a?null:a.clone())};exports.$wrapNodeInElement=function(a,b){b=b();a.replace(b);b.append(a);return b};
12
- exports.addClassNamesToElement=function(a,...b){b.forEach(c=>{"string"===typeof c&&(c=c.split(" ").filter(f=>""!==f),a.classList.add(...c))})};exports.isHTMLAnchorElement=function(a){return t(a)&&"A"===a.tagName};exports.isHTMLElement=t;exports.isMimeType=p;
13
- exports.mediaFileReader=function(a,b){let c=a[Symbol.iterator]();return new Promise((f,d)=>{let e=[],h=()=>{const {done:m,value:k}=c.next();if(m)return f(e);const l=new FileReader;l.addEventListener("error",d);l.addEventListener("load",()=>{const r=l.result;"string"===typeof r&&e.push({file:k,result:r});h()});p(k,b)?l.readAsDataURL(k):h()};h()})};exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}};
7
+ 'use strict';var g=require("@lexical/selection"),n=require("lexical");function p(a){let b=new URLSearchParams;b.append("code",a);for(let c=1;c<arguments.length;c++)b.append("v",arguments[c]);throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?${b} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function q(a,b){for(let c of b)if(a.type.startsWith(c))return!0;return!1}
8
+ function t(a,b){for(;a!==n.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null}function u(a){return 1===a.nodeType}exports.$splitNode=n.$splitNode;
9
+ exports.$dfs=function(a,b){let c=[];a=(a||n.$getRoot()).getLatest();b=b||(n.$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(c.push({depth:f,node:a}),n.$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)&&c.push({depth:f,node:a});return c};exports.$findMatchingParent=t;
10
+ exports.$getNearestBlockElementAncestorOrThrow=function(a){let b=t(a,c=>n.$isElementNode(c)&&!c.isInline());n.$isElementNode(b)||p(4,a.__key);return b};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=n.$getSelection()||n.$getPreviousSelection();if(n.$isRangeSelection(b)){var {focus:c}=b;b=c.getNode();c=c.offset;if(n.$isRootOrShadowRoot(b))c=b.getChildAtIndex(c),null==c?b.append(a):c.insertBefore(a),a.selectNext();else{let f,d;n.$isTextNode(b)?(f=b.getParentOrThrow(),d=b.getIndexWithinParent(),0<c&&(d+=1,b.splitText(c))):(f=b,d=c);[,b]=n.$splitNode(f,d);b.insertBefore(a);b.selectStart()}}else n.$isNodeSelection(b)||n.DEPRECATED_$isGridSelection(b)?
12
+ (b=b.getNodes(),b[b.length-1].getTopLevelElementOrThrow().insertAfter(a)):n.$getRoot().append(a),b=n.$createParagraphNode(),a.insertAfter(b),b.select();return a.getLatest()};exports.$restoreEditorState=function(a,b){let c=new Map,f=a._pendingEditorState;for(let [d,e]of b._nodeMap){let h=g.$cloneWithProperties(e);n.$isTextNode(h)&&(h.__text=e.__text);c.set(d,h)}f&&(f._nodeMap=c);a._dirtyType=2;a=b._selection;n.$setSelection(null===a?null:a.clone())};
13
+ exports.$wrapNodeInElement=function(a,b){b=b();a.replace(b);b.append(a);return b};exports.addClassNamesToElement=function(a,...b){b.forEach(c=>{"string"===typeof c&&(c=c.split(" ").filter(f=>""!==f),a.classList.add(...c))})};exports.isHTMLAnchorElement=function(a){return u(a)&&"A"===a.tagName};exports.isHTMLElement=u;exports.isMimeType=q;
14
+ exports.mediaFileReader=function(a,b){let c=a[Symbol.iterator]();return new Promise((f,d)=>{let e=[],h=()=>{const {done:m,value:k}=c.next();if(m)return f(e);const l=new FileReader;l.addEventListener("error",d);l.addEventListener("load",()=>{const r=l.result;"string"===typeof r&&e.push({file:k,result:r});h()});q(k,b)?l.readAsDataURL(k):h()};h()})};exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}};
15
+ exports.objectKlassEquals=function(a,b){return null!==a?Object.getPrototypeOf(a).constructor.name===b.name:!1};
14
16
  exports.registerNestedElementResolver=function(a,b,c,f){return a.registerNodeTransform(b,d=>{a:{var e=d.getChildren();for(var h=0;h<e.length;h++)if(e[h]instanceof b){e=null;break a}for(e=d;null!==e;)if(h=e,e=e.getParent(),e instanceof b){e={child:h,parent:e};break a}e=null}if(null!==e){const {child:m,parent:k}=e;if(m.is(d)){f(k,d);d=m.getNextSiblings();e=d.length;k.insertAfter(m);if(0!==e){h=c(k);m.insertAfter(h);for(let l=0;l<e;l++)h.append(d[l])}k.canBeEmpty()||0!==k.getChildrenSize()||k.remove()}}})};
15
17
  exports.removeClassNamesFromElement=function(a,...b){b.forEach(c=>{"string"===typeof c&&a.classList.remove(...c.split(" "))})}
package/index.d.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  */
9
9
  import { $splitNode, EditorState, ElementNode, Klass, LexicalEditor, LexicalNode } from 'lexical';
10
10
  export { $splitNode };
11
- export declare type DFSNode = Readonly<{
11
+ export type DFSNode = Readonly<{
12
12
  depth: number;
13
13
  node: LexicalNode;
14
14
  }>;
@@ -79,8 +79,8 @@ export declare function $getNearestNodeOfType<T extends ElementNode>(node: Lexic
79
79
  * @returns The ancestor node found
80
80
  */
81
81
  export declare function $getNearestBlockElementAncestorOrThrow(startNode: LexicalNode): ElementNode;
82
- export declare type DOMNodeToLexicalConversion = (element: Node) => LexicalNode;
83
- export declare type DOMNodeToLexicalConversionMap = Record<string, DOMNodeToLexicalConversion>;
82
+ export type DOMNodeToLexicalConversion = (element: Node) => LexicalNode;
83
+ export type DOMNodeToLexicalConversionMap = Record<string, DOMNodeToLexicalConversion>;
84
84
  /**
85
85
  * Starts with a node and moves up the tree (toward the root node) to find a matching node based on
86
86
  * the search parameters of the findFn. (Consider JavaScripts' .find() function where a testing function must be
@@ -90,7 +90,7 @@ export declare type DOMNodeToLexicalConversionMap = Record<string, DOMNodeToLexi
90
90
  * @returns A parent node that matches the findFn parameters, or null if one wasn't found.
91
91
  */
92
92
  export declare function $findMatchingParent(startingNode: LexicalNode, findFn: (node: LexicalNode) => boolean): LexicalNode | null;
93
- declare type Func = () => void;
93
+ type Func = () => void;
94
94
  /**
95
95
  * Returns a function that will execute all functions passed when called. It is generally used
96
96
  * to register multiple lexical listeners and then tear them down with a single function call, such
@@ -157,3 +157,10 @@ export declare function isHTMLAnchorElement(x: Node): x is HTMLAnchorElement;
157
157
  * @returns Returns true if x is an HTML element, false otherwise.
158
158
  */
159
159
  export declare function isHTMLElement(x: Node | EventTarget): x is HTMLElement;
160
+ type ObjectKlass<T> = new (...args: any[]) => T;
161
+ /**
162
+ * @param object = The instance of the type
163
+ * @param objectClass = The class of the type
164
+ * @returns Whether the object is has the same Klass of the objectClass, ignoring the difference across window (e.g. different iframs)
165
+ */
166
+ export declare function objectKlassEquals<T>(object: unknown, objectClass: ObjectKlass<T>): boolean;
package/package.json CHANGED
@@ -8,15 +8,15 @@
8
8
  "utils"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.11.1",
11
+ "version": "0.11.3",
12
12
  "main": "LexicalUtils.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.11.1"
14
+ "lexical": "0.11.3"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/list": "0.11.1",
18
- "@lexical/table": "0.11.1",
19
- "@lexical/selection": "0.11.1"
17
+ "@lexical/list": "0.11.3",
18
+ "@lexical/table": "0.11.3",
19
+ "@lexical/selection": "0.11.3"
20
20
  },
21
21
  "repository": {
22
22
  "type": "git",