Skip to content

Commit 40fa688

Browse files
authored
feat: Add support for clear undo redo stack. (#106)
Add function clearing both undo a redo stack. New test verifying undo-redo functionality. Updated README.md.
1 parent 287a264 commit 40fa688

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ public class UndoRedoComponent {
382382

383383
canUndo = this.store.canUndo; // use in template or in ts
384384
canRedo = this.store.canRedo; // use in template or in ts
385+
clearStack = this.store.clearStack; // use in template or in ts
385386

386387
undo(): void {
387388
if (!this.canUndo()) return;
@@ -392,6 +393,10 @@ public class UndoRedoComponent {
392393
if (!this.canRedo()) return;
393394
this.store.redo();
394395
}
396+
397+
clearStack(): void {
398+
this.store.clearStack();
399+
}
395400
}
396401
```
397402

libs/ngrx-toolkit/src/lib/with-undo-redo.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ describe('withUndoRedo', () => {
2121
'canUndo',
2222
'canRedo',
2323
'undo',
24-
'redo'
24+
'redo',
25+
'clearStack'
2526
]);
2627
});
2728
});
@@ -190,5 +191,24 @@ describe('withUndoRedo', () => {
190191
expect(store.canRedo()).toBe(true);
191192
});
192193
}));
194+
195+
it('clears undo redo stack', () => {
196+
const Store = signalStore(
197+
{ providedIn: 'root' },
198+
withState(testState),
199+
withMethods(store => ({ update: (value: string) => patchState(store, { test: value }) })),
200+
withUndoRedo({ keys: testKeys })
201+
);
202+
203+
const store = TestBed.inject(Store);
204+
205+
store.update('Foo');
206+
store.update('Bar');
207+
store.undo();
208+
store.clearStack();
209+
210+
expect(store.canUndo()).toBe(false);
211+
expect(store.canRedo()).toBe(false);
212+
})
193213
});
194214
});

libs/ngrx-toolkit/src/lib/with-undo-redo.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export function withUndoRedo<
6161
methods: {
6262
undo: () => void;
6363
redo: () => void;
64+
clearStack: () => void;
6465
};
6566
}
6667
> {
@@ -126,6 +127,11 @@ export function withUndoRedo<
126127

127128
updateInternal();
128129
},
130+
clearStack(): void {
131+
undoStack.splice(0);
132+
redoStack.splice(0);
133+
updateInternal();
134+
},
129135
})),
130136
withHooks({
131137
onInit(store) {

0 commit comments

Comments
 (0)