How to create a codemirror editor go blank when someone joins

I am new to Yjs, I’ve tried to solve this for quite a time but didn’t find a solution.

I am using Yjs with codemirror binding and webrtc provider.
I am trying to clear the slate (or the yText) whenever a new user enters.
But, when we initiate the user, we have access to yText after some time the binding is done.
so, how do we have access to the previous set text immediately. And how do we know if the yText is synced?
Please help!!

Hi @satyasaibhushan,

In y-webrtc the notion of “synced” is very controversial. There is no way to tell when you synced with every other user. The y-websocket provider has an event that tells you when you synced to a server instance wsprovider.on('synced', () => { console.log('local client synced with server') }). The y-webrtc provider has a similar event that works differently: webrtcProvider.on('synced', () => { console.log('Local client is synced to another user') }) is fired when the local client is synced to any other user (which might not have the initial content). I don’t recommend to use it for setting some kind of initial state.

You can get around this problem by storing the Yjs document on the server instead of the text-representation.

I hope my explanation helped. I’m not sure what this means. Do you clear the state whenever a new user enters?

I am trying to start with a cleared editor whenever a new user enters.

Is the goal really to clear the editor whenever a new user enters? I don’t really see how this can be useful. For this you can probably just use provider.on('synced', () => ..)

1 Like

I’m trying to handle the case where I could input the HTML input from old system when editor is opened without any content. When “synced” event is received and the document is still empty, is it safe to assume that nobody has a version open with non-empty content?

Is there a supported method to import untrusted user HTML input into the document as initial state? I’d be fine with either server end or client end method.

I recommend to simply store the Yjs document instead of the HTML representation. Alternatively you can store the Yjs document alongside the HTML representation.

If you load the HTML representation after you detected that you “synced” with the server, there is always a chance that you duplicate content. E.g. when a client joins with existing content after it has been disconnected.

Is there a supported method to import untrusted user HTML input into the document as initial state? I’d be fine with either server end or client end method.

That depends on the editor you are using. If you are using Y.Xml and want to represent HTML content in a 1-1 mapping, then no. It does not exist in Yjs. You’d need to implement that yourself.