1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-17 20:51:20 +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 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`. * 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` is not set, derive it.
if (isBackward == null) { if (isBackward == null) {
let texts = node.getTexts() if (anchorKey === focusKey) {
let anchorIndex = texts.indexOf(anchorNode) isBackward = anchorOffset > focusOffset
let focusIndex = texts.indexOf(focusNode) } else {
isBackward = anchorIndex == focusIndex isBackward = !node.areDescendantSorted(anchorKey, focusKey)
? anchorOffset > focusOffset
: anchorIndex > focusIndex
} }
// Merge in any updated properties. // Merge in any updated properties.