UNPKG

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