-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathVscodeTutorial.ts
More file actions
235 lines (203 loc) · 6.17 KB
/
VscodeTutorial.ts
File metadata and controls
235 lines (203 loc) · 6.17 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import path from "node:path";
import type {
CancellationToken,
ExtensionContext,
WebviewView,
WebviewViewProvider,
WebviewViewResolveContext,
} from "vscode";
import { ExtensionMode, Uri } from "vscode";
import type {
FileSystem,
TutorialId,
TutorialState,
} from "@cursorless/lib-common";
import { getCursorlessRepoRoot } from "@cursorless/lib-node-common";
import type { Tutorial } from "@cursorless/lib-tutorial";
import type { SpyWebViewEvent, VscodeApi } from "@cursorless/lib-vscode-common";
import type { ScopeVisualizer } from "./ScopeVisualizerCommandApi";
import { SpyWebviewView } from "./SpyWebviewView";
const VSCODE_TUTORIAL_WEBVIEW_ID = "cursorless.tutorial";
export class VscodeTutorial implements WebviewViewProvider {
private view?: WebviewView | SpyWebviewView;
private localResourceRoot: Uri;
constructor(
private context: ExtensionContext,
private vscodeApi: VscodeApi,
private tutorial: Tutorial,
scopeVisualizer: ScopeVisualizer,
fileSystem: FileSystem,
) {
this.onState = this.onState.bind(this);
this.start = this.start.bind(this);
this.documentationOpened = this.documentationOpened.bind(this);
this.next = this.next.bind(this);
this.previous = this.previous.bind(this);
this.restart = this.restart.bind(this);
this.resume = this.resume.bind(this);
this.list = this.list.bind(this);
this.localResourceRoot =
context.extensionMode === ExtensionMode.Development
? Uri.file(
path.join(
getCursorlessRepoRoot(),
"packages",
"lib-vscode-tutorial-webview",
"out",
),
)
: Uri.joinPath(context.extensionUri, "media");
context.subscriptions.push(
vscodeApi.window.registerWebviewViewProvider(
VSCODE_TUTORIAL_WEBVIEW_ID,
this,
),
tutorial.onState(this.onState),
scopeVisualizer.onDidChangeScopeType((scopeType) => {
this.tutorial.scopeTypeVisualized(scopeType);
}),
);
if (context.extensionMode === ExtensionMode.Development) {
context.subscriptions.push(
fileSystem.watchDir(this.localResourceRoot.fsPath, () => {
if (this.view != null) {
this.view.webview.html = this.getHtmlForWebview();
}
}),
);
}
}
public resolveWebviewView(
webviewView: WebviewView,
_context: WebviewViewResolveContext,
_token: CancellationToken,
) {
if (this.view != null && this.view instanceof SpyWebviewView) {
this.view.view = webviewView;
} else {
this.view =
this.context.extensionMode === ExtensionMode.Test
? new SpyWebviewView(webviewView)
: webviewView;
}
const { webview } = this.view;
webview.options = {
enableScripts: true,
localResourceRoots: [this.localResourceRoot],
};
webview.html = this.getHtmlForWebview();
webview.onDidReceiveMessage((data) => {
switch (data.type) {
case "getInitialState":
void webview.postMessage(this.tutorial.state);
break;
case "start":
void this.start(data.tutorialId);
break;
case "list":
void this.list();
break;
case "previous":
void this.previous();
break;
case "next":
void this.next();
break;
}
});
}
public getEventLog(): SpyWebViewEvent[] {
if (this.view instanceof SpyWebviewView) {
return this.view.getEventLog();
}
return [];
}
public async start(id: TutorialId | number) {
await this.tutorial.start(id);
await this.revealTutorial();
}
async documentationOpened() {
this.tutorial.documentationOpened();
await this.revealTutorial();
}
async next() {
await this.tutorial.next();
await this.revealTutorial();
}
async previous() {
await this.tutorial.previous();
await this.revealTutorial();
}
async restart() {
await this.tutorial.restart();
await this.revealTutorial();
}
async resume() {
await this.tutorial.resume();
await this.revealTutorial();
}
async list() {
await this.tutorial.list();
await this.revealTutorial();
}
private onState(state: TutorialState) {
void this.view?.webview.postMessage(state);
}
private async revealTutorial() {
if (this.view != null) {
this.view.show(true);
} else {
await this.vscodeApi.commands.executeCommand("cursorless.tutorial.focus");
}
}
private getHtmlForWebview() {
const { webview } = this.view!;
// Get the local path to main script run in the webview, then convert it to a uri we can use in the webview.
const scriptUri = webview.asWebviewUri(
Uri.joinPath(
this.localResourceRoot,
this.context.extensionMode === ExtensionMode.Development
? "index.js"
: "tutorialWebview.js",
),
);
// Do the same for the stylesheet.
const styleMainUri = webview.asWebviewUri(
Uri.joinPath(
this.localResourceRoot,
this.context.extensionMode === ExtensionMode.Development
? "index.css"
: "tutorialWebview.css",
),
);
// 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 styles from our extension directory,
and only allow scripts that have a specific nonce.
-->
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${webview.cspSource}; script-src 'nonce-${nonce}';">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="${styleMainUri}" rel="stylesheet">
<title>Cursorless tutorial</title>
</head>
<body>
<div id="root"></div>
<script nonce="${nonce}" src="${scriptUri}"></script>
</body>
</html>`;
}
}
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;
}