28 lines
46 KiB
JavaScript
28 lines
46 KiB
JavaScript
|
|
"use strict";var Nt=Object.defineProperty;var Pt=(e,n,t)=>n in e?Nt(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t;var P=(e,n,t)=>Pt(e,typeof n!="symbol"?n+"":n,t);const I=require("siyuan"),ce={debug:(...e)=>{}},ze=(e,n,t,i=!1)=>{var a;if(!e)return!1;e.nodeType===3&&(e=e.parentElement);let r=e,s=!1;for(;r&&!s&&(i?r.tagName!=="BODY":!r.classList.contains("protyle-wysiwyg"));)typeof t=="string"&&((a=r.getAttribute(n))!=null&&a.split(" ").includes(t))||typeof t!="string"&&r.hasAttribute(n)?s=!0:r=r.parentElement;return s&&r},Ot=(e,n,t=!1)=>{var s;if(!e)return!1;e.nodeType===3&&(e=e.parentElement);let i=e,r=!1;for(;i&&!r&&(t?i.tagName!=="BODY":!i.classList.contains("protyle-wysiwyg"));)(s=i.classList)!=null&&s.contains(n)?r=!0:i=i.parentElement;return r&&i},ve=(e,n)=>e.classList.contains("fn__none")||!e.classList.contains(n),Mt=(e,n,t="b3-list-item--focus",i)=>{var a;let r=e.querySelector("."+t);if(!r&&i){i.classList.add(t),i.scrollIntoView(!0);return}if(!r)return;const s=t.split("--")[0];if(n.key==="ArrowDown"){for(n.preventDefault(),n.stopPropagation(),r.classList.remove(t),r=r.nextElementSibling;r&&ve(r,s);)r=r.nextElementSibling;if(!r)for(r=e.children[0];r&&ve(r,s);)r=r.nextElementSibling;return r?(r.classList.add(t),(e.scrollTop<r.offsetTop-e.clientHeight+r.clientHeight||e.scrollTop>r.offsetTop)&&r.scrollIntoView(e.scrollTop>r.offsetTop),r):void 0}else if(n.key==="ArrowUp"){for(n.preventDefault(),n.stopPropagation(),r.classList.remove(t),r=r.previousElementSibling;r&&ve(r,s);)r=r.previousElementSibling;if(!r)for(r=e.children[e.children.length-1];r&&(r.classList.contains("fn__none")||!r.classList.contains(s));)r=r.previousElementSibling;if(!r)return;r.classList.add(t);const o=e.scrollTop>r.offsetTop-(((a=r.previousElementSibling)==null?void 0:a.clientHeight)||0);return(e.scrollTop<r.offsetTop-e.clientHeight+r.clientHeight||o)&&r.scrollIntoView(o),r}else if(n.key==="Home"){for(n.preventDefault(),n.stopPropagation(),r.classList.remove(t),r=e.children[0];r&&ve(r,s);)r=r.nextElementSibling;return r?(r.classList.add(t),r.scrollIntoView(),r):void 0}else if(n.key==="End"){for(n.preventDefault(),n.stopPropagation(),r.classList.remove(t),r=e.children[e.children.length-1];r&&ve(r,s);)r=r.previousElementSibling;return r?(r.classList.add(t),r.scrollIntoView(!1),r):void 0}},xt=["\\","/",":","*","?",'"',"<",">","|","$","&","^","."];function $e(e){return xt.some(n=>e.indexOf(n)!==-1)}function Ve(e){for(var n=document.querySelectorAll("iframe"),t=0;t<n.length;t++)if(n[t].contentWindow===e)return n[t];return null}const jt='<mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/></root></mxGraphModel>',Le="assets/drawio",De="storage/petal/siyuan-drawio-plugin",Bt="text/xml",Ie="plugin-config.json",Y=class Y{};P(Y,"SIYUAN_ASSETS_IMAGE",[".apng",".ico",".cur",".jpg",".jpe",".jpeg",".jfif",".pjp",".pjpeg",".png",".gif",".webp",".bmp",".svg",".avif"]),P(Y,"SIYUAN_ASSETS_AUDIO",[".mp3",".wav",".ogg",".m4a",".aac"]),P(Y,"SIYUAN_ASSETS_VIDEO",[".mov",".weba",".mkv",".mp4",".webm"]),P(Y,"SIYUAN_ASSETS_EXTS",[".pdf"].concat(Y.SIYUAN_ASSETS_IMAGE).concat(Y.SIYUAN_ASSETS_AUDIO).concat(Y.SIYUAN_ASSETS_VIDEO)),P(Y,"SIYUAN_ASSETS_SEARCH",[".txt",".md",".markdown",".docx",".xlsx",".pptx",".pdf",".json",".log",".sql",".html",".xml",".java",".h",".c",".cpp",".go",".rs",".swift",".kt",".py",".php",".js",".css",".ts",".sh",".bat",".cmd",".ini",".yaml",".rst",".adoc",".textile",".opml",".org",".wiki",".epub"]),P(Y,"ZWSP","");let Ke=Y;const ue="drawio_",Ne=ue+"tab",Ut=ue+"open",$t=ue+"update_title",Rt=ue+"newfile",Wt=ue+"callback",Ht=ue+"copyLink",qt=ue+"openTabByPath",ke=".drawio-config",K=".drawio",we="icon-drawio-standard",Ge=ue+"dock",ge="/data/";function Yt(e,n,t=Bt){const i=new Blob([n],{type:t});return new File([i],e,{type:t})}async function Re(e,n){let t=await I.fetchSyncPost(e,n);return t.code===0?t.data:null}async function ft(e,n,t){let i=new FormData;return i.append("path",e),i.append("isDir",n.toString()),i.append("modTime",Date.now().toString()),i.append("file",t),Re("/api/file/putFile",i)}async function zt(e){return R
|
|||
|
|
`+a};function an(e){return e.includes("-")?rn(e.split(".")[0]):""}const gt="%[a-f0-9]{2}",Ze=new RegExp("("+gt+")|([^%]+?)","gi"),Qe=new RegExp("("+gt+")+","gi");function Me(e,n){try{return[decodeURIComponent(e.join(""))]}catch{}if(e.length===1)return e;n=n||1;const t=e.slice(0,n),i=e.slice(n);return Array.prototype.concat.call([],Me(t),Me(i))}function on(e){try{return decodeURIComponent(e)}catch{let n=e.match(Ze)||[];for(let t=1;t<n.length;t++)e=Me(n,t).join(""),n=e.match(Ze)||[];return e}}function ln(e){const n={"%FE%FF":"<22><>","%FF%FE":"<22><>"};let t=Qe.exec(e);for(;t;){try{n[t[0]]=decodeURIComponent(t[0])}catch{const r=on(t[0]);r!==t[0]&&(n[t[0]]=r)}t=Qe.exec(e)}n["%C2"]="<22>";const i=Object.keys(n);for(const r of i)e=e.replace(new RegExp(r,"g"),n[r]);return e}function cn(e){if(typeof e!="string")throw new TypeError("Expected `encodedURI` to be of type `string`, got `"+typeof e+"`");try{return decodeURIComponent(e)}catch{return ln(e)}}function fn(e,n){const t={};if(Array.isArray(n))for(const i of n){const r=Object.getOwnPropertyDescriptor(e,i);r!=null&&r.enumerable&&Object.defineProperty(t,i,r)}else for(const i of Reflect.ownKeys(e)){const r=Object.getOwnPropertyDescriptor(e,i);if(r.enumerable){const s=e[i];n(i,s,e)&&Object.defineProperty(t,i,r)}}return t}function mt(e,n){if(!(typeof e=="string"&&typeof n=="string"))throw new TypeError("Expected the arguments to be of type `string`");if(e===""||n==="")return[];const t=e.indexOf(n);return t===-1?[]:[e.slice(0,t),e.slice(t+n.length)]}const un=e=>e==null,dn=e=>encodeURIComponent(e).replaceAll(/[!'()*]/g,n=>`%${n.charCodeAt(0).toString(16).toUpperCase()}`),xe=Symbol("encodeFragmentIdentifier");function hn(e){switch(e.arrayFormat){case"index":return n=>(t,i)=>{const r=t.length;return i===void 0||e.skipNull&&i===null||e.skipEmptyString&&i===""?t:i===null?[...t,[O(n,e),"[",r,"]"].join("")]:[...t,[O(n,e),"[",O(r,e),"]=",O(i,e)].join("")]};case"bracket":return n=>(t,i)=>i===void 0||e.skipNull&&i===null||e.skipEmptyString&&i===""?t:i===null?[...t,[O(n,e),"[]"].join("")]:[...t,[O(n,e),"[]=",O(i,e)].join("")];case"colon-list-separator":return n=>(t,i)=>i===void 0||e.skipNull&&i===null||e.skipEmptyString&&i===""?t:i===null?[...t,[O(n,e),":list="].join("")]:[...t,[O(n,e),":list=",O(i,e)].join("")];case"comma":case"separator":case"bracket-separator":{const n=e.arrayFormat==="bracket-separator"?"[]=":"=";return t=>(i,r)=>r===void 0||e.skipNull&&r===null||e.skipEmptyString&&r===""?i:(r=r===null?"":r,i.length===0?[[O(t,e),n,O(r,e)].join("")]:[[i,O(r,e)].join(e.arrayFormatSeparator)])}default:return n=>(t,i)=>i===void 0||e.skipNull&&i===null||e.skipEmptyString&&i===""?t:i===null?[...t,O(n,e)]:[...t,[O(n,e),"=",O(i,e)].join("")]}}function pn(e){let n;switch(e.arrayFormat){case"index":return(t,i,r)=>{if(n=/\[(\d*)]$/.exec(t),t=t.replace(/\[\d*]$/,""),!n){r[t]=i;return}r[t]===void 0&&(r[t]={}),r[t][n[1]]=i};case"bracket":return(t,i,r)=>{if(n=/(\[])$/.exec(t),t=t.replace(/\[]$/,""),!n){r[t]=i;return}if(r[t]===void 0){r[t]=[i];return}r[t]=[...r[t],i]};case"colon-list-separator":return(t,i,r)=>{if(n=/(:list)$/.exec(t),t=t.replace(/:list$/,""),!n){r[t]=i;return}if(r[t]===void 0){r[t]=[i];return}r[t]=[...r[t],i]};case"comma":case"separator":return(t,i,r)=>{const a=typeof i=="string"&&i.includes(e.arrayFormatSeparator)?i.split(e.arrayFormatSeparator).map(o=>he(o,e)):i===null?i:he(i,e);r[t]=a};case"bracket-separator":return(t,i,r)=>{const s=/(\[])$/.test(t);if(t=t.replace(/\[]$/,""),!s){r[t]=i&&he(i,e);return}const a=i===null?[]:he(i,e).split(e.arrayFormatSeparator);if(r[t]===void 0){r[t]=a;return}r[t]=[...r[t],...a]};default:return(t,i,r)=>{if(r[t]===void 0){r[t]=i;return}r[t]=[...[r[t]].flat(),i]}}}function bt(e){if(typeof e!="string"||e.length!==1)throw new TypeError("arrayFormatSeparator must be single character string")}function O(e,n){return n.encode?n.strict?dn(e):encodeURIComponent(e):e}function he(e,n){return n.decode?cn(e):e}function _t(e){return Array.isArray(e)?e.sort():typeof e=="object"?_t(Object.keys(e)).sort((n,t)=>Number(n)-Number(t)).map(n=>e[n]):e}function wt(e){const n=e.
|
|||
|
|
<path d="M16.634 11.932l1.756-1.016 5.090 8.814-1.756 1.014-5.090-8.812zM8.526 19.714l5.072-8.784 1.76 1.018-5.070 8.784-1.762-1.018z"></path>
|
|||
|
|
<path d="M12.276 4.296h7.448c0.9 0 1.348 0.45 1.348 1.348v5.786c0 0.9-0.45 1.348-1.348 1.348h-7.448c-0.9 0-1.348-0.45-1.348-1.348v-5.786c0-0.9 0.45-1.348 1.348-1.348zM19.714 19.224h7.45c0.898 0 1.346 0.448 1.346 1.346v5.788c0 0.898-0.448 1.346-1.346 1.346h-7.45c-0.898 0-1.348-0.448-1.348-1.346v-5.788c0-0.898 0.45-1.346 1.348-1.346zM4.838 19.224h7.448c0.9 0 1.348 0.448 1.348 1.346v5.788c0 0.898-0.45 1.346-1.348 1.346h-7.446c-0.9 0-1.348-0.448-1.348-1.346v-5.788c0-0.898 0.45-1.346 1.348-1.346z"></path>
|
|||
|
|
</symbol><symbol id="icon-drawio-inverse" viewBox="0 0 32 32">
|
|||
|
|
<path d="M31.454 31.454h-31.454v-31.454h31.454v31.454zM18.241 20.403v4.545c0 0.944 0.765 1.709 1.709 1.709h5.893c0.944 0 1.709-0.765 1.709-1.709v-4.545c0-0.944-0.765-1.709-1.709-1.709h-3.266l-3.476-5.92c0.753-0.179 1.313-0.855 1.313-1.662v-4.606c0-0.944-0.765-1.709-1.709-1.709h-5.893c-0.944 0-1.709 0.765-1.709 1.709v4.606c0 0.788 0.533 1.45 1.258 1.648l-3.484 5.934h-3.266c-0.944 0-1.709 0.765-1.709 1.709v4.545c0 0.944 0.765 1.709 1.709 1.709h5.893c0.944 0 1.709-0.765 1.709-1.709v-4.545c0-1.032-0.53-1.709-2.199-1.709l3.448-5.873h2.651l3.448 5.873c-1.681-0.116-2.366 0.988-2.322 1.709z"></path>
|
|||
|
|
</symbol>`),this.isMobile||this.addTopBar({icon:we,title:this.i18n.openDrawio,position:"right",callback:()=>{this.openNewCustomTab()}});const i=this;this.customTab=this.addTab({type:Ne,init(){const r=()=>{i.configLoaded?this.element.innerHTML=i.getIframeHtml(this.data||{}):setTimeout(r,100)};r()}}),this.protyleSlash=[{filter:["插入/选择drawio","insert/select drawio","crdrawio"],id:"insertDrawio",html:`<div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#icon-drawio-standard"></use></svg><span class="b3-list-item__text">${this.i18n.insertDrawio}</span></div>`,callback:this.showInsertDialog}],this.addCommand({langKey:"openDrawio",hotkey:"⇧⌘d",globalCallback:()=>{this.openNewCustomTab()}}),this.addDock({config:{position:"LeftBottom",size:{width:200,height:0},icon:we,title:i.i18n.title,hotkey:"⌥⌘W"},data:{},type:Ge,init:r=>{new Hn({target:r.element,props:{plugin:this}})},destroy(){console.log("destroy dock:",Ge)}}),this.restoreData()}getIframeHtml(t={}){return`<iframe class="siyuan-drawio-plugin__custom-tab" src="/plugins/siyuan-drawio-plugin/webapp/?${bn.stringify(t).toString()}"></iframe>`}async restoreData(){const t=await this.loadData(ke),i=typeof t=="string"?t:JSON.stringify(t),r=localStorage.getItem(ke);ce.debug("restoreData, remoteData: %s, localData: %s",i,r),i!=r&&(ce.debug("localStorage.setItem(DRAWIO_CONFIG)"),localStorage.setItem(ke,i)),this.configLoaded=!0}onLayoutReady(){}async onunload(){this.eventBus.off("open-siyuan-url-plugin",this.boundOnOpenTab),this.eventBus.off("loaded-protyle-static",this.boundBindStaticEvent),this.eventBus.off("ws-main",this.boundBindWsEvent),this.eventBus.off("open-menu-image",this.boundOpenMenuImage),window.removeEventListener("message",this.onMessage),window.removeEventListener("storage",this.onStorage)}uninstall(){}setStorageItem({key:t,value:i}){t==ke&&this.saveData(t,typeof i=="string"?i:JSON.stringify(i))}copyLink(t){var i=en(t);navigator.clipboard.writeText(i).then(()=>{I.showMessage(this.i18n.linkCopiedToClipboard)}).catch(r=>{ce.debug("Failed to copy link: ",r),I.showMessage(r,6e3,"error")})}copyRawLink(t){navigator.clipboard.writeText(t).then(()=>{I.showMessage(this.i18n.linkCopiedToClipboard)}).catch(i=>{ce.debug("Failed to copy link: ",i),I.showMessage(i,6e3,"error")})}updateTabTitle(t,i){var s;const r=(s=t==null?void 0:t.parentElement)==null?void 0:s.getAttribute("data-id");if(r){const o=this.getOpenedTab()[Ne].map(l=>l.tab).filter(l=>l.id===r);if(o.length>0){const l=o[0].model;l.data={url:We(i)},o[0].addModel(l),o[0].updateTitle(i)}}}showOpenDialog(t,i){const r=this,s={x:500,y:500},a=t?`<div class="search__tip">
|
|||
|
|
<kbd>shift ↵</kbd> ${this.i18n.create}
|
|||
|
|
<kbd>Esc</kbd> ${this.i18n.exitSearch}
|
|||
|
|
</div>`:"",o=new I.Dialog({title:`${this.i18n.selectDrawio}`,content:`<div class="fn__flex" style="max-height: 50vh">
|
|||
|
|
<div class="fn__flex-column" style="width:100%">
|
|||
|
|
<div class="fn__flex" style="margin: 0 8px 4px 8px">
|
|||
|
|
<input class="b3-text-field fn__flex-1"/>
|
|||
|
|
<span class="fn__space"></span>
|
|||
|
|
<span data-type="previous" class="block__icon block__icon--show"><svg><use xlink:href="#iconLeft"></use></svg></span>
|
|||
|
|
<span class="fn__space"></span>
|
|||
|
|
<span data-type="next" class="block__icon block__icon--show"><svg><use xlink:href="#iconRight"></use></svg></span>
|
|||
|
|
</div>
|
|||
|
|
<div class="b3-list fn__flex-1 b3-list--background" style="position: relative"><img style="margin: 0 auto;display: block;width: 64px;height: 64px" src="/stage/loading-pure.svg"></div>
|
|||
|
|
${a}
|
|||
|
|
</div>
|
|||
|
|
</div>`,width:this.isMobile?"92vw":"560px"});o.bindInput(o.element.querySelector("input"));function l(c){c.style.maxWidth="none";const m=c.querySelector(".b3-list"),h=c.querySelector("input");h.addEventListener("keydown",b=>{if(b.isComposing)return;const g=c.querySelector(".b3-list--empty");if(g||Mt(m,b),b.key==="Enter"){if(!g||b.shiftKey){const p=c.querySelector(".b3-list-item--focus");i&&i(p==null?void 0:p.getAttribute("data-value"),o,b.shiftKey,h.value)}b.preventDefault(),b.stopPropagation()}}),h.addEventListener("input",b=>{b.isComposing||(b.stopPropagation(),r.renderAssetList(c,h.value,s))}),h.addEventListener("compositionend",b=>{b.stopPropagation(),r.renderAssetList(c,h.value,s)}),c.lastElementChild.addEventListener("click",b=>{const g=b.target;if(ze(g,"data-type","previous")){h.dispatchEvent(new KeyboardEvent("keydown",{key:"ArrowUp"})),b.stopPropagation();return}if(ze(g,"data-type","next")){h.dispatchEvent(new KeyboardEvent("keydown",{key:"ArrowDown"})),b.stopPropagation();return}const y=Ot(g,"b3-list-item");if(y){b.stopPropagation();const E=y.getAttribute("data-value");i&&i(E,o,!1,h.value)}}),r.renderAssetList(c,"",s)}l(o.element)}showDrawioDialog(t,i){const r=this;return new I.Dialog({title:t||"drawio",content:r.getIframeHtml({...i,ui:"min"}),width:"100vw",height:"100vh"})}onSave(t,i,r){dt(i).then(s=>{t.destroy();const a=s.succMap[i]||s.succMap[i+K];r.insert(Oe(a),!0,!0),this.openCustomTab(Se(a),void 0,{url:a})}).catch(s=>{console.error(s),I.showMessage(s,6e3,"error")})}onOpenTab(t){if(t.detail.url){const i=new URL(t.detail.url);this.openCustomTab(i.searchParams.get("title"),i.searchParams.get("icon"),i.searchParams.get("data")?JSON.parse(i.searchParams.get("data")):{})}}openNewCustomTab(){this.openCustomTab(void 0,void 0,{nounce:new Date().getTime()})}openCustomTab(t,i,r){this.isMobile?this.showDrawioDialog(t,r||{}):I.openTab({app:this.app,custom:{icon:i||we,title:t||"drawio",data:r,id:this.name+Ne}})}openCustomTabByPath(t){this.openCustomTab(Se(t),void 0,{url:t})}openMenuImage({detail:t}){const s=t.element.querySelector("img").dataset.src;s&&s.startsWith(Le)&&window.siyuan.menus.menu.addItem({id:"edit-drawio",icon:"iconEdit",label:`${this.i18n.editDrawio}`,index:1,click:()=>{this.openCustomTabByPath(s)}})}bindStaticEvent(t){if(t.detail.protyle){const i=t.detail.protyle.wysiwyg.element;this.bindClickEvent(i)}}bindClickEvent(t){t.querySelectorAll('[data-type="a"]').forEach(r=>{r.dataset.href&&r.dataset.href.endsWith(K)&&r.addEventListener("click",this.onClickEvent)})}bindDynamicEvent(t){ce.debug("bindDynamicEvent",t),t.detail.protyle}bindWsEvent(t){if(t.detail.cmd==="savedoc"){const i=t.target;this.bindClickEvent(i.parentNode)}}async renderAssetList(t,i,r){const s=I.getFrontend(),a=s==="mobile"||s==="browser-mobile";try{const o=await ut(),l=await Gt(i,o);let c="";l.forEach((b,g)=>{c+=`<div data-value="${b.path}" class="b3-list-item${g===0?" b3-list-item--focus":""}" style="display:block">
|
|||
|
|
<div class="b3-list-item__text">
|
|||
|
|
${b.hName}</div>
|
|||
|
|
<div class="b3-list-item__text" style="font-size: 0.7em">
|
|||
|
|
${b.path}</div>
|
|||
|
|
</div>`});const m=t.querySelector(".b3-list"),h=t.querySelector("input");m.innerHTML=c||`<li class="b3-list--empty">${this.i18n.emptyContent}</li>`,a?window.siyuan.menus.menu.fullscreen():window.siyuan.menus.menu.popup(r),i||h.select()}catch(o){console.error("Error rendering asset list:",o);const l=t.querySelector(".b3-list");l.innerHTML=`<li class="b3-list--empty">${this.i18n.emptyContent}</li>`}}}module.exports=qn;
|