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.
25 lines
713 B
25 lines
713 B
import { MergeNodeOperation, Node } from 'slate'
|
|
|
|
import { SyncDoc } from '../../model'
|
|
import { getParent, getChildren } from '../../path'
|
|
import { toJS, cloneNode } from '../../utils'
|
|
|
|
const mergeNode = (doc: SyncDoc, op: MergeNodeOperation): SyncDoc => {
|
|
const [parent, index]: [any, number] = getParent(doc, op.path)
|
|
|
|
const prev = parent[index - 1] || parent.children[index - 1]
|
|
const next = parent[index] || parent.children[index]
|
|
|
|
if (prev.text) {
|
|
prev.text.insertAt(prev.text.length, ...toJS(next.text).split(''))
|
|
} else {
|
|
getChildren(next).forEach((n: Node) => getChildren(prev).push(cloneNode(n)))
|
|
}
|
|
|
|
getChildren(parent).deleteAt(index, 1)
|
|
|
|
return doc
|
|
}
|
|
|
|
export default mergeNode
|