Skip to content

Undo / redo

Editing through the canvas is undoable out of the box. There's nothing to switch on — Ctrl+Z undoes, Ctrl+Y (or Ctrl+Shift+Z) redoes, and you can drive it from code when you need to:

csharp
canvas.Undo();
canvas.Redo();
bool canUndo = canvas.CanUndo;             // handy for enabling a toolbar button
canvas.HistoryChanged += () => RefreshToolbar();

What's recorded

Behind the canvas, a DiagramHistory watches the diagram and records the edits worth undoing:

That covers adding nodes and links, finishing a drag, deleting (including the links a deleted node took with it), z-order changes, group/ungroup operations, and bend-point add/remove. Anything the history itself does while undoing or redoing is deliberately not re-recorded, and the diagram's starting contents aren't undoable — only the edits you make from there.

A gesture is one step

A whole pointer gesture collapses into a single undo. Drag five selected nodes and one Ctrl+Z puts all five back, not one at a time. You can fold your own bulk edits into a single step the same way:

csharp
// auto-layout (or any batch reposition) becomes one undo
canvas.RunAsUndoableMove(() => LayeredLayout.Arrange(diagram));

Under the covers that opens a transaction, runs your change, and records the net movement as one command.

Grouping and ordering

Canvas helpers record their work automatically:

csharp
canvas.GroupSelection();
canvas.UngroupSelection();
canvas.ToggleGroupingSelection();

canvas.BringSelectionToFront();
canvas.SendSelectionToBack();

These are the methods to call from toolbar buttons and context-menu actions when you want the normal undo stack to stay coherent.

Going lower

The whole thing is built on a small command model in Nodely.CommandsIDiagramCommand, UndoRedoStack, and concrete commands like AddNodeCommand, MoveNodeCommand, SetModelOrdersCommand, AddGroupCommand, and CompositeCommand. You can drive that directly for a headless or custom editor, but for the normal canvas you won't usually need to.

Released under the MIT License.