diff --git a/ipcTypes.js b/ipcTypes.js index 61bacc7..e8ae6a9 100644 --- a/ipcTypes.js +++ b/ipcTypes.js @@ -23,3 +23,6 @@ exports.OPEN_FILE = "open-file"; exports.OPEN_SETTING_WINDOW = "open-setting-window"; exports.OPEN_LOCATION_DIALOG = "open-location-dialog"; exports.SAVE_LOCATION_PATH = "save-location-path"; + +// more details +exports.SAVE_EDIT_FILE = "save-edit-file"; \ No newline at end of file diff --git a/main.js b/main.js index f4d5221..1b11777 100644 --- a/main.js +++ b/main.js @@ -150,6 +150,15 @@ app.whenReady().then(() => { ipcMain.handle(ipcTypes.OPEN_CONTEXT_MENU, (_e) => { return showContextMenu(_e) }) + ipcMain.handle(ipcTypes.SAVE_EDIT_FILE, (_e) => { + return dialog.showMessageBox({ + type: "question", + buttons: ["取消", "确定"], + title: "文件修改未保存", + message: "是否要保存正在关闭文件的修改" + }) + }) + }) app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() diff --git a/preload.js b/preload.js index 7662f49..8577b00 100644 --- a/preload.js +++ b/preload.js @@ -73,4 +73,7 @@ contextBridge.exposeInMainWorld("myApp", { // setting window openSettingDialog: () => ipcRenderer.invoke(ipcTypes.OPEN_LOCATION_DIALOG), saveSettingPath: (path) => settingStore.set("savedLocation", path), + + // more details + showSaveBox: () => ipcRenderer.invoke(ipcTypes.SAVE_EDIT_FILE), }) diff --git a/src/App.jsx b/src/App.jsx index 70ad7b9..9dd142a 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -145,11 +145,13 @@ function App() { const saveContent = () => { if (activeFile.current) { - const { path, body } = activeFile.current; + const { id, path, body } = activeFile.current; fileDealer.writeFile(path, body).then(() => { - setUnSavedFiles( - unSavedFiles.filter((id) => id !== activeFile.current.id) - ); + if (!unSavedFiles.filter((fileID) => fileID !== id)) { + setUnSavedFiles(unSavedFiles.filter((fileID) => fileID !== id)); + } else { + setUnSavedFiles([]); + } }); } }; @@ -158,11 +160,38 @@ function App() { const closeFile = (fileID) => { const afterCloseIDs = openedFilesID.filter((itemID) => itemID !== fileID); setOpenedFilesID(afterCloseIDs); - if (fileID === activeFileID) { - if (afterCloseIDs.length > 0) { - setActiveFileID(afterCloseIDs[0]); - } else { - setActiveFileID(""); + + if (unSavedFiles.includes(fileID)) { + window.myApp.showSaveBox().then((res) => { + if (res.response === 1) { + saveContent(); + if (fileID === activeFileID) { + if (afterCloseIDs.length > 0) { + setActiveFileID(afterCloseIDs[0]); + } else { + setActiveFileID(""); + } + } + } else if (res.response === 0) { + setUnSavedFiles( + unSavedFiles.filter((id) => id !== activeFile.current.id) + ); + if (fileID === activeFileID) { + if (afterCloseIDs.length > 0) { + setActiveFileID(afterCloseIDs[0]); + } else { + setActiveFileID(""); + } + } + } + }); + } else { + if (fileID === activeFileID) { + if (afterCloseIDs.length > 0) { + setActiveFileID(afterCloseIDs[0]); + } else { + setActiveFileID(""); + } } } };