|
| 1 | +import * as vscode from 'vscode'; |
| 2 | + |
| 3 | +let panel: vscode.WebviewPanel | undefined; |
| 4 | + |
| 5 | +function isHttpGitUrl(value: string): boolean { |
| 6 | + try { |
| 7 | + const url = new URL(value.trim()); |
| 8 | + return (url.protocol === 'http:' || url.protocol === 'https:') && !!url.hostname; |
| 9 | + } catch { |
| 10 | + return false; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +function escapeHtml(value: string): string { |
| 15 | + return value.replace(/[&<>"']/g, (character) => { |
| 16 | + const entities: Record<string, string> = { |
| 17 | + '&': '&', |
| 18 | + '<': '<', |
| 19 | + '>': '>', |
| 20 | + '"': '"', |
| 21 | + "'": ''', |
| 22 | + }; |
| 23 | + return entities[character] ?? character; |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +function welcomeHtml(webview: vscode.Webview): string { |
| 28 | + const nonce = `${Date.now()}${Math.random().toString(36).slice(2)}`; |
| 29 | + return `<!doctype html> |
| 30 | +<html lang="en"> |
| 31 | +<head> |
| 32 | + <meta charset="UTF-8"> |
| 33 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 34 | + <meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${webview.cspSource}; script-src 'nonce-${nonce}';"> |
| 35 | + <style> |
| 36 | + :root { color-scheme: light dark; } |
| 37 | + body { max-width: 680px; margin: 0 auto; padding: 12vh 32px; color: var(--vscode-foreground); font-family: var(--vscode-font-family); } |
| 38 | + h1 { font-size: 30px; font-weight: 600; margin: 0 0 8px; } |
| 39 | + p { color: var(--vscode-descriptionForeground); margin: 0 0 28px; } |
| 40 | + label { display: block; font-weight: 600; margin-bottom: 8px; } |
| 41 | + .row { display: flex; gap: 8px; } |
| 42 | + input { flex: 1; min-width: 0; padding: 9px 10px; color: var(--vscode-input-foreground); background: var(--vscode-input-background); border: 1px solid var(--vscode-input-border, transparent); border-radius: 3px; font: inherit; } |
| 43 | + button { padding: 9px 16px; color: var(--vscode-button-foreground); background: var(--vscode-button-background); border: 0; border-radius: 3px; font: inherit; cursor: pointer; } |
| 44 | + button:hover { background: var(--vscode-button-hoverBackground); } |
| 45 | + #error { min-height: 20px; margin-top: 10px; color: var(--vscode-errorForeground); } |
| 46 | + </style> |
| 47 | +</head> |
| 48 | +<body> |
| 49 | + <h1>Welcome to ZCode</h1> |
| 50 | + <p>Paste an HTTP or HTTPS Git repository URL to start working in your browser.</p> |
| 51 | + <form id="form"> |
| 52 | + <label for="url">Git repository URL</label> |
| 53 | + <div class="row"> |
| 54 | + <input id="url" type="url" autocomplete="url" placeholder="https://github.com/org/repo.git" autofocus> |
| 55 | + <button type="submit">Open Repository</button> |
| 56 | + </div> |
| 57 | + <div id="error" role="alert"></div> |
| 58 | + </form> |
| 59 | + <script nonce="${nonce}"> |
| 60 | + const vscode = acquireVsCodeApi(); |
| 61 | + const form = document.getElementById('form'); |
| 62 | + const input = document.getElementById('url'); |
| 63 | + const error = document.getElementById('error'); |
| 64 | + form.addEventListener('submit', (event) => { |
| 65 | + event.preventDefault(); |
| 66 | + const value = input.value.trim(); |
| 67 | + try { |
| 68 | + const url = new URL(value); |
| 69 | + if (!['http:', 'https:'].includes(url.protocol) || !url.hostname) throw new Error(); |
| 70 | + error.textContent = ''; |
| 71 | + vscode.postMessage({ type: 'openRepository', url: value }); |
| 72 | + } catch { |
| 73 | + error.textContent = 'Enter a valid HTTP(S) Git repository URL.'; |
| 74 | + input.focus(); |
| 75 | + } |
| 76 | + }); |
| 77 | + </script> |
| 78 | +</body> |
| 79 | +</html>`; |
| 80 | +} |
| 81 | + |
| 82 | +function openWelcome(context: vscode.ExtensionContext): void { |
| 83 | + if (panel) { |
| 84 | + panel.reveal(vscode.ViewColumn.One); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + panel = vscode.window.createWebviewPanel( |
| 89 | + 'zcode.welcome', |
| 90 | + 'Welcome to ZCode', |
| 91 | + vscode.ViewColumn.One, |
| 92 | + { enableScripts: true, retainContextWhenHidden: true }, |
| 93 | + ); |
| 94 | + panel.webview.html = welcomeHtml(panel.webview); |
| 95 | + panel.onDidDispose( |
| 96 | + () => { |
| 97 | + panel = undefined; |
| 98 | + }, |
| 99 | + null, |
| 100 | + context.subscriptions, |
| 101 | + ); |
| 102 | + panel.webview.onDidReceiveMessage( |
| 103 | + async (message: { type?: string; url?: string }) => { |
| 104 | + if (message.type !== 'openRepository' || typeof message.url !== 'string') return; |
| 105 | + const url = message.url.trim(); |
| 106 | + if (!isHttpGitUrl(url)) { |
| 107 | + void vscode.window.showErrorMessage('Enter a valid HTTP(S) Git repository URL.'); |
| 108 | + return; |
| 109 | + } |
| 110 | + await vscode.commands.executeCommand('zcode.git.openRepository', url); |
| 111 | + }, |
| 112 | + null, |
| 113 | + context.subscriptions, |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +export function activate(context: vscode.ExtensionContext): void { |
| 118 | + context.subscriptions.push( |
| 119 | + vscode.commands.registerCommand('zcode.welcome.open', () => openWelcome(context)), |
| 120 | + ); |
| 121 | + openWelcome(context); |
| 122 | +} |
| 123 | + |
| 124 | +export function deactivate(): void { |
| 125 | + panel = undefined; |
| 126 | +} |
0 commit comments