2
2
* Copyright (C) Microsoft Corporation. All rights reserved.
3
3
*--------------------------------------------------------*/
4
4
5
+ import fs = require( "fs" ) ;
5
6
import vscode = require( "vscode" ) ;
6
7
import { LanguageClient , NotificationType , RequestType } from "vscode-languageclient" ;
7
8
import { IFeature } from "../feature" ;
@@ -94,13 +95,7 @@ class PowerShellContentProvider implements vscode.TextDocumentContentProvider {
94
95
95
96
public showView ( id : string , viewColumn : vscode . ViewColumn ) {
96
97
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 ) ;
104
99
}
105
100
106
101
public closeView ( id : string ) {
@@ -164,6 +159,8 @@ class HtmlContentView extends CustomView {
164
159
styleSheetPaths : [ ] ,
165
160
} ;
166
161
162
+ private webviewPanel : vscode . WebviewPanel ;
163
+
167
164
constructor (
168
165
id : string ,
169
166
title : string ) {
@@ -179,42 +176,45 @@ class HtmlContentView extends CustomView {
179
176
}
180
177
181
178
public getContent ( ) : string {
182
- let styleSrc = "none" ;
183
179
let styleTags = "" ;
184
-
185
- function getNonce ( ) : number {
186
- return Math . floor ( Math . random ( ) * 100000 ) + 100000 ;
187
- }
188
-
189
180
if ( this . htmlContent . styleSheetPaths &&
190
181
this . htmlContent . styleSheetPaths . length > 0 ) {
191
- styleSrc = "" ;
192
182
this . htmlContent . styleSheetPaths . forEach (
193
183
( 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` ;
197
186
} ) ;
198
187
}
199
188
200
- let scriptSrc = "none" ;
201
189
let scriptTags = "" ;
202
-
203
190
if ( this . htmlContent . javaScriptPaths &&
204
191
this . htmlContent . javaScriptPaths . length > 0 ) {
205
- scriptSrc = "" ;
206
192
this . htmlContent . javaScriptPaths . forEach (
207
193
( 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` ;
211
196
} ) ;
212
197
}
213
198
214
199
// 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 ) ;
218
218
}
219
219
}
220
220
0 commit comments