1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Dimension } from 'vs/base/browser/dom';
import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
import { Emitter, Event } from 'vs/base/common/event';
import { Disposable, DisposableStore, MutableDisposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { ILayoutService } from 'vs/platform/layout/browser/layoutService';
import { IWebviewService, KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_ENABLED, KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE, IWebview, WebviewContentOptions, IWebviewElement, WebviewExtensionDescription, WebviewMessageReceivedEvent, WebviewOptions, IOverlayWebview } from 'vs/workbench/contrib/webview/browser/webview';
/**
* Webview that is absolutely positioned over another element and that can creates and destroys an underlying webview as needed.
*/
export class OverlayWebview extends Disposable implements IOverlayWebview {
private readonly _onDidWheel = this._register(new Emitter<IMouseWheelEvent>());
public readonly onDidWheel = this._onDidWheel.event;
private _isFirstLoad = true;
private readonly _firstLoadPendingMessages = new Set<{ readonly message: any; readonly transfer?: readonly ArrayBuffer[]; readonly resolve: (value: boolean) => void }>();
private readonly _webview = this._register(new MutableDisposable<IWebviewElement>());
private readonly _webviewEvents = this._register(new DisposableStore());
private _html: string = '';
private _initialScrollProgress: number = 0;
private _state: string | undefined = undefined;
private _extension: WebviewExtensionDescription | undefined;
private _contentOptions: WebviewContentOptions;
private _options: WebviewOptions;
private _owner: any = undefined;
private readonly _scopedContextKeyService = this._register(new MutableDisposable<IContextKeyService>());
private _findWidgetVisible: IContextKey<boolean> | undefined;
private _findWidgetEnabled: IContextKey<boolean> | undefined;
public constructor(
public readonly id: string,
initialOptions: WebviewOptions,
initialContentOptions: WebviewContentOptions,
extension: WebviewExtensionDescription | undefined,
@ILayoutService private readonly _layoutService: ILayoutService,
@IWebviewService private readonly _webviewService: IWebviewService,
@IContextKeyService private readonly _baseContextKeyService: IContextKeyService
) {
super();
this._extension = extension;
this._options = initialOptions;
this._contentOptions = initialContentOptions;
}
public get isFocused() {
return !!this._webview.value?.isFocused;
}
private _isDisposed = false;
private readonly _onDidDispose = this._register(new Emitter<void>());
public onDidDispose = this._onDidDispose.event;
override dispose() {
this._isDisposed = true;
this._container?.remove();
this._container = undefined;
for (const msg of this._firstLoadPendingMessages) {
msg.resolve(false);
}
this._firstLoadPendingMessages.clear();
this._onDidDispose.fire();
super.dispose();
}
private _container: HTMLElement | undefined;
public get container(): HTMLElement {
if (this._isDisposed) {
throw new Error(`DynamicWebviewEditorOverlay has been disposed`);
}
if (!this._container) {
this._container = document.createElement('div');
this._container.id = `webview-${this.id}`;
this._container.style.visibility = 'hidden';
// Webviews cannot be reparented in the dom as it will destroy their contents.
// Mount them to a high level node to avoid this.
this._layoutService.container.appendChild(this._container);
}
return this._container;
}
public claim(owner: any, scopedContextKeyService: IContextKeyService | undefined) {
const oldOwner = this._owner;
this._owner = owner;
this.show();
if (oldOwner !== owner) {
const contextKeyService = (scopedContextKeyService || this._baseContextKeyService);
// Explicitly clear before creating the new context.
// Otherwise we create the new context while the old one is still around
this._scopedContextKeyService.clear();
this._scopedContextKeyService.value = contextKeyService.createScoped(this.container);
this._findWidgetVisible?.reset();
this._findWidgetVisible = KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE.bindTo(contextKeyService);
this._findWidgetEnabled?.reset();
this._findWidgetEnabled = KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_ENABLED.bindTo(contextKeyService);
this._findWidgetEnabled.set(!!this.options.enableFindWidget);
this._webview.value?.setContextKeyService(this._scopedContextKeyService.value);
}
}
public release(owner: any) {
if (this._owner !== owner) {
return;
}
this._scopedContextKeyService.clear();
this._owner = undefined;
if (this._container) {
this._container.style.visibility = 'hidden';
}
if (!this._options.retainContextWhenHidden) {
this._webview.clear();
this._webviewEvents.clear();
}
}
public layoutWebviewOverElement(element: HTMLElement, dimension?: Dimension) {
if (!this._container || !this._container.parentElement) {
return;
}
const frameRect = element.getBoundingClientRect();
const containerRect = this._container.parentElement.getBoundingClientRect();
const parentBorderTop = (containerRect.height - this._container.parentElement.clientHeight) / 2.0;
const parentBorderLeft = (containerRect.width - this._container.parentElement.clientWidth) / 2.0;
this._container.style.position = 'absolute';
this._container.style.overflow = 'hidden';
this._container.style.top = `${frameRect.top - containerRect.top - parentBorderTop}px`;
this._container.style.left = `${frameRect.left - containerRect.left - parentBorderLeft}px`;
this._container.style.width = `${dimension ? dimension.width : frameRect.width}px`;
this._container.style.height = `${dimension ? dimension.height : frameRect.height}px`;
}
private show() {
if (this._isDisposed) {
throw new Error('Webview overlay is disposed');
}
if (!this._webview.value) {
const webview = this._webviewService.createWebviewElement(this.id, this._options, this._contentOptions, this.extension);
this._webview.value = webview;
webview.state = this._state;
if (this._scopedContextKeyService.value) {
this._webview.value.setContextKeyService(this._scopedContextKeyService.value);
}
if (this._html) {
webview.html = this._html;
}
if (this._options.tryRestoreScrollPosition) {
webview.initialScrollProgress = this._initialScrollProgress;
}
this._findWidgetEnabled?.set(!!this.options.enableFindWidget);
webview.mountTo(this.container);
// Forward events from inner webview to outer listeners
this._webviewEvents.clear();
this._webviewEvents.add(webview.onDidFocus(() => { this._onDidFocus.fire(); }));
this._webviewEvents.add(webview.onDidBlur(() => { this._onDidBlur.fire(); }));
this._webviewEvents.add(webview.onDidClickLink(x => { this._onDidClickLink.fire(x); }));
this._webviewEvents.add(webview.onMessage(x => { this._onMessage.fire(x); }));
this._webviewEvents.add(webview.onMissingCsp(x => { this._onMissingCsp.fire(x); }));
this._webviewEvents.add(webview.onDidWheel(x => { this._onDidWheel.fire(x); }));
this._webviewEvents.add(webview.onDidReload(() => { this._onDidReload.fire(); }));
this._webviewEvents.add(webview.onDidScroll(x => {
this._initialScrollProgress = x.scrollYPercentage;
this._onDidScroll.fire(x);
}));
this._webviewEvents.add(webview.onDidUpdateState(state => {
this._state = state;
this._onDidUpdateState.fire(state);
}));
if (this._isFirstLoad) {
this._firstLoadPendingMessages.forEach(async msg => {
msg.resolve(await webview.postMessage(msg.message, msg.transfer));
});
}
this._isFirstLoad = false;
this._firstLoadPendingMessages.clear();
}
this.container.style.visibility = 'visible';
}
public get html(): string { return this._html; }
public set html(value: string) {
this._html = value;
this.withWebview(webview => webview.html = value);
}
public get initialScrollProgress(): number { return this._initialScrollProgress; }
public set initialScrollProgress(value: number) {
this._initialScrollProgress = value;
this.withWebview(webview => webview.initialScrollProgress = value);
}
public get state(): string | undefined { return this._state; }
public set state(value: string | undefined) {
this._state = value;
this.withWebview(webview => webview.state = value);
}
public get extension(): WebviewExtensionDescription | undefined { return this._extension; }
public set extension(value: WebviewExtensionDescription | undefined) {
this._extension = value;
this.withWebview(webview => webview.extension = value);
}
public get options(): WebviewOptions { return this._options; }
public set options(value: WebviewOptions) { this._options = { customClasses: this._options.customClasses, ...value }; }
public get contentOptions(): WebviewContentOptions { return this._contentOptions; }
public set contentOptions(value: WebviewContentOptions) {
this._contentOptions = value;
this.withWebview(webview => webview.contentOptions = value);
}
public set localResourcesRoot(resources: URI[]) {
this.withWebview(webview => webview.localResourcesRoot = resources);
}
private readonly _onDidFocus = this._register(new Emitter<void>());
public readonly onDidFocus: Event<void> = this._onDidFocus.event;
private readonly _onDidBlur = this._register(new Emitter<void>());
public readonly onDidBlur: Event<void> = this._onDidBlur.event;
private readonly _onDidClickLink = this._register(new Emitter<string>());
public readonly onDidClickLink: Event<string> = this._onDidClickLink.event;
private readonly _onDidReload = this._register(new Emitter<void>());
public readonly onDidReload = this._onDidReload.event;
private readonly _onDidScroll = this._register(new Emitter<{ scrollYPercentage: number }>());
public readonly onDidScroll: Event<{ scrollYPercentage: number }> = this._onDidScroll.event;
private readonly _onDidUpdateState = this._register(new Emitter<string | undefined>());
public readonly onDidUpdateState: Event<string | undefined> = this._onDidUpdateState.event;
private readonly _onMessage = this._register(new Emitter<WebviewMessageReceivedEvent>());
public readonly onMessage = this._onMessage.event;
private readonly _onMissingCsp = this._register(new Emitter<ExtensionIdentifier>());
public readonly onMissingCsp: Event<any> = this._onMissingCsp.event;
public async postMessage(message: any, transfer?: readonly ArrayBuffer[]): Promise<boolean> {
if (this._webview.value) {
return this._webview.value.postMessage(message, transfer);
}
if (this._isFirstLoad) {
let resolve: (x: boolean) => void;
const p = new Promise<boolean>(r => resolve = r);
this._firstLoadPendingMessages.add({ message, transfer, resolve: resolve! });
return p;
}
return false;
}
focus(): void { this._webview.value?.focus(); }
reload(): void { this._webview.value?.reload(); }
selectAll(): void { this._webview.value?.selectAll(); }
copy(): void { this._webview.value?.copy(); }
paste(): void { this._webview.value?.paste(); }
cut(): void { this._webview.value?.cut(); }
undo(): void { this._webview.value?.undo(); }
redo(): void { this._webview.value?.redo(); }
showFind() {
if (this._webview.value) {
this._webview.value.showFind();
this._findWidgetVisible?.set(true);
}
}
hideFind() {
this._findWidgetVisible?.reset();
this._webview.value?.hideFind();
}
runFindAction(previous: boolean): void { this._webview.value?.runFindAction(previous); }
private withWebview(f: (webview: IWebview) => void): void {
if (this._webview.value) {
f(this._webview.value);
}
}
windowDidDragStart() {
this._webview.value?.windowDidDragStart();
}
windowDidDragEnd() {
this._webview.value?.windowDidDragEnd();
}
setContextKeyService(contextKeyService: IContextKeyService) {
this._webview.value?.setContextKeyService(contextKeyService);
}
}
|