mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-18 21:21:21 +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)
|
const inlineIndex = startBlock.nodes.indexOf(inlineChild)
|
||||||
|
|
||||||
firstBlock.nodes.forEach((inline, i) => {
|
firstBlock.nodes.forEach((inline, i) => {
|
||||||
const offset = startOffset == 0 ? 0 : 1
|
const o = startOffset == 0 ? 0 : 1
|
||||||
const newIndex = inlineIndex + i + offset
|
const newIndex = inlineIndex + i + o
|
||||||
transform.insertNodeByKey(startBlock.key, newIndex, inline)
|
transform.insertNodeByKey(startBlock.key, newIndex, inline)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
transform.normalizeDocument()
|
|
||||||
return transform
|
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
|
return transform
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user