You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
621 B
27 lines
621 B
import { MoveNodeOperation } from 'slate'
|
|
|
|
import { cloneNode } from '../../utils'
|
|
import { SyncValue } from '../../model'
|
|
import { getParent, getChildren } from '../../path'
|
|
|
|
const moveNode = (doc: SyncValue, op: MoveNodeOperation): SyncValue => {
|
|
const [from, fromIndex] = getParent(doc, op.path)
|
|
const [to, toIndex] = getParent(doc, op.newPath)
|
|
|
|
if (from.text || to.text) {
|
|
throw new TypeError("Can't move node as child of a text node")
|
|
}
|
|
|
|
getChildren(to).splice(
|
|
toIndex,
|
|
0,
|
|
...getChildren(from)
|
|
.splice(fromIndex, 1)
|
|
.map(cloneNode)
|
|
)
|
|
|
|
return doc
|
|
}
|
|
|
|
export default moveNode
|