yjs / y-prosemirror

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

Undo stops working after late-registering another prosemirror plugin with yjs 13.5.32+ due to new destroy logic in yjs. #114

Open ascott18 opened 2 years ago

ascott18 commented 2 years ago

Checklist

Describe the bug Scenario is as follows:

Expected behavior The UndoManager should not be destroyed when the EditorView is destroyed, since the UndoManager is initialized by the plugin, not by the plugin's EditorView. The plugin itself and the plugin's EditorView have different lifecycles.

Initializing the UndoManager along with the EditorView (e.g. in view: view => { ... } is not a good solution, since it will cause undo state to be wiped out whenever a prosemirror plugin is registered. Imagine a prosemirror editor that gradually registered plugins as a user toggles settings, or one that only registers plugins on demand).

I think the best solution would be to just remove the destroy hook from the EditorView of undo-plugin. Just leave the lifecycle of the UndoManager tied to the lifecycle of the YDoc (which it already hooks into in UndoManager's ctor).

Environment Information

dmonad commented 2 years ago

This looks like a duplicate of https://github.com/yjs/y-prosemirror/issues/102, is that right?

It is basically not possible to distinguish between destroy and reconfiguration which makes this issue hard to deal with. I'd prefer to continue the discussion in the existing tichet. Feel free to close this one and reopen the other ticket.

Alecyrus commented 2 years ago

https://github.com/ueberdosis/tiptap/issues/2761

ascott18 commented 2 years ago

@dmonad Yes, it looks like the same issue.

hamflx commented 2 years ago

I had the same problem and this is how I fixed it:

import { Collaboration } from '@tiptap/extension-collaboration'
import { ySyncPlugin, yUndoPlugin, yUndoPluginKey } from 'y-prosemirror'

export const CollaborationExtension = Collaboration.extend({
  addProseMirrorPlugins () {
    const fragment = this.options.fragment
      ? this.options.fragment
      : this.options.document.getXmlFragment(this.options.field)

    const yUndoPluginInstance = yUndoPlugin()
    const originalUndoPluginView = yUndoPluginInstance.spec.view
    yUndoPluginInstance.spec.view = view => {
      const undoManager = yUndoPluginKey.getState(view.state).undoManager
      if (undoManager.restore) {
        undoManager.restore()
        undoManager.restore = () => {}
      }
      const viewRet = originalUndoPluginView(view)
      return {
        destroy: () => {
          const hasUndoManSelf = undoManager.trackedOrigins.has(undoManager)
          const observers = undoManager._observers
          undoManager.restore = () => {
            if (hasUndoManSelf) {
              undoManager.trackedOrigins.add(undoManager)
            }
            undoManager.doc.on('afterTransaction', undoManager.afterTransactionHandler)
            undoManager._observers = observers
          }
          viewRet.destroy()
        }
      }
    }
    return [
      ySyncPlugin(fragment),
      yUndoPluginInstance
    ]
  }
})
LeeSanity commented 2 years ago

yUndoManager constructor has an optioanal undoManager parameter, which supports init an external undoManager instance and pass in. In our use case, we put many prosemirror editors and some other components into one Y.Doc, and use a global undoManager to listen changes, in this way, even some editor deleted from the dom (Y.Doc), we should't destroy the undoManager instance. So... I came up with two approches:

what do you think? @dmonad

dmonad commented 2 years ago

Hi @LeeSanity,

Let's go with the second approach to keep compatibility. Can you please create a PR that adds a parameter (shouldDestroyUndoManager) with some documentation? Happy to assist & merge.

hamflx commented 2 years ago

This PR https://github.com/yjs/yjs/pull/449 mentioned by @LeeSanity works fine for external UndoManager, but for internally created UndoManager, if shouldDestroyUndoManager is set to true, then there is still this problem, otherwise set to false, then the user needs to destroy the UndoManager instance manually..

I provide a solution here to solve both problems, implement a UndoManagerDelayedDestroy class inherited from UndoManager, providing two methods: preventDestroy and delayedDestroy. The delayedDestroy method is used to call super.destroy method in the next tick to destroy the UndoManager instance. The preventDestroy method is used to prevent the destruction.

see: https://github.com/hamflx/y-prosemirror/commit/fd79c58145e6cad99dddd8d7a2c9a4d7f582d8dc

The code is as follows.

// replace "new UndoManager"
// const _undoManager = undoManager || new UndoManagerDelayedDestroy(ystate.type, {

// Call undoManager.preventDestroy at the beginning of the view function
// const undoManager = yUndoPluginKey.getState(view.state).undoManager
// undoManager.preventDestroy()

// Call `undoManager.delayedDestroy` instead of `undoManager.destroy`.
//     if (typeof undoManager.delayedDestroy === 'function') {
//       undoManager.delayedDestroy()

class UndoManagerDelayedDestroy extends UndoManager {
  constructor (type, opts) {
    super(type, opts)
    this.destroyCounter = 0
  }

  preventDestroy () {
    this.destroyCounter++
  }

  delayedDestroy () {
    const memorizedCounter = this.destroyCounter
    queue(() => this.destroyCounter === memorizedCounter && super.destroy())
  }
}

const queue = fn => Promise.resolve().then(fn)

Translated with www.DeepL.com/Translator (free version)

LeeSanity commented 2 years ago

You are right @hamflx , my proposal just works for external UndoManager use cases. For internally created UndoManager, I think it's the problem between prosemirror editor & yUndoPlugin. There lacks one way to detect plugin reconfig.

hamflx commented 2 years ago

If there is no better way, is it possible to consider my proposal?

@dmonad

shlroland commented 1 year ago

@dmonad @hamflx @LeeSanity hi,Is there any progress on this issue? I have also encountered this problem while using remirror and I hope that an official fix will be available soon.

jsonkao commented 6 months ago

Hi, I also encountered this issue — I was sharing an UndoManager across multiple Prosemirror editors and it was being accidentally destroyed. I used a simplified version of @hamflx's fix to fix this (below), but it would be nice for a solution to be built into y-prosemirror.

export class IndestructibleUndoManager extends Y.UndoManager {
    constructor(type, opts) {
        super(type, opts);
    }

    destroy(actuallyDestroy = false) {
        if (actuallyDestroy) super.destroy();
        // y-prosemirror call does not end up destroying
    }
}