mirror of
https://github.com/antos-rde/antosdk-apps.git
synced 2025-06-02 08:54:22 +02:00
1 line
7.3 KiB
JavaScript
1 line
7.3 KiB
JavaScript
var OS;!function(e){let t;!function(e){const t={opendoc:"pkg://LibreOffice/open.png".asFileHandle().getlink(),newdoc:"pkg://LibreOffice/new.png".asFileHandle().getlink()};class i extends e.BaseApplication{constructor(e){super("LibreOffice",e),this.access_token=void 0,this.curr_file=void 0,this.eid="id"+Math.random().toString(36).replace(".",""),this.iframe=void 0,this.mimes=this.meta().mimes.map(e=>e),this.current_mode=void 0,this.post_msg_handle=e=>{this.process_iframe_msg(e)}}main(){this.args&&this.args.length>0&&(this.curr_file=this.args[0].path.asFileHandle()),this.placeholder=this.find("editor-area"),this.placeholder.id=this.eid,this.find("btn-open-file").onbtclick=e=>{this.openFile()},this.find("btn-new-doc").onbtclick=e=>{this.create("word")},this.find("btn-new-cell").onbtclick=e=>{this.create("sheet")},this.find("btn-new-slide").onbtclick=e=>{this.create("slide")},$(window).on("message",this.post_msg_handle),this.setting.loo_url||(this.setting.loo_url="https://loo.iohub.dev/hosting/discovery"),this.discover().then(e=>{this.editor_meta=e,this.curr_file&&this.open()}).catch(e=>{this.error(__("Unable to discover LibreOffice service: {0}",e.toString()),e)})}menu(){const e=[{text:"__(New)",dataid:"new"},{text:"__(Open)",dataid:"open"}];return"edit"==this.current_mode&&(e.push({text:"__(Save)",dataid:"save"}),e.push({text:"__(Save As)",dataid:"saveas"})),[{text:"__(File)",nodes:e,onchildselect:e=>{switch(e.data.item.data.dataid){case"new":this.check_dirty().then(e=>this.new_document());break;case"open":this.check_dirty().then(e=>this.openFile());break;case"save":this.post_message("Action_Save",{DontTerminateEdit:!0,DontSaveIfUnmodified:!0,Notify:!0});break;case"saveas":this.check_dirty().then(e=>this.save_as())}}},{text:"__(Document server URI)",onmenuselect:async e=>{try{const e=await this.openDialog("PromptDialog",{title:__("Document server URI"),label:__("Please enter LOO discovery URI"),value:this.setting.loo_url});this.setting.loo_url=e;const t=await this.discover();this.editor_meta=t,this.curr_file&&this.open()}catch(e){this.error(__("Unable to set new document server URI: {0}",e.toString()),e)}}}]}update_title(){let e=this.curr_file.path;this.curr_file.dirty&&(e+=" "+__("(modified)")),this.scheme.apptitle=e}post_message(e,t){let i={MessageId:e,SendTime:Date.now()};t&&(i.Values=t),this.iframe.contentWindow.postMessage(JSON.stringify(i),"*")}process_iframe_msg(e){if(e.originalEvent.source!=this.iframe.contentWindow)return;let i=e.originalEvent,s=JSON.parse(i.data);switch(console.log("receive data",s),s.MessageId){case"Action_Load_Resp":s.Values.success||this.error(s.Values.errorMsg);break;case"App_LoadingStatus":"Document_Loaded"==s.Values.Status&&(this.post_message("Host_PostmessageReady"),this.trigger("document_file_loaded"),"edit"==this.current_mode&&(this.post_message("Insert_Button",{id:"lool_new_file",imgurl:t.newdoc,label:__("New file").__(),hint:__("Create new document").__(),insertBefore:"save"}),this.post_message("Insert_Button",{id:"lool_open_file",imgurl:t.opendoc,label:__("Open file").__(),hint:__("Open document").__(),insertBefore:"lool_new_file"}))),"Frame_Ready"==s.Values.Status&&$(this.iframe).css("visibility","visible");break;case"Doc_ModifiedStatus":this.curr_file.dirty=s.Values.Modified,this.update_title();break;case"Clicked_Button":switch(s.Values.Id){case"lool_open_file":this.check_dirty().then(e=>this.openFile());break;case"lool_new_file":this.check_dirty().then(e=>this.new_document())}break;case"UI_SaveAs":this.check_dirty().then(e=>this.save_as(s.Values.format));break;default:console.log(s)}}save_as(e){e||(e=this.curr_file.ext);const t=`${this.curr_file.parent().path}/${this.curr_file.basename.replace(/\..*$/,"."+e)}`;console.log("export file data as ",t),this.openDialog("FileDialog",{title:__("Save file as"),type:"dir",file:t.asFileHandle()}).then(async t=>{const i=`${t.file.path}/${t.name}`.asFileHandle();try{if(this.curr_file.ext==i.ext){const e=await this.exec({action:"duplicate",args:{src:this.curr_file.path,dest:i.path}});if(e.error)throw e.error;this.curr_file=i,this.open()}else this.post_message("Action_SaveAs",{Filename:`${this.enb64u(i.path)}.${e}`,Notify:!0})}catch(e){this.error(__("Unable to save file as {0}: {1}",i.path,e.toString()),e)}})}new_document(){this.openDialog("SelectionDialog",{title:__("Create new"),data:[{text:__("Document"),iconclass:"fa fa-file-word-o",type:"word"},{text:__("Spreadsheet"),iconclass:"fa fa-file-excel-o",type:"sheet"},{text:__("Presentation"),iconclass:"fa fa-file-powerpoint-o",type:"slide"}]}).then(e=>{this.create(e.type)})}discover(){return new Promise(async(e,t)=>{try{let t=await this.setting.loo_url.asFileHandle().read(),i=(new DOMParser).parseFromString(t,"text/xml").getElementsByTagName("app"),s={};if(i)for(let e of i){let t=e.getAttribute("name");t.match(/^[^\/]*\/[^\/]*$/g)&&(this.mimes.includes(t)||this.mimes.push(t));let i=e.getElementsByTagName("action");if(i)for(let e of i){let t=e.getAttribute("ext"),i=e.getAttribute("name"),a=e.getAttribute("urlsrc");t&&""!=t&&a&&(s[t]={url:a,mode:i})}}e(s)}catch(e){t(__e(e))}})}openFile(){this.openDialog("FileDialog",{title:__("Open file"),type:"file",mimes:this.mimes}).then(e=>{this.curr_file=e.file.path.asFileHandle(),this.open()})}create(e){let t=void 0;switch(e){case"word":t="docx";break;case"sheet":t="xlsx";break;case"slide":t="pptx";break;default:this.error(__("Unknown doc type {0}",e))}this.openDialog("FileDialog",{title:__("Save file as"),type:"dir",file:("home://Untitled."+t).asFileHandle()}).then(async e=>{try{let i=`${e.file.path}/${e.name}`.asFileHandle(),s=`${this.path()}/templates/model.${t}`.asFileHandle(),a=await s.read("binary"),r=new Blob([a],{type:s.info.mime});i.cache=r,await i.write(s.info.mime),i.cache=void 0,this.curr_file=i,this.open()}catch(e){this.error(__("Unable to create document {0}",e.toString()),e)}})}open(){this.scheme.apptitle=__("Libre Office Online"),this.curr_file&&this.exec({action:"token",args:{file:this.curr_file.path}}).then(e=>{if(e.error)return void this.error(e.error);this.access_token=e.result.sid;let t=this.editor_meta[this.curr_file.ext];if(!t||!t.url)return this.error(__("Unknown editor for extension {0}",this.curr_file.ext));this.current_mode=t.mode,$(this.placeholder).empty();let i=$("<iframe>",{src:`${t.url}?WOPISrc=${this.uapi()}`,frameborder:0});this.iframe=i[0],i.css("width","100%").css("height","100%").css("display","block").css("visibility","hidden").appendTo(this.placeholder),this.load(new Promise((e,t)=>{this.one("document_file_loaded",t=>e(!0))}))}).catch(e=>{this.error(e.toString(),e)})}exec(e){let t={path:this.meta().path+"/api/api.lua",parameters:e};return this.call(t)}uapi(){let e={path:this.meta().path+"/api/api.lua",parameters:{action:"file",args:{file:this.curr_file.path}}},t=this.enb64u(JSON.stringify(e));return`${this._api.REST}/system/apigateway/${t}/wopi/files/${this.curr_file.path.hash()}&${this.access_token}`}enb64u(e){return btoa(e).trimBy("=").replace(/\+/g,"-").replace(/\//g,"_")}check_dirty(){return new Promise((e,t)=>{this.curr_file&&this.curr_file.dirty?this.ask({title:"__(Document unsaved)",text:"__(Continue action without saving?)"}).then(t=>{t&&e(!0)}):e(!0)})}cleanup(e){if(this.curr_file&&this.curr_file.dirty)return e.preventDefault(),void this.check_dirty().then(e=>{this.curr_file.dirty=!1,this.quit(!0)});$(window).off("message",this.post_msg_handle)}}e.LibreOffice=i}(t=e.application||(e.application={}))}(OS||(OS={})); |