@@ -86,6 +86,7 @@ export class ForkYDocPlugin extends BlockNoteExtension<{
86
86
private forkedState :
87
87
| {
88
88
originalFragment : Y . XmlFragment ;
89
+ undoStack : Y . UndoManager [ "undoStack" ] ;
89
90
forkedFragment : Y . XmlFragment ;
90
91
}
91
92
| undefined ;
@@ -114,6 +115,8 @@ export class ForkYDocPlugin extends BlockNoteExtension<{
114
115
const forkedFragment = this . findTypeInOtherYdoc ( originalFragment , doc ) ;
115
116
116
117
this . forkedState = {
118
+ undoStack : yUndoPluginKey . getState ( this . editor . prosemirrorState ) !
119
+ . undoManager . undoStack ,
117
120
originalFragment,
118
121
forkedFragment,
119
122
} ;
@@ -128,7 +131,9 @@ export class ForkYDocPlugin extends BlockNoteExtension<{
128
131
this . editor . _tiptapEditor . registerPlugin (
129
132
new SyncPlugin ( forkedFragment ) . plugins [ 0 ] ,
130
133
) ;
131
- this . editor . _tiptapEditor . registerPlugin ( new UndoPlugin ( ) . plugins [ 0 ] ) ;
134
+ this . editor . _tiptapEditor . registerPlugin (
135
+ new UndoPlugin ( { editor : this . editor } ) . plugins [ 0 ] ,
136
+ ) ;
132
137
// No need to register the cursor plugin again, it's a local fork
133
138
this . emit ( "forked" , true ) ;
134
139
}
@@ -146,17 +151,15 @@ export class ForkYDocPlugin extends BlockNoteExtension<{
146
151
this . editor . _tiptapEditor . unregisterPlugin ( ySyncPluginKey ) ;
147
152
this . editor . _tiptapEditor . unregisterPlugin ( yUndoPluginKey ) ;
148
153
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 ;
155
155
this . editor . extensions [ "ySyncPlugin" ] = new SyncPlugin ( originalFragment ) ;
156
156
this . editor . extensions [ "yCursorPlugin" ] = new CursorPlugin (
157
157
this . collaboration ! ,
158
158
) ;
159
- this . editor . extensions [ "yUndoPlugin" ] = new UndoPlugin ( ) ;
159
+ this . editor . extensions [ "yUndoPlugin" ] = new UndoPlugin ( {
160
+ editor : this . editor ,
161
+ } ) ;
162
+
160
163
// Register the plugins again, based on the original fragment
161
164
this . editor . _tiptapEditor . registerPlugin (
162
165
this . editor . extensions [ "ySyncPlugin" ] . plugins [ 0 ] ,
@@ -167,6 +170,21 @@ export class ForkYDocPlugin extends BlockNoteExtension<{
167
170
this . editor . _tiptapEditor . registerPlugin (
168
171
this . editor . extensions [ "yUndoPlugin" ] . plugins [ 0 ] ,
169
172
) ;
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
+ }
170
188
// Reset the forked state
171
189
this . forkedState = undefined ;
172
190
this . emit ( "forked" , false ) ;
0 commit comments