automerge / automerge-classic

A JSON-like data structure (a CRDT) that can be modified concurrently by different users, and merged again automatically.
http://automerge.org/
MIT License
14.75k stars 466 forks source link

Using Automerge for text collaboration #343

Closed lacraft closed 3 years ago

lacraft commented 3 years ago

I currently have a document represented as a list of text. I want to make this document collaborative with automerge. I have tried doing const doc = Automerge.from(lines) where lines is the list of text lines, and I want to be able to make the edits to the documents take place which the users create. How do I do this? Right now the operations treat the entire doc as a giant string i.e. insert 'a' at index 233.

pvh commented 3 years ago

Hi there -- you can solve this problem in a few ways. The way you're currently creating the document probably results in a list CRDT where each line is an atomically updated string. A simple improvement would be to do create Automerge.Text objects for each line.

That said (and without knowing the finer points of your application it's hard to say for certain) you probably want to model the document not as an array of text but as a single larger Text field -- otherwise you'll have to do a bunch of interesting gymnastics when a user splits a line in two. This will probably require you to make some changes to the code you're currently using for editing & rendering but will let Automerge do a lot more of the book keeping for you.

Best of luck!

lacraft commented 3 years ago

Thank you! That helps a lot