fn

useManualRefHistory

v0.0.14testeddemo

Manually-committed undo/redo history for a ref. Records snapshots only when commit() is called, with optional cloning, serialization, and a bounded capacity.

Examples

ts
const count = ref(0);
const { history, commit, undo, redo, canUndo } = useManualRefHistory(count);
count.value = 1;
commit();
undo(); // count.value === 0
ts
const state = ref({ items: [] });
const { commit, undo } = useManualRefHistory(state, { clone: true, capacity: 10 });

Demo

Loading demo…

Signature

ts
export function useManualRefHistory<Raw, Serialized = Raw>(
  source: Ref<Raw>,
  options: UseManualRefHistoryOptions<Raw, Serialized> ={ ... }

Type Parameters

Raw
Serialized= Raw

Parameters

ParameterTypeDescription
sourceRef<Raw>The ref whose value changes are tracked
options?UseManualRefHistoryOptions<Raw, Serialized>Options: capacity, clone, dump, parse, setSource

Returns

UseManualRefHistoryReturn<Raw, Serialized>History state plus commit, undo, redo, clear, and reset
PropertyTypeDescription
sourceRef<Raw>The tracked source ref.
historyComputedRef<Array<UseRefHistoryRecord<Serialized>>>All records, most recent first (the current last followed by the undo stack).
lastRef<UseRefHistoryRecord<Serialized>>The most recently committed record.
undoStackRef<Array<UseRefHistoryRecord<Serialized>>>Records available to undo to, most recent first.
redoStackRef<Array<UseRefHistoryRecord<Serialized>>>Records available to redo to, most recent first.
canUndoComputedRef<boolean>Whether there is at least one record to undo to.
canRedoComputedRef<boolean>Whether there is at least one record to redo to.
commit() => voidCapture the current source value as a new history record.
clear() => voidDrop every record from both stacks (keeps the current last).
undo() => voidRestore the previous record, moving the current one onto the redo stack.
redo() => voidReapply the most recently undone record.
reset() => voidRestore the source to the value of the current last record.