Add UndoManager.abort and .commit

When `UndoManager.transact` is called with no function, you later need
to `abort` or `commit` the transaction manually. This allows
transactions to last longer than the dynamic scope of the single
function passed to `transact`.
This commit is contained in:
Kevin Sawicki & Nathan Sobo
2013-01-04 11:52:39 -07:00
parent 428900fdba
commit e64047854d
2 changed files with 94 additions and 46 deletions

View File

@@ -29,19 +29,22 @@ class UndoManager
@clear()
transact: (fn) ->
safeFn = ->
shouldCommit = not @currentTransaction?
@currentTransaction ?= []
if fn
try
fn()
catch e
console.error e.stack
finally
@commit() if shouldCommit
if @currentTransaction
safeFn()
else
@currentTransaction = []
safeFn()
@undoHistory.push(@currentTransaction) if @currentTransaction?.length
@currentTransaction = null
commit: ->
@undoHistory.push(@currentTransaction) if @currentTransaction?.length
@currentTransaction = null
abort: ->
@commit()
@undo()
@redoHistory.pop()
undo: (editSession) ->
try