Skip to content

Commit

Permalink
Mark all transactions originating from this plugin as "not for history"
Browse files Browse the repository at this point in the history
Changes coming through Yjs are not changes from the local user, but rather they represent
changes in other places in the system. Such changes do not need to be tracked and made
undo-able. This plugin also triggers prosemirror changes when rendering snapshots
and when forcing re-renders, which do a full document replace step.

By marking these with the `"addToHistory"` meta the prosemirror-history plugin will be
able to ignore these changes for the purposes of providing undo/redo functionality on the
editor model level, without affecting a Yjs document level undo/redo.
  • Loading branch information
ankon committed Feb 6, 2022
1 parent 039be96 commit dbcf1cc
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/plugins/sync-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ export class ProsemirrorBinding {
this._domSelectionInView = null
}

/**
* Create a transaction for changing the prosemirror state.
*
* @returns
*/
get _tr () {
return this.prosemirrorView.state.tr.setMeta('addToHistory', false)
}

_isLocalCursorInView () {
if (!this.prosemirrorView.hasFocus()) return false
if (environment.isBrowser && this._domSelectionInView === null) {
Expand Down Expand Up @@ -259,15 +268,15 @@ export class ProsemirrorBinding {
if (!prevSnapshot) {
prevSnapshot = Y.createSnapshot(Y.createDeleteSet(), new Map())
}
this.prosemirrorView.dispatch(this.prosemirrorView.state.tr.setMeta(ySyncPluginKey, { snapshot, prevSnapshot }))
this.prosemirrorView.dispatch(this._tr.setMeta(ySyncPluginKey, { snapshot, prevSnapshot }))
}

unrenderSnapshot () {
this.mapping = new Map()
this.mux(() => {
const fragmentContent = this.type.toArray().map(t => createNodeFromYElement(/** @type {Y.XmlElement} */ (t), this.prosemirrorView.state.schema, this.mapping)).filter(n => n !== null)
// @ts-ignore
const tr = this.prosemirrorView.state.tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(new PModel.Fragment(fragmentContent), 0, 0))
const tr = this._tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(new PModel.Fragment(fragmentContent), 0, 0))
tr.setMeta(ySyncPluginKey, { snapshot: null, prevSnapshot: null })
this.prosemirrorView.dispatch(tr)
})
Expand All @@ -278,7 +287,7 @@ export class ProsemirrorBinding {
this.mux(() => {
const fragmentContent = this.type.toArray().map(t => createNodeFromYElement(/** @type {Y.XmlElement} */ (t), this.prosemirrorView.state.schema, this.mapping)).filter(n => n !== null)
// @ts-ignore
const tr = this.prosemirrorView.state.tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(new PModel.Fragment(fragmentContent), 0, 0))
const tr = this._tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(new PModel.Fragment(fragmentContent), 0, 0))
this.prosemirrorView.dispatch(tr.setMeta(ySyncPluginKey, { isChangeOrigin: true }))
})
}
Expand Down Expand Up @@ -323,7 +332,7 @@ export class ProsemirrorBinding {
}
}).filter(n => n !== null)
// @ts-ignore
const tr = this.prosemirrorView.state.tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(new PModel.Fragment(fragmentContent), 0, 0))
const tr = this._tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(new PModel.Fragment(fragmentContent), 0, 0))
this.prosemirrorView.dispatch(tr.setMeta(ySyncPluginKey, { isChangeOrigin: true }))
}, ySyncPluginKey)
})
Expand Down Expand Up @@ -351,7 +360,7 @@ export class ProsemirrorBinding {
transaction.changedParentTypes.forEach(delType)
const fragmentContent = this.type.toArray().map(t => createNodeIfNotExists(/** @type {Y.XmlElement | Y.XmlHook} */ (t), this.prosemirrorView.state.schema, this.mapping)).filter(n => n !== null)
// @ts-ignore
let tr = this.prosemirrorView.state.tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(new PModel.Fragment(fragmentContent), 0, 0))
let tr = this._tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(new PModel.Fragment(fragmentContent), 0, 0))
restoreRelativeSelection(tr, this.beforeTransactionSelection, this)
tr = tr.setMeta(ySyncPluginKey, { isChangeOrigin: true })
if (this.beforeTransactionSelection !== null && this._isLocalCursorInView()) {
Expand Down

0 comments on commit dbcf1cc

Please sign in to comment.