From ea1fed96161f9c86c970627b6c290c513350e8bd Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Tue, 20 Mar 2018 14:44:10 -0700 Subject: [PATCH] Use viewType instead of uri for webviews As discussed in #45994, move from using a uri to using a viewType. The view type is shared among all webviews of a given type, such as all markdown previews Fixes #44575 --- .../src/features/preview.ts | 7 ++----- src/vs/vscode.proposed.d.ts | 10 +++++----- .../api/electron-browser/mainThreadWebview.ts | 11 +++++++++-- src/vs/workbench/api/node/extHost.api.impl.ts | 4 ++-- src/vs/workbench/api/node/extHost.protocol.ts | 2 +- src/vs/workbench/api/node/extHostWebview.ts | 14 ++++++++------ 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/extensions/markdown-language-features/src/features/preview.ts b/extensions/markdown-language-features/src/features/preview.ts index 51e1c093f14fd..0c593430d60e1 100644 --- a/extensions/markdown-language-features/src/features/preview.ts +++ b/extensions/markdown-language-features/src/features/preview.ts @@ -18,10 +18,8 @@ const localize = nls.loadMessageBundle(); export class MarkdownPreview { - public static previewScheme = 'vscode-markdown-preview'; - private static previewCount = 0; + public static previewViewType = 'markdown.preview'; - public readonly uri: vscode.Uri; private readonly webview: vscode.Webview; private throttleTimer: any; private initialLine: number | undefined = undefined; @@ -41,9 +39,8 @@ export class MarkdownPreview { topmostLineMonitor: MarkdownFileTopmostLineMonitor, private readonly contributions: MarkdownContributions ) { - this.uri = vscode.Uri.parse(`${MarkdownPreview.previewScheme}:${MarkdownPreview.previewCount++}`); this.webview = vscode.window.createWebview( - this.uri, + MarkdownPreview.previewViewType, this.getPreviewTitle(this._resource), previewColumn, { enableScripts: true, diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index cfa11aad835d8..158325fdb6b8c 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -577,13 +577,13 @@ declare module 'vscode' { } /** - * A webview is an editor with html content, like an iframe. + * A webview displays html content, like an iframe. */ export interface Webview { /** - * Unique identifer of the webview. + * The type of the webview, such as `'markdownw.preview'` */ - readonly uri: Uri; + readonly viewType: string; /** * Content settings for the webview. @@ -653,12 +653,12 @@ declare module 'vscode' { /** * Create and show a new webview. * - * @param uri Unique identifier for the webview. + * @param viewType Identifier the type of the webview. * @param title Title of the webview. * @param column Editor column to show the new webview in. * @param options Content settings for the webview. */ - export function createWebview(uri: Uri, title: string, column: ViewColumn, options: WebviewOptions): Webview; + export function createWebview(viewType: string, title: string, column: ViewColumn, options: WebviewOptions): Webview; } //#endregion diff --git a/src/vs/workbench/api/electron-browser/mainThreadWebview.ts b/src/vs/workbench/api/electron-browser/mainThreadWebview.ts index b591dde55edd5..f5adf2d1f5be6 100644 --- a/src/vs/workbench/api/electron-browser/mainThreadWebview.ts +++ b/src/vs/workbench/api/electron-browser/mainThreadWebview.ts @@ -46,8 +46,15 @@ export class MainThreadWebviews implements MainThreadWebviewsShape { this._toDispose = dispose(this._toDispose); } - $createWebview(handle: WebviewHandle, uri: URI, title: string, column: Position, options: vscode.WebviewOptions, extensionFolderPath: string): void { - const webviewInput = new WebviewInput(URI.revive(uri), title, options, '', { + $createWebview( + handle: WebviewHandle, + viewType: string, + title: string, + column: Position, + options: vscode.WebviewOptions, + extensionFolderPath: string + ): void { + const webviewInput = new WebviewInput(URI.parse('webview://' + handle), title, options, '', { onMessage: message => this._proxy.$onMessage(handle, message), onDidChangePosition: position => this._proxy.$onDidChangePosition(handle, position), onDispose: () => { diff --git a/src/vs/workbench/api/node/extHost.api.impl.ts b/src/vs/workbench/api/node/extHost.api.impl.ts index 5cb2acadb8787..aedb82a56b17a 100644 --- a/src/vs/workbench/api/node/extHost.api.impl.ts +++ b/src/vs/workbench/api/node/extHost.api.impl.ts @@ -413,8 +413,8 @@ export function createApiFactory( registerDecorationProvider: proposedApiFunction(extension, (provider: vscode.DecorationProvider) => { return extHostDecorations.registerDecorationProvider(provider, extension.id); }), - createWebview: proposedApiFunction(extension, (uri: vscode.Uri, title: string, column: vscode.ViewColumn, options: vscode.WebviewOptions) => { - return extHostWebviews.createWebview(uri, title, column, options, extension.extensionFolderPath); + createWebview: proposedApiFunction(extension, (viewType: string, title: string, column: vscode.ViewColumn, options: vscode.WebviewOptions) => { + return extHostWebviews.createWebview(viewType, title, column, options, extension.extensionFolderPath); }) }; diff --git a/src/vs/workbench/api/node/extHost.protocol.ts b/src/vs/workbench/api/node/extHost.protocol.ts index 43bd8d279c78f..3d4158a315159 100644 --- a/src/vs/workbench/api/node/extHost.protocol.ts +++ b/src/vs/workbench/api/node/extHost.protocol.ts @@ -350,7 +350,7 @@ export interface MainThreadTelemetryShape extends IDisposable { export type WebviewHandle = number; export interface MainThreadWebviewsShape extends IDisposable { - $createWebview(handle: WebviewHandle, uri: URI, title: string, column: EditorPosition, options: vscode.WebviewOptions, extensionFolderPath: string): void; + $createWebview(handle: WebviewHandle, viewType: string, title: string, column: EditorPosition, options: vscode.WebviewOptions, extensionFolderPath: string): void; $disposeWebview(handle: WebviewHandle): void; $show(handle: WebviewHandle, column: EditorPosition): void; $setTitle(handle: WebviewHandle, value: string): void; diff --git a/src/vs/workbench/api/node/extHostWebview.ts b/src/vs/workbench/api/node/extHostWebview.ts index c2c06f972a3f7..25948743352f3 100644 --- a/src/vs/workbench/api/node/extHostWebview.ts +++ b/src/vs/workbench/api/node/extHostWebview.ts @@ -12,6 +12,7 @@ import { TPromise } from 'vs/base/common/winjs.base'; export class ExtHostWebview implements vscode.Webview { + private readonly _viewType: string; private _title: string; private _html: string; private _options: vscode.WebviewOptions; @@ -31,10 +32,11 @@ export class ExtHostWebview implements vscode.Webview { constructor( private readonly _handle: WebviewHandle, private readonly _proxy: MainThreadWebviewsShape, - private readonly _uri: vscode.Uri, + viewType: string, viewColumn: vscode.ViewColumn, options: vscode.WebviewOptions ) { + this._viewType = viewType; this._viewColumn = viewColumn; this._options = options; } @@ -52,9 +54,9 @@ export class ExtHostWebview implements vscode.Webview { this.onDidChangeViewStateEmitter.dispose(); } - get uri(): vscode.Uri { + get viewType(): string { this.assertNotDisposed(); - return this._uri; + return this._viewType; } get title(): string { @@ -141,16 +143,16 @@ export class ExtHostWebviews implements ExtHostWebviewsShape { } createWebview( - uri: vscode.Uri, + viewType: string, title: string, viewColumn: vscode.ViewColumn, options: vscode.WebviewOptions, extensionFolderPath: string ): vscode.Webview { const handle = ExtHostWebviews.handlePool++; - this._proxy.$createWebview(handle, uri, title, typeConverters.fromViewColumn(viewColumn), options, extensionFolderPath); + this._proxy.$createWebview(handle, viewType, title, typeConverters.fromViewColumn(viewColumn), options, extensionFolderPath); - const webview = new ExtHostWebview(handle, this._proxy, uri, viewColumn, options); + const webview = new ExtHostWebview(handle, this._proxy, viewType, viewColumn, options); this._webviews.set(handle, webview); return webview; }