I’m sorry, but this is quite a lot for me to go through. It would take a while for me to tell you exactly what is going wrong.
A few recommendations though:
Do not set the ProseMirror state AND the initial Yjs state. Ideally, just remove this line:
When you have the EditorView that is made collaborative using the
ySyncPlugin, then there is no need to use
prosemirrorToYDoc. Simply create an editor view with the Yjs document that is connected to the server using y-websocket. Then you can replace the whole editor state with a new editor state (using the ProseMirror API).
Note, however, that I discouraged “initializing” on the client-side in several threads on this forum. I’m not sure how one would prevent the case that two clients “initialize” at the same time. Yjs guarantees that all peers will always end up with the same state. But it really only can do that. Ideally, you initialize the content on the server, if you have to.