@@ -11,20 +11,21 @@ import {
1111 Uri ,
1212 ViewColumn ,
1313 Webview ,
14+ WebviewOptions ,
1415 WebviewPanel ,
1516 WebviewPanelOnDidChangeViewStateEvent ,
1617 window ,
1718} from 'vscode' ;
1819import { configuration } from '../util/config' ;
1920import { WebViewCommands } from './webviewCommands' ;
2021import { constants } from '../util/constants' ;
21- import * as path from 'path' ;
2222import { Control } from '../control' ;
2323
2424export abstract class GWebView implements Disposable {
2525 protected _disposable : Disposable ;
2626 private _panel : WebviewPanel | undefined ;
2727 private _dPanel : Disposable | undefined ;
28+ private _enabled : boolean ;
2829
2930 constructor (
3031 public readonly id : string ,
@@ -36,6 +37,14 @@ export abstract class GWebView implements Disposable {
3637 configuration . onDidChange ( this . onConfigurationChanged , this ) ,
3738 commands . registerCommand ( showCommand , this . onShowCommand , this ) ,
3839 ) ;
40+
41+ this . _enabled = < boolean > configuration . getParam ( 'webviews.enabled' ) ;
42+ }
43+
44+ private onConfigurationChanged ( e : ConfigurationChangeEvent ) {
45+ if ( e . affectsConfiguration ( 'webviews.enabled' ) ) {
46+ this . _enabled = < boolean > configuration . getParam ( 'webviews.enabled' ) ;
47+ }
3948 }
4049
4150 dispose ( ) {
@@ -60,7 +69,9 @@ export abstract class GWebView implements Disposable {
6069 }
6170
6271 protected onShowCommand ( ) {
63- void this . show ( this . _column ) ;
72+ if ( this . _enabled ) {
73+ void this . show ( this . _column ) ;
74+ }
6475 }
6576
6677 private onPanelDisposed ( ) {
@@ -80,13 +91,7 @@ export abstract class GWebView implements Disposable {
8091 this . id ,
8192 this . title ,
8293 { viewColumn : column , preserveFocus : false } ,
83- {
84- retainContextWhenHidden : false ,
85- enableFindWidget : true ,
86- enableCommandUris : true ,
87- enableScripts : true ,
88- localResourceRoots : [ Uri . file ( path . join ( Control . context . extensionPath , 'resources' ) ) ] ,
89- } ,
94+ this . getWebviewOptions ( ) ,
9095 ) ;
9196
9297 this . _panel . iconPath = Uri . file ( constants . gcodeIcon ) ;
@@ -108,7 +113,24 @@ export abstract class GWebView implements Disposable {
108113 }
109114 }
110115
111- protected abstract onConfigurationChanged ( e : ConfigurationChangeEvent ) : void ;
112-
113116 protected abstract getHtml ( webview : Webview ) : Promise < string > ;
117+
118+ protected getNonce ( ) : string {
119+ let text = '' ;
120+
121+ const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
122+
123+ for ( let i = 0 ; i < 32 ; i ++ ) {
124+ text += possible . charAt ( Math . floor ( Math . random ( ) * possible . length ) ) ;
125+ }
126+ return text ;
127+ }
128+
129+ private getWebviewOptions ( ) : WebviewOptions {
130+ return {
131+ enableScripts : true ,
132+ enableCommandUris : true ,
133+ localResourceRoots : [ Uri . joinPath ( Control . context . extensionUri , 'resources' , 'webviews' ) ] ,
134+ } ;
135+ }
114136}
0 commit comments