diff --git a/packages/utils/src/History.ts b/packages/utils/src/History.ts index 3a861a034..ad4fcc86c 100644 --- a/packages/utils/src/History.ts +++ b/packages/utils/src/History.ts @@ -12,6 +12,7 @@ export const HISTORY_ACTIONS = { THROTTLE: 'HISTORY_THROTTLE', IGNORE: 'HISTORY_IGNORE', MERGE: 'HISTORY_MERGE', + CLEAR: 'HISTORY_CLEAR', }; export class History { @@ -87,6 +88,11 @@ export class History { this.add(patches, inversePatches); } + clear() { + this.timeline = []; + this.pointer = -1; + } + canUndo() { return this.pointer >= 0; } diff --git a/packages/utils/src/useMethods.ts b/packages/utils/src/useMethods.ts index 86003b23f..e62a25583 100644 --- a/packages/utils/src/useMethods.ts +++ b/packages/utils/src/useMethods.ts @@ -43,6 +43,7 @@ export type CallbacksFor< history: { undo: () => void; redo: () => void; + clear: () => void; throttle: ( rate?: number ) => Delete< @@ -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: @@ -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) { @@ -333,6 +341,11 @@ export function useMethods< type: HISTORY_ACTIONS.REDO, }); }, + clear: () => { + return dispatch({ + type: HISTORY_ACTIONS.CLEAR, + }); + }, throttle: (rate) => { return { ...actionTypes