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

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

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

23 24 25
export const webviewHasOwnEditFunctionsContextKey = 'webviewHasOwnEditFunctions';
export const webviewHasOwnEditFunctionsContext = new RawContextKey<boolean>(webviewHasOwnEditFunctionsContextKey, false);

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

28 29 30 31 32
export interface WebviewIcons {
	readonly light: URI;
	readonly dark: URI;
}

M
Matt Bierner 已提交
33 34 35
/**
 * Handles the creation of webview elements.
 */
36
export interface IWebviewService {
37
	_serviceBrand: undefined;
38

39
	createWebviewElement(
40
		id: string,
41 42
		options: WebviewOptions,
		contentOptions: WebviewContentOptions,
43 44
	): WebviewElement;

45
	createWebviewOverlay(
46 47 48
		id: string,
		options: WebviewOptions,
		contentOptions: WebviewContentOptions,
49
	): WebviewOverlay;
50 51

	setIcons(id: string, value: WebviewIcons | undefined): void;
52 53 54
}

export interface WebviewOptions {
55
	readonly customClasses?: string;
56
	readonly enableFindWidget?: boolean;
57 58
	readonly tryRestoreScrollPosition?: boolean;
	readonly retainContextWhenHidden?: boolean;
59 60 61
}

export interface WebviewContentOptions {
R
rebornix 已提交
62
	readonly allowMultipleAPIAcquire?: boolean;
63 64
	readonly allowScripts?: boolean;
	readonly localResourceRoots?: ReadonlyArray<URI>;
65
	readonly portMapping?: ReadonlyArray<modes.IWebviewPortMapping>;
66
	readonly enableCommandUris?: boolean;
67 68
}

M
Matt Bierner 已提交
69 70 71 72 73
export interface WebviewExtensionDescription {
	readonly location: URI;
	readonly id: ExtensionIdentifier;
}

74
export interface Webview extends IDisposable {
M
Matt Bierner 已提交
75
	html: string;
76
	contentOptions: WebviewContentOptions;
M
Matt Bierner 已提交
77
	extension: WebviewExtensionDescription | undefined;
78 79 80 81
	initialScrollProgress: number;
	state: string | undefined;

	readonly onDidFocus: Event<void>;
82
	readonly onDidBlur: Event<void>;
83
	readonly onDidClickLink: Event<string>;
84
	readonly onDidScroll: Event<{ scrollYPercentage: number }>;
85
	readonly onDidWheel: Event<IMouseWheelEvent>;
86
	readonly onDidUpdateState: Event<string | undefined>;
87
	readonly onDidReload: Event<void>;
88
	readonly onMessage: Event<any>;
89
	readonly onMissingCsp: Event<ExtensionIdentifier>;
90 91 92 93 94 95

	sendMessage(data: any): void;

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

96 97
	showFind(): void;
	hideFind(): void;
98
	runFindAction(previous: boolean): void;
99

100 101
	selectAll(): void;

102 103
	windowDidDragStart(): void;
	windowDidDragEnd(): void;
104
}
105

106 107 108
/**
 * Basic webview rendered in the dom
 */
109 110 111 112
export interface WebviewElement extends Webview {
	mountTo(parent: HTMLElement): void;
}

113 114 115
/**
 * Dynamically created webview drawn over another element.
 */
116
export interface WebviewOverlay extends Webview {
117
	readonly container: HTMLElement;
118
	options: WebviewOptions;
119

120 121
	readonly onDispose: Event<void>;

122 123
	claim(owner: any): void;
	release(owner: any): void;
124 125

	getInnerWebview(): Webview | undefined;
126 127

	layoutWebviewOverElement(element: HTMLElement, dimension?: Dimension): void;
128 129
}

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