mirror of
https://github.com/atom/atom.git
synced 2026-02-11 15:14:59 -05:00
The `do`, `undo`, and `redo` methods on operations take an optional editSession argument, which can be used to determine the context in which they are being run. We restore selections on that edit session instead of the session where the operations originally occurred.
47 lines
1.0 KiB
CoffeeScript
47 lines
1.0 KiB
CoffeeScript
_ = require 'underscore'
|
|
|
|
module.exports =
|
|
|
|
class UndoManager
|
|
undoHistory: null
|
|
redoHistory: null
|
|
currentTransaction: null
|
|
|
|
constructor: ->
|
|
@startBatchCallCount = 0
|
|
@undoHistory = []
|
|
@redoHistory = []
|
|
|
|
pushOperation: (operation) ->
|
|
if @currentTransaction
|
|
@currentTransaction.push(operation)
|
|
else
|
|
@undoHistory.push([operation])
|
|
@redoHistory = []
|
|
operation.do?()
|
|
|
|
transact: (fn) ->
|
|
if @currentTransaction
|
|
fn()
|
|
else
|
|
@currentTransaction = []
|
|
fn()
|
|
@undoHistory.push(@currentTransaction) if @currentTransaction.length
|
|
@currentTransaction = null
|
|
|
|
undo: (editSession) ->
|
|
if batch = @undoHistory.pop()
|
|
opsInReverse = new Array(batch...)
|
|
opsInReverse.reverse()
|
|
op.undo?(editSession) for op in opsInReverse
|
|
@redoHistory.push batch
|
|
batch.oldSelectionRanges
|
|
|
|
redo: (editSession) ->
|
|
if batch = @redoHistory.pop()
|
|
for op in batch
|
|
op.do?(editSession)
|
|
op.redo?(editSession)
|
|
@undoHistory.push(batch)
|
|
batch.newSelectionRanges
|