Skip to content

Commit

Permalink
プロジェクトの変更を保存するかを聞く部分を統一化 (#1239)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba authored Mar 1, 2023
1 parent 6db58f3 commit d91ca4d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 37 deletions.
67 changes: 47 additions & 20 deletions src/store/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,11 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({
action: createUILockAction(
async (context, { confirm }: { confirm?: boolean }) => {
if (confirm !== false && context.getters.IS_EDITED) {
const result: number = await window.electron.showQuestionDialog({
type: "info",
title: "警告",
message:
"プロジェクトの変更が保存されていません。\n" +
"変更を破棄してもよろしいですか?",
buttons: ["破棄", "キャンセル"],
cancelId: 1,
});
if (result == 1) {
const result = await context.dispatch(
"SAVE_OR_DISCARD_PROJECT_FILE",
{}
);
if (result == "canceled") {
return;
}
}
Expand Down Expand Up @@ -326,16 +321,14 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({
}

if (confirm !== false && context.getters.IS_EDITED) {
const result: number = await window.electron.showQuestionDialog({
type: "info",
title: "警告",
message:
"プロジェクトをロードすると現在のプロジェクトは破棄されます。\n" +
"変更を破棄してもよろしいですか?",
buttons: ["破棄", "キャンセル"],
cancelId: 1,
});
if (result == 1) {
const result = await context.dispatch(
"SAVE_OR_DISCARD_PROJECT_FILE",
{
additionalMessage:
"プロジェクトをロードすると現在のプロジェクトは破棄されます。",
}
);
if (result == "canceled") {
return false;
}
}
Expand Down Expand Up @@ -435,6 +428,40 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({
),
},

/**
* プロジェクトファイルを保存するか破棄するかキャンセルするかのダイアログを出して、保存する場合は保存する。
* 何を選択したかが返る。
* 保存に失敗した場合はキャンセル扱いになる。
*/
SAVE_OR_DISCARD_PROJECT_FILE: {
action: createUILockAction(async ({ dispatch }, { additionalMessage }) => {
let message = "プロジェクトの変更が保存されていません。";
if (additionalMessage) {
message += "\n" + additionalMessage;
}
message += "\n変更を保存しますか?";

const result: number = await window.electron.showQuestionDialog({
type: "info",
title: "警告",
message,
buttons: ["保存", "破棄", "キャンセル"],
cancelId: 2,
defaultId: 2,
});
if (result == 0) {
const saved = await dispatch("SAVE_PROJECT_FILE", {
overwrite: true,
});
return saved ? "saved" : "canceled";
} else if (result == 1) {
return "discarded";
} else {
return "canceled";
}
}),
},

IS_EDITED: {
getter(state, getters) {
return (
Expand Down
6 changes: 6 additions & 0 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,12 @@ export type ProjectStoreTypes = {
action(payload: { overwrite?: boolean }): boolean;
};

SAVE_OR_DISCARD_PROJECT_FILE: {
action(palyoad: {
additionalMessage?: string;
}): "saved" | "discarded" | "canceled";
};

IS_EDITED: {
getter: boolean;
};
Expand Down
19 changes: 2 additions & 17 deletions src/store/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,23 +300,8 @@ export const uiStore = createPartialStore<UiStoreTypes>({
CHECK_EDITED_AND_NOT_SAVE: {
async action({ dispatch, getters }) {
if (getters.IS_EDITED) {
const result: number = await window.electron.showQuestionDialog({
type: "info",
title: "警告",
message:
"プロジェクトの変更が保存されていません。\n" +
"変更を保存しますか?",
buttons: ["保存", "破棄", "キャンセル"],
cancelId: 2,
defaultId: 2,
});
if (result == 0) {
const saved = await dispatch("SAVE_PROJECT_FILE", {
overwrite: true,
});
if (saved == false) return;
}
if (result == 2) {
const result = await dispatch("SAVE_OR_DISCARD_PROJECT_FILE", {});
if (result == "canceled") {
return;
}
}
Expand Down

0 comments on commit d91ca4d

Please sign in to comment.