> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tsdraw.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Undo and redo

> History tracking with snapshot-based undo/redo

tsdraw tracks document changes so users can undo and redo their actions. The system works by capturing full document snapshots at key moments.

## Using undo/redo

From the UI, the default toolbar includes undo and redo buttons. Users can also use keyboard shortcuts:

* **Undo**: `Ctrl/Cmd + Z`
* **Redo**: `Ctrl/Cmd + Shift + Z`

On touch devices, two-finger tap triggers undo and three-finger tap triggers redo.

Programmatically:

```tsx theme={null}
editor.undo()
editor.redo()
```

Check availability:

```tsx theme={null}
editor.canUndo() // true if there's history to undo
editor.canRedo() // true if there's a future state to restore
```

## How it works

The editor maintains a stack of document snapshots. Each user action that modifies the document (drawing a shape, erasing, moving) pushes a new snapshot to the stack.

When the user undoes an action, the editor restores the previous snapshot. Redo moves forward through the stack.

## History limits

The history stack is capped at **100 entries**. Once the limit is reached, the oldest entry is discarded when a new one is added.

## Batching

Multiple rapid changes (like dragging to move shapes) are batched into a single undo entry. This means undoing a drag restores the shapes to their position before the drag started, not to each intermediate position.

## Touch gestures

On mobile and tablet:

| Gesture          | Action |
| ---------------- | ------ |
| Two-finger tap   | Undo   |
| Three-finger tap | Redo   |

These gestures can be disabled through `touchOptions`:

```tsx theme={null}
<Tsdraw touchOptions={{ tapUndoRedo: false }} />
```
