1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 02:19:52 +02:00

Handle sibling moves in moveNodes transform (#3978)

* Handle sibling moves better in move nodes.
This commit is contained in:
Steve Marquis
2021-02-16 09:27:58 -08:00
committed by GitHub
parent e4936c3f32
commit 5201a1856f
3 changed files with 85 additions and 0 deletions

View File

@@ -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()

View File

@@ -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 = (
<editor>
<block>
<anchor />
one
</block>
<block>
two
<focus />
</block>
<block>three</block>
</editor>
)
export const output = (
<editor>
<block>three</block>
<block>
<anchor />
one
</block>
<block>
two
<focus />
</block>
</editor>
)

View File

@@ -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 = (
<editor>
<block>one</block>
<block>
two
<anchor />
</block>
<block>
three
<focus />
</block>
</editor>
)
export const output = (
<editor>
<block>
two
<anchor />
</block>
<block>
three
<focus />
</block>
<block>one</block>
</editor>
)