plushie/undo
Undo/redo support for reversible operations.
Each command has an apply function (model -> model) and an undo
function (model -> model). Apply a command to push it onto the undo
stack; undo/redo move commands between stacks while updating the model.
Commands with the same coalesce_key that arrive within
coalesce_window_ms of each other are merged into a single undo
entry. The merged entry keeps the original undo function (so one undo
reverses all coalesced changes) and composes the apply functions.
Types
A reversible command.
pub type UndoCommand(model) {
UndoCommand(
apply: fn(model) -> model,
undo: fn(model) -> model,
label: String,
coalesce_key: option.Option(String),
coalesce_window_ms: option.Option(Int),
)
}
Constructors
-
UndoCommand( apply: fn(model) -> model, undo: fn(model) -> model, label: String, coalesce_key: option.Option(String), coalesce_window_ms: option.Option(Int), )
Values
pub fn apply(
stack: UndoStack(model),
cmd: UndoCommand(model),
) -> UndoStack(model)
Apply a command: execute it, push to undo stack, clear redo stack.
If the command carries a coalesce_key that matches the top of the
undo stack and the time delta is within coalesce_window_ms, the
entry is merged rather than pushed.
pub fn redo(stack: UndoStack(model)) -> UndoStack(model)
Redo the last undone command. Returns unchanged if nothing to redo.
pub fn redo_history(stack: UndoStack(model)) -> List(String)
Get redo history labels (most recent first).
pub fn undo(stack: UndoStack(model)) -> UndoStack(model)
Undo the last command. Returns unchanged if nothing to undo.
pub fn undo_history(stack: UndoStack(model)) -> List(String)
Get undo history labels (most recent first).