1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-16 20:24:01 +02:00

More efficient update of Selection.isBackward

This commit is contained in:
Soreine
2016-11-07 14:51:39 +01:00
parent 550a17cf26
commit d58d7a0e10
2 changed files with 29 additions and 6 deletions

View File

@@ -470,6 +470,31 @@ const Node = {
return descendant
},
/**
* True if the node has both descendants in that order, false
* otherwise. The order is depth-first, post-order.
*
* @param {String} key1
* @param {String} key2
* @return {Boolean} True if nodes are found in this order
*/
areDescendantSorted(key1, key2) {
let sorted
this.forEachDescendant(n => {
if (n.key === key1) {
sorted = true
return false
} else if (n.key === key2) {
sorted = false
return false
}
})
return sorted
},
/**
* Get the depth of a child node by `key`, with optional `startAt`.
*

View File

@@ -332,12 +332,10 @@ class Selection extends new Record(DEFAULTS) {
// If `isBackward` is not set, derive it.
if (isBackward == null) {
let texts = node.getTexts()
let anchorIndex = texts.indexOf(anchorNode)
let focusIndex = texts.indexOf(focusNode)
isBackward = anchorIndex == focusIndex
? anchorOffset > focusOffset
: anchorIndex > focusIndex
if (anchorKey === focusKey) {
isBackward = anchorOffset > focusOffset
} else {
isBackward = !node.areDescendantSorted(anchorKey, focusKey)
}
// Merge in any updated properties.