From ee2c45408cba9ca827df3bab0696ba5cb5ace886 Mon Sep 17 00:00:00 2001 From: Ravi Lamkoti <44892121+RavenColEvol@users.noreply.github.com> Date: Thu, 26 Sep 2024 12:53:48 +0530 Subject: [PATCH] feat: add Node.getIf method (#5723) * feat: add Node.getIf support * chore: restructure get to use getIf --- .changeset/clean-icons-bow.md | 5 ++++ packages/slate/src/interfaces/node.ts | 23 +++++++++++++++---- .../slate/test/interfaces/Node/getIf/root.tsx | 17 ++++++++++++++ .../test/interfaces/Node/getIf/success.tsx | 19 +++++++++++++++ .../test/interfaces/Node/getIf/undefined.tsx | 15 ++++++++++++ 5 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 .changeset/clean-icons-bow.md create mode 100644 packages/slate/test/interfaces/Node/getIf/root.tsx create mode 100644 packages/slate/test/interfaces/Node/getIf/success.tsx create mode 100644 packages/slate/test/interfaces/Node/getIf/undefined.tsx diff --git a/.changeset/clean-icons-bow.md b/.changeset/clean-icons-bow.md new file mode 100644 index 0000000000..4b93c62f4c --- /dev/null +++ b/.changeset/clean-icons-bow.md @@ -0,0 +1,5 @@ +--- +'slate': patch +--- + +feat: add Node.getIf method diff --git a/packages/slate/src/interfaces/node.ts b/packages/slate/src/interfaces/node.ts index 05bea24e4c..6ab334e7fd 100644 --- a/packages/slate/src/interfaces/node.ts +++ b/packages/slate/src/interfaces/node.ts @@ -131,6 +131,11 @@ export interface NodeInterface { */ get: (root: Node, path: Path) => Node + /** + * Similar to get, but returns undefined if the node does not exist. + */ + getIf: (root: Node, path: Path) => Node | undefined + /** * Check if a descendant node exists at a specific path. */ @@ -389,17 +394,25 @@ export const Node: NodeInterface = { }, get(root: Node, path: Path): Node { + const node = Node.getIf(root, path) + if (node === undefined) { + throw new Error( + `Cannot find a descendant at path [${path}] in node: ${Scrubber.stringify( + root + )}` + ) + } + return node + }, + + getIf(root: Node, path: Path): Node | undefined { let node = root for (let i = 0; i < path.length; i++) { const p = path[i] if (Text.isText(node) || !node.children[p]) { - throw new Error( - `Cannot find a descendant at path [${path}] in node: ${Scrubber.stringify( - root - )}` - ) + return } node = node.children[p] diff --git a/packages/slate/test/interfaces/Node/getIf/root.tsx b/packages/slate/test/interfaces/Node/getIf/root.tsx new file mode 100644 index 0000000000..e42fe02fc2 --- /dev/null +++ b/packages/slate/test/interfaces/Node/getIf/root.tsx @@ -0,0 +1,17 @@ +/** @jsx jsx */ +import { Node } from 'slate' +import { jsx } from 'slate-hyperscript' +import { cloneDeep } from 'lodash' + +export const input = ( + + + + + +) +export const test = value => { + return Node.getIf(value, []) +} +export const skip = true // TODO: see https://github.com/ianstormtaylor/slate/pull/4188 +export const output = cloneDeep(input) diff --git a/packages/slate/test/interfaces/Node/getIf/success.tsx b/packages/slate/test/interfaces/Node/getIf/success.tsx new file mode 100644 index 0000000000..49879127a3 --- /dev/null +++ b/packages/slate/test/interfaces/Node/getIf/success.tsx @@ -0,0 +1,19 @@ +/** @jsx jsx */ +import { Node } from 'slate' +import { jsx } from 'slate-hyperscript' + +export const input = ( + + + + + +) +export const test = value => { + return Node.getIf(value, [0]) +} +export const output = ( + + + +) diff --git a/packages/slate/test/interfaces/Node/getIf/undefined.tsx b/packages/slate/test/interfaces/Node/getIf/undefined.tsx new file mode 100644 index 0000000000..c4c41331f9 --- /dev/null +++ b/packages/slate/test/interfaces/Node/getIf/undefined.tsx @@ -0,0 +1,15 @@ +/** @jsx jsx */ +import { Node } from 'slate' +import { jsx } from 'slate-hyperscript' + +export const input = ( + + + + + +) +export const test = value => { + return Node.getIf(value, [0, 0, 0]) +} +export const output = undefined