@polymorphism-tech/morph-spec 10.9.0 → 10.10.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,79 @@
1
+ const esc = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));
2
+
3
+ const CHAR_W = 7.2;
4
+ const LABEL_CHAR_W = 6.1;
5
+ const NODE_H = 38;
6
+ const ROW = 62;
7
+ const PAD = 16;
8
+ const MIN_NODE_W = 110;
9
+ const MAX_NODE_W = 280;
10
+ const MAX_LABEL = 46;
11
+ const MIN_GAP = 70;
12
+
13
+ const clip = (text, max) => (text.length > max ? `${text.slice(0, max - 1).trimEnd()}…` : text);
14
+
15
+ /** Column of each node: how many steps it is from where the flow starts; a step back never moves a node. */
16
+ export function flowLayers(transitions) {
17
+ const depth = new Map();
18
+ const queue = [];
19
+ const reach = (node, d) => { if (!depth.has(node)) { depth.set(node, d); queue.push(node); } };
20
+ for (const start of transitions.flatMap((t) => [t.from, t.to])) {
21
+ reach(start, 0);
22
+ while (queue.length) {
23
+ const node = queue.shift();
24
+ for (const t of transitions) if (t.from === node) reach(t.to, depth.get(node) + 1);
25
+ }
26
+ }
27
+ const layers = [];
28
+ for (const [node, d] of depth) (layers[d] ??= []).push(node);
29
+ return layers;
30
+ }
31
+
32
+ // Left to right by step, nodes of one step stacked: a fan-out opens downwards instead of running off the page.
33
+ export function layeredFlowSvg(transitions, label = 'Fluxo', id = 'flow') {
34
+ if (!transitions.length) return '';
35
+ const layers = flowLayers(transitions);
36
+ const column = new Map(layers.flatMap((nodes, i) => nodes.map((n) => [n, i])));
37
+ const widths = layers.map((nodes) => Math.min(MAX_NODE_W, Math.max(MIN_NODE_W, ...nodes.map((n) => Math.ceil(n.length * CHAR_W) + 28))));
38
+ const gaps = layers.slice(1).map((_, i) => {
39
+ const labels = transitions.filter((t) => column.get(t.from) === i && column.get(t.to) === i + 1).map((t) => clip(t.trigger, MAX_LABEL).length);
40
+ return Math.max(MIN_GAP, Math.ceil(Math.max(0, ...labels) * LABEL_CHAR_W) + 44);
41
+ });
42
+ const xs = layers.map((_, i) => PAD + widths.slice(0, i).reduce((a, b) => a + b, 0) + gaps.slice(0, i).reduce((a, b) => a + b, 0));
43
+ const tallest = Math.max(...layers.map((nodes) => nodes.length));
44
+ const backward = transitions.filter((t) => column.get(t.to) <= column.get(t.from) && t.to !== t.from);
45
+ const bodyH = tallest * ROW;
46
+ const height = PAD + bodyH + backward.length * 30 + PAD;
47
+ const width = xs[xs.length - 1] + widths[widths.length - 1] + PAD;
48
+ const box = new Map(layers.flatMap((nodes, i) => nodes.map((n, k) => {
49
+ const top = PAD + (bodyH - nodes.length * ROW) / 2 + k * ROW + (ROW - NODE_H) / 2;
50
+ return [n, { x: xs[i], y: top, w: widths[i], cy: top + NODE_H / 2 }];
51
+ })));
52
+
53
+ const forward = transitions.filter((t) => column.get(t.to) > column.get(t.from)).map((t) => {
54
+ const a = box.get(t.from);
55
+ const b = box.get(t.to);
56
+ const x1 = a.x + a.w;
57
+ const x2 = b.x - 4;
58
+ const bend = (x2 - x1) * 0.45;
59
+ return `<path class="edge" d="M${x1} ${a.cy} C${x1 + bend} ${a.cy} ${x2 - bend} ${b.cy} ${x2} ${b.cy}" marker-end="url(#${id})"/>`
60
+ + `<text class="edge-label" x="${x2 - 12}" y="${b.cy - 7}" text-anchor="end"><title>${esc(t.trigger)}</title>${esc(clip(t.trigger, MAX_LABEL))}</text>`;
61
+ });
62
+ const back = backward.map((t, i) => {
63
+ const a = box.get(t.from);
64
+ const b = box.get(t.to);
65
+ const y = PAD + bodyH + (i + 1) * 30 - 8;
66
+ const x1 = a.x + a.w / 2;
67
+ const x2 = b.x + b.w / 2;
68
+ return `<path class="edge" d="M${x1} ${a.y + NODE_H} C${x1} ${y} ${x2} ${y} ${x2} ${b.y + NODE_H + 4}" marker-end="url(#${id})"/>`
69
+ + `<text class="edge-label" x="${(x1 + x2) / 2}" y="${y - 2}" text-anchor="middle"><title>${esc(t.trigger)}</title>${esc(clip(t.trigger, MAX_LABEL))}</text>`;
70
+ });
71
+ const nodes = [...box].map(([name, b]) => `<g class="state"><rect x="${b.x}" y="${b.y}" width="${b.w}" height="${NODE_H}" rx="6"/><text x="${b.x + b.w / 2}" y="${b.cy + 5}" text-anchor="middle"><title>${esc(name)}</title>${esc(clip(name, Math.floor((b.w - 20) / CHAR_W)))}</text></g>`);
72
+
73
+ return `<svg class="machine" viewBox="0 0 ${width} ${height}" style="width:${width}px;max-width:100%" role="img" aria-label="${esc(label)}: ${esc([...box.keys()].join(', '))}">
74
+ <defs><marker id="${id}" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0 0L10 5L0 10z" class="arrow-head"/></marker></defs>
75
+ ${forward.join('\n')}
76
+ ${back.join('\n')}
77
+ ${nodes.join('\n')}
78
+ </svg>`;
79
+ }