diff --git a/packages/slate/src/interfaces/editor/queries/location.ts b/packages/slate/src/interfaces/editor/queries/location.ts index fcc3e2b95..402ee5fab 100644 --- a/packages/slate/src/interfaces/editor/queries/location.ts +++ b/packages/slate/src/interfaces/editor/queries/location.ts @@ -343,7 +343,7 @@ export const LocationQueries = { next( editor: Editor, at: Location, - match: NodeMatch, + match?: NodeMatch, options: { mode?: 'all' | 'highest' voids?: boolean @@ -353,6 +353,20 @@ export const LocationQueries = { const [, from] = Editor.last(editor, at) const [, to] = Editor.last(editor, []) 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 }) return next }, @@ -678,7 +692,7 @@ export const LocationQueries = { previous( editor: Editor, at: Location, - match: NodeMatch, + match?: NodeMatch, options: { mode?: 'all' | 'highest' voids?: boolean @@ -688,6 +702,20 @@ export const LocationQueries = { const [, from] = Editor.first(editor, at) const [, to] = Editor.first(editor, []) 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, { reverse: true, at: span, diff --git a/packages/slate/test/queries/next/default.js b/packages/slate/test/queries/next/default.js new file mode 100644 index 000000000..4ad5b5cc5 --- /dev/null +++ b/packages/slate/test/queries/next/default.js @@ -0,0 +1,17 @@ +/** @jsx jsx */ + +import { Editor } from 'slate' +import { jsx } from '../..' + +export const input = ( + + one + two + +) + +export const run = editor => { + return Editor.next(editor, [0]) +} + +export const output = [two, [1]] diff --git a/packages/slate/test/queries/previous/default.js b/packages/slate/test/queries/previous/default.js new file mode 100644 index 000000000..32b7b03df --- /dev/null +++ b/packages/slate/test/queries/previous/default.js @@ -0,0 +1,17 @@ +/** @jsx jsx */ + +import { Editor } from 'slate' +import { jsx } from '../..' + +export const input = ( + + one + two + +) + +export const run = editor => { + return Editor.previous(editor, [1]) +} + +export const output = [one, [0]]