Skip to content

Commit

Permalink
feat: tip on either save the editing file or not
Browse files Browse the repository at this point in the history
  • Loading branch information
wujinhjun committed Sep 23, 2022
1 parent b61e334 commit f139f0f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
3 changes: 3 additions & 0 deletions ipcTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
9 changes: 9 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
47 changes: 38 additions & 9 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
}
});
}
};
Expand All @@ -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("");
}
}
}
};
Expand Down

0 comments on commit f139f0f

Please sign in to comment.