Skip to content

Commit 7524cb2

Browse files
committed
feat: clear history (#281)
1 parent ff8c1e7 commit 7524cb2

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

packages/utils/src/History.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const HISTORY_ACTIONS = {
1212
THROTTLE: 'HISTORY_THROTTLE',
1313
IGNORE: 'HISTORY_IGNORE',
1414
MERGE: 'HISTORY_MERGE',
15+
CLEAR: 'HISTORY_CLEAR',
1516
};
1617

1718
export class History {
@@ -87,6 +88,11 @@ export class History {
8788
this.add(patches, inversePatches);
8889
}
8990

91+
clear() {
92+
this.timeline = [];
93+
this.pointer = -1;
94+
}
95+
9096
canUndo() {
9197
return this.pointer >= 0;
9298
}

packages/utils/src/useMethods.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type CallbacksFor<
4343
history: {
4444
undo: () => void;
4545
redo: () => void;
46+
clear: () => void;
4647
throttle: (
4748
rate?: number
4849
) => Delete<
@@ -229,6 +230,12 @@ export function useMethods<
229230
case HISTORY_ACTIONS.REDO: {
230231
return history.redo(draft);
231232
}
233+
case HISTORY_ACTIONS.CLEAR: {
234+
history.clear();
235+
return {
236+
...draft,
237+
};
238+
}
232239

233240
// TODO: Simplify History API
234241
case HISTORY_ACTIONS.IGNORE:
@@ -277,6 +284,7 @@ export function useMethods<
277284
HISTORY_ACTIONS.UNDO,
278285
HISTORY_ACTIONS.REDO,
279286
HISTORY_ACTIONS.IGNORE,
287+
HISTORY_ACTIONS.CLEAR,
280288
].includes(action.type as any)
281289
) {
282290
if (action.type === HISTORY_ACTIONS.THROTTLE) {
@@ -333,6 +341,11 @@ export function useMethods<
333341
type: HISTORY_ACTIONS.REDO,
334342
});
335343
},
344+
clear: () => {
345+
return dispatch({
346+
type: HISTORY_ACTIONS.CLEAR,
347+
});
348+
},
336349
throttle: (rate) => {
337350
return {
338351
...actionTypes

0 commit comments

Comments
 (0)