Collab cursors


I’m trying to figure out how I can add the display of user cursors when editing together. I think so that I need each user to know the cursor position of all other users in the editor. And then I will be able to draw my interface through the decorations. My misunderstanding is that the cursor positions of users will only be relevant for their local versions of the editor. And it can easily be, for example, that there is a user who:

  1. Has a version 10 editor.
  2. The cursor is located at position 5.

Let’s say that I also have a version 10 editor, then I can display this user’s cursor at position 5. But what should I do the moment I start changing the content in my local editor? On the server, the version of the editor will remain the same, because I will need to submit my changes and confirm them by updating the version of the editor of the central server, but the state of my local editor will change at the moment.

I guess I need to perform a position offset based on the transaction from the version in which the cursor was installed to my local one. But in this case, I have a lot of questions:

  1. Can I guarantee that the user’s cursor is actually in this position?
  2. Does this mean that I have to keep the transaction history on the client? To be able to map the position from the user’s version to the current state, it sounds scary…
  3. At the same time, I will need to define “assoc” in order to understand which way to make the offset. And handle the case when the cursor was in a range that was suddenly deleted.

I would like to understand if I am thinking in the right direction or if it can be organized in a simpler way? Maybe someone has experience in implementation?

No, mapping the cursor won’t guarantee it’ll stay in the same place it actually is for that user. One approach could be to send the cursor position over the same channel as the changes, so that they are updated in lockstep.

Hmm, thanks for the answer! But I didn’t understand how you imagine it

Do you mean that users should independently report their position for the actual version of the editor?

Hi all!

I’m still looking for a solution algorithm. If someone has an implementation, tell me the algorithm! :eye:

I still haven’t come up with an algorithm, does anyone know which way to think?