-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSidebarProvider.ts
103 lines (92 loc) · 3.15 KB
/
SidebarProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import * as vscode from 'vscode';
import { getNonce } from './getNonce';
export class SidebarProvider implements vscode.WebviewViewProvider {
_view?: vscode.WebviewView;
_doc?: vscode.TextDocument;
private _dataValue: any = null;
constructor(private readonly _extensionUri: vscode.Uri) {}
public resolveWebviewView(webviewView: vscode.WebviewView) {
this._view = webviewView;
webviewView.webview.options = {
// Allow scripts in the webview
enableScripts: true,
localResourceRoots: [this._extensionUri],
};
webviewView.webview.html = this._getHtmlForWebview(webviewView.webview);
webviewView.onDidChangeVisibility(async (e) => {
await vscode.commands.executeCommand('svisualize.activate', rootVal);
});
let rootVal = '';
//extension listener for svelte messages
webviewView.webview.onDidReceiveMessage(async (data) => {
switch (data.type) {
case 'resize': {
if (!data.value) {
return;
}
await vscode.commands.executeCommand('svisualize.sendUri', rootVal);
vscode.commands.executeCommand('svisualize.sendFileNames', rootVal);
break;
}
case 'update': {
if (!data.value) {
return;
}
await vscode.commands.executeCommand('svisualize.sendUri', rootVal);
vscode.commands.executeCommand('svisualize.sendFileNames', rootVal);
break;
}
case 'selection': {
if (!data.value) {
return;
}
rootVal = data.value;
await vscode.commands.executeCommand('svisualize.sendUri', rootVal);
vscode.commands.executeCommand('svisualize.sendFileNames', rootVal);
break;
}
case 'uri': {
if (!data.value) {
return;
}
const vscodeUri = vscode.Uri.file(data.value);
vscode.workspace.openTextDocument(vscodeUri).then((document) => {
vscode.window.showTextDocument(document);
});
break;
}
}
});
}
public revive(panel: vscode.WebviewView) {
this._view = panel;
}
private _getHtmlForWebview(webview: vscode.Webview) {
const scriptUri = webview.asWebviewUri(
vscode.Uri.joinPath(this._extensionUri, 'dist', 'sidebar.js')
);
// Use a nonce to only allow a specific script to be run.
const nonce = getNonce();
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--
Use a content security policy to only allow loading images from https or from our extension directory,
and only allow scripts that have a specific nonce.
-->
<meta http-equiv="Content-Security-Policy" content="img-src https: data:; style-src 'unsafe-inline' ${webview.cspSource}; script-src 'nonce-${nonce}';">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script nonce = "${nonce}">
const tsvscode = acquireVsCodeApi();
</script>
</head>
<body>
<script nonce="${nonce}" src="${scriptUri}"></script>
</body>
</html>`;
}
public get dataValue(): any {
return this._dataValue;
}
}