1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 18:39:51 +02:00

Speed up getting blocks at a range

Old implementation was far too slow if a selection included many nodes.
The performance hit occurred when converting each and every text node
into a block.  This is now avoided by using the same logic as
getTextsAtRangeAsArray for getBlocksAtRangeAsArray, but using blocks
directly.
This commit is contained in:
jacobbloomCDD
2017-08-08 11:13:11 -07:00
committed by Jacob Bloom
parent b4883ea552
commit e0d993617d

View File

@@ -254,9 +254,19 @@ const Node = {
*/ */
getBlocksAtRangeAsArray(range) { getBlocksAtRangeAsArray(range) {
return this range = range.normalize(this)
.getTextsAtRangeAsArray(range) const { startKey, endKey } = range
.map(text => this.getClosestBlock(text.key)) const startBlock = this.getClosestBlock(startKey)
// PERF: the most common case is when the range is in a single block node,
// where we can avoid a lot of iterating of the tree.
if (startKey == endKey) return [startBlock]
const endBlock = this.getClosestBlock(endKey)
const blocks = this.getBlocksAsArray()
const start = blocks.indexOf(startBlock)
const end = blocks.indexOf(endBlock)
return blocks.slice(start, end + 1)
}, },
/** /**