182 lines
5.8 KiB
TypeScript
182 lines
5.8 KiB
TypeScript
import * as vscode from "vscode";
|
|
|
|
import { getTheme } from "./util/getTheme";
|
|
import { getExtensionVersion, getvsCodeUriScheme } from "./util/util";
|
|
import { getExtensionUri, getNonce, getUniqueId } from "./util/vscode";
|
|
import { VsCodeWebviewProtocol } from "./webviewProtocol";
|
|
|
|
import type { FileEdit } from "core";
|
|
|
|
export class ContinueGUIWebviewViewProvider
|
|
implements vscode.WebviewViewProvider
|
|
{
|
|
public static readonly viewType = "continue.continueGUIView";
|
|
public webviewProtocol: VsCodeWebviewProtocol;
|
|
|
|
public get isReady(): boolean {
|
|
return !!this.webview;
|
|
}
|
|
|
|
resolveWebviewView(
|
|
webviewView: vscode.WebviewView,
|
|
_context: vscode.WebviewViewResolveContext,
|
|
_token: vscode.CancellationToken,
|
|
): void | Thenable<void> {
|
|
this.webviewProtocol.webview = webviewView.webview;
|
|
this._webviewView = webviewView;
|
|
this._webview = webviewView.webview;
|
|
webviewView.webview.html = this.getSidebarContent(
|
|
this.extensionContext,
|
|
webviewView,
|
|
);
|
|
}
|
|
|
|
private _webview?: vscode.Webview;
|
|
private _webviewView?: vscode.WebviewView;
|
|
|
|
get isVisible() {
|
|
return this._webviewView?.visible;
|
|
}
|
|
|
|
get webview() {
|
|
return this._webview;
|
|
}
|
|
|
|
public resetWebviewProtocolWebview(): void {
|
|
if (this._webview) {
|
|
this.webviewProtocol.webview = this._webview;
|
|
} else {
|
|
console.warn("no webview found during reset");
|
|
}
|
|
}
|
|
|
|
sendMainUserInput(input: string) {
|
|
this.webview?.postMessage({
|
|
type: "userInput",
|
|
input,
|
|
});
|
|
}
|
|
|
|
constructor(
|
|
private readonly windowId: string,
|
|
private readonly extensionContext: vscode.ExtensionContext,
|
|
) {
|
|
this.webviewProtocol = new VsCodeWebviewProtocol();
|
|
}
|
|
|
|
getSidebarContent(
|
|
context: vscode.ExtensionContext | undefined,
|
|
panel: vscode.WebviewPanel | vscode.WebviewView,
|
|
page: string | undefined = undefined,
|
|
edits: FileEdit[] | undefined = undefined,
|
|
isFullScreen = false,
|
|
): string {
|
|
const extensionUri = getExtensionUri();
|
|
let scriptUri: string;
|
|
let styleMainUri: string;
|
|
const vscMediaUrl: string = panel.webview
|
|
.asWebviewUri(vscode.Uri.joinPath(extensionUri, "gui"))
|
|
.toString();
|
|
|
|
const inDevelopmentMode =
|
|
context?.extensionMode === vscode.ExtensionMode.Development;
|
|
if (!inDevelopmentMode) {
|
|
scriptUri = panel.webview
|
|
.asWebviewUri(vscode.Uri.joinPath(extensionUri, "gui/assets/index.js"))
|
|
.toString();
|
|
styleMainUri = panel.webview
|
|
.asWebviewUri(vscode.Uri.joinPath(extensionUri, "gui/assets/index.css"))
|
|
.toString();
|
|
} else {
|
|
scriptUri = "http://localhost:5173/src/main.tsx";
|
|
styleMainUri = "http://localhost:5173/src/index.css";
|
|
}
|
|
|
|
panel.webview.options = {
|
|
enableScripts: true,
|
|
localResourceRoots: [
|
|
vscode.Uri.joinPath(extensionUri, "gui"),
|
|
vscode.Uri.joinPath(extensionUri, "assets"),
|
|
],
|
|
enableCommandUris: true,
|
|
portMapping: [
|
|
{
|
|
webviewPort: 65433,
|
|
extensionHostPort: 65433,
|
|
},
|
|
],
|
|
};
|
|
|
|
const nonce = getNonce();
|
|
|
|
const currentTheme = getTheme();
|
|
vscode.workspace.onDidChangeConfiguration((e) => {
|
|
if (
|
|
e.affectsConfiguration("workbench.colorTheme") ||
|
|
e.affectsConfiguration("window.autoDetectColorScheme") ||
|
|
e.affectsConfiguration("window.autoDetectHighContrast") ||
|
|
e.affectsConfiguration("workbench.preferredDarkColorTheme") ||
|
|
e.affectsConfiguration("workbench.preferredLightColorTheme") ||
|
|
e.affectsConfiguration("workbench.preferredHighContrastColorTheme") ||
|
|
e.affectsConfiguration("workbench.preferredHighContrastLightColorTheme")
|
|
) {
|
|
// Send new theme to GUI to update embedded Monaco themes
|
|
this.webviewProtocol?.request("setTheme", { theme: getTheme() });
|
|
}
|
|
});
|
|
|
|
this.webviewProtocol.webview = panel.webview;
|
|
|
|
return `<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<script>const vscode = acquireVsCodeApi();</script>
|
|
<link href="${styleMainUri}" rel="stylesheet">
|
|
|
|
<title>Continue</title>
|
|
</head>
|
|
<body>
|
|
<div id="root"></div>
|
|
|
|
${
|
|
inDevelopmentMode
|
|
? `<script type="module">
|
|
import RefreshRuntime from "http://localhost:5173/@react-refresh"
|
|
RefreshRuntime.injectIntoGlobalHook(window)
|
|
window.$RefreshReg$ = () => {}
|
|
window.$RefreshSig$ = () => (type) => type
|
|
window.__vite_plugin_react_preamble_installed__ = true
|
|
</script>`
|
|
: ""
|
|
}
|
|
|
|
<script type="module" nonce="${nonce}" src="${scriptUri}"></script>
|
|
|
|
<script>localStorage.setItem("ide", '"vscode"')</script>
|
|
<script>localStorage.setItem("vsCodeUriScheme", '"${getvsCodeUriScheme()}"')</script>
|
|
<script>localStorage.setItem("extensionVersion", '"${getExtensionVersion()}"')</script>
|
|
<script>window.windowId = "${this.windowId}"</script>
|
|
<script>window.vscMachineId = "${getUniqueId()}"</script>
|
|
<script>window.vscMediaUrl = "${vscMediaUrl}"</script>
|
|
<script>window.ide = "vscode"</script>
|
|
<script>window.fullColorTheme = ${JSON.stringify(currentTheme)}</script>
|
|
<script>window.colorThemeName = "dark-plus"</script>
|
|
<script>window.workspacePaths = ${JSON.stringify(
|
|
vscode.workspace.workspaceFolders?.map((folder) =>
|
|
folder.uri.toString(),
|
|
) || [],
|
|
)}</script>
|
|
<script>window.isFullScreen = ${isFullScreen}</script>
|
|
|
|
${
|
|
edits
|
|
? `<script>window.edits = ${JSON.stringify(edits)}</script>`
|
|
: ""
|
|
}
|
|
${page ? `<script>window.location.pathname = "${page}"</script>` : ""}
|
|
</body>
|
|
</html>`;
|
|
}
|
|
}
|