Moving the cursor without holding shift clears selection

This commit is contained in:
Corey Johnson & Nathan Sobo 2012-01-27 12:42:33 -08:00
parent 3987fb78b7
commit 29e7a6d774
6 changed files with 91 additions and 18 deletions

View File

@ -1,5 +1,6 @@
Buffer = require 'buffer'
Editor = require 'editor'
Range = require 'range'
$ = require 'jquery'
_ = require 'underscore'
fs = require 'fs'
@ -218,7 +219,7 @@ describe "Editor", ->
expect(editor.getCursorPosition()).toEqual(lastPosition)
describe "selection creation", ->
describe "selection bindings", ->
selection = null
beforeEach ->
@ -242,6 +243,43 @@ describe "Editor", ->
expect(range.start).toEqual(row: 1, column: 6)
expect(range.end).toEqual(row: 1, column: 8)
editor.trigger keydownEvent('down', shiftKey: true)
range = selection.getRange()
expect(range.start).toEqual(row: 1, column: 6)
expect(range.end).toEqual(row: 2, column: 8)
editor.trigger keydownEvent('left', shiftKey: true)
range = selection.getRange()
expect(range.start).toEqual(row: 1, column: 6)
expect(range.end).toEqual(row: 2, column: 7)
editor.trigger keydownEvent('up', shiftKey: true)
range = selection.getRange()
expect(range.start).toEqual(row: 1, column: 6)
expect(range.end).toEqual(row: 1, column: 7)
describe "when the arrow keys are pressed without the shift modifier", ->
makeNonEmpty = ->
selection.setRange(new Range({row: 1, column: 2}, {row: 1, column: 5}))
expect(selection.isEmpty()).toBeFalsy()
it "clears the selection", ->
makeNonEmpty()
editor.trigger keydownEvent('right')
expect(selection.isEmpty()).toBeTruthy()
makeNonEmpty()
editor.trigger keydownEvent('left')
expect(selection.isEmpty()).toBeTruthy()
makeNonEmpty()
editor.trigger keydownEvent('up')
expect(selection.isEmpty()).toBeTruthy()
makeNonEmpty()
editor.trigger keydownEvent('down')
expect(selection.isEmpty()).toBeTruthy()
describe "when text input events are triggered on the hidden input element", ->
it "inserts the typed character at the cursor position, both in the buffer and the pre element", ->

View File

@ -19,7 +19,7 @@ describe "Selection", ->
expect(selection.anchor.getPosition()).toEqual range.start
expect(selection.cursor.getPosition()).toEqual range.end
fdescribe ".updateAppearence()", ->
describe ".updateAppearence()", ->
[charWidth, lineHeight] = []
beforeEach ->

View File

@ -1,5 +1,4 @@
fs = require 'fs'
{Document} = require 'ace/document'
module.exports =
class Buffer
@ -70,10 +69,10 @@ class Buffer
fs.write @path, @getText()
on: (eventName, handler) ->
@handlers ?= {}
@handlers[eventName] ?= []
@handlers[eventName].push(handler)
@eventHandlers ?= {}
@eventHandlers[eventName] ?= []
@eventHandlers[eventName].push(handler)
trigger: (eventName, data) ->
@handlers?[eventName]?.forEach (handler) -> handler(data)
trigger: (eventName, event) ->
@eventHandlers?[eventName]?.forEach (handler) -> handler(event)

View File

@ -21,6 +21,7 @@ class Cursor extends Template
@point = @editor.clipPosition(point)
@goalColumn = null
@selection.updateAppearance()
@trigger 'cursor:position-changed'
getPosition: -> _.clone(@point)

View File

@ -33,6 +33,9 @@ class Editor extends Template
down: 'move-down'
up: 'move-up'
'shift-right': 'select-right'
'shift-left': 'select-left'
'shift-up': 'select-up'
'shift-down': 'select-down'
enter: 'newline'
backspace: 'backspace'
@ -41,6 +44,9 @@ class Editor extends Template
@on 'move-down', => @moveCursorDown()
@on 'move-up', => @moveCursorUp()
@on 'select-right', => @selectRight()
@on 'select-left', => @selectLeft()
@on 'select-up', => @selectUp()
@on 'select-down', => @selectDown()
@on 'newline', => @insertNewline()
@on 'backspace', => @backspace()
@ -137,6 +143,9 @@ class Editor extends Template
getCursorColumn: -> @selection.getCursorColumn()
selectRight: -> @selection.selectRight()
selectLeft: -> @selection.selectLeft()
selectUp: -> @selection.selectUp()
selectDown: -> @selection.selectDown()
insertText: (text) -> @selection.insertText(text)
insertNewline: -> @selection.insertNewline()

View File

@ -9,12 +9,20 @@ class Selection extends Template
@div()
viewProperties:
anchor: null
modifyingSelection: null
regions: null
initialize: (editor) ->
@editor = editor
@cursor = Cursor.build(this).appendTo(this)
@regions = []
@cursor = Cursor.build(this).appendTo(this)
@cursor.on 'cursor:position-changed', =>
@clearSelection() unless @modifyingSelection
clearSelection: ->
@anchor = null
@updateAppearance()
bufferChanged: (e) ->
@cursor.setPosition(e.postRange.end)
@ -58,10 +66,16 @@ class Selection extends Template
region.remove() for region in @regions
@regions = []
getRange: ->
if @anchor
new Range(@anchor.getPosition(), @cursor.getPosition())
else
new Range(@cursor.getPosition(), @cursor.getPosition())
setRange: (range) ->
@cursor.setPosition(range.start)
@placeAnchor()
@cursor.setPosition(range.end)
@modifySelection =>
@cursor.setPosition(range.end)
insertText: (text) ->
@editor.buffer.change(@getRange(), text)
@ -84,11 +98,11 @@ class Selection extends Template
isEmpty: ->
@getRange().isEmpty()
getRange: ->
if @anchor
new Range(@anchor.getPosition(), @cursor.getPosition())
else
new Range(@cursor.getPosition(), @cursor.getPosition())
modifySelection: (fn) ->
@placeAnchor()
@modifyingSelection = true
fn()
@modifyingSelection = false
placeAnchor: ->
return if @anchor
@ -126,8 +140,20 @@ class Selection extends Template
@cursor.moveRight()
selectRight: ->
@placeAnchor()
@cursor.moveRight()
@modifySelection =>
@cursor.moveRight()
selectLeft: ->
@modifySelection =>
@cursor.moveLeft()
selectUp: ->
@modifySelection =>
@cursor.moveUp()
selectDown: ->
@modifySelection =>
@cursor.moveDown()
moveCursorToLineEnd: ->
@cursor.moveToLineEnd()