Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/media-preview/src/audioPreview.ts
4772 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as vscode from 'vscode';
7
import { BinarySizeStatusBarEntry } from './binarySizeStatusBarEntry';
8
import { MediaPreview, reopenAsText } from './mediaPreview';
9
import { escapeAttribute } from './util/dom';
10
import { generateUuid } from './util/uuid';
11
12
class AudioPreviewProvider implements vscode.CustomReadonlyEditorProvider {
13
14
public static readonly viewType = 'vscode.audioPreview';
15
16
constructor(
17
private readonly extensionRoot: vscode.Uri,
18
private readonly binarySizeStatusBarEntry: BinarySizeStatusBarEntry,
19
) { }
20
21
public async openCustomDocument(uri: vscode.Uri) {
22
return { uri, dispose: () => { } };
23
}
24
25
public async resolveCustomEditor(document: vscode.CustomDocument, webviewEditor: vscode.WebviewPanel): Promise<void> {
26
new AudioPreview(this.extensionRoot, document.uri, webviewEditor, this.binarySizeStatusBarEntry);
27
}
28
}
29
30
31
class AudioPreview extends MediaPreview {
32
33
constructor(
34
private readonly extensionRoot: vscode.Uri,
35
resource: vscode.Uri,
36
webviewEditor: vscode.WebviewPanel,
37
binarySizeStatusBarEntry: BinarySizeStatusBarEntry,
38
) {
39
super(extensionRoot, resource, webviewEditor, binarySizeStatusBarEntry);
40
41
this._register(webviewEditor.webview.onDidReceiveMessage(message => {
42
switch (message.type) {
43
case 'reopen-as-text': {
44
reopenAsText(resource, webviewEditor.viewColumn);
45
break;
46
}
47
}
48
}));
49
50
this.updateBinarySize();
51
this.render();
52
this.updateState();
53
}
54
55
protected async getWebviewContents(): Promise<string> {
56
const version = Date.now().toString();
57
const settings = {
58
src: await this.getResourcePath(this._webviewEditor, this._resource, version),
59
};
60
61
const nonce = generateUuid();
62
63
const cspSource = this._webviewEditor.webview.cspSource;
64
return /* html */`<!DOCTYPE html>
65
<html lang="en">
66
<head>
67
<meta charset="UTF-8">
68
69
<!-- Disable pinch zooming -->
70
<meta name="viewport"
71
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
72
73
<title>Audio Preview</title>
74
75
<link rel="stylesheet" href="${escapeAttribute(this.extensionResource('media', 'audioPreview.css'))}" type="text/css" media="screen" nonce="${nonce}">
76
77
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src data: ${cspSource}; media-src ${cspSource}; script-src 'nonce-${nonce}'; style-src ${cspSource} 'nonce-${nonce}';">
78
<meta id="settings" data-settings="${escapeAttribute(JSON.stringify(settings))}">
79
</head>
80
<body class="container loading" data-vscode-context='{ "preventDefaultContextMenuItems": true }'>
81
<div class="loading-indicator"></div>
82
<div class="loading-error">
83
<p>${vscode.l10n.t("An error occurred while loading the audio file.")}</p>
84
<a href="#" class="open-file-link">${vscode.l10n.t("Open file using VS Code's standard text/binary editor?")}</a>
85
</div>
86
<script src="${escapeAttribute(this.extensionResource('media', 'audioPreview.js'))}" nonce="${nonce}"></script>
87
</body>
88
</html>`;
89
}
90
91
private async getResourcePath(webviewEditor: vscode.WebviewPanel, resource: vscode.Uri, version: string): Promise<string | null> {
92
if (resource.scheme === 'git') {
93
const stat = await vscode.workspace.fs.stat(resource);
94
if (stat.size === 0) {
95
// The file is stored on git lfs
96
return null;
97
}
98
}
99
100
// Avoid adding cache busting if there is already a query string
101
if (resource.query) {
102
return webviewEditor.webview.asWebviewUri(resource).toString();
103
}
104
return webviewEditor.webview.asWebviewUri(resource).with({ query: `version=${version}` }).toString();
105
}
106
107
private extensionResource(...parts: string[]) {
108
return this._webviewEditor.webview.asWebviewUri(vscode.Uri.joinPath(this.extensionRoot, ...parts));
109
}
110
}
111
112
export function registerAudioPreviewSupport(context: vscode.ExtensionContext, binarySizeStatusBarEntry: BinarySizeStatusBarEntry): vscode.Disposable {
113
const provider = new AudioPreviewProvider(context.extensionUri, binarySizeStatusBarEntry);
114
return vscode.window.registerCustomEditorProvider(AudioPreviewProvider.viewType, provider, {
115
supportsMultipleEditorsPerDocument: true,
116
webviewOptions: {
117
retainContextWhenHidden: true,
118
}
119
});
120
}
121
122