# Undo and redo

> Record moves, resizes, creates and removals in the scheduler, undo them with Ctrl+Z, and save undone changes from historyChange.

The scheduler records every change to its events: drags, resizes and keyboard deletes, and the `addEvent`, `updateEvent` and `removeEvent` calls, including the two-part change of a [recurring occurrence](https://docs.birchui.com/scheduler/recurrence.md).

```ts title="dispatch-board.ts"
import { createScheduler } from '@birchui/scheduler';

const scheduler = createScheduler(el, {
  resources: technicians,
  events: jobs,
  start: '2026-11-23',
  end: '2026-11-30',
  undoRedo: { limit: 200 },
});
```

Undo is on by default and keeps the last 100 changes. `{ limit }` keeps another number of changes, and `undoRedo: false` (or `{ limit: 0 }`) turns history off, in which case `historyChange` does not fire. Any other value logs one warning and keeps the default.

**`UndoRedoOptions`** (interface from `@birchui/scheduler`, full reference: https://docs.birchui.com/api/scheduler.md)

```ts
interface UndoRedoOptions
```

| Property | Type | Description |
| --- | --- | --- |
| `limit?` | `number` |  |

## Keys and methods

<kbd>Ctrl</kbd>+<kbd>Z</kbd> (<kbd>⌘</kbd>+<kbd>Z</kbd>) undoes, and <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Z</kbd> or <kbd>Ctrl</kbd>+<kbd>Y</kbd> redoes, while focus is inside the scheduler and it is not `readOnly`.

**`SchedulerApi`** (interface from `@birchui/scheduler`, full reference: https://docs.birchui.com/api/scheduler.md)

```ts
interface SchedulerApi<TEvent extends SchedulerEvent = SchedulerEvent, TResource extends SchedulerResource = SchedulerResource> extends Widget<SchedulerOptions<TEvent, TResource>, SchedulerEvents<TEvent, TResource>>
```

| Property | Type | Description |
| --- | --- | --- |
| `undo` | `() => boolean` |  |
| `redo` | `() => boolean` |  |
| `canUndo` | `() => boolean` |  |
| `canRedo` | `() => boolean` |  |
| `clearHistory` | `() => void` |  |

`setEvents()` and `update({ events })` start a new history, because the events they load are a new baseline.

## Saving undone changes

Undo and redo do not fire `eventDrop`, `eventResize` or `eventRemove` again. Every change, including undo and redo, is reported by `historyChange`:

| Field | Meaning |
| --- | --- |
| `action` | `'record'` for a new change, `'undo'`, `'redo'`, or `'clear'` when the history is reset |
| `kind` | `'move'`, `'resize'`, `'add'`, `'update'` or `'remove'`, or `null` for `'clear'` |
| `changes` | `{ id, before, after }` for every event touched; `before` is `null` for a created event and `after` is `null` for a removed one |
| `canUndo`, `canRedo` | The new availability, for toolbar buttons |

```ts
scheduler.on('historyChange', ({ action, changes, canUndo, canRedo }) => {
  undoButton.disabled = !canUndo;
  redoButton.disabled = !canRedo;
  if (action !== 'undo' && action !== 'redo') return;
  for (const { id, after } of changes) {
    if (after) void jobs.save(after);
    else void jobs.remove(id);
  }
});
```

If you already save from `eventDrop` and friends, save only undo and redo from `historyChange` as above. If you start fresh, save everything from `historyChange` and nothing else.

## Related

- [Save scheduler changes to a server](https://docs.birchui.com/recipes/scheduler-save-to-server.md)
- [Scheduler API and events](https://docs.birchui.com/scheduler/api.md)
- [Grid undo and redo](https://docs.birchui.com/grid/undo-redo.md)
