/
webviewElement.ts
824 lines (695 loc) · 23.3 KB
/
webviewElement.ts
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { OnBeforeRequestDetails, OnHeadersReceivedDetails, Response } from 'electron';
import { addClass, addDisposableListener } from 'vs/base/browser/dom';
import { Emitter, Event } from 'vs/base/common/event';
import { once } from 'vs/base/common/functional';
import { Disposable } from 'vs/base/common/lifecycle';
import { isMacintosh } from 'vs/base/common/platform';
import { endsWith } from 'vs/base/common/strings';
import { URI } from 'vs/base/common/uri';
import { EDITOR_FONT_DEFAULTS, IEditorOptions } from 'vs/editor/common/config/editorOptions';
import * as modes from 'vs/editor/common/modes';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { IFileService } from 'vs/platform/files/common/files';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { REMOTE_HOST_SCHEME } from 'vs/platform/remote/common/remoteHosts';
import { ITunnelService, RemoteTunnel } from 'vs/platform/remote/common/tunnel';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import * as colorRegistry from 'vs/platform/theme/common/colorRegistry';
import { DARK, ITheme, IThemeService, LIGHT } from 'vs/platform/theme/common/themeService';
import { Webview, WebviewContentOptions, WebviewOptions } from 'vs/workbench/contrib/webview/common/webview';
import { registerFileProtocol, WebviewProtocol } from 'vs/workbench/contrib/webview/electron-browser/webviewProtocols';
import { areWebviewInputOptionsEqual } from '../browser/webviewEditorService';
import { WebviewFindWidget } from '../browser/webviewFindWidget';
export interface WebviewPortMapping {
readonly port: number;
readonly resolvedPort: number;
}
export interface WebviewOptions {
readonly allowSvgs?: boolean;
readonly extension?: {
readonly location: URI;
readonly id?: ExtensionIdentifier;
};
readonly enableFindWidget?: boolean;
}
export interface WebviewContentOptions {
readonly allowScripts?: boolean;
readonly svgWhiteList?: string[];
readonly localResourceRoots?: ReadonlyArray<URI>;
readonly portMappings?: ReadonlyArray<WebviewPortMapping>;
}
interface IKeydownEvent {
key: string;
keyCode: number;
code: string;
shiftKey: boolean;
altKey: boolean;
ctrlKey: boolean;
metaKey: boolean;
repeat: boolean;
}
type OnBeforeRequestDelegate = (details: OnBeforeRequestDetails) => Promise<Response | undefined>;
type OnHeadersReceivedDelegate = (details: OnHeadersReceivedDetails) => { cancel: boolean } | undefined;
class WebviewSession extends Disposable {
private readonly _onBeforeRequestDelegates: Array<OnBeforeRequestDelegate> = [];
private readonly _onHeadersReceivedDelegates: Array<OnHeadersReceivedDelegate> = [];
public constructor(
webview: Electron.WebviewTag,
) {
super();
this._register(addDisposableListener(webview, 'did-start-loading', once(() => {
const contents = webview.getWebContents();
if (!contents) {
return;
}
contents.session.webRequest.onBeforeRequest(async (details, callback) => {
for (const delegate of this._onBeforeRequestDelegates) {
const result = await delegate(details);
if (typeof result !== 'undefined') {
callback(result);
return;
}
}
callback({});
});
contents.session.webRequest.onHeadersReceived((details, callback) => {
for (const delegate of this._onHeadersReceivedDelegates) {
const result = delegate(details);
if (typeof result !== 'undefined') {
callback(result);
return;
}
}
callback({ cancel: false, responseHeaders: details.responseHeaders });
});
})));
}
public onBeforeRequest(delegate: OnBeforeRequestDelegate) {
this._onBeforeRequestDelegates.push(delegate);
}
public onHeadersReceived(delegate: OnHeadersReceivedDelegate) {
this._onHeadersReceivedDelegates.push(delegate);
}
}
class WebviewProtocolProvider extends Disposable {
constructor(
webview: Electron.WebviewTag,
private readonly _extensionLocation: URI | undefined,
private readonly _getLocalResourceRoots: () => ReadonlyArray<URI>,
private readonly _environmentService: IEnvironmentService,
private readonly _fileService: IFileService,
) {
super();
this._register(addDisposableListener(webview, 'did-start-loading', once(() => {
const contents = webview.getWebContents();
if (contents) {
this.registerProtocols(contents);
}
})));
}
private registerProtocols(contents: Electron.WebContents) {
if (contents.isDestroyed()) {
return;
}
const appRootUri = URI.file(this._environmentService.appRoot);
registerFileProtocol(contents, WebviewProtocol.CoreResource, this._fileService, undefined, () => [
appRootUri
]);
registerFileProtocol(contents, WebviewProtocol.VsCodeResource, this._fileService, this._extensionLocation, () =>
this._getLocalResourceRoots()
);
}
}
class WebviewPortMappingProvider extends Disposable {
private readonly _tunnels = new Map<number, Promise<RemoteTunnel>>();
constructor(
session: WebviewSession,
extensionLocation: URI | undefined,
mappings: () => ReadonlyArray<modes.IWebviewPortMapping>,
private readonly tunnelService: ITunnelService,
extensionId: ExtensionIdentifier | undefined,
@ITelemetryService telemetryService: ITelemetryService
) {
super();
let hasLogged = false;
session.onBeforeRequest(async (details) => {
const uri = URI.parse(details.url);
if (uri.scheme !== 'http' && uri.scheme !== 'https') {
return undefined;
}
const localhostMatch = /^localhost:(\d+)$/.exec(uri.authority);
if (localhostMatch) {
if (!hasLogged && extensionId) {
hasLogged = true;
/* __GDPR__
"webview.accessLocalhost" : {
"extension" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
telemetryService.publicLog('webview.accessLocalhost', { extension: extensionId.value });
}
const port = +localhostMatch[1];
for (const mapping of mappings()) {
if (mapping.webviewPort === port) {
if (extensionLocation && extensionLocation.scheme === REMOTE_HOST_SCHEME) {
const tunnel = await this.getOrCreateTunnel(mapping.extensionHostPort);
if (tunnel) {
return {
redirectURL: details.url.replace(
new RegExp(`^${uri.scheme}://localhost:${mapping.webviewPort}/`),
`${uri.scheme}://localhost:${tunnel.tunnelLocalPort}/`)
};
}
}
if (mapping.webviewPort !== mapping.extensionHostPort) {
return {
redirectURL: details.url.replace(
new RegExp(`^${uri.scheme}://localhost:${mapping.webviewPort}/`),
`${uri.scheme}://localhost:${mapping.extensionHostPort}/`)
};
}
}
}
}
return undefined;
});
}
dispose() {
super.dispose();
for (const tunnel of this._tunnels.values()) {
tunnel.then(tunnel => tunnel.dispose());
}
this._tunnels.clear();
}
private getOrCreateTunnel(remotePort: number): Promise<RemoteTunnel> | undefined {
const existing = this._tunnels.get(remotePort);
if (existing) {
return existing;
}
const tunnel = this.tunnelService.openTunnel(remotePort);
if (tunnel) {
this._tunnels.set(remotePort, tunnel);
}
return tunnel;
}
}
class SvgBlocker extends Disposable {
private readonly _onDidBlockSvg = this._register(new Emitter<void>());
public readonly onDidBlockSvg = this._onDidBlockSvg.event;
constructor(
session: WebviewSession,
private readonly _options: WebviewContentOptions,
) {
super();
session.onBeforeRequest(async (details) => {
if (details.url.indexOf('.svg') > 0) {
const uri = URI.parse(details.url);
if (uri && !uri.scheme.match(/file/i) && endsWith(uri.path, '.svg') && !this.isAllowedSvg(uri)) {
this._onDidBlockSvg.fire();
return { cancel: true };
}
}
return undefined;
});
session.onHeadersReceived((details) => {
const contentType: string[] = details.responseHeaders['content-type'] || details.responseHeaders['Content-Type'];
if (contentType && Array.isArray(contentType) && contentType.some(x => x.toLowerCase().indexOf('image/svg') >= 0)) {
const uri = URI.parse(details.url);
if (uri && !this.isAllowedSvg(uri)) {
this._onDidBlockSvg.fire();
return { cancel: true };
}
}
return undefined;
});
}
private isAllowedSvg(uri: URI): boolean {
if (this._options.svgWhiteList) {
return this._options.svgWhiteList.indexOf(uri.authority.toLowerCase()) >= 0;
}
return false;
}
}
class WebviewKeyboardHandler extends Disposable {
private _ignoreMenuShortcut = false;
constructor(
private readonly _webview: Electron.WebviewTag
) {
super();
if (this.shouldToggleMenuShortcutsEnablement) {
this._register(addDisposableListener(this._webview, 'did-start-loading', () => {
const contents = this.getWebContents();
if (contents) {
contents.on('before-input-event', (_event, input) => {
if (input.type === 'keyDown' && document.activeElement === this._webview) {
this._ignoreMenuShortcut = input.control || input.meta;
this.setIgnoreMenuShortcuts(this._ignoreMenuShortcut);
}
});
}
}));
}
this._register(addDisposableListener(this._webview, 'ipc-message', (event) => {
switch (event.channel) {
case 'did-keydown':
// Electron: workaround for https://github.com/electron/electron/issues/14258
// We have to detect keyboard events in the <webview> and dispatch them to our
// keybinding service because these events do not bubble to the parent window anymore.
this.handleKeydown(event.args[0]);
return;
case 'did-focus':
this.setIgnoreMenuShortcuts(this._ignoreMenuShortcut);
break;
case 'did-blur':
this.setIgnoreMenuShortcuts(false);
return;
}
}));
}
private get shouldToggleMenuShortcutsEnablement() {
return isMacintosh;
}
private setIgnoreMenuShortcuts(value: boolean) {
if (!this.shouldToggleMenuShortcutsEnablement) {
return;
}
const contents = this.getWebContents();
if (contents) {
contents.setIgnoreMenuShortcuts(value);
}
}
private getWebContents(): Electron.WebContents | undefined {
const contents = this._webview.getWebContents();
if (contents && !contents.isDestroyed()) {
return contents;
}
return undefined;
}
private handleKeydown(event: IKeydownEvent): void {
// Create a fake KeyboardEvent from the data provided
const emulatedKeyboardEvent = new KeyboardEvent('keydown', event);
// Force override the target
Object.defineProperty(emulatedKeyboardEvent, 'target', {
get: () => this._webview
});
// And re-dispatch
window.dispatchEvent(emulatedKeyboardEvent);
}
}
interface WebviewContent {
readonly html: string;
readonly options: WebviewContentOptions;
readonly state: string | undefined;
}
export class WebviewElement extends Disposable implements Webview {
private _webview: Electron.WebviewTag | undefined;
private _ready: Promise<void>;
private _webviewFindWidget: WebviewFindWidget | undefined;
private _findStarted: boolean = false;
private content: WebviewContent;
private _focused = false;
private readonly _onDidFocus = this._register(new Emitter<void>());
public get onDidFocus(): Event<void> { return this._onDidFocus.event; }
constructor(
private readonly _options: WebviewOptions,
contentOptions: WebviewContentOptions,
@IInstantiationService instantiationService: IInstantiationService,
@IThemeService themeService: IThemeService,
@IEnvironmentService environmentService: IEnvironmentService,
@IFileService fileService: IFileService,
@ITunnelService tunnelService: ITunnelService,
@ITelemetryService telemetryService: ITelemetryService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
) {
super();
this.content = {
html: '',
options: contentOptions,
state: undefined
};
this._webview = document.createElement('webview');
this._webview.setAttribute('partition', `webview${Date.now()}`);
this._webview.setAttribute('webpreferences', 'contextIsolation=yes,nodeIntegration=yes');
this._webview.style.flex = '0 1';
this._webview.style.width = '0';
this._webview.style.height = '0';
this._webview.style.outline = '0';
this._webview.preload = require.toUrl('./pre/electron-index.js');
this._webview.src = 'data:text/html;charset=utf-8,%3C%21DOCTYPE%20html%3E%0D%0A%3Chtml%20lang%3D%22en%22%20style%3D%22width%3A%20100%25%3B%20height%3A%20100%25%22%3E%0D%0A%3Chead%3E%0D%0A%09%3Ctitle%3EVirtual%20Document%3C%2Ftitle%3E%0D%0A%3C%2Fhead%3E%0D%0A%3Cbody%20style%3D%22margin%3A%200%3B%20overflow%3A%20hidden%3B%20width%3A%20100%25%3B%20height%3A%20100%25%22%3E%0D%0A%3C%2Fbody%3E%0D%0A%3C%2Fhtml%3E';
this._ready = new Promise(resolve => {
const subscription = this._register(addDisposableListener(this._webview!, 'ipc-message', (event) => {
if (this._webview && event.channel === 'webview-ready') {
// console.info('[PID Webview] ' event.args[0]);
addClass(this._webview, 'ready'); // can be found by debug command
subscription.dispose();
resolve();
}
}));
});
const session = this._register(new WebviewSession(this._webview));
this._register(new WebviewProtocolProvider(
this._webview,
this._options.extension ? this._options.extension.location : undefined,
() => (this.content.options.localResourceRoots || []),
environmentService,
fileService));
this._register(new WebviewPortMappingProvider(
session,
_options.extension ? _options.extension.location : undefined,
() => (this.content.options.portMappings || []),
tunnelService,
_options.extension ? _options.extension.id : undefined,
telemetryService
));
if (!this._options.allowSvgs) {
const svgBlocker = this._register(new SvgBlocker(session, this.content.options));
svgBlocker.onDidBlockSvg(() => this.onDidBlockSvg());
}
this._register(new WebviewKeyboardHandler(this._webview));
this._register(addDisposableListener(this._webview, 'console-message', function (e: { level: number; message: string; line: number; sourceId: string; }) {
console.log(`[Embedded Page] ${e.message}`);
}));
this._register(addDisposableListener(this._webview, 'dom-ready', () => {
this.layout();
// Workaround for https://github.com/electron/electron/issues/14474
if (this._webview && (this._focused || document.activeElement === this._webview)) {
this._webview.blur();
this._webview.focus();
}
}));
this._register(addDisposableListener(this._webview, 'crashed', () => {
console.error('embedded page crashed');
}));
this._register(addDisposableListener(this._webview, 'ipc-message', (event) => {
if (!this._webview) {
return;
}
switch (event.channel) {
case 'onmessage':
if (event.args && event.args.length) {
this._onMessage.fire(event.args[0]);
}
return;
case 'did-click-link':
let [uri] = event.args;
this._onDidClickLink.fire(URI.parse(uri));
return;
case 'did-set-content':
this._webview.style.flex = '';
this._webview.style.width = '100%';
this._webview.style.height = '100%';
this.layout();
return;
case 'did-scroll':
if (event.args && typeof event.args[0] === 'number') {
this._onDidScroll.fire({ scrollYPercentage: event.args[0] });
}
return;
case 'do-reload':
this.reload();
return;
case 'do-update-state':
const state = event.args[0];
this.state = state;
this._onDidUpdateState.fire(state);
return;
case 'did-focus':
this.handleFocusChange(true);
return;
case 'did-blur':
this.handleFocusChange(false);
return;
}
}));
this._register(addDisposableListener(this._webview, 'devtools-opened', () => {
this._send('devtools-opened');
}));
if (_options.enableFindWidget) {
this._webviewFindWidget = this._register(instantiationService.createInstance(WebviewFindWidget, this));
}
this.style(themeService.getTheme());
this._register(themeService.onThemeChange(this.style, this));
}
public mountTo(parent: HTMLElement) {
if (!this._webview) {
return;
}
if (this._webviewFindWidget) {
parent.appendChild(this._webviewFindWidget.getDomNode()!);
}
parent.appendChild(this._webview);
}
dispose(): void {
if (this._webview) {
if (this._webview.parentElement) {
this._webview.parentElement.removeChild(this._webview);
}
}
this._webview = undefined;
this._webviewFindWidget = undefined;
super.dispose();
}
private readonly _onDidClickLink = this._register(new Emitter<URI>());
public readonly onDidClickLink = this._onDidClickLink.event;
private readonly _onDidScroll = this._register(new Emitter<{ scrollYPercentage: number }>());
public readonly onDidScroll = this._onDidScroll.event;
private readonly _onDidUpdateState = this._register(new Emitter<string | undefined>());
public readonly onDidUpdateState = this._onDidUpdateState.event;
private readonly _onMessage = this._register(new Emitter<any>());
public readonly onMessage = this._onMessage.event;
private _send(channel: string, ...args: any[]): void {
this._ready
.then(() => {
if (this._webview) {
this._webview.send(channel, ...args);
}
})
.catch(err => console.error(err));
}
public set initialScrollProgress(value: number) {
this._send('initial-scroll-position', value);
}
public set state(state: string | undefined) {
this.content = {
html: this.content.html,
options: this.content.options,
state,
};
}
public set options(options: WebviewContentOptions) {
if (areWebviewInputOptionsEqual(options, this.content.options)) {
return;
}
this.content = {
html: this.content.html,
options: options,
state: this.content.state,
};
this.doUpdateContent();
}
public set html(value: string) {
this.content = {
html: value,
options: this.content.options,
state: this.content.state,
};
this.doUpdateContent();
}
public update(html: string, options: WebviewContentOptions, retainContextWhenHidden: boolean) {
if (retainContextWhenHidden && html === this.content.html && areWebviewInputOptionsEqual(options, this.content.options)) {
return;
}
this.content = {
html: html,
options: options,
state: this.content.state,
};
this.doUpdateContent();
}
private doUpdateContent() {
this._send('content', {
contents: this.content.html,
options: this.content.options,
state: this.content.state
});
}
public focus(): void {
if (!this._webview) {
return;
}
this._webview.focus();
this._send('focus');
// Handle focus change programmatically (do not rely on event from <webview>)
this.handleFocusChange(true);
}
private handleFocusChange(isFocused: boolean): void {
this._focused = isFocused;
if (isFocused) {
this._onDidFocus.fire();
}
}
public sendMessage(data: any): void {
this._send('message', data);
}
private onDidBlockSvg() {
this.sendMessage({
name: 'vscode-did-block-svg'
});
}
private style(theme: ITheme): void {
const configuration = this._configurationService.getValue<IEditorOptions>('editor');
const editorFontFamily = configuration.fontFamily || EDITOR_FONT_DEFAULTS.fontFamily;
const editorFontWeight = configuration.fontWeight || EDITOR_FONT_DEFAULTS.fontWeight;
const editorFontSize = configuration.fontSize || EDITOR_FONT_DEFAULTS.fontSize;
const exportedColors = colorRegistry.getColorRegistry().getColors().reduce((colors, entry) => {
const color = theme.getColor(entry.id);
if (color) {
colors['vscode-' + entry.id.replace('.', '-')] = color.toString();
}
return colors;
}, {} as { [key: string]: string });
const styles = {
'vscode-font-family': '-apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif',
'vscode-font-weight': 'normal',
'vscode-font-size': '13px',
'vscode-editor-font-family': editorFontFamily,
'vscode-editor-font-weight': editorFontWeight,
'vscode-editor-font-size': editorFontSize,
...exportedColors
};
const activeTheme = ApiThemeClassName.fromTheme(theme);
this._send('styles', styles, activeTheme);
if (this._webviewFindWidget) {
this._webviewFindWidget.updateTheme(theme);
}
}
public layout(): void {
if (!this._webview) {
return;
}
const contents = this._webview.getWebContents();
if (!contents || contents.isDestroyed()) {
return;
}
const window = (contents as any).getOwnerBrowserWindow();
if (!window || !window.webContents || window.webContents.isDestroyed()) {
return;
}
window.webContents.getZoomFactor((factor: number) => {
if (contents.isDestroyed()) {
return;
}
contents.setZoomFactor(factor);
});
}
public startFind(value: string, options?: Electron.FindInPageOptions) {
if (!value || !this._webview) {
return;
}
// ensure options is defined without modifying the original
options = options || {};
// FindNext must be false for a first request
const findOptions: Electron.FindInPageOptions = {
forward: options.forward,
findNext: false,
matchCase: options.matchCase,
medialCapitalAsWordStart: options.medialCapitalAsWordStart
};
this._findStarted = true;
this._webview.findInPage(value, findOptions);
}
/**
* Webviews expose a stateful find API.
* Successive calls to find will move forward or backward through onFindResults
* depending on the supplied options.
*
* @param value The string to search for. Empty strings are ignored.
*/
public find(value: string, previous: boolean): void {
if (!this._webview) {
return;
}
// Searching with an empty value will throw an exception
if (!value) {
return;
}
const options = { findNext: true, forward: !previous };
if (!this._findStarted) {
this.startFind(value, options);
return;
}
this._webview.findInPage(value, options);
}
public stopFind(keepSelection?: boolean): void {
if (!this._webview) {
return;
}
this._findStarted = false;
this._webview.stopFindInPage(keepSelection ? 'keepSelection' : 'clearSelection');
}
public showFind() {
if (this._webviewFindWidget) {
this._webviewFindWidget.reveal();
}
}
public hideFind() {
if (this._webviewFindWidget) {
this._webviewFindWidget.hide();
}
}
public reload() {
this.doUpdateContent();
}
public selectAll() {
if (this._webview) {
this._webview.selectAll();
}
}
public copy() {
if (this._webview) {
this._webview.copy();
}
}
public paste() {
if (this._webview) {
this._webview.paste();
}
}
public cut() {
if (this._webview) {
this._webview.cut();
}
}
public undo() {
if (this._webview) {
this._webview.undo();
}
}
public redo() {
if (this._webview) {
this._webview.redo();
}
}
}
enum ApiThemeClassName {
light = 'vscode-light',
dark = 'vscode-dark',
highContrast = 'vscode-high-contrast'
}
namespace ApiThemeClassName {
export function fromTheme(theme: ITheme): ApiThemeClassName {
if (theme.type === LIGHT) {
return ApiThemeClassName.light;
} else if (theme.type === DARK) {
return ApiThemeClassName.dark;
} else {
return ApiThemeClassName.highContrast;
}
}
}