1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-01-19 06:18:16 +01:00

add Editor.unsetNodes and Node.children

This commit is contained in:
Ian Storm Taylor 2019-12-07 14:52:18 -05:00
parent 0856dd7115
commit 39d19b0f71
5 changed files with 124 additions and 0 deletions

View File

@ -630,6 +630,34 @@ export const NodeTransforms = {
})
},
/**
* Unset properties on the nodes at a location.
*/
unsetNodes(
editor: Editor,
props: string | string[],
options: {
at?: Location
match?: NodeMatch
mode?: 'all' | 'highest'
split?: boolean
voids?: boolean
} = {}
) {
if (!Array.isArray(props)) {
props = [props]
}
const obj = {}
for (const key of props) {
obj[key] = null
}
Editor.setNodes(editor, obj, options)
}
/**
* Unwrap the nodes at a location from a parent node, splitting the parent if
* necessary to ensure that only the content in the range is unwrapped.

View File

@ -77,6 +77,30 @@ export const Node = {
return c
},
/**
* Iterate over the children of a node at a specific path.
*/
*children(
root: Node,
path: Path,
options: {
reverse?: boolean
} = {}
): Iterable<DescendantEntry> {
const { reverse = false } = options
const ancestor = Node.ancestor(root, path)
const { children } = ancestor
let index = reverse ? children.length - 1 : 0
while (reverse ? index >= 0 : index < children.length) {
const child = Node.child(ancestor, index)
const childPath = path.concat(index)
yield [child, childPath]
index = reverse ? index - 1 : index + 1
}
},
/**
* Find the closest matching node entry starting from a specific path.
*/

View File

@ -0,0 +1,22 @@
/** @jsx jsx */
import { Node } from 'slate'
import { jsx } from 'slate-hyperscript'
export const input = (
<editor>
<element>
<text key="a" />
<text key="b" />
</element>
</editor>
)
export const test = value => {
return Array.from(Node.children(value, [0]))
}
export const output = [
[<text key="a" />, [0, 0]],
[<text key="b" />, [0, 1]],
]

View File

@ -0,0 +1,22 @@
/** @jsx jsx */
import { Node } from 'slate'
import { jsx } from 'slate-hyperscript'
export const input = (
<editor>
<element>
<text key="a" />
<text key="b" />
</element>
</editor>
)
export const test = value => {
return Array.from(Node.children(value, [0], { reverse: true }))
}
export const output = [
[<text key="b" />, [0, 1]],
[<text key="a" />, [0, 0]],
]

View File

@ -0,0 +1,28 @@
/** @jsx jsx */
import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.unsetNodes(editor, 'key', { match: 'text' })
}
export const input = (
<editor>
<block>
<text key>
<cursor />
word
</text>
</block>
</editor>
)
export const output = (
<editor>
<block>
<cursor />
word
</block>
</editor>
)