resourceViewer.ts 5.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import 'vs/css!./resourceviewer';
import nls = require('vs/nls');
import mimes = require('vs/base/common/mime');
11
import URI from 'vs/base/common/uri';
E
Erich Gamma 已提交
12 13 14
import paths = require('vs/base/common/paths');
import {Builder, $} from 'vs/base/browser/builder';
import DOM = require('vs/base/browser/dom');
15
import {DomScrollableElement} from 'vs/base/browser/ui/scrollbar/scrollableElement';
E
Erich Gamma 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

// Known media mimes that we can handle
const mapExtToMediaMimes = {
	'.bmp': 'image/bmp',
	'.gif': 'image/gif',
	'.jpg': 'image/jpg',
	'.jpeg': 'image/jpg',
	'.jpe': 'image/jpg',
	'.png': 'image/png',
	'.tiff': 'image/tiff',
	'.tif': 'image/tiff',
	'.ico': 'image/x-icon',
	'.tga': 'image/x-tga',
	'.psd': 'image/vnd.adobe.photoshop',
	'.mid': 'audio/midi',
	'.midi': 'audio/midi',
	'.mp4a': 'audio/mp4',
	'.mpga': 'audio/mpeg',
	'.mp2': 'audio/mpeg',
	'.mp2a': 'audio/mpeg',
	'.mp3': 'audio/mpeg',
	'.m2a': 'audio/mpeg',
	'.m3a': 'audio/mpeg',
	'.oga': 'audio/ogg',
	'.ogg': 'audio/ogg',
	'.spx': 'audio/ogg',
	'.aac': 'audio/x-aac',
	'.wav': 'audio/x-wav',
	'.wma': 'audio/x-ms-wma',
	'.mp4': 'video/mp4',
	'.mp4v': 'video/mp4',
	'.mpg4': 'video/mp4',
	'.mpeg': 'video/mpeg',
	'.mpg': 'video/mpeg',
	'.mpe': 'video/mpeg',
	'.m1v': 'video/mpeg',
	'.m2v': 'video/mpeg',
	'.ogv': 'video/ogg',
	'.qt': 'video/quicktime',
	'.mov': 'video/quicktime',
	'.webm': 'video/webm',
	'.mkv': 'video/x-matroska',
	'.mk3d': 'video/x-matroska',
	'.mks': 'video/x-matroska',
	'.wmv': 'video/x-ms-wmv',
	'.flv': 'video/x-flv',
	'.avi': 'video/x-msvideo',
	'.movie': 'video/x-sgi-movie'
B
Benjamin Pasero 已提交
64
};
E
Erich Gamma 已提交
65 66 67 68 69 70 71

/**
 * Helper to actually render the given resource into the provided container. Will adjust scrollbar (if provided) automatically based on loading
 * progress of the binary resource.
 */
export class ResourceViewer {

72
	public static show(name: string, resource: URI, container: Builder, scrollbar: DomScrollableElement): void {
E
Erich Gamma 已提交
73 74

		// Ensure CSS class
75
		$(container).setClass('monaco-resource-viewer');
E
Erich Gamma 已提交
76 77 78

		// Lookup media mime if any
		let mime: string;
79
		const ext = paths.extname(resource.toString());
E
Erich Gamma 已提交
80
		if (ext) {
81
			mime = mapExtToMediaMimes[ext.toLowerCase()];
E
Erich Gamma 已提交
82 83 84 85 86 87 88 89 90 91
		}

		if (!mime) {
			mime = mimes.MIME_BINARY;
		}

		// Show Image inline
		if (mime.indexOf('image/') >= 0) {
			$(container)
				.empty()
92
				.addClass('image')
E
Erich Gamma 已提交
93
				.img({
94 95 96 97 98 99 100 101 102 103 104
					src: resource.toString() // disabled due to https://github.com/electron/electron/issues/6275  + '?' + Date.now() // We really want to avoid the browser from caching this resource, so we add a fake query param that is unique
				}).on(DOM.EventType.LOAD, (e, img) => {
					const imgElement = <HTMLImageElement>img.getHTMLElement();
					if (imgElement.naturalWidth > imgElement.width || imgElement.naturalHeight > imgElement.height) {
						$(container).addClass('oversized');

						img.on(DOM.EventType.CLICK, (e, img) => {
							$(container).toggleClass('full-size');

							scrollbar.scanDomNode();
						});
E
Erich Gamma 已提交
105
					}
106

107 108 109
					// Update title when we know the image bounds
					img.title(nls.localize('imgTitle', "{0} ({1}x{2})", paths.basename(resource.fsPath), imgElement.naturalWidth, imgElement.naturalHeight));

110
					scrollbar.scanDomNode();
E
Erich Gamma 已提交
111 112 113 114 115
				});
		}

		// Embed Object (only PDF for now)
		else if (false /* PDF is currently not supported in Electron it seems */ && mime.indexOf('pdf') >= 0) {
B
Benjamin Pasero 已提交
116
			$(container)
E
Erich Gamma 已提交
117 118 119
				.empty()
				.element('object')
				.attr({
120
					data: resource.toString(), // disabled due to https://github.com/electron/electron/issues/6275  + '?' + Date.now(), // We really want to avoid the browser from caching this resource, so we add a fake query param that is unique
E
Erich Gamma 已提交
121 122 123 124 125 126 127
					width: '100%',
					height: '100%',
					type: mime
				});
		}

		// Embed Audio (if supported in browser)
128
		else if (false /* disabled due to unknown impact on memory usage */ && mime.indexOf('audio/') >= 0) {
E
Erich Gamma 已提交
129 130 131 132
			$(container)
				.empty()
				.element('audio')
				.attr({
133
					src: resource.toString(), // disabled due to https://github.com/electron/electron/issues/6275  + '?' + Date.now(), // We really want to avoid the browser from caching this resource, so we add a fake query param that is unique
E
Erich Gamma 已提交
134 135 136
					text: nls.localize('missingAudioSupport', "Sorry but playback of audio files is not supported."),
					controls: 'controls'
				}).on(DOM.EventType.LOAD, () => {
137
					scrollbar.scanDomNode();
E
Erich Gamma 已提交
138 139 140 141
				});
		}

		// Embed Video (if supported in browser)
142
		else if (false /* disabled due to unknown impact on memory usage */ && mime.indexOf('video/') >= 0) {
B
Benjamin Pasero 已提交
143
			$(container)
E
Erich Gamma 已提交
144 145 146
				.empty()
				.element('video')
				.attr({
147
					src: resource.toString(), // disabled due to https://github.com/electron/electron/issues/6275 + '?' + Date.now(), // We really want to avoid the browser from caching this resource, so we add a fake query param that is unique
E
Erich Gamma 已提交
148 149 150
					text: nls.localize('missingVideoSupport', "Sorry but playback of video files is not supported."),
					controls: 'controls'
				}).on(DOM.EventType.LOAD, () => {
151
					scrollbar.scanDomNode();
E
Erich Gamma 已提交
152 153 154 155 156 157 158 159
				});
		}

		// Handle generic Binary Files
		else {
			$(container)
				.empty()
				.span({
160
					text: nls.localize('nativeBinaryError', "The file cannot be displayed in the editor because it is either binary, very large or uses an unsupported text encoding.")
E
Erich Gamma 已提交
161 162
				});

163
			scrollbar.scanDomNode();
E
Erich Gamma 已提交
164 165 166
		}
	}
}