@miadi/stateloom-canvas 0.1.1 → 0.1.3

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,62 @@
1
+ /**
2
+ * The ERD design surface (Spec 80) — the sibling of `<StateMachineCanvas>`.
3
+ *
4
+ * Same contract: props in, callbacks out, no store, no socket. The host owns
5
+ * the definition, the box positions and the viewport; this component draws
6
+ * them and reports what the hand did. Same gestures too — wheel pans, ⌃/⌘
7
+ * wheel zooms at the pointer, a drag on the board pans, a drag on a box moves
8
+ * it, two fingers pinch — and the same `--slc-*` variables, so a host that has
9
+ * themed the state canvas has themed this one.
10
+ *
11
+ * An entity box lists its attributes; keys show as PK / UK / FK badges. An
12
+ * attribute that stores a machine's state (`stateOf`) is drawn in the accent
13
+ * ink with a ◉, and activating it reports the machine through
14
+ * `onOpenMachine` — the join between the two diagrams.
15
+ *
16
+ * Relationship lines reuse the state canvas's edge router, so several lines on
17
+ * one box face each get a port of their own. Each end carries its cardinality
18
+ * mark: a bar for "one", a crow's foot for "many".
19
+ *
20
+ * `notation` chooses the drawing, never the data. "crowsfoot" (the default) is
21
+ * the above. "chen" draws the same definition the way Chen did: the entity is a
22
+ * rectangle (double when `weak`), each attribute an oval beside it with the
23
+ * primary key underlined, each relationship a diamond carrying its verb, and
24
+ * the cardinality written as 1 / N / M beside the line. An oval shows the name
25
+ * only; its tooltip carries everything else the document says about it.
26
+ */
27
+ import { type CSSProperties, type ReactNode } from "react";
28
+ import { type EntityRelationshipDefinition, type ErdNotation, type LayoutBox, type Viewport } from "@miadi/stateloom-protocol";
29
+ import { type CanvasTheme } from "./theme.js";
30
+ export interface ErdCanvasTarget {
31
+ kind: "entity" | "canvas";
32
+ id?: string;
33
+ }
34
+ export interface EntityRelationshipCanvasProps {
35
+ definition: EntityRelationshipDefinition;
36
+ /** How to draw it. The host lays `positions` out for the same notation (`erdAutoLayout(def, { notation })`). */
37
+ notation?: ErdNotation;
38
+ /** Entity name → box. The host owns these; `erdAutoLayout` seeds them. */
39
+ positions: Record<string, LayoutBox>;
40
+ viewport: Viewport;
41
+ onViewportChange: (viewport: Viewport) => void;
42
+ /** The selected entity's name. */
43
+ selection?: string | null;
44
+ /** Entities failing validation. */
45
+ errorElements?: readonly string[];
46
+ readOnly?: boolean;
47
+ /** Change this to re-fit the board to its boxes (a new document, a re-layout). */
48
+ fitKey?: string | number;
49
+ onSelect?: (entity: string) => void;
50
+ onClearSelection?: () => void;
51
+ onEntityMove?: (name: string, box: LayoutBox) => void;
52
+ /** An attribute that stores a machine's state was activated. */
53
+ onOpenMachine?: (machine: string) => void;
54
+ onContextMenu?: (clientX: number, clientY: number, target: ErdCanvasTarget) => void;
55
+ emptyHint?: ReactNode;
56
+ theme?: Partial<CanvasTheme>;
57
+ className?: string;
58
+ style?: CSSProperties;
59
+ svgId?: string;
60
+ }
61
+ export declare function EntityRelationshipCanvas({ definition, notation, positions, viewport, onViewportChange, selection, errorElements, readOnly, fitKey, onSelect, onClearSelection, onEntityMove, onOpenMachine, onContextMenu, emptyHint, theme, className, style, svgId, }: EntityRelationshipCanvasProps): import("react").JSX.Element;
62
+ export default EntityRelationshipCanvas;
@@ -0,0 +1,319 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * The ERD design surface (Spec 80) — the sibling of `<StateMachineCanvas>`.
4
+ *
5
+ * Same contract: props in, callbacks out, no store, no socket. The host owns
6
+ * the definition, the box positions and the viewport; this component draws
7
+ * them and reports what the hand did. Same gestures too — wheel pans, ⌃/⌘
8
+ * wheel zooms at the pointer, a drag on the board pans, a drag on a box moves
9
+ * it, two fingers pinch — and the same `--slc-*` variables, so a host that has
10
+ * themed the state canvas has themed this one.
11
+ *
12
+ * An entity box lists its attributes; keys show as PK / UK / FK badges. An
13
+ * attribute that stores a machine's state (`stateOf`) is drawn in the accent
14
+ * ink with a ◉, and activating it reports the machine through
15
+ * `onOpenMachine` — the join between the two diagrams.
16
+ *
17
+ * Relationship lines reuse the state canvas's edge router, so several lines on
18
+ * one box face each get a port of their own. Each end carries its cardinality
19
+ * mark: a bar for "one", a crow's foot for "many".
20
+ *
21
+ * `notation` chooses the drawing, never the data. "crowsfoot" (the default) is
22
+ * the above. "chen" draws the same definition the way Chen did: the entity is a
23
+ * rectangle (double when `weak`), each attribute an oval beside it with the
24
+ * primary key underlined, each relationship a diamond carrying its verb, and
25
+ * the cardinality written as 1 / N / M beside the line. An oval shows the name
26
+ * only; its tooltip carries everything else the document says about it.
27
+ */
28
+ import { useCallback, useEffect, useMemo, useRef, } from "react";
29
+ import { CHEN, ERD_BOX, chenCardinalityText, chenDiamondHalfWidth, erdChenGeometry, erdRowText, fitToBoxes, panBy, placeLabels, routeEdges, screenDeltaToWorld, stateOfList, viewportTransform, zoomAt, } from "@miadi/stateloom-protocol";
30
+ import { themeStyle } from "./theme.js";
31
+ const WHEEL_LINE_HEIGHT = 16;
32
+ const WHEEL_PAGE_HEIGHT = 400;
33
+ /** Movement under this many screen pixels is a click, not a drag. */
34
+ const DRAG_THRESHOLD = 4;
35
+ function wheelPixels(delta, mode) {
36
+ if (mode === 1)
37
+ return delta * WHEEL_LINE_HEIGHT;
38
+ if (mode === 2)
39
+ return delta * WHEEL_PAGE_HEIGHT;
40
+ return delta;
41
+ }
42
+ /** Which mark each end of a relationship carries: [from end, to end]. */
43
+ const ENDS = {
44
+ "1:1": ["one", "one"],
45
+ "1:N": ["one", "many"],
46
+ "N:1": ["many", "one"],
47
+ "N:M": ["many", "many"],
48
+ };
49
+ /**
50
+ * The cardinality mark at `tip` (the point on the box edge), for a line that
51
+ * leaves toward `toward`. A bar for "one"; a crow's foot for "many", opening
52
+ * onto the box.
53
+ */
54
+ function endMark(tip, toward, kind) {
55
+ const dx = toward.x - tip.x;
56
+ const dy = toward.y - tip.y;
57
+ const len = Math.hypot(dx, dy) || 1;
58
+ const ux = dx / len;
59
+ const uy = dy / len;
60
+ const nx = -uy;
61
+ const ny = ux;
62
+ const at = (along, across) => `${(tip.x + ux * along + nx * across).toFixed(1)} ${(tip.y + uy * along + ny * across).toFixed(1)}`;
63
+ if (kind === "one")
64
+ return `M ${at(9, 6)} L ${at(9, -6)}`;
65
+ return `M ${at(13, 0)} L ${at(0, 7)} M ${at(13, 0)} L ${at(0, -7)} M ${at(13, 0)} L ${at(0, 0)}`;
66
+ }
67
+ /** Everything the document says about an attribute, for a tooltip — what a Chen oval does not have room to show. */
68
+ function attributeTitle(attr) {
69
+ const machines = stateOfList(attr);
70
+ return [
71
+ `${erdRowText(attr)}${attr.nullable ? " ?" : ""}`,
72
+ attr.key === "pk" ? "primary key" : attr.key === "uk" ? "unique" : "",
73
+ attr.references ? `references ${attr.references}` : "",
74
+ machines.length ? `stores the state of ${machines.join(", ")}` : "",
75
+ attr.description ?? "",
76
+ ]
77
+ .filter(Boolean)
78
+ .join(" — ");
79
+ }
80
+ /**
81
+ * Chen only: the short segment from where a routed line ends (on the corridor
82
+ * above or below the rectangle) to the rectangle's own edge.
83
+ */
84
+ function stubTo(rect, end) {
85
+ if (!rect)
86
+ return "";
87
+ const x = Math.min(rect.x + rect.width, Math.max(rect.x, end.x));
88
+ const y = Math.min(rect.y + rect.height, Math.max(rect.y, end.y));
89
+ return x === end.x && y === end.y ? "" : `M ${end.x} ${end.y} L ${x} ${y}`;
90
+ }
91
+ const keyBadges = (attr) => [attr.key === "pk" ? "PK" : attr.key === "uk" ? "UK" : "", attr.references ? "FK" : ""].filter(Boolean).join(" ");
92
+ export function EntityRelationshipCanvas({ definition, notation = "crowsfoot", positions, viewport, onViewportChange, selection = null, errorElements, readOnly = false, fitKey, onSelect, onClearSelection, onEntityMove, onOpenMachine, onContextMenu, emptyHint, theme, className, style, svgId, }) {
93
+ const svgRef = useRef(null);
94
+ const viewportRef = useRef(viewport);
95
+ viewportRef.current = viewport;
96
+ const gestureRef = useRef(null);
97
+ const pointersRef = useRef(new Map());
98
+ const pinchRef = useRef(null);
99
+ const entities = useMemo(() => {
100
+ const seen = new Set();
101
+ return (definition.entities ?? []).filter((e) => {
102
+ if (!e?.name || seen.has(e.name) || !positions[e.name])
103
+ return false;
104
+ seen.add(e.name);
105
+ return true;
106
+ });
107
+ }, [definition.entities, positions]);
108
+ const chen = notation === "chen";
109
+ const geometry = useMemo(() => new Map(chen ? entities.map((e) => [e.name, erdChenGeometry(e, positions[e.name])]) : []), [chen, entities, positions]);
110
+ // Routed by index, not by name: the router keys its faces on a space-joined
111
+ // string, and an entity may be called "Order Line".
112
+ const edges = useMemo(() => {
113
+ const indexOf = new Map(entities.map((e, i) => [e.name, `e${i}`]));
114
+ const drawn = (definition.relationships ?? []).filter((r) => r && indexOf.has(r.from) && indexOf.has(r.to));
115
+ const curves = routeEdges(drawn.map((r) => ({ from: indexOf.get(r.from), to: indexOf.get(r.to) })),
116
+ // In Chen a line attaches to the corridor above and below the rectangle,
117
+ // so it starts clear of the attribute ovals standing at its sides.
118
+ (id) => {
119
+ const name = entities[Number(id.slice(1))].name;
120
+ return geometry.get(name)?.routingBox ?? positions[name];
121
+ });
122
+ return drawn.map((rel, i) => ({ rel, curve: curves[i] }));
123
+ }, [definition.relationships, entities, positions, geometry]);
124
+ // The state canvas's chip placer: each label tries spots along its own line
125
+ // until it is clear of every box and of the labels already down.
126
+ const labels = useMemo(() => {
127
+ const pending = edges
128
+ // Chen gives every relationship its diamond, named or not; a crow's foot line only carries a chip when it has a verb.
129
+ .filter(({ rel }) => chen || rel.label)
130
+ .map(({ rel, curve }) => ({ event: rel.label || " ", at: curve.at, rel }));
131
+ return placeLabels(pending, entities.map((e) => positions[e.name]));
132
+ }, [chen, edges, entities, positions]);
133
+ const toCanvasPoint = useCallback((clientX, clientY) => {
134
+ const rect = svgRef.current?.getBoundingClientRect();
135
+ return { x: clientX - (rect?.left ?? 0), y: clientY - (rect?.top ?? 0) };
136
+ }, []);
137
+ // Fit on demand. Keyed rather than automatic so a box being dragged toward
138
+ // the edge never makes the board jump under the hand.
139
+ useEffect(() => {
140
+ if (fitKey === undefined)
141
+ return;
142
+ const rect = svgRef.current?.getBoundingClientRect();
143
+ const boxes = Object.values(positions);
144
+ if (!rect || boxes.length === 0)
145
+ return;
146
+ onViewportChange(fitToBoxes(boxes, rect.width, rect.height));
147
+ // Positions are read at the moment of the fit, not watched.
148
+ // eslint-disable-next-line react-hooks/exhaustive-deps
149
+ }, [fitKey]);
150
+ // Native, because React's wheel listener is passive and cannot stop the page
151
+ // from zooming out from under the board.
152
+ useEffect(() => {
153
+ const svg = svgRef.current;
154
+ if (!svg)
155
+ return;
156
+ const onWheel = (e) => {
157
+ e.preventDefault();
158
+ const vp = viewportRef.current;
159
+ const dx = wheelPixels(e.deltaX, e.deltaMode);
160
+ const dy = wheelPixels(e.deltaY, e.deltaMode);
161
+ if (e.ctrlKey || e.metaKey) {
162
+ onViewportChange(zoomAt(vp, Math.exp(-dy * 0.0025), toCanvasPoint(e.clientX, e.clientY)));
163
+ return;
164
+ }
165
+ const moveX = e.shiftKey ? -(dx || dy) : -dx;
166
+ const moveY = e.shiftKey ? 0 : -dy;
167
+ if (moveX !== 0 || moveY !== 0)
168
+ onViewportChange(panBy(vp, moveX, moveY));
169
+ };
170
+ svg.addEventListener("wheel", onWheel, { passive: false });
171
+ return () => svg.removeEventListener("wheel", onWheel);
172
+ }, [onViewportChange, toCanvasPoint]);
173
+ const pinchState = () => {
174
+ const pts = [...pointersRef.current.values()];
175
+ if (pts.length < 2)
176
+ return null;
177
+ const [a, b] = pts;
178
+ return { distance: Math.hypot(b.x - a.x, b.y - a.y) || 1, mid: { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 } };
179
+ };
180
+ const onPointerDown = (e, name) => {
181
+ if (e.button !== 0 && e.pointerType === "mouse")
182
+ return;
183
+ e.stopPropagation();
184
+ svgRef.current?.setPointerCapture(e.pointerId);
185
+ pointersRef.current.set(e.pointerId, toCanvasPoint(e.clientX, e.clientY));
186
+ if (pointersRef.current.size >= 2) {
187
+ // A second finger turns whatever was happening into a pinch.
188
+ gestureRef.current = null;
189
+ pinchRef.current = pinchState();
190
+ return;
191
+ }
192
+ const movable = name !== undefined && !readOnly;
193
+ gestureRef.current = {
194
+ kind: movable ? "move" : "pan",
195
+ pointerId: e.pointerId,
196
+ startX: e.clientX,
197
+ startY: e.clientY,
198
+ lastX: e.clientX,
199
+ lastY: e.clientY,
200
+ name,
201
+ origin: movable ? positions[name] : undefined,
202
+ moved: false,
203
+ };
204
+ };
205
+ const onPointerMove = (e) => {
206
+ if (pointersRef.current.has(e.pointerId)) {
207
+ pointersRef.current.set(e.pointerId, toCanvasPoint(e.clientX, e.clientY));
208
+ }
209
+ if (pinchRef.current) {
210
+ const next = pinchState();
211
+ if (!next)
212
+ return;
213
+ const prev = pinchRef.current;
214
+ const panned = panBy(viewportRef.current, next.mid.x - prev.mid.x, next.mid.y - prev.mid.y);
215
+ onViewportChange(zoomAt(panned, next.distance / prev.distance, next.mid));
216
+ pinchRef.current = next;
217
+ return;
218
+ }
219
+ const g = gestureRef.current;
220
+ if (!g || g.pointerId !== e.pointerId)
221
+ return;
222
+ if (!g.moved && Math.hypot(e.clientX - g.startX, e.clientY - g.startY) < DRAG_THRESHOLD)
223
+ return;
224
+ g.moved = true;
225
+ if (g.kind === "pan") {
226
+ onViewportChange(panBy(viewportRef.current, e.clientX - g.lastX, e.clientY - g.lastY));
227
+ }
228
+ else if (g.name && g.origin) {
229
+ const d = screenDeltaToWorld(viewportRef.current, e.clientX - g.startX, e.clientY - g.startY);
230
+ onEntityMove?.(g.name, { ...g.origin, x: g.origin.x + d.x, y: g.origin.y + d.y });
231
+ }
232
+ g.lastX = e.clientX;
233
+ g.lastY = e.clientY;
234
+ };
235
+ const onPointerUp = (e) => {
236
+ pointersRef.current.delete(e.pointerId);
237
+ if (pinchRef.current) {
238
+ if (pointersRef.current.size < 2)
239
+ pinchRef.current = null;
240
+ return;
241
+ }
242
+ const g = gestureRef.current;
243
+ if (!g || g.pointerId !== e.pointerId)
244
+ return;
245
+ gestureRef.current = null;
246
+ if (g.moved)
247
+ return;
248
+ if (g.name)
249
+ onSelect?.(g.name);
250
+ else
251
+ onClearSelection?.();
252
+ };
253
+ const errors = new Set(errorElements ?? []);
254
+ return (_jsxs("div", { className: `slc-pane${className ? ` ${className}` : ""}`, style: { ...themeStyle(theme), ...style }, children: [_jsx("svg", { ref: svgRef, id: svgId, className: "slc-surface", onPointerDown: (e) => onPointerDown(e), onPointerMove: onPointerMove, onPointerUp: onPointerUp, onPointerCancel: onPointerUp, onContextMenu: (e) => {
255
+ if (!onContextMenu)
256
+ return;
257
+ e.preventDefault();
258
+ onContextMenu(e.clientX, e.clientY, { kind: "canvas" });
259
+ }, children: _jsxs("g", { transform: viewportTransform(viewport), children: [edges.map(({ rel, curve }, i) => {
260
+ if (chen) {
261
+ const [fromText, toText] = chenCardinalityText(rel.cardinality);
262
+ const a = curve.at(0.12);
263
+ const b = curve.at(0.88);
264
+ return (_jsxs("g", { className: "slc-erd-rel", children: [_jsx("path", { d: stubTo(geometry.get(rel.from)?.rect, curve.p0), className: "slc-erd-line" }), _jsx("path", { d: stubTo(geometry.get(rel.to)?.rect, curve.p3), className: "slc-erd-line" }), _jsx("path", { d: curve.path, className: "slc-erd-line" }), _jsx("text", { x: a.x + 9, y: a.y + 4, className: "slc-erd-card", children: fromText }), _jsx("text", { x: b.x + 9, y: b.y + 4, className: "slc-erd-card", children: toText })] }, `r${i}`));
265
+ }
266
+ const [fromEnd, toEnd] = ENDS[rel.cardinality] ?? ENDS["1:N"];
267
+ return (_jsxs("g", { className: "slc-erd-rel", children: [_jsx("path", { d: curve.path, className: "slc-erd-line" }), _jsx("path", { d: endMark(curve.p0, curve.p1, fromEnd), className: "slc-erd-mark" }), _jsx("path", { d: endMark(curve.p3, curve.p2, toEnd), className: "slc-erd-mark" })] }, `r${i}`));
268
+ }), entities.map((entity) => {
269
+ const box = positions[entity.name];
270
+ const rows = (entity.attributes ?? []).filter((a) => a?.name);
271
+ const isSelected = selection === entity.name;
272
+ const boxClass = [
273
+ "slc-node",
274
+ isSelected ? "slc-node--selected" : "",
275
+ errors.has(entity.name) ? "slc-node--error" : "",
276
+ ]
277
+ .filter(Boolean)
278
+ .join(" ");
279
+ const shape = geometry.get(entity.name);
280
+ if (shape) {
281
+ const { rect, ovals } = shape;
282
+ return (_jsxs("g", { className: readOnly ? "slc-node--static" : "slc-node--draggable", onPointerDown: (e) => onPointerDown(e, entity.name), onContextMenu: (e) => {
283
+ if (!onContextMenu)
284
+ return;
285
+ e.preventDefault();
286
+ e.stopPropagation();
287
+ onContextMenu(e.clientX, e.clientY, { kind: "entity", id: entity.name });
288
+ }, children: [ovals.map((o) => (_jsx("path", { d: `M ${o.from.x} ${o.from.y} L ${o.to.x} ${o.to.y}`, className: "slc-erd-attr-line" }, `l-${o.attribute.name}`))), ovals.map((o) => {
289
+ const machines = stateOfList(o.attribute);
290
+ const opens = machines.length > 0 && !!onOpenMachine;
291
+ return (_jsxs("g", { className: opens ? "slc-erd-state" : undefined, onPointerDown: opens ? (e) => e.stopPropagation() : undefined, onClick: opens ? () => onOpenMachine(machines[0]) : undefined, children: [_jsx("title", { children: attributeTitle(o.attribute) }), _jsx("ellipse", { cx: o.cx, cy: o.cy, rx: o.rx, ry: o.ry, className: "slc-erd-oval" }), _jsxs("text", { x: o.cx, y: o.cy + 4, textAnchor: "middle", className: `slc-erd-oval-text${o.attribute.key === "pk" ? " slc-erd-oval-text--pk" : ""}${machines.length ? " slc-erd-row--state" : ""}`, children: [machines.length ? "◉ " : "", o.attribute.name] })] }, o.attribute.name));
292
+ }), entity.description && _jsx("title", { children: entity.description }), _jsx("rect", { x: rect.x, y: rect.y, width: rect.width, height: rect.height, rx: 2, className: boxClass }), entity.weak && (_jsx("rect", { x: rect.x + 4, y: rect.y + 4, width: rect.width - 8, height: rect.height - 8, rx: 1, className: "slc-erd-weak" })), _jsx("text", { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 + 5, textAnchor: "middle", className: "slc-node-name", children: entity.name })] }, entity.name));
293
+ }
294
+ return (_jsxs("g", { className: readOnly ? "slc-node--static" : "slc-node--draggable", onPointerDown: (e) => onPointerDown(e, entity.name), onContextMenu: (e) => {
295
+ if (!onContextMenu)
296
+ return;
297
+ e.preventDefault();
298
+ e.stopPropagation();
299
+ onContextMenu(e.clientX, e.clientY, { kind: "entity", id: entity.name });
300
+ }, children: [entity.description && _jsx("title", { children: entity.description }), _jsx("rect", { x: box.x, y: box.y, width: box.width, height: box.height, rx: 6, className: boxClass }), _jsx("path", { d: `M ${box.x} ${box.y + ERD_BOX.headerHeight} h ${box.width}`, className: "slc-erd-rule" }), _jsx("text", { x: box.x + box.width / 2, y: box.y + ERD_BOX.headerHeight / 2 + 5, textAnchor: "middle", className: "slc-node-name", children: entity.name }), rows.length === 0 && (_jsx("text", { x: box.x + ERD_BOX.padX, y: box.y + ERD_BOX.headerHeight + 15, className: "slc-erd-row slc-erd-row--empty", children: "no attributes" })), rows.map((attr, r) => {
301
+ const y = box.y + ERD_BOX.headerHeight + r * ERD_BOX.rowHeight + 15;
302
+ const machines = stateOfList(attr);
303
+ const badges = keyBadges(attr);
304
+ return (_jsxs("g", { className: machines.length && onOpenMachine ? "slc-erd-state" : undefined, onPointerDown: machines.length && onOpenMachine ? (e) => e.stopPropagation() : undefined, onClick: machines.length && onOpenMachine ? () => onOpenMachine(machines[0]) : undefined, children: [(machines.length > 0 || attr.description) && (_jsx("title", { children: [machines.length ? `stores the state of ${machines.join(", ")}` : "", attr.description ?? ""]
305
+ .filter(Boolean)
306
+ .join(" — ") })), _jsxs("text", { x: box.x + ERD_BOX.padX, y: y, className: `slc-erd-row${machines.length ? " slc-erd-row--state" : ""}${attr.key === "pk" ? " slc-erd-row--pk" : ""}`, children: [machines.length ? "◉ " : "", erdRowText(attr), attr.nullable ? " ?" : ""] }), badges && (_jsx("text", { x: box.x + box.width - ERD_BOX.padX, y: y, textAnchor: "end", className: "slc-erd-badge", children: badges }))] }, attr.name));
307
+ })] }, entity.name));
308
+ }), labels.map((spot, i) => {
309
+ if (!chen)
310
+ return null;
311
+ const cx = spot.x + spot.width / 2;
312
+ const cy = spot.y + spot.height / 2;
313
+ const hw = chenDiamondHalfWidth(spot.label.rel.label ?? "");
314
+ const hh = CHEN.diamondHalfHeight;
315
+ return (_jsxs("g", { pointerEvents: "none", children: [_jsx("path", { d: `M ${cx - hw} ${cy} L ${cx} ${cy - hh} L ${cx + hw} ${cy} L ${cx} ${cy + hh} Z`, className: "slc-erd-diamond" }), _jsx("text", { x: cx, y: cy + 4, textAnchor: "middle", className: "slc-erd-rel-label", children: spot.label.rel.label ?? "" })] }, `d${i}`));
316
+ }), !chen && labels.map((spot, i) => (_jsxs("g", { pointerEvents: "none", children: [_jsx("rect", { x: spot.x, y: spot.y, width: spot.width, height: spot.height, rx: 4, className: "slc-chip-plate" }), _jsx("text", { x: spot.cx, y: spot.cy - 5, textAnchor: "middle", className: "slc-erd-rel-label", children: spot.label.event })] }, `l${i}`)))] }) }), entities.length === 0 && emptyHint && _jsx("div", { className: "slc-erd-empty", children: emptyHint })] }));
317
+ }
318
+ export default EntityRelationshipCanvas;
319
+ //# sourceMappingURL=EntityRelationshipCanvas.js.map
package/dist/index.d.ts CHANGED
@@ -14,8 +14,10 @@
14
14
  */
