mirror of
https://github.com/atom/atom.git
synced 2026-02-10 14:45:11 -05:00
Add UndoManager.prototype.transact
The `transact` method takes a function and batches all operations within that function as a single transaction to be undone and redone. The edit session now uses generic operations to restore selection state around transactions. The `undo` and `do` methods on operations are now optional. In addition, the undo manager now supports an optional `redo` method on an operation for code that should *only* be run on redo, and not when the operation is initially pushed. This is used by edit session to restore selection state after redo.
This commit is contained in:
@@ -177,12 +177,10 @@ class EditSession
|
||||
@insertText($native.readFromPasteboard())
|
||||
|
||||
undo: ->
|
||||
if ranges = @buffer.undo()
|
||||
@setSelectedBufferRanges(ranges)
|
||||
@buffer.undo()
|
||||
|
||||
redo: ->
|
||||
if ranges = @buffer.redo()
|
||||
@setSelectedBufferRanges(ranges)
|
||||
@buffer.redo()
|
||||
|
||||
foldSelection: ->
|
||||
selection.fold() for selection in @getSelections()
|
||||
@@ -234,10 +232,15 @@ class EditSession
|
||||
@tokenizedBuffer.toggleLineCommentsInRange(range)
|
||||
|
||||
mutateSelectedText: (fn) ->
|
||||
selections = @getSelections()
|
||||
@buffer.startUndoBatch(@getSelectedBufferRanges())
|
||||
fn(selection) for selection in selections
|
||||
@buffer.endUndoBatch(@getSelectedBufferRanges())
|
||||
@transact => fn(selection) for selection in @getSelections()
|
||||
|
||||
transact: (fn) ->
|
||||
@buffer.transact =>
|
||||
oldSelectedRanges = @getSelectedBufferRanges()
|
||||
@buffer.pushOperation(undo: => @setSelectedBufferRanges(oldSelectedRanges))
|
||||
fn()
|
||||
newSelectedRanges = @getSelectedBufferRanges()
|
||||
@buffer.pushOperation(redo: => @setSelectedBufferRanges(newSelectedRanges))
|
||||
|
||||
getAnchors: ->
|
||||
new Array(@anchors...)
|
||||
|
||||
Reference in New Issue
Block a user