From ca08eac740e6cdff66b06136a2f9eb02e668fd55 Mon Sep 17 00:00:00 2001 From: andrewbeng89 Date: Mon, 22 Jun 2020 11:49:00 +0800 Subject: [PATCH] feat(#13): implement reset feature --- src/constants.ts | 2 ++ src/reset.ts | 33 +++++++++++++++++++++++++++++++++ src/undoRedo.ts | 11 +++++++++-- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/reset.ts diff --git a/src/constants.ts b/src/constants.ts index 4664095..fe714c0 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,7 @@ export const EMPTY_STATE = "emptyState"; +export const RESET_STATE = "resetState"; export const UPDATE_CAN_UNDO_REDO = "updateCanUndoRedo"; export const REDO = "redo"; export const UNDO = "undo"; export const CLEAR = "clear"; +export const RESET = "reset"; diff --git a/src/reset.ts b/src/reset.ts new file mode 100644 index 0000000..74aa225 --- /dev/null +++ b/src/reset.ts @@ -0,0 +1,33 @@ +import { RESET_STATE } from "./constants"; +import { + getConfig, + setConfig, + updateCanUndoRedo +} from "./utils-undo-redo"; + +export default ({ + paths, + store +}: { + paths: UndoRedoOptions[]; + store: any; +}) => async (namespace: string) => { + const config = getConfig(paths)(namespace); + + if (Object.keys(config).length) { + const done: [] = []; + const undone: [] = []; + config.newMutation = false; + store.commit(`${namespace}${RESET_STATE}`); + + config.newMutation = true; + + setConfig(paths)(namespace, { + ...config, + done, + undone + }); + + updateCanUndoRedo({ paths, store })(namespace); + } +}; diff --git a/src/undoRedo.ts b/src/undoRedo.ts index f452f4e..6ece77f 100644 --- a/src/undoRedo.ts +++ b/src/undoRedo.ts @@ -4,12 +4,14 @@ import { UPDATE_CAN_UNDO_REDO, REDO, UNDO, - CLEAR + CLEAR, + RESET } from "./constants"; import { getConfig, setConfig, updateCanUndoRedo } from "./utils-undo-redo"; import execRedo from "./redo"; import execUndo from "./undo"; import execClear from "./clear"; +import execReset from "./reset"; // Logic based on: https://github.com/anthonygore/vuex-undo-redo @@ -17,6 +19,7 @@ const noop = () => {}; export const undo = noop; export const redo = noop; export const clear = noop; +export const reset = noop; export const scaffoldState = (state: any) => ({ ...state, @@ -28,7 +31,8 @@ export const scaffoldActions = (actions: any) => ({ ...actions, undo, redo, - clear + clear, + reset }); export const scaffoldMutations = (mutations: any) => ({ @@ -159,6 +163,9 @@ export default (options: UndoRedoOptions = {}) => (store: any) => { case `${namespace}${CLEAR}`: await execClear({ paths, store })(namespace); break; + case `${namespace}${RESET}`: + await execReset({ paths, store })(namespace); + break; default: break; }