-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
95 lines (94 loc) · 13.9 KB
/
Copy pathmain.js
File metadata and controls
95 lines (94 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var E=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var P=(d,e)=>{for(var t in e)E(d,t,{get:e[t],enumerable:!0})},B=(d,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of $(e))!A.call(d,o)&&o!==t&&E(d,o,{get:()=>e[o],enumerable:!(i=k(e,o))||i.enumerable});return d};var M=d=>B(E({},"__esModule",{value:!0}),d);var D={};P(D,{default:()=>S});module.exports=M(D);var m=require("obsidian");var h=require("obsidian"),b=class extends h.Modal{constructor(e,t,i,o){var r,n,a,c,l,p;if(super(e),this.file=t,this.settings=i,this.onSubmit=o,this.isFolder=t instanceof h.TFolder,this.isFolder){this.config=(r=i.folders[t.path])!=null?r:{bgColorStart:"#000000",bgColorEnd:"#000000",textColorStart:"#ffffff",textColorEnd:"#ffffff",bold:!1,italic:!1,applyToSubfolders:!0,applyToFiles:!0,applyBgColor:!0};let s=this.config;s.applyToSubfolders===void 0&&(s.applyToSubfolders=!0),s.applyToFiles===void 0&&(s.applyToFiles=!0)}else{let s=i.files[t.path];this.config={bgColor:(n=s==null?void 0:s.bgColor)!=null?n:"#1e40af",textColor:(a=s==null?void 0:s.textColor)!=null?a:"#ffffff",bold:(c=s==null?void 0:s.bold)!=null?c:!1,italic:(l=s==null?void 0:s.italic)!=null?l:!1,applyBgColor:(p=s==null?void 0:s.applyBgColor)!=null?p:!0}}}onOpen(){let{contentEl:e}=this;e.empty(),e.addClass("folder-color-modal"),e.createEl("h2",{text:`Assign color \u2014 ${this.file.name}`}),this.isFolder?this.renderFolderForm(e):this.renderFileForm(e),this.renderPresets(e),this.renderActions(e)}renderFolderForm(e){var i,o,r;let t=this.config;this.renderCheckbox(e,"Apply background color",(i=t.applyBgColor)!=null?i:!0,n=>{this.config.applyBgColor=n}),this.renderColorRow(e,"Background start",t.bgColorStart,n=>{this.config.bgColorStart=n}),this.renderColorRow(e,"Background end",t.bgColorEnd,n=>{this.config.bgColorEnd=n}),this.renderColorRow(e,"Text start",t.textColorStart,n=>{this.config.textColorStart=n}),this.renderColorRow(e,"Text end",t.textColorEnd,n=>{this.config.textColorEnd=n}),this.renderCheckbox(e,"Bold",t.bold,n=>{this.config.bold=n}),this.renderCheckbox(e,"Italic",t.italic,n=>{this.config.italic=n}),this.renderCheckbox(e,"Apply to subfolders",(o=t.applyToSubfolders)!=null?o:!0,n=>{this.config.applyToSubfolders=n}),this.renderCheckbox(e,"Apply to files",(r=t.applyToFiles)!=null?r:!0,n=>{this.config.applyToFiles=n})}renderFileForm(e){var i;let t=this.config;this.renderCheckbox(e,"Apply background color",(i=t.applyBgColor)!=null?i:!0,o=>{this.config.applyBgColor=o}),this.renderColorRow(e,"Background",t.bgColor,o=>{this.config.bgColor=o}),this.renderColorRow(e,"Text color",t.textColor,o=>{this.config.textColor=o}),this.renderCheckbox(e,"Bold",t.bold,o=>{this.config.bold=o}),this.renderCheckbox(e,"Italic",t.italic,o=>{this.config.italic=o})}renderColorRow(e,t,i,o){let r=e.createDiv({cls:"color-row"});r.createEl("label",{text:t});let n=r.createEl("input",{type:"color"});n.value=i;let a=r.createEl("span",{text:i,cls:"color-hex"});n.oninput=()=>{o(n.value),a.textContent=n.value}}renderCheckbox(e,t,i,o){let r=e.createDiv({cls:"checkbox-row"}),n=r.createEl("input",{type:"checkbox"});n.checked=i,n.onchange=()=>o(n.checked),r.createEl("label",{text:t})}renderPresets(e){if(!this.isFolder)return;let t=e.createDiv({cls:"presets-section"});if(t.createEl("h3",{text:"Presets"}),this.settings.presets.length>0){let n=t.createDiv({cls:"presets-list"});this.settings.presets.forEach((a,c)=>{let l=n.createEl("button",{text:a.name,cls:"preset-btn"});l.onclick=()=>{this.config={...a.config},this.onOpen()};let p=n.createEl("button",{text:"\xD7",cls:"preset-delete-btn"});p.onclick=()=>{this.settings.presets.splice(c,1),this.onOpen()}})}else t.createEl("p",{text:"No presets saved yet.",cls:"presets-empty"});let i=t.createDiv({cls:"preset-save-row"}),o=i.createEl("input",{type:"text",placeholder:"Preset name",cls:"preset-name-input"}),r=i.createEl("button",{text:"Save preset",cls:"preset-save-btn"});r.onclick=()=>{if(!o.value.trim()){new h.Notice("Please enter a preset name");return}this.settings.presets.push({name:o.value.trim(),config:{...this.config}}),new h.Notice(`Preset "${o.value.trim()}" saved`),o.value="",this.onOpen()}}renderActions(e){let t=e.createDiv({cls:"actions-section"}),i=t.createEl("button",{text:"Remove color",cls:"remove-btn"});i.onclick=()=>{this.file instanceof h.TFolder?delete this.settings.folders[this.file.path]:this.file instanceof h.TFile&&delete this.settings.files[this.file.path],this.onSubmit(null),this.close(),new h.Notice("Color removed")};let o=t.createEl("button",{text:"Apply",cls:"apply-btn"});o.onclick=()=>{this.onSubmit(this.config),this.close()}}onClose(){this.contentEl.empty()}};var F={folders:{},files:{},presets:[]};var x=class{constructor(e,t){this.app=e;this.settings=t}hexToRgb(e){var i,o,r;let t=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);return t?{r:parseInt((i=t[1])!=null?i:"0",16),g:parseInt((o=t[2])!=null?o:"0",16),b:parseInt((r=t[3])!=null?r:"0",16)}:null}rgbToHex(e,t,i){return"#"+[e,t,i].map(o=>o.toString(16).padStart(2,"0")).join("")}interpolateColor(e,t,i){let o=this.hexToRgb(e),r=this.hexToRgb(t);if(!o||!r)return e;let n=Math.round(o.r+(r.r-o.r)*i),a=Math.round(o.g+(r.g-o.g)*i),c=Math.round(o.b+(r.b-o.b)*i);return this.rgbToHex(n,a,c)}findParentConfig(e){let t=e.split("/");for(let i=t.length-1;i>=1;i--){let o=t.slice(0,i).join("/");if(this.settings.folders[o])return{path:o,config:this.settings.folders[o]}}return null}getColorForPath(e,t,i){let o=this.app.vault.getAllFolders(),r=e.split("/").length-t.split("/").length,n=1;for(let c of o){if(!c.path.startsWith(t+"/")||this.settings.folders[c.path])continue;let l=c.path.split("/"),p=t.split("/").length,s=!1;for(let g=p+1;g<l.length;g++){let u=l.slice(0,g).join("/");if(this.settings.folders[u]){s=!0;break}}if(s)continue;let C=c.path.split("/").length-t.split("/").length;C>n&&(n=C)}let a=Math.min(r/n,1);return{bg:this.interpolateColor(i.bgColorStart,i.bgColorEnd,a),text:this.interpolateColor(i.textColorStart,i.textColorEnd,a)}}generateStyles(){var o,r,n,a;let e="";for(let[c,l]of Object.entries(this.settings.folders)){if(!l||!l.bgColorStart)continue;let p=c.replace(/"/g,'\\"');e+=`
.nav-folder-title[data-path="${p}"] {
background: transparent !important;
color: ${l.textColorStart} !important;
font-weight: ${l.bold?"bold":"normal"} !important;
font-style: ${l.italic?"italic":"normal"} !important;
align-items: center !important;
}
.nav-folder-title[data-path="${p}"] .tree-item-icon svg {
stroke: ${l.textColorStart} !important;
}
.nav-folder-title[data-path="${p}"] > .tree-item-inner {
background: ${l.applyBgColor?l.bgColorStart:"transparent"} !important;
border-radius: 4px !important;
padding: 2px 8px !important;
flex: 1 1 0 !important;
min-width: 0 !important;
align-self: stretch !important;
margin-right: -8px !important;
}`}let t=this.app.vault.getAllFolders();for(let c of t){let l=c.path;if(this.settings.folders[l])continue;let p=this.findParentConfig(l);if(!p||!p.config.applyToSubfolders)continue;let{bg:s,text:C}=this.getColorForPath(l,p.path,p.config),g=p.config.applyBgColor?s:"transparent",u=l.replace(/"/g,'\\"'),w=p.config.bold?"bold":"normal",T=p.config.italic?"italic":"normal";e+=`
.nav-folder-title[data-path="${u}"] {
background: transparent !important;
color: ${C} !important;
font-weight: ${w} !important;
font-style: ${T} !important;
align-items: center !important;
}
.nav-folder-title[data-path="${u}"] .tree-item-icon svg {
stroke: ${C} !important;
}
.nav-folder-title[data-path="${u}"] > .tree-item-inner {
background: ${g} !important;
border-radius: 4px !important;
padding: 2px 8px !important;
flex: 1 1 0 !important;
min-width: 0 !important;
align-self: stretch !important;
margin-right: -8px !important;
}`}let i=this.app.vault.getFiles();for(let c of i){let l=c.path;if(this.settings.files[l]){let g=this.settings.files[l];if(!g)continue;e+=this.generateFileStyle(l,g.bgColor,g.textColor,g.bold,g.italic,(o=g.applyBgColor)!=null?o:!0);continue}let p=this.findParentConfig(l);if(!p||!p.config.applyToFiles)continue;let s=l.split("/").slice(0,-1).join("/"),C;if(this.settings.folders[s]){let g=this.settings.folders[s];C={bg:(r=g==null?void 0:g.bgColorStart)!=null?r:"#000000",text:(n=g==null?void 0:g.textColorStart)!=null?n:"#ffffff"}}else C=this.getColorForPath(s,p.path,p.config);e+=this.generateFileStyle(l,C.bg,C.text,p.config.bold,p.config.italic,(a=p.config.applyBgColor)!=null?a:!0)}return e}generateFileStyle(e,t,i,o,r,n=!0){let a=e.replace(/"/g,'\\"'),c=!e.endsWith(".md")&&e.includes(".");return`
.nav-file-title[data-path="${a}"] {
background: transparent !important;
color: ${i} !important;
font-weight: ${o?"bold":"normal"} !important;
font-style: ${r?"italic":"normal"} !important;
align-items: center !important;
}
.nav-file-title[data-path="${a}"] > .tree-item-inner {
background: ${n?t:"transparent"} !important;
border-radius: ${c?"4px 0 0 4px":"4px"} !important;
padding: 2px 8px !important;
flex: 0 1 auto !important;
min-width: 0 !important;
align-self: stretch !important;
margin-right: 0 !important;
}${c?`
.nav-file-title[data-path="${a}"] > .nav-file-tag {
background: ${n?t:"transparent"} !important;
color: ${i} !important;
border-radius: 0 4px 4px 0 !important;
align-self: stretch !important;
display: flex !important;
align-items: center !important;
margin-left: 0 !important;
margin-right: 0 !important;
padding-right: 4px !important;
}`:""}`}};var y=class{constructor(e){this.styleEl=null;this.app=e;let t=this.app.workspace.containerEl.doc,i=t.getElementById("folder-color-plugin-styles");i||(i=t.createElement("style"),i.id="folder-color-plugin-styles",t.head.appendChild(i)),this.styleEl=i}applyStyles(e){this.styleEl&&(this.styleEl.textContent=e)}removeStyles(){this.styleEl&&(this.styleEl.remove(),this.styleEl=null)}};var f=require("obsidian"),v=class extends f.PluginSettingTab{constructor(e,t){super(e,t),this.plugin=t}display(){let{containerEl:e}=this;e.empty(),new f.Setting(e).setName("Configuration").setHeading(),new f.Setting(e).setName("Export configuration").setDesc("Save all colors, presets and settings to a JSON file.").addButton(t=>t.setButtonText("Export").onClick(()=>{let i=JSON.stringify(this.plugin.settings,null,2),o=new Blob([i],{type:"application/json"}),r=URL.createObjectURL(o),n=window.activeDocument.createElement("a");n.href=r,n.download="folder-color-config.json",n.click(),URL.revokeObjectURL(r),new f.Notice("Configuration exported")})),new f.Setting(e).setName("Import configuration").setDesc("Load colors and presets from a previously exported JSON file.").addButton(t=>t.setButtonText("Import").onClick(()=>{let i=window.activeDocument.createElement("input");i.type="file",i.accept=".json",i.onchange=()=>{var r;let o=(r=i.files)==null?void 0:r[0];o&&o.text().then(async n=>{try{let a=JSON.parse(n);Object.assign(this.plugin.settings,a),await this.plugin.saveSettings(),this.plugin.applyAllStyles(),new f.Notice("Configuration imported successfully. Please reopen this tab to see changes.")}catch(a){new f.Notice("Error: invalid JSON file")}}).catch(()=>{new f.Notice("Error: could not read file")})},i.click()})),new f.Setting(e).setName("Presets").setHeading(),this.plugin.settings.presets.length===0&&e.createEl("p",{text:"No presets saved yet.",cls:"presets-empty"}),this.plugin.settings.presets.forEach((t,i)=>{let o=new f.Setting(e).setName(t.name),r=o.nameEl.createDiv();r.style.cssText=`
display: inline-flex;
align-items: center;
gap: 6px;
margin-left: 10px;
padding: 3px 10px;
border-radius: 4px;
background: ${t.config.bgColorStart};
color: ${t.config.textColorStart};
font-weight: ${t.config.bold?"bold":"normal"};
font-style: ${t.config.italic?"italic":"normal"};
font-size: 12px;
`,r.createSpan({text:"Aa"});let n=o.nameEl.createDiv();n.style.cssText=`
display: inline-flex;
align-items: center;
gap: 6px;
margin-left: 4px;
padding: 3px 10px;
border-radius: 4px;
background: ${t.config.bgColorEnd};
color: ${t.config.textColorEnd};
font-weight: ${t.config.bold?"bold":"normal"};
font-style: ${t.config.italic?"italic":"normal"};
font-size: 12px;
`,n.createSpan({text:"Aa"}),o.addButton(a=>{a.setButtonText("Delete"),a.buttonEl.addClass("mod-warning"),a.onClick(async()=>{this.plugin.settings.presets.splice(i,1),await this.plugin.saveSettings(),new f.Notice(`Preset "${t.name}" deleted`),o.settingEl.remove()})})})}};var S=class extends m.Plugin{async onload(){await this.loadSettings(),this.colorManager=new x(this.app,this.settings),this.styleManager=new y(this.app),this.addSettingTab(new v(this.app,this)),this.applyAllStyles(),this.registerEvent(this.app.workspace.on("file-menu",(e,t)=>{e.addItem(i=>{i.setTitle("Assign color").setIcon("palette").onClick(()=>{new b(this.app,t,this.settings,o=>{o===null?t instanceof m.TFolder?delete this.settings.folders[t.path]:t instanceof m.TFile&&delete this.settings.files[t.path]:t instanceof m.TFolder?this.settings.folders[t.path]=o:t instanceof m.TFile&&(this.settings.files[t.path]=o),this.saveSettings().then(()=>this.applyAllStyles())}).open()})})})),this.registerEvent(this.app.vault.on("create",()=>{this.applyAllStyles()})),this.registerEvent(this.app.vault.on("delete",e=>{e instanceof m.TFolder?delete this.settings.folders[e.path]:e instanceof m.TFile&&delete this.settings.files[e.path],this.saveSettings().then(()=>this.applyAllStyles())})),this.registerEvent(this.app.vault.on("rename",()=>{this.applyAllStyles()}))}onunload(){this.styleManager.removeStyles()}async loadSettings(){this.settings=Object.assign({},F,await this.loadData())}async saveSettings(){await this.saveData(this.settings)}applyAllStyles(){this.styleManager.applyStyles(this.colorManager.generateStyles())}};