yjs / y-prosemirror

ProseMirror editor binding for Yjs
https://demos.yjs.dev/prosemirror/prosemirror.html
MIT License
328 stars 115 forks source link

TypeError: Cannot read properties of null (reading 'getUserByClientId') #128

Open milahu opened 1 year ago

milahu commented 1 year ago

Checklist

Describe the bug

the prosemirror-versions demo throws TypeError when permanentUserData is not passed to ySyncPlugin

To Reproduce

in yjs-demos/prosemirror-versions/prosemirror-versions.js

replace

        ySyncPlugin(yXmlFragment, { permanentUserData, colors }),

with

        ySyncPlugin(yXmlFragment),

edit, create snapshot edit, create snapshot click old snapshot -> error

Expected behavior

better error message or different error handling

Environment Information

y-prosemirror@1.2.0

Additional context

the error is caused by this call

  editorview.dispatch(editorview.state.tr.setMeta(ySyncPluginKey, { snapshot: Y.decodeSnapshot(version.snapshot), prevSnapshot: prevSnapshot == null ? Y.emptySnapshot : Y.decodeSnapshot(prevSnapshot) }))

which calls

y-prosemirror/src/plugins/sync-plugin.js

  /**
   * @param {Y.Snapshot} snapshot
   * @param {Y.Snapshot} prevSnapshot
   * @param {Object} pluginState
   */
  _renderSnapshot (snapshot, prevSnapshot, pluginState) {
    if (!snapshot) {
      snapshot = Y.snapshot(this.doc)
    }
    // clear mapping because we are going to rerender
    this.mapping = new Map()
    this.mux(() => {
      this.doc.transact((transaction) => {
        // before rendering, we are going to sanitize ops and split deleted ops
        // if they were deleted by seperate users.
        const pud = pluginState.permanentUserData
// pud == null
        if (pud) {
          pud.dss.forEach((ds) => {
            Y.iterateDeletedStructs(transaction, ds, (_item) => {})
          })
        }
        /**
         * @param {'removed'|'added'} type
         * @param {Y.ID} id
         */
        const computeYChange = (type, id) => {
          const user = type === 'added'
            ? pud.getUserByClientId(id.client)
            : pud.getUserByDeletedId(id)
// TypeError: Cannot read properties of null (reading 'getUserByClientId')
marek-baranowski commented 1 year ago

Hey @milahu, did you find some workaround maybe?

milahu commented 1 year ago

this works

        ySyncPlugin(yXmlFragment, { permanentUserData, colors }),
marek-baranowski commented 1 year ago

thank you. will need to tweak a bit as using hocuspocus with tiptap