forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMediaViewer.js
More file actions
1 lines (1 loc) · 5.35 KB
/
Copy pathMediaViewer.js
File metadata and controls
1 lines (1 loc) · 5.35 KB
1
define(function(require,exports,module){const DocumentManager=require("document/DocumentManager"),MediaViewTemplate=require("text!htmlContent/media-view.html"),ProjectManager=require("project/ProjectManager"),LanguageManager=require("language/LanguageManager"),MainViewFactory=require("view/MainViewFactory"),Strings=require("strings"),StringUtils=require("utils/StringUtils"),FileSystem=require("filesystem/FileSystem"),FileSystemError=require("filesystem/FileSystemError"),FileUtils=require("file/FileUtils"),_=require("thirdparty/lodash"),Mustache=require("thirdparty/mustache/mustache"),_viewers={},_MIME_TYPES={mp4:"video/mp4",m4v:"video/mp4",webm:"video/webm",mkv:"video/webm",ogv:"video/ogg",mov:"video/quicktime",mp3:"audio/mpeg",wav:"audio/wav",ogg:"audio/ogg",m4a:"audio/mp4",flac:"audio/flac",aac:"audio/aac",aif:"audio/aiff",aiff:"audio/aiff"};function _getMimeType(fullPath,isAudio){const extension=FileUtils.getFileExtension(fullPath).toLowerCase();return _MIME_TYPES[extension]||(isAudio?"audio/mpeg":"video/mp4")}function _mediaToDataURI(file,isAudio,cb){file.read({encoding:window.fs.BYTE_ARRAY_ENCODING},function(err,content){if(err)return void cb(err);const bytes=new Uint8Array(content),chunkSize=32768,chunks=[];for(let i=0;i<bytes.length;i+=32768)chunks.push(String.fromCharCode.apply(null,bytes.subarray(i,i+32768)));const dataURI="data:"+_getMimeType(file.fullPath,isAudio)+";base64,"+window.btoa(chunks.join(""));cb(null,dataURI)})}function _formatDuration(seconds){if(!isFinite(seconds))return"";const totalSeconds=Math.round(seconds),hrs=Math.floor(totalSeconds/3600),mins=Math.floor(totalSeconds%3600/60),secs=totalSeconds%60,paddedSecs=(secs<10?"0":"")+secs;if(hrs){const paddedMins=(mins<10?"0":"")+mins;return hrs+":"+paddedMins+":"+paddedSecs}return mins+":"+paddedSecs}function MediaView(file,$container){this.file=file,this._isAudio="audio"===LanguageManager.getLanguageForPath(file.fullPath).getId(),this.$el=$(Mustache.render(MediaViewTemplate,{isAudio:this._isAudio})),$container.append(this.$el),this._naturalWidth=0,this._naturalHeight=0,this.relPath=ProjectManager.getProjectRelativeOrDisplayPath(this.file.fullPath),this.$mediaPath=this.$el.find(".media-path"),this.$mediaPreview=this.$el.find(".media-preview"),this.$mediaData=this.$el.find(".media-data"),this.$mediaError=this.$el.find(".media-error"),this.$mediaPath.text(this.relPath).attr("title",this.relPath),this.$mediaPreview.on("loadedmetadata",_.bind(this._onMediaLoaded,this)),this.$mediaPreview.on("error",_.bind(this._onMediaError,this)),_viewers[file.fullPath]=this,DocumentManager.on("fileNameChange.MediaView",_.bind(this._onFilenameChange,this)),this._loadMedia()}function _createMediaView(file,pane){let view=pane.getViewForPath(file.fullPath);return view?pane.showView(view):(view=new MediaView(file,pane.$content),pane.addView(view,!0)),(new $.Deferred).resolve().promise()}function _handleFileSystemChange(event,entry,added,removed){if(!entry||entry.isDirectory)return;const viewer=_viewers[entry.fullPath];viewer&&viewer.refresh()}MediaView.prototype._loadMedia=function(){const self=this;_mediaToDataURI(this.file,this._isAudio,function(err,dataURI){err?self._showError(err===FileSystemError.EXCEEDS_MAX_FILE_SIZE?StringUtils.format(Strings.MEDIA_VIEWER_FILE_TOO_LARGE,"16 MB"):Strings.MEDIA_VIEWER_FORMAT_NOT_SUPPORTED):(self.$mediaError.hide(),self.$mediaPreview.show(),self.$mediaPreview[0].src=dataURI)})},MediaView.prototype._showError=function(message){this.$mediaPreview.hide(),this.$mediaError.text(message).show()},MediaView.prototype._onFilenameChange=function(e,oldPath,newPath){this.file.fullPath===newPath&&(this.relPath=ProjectManager.getProjectRelativeOrDisplayPath(newPath),this.$mediaPath.text(this.relPath).attr("title",this.relPath))},MediaView.prototype._onMediaLoaded=function(e){const parts=[];this._isAudio||(this._naturalWidth=e.currentTarget.videoWidth,this._naturalHeight=e.currentTarget.videoHeight,parts.push(this._naturalWidth+" × "+this._naturalHeight+" "+Strings.UNIT_PIXELS));const duration=_formatDuration(e.currentTarget.duration);duration&&parts.push(duration);const self=this;this.file.stat(function(err,stat){!err&&stat.size&&parts.push(StringUtils.prettyPrintBytes(stat.size,2));const mediaInfo=parts.join(" — ");self.$mediaData.html(mediaInfo).attr("title",mediaInfo.replace(/×/g,"x").replace(/—/g,"-"))})},MediaView.prototype._onMediaError=function(){this._showError(Strings.MEDIA_VIEWER_FORMAT_NOT_SUPPORTED)},MediaView.prototype.getFile=function(){return this.file},MediaView.prototype.updateLayout=function(){const $container=this.$el.parent(),pos=$container.position(),iWidth=$container.innerWidth(),iHeight=$container.innerHeight(),oWidth=$container.outerWidth(),oHeight=$container.outerHeight();this.$el.css({top:pos.top+(oHeight-iHeight)/2,left:pos.left+(oWidth-iWidth)/2,width:iWidth,height:iHeight})},MediaView.prototype.destroy=function(){delete _viewers[this.file.fullPath],DocumentManager.off(".MediaView"),this.$mediaPreview.off("loadedmetadata error"),this.$mediaPreview.removeAttr("src"),this.$el.remove()},MediaView.prototype.refresh=function(){this._loadMedia()},FileSystem.on("change",_handleFileSystemChange),MainViewFactory.registerViewFactory({canOpenFile:function(fullPath){const langId=LanguageManager.getLanguageForPath(fullPath).getId();return"video"===langId||"audio"===langId},openFile:function(file,pane){return _createMediaView(file,pane)}}),exports.MediaView=MediaView});