()
| 1095 | * Dual licensed under the MIT or GPL Version 2 licenses. |
| 1096 | **/ |
| 1097 | `,{name:"Superstate Indexer"})}var aW=class{constructor(t,r){this.numWorkers=t;this.cache=r;this.workers=[],this.busy=[],this.reloadQueue=[],this.reloadSet=new Set,this.callbacks=new Map;for(let n=0;n<t;n++){let i=new Zle({name:"Superstate Indexer "+(n+1)});i.onmessage=a=>this.finish(a.data.job,a.data.result,n),this.workers.push(i),this.busy.push(!1)}}reload(t){let r=Yle(t),n=new Promise((a,o)=>{this.callbacks.has(r)?this.callbacks.get(r)?.push([a,o]):this.callbacks.set(r,[[a,o]])});if(this.reloadSet.has(r))return n;this.reloadSet.add(r);let i=this.nextAvailableWorker();return i!==void 0?this.send(t,i):this.reloadQueue.push(t),n}finish(t,r,n){let i=Yle(t),a=[].concat(this.callbacks.get(i)??[]);this.reloadSet.delete(i),this.callbacks.delete(i),this.busy[n]=!1;let o=this.reloadQueue.shift();if(o!==void 0&&this.send(o,n),"$error"in r)for(let[s,l]of a)l(r.$error);else for(let[s,l]of a)s(r)}async send(t,r){if(t.type=="paths"){let n=await this.cache.spaceManager.allCaches(),i={settings:this.cache.settings,spacesCache:this.cache.spacesIndex,pathCache:n,oldMetadata:this.cache.pathsIndex};this.message(r,{job:t,payload:i}),this.busy[r]=!0;return}if(t.type=="path"){let n=this.cache.spacesIndex.get(t.path),i=t.path,a,o=!1;n&&(a=n.space.name,this.cache.settings.enableFolderNote&&await this.cache.spaceManager.pathExists(i=n.space.notePath)?(i=n.space.notePath,o=!0):i=n.space.defPath);let s=await this.cache.spaceManager.readPathCache(i)??await this.cache.spaceManager.readPathCache(t.path);if(o&&s){let d=await this.cache.spaceManager.readPathCache(t.path);d&&(s={...s,file:d.file,parent:d.parent,subtype:d.subtype,type:d.type,contentTypes:d.contentTypes})}a=a??s?.label.name;let l=await this.cache.spaceManager.parentPathForPath(t.path),u=n?"space":s?.type,c=n?n.type:s?.subtype,f={path:t.path,settings:this.cache.settings,spacesCache:this.cache.spacesIndex,pathMetadata:s,name:a,parent:l,type:u,subtype:c,oldMetadata:this.cache.pathsIndex.get(t.path)};this.message(r,{job:t,payload:f}),this.busy[r]=!0;return}if(t.type=="index"){let n=this.cache.pathsIndex;this.message(r,{job:t,payload:{pathsIndex:n}}),this.busy[r]=!0;return}if(t.type=="context"){let n=this.cache.spacesIndex.get(t.path)?.space;if(!n||!n.path){this.message(r,{job:t,payload:{space:n,mdb:null,paths:[...this.cache.spacesMap.getInverse(t.path)],settings:this.cache.settings,pathsIndex:this.cache.pathsIndex,spacesMap:this.cache.spacesMap,contextsIndex:this.cache.contextsIndex,options:t.payload}}),this.busy[r]=!0;return}let i=await this.cache.spaceManager.contextInitiated(n.path);this.cache.spaceManager.readAllTables(n.path).then(a=>{this.message(r,{job:t,payload:{space:n,mdb:a,paths:[...this.cache.spacesMap.getInverse(t.path)],spacesMap:this.cache.spacesMap,settings:this.cache.settings,dbExists:i,contextsIndex:this.cache.contextsIndex,pathsIndex:this.cache.pathsIndex,options:t.payload}}),this.busy[r]=!0});return}if(t.type=="contexts"){let n=this.cache.allSpaces().filter(a=>a.type!="default").map(a=>a.space),i=new Map;for(let a of n){let o=await this.cache.spaceManager.contextInitiated(a.path);await this.cache.spaceManager.readAllTables(a.path).then(s=>{i.set(a.path,{space:a,mdb:s,paths:[...this.cache.spacesMap.getInverse(a.path)],settings:this.cache.settings,spacesMap:this.cache.spacesMap,contextsIndex:this.cache.contextsIndex,dbExists:o})})}this.message(r,{job:t,payload:{map:i,pathsIndex:this.cache.pathsIndex,settings:this.cache.settings,spacesMap:this.cache.spacesMap,contextsIndex:this.cache.contextsIndex}}),this.busy[r]=!0}}message(t,r){this.workers[t].postMessage(r)}nextAvailableWorker(){let t=this.busy.indexOf(!1);return t==-1?void 0:t}};function Lze(e,t,r,n){let i=[],a={shouldSort:!0,threshold:0,ignoreLocation:!0,keys:[{name:"name",weight:2},"path","label.preview",{name:"spaceNames",weight:.5}]};return new Qd([...t.values()].filter(s=>s.hidden==!1),a,n).search(e).map(s=>s.item).slice(0,r)}function Rze(e){let{queries:t,pathsIndex:r,count:n}=e,i=[];for(let[a,o]of r)!o.hidden&&Qle(t,o,{},!0)&&i.push(o);return i.slice(0,n)}var YD=class{constructor(t,r,n,i,a){this.indexVersion=t;this.onChange=r;this.kit=[i6(),hz,bz(),xz,vz(),yz(),yze,VM(),Sje(),Sze,Mze,Nze,Eze,Cze,kze,Dze,Tze(),Oze,Fze,_ze,Ize,xze,bze,gz(),Ip,Pze,Xle,Kle,Az(),N4,jze,wze,Bze,XT];this.eventsDispatcher=new Yv;let o={...Kk,createAdd:X("add",[],()=>function(f,d){return f+d}),createEqual:X("equal",[],()=>function(f,d){return f==d}),createUnequal:X("unequal",[],()=>function(f,d){return f!=d})},l=Kx(o,{matrix:"Array"});l.import(Jk,{override:!0}),this.formulaContext=l,this.initialized=!1,this.spaceManager=n,this.spaceManager.superstate=this,this.ui=i,this.ui.superstate=this,this.cli=a;let u=new iW(this.cli,this);this.cli.superstate=this,this.cli.terminals.splice(0,0,u),this.cli.mainTerminal=u,this.allMetadata={},this.api=new Dy(this),n.api=new Dy(this,n),this.assets=null,this.pathsIndex=new Map,this.spacesIndex=new Map,this.contextsIndex=new Map,this.actionsIndex=new Map,this.kitFrames=new Map,this.kits=new Map,this.actions=new Map,this.templateCache=new Map,this.focuses=[],this.spacesMap=new hc,this.linksMap=new hc,this.tagsMap=new hc,this.liveSpaceLinkMap=new hc,this.iconsCache=new Map,this.imagesCache=new Map,this.contextStateQueue=Promise.resolve(),this.indexer=new aW(2,this),this.eventsDispatcher.addListener("pathStateUpdated",()=>{(0,Hh.debounce)(()=>this.reindexSearch(),300)()}),this.eventsDispatcher.addListener("superstateReindex",()=>{(0,Hh.debounce)(()=>this.reindexSearch(),300)()})}static create(t,r,n,i,a){return new YD(t,r,n,i,a)}async search(t,r,n){return r?Lze(r,this.pathsIndex,10,this.searchIndex):Rze({queries:n,pathsIndex:this.pathsIndex,count:10})}reindexSearch(){this.indexer.reload({type:"index",path:""}).then(t=>{this.searchIndex=Qd.parseIndex(t)})}refreshMetadata(){this.allMetadata=MG(this)}async initializeIndex(){await this.loadFromCache()}addToContextStateQueue(t){this.contextStateQueue=this.contextStateQueue.then(t).catch(()=>{})}async initialize(){if(!this.persister)return;let t=Date.now();this.initializeActions(),this.initializeFocuses(),this.initializeKits(),this.initializeTemplates(),this.settings.spacesEnabled&&await this.initializeSpaces(),await this.initializeBuiltins(),await this.initializeTags(),this.assets&&await this.assets.initialize(),await this.initializePaths(),await this.initializeContexts(),await this.initializeFrames(),this.refreshMetadata(),this.dispatchEvent("superstateUpdated",null),this.ui.notify(`Make.md - Superstate Loaded in ${(Date.now()-t)/1e3} seconds`,"console"),this.persister.cleanType("space"),this.persister.cleanType("path"),this.persister.cleanType("context"),this.persister.cleanType("frame")}async reloadSystemActions(){(await this.spaceManager.readSystemCommands()).forEach(r=>this.actions.set(r.name,r.commands)),this.dispatchEvent("actionStateUpdated",{path:"spaces://$actions"})}async initializeActions(){await this.reloadSystemActions();let t=this.allSpaces().filter(r=>r.type!="default").map(r=>r.space).map(r=>this.reloadActions(r));await Promise.all(t)}async initializeKits(){let t=await this.spaceManager.readAllKits();t.forEach(r=>this.kits.set(r.id,r)),t.length==0&&this.kits.set("default",{id:"default",name:N.labels.default,colors:{},frames:[]}),this.selectedKit=this.kits.get(this.settings.selectedKit)??this.kits.get("default"),this.selectedKit.frames=[...this.selectedKit.frames,...this.kit.map(r=>FP(r)).filter(r=>!this.selectedKit.frames.some(n=>n.schema.id==r.schema.id))];for(let r of this.selectedKit.frames){let n=Ni(r.schema).def.id,i=await VT(this,r,{...l0,screenType:this.ui.getScreenType()});this.kitFrames.set(n,i)}this.dispatchEvent("frameStateUpdated",{path:"spaces://$kit"})}async initializeTemplates(){let t=await this.spaceManager.readAllTemplates();Object.keys(t).forEach(r=>{this.templateCache.set(r,t[r])})}async initializeSpaces(){let t=[...this.spaceManager.allSpaces().values()],r=t.map(n=>this.reloadSpace(n,null,!0));[...this.spacesIndex.keys()].filter(n=>!t.some(i=>i.path==n)).forEach(n=>this.onSpaceDeleted(n)),await Promise.all(r)}getSpaceItems(t){let r=[...this.spacesMap.getInverse(t)],n=this.contextsIndex.get(t)?.paths??[];return r.map((i,a)=>(this.spacesIndex.has(i)?this.spaceManager.loadPath(this.spacesIndex.get(i).space.notePath):this.spaceManager.loadPath(i),{...this.pathsIndex.get(i),rank:n.indexOf(i)})).filter(i=>i?.hidden!=!0&&i.path!=t)}async initializeFrames(){await this.initializeTemplates()}async initializeContexts(){await this.indexer.reload({type:"contexts",path:""}).then(async t=>{let r=[...t.entries()].map(([n,{cache:i,changed:a}])=>this.contextReloaded(n,i,a,!0));await Promise.all(r)})}async loadFromCache(){this.dispatchEvent("superstateReindex",null);let t=await this.persister.loadAll("icon");this.spaceManager.allPaths(["svg"]).forEach(a=>{let o=t.find(s=>s.path==a);o?.cache.length>0&&this.assets&&this.assets.cacheIconFromPath(a,o.cache)});let r=await this.persister.loadAll("path"),n=await this.persister.loadAll("space"),i=await this.persister.loadAll("context");n.forEach(a=>{let o=or(a.cache);o&&o.type&&this.spacesIndex.set(a.path,o)}),i.forEach(a=>{let o=or(a.cache);o&&this.contextsIndex.set(a.path,o)}),r.forEach(a=>{let o=AG(a.cache);o&&(this.pathsIndex.set(a.path,o),this.tagsMap.set(a.path,new Set(o.tags)),this.spacesMap.set(a.path,new Set(o.spaces)),this.linksMap.set(a.path,new Set(o.outlinks)))}),this.settings.enhancedLogs,this.dispatchEvent("superstateUpdated",null)}dispatchEvent(t,r){this.eventsDispatcher.dispatchEvent(t,r)}async initializeBuiltins(){let r=Object.keys(Um).map(n=>this.reloadPath("spaces://$"+n,!0));await Promise.all(r)}async initializeTags(){let r=[...this.spaceManager.readTags().map(n=>ii(n))].map(n=>this.reloadPath(n,!0));await Promise.all(r)}async onSpaceDefinitionChanged(t,r){if(t.space.readOnly)return;let n=this.spacesMap.getInverse(t.path),i=[];if(t.metadata?.links&&!Hh.default.isEqual(t.metadata.links,r?.links)&&i.push(...t.metadata.links),t.metadata?.joins?.length>0){let s=t.metadata.joins.some(l=>l.groups.some(u=>u.filters.some(c=>c.fType=="property")));if(!Hh.default.isEqual(t.metadata?.joins,r?.joins)||s)for(let[l,u]of this.pathsIndex)t.metadata.joins.some(c=>c.path=="/"||u.path.startsWith(c.path+"/"))&&!u.hidden&&Aze(t.metadata?.joins,u,t.properties)&&i.push(l)}let o=[...Hh.default.difference(i,[...n]),...Hh.default.difference([...n],i)].map(s=>this.reloadPath(s,!0).then(l=>this.dispatchEvent("pathStateUpdated",{path:s})));await Promise.all(o)}async initializeFocuses(){let t=await this.spaceManager.readFocuses();if(t.length==0){this.spaceManager.saveFocuses([{name:N.labels.home,sticker:"ui//home",paths:["/"]}]);return}this.focuses=t,this.dispatchEvent("focusesChanged",null)}async initializePaths(){this.dispatchEvent("superstateReindex",null);let t=this.spaceManager.allPaths(),r=Date.now();await this.indexer.reload({type:"paths",path:""}).then(async i=>{for await(let[a,{cache:o,changed:s}]of Object.entries(i))await this.pathReloaded(a,o,s,!1)}),this.ui.notify(`Make.md - ${t.length} Paths Cached in ${(Date.now()-r)/1e3} seconds`,"console");let n=Mr([...this.spacesIndex.keys(),...t]);[...this.pathsIndex.keys()].filter(i=>!n.some(a=>a==i)).forEach(i=>this.onPathDeleted(i)),this.dispatchEvent("superstateUpdated",null)}async onTagRenamed(t,r){let n=Jce(t),i=t1(this.spaceManager,r);await this.onSpaceRenamed(n,i),await this.onPathRename(n,i.path),this.dispatchEvent("spaceChanged",{path:n,newPath:i.path});let a=[];for(let[o,s]of this.spacesIndex)this.contextsIndex.get(o)?.contexts.includes(t)&&this.addToContextStateQueue(()=>c0e(this.spaceManager,t,r,a)),s.metadata?.contexts.includes(t)&&hu(this,s.space,{...s.metadata,contexts:s.metadata.contexts.map(u=>u==t?r:u)});this.dispatchEvent("spaceStateUpdated",{path:Qv})}async onTagDeleted(t){this.tagsMap.getInverse(t).forEach(i=>{this.deleteTagInPath(t,i)});let r=Lj(t,this.settings);await this.spaceManager.deletePath(r),this.onSpaceDeleted(ii(t));for(let[i,a]of this.spacesIndex)a.metadata?.contexts.includes(t)&&hu(this,a.space,{...a.metadata,contexts:a.metadata.contexts.filter(o=>o!=t)});let n=[];for(let[i,a]of this.contextsIndex)a.contexts.includes(t)&&n.push(this.spaceManager.spaceInfoForPath(a.path));this.addToContextStateQueue(()=>f0e(this.spaceManager,t,n)),this.dispatchEvent("spaceStateUpdated",{path:Qv})}async deleteTagInPath(t,r){let n;if(this.pathsIndex.has(r)&&(n=this.pathsIndex.get(r)),n){let i={...n,tags:n.tags.filter(a=>a!=t),spaces:n.spaces.filter(a=>a!=ii(t))};this.pathsIndex.set(r,i),this.tagsMap.set(r,new Set(i.tags)),this.spacesMap.set(r,new Set(i.spaces))}else await this.reloadPath(r);this.onPathReloaded(r),this.dispatchEvent("pathStateUpdated",{path:r})}onMetadataChange(t){this.settings.enhancedLogs,this.pathsIndex.has(t)&&this.reloadPath(t).then(r=>{let n=this.pathsIndex.get(t),i=this.spacesIndex.get(t);i&&this.reloadSpace(i.space).then(o=>this.onSpaceDefinitionChanged(o,i.metadata));let a=n.spaces.map(o=>this.spacesIndex.get(o)?.space).filter(o=>o);this.addToContextStateQueue(()=>tJ(this,t,a)),this.dispatchEvent("pathStateUpdated",{path:t})})}reloadSpaceByPath(t,r){return this.reloadSpace(this.spaceManager.spaceInfoForPath(t),r)}async onPathRename(t,r){let n=r,i=this.pathsIndex.get(t),a=i?.spaces??[];if(i){this.spacesMap.delete(t),this.spacesMap.deleteInverse(t),this.linksMap.delete(t),this.linksMap.deleteInverse(t),this.tagsMap.delete(t),this.pathsIndex.delete(t);let u=a.map(f=>this.spacesIndex.get(f)).filter(f=>f);await this.reloadPath(n,!0),await g0e(this.spaceManager,t,n,u.map(f=>f.space)),await rJ(this.spaceManager,t,u.map(f=>f.space));for(let f of u)f.metadata?.links?.includes(t)&&this.addToContextStateQueue(()=>P3(this,f.path,"links",f.metadata.links.map(d=>d==t?r:d))),await this.reloadContext(f.space,{force:!0,calculate:!0});let c=[];for(let[f,d]of this.contextsIndex)d.outlinks.includes(t)&&c.push(this.spacesIndex.get(d.path).space);this.addToContextStateQueue(()=>m0e(this.spaceManager,t,n,c).then(f=>Promise.all(c.map(d=>this.reloadContext(d,{force:!0,calculate:!0})))))}let o=!1;this.focuses.forEach(u=>{u.paths.includes(t)&&(u.paths=u.paths.map(c=>c==t?r:c),o=!0)}),o&&(await this.spaceManager.saveFocuses(this.focuses),this.dispatchEvent("focusesChanged",null)),await this.reloadPath(r,!0),this.persister.remove(t,"path");let s=Mr([...this.spacesMap.get(r)??[],...a]),l=s.map(u=>this.reloadContext(this.spacesIndex.get(u)?.space,{force:!1,calculate:!0}));await Promise.all(l),s.forEach(u=>this.dispatchEvent("spaceStateUpdated",{path:u})),this.dispatchEvent("pathChanged",{path:t,newPath:r}),this.ui.viewsByPath(t).forEach(u=>{u.openPath(r)})}async onPathCreated(t){await this.reloadPath(t,!0);let r=Rc(t);this.spacesIndex.has(r)&&this.spacesIndex.get(r).space.notePath==t&&(await this.reloadSpace(this.spacesIndex.get(r).space),await this.reloadContextByPath(r,{force:!0})),this.dispatchEvent("pathCreated",{path:t})}onPathDeleted(t){this.spacesMap.delete(t),this.linksMap.delete(t),this.linksMap.deleteInverse(t),this.persister.remove(t,"path");let r=this.pathsIndex.get(t);if(!r)return;let n=(r.spaces??[]).map(a=>this.spacesIndex.get(a)?.space).filter(a=>a);this.addToContextStateQueue(()=>rJ(this.spaceManager,t,n).then(a=>n.forEach(o=>this.reloadContext(o,{force:!1,calculate:!0}))));let i=[];for(let[a,o]of this.contextsIndex)o.outlinks.includes(t)&&this.spacesIndex.has(o.path)&&i.push(this.spacesIndex.get(o.path).space);this.addToContextStateQueue(()=>h0e(this.spaceManager,t,i).then(a=>n.forEach(o=>this.reloadContext(o,{force:!1,calculate:!0})))),(r.spaces??[]).forEach(a=>{this.dispatchEvent("spaceStateUpdated",{path:a})}),this.pathsIndex.delete(t),this.dispatchEvent("pathDeleted",{path:t})}async onSpaceRenamed(t,r){if(this.spacesIndex.has(t)){let n=this.spacesIndex.get(t).metadata;this.spacesIndex.set(r.path,{...this.spacesIndex.get(t),path:r.path,name:r.name,space:r}),this.spacesMap.rename(t,r.path),this.spacesMap.renameInverse(t,r.path),this.spacesIndex.delete(t),this.contextsIndex.delete(t),this.actionsIndex.delete(t),await this.reloadSpace(r,n).then(i=>this.onSpaceDefinitionChanged(i,n)),await this.reloadContext(r,{force:!0,calculate:!0}),await this.reloadActions(r)}}onSpaceDeleted(t){this.spacesIndex.has(t)&&(this.spacesIndex.delete(t),this.contextsIndex.delete(t)),this.spacesMap.delete(t),this.spacesMap.deleteInverse(t),this.persister.remove(t,"space"),this.dispatchEvent("spaceDeleted",{path:t})}async reloadActions(t){if(!t)return!1;this.spaceManager.commandsForSpace(t.path).then(r=>{this.actionsIndex.set(t.path,r),this.dispatchEvent("actionStateUpdated",{path:t.path})})}async reloadContextByPath(t,r){return this.reloadContext(this.spaceManager.spaceInfoForPath(t),r)}async reloadContext(t,r){return t?this.indexer.reload({type:"context",path:t.path,payload:r}).then(n=>this.contextReloaded(t.path,n.cache,n.changed,r?.force)):!1}async contextReloaded(t,r,n,i){if(!r||(this.settings.enhancedLogs,!n&&!i))return!1;this.contextsIndex.set(t,r);let a=this.pathsIndex.get(t);if(a&&r.dbExists&&!a.readOnly&&this.settings.syncFormulaToFrontmatter){let o=r.contextTable?.rows??[],s=r.contextTable?.cols??[],l=o.filter(u=>{let c=u[ut],f=this.pathsIndex.get(c);return!f||f.type=="file"&&f.subtype!="md"?!1:s.reduce((d,p,h)=>d||p.type!="fileprop"||p.primary=="true"?d:u[p.name]?.length>0&&f.metadata?.property?.[p.name]!=u[p.name]?!0:d,!1)});l.length>0&&l.forEach(u=>ma(this,u[ut],s.reduce((c,f,d)=>f.type=="fileprop"&&f.primary!="true"?{...c,[f.name]:zi(Ns(f),u[f.name],!0)}:c,{})))}return r.dbExists&&n&&await this.spaceManager.saveTable(t,r.contextTable),this.persister.store(t,JSON.stringify(r),"context"),this.dispatchEvent("contextStateUpdated",{path:t}),!0}allSpaces(t){return t?Gce([...this.spacesIndex.values()],this.spaceOrder(),"path"):[...this.spacesIndex.values()]}spaceOrder(){return[...this.focuses.flatMap(t=>t.paths)]}async updateSpaceMetadata(t,r){let n=this.spacesIndex.get(t),i=n?.metadata;if(!n)return this.reloadSpaceByPath(t);let a=!1,s=(r?.sort??{field:"rank",asc:!0,group:!0}).field=="rank";(!Hh.default.isEqual(n.metadata.links,r.links)||!Hh.default.isEqual(n.metadata.joins,r.joins))&&(a=!0);let l={...n,metadata:r,contexts:r?.contexts??[],sortable:s};return this.spacesIndex.set(t,l),a&&await this.onSpaceDefinitionChanged(l,i),this.dispatchEvent("spaceStateUpdated",{path:n.path}),l}async reloadSpace(t,r,n=!0){if(!t)return;this.settings.enhancedLogs;let i=r??await this.spaceManager.spaceDefForSpace(t.path),a=this.pathsIndex.get(t.path),o=this.spaceManager.uriByString(t.path);if(!o)return null;let s=this.spaceManager.spaceTypeByString(o);(s=="default"||s=="tag")&&(i.joins=[]);let l=[],u={},c=await this.spaceManager.readFrame(t.path,Qb).then(x=>x?.cols??[]);if(l.push(...c),l.length>0){if(!a)if(this.settings.enableFolderNote){let x=await this.spaceManager.readPathCache(t.notePath);a={path:t.path,name:t.name,tags:[],spaces:[],outlinks:[],readOnly:t.readOnly,hidden:!1,metadata:x?.metadata,type:"space",subtype:s,label:x?.label}}else{let x=await this.spaceManager.readPathCache(t.path);a={path:t.path,name:t.name,tags:[],spaces:[],outlinks:[],readOnly:t.readOnly,hidden:!1,metadata:x?.metadata,type:"space",subtype:s,label:x?.label}}u=await this.spaceManager.readProperties(t.notePath).then(x=>Gf(this.formulaContext,this.pathsIndex,this.contextsIndex,this.spacesMap,x,l,a,this.settings))}[...this.spacesMap.get(t.path)].map(x=>this.contextsIndex.get(x)).forEach(x=>{if(x){let g=x.contextTable?.cols??[];l.push(...g),u={...u,...x.contextTable?.rows.find(w=>w[ut]==t.path)??{}}}});let f=i?.sort??Jx,d=f.field=="rank"||!f,p=i?.contexts??[],h=Mr((i.joins??[]).flatMap(x=>x.groups).flatMap(x=>x.filters).flatMap(x=>x.type=="context"?[x.field.split(".")[0]]:x.type=="path"&&x.field=="space"?Rt(x.value):[])),m=Mr((i.joins??[]).flatMap(x=>x.groups).flatMap(x=>x.filters).flatMap(x=>x.type.startsWith("link")?Rt(x.value):[]));if(s=="tag"&&this.settings.autoAddContextsToSubtags){let x=gde(t.name);p.push(...x)}let v=await this.spaceManager.readTemplates(t.path),A={name:t.name,space:t,path:t.path,type:s,templates:v,contexts:p.map(x=>x.toLowerCase()),metadata:i,dependencies:h,sortable:d,properties:u,propertyTypes:l};if(this.spacesIndex.set(t.path,A),this.persister.store(t.path,JSON.stringify(A),"space"),A.metadata?.links?.forEach(x=>{Yce(this,x)&&this.spacesMap.set(x,new Set([...this.spacesMap.get(x),t.path]))}),n)return this.dispatchEvent("spaceStateUpdated",{path:t.path}),A}async pathReloaded(t,r,n,i){if(!r||(this.settings.enhancedLogs,this.pathsIndex.set(t,r),await this.onPathReloaded(t),(r.subtype=="image"||r.metadata?.file?.extension=="svg")&&this.imagesCache.set(r.metadata.file.filename,t),!n&&!i))return!1;if(this.tagsMap.set(t,new Set(r.tags)),this.linksMap.set(t,new Set(r.outlinks)),!Hh.default.isEqual(r.spaces,Array.from(this.spacesMap.get(t)))){this.spacesMap.set(t,new Set(r.spaces));let o=r.tags.map(l=>t1(this.spaceManager,l)).filter(l=>!this.spacesIndex.has(l.path)).map(async l=>{await this.reloadSpace(l),this.reloadContext(l,{force:!1,calculate:!0}),await this.reloadPath(l.path)});await Promise.all(o).then(l=>{this.dispatchEvent("spaceStateUpdated",{path:Qv})})}if(i){let o=r.spaces.map(s=>this.spacesIndex.get(s)?.space).filter(s=>s);this.addToContextStateQueue(()=>tJ(this,t,o).then(s=>{o.forEach(l=>{this.dispatchEvent("spaceStateUpdated",{path:l.path})})}))}r.metadata?.file?.extension==="svg"&&this.assets&&(this.assets.iconPathMapping.has(t)||this.settings.indexSVG)&&this.spaceManager.readPath(t).then(o=>{this.assets.cacheIconFromPath(t,o),this.persister.store(t,o,"icon")})}async reloadPath(t,r){return t?this.indexer.reload({type:"path",path:t}).then(async n=>(await this.pathReloaded(t,n.cache,n.changed,r),!0)):!1}async onPathReloaded(t){let r;if(this.pathsIndex.has(t)&&(r=this.pathsIndex.get(t)),!r)return!1;await this.persister.store(t,vze(r),"path")}};var qze=.999,oW=class extends jc.Plugin{constructor(){super(...arguments);this.debouncedRefresh=()=>null;this.openPath=async(r,n,i)=>{let a=this.superstate.spaceManager.uriByString(n);if(!!a){if(a.scheme=="https"||a.scheme=="http"){if(this.superstate.spacesIndex.has(n)){let o=Mc;this.app.workspace.setActiveLeaf(r,{focus:!0}),await r.setViewState({type:o,state:{path:n,flow:i}});return}else if(this.superstate.pathsIndex.has(n)){let o=R2;this.app.workspace.setActiveLeaf(r,{focus:!0}),await r.setViewState({type:o,state:{path:n,flow:i}});return}window.open(a.fullPath,"_blank");return}if(a.scheme=="obsidian"){await r.setViewState({type:a.authority});return}if(a.ref&&(this.superstate.pathsIndex.get(a.path)?.type=="space"||a.scheme=="spaces")){i&&a.ref=="main"?await r.setViewState({type:B2,state:{path:a.fullPath}}):await r.setViewState({type:_G,state:{path:a.fullPath,flow:i}});return}if(a.scheme=="spaces"||a.scheme=="mk-core"){NRe(r,a.basePath,this.app);return}this.files.getFile(n).then(o=>{if(o)if(o.isFolder)TRe(r,Li(this.app,o.path),this,i);else if(o)DRe(r,Li(this.app,o.path),this.app);else return;else if(n.contains("/")){let s=Vu(Rc(n)),l=this.superstate.spacesIndex.get(s);l&&Xa(this.superstate,l,sle(n),lle(n))}else P0(this.superstate,this.superstate.pathsIndex.get(this.superstate.ui.activePath)).then(s=>{s&&Xa(this.superstate,s,sle(n),lle(n))})})}};this.metadataChange=r=>{this.markdownAdapter.metadataChange(r)};this.onDelete=async r=>{this.activeFileChange()};this.onRename=async(r,n)=>{this.activeFileChange()};this.openFileTreeLeaf=async r=>{if(this.app.workspace.getLeavesOfType(ld).length==0){let i=this.superstate.settings.spacesRightSplit?this.app.workspace.getRightLeaf(!1):this.app.workspace.getLeftLeaf(!1);await i.setViewState({type:ld}),r&&!this.app.workspace.leftSplit.collapsed&&this.app.workspace.revealLeaf(i)}else if(!this.app.workspace.leftSplit.collapsed&&r){let i=this.app.workspace.getLeavesOfType(ld);for(let o of i)o.view instanceof jD&&o.view.destroy(),o.detach();let a=this.superstate.settings.spacesRightSplit?this.app.workspace.getRightLeaf(!1):this.app.workspace.getLeftLeaf(!1);await a.setViewState({type:ld}),this.app.workspace.revealLeaf(a)}Or(this.superstate.ui)&&this.app.workspace.leftSplit.collapse(),this.closeDuplicateTabs()};this.closeDuplicateTabs=()=>{try{this.app.workspace.leftSplit.children[0].children.filter((r,n,i)=>n!=i.findIndex(a=>a.view.getViewType()==r.view.getViewType())).forEach(r=>this.app.workspace.leftSplit.children[0].removeChild(r))}catch{}};this.detachFileTreeLeafs=()=>{let r=this.app.workspace.getLeavesOfType(ld);for(let n of r)n.view instanceof jD&&n.view.destroy(),n.detach()};this.detachFileContextLeafs=()=>{let r=this.app.workspace.getLeavesOfType(gb);for(let n of r)n.view instanceof u4&&n.view.destroy(),n.detach()};this.openFileContextLeaf=async(r,n)=>{let i=this.app.workspace.getLeavesOfType(r);if(i.length==0){let a=this.app.workspace.getRightLeaf(!1);await a.setViewState({type:r}),this.app.workspace.revealLeaf(a)}else i.forEach(a=>this.app.workspace.revealLeaf(a));Or(this.superstate.ui)&&!n&&this.app.workspace.rightSplit.collapse()};this.refreshFileContextLeafs=()=>{this.detachFileContextLeafs(),this.openFileContextLeaf(gb)}}quickOpen(r,n,i,a){let o=je(this.app.workspace.getLeaf()?.containerEl.ownerDocument);d$e(r,n,o,i,a)}loadSuperState(){this.app.workspace.onLayoutReady(async()=>{if(this.superstate.settings.spacesEnabled){if(await this.superstate.initializeIndex(),this.obsidianAdapter.loadCacheFromObsidianCache(),this.superstate.settings.navigatorEnabled&&this.openFileTreeLeaf(this.superstate.settings.openSpacesOnLaunch),this.superstate.settings.homepagePath){let r=this.app.workspace.getLeaf(!1);await this.openPath(r,this.superstate.settings.homepagePath)}}else await this.superstate.loadFromCache(),this.superstate.initialize();this.registerEvent(this.app.vault.on("delete",this.onDelete)),this.registerEvent(this.app.vault.on("rename",this.onRename)),this.app.metadataCache.on("changed",this.metadataChange),qze>this.superstate.settings.releaseNotesPrompt&&(this.releaseTheNotes(),this.superstate.settings.releaseNotesPrompt=qze,this.saveSettings()),this.superstate.settings.firstLaunch||(this.getStarted(),this.superstate.settings.firstLaunch=!0,this.saveSettings())})}loadViews(){this.registerView(TA,r=>new zV(r,this.superstate,this.ui)),this.registerView(ld,r=>new jD(r,this.superstate,this.ui)),this.registerView(Mc,r=>new q2(r,this.superstate,this.ui,Mc)),this.registerView(_G,r=>new PG(r,this)),this.registerView(B2,r=>new TV(r,this)),this.superstate.settings.contextEnabled&&(this.registerView(R2,r=>new $V(r,this.app,R2,this.superstate)),this.registerView(gb,r=>new u4(r,this.superstate,this.ui)),this.registerView(sG,r=>new oG(r,this)),this.registerView(rG,r=>new tG(r,this)),this.registerView(aG,r=>new iG(r,this)))}async loadSpaces(){document.body.querySelector(".app-container").setAttribute("vaul-drawer-wrapper",""),document.body.classList.toggle("mk-spaces-right",this.superstate.settings.spacesRightSplit),document.body.classList.toggle("mk-readable-line",this.app.vault.getConfig("readableLineLength")),this.superstate.settings.readableLineWidth=this.app.vault.getConfig("readableLineLength"),this.superstate.settings.spacesEnabled&&(document.body.classList.toggle("mk-hide-tabs",!this.superstate.settings.sidebarTabs),document.body.classList.toggle("mk-hide-ribbon",!this.superstate.settings.showRibbon),document.body.classList.toggle("mk-hide-vault-selector",!this.superstate.settings.vaultSelector),document.body.classList.toggle("mk-mobile-header",this.superstate.settings.mobileMakeHeader),document.body.classList.toggle("mk-folder-lines",this.superstate.settings.folderIndentationLines),this.app.vault.config.cssTheme=="Minimal"&&document.body.classList.toggle("mk-minimal-fix",!0),document.body.classList.toggle("mk-spaces-enabled",this.superstate.settings.spacesEnabled),!this.superstate.settings.spacesDisablePatch&&this.superstate.settings.navigatorEnabled&&xqe(this)),this.registerEvent(this.app.workspace.on("active-leaf-change",()=>this.activeFileChange())),this.registerEvent(this.app.workspace.on("layout-change",()=>{this.activeFileChange()}))}convertPathToSpace(){if(this.app.workspace.activeLeaf?.view.getViewType()=="markdown"){let n=this.app.workspace.getActiveViewOfType(jc.MarkdownView);n instanceof jc.MarkdownView&&n.file instanceof jc.TFile&&mP(this.superstate,n.file.path,!0)}else this.superstate.ui.notify(N.notice.cantConvertNoteToSpace)}toggleExperimental(){this.superstate.settings.experimental=!this.superstate.settings.experimental,this.saveSettings()}getActiveFile(){let r=null,n=null,i=this.app.workspace.getActiveViewOfType(jc.MarkdownView)?.leaf;i||(i=this.app.workspace.getActiveViewOfType(q2)?.leaf);let a=i?.view;return!a||i.isFlowBlock||(a.getViewType()==Mc?(h4(this),n=a.getState(),r=a.getState().path):a.getViewType()=="markdown"&&(r=a.file.path,n=a.getState(),Sqe(this),h4(this)),!r||!n)?null:{path:r,state:n}}fixFileWarnings(){FG(this)}activeFileChange(){let r=this.getActiveFile();if(r){if(this.superstate.ui.activePath==r?.path){this.superstate.ui.setActiveState(r.state);return}this.superstate.ui.setActivePath(r.path),this.superstate.ui.setActiveState(r.state)}}releaseTheNotes(){cle("https://www.make.md/static/latest.md",this.app,!0)}getStarted(){cle("https://www.make.md/static/GetStarted.md",this.app,!0)}closeExtraFileTabs(){let r=!1;if(jc.Platform.isMobile){this.app.workspace.leftSplit?.children.forEach(n=>{n.view.getViewType()=="file-explorer"&&(r?this.app.workspace.leftSplit.removeChild(n):r=!0)});return}this.app.workspace.leftSplit?.children.forEach(n=>{n?.children.forEach(i=>{i.view.getViewType()=="file-explorer"&&(r?n.removeChild(i):r=!0)})})}loadCommands(){dze(this)}loadContext(){if(this.superstate.settings.contextEnabled){this.app.workspace.onLayoutReady(async()=>{this.closeExtraFileTabs(),this.superstate.settings.enableDefaultSpaces&&await this.files.createFolder(this.superstate.settings.spacesFolder)}),this.registerExtensions(["mdb"],sG);try{this.registerExtensions(["html","htm"],rG)}catch{}try{this.registerExtensions(["mkit"],aG)}catch{}this.app.workspace.onLayoutReady(async()=>{this.superstate.settings.autoOpenFileContext&&await this.openFileContextLeaf(gb),setTimeout(()=>this.activeFileChange(),2e3)})}this.superstate.settings.inlineContext&&(this.registerMarkdownPostProcessor((r,n)=>{Aqe(this,r,n)}),document.body.classList.toggle("mk-inline-context-enabled",this.superstate.settings.inlineContext))}openEverView(){let r=this.app.workspace.getLeavesOfType(TA);if(r.length==0){let n=this.app.workspace.createLeafBySplit(this.app.workspace.getLeaf(),"vertical",!0);n.setViewState({type:TA}),n.setPinned(!0)}else r.forEach(n=>{n.getRoot()!=this.app.workspace.rootSplit&&n.detach();let i=this.app.workspace.createLeafBySplit(this.app.workspace.getLeaf(),"vertical",!0);i.setViewState({type:TA}),i.setPinned(!0)});wqe(this)}async onload(){let r=Date.now(),n=Object.assign({},FD,await this.loadData());this.mdbFileAdapter=new hG(this),this.files=tx.create(),this.obsidianAdapter=new yG(this,this.files,(0,jc.normalizePath)(this.app.vault.configDir+"/plugins/make-md/Spaces.mdb")),this.files.initiateFileSystemAdapter(this.obsidianAdapter,!0),this.markdownAdapter=new xG(this),this.files.initiateFiletypeAdapter(this.mdbFileAdapter),this.files.initiateFiletypeAdapter(this.markdownAdapter),this.files.initiateFiletypeAdapter(new bG(this)),this.files.initiateFiletypeAdapter(new jG(this)),this.files.initiateFiletypeAdapter(new OG(this)),this.files.initiateFiletypeAdapter(new TG(this));let i=new z3(this.files,n.spaceSubFolder),a=new nW;this.ui=new DG(this);let o=lw.create(this.ui),s=zD.create(new NG(this));this.superstate=YD.create("0.9",()=>{this.debouncedRefresh()},new RT,o,s),await this.loadSettings(),this.superstate.spaceManager.addSpaceAdapter(i,!0),this.superstate.spaceManager.addSpaceAdapter(a);try{let u=".space/lang.json",c=await this.obsidianAdapter.readTextFromFile(u);if(c){let f=JSON.parse(c);Im.setOverridesFromFile(f)}}catch{}(0,jc.addIcon)("mk-logo",bqe),this.superstate.saveSettings=()=>this.saveSettings(),this.loadViews();let l;jc.Platform.isMobile?l=new ID(".makemd/superstate.mdc",this.mdbFileAdapter,["path","space","frame","context","icon"]):l=new _D(".makemd/superstate.mdc",this.mdbFileAdapter,["path","space","frame","context","icon"]),this.superstate.settings.cacheIndex&&await l.initialize(),this.superstate.persister=l,this.superstate.assets=new gG(this.superstate.spaceManager,this.superstate.ui,l,this),this.loadSuperState(),this.addSettingTab(new JV(this.app,this)),await this.loadSpaces(),this.loadContext(),Object.keys(this.superstate.settings).some(u=>u=="makerMode")&&(this.superstate.settings.basics=this.superstate.settings.makerMode,delete this.superstate.settings.makerMode,this.saveSettings()),this.superstate.settings.basics&&(this.basics=new T4(this),this.basics.loadBasics()),this.loadCommands(),this.superstate.ui.notify(`Make.md - Plugin loaded in ${(Date.now()-r)/1e3} seconds`,"console"),this.superstate.settings.systemName=="Vault"&&(this.superstate.settings.systemName=this.app.vault.getName(),this.saveSettings())}async loadSettings(){this.superstate.settings=Object.assign({},FD,await this.loadData()),this.superstate.settings.hiddenExtensions.length==1&&this.superstate.settings.hiddenExtensions[0]==".mdb"&&(this.superstate.settings.hiddenExtensions=FD.hiddenExtensions);let r=or(await kRe(this));this.superstate.settings.newFileFolderPath=r.newFileFolderPath,this.superstate.settings.newFileLocation=r.newFileLocation,this.saveSettings()}async saveSettings(r=!0){await this.saveData(this.superstate.settings),this.obsidianAdapter.pathLastUpdated.set((0,jc.normalizePath)(this.app.vault.configDir+"/plugins/make-md/data.json"),Date.now()),r&&this.superstate.dispatchEvent("settingsChanged",null)}onunload(){this.superstate.persister.unload(),this.detachFileTreeLeafs()}}; |
| 1098 | /* |
| 1099 | object-assign |
| 1100 | (c) Sindre Sorhus |
no test coverage detected