Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
6 lines (5 loc) · 17.4 KB
/
Copy pathmain.js
File metadata and controls
6 lines (5 loc) · 17.4 KB
1
2
3
4
5
6
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
If you want to view the source, visit the github repository of this plugin
*/
"use strict";var C=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var K=Object.prototype.hasOwnProperty;var W=(o,i)=>{for(var e in i)C(o,e,{get:i[e],enumerable:!0})},Y=(o,i,e,t)=>{if(i&&typeof i=="object"||typeof i=="function")for(let r of q(i))!K.call(o,r)&&r!==e&&C(o,r,{get:()=>i[r],enumerable:!(t=z(i,r))||t.enumerable});return o};var $=o=>Y(C({},"__esModule",{value:!0}),o);var ie={};W(ie,{default:()=>T});module.exports=$(ie);var G=require("obsidian");var d="#5c8af5";function u(o){return(o.label||"").trim()||o.property}var N={properties:[],showPropertyNodes:!0,weightNodesBySubtree:!0,hiddenNodes:{}},k=["#5c8af5","#f5975c","#5cf5a8","#f55c8a","#c85cf5","#f5e05c","#5cd7f5","#f55c5c"];function O(o){return k[o%k.length]}function D(o){let i=Object.assign({},N,o);i.properties=Array.isArray(i.properties)?i.properties.map(n=>{let a=n&&n.label,l={property:n&&n.property||"",color:n&&n.color||d,visible:!(n&&n.visible===!1)};return a&&(l.label=a),l}).filter(n=>!!n.property):[];let e=o==null?void 0:o.propertyName,t=o==null?void 0:o.propertyNodeColor,r=!1;return e&&!i.properties.some(n=>n.property===e)&&i.properties.unshift({property:e,color:t||d}),o&&("propertyName"in o||"propertyNodeColor"in o)&&(r=!0),{settings:i,migrated:r}}var v=class{constructor(){this.children=new Map;this.parent=new Map;this.collapsed=new Set;this.indirect=new Map}reset(){this.children.clear(),this.parent.clear(),this.collapsed.clear(),this.indirect.clear()}addChild(i,e){if(i===e)return;this.children.has(i)||this.children.set(i,[]);let t=this.children.get(i);t.includes(e)||t.push(e),this.parent.set(e,i)}getChildren(i){return this.children.get(i)||[]}getDescendants(i){let e=[],t=new Set,r=[...this.getChildren(i)];for(;r.length;){let n=r.pop();t.has(n)||(t.add(n),e.push(n),r.push(...this.getChildren(n)))}return e}computeCollapsed(i){this.collapsed.clear();for(let[e,t]of this.children.entries()){let r=this.getDescendants(e);t.length&&r.length&&r.every(n=>i[n])&&this.collapsed.add(e)}}computeIndirectCounts(i){this.indirect.clear();let e=(t,r=new Set)=>{if(r.has(t))return 0;r.add(t);let n=this.getChildren(t),a=0,l=0;for(let s of n)i[s]||(l++,a+=1+e(s,new Set(r)));return this.indirect.set(t,Math.max(0,a-l)),a};for(let t of this.children.keys())e(t)}};var h=require("obsidian");var b=class extends h.PluginSettingTab{constructor(i,e){super(i,e),this.plugin=e}display(){let{containerEl:i}=this;i.empty(),i.createEl("p",{text:"Choose one or more frontmatter properties. Each unique value of each property becomes a virtual, colour-coded node in Obsidian's graph."});let e=this.plugin.getAvailableProperties(),t=new Set(this.plugin.settings.properties.map(r=>r.property));new h.Setting(i).setName("Properties").setHeading(),this.plugin.settings.properties.length||i.createEl("p",{text:'No properties added yet. Use "Add property" below to start grouping notes.',cls:"setting-item-description"}),this.plugin.settings.properties.forEach((r,n)=>{let a=new h.Setting(i).setName(r.property?u(r):`Property ${n+1}`).addDropdown(s=>{s.addOption("","\u2014 Select a property \u2014");for(let p of e)(p===r.property||!t.has(p))&&s.addOption(p,p);s.setValue(r.property||""),s.onChange(async p=>{this.plugin.settings.properties[n].property=p,await this.plugin.saveSettings(),this.plugin.refreshGraphLeaves(),this.display()})}).addText(s=>s.setPlaceholder(r.property||"Display name").setValue(r.label||"").onChange(async p=>{this.plugin.settings.properties[n].label=p,a.setName(u(this.plugin.settings.properties[n])),await this.plugin.saveSettings(),this.plugin.refreshGraphLeaves(!1),this.plugin.refreshGraphControls()})).addColorPicker(s=>s.setValue(r.color||d).onChange(async p=>{this.plugin.settings.properties[n].color=p,await this.plugin.saveSettings(),this.plugin.refreshGraphLeaves()})).addToggle(s=>s.setTooltip("Show this property's nodes in the graph").setValue(r.visible!==!1).onChange(async p=>{this.plugin.settings.properties[n].visible=p,await this.plugin.saveSettings(),this.plugin.refreshGraphLeaves(),this.plugin.refreshGraphControls()})).addExtraButton(s=>s.setIcon("trash").setTooltip("Remove this property").onClick(async()=>{this.plugin.settings.properties.splice(n,1),await this.plugin.saveSettings(),this.plugin.refreshGraphLeaves(),this.plugin.refreshGraphControls(),this.display()}));a.settingEl.addClass("p2g-property-row");let l=a.controlEl.querySelector('input[type="text"]');l&&l.addClass("p2g-property-label-input")}),this.plugin.settings.properties.length&&i.createEl("p",{text:"Display name renames how a property appears in this settings page and in the graph's Filters panel. It never changes the underlying frontmatter key used for searching.",cls:"setting-item-description"}),new h.Setting(i).addButton(r=>r.setButtonText("Add property").setCta().onClick(async()=>{this.plugin.settings.properties.push({property:"",color:O(this.plugin.settings.properties.length)}),await this.plugin.saveSettings(),this.display()})),new h.Setting(i).setName("Graph display").setHeading(),new h.Setting(i).setName("Show property nodes").setDesc("Show or hide the virtual nodes representing property values.").addToggle(r=>r.setValue(this.plugin.settings.showPropertyNodes).onChange(async n=>{this.plugin.settings.showPropertyNodes=n,await this.plugin.saveSettings(),this.plugin.refreshGraphLeaves()})),new h.Setting(i).setName("Weight nodes by descendants").setDesc("Make property nodes larger when they contain many notes, including indirect descendants.").addToggle(r=>r.setValue(this.plugin.settings.weightNodesBySubtree).onChange(async n=>{this.plugin.settings.weightNodesBySubtree=n,await this.plugin.saveSettings(),this.plugin.refreshGraphLeaves(!1)})),new h.Setting(i).setName("Reset folded nodes").setDesc("Reveal all notes hidden by Shift+click folding.").addButton(r=>r.setButtonText("Unfold all").onClick(()=>this.plugin.unfoldAll())),new h.Setting(i).setName("Refresh available properties").setDesc("Rebuild this settings page after adding new properties to notes.").addButton(r=>r.setButtonText("Refresh").onClick(()=>this.display()))}};var P=require("obsidian"),y="properties2graph_node",E="p2g:";function J(o,i){return E+encodeURIComponent(o)+":"+encodeURIComponent(i)}function F(o){try{let i=o.split(":");return decodeURIComponent(i.slice(2).join(":"))}catch(i){return o}}function S(o){try{let i=o.split(":");return decodeURIComponent(i[1])}catch(i){return""}}function x(o){return String(o).trim()}function R(o){if(Array.isArray(o)){let i=o,e=[];for(let t of i)e.push(...R(t));return e}if(o==null)return[];if(typeof o=="object"){let i=o.path;if(typeof i=="string"||typeof i=="number"||typeof i=="boolean")return[x(i)];try{return[JSON.stringify(o)]}catch(e){return[]}}if(typeof o=="string"||typeof o=="number"||typeof o=="boolean"){let i=x(o);return i?[i]:[]}return[]}function X(o,i){let e=i.split("#")[0],t=o.vault.getAbstractFileByPath(e);if(t instanceof P.TFile&&t.extension==="md"||!e.endsWith(".md")&&(t=o.vault.getAbstractFileByPath(e+".md"),t instanceof P.TFile&&t.extension==="md"))return t;let r=o.metadataCache.getFirstLinkpathDest(e,"");return r instanceof P.TFile&&r.extension==="md"?r:null}function I(o,i,e,t){let r={propertyNodeIds:new Set,nodeLabels:new Map,nodeProperty:new Map,allNodeIds:new Set};e.reset();let n=(i.properties||[]).filter(s=>s&&s.property&&s.visible!==!1);if(!i.showPropertyNodes||!n.length||!t||!t.nodes)return r;let a=[],l=Object.entries(t.nodes).filter(s=>{let[,p]=s;return typeof p=="object"&&p!==null});for(let[s,p]of l){if(p.type===y||s.startsWith(E)||p.type==="tag"||p.type==="unresolved")continue;let c=X(o,s);if(!c)continue;let f=o.metadataCache.getFileCache(c),g=f==null?void 0:f.frontmatter,m=g&&typeof g=="object"?g:void 0;if(m)for(let{property:w}of n){if(!(w in m))continue;let U=R(m[w]);for(let _ of U)a.push([J(w,_),_,s,w])}}for(let[s,p,,c]of a)t.nodes[s]||(t.nodes[s]={type:y,links:{},propertyNode:!0}),r.propertyNodeIds.add(s),r.nodeLabels.set(s,p),r.nodeProperty.set(s,c);for(let[s,,p]of a)t.nodes[s]&&t.nodes[p]&&(t.nodes[s].links[p]=!0,e.addChild(s,p));return r.allNodeIds=new Set(Object.keys(t.nodes)),e.computeCollapsed(i.hiddenNodes),e.computeIndirectCounts(i.hiddenNodes),Q(i,t),r}function Q(o,i){for(let e of Object.keys(o.hiddenNodes))delete i.nodes[e];for(let e of Object.values(i.nodes)){if(typeof e!="object"||e===null)continue;let t=e;if(!(!t.links||typeof t.links!="object"))for(let r of Object.keys(t.links))o.hiddenNodes[r]&&delete t.links[r]}}function A(o){let i=String(o||d).replace("#",""),e=parseInt(i,16);return Number.isFinite(e)?e:A(d)}function j(o,i){if(!o||!o.nodes||!o.nodes.length)return;let e=Object.getPrototypeOf(o.nodes[0]);!e||e.__p2gPatched||(e.__p2gPatched=!0,typeof e.getFillColor=="function"&&(e.__p2gOriginalGetFillColor=e.getFillColor,e.getFillColor=function(){if(this.type===y){let t=i.nodeProperty.get(this.id)||S(this.id);return{a:1,rgb:A(i.getColorForProperty(t))}}return e.__p2gOriginalGetFillColor.call(this)}),typeof e.getDisplayText=="function"&&(e.__p2gOriginalGetDisplayText=e.getDisplayText,e.getDisplayText=function(){if(this.type===y){let t=this.id;return i.nodeLabels.get(t)||t}return e.__p2gOriginalGetDisplayText.call(this)}),typeof e.getSize=="function"&&(e.__p2gOriginalGetSize=e.getSize,e.getSize=function(){if(!i.weightNodesBySubtree())return e.__p2gOriginalGetSize.call(this);let t=i.hierarchy.indirect.get(this.id)||0;if(!t||typeof this.weight!="number")return e.__p2gOriginalGetSize.call(this);let r=this.weight;this.weight=r+t;try{return e.__p2gOriginalGetSize.call(this)}finally{this.weight=r}}))}function M(o){for(let i of o.getLeavesOfType("graph")){let e=i.view.renderer;if(!e||!e.nodes||!e.nodes.length)continue;let t=Object.getPrototypeOf(e.nodes[0]);!t||!t.__p2gPatched||(t.__p2gOriginalGetFillColor&&(t.getFillColor=t.__p2gOriginalGetFillColor),t.__p2gOriginalGetDisplayText&&(t.getDisplayText=t.__p2gOriginalGetDisplayText),t.__p2gOriginalGetSize&&(t.getSize=t.__p2gOriginalGetSize),delete t.__p2gOriginalGetFillColor,delete t.__p2gOriginalGetDisplayText,delete t.__p2gOriginalGetSize,delete t.__p2gPatched)}}var V=require("obsidian");var B="p2g-control-row",Z="mod-filter";function ee(o){var e;let i=Array.from(o.querySelectorAll(".graph-control-section"));for(let t of i)if(t.classList.contains(Z))return t;for(let t of i){let r=t.querySelector(".graph-control-section-header");if(((e=r==null?void 0:r.textContent)==null?void 0:e.trim().toLowerCase())==="filters")return t}return null}function te(o){return o.querySelector(".tree-item-children")}function L(o){o.querySelectorAll("."+B).forEach(i=>i.remove())}function H(o,i){let e=o.containerEl;if(!e)return!1;L(e);let t=i.getProperties();if(!t.length)return!0;let r=ee(e);if(!r)return!1;let n=te(r);if(!n)return!1;for(let a of t)if(a.property)try{re(n,a,i)}catch(l){}return!0}function re(o,i,e){let t=o.createDiv({cls:B});new V.Setting(t).setName(u(i)).addToggle(r=>r.setValue(e.isVisible(i.property)).onChange(n=>{e.setVisible(i.property,n)}))}var T=class extends G.Plugin{constructor(){super(...arguments);this.propertyNodeIds=new Set;this.nodeLabels=new Map;this.nodeProperty=new Map;this.allNodeIds=new Set;this.hierarchy=new v;this.refreshTimer=null;this.originalOpenLinkText=null;this.shiftHeld=!1;this.onKeyDown=e=>{e.key==="Shift"&&(this.shiftHeld=!0)};this.onKeyUp=e=>{e.key==="Shift"&&(this.shiftHeld=!1)};this.onBlur=()=>{this.shiftHeld=!1}}async onload(){await this.loadSettings(),window.addEventListener("keydown",this.onKeyDown,!0),window.addEventListener("keyup",this.onKeyUp,!0),window.addEventListener("blur",this.onBlur,!0),this.addSettingTab(new b(this.app,this)),this.addCommand({id:"toggle-property-nodes",name:"Toggle property nodes",callback:async()=>{this.settings.showPropertyNodes=!this.settings.showPropertyNodes,await this.saveSettings(),this.refreshGraphLeaves()}}),this.addCommand({id:"unfold-all-property-graph-nodes",name:"Unfold all property graph nodes",callback:()=>this.unfoldAll()});let e=()=>this.scheduleRefresh();this.registerEvent(this.app.vault.on("create",e)),this.registerEvent(this.app.vault.on("delete",e)),this.registerEvent(this.app.vault.on("rename",e)),this.registerEvent(this.app.metadataCache.on("changed",e)),this.registerEvent(this.app.workspace.on("layout-change",()=>{this.refreshGraphLeaves(!1),this.refreshGraphControls()})),this.registerEvent(this.app.workspace.on("active-leaf-change",()=>{this.refreshGraphLeaves(!1),this.refreshGraphControls()})),this.wrapOpenLinkText(),this.refreshGraphLeaves(),this.refreshGraphControls()}onunload(){this.unwrapOpenLinkText(),window.removeEventListener("keydown",this.onKeyDown,!0),window.removeEventListener("keyup",this.onKeyUp,!0),window.removeEventListener("blur",this.onBlur,!0),this.refreshTimer&&window.clearTimeout(this.refreshTimer);for(let e of this.app.workspace.getLeavesOfType("graph")){let t=e.view,r=t.renderer;r&&r.__p2gOriginalSetData&&(r.setData=r.__p2gOriginalSetData,delete r.__p2gOriginalSetData),t.containerEl&&L(t.containerEl)}M(this.app.workspace)}async loadSettings(){let t=await this.loadData(),{settings:r,migrated:n}=D(t);this.settings=r,n&&await this.saveSettings()}async saveSettings(){await this.saveData(this.settings)}scheduleRefresh(){this.refreshTimer&&window.clearTimeout(this.refreshTimer),this.refreshTimer=window.setTimeout(()=>{this.refreshTimer=null,this.refreshGraphLeaves()},350)}getAvailableProperties(){let e=new Set;for(let t of this.app.vault.getMarkdownFiles()){let r=this.app.metadataCache.getFileCache(t);if(!(!r||!r.frontmatter))for(let n of Object.keys(r.frontmatter))n.startsWith("position")||e.add(n)}return Array.from(e).sort((t,r)=>t.localeCompare(r))}getColorForProperty(e){var r;let t=this.settings.properties.find(n=>n.property===e);return t&&t.color||((r=N.properties[0])==null?void 0:r.color)||"#5c8af5"}refreshGraphLeaves(e=!0){let t=this.app.workspace.getLeavesOfType("graph");for(let r of t){let n=r.view,a=n.renderer;if(!(!a||!(e||!a.__p2gOriginalSetData))){this.installInjector(a),this.patchNodePrototypeFor(a);try{let s=n.dataEngine&&n.dataEngine.getOptions?n.dataEngine.getOptions():void 0;n.unload(),n.load(),s!==void 0&&n.dataEngine&&n.dataEngine.setOptions&&n.dataEngine.setOptions(s),n.renderer&&this.patchNodePrototypeFor(n.renderer)}catch(s){console.error("Properties to Graph: graph refresh failed",s)}n.renderer&&n.renderer.changed&&n.renderer.changed()}}this.refreshGraphControls()}refreshGraphControls(){let e=this.app.workspace.getLeavesOfType("graph");for(let t of e){let r=t.view;if(r.containerEl)try{H(r,{getProperties:()=>this.settings.properties,isVisible:n=>{let a=this.settings.properties.find(l=>l.property===n);return!a||a.visible!==!1},setVisible:(n,a)=>{let l=this.settings.properties.find(s=>s.property===n);l&&(l.visible=a,this.saveSettings().then(()=>this.refreshGraphLeaves()))}})}catch(n){console.error("Properties to Graph: graph controls injection failed",n)}}}installInjector(e){e.__p2gOriginalSetData||(e.__p2gOriginalSetData=e.setData.bind(e));let t=this.handleSetData.bind(this);e.setData=function(r){return t(this,r)}}handleSetData(e,t){if(!e.__p2gOriginalSetData)return;try{let n=I(this.app,this.settings,this.hierarchy,t);this.propertyNodeIds=n.propertyNodeIds,this.allNodeIds=n.allNodeIds,this.nodeLabels.clear(),n.nodeLabels.forEach((a,l)=>this.nodeLabels.set(l,a)),this.nodeProperty.clear(),n.nodeProperty.forEach((a,l)=>this.nodeProperty.set(l,a))}catch(n){console.error("Properties to Graph: injection failed",n)}let r=e.__p2gOriginalSetData(t);return this.patchNodePrototypeFor(e),r}patchNodePrototypeFor(e){j(e,{getColorForProperty:t=>this.getColorForProperty(t),nodeLabels:this.nodeLabels,nodeProperty:this.nodeProperty,hierarchy:this.hierarchy,weightNodesBySubtree:()=>this.settings.weightNodesBySubtree})}wrapOpenLinkText(){if(this.originalOpenLinkText)return;let e=this.app.workspace;this.originalOpenLinkText=e.openLinkText.bind(e),e.openLinkText=(t,r,n,a)=>{var s,p,c;let l=((c=(p=(s=this.app.workspace.getMostRecentLeaf())==null?void 0:s.view)==null?void 0:p.getViewType)==null?void 0:c.call(p))==="graph";return this.shiftHeld&&l&&this.allNodeIds.has(t)?(this.handleFoldToggle(t),Promise.resolve()):this.propertyNodeIds.has(t)?(this.applyPropertyFilter(t),Promise.resolve()):this.originalOpenLinkText(t,r,n,a)}}unwrapOpenLinkText(){this.originalOpenLinkText&&(this.app.workspace.openLinkText=this.originalOpenLinkText,this.originalOpenLinkText=null,this.shiftHeld=!1,this.hierarchy.reset(),this.allNodeIds=new Set,this.propertyNodeIds=new Set,this.nodeLabels=new Map,this.nodeProperty=new Map)}applyPropertyFilter(e){var g;let t=this.app.workspace.getMostRecentLeaf(),r=t&&t.view,n=r&&r.dataEngine,a=n&&n.filterOptions&&n.filterOptions.search;if(!a||typeof a.setValue!="function"){new G.Notice("Properties to Graph: graph search API is unavailable.");return}let l=this.nodeLabels.get(e)||F(e),s=this.nodeProperty.get(e)||S(e);if(!s||!l)return;let p=String(s).replace(/([\\[\]:"])/g,"\\$1"),c=String(l).replace(/([\\"])/g,"\\$1"),f="["+p+':"'+c+'"]';a.setValue(f),n&&typeof n.updateSearch=="function"?n.updateSearch():n&&n.requestUpdateSearch&&typeof n.requestUpdateSearch.run=="function"&&n.requestUpdateSearch.run();try{(g=r==null?void 0:r.showSearch)==null||g.call(r)}catch(m){}}handleFoldToggle(e){let t=this.hierarchy.getChildren(e);if(!t.length)return;let r=this.hierarchy.getDescendants(e);if(!r.length)return;if(r.every(a=>this.settings.hiddenNodes[a]))for(let a of t)delete this.settings.hiddenNodes[a];else for(let a of r)this.settings.hiddenNodes[a]=!0;this.saveSettings().then(()=>this.refreshGraphLeaves())}unfoldAll(){this.settings.hiddenNodes={},this.saveSettings().then(()=>this.refreshGraphLeaves())}};