From 64e13c497f8baa760b00ceb46d171c2f4359f900 Mon Sep 17 00:00:00 2001 From: Anton Kosyakov Date: Thu, 7 Nov 2019 09:05:43 +0000 Subject: [PATCH] [plugin] register command open handler webviews can use such URIs to trigger a command Signed-off-by: Anton Kosyakov --- .../browser/plugin-command-open-handler.ts | 50 +++++++++++++++++++ .../browser/plugin-ext-frontend-module.ts | 6 ++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 packages/plugin-ext/src/main/browser/plugin-command-open-handler.ts diff --git a/packages/plugin-ext/src/main/browser/plugin-command-open-handler.ts b/packages/plugin-ext/src/main/browser/plugin-command-open-handler.ts new file mode 100644 index 0000000000000..8532760a24c0e --- /dev/null +++ b/packages/plugin-ext/src/main/browser/plugin-command-open-handler.ts @@ -0,0 +1,50 @@ +/******************************************************************************** + * Copyright (C) 2019 TypeFox and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the Eclipse + * Public License v. 2.0 are satisfied: GNU General Public License, version 2 + * with the GNU Classpath Exception which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + ********************************************************************************/ + +import { injectable, inject } from 'inversify'; +import URI from '@theia/core/lib/common/uri'; +import { OpenHandler } from '@theia/core/lib/browser/opener-service'; +import { Schemes } from '../../common/uri-components'; +import { CommandService } from '@theia/core/lib/common/command'; + +@injectable() +export class PluginCommandOpenHandler implements OpenHandler { + + readonly id = 'plugin-command'; + + @inject(CommandService) + protected readonly commands: CommandService; + + canHandle(uri: URI): number { + return uri.scheme === Schemes.COMMAND ? 500 : -1; + } + + async open(uri: URI): Promise { + // tslint:disable-next-line:no-any + let args: any = []; + try { + args = JSON.parse(uri.query); + if (!Array.isArray(args)) { + args = [args]; + } + } catch (e) { + // ignore error + } + await this.commands.executeCommand(uri.path.toString()); + return true; + } + +} diff --git a/packages/plugin-ext/src/main/browser/plugin-ext-frontend-module.ts b/packages/plugin-ext/src/main/browser/plugin-ext-frontend-module.ts index e0ec04bd32f29..03ab43ade3f1d 100644 --- a/packages/plugin-ext/src/main/browser/plugin-ext-frontend-module.ts +++ b/packages/plugin-ext/src/main/browser/plugin-ext-frontend-module.ts @@ -20,7 +20,7 @@ import '../../../src/main/browser/style/index.css'; import { ContainerModule } from 'inversify'; import { FrontendApplicationContribution, FrontendApplication, WidgetFactory, bindViewContribution, - ViewContainerIdentifier, ViewContainer, createTreeContainer, TreeImpl, TreeWidget, TreeModelImpl + ViewContainerIdentifier, ViewContainer, createTreeContainer, TreeImpl, TreeWidget, TreeModelImpl, OpenHandler } from '@theia/core/lib/browser'; import { MaybePromise, CommandContribution, ResourceResolver, bindContributionProvider } from '@theia/core/lib/common'; import { WebSocketConnectionProvider } from '@theia/core/lib/browser/messaging'; @@ -66,6 +66,7 @@ import { InPluginFileSystemWatcherManager } from './in-plugin-filesystem-watcher import { WebviewWidget, WebviewWidgetIdentifier } from './webview/webview'; import { WebviewEnvironment } from './webview/webview-environment'; import { WebviewThemeDataProvider } from './webview/webview-theme-data-provider'; +import { PluginCommandOpenHandler } from './plugin-command-open-handler'; export default new ContainerModule((bind, unbind, isBound, rebind) => { @@ -149,6 +150,9 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => { } })).inSingletonScope(); + bind(PluginCommandOpenHandler).toSelf().inSingletonScope(); + bind(OpenHandler).toService(PluginCommandOpenHandler); + bind(WebviewEnvironment).toSelf().inSingletonScope(); bind(WebviewThemeDataProvider).toSelf().inSingletonScope(); bind(WebviewWidget).toSelf();