Handle changing ::scrollTop in TextEditorPresenter

Esse commit está contido em:
Nathan Sobo
2015-01-19 11:33:20 -07:00
commit 880e1ce1f0
2 arquivos alterados com 77 adições e 11 exclusões
+36
Ver Arquivo
@@ -78,3 +78,39 @@ describe "TextEditorPresenter", ->
}
# rows beyond the end of the content are not rendered
describe "when ::scrollTop changes", ->
it "updates the lines that are visible on screen", ->
presenter = new TextEditorPresenter(model: editor, clientHeight: 25, scrollTop: 0, lineHeight: 10, lineOverdrawMargin: 1)
presenter.setScrollTop(25)
line0 = editor.tokenizedLineForScreenRow(0)
expect(presenter.state.lines[line0.id]).toBeUndefined()
line1 = editor.tokenizedLineForScreenRow(1)
expectValues presenter.state.lines[line1.id], {
screenRow: 1
tokens: line1.tokens
top: 10 * 1
}
line2 = editor.tokenizedLineForScreenRow(2)
expectValues presenter.state.lines[line2.id], {
screenRow: 2
tokens: line2.tokens
top: 10 * 2
}
line3 = editor.tokenizedLineForScreenRow(3)
expectValues presenter.state.lines[line3.id], {
screenRow: 3
tokens: line3.tokens
top: 10 * 3
}
line4 = editor.tokenizedLineForScreenRow(4)
expectValues presenter.state.lines[line4.id], {
screenRow: 4
tokens: line4.tokens
top: 10 * 4
}
+41 -11
Ver Arquivo
@@ -6,15 +6,45 @@ class TextEditorPresenter
buildLinesState: ->
@state.lines = {}
startRow = Math.floor(@scrollTop / @lineHeight) - @lineOverdrawMargin
startRow = Math.max(0, startRow)
endRow = startRow + Math.ceil(@clientHeight / @lineHeight) + @lineOverdrawMargin
endRow = Math.min(@model.getScreenLineCount(), endRow)
@updateLinesState()
for line, i in @model.tokenizedLinesForScreenRows(startRow, endRow)
row = startRow + i
@state.lines[line.id] = {
screenRow: row
tokens: line.tokens
top: row * @lineHeight
}
updateLinesState: ->
visibleLineIds = {}
endRow = @getEndRow()
startRow = @getStartRow()
row = startRow
while row < endRow
line = @model.tokenizedLineForScreenRow(row)
visibleLineIds[line.id] = true
if @state.lines.hasOwnProperty(line.id)
@updateLineState(row, line)
else
@buildLineState(row, line)
row++
for id, line of @state.lines
unless visibleLineIds.hasOwnProperty(id)
delete @state.lines[id]
updateLineState: (row, line) ->
lineState = @state.lines[line.id]
lineState.screenRow = row
lineState.top = row * @lineHeight
buildLineState: (row, line) ->
@state.lines[line.id] =
screenRow: row
tokens: line.tokens
top: row * @lineHeight
getStartRow: ->
startRow = Math.floor(@scrollTop / @lineHeight) - @lineOverdrawMargin
Math.max(0, startRow)
getEndRow: ->
endRow = @getStartRow() + Math.ceil(@clientHeight / @lineHeight) + @lineOverdrawMargin
Math.min(@model.getScreenLineCount(), endRow)
setScrollTop: (@scrollTop) ->
@updateLinesState()