Birch UI v0.1
Pricing

Compare and version historyDocument

Compare two versions of a document into a redline or side by side, combine two reviewed copies, save named versions, and restore or compare them in Birch UI Document Editor.

Open .md

Compare#

compareWith(revised, options) replaces the content with a redline: text that only the current version has is marked as deleted, and text that only revised has is marked as inserted. It returns the number of changes.

TypeScript
import { docxToDocument } from '@birchui/document';

const revised = await docxToDocument(await counterpartyFile.arrayBuffer());
const count = editor.compareWith(revised, { author: 'Counterparty draft', granularity: 'word' });
const same = await editor.compareWithDocx(counterpartyFile, { author: 'Counterparty draft' });
  • Paragraphs are aligned first, and changes inside a paragraph are found word by word (granularity: 'character' compares letter by letter). Unchanged paragraphs are not touched.
  • Formatting and table structure are compared too; see Formatting and Tables.
  • Tracked changes that are already in the document are resolved before comparing.
  • The result is ordinary tracked changes: review them with the Review tab, the Revisions pane, or getRevisions(), acceptChanges() and rejectChanges(). See Track changes.
  • The compare is one edit, so undo() brings the original back.

Review → Compare opens a dialog that takes the revised version from a .docx file or from a saved version. Show the result chooses between tracked changes in the document and the side-by-side view.

Without an editor, compareDocuments(original, revised, options) returns the redline as document content, countRevisions(content) counts its changes, and acceptAllRevisions(content) and rejectAllRevisions(content) resolve them. All four are DOM-free and run in Node.js.

Formatting#

A difference in formatting alone is reported as a tracked formatting change, the same kind the editor records when somebody formats text with tracking on. It keeps the earlier formatting, so rejecting it restores the original and accepting it keeps the revised formatting.

  • Character formatting: bold, italic, underline, strikethrough, superscript, subscript, font, size, colour and highlight. Each run of words whose formatting changed is one change (kind: 'format').
  • Paragraph formatting: style, alignment, indents, spacing and the other paragraph attributes. The whole paragraph is one change.
TypeScript
editor.compareWith(revised); // formatting differences are reported
editor.compareWith(revised, { formatting: false }); // only text and structure
editor.getRevisions({ kinds: ['format'] });

formatting defaults to true. With formatting: false formatting differences are not reported and the result carries the formatting of the revised version. Before this option existed a word whose formatting changed was reported as deleted and inserted again.

A formatting change made by the comparison is written to DOCX as w:rPrChange or w:pPrChange with empty earlier properties, like a formatting change tracked in the editor.

Tables#

Two tables are compared row by row and column by column instead of being replaced as a whole.

Difference Reported as Accept Reject
A row was added One tableRow change; the row is tinted Keeps the row Removes the row
A row was removed One tableRow change; the row stays, struck through Removes the row Keeps the row
A column was added One tableCell change that covers every cell of the column Keeps the cells Removes the cells
A column was removed One tableCell change that covers every cell of the column Removes the cells Keeps the cells
The text of a cell changed Insertions and deletions inside the cell

Row and column changes are marked on the row or the cells, not on the text inside them. In a DOCX file they are w:ins and w:del in the row properties and w:cellIns and w:cellDel in the cell properties; a file that is imported again lists a column as one change for each cell.

Rows are matched by their text. Columns are matched when the two tables have a different number of columns and neither has merged cells: a column is the same column when at least half of its cells have the same text. In a table with merged cells rows are still matched, and a row whose number of cells changed is reported as a deleted row and an inserted row. Two tables with a different number of columns and no column in common are reported as a deleted table and an inserted table.

Side by side#

compareSideBySide(revised, options) opens a read-only view over the page with three panes: the original, the revised version and the redline. It does not change the document.

