webview.ts 4.7 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
	readonly _serviceBrand: undefined;
38

39
	createWebviewElement(
40
		id: string,
41 42
		options: WebviewOptions,
		contentOptions: WebviewContentOptions,
43
		extension: WebviewExtensionDescription | undefined,
44 45
	): WebviewElement;

46
	createWebviewOverlay(
47 48 49
		id: string,
		options: WebviewOptions,
		contentOptions: WebviewContentOptions,
50
		extension: WebviewExtensionDescription | undefined,
51
	): WebviewOverlay;
52 53

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

56 57 58 59 60
export const enum WebviewContentPurpose {
	NotebookRenderer = 'notebookRenderer',
	CustomEditor = 'customEditor',
}

61
export interface WebviewOptions {
62 63
	// The purpose of the webview; this is (currently) only used for filtering in js-debug
	readonly purpose?: WebviewContentPurpose;
64
	readonly customClasses?: string;
65
	readonly enableFindWidget?: boolean;
66 67
	readonly tryRestoreScrollPosition?: boolean;
	readonly retainContextWhenHidden?: boolean;
68 69 70
}

export interface WebviewContentOptions {
R
rebornix 已提交
71
	readonly allowMultipleAPIAcquire?: boolean;
72 73
	readonly allowScripts?: boolean;
	readonly localResourceRoots?: ReadonlyArray<URI>;
74
	readonly portMapping?: ReadonlyArray<modes.IWebviewPortMapping>;
75
	readonly enableCommandUris?: boolean;
76 77
}

M
Matt Bierner 已提交
78 79 80 81 82
export interface WebviewExtensionDescription {
	readonly location: URI;
	readonly id: ExtensionIdentifier;
}

83 84 85 86 87
export interface IDataLinkClickEvent {
	dataURL: string;
	downloadName?: string;
}

88
export interface Webview extends IDisposable {
89 90 91

	readonly id: string;

M
Matt Bierner 已提交
92
	html: string;
93
	contentOptions: WebviewContentOptions;
94
	localResourcesRoot: URI[];
M
Matt Bierner 已提交
95
	extension: WebviewExtensionDescription | undefined;
96 97 98
	initialScrollProgress: number;
	state: string | undefined;

99 100
	readonly isFocused: boolean;

101
	readonly onDidFocus: Event<void>;
102
	readonly onDidBlur: Event<void>;
103
	readonly onDidClickLink: Event<string>;
104
	readonly onDidScroll: Event<{ scrollYPercentage: number }>;
105
	readonly onDidWheel: Event<IMouseWheelEvent>;
106
	readonly onDidUpdateState: Event<string | undefined>;
107
	readonly onDidReload: Event<void>;
108
	readonly onMessage: Event<any>;
109
	readonly onMissingCsp: Event<ExtensionIdentifier>;
110

111
	postMessage(data: any): void;
112 113 114 115

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

116 117
	showFind(): void;
	hideFind(): void;
118
	runFindAction(previous: boolean): void;
119

120
	selectAll(): void;
121 122 123 124 125
	copy(): void;
	paste(): void;
	cut(): void;
	undo(): void;
	redo(): void;
126

127 128
	windowDidDragStart(): void;
	windowDidDragEnd(): void;
129
}
130

131 132 133
/**
 * Basic webview rendered in the dom
 */
134 135 136 137
export interface WebviewElement extends Webview {
	mountTo(parent: HTMLElement): void;
}

138 139 140
/**
 * Dynamically created webview drawn over another element.
 */
141
export interface WebviewOverlay extends Webview {
142
	readonly container: HTMLElement;
143
	options: WebviewOptions;
144

145 146
	readonly onDispose: Event<void>;

147 148
	claim(owner: any): void;
	release(owner: any): void;
149 150

	getInnerWebview(): Webview | undefined;
151 152

	layoutWebviewOverElement(element: HTMLElement, dimension?: Dimension): void;
153 154
}

B
Benjamin Pasero 已提交
155
export const webviewDeveloperCategory = { value: nls.localize({ key: 'developer', comment: ['A developer on Code itself or someone diagnosing issues in Code'] }, "Developer"), original: 'Developer' };