# Edit mode

> Let a reader drag annotations and edit text in place, and apply each edit transaction to the spec.

In edit mode a reader can drag annotations, nudge them with the keyboard and rewrite chart text in place. Each change reaches `onEdit` as one transaction, and your code decides whether to apply it to the spec.

```json
{
  "mark": {
    "type": "line",
    "strokeWidth": 2
  },
  "data": [
    {
      "date": "2019-01-01",
      "rate": 1.6
    },
    {
      "date": "2019-04-01",
      "rate": 2
    },
    {
      "date": "2019-07-01",
      "rate": 1.8
    },
    {
      "date": "2019-10-01",
      "rate": 1.8
    },
    {
      "date": "2020-01-01",
      "rate": 2.5
    },
    {
      "date": "2020-04-01",
      "rate": 0.3
    },
    {
      "date": "2020-07-01",
      "rate": 1
    },
    {
      "date": "2020-10-01",
      "rate": 1.2
    },
    {
      "date": "2021-01-01",
      "rate": 1.4
    },
    {
      "date": "2021-04-01",
      "rate": 4.2
    },
    {
      "date": "2021-07-01",
      "rate": 5.4
    },
    {
      "date": "2021-10-01",
      "rate": 6.2
    },
    {
      "date": "2022-01-01",
      "rate": 7.5
    },
    {
      "date": "2022-04-01",
      "rate": 8.3
    },
    {
      "date": "2022-07-01",
      "rate": 8.5
    },
    {
      "date": "2022-10-01",
      "rate": 7.7
    },
    {
      "date": "2023-01-01",
      "rate": 6.4
    },
    {
      "date": "2023-04-01",
      "rate": 4.9
    },
    {
      "date": "2023-07-01",
      "rate": 3.2
    },
    {
      "date": "2023-10-01",
      "rate": 3.2
    },
    {
      "date": "2024-01-01",
      "rate": 3.1
    },
    {
      "date": "2024-04-01",
      "rate": 3.4
    },
    {
      "date": "2024-07-01",
      "rate": 2.9
    },
    {
      "date": "2024-10-01",
      "rate": 2.6
    }
  ],
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal",
      "axis": {
        "tickCount": 6
      }
    },
    "y": {
      "field": "rate",
      "type": "quantitative",
      "axis": {
        "title": "Inflation (%)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": "2022-07-01",
      "y": 8.5,
      "text": "Peak: 8.5%",
      "anchor": "right",
      "offset": {
        "dx": 16,
        "dy": 0
      },
      "connector": true
    },
    {
      "type": "refline",
      "y": 2,
      "label": "Fed 2% target",
      "style": "dashed",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "US inflation peaked at 8.5% in 2022 and was down to 2.6% by late 2024",
    "subtitle": "US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)"
  }
}
```

Mount with `interaction: "edit"` to turn it on. Drag the note and the event log shows the transaction the gesture produced: a list of operations, each a `set`, `add` or `remove` at a JSON Pointer path into the spec, such as `/annotations/0/offset`. This page applies each transaction and redraws the chart with `update`, so the note stays where you leave it. The Code panel shows the spec as the page loaded it, not the state or edits on screen.

## Applying an edit and undoing it

`applyEditTransaction` from the core package applies a transaction to a copy of the spec. Keep its `inverse` and an undo is the same call with the inverse:

```ts title="edit.ts"
import { applyEditTransaction, type EditTransaction } from "@opendata-ai/openchart-core";
import { createChart } from "@opendata-ai/openchart-vanilla";

let spec = initialSpec;
const undoStack: EditTransaction[] = [];

const chart = createChart(container, spec, {
  interaction: "edit",
  onEdit(transaction) {
    const result = applyEditTransaction(spec, transaction);
    if (!result.ok) return; // a stale or invalid edit: keep the current spec
    spec = result.spec;
    undoStack.push(result.inverse);
    chart.update(spec);
  },
});

function undo() {
  const inverse = undoStack.pop();
  if (!inverse) return;
  const result = applyEditTransaction(spec, inverse);
  if (!result.ok) return;
  spec = result.spec;
  chart.update(spec);
}
```

