Skip to content

Commit 95a72b7

Browse files
authored
fix(ai): undo-redo after accepting/rejecting changes will undo as expected (#1752)
1 parent 81088d5 commit 95a72b7

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

packages/core/src/editor/BlockNoteExtensions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export const getBlockNoteExtensions = <
116116

117117
if (opts.collaboration) {
118118
ret["ySyncPlugin"] = new SyncPlugin(opts.collaboration.fragment);
119-
ret["yUndoPlugin"] = new UndoPlugin();
119+
ret["yUndoPlugin"] = new UndoPlugin({ editor: opts.editor });
120120

121121
if (opts.collaboration.provider?.awareness) {
122122
ret["yCursorPlugin"] = new CursorPlugin(opts.collaboration);

packages/core/src/extensions/Collaboration/ForkYDocPlugin.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export class ForkYDocPlugin extends BlockNoteExtension<{
8686
private forkedState:
8787
| {
8888
originalFragment: Y.XmlFragment;
89+
undoStack: Y.UndoManager["undoStack"];
8990
forkedFragment: Y.XmlFragment;
9091
}
9192
| undefined;
@@ -114,6 +115,8 @@ export class ForkYDocPlugin extends BlockNoteExtension<{
114115
const forkedFragment = this.findTypeInOtherYdoc(originalFragment, doc);
115116

116117
this.forkedState = {
118+
undoStack: yUndoPluginKey.getState(this.editor.prosemirrorState)!
119+
.undoManager.undoStack,
117120
originalFragment,
118121
forkedFragment,
119122
};
@@ -128,7 +131,9 @@ export class ForkYDocPlugin extends BlockNoteExtension<{
128131
this.editor._tiptapEditor.registerPlugin(
129132
new SyncPlugin(forkedFragment).plugins[0],
130133
);
131-
this.editor._tiptapEditor.registerPlugin(new UndoPlugin().plugins[0]);
134+
this.editor._tiptapEditor.registerPlugin(
135+
new UndoPlugin({ editor: this.editor }).plugins[0],
136+
);
132137
// No need to register the cursor plugin again, it's a local fork
133138
this.emit("forked", true);
134139
}
@@ -146,17 +151,15 @@ export class ForkYDocPlugin extends BlockNoteExtension<{
146151
this.editor._tiptapEditor.unregisterPlugin(ySyncPluginKey);
147152
this.editor._tiptapEditor.unregisterPlugin(yUndoPluginKey);
148153

149-
const { originalFragment, forkedFragment } = this.forkedState;
150-
if (keepChanges) {
151-
// Apply any changes that have been made to the fork, onto the original doc
152-
const update = Y.encodeStateAsUpdate(forkedFragment.doc!);
153-
Y.applyUpdate(originalFragment.doc!, update);
154-
}
154+
const { originalFragment, forkedFragment, undoStack } = this.forkedState;
155155
this.editor.extensions["ySyncPlugin"] = new SyncPlugin(originalFragment);
156156
this.editor.extensions["yCursorPlugin"] = new CursorPlugin(
157157
this.collaboration!,
158158
);
159-
this.editor.extensions["yUndoPlugin"] = new UndoPlugin();
159+
this.editor.extensions["yUndoPlugin"] = new UndoPlugin({
160+
editor: this.editor,
161+
});
162+
160163
// Register the plugins again, based on the original fragment
161164
this.editor._tiptapEditor.registerPlugin(
162165
this.editor.extensions["ySyncPlugin"].plugins[0],
@@ -167,6 +170,21 @@ export class ForkYDocPlugin extends BlockNoteExtension<{
167170
this.editor._tiptapEditor.registerPlugin(
168171
this.editor.extensions["yUndoPlugin"].plugins[0],
169172
);
173+
174+
// Reset the undo stack to the original undo stack
175+
yUndoPluginKey.getState(
176+
this.editor.prosemirrorState,
177+
)!.undoManager.undoStack = undoStack;
178+
179+
if (keepChanges) {
180+
// Apply any changes that have been made to the fork, onto the original doc
181+
const update = Y.encodeStateAsUpdate(
182+
forkedFragment.doc!,
183+
Y.encodeStateVector(originalFragment.doc!),
184+
);
185+
// Applying this change will add to the undo stack, allowing it to be undone normally
186+
Y.applyUpdate(originalFragment.doc!, update, this.editor);
187+
}
170188
// Reset the forked state
171189
this.forkedState = undefined;
172190
this.emit("forked", false);

packages/core/src/extensions/Collaboration/UndoPlugin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { yUndoPlugin } from "y-prosemirror";
2+
import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
23
import { BlockNoteExtension } from "../../editor/BlockNoteExtension.js";
34

45
export class UndoPlugin extends BlockNoteExtension {
56
public static key() {
67
return "yUndoPlugin";
78
}
89

9-
constructor() {
10+
constructor({ editor }: { editor: BlockNoteEditor<any, any, any> }) {
1011
super();
11-
this.addProsemirrorPlugin(yUndoPlugin());
12+
this.addProsemirrorPlugin(yUndoPlugin({ trackedOrigins: [editor] }));
1213
}
1314

1415
public get priority() {

0 commit comments

Comments
 (0)