# Track changes and compare

> Suggest edits with tracked insertions and deletions, accept or reject them, compare two versions into a redline and propose changes from other tools in Birch UI Document Editor.

With track changes on, everything users type is recorded as an insertion and everything they delete stays visible as a deletion, each with the author's name and the time. Reviewers accept or reject the changes one by one or all at once. Changes are saved in the document JSON and in DOCX files as Word revisions (`w:ins` and `w:del`), so they round-trip with Microsoft Word and LibreOffice.

```ts
const editor = createDocumentEditor(host, { author: 'Priya Raman', trackChanges: true });

editor.setTrackChanges(false);
editor.isTrackingChanges();
editor.setShowMarkup(false); // show the document as if every change were accepted
```

<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>E</kbd> and the toolbar toggle tracking. Each author gets their own colour.

## How edits are recorded

- Typed and pasted text is marked as an insertion. Consecutive typing by the same author forms one change.
- Deleted text is kept and marked as a deletion. The cursor moves past it, as in Word.
- Deleting your own tracked insertion removes it for real, because it was never part of the original.
- Deleting text that another author inserted marks it as deleted, so both suggestions remain visible.
- Formatting changes (bold, colour, font, alignment, indents, styles) are recorded with the formatting that was there before, so rejecting one restores it.
- <kbd>Enter</kbd> and <kbd>Backspace</kbd> at a paragraph boundary are recorded as an inserted or deleted paragraph mark (shown as a coloured ¶). Rejecting an inserted mark joins the paragraphs again; accepting a deleted one joins them.
- Inserted and deleted table rows are tinted and stay in the table until the change is resolved.
- Text that is cut and pasted, or dragged, to another place becomes one move: both ends are shown in green with a double underline or strikethrough and are accepted or rejected together.
- Text typed with an input method editor is marked when the composition ends.

## Reviewing changes

```ts
const changes = editor.getTrackedChanges();
// [{ id: 'ch-…', kind: 'deletion', author: 'Priya Raman', date: '2026-11-20T09:30:00Z', text: '30', from: 112, to: 114 }, …]

editor.acceptChange(changes[0].id);
editor.rejectChange(changes[1].id);
editor.acceptAllChanges(); // returns the number resolved
editor.rejectAllChanges();

editor.on('trackedChangesChange', ({ changes }) => renderReviewList(changes));
```

Right-click a change for **Accept change** and **Reject change**. Accepting a deletion that covers a whole paragraph removes the paragraph too.

`getTrackedChanges()` lists insertions and deletions one mark at a time. `getRevisions(filter?)` returns what a reviewer sees: a deletion directly followed by an insertion from the same author is one `replace`, and formatting, paragraph marks, table rows, table cells and moves have their own kinds.

```ts
editor.getRevisions();
// [{ id, ids, kind: 'replace', author: 'Priya Raman', date, text: 'sixty', deletedText: 'thirty',
//    summary: 'Replaced “thirty” with “sixty”', from: 48, to: 59 }, …]
// kind: 'insert' | 'delete' | 'replace' | 'format' | 'paragraph' | 'move' | 'tableRow' | 'tableCell'
//     | 'tableFormat' | 'sectionFormat' | 'numbering'

editor.acceptChanges({ author: 'Daniel Brandt' }); // by author
editor.rejectChanges({ from: 120, to: 480 }); // inside a range
editor.acceptChanges({ kinds: ['format'] });
editor.acceptChanges({ authors: ['Daniel Brandt'], after: '2026-11-01', before: new Date() }); // by author and date
editor.nextChange(); // selects the next change and returns it; previousChange() goes back
editor.goToChange(id);

editor.on('revisionsChange', ({ revisions }) => updateBadge(revisions.length));
```

The Review tab has **Accept** and **Reject** split buttons (this change, and move to next, all shown, all), **Previous** and **Next**, and a **Reviewing pane** that lists every change with its author, time and a summary; filter it by author or kind, move through it with the arrow keys and press <kbd>Enter</kbd> to jump to a change. **Accept all shown** and **Reject all shown** in the pane resolve only the changes that the two filters leave in the list, for example every formatting change of one reviewer. Pointing at a change, or reaching it with Next, opens a small card with Accept and Reject. The same actions are commands: `acceptChange`, `rejectChange`, `acceptAndNext`, `rejectAndNext`, `acceptShownChanges`, `rejectShownChanges`, `nextChange`, `previousChange` and `revisionsPane`.

### Property changes from Word

Word records three kinds of change that are not text: a change of table properties (`w:tblPrChange`), a change of section properties such as page size, orientation, margins and columns (`w:sectPrChange`) and a change of list numbering (`w:numberingChange`). The editor reads all three from a DOCX file, lists them in the revisions pane and in `getRevisions()` with the kinds `tableFormat`, `sectionFormat` and `numbering`, and writes them back until they are resolved. They follow the **Formatting** switch of the markup options.

| Change | Accept | Reject |
| --- | --- | --- |
| `tableFormat` | Keeps the table as it is | Restores the earlier table style, alignment, indent, width and layout; properties the editor does not model come back from the stored XML when the file is saved |
| `sectionFormat` | Keeps the page setup | Restores the earlier page setup of that section and lays the pages out again |
| `numbering` | Keeps the numbering | Keeps the numbering and drops the record: the old number is stored as text, not as a list definition |

