Skip to content

Commit 52ee3fe

Browse files
Fix CustomViews by switching to WebViews
1 parent 0cbb2f4 commit 52ee3fe

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

src/features/CustomViews.ts

+26-26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (C) Microsoft Corporation. All rights reserved.
33
*--------------------------------------------------------*/
44

5+
import fs = require("fs");
56
import vscode = require("vscode");
67
import { LanguageClient, NotificationType, RequestType } from "vscode-languageclient";
78
import { IFeature } from "../feature";
@@ -94,13 +95,7 @@ class PowerShellContentProvider implements vscode.TextDocumentContentProvider {
9495

9596
public showView(id: string, viewColumn: vscode.ViewColumn) {
9697
const uriString = this.getUri(id);
97-
const view: CustomView = this.viewIndex[uriString];
98-
99-
vscode.commands.executeCommand(
100-
"vscode.previewHtml",
101-
uriString,
102-
viewColumn,
103-
view.title);
98+
(this.viewIndex[uriString] as HtmlContentView).showContent(viewColumn);
10499
}
105100

106101
public closeView(id: string) {
@@ -164,6 +159,8 @@ class HtmlContentView extends CustomView {
164159
styleSheetPaths: [],
165160
};
166161

162+
private webviewPanel: vscode.WebviewPanel;
163+
167164
constructor(
168165
id: string,
169166
title: string) {
@@ -179,42 +176,45 @@ class HtmlContentView extends CustomView {
179176
}
180177

181178
public getContent(): string {
182-
let styleSrc = "none";
183179
let styleTags = "";
184-
185-
function getNonce(): number {
186-
return Math.floor(Math.random() * 100000) + 100000;
187-
}
188-
189180
if (this.htmlContent.styleSheetPaths &&
190181
this.htmlContent.styleSheetPaths.length > 0) {
191-
styleSrc = "";
192182
this.htmlContent.styleSheetPaths.forEach(
193183
(p) => {
194-
const nonce = getNonce();
195-
styleSrc += `'nonce-${nonce}' `;
196-
styleTags += `<link nonce="${nonce}" href="${p}" rel="stylesheet" type="text/css" />\n`;
184+
const path = vscode.Uri.parse(p).fsPath;
185+
styleTags += `<style>${fs.readFileSync(path, "utf8")}</style>\n`;
197186
});
198187
}
199188

200-
let scriptSrc = "none";
201189
let scriptTags = "";
202-
203190
if (this.htmlContent.javaScriptPaths &&
204191
this.htmlContent.javaScriptPaths.length > 0) {
205-
scriptSrc = "";
206192
this.htmlContent.javaScriptPaths.forEach(
207193
(p) => {
208-
const nonce = getNonce();
209-
scriptSrc += `'nonce-${nonce}' `;
210-
scriptTags += `<script nonce="${nonce}" src="${p}"></script>\n`;
194+
const path = vscode.Uri.parse(p).fsPath;
195+
scriptTags += `<script>${fs.readFileSync(path, "utf8")}</script>\n`;
211196
});
212197
}
213198

214199
// Return an HTML page with the specified content
215-
return `<html><head><meta http-equiv="Content-Security-Policy" ` +
216-
`content="default-src 'none'; img-src *; style-src ${styleSrc}; script-src ${scriptSrc};">` +
217-
`${styleTags}</head><body>\n${this.htmlContent.bodyContent}\n${scriptTags}</body></html>`;
200+
return `<html><head>${styleTags}</head><body>\n${this.htmlContent.bodyContent}\n${scriptTags}</body></html>`;
201+
}
202+
203+
public showContent(viewColumn: vscode.ViewColumn): void {
204+
if (!this.webviewPanel) {
205+
this.webviewPanel = vscode.window.createWebviewPanel(
206+
this.id,
207+
this.title,
208+
viewColumn,
209+
{
210+
enableScripts: true,
211+
enableFindWidget: true,
212+
enableCommandUris: true,
213+
retainContextWhenHidden: true,
214+
});
215+
}
216+
this.webviewPanel.webview.html = this.getContent();
217+
this.webviewPanel.reveal(viewColumn);
218218
}
219219
}
220220

0 commit comments

Comments
 (0)