Shift-meta-z to redo.

This commit is contained in:
Nathan Sobo
2012-02-07 12:10:14 -07:00
parent b3d1291643
commit dd04475756
4 changed files with 62 additions and 10 deletions

View File

@@ -51,6 +51,7 @@ class Editor extends View
'meta-c': 'copy'
'meta-v': 'paste'
'meta-z': 'undo'
'meta-Z': 'redo'
@on 'move-right', => @moveCursorRight()
@on 'move-left', => @moveCursorLeft()
@@ -67,6 +68,7 @@ class Editor extends View
@on 'copy', => @copySelection()
@on 'paste', => @paste()
@on 'undo', => @undo()
@on 'redo', => @redo()
buildCursorAndSelection: ->
@cursor = new Cursor(this)
@@ -244,3 +246,6 @@ class Editor extends View
undo: ->
@undoManager.undo()
redo: ->
@undoManager.redo()

View File

@@ -1,17 +1,30 @@
module.exports =
class UndoManager
undoHistory: null
undoInProgress: null
redoHistory: null
preserveHistory: false
constructor: (@buffer) ->
@undoHistory = []
@redoHistory = []
@buffer.on 'change', (op) =>
@undoHistory.push(op) unless @undoInProgress
unless @preserveHistory
@undoHistory.push(op)
@redoHistory = []
undo: ->
return unless @undoHistory.length
op = @undoHistory.pop()
@undoInProgress = true
@buffer.change op.newRange, op.oldText
@undoInProgress = false
if op = @undoHistory.pop()
@preservingHistory =>
@buffer.change op.newRange, op.oldText
@redoHistory.push op
redo: ->
if op = @redoHistory.pop()
@preservingHistory =>
@buffer.change op.oldRange, op.newText
@undoHistory.push op
preservingHistory: (fn) ->
@preserveHistory = true
fn()
@preserveHistory = false