Skip to content

Commit

Permalink
feat: clear history (prevwong#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
prevwong authored Jul 26, 2021
1 parent 8ab4687 commit 3117ba4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/utils/src/History.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const HISTORY_ACTIONS = {
THROTTLE: 'HISTORY_THROTTLE',
IGNORE: 'HISTORY_IGNORE',
MERGE: 'HISTORY_MERGE',
CLEAR: 'HISTORY_CLEAR',
};

export class History {
Expand Down Expand Up @@ -87,6 +88,11 @@ export class History {
this.add(patches, inversePatches);
}

clear() {
this.timeline = [];
this.pointer = -1;
}

canUndo() {
return this.pointer >= 0;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/utils/src/useMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type CallbacksFor<
history: {
undo: () => void;
redo: () => void;
clear: () => void;
throttle: (
rate?: number
) => Delete<
Expand Down Expand Up @@ -229,6 +230,12 @@ export function useMethods<
case HISTORY_ACTIONS.REDO: {
return history.redo(draft);
}
case HISTORY_ACTIONS.CLEAR: {
history.clear();
return {
...draft,
};
}

// TODO: Simplify History API
case HISTORY_ACTIONS.IGNORE:
Expand Down Expand Up @@ -277,6 +284,7 @@ export function useMethods<
HISTORY_ACTIONS.UNDO,
HISTORY_ACTIONS.REDO,
HISTORY_ACTIONS.IGNORE,
HISTORY_ACTIONS.CLEAR,
].includes(action.type as any)
) {
if (action.type === HISTORY_ACTIONS.THROTTLE) {
Expand Down Expand Up @@ -333,6 +341,11 @@ export function useMethods<
type: HISTORY_ACTIONS.REDO,
});
},
clear: () => {
return dispatch({
type: HISTORY_ACTIONS.CLEAR,
});
},
throttle: (rate) => {
return {
...actionTypes
Expand Down

0 comments on commit 3117ba4

Please sign in to comment.