Skip to content

Commit

Permalink
always enable file upload menu item
Browse files Browse the repository at this point in the history
always-available upload option
  • Loading branch information
qx220 committed Jul 23, 2019
1 parent 3587c23 commit 81e69f2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
16 changes: 13 additions & 3 deletions packages/core/src/common/selection-command-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,27 @@ export class SelectionCommandHandler<S> implements CommandHandler {

execute(...args: any[]): Object | undefined {
const selection = this.getSelection(...args);
return selection ? (this.options.execute as any)(selection, ...args) : undefined;
return selection ? (this.options.execute as any)(selection, ...args) : (this.options.execute as any)(undefined);
}

isVisible(...args: any[]): boolean {
const selection = this.getSelection(...args);
return !!selection && (!this.options.isVisible || (this.options.isVisible as any)(selection as any, ...args));
//custom isVisible function overrides default checking mechanism
if (!this.options.isVisible) {
return !!selection;
} else {
return (this.options.isVisible as any)(selection as any, ...args);
}
}

isEnabled(...args: any[]): boolean {
const selection = this.getSelection(...args);
return !!selection && (!this.options.isEnabled || (this.options.isEnabled as any)(selection as any, ...args));
//custom isEnabled function overrides default checking mechanism
if (!this.options.isEnabled) {
return !!selection;
} else {
return (this.options.isEnabled as any)(selection as any, ...args);
}
}

protected isMulti(): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { TreeWidgetSelection } from '@theia/core/lib/browser/tree/tree-widget-se
import { FileSystemPreferences } from './filesystem-preferences';
import { FileSelection } from './file-selection';
import { FileUploadService } from './file-upload-service';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';

This comment has been minimized.

Copy link
@akosyakov

akosyakov Jul 23, 2019

you cannot use workspace extension in the filesystem extension, only in another way around

This comment has been minimized.

Copy link
@qx220

qx220 Jul 23, 2019

Author Owner

then how can we obtain the workspace root information during startup (before anything is selected) so we can register the upload command?

This comment has been minimized.

Copy link
@akosyakov

akosyakov Jul 23, 2019

We could introduce Upload command in the workspace extension which contribute to main menu and delegate to upload command in the filesystem with a proper URI.

It is better to discuss such things on PR that they are visible for others.


export namespace FileSystemCommands {

Expand Down Expand Up @@ -65,6 +66,9 @@ export class FileSystemFrontendContribution implements FrontendApplicationContri
@inject(FileUploadService)
protected readonly uploadService: FileUploadService;

@inject(WorkspaceService)
protected readonly workspaceService: WorkspaceService;

initialize(): void {
this.fileSystemWatcher.onFilesChanged(event => this.run(() => this.updateWidgets(event)));
this.fileSystemWatcher.onDidMove(event => this.run(() => this.moveWidgets(event)));
Expand All @@ -82,23 +86,40 @@ export class FileSystemFrontendContribution implements FrontendApplicationContri
registerCommands(commands: CommandRegistry): void {
commands.registerCommand(FileSystemCommands.UPLOAD, new FileSelection.CommandHandler(this.selectionService, {
multi: false,
isEnabled: selection => this.canUpload(selection),
isVisible: selection => this.canUpload(selection),
isEnabled: selection => this.menuItemEnabled(selection),
isVisible: selection => this.menuItemVisible(selection),
execute: selection => this.upload(selection)
}));
}

protected canUpload({ fileStat }: FileSelection): boolean {
return !environment.electron.is() && fileStat.isDirectory;
//the top-left corner menu should always be enabled
protected menuItemEnabled(selection: FileSelection | undefined): boolean {
selection;
return !environment.electron.is();
}

//the right-click context menu should be invisible when the selected item is a regular file
protected menuItemVisible(selection: FileSelection | undefined): boolean {
//the only cases we can't upload are: electron environment, and fileSelection is a regular file
return !environment.electron.is() && (selection == undefined || selection.fileStat.isDirectory);
}

protected async upload(selection: FileSelection): Promise<void> {
try {
const source = TreeWidgetSelection.getSource(this.selectionService.selection);
await this.uploadService.upload(selection.fileStat.uri);
if (ExpandableTreeNode.is(selection) && source) {
await source.model.expandNode(selection);
//use workspace root if there is no selection
if (selection == undefined) {
await this.uploadService.upload(this.workspaceService.tryGetRoots()[0].uri);
//if selection is a regular file, then upload to its parent folder
} else if (!selection.fileStat.isDirectory) {
await this.uploadService.upload((new URI(selection.fileStat.uri)).parent);
} else {
const source = TreeWidgetSelection.getSource(this.selectionService.selection);
await this.uploadService.upload(selection.fileStat.uri);
if (ExpandableTreeNode.is(selection) && source) {
await source.model.expandNode(selection);
}
}

} catch (e) {
if (!isCancelled(e)) {
console.error(e);
Expand Down

0 comments on commit 81e69f2

Please sign in to comment.