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.
cudr_slate-collaborative/packages/bridge/src/apply/node/splitNode.ts

28 lines
827 B

import { SplitNodeOperation } from 'slate'
import { SyncDoc } from '../../model'
import { getParent, getChildren } from '../../path'
import { cloneNode } from '../../utils'
const splitNode = (doc: SyncDoc, op: SplitNodeOperation): SyncDoc => {
const [parent, index]: [any, number] = getParent(doc, op.path)
const target = getChildren(parent)[index]
const inject = cloneNode(target)
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