|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { getConfig } from './config' |
| 3 | +import { nanoid } from './utils' |
| 4 | + |
| 5 | +export class SettingsWebview implements vscode.WebviewViewProvider, vscode.Disposable { |
| 6 | + private disposables: vscode.Disposable[] |
| 7 | + private view: vscode.WebviewView | undefined |
| 8 | + |
| 9 | + constructor( |
| 10 | + private extensionUri: vscode.Uri, |
| 11 | + ) { |
| 12 | + this.disposables = [ |
| 13 | + vscode.window.registerWebviewViewProvider('vitest.webviewSettings', this), |
| 14 | + ] |
| 15 | + } |
| 16 | + |
| 17 | + resolveWebviewView(webviewView: vscode.WebviewView, _context: vscode.WebviewViewResolveContext, _token: vscode.CancellationToken): Thenable<void> | void { |
| 18 | + this.view = webviewView |
| 19 | + |
| 20 | + webviewView.webview.options = { |
| 21 | + enableScripts: true, |
| 22 | + localResourceRoots: [this.extensionUri], |
| 23 | + } |
| 24 | + |
| 25 | + webviewView.webview.html = createHtmlView(webviewView.webview) |
| 26 | + |
| 27 | + this.disposables.push( |
| 28 | + // when we get the message from the view, process it |
| 29 | + webviewView.webview.onDidReceiveMessage((message) => { |
| 30 | + if (message.method === 'toggle') { |
| 31 | + const settings = vscode.workspace.getConfiguration('vitest') |
| 32 | + settings.update(message.args.setting, !settings.get(message.args.setting)) |
| 33 | + } |
| 34 | + }), |
| 35 | + // when the user changes the configuration manually, update the view |
| 36 | + vscode.workspace.onDidChangeConfiguration((e) => { |
| 37 | + if (e.affectsConfiguration('vitest')) { |
| 38 | + this.updateSettings() |
| 39 | + } |
| 40 | + }), |
| 41 | + // when the weview is opened, make sure it's up to date |
| 42 | + webviewView.onDidChangeVisibility(() => { |
| 43 | + if (!webviewView.visible) |
| 44 | + return |
| 45 | + this.updateSettings() |
| 46 | + }), |
| 47 | + ) |
| 48 | + |
| 49 | + this.updateSettings() |
| 50 | + } |
| 51 | + |
| 52 | + updateSettings() { |
| 53 | + this.view?.webview.postMessage({ |
| 54 | + method: 'settings', |
| 55 | + args: { |
| 56 | + settings: getConfig(), |
| 57 | + }, |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + dispose() { |
| 62 | + this.disposables.forEach(d => d.dispose()) |
| 63 | + this.disposables = [] |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// based on |
| 68 | +// https://github.com/microsoft/playwright-vscode/blob/4454e6876bfde1b4a8570dbaeca1ad14e8cd37c8/src/settingsView.ts |
| 69 | +function createHtmlView(webview: vscode.Webview) { |
| 70 | + // <link href="${styleUri}" rel="stylesheet"> |
| 71 | + const nonce = nanoid() |
| 72 | + return ` |
| 73 | +<!DOCTYPE html> |
| 74 | + <html lang="en"> |
| 75 | + <head> |
| 76 | + <meta charset="UTF-8"> |
| 77 | + <meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${webview.cspSource}; script-src 'nonce-${nonce}';"> |
| 78 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 79 | + <title>Vitest</title> |
| 80 | + </head> |
| 81 | + <body> |
| 82 | + <div class="list"> |
| 83 | + <div> |
| 84 | + <label title="${vscode.l10n.t('Show the browser when running tests in the Browser Mode. This will disable parallel execution.')}"> |
| 85 | + <input type="checkbox" data-setting="previewBrowser"></input> |
| 86 | + ${vscode.l10n.t('Show browser')} |
| 87 | + </label> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + <script nonce="${nonce}"> |
| 91 | + const vscode = acquireVsCodeApi(); |
| 92 | + for (const input of document.querySelectorAll('input[type=checkbox]')) { |
| 93 | + console.log('input', input) |
| 94 | + input.addEventListener('change', event => { |
| 95 | + vscode.postMessage({ method: 'toggle', args: { setting: event.target.dataset.setting } }); |
| 96 | + }); |
| 97 | + } |
| 98 | + window.addEventListener('message', event => { |
| 99 | + const { method, args } = event.data; |
| 100 | + if (method === 'settings') { |
| 101 | + for (const [key, value] of Object.entries(args.settings)) { |
| 102 | + const input = document.querySelector('input[data-setting=' + key + ']'); |
| 103 | + console.log('input', input, key, value) |
| 104 | + if (!input) |
| 105 | + continue; |
| 106 | + if (typeof value === 'boolean') |
| 107 | + input.checked = value; |
| 108 | + else |
| 109 | + input.value = value; |
| 110 | + } |
| 111 | + } |
| 112 | + }) |
| 113 | + </script> |
| 114 | + </body> |
| 115 | +</html> |
| 116 | + ` |
| 117 | +} |
0 commit comments