C

UndoEngine

The main class to provide undo and redo functionality.
ImplementsInheritance Hierarchy

Remarks

How to enable the undo support
graph.undoEngineEnabled = true

The UndoEngine is in charge of recording, managing and executing single units of work that can be undone and redone, represented by the interface IUndoUnit.

How to get the UndoEngine from the IGraph
const engine = graph.undoEngine

These units are managed in a queue-like structure and can be added manually to the queue by calling the addUnit method.

How to add an undo unit to the engine
engine!.addUnit(myUnit)

Executing the units is done by client code or commands by calling undo or redo. The engine automatically manages the queue so that calling these two methods will consistently move the units in the queue.

The beginCompoundEdit method allows to record units by bracketing several changes in an ICompoundEdit. Implementations of the ICompoundEdit interface record all subsequent changes until it is committed and therefore all tecorded units are added to the engine. For more information and examples see ICompoundEdit.

Using an ICompoundEdit
const edit = engine!.beginCompoundEdit(undoName, redoName)

// add custom undo units or change the graph structure programmatically here

if (!success) {
  // if we don't want the changes to be done after all, then we need to cancel the edit
  edit.cancel()
}

Depending on the properties mergeUnits and autoMergeTime, the engine tries to collapse added IUndoUnits if possible. This enhances the performance and reduces the required memory for undo changes. For example, when a node is moved interactively, instead of saving every position that the node had when being dragged the engine will only hold one IUndoUnit at the end to undo the entire movement.

Customizing Undo/Redo

  • In general, to be able to undo or redo certain work, client code can implement IUndoUnit. The client application needs to encapsulate changes in this implementation and provide the logic to completely undo and redo these changes. Instances of your custom IUndoUnit implementations need to be added to the UndoEngine manually via addUnit.
  • In certain cases it is difficult or not efficient to track and save the changes between certain states, and better to save the states themselves and to restore these. For example when there are potentially many intermediate steps between two states of interest and it would be complicated to produce IUndoUnits for each intermediate step and merge them in the end. In such cases client code should implement IMementoSupport instead. See the documentation of IMementoSupport for examples and guidelines on how to implement this memento design pattern concept.
  • It is generally not necessary to subclass UndoEngine unless you want to customize the internal handling of the units, which is both complicated and prone to errors. In most use cases it is sufficient to provide custom IUndoUnits or IMementoSupport.

See Also

Developer's Guide

API

IUndoUnit, IMementoSupport, ICompoundEdit

Members

Show:

Constructors

Initializes a new instance of the UndoEngine class.

Properties

Gets or sets the duration during which the engine will try to merge newly added units.
If this is set to ZERO, the engine will try to merge incoming units depending on the mergeUnits property. Otherwise, if the time span between the last added unit and the new unit exceeds the set value, the engine does not try to merge the units regardless of what the mergeUnits property is set to. Likewise, if the time span is smaller than the set value, the engine will always try to merge units.
conversionfinal

See Also

Developer's Guide
API
mergeUnits
Gets or sets a value that indicates whether or not this instance should try to merge newly added units.
If true this instance will try to merge or replace units in the queue. Note that when the autoMergeTime property is set to ZERO and this property is set to true, the engine will always try to merge incoming units. Otherwise, the autoMergeTime dictates whether the engine will try to merge a unit or not.
final

See Also

Developer's Guide
API
autoMergeTime
Gets whether this instance is currently performing a redo operation.
readonlyfinal
Gets whether this instance is currently performing an undo operation.
readonlyfinal
Gets the redoName of the next redo operation.
readonlyfinal
Gets or sets the maximum size of the undo queue this instance is managing.
A size of 0 effectively disables this implementation.
final
Gets a token that can be used to store and compare the state of the undo queue.
For example, an application can retrieve the token once the user has saved their document. Comparing the token returned by this instance with another one retrieved at a later point in time enables the application to determine whether the document is in the same state.
readonly

Property Value

An object that can be checked against other tokens via a equals method.
Gets the undoName of the next undo operation.
readonlyfinal

Methods

Adds a new IUndoUnit to the queue.
This implementation will automatically group multiple units into a single unit if the time since the last add is less than autoMergeTime.

Parameters

unit: IUndoUnit
The unit of work to add.

See Also

Developer's Guide
Determines whether a call to redo can be made.
Determines whether a call to undo can be made.
Clears the internal queue and disposes all units in it.
This will trigger the corresponding property-changed event.
protected

Parameters

name: string
The name of the property that changed.
Redoes the next IUndoUnit.
Returns a string representation for this object.
Undoes the next IUndoUnit.

Events

Occurs when canUndo, canRedo, undoName, redoName, or token changed its value.
propertyName: string
The name of the changed property.
Occurs when the engine has successfully executed the redo operation of an IUndoUnit.

Properties of

EventArgs

See Also

Developer's Guide
Occurs when the engine has successfully executed the undo operation of an IUndoUnit.

Properties of

EventArgs

See Also

Developer's Guide