From 8ac1e5f8d9123196755fa02cb09d7b21eb311457 Mon Sep 17 00:00:00 2001 From: vince-fugnitto Date: Thu, 23 Jul 2020 10:59:28 -0400 Subject: [PATCH] navigator: fix 'open' command the following commit fixes the `open` command for the navigator to only display the command for file nodes and not directories (since directories cannot be opened). Signed-off-by: vince-fugnitto --- .../browser/common-frontend-contribution.ts | 2 -- .../src/browser/navigator-contribution.ts | 24 +++++++++++++++++-- .../src/browser/navigator-widget.tsx | 5 ++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/core/src/browser/common-frontend-contribution.ts b/packages/core/src/browser/common-frontend-contribution.ts index 76cffe1b49754..0772465578dc3 100644 --- a/packages/core/src/browser/common-frontend-contribution.ts +++ b/packages/core/src/browser/common-frontend-contribution.ts @@ -88,8 +88,6 @@ export namespace CommonCommands { export const OPEN: Command = { id: 'core.open', - category: FILE_CATEGORY, - label: 'Open', }; export const CUT: Command = { diff --git a/packages/navigator/src/browser/navigator-contribution.ts b/packages/navigator/src/browser/navigator-contribution.ts index 5c4f6b2ee84c1..f5f2b7df7b373 100644 --- a/packages/navigator/src/browser/navigator-contribution.ts +++ b/packages/navigator/src/browser/navigator-contribution.ts @@ -62,7 +62,7 @@ import { import { FileSystemCommands } from '@theia/filesystem/lib/browser/filesystem-frontend-contribution'; import { NavigatorDiff, NavigatorDiffCommands } from './navigator-diff'; import { UriSelection } from '@theia/core/lib/common/selection'; -import { DirNode } from '@theia/filesystem/lib/browser'; +import { DirNode, FileNode } from '@theia/filesystem/lib/browser'; import { FileNavigatorModel } from './navigator-model'; import { ClipboardService } from '@theia/core/lib/browser/clipboard-service'; import { SelectionService } from '@theia/core/lib/common/selection-service'; @@ -106,6 +106,11 @@ export namespace FileNavigatorCommands { export const COPY_RELATIVE_FILE_PATH: Command = { id: 'navigator.copyRelativeFilePath' }; + export const OPEN: Command = { + id: 'navigator.open', + category: 'File', + label: 'Open' + }; } /** @@ -330,6 +335,20 @@ export class FileNavigatorContribution extends AbstractViewContribution this.getSelectedFileNodes().length > 0, + isVisible: () => this.getSelectedFileNodes().length > 0, + execute: () => { + this.getSelectedFileNodes().forEach(async node => { + const opener = await this.openerService.getOpener(node.uri); + opener.open(node.uri); + }); + } + }); + } + + protected getSelectedFileNodes(): FileNode[] { + return this.tryGetWidget()?.model.selectedNodes.filter(FileNode.is) || []; } protected withWidget(widget: Widget | undefined = this.tryGetWidget(), cb: (navigator: FileNavigatorWidget) => T): T | false { @@ -348,7 +367,8 @@ export class FileNavigatorContribution extends AbstractViewContribution { diff --git a/packages/navigator/src/browser/navigator-widget.tsx b/packages/navigator/src/browser/navigator-widget.tsx index b883a9db6abc6..515e129304f9c 100644 --- a/packages/navigator/src/browser/navigator-widget.tsx +++ b/packages/navigator/src/browser/navigator-widget.tsx @@ -18,7 +18,7 @@ import { injectable, inject, postConstruct } from 'inversify'; import { Message } from '@phosphor/messaging'; import URI from '@theia/core/lib/common/uri'; import { CommandService, SelectionService } from '@theia/core/lib/common'; -import { CommonCommands, CorePreferences, ViewContainerTitleOptions, Key } from '@theia/core/lib/browser'; +import { CorePreferences, ViewContainerTitleOptions, Key } from '@theia/core/lib/browser'; import { ContextMenuRenderer, ExpandableTreeNode, TreeProps, TreeModel, TreeNode @@ -31,6 +31,7 @@ import { FileNavigatorModel } from './navigator-model'; import { isOSX, environment } from '@theia/core'; import * as React from 'react'; import { NavigatorContextKeyService } from './navigator-context-key-service'; +import { FileNavigatorCommands } from './navigator-contribution'; export const FILE_NAVIGATOR_ID = 'files'; export const EXPLORER_VIEW_CONTAINER_ID = 'explorer-view-container'; @@ -108,7 +109,7 @@ export class FileNavigatorWidget extends FileTreeWidget { const mainPanelNode = this.shell.mainPanel.node; this.addEventListener(mainPanelNode, 'drop', async ({ dataTransfer }) => { const treeNodes = dataTransfer && this.getSelectedTreeNodesFromData(dataTransfer) || []; - treeNodes.filter(FileNode.is).forEach(treeNode => this.commandService.executeCommand(CommonCommands.OPEN.id, treeNode.uri)); + treeNodes.filter(FileNode.is).forEach(treeNode => this.commandService.executeCommand(FileNavigatorCommands.OPEN.id, treeNode.uri)); }); const handler = (e: DragEvent) => { if (e.dataTransfer) {