Path: blob/main/extensions/media-preview/src/mediaPreview.ts
4772 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as vscode from 'vscode';6import { Utils } from 'vscode-uri';7import { BinarySizeStatusBarEntry } from './binarySizeStatusBarEntry';8import { Disposable } from './util/dispose';910export async function reopenAsText(resource: vscode.Uri, viewColumn: vscode.ViewColumn | undefined): Promise<void> {11await vscode.commands.executeCommand('vscode.openWith', resource, 'default', viewColumn);12}1314export const enum PreviewState {15Disposed,16Visible,17Active,18}1920export abstract class MediaPreview extends Disposable {2122protected previewState = PreviewState.Visible;23private _binarySize: number | undefined;2425constructor(26extensionRoot: vscode.Uri,27protected readonly _resource: vscode.Uri,28protected readonly _webviewEditor: vscode.WebviewPanel,29private readonly _binarySizeStatusBarEntry: BinarySizeStatusBarEntry,30) {31super();3233_webviewEditor.webview.options = {34enableScripts: true,35enableForms: false,36localResourceRoots: [37Utils.dirname(_resource),38extensionRoot,39]40};4142this._register(_webviewEditor.onDidChangeViewState(() => {43this.updateState();44}));4546this._register(_webviewEditor.onDidDispose(() => {47this.previewState = PreviewState.Disposed;48this.dispose();49}));5051const watcher = this._register(vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(_resource, '*')));52this._register(watcher.onDidChange(e => {53if (e.toString() === this._resource.toString()) {54this.updateBinarySize();55this.render();56}57}));5859this._register(watcher.onDidDelete(e => {60if (e.toString() === this._resource.toString()) {61this._webviewEditor.dispose();62}63}));64}6566public override dispose() {67super.dispose();68this._binarySizeStatusBarEntry.hide(this);69}7071public get resource() {72return this._resource;73}7475protected updateBinarySize() {76vscode.workspace.fs.stat(this._resource).then(({ size }) => {77this._binarySize = size;78this.updateState();79});80}8182protected async render() {83if (this.previewState === PreviewState.Disposed) {84return;85}8687const content = await this.getWebviewContents();88if (this.previewState as PreviewState === PreviewState.Disposed) {89return;90}9192this._webviewEditor.webview.html = content;93}9495protected abstract getWebviewContents(): Promise<string>;9697protected updateState() {98if (this.previewState === PreviewState.Disposed) {99return;100}101102if (this._webviewEditor.active) {103this.previewState = PreviewState.Active;104this._binarySizeStatusBarEntry.show(this, this._binarySize);105} else {106this._binarySizeStatusBarEntry.hide(this);107this.previewState = PreviewState.Visible;108}109}110}111112113