Files
fflow/main.js
T

6 lines
13 KiB
JavaScript

/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
*/
var P=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var X=(n,s)=>{for(var t in s)P(n,t,{get:s[t],enumerable:!0})},Z=(n,s,t,e)=>{if(s&&typeof s=="object"||typeof s=="function")for(let o of J(s))!Q.call(n,o)&&o!==t&&P(n,o,{get:()=>s[o],enumerable:!(e=G(s,o))||e.enumerable});return n};var ee=n=>Z(P({},"__esModule",{value:!0}),n);var re={};X(re,{default:()=>M});module.exports=ee(re);var U=require("obsidian");var I=require("obsidian");var H=require("obsidian");async function b(n,s,t){let e=n.workspace.getActiveViewOfType(H.MarkdownView);(e==null?void 0:e.file)===s&&await e.save(),await n.fileManager.processFrontMatter(s,t)}function K(n,s,t,e){if(!e){n[s]=t;return}let o=V(n[s],s);o.includes(t)||o.push(t),n[s]=o}function x(n,s,t,e){var p;s[t.property]=e;let o=t.steps.find(l=>l.value===e),r=new Map,i=l=>{let d=r.get(l);return d===void 0&&r.set(l,d=te(n,s,l)),d},a=l=>l.property!==""&&l.property!==t.property;for(let l of t.steps)if(l!==o)for(let d of l.effects.filter(a)){let c=d.property;if(i(c)){let m=new Set($(d.value)),g=V(s[c],c).filter(h=>!m.has(h));g.length?s[c]=g:delete s[c]}else c in s&&String(s[c])===String(j(d.value))&&delete s[c]}for(let l of(p=o==null?void 0:o.effects.filter(a))!=null?p:[]){let d=l.property;if(i(d)){let c=V(s[d],d);for(let m of $(l.value))c.includes(m)||c.push(m);c.length&&(s[d]=c)}else s[d]=j(l.value)}}function te(n,s,t){if(["cssclasses","tags","aliases"].includes(t)||Array.isArray(s[t]))return!0;let e=Reflect.get(n,"metadataTypeManager");if(!e||typeof e!="object"||!("getPropertyInfo"in e)||typeof e.getPropertyInfo!="function")return!1;let o=e.getPropertyInfo(t),r=o&&typeof o=="object"&&"widget"in o?o.widget:null;return r==="multitext"||r==="tags"||r==="aliases"}function V(n,s){return Array.isArray(n)?n.map(String):n==null||n===""?[]:s==="cssclasses"?String(n).split(/[\s,]+/).filter(Boolean):[String(n)]}function $(n){return n.split(",").map(s=>s.trim()).filter(Boolean)}function j(n){let s=n.trim();return s===""?null:s==="true"||s==="false"?s==="true":/^-?\d+(\.\d+)?$/.test(s)?Number(s):s}var C=class{constructor(s){this.plugin=s;this.registered=[]}sync(){for(let s of this.registered)this.plugin.removeCommand(s);this.registered=[];for(let s of this.plugin.settings.flows)s.kind==="ordered"&&(this.add(s,1,"next","next step"),this.add(s,-1,"previous","previous step"))}add(s,t,e,o){let r=`flow-${s.id}-${e}`;this.registered.push(r),this.plugin.addCommand({id:r,name:`${s.name||s.property||"Unnamed flow"}: ${o}`,checkCallback:i=>{let a=this.plugin.app.workspace.getActiveFile();return!a||a.extension!=="md"||!s.property||!s.steps.some(p=>p.value)?!1:(i||se(this.plugin,a,s,t),!0)}})}};async function se(n,s,t,e){let o=t.steps.filter(i=>i.value),r="";try{await b(n.app,s,i=>{let a=o.findIndex(l=>l.value===String(i[t.property])),p=a===-1?0:a+e;if(p<0||p>=o.length){if(!t.loop){r=`${t.property}: already at the ${e===1?"last":"first"} step`;return}p=(p+o.length)%o.length}x(n.app,i,t,o[p].value),r=`${t.property}: ${o[p].value}`}),new I.Notice(r)}catch(i){console.error("FFLOW: could not move flow step",i),new I.Notice(`FFLOW: could not set "${t.property}". See console.`)}}function v(n){return{id:Date.now().toString(36)+Math.random().toString(36).slice(2,6),name:"",symbol:"",property:"",kind:"ordered",valueMode:"single",loop:!1,steps:[],...n}}function ne(){return{flows:[v({name:"Type",symbol:"@",property:"type",kind:"open"})]}}var k=["#","[","/",":"];function S(n,s,t){return n===""?null:[...n].length!==1?"Use exactly one character.":/[\p{L}\p{N}\s]/u.test(n)?"Letters, digits and spaces are not allowed.":k.includes(n)?`"${n}" is reserved (${k.join(" ")}).`:s.some((e,o)=>o!==t&&e.symbol===n)?`"${n}" is already used by another flow.`:null}function W(n){var o,r;if(!n||typeof n!="object")return ne();if("flows"in n&&!("rules"in n)&&Array.isArray(n.flows))return{flows:n.flows.map(i=>v(i))};let s=n,t=(o=s.rules)!=null?o:[],e=((r=s.flows)!=null?r:[]).map(i=>{var a,p;return v({id:i.id,name:i.name,property:i.property,loop:i.loop,symbol:(p=(a=t.find(l=>l.source==="flow"&&l.flowId===i.id))==null?void 0:a.symbol)!=null?p:"",steps:i.steps.map(l=>({value:l.value,effects:l.cssclass.trim()?[{property:"cssclasses",value:l.cssclass.trim().split(/\s+/).join(", ")}]:[]}))})});for(let i of t)i.source==="open"&&e.push(v({name:i.property,symbol:i.symbol,property:i.property,kind:"open",valueMode:i.valueMode})),i.source==="finite"&&e.push(v({name:i.property,symbol:i.symbol,property:i.property,steps:i.values.map(a=>({value:a,effects:[]}))}));return{flows:e}}var u=require("obsidian");var E=class extends u.PluginSettingTab{constructor(t,e){super(t,e);this.plugin=e;this.expanded=new Set;this.save=()=>this.plugin.saveSettings()}async saveAndRedraw(){await this.save(),this.display()}display(){let{containerEl:t}=this,e=this.plugin.settings.flows;t.empty(),new u.Setting(t).setHeading().setName("Flows").setDesc("Type a flow's symbol at the start of a word to pick a value. Ordered flows also get next/previous step commands.").addButton(r=>r.setButtonText("Hotkeys").onClick(()=>this.openHotkeys())).addButton(r=>r.setButtonText("Add flow").setCta().onClick(async()=>{let i=v({steps:[{value:"",effects:[]}]});e.push(i),this.expanded.add(i.id),await this.saveAndRedraw()}));let o=t.createDiv({cls:"fflow-list"});e.forEach((r,i)=>this.renderFlow(o,r,i)),e.length===0&&o.createDiv({cls:"fflow-empty",text:"No flows yet."})}renderFlow(t,e,o){var R;let r=this.plugin.settings.flows,i=t.createDiv({cls:"fflow-card"});i.toggleClass("is-collapsed",!this.expanded.has(e.id));let a=i.createDiv({cls:"fflow-card-header"});(0,u.setIcon)(a.createSpan({cls:"fflow-chevron"}),"chevron-right");let p=a.createSpan({cls:"fflow-badge"}),l=a.createSpan({cls:"fflow-title"}),d=a.createSpan({cls:"fflow-summary"}),c=()=>{p.setText(e.symbol),p.toggle(e.symbol!==""),l.setText(e.name||e.property||"New flow");let f=e.steps.map(w=>w.value).filter(Boolean);d.setText([e.property,e.kind==="open"?"open list":f.join(" \u2192 ")||"no steps"].filter(Boolean).join(" \xB7 "))};c(),a.addEventListener("click",()=>{let f=!this.expanded.has(e.id);f?this.expanded.add(e.id):this.expanded.delete(e.id),i.toggleClass("is-collapsed",!f)}),new u.ExtraButtonComponent(a.createDiv({cls:"fflow-header-actions"})).setIcon("trash-2").setTooltip("Delete flow").onClick(async()=>{r.splice(o,1),this.expanded.delete(e.id),await this.saveAndRedraw()}).extraSettingsEl.addEventListener("click",f=>f.stopPropagation());let g=i.createDiv({cls:"fflow-card-body"}),h=g.createDiv({cls:"fflow-fields"}),y=(f,w="")=>{let B=h.createDiv({cls:`fflow-field ${w}`});return B.createDiv({cls:"fflow-label",text:f}),B};new u.TextComponent(y("Name","is-wide")).setPlaceholder("Status").setValue(e.name).onChange(async f=>{e.name=f,c(),await this.save()});let Y=y("Symbol","is-narrow"),F=new u.TextComponent(Y).setPlaceholder("\u2014").setValue(e.symbol);F.inputEl.addClass("fflow-symbol"),(0,u.setTooltip)(F.inputEl,`Optional. One character, not a letter or digit. Reserved: ${k.join(" ")}`);let z=g.createDiv({cls:"fflow-error",text:(R=S(e.symbol,r,o))!=null?R:""});if(F.onChange(async f=>{let w=S(f,r,o);z.setText(w!=null?w:""),F.inputEl.toggleClass("is-invalid",w!==null),!w&&(e.symbol=f,c(),await this.save())}),new u.TextComponent(y("Property","is-wide")).setPlaceholder("status").setValue(e.property).onChange(async f=>{e.property=f.trim(),c(),await this.save()}),new u.DropdownComponent(y("Kind")).addOptions({ordered:"Ordered list",open:"Open (vault)"}).setValue(e.kind).onChange(async f=>{e.kind=f,await this.saveAndRedraw()}),e.kind==="open"){new u.DropdownComponent(y("Values")).addOptions({single:"Single value",list:"List"}).setValue(e.valueMode).onChange(async f=>{e.valueMode=f,await this.save()}),g.createDiv({cls:"fflow-hint",text:"Suggests every value already used for this property in the vault. New values are allowed. No steps or hotkeys."});return}new u.ToggleComponent(y("Loop","is-toggle")).setValue(e.loop).setTooltip("After the last step, \u201Cnext\u201D goes back to the first").onChange(async f=>{e.loop=f,await this.save()});let O=g.createDiv({cls:"fflow-steps"}),A=O.createDiv({cls:"fflow-step-row fflow-steps-head"});A.createSpan(),A.createSpan({text:"Value"}),A.createSpan({text:"Also sets"}),e.steps.forEach((f,w)=>this.renderStep(O,e,f,w,c)),q(g.createDiv({cls:"fflow-add-step"}),"Add step",async()=>{e.steps.push({value:"",effects:[]}),await this.saveAndRedraw()})}renderStep(t,e,o,r,i){let a=t.createDiv({cls:"fflow-step-row"});a.createSpan({cls:"fflow-step-num",text:String(r+1)}),new u.TextComponent(a.createDiv({cls:"fflow-step-value"})).setPlaceholder("value").setValue(o.value).onChange(async c=>{o.value=c.trim(),i(),await this.save()});let p=a.createDiv({cls:"fflow-effects"});o.effects.forEach((c,m)=>{let g=p.createDiv({cls:"fflow-effect"});new u.TextComponent(g).setPlaceholder("property").setValue(c.property).onChange(async h=>{c.property=h.trim(),await this.save()}),g.createSpan({cls:"fflow-equals",text:"="}),new u.TextComponent(g).setPlaceholder("value (lists: a, b)").setValue(c.value).onChange(async h=>{c.value=h,await this.save()}),new u.ExtraButtonComponent(g).setIcon("minus").setTooltip("Remove this property").onClick(async()=>{o.effects.splice(m,1),await this.saveAndRedraw()})}),q(p,"property",async()=>{o.effects.push({property:"",value:""}),await this.saveAndRedraw()});let l=a.createDiv({cls:"fflow-step-actions"}),d=async c=>{e.steps.splice(c,0,...e.steps.splice(r,1)),await this.saveAndRedraw()};new u.ExtraButtonComponent(l).setIcon("arrow-up").setTooltip("Move up").setDisabled(r===0).onClick(()=>d(r-1)),new u.ExtraButtonComponent(l).setIcon("arrow-down").setTooltip("Move down").setDisabled(r===e.steps.length-1).onClick(()=>d(r+1)),new u.ExtraButtonComponent(l).setIcon("trash-2").setTooltip("Delete step").onClick(async()=>{e.steps.splice(r,1),await this.saveAndRedraw()})}openHotkeys(){let t=Reflect.get(this.app,"setting");if(!t||typeof t!="object"||!("openTabById"in t)||typeof t.openTabById!="function")return;let e=t.openTabById("hotkeys"),o=e&&typeof e=="object"&&"searchComponent"in e?e.searchComponent:null;o instanceof u.SearchComponent&&(o.setValue(this.plugin.manifest.name),o.inputEl.dispatchEvent(new Event("input")))}};function q(n,s,t){let e=n.createEl("button",{cls:"fflow-add clickable-icon"});(0,u.setIcon)(e.createSpan(),"plus"),e.createSpan({text:s}),e.addEventListener("click",()=>void t())}var D=require("obsidian");var L=class{constructor(s){this.app=s;this.cache=new Map}invalidate(){this.cache.clear()}get(s){var e,o,r;let t=this.cache.get(s);if(t)return t;t=new Map;for(let i of this.app.vault.getMarkdownFiles()){let a=(o=(e=this.app.metadataCache.getFileCache(i))==null?void 0:e.frontmatter)==null?void 0:o[s];for(let p of new Set(_(a)))t.set(p,((r=t.get(p))!=null?r:0)+1)}return this.cache.set(s,t),t}};function _(n){return Array.isArray(n)?n.flatMap(_):typeof n=="string"?n.trim()?[n.trim()]:[]:typeof n=="number"||typeof n=="boolean"?[String(n)]:[]}var N=n=>n!==void 0&&/\s/.test(n),T=class extends D.EditorSuggest{constructor(t,e,o){super(t);this.plugin=e;this.index=o;this.flow=null}onTrigger(t,e,o){let r=e.getLine(t.line).slice(0,t.ch),i=r.length;for(;i>0&&!N(r[i-1]);)i--;let a=r.slice(i);if(!a)return null;let p=this.plugin.settings.flows,l=p.findIndex(c=>c.symbol!==""&&c.symbol===a[0]),d=p[l];return!d||!d.property||S(d.symbol,p,l)||oe(e,t.line)||ie(e,t.line,r.slice(0,i))?null:(this.flow=d,{start:{line:t.line,ch:i},end:t,query:a.slice(1)})}getSuggestions(t){let e=this.flow;if(!e)return[];let o=t.query.toLowerCase(),r=e.kind==="ordered"?e.steps.filter(a=>a.value).map(a=>({value:a.value,isNew:!1})):[...this.index.get(e.property)].sort((a,p)=>p[1]-a[1]||a[0].localeCompare(p[0])).map(([a,p])=>({value:a,count:p,isNew:!1})),i=r.filter(a=>a.value.toLowerCase().includes(o));return i.sort((a,p)=>+!a.value.toLowerCase().startsWith(o)-+!p.value.toLowerCase().startsWith(o)),e.kind==="open"&&o&&!r.some(a=>a.value.toLowerCase()===o)&&i.push({value:t.query,isNew:!0}),i}renderSuggestion(t,e){if(e.addClass("fflow-suggestion"),t.isNew){e.createSpan({cls:"fflow-new",text:"New: "}),e.createSpan({text:t.value});return}e.createSpan({text:t.value}),t.count!==void 0&&e.createSpan({cls:"fflow-count",text:String(t.count)})}selectSuggestion(t,e){let o=this.context,r=this.flow;!o||!r||this.apply(o,r,t.value)}async apply(t,e,o){let{editor:r,file:i,start:a}=t,p=r.getLine(a.line),l=t.end.ch;for(;l<p.length&&!N(p[l]);)l++;N(p[l])&&l++,r.replaceRange("",a,{line:a.line,ch:l}),r.setCursor(a);try{await b(this.app,i,d=>{e.kind==="ordered"?x(this.app,d,e,o):K(d,e.property,o,e.valueMode==="list")})}catch(d){console.error("FFLOW: could not update frontmatter",d),new D.Notice(`FFLOW: could not set "${e.property}". See console.`)}}};function oe(n,s){if(n.getLine(0)!=="---")return!1;for(let t=1;t<n.lineCount();t++)if(n.getLine(t)==="---")return s<=t;return!1}function ie(n,s,t){var o,r;let e=0;for(let i=0;i<s;i++)/^\s*(```|~~~)/.test(n.getLine(i))&&e++;return e%2===1?!0:((r=(o=t.match(/`/g))==null?void 0:o.length)!=null?r:0)%2===1}var M=class extends U.Plugin{constructor(){super(...arguments);this.flowCommands=new C(this)}async onload(){let t=await this.loadData();this.settings=W(t),t&&typeof t=="object"&&"rules"in t&&await this.saveData(this.settings);let e=new L(this.app);this.registerEvent(this.app.metadataCache.on("changed",()=>e.invalidate())),this.registerEvent(this.app.metadataCache.on("deleted",()=>e.invalidate())),this.registerEditorSuggest(new T(this.app,this,e)),this.flowCommands.sync(),this.addSettingTab(new E(this.app,this))}async saveSettings(){await this.saveData(this.settings),this.flowCommands.sync()}};