diff --git a/src/transforms/at-range.js b/src/transforms/at-range.js index fc2b1b289..deccb472a 100644 --- a/src/transforms/at-range.js +++ b/src/transforms/at-range.js @@ -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 } diff --git a/src/transforms/by-key.js b/src/transforms/by-key.js index f1d1ac724..5c2964872 100644 --- a/src/transforms/by-key.js +++ b/src/transforms/by-key.js @@ -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 }