The editor does not record these three kinds itself: a table or page setup change made with tracking on is applied directly.

### Balloons in the margin

In the `all` markup mode each tracked change near the viewport also gets a balloon in the margin beside the page: the author, a summary such as *Replaced "thirty" with "sixty"*, and **Accept** and **Reject**. Balloons share the margin with the comment threads and never overlap them. Clicking a balloon selects the change. Pass `changeBalloons: false` to keep only the inline marks and the change bars.

### Lock tracking with a password

```ts
await editor.protectDocument('trackedChanges', 'Harbour-2026'); // tracking stays on
editor.setTrackChanges(false); // ignored and announced while the lock is on
editor.acceptChanges(); // 0: changes cannot be resolved while tracking is locked
editor.isPasswordProtected(); // true
await editor.unprotectDocument('Harbour-2026'); // false when the password is wrong
```

**Review → Restrict editing → Lock tracking with a password…** does the same from the ribbon. The password is stored the way Word stores it: a salted SHA-512 hash with 100,000 iterations over Word's legacy password key, written to `w:documentProtection` in the DOCX file, so the lock survives export and import. This keeps cooperating reviewers honest; it is not encryption, and anybody who edits the file outside the editor can remove the element. The hash is tested against the values that Apache POI 5.2.5 writes for the same password and salt; it has not been checked against Microsoft Word itself.

## Markup modes

```ts
createDocumentEditor(host, { markupMode: 'simple' });
editor.setMarkupMode('original'); // 'all' | 'simple' | 'none' | 'original'
editor.setMarkupVisibility({ formatting: false, hiddenAuthors: ['Daniel Brandt'] });
```

| Mode | Shows |
| --- | --- |
| `all` | Coloured insertions, deletions, moves and formatting marks, with a grey bar in the margin beside every changed line. |
| `simple` | The final text, with only a red bar in the margin. |
| `none` | The final text without any marks. |
| `original` | The text as it was before the changes. |

The modes only change how the document is drawn; the content and all positions stay the same, so editing keeps working in every mode. `setShowMarkup(false)` is the same as `'none'` and `setShowMarkup(true)` the same as `'all'`. `changeBars: false` turns the margin bars off, and `markupModeChange` reports a change of mode. `setMarkupVisibility` hides insertions and deletions, formatting marks, comment highlights or the changes of single authors; **Accept all shown** then leaves the hidden ones alone.

## Compare two versions

`compareWith(revised, options)` replaces the editor content with a redline: everything removed from the current version is marked as deleted and everything added in `revised` is marked as inserted. Changes inside a paragraph are found word by word and unchanged paragraphs stay untouched. Formatting differences become tracked formatting changes (`formatting: false` leaves them out), and added or removed table rows and columns become `tableRow` and `tableCell` changes. The return value is the number of changes. The compare is a normal edit, so `undo()` restores the original.

```ts
const revised = await docxToDocument(await counterpartyFile.arrayBuffer());
const count = editor.compareWith(revised, { author: 'Counterparty draft', granularity: 'word' });
```

**Review → Compare** opens a dialog that takes the revised version from a `.docx` file or from a saved version, with the author name and the granularity, and then shows the number of changes with **Accept all**, **Reject all** and **Review one by one**. `compareWithDocx(file, options)` does the same from code. Comparing resolves the tracked changes that were already in the document first.

`compareSideBySide(revised, options)` shows the original, the revised version and the redline next to each other without changing the document, and **Review → Combine** merges two reviewed copies. Both are described in [Compare and version history](https://docs.birchui.com/document/compare-and-versions.md).

The same algorithm is available without an editor as `compareDocuments(original, revised, options)`, which returns the redline content. `acceptAllRevisions(doc)` and `rejectAllRevisions(doc)` resolve the revisions in any document JSON.

## Propose changes from other tools

`proposeChange` lets another part of your application, such as a clause library, a contract review service or a language model you call, suggest edits that people then review. It records a tracked replacement whether or not tracking is on, and returns the ids of the new changes.

```ts
editor.proposeChange({
  find: 'reasonable efforts',
  text: 'best efforts',
  author: 'Clause library',
});

editor.proposeChange({ from: 120, to: 134, text: 'thirty (30) days', author: 'Contract review' });
```

`find` matches case-sensitively; `occurrence` picks the n-th match (from 0). An empty `text` proposes a deletion. If the text is not found or the range is invalid, the call returns an empty array.

## Versions

A version is a snapshot of the whole document as compact JSON.

```ts
const version = editor.saveSnapshot('Sent to counterparty'); // { id, label, author, date }, or null
editor.getSnapshots();
if (version) {
  editor.compareWithSnapshot(version.id); // redline of everything changed since then
  editor.restoreSnapshot(version.id);
  editor.deleteSnapshot(version.id);
}
editor.on('snapshotsChange', ({ snapshots }) => renderHistory(snapshots));

createDocumentEditor(host, {
  snapshots: {
    load: () => api.loadVersions(matterId), // DocumentSnapshot[]: { id, label, author, date, data }
    save: (snapshots) => api.saveVersions(matterId, snapshots),
  },
});
```

Without the `snapshots` option versions live in memory for as long as the editor does. **Review → Version history** opens a pane that lists them with author and date and has **Compare**, **Restore** and **Delete** for each one.
