UNPKG

9.67 kBJavaScriptView Raw
1import { p as parser, d as db, s as styles } from "./styles-38a11c49.js";
2import { l as log, c as getConfig, j as select, x as utils, t as setupGraphViewbox, r as interpolateToCurve, o as curveLinear, p as getStylesFromArray, f as common } from "./mermaid-4b4b971d.js";
3import { G as Graph } from "./layout-e57aec3f.js";
4import { r as render } from "./index-f7dc402e.js";
5import "./edges-a0c69811.js";
6import "./createText-b06b2794.js";
7import "./svgDraw-1b15aedc.js";
8import "./line-53c588d2.js";
9import "./array-b7dcf730.js";
10import "./constant-b644328d.js";
11const sanitizeText = (txt) => common.sanitizeText(txt, getConfig());
12let conf = {
13 dividerMargin: 10,
14 padding: 5,
15 textHeight: 10,
16 curve: void 0
17};
18const addNamespaces = function(namespaces, g, _id, diagObj) {
19 const keys = Object.keys(namespaces);
20 log.info("keys:", keys);
21 log.info(namespaces);
22 keys.forEach(function(id) {
23 var _a, _b;
24 const vertex = namespaces[id];
25 const shape = "rect";
26 const node = {
27 shape,
28 id: vertex.id,
29 domId: vertex.domId,
30 labelText: sanitizeText(vertex.id),
31 labelStyle: "",
32 style: "fill: none; stroke: black",
33 // TODO V10: Flowchart ? Keeping flowchart for backwards compatibility. Remove in next major release
34 padding: ((_a = getConfig().flowchart) == null ? void 0 : _a.padding) ?? ((_b = getConfig().class) == null ? void 0 : _b.padding)
35 };
36 g.setNode(vertex.id, node);
37 addClasses(vertex.classes, g, _id, diagObj, vertex.id);
38 log.info("setNode", node);
39 });
40};
41const addClasses = function(classes, g, _id, diagObj, parent) {
42 const keys = Object.keys(classes);
43 log.info("keys:", keys);
44 log.info(classes);
45 keys.forEach(function(id) {
46 var _a, _b;
47 const vertex = classes[id];
48 let cssClassStr = "";
49 if (vertex.cssClasses.length > 0) {
50 cssClassStr = cssClassStr + " " + vertex.cssClasses.join(" ");
51 }
52 const styles2 = { labelStyle: "", style: "" };
53 const vertexText = vertex.label ?? vertex.id;
54 const radius = 0;
55 const shape = "class_box";
56 const node = {
57 labelStyle: styles2.labelStyle,
58 shape,
59 labelText: sanitizeText(vertexText),
60 classData: vertex,
61 rx: radius,
62 ry: radius,
63 class: cssClassStr,
64 style: styles2.style,
65 id: vertex.id,
66 domId: vertex.domId,
67 tooltip: diagObj.db.getTooltip(vertex.id, parent) || "",
68 haveCallback: vertex.haveCallback,
69 link: vertex.link,
70 width: vertex.type === "group" ? 500 : void 0,
71 type: vertex.type,
72 // TODO V10: Flowchart ? Keeping flowchart for backwards compatibility. Remove in next major release
73 padding: ((_a = getConfig().flowchart) == null ? void 0 : _a.padding) ?? ((_b = getConfig().class) == null ? void 0 : _b.padding)
74 };
75 g.setNode(vertex.id, node);
76 if (parent) {
77 g.setParent(vertex.id, parent);
78 }
79 log.info("setNode", node);
80 });
81};
82const addNotes = function(notes, g, startEdgeId, classes) {
83 log.info(notes);
84 notes.forEach(function(note, i) {
85 var _a, _b;
86 const vertex = note;
87 const cssNoteStr = "";
88 const styles2 = { labelStyle: "", style: "" };
89 const vertexText = vertex.text;
90 const radius = 0;
91 const shape = "note";
92 const node = {
93 labelStyle: styles2.labelStyle,
94 shape,
95 labelText: sanitizeText(vertexText),
96 noteData: vertex,
97 rx: radius,
98 ry: radius,
99 class: cssNoteStr,
100 style: styles2.style,
101 id: vertex.id,
102 domId: vertex.id,
103 tooltip: "",
104 type: "note",
105 // TODO V10: Flowchart ? Keeping flowchart for backwards compatibility. Remove in next major release
106 padding: ((_a = getConfig().flowchart) == null ? void 0 : _a.padding) ?? ((_b = getConfig().class) == null ? void 0 : _b.padding)
107 };
108 g.setNode(vertex.id, node);
109 log.info("setNode", node);
110 if (!vertex.class || !(vertex.class in classes)) {
111 return;
112 }
113 const edgeId = startEdgeId + i;
114 const edgeData = {
115 id: `edgeNote${edgeId}`,
116 //Set relationship style and line type
117 classes: "relation",
118 pattern: "dotted",
119 // Set link type for rendering
120 arrowhead: "none",
121 //Set edge extra labels
122 startLabelRight: "",
123 endLabelLeft: "",
124 //Set relation arrow types
125 arrowTypeStart: "none",
126 arrowTypeEnd: "none",
127 style: "fill:none",
128 labelStyle: "",
129 curve: interpolateToCurve(conf.curve, curveLinear)
130 };
131 g.setEdge(vertex.id, vertex.class, edgeData, edgeId);
132 });
133};
134const addRelations = function(relations, g) {
135 const conf2 = getConfig().flowchart;
136 let cnt = 0;
137 relations.forEach(function(edge) {
138 var _a;
139 cnt++;
140 const edgeData = {
141 //Set relationship style and line type
142 classes: "relation",
143 pattern: edge.relation.lineType == 1 ? "dashed" : "solid",
144 id: "id" + cnt,
145 // Set link type for rendering
146 arrowhead: edge.type === "arrow_open" ? "none" : "normal",
147 //Set edge extra labels
148 startLabelRight: edge.relationTitle1 === "none" ? "" : edge.relationTitle1,
149 endLabelLeft: edge.relationTitle2 === "none" ? "" : edge.relationTitle2,
150 //Set relation arrow types
151 arrowTypeStart: getArrowMarker(edge.relation.type1),
152 arrowTypeEnd: getArrowMarker(edge.relation.type2),
153 style: "fill:none",
154 labelStyle: "",
155 curve: interpolateToCurve(conf2 == null ? void 0 : conf2.curve, curveLinear)
156 };
157 log.info(edgeData, edge);
158 if (edge.style !== void 0) {
159 const styles2 = getStylesFromArray(edge.style);
160 edgeData.style = styles2.style;
161 edgeData.labelStyle = styles2.labelStyle;
162 }
163 edge.text = edge.title;
164 if (edge.text === void 0) {
165 if (edge.style !== void 0) {
166 edgeData.arrowheadStyle = "fill: #333";
167 }
168 } else {
169 edgeData.arrowheadStyle = "fill: #333";
170 edgeData.labelpos = "c";
171 if (((_a = getConfig().flowchart) == null ? void 0 : _a.htmlLabels) ?? getConfig().htmlLabels) {
172 edgeData.labelType = "html";
173 edgeData.label = '<span class="edgeLabel">' + edge.text + "</span>";
174 } else {
175 edgeData.labelType = "text";
176 edgeData.label = edge.text.replace(common.lineBreakRegex, "\n");
177 if (edge.style === void 0) {
178 edgeData.style = edgeData.style || "stroke: #333; stroke-width: 1.5px;fill:none";
179 }
180 edgeData.labelStyle = edgeData.labelStyle.replace("color:", "fill:");
181 }
182 }
183 g.setEdge(edge.id1, edge.id2, edgeData, cnt);
184 });
185};
186const setConf = function(cnf) {
187 conf = {
188 ...conf,
189 ...cnf
190 };
191};
192const draw = async function(text, id, _version, diagObj) {
193 log.info("Drawing class - ", id);
194 const conf2 = getConfig().flowchart ?? getConfig().class;
195 const securityLevel = getConfig().securityLevel;
196 log.info("config:", conf2);
197 const nodeSpacing = (conf2 == null ? void 0 : conf2.nodeSpacing) ?? 50;
198 const rankSpacing = (conf2 == null ? void 0 : conf2.rankSpacing) ?? 50;
199 const g = new Graph({
200 multigraph: true,
201 compound: true
202 }).setGraph({
203 rankdir: diagObj.db.getDirection(),
204 nodesep: nodeSpacing,
205 ranksep: rankSpacing,
206 marginx: 8,
207 marginy: 8
208 }).setDefaultEdgeLabel(function() {
209 return {};
210 });
211 const namespaces = diagObj.db.getNamespaces();
212 const classes = diagObj.db.getClasses();
213 const relations = diagObj.db.getRelations();
214 const notes = diagObj.db.getNotes();
215 log.info(relations);
216 addNamespaces(namespaces, g, id, diagObj);
217 addClasses(classes, g, id, diagObj);
218 addRelations(relations, g);
219 addNotes(notes, g, relations.length + 1, classes);
220 let sandboxElement;
221 if (securityLevel === "sandbox") {
222 sandboxElement = select("#i" + id);
223 }
224 const root = securityLevel === "sandbox" ? (
225 // @ts-ignore Ignore type error for now
226 select(sandboxElement.nodes()[0].contentDocument.body)
227 ) : select("body");
228 const svg = root.select(`[id="${id}"]`);
229 const element = root.select("#" + id + " g");
230 await render(
231 element,
232 g,
233 ["aggregation", "extension", "composition", "dependency", "lollipop"],
234 "classDiagram",
235 id
236 );
237 utils.insertTitle(svg, "classTitleText", (conf2 == null ? void 0 : conf2.titleTopMargin) ?? 5, diagObj.db.getDiagramTitle());
238 setupGraphViewbox(g, svg, conf2 == null ? void 0 : conf2.diagramPadding, conf2 == null ? void 0 : conf2.useMaxWidth);
239 if (!(conf2 == null ? void 0 : conf2.htmlLabels)) {
240 const doc = securityLevel === "sandbox" ? sandboxElement.nodes()[0].contentDocument : document;
241 const labels = doc.querySelectorAll('[id="' + id + '"] .edgeLabel .label');
242 for (const label of labels) {
243 const dim = label.getBBox();
244 const rect = doc.createElementNS("http://www.w3.org/2000/svg", "rect");
245 rect.setAttribute("rx", 0);
246 rect.setAttribute("ry", 0);
247 rect.setAttribute("width", dim.width);
248 rect.setAttribute("height", dim.height);
249 label.insertBefore(rect, label.firstChild);
250 }
251 }
252};
253function getArrowMarker(type) {
254 let marker;
255 switch (type) {
256 case 0:
257 marker = "aggregation";
258 break;
259 case 1:
260 marker = "extension";
261 break;
262 case 2:
263 marker = "composition";
264 break;
265 case 3:
266 marker = "dependency";
267 break;
268 case 4:
269 marker = "lollipop";
270 break;
271 default:
272 marker = "none";
273 }
274 return marker;
275}
276const renderer = {
277 setConf,
278 draw
279};
280const diagram = {
281 parser,
282 db,
283 renderer,
284 styles,
285 init: (cnf) => {
286 if (!cnf.class) {
287 cnf.class = {};
288 }
289 cnf.class.arrowMarkerAbsolute = cnf.arrowMarkerAbsolute;
290 db.clear();
291 }
292};
293export {
294 diagram
295};