1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-01 03:11:44 +02:00

Add pass option to Editor.nodes (#5843)

This commit is contained in:
Joe Anderson
2025-04-15 16:41:31 +01:00
committed by GitHub
parent 5adb8ededf
commit 63c0db5b86
6 changed files with 51 additions and 6 deletions

View File

@@ -0,0 +1,5 @@
---
'slate': minor
---
Add `pass` option to `Editor.nodes`, which is passed through to `Node.nodes`.

View File

@@ -141,7 +141,7 @@ Options: `depth?: number, edge?: 'start' | 'end'`
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. 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.
Options: `{at?: Location | Span, match?: NodeMatch, mode?: 'all' | 'highest' | 'lowest', universal?: boolean, reverse?: boolean, voids?: boolean}` Options: `{at?: Location | Span, match?: NodeMatch, mode?: 'all' | 'highest' | 'lowest', universal?: boolean, reverse?: boolean, voids?: boolean, pass?: (node: NodeEntry => boolean), ignoreNonSelectable?: boolean}`
`options.match`: Provide a value to the `match?` option to limit the `NodeEntry` objects that are returned. `options.match`: Provide a value to the `match?` option to limit the `NodeEntry` objects that are returned.
@@ -151,6 +151,8 @@ Options: `{at?: Location | Span, match?: NodeMatch, mode?: 'all' | 'highest' | '
- `'highest'`: in a hierarchy of nodes, only return the highest level matching nodes - `'highest'`: in a hierarchy of nodes, only return the highest level matching nodes
- `'lowest'`: in a hierarchy of nodes, only return the lowest level matching nodes - `'lowest'`: in a hierarchy of nodes, only return the lowest level matching nodes
`options.pass`: Skip the descendants of certain nodes (but not the nodes themselves).
#### `Editor.parent(editor: Editor, at: Location, options?) => NodeEntry<Ancestor>` #### `Editor.parent(editor: Editor, at: Location, options?) => NodeEntry<Ancestor>`
Get the parent node of a location. Get the parent node of a location.

View File

@@ -15,6 +15,7 @@ export function* nodes<T extends Node>(
universal = false, universal = false,
reverse = false, reverse = false,
voids = false, voids = false,
pass,
ignoreNonSelectable = false, ignoreNonSelectable = false,
} = options } = options
let { match } = options let { match } = options
@@ -44,7 +45,8 @@ export function* nodes<T extends Node>(
reverse, reverse,
from, from,
to, to,
pass: ([node]) => { pass: ([node, path]) => {
if (pass && pass([node, path])) return true
if (!Element.isElement(node)) return false if (!Element.isElement(node)) return false
if ( if (
!voids && !voids &&

View File

@@ -244,6 +244,7 @@ export interface EditorNodesOptions<T extends Node> {
universal?: boolean universal?: boolean
reverse?: boolean reverse?: boolean
voids?: boolean voids?: boolean
pass?: (entry: NodeEntry) => boolean
ignoreNonSelectable?: boolean ignoreNonSelectable?: boolean
} }

View File

@@ -0,0 +1,35 @@
/** @jsx jsx */
import { Editor } from 'slate'
import { jsx } from '../../../..'
export const input = (
<editor>
<block pass>
<block match>one</block>
</block>
<block>
<block match>two</block>
<block pass match>
three
</block>
</block>
</editor>
)
export const test = editor => {
return Array.from(
Editor.nodes(editor, {
at: [],
match: n => !!n.match,
pass: ([n]) => !!n.pass,
})
)
}
export const output = [
[<block match>two</block>, [1, 0]],
[
<block pass match>
three
</block>,
[1, 1],
],
]

View File

@@ -5,27 +5,27 @@ import { jsx } from 'slate-hyperscript'
export const input = ( export const input = (
<editor> <editor>
<element> <element>
<element> <element pass>
<text key="a" /> <text key="a" />
</element> </element>
</element> </element>
</editor> </editor>
) )
export const test = value => { export const test = value => {
return Array.from(Node.nodes(value, { pass: ([n, p]) => p.length > 1 })) return Array.from(Node.nodes(value, { pass: ([n]) => !!n.pass }))
} }
export const output = [ export const output = [
[input, []], [input, []],
[ [
<element> <element>
<element> <element pass>
<text key="a" /> <text key="a" />
</element> </element>
</element>, </element>,
[0], [0],
], ],
[ [
<element> <element pass>
<text key="a" /> <text key="a" />
</element>, </element>,
[0, 0], [0, 0],