Skip to content

Commit

Permalink
Merge pull request microsoft#93048 from microsoft/rebornix/multi-webv…
Browse files Browse the repository at this point in the history
…iew-acquire

Allow multiple api acquire in notebook webview
  • Loading branch information
rebornix authored Mar 19, 2020
2 parents f0d8464 + b75371c commit a67cde2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ export class BackLayerWebView extends Disposable {
const webview = webviewService.createWebviewElement('' + UUID.generateUuid(), {
enableFindWidget: false,
}, {
allowMultipleAPIAcquire: true,
allowScripts: true,
localResourceRoots: this.localResourceRootsCache
});
Expand Down Expand Up @@ -409,6 +410,7 @@ export class BackLayerWebView extends Disposable {
const mixedResourceRoots = [...(this.localResourceRootsCache || []), ...this.rendererRootsCache];

this.webview.contentOptions = {
allowMultipleAPIAcquire: true,
allowScripts: true,
enableCommandUris: true,
localResourceRoots: mixedResourceRoots
Expand Down
7 changes: 4 additions & 3 deletions src/vs/workbench/contrib/webview/browser/pre/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@
}`;

/**
* @param {boolean} allowMultipleAPIAcquire
* @param {*} [state]
* @return {string}
*/
function getVsCodeApiScript(state) {
function getVsCodeApiScript(allowMultipleAPIAcquire, state) {
return `
const acquireVsCodeApi = (function() {
const originalPostMessage = window.parent.postMessage.bind(window.parent);
Expand All @@ -138,7 +139,7 @@
let state = ${state ? `JSON.parse(${JSON.stringify(state)})` : undefined};
return () => {
if (acquired) {
if (acquired && !${allowMultipleAPIAcquire}) {
throw new Error('An instance of the VS Code API has already been acquired');
}
acquired = true;
Expand Down Expand Up @@ -325,7 +326,7 @@
if (options.allowScripts) {
const defaultScript = newDocument.createElement('script');
defaultScript.id = '_vscodeApiScript';
defaultScript.textContent = getVsCodeApiScript(data.state);
defaultScript.textContent = getVsCodeApiScript(options.allowMultipleAPIAcquire, data.state);
newDocument.head.prepend(defaultScript);
}

Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/contrib/webview/browser/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface WebviewOptions {
}

export interface WebviewContentOptions {
readonly allowMultipleAPIAcquire?: boolean;
readonly allowScripts?: boolean;
readonly localResourceRoots?: ReadonlyArray<URI>;
readonly portMapping?: ReadonlyArray<modes.IWebviewPortMapping>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function areWebviewInputOptionsEqual(a: WebviewInputOptions, b: WebviewIn
return a.enableCommandUris === b.enableCommandUris
&& a.enableFindWidget === b.enableFindWidget
&& a.allowScripts === b.allowScripts
&& a.allowMultipleAPIAcquire === b.allowMultipleAPIAcquire
&& a.retainContextWhenHidden === b.retainContextWhenHidden
&& a.tryRestoreScrollPosition === b.tryRestoreScrollPosition
&& equals(a.localResourceRoots, b.localResourceRoots, isEqual)
Expand Down

0 comments on commit a67cde2

Please sign in to comment.