15
15
  export { StateMachineCanvas, default } from "./StateMachineCanvas.js";
16
16
  export type { CanvasSelection, CanvasTarget, DrawMode, StateMachineCanvasHandle, StateMachineCanvasProps, } from "./StateMachineCanvas.js";
17
+ export { EntityRelationshipCanvas } from "./EntityRelationshipCanvas.js";
18
+ export type { EntityRelationshipCanvasProps, ErdCanvasTarget } from "./EntityRelationshipCanvas.js";
17
19
  export { themeStyle, CANVAS_THEME_VARS } from "./theme.js";
18
20
  export type { CanvasTheme } from "./theme.js";
19
21
  export { childByName, childStatesAt, childrenOf, definedEvents, findState, livePath, parentAt, } from "./drill.js";
20
- export { autoLayout, AUTO_LAYOUT_DEFAULTS, IDENTITY_VIEWPORT, VIEWPORT_LIMITS, clampScale, fitToBoxes, normalizeViewport, panBy, sameViewport, screenDeltaToWorld, screenToWorld, viewportTransform, worldToScreen, zoomAt, zoomTo, routeEdges, placeLabels, eventGlyph, guardText, } from "@miadi/stateloom-protocol";
21
- export type { AutoLayoutOptions, Box, FitOptions, LayoutBox, Point, ScaleLimits, StateDef, StateMachineDefinition, TransitionDef, Viewport, } from "@miadi/stateloom-protocol";
22
+ export { autoLayout, AUTO_LAYOUT_DEFAULTS, IDENTITY_VIEWPORT, VIEWPORT_LIMITS, clampScale, fitToBoxes, normalizeViewport, panBy, sameViewport, screenDeltaToWorld, screenToWorld, viewportTransform, worldToScreen, zoomAt, zoomTo, routeEdges, erdAutoLayout, erdEntitySize, placeLabels, eventGlyph, guardText, } from "@miadi/stateloom-protocol";
23
+ export type { AutoLayoutOptions, Box, EntityRelationshipDefinition, FitOptions, LayoutBox, Point, ScaleLimits, StateDef, StateMachineDefinition, TransitionDef, Viewport, } from "@miadi/stateloom-protocol";
package/dist/index.js CHANGED
@@ -13,9 +13,10 @@
13
13
  * here, so a host needs one dependency to draw a board rather than two.
