UNPKG

5.97 kBJavaScriptView Raw
1import { p as parser, d as db, s as styles } from "./styles-237fcbdf.js";
2import { 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, l as log, i as configureSvgSize } from "./mermaid-a953d906.js";
6import { s as svgDraw } from "./svgDraw-6750006d.js";
7import "ts-dedent";
8import "dayjs/esm/index.js";
9import "@braintree/sanitize-url";
10import "dompurify";
11import "khroma";
12import "lodash-es/memoize.js";
13import "stylis";
14import "lodash-es/isEmpty.js";
15let idCache = {};
16const padding = 20;
17const getGraphId = function(label) {
18 const foundEntry = Object.entries(idCache).find((entry) => entry[1].label === label);
19 if (foundEntry) {
20 return foundEntry[0];
21 }
22};
23const insertMarkers = function(elem) {
24 elem.append("defs").append("marker").attr("id", "extensionStart").attr("class", "extension").attr("refX", 0).attr("refY", 7).attr("markerWidth", 190).attr("markerHeight", 240).attr("orient", "auto").append("path").attr("d", "M 1,7 L18,13 V 1 Z");
25 elem.append("defs").append("marker").attr("id", "extensionEnd").attr("refX", 19).attr("refY", 7).attr("markerWidth", 20).attr("markerHeight", 28).attr("orient", "auto").append("path").attr("d", "M 1,1 V 13 L18,7 Z");
26 elem.append("defs").append("marker").attr("id", "compositionStart").attr("class", "extension").attr("refX", 0).attr("refY", 7).attr("markerWidth", 190).attr("markerHeight", 240).attr("orient", "auto").append("path").attr("d", "M 18,7 L9,13 L1,7 L9,1 Z");
27 elem.append("defs").append("marker").attr("id", "compositionEnd").attr("refX", 19).attr("refY", 7).attr("markerWidth", 20).attr("markerHeight", 28).attr("orient", "auto").append("path").attr("d", "M 18,7 L9,13 L1,7 L9,1 Z");
28 elem.append("defs").append("marker").attr("id", "aggregationStart").attr("class", "extension").attr("refX", 0).attr("refY", 7).attr("markerWidth", 190).attr("markerHeight", 240).attr("orient", "auto").append("path").attr("d", "M 18,7 L9,13 L1,7 L9,1 Z");
29 elem.append("defs").append("marker").attr("id", "aggregationEnd").attr("refX", 19).attr("refY", 7).attr("markerWidth", 20).attr("markerHeight", 28).attr("orient", "auto").append("path").attr("d", "M 18,7 L9,13 L1,7 L9,1 Z");
30 elem.append("defs").append("marker").attr("id", "dependencyStart").attr("class", "extension").attr("refX", 0).attr("refY", 7).attr("markerWidth", 190).attr("markerHeight", 240).attr("orient", "auto").append("path").attr("d", "M 5,7 L9,13 L1,7 L9,1 Z");
31 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 18,7 L9,13 L14,7 L9,1 Z");
32};
33const draw = function(text, id, _version, diagObj) {
34 const conf = getConfig().class;
35 idCache = {};
36 log.info("Rendering diagram " + text);
37 const securityLevel = getConfig().securityLevel;
38 let sandboxElement;
39 if (securityLevel === "sandbox") {
40 sandboxElement = select("#i" + id);
41 }
42 const root = securityLevel === "sandbox" ? select(sandboxElement.nodes()[0].contentDocument.body) : select("body");
43 const diagram2 = root.select(`[id='${id}']`);
44 insertMarkers(diagram2);
45 const g = new graphlib.Graph({
46 multigraph: true
47 });
48 g.setGraph({
49 isMultiGraph: true
50 });
51 g.setDefaultEdgeLabel(function() {
52 return {};
53 });
54 const classes = diagObj.db.getClasses();
55 const keys = Object.keys(classes);
56 for (const key of keys) {
57 const classDef = classes[key];
58 const node = svgDraw.drawClass(diagram2, classDef, conf, diagObj);
59 idCache[node.id] = node;
60 g.setNode(node.id, node);
61 log.info("Org height: " + node.height);
62 }
63 const relations = diagObj.db.getRelations();
64 relations.forEach(function(relation) {
65 log.info(
66 "tjoho" + getGraphId(relation.id1) + getGraphId(relation.id2) + JSON.stringify(relation)
67 );
68 g.setEdge(
69 getGraphId(relation.id1),
70 getGraphId(relation.id2),
71 {
72 relation
73 },
74 relation.title || "DEFAULT"
75 );
76 });
77 const notes = diagObj.db.getNotes();
78 notes.forEach(function(note) {
79 log.debug(`Adding note: ${JSON.stringify(note)}`);
80 const node = svgDraw.drawNote(diagram2, note, conf, diagObj);
81 idCache[node.id] = node;
82 g.setNode(node.id, node);
83 if (note.class && note.class in classes) {
84 g.setEdge(
85 note.id,
86 getGraphId(note.class),
87 {
88 relation: {
89 id1: note.id,
90 id2: note.class,
91 relation: {
92 type1: "none",
93 type2: "none",
94 lineType: 10
95 }
96 }
97 },
98 "DEFAULT"
99 );
100 }
101 });
102 layout(g);
103 g.nodes().forEach(function(v) {
104 if (v !== void 0 && g.node(v) !== void 0) {
105 log.debug("Node " + v + ": " + JSON.stringify(g.node(v)));
106 root.select("#" + (diagObj.db.lookUpDomId(v) || v)).attr(
107 "transform",
108 "translate(" + (g.node(v).x - g.node(v).width / 2) + "," + (g.node(v).y - g.node(v).height / 2) + " )"
109 );
110 }
111 });
112 g.edges().forEach(function(e) {
113 if (e !== void 0 && g.edge(e) !== void 0) {
114 log.debug("Edge " + e.v + " -> " + e.w + ": " + JSON.stringify(g.edge(e)));
115 svgDraw.drawEdge(diagram2, g.edge(e), g.edge(e).relation, conf, diagObj);
116 }
117 });
118 const svgBounds = diagram2.node().getBBox();
119 const width = svgBounds.width + padding * 2;
120 const height = svgBounds.height + padding * 2;
121 configureSvgSize(diagram2, height, width, conf.useMaxWidth);
122 const vBox = `${svgBounds.x - padding} ${svgBounds.y - padding} ${width} ${height}`;
123 log.debug(`viewBox ${vBox}`);
124 diagram2.attr("viewBox", vBox);
125};
126const renderer = {
127 draw
128};
129const diagram = {
130 parser,
131 db,
132 renderer,
133 styles,
134 init: (cnf) => {
135 if (!cnf.class) {
136 cnf.class = {};
137 }
138 cnf.class.arrowMarkerAbsolute = cnf.arrowMarkerAbsolute;
139 db.clear();
140 }
141};
142export {
143 diagram
144};