@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.
- package/LICENSE +21 -0
- package/README.md +302 -0
- package/index.mjs +89 -0
- package/package.json +53 -0
- package/src/layout/elk_layout.mjs +517 -0
- package/src/layout/sequence_layout.mjs +90 -0
- package/src/layout/sizing.mjs +160 -0
- package/src/model/diagram.mjs +207 -0
- package/src/parser/component_context.mjs +129 -0
- package/src/parser/engine.mjs +87 -0
- package/src/parser/plantuml.mjs +126 -0
- package/src/parser/plugins/component/connections.mjs +26 -0
- package/src/parser/plugins/component/containers.mjs +36 -0
- package/src/parser/plugins/component/notes.mjs +65 -0
- package/src/parser/plugins/component/preamble.mjs +25 -0
- package/src/parser/plugins/component/shapes.mjs +111 -0
- package/src/parser/plugins/sequence/messages.mjs +37 -0
- package/src/parser/plugins/sequence/notes.mjs +84 -0
- package/src/parser/plugins/sequence/participants.mjs +18 -0
- package/src/parser/plugins/sequence/preamble.mjs +11 -0
- package/src/parser/sequence_context.mjs +39 -0
- package/src/parser/utils.mjs +150 -0
- package/src/render/excalidraw.mjs +736 -0
- package/src/render/sequence_render.mjs +201 -0
- package/src/render/svg.mjs +167 -0
- package/src/style/colors.mjs +75 -0
- package/src/style/text.mjs +42 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// Sequence-diagram → Excalidraw renderer.
|
|
2
|
+
//
|
|
3
|
+
// Receives the same primitive helpers (rect/text/arrow/ellipse/line)
|
|
4
|
+
// as the component renderer so id/seed/version generation stays in
|
|
5
|
+
// one place.
|
|
6
|
+
|
|
7
|
+
import { FONT } from "../style/text.mjs";
|
|
8
|
+
|
|
9
|
+
const HEAD_STROKE = "#1f2933";
|
|
10
|
+
const HEAD_FILL = "#f5f7fa";
|
|
11
|
+
const LIFELINE_STROKE = "#888";
|
|
12
|
+
const NOTE_STROKE = "#a07b00";
|
|
13
|
+
const NOTE_FILL = "#fff5b1";
|
|
14
|
+
const ACTOR_STROKE = "#1f2933";
|
|
15
|
+
|
|
16
|
+
export function exportSequenceDiagram(diagram, { sourceLabel, primitives }) {
|
|
17
|
+
const { rect, text, arrow, ellipse, line } = primitives;
|
|
18
|
+
const elements = [];
|
|
19
|
+
|
|
20
|
+
// Title
|
|
21
|
+
if (diagram.title) {
|
|
22
|
+
elements.push(text({
|
|
23
|
+
x: 20, y: 16, width: diagram.width - 40, height: 30,
|
|
24
|
+
value: diagram.title, fontSize: FONT.sizePlaneTitle,
|
|
25
|
+
color: HEAD_STROKE, align: "center",
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Lifelines
|
|
30
|
+
for (const p of diagram.participants) {
|
|
31
|
+
elements.push(line({
|
|
32
|
+
points: [
|
|
33
|
+
{ x: p.x, y: p.lifelineTop },
|
|
34
|
+
{ x: p.x, y: p.lifelineBottom },
|
|
35
|
+
],
|
|
36
|
+
strokeColor: LIFELINE_STROKE,
|
|
37
|
+
dashed: true,
|
|
38
|
+
strokeWidth: 1,
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Participant heads
|
|
43
|
+
for (const p of diagram.participants) {
|
|
44
|
+
if (p.shape === "actor") renderActorHead(p, elements, { line, ellipse, text });
|
|
45
|
+
else renderParticipantHead(p, elements, { rect, text, ellipse, line });
|
|
46
|
+
// Tail box (mirrors head at bottom)
|
|
47
|
+
if (p.shape !== "actor") {
|
|
48
|
+
renderParticipantHead({
|
|
49
|
+
...p,
|
|
50
|
+
headY: p.lifelineBottom - p.headHeight,
|
|
51
|
+
}, elements, { rect, text, ellipse, line });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Notes
|
|
56
|
+
for (const n of diagram.notes) {
|
|
57
|
+
elements.push(rect({
|
|
58
|
+
x: n.x, y: n.y, width: n.width, height: n.height,
|
|
59
|
+
strokeColor: NOTE_STROKE, backgroundColor: NOTE_FILL,
|
|
60
|
+
}));
|
|
61
|
+
elements.push(text({
|
|
62
|
+
x: n.x + 8, y: n.y + 8,
|
|
63
|
+
width: n.width - 16, height: n.height - 16,
|
|
64
|
+
value: n.text, fontSize: FONT.sizeDescription,
|
|
65
|
+
color: "#000",
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Messages
|
|
70
|
+
for (const m of diagram.messages) {
|
|
71
|
+
if (m.isSelf) {
|
|
72
|
+
renderSelfMessage(m, elements, { arrow, text });
|
|
73
|
+
} else {
|
|
74
|
+
renderMessage(m, elements, { arrow, text });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
type: "excalidraw",
|
|
80
|
+
version: 2,
|
|
81
|
+
source: "https://excalidraw.com",
|
|
82
|
+
elements,
|
|
83
|
+
appState: {
|
|
84
|
+
viewBackgroundColor: "#ffffff",
|
|
85
|
+
gridSize: null,
|
|
86
|
+
name: sourceLabel,
|
|
87
|
+
},
|
|
88
|
+
files: {},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function renderParticipantHead(p, elements, { rect, text }) {
|
|
93
|
+
const x = p.x - p.headWidth / 2;
|
|
94
|
+
elements.push(rect({
|
|
95
|
+
x, y: p.headY,
|
|
96
|
+
width: p.headWidth, height: p.headHeight,
|
|
97
|
+
strokeColor: HEAD_STROKE, backgroundColor: HEAD_FILL,
|
|
98
|
+
}));
|
|
99
|
+
if (p.stereotype) {
|
|
100
|
+
elements.push(text({
|
|
101
|
+
x, y: p.headY + 4,
|
|
102
|
+
width: p.headWidth, height: FONT.sizeDescription * FONT.lineHeight,
|
|
103
|
+
value: `«${p.stereotype}»`,
|
|
104
|
+
fontSize: FONT.sizeDescription,
|
|
105
|
+
color: HEAD_STROKE, align: "center",
|
|
106
|
+
}));
|
|
107
|
+
}
|
|
108
|
+
const titleLines = String(p.title).split("\n").length;
|
|
109
|
+
const th = FONT.sizeTitle * FONT.lineHeight * titleLines;
|
|
110
|
+
elements.push(text({
|
|
111
|
+
x, y: p.headY + (p.headHeight - th) / 2 + (p.stereotype ? 6 : 0),
|
|
112
|
+
width: p.headWidth, height: th,
|
|
113
|
+
value: p.title, fontSize: FONT.sizeTitle,
|
|
114
|
+
color: HEAD_STROKE, align: "center",
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function renderActorHead(p, elements, { line, ellipse, text }) {
|
|
119
|
+
const cx = p.x;
|
|
120
|
+
const top = p.headY + 4;
|
|
121
|
+
const headD = 22;
|
|
122
|
+
const bodyTop = top + headD;
|
|
123
|
+
const bodyBottom = bodyTop + 22;
|
|
124
|
+
elements.push(ellipse({
|
|
125
|
+
x: cx - headD / 2, y: top, width: headD, height: headD,
|
|
126
|
+
strokeColor: ACTOR_STROKE, backgroundColor: "#ffffff",
|
|
127
|
+
}));
|
|
128
|
+
elements.push(line({
|
|
129
|
+
points: [{ x: cx, y: bodyTop }, { x: cx, y: bodyBottom }],
|
|
130
|
+
strokeColor: ACTOR_STROKE,
|
|
131
|
+
}));
|
|
132
|
+
elements.push(line({
|
|
133
|
+
points: [{ x: cx - 12, y: bodyTop + 6 }, { x: cx + 12, y: bodyTop + 6 }],
|
|
134
|
+
strokeColor: ACTOR_STROKE,
|
|
135
|
+
}));
|
|
136
|
+
elements.push(line({
|
|
137
|
+
points: [{ x: cx, y: bodyBottom }, { x: cx - 10, y: bodyBottom + 12 }],
|
|
138
|
+
strokeColor: ACTOR_STROKE,
|
|
139
|
+
}));
|
|
140
|
+
elements.push(line({
|
|
141
|
+
points: [{ x: cx, y: bodyBottom }, { x: cx + 10, y: bodyBottom + 12 }],
|
|
142
|
+
strokeColor: ACTOR_STROKE,
|
|
143
|
+
}));
|
|
144
|
+
// Label below
|
|
145
|
+
const labelY = bodyBottom + 18;
|
|
146
|
+
elements.push(text({
|
|
147
|
+
x: cx - p.headWidth / 2, y: labelY,
|
|
148
|
+
width: p.headWidth, height: FONT.sizeTitle * FONT.lineHeight,
|
|
149
|
+
value: p.title, fontSize: FONT.sizeTitle,
|
|
150
|
+
color: ACTOR_STROKE, align: "center",
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function renderMessage(m, elements, { arrow, text }) {
|
|
155
|
+
const a = arrow({
|
|
156
|
+
points: [
|
|
157
|
+
{ x: m.from.x, y: m.y },
|
|
158
|
+
{ x: m.to.x, y: m.y },
|
|
159
|
+
],
|
|
160
|
+
strokeColor: "#1f2933",
|
|
161
|
+
dashed: m.dashed,
|
|
162
|
+
startArrowhead: m.startArrowhead ?? null,
|
|
163
|
+
endArrowhead: m.endArrowhead ?? "arrow",
|
|
164
|
+
});
|
|
165
|
+
if (a) elements.push(a);
|
|
166
|
+
if (m.label) {
|
|
167
|
+
const x1 = Math.min(m.from.x, m.to.x);
|
|
168
|
+
const x2 = Math.max(m.from.x, m.to.x);
|
|
169
|
+
elements.push(text({
|
|
170
|
+
x: x1, y: m.y - FONT.sizeDescription * FONT.lineHeight - 2,
|
|
171
|
+
width: x2 - x1, height: FONT.sizeDescription * FONT.lineHeight,
|
|
172
|
+
value: m.label, fontSize: FONT.sizeDescription,
|
|
173
|
+
color: "#222", align: "center",
|
|
174
|
+
}));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function renderSelfMessage(m, elements, { arrow, text }) {
|
|
179
|
+
const x = m.from.x;
|
|
180
|
+
const off = 30;
|
|
181
|
+
const a = arrow({
|
|
182
|
+
points: [
|
|
183
|
+
{ x, y: m.y },
|
|
184
|
+
{ x: x + off, y: m.y },
|
|
185
|
+
{ x: x + off, y: m.y + 24 },
|
|
186
|
+
{ x, y: m.y + 24 },
|
|
187
|
+
],
|
|
188
|
+
strokeColor: "#1f2933",
|
|
189
|
+
dashed: m.dashed,
|
|
190
|
+
startArrowhead: null,
|
|
191
|
+
endArrowhead: m.endArrowhead ?? "arrow",
|
|
192
|
+
});
|
|
193
|
+
if (a) elements.push(a);
|
|
194
|
+
if (m.label) {
|
|
195
|
+
elements.push(text({
|
|
196
|
+
x: x + off + 6, y: m.y + 4,
|
|
197
|
+
width: 200, height: FONT.sizeDescription * FONT.lineHeight,
|
|
198
|
+
value: m.label, fontSize: FONT.sizeDescription, color: "#222",
|
|
199
|
+
}));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// Convert an Excalidraw JSON document to a self-contained SVG string.
|
|
2
|
+
//
|
|
3
|
+
// The Excalidraw renderer emits a flat list of elements with absolute
|
|
4
|
+
// positions; SVG needs the same. We translate the subset we actually
|
|
5
|
+
// produce: rectangle, ellipse, line, arrow, text. Roughness is dropped
|
|
6
|
+
// (we render geometric primitives — readable in README, no rough-style
|
|
7
|
+
// wobble needed).
|
|
8
|
+
//
|
|
9
|
+
// This exists to give the build pipeline a Node-only path from
|
|
10
|
+
// PlantUML → SVG without spinning up a headless browser.
|
|
11
|
+
|
|
12
|
+
const FONT_FAMILY = "Helvetica, Arial, sans-serif";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {object} doc Excalidraw JSON document.
|
|
16
|
+
* @param {object} [opts]
|
|
17
|
+
* @param {number} [opts.padding=16] Extra margin around content.
|
|
18
|
+
* @param {string} [opts.background] Document background colour.
|
|
19
|
+
* @returns {string} SVG markup.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Convert an Excalidraw JSON document to a stand-alone SVG string.
|
|
23
|
+
* Used by the build-time documentation pipeline
|
|
24
|
+
* ({@link ../../scripts/build-docs.mjs}).
|
|
25
|
+
*
|
|
26
|
+
* @param {object} doc The Excalidraw JSON document.
|
|
27
|
+
* @param {object} [opts]
|
|
28
|
+
* @param {number} [opts.padding=16] Whitespace around the bounds.
|
|
29
|
+
* @param {string} [opts.background] Optional background fill colour.
|
|
30
|
+
* @returns {string} A complete `<svg>...</svg>` document.
|
|
31
|
+
*/
|
|
32
|
+
export function excalidrawToSvg(doc, opts = {}) {
|
|
33
|
+
const padding = opts.padding ?? 16;
|
|
34
|
+
const elements = (doc.elements || []).filter((e) => !e.isDeleted);
|
|
35
|
+
const bounds = computeBounds(elements);
|
|
36
|
+
const w = Math.max(1, bounds.maxX - bounds.minX) + padding * 2;
|
|
37
|
+
const h = Math.max(1, bounds.maxY - bounds.minY) + padding * 2;
|
|
38
|
+
const tx = -bounds.minX + padding;
|
|
39
|
+
const ty = -bounds.minY + padding;
|
|
40
|
+
|
|
41
|
+
const bg = opts.background ?? doc.appState?.viewBackgroundColor ?? "#ffffff";
|
|
42
|
+
const out = [];
|
|
43
|
+
out.push(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${w} ${h}" width="${w}" height="${h}" font-family="${FONT_FAMILY}">`);
|
|
44
|
+
out.push(`<rect width="100%" height="100%" fill="${escapeAttr(bg)}"/>`);
|
|
45
|
+
out.push(`<g transform="translate(${tx} ${ty})">`);
|
|
46
|
+
out.push(arrowheadDefs());
|
|
47
|
+
|
|
48
|
+
for (const el of elements) {
|
|
49
|
+
const node = renderOne(el);
|
|
50
|
+
if (node) out.push(node);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
out.push(`</g></svg>`);
|
|
54
|
+
return out.join("\n");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function computeBounds(elements) {
|
|
58
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
59
|
+
const include = (x, y) => {
|
|
60
|
+
if (x < minX) minX = x;
|
|
61
|
+
if (y < minY) minY = y;
|
|
62
|
+
if (x > maxX) maxX = x;
|
|
63
|
+
if (y > maxY) maxY = y;
|
|
64
|
+
};
|
|
65
|
+
for (const e of elements) {
|
|
66
|
+
if (e.type === "arrow" || e.type === "line") {
|
|
67
|
+
for (const [dx, dy] of e.points || []) include(e.x + dx, e.y + dy);
|
|
68
|
+
} else {
|
|
69
|
+
include(e.x, e.y);
|
|
70
|
+
include(e.x + (e.width || 0), e.y + (e.height || 0));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (!Number.isFinite(minX)) { minX = 0; minY = 0; maxX = 1; maxY = 1; }
|
|
74
|
+
return { minX, minY, maxX, maxY };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function renderOne(el) {
|
|
78
|
+
switch (el.type) {
|
|
79
|
+
case "rectangle": return svgRect(el);
|
|
80
|
+
case "ellipse": return svgEllipse(el);
|
|
81
|
+
case "line": return svgPolyline(el, false);
|
|
82
|
+
case "arrow": return svgPolyline(el, true);
|
|
83
|
+
case "text": return svgText(el);
|
|
84
|
+
default: return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function svgRect(el) {
|
|
89
|
+
const fill = colorOrNone(el.backgroundColor);
|
|
90
|
+
const stroke = el.strokeColor || "#000";
|
|
91
|
+
const rx = el.roundness ? 6 : 0;
|
|
92
|
+
return `<rect x="${el.x}" y="${el.y}" width="${el.width}" height="${el.height}" rx="${rx}" ry="${rx}" fill="${escapeAttr(fill)}" stroke="${escapeAttr(stroke)}" stroke-width="${el.strokeWidth || 1.5}"/>`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function svgEllipse(el) {
|
|
96
|
+
const cx = el.x + el.width / 2;
|
|
97
|
+
const cy = el.y + el.height / 2;
|
|
98
|
+
const rx = el.width / 2;
|
|
99
|
+
const ry = el.height / 2;
|
|
100
|
+
const fill = colorOrNone(el.backgroundColor);
|
|
101
|
+
const stroke = el.strokeColor || "#000";
|
|
102
|
+
return `<ellipse cx="${cx}" cy="${cy}" rx="${rx}" ry="${ry}" fill="${escapeAttr(fill)}" stroke="${escapeAttr(stroke)}" stroke-width="${el.strokeWidth || 1.5}"/>`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function svgPolyline(el, withArrow) {
|
|
106
|
+
const pts = (el.points || []).map(([dx, dy]) => `${el.x + dx},${el.y + dy}`).join(" ");
|
|
107
|
+
const stroke = el.strokeColor || "#000";
|
|
108
|
+
const dash = el.strokeStyle === "dashed" ? `stroke-dasharray="6 4"` : "";
|
|
109
|
+
const startMarker = withArrow && el.startArrowhead ? markerForArrowhead(el.startArrowhead, "start") : "";
|
|
110
|
+
const endMarker = withArrow && el.endArrowhead ? markerForArrowhead(el.endArrowhead, "end") : "";
|
|
111
|
+
return `<polyline points="${pts}" fill="none" stroke="${escapeAttr(stroke)}" stroke-width="${el.strokeWidth || 1.5}" ${dash} ${startMarker} ${endMarker}/>`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function svgText(el) {
|
|
115
|
+
const fill = el.strokeColor || "#000";
|
|
116
|
+
const fs = el.fontSize || 16;
|
|
117
|
+
const lineHeight = fs * (el.lineHeight || 1.2);
|
|
118
|
+
const lines = String(el.text ?? "").split("\n");
|
|
119
|
+
const align = el.textAlign || "left";
|
|
120
|
+
const anchor = align === "center" ? "middle" : align === "right" ? "end" : "start";
|
|
121
|
+
// Excalidraw text x is the box's left edge; baseline depends on align.
|
|
122
|
+
let xRef = el.x;
|
|
123
|
+
if (align === "center") xRef = el.x + (el.width || 0) / 2;
|
|
124
|
+
else if (align === "right") xRef = el.x + (el.width || 0);
|
|
125
|
+
const yTop = el.y + fs; // first baseline
|
|
126
|
+
const tspans = lines.map((l, i) =>
|
|
127
|
+
`<tspan x="${xRef}" dy="${i === 0 ? 0 : lineHeight}">${escapeText(l)}</tspan>`,
|
|
128
|
+
).join("");
|
|
129
|
+
return `<text x="${xRef}" y="${yTop}" fill="${escapeAttr(fill)}" font-size="${fs}" text-anchor="${anchor}">${tspans}</text>`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Arrowhead helpers --------------------------------------------------------
|
|
133
|
+
|
|
134
|
+
function markerForArrowhead(kind, end) {
|
|
135
|
+
const id = `m_${kind}_${end}`;
|
|
136
|
+
return `marker-${end}="url(#${id})"`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function arrowheadDefs() {
|
|
140
|
+
// Inline definitions so each generated SVG is self-contained.
|
|
141
|
+
return `<defs>
|
|
142
|
+
<marker id="m_arrow_end" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="8" markerHeight="8" orient="auto"><path d="M0,0 L10,5 L0,10 z" fill="#000"/></marker>
|
|
143
|
+
<marker id="m_arrow_start" viewBox="0 0 10 10" refX="1" refY="5" markerWidth="8" markerHeight="8" orient="auto-start-reverse"><path d="M0,0 L10,5 L0,10 z" fill="#000"/></marker>
|
|
144
|
+
<marker id="m_triangle_end" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="9" markerHeight="9" orient="auto"><path d="M0,0 L10,5 L0,10 z" fill="#000"/></marker>
|
|
145
|
+
<marker id="m_triangle_start" viewBox="0 0 10 10" refX="1" refY="5" markerWidth="9" markerHeight="9" orient="auto-start-reverse"><path d="M0,0 L10,5 L0,10 z" fill="#000"/></marker>
|
|
146
|
+
<marker id="m_triangle_outline_end" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="10" markerHeight="10" orient="auto"><path d="M0,0 L10,5 L0,10 z" fill="#fff" stroke="#000"/></marker>
|
|
147
|
+
<marker id="m_triangle_outline_start" viewBox="0 0 10 10" refX="1" refY="5" markerWidth="10" markerHeight="10" orient="auto-start-reverse"><path d="M0,0 L10,5 L0,10 z" fill="#fff" stroke="#000"/></marker>
|
|
148
|
+
<marker id="m_diamond_end" viewBox="0 0 12 10" refX="11" refY="5" markerWidth="10" markerHeight="8" orient="auto"><path d="M0,5 L6,0 L12,5 L6,10 z" fill="#000"/></marker>
|
|
149
|
+
<marker id="m_diamond_start" viewBox="0 0 12 10" refX="1" refY="5" markerWidth="10" markerHeight="8" orient="auto-start-reverse"><path d="M0,5 L6,0 L12,5 L6,10 z" fill="#000"/></marker>
|
|
150
|
+
<marker id="m_diamond_outline_end" viewBox="0 0 12 10" refX="11" refY="5" markerWidth="10" markerHeight="8" orient="auto"><path d="M0,5 L6,0 L12,5 L6,10 z" fill="#fff" stroke="#000"/></marker>
|
|
151
|
+
<marker id="m_diamond_outline_start" viewBox="0 0 12 10" refX="1" refY="5" markerWidth="10" markerHeight="8" orient="auto-start-reverse"><path d="M0,5 L6,0 L12,5 L6,10 z" fill="#fff" stroke="#000"/></marker>
|
|
152
|
+
</defs>`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Misc helpers -------------------------------------------------------------
|
|
156
|
+
|
|
157
|
+
function colorOrNone(c) {
|
|
158
|
+
if (!c || c === "transparent") return "none";
|
|
159
|
+
return c;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function escapeAttr(s) {
|
|
163
|
+
return String(s).replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
164
|
+
}
|
|
165
|
+
function escapeText(s) {
|
|
166
|
+
return String(s).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
167
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Deterministic plane colour generation.
|
|
2
|
+
// A 32-bit FNV-1a hash maps plane ids to a fixed hue. The plane stroke,
|
|
3
|
+
// background, and title fill are derived in HSL so subplanes / boxes can
|
|
4
|
+
// gently shift the same hue without picking arbitrary palettes.
|
|
5
|
+
|
|
6
|
+
const FNV_OFFSET = 0x811c9dc5;
|
|
7
|
+
const FNV_PRIME = 0x01000193;
|
|
8
|
+
|
|
9
|
+
function hash32(text) {
|
|
10
|
+
let h = FNV_OFFSET;
|
|
11
|
+
for (let i = 0; i < text.length; i++) {
|
|
12
|
+
h ^= text.charCodeAt(i);
|
|
13
|
+
h = Math.imul(h, FNV_PRIME);
|
|
14
|
+
}
|
|
15
|
+
return h >>> 0;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function hslToHex(h, s, l) {
|
|
19
|
+
const a = s * Math.min(l, 1 - l);
|
|
20
|
+
const f = (n) => {
|
|
21
|
+
const k = (n + h / 30) % 12;
|
|
22
|
+
const c = l - a * Math.max(-1, Math.min(k - 3, 9 - k, 1));
|
|
23
|
+
return Math.round(255 * c).toString(16).padStart(2, "0");
|
|
24
|
+
};
|
|
25
|
+
return `#${f(0)}${f(8)}${f(4)}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function planeColor(id) {
|
|
29
|
+
const hue = hash32(id) % 360;
|
|
30
|
+
return {
|
|
31
|
+
stroke: hslToHex(hue, 0.62, 0.36),
|
|
32
|
+
fill: hslToHex(hue, 0.55, 0.94),
|
|
33
|
+
titleFill: hslToHex(hue, 0.50, 0.86),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function subplaneColor(planeColor) {
|
|
38
|
+
return {
|
|
39
|
+
stroke: planeColor.stroke,
|
|
40
|
+
fill: shiftLightness(planeColor.fill, -0.04),
|
|
41
|
+
titleFill: shiftLightness(planeColor.titleFill, -0.04),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function boxColor(planeColor) {
|
|
46
|
+
return {
|
|
47
|
+
stroke: planeColor.stroke,
|
|
48
|
+
fill: "#ffffff",
|
|
49
|
+
titleFill: shiftLightness(planeColor.fill, -0.02),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function shiftLightness(hex, delta) {
|
|
54
|
+
const { h, s, l } = hexToHsl(hex);
|
|
55
|
+
return hslToHex(h, s, Math.max(0, Math.min(1, l + delta)));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function hexToHsl(hex) {
|
|
59
|
+
const r = parseInt(hex.slice(1, 3), 16) / 255;
|
|
60
|
+
const g = parseInt(hex.slice(3, 5), 16) / 255;
|
|
61
|
+
const b = parseInt(hex.slice(5, 7), 16) / 255;
|
|
62
|
+
const max = Math.max(r, g, b);
|
|
63
|
+
const min = Math.min(r, g, b);
|
|
64
|
+
const l = (max + min) / 2;
|
|
65
|
+
let h = 0;
|
|
66
|
+
let s = 0;
|
|
67
|
+
if (max !== min) {
|
|
68
|
+
const d = max - min;
|
|
69
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
70
|
+
if (max === r) h = ((g - b) / d + (g < b ? 6 : 0)) * 60;
|
|
71
|
+
else if (max === g) h = ((b - r) / d + 2) * 60;
|
|
72
|
+
else h = ((r - g) / d + 4) * 60;
|
|
73
|
+
}
|
|
74
|
+
return { h, s, l };
|
|
75
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Approximate text measurement. Excalidraw uses Virgil (font family 1) at a
|
|
2
|
+
// configurable size. We never load the real font here, so we use a fixed
|
|
3
|
+
// average glyph ratio that has empirically matched Virgil well enough for
|
|
4
|
+
// layout purposes. The exporter writes the same fontSize, so visible labels
|
|
5
|
+
// stay inside the boxes computed here.
|
|
6
|
+
|
|
7
|
+
export const FONT = {
|
|
8
|
+
family: 1, // Virgil
|
|
9
|
+
sizeTitle: 18,
|
|
10
|
+
sizeDescription: 13,
|
|
11
|
+
sizePlaneTitle: 22,
|
|
12
|
+
sizeSubplaneTitle: 16,
|
|
13
|
+
glyphRatio: 0.55, // average char width / fontSize for Virgil
|
|
14
|
+
lineHeight: 1.25,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function measureLine(text, fontSize) {
|
|
18
|
+
if (!text) return { width: 0, height: 0 };
|
|
19
|
+
return {
|
|
20
|
+
width: text.length * fontSize * FONT.glyphRatio,
|
|
21
|
+
height: fontSize * FONT.lineHeight,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function measureWrapped(text, fontSize, maxWidth) {
|
|
26
|
+
if (!text) return { width: 0, height: 0, lines: [] };
|
|
27
|
+
const charsPerLine = Math.max(4, Math.floor(maxWidth / (fontSize * FONT.glyphRatio)));
|
|
28
|
+
const words = String(text).split(/\s+/);
|
|
29
|
+
const lines = [];
|
|
30
|
+
let current = "";
|
|
31
|
+
for (const word of words) {
|
|
32
|
+
if (!current) { current = word; continue; }
|
|
33
|
+
if ((current.length + 1 + word.length) <= charsPerLine) current += ` ${word}`;
|
|
34
|
+
else { lines.push(current); current = word; }
|
|
35
|
+
}
|
|
36
|
+
if (current) lines.push(current);
|
|
37
|
+
return {
|
|
38
|
+
width: Math.min(maxWidth, Math.max(0, ...lines.map((l) => l.length * fontSize * FONT.glyphRatio))),
|
|
39
|
+
height: lines.length * fontSize * FONT.lineHeight,
|
|
40
|
+
lines,
|
|
41
|
+
};
|
|
42
|
+
}
|