diff --git a/packages/slate/src/transforms/node.ts b/packages/slate/src/transforms/node.ts index 41a88f21f..521f89d9e 100644 --- a/packages/slate/src/transforms/node.ts +++ b/packages/slate/src/transforms/node.ts @@ -487,6 +487,17 @@ export const NodeTransforms: NodeTransforms = { if (path.length !== 0) { editor.apply({ type: 'move_node', path, newPath }) } + + if ( + toRef.current && + Path.isSibling(newPath, path) && + Path.isAfter(newPath, path) + ) { + // When performing a sibling move to a later index, the path at the destination is shifted + // to before the insertion point instead of after. To ensure our group of nodes are inserted + // in the correct order we increment toRef to account for that + toRef.current = Path.next(toRef.current) + } } toRef.unref() diff --git a/packages/slate/test/transforms/moveNodes/selection/block-siblings-after.tsx b/packages/slate/test/transforms/moveNodes/selection/block-siblings-after.tsx new file mode 100644 index 000000000..37a6135fd --- /dev/null +++ b/packages/slate/test/transforms/moveNodes/selection/block-siblings-after.tsx @@ -0,0 +1,37 @@ +/** @jsx jsx */ +import { Editor, Transforms } from 'slate' +import { jsx } from '../../..' + +export const run = editor => { + Transforms.moveNodes(editor, { + match: n => Editor.isBlock(editor, n), + to: [2], + }) +} +export const input = ( + + + + one + + + two + + + three + +) + +export const output = ( + + three + + + one + + + two + + + +) diff --git a/packages/slate/test/transforms/moveNodes/selection/block-siblings-before.tsx b/packages/slate/test/transforms/moveNodes/selection/block-siblings-before.tsx new file mode 100644 index 000000000..7a6c478d8 --- /dev/null +++ b/packages/slate/test/transforms/moveNodes/selection/block-siblings-before.tsx @@ -0,0 +1,37 @@ +/** @jsx jsx */ +import { Editor, Transforms } from 'slate' +import { jsx } from '../../..' + +export const run = editor => { + Transforms.moveNodes(editor, { + match: n => Editor.isBlock(editor, n), + to: [0], + }) +} +export const input = ( + + one + + two + + + + three + + + +) + +export const output = ( + + + two + + + + three + + + one + +)