@lexical/utils 0.11.3 → 0.12.1

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.
@@ -480,12 +480,49 @@ function isHTMLElement(x) {
480
480
  function objectKlassEquals(object, objectClass) {
481
481
  return object !== null ? Object.getPrototypeOf(object).constructor.name === objectClass.name : false;
482
482
  }
483
+ /**
484
+ * Filter the nodes
485
+ * @param nodes Array of nodes that needs to be filtered
486
+ * @param filterFn A filter function that returns node if the current node satisfies the condition otherwise null
487
+ * @returns Array of filtered nodes
488
+ */
489
+
490
+ function $filter(nodes, filterFn) {
491
+ const result = [];
492
+
493
+ for (let i = 0; i < nodes.length; i++) {
494
+ const node = filterFn(nodes[i]);
495
+
496
+ if (node !== null) {
497
+ result.push(node);
498
+ }
499
+ }
500
+
501
+ return result;
502
+ }
503
+ /**
504
+ * Appends the node before the first child of the parent node
505
+ * @param parent A parent node
506
+ * @param node Node that needs to be appended
507
+ */
508
+
509
+ function $insertFirst(parent, node) {
510
+ const firstChild = parent.getFirstChild();
511
+
512
+ if (firstChild !== null) {
513
+ firstChild.insertBefore(node);
514
+ } else {
515
+ parent.append(node);
516
+ }
517
+ }
483
518
 
484
519
  exports.$splitNode = lexical.$splitNode;
485
520
  exports.$dfs = $dfs;
521
+ exports.$filter = $filter;
486
522
  exports.$findMatchingParent = $findMatchingParent;
487
523
  exports.$getNearestBlockElementAncestorOrThrow = $getNearestBlockElementAncestorOrThrow;
488
524
  exports.$getNearestNodeOfType = $getNearestNodeOfType;
525
+ exports.$insertFirst = $insertFirst;
489
526
  exports.$insertNodeToNearestRoot = $insertNodeToNearestRoot;
490
527
  exports.$restoreEditorState = $restoreEditorState;
491
528
  exports.$wrapNodeInElement = $wrapNodeInElement;
@@ -6,12 +6,12 @@
6
6
  */
7
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
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())}};
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.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((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 r=l.result;"string"===typeof r&&f.push({file:k,result:r});h()});q(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
@@ -164,3 +164,16 @@ type ObjectKlass<T> = new (...args: any[]) => T;
164
164
  * @returns Whether the object is has the same Klass of the objectClass, ignoring the difference across window (e.g. different iframs)
165
165
  */
166
166
  export declare function objectKlassEquals<T>(object: unknown, objectClass: ObjectKlass<T>): boolean;
167
+ /**
168
+ * Filter the nodes
169
+ * @param nodes Array of nodes that needs to be filtered
170
+ * @param filterFn A filter function that returns node if the current node satisfies the condition otherwise null
171
+ * @returns Array of filtered nodes
172
+ */
173
+ export declare function $filter<T>(nodes: Array<LexicalNode>, filterFn: (node: LexicalNode) => null | T): Array<T>;
174
+ /**
175
+ * Appends the node before the first child of the parent node
176
+ * @param parent A parent node
177
+ * @param node Node that needs to be appended
178
+ */
179
+ 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.11.3",
11
+ "version": "0.12.1",
12
12
  "main": "LexicalUtils.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.11.3"
14
+ "lexical": "0.12.1"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/list": "0.11.3",
18
- "@lexical/table": "0.11.3",
19
- "@lexical/selection": "0.11.3"
17
+ "@lexical/list": "0.12.1",
18
+ "@lexical/table": "0.12.1",
19
+ "@lexical/selection": "0.12.1"
20
20
  },
21
21
  "repository": {
22
22
  "type": "git",