@lexical/utils 0.1.19 → 0.1.20

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.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
7
  */
8
- import type {LexicalNode} from 'lexical';
8
+ import type {LexicalNode, ElementNode} from 'lexical';
9
9
  export type DFSNode = $ReadOnly<{
10
10
  depth: number;
11
11
  node: LexicalNode;
@@ -37,3 +37,6 @@ declare function $findMatchingParent(
37
37
  ): LexicalNode | null;
38
38
  type Func = () => void;
39
39
  declare function mergeRegister(...func: Array<Func>): () => void;
40
+ declare function $getNearestBlockElementAncestorOrThrow(
41
+ startNode: LexicalNode,
42
+ ): ElementNode;
@@ -95,6 +95,17 @@ function $getNearestNodeOfType(node, klass) {
95
95
 
96
96
  return parent;
97
97
  }
98
+ function $getNearestBlockElementAncestorOrThrow(startNode) {
99
+ const blockNode = $findMatchingParent(startNode, node => lexical.$isElementNode(node) && !node.isInline());
100
+
101
+ if (!lexical.$isElementNode(blockNode)) {
102
+ {
103
+ throw Error(`Expected node ${startNode.__key} to have closest block element node.`);
104
+ }
105
+ }
106
+
107
+ return blockNode;
108
+ }
98
109
  function $findMatchingParent(startingNode, findFn) {
99
110
  let curr = startingNode;
100
111
 
@@ -116,6 +127,7 @@ function mergeRegister(...func) {
116
127
 
117
128
  exports.$dfs = $dfs;
118
129
  exports.$findMatchingParent = $findMatchingParent;
130
+ exports.$getNearestBlockElementAncestorOrThrow = $getNearestBlockElementAncestorOrThrow;
119
131
  exports.$getNearestNodeOfType = $getNearestNodeOfType;
120
132
  exports.addClassNamesToElement = addClassNamesToElement;
121
133
  exports.mergeRegister = mergeRegister;
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * @flow strict
8
8
  */
9
- import type {LexicalNode} from 'lexical';
9
+ import type {LexicalNode, ElementNode} from 'lexical';
10
10
  export type DFSNode = $ReadOnly<{
11
11
  depth: number,
12
12
  node: LexicalNode,
@@ -38,3 +38,6 @@ declare export function $findMatchingParent(
38
38
  ): LexicalNode | null;
39
39
  type Func = () => void;
40
40
  declare export function mergeRegister(...func: Array<Func>): () => void;
41
+ declare export function $getNearestBlockElementAncestorOrThrow(
42
+ startNode: LexicalNode,
43
+ ): ElementNode;
@@ -4,5 +4,7 @@
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
- var f=require("lexical");exports.$dfs=function(a,b){const c=[];a=(a||f.$getRoot()).getLatest();b=b||(f.$isElementNode(a)?a.getLastDescendant():a);for(var d=a,e=0;null!==(d=d.getParent());)e++;for(d=e;null!==a&&!a.is(b);)if(c.push({depth:d,node:a}),f.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),d++;else for(e=null;null===e&&null!==a;)e=a.getNextSibling(),null===e?(a=a.getParent(),d--):a=e;null!==a&&a.is(b)&&c.push({depth:d,node:a});return c};
8
- exports.$findMatchingParent=function(a,b){for(;a!==f.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null};exports.$getNearestNodeOfType=function(a,b){for(;null!=a&&!(a instanceof b);)a=a.getParent();return a};exports.addClassNamesToElement=function(a,...b){b.forEach(c=>{null!=c&&"string"===typeof c&&a.classList.add(...c.split(" "))})};exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}};exports.removeClassNamesFromElement=function(a,...b){b.forEach(c=>{a.classList.remove(...c.split(" "))})};
7
+ var f=require("lexical");function g(a,b){for(;a!==f.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null}
8
+ exports.$dfs=function(a,b){const c=[];a=(a||f.$getRoot()).getLatest();b=b||(f.$isElementNode(a)?a.getLastDescendant():a);for(var d=a,e=0;null!==(d=d.getParent());)e++;for(d=e;null!==a&&!a.is(b);)if(c.push({depth:d,node:a}),f.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),d++;else for(e=null;null===e&&null!==a;)e=a.getNextSibling(),null===e?(a=a.getParent(),d--):a=e;null!==a&&a.is(b)&&c.push({depth:d,node:a});return c};exports.$findMatchingParent=g;
9
+ exports.$getNearestBlockElementAncestorOrThrow=function(a){const b=g(a,c=>f.$isElementNode(c)&&!c.isInline());if(!f.$isElementNode(b))throw Error(`Expected node ${a.__key} to have closest block element node.`);return b};exports.$getNearestNodeOfType=function(a,b){for(;null!=a&&!(a instanceof b);)a=a.getParent();return a};exports.addClassNamesToElement=function(a,...b){b.forEach(c=>{null!=c&&"string"===typeof c&&a.classList.add(...c.split(" "))})};
10
+ exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}};exports.removeClassNamesFromElement=function(a,...b){b.forEach(c=>{a.classList.remove(...c.split(" "))})};
package/package.json CHANGED
@@ -8,14 +8,14 @@
8
8
  "utils"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.1.19",
11
+ "version": "0.1.20",
12
12
  "main": "LexicalUtils.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.1.19"
14
+ "lexical": "0.1.20"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/list": "0.1.19",
18
- "@lexical/table": "0.1.19"
17
+ "@lexical/list": "0.1.20",
18
+ "@lexical/table": "0.1.20"
19
19
  },
20
20
  "repository": {
21
21
  "type": "git",