From 70e8e31ff6082c978441ce18ab7829d913471839 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Tue, 14 Apr 2020 11:48:36 +0200 Subject: [PATCH] web - add API to execute command --- src/vs/workbench/browser/web.main.ts | 24 ++++++--- .../electron-browser/desktop.main.ts | 4 +- src/vs/workbench/workbench.web.api.ts | 53 +++++++++++++++---- 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/src/vs/workbench/browser/web.main.ts b/src/vs/workbench/browser/web.main.ts index 59785385c2a..b3cd6fdc700 100644 --- a/src/vs/workbench/browser/web.main.ts +++ b/src/vs/workbench/browser/web.main.ts @@ -33,7 +33,7 @@ import { WorkspaceService } from 'vs/workbench/services/configuration/browser/co import { ConfigurationCache } from 'vs/workbench/services/configuration/browser/configurationCache'; import { ISignService } from 'vs/platform/sign/common/sign'; import { SignService } from 'vs/platform/sign/browser/signService'; -import { IWorkbenchConstructionOptions, IWorkspace } from 'vs/workbench/workbench.web.api'; +import { IWorkbenchConstructionOptions, IWorkspace, IWorkbench } from 'vs/workbench/workbench.web.api'; import { FileUserDataProvider } from 'vs/workbench/services/userData/common/fileUserDataProvider'; import { BACKUPS } from 'vs/platform/environment/common/environment'; import { joinPath } from 'vs/base/common/resources'; @@ -51,6 +51,7 @@ import { getWorkspaceIdentifier } from 'vs/workbench/services/workspaces/browser import { coalesce } from 'vs/base/common/arrays'; import { InMemoryFileSystemProvider } from 'vs/platform/files/common/inMemoryFilesystemProvider'; import { WebResourceIdentityService, IResourceIdentityService } from 'vs/platform/resource/common/resourceIdentityService'; +import { ICommandService } from 'vs/platform/commands/common/commands'; class BrowserMain extends Disposable { @@ -61,7 +62,7 @@ class BrowserMain extends Disposable { super(); } - async open(): Promise { + async open(): Promise { const services = await this.initServices(); await domContentLoaded(); @@ -86,7 +87,18 @@ class BrowserMain extends Disposable { } // Startup - workbench.startup(); + const instantiationService = workbench.startup(); + + // Return API Facade + return instantiationService.invokeFunction(accessor => { + const commandService = accessor.get(ICommandService); + + return { + commands: { + executeCommand: (command, ...args) => commandService.executeCommand(command, ...args) + } + }; + }); } private registerListeners(workbench: Workbench, storageService: BrowserStorageService): void { @@ -335,8 +347,8 @@ class BrowserMain extends Disposable { } } -export function main(domElement: HTMLElement, options: IWorkbenchConstructionOptions): Promise { - const renderer = new BrowserMain(domElement, options); +export function main(domElement: HTMLElement, options: IWorkbenchConstructionOptions): Promise { + const workbench = new BrowserMain(domElement, options); - return renderer.open(); + return workbench.open(); } diff --git a/src/vs/workbench/electron-browser/desktop.main.ts b/src/vs/workbench/electron-browser/desktop.main.ts index 84c22595e12..80d2874f9bb 100644 --- a/src/vs/workbench/electron-browser/desktop.main.ts +++ b/src/vs/workbench/electron-browser/desktop.main.ts @@ -338,7 +338,7 @@ class DesktopMain extends Disposable { } export function main(configuration: INativeWindowConfiguration): Promise { - const renderer = new DesktopMain(configuration); + const workbench = new DesktopMain(configuration); - return renderer.open(); + return workbench.open(); } diff --git a/src/vs/workbench/workbench.web.api.ts b/src/vs/workbench/workbench.web.api.ts index 7908789aba5..9aeaec12a62 100644 --- a/src/vs/workbench/workbench.web.api.ts +++ b/src/vs/workbench/workbench.web.api.ts @@ -35,33 +35,37 @@ interface IExternalUriResolver { (uri: URI): Promise; } -interface TunnelOptions { +interface ITunnelFactory { + (tunnelOptions: ITunnelOptions): Promise | undefined; +} + +interface ITunnelOptions { remoteAddress: { port: number, host: string }; + /** * The desired local port. If this port can't be used, then another will be chosen. */ localAddressPort?: number; + label?: string; } -interface Tunnel extends IDisposable { +interface ITunnel extends IDisposable { remoteAddress: { port: number, host: string }; + /** * The complete local address(ex. localhost:1234) */ localAddress: string; + /** * Implementers of Tunnel should fire onDidDispose when dispose is called. */ onDidDispose: Event; } -interface ITunnelFactory { - (tunnelOptions: TunnelOptions): Thenable | undefined; -} - -interface IShowCandidate { - (host: string, port: number, detail: string): Thenable; +interface IShowPortCandidate { + (host: string, port: number, detail: string): Promise; } interface ICommand { @@ -126,7 +130,7 @@ interface IWorkbenchConstructionOptions { /** * Support for filtering candidate ports */ - readonly showCandidate?: IShowCandidate; + readonly showCandidate?: IShowPortCandidate; //#endregion @@ -195,14 +199,30 @@ interface IWorkbenchConstructionOptions { //#endregion } +interface IWorkbench { + commands: { + + /** + * Allows to execute any command if known with the provided arguments. + * + * @param command Identifier of the command to execute. + * @param rest Parameters passed to the command function. + * @return A promise that resolves to the returned value of the given command. + */ + executeCommand(command: string, ...rest: any[]): Promise; + } +} + /** * Creates the workbench with the provided options in the provided container. * * @param domElement the container to create the workbench in * @param options for setting up the workbench + * + * @returns the workbench API facade. */ let created = false; -async function create(domElement: HTMLElement, options: IWorkbenchConstructionOptions): Promise { +async function create(domElement: HTMLElement, options: IWorkbenchConstructionOptions): Promise { // Assert that the workbench is not created more than once. We currently // do not support this and require a full context switch to clean-up. @@ -213,7 +233,7 @@ async function create(domElement: HTMLElement, options: IWorkbenchConstructionOp } // Startup workbench - await main(domElement, options); + const workbench = await main(domElement, options); // Register commands if any if (Array.isArray(options.commands)) { @@ -225,6 +245,8 @@ async function create(domElement: HTMLElement, options: IWorkbenchConstructionOp }); } } + + return workbench; } export { @@ -232,6 +254,7 @@ export { // Factory create, IWorkbenchConstructionOptions, + IWorkbench, // Basic Types URI, @@ -281,6 +304,14 @@ export { // External Uris IExternalUriResolver, + // Tunnel + ITunnelFactory, + ITunnel, + ITunnelOptions, + + // Ports + IShowPortCandidate, + // Commands ICommand }; -- GitLab