Skip to content

Commit

Permalink
add useUndoRedo flag in state (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
Segu-g authored Sep 30, 2021
1 parent 5488b92 commit d343bdf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/components/HeaderBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<q-space />
<q-btn
v-if="isDevelopment"
v-if="useUndoRedo"
unelevated
color="white"
text-color="secondary"
Expand All @@ -32,7 +32,7 @@
>元に戻す</q-btn
>
<q-btn
v-if="isDevelopment"
v-if="useUndoRedo"
unelevated
color="white"
text-color="secondary"
Expand Down Expand Up @@ -73,7 +73,7 @@ export default defineComponent({
const store = useStore();
const $q = useQuasar();
const isDevelopment = process.env.NODE_ENV === "development";
const useUndoRedo = computed(() => store.state.useUndoRedo);
const uiLocked = computed(() => store.getters.UI_LOCKED);
const canUndo = computed(() => store.getters.CAN_UNDO);
Expand Down Expand Up @@ -114,7 +114,7 @@ export default defineComponent({
};
return {
isDevelopment,
useUndoRedo,
uiLocked,
canUndo,
canRedo,
Expand Down
21 changes: 13 additions & 8 deletions src/store/command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Action } from "vuex";
import { Action, Store } from "vuex";

import { enablePatches, enableMapSet, Patch, Draft, Immer } from "immer";
import { applyPatch, Operation } from "rfc6902";
Expand Down Expand Up @@ -85,7 +85,7 @@ export function oldCreateCommandAction<S, P>(
};
}

export type PayloadRecipe<S, P> = (draft: Draft<S>, payload: P) => void;
export type PayloadRecipe<S, P> = (draft: S, payload: P) => void;
export type PayloadRecipeTree<S, M> = {
[K in keyof M]: PayloadRecipe<S, M[K]>;
};
Expand All @@ -96,6 +96,7 @@ export type PayloadMutationTree<S> = Record<string, PayloadMutation<S, any>>;
interface UndoRedoState {
undoCommands: Command[];
redoCommands: Command[];
useUndoRedo: boolean;
}

/**
Expand Down Expand Up @@ -130,10 +131,14 @@ export const createCommandMutation =
payloadRecipe: PayloadRecipe<S, P>
): Mutation<S, P> =>
(state: S, payload: P): void => {
const command = recordOperations(payloadRecipe)(state, payload);
applyPatch(state, command.redoOperations);
state.undoCommands.push(command);
state.redoCommands.splice(0);
if (state.useUndoRedo) {
const command = recordOperations(payloadRecipe)(state, payload);
applyPatch(state, command.redoOperations);
state.undoCommands.push(command);
state.redoCommands.splice(0);
} else {
payloadRecipe(state, payload);
}
};

const patchToOperation = (patch: Patch): Operation => ({
Expand All @@ -155,8 +160,8 @@ const recordOperations =
const [_, doPatches, undoPatches] = immer.produceWithPatches(
// Taking snapshots has negative effects on performance.
// This approach may cause a bottleneck.
JSON.parse(JSON.stringify(state)) as State,
(draft: Draft<S>) => recipe(draft, payload)
JSON.parse(JSON.stringify(state)) as S,
(draft: S) => recipe(draft, payload)
);
return {
redoOperations: doPatches.map(patchToOperation),
Expand Down
1 change: 1 addition & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const store = createStore<State, AllGetters, AllActions, AllMutations>({
nowPlayingContinuously: false,
undoCommands: [],
redoCommands: [],
useUndoRedo: isDevelopment,
useGpu: false,
isHelpDialogOpen: false,
isSettingDialogOpen: false,
Expand Down
1 change: 1 addition & 0 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type State = {
nowPlayingContinuously: boolean;
undoCommands: Command[];
redoCommands: Command[];
useUndoRedo: boolean;
useGpu: boolean;
isHelpDialogOpen: boolean;
isSettingDialogOpen: boolean;
Expand Down

0 comments on commit d343bdf

Please sign in to comment.