mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-27 17:09:53 +02:00
feat: add Node.getIf method (#5723)
* feat: add Node.getIf support * chore: restructure get to use getIf
This commit is contained in:
5
.changeset/clean-icons-bow.md
Normal file
5
.changeset/clean-icons-bow.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'slate': patch
|
||||
---
|
||||
|
||||
feat: add Node.getIf method
|
@@ -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]
|
||||
|
17
packages/slate/test/interfaces/Node/getIf/root.tsx
Normal file
17
packages/slate/test/interfaces/Node/getIf/root.tsx
Normal file
@@ -0,0 +1,17 @@
|
||||
/** @jsx jsx */
|
||||
import { Node } from 'slate'
|
||||
import { jsx } from 'slate-hyperscript'
|
||||
import { cloneDeep } from 'lodash'
|
||||
|
||||
export const input = (
|
||||
<editor>
|
||||
<element>
|
||||
<text />
|
||||
</element>
|
||||
</editor>
|
||||
)
|
||||
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)
|
19
packages/slate/test/interfaces/Node/getIf/success.tsx
Normal file
19
packages/slate/test/interfaces/Node/getIf/success.tsx
Normal file
@@ -0,0 +1,19 @@
|
||||
/** @jsx jsx */
|
||||
import { Node } from 'slate'
|
||||
import { jsx } from 'slate-hyperscript'
|
||||
|
||||
export const input = (
|
||||
<editor>
|
||||
<element>
|
||||
<text />
|
||||
</element>
|
||||
</editor>
|
||||
)
|
||||
export const test = value => {
|
||||
return Node.getIf(value, [0])
|
||||
}
|
||||
export const output = (
|
||||
<element>
|
||||
<text />
|
||||
</element>
|
||||
)
|
15
packages/slate/test/interfaces/Node/getIf/undefined.tsx
Normal file
15
packages/slate/test/interfaces/Node/getIf/undefined.tsx
Normal file
@@ -0,0 +1,15 @@
|
||||
/** @jsx jsx */
|
||||
import { Node } from 'slate'
|
||||
import { jsx } from 'slate-hyperscript'
|
||||
|
||||
export const input = (
|
||||
<editor>
|
||||
<element>
|
||||
<text />
|
||||
</element>
|
||||
</editor>
|
||||
)
|
||||
export const test = value => {
|
||||
return Node.getIf(value, [0, 0, 0])
|
||||
}
|
||||
export const output = undefined
|
Reference in New Issue
Block a user