@lexical/utils 0.12.0 → 0.12.2

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.
@@ -453,23 +453,6 @@ function $wrapNodeInElement(node, createElementNode) {
453
453
  node.replace(elementNode);
454
454
  elementNode.append(node);
455
455
  return elementNode;
456
- }
457
- /**
458
- * @param x - The element being tested
459
- * @returns Returns true if x is an HTML anchor tag, false otherwise
460
- */
461
-
462
- function isHTMLAnchorElement(x) {
463
- return isHTMLElement(x) && x.tagName === 'A';
464
- }
465
- /**
466
- * @param x - The element being testing
467
- * @returns Returns true if x is an HTML element, false otherwise.
468
- */
469
-
470
- function isHTMLElement(x) {
471
- // @ts-ignore-next-line - strict check on nodeType here should filter out non-Element EventTarget implementors
472
- return x.nodeType === 1;
473
456
  } // eslint-disable-next-line @typescript-eslint/no-explicit-any
474
457
 
475
458
  /**
@@ -480,18 +463,55 @@ function isHTMLElement(x) {
480
463
  function objectKlassEquals(object, objectClass) {
481
464
  return object !== null ? Object.getPrototypeOf(object).constructor.name === objectClass.name : false;
482
465
  }
466
+ /**
467
+ * Filter the nodes
468
+ * @param nodes Array of nodes that needs to be filtered
469
+ * @param filterFn A filter function that returns node if the current node satisfies the condition otherwise null
470
+ * @returns Array of filtered nodes
471
+ */
472
+
473
+ function $filter(nodes, filterFn) {
474
+ const result = [];
475
+
476
+ for (let i = 0; i < nodes.length; i++) {
477
+ const node = filterFn(nodes[i]);
478
+
479
+ if (node !== null) {
480
+ result.push(node);
481
+ }
482
+ }
483
+
484
+ return result;
485
+ }
486
+ /**
487
+ * Appends the node before the first child of the parent node
488
+ * @param parent A parent node
489
+ * @param node Node that needs to be appended
490
+ */
491
+
492
+ function $insertFirst(parent, node) {
493
+ const firstChild = parent.getFirstChild();
494
+
495
+ if (firstChild !== null) {
496
+ firstChild.insertBefore(node);
497
+ } else {
498
+ parent.append(node);
499
+ }
500
+ }
483
501
 
484
502
  exports.$splitNode = lexical.$splitNode;
503
+ exports.isHTMLAnchorElement = lexical.isHTMLAnchorElement;
504
+ exports.isHTMLElement = lexical.isHTMLElement;
485
505
  exports.$dfs = $dfs;
506
+ exports.$filter = $filter;
486
507
  exports.$findMatchingParent = $findMatchingParent;
487
508
  exports.$getNearestBlockElementAncestorOrThrow = $getNearestBlockElementAncestorOrThrow;
488
509
  exports.$getNearestNodeOfType = $getNearestNodeOfType;
510
+ exports.$insertFirst = $insertFirst;
489
511
  exports.$insertNodeToNearestRoot = $insertNodeToNearestRoot;
490
512
  exports.$restoreEditorState = $restoreEditorState;
491
513
  exports.$wrapNodeInElement = $wrapNodeInElement;
492
514
  exports.addClassNamesToElement = addClassNamesToElement;
493
- exports.isHTMLAnchorElement = isHTMLAnchorElement;
494
- exports.isHTMLElement = isHTMLElement;
495
515
  exports.isMimeType = isMimeType;
496
516
  exports.mediaFileReader = mediaFileReader;
497
517
  exports.mergeRegister = mergeRegister;
