UNPKG

15.4 kBJavaScriptView Raw
1import * as graphlib from "dagre-d3-es/src/graphlib/index.js";
2import { curveLinear, select, selectAll } from "d3";
3import { k as getStylesFromArray, l as log, m as evaluate, c as getConfig, e as common, r as renderKatex, n as interpolateToCurve, u as utils, o as setupGraphViewbox } from "./mermaid-6dc72991.js";
4import { r as render } from "./index-01f381cb.js";
5import { addHtmlLabel } from "dagre-d3-es/src/dagre-js/label/add-html-label.js";
6import * as khroma from "khroma";
7const conf = {};
8const setConf = function(cnf) {
9 const keys = Object.keys(cnf);
10 for (const key of keys) {
11 conf[key] = cnf[key];
12 }
13};
14const addVertices = async function(vert, g, svgId, root, doc, diagObj) {
15 const svg = root.select(`[id="${svgId}"]`);
16 const keys = Object.keys(vert);
17 for (const id of keys) {
18 const vertex = vert[id];
19 let classStr = "default";
20 if (vertex.classes.length > 0) {
21 classStr = vertex.classes.join(" ");
22 }
23 classStr = classStr + " flowchart-label";
24 const styles = getStylesFromArray(vertex.styles);
25 let vertexText = vertex.text !== void 0 ? vertex.text : vertex.id;
26 let vertexNode;
27 log.info("vertex", vertex, vertex.labelType);
28 if (vertex.labelType === "markdown") {
29 log.info("vertex", vertex, vertex.labelType);
30 } else {
31 if (evaluate(getConfig().flowchart.htmlLabels)) {
32 const node = {
33 label: vertexText
34 };
35 vertexNode = addHtmlLabel(svg, node).node();
36 vertexNode.parentNode.removeChild(vertexNode);
37 } else {
38 const svgLabel = doc.createElementNS("http://www.w3.org/2000/svg", "text");
39 svgLabel.setAttribute("style", styles.labelStyle.replace("color:", "fill:"));
40 const rows = vertexText.split(common.lineBreakRegex);
41 for (const row of rows) {
42 const tspan = doc.createElementNS("http://www.w3.org/2000/svg", "tspan");
43 tspan.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:space", "preserve");
44 tspan.setAttribute("dy", "1em");
45 tspan.setAttribute("x", "1");
46 tspan.textContent = row;
47 svgLabel.appendChild(tspan);
48 }
49 vertexNode = svgLabel;
50 }
51 }
52 let radius = 0;
53 let _shape = "";
54 switch (vertex.type) {
55 case "round":
56 radius = 5;
57 _shape = "rect";
58 break;
59 case "square":
60 _shape = "rect";
61 break;
62 case "diamond":
63 _shape = "question";
64 break;
65 case "hexagon":
66 _shape = "hexagon";
67 break;
68 case "odd":
69 _shape = "rect_left_inv_arrow";
70 break;
71 case "lean_right":
72 _shape = "lean_right";
73 break;
74 case "lean_left":
75 _shape = "lean_left";
76 break;
77 case "trapezoid":
78 _shape = "trapezoid";
79 break;
80 case "inv_trapezoid":
81 _shape = "inv_trapezoid";
82 break;
83 case "odd_right":
84 _shape = "rect_left_inv_arrow";
85 break;
86 case "circle":
87 _shape = "circle";
88 break;
89 case "ellipse":
90 _shape = "ellipse";
91 break;
92 case "stadium":
93 _shape = "stadium";
94 break;
95 case "subroutine":
96 _shape = "subroutine";
97 break;
98 case "cylinder":
99 _shape = "cylinder";
100 break;
101 case "group":
102 _shape = "rect";
103 break;
104 case "doublecircle":
105 _shape = "doublecircle";
106 break;
107 default:
108 _shape = "rect";
109 }
110 const labelText = await renderKatex(vertexText, getConfig());
111 g.setNode(vertex.id, {
112 labelStyle: styles.labelStyle,
113 shape: _shape,
114 labelText,
115 labelType: vertex.labelType,
116 rx: radius,
117 ry: radius,
118 class: classStr,
119 style: styles.style,
120 id: vertex.id,
121 link: vertex.link,
122 linkTarget: vertex.linkTarget,
123 tooltip: diagObj.db.getTooltip(vertex.id) || "",
124 domId: diagObj.db.lookUpDomId(vertex.id),
125 haveCallback: vertex.haveCallback,
126 width: vertex.type === "group" ? 500 : void 0,
127 dir: vertex.dir,
128 type: vertex.type,
129 props: vertex.props,
130 padding: getConfig().flowchart.padding
131 });
132 log.info("setNode", {
133 labelStyle: styles.labelStyle,
134 labelType: vertex.labelType,
135 shape: _shape,
136 labelText,
137 rx: radius,
138 ry: radius,
139 class: classStr,
140 style: styles.style,
141 id: vertex.id,
142 domId: diagObj.db.lookUpDomId(vertex.id),
143 width: vertex.type === "group" ? 500 : void 0,
144 type: vertex.type,
145 dir: vertex.dir,
146 props: vertex.props,
147 padding: getConfig().flowchart.padding
148 });
149 }
150};
151const addEdges = async function(edges, g, diagObj) {
152 log.info("abc78 edges = ", edges);
153 let cnt = 0;
154 let linkIdCnt = {};
155 let defaultStyle;
156 let defaultLabelStyle;
157 if (edges.defaultStyle !== void 0) {
158 const defaultStyles = getStylesFromArray(edges.defaultStyle);
159 defaultStyle = defaultStyles.style;
160 defaultLabelStyle = defaultStyles.labelStyle;
161 }
162 for (const edge of edges) {
163 cnt++;
164 const linkIdBase = "L-" + edge.start + "-" + edge.end;
165 if (linkIdCnt[linkIdBase] === void 0) {
166 linkIdCnt[linkIdBase] = 0;
167 log.info("abc78 new entry", linkIdBase, linkIdCnt[linkIdBase]);
168 } else {
169 linkIdCnt[linkIdBase]++;
170 log.info("abc78 new entry", linkIdBase, linkIdCnt[linkIdBase]);
171 }
172 let linkId = linkIdBase + "-" + linkIdCnt[linkIdBase];
173 log.info("abc78 new link id to be used is", linkIdBase, linkId, linkIdCnt[linkIdBase]);
174 const linkNameStart = "LS-" + edge.start;
175 const linkNameEnd = "LE-" + edge.end;
176 const edgeData = { style: "", labelStyle: "" };
177 edgeData.minlen = edge.length || 1;
178 if (edge.type === "arrow_open") {
179 edgeData.arrowhead = "none";
180 } else {
181 edgeData.arrowhead = "normal";
182 }
183 edgeData.arrowTypeStart = "arrow_open";
184 edgeData.arrowTypeEnd = "arrow_open";
185 switch (edge.type) {
186 case "double_arrow_cross":
187 edgeData.arrowTypeStart = "arrow_cross";
188 case "arrow_cross":
189 edgeData.arrowTypeEnd = "arrow_cross";
190 break;
191 case "double_arrow_point":
192 edgeData.arrowTypeStart = "arrow_point";
193 case "arrow_point":
194 edgeData.arrowTypeEnd = "arrow_point";
195 break;
196 case "double_arrow_circle":
197 edgeData.arrowTypeStart = "arrow_circle";
198 case "arrow_circle":
199 edgeData.arrowTypeEnd = "arrow_circle";
200 break;
201 }
202 let style = "";
203 let labelStyle = "";
204 switch (edge.stroke) {
205 case "normal":
206 style = "fill:none;";
207 if (defaultStyle !== void 0) {
208 style = defaultStyle;
209 }
210 if (defaultLabelStyle !== void 0) {
211 labelStyle = defaultLabelStyle;
212 }
213 edgeData.thickness = "normal";
214 edgeData.pattern = "solid";
215 break;
216 case "dotted":
217 edgeData.thickness = "normal";
218 edgeData.pattern = "dotted";
219 edgeData.style = "fill:none;stroke-width:2px;stroke-dasharray:3;";
220 break;
221 case "thick":
222 edgeData.thickness = "thick";
223 edgeData.pattern = "solid";
224 edgeData.style = "stroke-width: 3.5px;fill:none;";
225 break;
226 case "invisible":
227 edgeData.thickness = "invisible";
228 edgeData.pattern = "solid";
229 edgeData.style = "stroke-width: 0;fill:none;";
230 break;
231 }
232 if (edge.style !== void 0) {
233 const styles = getStylesFromArray(edge.style);
234 style = styles.style;
235 labelStyle = styles.labelStyle;
236 }
237 edgeData.style = edgeData.style += style;
238 edgeData.labelStyle = edgeData.labelStyle += labelStyle;
239 if (edge.interpolate !== void 0) {
240 edgeData.curve = interpolateToCurve(edge.interpolate, curveLinear);
241 } else if (edges.defaultInterpolate !== void 0) {
242 edgeData.curve = interpolateToCurve(edges.defaultInterpolate, curveLinear);
243 } else {
244 edgeData.curve = interpolateToCurve(conf.curve, curveLinear);
245 }
246 if (edge.text === void 0) {
247 if (edge.style !== void 0) {
248 edgeData.arrowheadStyle = "fill: #333";
249 }
250 } else {
251 edgeData.arrowheadStyle = "fill: #333";
252 edgeData.labelpos = "c";
253 }
254 edgeData.labelType = edge.labelType;
255 edgeData.label = await renderKatex(edge.text.replace(common.lineBreakRegex, "\n"), getConfig());
256 if (edge.style === void 0) {
257 edgeData.style = edgeData.style || "stroke: #333; stroke-width: 1.5px;fill:none;";
258 }
259 edgeData.labelStyle = edgeData.labelStyle.replace("color:", "fill:");
260 edgeData.id = linkId;
261 edgeData.classes = "flowchart-link " + linkNameStart + " " + linkNameEnd;
262 g.setEdge(edge.start, edge.end, edgeData, cnt);
263 }
264};
265const getClasses = function(text, diagObj) {
266 return diagObj.db.getClasses();
267};
268const draw = async function(text, id, _version, diagObj) {
269 log.info("Drawing flowchart");
270 let dir = diagObj.db.getDirection();
271 if (dir === void 0) {
272 dir = "TD";
273 }
274 const { securityLevel, flowchart: conf2 } = getConfig();
275 const nodeSpacing = conf2.nodeSpacing || 50;
276 const rankSpacing = conf2.rankSpacing || 50;
277 let sandboxElement;
278 if (securityLevel === "sandbox") {
279 sandboxElement = select("#i" + id);
280 }
281 const root = securityLevel === "sandbox" ? select(sandboxElement.nodes()[0].contentDocument.body) : select("body");
282 const doc = securityLevel === "sandbox" ? sandboxElement.nodes()[0].contentDocument : document;
283 const g = new graphlib.Graph({
284 multigraph: true,
285 compound: true
286 }).setGraph({
287 rankdir: dir,
288 nodesep: nodeSpacing,
289 ranksep: rankSpacing,
290 marginx: 0,
291 marginy: 0
292 }).setDefaultEdgeLabel(function() {
293 return {};
294 });
295 let subG;
296 const subGraphs = diagObj.db.getSubGraphs();
297 log.info("Subgraphs - ", subGraphs);
298 for (let i2 = subGraphs.length - 1; i2 >= 0; i2--) {
299 subG = subGraphs[i2];
300 log.info("Subgraph - ", subG);
301 diagObj.db.addVertex(
302 subG.id,
303 { text: subG.title, type: subG.labelType },
304 "group",
305 void 0,
306 subG.classes,
307 subG.dir
308 );
309 }
310 const vert = diagObj.db.getVertices();
311 const edges = diagObj.db.getEdges();
312 log.info("Edges", edges);
313 let i = 0;
314 for (i = subGraphs.length - 1; i >= 0; i--) {
315 subG = subGraphs[i];
316 selectAll("cluster").append("text");
317 for (let j = 0; j < subG.nodes.length; j++) {
318 log.info("Setting up subgraphs", subG.nodes[j], subG.id);
319 g.setParent(subG.nodes[j], subG.id);
320 }
321 }
322 await addVertices(vert, g, id, root, doc, diagObj);
323 await addEdges(edges, g);
324 const svg = root.select(`[id="${id}"]`);
325 const element = root.select("#" + id + " g");
326 await render(element, g, ["point", "circle", "cross"], "flowchart", id);
327 utils.insertTitle(svg, "flowchartTitleText", conf2.titleTopMargin, diagObj.db.getDiagramTitle());
328 setupGraphViewbox(g, svg, conf2.diagramPadding, conf2.useMaxWidth);
329 diagObj.db.indexNodes("subGraph" + i);
330 if (!conf2.htmlLabels) {
331 const labels = doc.querySelectorAll('[id="' + id + '"] .edgeLabel .label');
332 for (const label of labels) {
333 const dim = label.getBBox();
334 const rect = doc.createElementNS("http://www.w3.org/2000/svg", "rect");
335 rect.setAttribute("rx", 0);
336 rect.setAttribute("ry", 0);
337 rect.setAttribute("width", dim.width);
338 rect.setAttribute("height", dim.height);
339 label.insertBefore(rect, label.firstChild);
340 }
341 }
342 const keys = Object.keys(vert);
343 keys.forEach(function(key) {
344 const vertex = vert[key];
345 if (vertex.link) {
346 const node = select("#" + id + ' [id="' + key + '"]');
347 if (node) {
348 const link = doc.createElementNS("http://www.w3.org/2000/svg", "a");
349 link.setAttributeNS("http://www.w3.org/2000/svg", "class", vertex.classes.join(" "));
350 link.setAttributeNS("http://www.w3.org/2000/svg", "href", vertex.link);
351 link.setAttributeNS("http://www.w3.org/2000/svg", "rel", "noopener");
352 if (securityLevel === "sandbox") {
353 link.setAttributeNS("http://www.w3.org/2000/svg", "target", "_top");
354 } else if (vertex.linkTarget) {
355 link.setAttributeNS("http://www.w3.org/2000/svg", "target", vertex.linkTarget);
356 }
357 const linkNode = node.insert(function() {
358 return link;
359 }, ":first-child");
360 const shape = node.select(".label-container");
361 if (shape) {
362 linkNode.append(function() {
363 return shape.node();
364 });
365 }
366 const label = node.select(".label");
367 if (label) {
368 linkNode.append(function() {
369 return label.node();
370 });
371 }
372 }
373 }
374 });
375};
376const flowRendererV2 = {
377 setConf,
378 addVertices,
379 addEdges,
380 getClasses,
381 draw
382};
383const fade = (color, opacity) => {
384 const channel = khroma.channel;
385 const r = channel(color, "r");
386 const g = channel(color, "g");
387 const b = channel(color, "b");
388 return khroma.rgba(r, g, b, opacity);
389};
390const getStyles = (options) => `.label {
391 font-family: ${options.fontFamily};
392 color: ${options.nodeTextColor || options.textColor};
393 }
394 .cluster-label text {
395 fill: ${options.titleColor};
396 }
397 .cluster-label span,p {
398 color: ${options.titleColor};
399 }
400
401 .label text,span,p {
402 fill: ${options.nodeTextColor || options.textColor};
403 color: ${options.nodeTextColor || options.textColor};
404 }
405
406 .node rect,
407 .node circle,
408 .node ellipse,
409 .node polygon,
410 .node path {
411 fill: ${options.mainBkg};
412 stroke: ${options.nodeBorder};
413 stroke-width: 1px;
414 }
415 .flowchart-label text {
416 text-anchor: middle;
417 }
418 // .flowchart-label .text-outer-tspan {
419 // text-anchor: middle;
420 // }
421 // .flowchart-label .text-inner-tspan {
422 // text-anchor: start;
423 // }
424
425 .node .katex path {
426 fill: #000;
427 stroke: #000;
428 stroke-width: 1px;
429 }
430
431 .node .label {
432 text-align: center;
433 }
434 .node.clickable {
435 cursor: pointer;
436 }
437
438 .arrowheadPath {
439 fill: ${options.arrowheadColor};
440 }
441
442 .edgePath .path {
443 stroke: ${options.lineColor};
444 stroke-width: 2.0px;
445 }
446
447 .flowchart-link {
448 stroke: ${options.lineColor};
449 fill: none;
450 }
451
452 .edgeLabel {
453 background-color: ${options.edgeLabelBackground};
454 rect {
455 opacity: 0.5;
456 background-color: ${options.edgeLabelBackground};
457 fill: ${options.edgeLabelBackground};
458 }
459 text-align: center;
460 }
461
462 /* For html labels only */
463 .labelBkg {
464 background-color: ${fade(options.edgeLabelBackground, 0.5)};
465 // background-color:
466 }
467
468 .cluster rect {
469 fill: ${options.clusterBkg};
470 stroke: ${options.clusterBorder};
471 stroke-width: 1px;
472 }
473
474 .cluster text {
475 fill: ${options.titleColor};
476 }
477
478 .cluster span,p {
479 color: ${options.titleColor};
480 }
481 /* .cluster div {
482 color: ${options.titleColor};
483 } */
484
485 div.mermaidTooltip {
486 position: absolute;
487 text-align: center;
488 max-width: 200px;
489 padding: 2px;
490 font-family: ${options.fontFamily};
491 font-size: 12px;
492 background: ${options.tertiaryColor};
493 border: 1px solid ${options.border2};
494 border-radius: 2px;
495 pointer-events: none;
496 z-index: 100;
497 }
498
499 .flowchartTitleText {
500 text-anchor: middle;
501 font-size: 18px;
502 fill: ${options.textColor};
503 }
504`;
505const flowStyles = getStyles;
506export {
507 flowStyles as a,
508 flowRendererV2 as f
509};