More refactoring. Splitting up code.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta
2020-07-17 18:04:38 +02:00
parent cfde197198
commit 3a6b2f2bc8
29 changed files with 145 additions and 110 deletions

View File

@@ -1,4 +1,5 @@
import { injectable } from 'inversify';
import { remote } from 'electron';
import { WorkspaceCommands } from '@theia/workspace/lib/browser/workspace-commands';
import { ArduinoMenus } from '../menu/arduino-menus';
import { SketchContribution, Command, CommandRegistry, MenuModelRegistry, KeybindingRegistry } from './contribution';
@@ -16,9 +17,24 @@ export class CloseSketch extends SketchContribution {
}
const isTemp = await this.sketchService.isTemp(sketch);
if (isTemp) {
await this.commandService.executeCommand(SaveAsSketch.Commands.SAVE_AS_SKETCH.id, { openAfterMove: false, execOnlyIfTemp: true });
await this.commandService.executeCommand(WorkspaceCommands.CLOSE.id);
// TODO: check monaco model version. If `0` just close the app.
const { response } = await remote.dialog.showMessageBox({
type: 'question',
buttons: ["Don't Save", 'Cancel', 'Save'],
message: 'Do you want to save changes to this sketch before closing?',
detail: "If you don't save, your changes will be lost."
});
if (response === 1) { // Cancel
return;
}
if (response === 2) { // Save
const saved = await this.commandService.executeCommand(SaveAsSketch.Commands.SAVE_AS_SKETCH.id, { openAfterMove: false, execOnlyIfTemp: true });
if (!saved) { // If it was not saved, do bail the close.
return;
}
}
}
await this.commandService.executeCommand(WorkspaceCommands.CLOSE.id);
}
});
}

View File

@@ -28,15 +28,18 @@ export class SaveAsSketch extends SketchContribution {
});
}
async saveAs({ execOnlyIfTemp, openAfterMove }: SaveAsSketch.Options = SaveAsSketch.Options.DEFAULT): Promise<void> {
/**
* Resolves `true` if the sketch was successfully saved as something.
*/
async saveAs({ execOnlyIfTemp, openAfterMove }: SaveAsSketch.Options = SaveAsSketch.Options.DEFAULT): Promise<boolean> {
const sketch = await this.getCurrentSketch();
if (!sketch) {
return;
return false;
}
const isTemp = await this.sketchService.isTemp(sketch);
if (!isTemp && !!execOnlyIfTemp) {
return;
return false;
}
// If target does not exist, propose a `directories.user`/${sketch.name} path
@@ -49,16 +52,17 @@ export class SaveAsSketch extends SketchContribution {
const defaultPath = await this.fileSystem.getFsPath(defaultUri.toString())!;
const { filePath, canceled } = await remote.dialog.showSaveDialog({ title: 'Save sketch folder as...', defaultPath });
if (!filePath || canceled) {
return;
return false;
}
const destinationUri = await this.fileSystemExt.getUri(filePath);
if (!destinationUri) {
return;
return false;
}
const workspaceUri = await this.sketchService.copy(sketch, { destinationUri });
if (workspaceUri && openAfterMove) {
this.workspaceService.open(new URI(workspaceUri));
}
return !!workspaceUri;
}
}