Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/media-preview/src/mediaPreview.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 { Utils } from 'vscode-uri';
8
import { BinarySizeStatusBarEntry } from './binarySizeStatusBarEntry';
9
import { Disposable } from './util/dispose';
10
11
export async function reopenAsText(resource: vscode.Uri, viewColumn: vscode.ViewColumn | undefined): Promise<void> {
12
await vscode.commands.executeCommand('vscode.openWith', resource, 'default', viewColumn);
13
}
14
15
export const enum PreviewState {
16
Disposed,
17
Visible,
18
Active,
19
}
20
21
export abstract class MediaPreview extends Disposable {
22
23
protected previewState = PreviewState.Visible;
24
private _binarySize: number | undefined;
25
26
constructor(
27
extensionRoot: vscode.Uri,
28
protected readonly _resource: vscode.Uri,
29
protected readonly _webviewEditor: vscode.WebviewPanel,
30
private readonly _binarySizeStatusBarEntry: BinarySizeStatusBarEntry,
31
) {
32
super();
33
34
_webviewEditor.webview.options = {
35
enableScripts: true,
36
enableForms: false,
37
localResourceRoots: [
38
Utils.dirname(_resource),
39
extensionRoot,
40
]
41
};
42
43
this._register(_webviewEditor.onDidChangeViewState(() => {
44
this.updateState();
45
}));
46
47
this._register(_webviewEditor.onDidDispose(() => {
48
this.previewState = PreviewState.Disposed;
49
this.dispose();
50
}));
51
52
const watcher = this._register(vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(_resource, '*')));
53
this._register(watcher.onDidChange(e => {
54
if (e.toString() === this._resource.toString()) {
55
this.updateBinarySize();
56
this.render();
57
}
58
}));
59
60
this._register(watcher.onDidDelete(e => {
61
if (e.toString() === this._resource.toString()) {
62
this._webviewEditor.dispose();
63
}
64
}));
65
}
66
67
public override dispose() {
68
super.dispose();
69
this._binarySizeStatusBarEntry.hide(this);
70
}
71
72
public get resource() {
73
return this._resource;
74
}
75
76
protected updateBinarySize() {
77
vscode.workspace.fs.stat(this._resource).then(({ size }) => {
78
this._binarySize = size;
79
this.updateState();
80
});
81
}
82
83
protected async render() {
84
if (this.previewState === PreviewState.Disposed) {
85
return;
86
}
87
88
const content = await this.getWebviewContents();
89
if (this.previewState as PreviewState === PreviewState.Disposed) {
90
return;
91
}
92
93
this._webviewEditor.webview.html = content;
94
}
95
96
protected abstract getWebviewContents(): Promise<string>;
97
98
protected updateState() {
99
if (this.previewState === PreviewState.Disposed) {
100
return;
101
}
102
103
if (this._webviewEditor.active) {
104
this.previewState = PreviewState.Active;
105
this._binarySizeStatusBarEntry.show(this, this._binarySize);
106
} else {
107
this._binarySizeStatusBarEntry.hide(this);
108
this.previewState = PreviewState.Visible;
109
}
110
}
111
}
112
113