The helper checks every operation before it returns anything and never changes the spec you pass in. When one operation fails, for example because the annotation it targets has been removed or reordered since the gesture began, `result.ok` is false and `result.error` names the operation's path and the reason. Save `result.spec` wherever your page keeps the document; React, Vue and Svelte take the same handler as `onEdit`, `@edit` and `onedit`.

## Editing text

```json
{
  "mark": "bar",
  "data": [
    {
      "country": "India",
      "population": 1463000000
    },
    {
      "country": "China",
      "population": 1410000000
    },
    {
      "country": "United States",
      "population": 347000000
    },
    {
      "country": "Indonesia",
      "population": 285000000
    },
    {
      "country": "Pakistan",
      "population": 255000000
    },
    {
      "country": "Nigeria",
      "population": 240000000
    },
    {
      "country": "Brazil",
      "population": 217000000
    },
    {
      "country": "Bangladesh",
      "population": 175000000
    },
    {
      "country": "Russia",
      "population": 144000000
    },
    {
      "country": "Ethiopia",
      "population": 135000000
    }
  ],
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": "-x"
    },
    "x": {
      "field": "population",
      "type": "quantitative",
      "axis": {
        "title": "Population",
        "format": ".2s"
      }
    }
  },
  "labels": {
    "density": "all",
    "format": ".3s"
  },
  "chrome": {
    "title": "India and China each have more than 1.4 billion people",
    "subtitle": "Population of the 10 most populous countries, 2025, UN projection"
  }
}
```

Select the title, the subtitle or a text annotation and press Enter, or double-click it, to edit its text in place. Enter commits the new text as one transaction and Escape cancels. Double-clicking an empty spot in the plot area opens the same editor and adds a new text annotation there.

## Keyboard

| Key | In edit mode |
| --- | --- |
| Tab, Shift+Tab | Select the next or previous element: each chrome text the chart shows (title, subtitle, source, byline, footer), each annotation, then the legend. Past the last one, focus leaves the chart. |
| Arrow keys | Nudge the selected text annotation 1px. |
| Shift+arrow keys | Nudge it 10px. |
| Enter | Edit the selected element's text. |
| Delete, Backspace | Remove the selected element. |
| Escape | Close the text editor, or clear the selection. |

A run of nudges becomes one transaction 500 milliseconds after the last arrow key is released, or sooner if the reader selects something else or focus leaves the chart. Because the arrow keys nudge, edit mode turns off the arrow-key navigation readers use to step through data points; [select mode](/docs/behavior/interaction/) keeps it.

## Editing a playback chart

A chart with `encoding.time` mounted in edit mode never plays by itself. It opens on the final period with the player below it, so an author can press Play or scrub to check another period while editing. Starting a drag pauses the playback.

Edits to the title, annotations and legend work as on any chart. When your `onEdit` handler applies one with `update`, the chart keeps its current period and recomputes its axes and colors over every period, so they stay fixed while it plays. See [Playback](/docs/change-over-time/playback/) for the player and its controls.

## Fields that matter

| Field | What it does |
| --- | --- |
| `interaction` | Mount option. `"edit"` allows selection plus every edit gesture. An `onEdit` handler alone also turns it on. |
| `onEdit` | Receives one `EditTransaction` per finished gesture. |
| `applyEditTransaction` | Applies a transaction to a copy of the spec and returns the new spec and its `inverse`, or an error. |
| `revision` | Mount option for your own bookkeeping, stamped on each transaction emitted after it is set. |
| `annotations[].id` | A stable id that goes into each transaction's `expect`, so an edit made before the list changed is rejected as stale instead of applied to the wrong annotation. |

## Related

- [Events reference](/docs/api/events/#persisting-edits) covers persisting edits and the other handlers.
- [Annotations](/docs/framing/annotations/) shows the callouts, reference lines and ranges a reader can move.
- [Line](/docs/change-over-time/line/) covers annotated line charts.
