Files
server-configs/siyuan/data/plugins/move-doc/index.js
2026-02-13 22:24:27 +08:00

2 lines
2.8 KiB
JavaScript

(()=>{"use strict";var c={};c.d=(a,t)=>{for(var e in t)c.o(t,e)&&!c.o(a,e)&&Object.defineProperty(a,e,{enumerable:!0,get:t[e]})},c.o=(a,t)=>Object.prototype.hasOwnProperty.call(a,t),c.r=a=>{typeof Symbol<"u"&&Symbol.toStringTag&&Object.defineProperty(a,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(a,"__esModule",{value:!0})};var f={};c.r(f),c.d(f,{default:()=>y});const u=require("siyuan");var b=(a,t,e)=>new Promise((o,r)=>{var d=n=>{try{l(e.next(n))}catch(i){r(i)}},s=n=>{try{l(e.throw(n))}catch(i){r(i)}},l=n=>n.done?o(n.value):Promise.resolve(n.value).then(d,s);l((e=e.apply(a,t)).next())});class y extends u.Plugin{constructor(){super(...arguments),this.openMenuDoctree=t=>{var e,o,r;const d=t.detail.type,s=t.detail.menu,l=t.detail.elements,n=this.getCurrentDocParams();if(n){if(d==="doc"){const i=(e=l[0])==null?void 0:e.getAttribute("data-node-id");if(!i||n.path.slice(-48).includes(i))return;s.addItem({id:"move-doc_to-this-doc",label:this.i18n.moveToThisDoc,click:()=>b(this,null,function*(){const h=yield(0,u.fetchSyncPost)("/api/filetree/getPathByID",{id:i}),p=h.data.notebook,m=h.data.path;!p||!m||(0,u.fetchPost)("/api/filetree/moveDocs",{fromPaths:[n.path],toNotebook:p,toPath:m})})})}else if(d==="notebook"){const i=(r=(o=l[0])==null?void 0:o.parentElement)==null?void 0:r.getAttribute("data-url");if(!i||n.notebookId===i&&n.path.length<=26)return;s.addItem({id:"move-doc_to-this-notebook",label:this.i18n.moveToThisNotebook,click:()=>b(this,null,function*(){const h=n.path;h&&(0,u.fetchPost)("/api/filetree/moveDocs",{fromPaths:[h],toNotebook:i,toPath:"/"})})})}}},this.getCurrentDocParams=()=>{if(this.isMobile){if(window.siyuan.mobile.editor&&window.siyuan.mobile.editor.protyle)return{path:window.siyuan.mobile.editor.protyle.path,notebookId:window.siyuan.mobile.editor.protyle.notebookId}}else{let t=document.querySelector(".layout__wnd--active > .fn__flex > .layout-tab-bar > .item--focus");if(t||document.querySelectorAll("ul.layout-tab-bar > .item--focus").forEach((e,o)=>{(o===0||e.dataset.activetime>t.dataset.activetime)&&(t=e)}),t){const e=this.getInstanceById(t.getAttribute("data-id"));if(e&&e.model&&typeof e.model=="object"&&"editor"in e.model){const o=e.model;if(o.editor&&o.editor.protyle)return{path:o.editor.protyle.path,notebookId:o.editor.protyle.notebookId}}}return!1}},this.getInstanceById=(t,e=window.siyuan.layout.centerLayout)=>{const o=(r,d)=>{if(r.id===d)return r;if(!r.children)return;let s;for(let l=0;l<r.children.length;l++)if(s=o(r.children[l],d),s)return s};return o(e,t)}}onload(){const t=(0,u.getFrontend)();this.isMobile=t==="mobile"||t==="browser-mobile",this.eventBus.on("open-menu-doctree",this.openMenuDoctree)}onunload(){this.eventBus.off("open-menu-doctree",this.openMenuDoctree)}uninstall(){this.eventBus.off("open-menu-doctree",this.openMenuDoctree)}}module.exports=f})();