Fix issues with selections when switching between edit sessions

SelectionViews now update their appearance immediately when constructed. We can't assume they're empty. CursorView doesn't do a blanket `off()` call to its model when it's removed anymore, which was screwing up selection updates when switching back. Only attach selections / cursors when the editor is attached, and extract everything we do into a `renderWhenAttached` method.
This commit is contained in:
Nathan Sobo
2012-06-11 22:01:27 -06:00
parent b51ab212f6
commit 4e74f1bf2e
6 changed files with 80 additions and 71 deletions

View File

@@ -13,13 +13,12 @@ class CursorView extends View
hidden: false
initialize: (@cursor, @editor) ->
@cursor.on 'change-screen-position', (position, options) =>
@cursor.on 'change-screen-position.cursor-view', (position, { bufferChange }) =>
@updateAppearance()
unless options.bufferChange
@removeIdleClassTemporarily()
@trigger 'cursor-move', bufferChange: options.bufferChange
@removeIdleClassTemporarily() unless bufferChange
@trigger 'cursor-move', bufferChange: bufferChange
@cursor.on 'destroy', => @remove()
@cursor.on 'destroy.cursor-view', => @remove()
afterAttach: (onDom) ->
return unless onDom
@@ -28,7 +27,7 @@ class CursorView extends View
remove: ->
@editor.removeCursorView(this)
@cursor.off()
@cursor.off('.cursor-view')
super
updateAppearance: ->

View File

@@ -224,11 +224,11 @@ class Editor extends View
@calculateDimensions()
@hiddenInput.width(@charWidth)
@setSoftWrapColumn() if @softWrap
@prepareForScrolling()
@setScrollPositionFromActiveEditSession() # this also renders the visible lines
@activeEditSession.on 'screen-lines-change', (e) => @handleRendererChange(e)
$(window).on "resize.editor#{@id}", => @updateRenderedLines()
@focus() if @isFocused
@renderWhenAttached()
@trigger 'editor-open', [this]
addCursorView: (cursor) ->
@@ -382,7 +382,6 @@ class Editor extends View
if @activeEditSession
@saveActiveEditSession()
@removeAllCursorViews()
@activeEditSession.off()
@activeEditSession = @editSessions[index]
@@ -394,24 +393,22 @@ class Editor extends View
@trigger 'editor-path-change'
@renderer = @activeEditSession.renderer
@renderWhenAttached()
if @attached
@prepareForScrolling()
@setScrollPositionFromActiveEditSession()
@renderLines()
@activeEditSession.on 'screen-lines-change', (e) => @handleRendererChange(e)
renderWhenAttached: ->
return unless @attached
for cursor in @activeEditSession.getCursors()
@addCursorView(cursor)
@removeAllCursorAndSelectionViews()
@addCursorView(cursor) for cursor in @activeEditSession.getCursors()
@addSelectionView(selection) for selection in @activeEditSession.getSelections()
@activeEditSession.on 'add-cursor', (cursor) => @addCursorView(cursor)
@activeEditSession.on 'add-selection', (selection) => @addSelectionView(selection)
for selection in @activeEditSession.getSelections()
@addSelectionView(selection)
@prepareForScrolling()
@setScrollPositionFromActiveEditSession()
@activeEditSession.on 'add-cursor', (cursor) =>
@addCursorView(cursor)
@activeEditSession.on 'add-selection', (selection) =>
@addSelectionView(selection)
@renderLines()
@activeEditSession.on 'screen-lines-change', (e) => @handleRendererChange(e)
destroyEditSessions: ->
session.destroy() for session in @editSessions
@@ -421,7 +418,6 @@ class Editor extends View
@scrollView.scrollLeft(@activeEditSession.scrollLeft ? 0)
saveActiveEditSession: ->
@activeEditSession.setCursorScreenPosition(@getCursorScreenPosition())
@activeEditSession.setScrollTop(@scrollTop())
@activeEditSession.setScrollLeft(@scrollView.scrollLeft())
@@ -678,8 +674,9 @@ class Editor extends View
getCursorViews: ->
new Array(@cursorViews...)
removeAllCursorViews: ->
removeAllCursorAndSelectionViews: ->
cursorView.remove() for cursorView in @getCursorViews()
selectionView.remove() for selectionView in @getSelectionViews()
getCursor: (index) -> @activeEditSession.getCursor(index)
getCursors: -> @activeEditSession.getCursors()
@@ -705,6 +702,9 @@ class Editor extends View
index ?= @selectionViews.length - 1
@selectionViews[index]
getSelectionViews: ->
new Array(@selectionViews...)
getSelection: (index) -> @activeEditSession.getSelection(index)
getSelections: -> @activeEditSession.getSelections()
getSelectionsOrderedByBufferPosition: -> @activeEditSession.getSelectionsOrderedByBufferPosition()

View File

@@ -15,6 +15,7 @@ class SelectionView extends View
@regions = []
@selection.on 'change-screen-range', => @updateAppearance()
@selection.on 'destroy', => @remove('ignore')
@updateAppearance()
updateAppearance: ->
@clearRegions()
@@ -54,6 +55,6 @@ class SelectionView extends View
getScreenRange: ->
@selection.getScreenRange()
remove: (ignore) ->
remove: ->
@editor.removeSelectionView(this)
super