TypeScript
editor.compareSideBySide(revised, { author: 'Counterparty draft', revisedLabel: 'Counterparty draft' });
editor.compareSideBySide(editor.getJSON(), {
  original: editor.getSnapshotDocument(version.id) ?? undefined,
  originalLabel: version.label,
});
editor.isSideBySideOpen();
editor.closeSideBySide();
  • original is the document to compare from; without it the current document is used. originalLabel and revisedLabel replace the pane titles. The other options are those of compareWith.
  • Scrolling one pane scrolls the other two to the same paragraph.
  • Next difference and Previous difference (Alt+↓ and Alt+↑) step through the changes, highlight the current one in all three panes and name it in the header.
  • Esc or Close comparison closes the view and puts the focus back where it was. While the view is open the editor behind it cannot be reached with the keyboard.
  • The original and revised panes are drawn from the redline with every change rejected or accepted. A difference the comparison does not report, such as a changed list style, therefore looks the same in both.
  • The view covers the browser window and is placed next to the editor element in the page, so it follows the theme of the editor. The text reflows to the width of a pane; it is not paginated.

The commands are compareSideBySide (with the revised document as its value) and closeSideBySide. compareSideBySide returns false when the revised document is missing or cannot be read.

Moves#

Pass moves: true to pair a paragraph that was deleted in one place with the same paragraph inserted in another. Both halves get the same moveId, are drawn in the move colour with a double underline, and are listed as one move in the Reviewing pane.

TypeScript
const redline = compareDocuments(original.content, revised.content, { author: 'Priya Raman', moves: true });
editor.compareWith(revisedDocument, { moves: true });

Only whole paragraphs of 16 characters or more are paired; a moved sentence inside a paragraph is reported as a deletion and an insertion.

Combine two reviewed copies#

When two people reviewed copies of the same document separately, combining merges both sets of edits into one redline in which each change carries its reviewer's name. The open document is the original:

TypeScript
const count = editor.combineWith(priyaCopy, danielCopy, { firstAuthor: 'Priya Raman', secondAuthor: 'Daniel Brandt' });
const same = await editor.combineWithDocx(priyaFile, danielFile, { firstAuthor: 'Priya Raman', secondAuthor: 'Daniel Brandt' });

Review → Combine asks for the two .docx files and the two reviewer names, and then shows the number of changes with Accept all, Reject all and Review one by one. The command is combineDialog. Combining is one edit, so undo() brings the original back.

Without an editor, use combineDocuments:

TypeScript
import { combineDocuments } from '@birchui/document';

const combined = combineDocuments(original.content, priyaCopy.content, danielCopy.content, {
  firstAuthor: 'Priya Raman',
  secondAuthor: 'Daniel Brandt',
});
editor.setJSON({ ...original, content: combined });

The three documents are aligned paragraph by paragraph. A paragraph that only one reviewer changed gets that reviewer's tracked changes. When both changed it to the same text the change is recorded once; when they changed it differently, the first reviewer's version is shown as a change to the paragraph and the second reviewer's version follows as an inserted paragraph, so nothing is lost and you resolve the conflict by accepting one and rejecting the other. Rejecting everything gives the original back. Tracked changes that are already in the copies are accepted before the comparison.

A paragraph in which a reviewer changed only the formatting gets a formatting change under that reviewer's name; when both reviewers reformatted the same unchanged paragraph, the first reviewer's formatting is reported. Pass formatting: false to leave formatting out.

Versions#

A version is a snapshot of the whole document as compact JSON, with a label, the author and the time. These versions belong to one editor. In a collaborative session the authority keeps named versions for everyone and stores them as the steps since the previous version; see Version history.

TypeScript
const version = editor.saveSnapshot('Sent to the customer'); // { id, label, author, date }, or null after destroy()
editor.getSnapshots();
if (version) {
  editor.getSnapshotDocument(version.id); // DocumentData
  editor.compareWithSnapshot(version.id); // redline of everything changed since then
  editor.restoreSnapshot(version.id);
  editor.deleteSnapshot(version.id);
}
editor.on('snapshotsChange', ({ snapshots }) => renderHistory(snapshots));

Review → Save version and Version history do the same from the ribbon; the pane lists versions with Restore, Compare and Delete.

Versions are kept in memory unless you pass storage:

TypeScript
createDocumentEditor(host, {
  snapshots: {
    load: () => fetch('/api/contracts/481/versions').then((response) => response.json()),
    save: (snapshots) =>
      fetch('/api/contracts/481/versions', { method: 'PUT', body: JSON.stringify(snapshots) }).then(() => undefined),
  },
});

load runs once when the editor is created and save after every change to the list, with the full list of DocumentSnapshot objects (data is the serialised document). A failed save is reported with console.error and does not interrupt editing.

↑ ↓ to move, Enter to open, Esc to close