mirror of
https://github.com/antos-rde/antosdk-apps.git
synced 2024-11-17 02:38:23 +01:00
1 line
13 KiB
JavaScript
1 line
13 KiB
JavaScript
(function(){var t,e,i,r,n,a,s,o;(t=class extends this.OS.application.BaseApplication{constructor(t){super("Booklet",t)}main(){return this.tree=this.find("toc-ui"),this.currentToc=void 0,this.dirty=!1,this.emux=!1,this.on("treeselect",t=>{var e;return e=t.data.item.data,this.currentToc===e||void 0===e||0===e.treepath?this.reloadEditor():(e.treepath=e.path,this.load(e).then(()=>this.open(e)).catch(t=>(e.loaded=!0,this.open(e),this.error(__("Error when loading '{0}': {1}",e.text,t.toString()),t))))}),this.tree.ondragndrop=t=>this.dndhandle(t),this.initEditor(),this.resizeContent(),this.tree.contextmenuHandle=(t,e)=>{var i;if(i=this.contextMenu())return e.items=i,e.onmenuselect=t=>this[t.data.item.data.dataid](),e.show(t)},this.editor.codemirror.on("change",()=>{if(!this.emux&&this.currentToc)return this.currentToc.descFile.dirty=!0,this.dirty=!0})}newChapter(){var t;return this.currentToc&&"Book"===this.currentToc.type?(t=new i(this.book),this.displayToc(),t.treepath=t.path):this.error(__("No book selected"))}newSection(){var t;return this.currentToc&&"Chapter"===this.currentToc.type?(t=new s(this.currentToc),this.displayToc(),t.treepath=t.path):this.error(__("No chapter selected"))}newFile(){var t;return this.currentToc&&"Section"===this.currentToc.type?(t=new n(this.currentToc),this.displayToc(),t.treepath=t.path):this.error(__("No section selected"))}delete(){var t;return this.currentToc?(t=()=>(this.currentToc=void 0,this.displayToc(),this.reloadEditor()),this.currentToc.remove().then(()=>(this.notify(__("Entrie deleted")),t())).catch(e=>(this.error(e.toString(),e),t()))):this.error(__("No entrie select"))}goUp(){if(this.currentToc&&"Book"!==this.currentToc.type)return this.currentToc.parent.up(this.currentToc),this.displayToc()}goDown(){if(this.currentToc&&"Book"!==this.currentToc.type)return this.currentToc.parent.down(this.currentToc),this.displayToc()}load(t){return new Promise((e,i)=>t.loaded?e():t.descFile.meta().then(r=>t.descFile.read().then(i=>(t.descFile.cache=i,t.loaded=!0,t.descFile.dirty=!1,e())).catch((function(t){return i(__e(t))}))).catch((function(t){return i(__e(t))})))}dndhandle(t){var e,i;if(t&&t.data&&(e=t.data.from[0].data,i=t.data.to.data,e&&i&&"Book"!==e.type&&"Chapter"!==e.type&&e.parent!==i.parent&&e.parent!==i))return i.type===e.type&&(i=i.parent),i.type===e.parent.type?e.parent.removeChild(e).then(()=>(i.add(e),this.displayToc())):void 0}upload(){if(this.currentToc&&"File"!==this.currentToc.type)return this.currentToc.path.asFileHandle().upload().then(()=>this.notify(__("File uploaded"))).catch(t=>this.error(__("Unable to upload file {0}",t.toString()),t))}contextMenu(){if(this.currentToc)switch(this.currentToc.type){case"Book":return[{text:__("New chapter"),dataid:"newChapter"},{text:__("Delete book"),dataid:"delete"},{text:__("Upload media"),dataid:"upload"}];case"Chapter":return[{text:__("New section"),dataid:"newSection"},{text:__("Delete chapter"),dataid:"delete"},{text:__("Go up"),dataid:"goUp"},{text:__("Go down"),dataid:"goDown"},{text:__("Upload media"),dataid:"upload"}];case"Section":return[{text:__("New file"),dataid:"newFile"},{text:__("Delete section"),dataid:"delete"},{text:__("Go up"),dataid:"goUp"},{text:__("Go down"),dataid:"goDown"},{text:__("Upload media"),dataid:"upload"}];case"File":return[{text:__("Delete file"),dataid:"delete"},{text:__("Go up"),dataid:"goUp"},{text:__("Go down"),dataid:"goDown"}]}}shareFile(t,e){return this.openDialog("FileDialog",{title:__("Select a file"),mimes:t}).then(t=>t.file.path.asFileHandle().publish().then((function(t){return e(t.result)})).catch(t=>this.error(__("Cannot export file for embedding to text"),t))).catch(t=>this.error(t.toString(),t))}initEditor(){var t;return t=this.find("markarea"),this.container=this.find("mycontainer"),this.previewOn=!1,this.editormux=!1,this.editor=new EasyMDE({element:t,autoDownloadFontAwesome:!1,autofocus:!0,tabSize:4,indentWithTabs:!0,toolbar:["bold","italic","heading","|","quote","code","unordered-list","ordered-list","|","link","image","table","horizontal-rule",{name:"shared image",className:"fa fa-share-square",action:t=>this.shareFile(["image/.*"],t=>this.editor.codemirror.getDoc().replaceSelection(`![](${this._api.handler.shared}/${t})`))},{name:"local image",className:"fa fa-file-image-o",action:t=>{if(this.book)return this.openDialog("FileDialog",{title:__("Select image file"),mimes:["image/.*"],root:this.book.path}).then(t=>{var e;return e=t.file.path.replace(this.book.path,""),this.editor.codemirror.getDoc().replaceSelection(`[[@book:image:${e}]]`)}).catch(t=>this.error(t.toString(),t))}},{name:"Youtube",className:"fa fa-youtube",action:t=>{var e,i;return e=this.editor.codemirror.getDoc(),i=this.editor.codemirror.getSelection()||"",e.replaceSelection(`[[youtube:${i}]]`)}},{name:"3d object",className:"fa fa-cube",action:t=>{if(this.book)return this.openDialog("FileDialog",{title:__("Select 3d model"),mimes:["text/wavefront-obj","model/gltf-binary"],root:this.book.path}).then(t=>{var e;return e=t.file.path.replace(this.book.path,""),this.editor.codemirror.getDoc().replaceSelection(`[[@book:3dmodel:${e}]]`)}).catch(t=>this.error(t.toString(),t))}},"|",{name:__("Preview"),className:"fa fa-eye no-disable",action:t=>(EasyMDE.togglePreview(t),renderMathInElement(this.find("mycontainer")),this.renderLocalElement())}],previewRender:(t,e)=>{var i;return this.book&&(t=t.replace(/\[\[@book:image:([^\]]*)\]\]/g,(t,e)=>`![](${this._api.handle.get}/${this.book.path}/${e})`)),i=this.editor.markdown(t),e.innerHTML=i}}),this.on("resize",t=>this.resizeContent()),this.bindKey("ALT-N",()=>this.actionFile(this.name+"-New")),this.bindKey("ALT-O",()=>this.actionFile(this.name+"-Open")),this.bindKey("CTRL-S",()=>this.actionFile(this.name+"-Save"))}reloadEditor(){return void 0===this.currentToc?(this.editor.value(""),this.scheme.apptitle=this.name):(this.editor.value(this.currentToc.descFile.cache||""),this.scheme.apptitle="Booklet - "+this.currentToc.descFile.path)}saveContext(){if(this.currentToc)return this.currentToc.descFile.cache=this.editor.value()}resizeContent(){var t,e,i,r,n;return e=$(".EasyMDEContainer",this.container).children(),r=$(this.scheme).find(".afx-window-top")[0],n=e[0],i=e[3],t=$(this.scheme).height()-$(r).height()-$(n).height()-$(i).height()-40,$(e[1]).css("height",t+"px")}menu(){return[{text:"__(File)",nodes:[{text:"__(New booklet)",dataid:this.name+"-New",shortcut:"A-N"},{text:"__(Open a booklet)",dataid:this.name+"-Open",shortcut:"A-O"},{text:"__(Save a booklet)",dataid:this.name+"-Save",shortcut:"C-S"}],onchildselect:t=>this.actionFile(t.data.item.data.dataid)}]}actionFile(t){switch(t){case this.name+"-Open":return this.checkForDirty(()=>this.openDialog("FileDialog",{title:__("Open book"),mimes:["dir"]}).then(t=>(this.book=new e(t.file.path),this.book.read(t.file.path).then(()=>(this.book.treepath=this.book.path,this.tree.selectedItem=void 0,this.displayToc(),this.notify(__("Book loaded")))).catch(t=>this.error(__("Cannot load book: {0}",t.toString()),t)))).catch(t=>this.error(t.toString(),t)));case this.name+"-New":return this.openDialog("FileDialog",{title:__("New book at"),mimes:["dir"],file:{basename:__("BookName")}}).then(t=>this.newAt(`${t.file.path}/${t.name}`)).catch(t=>this.error(t.toString(),t));case this.name+"-Save":if(!this.book)return;return this.currentToc&&this.saveContext(),this.displayToc(),this.book.save().then(()=>(this.dirty=!1,this.notify(__("Book saved")))).catch(t=>this.error(__("Can't save the book : {0}",t.toString()),t))}}checkForDirty(t){return this.dirty?this.ask({title:__("Continue ?"),text:__("Book is unsaved, you want to continue ?")}).then(e=>{if(e)return t()}):t()}open(t){return this.emux=!0,this.saveContext(),this.currentToc=t,this.reloadEditor(),this.displayToc(),this.emux=!1}newAt(t){return this.tree.selectedItem=void 0,this.book=new e(t),this.book.treepath=this.book.path,this.currentToc=void 0,this.reloadEditor(),this.displayToc()}displayToc(){return this.book.toc(),this.tree.data=this.book,this.tree.expandAll()}cleanup(t){if(this.dirty)return t.preventDefault(),this.checkForDirty(()=>(this.dirty=!1,this.quit()))}renderLocalElement(){}}).dependencies=["pkg://SimpleMDE/main.js","pkg://SimpleMDE/main.css","pkg://Katex/main.js","pkg://Katex/main.css"],this.OS.register("Booklet",t),r=class{constructor(t){this.text=t,this.loaded=!0}save(){}remove(){}toc(){}updateName(){var t;return(t=new RegExp("^s*#+(.*)\n","g").exec(this.descFile.cache))&&2===t.length?(this.hasMeta&&this.metaFile&&(this.metaFile.dirty=!0),this.parent&&this.parent.hasMeta&&this.parent.metaFile&&(this.parent.metaFile.dirty=!0),this.text=t[1].trim()):this.text}remove(){return new Promise((t,e)=>{var i;return(i=this.path.asFileHandle()).meta().then(r=>i.remove().then(i=>this.parent?this.parent.removeChild(this).then((function(){return t()})).catch((function(t){return e(__e(t))})):t()).catch((function(t){return e(__e(t))}))).catch(i=>this.parent?this.parent.removeChild(this).then((function(){return t()})).catch((function(t){return e(__e(t))})):t())})}},a=class extends r{constructor(t,e,i){super("Untitle"),this.type=t,this.path=e,this.hasMeta=i,this.init()}init(){return this.cnt=0,this.nodes=[],this.hasMeta&&(this.metaFile=(this.path+"/meta.json").asFileHandle()),this.descFile=(this.path+"/INTRO.md").asFileHandle()}up(t){var e;if(t&&(e=this.nodes.indexOf(t))>0)return this.nodes.splice(e,1),this.nodes.splice(e-1,0,t)}down(t){var e;if(t&&(e=this.nodes.indexOf(t))>=0&&e<this.nodes.length-1)return this.nodes.splice(e,1),this.nodes.splice(e+1,0,t)}add(t){return t.parent=this,t.root=this.root,this.nodes.push(t),this.hasMeta&&this.metaFile&&(this.metaFile.dirty=!0),t.metaFile&&t.hasMeta&&(t.metaFile.dirty=!0),this.cnt=this.cnt+1}removeChild(t){return new Promise((e,i)=>(this.nodes.splice(this.nodes.indexOf(t),1),this.hasMeta&&this.metaFile?(this.metaFile.dirty=!0,this.updateMeta().then((function(){return e()})).catch((function(t){return i(__e(t))}))):e()))}read(t){return new Promise((e,i)=>(this.path=t,this.init(),this.loaded=!1,this.metaFile.meta().then(t=>this.metaFile.read("json").then(t=>{var i,r,n,a,s,h,c;for(this.text=t.name,s=[],r=n=0,a=(h=t.entries).length;n<a;r=++n)c=h[r],s[r]=c;return(i=r=>{var n,a;return 0===r.length?(this.cnt=t.cnt,e()):(n=r.splice(0,1)[0],(a=new o[n.type](this)).name=n.name,a.read(n.path.replace("book://",this.root)).then(()=>i(r)).catch(t=>i(r)))})(s)}).catch((function(t){return i(__e(t))}))).catch((function(t){return i(__e(t))}))))}size(){return this.nodes.length}mkdir(){return new Promise((t,e)=>{var i;return(i=this.path.asFileHandle()).meta().then(e=>t()).catch(r=>{var n;return n=i.basename,(i=i.parent().asFileHandle()).mk(n).then(e=>t()).catch((function(t){return e(__e(t))}))})})}mkdirs(){return new Promise((t,e)=>{var i,r,n,a,s,o;if(a=[],"Section"!==this.type)for(i=r=0,n=(s=this.nodes).length;r<n;i=++r)o=s[i],a[i]=o;return this.mkdir().then(()=>{var i;return(i=r=>0===r.length?t():r.splice(0,1)[0].mkdirs().then(()=>i(r)).catch((function(t){return e(__e(t))})))(a)}).catch((function(t){return e(__e(t))}))})}updateMeta(){return new Promise((t,e)=>{var i,r,n,a,s,o,h;if(!this.metaFile.dirty)return t();for(r=[],n=a=0,s=(o=this.nodes).length;a<s;n=++a)h=o[n],r[n]={name:h.name,path:h.path.replace(this.root,"book://"),type:h.type};return i={name:this.text,entries:r,cnt:this.cnt,meta:this.hasMeta},this.metaFile.cache=i,this.metaFile.write("object").then(e=>(this.metaFile.dirty=!1,t())).catch((function(t){return e(__e(t))}))})}update(){return new Promise((t,e)=>this.updateMeta().then(()=>this.descFile.dirty?this.descFile.write("text/plain").then(e=>(this.descFile.dirty=!1,t())).catch((function(t){return e(__e(t))})):t()).catch((function(t){return e(__e(t))})))}updateAll(){return new Promise((t,e)=>{var i,r,n,a,s,o;for(a=[],i=r=0,n=(s=this.nodes).length;r<n;i=++r)o=s[i],a[i]=o;return this.update().then(()=>{var i;return(i=r=>0===r.length?t():r.splice(0,1)[0].updateAll().then(()=>i(r)).catch((function(t){return e(__e(t))})))(a)}).catch((function(t){return e(__e(t))}))})}toc(){var t,e,i;for(this.updateName(),t=0,e=(i=this.nodes).length;t<e;t++)i[t].toc();return this}},o={Book:e=class extends a{constructor(t){super("Book",t,!0)}init(){return super.init(),this.root=this.path}save(){return new Promise((t,e)=>this.mkdirs().then(()=>this.updateAll().then(()=>t()).catch((function(t){return e(__e(t))}))).catch((function(t){return e(__e(t))})))}},Chapter:i=class extends a{constructor(t){super("Chapter",`${t.path}/c_${t.cnt}`,!0),t.add(this)}},Section:s=class extends a{constructor(t){super("Section",`${t.path}/s_${t.cnt}`,!0),t.add(this)}},File:n=class extends r{constructor(t){super("Untitle file"),this.section=t,this.hasMeta=!1,this.type="File",this.path=`${this.section.path}/f_${this.section.cnt}.md`,this.descFile=this.path.asFileHandle(),this.section.add(this)}updateAll(){return new Promise((t,e)=>this.descFile.dirty?this.descFile.write("text/plain").then(e=>(this.descFile.dirty=!1,t())).catch(t=>e(__e(t))):t())}read(t){return new Promise((e,i)=>(this.loaded=!1,this.treepath=t,this.path=t,this.descFile=this.path.asFileHandle(),e()))}toc(){return this.updateName(),this}}}}).call(this); |