1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-27 09:04:31 +02:00

add previous/next sibling query support (#3290)

This commit is contained in:
Ian Storm Taylor
2019-12-10 16:10:06 -05:00
committed by GitHub
parent 5bddee673f
commit bad6472e13
3 changed files with 64 additions and 2 deletions

View File

@@ -343,7 +343,7 @@ export const LocationQueries = {
next( next(
editor: Editor, editor: Editor,
at: Location, at: Location,
match: NodeMatch, match?: NodeMatch,
options: { options: {
mode?: 'all' | 'highest' mode?: 'all' | 'highest'
voids?: boolean voids?: boolean
@@ -353,6 +353,20 @@ export const LocationQueries = {
const [, from] = Editor.last(editor, at) const [, from] = Editor.last(editor, at)
const [, to] = Editor.last(editor, []) const [, to] = Editor.last(editor, [])
const span: Span = [from, to] const span: Span = [from, to]
if (Path.isPath(at) && at.length === 0) {
throw new Error(`Cannot get the next node from the root node!`)
}
if (match == null) {
if (Path.isPath(at)) {
const [parent] = Editor.parent(editor, at)
match = ([n]) => parent.children.includes(n)
} else {
match = () => true
}
}
const [, next] = Editor.nodes(editor, { at: span, match, mode, voids }) const [, next] = Editor.nodes(editor, { at: span, match, mode, voids })
return next return next
}, },
@@ -678,7 +692,7 @@ export const LocationQueries = {
previous( previous(
editor: Editor, editor: Editor,
at: Location, at: Location,
match: NodeMatch, match?: NodeMatch,
options: { options: {
mode?: 'all' | 'highest' mode?: 'all' | 'highest'
voids?: boolean voids?: boolean
@@ -688,6 +702,20 @@ export const LocationQueries = {
const [, from] = Editor.first(editor, at) const [, from] = Editor.first(editor, at)
const [, to] = Editor.first(editor, []) const [, to] = Editor.first(editor, [])
const span: Span = [from, to] const span: Span = [from, to]
if (Path.isPath(at) && at.length === 0) {
throw new Error(`Cannot get the previous node from the root node!`)
}
if (match == null) {
if (Path.isPath(at)) {
const [parent] = Editor.parent(editor, at)
match = ([n]) => parent.children.includes(n)
} else {
match = () => true
}
}
const [, previous] = Editor.nodes(editor, { const [, previous] = Editor.nodes(editor, {
reverse: true, reverse: true,
at: span, at: span,

View File

@@ -0,0 +1,17 @@
/** @jsx jsx */
import { Editor } from 'slate'
import { jsx } from '../..'
export const input = (
<editor>
<block>one</block>
<block>two</block>
</editor>
)
export const run = editor => {
return Editor.next(editor, [0])
}
export const output = [<block>two</block>, [1]]

View File

@@ -0,0 +1,17 @@
/** @jsx jsx */
import { Editor } from 'slate'
import { jsx } from '../..'
export const input = (
<editor>
<block>one</block>
<block>two</block>
</editor>
)
export const run = editor => {
return Editor.previous(editor, [1])
}
export const output = [<block>one</block>, [0]]