1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-11 17:53:59 +02:00

InsertTextAtRange normalize (#1875)

* fix normalization condition on insertTextAtRange

* normalize if marks exists

* add comments

* add test for expanded insertion with mark
This commit is contained in:
Irwan Fario Subastian
2018-06-11 09:31:36 +10:00
committed by Ian Storm Taylor
parent 178fc78f1a
commit dd3e9effff
2 changed files with 46 additions and 3 deletions

View File

@@ -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)
}
}

View File

@@ -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 = (
<value>
<document>
<paragraph>
<b>
<anchor />lorem
</b>
ipsum
</paragraph>
<paragraph>
ipsum<focus />
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<b>
ab<cursor />
</b>
</paragraph>
</document>
</value>
)