forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulatorWebView.ts
More file actions
154 lines (139 loc) · 4.87 KB
/
Copy pathsimulatorWebView.ts
File metadata and controls
154 lines (139 loc) · 4.87 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as vscode from "vscode"
import { JDEventSource } from "jacdac-ts"
import { DeviceScriptExtensionState } from "./state"
import { logo } from "./assets"
class SimulatorsSerializer implements vscode.WebviewPanelSerializer {
constructor(readonly deserialize: (view: vscode.WebviewPanel) => void) {}
async deserializeWebviewPanel(
webviewPanel: vscode.WebviewPanel,
state: any
) {
this.deserialize(webviewPanel)
}
}
function getNonce() {
let text = ""
const possible =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
for (let i = 0; i < 32; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length))
}
return text
}
export class SimulatorsWebView extends JDEventSource {
// devtool web panel
private simulatorsWebviewPanel: vscode.WebviewPanel
private async generateSimulatorsHtml() {
const { kind: colorThemeKind } = vscode.window.activeColorTheme
const darkMode =
colorThemeKind === vscode.ColorThemeKind.Dark ||
colorThemeKind === vscode.ColorThemeKind.HighContrast
? "dark"
: "light"
const fullWebServerUri = await vscode.env.asExternalUri(
vscode.Uri.parse(`http://localhost:8081/`)
)
const cspSource = this.simulatorsWebviewPanel.webview.cspSource
const nonce = getNonce()
return `
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta
http-equiv="Content-Security-Policy"
content="default-src 'none'; frame-src ${fullWebServerUri} ${cspSource} https:; img-src ${fullWebServerUri} ${cspSource} https:; script-src ${fullWebServerUri} ${cspSource} 'nonce-${nonce}'; style-src ${fullWebServerUri} ${cspSource} 'nonce-${nonce}';"
/>
<style nonce="${nonce}">
body {
margin: 0;
padding: 0;
background-color: transparent;
}
iframe {
position: absolute;
left: 0; right: 0;
width: 100%; height: 100%;
border: none;
}
</style>
</head>
<body>
<iframe src="${fullWebServerUri}?${darkMode}=1" />
</body>
</html>
`
}
private async updateDeveloperToolsPanelUrl() {
const panel = this.simulatorsWebviewPanel
if (!panel) return
panel.webview.html = await this.generateSimulatorsHtml()
}
constructor(readonly extensionState: DeviceScriptExtensionState) {
super()
const { context } = this.extensionState
const { subscriptions } = context
// open ui command
subscriptions.push(
vscode.commands.registerCommand(
"extension.devicescript.openSimulators",
this.handleOpen,
this
)
)
// reloaded from previous run
subscriptions.push(
vscode.window.registerWebviewPanelSerializer(
"extension.devicescript.simulators",
new SimulatorsSerializer(async view =>
this.handleDeserialize(view)
)
)
)
// track color theme
vscode.window.onDidChangeActiveColorTheme(
this.updateDeveloperToolsPanelUrl,
this,
subscriptions
)
}
private async handleDeserialize(view: vscode.WebviewPanel) {
if (!this.simulatorsWebviewPanel) {
this.simulatorsWebviewPanel = view
await this.configureWebviewPanel()
}
}
private async handleOpen() {
if (this.simulatorsWebviewPanel) {
this.simulatorsWebviewPanel.reveal(vscode.ViewColumn.Nine)
// make sure the tools are running
await this.extensionState.devtools.start()
return
}
console.log("Opening Developer Tools...")
// http://localhost:8081/
this.simulatorsWebviewPanel = vscode.window.createWebviewPanel(
"extension.devicescript.simulators",
"DeviceScript Simulators",
vscode.ViewColumn.Nine,
{
enableScripts: true,
retainContextWhenHidden: true,
}
)
await this.configureWebviewPanel()
}
private async configureWebviewPanel() {
this.simulatorsWebviewPanel.iconPath = logo(this.extensionState.context)
this.simulatorsWebviewPanel.onDidDispose(
this.handleViewDispose,
this,
this.extensionState.context.subscriptions
)
await this.extensionState.devtools.start()
await this.updateDeveloperToolsPanelUrl()
}
private handleViewDispose() {
this.simulatorsWebviewPanel = undefined
}
}