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),
    )

Undo/redo state wrapping a model.

pub opaque type UndoStack(model)

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 can_redo(stack: UndoStack(model)) -> Bool

Check if redo is available.

pub fn can_undo(stack: UndoStack(model)) -> Bool

Check if undo is available.

pub fn current(stack: UndoStack(model)) -> model

Get the current model.

pub fn new(model: model) -> UndoStack(model)

Create a new undo stack with initial model.

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).

Search Document