From 28220e7007adc232fa5fefae52c970d7a3531d3d Mon Sep 17 00:00:00 2001 From: Jinxuan Zhu Date: Fri, 23 Mar 2018 13:53:54 -0400 Subject: [PATCH] Fix normalize range of the insertTextAtRange (#1659) --- packages/slate/src/changes/at-range.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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) + } } /**