Skip to content

Commit 6cea141

Browse files
committed
Add API for storing state in webviews contents themselves
1 parent 9370ce9 commit 6cea141

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/vs/workbench/parts/webview/electron-browser/webview-pre.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@
179179
const originalPostMessage = window.parent.postMessage.bind(window.parent);
180180
let acquired = false;
181181
182+
let state = ${data.state ? `JSON.parse(${JSON.stringify(data.state)})` : undefined};
183+
182184
return () => {
183185
if (acquired) {
184186
throw new Error('An instance of the VS Code API has already been acquired');
@@ -187,6 +189,12 @@
187189
return Object.freeze({
188190
postMessage: function(msg) {
189191
return originalPostMessage({ command: 'onmessage', data: msg }, '*');
192+
},
193+
setState: function(state) {
194+
return originalPostMessage({ command: 'do-update-state', data: JSON.stringify(state) }, '*');
195+
},
196+
getState: function() {
197+
return state;
190198
}
191199
});
192200
};

src/vs/workbench/parts/webview/electron-browser/webviewEditor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ export class WebviewEditor extends BaseWebviewEditor {
219219
this._webview.initialScrollProgress = input.scrollYPercentage;
220220
}
221221

222+
this._webview.state = input.webviewState;
223+
222224
this.content.setAttribute('aria-flowto', this.webviewContent.id);
223225

224226
this.doUpdateContainer();

src/vs/workbench/parts/webview/electron-browser/webviewEditorInput.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class WebviewEditorInput extends EditorInput {
3030
private _position?: Position;
3131
private _scrollYPercentage: number = 0;
3232
private _state: any;
33+
private _webviewState: string | undefined;
3334

3435
private _revived: boolean = false;
3536

@@ -130,6 +131,10 @@ export class WebviewEditorInput extends EditorInput {
130131
this._state = value;
131132
}
132133

134+
public get webviewState() {
135+
return this._webviewState;
136+
}
137+
133138
public get options(): WebviewInputOptions {
134139
return this._options;
135140
}
@@ -185,6 +190,10 @@ export class WebviewEditorInput extends EditorInput {
185190
this._webview.onDidScroll(message => {
186191
this._scrollYPercentage = message.scrollYPercentage;
187192
}, null, this._webviewDisposables);
193+
194+
this._webview.onDidUpdateState(newState => {
195+
this._webviewState = newState;
196+
}, null, this._webviewDisposables);
188197
}
189198

190199
public get scrollYPercentage() {

src/vs/workbench/parts/webview/electron-browser/webviewElement.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class WebviewElement {
3333
private _webviewFindWidget: WebviewFindWidget;
3434
private _findStarted: boolean = false;
3535
private _contents: string = '';
36+
private _state: string | undefined = undefined;
3637

3738
constructor(
3839
private readonly _styleElement: Element,
@@ -162,6 +163,11 @@ export class WebviewElement {
162163
case 'do-reload':
163164
this.reload();
164165
return;
166+
167+
case 'do-update-state':
168+
this._state = event.args[0];
169+
this._onDidUpdateState.fire(this._state);
170+
return;
165171
}
166172
}),
167173
addDisposableListener(this._webview, 'focus', () => {
@@ -220,6 +226,9 @@ export class WebviewElement {
220226
private readonly _onDidScroll = new Emitter<{ scrollYPercentage: number }>();
221227
public readonly onDidScroll: Event<{ scrollYPercentage: number }> = this._onDidScroll.event;
222228

229+
private readonly _onDidUpdateState = new Emitter<string | undefined>();
230+
public readonly onDidUpdateState: Event<string | undefined> = this._onDidUpdateState.event;
231+
223232
private readonly _onMessage = new Emitter<any>();
224233
public readonly onMessage: Event<any> = this._onMessage.event;
225234

@@ -233,6 +242,10 @@ export class WebviewElement {
233242
this._send('initial-scroll-position', value);
234243
}
235244

245+
public set state(value: string | undefined) {
246+
this._state = value;
247+
}
248+
236249
public set options(value: WebviewOptions) {
237250
this._options = value;
238251
}
@@ -241,7 +254,8 @@ export class WebviewElement {
241254
this._contents = value;
242255
this._send('content', {
243256
contents: value,
244-
options: this._options
257+
options: this._options,
258+
state: this._state
245259
});
246260
}
247261

0 commit comments

Comments
 (0)