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

# Undo and redo

> Go back one edit, or do it again.

export const LiveExample = ({path, title = 'Live example', height = 460}) => {
  return <Frame>
      <iframe src={`https://floorkit-examples.ploterr.com/floorkit/${path}?embed=1`} ref={frame => {
    if (!frame || !['localhost', '127.0.0.1'].includes(window.location.hostname)) return;
    const local = `http://localhost:5555/${path}?embed=1`;
    if (frame.src !== local) frame.src = local;
  }} title={title} loading="lazy" allow="fullscreen" style={{
    width: '100%',
    height: `${height}px`,
    border: 0,
    display: 'block'
  }} />
    </Frame>;
};

A session keeps each edit in a history. Undo and redo are one call each. Floorkit builds the geometry of that step again, so the 3D model and the drawings always agree with the model.

<LiveExample path="guides/undo-and-redo" />

## Before you start

You have a session and you applied one edit or more.

## Add the two actions

```typescript theme={null}
import type { BuildingSession } from '@opengeometry/floorkit';

export function undo(session: BuildingSession): void {
  if (session.canUndo) session.undo(session.revision);
}

export function redo(session: BuildingSession): void {
  if (session.canRedo) session.redo(session.revision);
}
```

## Keep your buttons up to date

`subscribe` calls your function after each edit, undo or redo. It returns a function that stops the calls.

```typescript theme={null}
export function watchHistory(
  session: BuildingSession,
  undoButton: HTMLButtonElement,
  redoButton: HTMLButtonElement,
): () => void {
  const refresh = (): void => {
    undoButton.disabled = !session.canUndo;
    redoButton.disabled = !session.canRedo;
  };
  refresh();
  return session.subscribe(refresh);
}
```

## Rules

* A new edit clears the redo list.
* `undo` and `redo` return the new revision number, or `null` when there is nothing to do.
* The history is not saved in the project file. A session that you open from a file starts with an empty history.
* After an undo, get elements again from `session.currentBuild` by their ids. A changed element can be a new object, and Floorkit then disposes the old object. A changed wall, and a door, a window or a hole on a wall, can also stay the same object with new geometry.

## Next

<CardGroup cols={2}>
  <Card title="Save and reopen" icon="floppy-disk" href="/floorkit/guides/save-and-reopen">
    Keep the building between visits.
  </Card>

  <Card title="BuildingSession API" icon="code" href="/floorkit/api/editor/building-session">
    See each member of a session.
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.