Don't move the cursor on buffer change unless the editor is focused

Add a boolean isFocused flag to editor that is assigned when the hidden input gains / loses focus. This makes it easier in specs because we can treat the editor as if its focused without having to add it to the DOM. It's also a bit more abstract.
This commit is contained in:
Corey Johnson & Nathan Sobo 2012-03-19 09:04:39 -06:00
parent 2903126047
commit 9bd6751a46
5 changed files with 27 additions and 2 deletions

View File

@ -16,6 +16,7 @@ describe "Editor", ->
editor.autoIndent = false editor.autoIndent = false
editor.enableKeymap() editor.enableKeymap()
editor.setBuffer(buffer) editor.setBuffer(buffer)
editor.isFocused = true
describe "text rendering", -> describe "text rendering", ->
it "creates a line element for each line in the buffer with the html-escaped text of the line", -> it "creates a line element for each line in the buffer with the html-escaped text of the line", ->
@ -769,6 +770,11 @@ describe "Editor", ->
expect(editor.getCursorScreenPosition()).toEqual(row: 1, column: 7) expect(editor.getCursorScreenPosition()).toEqual(row: 1, column: 7)
expect(editor.lines.find('.line:eq(1)')).toHaveText editor.getCurrentBufferLine() expect(editor.lines.find('.line:eq(1)')).toHaveText editor.getCurrentBufferLine()
it "does not update the cursor position if the editor is not focused", ->
editor.isFocused = false
editor.buffer.insert([5, 0], 'blah')
expect(editor.getCursorScreenPosition()).toEqual [0, 0]
describe "when there is a selection", -> describe "when there is a selection", ->
it "replaces the selected text with the typed text", -> it "replaces the selected text with the typed text", ->
editor.selection.setBufferRange(new Range([1, 6], [2, 4])) editor.selection.setBufferRange(new Range([1, 6], [2, 4]))
@ -925,6 +931,16 @@ describe "Editor", ->
expect(editor).not.toMatchSelector ':focus' expect(editor).not.toMatchSelector ':focus'
expect(editor.hiddenInput).toMatchSelector ':focus' expect(editor.hiddenInput).toMatchSelector ':focus'
describe "when the hidden input is focused / unfocused", ->
it "assigns the isFocused flag on the editor", ->
editor.attachToDom()
editor.isFocused = false
editor.hiddenInput.focus()
expect(editor.isFocused).toBeTruthy()
editor.hiddenInput.focusout()
expect(editor.isFocused).toBeFalsy()
describe "construction", -> describe "construction", ->
it "assigns an empty buffer and correctly handles text input (regression coverage)", -> it "assigns an empty buffer and correctly handles text input (regression coverage)", ->
editor = new Editor editor = new Editor

View File

@ -35,7 +35,7 @@ describe "RootView", ->
describe "split editor panes", -> describe "split editor panes", ->
describe "when split-right is triggered on the editor", -> describe "when split-right is triggered on the editor", ->
fit "places the a new editor to the right of the current editor in a .horizontal div, and focuses the new editor", -> it "places the a new editor to the right of the current editor in a .horizontal div, and focuses the new editor", ->
rootView.attachToDom() rootView.attachToDom()
expect(rootView.find('.horizontal')).not.toExist() expect(rootView.find('.horizontal')).not.toExist()

View File

@ -10,6 +10,7 @@ describe "Selection", ->
editor = new Editor editor = new Editor
editor.enableKeymap() editor.enableKeymap()
editor.setBuffer(buffer) editor.setBuffer(buffer)
editor.isFocused = true
selection = editor.selection selection = editor.selection
describe ".setBufferRange(range)", -> describe ".setBufferRange(range)", ->

View File

@ -7,6 +7,7 @@ describe "VimMode", ->
beforeEach -> beforeEach ->
editor = new Editor editor = new Editor
editor.enableKeymap() editor.enableKeymap()
editor.isFocused = true
vimMode = new VimMode(editor) vimMode = new VimMode(editor)
describe "initialize", -> describe "initialize", ->

View File

@ -37,6 +37,7 @@ class Editor extends View
renderer: null renderer: null
autoIndent: null autoIndent: null
lineCache: null lineCache: null
isFocused: false
initialize: ({buffer}) -> initialize: ({buffer}) ->
requireStylesheet 'editor.css' requireStylesheet 'editor.css'
@ -105,6 +106,9 @@ class Editor extends View
@hiddenInput.focus() @hiddenInput.focus()
false false
@hiddenInput.on 'focus', => @isFocused = true
@hiddenInput.on 'focusout', => @isFocused = false
@on 'mousedown', '.fold-placeholder', (e) => @on 'mousedown', '.fold-placeholder', (e) =>
@destroyFold($(e.currentTarget).attr('foldId')) @destroyFold($(e.currentTarget).attr('foldId'))
false false
@ -178,7 +182,7 @@ class Editor extends View
@loadEditSessionForBuffer(@buffer) @loadEditSessionForBuffer(@buffer)
@buffer.on "change.editor#{@id}", (e) => @cursor.bufferChanged(e) @buffer.on "change.editor#{@id}", (e) => @handleBufferChange(e)
@renderer.on 'change', (e) => @handleRendererChange(e) @renderer.on 'change', (e) => @handleRendererChange(e)
loadEditSessionForBuffer: (buffer) -> loadEditSessionForBuffer: (buffer) ->
@ -192,6 +196,9 @@ class Editor extends View
@editSession.scrollTop = @scrollTop() @editSession.scrollTop = @scrollTop()
@editSession.scrollLeft = @horizontalScroller.scrollLeft() @editSession.scrollLeft = @horizontalScroller.scrollLeft()
handleBufferChange: (e) ->
@cursor.bufferChanged(e) if @isFocused
handleRendererChange: (e) -> handleRendererChange: (e) ->
{ oldRange, newRange } = e { oldRange, newRange } = e
unless newRange.isSingleLine() and newRange.coversSameRows(oldRange) unless newRange.isSingleLine() and newRange.coversSameRows(oldRange)