blockcn

Version history

Snapshots of a document, a read-only preview, a diff, and restore in one undo step.

The versions add-on saves snapshots on a timer (every 5 minutes by default) and when you ask it to. The history panel lists them with the time and the people who edited, previews one read-only, highlights insertions and deletions against another version or the current document, and restores one. Restore is a single undo step.

Install

npx shadcn@latest add https://blockcn.com/r/versions.json

Wire it in

storage is where versions live. getCurrentUser is the person recorded on snapshots this editor saves.

src/components/editor-with-versions.tsx
"use client";

import { Editor } from "@/components/editor/Editor";
import { useEditor } from "@/components/editor/useEditor";
import { createEditorExtensions } from "@/editor-core/schema/extensions";
import { createMemoryVersionStore } from "@/components/editor/versions/versionStore";
import { createVersionExtensions } from "@/components/editor/versions/versionExtension";
import { HistoryPanel } from "@/components/editor/versions/HistoryPanel";
import { currentUser } from "@/lib/auth";

const storage = createMemoryVersionStore({
  onChange: (versions) => localStorage.setItem("versions", JSON.stringify(versions)),
});

const extensions = [
  ...createEditorExtensions(),
  ...createVersionExtensions({
    storage,
    getCurrentUser: () => ({ id: currentUser.id, name: currentUser.name }),
  }),
];

Editor.Chrome hides while the editor is read-only, so render HistoryPanel next to the editor if viewers should still open history. versions({ chrome: false }) from @/components/editor/versions/versionsAddon does the same when you build the editor with createBlockcnEditor.

Snapshots

Without collaboration, a version is the document JSON. With collaboration, it is a Yjs snapshot (Y.snapshot / Y.encodeSnapshot). Restoring a Yjs snapshot needs the same document, with garbage collection off, because a snapshot points at deleted text instead of copying it.

Pass gc: false to useCollaboration (or create the Y.Doc with { gc: false }) before the provider connects. The versions extension also sets doc.gc = false when the editor is created, which is too late for text deleted before that. Turning garbage collection off keeps every edit in memory for the life of the document. That is the cost of being able to restore an old snapshot.

On a timer, only the lowest editable client in the room saves, so two people don't store the same moment twice. Anyone who can edit can still name or restore. Authors are the people who typed on the client that saved the version. A snapshot taken with no local edits records that client’s current user.

maxVersions drops the oldest versions past the cap. A named version is kept until every unnamed one is already gone.

Server

The example server stores versions as JSON files next to the room files (VERSIONS_DATA_DIR, or COLLAB_DATA_DIR/versions). The same room token authorizes them: read can list and fetch a preview, write can save, delete, and POST /restore.

GET /api/versions/:roomList. Read or write.
GET /api/versions/:room/:idOne version, including the snapshot. Read or write.
PUT /api/versions/:room/:idSave. Write only.
DELETE /api/versions/:room/:idDelete. Write only.
POST /api/versions/:room/:id/restoreThe snapshot, for a client that is about to write it back. Write only.

createServerVersionStorage({ endpoint, room, token }) is the client for that API. Restoring the document itself still goes through the editor, so a read-only collaboration token cannot apply it. Set COLLAB_GC=off on the server or a snapshot cannot be restored after the room is saved and loaded again: the server would have thrown away the deleted text. With garbage collection off, the room file grows with every edit.

A room keeps at most VERSIONS_MAX versions (default 100). Named ones are dropped last.

On this page