diff --git a/src/store/project.ts b/src/store/project.ts index 58e47b6674..755cafa1c0 100755 --- a/src/store/project.ts +++ b/src/store/project.ts @@ -40,16 +40,11 @@ export const projectStore = createPartialStore({ 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; } } @@ -326,16 +321,14 @@ export const projectStore = createPartialStore({ } 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; } } @@ -435,6 +428,40 @@ export const projectStore = createPartialStore({ ), }, + /** + * プロジェクトファイルを保存するか破棄するかキャンセルするかのダイアログを出して、保存する場合は保存する。 + * 何を選択したかが返る。 + * 保存に失敗した場合はキャンセル扱いになる。 + */ + 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 ( diff --git a/src/store/type.ts b/src/store/type.ts index 6054db502c..85e4818d00 100644 --- a/src/store/type.ts +++ b/src/store/type.ts @@ -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; }; diff --git a/src/store/ui.ts b/src/store/ui.ts index e044623a0f..38908d18b9 100644 --- a/src/store/ui.ts +++ b/src/store/ui.ts @@ -300,23 +300,8 @@ export const uiStore = createPartialStore({ 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; } }