Add save command and keyboard shortcuts to editor.

This commit is contained in:
Corey Johnson 2012-03-07 12:43:29 -08:00
parent fabe8fd609
commit f6414c543c
2 changed files with 56 additions and 4 deletions

View File

@ -163,9 +163,6 @@ describe "Editor", ->
expect(editor.gutter.find('.line-number:eq(3)').text()).toBe '4'
expect(editor.gutter.find('.line-number:eq(4)').text()).toBe '7'
describe "cursor movement", ->
describe ".setCursorScreenPosition({row, column})", ->
beforeEach ->
@ -906,3 +903,49 @@ describe "Editor", ->
expect(editor.lines.find('.line:eq(5)').text()).toBe ' current = items.shift();'
expect(editor.getCursorBufferPosition()).toEqual [4, 29]
describe ".save()", ->
describe "when the current buffer has a path", ->
tempFilePath = null
beforeEach ->
tempFilePath = '/tmp/temp.txt'
expect(fs.remove(tempFilePath))
editor.setBuffer new Buffer(tempFilePath)
expect(editor.buffer.path).toBe tempFilePath
it "saves the current buffer to disk", ->
editor.buffer.setText 'Edited!'
expect(fs.exists(tempFilePath)).toBeFalsy()
editor.save()
expect(fs.exists(tempFilePath)).toBeTruthy()
expect(fs.read(tempFilePath)).toBe 'Edited!'
describe "when the current buffer has no path", ->
selectedFilePath = null
beforeEach ->
editor.setBuffer new Buffer()
expect(editor.buffer.path).toBeUndefined()
editor.buffer.setText 'Save me to a new path'
spyOn($native, 'saveDialog').andCallFake -> selectedFilePath
it "presents a 'save as' dialog", ->
editor.save()
expect($native.saveDialog).toHaveBeenCalled()
describe "when a path is chosen", ->
it "saves the buffer to the chosen path", ->
selectedFilePath = '/tmp/temp.txt'
editor.save()
expect(fs.exists(selectedFilePath)).toBeTruthy()
expect(fs.read(selectedFilePath)).toBe 'Save me to a new path'
describe "when dialog is cancelled", ->
it "does not save the buffer", ->
selectedFilePath = null
editor.save()
expect(fs.exists(selectedFilePath)).toBeFalsy()

View File

@ -46,6 +46,7 @@ class Editor extends View
bindKeys: ->
window.keymap.bindKeys '*:not(.editor *)',
'meta-s': 'save'
right: 'move-right'
left: 'move-left'
down: 'move-down'
@ -56,7 +57,7 @@ class Editor extends View
'shift-down': 'select-down'
enter: 'newline'
backspace: 'backspace'
delete: 'delete'
'delete': 'delete'
'meta-x': 'cut'
'meta-c': 'copy'
'meta-v': 'paste'
@ -65,6 +66,7 @@ class Editor extends View
'alt-meta-w': 'toggle-soft-wrap'
'alt-meta-f': 'fold-selection'
@on 'save', => @save()
@on 'move-right', => @moveCursorRight()
@on 'move-left', => @moveCursorLeft()
@on 'move-down', => @moveCursorDown()
@ -237,6 +239,13 @@ class Editor extends View
@removeClass 'soft-wrap'
$(window).off 'resize', @_setMaxLineLength
save: ->
if not @buffer.path
path = $native.saveDialog()
return if not path
@buffer.path = path
@buffer.save()
clipScreenPosition: (screenPosition, options={}) ->
@renderer.clipScreenPosition(screenPosition, options)