Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/media-preview/media/audioPreview.js
3292 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
// @ts-check
6
"use strict";
7
8
(function () {
9
// @ts-ignore
10
const vscode = acquireVsCodeApi();
11
12
function getSettings() {
13
const element = document.getElementById('settings');
14
if (element) {
15
const data = element.getAttribute('data-settings');
16
if (data) {
17
return JSON.parse(data);
18
}
19
}
20
21
throw new Error(`Could not load settings`);
22
}
23
24
const settings = getSettings();
25
26
// State
27
let hasLoadedMedia = false;
28
29
// Elements
30
const container = document.createElement('div');
31
container.className = 'audio-container';
32
document.body.appendChild(container);
33
34
const audio = new Audio(settings.src === null ? undefined : settings.src);
35
audio.controls = true;
36
37
function onLoaded() {
38
if (hasLoadedMedia) {
39
return;
40
}
41
hasLoadedMedia = true;
42
43
document.body.classList.remove('loading');
44
document.body.classList.add('ready');
45
container.append(audio);
46
}
47
48
audio.addEventListener('error', e => {
49
if (hasLoadedMedia) {
50
return;
51
}
52
53
hasLoadedMedia = true;
54
document.body.classList.add('error');
55
document.body.classList.remove('loading');
56
});
57
58
if (settings.src === null) {
59
onLoaded();
60
} else {
61
audio.addEventListener('canplaythrough', () => {
62
onLoaded();
63
});
64
}
65
66
document.querySelector('.open-file-link')?.addEventListener('click', (e) => {
67
e.preventDefault();
68
vscode.postMessage({
69
type: 'reopen-as-text',
70
});
71
});
72
}());
73
74