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:
@@ -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
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user