Skip to content

Commit

Permalink
fix capturing after addToHistory=false - #111
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed May 27, 2022
1 parent 5092b77 commit 75c129e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/plugins/sync-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as object from 'lib0/object'
import * as set from 'lib0/set'
import { simpleDiff } from 'lib0/diff'
import * as error from 'lib0/error'
import { ySyncPluginKey } from './keys.js'
import { ySyncPluginKey, yUndoPluginKey } from './keys.js'
import * as Y from 'yjs'
import { absolutePositionToRelativePosition, relativePositionToAbsolutePosition } from '../lib.js'
import * as random from 'lib0/random'
Expand Down Expand Up @@ -157,6 +157,16 @@ export const ySyncPlugin = (yXmlFragment, {
if (pluginState.snapshot == null && pluginState.prevSnapshot == null) {
if (changedInitialContent || view.state.doc.content.findDiffStart(view.state.doc.type.createAndFill().content) !== null) {
changedInitialContent = true
if (pluginState.addToHistory === false && !pluginState.isChangeOrigin) {
const yUndoPluginState = yUndoPluginKey.getState(view.state)
/**
* @type {Y.UndoManager}
*/
const um = yUndoPluginState && yUndoPluginState.undoManager
if (um) {
um.stopCapturing()
}
}
pluginState.doc.transact(tr => {
tr.meta.set('addToHistory', pluginState.addToHistory)
binding._prosemirrorChanged(view.state.doc)
Expand Down Expand Up @@ -394,7 +404,7 @@ export class ProsemirrorBinding {

_prosemirrorChanged (doc) {
this.mux(() => {
this.doc.transact(() => {
this.doc.transact(tr => {
updateYFragment(this.doc, this.type, doc, this.mapping)
this.beforeTransactionSelection = getRelativeSelection(this, this.prosemirrorView.state)
}, ySyncPluginKey)
Expand Down
19 changes: 19 additions & 0 deletions test/y-prosemirror.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ export const testAddToHistory = tc => {
t.assert(yxml.length === 2 && yxml.get(0).length === 1, 'insertion was *not* undone')
}

export const testAddToHistoryIgnore = tc => {
const ydoc = new Y.Doc()
const view = createNewProsemirrorViewWithUndoManager(ydoc)
// perform two changes that are tracked by um - supposed to be merged into a single undo-manager item
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('123')))))
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('456')))))
const yxml = ydoc.get('prosemirror')
t.assert(yxml.length === 3 && yxml.get(0).length === 1, 'contains inserted content (1)')
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('abc')))).setMeta('addToHistory', false))
t.assert(yxml.length === 4 && yxml.get(0).length === 1, 'contains inserted content (2)')
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('xyz')))))
t.assert(yxml.length === 5 && yxml.get(0).length === 1, 'contains inserted content (3)')
undo(view.state)
t.assert(yxml.length === 4, 'insertion (3) was undone')
undo(view.state)
console.log(yxml.toString())
t.assert(yxml.length === 1 && yxml.get(0).toString() === '<paragraph>abc</paragraph>', 'insertion (1) was undone')
}

const createNewProsemirrorViewWithSchema = (y, schema, undoManager = false) => {
const view = new EditorView(null, {
// @ts-ignore
Expand Down

0 comments on commit 75c129e

Please sign in to comment.