diagram-contracts 0.2.6 → 0.2.8

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/dist/index.d.ts CHANGED
@@ -445,7 +445,7 @@ declare const ELEMENT_CATALOG: {
445
445
  readonly properties: readonly ["id", "width", "height", "padding", "background", "variant"];
446
446
  readonly children: {
447
447
  readonly kind: "list";
448
- readonly items: readonly ["hstack", "vstack", "overlay", "grid", "flow", "absolute", "box", "group", "table", "connector", "alignConstraint", "equalizeWidth", "equalizeHeight", "minGap", "keepInsideConstraint", "orderConstraint"];
448
+ readonly items: readonly ["hstack", "vstack", "overlay", "grid", "flow", "absolute", "box", "group", "table", "row", "cell", "divider", "text", "label", "image", "icon", "svgAsset", "connector", "alignConstraint", "equalizeWidth", "equalizeHeight", "minGap", "keepInsideConstraint", "orderConstraint"];
449
449
  };
450
450
  };
451
451
  readonly group: {
@@ -603,7 +603,7 @@ declare const ELEMENT_CATALOG: {
603
603
  readonly properties: readonly ["id", "visible", "from", "to", "fromAnchor", "toAnchor", "fromT", "toT", "route", "cornerRadius", "curvature", "startArrow", "endArrow", "arrowSize", "stroke", "strokeWidth", "strokeStyle", "strokeDasharray", "opacity", "variant"];
604
604
  readonly children: {
605
605
  readonly kind: "list";
606
- readonly items: readonly ["label"];
606
+ readonly items: readonly ["label", "text"];
607
607
  };
608
608
  readonly notes: "Child Labels are annotation text rendered at the connector midpoint. Label.textPosition is reserved (planned, not implemented)";
609
609
  };
package/dist/index.mjs CHANGED
@@ -150,6 +150,14 @@ var ELEMENT_CATALOG = {
150
150
  "box",
151
151
  "group",
152
152
  "table",
153
+ "row",
154
+ "cell",
155
+ "divider",
156
+ "text",
157
+ "label",
158
+ "image",
159
+ "icon",
160
+ "svgAsset",
153
161
  "connector",
154
162
  "alignConstraint",
155
163
  "equalizeWidth",
@@ -435,7 +443,7 @@ var ELEMENT_CATALOG = {
435
443
  "opacity",
436
444
  "variant"
437
445
  ],
438
- children: { kind: "list", items: ["label"] },
446
+ children: { kind: "list", items: ["label", "text"] },
439
447
  notes: "Child Labels are annotation text rendered at the connector midpoint. Label.textPosition is reserved (planned, not implemented)"
440
448
  },
441
449
  // ── constraint ────────────────────────────────────────────────────────
@@ -480,6 +488,10 @@ var ELEMENT_CATALOG = {
480
488
  notes: "targets array order is source order. axis: vertical = top-to-bottom, horizontal = left-to-right"
481
489
  }
482
490
  };
491
+ function hasOwnRect(kind) {
492
+ const category = ELEMENT_CATALOG[kind].category;
493
+ return category !== "connector" && category !== "constraint";
494
+ }
483
495
 
484
496
  // src/ir/index.ts
485
497
  import { z } from "zod";
