Y-prosemiror get doc before update

I’m using y-prosemirror and using yXmlFragment as per this post.

When any user edits a doc I need to access the value of the Prosemirror doc before the changes are applied as well as the changed doc.

I’ve tried various approaches including yXmlFragment.observeDeep() however this only gives me a delta of then Y.XmlText property that changed. Is there any way to get get the oldValue like you can with yMap.observeDeep() event.changes.

For performance reasons, we don’t keep the content around. If you really need the “old value”, then you should clone the content into a separate object and update it whenever the observer fires. E.g.

let oldValue = ytype.toJSON() // or whatever you are using to read the content
ytype.observeDeep(() => {
  // perform your changes
  // update oldValue
  oldValue = ytype.toJSON()

I’m not endorsing this approach in any way. But it’s one approach that you could take.

@dmonad Thanks, I had considered your approach if I had no other option. My concern is the memory overhead needed to duplicate ever item.

I only need to access the oldValue when observeDeep() fires and certainly don’t expect the old content to be kept around. I assume the oldValue is available before the update transaction is applied? I am curious why you can get oldValue for yMap.observeDeep() but not for types other the yMap.

I did try ydoc.on( 'beforeTransaction', ( transaction, ydoc ) => {..} however as this fires before the transaction, transaction doesn’t provide any details on what is about to change.

For a Y.Map it’s very cheap to retain the previous values of specific keys. However, for Y.Text, we don’t want to clone the complete Y.Text value whenever something changes. That would be very expensive.

In the past I’ve been very protective of offering an option to compute and clone the Y.Text or Y.Array whenever something changes. While this is technically certainly possible, it is a bad pattern that is never necessary and leads to memory issues. So I recommend to rethink your architecture.