A Visual Studio Code extension for Pterodactyl
1import * as vscode from 'vscode';
2
3export class AssistantPanelController {
4 constructor(private context: vscode.ExtensionContext) { }
5
6 private _panel: vscode.WebviewPanel | undefined;
7 private cachedHtml: Map<string, string> = new Map();
8
9 private getPanel(): vscode.WebviewPanel {
10 if (this._panel) { return this._panel; }
11
12 const panel = vscode.window.createWebviewPanel(
13 'pterodactylProofAssistant',
14 'Proof Assistant',
15 { viewColumn: vscode.ViewColumn.Beside, preserveFocus: true },
16 { enableScripts: true, retainContextWhenHidden: true }
17 );
18
19 panel.onDidDispose(() => {
20 this._panel = undefined;
21 });
22
23 this._panel = panel;
24 return panel;
25 }
26
27 public update(editor?: vscode.TextEditor) {
28 // Ignore non-text editors (e.g., clicking the panel itself)
29 if (!editor?.document) { return; }
30
31 const isPtero = editor.document.languageId === 'pterodactyl';
32 if (isPtero) {
33 const panel = this.getPanel();
34 const uri = editor.document.uri.toString();
35 panel.webview.html = this.cachedHtml.get(uri) ?? this.getLoadingHtml();
36 panel.reveal(vscode.ViewColumn.Beside, true);
37 } else {
38 this._panel?.dispose();
39 }
40 }
41
42 public updateHtml(uri: string, html: string) {
43 this.cachedHtml.set(uri, html);
44
45 if (this._panel) {
46 const activeUri = vscode.window.activeTextEditor?.document.uri.toString();
47 if (activeUri === uri) {
48 this._panel.webview.html = html;
49 }
50 }
51 }
52
53 private getLoadingHtml() {
54 return `<html>
55 <body>
56 <h2>Proof Assistant</h2>
57 <p>Loading...</p>
58 </body>
59 </html>`;
60 }
61}