@@ -3493,7 +3505,14 @@ function deepCloneAndSubstitute(node, paramValues, compiledChildren, diagnostics
3493
3505
  function expandComponent(def, paramValues, compiledChildren, diagnostics, extraProps, id, idIsParam) {
3494
3506
  const expanded = deepCloneAndSubstitute(def.template, paramValues, compiledChildren, diagnostics, def.name);
3495
3507
  Object.assign(expanded.props, extraProps);
3496
- if (id && !idIsParam) expanded.id = id;
3508
+ if (!idIsParam) {
3509
+ if (typeof id === "string" && id !== "") {
3510
+ expanded.id = id;
3511
+ delete expanded.props["__idParam"];
3512
+ } else if (isParamRef(id) || isTemplateRef(id) || isExprRef(id)) {
3513
+ expanded.props["__idParam"] = id;
3514
+ }
3515
+ }
3497
3516
  return expanded;
3498
3517
  }
3499
3518
  function componentDefToJSON(def) {
@@ -3603,12 +3622,22 @@ function buildResolvedProps(bind, def, target, diagnostics) {
3603
3622
  continue;
3604
3623
  }
3605
3624
  const value = resolvePropValue(prop, target, bind.component, bind.line, diagnostics);
3625
+ if (prop.param === "id") {
3626
+ if (value !== void 0 && value !== target.id) {
3627
+ diagnostics.push({
3628
+ level: "error",
3629
+ message: `bind <${bind.component}>: the 'id' param cannot be mapped to ${JSON.stringify(value)} \u2014 it is always the node id ('${target.id}').`,
3630
+ path: `line:${bind.line}`
3631
+ });
3632
+ }
3633
+ continue;
3634
+ }
3606
3635
  if (value !== void 0) {
3607
3636
  resolved[prop.param] = value;
3608
3637
  mapped.add(prop.param);
3609
3638
  }
3610
3639
  }
3611
- if (def.paramNames.has("id") && !mapped.has("id")) {
3640
+ if (def.paramNames.has("id")) {
3612
3641
  resolved.id = target.id;
3613
3642
  }
3614
3643
  if (def.paramNames.has("label") && !mapped.has("label")) {
@@ -4568,8 +4597,7 @@ function extractComponentDefs(sourceFile, diagnostics, knownComponents) {
4568
4597
  const ctx = {
4569
4598
  diagnostics,
4570
4599
  registry,
4571
- paramNames: pNames,
4572
- expandingStack: /* @__PURE__ */ new Set()
4600
+ paramNames: pNames
4573
4601
  };
4574
4602
  const template = compileElement(jsx, ctx);
4575
4603
  if (template) {
@@ -4598,8 +4626,7 @@ function extractComponentDefs(sourceFile, diagnostics, knownComponents) {
4598
4626
  const ctx = {
4599
4627
  diagnostics,
4600
4628
  registry,
4601
- paramNames: pNames,
4602
- expandingStack: /* @__PURE__ */ new Set()
4629
+ paramNames: pNames
4603
4630
  };
4604
4631
  const template = compileElement(jsx, ctx);
4605
4632
  if (template) {
@@ -4613,17 +4640,8 @@ function extractComponentDefs(sourceFile, diagnostics, knownComponents) {
4613
4640
  return defs;
4614
4641
  }
4615
4642
  function expandComponentTag(tagName, props, childNodes, ctx) {
4616
- if (ctx.expandingStack.has(tagName)) {
4617
- ctx.diagnostics.push({
4618
- level: "error",
4619
- message: `Circular component reference: ${tagName}`,
4620
- path: tagName
4621
- });
4622
- return null;
4623
- }
4624
4643
  const def = ctx.registry.get(tagName);
4625
4644
  if (!def) return null;
4626
- ctx.expandingStack.add(tagName);
4627
4645
  const compiledChildren = [];
4628
4646
  if (childNodes) {
4629
4647
  for (const child of childNodes) {
@@ -4656,7 +4674,6 @@ function expandComponentTag(tagName, props, childNodes, ctx) {
4656
4674
  id,
4657
4675
  idIsParam
4658
4676
  );
4659
- ctx.expandingStack.delete(tagName);
4660
4677
  return expanded;
4661
4678
  }
4662
4679
  function resolveImports(sourceFile, resolveImport, diagnostics, fromFile, fileCache) {
@@ -4910,8 +4927,7 @@ function compileDrawTsx(source, options) {
4910
4927
  }
4911
4928
  const ctx = {
4912
4929
  diagnostics,
4913
- registry,
4914
- expandingStack: /* @__PURE__ */ new Set()
4930
+ registry
4915
4931
  };
4916
4932
  const ir = compileElement(root, ctx);
4917
4933
  const compiledIr = ir ?? { kind: "document", props: {} };
@@ -5091,32 +5107,39 @@ function kindToPascal(kind) {
5091
5107
  function normalize(ir, options) {
5092
5108
  const diagnostics = [];
5093
5109
  const result = structuredClone(ir);
5094
- const kindCounters = /* @__PURE__ */ new Map();
5095
- assignAutoIds(result, kindCounters);
5096
- checkDuplicateIds(result, diagnostics);
5110
+ const explicitIds = /* @__PURE__ */ new Map();
5111
+ collectIds(result, explicitIds, "root");
5112
+ assignAutoIds(result, /* @__PURE__ */ new Map(), explicitIds);
5113
+ const idLocations = /* @__PURE__ */ new Map();
5114
+ collectIds(result, idLocations, "root");
5115
+ checkDuplicateIds(idLocations, diagnostics);
5116
+ validateChildren(result, diagnostics, "root");
5097
5117
  applyThemeAndVariants(result, options.theme);
5098
5118
  resolveAllTokens(result, options.tokens, diagnostics, "root");
5099
5119
  validateEnumProps(result, diagnostics, "root");
5100
5120
  validateRangeProps(result, diagnostics, "root");
5101
5121
  const geometryRegistry = buildGeometryRegistry(result, diagnostics);
5102
- resolveGeometryReferences(result, geometryRegistry, diagnostics);
5122
+ const referable = /* @__PURE__ */ new Map();
5123
+ collectReferable(result, true, referable);
5124
+ resolveGeometryReferences(result, geometryRegistry, referable, diagnostics);
5103
5125
  return { ir: result, diagnostics, geometryRegistry };
5104
5126
  }
5105
- function assignAutoIds(node, counters) {
5127
+ function assignAutoIds(node, counters, explicitIds) {
5106
5128
  if (!node.id) {
5107
- const count = (counters.get(node.kind) ?? 0) + 1;
5129
+ let count = counters.get(node.kind) ?? 0;
5130
+ do {
5131
+ count += 1;
5132
+ } while (explicitIds.has(`${node.kind}_${count}`));
5108
5133
  counters.set(node.kind, count);
5109
5134
  node.id = `${node.kind}_${count}`;
5110
5135
  }
5111
5136
  if (node.children) {
5112
5137
  for (const child of node.children) {
5113
- assignAutoIds(child, counters);
5138
+ assignAutoIds(child, counters, explicitIds);
5114
5139
  }
5115
5140
  }
5116
5141
  }
5117
- function checkDuplicateIds(root, diagnostics) {
5118
- const idLocations = /* @__PURE__ */ new Map();
5119
- collectIds(root, idLocations, "root");
5142
+ function checkDuplicateIds(idLocations, diagnostics) {
5120
5143
  for (const [id, paths] of idLocations) {
5121
5144
  if (paths.length > 1) {
5122
5145
  diagnostics.push({
@@ -5142,6 +5165,24 @@ function collectIds(node, idLocations, path) {
5142
5165
  }
5143
5166
  }
5144
5167
  }
5168
+ function validateChildren(node, diagnostics, path) {
5169
+ if (!node.children) return;
5170
+ const allowed = ELEMENT_CATALOG[node.kind].children;
5171
+ for (let i = 0; i < node.children.length; i++) {
5172
+ const child = node.children[i];
5173
+ const childPath = `${path}.children[${i}]`;
5174
+ const ok = allowed.kind === "any" || allowed.kind === "list" && allowed.items.includes(child.kind);
5175
+ if (!ok) {
5176
+ const expected = allowed.kind === "list" ? `allowed children: ${allowed.items.join(", ")}` : allowed.kind === "text" ? "it takes text content only" : "it takes no children";
5177
+ diagnostics.push({
5178
+ level: "error",
5179
+ message: `${kindToPascal(child.kind)} '${child.id}' cannot be placed inside ${kindToPascal(node.kind)} '${node.id}' \u2014 ${expected}`,
5180
+ path: childPath
5181
+ });
5182
+ }
5183
+ validateChildren(child, diagnostics, childPath);
5184
+ }
5185
+ }
5145
5186
  function applyThemeAndVariants(node, theme) {
5146
5187
  const pascalKind = kindToPascal(node.kind);
5147
5188
  const explicitKeys = new Set(Object.keys(node.props));
@@ -5301,6 +5342,13 @@ function collectGeometryProps(node, registry, diagnostics) {
5301
5342
  }
5302
5343
  }
5303
5344
  }
5345
+ function collectReferable(node, parentHasRect, out) {
5346
+ const ownRect = parentHasRect && hasOwnRect(node.kind);
5347
+ if (node.id) out.set(node.id, ownRect);
5348
+ if (node.children) {
5349
+ for (const child of node.children) collectReferable(child, ownRect, out);
5350
+ }
5351
+ }
5304
5352
  function parseGeometryRef(ref) {
5305
5353
  const colonIdx = ref.indexOf(":");
5306
5354
  if (colonIdx === -1) return { nodeId: ref };
@@ -5319,20 +5367,33 @@ function resolveWithRole(nodeId, role, registry) {
5319
5367
  }
5320
5368
  return current;
5321
5369
  }
5322
- function resolveGeometryRef(ref, implicitRole, registry) {
5323
- const { nodeId, role } = parseGeometryRef(ref);
5324
- const effectiveRole = role ?? implicitRole;
5325
- if (!effectiveRole) return nodeId;
5326
- return resolveWithRole(nodeId, effectiveRole, registry);
5327
- }
5328
- function resolveGeometryReferences(node, registry, diagnostics) {
5370
+ function resolveGeometryReferences(node, registry, referable, diagnostics) {
5329
5371
  const kind = node.kind;
5372
+ const report = (prop, message) => {
5373
+ diagnostics.push({
5374
+ level: "error",
5375
+ message: `${kindToPascal(kind)} '${node.id}': ${message}`,
5376
+ path: `${node.id}.props.${prop}`
5377
+ });
5378
+ };
5379
+ const resolveRef = (ref, implicitRole, prop) => {
5380
+ const { nodeId, role } = parseGeometryRef(ref);
5381
+ const effectiveRole = role ?? implicitRole;
5382
+ const resolved = effectiveRole ? resolveWithRole(nodeId, effectiveRole, registry) : nodeId;
5383
+ const ownRect = referable.get(resolved);
5384
+ if (ownRect === void 0) {
5385
+ report(prop, `${prop} "${ref}" refers to an element that does not exist ('${resolved}')`);
5386
+ } else if (!ownRect) {
5387
+ report(prop, `${prop} "${ref}" refers to an element without a rectangle of its own ('${resolved}' \u2014 a connector, a connector's label or a constraint)`);
5388
+ }
5389
+ return resolved;
5390
+ };
5330
5391
  if (kind === "alignConstraint") {
5331
5392
  const targets = node.props.targets;
5332
5393
  if (targets) {
5333
5394
  const alignRefs = {};
5334
5395
  for (const t of targets) {
5335
- const resolved = resolveGeometryRef(t, "align", registry);
5396
+ const resolved = resolveRef(t, "align", "targets");
5336
5397
  if (resolved !== t) alignRefs[t] = resolved;
5337
5398
  }
5338
5399
  if (Object.keys(alignRefs).length > 0) {
@@ -5341,39 +5402,41 @@ function resolveGeometryReferences(node, registry, diagnostics) {
5341
5402
  }
5342
5403
  const anchor = node.props.anchor;
5343
5404
  if (anchor) {
5344
- const resolved = resolveGeometryRef(anchor, "align", registry);
5405
+ const resolved = resolveRef(anchor, "align", "anchor");
5345
5406
  if (resolved !== anchor) {
5346
5407
  node.props.alignRefs = { ...node.props.alignRefs ?? {}, [anchor]: resolved };
5347
5408
  }
5348
5409
  }
5410
+ const mover = node.props.mover;
5411
+ if (mover) {
5412
+ resolveRef(mover, void 0, "mover");
5413
+ }
5349
5414
  }
5350
5415
  if (kind === "connector") {
5351
- const from = node.props.from;
5352
- if (from) {
5353
- node.props.from = resolveGeometryRef(from, "connect", registry);
5354
- }
5355
- const to = node.props.to;
5356
- if (to) {
5357
- node.props.to = resolveGeometryRef(to, "connect", registry);
5416
+ for (const prop of ["from", "to"]) {
5417
+ const ref = node.props[prop];
5418
+ if (ref) {
5419
+ node.props[prop] = resolveRef(ref, "connect", prop);
5420
+ } else {
5421
+ report(prop, `'${prop}' is required \u2014 a connector runs between two elements`);
5422
+ }
5358
5423
  }
5359
5424
  }
5360
5425
  if (kind === "equalizeWidth" || kind === "equalizeHeight" || kind === "minGap" || kind === "keepInsideConstraint" || kind === "orderConstraint") {
5361
5426
  const targets = node.props.targets;
5362
5427
  if (targets) {
5363
- node.props.targets = targets.map(
5364
- (t) => resolveGeometryRef(t, void 0, registry)
5365
- );
5428
+ node.props.targets = targets.map((t) => resolveRef(t, void 0, "targets"));
5366
5429
  }
5367
5430
  }
5368
5431
  if (node.children) {
5369
5432
  for (const child of node.children) {
5370
- resolveGeometryReferences(child, registry, diagnostics);
5433
+ resolveGeometryReferences(child, registry, referable, diagnostics);
5371
5434
  }
5372
5435
  }
5373
5436
  }
5374
5437
 
5375
5438
  // src/artifact/index.ts
5376
- var CONNECTOR_LABEL_KINDS = /* @__PURE__ */ new Set(["label", "text"]);
5439
+ var CONNECTOR_LABEL_KINDS = new Set(ELEMENT_CATALOG.connector.children.items);
5377
5440
  function isConnectorLabelKind(kind) {
5378
5441
  return CONNECTOR_LABEL_KINDS.has(kind);
5379
5442
  }
@@ -5445,10 +5508,14 @@ function getDynamicMarkerDefs() {
5445
5508
  if (dynamicMarkerRegistry.size === 0) return "";
5446
5509
  return Array.from(dynamicMarkerRegistry.values()).join("\n");
5447
5510
  }
5511
+ function colorKey(color) {
5512
+ if (/^#[0-9a-fA-F]+$/.test(color)) return color.slice(1);
5513
+ if (/^[0-9a-zA-Z]+$/.test(color) && /[g-zG-Z]/.test(color)) return color;
5514
+ return "_" + color.replace(/[^a-zA-Z0-9]/g, (c) => `_${c.charCodeAt(0).toString(16)}_`);
5515
+ }
5448
5516
  function ensureColoredMarker(arrowType, arrowSize, color) {
5449
- const colorKey = color.replace(/[^a-zA-Z0-9]/g, "");
5450
5517
  const sizeKey = arrowSize === 1 ? "" : `-s${arrowSize}`;
5451
- const id = `marker-${arrowType}-${colorKey}${sizeKey}`;
5518
+ const id = `marker-${arrowType}-${colorKey(color)}${sizeKey}`;
5452
5519
  if (!dynamicMarkerRegistry.has(id)) {
5453
5520
  const template = MARKER_TEMPLATES.find((t) => t.type === arrowType);
5454
5521
  if (template) {
@@ -5621,9 +5688,6 @@ function renderNode(node) {
5621
5688
  }
5622
5689
  if (node.children) {
5623
5690
  for (const child of node.children) {
5624
- if (node.kind === "row" && child.kind === "cell" && node.styles.fill != null && child.styles.fill == null) {
5625
- child.styles.fill = node.styles.fill;
5626
- }
5627
5691
  const childSvg = renderNode(child);
5628
5692
  if (childSvg) parts.push(childSvg);
5629
5693
  }
@@ -6657,7 +6721,11 @@ function buildResolvedNode(graph, ir, parentWorldX, parentWorldY, diagnostics) {
6657
6721
  const children = [];
6658
6722
  for (const child of ir.children) {
6659
6723
  const childNode = buildResolvedNode(graph, child, worldRect.x, worldRect.y, diagnostics);
6660
- if (childNode) children.push(childNode);
6724
+ if (!childNode) continue;
6725
+ if (kind === "row" && childNode.kind === "cell" && styles.fill != null && childNode.styles.fill == null) {
6726
+ childNode.styles.fill = styles.fill;
6727
+ }
6728
+ children.push(childNode);
6661
6729
  }
6662
6730
  if (children.length > 0) {
6663
6731
  resolved.children = children;
@@ -6673,10 +6741,10 @@ var CONNECTOR_DEFAULTS = {
6673
6741
  };
6674
6742
  function fixConnectorPaths(nodes, _graph, ir) {
6675
6743
  const worldRectMap = /* @__PURE__ */ new Map();
6676
- collectWorldRects(nodes, worldRectMap);
6744
+ const connectors = [];
6745
+ collectWorldRects(nodes, worldRectMap, connectors);
6677
6746
  const connectorInfos = [];
6678
- for (const node of nodes) {
6679
- if (node.kind !== "connector") continue;
6747
+ for (const node of connectors) {
6680
6748
  const irNode = findIrNode(ir, node.id);
6681
6749
  if (!irNode) continue;
6682
6750
  const fromId = irNode.props.from;
@@ -6956,11 +7024,12 @@ function edgePoint(rect, edge, t) {
6956
7024
  return { x: rect.x + rect.width, y: rect.y + rect.height * t };
6957
7025
  }
6958
7026
  }
6959
- function collectWorldRects(nodes, map) {
7027
+ function collectWorldRects(nodes, map, connectors) {
6960
7028
  for (const node of nodes) {
6961
7029
  map.set(node.id, node.worldRect);
7030
+ if (node.kind === "connector") connectors.push(node);
6962
7031
  if (node.children) {
6963
- collectWorldRects(node.children, map);
7032
+ collectWorldRects(node.children, map, connectors);
6964
7033
  }
6965
7034
  }
6966
7035
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "diagram-contracts",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "type": "module",
5
5
  "description": "Rule Catalog driven layout runtime — compile diagrams to deterministic SVG from Draw TSX, Draw Text, or Layout IR",
6
6
  "keywords": [
@@ -64,7 +64,7 @@
64
64
  {
65
65
  "name": "browser bundle (self-contained, no TS compiler — issue #53)",
66
66
  "path": "dist/diagram-contracts.browser.mjs",
67
- "limit": "145 KB"
67
+ "limit": "170 KB"
68
68
  },
69
69
  {
70
70
  "name": "node library entry (dist/index.mjs, typescript external)",
@@ -91,7 +91,7 @@
91
91
  "js-yaml": "^5.0.0",
92
92
  "opentype.js": "^2.0.0",
93
93
  "typescript": "^6.0.3",
94
- "zod": "^4.4.3"
94
+ "zod": "^4.6.7"
95
95
  },
96
96
  "devDependencies": {
97
97
  "@size-limit/file": "^11.0.0",
@@ -104,9 +104,12 @@
104
104
  "jsdom": "^29.1.1",
105
105
  "size-limit": "^11.0.0",
106
106
  "tsup": "^8.5.1",
107
- "vitest": "^4.1.9"
107
+ "vitest": "^4.1.12"
108
108
  },
109
109
  "overrides": {
110
- "esbuild": "$esbuild"
110
+ "esbuild": "$esbuild",
111
+ "embedoc": {
112
+ "glob": "^14.0.6"
113
+ }
111
114
  }
112
115
  }