-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get node performance list normalization (#1707)
* improve performance of list normalizations * add changeset * fix return type * better error handling * better error handling, take 3
- Loading branch information
Showing
2 changed files
with
21 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@udecode/plate-core': patch | ||
--- | ||
|
||
improve performance of list normalizations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,31 @@ | ||
import { Node, Path } from 'slate'; | ||
import { Path } from 'slate'; | ||
import { isText } from '../text'; | ||
import { NodeOf, TNode } from './TNode'; | ||
|
||
/** | ||
* Get the descendant node referred to by a specific path. | ||
* If the path is an empty array, it refers to the root node itself. | ||
* If the node is not found, return null. | ||
* Based on Slate get and has, performance optimization without overhead of | ||
* stringify on throwing | ||
*/ | ||
export const getNode = <N extends NodeOf<R>, R extends TNode = TNode>( | ||
root: R, | ||
path: Path | ||
) => { | ||
try { | ||
return Node.get(root, path) as N; | ||
} catch (err) { | ||
for (let i = 0; i < path.length; i++) { | ||
const p = path[i]; | ||
|
||
if (isText(root) || !root.children[p]) { | ||
return null; | ||
} | ||
|
||
root = root.children[p] as R; | ||
} | ||
|
||
return root as N; | ||
} catch (e) { | ||
return null; | ||
} | ||
}; |
156f0e6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
plate – ./
plate.udecode.io
plate-git-main-udecode.vercel.app
plate-udecode.vercel.app
www.plate.udecode.io