|
| 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