@@ -134,6 +134,7 @@ export function useGridEditing() {
134134 setDeletes ( new Set ( ) ) ;
135135 undoStack . current = [ ] ;
136136 redoStack . current = [ ] ;
137+ bumpVersion ( ) ;
137138 } , [ ] ) ;
138139
139140 const applyAction = useCallback ( ( action : EditAction ) : EditAction => {
@@ -143,7 +144,11 @@ export function useGridEditing() {
143144 const next = new Map ( prev ) ;
144145 const rowChanges = [ ...( next . get ( action . rowIndex ) ?? [ ] ) ] ;
145146 const existing = rowChanges . findIndex ( ( c ) => c . column === action . column ) ;
146- if ( action . newValue === action . oldValue || ( action . oldValue === undefined && action . newValue === action . oldValue ) ) {
147+ if ( existing >= 0 && action . newValue === rowChanges [ existing ] . originalValue ) {
148+ rowChanges . splice ( existing , 1 ) ;
149+ if ( rowChanges . length === 0 ) next . delete ( action . rowIndex ) ;
150+ else next . set ( action . rowIndex , rowChanges ) ;
151+ } else if ( action . newValue === action . oldValue ) {
147152 if ( existing >= 0 ) {
148153 rowChanges . splice ( existing , 1 ) ;
149154 if ( rowChanges . length === 0 ) next . delete ( action . rowIndex ) ;
@@ -172,6 +177,10 @@ export function useGridEditing() {
172177 return { ...action , index : - 1 } ;
173178 }
174179 case "deleteRow" : {
180+ if ( action . rowIndex === - 1 ) {
181+ setInserts ( ( prev ) => prev . slice ( 0 , - 1 ) ) ;
182+ return { type : "insertRow" , index : 0 } ; // forward action for redo
183+ }
175184 setDeletes ( ( prev : Set < number > ) => {
176185 const next = new Set ( prev ) ;
177186 if ( next . has ( action . rowIndex ) ) next . delete ( action . rowIndex ) ;
@@ -194,7 +203,9 @@ export function useGridEditing() {
194203 const redo = useCallback ( ( ) => {
195204 const action = redoStack . current . pop ( ) ;
196205 if ( ! action ) return ;
197- const reverse = applyAction ( reverseAction ( action ) ) ;
206+ const reverse = action . type === "insertRow"
207+ ? applyAction ( action )
208+ : applyAction ( reverseAction ( action ) ) ;
198209 undoStack . current . push ( reverse ) ;
199210 bumpVersion ( ) ;
200211 } , [ applyAction ] ) ;
0 commit comments