UNPKG

15.8 kBJavaScriptView Raw
1import { i as N, G as A } from "./graph-0ee63739.js";
2import { m as J, l as H } from "./layout-fd473db2.js";
3import { c as V } from "./clone-afc2f047.js";
4import { c as L, g as D, i as p, a as U, b as W, d as _, u as q, s as z, e as K, f as Q, p as O, h as Y, j as Z } from "./edges-16357fde.js";
5import { l as s, c as T, p as S, j as R } from "./mermaid-9f2aa176.js";
6import { a as I } from "./createText-03b82060.js";
7function m(e) {
8 var t = {
9 options: {
10 directed: e.isDirected(),
11 multigraph: e.isMultigraph(),
12 compound: e.isCompound()
13 },
14 nodes: tt(e),
15 edges: et(e)
16 };
17 return N(e.graph()) || (t.value = V(e.graph())), t;
18}
19function tt(e) {
20 return J(e.nodes(), function(t) {
21 var n = e.node(t), r = e.parent(t), i = { v: t };
22 return N(n) || (i.value = n), N(r) || (i.parent = r), i;
23 });
24}
25function et(e) {
26 return J(e.edges(), function(t) {
27 var n = e.edge(t), r = { v: t.v, w: t.w };
28 return N(t.name) || (r.name = t.name), N(n) || (r.value = n), r;
29 });
30}
31let l = {}, g = {}, P = {};
32const nt = () => {
33 g = {}, P = {}, l = {};
34}, B = (e, t) => (s.trace("In isDescendant", t, " ", e, " = ", g[t].includes(e)), !!g[t].includes(e)), it = (e, t) => (s.info("Descendants of ", t, " is ", g[t]), s.info("Edge is ", e), e.v === t || e.w === t ? !1 : g[t] ? g[t].includes(e.v) || B(e.v, t) || B(e.w, t) || g[t].includes(e.w) : (s.debug("Tilt, ", t, ",not in descendants"), !1)), k = (e, t, n, r) => {
35 s.warn(
36 "Copying children of ",
37 e,
38 "root",
39 r,
40 "data",
41 t.node(e),
42 r
43 );
44 const i = t.children(e) || [];
45 e !== r && i.push(e), s.warn("Copying (nodes) clusterId", e, "nodes", i), i.forEach((a) => {
46 if (t.children(a).length > 0)
47 k(a, t, n, r);
48 else {
49 const d = t.node(a);
50 s.info("cp ", a, " to ", r, " with parent ", e), n.setNode(a, d), r !== t.parent(a) && (s.warn("Setting parent", a, t.parent(a)), n.setParent(a, t.parent(a))), e !== r && a !== e ? (s.debug("Setting parent", a, e), n.setParent(a, e)) : (s.info("In copy ", e, "root", r, "data", t.node(e), r), s.debug(
51 "Not Setting parent for node=",
52 a,
53 "cluster!==rootId",
54 e !== r,
55 "node!==clusterId",
56 a !== e
57 ));
58 const u = t.edges(a);
59 s.debug("Copying Edges", u), u.forEach((f) => {
60 s.info("Edge", f);
61 const h = t.edge(f.v, f.w, f.name);
62 s.info("Edge data", h, r);
63 try {
64 it(f, r) ? (s.info("Copying as ", f.v, f.w, h, f.name), n.setEdge(f.v, f.w, h, f.name), s.info("newGraph edges ", n.edges(), n.edge(n.edges()[0]))) : s.info(
65 "Skipping copy of edge ",
66 f.v,
67 "-->",
68 f.w,
69 " rootId: ",
70 r,
71 " clusterId:",
72 e
73 );
74 } catch (w) {
75 s.error(w);
76 }
77 });
78 }
79 s.debug("Removing node", a), t.removeNode(a);
80 });
81}, $ = (e, t) => {
82 const n = t.children(e);
83 let r = [...n];
84 for (const i of n)
85 P[i] = e, r = [...r, ...$(i, t)];
86 return r;
87}, C = (e, t) => {
88 s.trace("Searching", e);
89 const n = t.children(e);
90 if (s.trace("Searching children of id ", e, n), n.length < 1)
91 return s.trace("This is a valid node", e), e;
92 for (const r of n) {
93 const i = C(r, t);
94 if (i)
95 return s.trace("Found replacement for", e, " => ", i), i;
96 }
97}, X = (e) => !l[e] || !l[e].externalConnections ? e : l[e] ? l[e].id : e, st = (e, t) => {
98 if (!e || t > 10) {
99 s.debug("Opting out, no graph ");
100 return;
101 } else
102 s.debug("Opting in, graph ");
103 e.nodes().forEach(function(n) {
104 e.children(n).length > 0 && (s.warn(
105 "Cluster identified",
106 n,
107 " Replacement id in edges: ",
108 C(n, e)
109 ), g[n] = $(n, e), l[n] = { id: C(n, e), clusterData: e.node(n) });
110 }), e.nodes().forEach(function(n) {
111 const r = e.children(n), i = e.edges();
112 r.length > 0 ? (s.debug("Cluster identified", n, g), i.forEach((a) => {
113 if (a.v !== n && a.w !== n) {
114 const d = B(a.v, n), u = B(a.w, n);
115 d ^ u && (s.warn("Edge: ", a, " leaves cluster ", n), s.warn("Descendants of XXX ", n, ": ", g[n]), l[n].externalConnections = !0);
116 }
117 })) : s.debug("Not a cluster ", n, g);
118 });
119 for (let n of Object.keys(l)) {
120 const r = l[n].id, i = e.parent(r);
121 i !== n && l[i] && !l[i].externalConnections && (l[n].id = i);
122 }
123 e.edges().forEach(function(n) {
124 const r = e.edge(n);
125 s.warn("Edge " + n.v + " -> " + n.w + ": " + JSON.stringify(n)), s.warn("Edge " + n.v + " -> " + n.w + ": " + JSON.stringify(e.edge(n)));
126 let i = n.v, a = n.w;
127 if (s.warn(
128 "Fix XXX",
129 l,
130 "ids:",
131 n.v,
132 n.w,
133 "Translating: ",
134 l[n.v],
135 " --- ",
136 l[n.w]
137 ), l[n.v] && l[n.w] && l[n.v] === l[n.w]) {
138 s.warn("Fixing and trixing link to self - removing XXX", n.v, n.w, n.name), s.warn("Fixing and trixing - removing XXX", n.v, n.w, n.name), i = X(n.v), a = X(n.w), e.removeEdge(n.v, n.w, n.name);
139 const d = n.w + "---" + n.v;
140 e.setNode(d, {
141 domId: d,
142 id: d,
143 labelStyle: "",
144 labelText: r.label,
145 padding: 0,
146 shape: "labelRect",
147 style: ""
148 });
149 const u = structuredClone(r), f = structuredClone(r);
150 u.label = "", u.arrowTypeEnd = "none", f.label = "", u.fromCluster = n.v, f.toCluster = n.v, e.setEdge(i, d, u, n.name + "-cyclic-special"), e.setEdge(d, a, f, n.name + "-cyclic-special");
151 } else if (l[n.v] || l[n.w]) {
152 if (s.warn("Fixing and trixing - removing XXX", n.v, n.w, n.name), i = X(n.v), a = X(n.w), e.removeEdge(n.v, n.w, n.name), i !== n.v) {
153 const d = e.parent(i);
154 l[d].externalConnections = !0, r.fromCluster = n.v;
155 }
156 if (a !== n.w) {
157 const d = e.parent(a);
158 l[d].externalConnections = !0, r.toCluster = n.w;
159 }
160 s.warn("Fix Replacing with XXX", i, a, n.name), e.setEdge(i, a, r, n.name);
161 }
162 }), s.warn("Adjusted Graph", m(e)), F(e, 0), s.trace(l);
163}, F = (e, t) => {
164 if (s.warn("extractor - ", t, m(e), e.children("D")), t > 10) {
165 s.error("Bailing out");
166 return;
167 }
168 let n = e.nodes(), r = !1;
169 for (const i of n) {
170 const a = e.children(i);
171 r = r || a.length > 0;
172 }
173 if (!r) {
174 s.debug("Done, no node has children", e.nodes());
175 return;
176 }
177 s.debug("Nodes = ", n, t);
178 for (const i of n)
179 if (s.debug(
180 "Extracting node",
181 i,
182 l,
183 l[i] && !l[i].externalConnections,
184 !e.parent(i),
185 e.node(i),
186 e.children("D"),
187 " Depth ",
188 t
189 ), !l[i])
190 s.debug("Not a cluster", i, t);
191 else if (!l[i].externalConnections && // !graph.parent(node) &&
192 e.children(i) && e.children(i).length > 0) {
193 s.warn(
194 "Cluster without external connections, without a parent and with children",
195 i,
196 t
197 );
198 let d = e.graph().rankdir === "TB" ? "LR" : "TB";
199 l[i] && l[i].clusterData && l[i].clusterData.dir && (d = l[i].clusterData.dir, s.warn("Fixing dir", l[i].clusterData.dir, d));
200 const u = new A({
201 multigraph: !0,
202 compound: !0
203 }).setGraph({
204 rankdir: d,
205 // Todo: set proper spacing
206 nodesep: 50,
207 ranksep: 50,
208 marginx: 8,
209 marginy: 8
210 }).setDefaultEdgeLabel(function() {
211 return {};
212 });
213 s.warn("Old graph before copy", m(e)), k(i, e, u, i), e.setNode(i, {
214 clusterNode: !0,
215 id: i,
216 clusterData: l[i].clusterData,
217 labelText: l[i].labelText,
218 graph: u
219 }), s.warn("New graph after copy node: (", i, ")", m(u)), s.debug("Old graph after copy", m(e));
220 } else
221 s.warn(
222 "Cluster ** ",
223 i,
224 " **not meeting the criteria !externalConnections:",
225 !l[i].externalConnections,
226 " no parent: ",
227 !e.parent(i),
228 " children ",
229 e.children(i) && e.children(i).length > 0,
230 e.children("D"),
231 t
232 ), s.debug(l);
233 n = e.nodes(), s.warn("New list of nodes", n);
234 for (const i of n) {
235 const a = e.node(i);
236 s.warn(" Now next level", i, a), a.clusterNode && F(a.graph, t + 1);
237 }
238}, G = (e, t) => {
239 if (t.length === 0)
240 return [];
241 let n = Object.assign(t);
242 return t.forEach((r) => {
243 const i = e.children(r), a = G(e, i);
244 n = [...n, ...a];
245 }), n;
246}, rt = (e) => G(e, e.children()), at = (e, t) => {
247 s.info("Creating subgraph rect for ", t.id, t);
248 const n = T(), r = e.insert("g").attr("class", "cluster" + (t.class ? " " + t.class : "")).attr("id", t.id), i = r.insert("rect", ":first-child"), a = S(n.flowchart.htmlLabels), d = r.insert("g").attr("class", "cluster-label"), u = t.labelType === "markdown" ? I(d, t.labelText, { style: t.labelStyle, useHtmlLabels: a }) : d.node().appendChild(L(t.labelText, t.labelStyle, void 0, !0));
249 let f = u.getBBox();
250 if (S(n.flowchart.htmlLabels)) {
251 const c = u.children[0], o = R(u);
252 f = c.getBoundingClientRect(), o.attr("width", f.width), o.attr("height", f.height);
253 }
254 const h = 0 * t.padding, w = h / 2, x = t.width <= f.width + h ? f.width + h : t.width;
255 t.width <= f.width + h ? t.diff = (f.width - t.width) / 2 - t.padding / 2 : t.diff = -t.padding / 2, s.trace("Data ", t, JSON.stringify(t)), i.attr("style", t.style).attr("rx", t.rx).attr("ry", t.ry).attr("x", t.x - x / 2).attr("y", t.y - t.height / 2 - w).attr("width", x).attr("height", t.height + h);
256 const { subGraphTitleTopMargin: v } = D(n);
257 a ? d.attr(
258 "transform",
259 // This puts the label on top of the box instead of inside it
260 `translate(${t.x - f.width / 2}, ${t.y - t.height / 2 + v})`
261 ) : d.attr(
262 "transform",
263 // This puts the label on top of the box instead of inside it
264 `translate(${t.x}, ${t.y - t.height / 2 + v})`
265 );
266 const y = i.node().getBBox();
267 return t.width = y.width, t.height = y.height, t.intersect = function(c) {
268 return p(t, c);
269 }, r;
270}, ct = (e, t) => {
271 const n = e.insert("g").attr("class", "note-cluster").attr("id", t.id), r = n.insert("rect", ":first-child"), i = 0 * t.padding, a = i / 2;
272 r.attr("rx", t.rx).attr("ry", t.ry).attr("x", t.x - t.width / 2 - a).attr("y", t.y - t.height / 2 - a).attr("width", t.width + i).attr("height", t.height + i).attr("fill", "none");
273 const d = r.node().getBBox();
274 return t.width = d.width, t.height = d.height, t.intersect = function(u) {
275 return p(t, u);
276 }, n;
277}, ot = (e, t) => {
278 const n = T(), r = e.insert("g").attr("class", t.classes).attr("id", t.id), i = r.insert("rect", ":first-child"), a = r.insert("g").attr("class", "cluster-label"), d = r.append("rect"), u = a.node().appendChild(L(t.labelText, t.labelStyle, void 0, !0));
279 let f = u.getBBox();
280 if (S(n.flowchart.htmlLabels)) {
281 const c = u.children[0], o = R(u);
282 f = c.getBoundingClientRect(), o.attr("width", f.width), o.attr("height", f.height);
283 }
284 f = u.getBBox();
285 const h = 0 * t.padding, w = h / 2, x = t.width <= f.width + t.padding ? f.width + t.padding : t.width;
286 t.width <= f.width + t.padding ? t.diff = (f.width + t.padding * 0 - t.width) / 2 : t.diff = -t.padding / 2, i.attr("class", "outer").attr("x", t.x - x / 2 - w).attr("y", t.y - t.height / 2 - w).attr("width", x + h).attr("height", t.height + h), d.attr("class", "inner").attr("x", t.x - x / 2 - w).attr("y", t.y - t.height / 2 - w + f.height - 1).attr("width", x + h).attr("height", t.height + h - f.height - 3);
287 const { subGraphTitleTopMargin: v } = D(n);
288 a.attr(
289 "transform",
290 `translate(${t.x - f.width / 2}, ${t.y - t.height / 2 - t.padding / 3 + (S(n.flowchart.htmlLabels) ? 5 : 3) + v})`
291 );
292 const y = i.node().getBBox();
293 return t.height = y.height, t.intersect = function(c) {
294 return p(t, c);
295 }, r;
296}, lt = (e, t) => {
297 const n = e.insert("g").attr("class", t.classes).attr("id", t.id), r = n.insert("rect", ":first-child"), i = 0 * t.padding, a = i / 2;
298 r.attr("class", "divider").attr("x", t.x - t.width / 2 - a).attr("y", t.y - t.height / 2).attr("width", t.width + i).attr("height", t.height + i);
299 const d = r.node().getBBox();
300 return t.width = d.width, t.height = d.height, t.diff = -t.padding / 2, t.intersect = function(u) {
301 return p(t, u);
302 }, n;
303}, ft = { rect: at, roundedWithTitle: ot, noteGroup: ct, divider: lt };
304let j = {};
305const dt = (e, t) => {
306 s.trace("Inserting cluster");
307 const n = t.shape || "rect";
308 j[t.id] = ft[n](e, t);
309}, ut = () => {
310 j = {};
311}, M = async (e, t, n, r, i, a) => {
312 s.info("Graph in recursive render: XXX", m(t), i);
313 const d = t.graph().rankdir;
314 s.trace("Dir in recursive render - dir:", d);
315 const u = e.insert("g").attr("class", "root");
316 t.nodes() ? s.info("Recursive render XXX", t.nodes()) : s.info("No nodes found for", t), t.edges().length > 0 && s.trace("Recursive edges", t.edge(t.edges()[0]));
317 const f = u.insert("g").attr("class", "clusters"), h = u.insert("g").attr("class", "edgePaths"), w = u.insert("g").attr("class", "edgeLabels"), x = u.insert("g").attr("class", "nodes");
318 await Promise.all(
319 t.nodes().map(async function(c) {
320 const o = t.node(c);
321 if (i !== void 0) {
322 const b = JSON.parse(JSON.stringify(i.clusterData));
323 s.info("Setting data for cluster XXX (", c, ") ", b, i), t.setNode(i.id, b), t.parent(c) || (s.trace("Setting parent", c, i.id), t.setParent(c, i.id, b));
324 }
325 if (s.info("(Insert) Node XXX" + c + ": " + JSON.stringify(t.node(c))), o && o.clusterNode) {
326 s.info("Cluster identified", c, o.width, t.node(c));
327 const b = await M(
328 x,
329 o.graph,
330 n,
331 r,
332 t.node(c),
333 a
334 ), E = b.elem;
335 q(o, E), o.diff = b.diff || 0, s.info("Node bounds (abc123)", c, o, o.width, o.x, o.y), z(E, o), s.warn("Recursive render complete ", E, o);
336 } else
337 t.children(c).length > 0 ? (s.info("Cluster - the non recursive path XXX", c, o.id, o, t), s.info(C(o.id, t)), l[o.id] = { id: C(o.id, t), node: o }) : (s.info("Node - the non recursive path", c, o.id, o), await K(x, t.node(c), d));
338 })
339 ), t.edges().forEach(function(c) {
340 const o = t.edge(c.v, c.w, c.name);
341 s.info("Edge " + c.v + " -> " + c.w + ": " + JSON.stringify(c)), s.info("Edge " + c.v + " -> " + c.w + ": ", c, " ", JSON.stringify(t.edge(c))), s.info("Fix", l, "ids:", c.v, c.w, "Translating: ", l[c.v], l[c.w]), Q(w, o);
342 }), t.edges().forEach(function(c) {
343 s.info("Edge " + c.v + " -> " + c.w + ": " + JSON.stringify(c));
344 }), s.info("#############################################"), s.info("### Layout ###"), s.info("#############################################"), s.info(t), H(t), s.info("Graph after layout:", m(t));
345 let v = 0;
346 const { subGraphTitleTotalMargin: y } = D(a);
347 return rt(t).forEach(function(c) {
348 const o = t.node(c);
349 s.info("Position " + c + ": " + JSON.stringify(t.node(c))), s.info(
350 "Position " + c + ": (" + o.x,
351 "," + o.y,
352 ") width: ",
353 o.width,
354 " height: ",
355 o.height
356 ), o && o.clusterNode ? (o.y += y, O(o)) : t.children(c).length > 0 ? (o.height += y, dt(f, o), l[o.id].node = o) : (o.y += y / 2, O(o));
357 }), t.edges().forEach(function(c) {
358 const o = t.edge(c);
359 s.info("Edge " + c.v + " -> " + c.w + ": " + JSON.stringify(o), o), o.points.forEach((E) => E.y += y / 2);
360 const b = Y(h, c, o, l, n, t, r);
361 Z(o, b);
362 }), t.nodes().forEach(function(c) {
363 const o = t.node(c);
364 s.info(c, o.type, o.diff), o.type === "group" && (v = o.diff);
365 }), { elem: u, diff: v };
366}, bt = async (e, t, n, r, i) => {
367 U(e, n, r, i), W(), _(), ut(), nt(), s.warn("Graph at first:", JSON.stringify(m(t))), st(t), s.warn("Graph after:", JSON.stringify(m(t)));
368 const a = T();
369 await M(e, t, r, i, void 0, a);
370};
371export {
372 bt as r
373};