Skip to content

Commit

Permalink
feat(#13): implement reset feature
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbeng89 committed Jun 22, 2020
1 parent f910698 commit ca08eac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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";
33 changes: 33 additions & 0 deletions src/reset.ts
Original file line number Diff line number Diff line change
@@ -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);
}
};
11 changes: 9 additions & 2 deletions src/undoRedo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ 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

const noop = () => {};
export const undo = noop;
export const redo = noop;
export const clear = noop;
export const reset = noop;

export const scaffoldState = (state: any) => ({
...state,
Expand All @@ -28,7 +31,8 @@ export const scaffoldActions = (actions: any) => ({
...actions,
undo,
redo,
clear
clear,
reset
});

export const scaffoldMutations = (mutations: any) => ({
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit ca08eac

Please sign in to comment.