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:
@@ -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()
|
||||
|
@@ -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>
|
||||
)
|
@@ -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>
|
||||
)
|
Reference in New Issue
Block a user