Skip to content

Commit c366e62

Browse files
author
Benjamin Pasero
committed
Web API: registerCommand() (fix microsoft#90569)
1 parent fb6b284 commit c366e62

2 files changed

Lines changed: 50 additions & 15 deletions

File tree

src/vs/workbench/browser/web.main.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,16 @@ class BrowserMain extends Disposable {
9393

9494
// Layout
9595
const viewport = platform.isIOS && (<any>window).visualViewport ? (<any>window).visualViewport /** Visual viewport */ : window /** Layout viewport */;
96-
this._register(addDisposableListener(viewport, EventType.RESIZE, () => {
97-
workbench.layout();
98-
}));
96+
this._register(addDisposableListener(viewport, EventType.RESIZE, () => workbench.layout()));
9997

10098
// Prevent the back/forward gestures in macOS
101-
this._register(addDisposableListener(this.domElement, EventType.WHEEL, (e) => {
102-
e.preventDefault();
103-
}, { passive: false }));
99+
this._register(addDisposableListener(this.domElement, EventType.WHEEL, e => e.preventDefault(), { passive: false }));
104100

105101
// Prevent native context menus in web
106-
this._register(addDisposableListener(this.domElement, EventType.CONTEXT_MENU, (e) => EventHelper.stop(e, true)));
102+
this._register(addDisposableListener(this.domElement, EventType.CONTEXT_MENU, e => EventHelper.stop(e, true)));
107103

108104
// Prevent default navigation on drop
109-
this._register(addDisposableListener(this.domElement, EventType.DROP, (e) => EventHelper.stop(e, true)));
105+
this._register(addDisposableListener(this.domElement, EventType.DROP, e => EventHelper.stop(e, true)));
110106

111107
// Workbench Lifecycle
112108
this._register(workbench.onBeforeShutdown(event => {

src/vs/workbench/workbench.web.api.ts

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { IUpdateProvider, IUpdate } from 'vs/workbench/services/update/browser/u
1616
import { Event, Emitter } from 'vs/base/common/event';
1717
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
1818
import { IWorkspaceProvider, IWorkspace } from 'vs/workbench/services/host/browser/browserHostService';
19+
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
1920

2021
interface IResourceUriProvider {
2122
(uri: URI): URI;
@@ -36,18 +37,23 @@ interface IExternalUriResolver {
3637

3738
interface TunnelOptions {
3839
remoteAddress: { port: number, host: string };
39-
// The desired local port. If this port can't be used, then another will be chosen.
40+
/**
41+
* The desired local port. If this port can't be used, then another will be chosen.
42+
*/
4043
localAddressPort?: number;
4144
label?: string;
4245
}
4346

44-
interface Tunnel {
47+
interface Tunnel extends IDisposable {
4548
remoteAddress: { port: number, host: string };
46-
//The complete local address(ex. localhost:1234)
49+
/**
50+
* The complete local address(ex. localhost:1234)
51+
*/
4752
localAddress: string;
48-
// Implementers of Tunnel should fire onDidDispose when dispose is called.
53+
/**
54+
* Implementers of Tunnel should fire onDidDispose when dispose is called.
55+
*/
4956
onDidDispose: Event<void>;
50-
dispose(): void;
5157
}
5258

5359
interface ITunnelFactory {
@@ -186,14 +192,43 @@ interface IWorkbenchConstructionOptions {
186192
readonly driver?: boolean;
187193
}
188194

195+
interface ICommandHandler {
196+
(...args: any[]): void;
197+
}
198+
199+
interface IWorkbench {
200+
201+
/**
202+
* Register a command with the provided identifier and handler with
203+
* the workbench. The command can be called from extensions using the
204+
* `vscode.commands.executeCommand` API.
205+
*/
206+
registerCommand(id: string, command: ICommandHandler): IDisposable;
207+
}
208+
189209
/**
190210
* Creates the workbench with the provided options in the provided container.
191211
*
192212
* @param domElement the container to create the workbench in
193213
* @param options for setting up the workbench
214+
*
215+
* @returns the workbench facade with additional methods to call on.
194216
*/
195-
function create(domElement: HTMLElement, options: IWorkbenchConstructionOptions): Promise<void> {
196-
return main(domElement, options);
217+
async function create(domElement: HTMLElement, options: IWorkbenchConstructionOptions): Promise<IWorkbench> {
218+
219+
// Startup workbench
220+
await main(domElement, options);
221+
222+
// Return facade
223+
return {
224+
registerCommand: (id: string, command: ICommandHandler): IDisposable => {
225+
return CommandsRegistry.registerCommand(id, (accessor, ...args: any[]) => {
226+
// we currently only pass on the arguments but not the accessor
227+
// to the command to reduce our exposure of internal API.
228+
command(...args);
229+
});
230+
}
231+
};
197232
}
198233

199234
export {
@@ -202,6 +237,10 @@ export {
202237
create,
203238
IWorkbenchConstructionOptions,
204239

240+
// Workbench Facade
241+
IWorkbench,
242+
ICommandHandler,
243+
205244
// Basic Types
206245
URI,
207246
UriComponents,

0 commit comments

Comments
 (0)