mirror of
https://github.com/antos-rde/antosdk-apps.git
synced 2024-12-26 12:18:21 +01:00
1 line
3.3 KiB
JavaScript
1 line
3.3 KiB
JavaScript
(function(){var i;i=class extends this.OS.application.BaseApplication{constructor(i){super("Preview",i)}main(){return this.currfile=void 0,this.args&&this.args.length>0&&(this.currfile=this.args[0].path.asFileHandle()),this.view=this.find("view"),this.status=this.find("status"),this.zoom=this.find("zoom"),this.btreset=this.find("btreset"),this.zoom.onvaluechange=i=>this.setViewScale(i.data),this.btreset.onbtclick=i=>(this.zoom.value=100,this.setViewScale(100)),this.img=void 0,this.bindKey("ALT-O",()=>this.actionFile(this.name+"-Open")),this.bindKey("CTRL-X",()=>this.actionFile(this.name+"-Close")),this.zoom.max=200,this.zoom.value=100,this.open(this.currfile)}open(i){if(i)return this.currfile!==i&&(this.currfile=i),i.onready().then(()=>(i.info.size=(i.info.size/1024).toFixed(2),this.renderFile())).catch(t=>this.error(__("File not found {0}",i.path),t))}renderFile(){var i;if(i=this.currfile.info.mime)return this.img=void 0,$(this.view).empty(),this.zoom.value=100,this.scheme.apptitle=this.currfile.info.name,i.match(/^[^\/]+\/.*pdf.*/g)?this.renderPDF():i.match(/image\/.*svg.*/g)?this.renderSVG():i.match(/image\/.*/g)?this.renderImage():this.notify(__("Mime type {0} is not supported",file.info.mime))}setStatus(i){return $(this.status).html(i)}setViewScale(i){var t,e,s,h,r,a;if(this.currfile){if(r=i/100,(h=this.currfile.info.mime).match(/image\/.*svg.*/g))return $($(this.view).children()[0]).css("width",Math.round(i)+"%").css("height",Math.round(i)+"%");if(h.match(/image\/.*/g)){if(!this.img)return;return e=(t=$(this.view).children()[0]).getContext("2d"),a=this.img.width*r,s=this.img.height*r,t.height=s,t.width=a,e.clearRect(0,0,t.width,t.height),e.scale(r,r),e.drawImage(this.img,0,0)}}}renderPDF(){var i;return $(this.find("statcontainer")).hide(),this.trigger("resize"),$(this.view).attr("class","pdf"),i=$("<iframe/>").css("width","100%").css("height","100%"),$(this.view).append(i[0]),i[0].src="pkg://libpdfjs/web/viewer.html".asFileHandle().getlink()+"?file="+this.currfile.getlink()}renderSVG(){return $(this.find("statcontainer")).show(),this.trigger("resize"),$(this.view).attr("class","image"),this.currfile.read().then(i=>(this.view.innerHTML=i,$($(this.view).children()[0]).css("width","100%").css("height","100%"))).catch(i=>this.error(__("Unable to read file: {0}",this.currfile.path),i))}renderImage(){return $(this.find("statcontainer")).show(),this.trigger("resize"),$(this.view).attr("class","image"),this.currfile.read("binary").then(i=>{var t,e,s;return s=new Image,e=$("<canvas/>")[0],$(this.view).append(e),s.onload=()=>{var i;return i=e.getContext("2d"),e.height=s.height,e.width=s.width,this.img=s,i.drawImage(s,0,0),this.setStatus(`${this.currfile.info.size} Kb - ${s.width}x${s.height}`)},t=new Blob([i],{type:this.currfile.info.mime}),s.src=URL.createObjectURL(t)}).catch(i=>this.error(__("Unable to read file: {0}",this.currfile.path),i))}menu(){return[{text:"__(File)",nodes:[{text:"__(Open)",dataid:this.name+"-Open",shortcut:"A-O"},{text:"__(Close)",dataid:this.name+"-Close",shortcut:"C-X"}],onchildselect:i=>this.actionFile(i.data.item.data.dataid)}]}actionFile(i){switch(i){case this.name+"-Open":return this.openDialog("FileDialog",{title:__("Open file"),mimes:this.meta().mimes}).then(i=>this.open(i.file.path.asFileHandle()));case this.name+"-Close":return this.quit()}}},this.OS.register("Preview",i)}).call(this); |