@@ -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 ) ;
0 commit comments