Formatting a document backed by yJS

I’m building an editor where users can edit JSON documents (in a text editor). This is all synced via yJS between users in a Y.XmlFragment.

Now, I want to implement a “format document”, that formats the document (adds correct indentation, etc). The library I’m using for this of course returns a string with the reformatted document.

Now, how would I apply this cleaned document to the Y.Text? Is the recommended way to calculate a diff between preformatted and formatted, convert the diff to edits (.insert(), etc.). Or is there an easier recommended approach?

Yeah, you can calculate the differences and then apply them using the methods that Y.Text exposes. I don’t think there is a way around that.

1 Like