xwberry_obsidian-drag-out/main.js

6 lines
5.1 KiB
JavaScript

/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var v=Object.create;var u=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var C=Object.getPrototypeOf,S=Object.prototype.hasOwnProperty;var E=(i,r)=>{for(var t in r)u(i,t,{get:r[t],enumerable:!0})},m=(i,r,t,e)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of w(r))!S.call(i,n)&&n!==t&&u(i,n,{get:()=>r[n],enumerable:!(e=D(r,n))||e.enumerable});return i};var P=(i,r,t)=>(t=i!=null?v(C(i)):{},m(r||!i||!i.__esModule?u(t,"default",{value:i,enumerable:!0}):t,i)),I=i=>m(u({},"__esModule",{value:!0}),i);var K={};E(K,{default:()=>f});module.exports=I(K);var o=require("obsidian"),A=require("fs"),h=P(require("path")),y={modifierKey:process.platform==="darwin"?"alt":"ctrl",debugLogging:!1},W=".nav-file-title, .nav-folder-title",O="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII=";function b(i){return i==="alt"||i==="shift"||i==="ctrl"||i==="none"}function L(i){if(!i||typeof i!="object")return!1;let r=i;return(r.modifierKey===void 0||b(r.modifierKey))&&(r.debugLogging===void 0||typeof r.debugLogging=="boolean")}var f=class extends o.Plugin{constructor(){super(...arguments);this.settings=y;this.iconPath=null}async onload(){await this.loadSettings(),this.resolveIconPath(),this.registerDomEvent(activeDocument,"dragstart",t=>this.handleDragStart(t),{capture:!0}),this.addSettingTab(new p(this.app,this)),this.log("Drag Out loaded")}onunload(){this.log("Drag Out unloaded")}resolveIconPath(){let t=this.app.vault.adapter;if(!(t instanceof o.FileSystemAdapter)){this.iconPath=null;return}let e=h.join(t.getBasePath(),this.app.vault.configDir,"plugins",this.manifest.id),n=h.join(e,"icon.png");this.iconPath=(0,A.existsSync)(n)?n:null}isModifierPressed(t){switch(this.settings.modifierKey){case"alt":return t.altKey;case"shift":return t.shiftKey;case"ctrl":return t.ctrlKey||t.metaKey;case"none":return!0}}handleDragStart(t){if(!this.isModifierPressed(t))return;let e=t.target;if(!e)return;let n=e.closest(W);if(!n)return;let c=n.getAttribute("data-path");if(!c){this.log("file tree item missing data-path attribute");return}let a=this.app.vault.adapter;if(!(a instanceof o.FileSystemAdapter))return;let d=a.getBasePath(),g=this.collectSelectedAbsolutePaths(c,d);if(g.length===0){this.log("no paths resolved, falling through to default drag");return}let s=this.getWebContents();if(!s){new o.Notice("Could not access Electron drag APIs. Check the developer console (Ctrl+Shift+I) for details.");return}try{let l=this.buildDragItem(g);s.startDrag(l),t.preventDefault(),t.stopPropagation(),this.log("startDrag invoked",g)}catch(l){console.error("[DragOut] startDrag failed:",l),new o.Notice("Drag failed. See console for details.")}}buildDragItem(t){let e=this.getDragIcon();return t.length===1?{file:t[0],icon:e}:{file:t[0],files:t,icon:e}}getDragIcon(){if(this.iconPath)return this.iconPath;let t=this.requireElectron(),e=t.nativeImage.createFromDataURL(O);return e.isEmpty()?t.nativeImage.createEmpty():e}collectSelectedAbsolutePaths(t,e){let n=activeDocument.querySelectorAll(".nav-file-title.is-selected, .nav-folder-title.is-selected"),c=new Set,a=[],d=s=>{let l=h.join(e,s);c.has(l)||(c.add(l),a.push(l))};if(n.length===0)return d(t),a;let g=Array.from(n).map(s=>s.getAttribute("data-path")).filter(s=>!!s);return g.includes(t)?(g.forEach(d),a):(d(t),a)}getWebContents(){var t,e;try{let n=(t=activeWindow.require)==null?void 0:t.call(activeWindow,"@electron/remote");if(n!=null&&n.getCurrentWebContents)return n.getCurrentWebContents()}catch(n){}try{let n=this.requireElectron();if((e=n.remote)!=null&&e.getCurrentWebContents)return n.remote.getCurrentWebContents()}catch(n){}return null}requireElectron(){var e;return((e=activeWindow.require)!=null?e:require)("electron")}log(...t){this.settings.debugLogging&&console.debug("[DragOut]",...t)}async loadSettings(){let t=await this.loadData(),e=L(t)?t:{};this.settings={...y,...e}}async saveSettings(){await this.saveData(this.settings)}},p=class extends o.PluginSettingTab{constructor(r,t){super(r,t),this.plugin=t}display(){let{containerEl:r}=this;r.empty(),new o.Setting(r).setName("Modifier key").setDesc("Hold this key while dragging a file from the file explorer to drag the actual file (instead of an obsidian:// link). Pick 'None' to always use external drag \u2014 but be aware this disables Obsidian's built-in file-tree drag behavior.").addDropdown(t=>t.addOption("ctrl","Ctrl/cmd (default on Windows/Linux)").addOption("alt","Alt/option (default on macOS)").addOption("shift","Shift").addOption("none","None \u2014 always intercept").setValue(this.plugin.settings.modifierKey).onChange(async e=>{b(e)&&(this.plugin.settings.modifierKey=e,await this.plugin.saveSettings())})),new o.Setting(r).setName("Debug logging").setDesc("Log drag events to the developer console (Ctrl+Shift+I). Useful for troubleshooting; leave off otherwise.").addToggle(t=>t.setValue(this.plugin.settings.debugLogging).onChange(async e=>{this.plugin.settings.debugLogging=e,await this.plugin.saveSettings()}))}};