Update prosemirror state using y-prosemirror in nodejs

Is it possible to update the ProseMirror state in Node.js using Yjs? Obviously, it should be possible, but it’s quite challenging to update this state using only Yjs operations. I tried to decode the SyncPlugin in Y-ProseMirror, but if someone is not familiar with CRDT, it can be difficult to understand the operations used there. I found an issue that it’s not really possible to remove the view dependency in SyncPlugin (issue-75). So, I tried to find my own workaround on how to use ProseMirror + Yjs in Node.js using updateYFragment.

I would like to ask if this workaround is safe to use.

  1. I don’t really understand why Yjs uses mapping in updateYFragment and if new Map() is safe to use.
  2. Can fragment.doc be null?
  3. Is it possible to listen to external Yjs updates and apply them to the state? I’m not sure how to do it.
  4. For initial load I used Node.fromJSON(schema, yDocToProsemirrorJSON(ydoc, 'default')), is it okay? Or is better way how to implement it?
  const ydoc = ...
  const fragment = ydoc.getXmlFragment('default')
  const doc = fragment.doc
  const node = Node.fromJSON(schema, yDocToProsemirrorJSON(ydoc, 'default'))

  const state = EditorState.create({
    doc: node,
    schema: schema,
    plugins: [
      new Plugin({
        appendTransaction(tr, oldState, newState) {
          doc.transact(() => {
            updateYFragment(doc, fragment, newState.doc, new Map())
          })
          return undefined
        },
      }),
    ],
  })

  const bold = schema.marks['bold']

  const tr = state.tr.insertText('HDDDHHELLO', 1).addMark(2, 5, bold.create())
  state.apply(tr)

Thank you guys :pray:

4 Likes