UNPKG

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