@grethel-labs/excaliplant 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,736 @@
1
+ /**
2
+ * @module render
3
+ *
4
+ * Emits Excalidraw JSON. Each model shape is dispatched to a
5
+ * dedicated `renderXxx()` function that produces one or more
6
+ * Excalidraw primitive elements (rectangle, ellipse, line, arrow,
7
+ * text). The output document is a stand-alone `.excalidraw` file
8
+ * that any Excalidraw front-end can open. The companion module
9
+ * [`src/render/svg.mjs`](./src/render/svg.mjs) converts the same
10
+ * JSON to SVG for the build-time documentation pipeline.
11
+ */
12
+
13
+ // Excalidraw export.
14
+ //
15
+ // Emits plain rectangle / text / arrow elements. Arrows use roughness 0
16
+ // and roundness null so the output stays exactly orthogonal — no wobble,
17
+ // no Excalidraw-internal version-specific arrow fields.
18
+
19
+ import { Box, Plane, Subplane, SequenceDiagram } from "../model/diagram.mjs";
20
+ import { FONT } from "../style/text.mjs";
21
+ import { SIZING } from "../layout/sizing.mjs";
22
+ import { boxColor } from "../style/colors.mjs";
23
+
24
+ import { exportSequenceDiagram } from "./sequence_render.mjs";
25
+
26
+ let idCounter = 0;
27
+ const newId = () => `el_${(idCounter++).toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
28
+
29
+ const baseElement = () => ({
30
+ id: newId(),
31
+ angle: 0,
32
+ strokeStyle: "solid",
33
+ fillStyle: "solid",
34
+ roughness: 0,
35
+ opacity: 100,
36
+ strokeWidth: 2,
37
+ groupIds: [],
38
+ frameId: null,
39
+ roundness: null,
40
+ seed: Math.floor(Math.random() * 2_000_000_000),
41
+ version: 1,
42
+ versionNonce: Math.floor(Math.random() * 2_000_000_000),
43
+ isDeleted: false,
44
+ boundElements: [],
45
+ updated: 1,
46
+ link: null,
47
+ locked: false,
48
+ });
49
+
50
+ function rect({ x, y, width, height, strokeColor, backgroundColor, fillStyle = "solid" }) {
51
+ return {
52
+ ...baseElement(),
53
+ type: "rectangle",
54
+ x, y, width, height,
55
+ strokeColor,
56
+ backgroundColor,
57
+ fillStyle,
58
+ // Typical Excalidraw look: hand-drawn wobble + rounded corners.
59
+ // Roundness type 3 = "proportional" radius (Excalidraw's default
60
+ // for rectangles when the user toggles rounded corners).
61
+ roughness: 1,
62
+ roundness: { type: 3 },
63
+ };
64
+ }
65
+
66
+ function text({ x, y, width, height, value, fontSize, color, align = "left", vAlign = "top" }) {
67
+ return {
68
+ ...baseElement(),
69
+ type: "text",
70
+ x, y,
71
+ width: Math.max(20, width),
72
+ height: Math.max(fontSize * FONT.lineHeight, height),
73
+ strokeColor: color,
74
+ backgroundColor: "transparent",
75
+ text: value,
76
+ originalText: value,
77
+ fontSize,
78
+ fontFamily: FONT.family,
79
+ textAlign: align,
80
+ verticalAlign: vAlign,
81
+ baseline: Math.round(fontSize * 0.85),
82
+ containerId: null,
83
+ lineHeight: FONT.lineHeight,
84
+ strokeWidth: 1,
85
+ };
86
+ }
87
+
88
+ function arrow({ points, strokeColor, dashed, startArrowhead = null, endArrowhead = "arrow" }) {
89
+ if (!points || points.length < 2) return null;
90
+ const first = points[0];
91
+ const rel = points.map((p) => [p.x - first.x, p.y - first.y]);
92
+ return {
93
+ ...baseElement(),
94
+ type: "arrow",
95
+ x: first.x,
96
+ y: first.y,
97
+ width: Math.max(...rel.map(([px]) => px)) - Math.min(...rel.map(([px]) => px)),
98
+ height: Math.max(...rel.map(([, py]) => py)) - Math.min(...rel.map(([, py]) => py)),
99
+ strokeColor,
100
+ backgroundColor: "transparent",
101
+ strokeStyle: dashed ? "dashed" : "solid",
102
+ points: rel,
103
+ lastCommittedPoint: rel[rel.length - 1],
104
+ startBinding: null,
105
+ endBinding: null,
106
+ startArrowhead,
107
+ endArrowhead,
108
+ };
109
+ }
110
+
111
+ function ellipse({ x, y, width, height, strokeColor, backgroundColor, fillStyle = "solid" }) {
112
+ return {
113
+ ...baseElement(),
114
+ type: "ellipse",
115
+ x, y, width, height,
116
+ strokeColor, backgroundColor, fillStyle,
117
+ roughness: 1,
118
+ };
119
+ }
120
+
121
+ function line({ points, strokeColor, dashed = false, strokeWidth = 2 }) {
122
+ if (!points || points.length < 2) return null;
123
+ const first = points[0];
124
+ const rel = points.map((p) => [p.x - first.x, p.y - first.y]);
125
+ return {
126
+ ...baseElement(),
127
+ type: "line",
128
+ x: first.x,
129
+ y: first.y,
130
+ width: Math.max(...rel.map(([px]) => px)) - Math.min(...rel.map(([px]) => px)),
131
+ height: Math.max(...rel.map(([, py]) => py)) - Math.min(...rel.map(([, py]) => py)),
132
+ strokeColor,
133
+ backgroundColor: "transparent",
134
+ strokeStyle: dashed ? "dashed" : "solid",
135
+ strokeWidth,
136
+ points: rel,
137
+ lastCommittedPoint: rel[rel.length - 1],
138
+ startBinding: null,
139
+ endBinding: null,
140
+ startArrowhead: null,
141
+ endArrowhead: null,
142
+ };
143
+ }
144
+
145
+ /**
146
+ * Render a laid-out diagram to an Excalidraw JSON document.
147
+ *
148
+ * @param {import("../model/diagram.mjs").Diagram
149
+ * | import("../model/diagram.mjs").SequenceDiagram} diagram
150
+ * A diagram that has already been processed by
151
+ * {@link import("../layout/elk_layout.mjs").layoutDiagram}.
152
+ * @param {object} [opts]
153
+ * @param {string} [opts.sourceLabel=""] Used as `appState.name`.
154
+ * @param {boolean} [opts.debugCorridors=false]
155
+ * If true, emit the routing corridors (debugging only).
156
+ * @returns {object} Excalidraw JSON.
157
+ */
158
+ export function exportDiagram(diagram, { sourceLabel = "", debugCorridors = false } = {}) {
159
+ idCounter = 0;
160
+ if (diagram instanceof SequenceDiagram) {
161
+ return exportSequenceDiagram(diagram, {
162
+ sourceLabel,
163
+ primitives: { rect, text, arrow, ellipse, line },
164
+ });
165
+ }
166
+ const elements = [];
167
+
168
+ if (debugCorridors) renderDebugCorridors(diagram, elements);
169
+
170
+ for (const plane of diagram.planes) renderPlane(plane, elements);
171
+ for (const conn of diagram.connections) {
172
+ const a = arrow({
173
+ points: conn.path,
174
+ strokeColor: conn.from.plane?.color?.stroke || "#444",
175
+ dashed: conn.dashed,
176
+ startArrowhead: conn.startArrowhead ?? null,
177
+ endArrowhead: conn.endArrowhead ?? "arrow",
178
+ });
179
+ if (a) elements.push(a);
180
+ if (conn.label) {
181
+ const labelEl = renderEdgeLabel(conn);
182
+ if (labelEl) elements.push(labelEl);
183
+ }
184
+ }
185
+
186
+ const appState = {
187
+ viewBackgroundColor: "#ffffff",
188
+ gridSize: null,
189
+ name: sourceLabel,
190
+ };
191
+
192
+ return {
193
+ type: "excalidraw",
194
+ version: 2,
195
+ source: "https://excalidraw.com",
196
+ elements,
197
+ appState,
198
+ files: {},
199
+ };
200
+ }
201
+
202
+ function renderDebugCorridors(diagram, elements) {
203
+ const planes = diagram.planes;
204
+ if (!planes.length) return;
205
+
206
+ const corridors = diagram.corridors;
207
+ if (!corridors || !corridors.vLines.length || !corridors.hLines.length) return;
208
+
209
+ const graph = corridors.graph;
210
+ if (!graph || !graph.nodes.length) return;
211
+
212
+ // Edge styles: lane = red bands, diagonals = orange, box edges = blue
213
+ // hairlines. Bands are rendered as rectangles; thinner "hairlines"
214
+ // use stroke-only rectangles for box-passthrough connectors.
215
+ const STYLES = {
216
+ "lane-v": { thick: 14, fill: "#ff4d4d", stroke: "#d60000", opacity: 55 },
217
+ "lane-h": { thick: 14, fill: "#ff4d4d", stroke: "#d60000", opacity: 55 },
218
+ // Through-diagonals at every crossing — these are the clean 45°
219
+ // segments that absorb the per-row/col offset.
220
+ "cross-v": { thick: 14, fill: "#ffd166", stroke: "#c98a16", opacity: 75 },
221
+ "cross-h": { thick: 14, fill: "#ffd166", stroke: "#c98a16", opacity: 75 },
222
+ // Shortcuts: 45° pieces from each anchor to the next perpendicular
223
+ // lane line. Distinct (teal) so they're visually separable from
224
+ // through-diagonals and from human ground-truth annotations.
225
+ "cross-shortcut": { thick: 10, fill: "#7fd1c4", stroke: "#0f766e", opacity: 70 },
226
+ // Turn edges between non-paired anchors at a crossing.
227
+ "cross-turn": { thick: 3, fill: "#bdbdbd", stroke: "#7d7d7d", opacity: 35 },
228
+ "box": { thick: 4, fill: "#5b9bff", stroke: "#1f5fd1", opacity: 70 },
229
+ "box-spur": { thick: 4, fill: "#9cc2ff", stroke: "#1f5fd1", opacity: 65 },
230
+ };
231
+
232
+ for (const e of graph.edges) {
233
+ const s = STYLES[e.kind];
234
+ if (!s) continue;
235
+ // Lane edges (orthogonal) are drawn as-is. All diagonals
236
+ // (cross-v, cross-h, cross-turn at crossings) are coalesced
237
+ // first: many are collinear (the same crossing piece may be
238
+ // emitted twice when multiple lanes share its midline), so a
239
+ // single band per shared line is enough.
240
+ if (e.kind === "cross-v" || e.kind === "cross-h"
241
+ || e.kind === "cross-turn") continue;
242
+ const el = bandFromEdge(e.x0, e.y0, e.x1, e.y1, s.thick, s.stroke, s.fill);
243
+ el.opacity = s.opacity;
244
+ el.strokeWidth = 1;
245
+ elements.push(el);
246
+ }
247
+
248
+ // Coalesce collinear diagonal edges into single bands.
249
+ const diagEdges = graph.edges.filter(
250
+ (e) => (e.kind === "cross-v" || e.kind === "cross-h"
251
+ || e.kind === "cross-turn")
252
+ && Math.hypot(e.x1 - e.x0, e.y1 - e.y0) > 0.5,
253
+ );
254
+ for (const merged of mergeCollinearEdges(diagEdges)) {
255
+ const style = STYLES[merged.kind] || STYLES["cross-v"];
256
+ const el = bandFromEdge(merged.x0, merged.y0, merged.x1, merged.y1,
257
+ style.thick, style.stroke, style.fill);
258
+ el.opacity = style.opacity;
259
+ el.strokeWidth = 1;
260
+ elements.push(el);
261
+ }
262
+
263
+ // Node markers — small dots so the graph topology is readable.
264
+ const NODE_STYLES = {
265
+ "cross-anchor": { size: 6, fill: "#ffd166", stroke: "#7a4a00" },
266
+ "cross-joint": { size: 8, fill: "#ff8f00", stroke: "#7a4a00" },
267
+ "cross-shortcut-end": { size: 6, fill: "#7fd1c4", stroke: "#0f766e" },
268
+ "box-diagonal-crossing": { size: 8, fill: "#9cc2ff", stroke: "#0a2e6e" },
269
+ "box-side": { size: 10, fill: "#1f5fd1", stroke: "#0a2e6e" },
270
+ "box-center": { size: 12, fill: "#0a2e6e", stroke: "#000000" },
271
+ };
272
+ for (const n of graph.nodes) {
273
+ const s = NODE_STYLES[n.kind];
274
+ if (!s) continue;
275
+ const el = rect({
276
+ x: n.x - s.size / 2,
277
+ y: n.y - s.size / 2,
278
+ width: s.size,
279
+ height: s.size,
280
+ strokeColor: s.stroke,
281
+ backgroundColor: s.fill,
282
+ fillStyle: "solid",
283
+ });
284
+ el.opacity = 80;
285
+ el.strokeWidth = 1;
286
+ elements.push(el);
287
+ }
288
+ }
289
+
290
+ // Group collinear edges and emit one merged segment per group. Two
291
+ // edges are considered collinear when both endpoints of one lie on
292
+ // the (infinite) line through the other within COLINEAR_TOL. We use
293
+ // union-find over all pairs so that A↔B↔C transitively merge into a
294
+ // single group even when A and C are not directly comparable.
295
+ // Within each group the merged segment spans from the min to the max
296
+ // projection onto the shared direction. The output edge's `kind` is
297
+ // the highest-priority kind in the group: cross-v / cross-h
298
+ // (the through diagonals) outrank cross-turn (a routing-only corner
299
+ // piece that should never show as a separate segment).
300
+ const COLINEAR_TOL = 2.5;
301
+
302
+ function mergeCollinearEdges(edges) {
303
+ const PRIO = { "cross-v": 2, "cross-h": 2, "cross-turn": 1 };
304
+ const lines = edges.map(lineFromEdge);
305
+ const parent = edges.map((_, i) => i);
306
+ const find = (i) => (parent[i] === i ? i : (parent[i] = find(parent[i])));
307
+ const union = (a, b) => {
308
+ const ra = find(a), rb = find(b);
309
+ if (ra !== rb) parent[ra] = rb;
310
+ };
311
+ for (let i = 0; i < edges.length; i++) {
312
+ for (let j = i + 1; j < edges.length; j++) {
313
+ if (find(i) === find(j)) continue;
314
+ // Direction must be parallel (allow opposite signs).
315
+ const cross = lines[i].dx * lines[j].dy - lines[i].dy * lines[j].dx;
316
+ if (Math.abs(cross) > 0.05) continue;
317
+ // Both endpoints of edge j must lie on line i.
318
+ if (pointOnLine(edges[j].x0, edges[j].y0, lines[i])
319
+ && pointOnLine(edges[j].x1, edges[j].y1, lines[i])) {
320
+ union(i, j);
321
+ }
322
+ }
323
+ }
324
+ // Bucket edges by root.
325
+ const buckets = new Map();
326
+ for (let i = 0; i < edges.length; i++) {
327
+ const r = find(i);
328
+ if (!buckets.has(r)) buckets.set(r, []);
329
+ buckets.get(r).push(i);
330
+ }
331
+ const out = [];
332
+ for (const idxs of buckets.values()) {
333
+ let kind = edges[idxs[0]].kind;
334
+ for (const i of idxs) {
335
+ if ((PRIO[edges[i].kind] || 0) > (PRIO[kind] || 0)) kind = edges[i].kind;
336
+ }
337
+ // Project all endpoints onto the bucket's primary direction
338
+ // (taken from the first edge — all edges in the bucket are
339
+ // parallel within tolerance).
340
+ const ld = lines[idxs[0]];
341
+ const t = (px, py) => (px - ld.x) * ld.dx + (py - ld.y) * ld.dy;
342
+ const pts = [];
343
+ for (const i of idxs) {
344
+ pts.push({ t: t(edges[i].x0, edges[i].y0), x: edges[i].x0, y: edges[i].y0 });
345
+ pts.push({ t: t(edges[i].x1, edges[i].y1), x: edges[i].x1, y: edges[i].y1 });
346
+ }
347
+ pts.sort((a, b) => a.t - b.t);
348
+ const a = pts[0], b = pts[pts.length - 1];
349
+ out.push({ kind, x0: a.x, y0: a.y, x1: b.x, y1: b.y });
350
+ }
351
+ return out;
352
+ }
353
+
354
+ function lineFromEdge(e) {
355
+ const vx = e.x1 - e.x0;
356
+ const vy = e.y1 - e.y0;
357
+ const len = Math.hypot(vx, vy) || 1;
358
+ return { x: e.x0, y: e.y0, dx: vx / len, dy: vy / len };
359
+ }
360
+
361
+ function pointOnLine(px, py, line) {
362
+ const ex = px - line.x;
363
+ const ey = py - line.y;
364
+ const cross = ex * line.dy - ey * line.dx;
365
+ return Math.abs(cross) <= COLINEAR_TOL;
366
+ }
367
+
368
+ // Render any edge (orthogonal or diagonal) as a rotated rectangle band.
369
+ function bandFromEdge(x0, y0, x1, y1, thick, stroke, fill) {
370
+ const dx = x1 - x0;
371
+ const dy = y1 - y0;
372
+ const len = Math.hypot(dx, dy);
373
+ if (len < 0.001) {
374
+ return rect({
375
+ x: x0 - thick / 2, y: y0 - thick / 2,
376
+ width: thick, height: thick,
377
+ strokeColor: stroke, backgroundColor: fill, fillStyle: "solid",
378
+ });
379
+ }
380
+ const angle = Math.atan2(dy, dx);
381
+ const cx = (x0 + x1) / 2;
382
+ const cy = (y0 + y1) / 2;
383
+ return {
384
+ ...rect({
385
+ x: cx - len / 2,
386
+ y: cy - thick / 2,
387
+ width: len,
388
+ height: thick,
389
+ strokeColor: stroke,
390
+ backgroundColor: fill,
391
+ fillStyle: "solid",
392
+ }),
393
+ angle,
394
+ };
395
+ }
396
+
397
+ function renderPlane(plane, elements) {
398
+ const color = plane.color || { stroke: "#444", fill: "#fafafa", titleFill: "#eaeaea" };
399
+ elements.push(rect({
400
+ x: plane.x, y: plane.y, width: plane.width, height: plane.height,
401
+ strokeColor: color.stroke, backgroundColor: color.fill,
402
+ }));
403
+ renderTitleTab({
404
+ parent: plane, color, title: plane.title, fontSize: FONT.sizePlaneTitle,
405
+ height: SIZING.planeTitleHeight, paddingX: SIZING.planePaddingX,
406
+ }, elements);
407
+
408
+ for (const child of plane.children) {
409
+ if (child instanceof Subplane) renderSubplane(child, color, elements);
410
+ else if (child instanceof Box) renderBox(child, color, elements);
411
+ }
412
+ }
413
+
414
+ function renderSubplane(sub, planeC, elements) {
415
+ const color = sub.color || planeC;
416
+ elements.push(rect({
417
+ x: sub.x, y: sub.y, width: sub.width, height: sub.height,
418
+ strokeColor: color.stroke, backgroundColor: color.fill, fillStyle: "solid",
419
+ }));
420
+ // Subplane title sits inside the subplane, top-left, in border colour.
421
+ const titleX = sub.x + SIZING.subplanePaddingX;
422
+ const titleY = sub.y + SIZING.subplanePaddingY * 0.4;
423
+ elements.push(text({
424
+ x: titleX, y: titleY, width: sub.width - SIZING.subplanePaddingX * 2,
425
+ height: SIZING.subplaneTitleHeight, value: sub.title,
426
+ fontSize: FONT.sizeSubplaneTitle, color: color.stroke,
427
+ }));
428
+ for (const box of sub.boxes) renderBox(box, color, elements);
429
+ }
430
+
431
+ function renderBox(box, parentColor, elements) {
432
+ const color = boxColor(parentColor);
433
+ switch (box.shape) {
434
+ case "actor": return renderActor(box, color, elements);
435
+ case "usecase": return renderUsecase(box, color, elements);
436
+ case "database": return renderDatabase(box, color, elements);
437
+ case "cloud": return renderCloud(box, color, elements);
438
+ case "interface": return renderInterface(box, color, elements);
439
+ case "entity": return renderEntity(box, color, elements);
440
+ case "node": return renderNodeShape(box, color, elements);
441
+ case "class": return renderClass(box, color, elements);
442
+ case "note": return renderNote(box, elements);
443
+ default: return renderRectangleShape(box, color, elements);
444
+ }
445
+ }
446
+
447
+ function renderRectangleShape(box, color, elements) {
448
+ elements.push(rect({
449
+ x: box.x, y: box.y, width: box.width, height: box.height,
450
+ strokeColor: color.stroke, backgroundColor: color.fill,
451
+ }));
452
+ renderBoxText(box, color, elements);
453
+ }
454
+
455
+ function renderBoxText(box, color, elements, { titleColor } = {}) {
456
+ const tx = box.x + SIZING.boxPaddingX;
457
+ const ty = box.y + SIZING.boxPaddingY;
458
+ const titleLines = String(box.title || "").split("\n").length;
459
+ const titleHeight = FONT.sizeTitle * FONT.lineHeight * titleLines;
460
+ if (box.stereotype) {
461
+ elements.push(text({
462
+ x: tx, y: ty - 4,
463
+ width: box.width - SIZING.boxPaddingX * 2,
464
+ height: FONT.sizeDescription * FONT.lineHeight,
465
+ value: `«${box.stereotype}»`,
466
+ fontSize: FONT.sizeDescription,
467
+ color: color.stroke, align: "center",
468
+ }));
469
+ }
470
+ elements.push(text({
471
+ x: tx, y: ty + (box.stereotype ? FONT.sizeDescription * FONT.lineHeight : 0),
472
+ width: box.width - SIZING.boxPaddingX * 2,
473
+ height: titleHeight,
474
+ value: box.title, fontSize: FONT.sizeTitle,
475
+ color: titleColor || color.stroke, align: "center",
476
+ }));
477
+ if (box.description) {
478
+ elements.push(text({
479
+ x: tx,
480
+ y: ty + titleHeight + (box.stereotype ? FONT.sizeDescription * FONT.lineHeight : 0) + SIZING.boxTitleGap,
481
+ width: box.width - SIZING.boxPaddingX * 2,
482
+ height: box.height - SIZING.boxPaddingY * 2 - titleHeight,
483
+ value: box.description, fontSize: FONT.sizeDescription, color: "#444",
484
+ }));
485
+ }
486
+ }
487
+
488
+ // --- Actor (stickman) ---------------------------------------------------
489
+ function renderActor(box, color, elements) {
490
+ // Stickman occupies upper portion; label below.
491
+ const cx = box.x + box.width / 2;
492
+ const top = box.y + 4;
493
+ const headD = Math.min(28, box.height * 0.32);
494
+ const bodyTop = top + headD;
495
+ const bodyBottom = top + box.height * 0.7;
496
+ elements.push(ellipse({
497
+ x: cx - headD / 2, y: top, width: headD, height: headD,
498
+ strokeColor: color.stroke, backgroundColor: "#ffffff",
499
+ }));
500
+ // Body
501
+ elements.push(line({
502
+ points: [{ x: cx, y: bodyTop }, { x: cx, y: bodyBottom }],
503
+ strokeColor: color.stroke,
504
+ }));
505
+ // Arms
506
+ const armY = bodyTop + (bodyBottom - bodyTop) * 0.25;
507
+ const armR = Math.min(22, box.width * 0.35);
508
+ elements.push(line({
509
+ points: [{ x: cx - armR, y: armY }, { x: cx + armR, y: armY }],
510
+ strokeColor: color.stroke,
511
+ }));
512
+ // Legs
513
+ const legR = Math.min(18, box.width * 0.3);
514
+ elements.push(line({
515
+ points: [{ x: cx, y: bodyBottom }, { x: cx - legR, y: bodyBottom + 18 }],
516
+ strokeColor: color.stroke,
517
+ }));
518
+ elements.push(line({
519
+ points: [{ x: cx, y: bodyBottom }, { x: cx + legR, y: bodyBottom + 18 }],
520
+ strokeColor: color.stroke,
521
+ }));
522
+ // Label below the stickman
523
+ const labelY = bodyBottom + 22;
524
+ const titleLines = String(box.title || "").split("\n").length;
525
+ elements.push(text({
526
+ x: box.x, y: labelY,
527
+ width: box.width, height: FONT.sizeTitle * FONT.lineHeight * titleLines,
528
+ value: box.title, fontSize: FONT.sizeTitle, color: color.stroke,
529
+ align: "center",
530
+ }));
531
+ }
532
+
533
+ // --- Use case (ellipse) -------------------------------------------------
534
+ function renderUsecase(box, color, elements) {
535
+ elements.push(ellipse({
536
+ x: box.x, y: box.y, width: box.width, height: box.height,
537
+ strokeColor: color.stroke, backgroundColor: color.fill,
538
+ }));
539
+ const titleLines = String(box.title || "").split("\n").length;
540
+ const th = FONT.sizeTitle * FONT.lineHeight * titleLines;
541
+ elements.push(text({
542
+ x: box.x + SIZING.boxPaddingX,
543
+ y: box.y + (box.height - th) / 2,
544
+ width: box.width - SIZING.boxPaddingX * 2,
545
+ height: th,
546
+ value: box.title, fontSize: FONT.sizeTitle,
547
+ color: color.stroke, align: "center",
548
+ }));
549
+ }
550
+
551
+ // --- Database (cylinder approximation) ----------------------------------
552
+ function renderDatabase(box, color, elements) {
553
+ const lipH = Math.min(18, box.height * 0.18);
554
+ // Body rect
555
+ elements.push(rect({
556
+ x: box.x, y: box.y + lipH / 2,
557
+ width: box.width, height: box.height - lipH,
558
+ strokeColor: color.stroke, backgroundColor: color.fill,
559
+ }));
560
+ // Top ellipse (lid)
561
+ elements.push(ellipse({
562
+ x: box.x, y: box.y, width: box.width, height: lipH,
563
+ strokeColor: color.stroke, backgroundColor: color.fill,
564
+ }));
565
+ // Bottom ellipse (base) — only the front half is actually visible but
566
+ // Excalidraw draws full ellipses; this still reads as a cylinder.
567
+ elements.push(ellipse({
568
+ x: box.x, y: box.y + box.height - lipH,
569
+ width: box.width, height: lipH,
570
+ strokeColor: color.stroke, backgroundColor: "transparent",
571
+ fillStyle: "hachure",
572
+ }));
573
+ renderBoxText({ ...box, y: box.y + lipH }, color, elements);
574
+ }
575
+
576
+ // --- Cloud ---------------------------------------------------------------
577
+ function renderCloud(box, color, elements) {
578
+ // Approximation: large ellipse with two smaller ones for puffy look.
579
+ elements.push(ellipse({
580
+ x: box.x, y: box.y + box.height * 0.12,
581
+ width: box.width, height: box.height * 0.85,
582
+ strokeColor: color.stroke, backgroundColor: color.fill,
583
+ }));
584
+ const puff = Math.min(box.width, box.height) * 0.35;
585
+ elements.push(ellipse({
586
+ x: box.x + box.width * 0.15, y: box.y,
587
+ width: puff, height: puff,
588
+ strokeColor: color.stroke, backgroundColor: color.fill,
589
+ }));
590
+ elements.push(ellipse({
591
+ x: box.x + box.width * 0.55, y: box.y + 4,
592
+ width: puff, height: puff,
593
+ strokeColor: color.stroke, backgroundColor: color.fill,
594
+ }));
595
+ renderBoxText(box, color, elements);
596
+ }
597
+
598
+ // --- Interface (small filled circle, lollipop-style) --------------------
599
+ function renderInterface(box, color, elements) {
600
+ const d = Math.min(box.width, box.height) * 0.55;
601
+ const cx = box.x + box.width / 2;
602
+ const cy = box.y + d / 2 + 4;
603
+ elements.push(ellipse({
604
+ x: cx - d / 2, y: cy - d / 2, width: d, height: d,
605
+ strokeColor: color.stroke, backgroundColor: "#ffffff",
606
+ }));
607
+ const labelY = cy + d / 2 + 6;
608
+ const titleLines = String(box.title || "").split("\n").length;
609
+ elements.push(text({
610
+ x: box.x, y: labelY,
611
+ width: box.width, height: FONT.sizeTitle * FONT.lineHeight * titleLines,
612
+ value: box.title, fontSize: FONT.sizeTitle, color: color.stroke,
613
+ align: "center",
614
+ }));
615
+ }
616
+
617
+ // --- Entity (rectangle + horizontal line below title) -------------------
618
+ function renderEntity(box, color, elements) {
619
+ elements.push(rect({
620
+ x: box.x, y: box.y, width: box.width, height: box.height,
621
+ strokeColor: color.stroke, backgroundColor: color.fill,
622
+ }));
623
+ renderBoxText(box, color, elements);
624
+ }
625
+
626
+ // --- Node (3-D box) ------------------------------------------------------
627
+ function renderNodeShape(box, color, elements) {
628
+ const off = 8;
629
+ // Back rectangle (offset)
630
+ elements.push(rect({
631
+ x: box.x + off, y: box.y - off,
632
+ width: box.width - off, height: box.height - off,
633
+ strokeColor: color.stroke, backgroundColor: color.fill,
634
+ }));
635
+ // Front rectangle
636
+ elements.push(rect({
637
+ x: box.x, y: box.y, width: box.width - off, height: box.height - off,
638
+ strokeColor: color.stroke, backgroundColor: color.fill,
639
+ }));
640
+ renderBoxText({ ...box, width: box.width - off, height: box.height - off }, color, elements);
641
+ }
642
+
643
+ // --- Class (rectangle with member compartment) --------------------------
644
+ function renderClass(box, color, elements) {
645
+ elements.push(rect({
646
+ x: box.x, y: box.y, width: box.width, height: box.height,
647
+ strokeColor: color.stroke, backgroundColor: color.fill,
648
+ }));
649
+ const titleLines = String(box.title || "").split("\n").length;
650
+ const titleH = FONT.sizeTitle * FONT.lineHeight * titleLines;
651
+ const ty = box.y + SIZING.boxPaddingY;
652
+ elements.push(text({
653
+ x: box.x, y: ty,
654
+ width: box.width, height: titleH,
655
+ value: box.title, fontSize: FONT.sizeTitle,
656
+ color: color.stroke, align: "center",
657
+ }));
658
+ if (box.members && box.members.length) {
659
+ const sepY = ty + titleH + 4;
660
+ elements.push(line({
661
+ points: [{ x: box.x, y: sepY }, { x: box.x + box.width, y: sepY }],
662
+ strokeColor: color.stroke, strokeWidth: 1,
663
+ }));
664
+ elements.push(text({
665
+ x: box.x + SIZING.boxPaddingX, y: sepY + 4,
666
+ width: box.width - SIZING.boxPaddingX * 2,
667
+ height: box.members.length * FONT.sizeDescription * FONT.lineHeight,
668
+ value: box.members.join("\n"),
669
+ fontSize: FONT.sizeDescription, color: "#222", align: "left",
670
+ }));
671
+ }
672
+ }
673
+
674
+ // --- Note (yellow sticky) ----------------------------------------------
675
+ function renderNote(box, elements) {
676
+ const NOTE_FILL = "#fff5b1";
677
+ const NOTE_STROKE = "#a07b00";
678
+ elements.push(rect({
679
+ x: box.x, y: box.y, width: box.width, height: box.height,
680
+ strokeColor: NOTE_STROKE, backgroundColor: NOTE_FILL,
681
+ }));
682
+ // Folded corner triangle (fake — just a small line).
683
+ const fold = 12;
684
+ elements.push(line({
685
+ points: [
686
+ { x: box.x + box.width - fold, y: box.y },
687
+ { x: box.x + box.width - fold, y: box.y + fold },
688
+ { x: box.x + box.width, y: box.y + fold },
689
+ ],
690
+ strokeColor: NOTE_STROKE, strokeWidth: 1,
691
+ }));
692
+ const text_value = box.description || box.title;
693
+ elements.push(text({
694
+ x: box.x + SIZING.boxPaddingX,
695
+ y: box.y + SIZING.boxPaddingY,
696
+ width: box.width - SIZING.boxPaddingX * 2,
697
+ height: box.height - SIZING.boxPaddingY * 2,
698
+ value: text_value, fontSize: FONT.sizeDescription, color: "#000",
699
+ }));
700
+ }
701
+
702
+ function renderEdgeLabel(conn) {
703
+ const path = conn.path;
704
+ if (!path || path.length < 2) return null;
705
+ // Place at middle-segment midpoint.
706
+ const midIdx = Math.floor((path.length - 1) / 2);
707
+ const a = path[midIdx];
708
+ const b = path[midIdx + 1];
709
+ const cx = (a.x + b.x) / 2;
710
+ const cy = (a.y + b.y) / 2;
711
+ const lines = String(conn.label).split("\n");
712
+ const w = Math.max(40, Math.max(...lines.map((l) => l.length)) * FONT.sizeDescription * FONT.glyphRatio + 12);
713
+ const h = lines.length * FONT.sizeDescription * FONT.lineHeight;
714
+ return text({
715
+ x: cx - w / 2, y: cy - h - 4,
716
+ width: w, height: h,
717
+ value: conn.label, fontSize: FONT.sizeDescription,
718
+ color: "#222", align: "center",
719
+ });
720
+ }
721
+
722
+ function renderTitleTab({ parent, color, title, fontSize, height, paddingX }, elements) {
723
+ const padX = 14;
724
+ const w = Math.min(parent.width - paddingX * 2, Math.max(120, title.length * fontSize * FONT.glyphRatio + padX * 2));
725
+ const x = parent.x + paddingX;
726
+ const y = parent.y + 8;
727
+ elements.push(rect({
728
+ x, y, width: w, height,
729
+ strokeColor: color.stroke, backgroundColor: color.titleFill,
730
+ }));
731
+ elements.push(text({
732
+ x: x + padX, y: y + (height - fontSize * FONT.lineHeight) / 2,
733
+ width: w - padX * 2, height: fontSize * FONT.lineHeight,
734
+ value: title, fontSize, color: color.stroke, align: "left", vAlign: "middle",
735
+ }));
736
+ }