@@ -4,14 +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){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())}};
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 r(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}exports.$splitNode=n.$splitNode;exports.isHTMLAnchorElement=n.isHTMLAnchorElement;exports.isHTMLElement=n.isHTMLElement;
9
+ exports.$dfs=function(a,b){let c=[];a=(a||n.$getRoot()).getLatest();b=b||(n.$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}),n.$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};
10
+ exports.$filter=function(a,b){let c=[];for(let e=0;e<a.length;e++){let d=b(a[e]);null!==d&&c.push(d)}return c};exports.$findMatchingParent=t;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};exports.$insertFirst=function(a,b){let c=a.getFirstChild();null!==c?c.insertBefore(b):a.append(b)};
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 e,d;n.$isTextNode(b)?(e=b.getParentOrThrow(),d=b.getIndexWithinParent(),0<c&&(d+=1,b.splitText(c))):(e=b,d=c);[,b]=n.$splitNode(e,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,e=a._pendingEditorState;for(let [d,f]of b._nodeMap){let h=g.$cloneWithProperties(f);n.$isTextNode(h)&&(h.__text=f.__text);c.set(d,h)}e&&(e._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(e=>""!==e),a.classList.add(...c))})};exports.isMimeType=r;
14
+ exports.mediaFileReader=function(a,b){let c=a[Symbol.iterator]();return new Promise((e,d)=>{let f=[],h=()=>{const {done:m,value:k}=c.next();if(m)return e(f);const l=new FileReader;l.addEventListener("error",d);l.addEventListener("load",()=>{const q=l.result;"string"===typeof q&&f.push({file:k,result:q});h()});r(k,b)?l.readAsDataURL(k):h()};h()})};exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}};
15
15
  exports.objectKlassEquals=function(a,b){return null!==a?Object.getPrototypeOf(a).constructor.name===b.name:!1};
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()}}})};
16
+ exports.registerNestedElementResolver=function(a,b,c,e){return a.registerNodeTransform(b,d=>{a:{var f=d.getChildren();for(var h=0;h<f.length;h++)if(f[h]instanceof b){f=null;break a}for(f=d;null!==f;)if(h=f,f=f.getParent(),f instanceof b){f={child:h,parent:f};break a}f=null}if(null!==f){const {child:m,parent:k}=f;if(m.is(d)){e(k,d);d=m.getNextSiblings();f=d.length;k.insertAfter(m);if(0!==f){h=c(k);m.insertAfter(h);for(let l=0;l<f;l++)h.append(d[l])}k.canBeEmpty()||0!==k.getChildrenSize()||k.remove()}}})};
17
17
  exports.removeClassNamesFromElement=function(a,...b){b.forEach(c=>{"string"===typeof c&&a.classList.remove(...c.split(" "))})}
package/index.d.ts CHANGED
@@ -6,8 +6,8 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  *
8
8
  */
9
- import { $splitNode, EditorState, ElementNode, Klass, LexicalEditor, LexicalNode } from 'lexical';
10
- export { $splitNode };
9
+ import { $splitNode, EditorState, ElementNode, isHTMLAnchorElement, isHTMLElement, Klass, LexicalEditor, LexicalNode } from 'lexical';
10
+ export { $splitNode, isHTMLAnchorElement, isHTMLElement };
11
11
  export type DFSNode = Readonly<{
12
12
  depth: number;
13
13
  node: LexicalNode;
@@ -147,16 +147,6 @@ export declare function $insertNodeToNearestRoot<T extends LexicalNode>(node: T)
147
147
  * @returns A new lexcial element with the previous node appended within (as a child, including its children).
148
148
  */
149
149
  export declare function $wrapNodeInElement(node: LexicalNode, createElementNode: () => ElementNode): ElementNode;
150
- /**
151
- * @param x - The element being tested
152
- * @returns Returns true if x is an HTML anchor tag, false otherwise
153
- */
154
- export declare function isHTMLAnchorElement(x: Node): x is HTMLAnchorElement;
155
- /**
156
- * @param x - The element being testing
157
- * @returns Returns true if x is an HTML element, false otherwise.
158
- */
159
- export declare function isHTMLElement(x: Node | EventTarget): x is HTMLElement;
160
150
  type ObjectKlass<T> = new (...args: any[]) => T;
161
151
  /**
162
152
  * @param object = The instance of the type
@@ -164,3 +154,16 @@ type ObjectKlass<T> = new (...args: any[]) => T;
164
154
  * @returns Whether the object is has the same Klass of the objectClass, ignoring the difference across window (e.g. different iframs)
165
155
  */
166
156
  export declare function objectKlassEquals<T>(object: unknown, objectClass: ObjectKlass<T>): boolean;
157
+ /**
158
+ * Filter the nodes
159
+ * @param nodes Array of nodes that needs to be filtered
160
+ * @param filterFn A filter function that returns node if the current node satisfies the condition otherwise null
161
+ * @returns Array of filtered nodes
162
+ */
163
+ export declare function $filter<T>(nodes: Array<LexicalNode>, filterFn: (node: LexicalNode) => null | T): Array<T>;
164
+ /**
165
+ * Appends the node before the first child of the parent node
166
+ * @param parent A parent node
167
+ * @param node Node that needs to be appended
168
+ */
169
+ export declare function $insertFirst(parent: ElementNode, node: LexicalNode): void;
package/package.json CHANGED
@@ -8,15 +8,15 @@
8
8
  "utils"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.12.0",
11
+ "version": "0.12.2",
12
12
  "main": "LexicalUtils.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.12.0"
14
+ "lexical": "0.12.2"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/list": "0.12.0",
18
- "@lexical/table": "0.12.0",
19
- "@lexical/selection": "0.12.0"
17
+ "@lexical/list": "0.12.2",
18
+ "@lexical/table": "0.12.2",
19
+ "@lexical/selection": "0.12.2"
20
20
  },
21
21
  "repository": {
22
22
  "type": "git",