Skip to content

Undo & Redo

Keep the undo stack yourself with BlowHistory: capture the graph after each edit, labelled with what it was, and restore the snapshot Undo or Redo hands back. For an app that already has an undo stack, or wants to decide what one step is; the canvas can also keep one for you (see Built-in Undo & Redo).

0 back · 0 forward · capacity 10 Edit
Start
Middle
End
bit Blow

Try it

  • Drag a node, drag from a handle to connect two, or select something and press Delete: each is one step, and the Undo button names it - move, connect, delete. Moving five selected nodes together is one step too.
  • Undo and Redo walk the steps; the view stays where you put it. Make an edit after undoing and the redo steps are gone, as in every editor.
  • Add node and Scramble are steps as well. Press Scramble more than ten times: the step count stops at ten, because the oldest step is dropped.
  • Simulate a remote edit moves Middle to the right as another user's change would. Undo as far as you like: the other steps come back, but Middle stays where the remote edit put it.
  • Clear history forgets every step and starts again from the graph as it is.

How it works

  • new BlowHistory(capacity: 10) keeps up to ten steps back; past that the oldest is dropped. Capacity, UndoDepth, RedoDepth, CanUndo and CanRedo drive the toolbar.
  • Capture(nodes, edges, viewport, label) records the live graph as a step, copying only the nodes and edges that changed since the last one. It returns false and records nothing when nothing changed - selecting is not an edit - so capturing liberally is harmless and Undo always does something.
  • The page captures in OnGraphChanged, raised once per completed edit with a BlowGraphChange whose Label (move, connect, delete, add, ...) becomes the step's label; UndoLabel and RedoLabel name the steps either side. A change of kind Replaced is the page's own restore, and is not captured. Handling OnNodeDragStop, OnConnect and OnDelete would do as well.
  • Add node goes through AddNodes, so it arrives in OnGraphChanged like a user's edit. Scramble moves the nodes directly, which the canvas cannot see, so the page captures it itself, labelled scramble.
  • Undo() and Redo() step through the history and return the BlowSnapshot to apply, or null. RestoreAsync(snapshot, restoreViewport: false) puts the graph back in place, as copies, and leaves the viewport alone.
  • Rebase(nodes, edges, viewport) writes a change nobody should undo into every step, back and forward; a step whose whole effect it overwrote is dropped. Clear() forgets everything, so the page captures the starting point again - as OnInit does, or the first undo would have nothing to return to.
An unhandled error has occurred. Reload 🗙