Cache line elements in Editor.

Shaves off 30ms
This commit is contained in:
Corey Johnson
2012-03-09 15:30:21 -08:00
parent 8e97fbd5ed
commit f7be856f2f

View File

@@ -35,6 +35,7 @@ class Editor extends View
renderer: null
undoManager: null
autoIndent: null
lineCache: null
initialize: () ->
requireStylesheet 'editor.css'
@@ -44,6 +45,7 @@ class Editor extends View
@handleEvents()
@setBuffer(new Buffer)
@autoIndent = true
@lineCache = []
bindKeys: ->
window.keymap.bindKeys '*:not(.editor *)',
@@ -149,9 +151,12 @@ class Editor extends View
@raw ' ' if appendNbsp
renderLines: ->
@lineCache = []
@lines.find('.line').remove()
for screenLine in @getScreenLines()
@lines.append @buildLineElement(screenLine)
line = @buildLineElement(screenLine)
@lineCache.push line
@lines.append line
getScreenLines: ->
@renderer.getLines()
@@ -201,21 +206,29 @@ class Editor extends View
@updateLineElement(row, screenLines.shift())
updateLineElement: (row, screenLine) ->
@getLineElement(row).replaceWith(@buildLineElement(screenLine))
if @lineCache.length == 0
@insertLineElement(row, screenLine)
else
line = @buildLineElement(screenLine)
@lineCache[row].replaceWith(line)
@lineCache[row] = line
insertLineElement: (row, screenLine) ->
newLineElement = @buildLineElement(screenLine)
insertBefore = @getLineElement(row)
if insertBefore.length
if insertBefore
@lineCache.splice(row, 0, newLineElement)
insertBefore.before(newLineElement)
else
@lines.append(newLineElement)
@lineCache.push newLineElement
@lines.append newLineElement
removeLineElement: (row) ->
@getLineElement(row).remove()
[lineElement] = @lineCache.splice(row, 1)
lineElement.remove()
getLineElement: (row) ->
$(@lines[0].querySelectorAll("div.line")[row])
@lineCache[row]
toggleSoftWrap: ->
@setSoftWrap(not @softWrap)