diff --git a/packages/slate/src/changes/at-range.js b/packages/slate/src/changes/at-range.js index 1bae929ba..28f7c85e1 100644 --- a/packages/slate/src/changes/at-range.js +++ b/packages/slate/src/changes/at-range.js @@ -851,8 +851,19 @@ Changes.insertTextAtRange = (change, range, text, marks, options = {}) => { if (normalize !== undefined) { normalize = range.isExpanded } + change.insertTextByKey(key, offset, text, marks, { normalize: false }) - change.insertTextByKey(key, offset, text, marks, { normalize }) + if (normalize) { + // normalize in the narrowest existing block that originally contains startKey and endKey + const commonAncestor = document.getCommonAncestor(startKey, range.endKey) + const ancestors = document + .getAncestors(commonAncestor.key) + .push(commonAncestor) + const normalizeAncestor = ancestors.findLast(n => + change.value.document.getDescendant(n.key) + ) + change.normalizeNodeByKey(normalizeAncestor.key) + } } /**