How to track comment positions

I’m adding a comment system (similar to Google Docs) to Prosemirror with Yjs and am currently tracking comment positions using marks. I saw this thread which recommends storing comment ranges separately.

Comments are usually not modeled as document nodes—rather, they are references to ranges, that are tracked separately, outside of the document

I agree this is a much clearner way to store comments, but I’m a little concerned about performance. If we have a long document with a lot of comments then any character typed in the first paragraph will offset all comments and therefore require significant changes with each update.

As an alternative, is it possible to store the comment position in an offset relative to a node? This way changes to other nodes won’t require comments to update. A couple questions.

  1. Is there a recommended way to reference nodes outside of the prosemirror document? An index is the only thing I can think of but uncertain how easy that will be to update as the document changes.
  2. Is there a way to attach metadata to each node in the document? I could store a unique id or even the comment positions within this.

No, since see below…

You can track positions, but not nodes. Nodes don’t have a meaningful identity in ProseMirror.

You could add node attributes in your schema.

But the recommended way to do this is to keep a data structure that holds the positions of your comments, and map them forward on document changes.

Thanks for the response! Sounds like there isn’t an easy way to track the comment position relative to the node it’s in.

Hi @ryanb,

this might not be completely related to your question, but I’ve already created an extension for Google Docs like commenting for Tiptap, which uses ProseMirror under the hood. Although I am using marks to keep track of the comments.

Here’s the repo, GitHub - sereneinserenade/tiptap-comment-extension: Google docs like commenting solution for Tiptap 2. and you can try it out at https://tiptap-comment-extension.vercel.app/ .

@sereneinserenade I’m using Tiptap and marks for comments as well. I’ll check out the comment extension you linked and it might give me some ideas. Thanks!

1 Like

@ryanb if you’re using Yjs then you could save the Yjs cursors and store it independently of the document:

This is for decorations but the same could be used for threads too ( this way people could add threads to your document without being able to edit it for ex. )

1 Like