Skip to the content
v1.0.1-beta.17

Version history

Premium add-on. This page covers a premium add-on, included in Scriviamo Pro and Pro + Collaboration. See pricing

The premium versions add-on keeps versions of the document: saved by name when someone asks, and on their own as the document changes. Any version can be compared with the current document, block by block and word by word, and restored as one undo step.

With the inspector add-on loaded, the versions are in the inspector's History tab. The Save Version tool saves one by name.

import {VersionHistory} from "./dist/addons/scriviamo.versions.js"

let history = VersionHistory.for(editor)
let version = await history.save("Sent to the client")
let versions = await history.list()              // newest first
await history.restore(version.id)                // one undo step
let changes = VersionHistory.compare(await history.content(version.id), editor.toJSONObject(), editor)
MemberMeaning
VersionHistory.for(editor)An editor's history
save(name, automatic)Saves the document as a version
list()The versions: {id, name, createdAt, author, automatic}, newest first
content(id)A version's document, as a JSON object
restore(id)Puts a version in place of the document; undo puts it back
remove(id)Deletes a version
VersionHistory.compare(before, after, editor)Each block as added, removed, changed (with its words) or unchanged
VersionHistory.automaticMinutesMinutes between automatic versions while the document changes; 10 by default, 0 for none
VersionHistory.automaticLimitHow many automatic versions are kept; 20 by default. Named versions stay until deleted

A history raises changed when versions are saved or deleted, and restored when one is restored.

Where versions are kept

A VersionStore keeps them. There are three:

  • In the browser, by default: BrowserVersionStore keeps them in IndexedDB, for that browser alone.
  • On your server: set VersionHistory.store, or history.store for one editor, to an object with list, save, load and remove, each returning a promise, which calls your server.
  • Shared, with the collaboration add-on: while a collaboration client has the document open, versions are kept by the collaboration server, and everyone in the document sees the same ones. The History tab says so. A version saved there holds the document as everyone sees it.
VersionHistory.store = {
  list: documentID => fetch(`/api/documents/${documentID}/versions`).then(response => response.json()),
  save: (documentID, version, content) => fetch(`/api/documents/${documentID}/versions`, {method: "POST", body: JSON.stringify({...version, content})}).then(response => response.json()),
  load: (documentID, id) => fetch(`/api/documents/${documentID}/versions/${id}`).then(response => response.text()),
  remove: (documentID, id) => fetch(`/api/documents/${documentID}/versions/${id}`, {method: "DELETE"}).then(() => {})
}

A store can also have canRemove: false, when its versions can't be deleted, and a describe(documentID) that resolves to {text, url, icon}, a line the History tab shows above the versions; a version can have a url to see it elsewhere. The git add-on's store uses all three. An add-on can give an editor a store with editor.versionStore.

The History tab compares a version with the current document, or with another version, chosen from the list.

Versions belong to a document by editor.documentID, or, while collaborating, the collaboration document's ID. The author is the collaborating user's name.

Restoring a shared version changes the document like any other edit, so everyone sees it, and the person who restored it can undo it.