@miadi/stateloom-canvas 0.1.2 → 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.
@@ -16,9 +16,16 @@
16
16
  * Relationship lines reuse the state canvas's edge router, so several lines on
17
17
  * one box face each get a port of their own. Each end carries its cardinality
18
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.
19
26
  */
20
27
  import { type CSSProperties, type ReactNode } from "react";
21
- import { type EntityRelationshipDefinition, type LayoutBox, type Viewport } from "@miadi/stateloom-protocol";
28
+ import { type EntityRelationshipDefinition, type ErdNotation, type LayoutBox, type Viewport } from "@miadi/stateloom-protocol";
22
29
  import { type CanvasTheme } from "./theme.js";
23
30
  export interface ErdCanvasTarget {
24
31
  kind: "entity" | "canvas";
@@ -26,6 +33,8 @@ export interface ErdCanvasTarget {
26
33
  }
27
34
  export interface EntityRelationshipCanvasProps {
28
35
  definition: EntityRelationshipDefinition;
36
+ /** How to draw it. The host lays `positions` out for the same notation (`erdAutoLayout(def, { notation })`). */
37
+ notation?: ErdNotation;
29
38
  /** Entity name → box. The host owns these; `erdAutoLayout` seeds them. */
30
39
  positions: Record<string, LayoutBox>;
31
40
  viewport: Viewport;
@@ -49,5 +58,5 @@ export interface EntityRelationshipCanvasProps {
49
58
  style?: CSSProperties;
50
59
  svgId?: string;
51
60
  }
52
- export declare function EntityRelationshipCanvas({ definition, positions, viewport, onViewportChange, selection, errorElements, readOnly, fitKey, onSelect, onClearSelection, onEntityMove, onOpenMachine, onContextMenu, emptyHint, theme, className, style, svgId, }: EntityRelationshipCanvasProps): import("react").JSX.Element;
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;
53
62
  export default EntityRelationshipCanvas;
@@ -17,9 +17,16 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
17
17
  * Relationship lines reuse the state canvas's edge router, so several lines on
18
18
  * one box face each get a port of their own. Each end carries its cardinality
19
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.
20
27
  */
21
28
  import { useCallback, useEffect, useMemo, useRef, } from "react";
22
- import { ERD_BOX, erdRowText, fitToBoxes, panBy, placeLabels, routeEdges, screenDeltaToWorld, stateOfList, viewportTransform, zoomAt, } from "@miadi/stateloom-protocol";
29
+ import { CHEN, ERD_BOX, chenCardinalityText, chenDiamondHalfWidth, erdChenGeometry, erdRowText, fitToBoxes, panBy, placeLabels, routeEdges, screenDeltaToWorld, stateOfList, viewportTransform, zoomAt, } from "@miadi/stateloom-protocol";
23
30
  import { themeStyle } from "./theme.js";
24
31
  const WHEEL_LINE_HEIGHT = 16;
25
32
  const WHEEL_PAGE_HEIGHT = 400;
@@ -57,8 +64,32 @@ function endMark(tip, toward, kind) {
57
64
  return `M ${at(9, 6)} L ${at(9, -6)}`;
58
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)}`;
59
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
+ }
60
91
  const keyBadges = (attr) => [attr.key === "pk" ? "PK" : attr.key === "uk" ? "UK" : "", attr.references ? "FK" : ""].filter(Boolean).join(" ");
61
- export function EntityRelationshipCanvas({ definition, positions, viewport, onViewportChange, selection = null, errorElements, readOnly = false, fitKey, onSelect, onClearSelection, onEntityMove, onOpenMachine, onContextMenu, emptyHint, theme, className, style, svgId, }) {
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, }) {
62
93
  const svgRef = useRef(null);
63
94
  const viewportRef = useRef(viewport);
64
95
  viewportRef.current = viewport;
@@ -74,22 +105,31 @@ export function EntityRelationshipCanvas({ definition, positions, viewport, onVi
74
105
  return true;
75
106
  });
76
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]);
77
110
  // Routed by index, not by name: the router keys its faces on a space-joined
78
111
  // string, and an entity may be called "Order Line".
79
112
  const edges = useMemo(() => {
80
113
  const indexOf = new Map(entities.map((e, i) => [e.name, `e${i}`]));
81
114
  const drawn = (definition.relationships ?? []).filter((r) => r && indexOf.has(r.from) && indexOf.has(r.to));
82
- const curves = routeEdges(drawn.map((r) => ({ from: indexOf.get(r.from), to: indexOf.get(r.to) })), (id) => positions[entities[Number(id.slice(1))].name]);
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
+ });
83
122
  return drawn.map((rel, i) => ({ rel, curve: curves[i] }));
84
- }, [definition.relationships, entities, positions]);
123
+ }, [definition.relationships, entities, positions, geometry]);
85
124
  // The state canvas's chip placer: each label tries spots along its own line
86
125
  // until it is clear of every box and of the labels already down.
87
126
  const labels = useMemo(() => {
88
127
  const pending = edges
89
- .filter(({ rel }) => rel.label)
90
- .map(({ rel, curve }) => ({ event: rel.label, at: curve.at }));
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 }));
91
131
  return placeLabels(pending, entities.map((e) => positions[e.name]));
92
- }, [edges, entities, positions]);
132
+ }, [chen, edges, entities, positions]);
93
133
  const toCanvasPoint = useCallback((clientX, clientY) => {
94
134
  const rect = svgRef.current?.getBoundingClientRect();
95
135
  return { x: clientX - (rect?.left ?? 0), y: clientY - (rect?.top ?? 0) };
@@ -217,6 +257,12 @@ export function EntityRelationshipCanvas({ definition, positions, viewport, onVi
217
257
  e.preventDefault();
218
258
  onContextMenu(e.clientX, e.clientY, { kind: "canvas" });
219
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
+ }
220
266
  const [fromEnd, toEnd] = ENDS[rel.cardinality] ?? ENDS["1:N"];
221
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}`));
222
268
  }), entities.map((entity) => {
@@ -230,6 +276,21 @@ export function EntityRelationshipCanvas({ definition, positions, viewport, onVi
230
276
  ]
231
277
  .filter(Boolean)
232
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
+ }
233
294
  return (_jsxs("g", { className: readOnly ? "slc-node--static" : "slc-node--draggable", onPointerDown: (e) => onPointerDown(e, entity.name), onContextMenu: (e) => {
234
295
  if (!onContextMenu)
235
296
  return;
@@ -244,7 +305,15 @@ export function EntityRelationshipCanvas({ definition, positions, viewport, onVi
244
305
  .filter(Boolean)
245
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));
246
307
  })] }, entity.name));
247
- }), 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 })] }));
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 })] }));
248
317
  }
249
318
  export default EntityRelationshipCanvas;
250
319
  //# sourceMappingURL=EntityRelationshipCanvas.js.map
package/dist/styles.css CHANGED
@@ -456,6 +456,56 @@
456
456
  user-select: none;
457
457
  }
458
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
+
459
509
  .slc-erd-empty {
460
510
  position: absolute;
461
511
  inset: 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miadi/stateloom-canvas",
3
- "version": "0.1.2",
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.6"
46
+ "@miadi/stateloom-protocol": "^0.1.7"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "react": "^19"