1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-18 13:11:17 +02:00

handle joining text nodes with insertFragment

This commit is contained in:
Ian Storm Taylor
2016-10-14 17:22:45 -07:00
parent 8cf0a05926
commit 1443968b83
2 changed files with 18 additions and 3 deletions

View File

@@ -347,13 +347,12 @@ export function insertFragmentAtRange(transform, range, fragment) {
const inlineIndex = startBlock.nodes.indexOf(inlineChild)
firstBlock.nodes.forEach((inline, i) => {
const offset = startOffset == 0 ? 0 : 1
const newIndex = inlineIndex + i + offset
const o = startOffset == 0 ? 0 : 1
const newIndex = inlineIndex + i + o
transform.insertNodeByKey(startBlock.key, newIndex, inline)
})
}
transform.normalizeDocument()
return transform
}

View File

@@ -56,6 +56,22 @@ export function insertNodeByKey(transform, key, index, node) {
}
}
// If the node is a text node, and it is insert next to a text node, it should
// be joined with it.
if (node.kind == 'text') {
const parent = document.assertDescendant(key)
const previous = index == 0 ? null : parent.nodes.get(index - 1)
const next = parent.nodes.get(index)
if (next && next.kind == 'text') {
transform.joinNodeByKey(next.key, node.key)
}
if (previous && previous.kind == 'text') {
transform.joinNodeByKey(node.key, previous.key)
}
}
return transform
}