Skip to content

Commit c596b4f

Browse files
committed
Updated webview base code. Initial test view
1 parent 0c6b987 commit c596b4f

2 files changed

Lines changed: 62 additions & 23 deletions

File tree

src/webviews/codesWebview.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
/* eslint-disable max-len */
2+
/* eslint-disable @typescript-eslint/restrict-template-expressions */
13
/* ---------------------------------------------------------------------------------------------
24
* Copyright (c) Applied Eng & Design All rights reserved.
35
* Licensed under the MIT License. See License.md in the project root for license information.
46
* -------------------------------------------------------------------------------------------- */
57

68
'use strict';
79

8-
import { ConfigurationChangeEvent, Uri, Webview, workspace } from 'vscode';
10+
import { Uri, Webview } from 'vscode';
911
import { Control } from '../control';
1012
import { WebViewCommands } from './webviewCommands';
1113
import { GWebView } from './webviews';
@@ -20,18 +22,33 @@ export class CodesWebview extends GWebView {
2022
super(GCodesWebviewInfo.ViewId, GCodesWebviewInfo.Title, WebViewCommands.ShowCodesWebview);
2123
}
2224

23-
onConfigurationChanged(e: ConfigurationChangeEvent) {
24-
return;
25-
}
26-
27-
async getHtml(webview: Webview): Promise<string> {
28-
const csps = webview.cspSource;
29-
const jsonUri = Uri.joinPath(Control.context.extensionUri, 'resources', 'reference', 'milling', 'gcodes.json');
25+
getHtml(webview: Webview): Promise<string> {
26+
// CSS styles
27+
const stylesReset = webview.asWebviewUri(
28+
Uri.joinPath(Control.context.extensionUri, 'resources', 'webviews', 'css', 'reset.css'),
29+
);
3030

31-
return Promise.resolve(
32-
await workspace.fs.readFile(jsonUri).then(value => {
33-
return value.toString();
34-
}),
31+
const stylesMain = webview.asWebviewUri(
32+
Uri.joinPath(Control.context.extensionUri, 'resources', 'webviews', 'css', 'vscode.css'),
3533
);
34+
35+
const nonce = this.getNonce();
36+
37+
return Promise.resolve(`<!DOCTYPE html>
38+
<html lang="en">
39+
<head>
40+
<meta charset="UTF-8">
41+
42+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${webview.cspSource}; img-src ${webview.cspSource} https:; script-src 'nonce-${nonce}';">
43+
44+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
45+
46+
<link href="${stylesReset}" rel="stylesheet">
47+
<link href="${stylesMain}" rel="stylesheet">
48+
49+
<title>G/M Codes</title>
50+
</head>
51+
<body>Test</body>
52+
</html>`);
3653
}
3754
}

src/webviews/webviews.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ import {
1111
Uri,
1212
ViewColumn,
1313
Webview,
14+
WebviewOptions,
1415
WebviewPanel,
1516
WebviewPanelOnDidChangeViewStateEvent,
1617
window,
1718
} from 'vscode';
1819
import { configuration } from '../util/config';
1920
import { WebViewCommands } from './webviewCommands';
2021
import { constants } from '../util/constants';
21-
import * as path from 'path';
2222
import { Control } from '../control';
2323

2424
export 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

Comments
 (0)