Skip to content

Commit

Permalink
Get node performance list normalization (#1707)
Browse files Browse the repository at this point in the history
* improve performance of list normalizations

* add changeset

* fix return type

* better error handling

* better error handling, take 3
  • Loading branch information
dylans authored Jul 25, 2022
1 parent 9b894ae commit 156f0e6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-clocks-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-core': patch
---

improve performance of list normalizations
19 changes: 16 additions & 3 deletions packages/core/src/slate/node/getNode.ts
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;
}
};

1 comment on commit 156f0e6

@vercel
Copy link

@vercel vercel bot commented on 156f0e6 Jul 25, 2022

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

Please sign in to comment.