Skip to content

Commit 2091f71

Browse files
author
Matt Bierner
committed
Don't hang on to webview in WebviewEditor
1 parent eb0dc4c commit 2091f71

2 files changed

Lines changed: 23 additions & 33 deletions

File tree

src/vs/workbench/contrib/webview/browser/webviewEditor.ts

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class WebviewEditor extends BaseEditor {
2323

2424
public static readonly ID = 'WebviewEditor';
2525

26-
private _webview: WebviewEditorOverlay | undefined;
26+
private readonly _scopedContextKeyService = this._register(new MutableDisposable<IContextKeyService>());
2727
private _findWidgetVisible: IContextKey<boolean>;
2828

2929
private _editorFrame?: HTMLElement;
@@ -38,15 +38,14 @@ export class WebviewEditor extends BaseEditor {
3838
constructor(
3939
@ITelemetryService telemetryService: ITelemetryService,
4040
@IThemeService themeService: IThemeService,
41-
@IContextKeyService private _contextKeyService: IContextKeyService,
41+
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
4242
@IEditorService private readonly _editorService: IEditorService,
4343
@IWindowService private readonly _windowService: IWindowService,
4444
@IStorageService storageService: IStorageService
4545
) {
4646
super(WebviewEditor.ID, telemetryService, themeService, storageService);
47-
if (_contextKeyService) {
48-
this._findWidgetVisible = KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE.bindTo(_contextKeyService);
49-
}
47+
48+
this._findWidgetVisible = KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE.bindTo(_contextKeyService);
5049
}
5150

5251
public get isWebviewEditor() {
@@ -85,16 +84,15 @@ export class WebviewEditor extends BaseEditor {
8584
}
8685

8786
public layout(_dimension: DOM.Dimension): void {
88-
if (this._webview) {
89-
this.doUpdateContainer(this._webview);
90-
this._webview.layout();
87+
if (this.input && this.input instanceof WebviewEditorInput) {
88+
this.synchronizeWebviewContainerDimensions(this.input.webview);
89+
this.input.webview.layout();
9190
}
9291
}
9392

9493
public focus(): void {
9594
super.focus();
9695
if (!this._onFocusWindowHandler.value) {
97-
9896
// Make sure we restore focus when switching back to a VS Code window
9997
this._onFocusWindowHandler.value = this._windowService.onDidChangeFocus(focused => {
10098
if (focused && this._editorService.activeControl === this) {
@@ -106,13 +104,13 @@ export class WebviewEditor extends BaseEditor {
106104
}
107105

108106
public withWebview(f: (element: Webview) => void): void {
109-
if (this._webview) {
110-
f(this._webview);
107+
if (this.input && this.input instanceof WebviewEditorInput) {
108+
f(this.input.webview);
111109
}
112110
}
113111

114112
protected setEditorVisible(visible: boolean, group: IEditorGroup): void {
115-
const webview = (this.input && (this.input as WebviewEditorInput).webview) || this._webview;
113+
const webview = this.input && (this.input as WebviewEditorInput).webview;
116114
if (webview) {
117115
if (visible) {
118116
webview.claim(this);
@@ -130,14 +128,12 @@ export class WebviewEditor extends BaseEditor {
130128
this.input.webview.release(this);
131129
}
132130

133-
this._webview = undefined;
134131
super.clearInput();
135132
}
136133

137134
public async setInput(input: WebviewEditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
138-
if (this.input && (this.input as WebviewEditorInput).webview) {
139-
(this.input as WebviewEditorInput).webview!.release(this);
140-
this._webview = undefined;
135+
if (this.input && this.input instanceof WebviewEditorInput) {
136+
this.input.webview.release(this);
141137
}
142138

143139
await super.setInput(input, options, token);
@@ -149,32 +145,27 @@ export class WebviewEditor extends BaseEditor {
149145
if (this.group) {
150146
input.updateGroup(this.group.id);
151147
}
148+
152149
this.claimWebview(input);
153150
}
154151

155152
private claimWebview(input: WebviewEditorInput): void {
156-
if (this._webview) {
157-
this._webview.claim(this);
158-
return;
159-
}
160-
161-
this._webview = input.webview;
162-
this._webview.claim(this);
153+
input.webview.claim(this);
163154

164-
if (this._webview.options.enableFindWidget) {
165-
this._contextKeyService = this._register(this._contextKeyService.createScoped(this._webview.container));
166-
this._findWidgetVisible = KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE.bindTo(this._contextKeyService);
155+
if (input.webview.options.enableFindWidget) {
156+
this._scopedContextKeyService.value = this._contextKeyService.createScoped(input.webview.container);
157+
this._findWidgetVisible = KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE.bindTo(this._scopedContextKeyService.value);
167158
}
168159

169160
if (this._content) {
170-
this._content.setAttribute('aria-flowto', this._webview.container.id);
161+
this._content.setAttribute('aria-flowto', input.webview.container.id);
171162
}
172163

173-
this.doUpdateContainer(this._webview);
174-
this.trackFocus(this._webview);
164+
this.synchronizeWebviewContainerDimensions(input.webview);
165+
this.trackFocus(input.webview);
175166
}
176167

177-
private doUpdateContainer(webview: WebviewEditorOverlay) {
168+
private synchronizeWebviewContainerDimensions(webview: WebviewEditorOverlay) {
178169
const webviewContainer = webview.container;
179170
if (webviewContainer && webviewContainer.parentElement && this._editorFrame) {
180171
const frameRect = this._editorFrame.getBoundingClientRect();

src/vs/workbench/contrib/webview/browser/webviewEditorInput.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export class WebviewEditorInput extends EditorInput {
5959
private _iconPath?: { light: URI, dark: URI };
6060
private _group?: GroupIdentifier;
6161

62-
6362
constructor(
6463
public readonly id: string,
6564
public readonly viewType: string,
@@ -123,8 +122,8 @@ export class WebviewEditorInput extends EditorInput {
123122
return this._group;
124123
}
125124

126-
public resolve(): Promise<IEditorModel> {
127-
return Promise.resolve(new EditorModel());
125+
public async resolve(): Promise<IEditorModel> {
126+
return new EditorModel();
128127
}
129128

130129
public supportsSplitEditor() {

0 commit comments

Comments
 (0)