blockcn

Comments

Comment threads anchored to text, with replies, resolve, @mentions, and a sidebar.

The comments add-on lets people select text and start a thread on it. Others reply, edit or delete their own comments, and resolve the thread when it is done. When the Ask AI add-on is installed, an open thread can suggest a reply into the reply box. Threads are shared live when collaboration is installed, and kept wherever you choose when it is not.

Install

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

Wire it in

getCurrentUser returns the signed-in person. Their id decides who may edit or delete a comment, so use your user id, not a display name.

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

import { Editor } from "@/components/editor/Editor";
import { useEditor } from "@/components/editor/useEditor";
import { createEditorExtensions } from "@/editor-core/schema/extensions";
import { createCommentExtensions } from "@/components/editor/comments/comments";
import { CommentsLayer } from "@/components/editor/comments/CommentsLayer";
import { CommentsSidebar } from "@/components/editor/comments/CommentsSidebar";
import { currentUser } from "@/lib/auth";

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

export function EditorWithComments() {
  const editor = useEditor({ extensions });
  return (
    <Editor editor={editor}>
      <div className="grid gap-8 xl:grid-cols-[minmax(0,48rem)_20rem]">
        <Editor.Frame>
          <Editor.Content />
          <Editor.Chrome />
        </Editor.Frame>
        <CommentsSidebar />
      </div>
      <CommentsLayer />
    </Editor>
  );
}

The formatting toolbar shows an Add comment button once the add-on is installed. Nothing else is needed for it.

With createBlockcnEditor, comments({ getCurrentUser }) from @/components/editor/comments/commentsAddon adds the same extensions and mounts CommentsLayer as add-on chrome.

What people can do

  • Select text and press Add comment in the toolbar, or Mod+Alt+M. Type the comment and press Comment or Mod+Enter.
  • Click highlighted text to open its thread. With the caret inside it, Mod+Alt+M opens the thread and moves focus into it. Escape closes it and returns to the text.
  • Reply, edit or delete your own comments, resolve a thread, and reopen it from the sidebar's Resolved tab. Deleting the last comment deletes the thread and its highlight.
  • Type @ in a comment to mention someone. The list comes from the mentions add-on when it is installed, or from comments({ mentions }). Pass mentions: false to turn it off.
PartWhat it is
CommentsLayerThe composer for a new comment and the popover for the open thread.
CommentsSidebarEvery thread anchored in the document, open or resolved, in document order. Clicking one scrolls to its text.
useCommentThreads(editor)The threads, for building your own list.

Options

OptionDefault
getCurrentUserrequired(editor) => { id, name, color? } | null. null means nobody can comment.
storagesee belowA CommentStore, or (editor) => CommentStore.
canCommenteditable editors onlytrue lets read-only viewers comment, or pass (editor) => boolean.
mentionsthe mentions add-on(query) => { id, label }[], or false.
onCreate(thread, editor) after a thread is created.
onResolve(thread, editor) after a thread is resolved.
shortcutMod-Alt-mA key, a list of keys, or false.
exportAs"drop""footnotes" keeps comments in exported files. See Export.
chrometruePreset only. false stops the preset mounting CommentsLayer, so you can place it yourself.

Where threads are stored

The anchor is a comment mark with a threadId on the text, saved with the document. The threads themselves live in a store:

  • With collaboration, the default store is a Y.Map named blockcn-comments on the collaboration document. Every client sees the same threads, concurrent replies merge, and the server saves them with the document. Nothing else to set up.
  • Without collaboration, the default store is memory, which is lost on reload. Load and save threads with createMemoryCommentStore:
import { createCommentExtensions, createMemoryCommentStore } from "@/components/editor/comments/comments";

const extensions = createCommentExtensions({
  getCurrentUser,
  storage: createMemoryCommentStore({
    threads: await api.loadThreads(documentId),
    onChange: (threads) => api.saveThreads(documentId, threads),
  }),
});
  • Your own backend: implement CommentStore (list, get, createThread, addComment, updateComment, deleteComment, setStatus, deleteThread, subscribe). list must return the same array until something changes.

A thread is:

interface CommentThread {
  id: string;
  status: "open" | "resolved";
  /** The anchored text when the thread was created. */
  quote: string;
  createdAt: number;
  comments: { id: string; author: { id: string; name: string; color?: string }; body: string; createdAt: number; editedAt: number | null }[];
}

A comment body is plain text. A mention is stored as @[Ada Lovelace](ada). commentPlainText(body) turns it into @Ada Lovelace.

Permissions

By default only an editable editor can comment. A read-only viewer can open threads (click the text or use the sidebar) but sees no reply box or Resolve button. Pass canComment: true to let viewers comment.

With collaboration, a read-only token cannot write anything to the room, comments included: the example server, like any Hocuspocus server, drops every update from a read-only connection. canComment: true only helps without collaboration, or with a server that accepts comment updates from those clients.

Anyone who can comment can resolve or reopen any thread. Only the author can edit or delete a comment. These checks run in the browser; a backend that stores threads should enforce its own rules.

Editing behaviour

  • Typing at either edge of commented text does not extend the comment. Typing inside it does.
  • Splitting and joining paragraphs keeps the thread on both parts.
  • Deleting all of a thread's text hides the thread. Undo brings the text and the thread back.
  • Adding a comment is not an undo step, so Mod+Z never removes a highlight by surprise.
  • Copy and paste: a pasted copy of commented text drops the comment while the original is still in the document, because a thread has one anchor. Cut and paste, and dragging the text, keep it. Comments pasted from another document are dropped.
  • Comments can overlap. They cannot start inside a code block.
  • A resolved thread keeps its anchor, so reopening it brings the highlight back.

Export

HTML and Markdown export drop comments by default. exportAs: "footnotes" numbers each open thread after its text (release[1]) and lists the threads under a Comments heading at the end. Markdown escapes the brackets, which still render as [1]:

Ship the release\[1\] on Friday

## Comments

1. *“release”*
   - **Ada:** Can we move this to Monday?
   - **Grace:** Friday is fine.

renderDocument has no editor, so it has no threads and drops comment marks. To render footnotes on the server, run commentFootnotes(doc, threads) first, or stripCommentMarks(doc) to drop them.

Theming

VariableDefault
--blockcn-commentrgb(250 204 21 / 26%) / rgb(250 204 21 / 18%)
--blockcn-comment-activergb(250 204 21 / 55%) / rgb(250 204 21 / 38%)

Highlights are decorations on open threads only. The mark itself renders a plain <span data-comment-thread>.

On this page