2020-05-10 13:50:12 +00:00
|
|
|
import { SplitNodeOperation } from 'slate'
|
|
|
|
|
2020-05-11 06:21:49 +00:00
|
|
|
import { SyncValue } from '../../model'
|
2020-05-10 13:50:12 +00:00
|
|
|
import { getParent, getChildren } from '../../path'
|
|
|
|
import { cloneNode } from '../../utils'
|
|
|
|
|
2020-05-11 06:21:49 +00:00
|
|
|
const splitNode = (doc: SyncValue, op: SplitNodeOperation): SyncValue => {
|
2020-05-10 13:50:12 +00:00
|
|
|
const [parent, index]: [any, number] = getParent(doc, op.path)
|
|
|
|
|
|
|
|
const target = getChildren(parent)[index]
|
2021-01-18 16:49:34 +00:00
|
|
|
const inject = {
|
|
|
|
...cloneNode(target),
|
|
|
|
...op.properties
|
|
|
|
}
|
2020-05-10 13:50:12 +00:00
|
|
|
|
|
|
|
if (target.text) {
|
|
|
|
target.text.length > op.position &&
|
|
|
|
target.text.deleteAt(op.position, target.text.length - op.position)
|
|
|
|
op.position && inject.text.deleteAt(0, op.position)
|
|
|
|
} else {
|
|
|
|
target.children.splice(op.position, target.children.length - op.position)
|
|
|
|
op.position && inject.children.splice(0, op.position)
|
|
|
|
}
|
|
|
|
|
|
|
|
getChildren(parent).insertAt(index + 1, inject)
|
|
|
|
|
|
|
|
return doc
|
|
|
|
}
|
|
|
|
|
|
|
|
export default splitNode
|