| 1 | import * as vscode from "vscode"; |
| 2 | |
| 3 | import { getTheme } from "./util/getTheme"; |
| 4 | import { getExtensionVersion, getvsCodeUriScheme } from "./util/util"; |
| 5 | import { getExtensionUri, getNonce, getUniqueId } from "./util/vscode"; |
| 6 | import { VsCodeWebviewProtocol } from "./webviewProtocol"; |
| 7 | |
| 8 | import type { FileEdit } from "core"; |
| 9 | |
| 10 | export class ContinueGUIWebviewViewProvider |
| 11 | implements vscode.WebviewViewProvider |
| 12 | { |
| 13 | public static readonly viewType = "continue.continueGUIView"; |
| 14 | public webviewProtocol: VsCodeWebviewProtocol; |
| 15 | |
| 16 | public get isReady(): boolean { |
| 17 | return !!this.webview; |
| 18 | } |
| 19 | |
| 20 | resolveWebviewView( |
| 21 | webviewView: vscode.WebviewView, |
| 22 | _context: vscode.WebviewViewResolveContext, |
| 23 | _token: vscode.CancellationToken, |
| 24 | ): void | Thenable<void> { |
| 25 | this.webviewProtocol.webview = webviewView.webview; |
| 26 | this._webviewView = webviewView; |
| 27 | this._webview = webviewView.webview; |
| 28 | webviewView.webview.html = this.getSidebarContent( |
| 29 | this.extensionContext, |
| 30 | webviewView, |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | private _webview?: vscode.Webview; |
| 35 | private _webviewView?: vscode.WebviewView; |
| 36 | |
| 37 | get isVisible() { |
| 38 | return this._webviewView?.visible; |
| 39 | } |
| 40 | |
| 41 | get webview() { |
| 42 | return this._webview; |
| 43 | } |
| 44 | |
| 45 | public resetWebviewProtocolWebview(): void { |
| 46 | if (this._webview) { |
| 47 | this.webviewProtocol.webview = this._webview; |
| 48 | } else { |
| 49 | console.warn("no webview found during reset"); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | sendMainUserInput(input: string) { |
| 54 | this.webview?.postMessage({ |
| 55 | type: "userInput", |
| 56 | input, |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | constructor( |
| 61 | private readonly windowId: string, |
| 62 | private readonly extensionContext: vscode.ExtensionContext, |
| 63 | ) { |
| 64 | this.webviewProtocol = new VsCodeWebviewProtocol(); |
| 65 | } |
| 66 | |
| 67 | getSidebarContent( |
| 68 | context: vscode.ExtensionContext | undefined, |
| 69 | panel: vscode.WebviewPanel | vscode.WebviewView, |
| 70 | page: string | undefined = undefined, |
| 71 | edits: FileEdit[] | undefined = undefined, |
| 72 | isFullScreen = false, |
| 73 | ): string { |
| 74 | const extensionUri = getExtensionUri(); |
| 75 | let scriptUri: string; |
| 76 | let styleMainUri: string; |
| 77 | const vscMediaUrl: string = panel.webview |
| 78 | .asWebviewUri(vscode.Uri.joinPath(extensionUri, "gui")) |
| 79 | .toString(); |
| 80 | |
| 81 | const inDevelopmentMode = |
| 82 | context?.extensionMode === vscode.ExtensionMode.Development; |
| 83 | if (!inDevelopmentMode) { |
| 84 | scriptUri = panel.webview |
| 85 | .asWebviewUri(vscode.Uri.joinPath(extensionUri, "gui/assets/index.js")) |
| 86 | .toString(); |
| 87 | styleMainUri = panel.webview |
| 88 | .asWebviewUri(vscode.Uri.joinPath(extensionUri, "gui/assets/index.css")) |
| 89 | .toString(); |
| 90 | } else { |
| 91 | scriptUri = "http://localhost:5173/src/main.tsx"; |
| 92 | styleMainUri = "http://localhost:5173/src/index.css"; |
| 93 | } |
| 94 | |
| 95 | panel.webview.options = { |
| 96 | enableScripts: true, |
| 97 | localResourceRoots: [ |
| 98 | vscode.Uri.joinPath(extensionUri, "gui"), |
| 99 | vscode.Uri.joinPath(extensionUri, "assets"), |
| 100 | ], |
| 101 | enableCommandUris: true, |
| 102 | portMapping: [ |
| 103 | { |
| 104 | webviewPort: 65433, |
| 105 | extensionHostPort: 65433, |
| 106 | }, |
| 107 | ], |
| 108 | }; |
| 109 | |
| 110 | const nonce = getNonce(); |
| 111 | |
| 112 | const currentTheme = getTheme(); |
| 113 | vscode.workspace.onDidChangeConfiguration((e) => { |
| 114 | if ( |
| 115 | e.affectsConfiguration("workbench.colorTheme") || |
| 116 | e.affectsConfiguration("window.autoDetectColorScheme") || |
| 117 | e.affectsConfiguration("window.autoDetectHighContrast") || |
| 118 | e.affectsConfiguration("workbench.preferredDarkColorTheme") || |
| 119 | e.affectsConfiguration("workbench.preferredLightColorTheme") || |
| 120 | e.affectsConfiguration("workbench.preferredHighContrastColorTheme") || |
| 121 | e.affectsConfiguration("workbench.preferredHighContrastLightColorTheme") |
| 122 | ) { |
| 123 | // Send new theme to GUI to update embedded Monaco themes |
| 124 | this.webviewProtocol?.request("setTheme", { theme: getTheme() }); |
| 125 | } |
| 126 | }); |
| 127 | |
| 128 | this.webviewProtocol.webview = panel.webview; |
| 129 | |
| 130 | return `<!DOCTYPE html> |
| 131 | <html lang="en"> |
| 132 | <head> |
| 133 | <meta charset="UTF-8"> |
| 134 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 135 | <script>const vscode = acquireVsCodeApi();</script> |
| 136 | <link href="${styleMainUri}" rel="stylesheet"> |
| 137 | |
| 138 | <title>Continue</title> |
| 139 | </head> |
| 140 | <body> |
| 141 | <div id="root"></div> |
| 142 | |
| 143 | ${ |
| 144 | inDevelopmentMode |
| 145 | ? `<script type="module"> |
| 146 | import RefreshRuntime from "http://localhost:5173/@react-refresh" |
| 147 | RefreshRuntime.injectIntoGlobalHook(window) |
| 148 | window.$RefreshReg$ = () => {} |
| 149 | window.$RefreshSig$ = () => (type) => type |
| 150 | window.__vite_plugin_react_preamble_installed__ = true |
| 151 | </script>` |
| 152 | : "" |
| 153 | } |
| 154 | |
| 155 | <script type="module" nonce="${nonce}" src="${scriptUri}"></script> |
| 156 | |
| 157 | <script>localStorage.setItem("ide", '"vscode"')</script> |
| 158 | <script>localStorage.setItem("vsCodeUriScheme", '"${getvsCodeUriScheme()}"')</script> |
| 159 | <script>localStorage.setItem("extensionVersion", '"${getExtensionVersion()}"')</script> |
| 160 | <script>window.windowId = "${this.windowId}"</script> |
| 161 | <script>window.vscMachineId = "${getUniqueId()}"</script> |
| 162 | <script>window.vscMediaUrl = "${vscMediaUrl}"</script> |
| 163 | <script>window.ide = "vscode"</script> |
| 164 | <script>window.fullColorTheme = ${JSON.stringify(currentTheme)}</script> |
| 165 | <script>window.colorThemeName = "dark-plus"</script> |
| 166 | <script>window.workspacePaths = ${JSON.stringify( |
| 167 | vscode.workspace.workspaceFolders?.map((folder) => |
| 168 | folder.uri.toString(), |
| 169 | ) || [], |
| 170 | )}</script> |
| 171 | <script>window.isFullScreen = ${isFullScreen}</script> |
| 172 | |
| 173 | ${ |
| 174 | edits |
| 175 | ? `<script>window.edits = ${JSON.stringify(edits)}</script>` |
| 176 | : "" |
| 177 | } |
| 178 | ${page ? `<script>window.location.pathname = "${page}"</script>` : ""} |
| 179 | </body> |
| 180 | </html>`; |
| 181 | } |
| 182 | } |