14
14
  */
15
15
  export { StateMachineCanvas, default } from "./StateMachineCanvas.js";
16
+ export { EntityRelationshipCanvas } from "./EntityRelationshipCanvas.js";
16
17
  export { themeStyle, CANVAS_THEME_VARS } from "./theme.js";
17
18
  export { childByName, childStatesAt, childrenOf, definedEvents, findState, livePath, parentAt, } from "./drill.js";
18
19
  // The geometry and the model, re-exported at the address a host reaching for a
19
20
  // canvas is already looking at.
20
- export { autoLayout, AUTO_LAYOUT_DEFAULTS, IDENTITY_VIEWPORT, VIEWPORT_LIMITS, clampScale, fitToBoxes, normalizeViewport, panBy, sameViewport, screenDeltaToWorld, screenToWorld, viewportTransform, worldToScreen, zoomAt, zoomTo, routeEdges, placeLabels, eventGlyph, guardText, } from "@miadi/stateloom-protocol";
21
+ export { autoLayout, AUTO_LAYOUT_DEFAULTS, IDENTITY_VIEWPORT, VIEWPORT_LIMITS, clampScale, fitToBoxes, normalizeViewport, panBy, sameViewport, screenDeltaToWorld, screenToWorld, viewportTransform, worldToScreen, zoomAt, zoomTo, routeEdges, erdAutoLayout, erdEntitySize, placeLabels, eventGlyph, guardText, } from "@miadi/stateloom-protocol";
21
22
  //# sourceMappingURL=index.js.map
