let e=require(`obsidian`);var t=class e{static logLevel=`silent`;style;prefix;constructor(e,t){this.scope=e,this.color=t,this.style=`color: ${this.color}; background: #21202a; padding: 1px 5px; border-radius: 5px; font-family: consolas, monospace; font-size: 11px; border: 1px solid ${this.color}50;`,this.prefix=`%cMS|${this.scope}`}info=(...e)=>this.log(`log`,...e);warn=(...e)=>this.log(`warn`,...e);error=(...e)=>this.log(`error`,...e);infoCompact=(t,...n)=>{e.logLevel!==`silent`&&(console.groupCollapsed(this.prefix,this.style,t),console.log(...n),console.groupEnd())};log(t=`log`,...n){e.logLevel!==`silent`&&console[t](this.prefix,this.style,...n)}},n=class extends e.PluginSettingTab{constructor(e,t){super(e,t),this.plugin=t}display(){this.containerEl.empty(),new e.Setting(this.containerEl).setName(`New item placement`).setDesc(`Default new item placement.`).addDropdown(e=>e.addOption(`top`,`Top`).addOption(`bottom`,`Bottom`).setValue(this.plugin.settings.newItemPlacement).onChange(async e=>{this.plugin.settings.newItemPlacement=e,await this.plugin.saveSettings()})),new e.Setting(this.containerEl).setName(`Debug Mode`).setDesc(`Show debug logs in the console.`).addToggle(e=>e.setValue(this.plugin.settings.debugMode).onChange(async e=>{this.plugin.settings.debugMode=e,t.logLevel=e?`debug`:`silent`,await this.plugin.saveSettings()}))}},r=class extends e.Modal{constructor(t,n){super(t),this.setTitle(`Manual Sorting`),this.modalEl.addClass(`manual-sorting-modal`);let r=this.contentEl.createEl(`div`);r.createEl(`p`,{text:`Reset custom order?`});let i=r.createEl(`div`,{cls:`modal-buttons`});new e.ButtonComponent(i).setButtonText(`Yep`).setCta().onClick(()=>{this.close(),n()}),new e.ButtonComponent(i).setButtonText(`Nope`).onClick(()=>{this.close()})}},i=class{log=new t(`DND-MANAGER`,`#a6ff00`);explorerEl;dragStartHandler=null;dragStartEventType=e.Platform.isMobile?`touchstart`:`dragstart`;dragEventType=e.Platform.isMobile?`touchmove`:`drag`;dropEventType=e.Platform.isMobile?`touchend`:`dragend`;rafId=0;folderExpandTimeout=null;pendingExpandFolder=null;scrollRafId=null;scrollZone=50;scrollSpeed=5;dropZonesActivationDelay=250;dropZonesActivationTimeout=null;dragZoneWidth=36;constructor(e){this.plugin=e}async enable(){this.explorerEl=await this.plugin.explorerManager.waitForExplorerElement(!0);let t=null,n=`before`;this.dragStartHandler=r=>{let i=r.target.closest(`.tree-item-self`);if(!i)return;let a=r instanceof DragEvent?r:r.touches[0],o=i.getBoundingClientRect().right-a.clientX;if(e.Platform.isMobile){if(o>this.dragZoneWidth)return;r.preventDefault()}this.log.info(`Drag started`);let s=this.explorerEl.getBoundingClientRect(),c=!1;this.explorerEl.dataset.dragActive=``,i.dataset.isBeingDragged=``;let l=r=>{e.Platform.isMobile&&(r.stopPropagation(),r.preventDefault()),cancelAnimationFrame(this.rafId),this.rafId=requestAnimationFrame(()=>{this.log.info(`Dragging...`);let e=r instanceof DragEvent?r:r.touches[0];if(c=e.clientXs.right||e.clientYs.bottom,c){this.clearDropIndicators();return}this.collapseDraggedFolder(i),{futureSibling:t,dropPosition:n}=this.findDropTarget(e.clientY),this.updateDropIndicators(t,n),this.handleAutoScroll(e.clientY,s)})};i.addEventListener(this.dragEventType,l),i.addEventListener(this.dropEventType,()=>{if(this.log.info(`Item dropped`),cancelAnimationFrame(this.rafId),i.removeEventListener(this.dragEventType,l),this.clearDropIndicators(),this.stopAutoScroll(),delete this.explorerEl.dataset.dragActive,c)return;let e=i.dataset.path,r=this.plugin.getFileExplorerView().fileItems[e];if(!t)return;let a=t.querySelector(`.tree-item-self`)?.dataset.path??``,o=t.classList.contains(`temp-child`);this.plugin.orderManager.reconcileOrder();let s=this.plugin.getFileExplorerView().tree.selectedDoms;s.has(r)?this.moveSelectedItems(s,a,o,n):this.moveItem(r,a,n,o),t=null},{once:!0})},this.explorerEl.addEventListener(`mousedown`,()=>{this.dropZonesActivationTimeout=window.setTimeout(()=>{this.explorerEl.dataset.dragActive===void 0&&(this.explorerEl.dataset.dragActive=``),this.dropZonesActivationTimeout=null},this.dropZonesActivationDelay)}),this.explorerEl.addEventListener(`mouseup`,()=>{delete this.explorerEl.dataset.dragActive,this.dropZonesActivationTimeout&&=(clearTimeout(this.dropZonesActivationTimeout),null)}),this.explorerEl.addEventListener(this.dragStartEventType,this.dragStartHandler),this.log.info(`Drag and drop enabled`)}disable(){this.dragStartHandler&&this.explorerEl.removeEventListener(this.dragStartEventType,this.dragStartHandler),this.log.info(`Drag and drop disabled`)}collapseDraggedFolder(e){if(e.classList.contains(`nav-folder-title`)&&e.dataset.path){let t=this.plugin.getFileExplorerView().fileItems[e.dataset.path];t.collapsed||t.setCollapsed(!0,!0)}}findDropTarget(e){let t=Array.from(this.explorerEl.querySelectorAll(`.tree-item:not(.nav-folder:has(> [data-is-being-dragged]) .tree-item)`));if(!t.length)return{futureSibling:t[0],dropPosition:`before`};let n=t[0],r=t[0].matches(`.tree-item:nth-child(1 of .tree-item)`)?`before`:`after`;return t.forEach(t=>{let i=t.classList.contains(`temp-child`),a=t.getBoundingClientRect(),o=a.top,s=a.bottom;t.matches(`.tree-item-children .tree-item:nth-last-child(1 of .tree-item)`)&&(s-=.01);let c=n.getBoundingClientRect()[r===`before`?`top`:`bottom`],l=Math.abs(c-e),u=Math.abs(s-e);if(ue.removeAttribute(`data-drop-position`)),t.dataset.dropPosition=n;let r=t.querySelector(`:scope > .tree-item-self`),i=r?.dataset.path??``;e.Platform.isMobile&&[`nav-folder`,`is-collapsed`].every(e=>t.classList.contains(e))&&r?.dataset.isBeingDragged===void 0?this.pendingExpandFolder!==i&&(this.folderExpandTimeout!==null&&(clearTimeout(this.folderExpandTimeout),this.folderExpandTimeout=null),this.pendingExpandFolder=i,this.folderExpandTimeout=window.setTimeout(()=>{this.plugin.getFileExplorerView().fileItems[i].setCollapsed(!1,!0),this.folderExpandTimeout=null,this.pendingExpandFolder=null},800)):this.folderExpandTimeout!==null&&(clearTimeout(this.folderExpandTimeout),this.folderExpandTimeout=null,this.pendingExpandFolder=null);let a=t.querySelector(`.tree-item-children`);if(a&&!a.children.length){let e=Object.assign(document.createElement(`div`),{className:`tree-item temp-child`,innerHTML:`
`});a.appendChild(e)}document.querySelectorAll(`.nav-folder.is-drop-target`).forEach(e=>e.classList.remove(`is-drop-target`));let o=t.parentElement.closest(`.nav-folder, [data-type="file-explorer"] > .nav-files-container > div`);o&&o.classList.add(`is-drop-target`)}clearDropIndicators(){this.folderExpandTimeout!==null&&(clearTimeout(this.folderExpandTimeout),this.folderExpandTimeout=null,this.pendingExpandFolder=null),document.querySelectorAll(`[data-is-being-dragged]`).forEach(e=>e.removeAttribute(`data-is-being-dragged`)),document.querySelectorAll(`[data-drop-position]`).forEach(e=>e.removeAttribute(`data-drop-position`)),document.querySelectorAll(`.is-drop-target`).forEach(e=>e.classList.remove(`is-drop-target`)),document.querySelectorAll(`.temp-child`).forEach(e=>e.remove())}moveItem(t,n,r,i){let a=t.file,o=a.path,s=n.substring(0,n.lastIndexOf(`/`)),c=s?s+`/`+a.name:a.name;if(o!==c&&this.plugin.app.vault.getAbstractFileByPath(c)){let t=a instanceof e.TFile?c.slice(0,-(a.extension.length+1)):c;c=this.plugin.app.vault.getAvailablePath(t,a instanceof e.TFile?a.extension:``)}return i&&(n=``),o!==c||c!==n?(this.log.info(`Moving '${o}' to '${c}' (${r} '${n}')`),this.plugin.app.fileManager.renameFile(a,c),this.plugin.orderManager.move(o,c,n,r)):this.log.info(`No move needed: '${o}' is already at the target position`),this.plugin.saveSettings(),this.plugin.getFileExplorerView().lastDropTargetEl=t.el,c}moveSelectedItems(t,n,r,i){let a=Array.from(t);a.some(t=>t.file instanceof e.TFolder)&&(a=a.filter(t=>{if(t.file instanceof e.TFolder)return!0;let n=t.file;return!a.some(t=>t.file instanceof e.TFolder&&n.path.startsWith(t.file.path+`/`))})),a=a.sort((e,t)=>{let n=e.el,r=t.el;return n.compareDocumentPosition(r)&Node.DOCUMENT_POSITION_FOLLOWING?-1:n.compareDocumentPosition(r)&Node.DOCUMENT_POSITION_PRECEDING?1:0}),a.forEach((e,t)=>{t>0&&(i=`after`,r=!1),n=this.moveItem(e,n,i,r)})}handleAutoScroll(e,t){let n=e-t.top,r=t.bottom-e;n{this.explorerEl.scrollTop+=e,this.scrollRafId=requestAnimationFrame(t)};this.scrollRafId=requestAnimationFrame(t)}stopAutoScroll(){this.scrollRafId&&=(cancelAnimationFrame(this.scrollRafId),null)}};const a=`[data-type="file-explorer"] > .nav-files-container`;var o=class{log=new t(`EXPLORER-MANAGER`,`#ffa700`);constructor(e){this.plugin=e}waitForExplorerElement=async(e=!1)=>new Promise(t=>{this.observeExplorerMount(t,!0,!0,e)});refreshExplorerOnMount=()=>this.observeExplorerMount(()=>this.refreshExplorer(),!1,!1);refreshExplorer(){this.log.info(`Refreshing Explorer after mount`),this.plugin.orderManager.reconcileOrder(),this.plugin.getFileExplorerView().setSortOrder(this.plugin.settings.sortOrder),this.plugin.isCustomSortingActive()&&this.plugin.dndManager.enable()}observeExplorerMount(e,t=!1,n=!0,r=!1){if(n){let t=document.querySelector(a);if(t instanceof HTMLElement){r||this.log.info(`Explorer already mounted`,t),console.log(),e(t);return}}new MutationObserver((n,i)=>{for(let o of n)for(let n of o.addedNodes)if(n instanceof HTMLElement&&n.matches(a)){t&&i.disconnect(),r||this.log.info(`Explorer mounted`,n),e(n);return}}).observe(document.querySelector(`.workspace`)??document.body,{childList:!0,subtree:!0})}};const s=`custom`,c={customOrder:{"/":{children:[],sortOrder:`custom`}},sortOrder:`custom`,debugMode:!1,newItemPlacement:`top`};var l=class{log=new t(`ORDER-MANAGER`,`#00ccff`);constructor(e){this.plugin=e}add(t){let n=t.path;this.log.info(`Inserting new item: '${n}'`);let r=this.plugin.settings.customOrder,i=n.substring(0,n.lastIndexOf(`/`))||`/`,a=t instanceof e.TFolder,o=this.plugin.settings.newItemPlacement;a&&(r[n]={children:[],sortOrder:`custom`}),o===`top`?r[i].children.unshift(n):r[i].children.push(n),this.logOrder(`Updated order after adding new item:`)}rename(e,t){this.log.info(`Renaming '${e}' to '${t}'`);let n=this.plugin.settings.customOrder,r=e.substring(0,e.lastIndexOf(`/`))||`/`;n[r].children=n[r].children.map(n=>n===e?t:n),e in n&&this.renameFolder(e,t),this.logOrder(`Updated order after renaming item:`)}move(e,t,n,r){this.log.info(`Moving '${e}' to '${t}' (${r} '${n}')`);let i=this.plugin.settings.customOrder,a=e.substring(0,e.lastIndexOf(`/`))||`/`,o=t.substring(0,t.lastIndexOf(`/`))||`/`,s=e in i,c=a!==o;i[a].children=i[a].children.filter(t=>t!==e);let l=0;if(n){let e=i[o].children.indexOf(n);l=r===`before`?e:e+1}i[o].children.splice(l,0,t),s&&this.renameFolder(e,t),this.logOrder(`Updated order after moving item:`),c||(this.log.info(`Directory did not change, calling sort on File Explorer manually`),this.plugin.getFileExplorerView().sort())}remove(e){this.log.info(`Removing item: '${e}'`);let t=this.plugin.settings.customOrder,n=e.substring(0,e.lastIndexOf(`/`))||`/`,r=e in t;t[n].children=t[n].children.filter(t=>t!==e),r&&delete t[e],this.logOrder(`Updated order after removing item:`)}reconcileOrder(){this.log.info(`Updating order...`);let e=this.getCurrentOrder(),t=this.plugin.settings.customOrder,n=this.matchSavedOrder(e,t);this.plugin.settings.customOrder=n,this.logOrder(`Order updated:`)}resetOrder(){this.plugin.settings.customOrder={"/":{children:[],sortOrder:`custom`}}}logOrder(e){this.log.infoCompact(e,JSON.stringify(this.plugin.settings.customOrder,null,4))}getCurrentOrder(){let t={},n=this.plugin.getFileExplorerView(),r=i=>{let a=n.getSortedFolderItems(i,!0),o=a.map(e=>e.file.path);t[i.path]={children:o,sortOrder:`custom`};for(let t of a){let n=t.file;n instanceof e.TFolder&&r(n)}};return r(this.plugin.app.vault.root),t}matchSavedOrder(e,t){let n={};for(let r in e)if(r in t){let i=t[r],a=e[r],o=i.children.filter(e=>a.children.includes(e)),s=a.children.filter(e=>!i.children.includes(e));this.plugin.settings.newItemPlacement===`top`?n[r]={children:Array.from(new Set([...s,...o])),sortOrder:`custom`}:n[r]={children:Array.from(new Set([...o,...s])),sortOrder:`custom`}}else n[r]={children:Array.from(new Set(e[r].children)),sortOrder:`custom`};return n}removeFolder(e){let t=this.plugin.settings.customOrder;t[e].children.forEach(e=>{e in t&&this.removeFolder(e)}),delete t[e]}renameFolder(e,t){if(e===t)return;let n=this.plugin.settings.customOrder;n[t]=n[e],delete n[e],n[t].children=n[t].children.map(r=>{let i=r.replace(e,t);return r in n&&this.renameFolder(r,i),i})}};function u(e,t){let n=Object.keys(t).map(n=>d(e,n,t[n]));return n.length===1?n[0]:function(){n.forEach(e=>e())}}function d(e,t,n){let r=e[t],i=e.hasOwnProperty(t),a=i?r:function(){return Object.getPrototypeOf(e)[t].apply(this,arguments)},o=n(a);return r&&Object.setPrototypeOf(o,r),Object.setPrototypeOf(s,o),e[t]=s,c;function s(...n){return o===a&&e[t]===s&&c(),o.apply(this,n)}function c(){e[t]===s&&(i?e[t]=a:delete e[t]),o!==a&&(o=a,Object.setPrototypeOf(s,r||Function))}}var f=class{explorerUninstaller=null;menuUninstaller=null;log=new t(`PATCHER`,`#988bff`);constructor(e){this.plugin=e}patchExplorer(){let e=this,t=this.plugin,n=t.getFileExplorerView();this.explorerUninstaller=u(Object.getPrototypeOf(n),{getSortedFolderItems:e=>function(n,r){let i=e.call(this,n);if(r||!t.isCustomSortingActive())return i;let a=n.path,o=t.settings.customOrder[a].children;return i.sort((e,t)=>o.indexOf(e.file.path)-o.indexOf(t.file.path))},setSortOrder:n=>function(r){n.call(this,r),e.log.info(`Sort order changed to: '${r}'`);let i=t.isCustomSortingActive();t.settings.sortOrder=r,t.saveSettings(),i&&!t.isCustomSortingActive()&&(t.dndManager.disable(),t.getFileExplorerView().sort())}})}patchSortOrderMenu(){let t=this.plugin;this.menuUninstaller=u(e.Menu.prototype,{showAtMouseEvent:n=>function(...i){let a=i[0].target;if(a.getAttribute(`aria-label`)===i18next.t(`plugins.file-explorer.action-change-sort`)&&a.classList.contains(`nav-action-button`)){let n=this;if(t.isCustomSortingActive()){let t=n.items.find(t=>t instanceof e.MenuItem&&t.checked===!0);t&&t.setChecked(!1)}let i=s;n.addItem(e=>{e.setTitle(`Manual sorting`).setIcon(`pin`).setChecked(t.isCustomSortingActive()).setSection(i).onClick(()=>{t.isCustomSortingActive()||(t.settings.sortOrder=s,t.orderManager.reconcileOrder(),t.saveSettings(),t.getFileExplorerView().sort(),t.dndManager.enable())})}),n.addItem(e=>{e.setTitle(`Reset order`).setIcon(`trash-2`).setSection(i).onClick(()=>{new r(t.app,()=>{t.orderManager.resetOrder(),t.orderManager.reconcileOrder(),t.saveSettings(),t.getFileExplorerView().sort()}).open()})});let a=n.items,o=a.splice(8,1)[0];a.splice(0,0,o)}return n.apply(this,i)}})}unpatchExplorer(){this.explorerUninstaller&&(this.explorerUninstaller(),this.explorerUninstaller=null,this.log.info(`Explorer unpatched`))}unpatchSortOrderMenu(){this.menuUninstaller&&(this.menuUninstaller(),this.menuUninstaller=null,this.log.info(`Sort order menu unpatched`))}},p=class extends e.Plugin{orderManager=new l(this);patcher=new f(this);explorerManager=new o(this);dndManager=new i(this);log=new t(`CORE`,`#ff4828`);settings;async onload(){await this.loadSettings(),t.logLevel=this.settings.debugMode?`debug`:`silent`,this.addSettingTab(new n(this.app,this)),this.app.workspace.onLayoutReady(()=>this.initialize())}onunload(){this.patcher.unpatchExplorer(),this.patcher.unpatchSortOrderMenu(),this.dndManager.disable(),this.getFileExplorerView().sort(),this.log.info(`Manual Sorting unloaded`)}async initialize(){await this.explorerManager.waitForExplorerElement(),this.patcher.patchExplorer(),this.patcher.patchSortOrderMenu(),this.explorerManager.refreshExplorer(),this.explorerManager.refreshExplorerOnMount(),this.registerVaultHandlers()}registerVaultHandlers(){this.app.vault.on(`rename`,(e,t)=>{(t.substring(0,t.lastIndexOf(`/`))||`/`)===(e.path.substring(0,e.path.lastIndexOf(`/`))||`/`)&&(this.log.info(`Item renamed from '${t}' to '${e.path}'`),this.orderManager.rename(t,e.path))}),this.app.vault.on(`create`,e=>{this.log.info(`Item created: '${e.path}'`),this.orderManager.add(e)}),this.app.vault.on(`delete`,e=>{this.log.info(`Item deleted: '${e.path}'`),this.orderManager.remove(e.path)})}async loadSettings(){let e=await this.loadData()||{},t;e.customOrder?.[`/`]&&Array.isArray(e.customOrder[`/`])?(this.log.info(`Migrating settings to v4 format`),t=this.migrateLegacySettings(e)):t=e,this.settings={...c,...Object.fromEntries(Object.keys(c).filter(e=>e in t).map(e=>[e,t[e]]))},this.log.info(`Settings loaded:`,this.settings)}async saveSettings(){await this.saveData(this.settings),this.log.info(`Settings saved:`,this.settings)}async onExternalSettingsChange(){await this.loadSettings(),this.log.warn(`Settings changed externally`),this.getFileExplorerView().sort()}migrateLegacySettings=e=>({...e,customOrder:Object.fromEntries(Object.entries(e.customOrder).map(([e,t])=>[e,{children:t,sortOrder:`custom`}])),sortOrder:e.sortOrder===`customOrder`?s:e.sortOrder});isCustomSortingActive=()=>this.settings.sortOrder===s;getFileExplorerView=()=>this.app.workspace.getLeavesOfType(`file-explorer`)[0].view};module.exports=p; /* nosourcemap */