webview.ts 3.2 KB
Newer Older
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { Event } from 'vs/base/common/event';
7
import { IDisposable } from 'vs/base/common/lifecycle';
8
import { URI } from 'vs/base/common/uri';
9
import * as modes from 'vs/editor/common/modes';
10
import * as nls from 'vs/nls';
11
import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';
12 13
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
14 15 16 17 18

/**
 * Set when the find widget in a webview is visible.
 */
export const KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE = new RawContextKey<boolean>('webviewFindWidgetVisible', false);
19
export const KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_FOCUSED = new RawContextKey<boolean>('webviewFindWidgetFocused', false);
20 21 22

export const IWebviewService = createDecorator<IWebviewService>('webviewService');

M
Matt Bierner 已提交
23 24 25
/**
 * Handles the creation of webview elements.
 */
26
export interface IWebviewService {
27
	_serviceBrand: undefined;
28 29

	createWebview(
30
		id: string,
31 32
		options: WebviewOptions,
		contentOptions: WebviewContentOptions,
33 34 35 36 37 38 39
	): WebviewElement;

	createWebviewEditorOverlay(
		id: string,
		options: WebviewOptions,
		contentOptions: WebviewContentOptions,
	): WebviewEditorOverlay;
40 41
}

42 43
export const WebviewResourceScheme = 'vscode-resource';

44 45 46 47 48 49
export interface WebviewOptions {
	readonly extension?: {
		readonly location: URI;
		readonly id?: ExtensionIdentifier;
	};
	readonly enableFindWidget?: boolean;
50 51
	readonly tryRestoreScrollPosition?: boolean;
	readonly retainContextWhenHidden?: boolean;
52 53 54 55 56
}

export interface WebviewContentOptions {
	readonly allowScripts?: boolean;
	readonly localResourceRoots?: ReadonlyArray<URI>;
57
	readonly portMapping?: ReadonlyArray<modes.IWebviewPortMapping>;
58
	readonly enableCommandUris?: boolean;
59 60
}

61
export interface Webview extends IDisposable {
62

M
Matt Bierner 已提交
63
	html: string;
64
	contentOptions: WebviewContentOptions;
65 66 67 68 69 70 71 72
	initialScrollProgress: number;
	state: string | undefined;

	readonly onDidFocus: Event<void>;
	readonly onDidClickLink: Event<URI>;
	readonly onDidScroll: Event<{ scrollYPercentage: number }>;
	readonly onDidUpdateState: Event<string | undefined>;
	readonly onMessage: Event<any>;
73
	readonly onMissingCsp: Event<ExtensionIdentifier>;
74 75 76

	sendMessage(data: any): void;
	update(
77
		html: string,
78 79 80 81 82 83 84 85
		options: WebviewContentOptions,
		retainContextWhenHidden: boolean
	): void;

	layout(): void;
	focus(): void;
	reload(): void;

86 87
	showFind(): void;
	hideFind(): void;
88
	runFindAction(previous: boolean): void;
89
}
90

91 92 93 94 95 96 97 98 99 100
export interface WebviewElement extends Webview {
	mountTo(parent: HTMLElement): void;
}

export interface WebviewEditorOverlay extends Webview {
	readonly container: HTMLElement;
	readonly options: WebviewOptions;

	claim(owner: any): void;
	release(owner: any): void;
101 102

	getInnerWebview(): Webview | undefined;
103 104
}

105
export const webviewDeveloperCategory = nls.localize('developer', "Developer");