/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the Source EULA. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as vscode from 'vscode'; import { BinarySizeStatusBarEntry } from './binarySizeStatusBarEntry'; import { MediaPreview, reopenAsText } from './mediaPreview'; import { escapeAttribute, getNonce } from './util/dom'; class AudioPreviewProvider implements vscode.CustomReadonlyEditorProvider { public static readonly viewType = 'vscode.audioPreview'; constructor( private readonly extensionRoot: vscode.Uri, private readonly binarySizeStatusBarEntry: BinarySizeStatusBarEntry, ) { } public async openCustomDocument(uri: vscode.Uri) { return { uri, dispose: () => { } }; } public async resolveCustomEditor(document: vscode.CustomDocument, webviewEditor: vscode.WebviewPanel): Promise { new AudioPreview(this.extensionRoot, document.uri, webviewEditor, this.binarySizeStatusBarEntry); } } class AudioPreview extends MediaPreview { constructor( private readonly extensionRoot: vscode.Uri, resource: vscode.Uri, webviewEditor: vscode.WebviewPanel, binarySizeStatusBarEntry: BinarySizeStatusBarEntry, ) { super(extensionRoot, resource, webviewEditor, binarySizeStatusBarEntry); this._register(webviewEditor.webview.onDidReceiveMessage(message => { switch (message.type) { case 'reopen-as-text': { reopenAsText(resource, webviewEditor.viewColumn); break; } } })); this.updateBinarySize(); this.render(); this.updateState(); } protected async getWebviewContents(): Promise { const version = Date.now().toString(); const settings = { src: await this.getResourcePath(this.webviewEditor, this.resource, version), }; const nonce = getNonce(); const cspSource = this.webviewEditor.webview.cspSource; return /* html */` Audio Preview

${vscode.l10n.t("An error occurred while loading the audio file.")}

${vscode.l10n.t("Open file using VS Code's standard text/binary editor?")}
`; } private async getResourcePath(webviewEditor: vscode.WebviewPanel, resource: vscode.Uri, version: string): Promise { if (resource.scheme === 'git') { const stat = await vscode.workspace.fs.stat(resource); if (stat.size === 0) { // The file is stored on git lfs return null; } } // Avoid adding cache busting if there is already a query string if (resource.query) { return webviewEditor.webview.asWebviewUri(resource).toString(); } return webviewEditor.webview.asWebviewUri(resource).with({ query: `version=${version}` }).toString(); } private extensionResource(...parts: string[]) { return this.webviewEditor.webview.asWebviewUri(vscode.Uri.joinPath(this.extensionRoot, ...parts)); } } export function registerAudioPreviewSupport(context: vscode.ExtensionContext, binarySizeStatusBarEntry: BinarySizeStatusBarEntry): vscode.Disposable { const provider = new AudioPreviewProvider(context.extensionUri, binarySizeStatusBarEntry); return vscode.window.registerCustomEditorProvider(AudioPreviewProvider.viewType, provider, { supportsMultipleEditorsPerDocument: true, webviewOptions: { retainContextWhenHidden: true, } }); }