Use cached text nodes instead of NodeIterator

This commit is contained in:
Antonio Scandurra
2015-10-06 10:43:32 +02:00
parent 175c21f47e
commit 61892f932b
3 changed files with 18 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
LinesYardstick = require "../src/lines-yardstick"
{toArray} = require 'underscore-plus'
describe "LinesYardstick", ->
[editor, mockPresenter, mockLineNodesProvider, createdLineNodes, linesYardstick] = []
@@ -46,6 +47,13 @@ describe "LinesYardstick", ->
return if availableScreenRows[lineId] isnt screenRow
buildLineNode(screenRow)
textNodesForLineIdAndScreenRow: (lineId, screenRow) ->
lineNode = @lineNodeForLineIdAndScreenRow(lineId, screenRow)
textNodes = []
for span in lineNode.children
for textNode in span.childNodes
textNodes.push(textNode)
textNodes
editor.setLineHeightInPixels(14)
linesYardstick = new LinesYardstick(editor, mockPresenter, mockLineNodesProvider)

View File

@@ -86,3 +86,7 @@ class LinesComponent extends TiledComponent
lineNodeForLineIdAndScreenRow: (lineId, screenRow) ->
tile = @presenter.tileForRow(screenRow)
@getComponentForTile(tile)?.lineNodeForLineId(lineId)
textNodesForLineIdAndScreenRow: (lineId, screenRow) ->
tile = @presenter.tileForRow(screenRow)
@getComponentForTile(tile)?.textNodesForLineId(lineId)

View File

@@ -35,7 +35,7 @@ class LinesYardstick
return new Point(row, 0) unless lineNode? and line?
iterator = document.createNodeIterator(lineNode, NodeFilter.SHOW_TEXT)
textNodes = @lineNodesProvider.textNodesForLineIdAndScreenRow(line.id, row)
column = 0
previousColumn = 0
previousLeft = 0
@@ -55,13 +55,13 @@ class LinesYardstick
textIndex++
unless textNode?
textNode = iterator.nextNode()
textNode = textNodes.shift()
textNodeLength = textNode.textContent.length
textNodeIndex = 0
nextTextNodeIndex = textNodeLength
while nextTextNodeIndex <= column
textNode = iterator.nextNode()
textNode = textNodes.shift()
textNodeLength = textNode.textContent.length
textNodeIndex = nextTextNodeIndex
nextTextNodeIndex = textNodeIndex + textNodeLength
@@ -108,8 +108,8 @@ class LinesYardstick
if cachedPosition = @pixelPositionsByLineIdAndColumn[line.id]?[column]
return cachedPosition
textNodes = @lineNodesProvider.textNodesForLineIdAndScreenRow(line.id, row)
indexWithinTextNode = null
iterator = document.createNodeIterator(lineNode, NodeFilter.SHOW_TEXT)
charIndex = 0
@tokenIterator.reset(line)
@@ -130,13 +130,13 @@ class LinesYardstick
textIndex++
unless textNode?
textNode = iterator.nextNode()
textNode = textNodes.shift()
textNodeLength = textNode.textContent.length
textNodeIndex = 0
nextTextNodeIndex = textNodeLength
while nextTextNodeIndex <= charIndex
textNode = iterator.nextNode()
textNode = textNodes.shift()
textNodeLength = textNode.textContent.length
textNodeIndex = nextTextNodeIndex
nextTextNodeIndex = textNodeIndex + textNodeLength