mermaid
Version:
Markdown-ish syntax for generating flowcharts, mindmaps, sequence diagrams, class diagrams, gantt charts, git graphs and more.
2 lines (1 loc) • 7.02 kB
JavaScript
import{D as b,d as _,l as C,q as g}from"./chunk-W44A43WB.mjs";import{b as i}from"./chunk-LIEV3EAG.mjs";import{a as f}from"./chunk-AQ6EADP3.mjs";function m(e){var r={options:{directed:e.isDirected(),multigraph:e.isMultigraph(),compound:e.isCompound()},nodes:A(e),edges:P(e)};return g(e.graph())||(r.value=_(e.graph())),r}f(m,"write");function A(e){return C(e.nodes(),function(r){var n=e.node(r),s=e.parent(r),t={v:r};return g(n)||(t.value=n),g(s)||(t.parent=s),t})}f(A,"writeNodes");function P(e){return C(e.edges(),function(r){var n=e.edge(r),s={v:r.v,w:r.w};return g(r.name)||(s.name=r.name),g(n)||(s.value=n),s})}f(P,"writeEdges");var a=new Map,w=new Map,S=new Map,W=f(()=>{w.clear(),S.clear(),a.clear()},"clear"),v=f((e,r)=>{let n=w.get(r)||[];return i.trace("In isDescendant",r," ",e," = ",n.includes(e)),n.includes(e)},"isDescendant"),U=f((e,r)=>{let n=w.get(r)||[];return i.info("Descendants of ",r," is ",n),i.info("Edge is ",e),e.v===r||e.w===r?!1:n?n.includes(e.v)||v(e.v,r)||v(e.w,r)||n.includes(e.w):(i.debug("Tilt, ",r,",not in descendants"),!1)},"edgeInCluster"),D=f((e,r,n,s)=>{i.warn("Copying children of ",e,"root",s,"data",r.node(e),s);let t=r.children(e)||[];e!==s&&t.push(e),i.warn("Copying (nodes) clusterId",e,"nodes",t),t.forEach(o=>{if(r.children(o).length>0)D(o,r,n,s);else{let l=r.node(o);i.info("cp ",o," to ",s," with parent ",e),n.setNode(o,l),s!==r.parent(o)&&(i.warn("Setting parent",o,r.parent(o)),n.setParent(o,r.parent(o))),e!==s&&o!==e?(i.debug("Setting parent",o,e),n.setParent(o,e)):(i.info("In copy ",e,"root",s,"data",r.node(e),s),i.debug("Not Setting parent for node=",o,"cluster!==rootId",e!==s,"node!==clusterId",o!==e));let u=r.edges(o);i.debug("Copying Edges",u),u.forEach(c=>{i.info("Edge",c);let d=r.edge(c.v,c.w,c.name);i.info("Edge data",d,s);try{if(U(c,s)){let h=w.get(s)||[],p=h.includes(c.v)||v(c.v,s)||c.v===s,E=h.includes(c.w)||v(c.w,s)||c.w===s;if(p&&E)i.info("Copying as ",c.v,c.w,d,c.name),n.setEdge(c.v,c.w,d,c.name),i.info("newGraph edges ",n.edges(),n.edge(n.edges()[0]));else{let N=p?s:c.v,X=E?s:c.w;i.info("Rebinding cross-boundary edge as ",N,X,d,c.name),r.setEdge(N,X,d,c.name)}}else i.info("Skipping copy of edge ",c.v,"-->",c.w," rootId: ",s," clusterId:",e)}catch(h){i.error(h)}})}i.debug("Removing node",o),r.removeNode(o)})},"copy"),k=f((e,r)=>{let n=r.children(e),s=[...n];for(let t of n)S.set(t,e),s=[...s,...k(t,r)];return s},"extractDescendants");var B=f((e,r,n)=>{let s=e.edges().filter(c=>c.v===r||c.w===r),t=e.edges().filter(c=>c.v===n||c.w===n),o=s.map(c=>({v:c.v===r?n:c.v,w:c.w===r?r:c.w})),l=t.map(c=>({v:c.v,w:c.w}));return o.filter(c=>l.some(d=>c.v===d.v&&c.w===d.w))},"findCommonEdges"),x=f((e,r,n)=>{let s=r.children(e);if(i.trace("Searching children of id ",e,s),s.length<1)return e;let t;for(let o of s){let l=x(o,r,n),u=B(r,n,l);if(l)if(u.length>0)t=l;else return l}return t},"findNonClusterChild"),y=f(e=>!a.has(e)||!a.get(e).externalConnections?e:a.has(e)?a.get(e).id:e,"getAnchorId"),q=f((e,r)=>{if(!e||r>10){i.debug("Opting out, no graph ");return}else i.debug("Opting in, graph ");e.nodes().forEach(function(n){e.children(n).length>0&&(i.warn("Cluster identified",n," Replacement id in edges: ",x(n,e,n)),w.set(n,k(n,e)),a.set(n,{id:x(n,e,n),clusterData:e.node(n)}))}),e.nodes().forEach(function(n){let s=e.children(n),t=e.edges();s.length>0?(i.debug("Cluster identified",n,w),t.forEach(o=>{let l=v(o.v,n),u=v(o.w,n);l^u&&(i.warn("Edge: ",o," leaves cluster ",n),i.warn("Descendants of XXX ",n,": ",w.get(n)),a.get(n).externalConnections=!0)})):i.debug("Not a cluster ",n,w)});for(let n of a.keys()){let s=a.get(n).id,t=e.parent(s);t!==n&&a.has(t)&&!a.get(t).externalConnections&&(a.get(n).id=t);let o=e.edges().some(l=>l.v===n);if(s&&a.get(n)?.externalConnections&&o&&T(e,s,n)){let l=F(e,n,e.parent(s));l&&(a.get(n).id=l)}}e.edges().forEach(function(n){let s=e.edge(n);i.warn("Edge "+n.v+" -> "+n.w+": "+JSON.stringify(n)),i.warn("Edge "+n.v+" -> "+n.w+": "+JSON.stringify(e.edge(n)));let t=n.v,o=n.w;if(i.warn("Fix XXX",a,"ids:",n.v,n.w,"Translating: ",a.get(n.v)," --- ",a.get(n.w)),a.get(n.v)||a.get(n.w)){if(i.warn("Fixing and trying - removing XXX",n.v,n.w,n.name),t=y(n.v),o=y(n.w),e.removeEdge(n.v,n.w,n.name),t!==n.v){let l=e.parent(t);a.get(l).externalConnections=!0,s.fromCluster=n.v}if(o!==n.w){let l=e.parent(o);a.get(l).externalConnections=!0,s.toCluster=n.w}i.warn("Fix Replacing with XXX",t,o,n.name),e.setEdge(t,o,s,n.name)}}),i.warn("Adjusted Graph",m(e)),J(e,0),i.trace(a)},"adjustClustersAndEdges"),J=f((e,r)=>{if(i.warn("extractor - ",r,m(e),e.children("D")),r>10){i.error("Bailing out");return}let n=e.nodes(),s=!1;for(let t of n){let o=e.children(t);s=s||o.length>0}if(!s){i.debug("Done, no node has children",e.nodes());return}i.debug("Nodes = ",n,r);for(let t of n)if(i.debug("Extracting node",t,a,a.has(t)&&!a.get(t).externalConnections,!e.parent(t),e.node(t),e.children("D")," Depth ",r),!a.has(t))i.debug("Not a cluster",t,r);else if(a.get(t)?.clusterData?.explicitDir&&e.children(t)&&e.children(t).length>0){i.warn("Cluster with explicit dir, creating subgraph for children",t,r);let o=a.get(t).clusterData.dir,l=new b({multigraph:!0,compound:!0}).setGraph({rankdir:o,nodesep:50,ranksep:50,marginx:8,marginy:8}).setDefaultEdgeLabel(function(){return{}});D(t,e,l,t);let u=e.node(t)||{};e.setNode(t,{...u,clusterNode:!0,id:t,clusterData:a.get(t).clusterData,label:a.get(t).label,graph:l}),i.warn("Subgraph for cluster with explicit dir created:",t,m(l))}else if(!a.get(t).externalConnections&&e.children(t)&&e.children(t).length>0){i.warn("Cluster without external connections, without a parent and with children",t,r);let l=e.graph().rankdir==="TB"?"LR":"TB";a.get(t)?.clusterData?.dir&&(l=a.get(t).clusterData.dir,i.warn("Fixing dir",a.get(t).clusterData.dir,l));let u=new b({multigraph:!0,compound:!0}).setGraph({rankdir:l,nodesep:50,ranksep:50,marginx:8,marginy:8}).setDefaultEdgeLabel(function(){return{}});D(t,e,u,t);let c=e.node(t)||{};e.setNode(t,{...c,clusterNode:!0,id:t,clusterData:a.get(t).clusterData,label:a.get(t).label,graph:u}),i.debug("Old graph after copy",m(e))}else i.warn("Cluster ** ",t," **not meeting the criteria !externalConnections:",!a.get(t).externalConnections," no parent: ",!e.parent(t)," children ",e.children(t)&&e.children(t).length>0,e.children("D"),r),i.debug(a);n=e.nodes(),i.warn("New list of nodes",n);for(let t of n){let o=e.node(t);i.warn(" Now next level",t,o),o?.clusterNode&&J(o.graph,r+1)}},"extractor"),O=f((e,r)=>{if(r.length===0)return[];let n=Object.assign([],r);return r.forEach(s=>{let t=e.children(s),o=O(e,t);n=[...n,...o]}),n},"sorter"),z=f(e=>O(e,e.children()),"sortNodesByHierarchy"),T=f((e,r,n)=>{let s=e.parent(r);for(;s&&s!==n;){let t=a.get(s);if(t&&!t.externalConnections)return!0;s=e.parent(s)}return!1},"isNodeInExtractableCluster"),F=f((e,r,n)=>{let s=e.children(r)??[];for(let t of s){if(t===n||v(t,n))continue;let o=x(t,e,r);if(o&&!T(e,o,r))return o}return null},"findSafeAnchorNode");export{m as a,a as b,W as c,x as d,q as e,z as f};