Skip to content

Commit

Permalink
[plugin] register command open handler
Browse files Browse the repository at this point in the history
webviews can use such URIs to trigger a command

Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Nov 13, 2019
1 parent d471c84 commit 9e0d246
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<boolean> {
// 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -66,6 +66,7 @@ import { InPluginFileSystemWatcherManager } from './in-plugin-filesystem-watcher
import { WebviewWidget, WebviewWidgetIdentifier, WebviewWidgetExternalEndpoint } 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) => {

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 9e0d246

Please sign in to comment.