@rasamjs/history
v0.3.2
Published
Automatic model undo and redo for rasam drawings.
Readme
@rasamjs/history
Automatic undo and redo for a rasam drawing. Attribute, transform, text, resource, and structural edits share one stack. Transformer gestures are grouped automatically.
import { SVG } from '@rasamjs/core';
import { History } from '@rasamjs/history';
const drawing = SVG(document.querySelector<HTMLElement>('#drawing')!);
const rect = drawing.rect({ width: 20, height: 10 });
const history = new History(drawing, { limit: 100 });
rect.fill('red');
rect.size(100, 50);
history.transaction(() => rect.stroke('blue', 3), { label: 'Change stroke' });
history.undo();
history.redo();Construct one recorder on the owning drawing root. Existing nodes and future nodes,
including definitions, are observed. Importing the package needs no document.
@rasamjs/core is a peer dependency; the transformer is optional.
Use transaction() for synchronous compound edits. For a control spanning multiple
browser events, use begin(), edit.run(), and edit.commit() or edit.cancel().
Callbacks must be synchronous. While recording is active, a transaction failure
restores its savepoint; arbitrary application side effects are outside that rollback.
In the failed state, transactions run without rollback, so changes made before a
callback throws remain applied.
While any edit is open, including a transformer drag, canUndo and canRedo are
false and undo()/redo() return false. Opening and closing groups emit editstart
and editend change reasons, including empty and cancelled groups. begin() and
transaction() cannot start from a replay observer; start those edits after replay
returns.
const edit = history.begin({ label: 'Pick fill' });
edit.run(() => rect.fill('orange'));
edit.run(() => rect.fill('tomato'));
edit.commit(); // One step.Separate committed edits merge only with an explicit compatible mergeKey within
mergeWindow milliseconds (default 300). Unkeyed quick edits remain separate.
breakMerge() ends a sequence. Limits count entries rather than bytes.
remove() retains live object identity for reversible deletion. Semantic operations
that dispose old content, such as text replacement, gradient updates, clear(), and
ungroup(), restore disposed objects as new wrappers. Old wrappers remain destroyed;
event listeners, measurement callbacks, application memory, and external controllers
are not reconstructed. Logical history handles keep older entries usable. Core replay
batches expose replacement mappings through observeChanges() from @rasamjs/core/changes.
Use unwrap() to release group members while retaining the reusable group wrapper. Undo and
redo preserve that wrapper's identity, including when replay observers or transformers inspect
the released members synchronously.
Use remove() for deletable shapes. Explicit destroy() on any observed shape is
permanent disposal and clears the entire undo/redo history. Destroying the drawing
disposes its recorder. Clear/eviction/history destruction never disposes a live
application node.
Conflicting unrecorded state throws ChangeConflictError and leaves the entry on
top of its stack. Retrying without resolving that conflict throws again. Restore
the expected model state with withoutRecording(), or call clear() to discard
both stacks and accept the current drawing as a new baseline.
Undo/redo restores placements without firing node remove events, including when
undoing creation. Observe replay batches through observeChanges() from
@rasamjs/core/changes to track those structural changes after restoration has settled.
The transformer refreshes its overlay automatically.
Direct writes to .el, raw attribute storage, external CSS, focus, pointer capture,
subscriptions, arbitrary remember() values, and media/playback runtime are outside
the contract. Animation writes are observed with an animation source; stop conflicting
playback before replay. Template updates restore model values, while a later application
render can replace them. Custom private model state requires the core capture/restore
hooks; opaque DOM content has no automatic lossless serialization guarantee.
History emits change with stack counts, availability, and a reason. Bind application
buttons or shortcuts explicitly; the package installs no global keyboard handlers.
Notification errors can be reported after a successful logical edit or replay; inspect
ChangeObserverError from @rasamjs/core/changes to distinguish them from restoration failure.
An overlapping write between open edits is rolled back and throws ChangeConflictError
directly when it is the only failure. If other observers also fail, inspect the
ChangeObserverError.errors list for the conflict and those notification failures.
Observer reactions to undo/redo are ordinary new edits and discard redo. Wrap
intentional unrecorded reactions in history.withoutRecording(() => ...) to retain
redo; changes to the entry's own state can still conflict with its next replay.
If restoration and its rollback both fail, core throws ChangeRollbackError from
@rasamjs/core/changes. History emits failed, disables undo/redo, and suspends recording;
ordinary drawing edits continue. transaction() and withoutRecording() still run
synchronous callbacks and return their results, without recording or transaction
rollback. Callback errors propagate and thenables are rejected. on('change', listener)
remains available while failed, so UI components can subscribe after the failure and
receive the clear event when recording resumes. Repair or accept the drawing's
current state, then call clear() to release the old stacks and resume recording.
Destroyed history instances reject subscriptions and both callback methods.
See API.md and RELEASING.md. Run the example with
bun run --filter @rasamjs/history dev and open /examples/.
