Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get node performance list normalization #1707

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
};