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:
@@ -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`.
|
||||
*
|
||||
|
@@ -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.
|
||||
|
Reference in New Issue
Block a user