| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | import { Terminal } from "@xterm/xterm"; |
| 2 | import { FitAddon } from "@xterm/addon-fit"; |
| 3 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 4 | import { css, html, LitElement } from "lit"; |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 5 | import { customElement } from "lit/decorators.js"; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 6 | import "./sketch-container-status"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 7 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 8 | @customElement("sketch-terminal") |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 9 | export class SketchTerminal extends LitElement { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 10 | // Terminal instance |
| 11 | private terminal: Terminal | null = null; |
| 12 | // Terminal fit addon for handling resize |
| 13 | private fitAddon: FitAddon | null = null; |
| Philip Zeyliger | 37aaf08 | 2025-05-06 03:15:55 +0000 | [diff] [blame] | 14 | // Flag to track if terminal has been fully initialized |
| 15 | private isInitialized: boolean = false; |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 16 | // Terminal EventSource for SSE |
| 17 | private terminalEventSource: EventSource | null = null; |
| 18 | // Terminal ID (always 1 for now, will support 1-9 later) |
| 19 | private terminalId: string = "1"; |
| 20 | // Queue for serializing terminal inputs |
| 21 | private terminalInputQueue: string[] = []; |
| 22 | // Flag to track if we're currently processing a terminal input |
| 23 | private processingTerminalInput: boolean = false; |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 24 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 25 | static styles = css` |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 26 | /* Terminal View Styles */ |
| 27 | .terminal-view { |
| 28 | width: 100%; |
| 29 | background-color: #f5f5f5; |
| 30 | border-radius: 8px; |
| 31 | overflow: hidden; |
| 32 | margin-bottom: 20px; |
| 33 | box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); |
| 34 | padding: 15px; |
| 35 | height: 70vh; |
| 36 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 37 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 38 | .terminal-container { |
| 39 | width: 100%; |
| 40 | height: 100%; |
| 41 | overflow: hidden; |
| 42 | } |
| 43 | `; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 44 | |
| 45 | constructor() { |
| 46 | super(); |
| 47 | this._resizeHandler = this._resizeHandler.bind(this); |
| 48 | } |
| 49 | |
| 50 | connectedCallback() { |
| 51 | super.connectedCallback(); |
| 52 | this.loadXtermlCSS(); |
| 53 | // Setup resize handler |
| 54 | window.addEventListener("resize", this._resizeHandler); |
| Philip Zeyliger | 37aaf08 | 2025-05-06 03:15:55 +0000 | [diff] [blame] | 55 | // Listen for view mode changes to detect when terminal becomes visible |
| 56 | window.addEventListener( |
| 57 | "view-mode-select", |
| 58 | this._handleViewModeSelect.bind(this), |
| 59 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | disconnectedCallback() { |
| 63 | super.disconnectedCallback(); |
| 64 | |
| 65 | window.removeEventListener("resize", this._resizeHandler); |
| Philip Zeyliger | 37aaf08 | 2025-05-06 03:15:55 +0000 | [diff] [blame] | 66 | window.removeEventListener("view-mode-select", this._handleViewModeSelect); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 67 | |
| 68 | this.closeTerminalConnections(); |
| 69 | |
| 70 | if (this.terminal) { |
| 71 | this.terminal.dispose(); |
| 72 | this.terminal = null; |
| 73 | } |
| 74 | this.fitAddon = null; |
| 75 | } |
| 76 | |
| 77 | firstUpdated() { |
| Philip Zeyliger | 37aaf08 | 2025-05-06 03:15:55 +0000 | [diff] [blame] | 78 | // Do nothing - we'll initialize the terminal when it becomes visible |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | _resizeHandler() { |
| Philip Zeyliger | 37aaf08 | 2025-05-06 03:15:55 +0000 | [diff] [blame] | 82 | // Only handle resize if terminal has been initialized |
| 83 | if (this.fitAddon && this.isInitialized) { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 84 | this.fitAddon.fit(); |
| 85 | // Send resize information to server |
| 86 | this.sendTerminalResize(); |
| 87 | } |
| 88 | } |
| 89 | |
| Philip Zeyliger | 37aaf08 | 2025-05-06 03:15:55 +0000 | [diff] [blame] | 90 | /** |
| 91 | * Handle view mode selection event to detect when terminal becomes visible |
| 92 | */ |
| 93 | private _handleViewModeSelect(event: CustomEvent) { |
| 94 | const mode = event.detail.mode as "chat" | "diff" | "terminal"; |
| 95 | if (mode === "terminal") { |
| 96 | // Terminal tab is now visible |
| 97 | if (!this.isInitialized) { |
| 98 | // First time the terminal is shown - initialize it |
| 99 | this.isInitialized = true; |
| 100 | setTimeout(() => this.initializeTerminal(), 10); |
| 101 | } else if (this.fitAddon) { |
| 102 | // Terminal already initialized - just resize it |
| 103 | setTimeout(() => { |
| 104 | this.fitAddon?.fit(); |
| 105 | this.sendTerminalResize(); |
| 106 | this.terminal?.focus(); |
| 107 | }, 10); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 112 | // Load xterm CSS into the shadow DOM |
| 113 | private async loadXtermlCSS() { |
| 114 | try { |
| 115 | // Check if diff2html styles are already loaded |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 116 | const styleId = "xterm-styles"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 117 | if (this.shadowRoot?.getElementById(styleId)) { |
| 118 | return; // Already loaded |
| 119 | } |
| 120 | |
| 121 | // Fetch the diff2html CSS |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 122 | const response = await fetch("static/xterm.css"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 123 | |
| 124 | if (!response.ok) { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 125 | console.error( |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 126 | `Failed to load xterm CSS: ${response.status} ${response.statusText}`, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 127 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
| 131 | const cssText = await response.text(); |
| 132 | |
| 133 | // Create a style element and append to shadow DOM |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 134 | const style = document.createElement("style"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 135 | style.id = styleId; |
| 136 | style.textContent = cssText; |
| 137 | this.renderRoot?.appendChild(style); |
| 138 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 139 | console.log("xterm CSS loaded into shadow DOM"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 140 | } catch (error) { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 141 | console.error("Error loading xterm CSS:", error); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 142 | } |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Initialize the terminal component |
| 147 | * @param terminalContainer The DOM element to contain the terminal |
| 148 | */ |
| 149 | public async initializeTerminal(): Promise<void> { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 150 | const terminalContainer = this.renderRoot.querySelector( |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 151 | "#terminalContainer", |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 152 | ) as HTMLElement; |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 153 | |
| 154 | if (!terminalContainer) { |
| 155 | console.error("Terminal container not found"); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | // If terminal is already initialized, just focus it |
| 160 | if (this.terminal) { |
| 161 | this.terminal.focus(); |
| 162 | if (this.fitAddon) { |
| 163 | this.fitAddon.fit(); |
| 164 | } |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | // Clear the terminal container |
| 169 | terminalContainer.innerHTML = ""; |
| 170 | |
| 171 | // Create new terminal instance |
| 172 | this.terminal = new Terminal({ |
| 173 | cursorBlink: true, |
| 174 | theme: { |
| 175 | background: "#f5f5f5", |
| 176 | foreground: "#333333", |
| 177 | cursor: "#0078d7", |
| 178 | selectionBackground: "rgba(0, 120, 215, 0.4)", |
| 179 | }, |
| 180 | }); |
| 181 | |
| 182 | // Add fit addon to handle terminal resizing |
| 183 | this.fitAddon = new FitAddon(); |
| 184 | this.terminal.loadAddon(this.fitAddon); |
| 185 | |
| 186 | // Open the terminal in the container |
| 187 | this.terminal.open(terminalContainer); |
| 188 | |
| 189 | // Connect to WebSocket |
| 190 | await this.connectTerminal(); |
| 191 | |
| 192 | // Fit the terminal to the container |
| 193 | this.fitAddon.fit(); |
| 194 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 195 | // Focus the terminal |
| 196 | this.terminal.focus(); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Connect to terminal events stream |
| 201 | */ |
| 202 | private async connectTerminal(): Promise<void> { |
| 203 | if (!this.terminal) { |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | // Close existing connections if any |
| 208 | this.closeTerminalConnections(); |
| 209 | |
| 210 | try { |
| 211 | // Connect directly to the SSE endpoint for terminal 1 |
| 212 | // Use relative URL based on current location |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 213 | const baseUrl = window.location.pathname.endsWith("/") ? "." : "."; |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 214 | const eventsUrl = `${baseUrl}/terminal/events/${this.terminalId}`; |
| 215 | this.terminalEventSource = new EventSource(eventsUrl); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 216 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 217 | // Handle SSE events |
| 218 | this.terminalEventSource.onopen = () => { |
| 219 | console.log("Terminal SSE connection opened"); |
| 220 | this.sendTerminalResize(); |
| 221 | }; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 222 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 223 | this.terminalEventSource.onmessage = (event) => { |
| 224 | if (this.terminal) { |
| 225 | // Decode base64 data before writing to terminal |
| 226 | try { |
| Philip Zeyliger | 000c1f7 | 2025-04-22 11:57:37 -0700 | [diff] [blame] | 227 | // @ts-ignore This isn't in the type definitions yet; it's pretty new?!? |
| 228 | const decoded = base64ToUint8Array(event.data); |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 229 | this.terminal.write(decoded); |
| 230 | } catch (e) { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 231 | console.error("Error decoding terminal data:", e); |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | }; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 235 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 236 | this.terminalEventSource.onerror = (error) => { |
| 237 | console.error("Terminal SSE error:", error); |
| 238 | if (this.terminal) { |
| 239 | this.terminal.write("\r\n\x1b[1;31mConnection error\x1b[0m\r\n"); |
| 240 | } |
| 241 | // Attempt to reconnect if the connection was lost |
| 242 | if (this.terminalEventSource?.readyState === EventSource.CLOSED) { |
| 243 | this.closeTerminalConnections(); |
| 244 | } |
| 245 | }; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 246 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 247 | // Send key inputs to the server via POST requests |
| 248 | if (this.terminal) { |
| 249 | this.terminal.onData((data) => { |
| 250 | this.sendTerminalInput(data); |
| 251 | }); |
| 252 | } |
| 253 | } catch (error) { |
| 254 | console.error("Failed to connect to terminal:", error); |
| 255 | if (this.terminal) { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 256 | this.terminal.write( |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 257 | `\r\n\x1b[1;31mFailed to connect: ${error}\x1b[0m\r\n`, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 258 | ); |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Close any active terminal connections |
| 265 | */ |
| 266 | private closeTerminalConnections(): void { |
| 267 | if (this.terminalEventSource) { |
| 268 | this.terminalEventSource.close(); |
| 269 | this.terminalEventSource = null; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Send input to the terminal |
| 275 | * @param data The input data to send |
| 276 | */ |
| 277 | private async sendTerminalInput(data: string): Promise<void> { |
| 278 | // Add the data to the queue |
| 279 | this.terminalInputQueue.push(data); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 280 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 281 | // If we're not already processing inputs, start processing |
| 282 | if (!this.processingTerminalInput) { |
| 283 | await this.processTerminalInputQueue(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Process the terminal input queue in order |
| 289 | */ |
| 290 | private async processTerminalInputQueue(): Promise<void> { |
| 291 | if (this.terminalInputQueue.length === 0) { |
| 292 | this.processingTerminalInput = false; |
| 293 | return; |
| 294 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 295 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 296 | this.processingTerminalInput = true; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 297 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 298 | // Concatenate all available inputs from the queue into a single request |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 299 | let combinedData = ""; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 300 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 301 | // Take all currently available items from the queue |
| 302 | while (this.terminalInputQueue.length > 0) { |
| 303 | combinedData += this.terminalInputQueue.shift()!; |
| 304 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 305 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 306 | try { |
| 307 | // Use relative URL based on current location |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 308 | const baseUrl = window.location.pathname.endsWith("/") ? "." : "."; |
| 309 | const response = await fetch( |
| 310 | `${baseUrl}/terminal/input/${this.terminalId}`, |
| 311 | { |
| 312 | method: "POST", |
| 313 | body: combinedData, |
| 314 | headers: { |
| 315 | "Content-Type": "text/plain", |
| 316 | }, |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 317 | }, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 318 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 319 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 320 | if (!response.ok) { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 321 | console.error( |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 322 | `Failed to send terminal input: ${response.status} ${response.statusText}`, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 323 | ); |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 324 | } |
| 325 | } catch (error) { |
| 326 | console.error("Error sending terminal input:", error); |
| 327 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 328 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 329 | // Continue processing the queue (for any new items that may have been added) |
| 330 | await this.processTerminalInputQueue(); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Send terminal resize information to the server |
| 335 | */ |
| 336 | private async sendTerminalResize(): Promise<void> { |
| 337 | if (!this.terminal || !this.fitAddon) { |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | // Get terminal dimensions |
| 342 | try { |
| 343 | // Send resize message in a format the server can understand |
| 344 | // Use relative URL based on current location |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 345 | const baseUrl = window.location.pathname.endsWith("/") ? "." : "."; |
| 346 | const response = await fetch( |
| 347 | `${baseUrl}/terminal/input/${this.terminalId}`, |
| 348 | { |
| 349 | method: "POST", |
| 350 | body: JSON.stringify({ |
| 351 | type: "resize", |
| 352 | cols: this.terminal.cols || 80, // Default to 80 if undefined |
| 353 | rows: this.terminal.rows || 24, // Default to 24 if undefined |
| 354 | }), |
| 355 | headers: { |
| 356 | "Content-Type": "application/json", |
| 357 | }, |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 358 | }, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 359 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 360 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 361 | if (!response.ok) { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 362 | console.error( |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 363 | `Failed to send terminal resize: ${response.status} ${response.statusText}`, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 364 | ); |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 365 | } |
| 366 | } catch (error) { |
| 367 | console.error("Error sending terminal resize:", error); |
| 368 | } |
| 369 | } |
| 370 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 371 | render() { |
| 372 | return html` |
| 373 | <div id="terminalView" class="terminal-view"> |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 374 | <div id="terminalContainer" class="terminal-container"></div> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 375 | </div> |
| 376 | `; |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 379 | |
| Philip Zeyliger | 000c1f7 | 2025-04-22 11:57:37 -0700 | [diff] [blame] | 380 | function base64ToUint8Array(base64String) { |
| 381 | // This isn't yet available in Chrome, but Safari has it! |
| 382 | // @ts-ignore |
| 383 | if (Uint8Array.fromBase64) { |
| 384 | // @ts-ignore |
| 385 | return Uint8Array.fromBase64(base64String); |
| 386 | } |
| 387 | |
| 388 | const binaryString = atob(base64String); |
| 389 | return Uint8Array.from(binaryString, (char) => char.charCodeAt(0)); |
| 390 | } |
| 391 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 392 | declare global { |
| 393 | interface HTMLElementTagNameMap { |
| 394 | "sketch-terminal": SketchTerminal; |
| 395 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 396 | } |