Skip to content

Commit 63c0db5

Browse files
authored
Add pass option to Editor.nodes (#5843)
1 parent 5adb8ed commit 63c0db5

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed

.changeset/rotten-taxis-battle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'slate': minor
3+
---
4+
5+
Add `pass` option to `Editor.nodes`, which is passed through to `Node.nodes`.

docs/api/nodes/editor.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Options: `depth?: number, edge?: 'start' | 'end'`
141141

142142
At any given `Location` or `Span` in the editor provided by `at` (default is the current selection), the method returns a Generator of `NodeEntry` objects that represent the nodes that include `at`. At the top of the hierarchy is the `Editor` object itself.
143143

144-
Options: `{at?: Location | Span, match?: NodeMatch, mode?: 'all' | 'highest' | 'lowest', universal?: boolean, reverse?: boolean, voids?: boolean}`
144+
Options: `{at?: Location | Span, match?: NodeMatch, mode?: 'all' | 'highest' | 'lowest', universal?: boolean, reverse?: boolean, voids?: boolean, pass?: (node: NodeEntry => boolean), ignoreNonSelectable?: boolean}`
145145

146146
`options.match`: Provide a value to the `match?` option to limit the `NodeEntry` objects that are returned.
147147

@@ -151,6 +151,8 @@ Options: `{at?: Location | Span, match?: NodeMatch, mode?: 'all' | 'highest' | '
151151
- `'highest'`: in a hierarchy of nodes, only return the highest level matching nodes
152152
- `'lowest'`: in a hierarchy of nodes, only return the lowest level matching nodes
153153

154+
`options.pass`: Skip the descendants of certain nodes (but not the nodes themselves).
155+
154156
#### `Editor.parent(editor: Editor, at: Location, options?) => NodeEntry<Ancestor>`
155157

156158
Get the parent node of a location.

packages/slate/src/editor/nodes.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function* nodes<T extends Node>(
1515
universal = false,
1616
reverse = false,
1717
voids = false,
18+
pass,
1819
ignoreNonSelectable = false,
1920
} = options
2021
let { match } = options
@@ -44,7 +45,8 @@ export function* nodes<T extends Node>(
4445
reverse,
4546
from,
4647
to,
47-
pass: ([node]) => {
48+
pass: ([node, path]) => {
49+
if (pass && pass([node, path])) return true
4850
if (!Element.isElement(node)) return false
4951
if (
5052
!voids &&

packages/slate/src/interfaces/editor.ts

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ export interface EditorNodesOptions<T extends Node> {
244244
universal?: boolean
245245
reverse?: boolean
246246
voids?: boolean
247+
pass?: (entry: NodeEntry) => boolean
247248
ignoreNonSelectable?: boolean
248249
}
249250

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/** @jsx jsx */
2+
import { Editor } from 'slate'
3+
import { jsx } from '../../../..'
4+
5+
export const input = (
6+
<editor>
7+
<block pass>
8+
<block match>one</block>
9+
</block>
10+
<block>
11+
<block match>two</block>
12+
<block pass match>
13+
three
14+
</block>
15+
</block>
16+
</editor>
17+
)
18+
export const test = editor => {
19+
return Array.from(
20+
Editor.nodes(editor, {
21+
at: [],
22+
match: n => !!n.match,
23+
pass: ([n]) => !!n.pass,
24+
})
25+
)
26+
}
27+
export const output = [
28+
[<block match>two</block>, [1, 0]],
29+
[
30+
<block pass match>
31+
three
32+
</block>,
33+
[1, 1],
34+
],
35+
]

packages/slate/test/interfaces/Node/nodes/pass.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@ import { jsx } from 'slate-hyperscript'
55
export const input = (
66
<editor>
77
<element>
8-
<element>
8+
<element pass>
99
<text key="a" />
1010
</element>
1111
</element>
1212
</editor>
1313
)
1414
export const test = value => {
15-
return Array.from(Node.nodes(value, { pass: ([n, p]) => p.length > 1 }))
15+
return Array.from(Node.nodes(value, { pass: ([n]) => !!n.pass }))
1616
}
1717
export const output = [
1818
[input, []],
1919
[
2020
<element>
21-
<element>
21+
<element pass>
2222
<text key="a" />
2323
</element>
2424
</element>,
2525
[0],
2626
],
2727
[
28-
<element>
28+
<element pass>
2929
<text key="a" />
3030
</element>,
3131
[0, 0],

0 commit comments

Comments
 (0)