From dd3e9effff744d9bb58c0f80aa55b0a678c1645d Mon Sep 17 00:00:00 2001 From: Irwan Fario Subastian Date: Mon, 11 Jun 2018 09:31:36 +1000 Subject: [PATCH] InsertTextAtRange normalize (#1875) * fix normalization condition on insertTextAtRange * normalize if marks exists * add comments * add test for expanded insertion with mark --- packages/slate/src/changes/at-range.js | 9 +++-- .../insert-text/expanded-with-mark.js | 40 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 packages/slate/test/changes/at-current-range/insert-text/expanded-with-mark.js diff --git a/packages/slate/src/changes/at-range.js b/packages/slate/src/changes/at-range.js index ca5e5bc11..b98c5e7c1 100644 --- a/packages/slate/src/changes/at-range.js +++ b/packages/slate/src/changes/at-range.js @@ -863,8 +863,8 @@ Changes.insertTextAtRange = (change, range, text, marks, options = {}) => { } // PERF: Unless specified, don't normalize if only inserting text. - if (normalize !== undefined) { - normalize = range.isExpanded + if (normalize === undefined) { + normalize = range.isExpanded && marks.size !== 0 } change.insertTextByKey(key, offset, text, marks, { normalize: false }) @@ -877,7 +877,10 @@ Changes.insertTextAtRange = (change, range, text, marks, options = {}) => { const normalizeAncestor = ancestors.findLast(n => change.value.document.getDescendant(n.key) ) - change.normalizeNodeByKey(normalizeAncestor.key) + // it is possible that normalizeAncestor doesn't return any node + // on that case fallback to startKey to be normalized + const normalizeKey = normalizeAncestor ? normalizeAncestor.key : startKey + change.normalizeNodeByKey(normalizeKey) } } diff --git a/packages/slate/test/changes/at-current-range/insert-text/expanded-with-mark.js b/packages/slate/test/changes/at-current-range/insert-text/expanded-with-mark.js new file mode 100644 index 000000000..25a25d2d4 --- /dev/null +++ b/packages/slate/test/changes/at-current-range/insert-text/expanded-with-mark.js @@ -0,0 +1,40 @@ +/** @jsx h */ + +import h from '../../../helpers/h' + +/* + * This test makes sure a normalization happens on insertText on expandedRange + */ + +export default function(change) { + change.insertText('a') + change.insertText('b') +} + +export const input = ( + + + + + lorem + + ipsum + + + ipsum + + + +) + +export const output = ( + + + + + ab + + + + +)