package/dist/styles.css CHANGED
@@ -402,3 +402,117 @@
402
402
  filter: drop-shadow(0 0 4px rgb(34 197 94 / 0.85));
403
403
  }
404
404
  }
405
+
406
+ /* ─── Entity-relationship canvas (Spec 80) ────────────────────────────────── */
407
+ /* Drawn with the same variables as the state board, so one theme skins both. */
408
+
409
+ .slc-erd-line {
410
+ fill: none;
411
+ stroke: var(--slc-edge);
412
+ stroke-width: 1.5;
413
+ }
414
+
415
+ .slc-erd-mark {
416
+ fill: none;
417
+ stroke: var(--slc-edge);
418
+ stroke-width: 1.5;
419
+ stroke-linecap: round;
420
+ }
421
+
422
+ .slc-erd-rel-label {
423
+ fill: var(--slc-muted);
424
+ font-size: 11px;
425
+ font-weight: 500;
426
+ pointer-events: none;
427
+ user-select: none;
428
+ }
429
+
430
+ .slc-erd-rule {
431
+ stroke: var(--slc-node-stroke);
432
+ stroke-width: 1;
433
+ pointer-events: none;
434
+ }
435
+
436
+ .slc-erd-row {
437
+ fill: var(--slc-muted);
438
+ font-size: 11px;
439
+ font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
440
+ user-select: none;
441
+ }
442
+
443
+ .slc-erd-row--pk { fill: var(--slc-label); font-weight: 600; }
444
+ .slc-erd-row--state { fill: var(--slc-accent-ink); }
445
+ .slc-erd-row--empty { fill: var(--slc-faint); font-style: italic; }
446
+
447
+ .slc-erd-state { cursor: pointer; }
448
+ .slc-erd-state:hover .slc-erd-row--state { text-decoration: underline; }
449
+
450
+ .slc-erd-badge {
451
+ fill: var(--slc-faint);
452
+ font-size: 9px;
453
+ font-weight: 600;
454
+ letter-spacing: 0.04em;
455
+ pointer-events: none;
456
+ user-select: none;
457
+ }
458
+
459
+ /* Chen notation: ovals, diamonds, and the cardinality written beside the line. */
460
+
461
+ .slc-erd-oval {
462
+ fill: var(--slc-node-fill);
463
+ stroke: var(--slc-node-stroke);
464
+ stroke-width: 1.2;
465
+ }
466
+
467
+ .slc-erd-oval-text {
468
+ fill: var(--slc-muted);
469
+ font-size: 11px;
470
+ pointer-events: none;
471
+ user-select: none;
472
+ }
473
+
474
+ .slc-erd-oval-text--pk {
475
+ fill: var(--slc-label);
476
+ font-weight: 600;
477
+ text-decoration: underline;
478
+ }
479
+
480
+ .slc-erd-attr-line {
481
+ fill: none;
482
+ stroke: var(--slc-chip-stroke);
483
+ stroke-width: 1;
484
+ }
485
+
486
+ .slc-erd-diamond {
487
+ fill: var(--slc-chip-fill);
488
+ stroke: var(--slc-edge);
489
+ stroke-width: 1.5;
490
+ }
491
+
492
+ .slc-erd-weak {
493
+ fill: none;
494
+ stroke: var(--slc-node-stroke);
495
+ stroke-width: 1.2;
496
+ pointer-events: none;
497
+ }
498
+
499
+ .slc-erd-card {
500
+ fill: var(--slc-label);
501
+ font-size: 12px;
502
+ font-weight: 700;
503
+ pointer-events: none;
504
+ user-select: none;
505
+ }
506
+
507
+ .slc-erd-state:hover .slc-erd-oval { stroke: var(--slc-accent); }
508
+
509
+ .slc-erd-empty {
510
+ position: absolute;
511
+ inset: 0;
512
+ display: flex;
513
+ align-items: center;
514
+ justify-content: center;
515
+ color: var(--slc-faint);
516
+ font-size: 16px;
517
+ pointer-events: none;
518
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miadi/stateloom-canvas",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "The reusable SMDF design surface: a pannable, zoomable, drillable React SVG canvas with routed edges, settled labels and a navigation HUD",
5
5
  "repository": {
6
6
  "type": "git",
@@ -43,7 +43,7 @@
43
43
  "node": ">=20"
44
44
  },
45
45
  "dependencies": {
46
- "@miadi/stateloom-protocol": "^0.1.5"
46
+ "@miadi/stateloom-protocol": "^0.1.7"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "react": "^19"