Skip to content

Commit a8e6069

Browse files
Akos Kittakittaakos
Akos Kitta
authored andcommitted
ATL-836: Implemented 'Add File...'.
Signed-off-by: Akos Kitta <[email protected]>
1 parent 52b0fd3 commit a8e6069

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

Diff for: arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ import { OpenRecentSketch } from './contributions/open-recent-sketch';
138138
import { Help } from './contributions/help';
139139
import { bindArduinoPreferences } from './arduino-preferences'
140140
import { SettingsService, SettingsDialog, SettingsWidget, SettingsDialogProps } from './settings';
141+
import { AddFile } from './contributions/add-file';
141142

142143
const ElementQueries = require('css-element-queries/src/ElementQueries');
143144

@@ -344,6 +345,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
344345
Contribution.configure(bind, BoardSelection);
345346
Contribution.configure(bind, OpenRecentSketch);
346347
Contribution.configure(bind, Help);
348+
Contribution.configure(bind, AddFile);
347349

348350
bind(OutputServiceImpl).toSelf().inSingletonScope().onActivation(({ container }, outputService) => {
349351
WebSocketConnectionProvider.createProxy(container, OutputServicePath, outputService);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { inject, injectable } from 'inversify';
2+
import { remote } from 'electron';
3+
import { ArduinoMenus } from '../menu/arduino-menus';
4+
import { SketchContribution, Command, CommandRegistry, MenuModelRegistry, URI } from './contribution';
5+
import { FileDialogService } from '@theia/filesystem/lib/browser';
6+
7+
@injectable()
8+
export class AddFile extends SketchContribution {
9+
10+
@inject(FileDialogService)
11+
protected readonly fileDialogService: FileDialogService;
12+
13+
registerCommands(registry: CommandRegistry): void {
14+
registry.registerCommand(AddFile.Commands.ADD_FILE, {
15+
execute: () => this.addFile()
16+
});
17+
}
18+
19+
registerMenus(registry: MenuModelRegistry): void {
20+
registry.registerMenuAction(ArduinoMenus.SKETCH__UTILS_GROUP, {
21+
commandId: AddFile.Commands.ADD_FILE.id,
22+
label: 'Add File...',
23+
order: '2'
24+
});
25+
}
26+
27+
protected async addFile(): Promise<void> {
28+
const sketch = await this.sketchServiceClient.currentSketch();
29+
if (!sketch) {
30+
return;
31+
}
32+
const toAddUri = await this.fileDialogService.showOpenDialog({
33+
title: 'Add File',
34+
canSelectFiles: true,
35+
canSelectFolders: false,
36+
canSelectMany: false
37+
});
38+
if (!toAddUri) {
39+
return;
40+
}
41+
const sketchUri = new URI(sketch.uri);
42+
const filename = toAddUri.path.base;
43+
const targetUri = sketchUri.resolve('data').resolve(filename);
44+
const exists = await this.fileService.exists(targetUri);
45+
if (exists) {
46+
const { response } = await remote.dialog.showMessageBox({
47+
type: 'question',
48+
title: 'Replace',
49+
buttons: ['Cancel', 'OK'],
50+
message: `Replace the existing version of ${filename}?`
51+
});
52+
if (response === 0) { // Cancel
53+
return;
54+
}
55+
}
56+
await this.fileService.copy(toAddUri, targetUri, { overwrite: true });
57+
this.messageService.info('One file added to the sketch.', { timeout: 2000 });
58+
}
59+
60+
}
61+
62+
export namespace AddFile {
63+
export namespace Commands {
64+
export const ADD_FILE: Command = {
65+
id: 'arduino-add-file'
66+
};
67+
}
68+
}

0 commit comments

Comments
 (0)