UNPKG

18.7 kBJavaScriptView Raw
1import { d as db, p as parser, s as styles } from "./styles-2797ae0f.js";
2import { line, curveBasis, select } from "d3";
3import { layout } from "dagre-d3-es/src/dagre/index.js";
4import * as graphlib from "dagre-d3-es/src/graphlib/index.js";
5import { c as getConfig, u as utils, e as common, l as log, i as configureSvgSize } from "./mermaid-a953d906.js";
6import "ts-dedent";
7import "dayjs/esm/index.js";
8import "@braintree/sanitize-url";
9import "dompurify";
10import "khroma";
11import "lodash-es/memoize.js";
12import "stylis";
13import "lodash-es/isEmpty.js";
14const idCache = {};
15const set = (key, val) => {
16 idCache[key] = val;
17};
18const get = (k) => idCache[k];
19const keys = () => Object.keys(idCache);
20const size = () => keys().length;
21const idCache$1 = {
22 get,
23 set,
24 keys,
25 size
26};
27const drawStartState = (g) => g.append("circle").attr("class", "start-state").attr("r", getConfig().state.sizeUnit).attr("cx", getConfig().state.padding + getConfig().state.sizeUnit).attr("cy", getConfig().state.padding + getConfig().state.sizeUnit);
28const drawDivider = (g) => g.append("line").style("stroke", "grey").style("stroke-dasharray", "3").attr("x1", getConfig().state.textHeight).attr("class", "divider").attr("x2", getConfig().state.textHeight * 2).attr("y1", 0).attr("y2", 0);
29const drawSimpleState = (g, stateDef) => {
30 const state = g.append("text").attr("x", 2 * getConfig().state.padding).attr("y", getConfig().state.textHeight + 2 * getConfig().state.padding).attr("font-size", getConfig().state.fontSize).attr("class", "state-title").text(stateDef.id);
31 const classBox = state.node().getBBox();
32 g.insert("rect", ":first-child").attr("x", getConfig().state.padding).attr("y", getConfig().state.padding).attr("width", classBox.width + 2 * getConfig().state.padding).attr("height", classBox.height + 2 * getConfig().state.padding).attr("rx", getConfig().state.radius);
33 return state;
34};
35const drawDescrState = (g, stateDef) => {
36 const addTspan = function(textEl, txt, isFirst2) {
37 const tSpan = textEl.append("tspan").attr("x", 2 * getConfig().state.padding).text(txt);
38 if (!isFirst2) {
39 tSpan.attr("dy", getConfig().state.textHeight);
40 }
41 };
42 const title = g.append("text").attr("x", 2 * getConfig().state.padding).attr("y", getConfig().state.textHeight + 1.3 * getConfig().state.padding).attr("font-size", getConfig().state.fontSize).attr("class", "state-title").text(stateDef.descriptions[0]);
43 const titleBox = title.node().getBBox();
44 const titleHeight = titleBox.height;
45 const description = g.append("text").attr("x", getConfig().state.padding).attr(
46 "y",
47 titleHeight + getConfig().state.padding * 0.4 + getConfig().state.dividerMargin + getConfig().state.textHeight
48 ).attr("class", "state-description");
49 let isFirst = true;
50 let isSecond = true;
51 stateDef.descriptions.forEach(function(descr) {
52 if (!isFirst) {
53 addTspan(description, descr, isSecond);
54 isSecond = false;
55 }
56 isFirst = false;
57 });
58 const descrLine = g.append("line").attr("x1", getConfig().state.padding).attr("y1", getConfig().state.padding + titleHeight + getConfig().state.dividerMargin / 2).attr("y2", getConfig().state.padding + titleHeight + getConfig().state.dividerMargin / 2).attr("class", "descr-divider");
59 const descrBox = description.node().getBBox();
60 const width = Math.max(descrBox.width, titleBox.width);
61 descrLine.attr("x2", width + 3 * getConfig().state.padding);
62 g.insert("rect", ":first-child").attr("x", getConfig().state.padding).attr("y", getConfig().state.padding).attr("width", width + 2 * getConfig().state.padding).attr("height", descrBox.height + titleHeight + 2 * getConfig().state.padding).attr("rx", getConfig().state.radius);
63 return g;
64};
65const addTitleAndBox = (g, stateDef, altBkg) => {
66 const pad = getConfig().state.padding;
67 const dblPad = 2 * getConfig().state.padding;
68 const orgBox = g.node().getBBox();
69 const orgWidth = orgBox.width;
70 const orgX = orgBox.x;
71 const title = g.append("text").attr("x", 0).attr("y", getConfig().state.titleShift).attr("font-size", getConfig().state.fontSize).attr("class", "state-title").text(stateDef.id);
72 const titleBox = title.node().getBBox();
73 const titleWidth = titleBox.width + dblPad;
74 let width = Math.max(titleWidth, orgWidth);
75 if (width === orgWidth) {
76 width = width + dblPad;
77 }
78 let startX;
79 const graphBox = g.node().getBBox();
80 if (stateDef.doc)
81 ;
82 startX = orgX - pad;
83 if (titleWidth > orgWidth) {
84 startX = (orgWidth - width) / 2 + pad;
85 }
86 if (Math.abs(orgX - graphBox.x) < pad && titleWidth > orgWidth) {
87 startX = orgX - (titleWidth - orgWidth) / 2;
88 }
89 const lineY = 1 - getConfig().state.textHeight;
90 g.insert("rect", ":first-child").attr("x", startX).attr("y", lineY).attr("class", altBkg ? "alt-composit" : "composit").attr("width", width).attr(
91 "height",
92 graphBox.height + getConfig().state.textHeight + getConfig().state.titleShift + 1
93 ).attr("rx", "0");
94 title.attr("x", startX + pad);
95 if (titleWidth <= orgWidth) {
96 title.attr("x", orgX + (width - dblPad) / 2 - titleWidth / 2 + pad);
97 }
98 g.insert("rect", ":first-child").attr("x", startX).attr(
99 "y",
100 getConfig().state.titleShift - getConfig().state.textHeight - getConfig().state.padding
101 ).attr("width", width).attr("height", getConfig().state.textHeight * 3).attr("rx", getConfig().state.radius);
102 g.insert("rect", ":first-child").attr("x", startX).attr(
103 "y",
104 getConfig().state.titleShift - getConfig().state.textHeight - getConfig().state.padding
105 ).attr("width", width).attr("height", graphBox.height + 3 + 2 * getConfig().state.textHeight).attr("rx", getConfig().state.radius);
106 return g;
107};
108const drawEndState = (g) => {
109 g.append("circle").attr("class", "end-state-outer").attr("r", getConfig().state.sizeUnit + getConfig().state.miniPadding).attr(
110 "cx",
111 getConfig().state.padding + getConfig().state.sizeUnit + getConfig().state.miniPadding
112 ).attr(
113 "cy",
114 getConfig().state.padding + getConfig().state.sizeUnit + getConfig().state.miniPadding
115 );
116 return g.append("circle").attr("class", "end-state-inner").attr("r", getConfig().state.sizeUnit).attr("cx", getConfig().state.padding + getConfig().state.sizeUnit + 2).attr("cy", getConfig().state.padding + getConfig().state.sizeUnit + 2);
117};
118const drawForkJoinState = (g, stateDef) => {
119 let width = getConfig().state.forkWidth;
120 let height = getConfig().state.forkHeight;
121 if (stateDef.parentId) {
122 let tmp = width;
123 width = height;
124 height = tmp;
125 }
126 return g.append("rect").style("stroke", "black").style("fill", "black").attr("width", width).attr("height", height).attr("x", getConfig().state.padding).attr("y", getConfig().state.padding);
127};
128const _drawLongText = (_text, x, y, g) => {
129 let textHeight = 0;
130 const textElem = g.append("text");
131 textElem.style("text-anchor", "start");
132 textElem.attr("class", "noteText");
133 let text = _text.replace(/\r\n/g, "<br/>");
134 text = text.replace(/\n/g, "<br/>");
135 const lines = text.split(common.lineBreakRegex);
136 let tHeight = 1.25 * getConfig().state.noteMargin;
137 for (const line2 of lines) {
138 const txt = line2.trim();
139 if (txt.length > 0) {
140 const span = textElem.append("tspan");
141 span.text(txt);
142 if (tHeight === 0) {
143 const textBounds = span.node().getBBox();
144 tHeight += textBounds.height;
145 }
146 textHeight += tHeight;
147 span.attr("x", x + getConfig().state.noteMargin);
148 span.attr("y", y + textHeight + 1.25 * getConfig().state.noteMargin);
149 }
150 }
151 return { textWidth: textElem.node().getBBox().width, textHeight };
152};
153const drawNote = (text, g) => {
154 g.attr("class", "state-note");
155 const note = g.append("rect").attr("x", 0).attr("y", getConfig().state.padding);
156 const rectElem = g.append("g");
157 const { textWidth, textHeight } = _drawLongText(text, 0, 0, rectElem);
158 note.attr("height", textHeight + 2 * getConfig().state.noteMargin);
159 note.attr("width", textWidth + getConfig().state.noteMargin * 2);
160 return note;
161};
162const drawState = function(elem, stateDef) {
163 const id = stateDef.id;
164 const stateInfo = {
165 id,
166 label: stateDef.id,
167 width: 0,
168 height: 0
169 };
170 const g = elem.append("g").attr("id", id).attr("class", "stateGroup");
171 if (stateDef.type === "start") {
172 drawStartState(g);
173 }
174 if (stateDef.type === "end") {
175 drawEndState(g);
176 }
177 if (stateDef.type === "fork" || stateDef.type === "join") {
178 drawForkJoinState(g, stateDef);
179 }
180 if (stateDef.type === "note") {
181 drawNote(stateDef.note.text, g);
182 }
183 if (stateDef.type === "divider") {
184 drawDivider(g);
185 }
186 if (stateDef.type === "default" && stateDef.descriptions.length === 0) {
187 drawSimpleState(g, stateDef);
188 }
189 if (stateDef.type === "default" && stateDef.descriptions.length > 0) {
190 drawDescrState(g, stateDef);
191 }
192 const stateBox = g.node().getBBox();
193 stateInfo.width = stateBox.width + 2 * getConfig().state.padding;
194 stateInfo.height = stateBox.height + 2 * getConfig().state.padding;
195 idCache$1.set(id, stateInfo);
196 return stateInfo;
197};
198let edgeCount = 0;
199const drawEdge = function(elem, path, relation) {
200 const getRelationType = function(type) {
201 switch (type) {
202 case db.relationType.AGGREGATION:
203 return "aggregation";
204 case db.relationType.EXTENSION:
205 return "extension";
206 case db.relationType.COMPOSITION:
207 return "composition";
208 case db.relationType.DEPENDENCY:
209 return "dependency";
210 }
211 };
212 path.points = path.points.filter((p) => !Number.isNaN(p.y));
213 const lineData = path.points;
214 const lineFunction = line().x(function(d) {
215 return d.x;
216 }).y(function(d) {
217 return d.y;
218 }).curve(curveBasis);
219 const svgPath = elem.append("path").attr("d", lineFunction(lineData)).attr("id", "edge" + edgeCount).attr("class", "transition");
220 let url = "";
221 if (getConfig().state.arrowMarkerAbsolute) {
222 url = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search;
223 url = url.replace(/\(/g, "\\(");
224 url = url.replace(/\)/g, "\\)");
225 }
226 svgPath.attr(
227 "marker-end",
228 "url(" + url + "#" + getRelationType(db.relationType.DEPENDENCY) + "End)"
229 );
230 if (relation.title !== void 0) {
231 const label = elem.append("g").attr("class", "stateLabel");
232 const { x, y } = utils.calcLabelPosition(path.points);
233 const rows = common.getRows(relation.title);
234 let titleHeight = 0;
235 const titleRows = [];
236 let maxWidth = 0;
237 let minX = 0;
238 for (let i = 0; i <= rows.length; i++) {
239 const title = label.append("text").attr("text-anchor", "middle").text(rows[i]).attr("x", x).attr("y", y + titleHeight);
240 const boundstmp = title.node().getBBox();
241 maxWidth = Math.max(maxWidth, boundstmp.width);
242 minX = Math.min(minX, boundstmp.x);
243 log.info(boundstmp.x, x, y + titleHeight);
244 if (titleHeight === 0) {
245 const titleBox = title.node().getBBox();
246 titleHeight = titleBox.height;
247 log.info("Title height", titleHeight, y);
248 }
249 titleRows.push(title);
250 }
251 let boxHeight = titleHeight * rows.length;
252 if (rows.length > 1) {
253 const heightAdj = (rows.length - 1) * titleHeight * 0.5;
254 titleRows.forEach((title, i) => title.attr("y", y + i * titleHeight - heightAdj));
255 boxHeight = titleHeight * rows.length;
256 }
257 const bounds = label.node().getBBox();
258 label.insert("rect", ":first-child").attr("class", "box").attr("x", x - maxWidth / 2 - getConfig().state.padding / 2).attr("y", y - boxHeight / 2 - getConfig().state.padding / 2 - 3.5).attr("width", maxWidth + getConfig().state.padding).attr("height", boxHeight + getConfig().state.padding);
259 log.info(bounds);
260 }
261 edgeCount++;
262};
263let conf;
264const transformationLog = {};
265const setConf = function() {
266};
267const insertMarkers = function(elem) {
268 elem.append("defs").append("marker").attr("id", "dependencyEnd").attr("refX", 19).attr("refY", 7).attr("markerWidth", 20).attr("markerHeight", 28).attr("orient", "auto").append("path").attr("d", "M 19,7 L9,13 L14,7 L9,1 Z");
269};
270const draw = function(text, id, _version, diagObj) {
271 conf = getConfig().state;
272 const securityLevel = getConfig().securityLevel;
273 let sandboxElement;
274 if (securityLevel === "sandbox") {
275 sandboxElement = select("#i" + id);
276 }
277 const root = securityLevel === "sandbox" ? select(sandboxElement.nodes()[0].contentDocument.body) : select("body");
278 const doc = securityLevel === "sandbox" ? sandboxElement.nodes()[0].contentDocument : document;
279 log.debug("Rendering diagram " + text);
280 const diagram2 = root.select(`[id='${id}']`);
281 insertMarkers(diagram2);
282 const graph = new graphlib.Graph({
283 multigraph: true,
284 compound: true,
285 // acyclicer: 'greedy',
286 rankdir: "RL"
287 // ranksep: '20'
288 });
289 graph.setDefaultEdgeLabel(function() {
290 return {};
291 });
292 const rootDoc = diagObj.db.getRootDoc();
293 renderDoc(rootDoc, diagram2, void 0, false, root, doc, diagObj);
294 const padding = conf.padding;
295 const bounds = diagram2.node().getBBox();
296 const width = bounds.width + padding * 2;
297 const height = bounds.height + padding * 2;
298 const svgWidth = width * 1.75;
299 configureSvgSize(diagram2, height, svgWidth, conf.useMaxWidth);
300 diagram2.attr(
301 "viewBox",
302 `${bounds.x - conf.padding} ${bounds.y - conf.padding} ` + width + " " + height
303 );
304};
305const getLabelWidth = (text) => {
306 return text ? text.length * conf.fontSizeFactor : 1;
307};
308const renderDoc = (doc, diagram2, parentId, altBkg, root, domDocument, diagObj) => {
309 const graph = new graphlib.Graph({
310 compound: true,
311 multigraph: true
312 });
313 let i;
314 let edgeFreeDoc = true;
315 for (i = 0; i < doc.length; i++) {
316 if (doc[i].stmt === "relation") {
317 edgeFreeDoc = false;
318 break;
319 }
320 }
321 if (parentId) {
322 graph.setGraph({
323 rankdir: "LR",
324 multigraph: true,
325 compound: true,
326 // acyclicer: 'greedy',
327 ranker: "tight-tree",
328 ranksep: edgeFreeDoc ? 1 : conf.edgeLengthFactor,
329 nodeSep: edgeFreeDoc ? 1 : 50,
330 isMultiGraph: true
331 // ranksep: 5,
332 // nodesep: 1
333 });
334 } else {
335 graph.setGraph({
336 rankdir: "TB",
337 multigraph: true,
338 compound: true,
339 // isCompound: true,
340 // acyclicer: 'greedy',
341 // ranker: 'longest-path'
342 ranksep: edgeFreeDoc ? 1 : conf.edgeLengthFactor,
343 nodeSep: edgeFreeDoc ? 1 : 50,
344 ranker: "tight-tree",
345 // ranker: 'network-simplex'
346 isMultiGraph: true
347 });
348 }
349 graph.setDefaultEdgeLabel(function() {
350 return {};
351 });
352 diagObj.db.extract(doc);
353 const states = diagObj.db.getStates();
354 const relations = diagObj.db.getRelations();
355 const keys2 = Object.keys(states);
356 for (const key of keys2) {
357 const stateDef = states[key];
358 if (parentId) {
359 stateDef.parentId = parentId;
360 }
361 let node;
362 if (stateDef.doc) {
363 let sub = diagram2.append("g").attr("id", stateDef.id).attr("class", "stateGroup");
364 node = renderDoc(stateDef.doc, sub, stateDef.id, !altBkg, root, domDocument, diagObj);
365 {
366 sub = addTitleAndBox(sub, stateDef, altBkg);
367 let boxBounds = sub.node().getBBox();
368 node.width = boxBounds.width;
369 node.height = boxBounds.height + conf.padding / 2;
370 transformationLog[stateDef.id] = { y: conf.compositTitleSize };
371 }
372 } else {
373 node = drawState(diagram2, stateDef);
374 }
375 if (stateDef.note) {
376 const noteDef = {
377 descriptions: [],
378 id: stateDef.id + "-note",
379 note: stateDef.note,
380 type: "note"
381 };
382 const note = drawState(diagram2, noteDef);
383 if (stateDef.note.position === "left of") {
384 graph.setNode(node.id + "-note", note);
385 graph.setNode(node.id, node);
386 } else {
387 graph.setNode(node.id, node);
388 graph.setNode(node.id + "-note", note);
389 }
390 graph.setParent(node.id, node.id + "-group");
391 graph.setParent(node.id + "-note", node.id + "-group");
392 } else {
393 graph.setNode(node.id, node);
394 }
395 }
396 log.debug("Count=", graph.nodeCount(), graph);
397 let cnt = 0;
398 relations.forEach(function(relation) {
399 cnt++;
400 log.debug("Setting edge", relation);
401 graph.setEdge(
402 relation.id1,
403 relation.id2,
404 {
405 relation,
406 width: getLabelWidth(relation.title),
407 height: conf.labelHeight * common.getRows(relation.title).length,
408 labelpos: "c"
409 },
410 "id" + cnt
411 );
412 });
413 layout(graph);
414 log.debug("Graph after layout", graph.nodes());
415 const svgElem = diagram2.node();
416 graph.nodes().forEach(function(v) {
417 if (v !== void 0 && graph.node(v) !== void 0) {
418 log.warn("Node " + v + ": " + JSON.stringify(graph.node(v)));
419 root.select("#" + svgElem.id + " #" + v).attr(
420 "transform",
421 "translate(" + (graph.node(v).x - graph.node(v).width / 2) + "," + (graph.node(v).y + (transformationLog[v] ? transformationLog[v].y : 0) - graph.node(v).height / 2) + " )"
422 );
423 root.select("#" + svgElem.id + " #" + v).attr("data-x-shift", graph.node(v).x - graph.node(v).width / 2);
424 const dividers = domDocument.querySelectorAll("#" + svgElem.id + " #" + v + " .divider");
425 dividers.forEach((divider) => {
426 const parent = divider.parentElement;
427 let pWidth = 0;
428 let pShift = 0;
429 if (parent) {
430 if (parent.parentElement) {
431 pWidth = parent.parentElement.getBBox().width;
432 }
433 pShift = parseInt(parent.getAttribute("data-x-shift"), 10);
434 if (Number.isNaN(pShift)) {
435 pShift = 0;
436 }
437 }
438 divider.setAttribute("x1", 0 - pShift + 8);
439 divider.setAttribute("x2", pWidth - pShift - 8);
440 });
441 } else {
442 log.debug("No Node " + v + ": " + JSON.stringify(graph.node(v)));
443 }
444 });
445 let stateBox = svgElem.getBBox();
446 graph.edges().forEach(function(e) {
447 if (e !== void 0 && graph.edge(e) !== void 0) {
448 log.debug("Edge " + e.v + " -> " + e.w + ": " + JSON.stringify(graph.edge(e)));
449 drawEdge(diagram2, graph.edge(e), graph.edge(e).relation);
450 }
451 });
452 stateBox = svgElem.getBBox();
453 const stateInfo = {
454 id: parentId ? parentId : "root",
455 label: parentId ? parentId : "root",
456 width: 0,
457 height: 0
458 };
459 stateInfo.width = stateBox.width + 2 * conf.padding;
460 stateInfo.height = stateBox.height + 2 * conf.padding;
461 log.debug("Doc rendered", stateInfo, graph);
462 return stateInfo;
463};
464const renderer = {
465 setConf,
466 draw
467};
468const diagram = {
469 parser,
470 db,
471 renderer,
472 styles,
473 init: (cnf) => {
474 if (!cnf.state) {
475 cnf.state = {};
476 }
477 cnf.state.arrowMarkerAbsolute = cnf.arrowMarkerAbsolute;
478 db.clear();
479 }
480};
481export {
482 diagram
483};