Skip to content

Commit 72b9273

Browse files
reorganizing the logic to make sure app.addRecentDocument() is only called after the file is written to disk (#136)
1 parent 7d6bf83 commit 72b9273

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

electron/app/js/project.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,34 +169,40 @@ async function openProjectFile(targetWindow, projectFile, isDirty) {
169169
// request the existing project file, prompting the user if needed.
170170
// return null values if no project file was established or selected.
171171
// usually called by the confirm-project-file IPC invocation.
172+
//
173+
// This method is always used in the "Save All" flow.
174+
//
172175
async function confirmProjectFile(targetWindow) {
173176
let projectFile = _getProjectFilePath(targetWindow);
174-
let projectName = null;
175-
let projectUuid = null;
176-
if(!projectFile) {
177-
projectFile = await _chooseProjectSaveFile(targetWindow);
178-
projectName = projectFile ? _generateProjectName(projectFile) : null;
179-
projectUuid = projectFile ? _generateProjectUuid() : null;
180-
if (projectFile) {
181-
getLogger().debug('confirmProjectFile adding %s to recent documents', projectFile);
182-
app.addRecentDocument(projectFile);
183-
}
177+
if (projectFile) {
178+
// if the project file exists, no need to worry about
179+
// the project name and project UUID because they are
180+
// already defined in the file.
181+
//
182+
return [projectFile, null, null, false];
183+
} else {
184+
return chooseProjectFile(targetWindow);
184185
}
185-
return [projectFile, projectName, projectUuid];
186186
}
187187

188188
// choose a new project file for save.
189189
// return null values if no project file was established or selected.
190190
// usually called by the choose-project-file IPC invocation.
191+
//
192+
// This method is used directly in the "Save As" flow and
193+
// indirectly in the "Save All" flow.
194+
//
191195
async function chooseProjectFile(targetWindow) {
192196
const projectFile = await _chooseProjectSaveFile(targetWindow);
193-
const projectName = projectFile ? _generateProjectName(projectFile) : null;
194-
const projectUuid = projectFile ? _generateProjectUuid() : null;
197+
let projectName = null;
198+
let projectUuid = null;
199+
let isNewFile = false;
195200
if (projectFile) {
196-
getLogger().debug('chooseProjectFile adding %s to recent documents', projectFile);
197-
app.addRecentDocument(projectFile);
201+
projectName = _generateProjectName(projectFile);
202+
projectUuid = _generateProjectUuid();
203+
isNewFile = !await fsUtils.exists(projectFile);
198204
}
199-
return [projectFile, projectName, projectUuid];
205+
return [projectFile, projectName, projectUuid, isNewFile];
200206
}
201207

202208
// initiate the save process by sending a message to the web app.
@@ -213,7 +219,7 @@ function startSaveProjectAs(targetWindow) {
213219

214220
// save the specified project and model contents to the project file.
215221
// usually invoked by the save-project IPC invocation.
216-
async function saveProject(targetWindow, projectFile, projectContents, externalFileContents, showErrors = true) {
222+
async function saveProject(targetWindow, projectFile, projectContents, externalFileContents, isNewFile, showErrors = true) {
217223
// the result will contain only sections that were updated due to save, such as model.archiveFiles
218224
const saveResult = {
219225
isProjectFileSaved: false,
@@ -237,6 +243,10 @@ async function saveProject(targetWindow, projectFile, projectContents, externalF
237243
if (saveResult.isProjectFileSaved) {
238244
const wktWindow = require('./wktWindow');
239245
wktWindow.setTitleFileName(targetWindow, projectFile, false);
246+
if (isNewFile) {
247+
app.addRecentDocument(projectFile);
248+
}
249+
240250
try {
241251
saveResult['model'] = await _saveExternalFileContents(_getProjectDirectory(targetWindow), externalFileContents);
242252
saveResult.areModelFilesSaved = true;

electron/app/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,8 @@ class Main {
616616
});
617617

618618
ipcMain.handle('save-project',async (event, projectFile, projectContents,
619-
externalFileContents, displayElectronSideErrors = true) => {
620-
return project.saveProject(event.sender.getOwnerBrowserWindow(), projectFile, projectContents, externalFileContents, displayElectronSideErrors);
619+
externalFileContents, isNewFile, displayElectronSideErrors = true) => {
620+
return project.saveProject(event.sender.getOwnerBrowserWindow(), projectFile, projectContents, externalFileContents, isNewFile, displayElectronSideErrors);
621621
});
622622

623623
ipcMain.handle('close-project', async (event, keepWindow) => {

webui/src/js/utils/project-io.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ define(['knockout', 'models/wkt-project', 'utils/i18n'],
2020
const projectNotSaved = !project.getProjectFileName();
2121

2222
if(forceSave || project.isDirty() || projectNotSaved) {
23-
const [projectFile, projectName, projectUuid] = await window.api.ipc.invoke('confirm-project-file');
23+
const [projectFile, projectName, projectUuid, isNewFile] = await window.api.ipc.invoke('confirm-project-file');
2424

2525
// if the project file is null, the user cancelled when selecting a new file.
2626
if(!projectFile) {
2727
return {saved: false, reason: i18n.t('project-io-user-cancelled-save-message')};
2828
}
2929

30-
return saveToFile(projectFile, projectName, projectUuid, displayElectronSideErrors);
30+
return saveToFile(projectFile, projectName, projectUuid, isNewFile, displayElectronSideErrors);
3131
}
3232

3333
return {saved: true};
3434
};
3535

3636
// select a new project file for the project, and save the project contents to the specified file.
3737
this.saveProjectAs = async() => {
38-
const [projectFile, projectName, projectUuid] = await window.api.ipc.invoke('choose-project-file');
38+
const [projectFile, projectName, projectUuid, isNewFile] = await window.api.ipc.invoke('choose-project-file');
3939
// if the project file is null, the user cancelled when selecting a new file.
4040
if(!projectFile) {
4141
return {saved: false, reason: i18n.t('project-io-user-cancelled-save-message')};
@@ -50,14 +50,14 @@ define(['knockout', 'models/wkt-project', 'utils/i18n'],
5050
// this will cause the model files to be written with new names
5151
project.wdtModel.clearModelFileNames();
5252

53-
return saveToFile(projectFile, projectName, projectUuid);
53+
return saveToFile(projectFile, projectName, projectUuid, isNewFile);
5454
};
5555

5656
// save the project to the specified project file with name and UUID.
5757
// if project file is null, do not save.
5858
// if project name and UUID are specified, this is a new file, so assign those.
5959
// return object with saved status and reason if not saved.
60-
async function saveToFile(projectFile, projectName, projectUuid, displayElectronSideErrors = true) {
60+
async function saveToFile(projectFile, projectName, projectUuid, isNewFile, displayElectronSideErrors = true) {
6161
const result = { saved: true };
6262
// if project name or UUID are null, they were previously assigned.
6363
if(projectName) {
@@ -70,7 +70,7 @@ define(['knockout', 'models/wkt-project', 'utils/i18n'],
7070
let projectContents = project.getProjectContents();
7171
let modelContents = project.wdtModel.getModelContents();
7272
const saveResult = await window.api.ipc.invoke('save-project', projectFile, projectContents,
73-
modelContents, displayElectronSideErrors);
73+
modelContents, isNewFile, displayElectronSideErrors);
7474

7575
if (saveResult['isProjectFileSaved']) {
7676
project.setProjectFileName(projectFile);

0 commit comments

Comments
 (0)