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:
5
.changeset/rotten-taxis-battle.md
Normal file
5
.changeset/rotten-taxis-battle.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Add `pass` option to `Editor.nodes`, which is passed through to `Node.nodes`.
|
@@ -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.
|
||||||
|
@@ -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 &&
|
||||||
|
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
35
packages/slate/test/interfaces/Editor/nodes/pass/block.tsx
Normal file
35
packages/slate/test/interfaces/Editor/nodes/pass/block.tsx
Normal 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],
|
||||||
|
],
|
||||||
|
]
|
@@ -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],
|
||||||
|
Reference in New Issue
Block a user