@ogabrielluiz/patchflow 0.1.1 → 0.1.4

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.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  SIGNAL_OPERATORS,
3
3
  __commonJS,
4
4
  __toESM
5
- } from "./chunk-B7DUSPQH.js";
5
+ } from "./chunk-YONEHQMI.js";
6
6
 
7
7
  // node_modules/@dagrejs/graphlib/lib/graph.js
8
8
  var require_graph = __commonJS({
@@ -3542,7 +3542,8 @@ var errorMessages = {
3542
3542
  duplicateModule: (name) => `Module "${name}" declared more than once.`,
3543
3543
  didYouMean: (got, suggestion) => `Module "${got}" is not declared. Did you mean "${suggestion}"?`,
3544
3544
  syntaxError: (detail) => `Syntax error: ${detail}`,
3545
- emptyDiagram: () => `Diagram is empty \u2014 no connections found.`
3545
+ emptyDiagram: () => `Diagram is empty \u2014 no connections found.`,
3546
+ ambiguousPortDirection: (port, module) => `Port "${port}" on module "${module}" is used as both input and output. Use distinct names like "In ${port}"/"Out ${port}" to disambiguate.`
3546
3547
  };
3547
3548
  function editDistance(a, b) {
3548
3549
  const la = a.length;
@@ -3852,6 +3853,39 @@ function parse(input) {
3852
3853
  }
3853
3854
  }
3854
3855
  }
3856
+ const portUsageMap = /* @__PURE__ */ new Map();
3857
+ const allConns = [...forward, ...feedback];
3858
+ for (const conn of allConns) {
3859
+ const srcBlock = conn.source.blockId;
3860
+ if (!portUsageMap.has(srcBlock)) portUsageMap.set(srcBlock, /* @__PURE__ */ new Map());
3861
+ const srcPorts = portUsageMap.get(srcBlock);
3862
+ if (!srcPorts.has(conn.source.portId)) srcPorts.set(conn.source.portId, { asSource: [], asTarget: [] });
3863
+ srcPorts.get(conn.source.portId).asSource.push(0);
3864
+ const tgtBlock = conn.target.blockId;
3865
+ if (!portUsageMap.has(tgtBlock)) portUsageMap.set(tgtBlock, /* @__PURE__ */ new Map());
3866
+ const tgtPorts = portUsageMap.get(tgtBlock);
3867
+ if (!tgtPorts.has(conn.target.portId)) tgtPorts.set(conn.target.portId, { asSource: [], asTarget: [] });
3868
+ tgtPorts.get(conn.target.portId).asTarget.push(0);
3869
+ }
3870
+ for (const [blockId, portMap] of portUsageMap) {
3871
+ for (const [portId, usage] of portMap) {
3872
+ if (usage.asSource.length > 0 && usage.asTarget.length > 0) {
3873
+ const block = allBlocks.get(blockId);
3874
+ const blockLabel = block ? block.label : blockId;
3875
+ const srcConn = allConns.find((c) => c.source.blockId === blockId && c.source.portId === portId);
3876
+ const tgtConn = allConns.find((c) => c.target.blockId === blockId && c.target.portId === portId);
3877
+ const portDisplay = srcConn ? srcConn.source.portDisplay : tgtConn ? tgtConn.target.portDisplay : portId;
3878
+ warnings.push({
3879
+ code: "AMBIGUOUS_PORT_DIRECTION",
3880
+ message: errorMessages.ambiguousPortDirection(portDisplay, blockLabel),
3881
+ line: 0,
3882
+ column: 1,
3883
+ length: 1,
3884
+ severity: "warning"
3885
+ });
3886
+ }
3887
+ }
3888
+ }
3855
3889
  const graph = {
3856
3890
  declaredBlocks: declaredBlocksList,
3857
3891
  stubBlocks,
@@ -4068,23 +4102,33 @@ function collectPorts(blocks, connections) {
4068
4102
  }
4069
4103
  return result;
4070
4104
  }
4105
+ function startYForFace(blockY, blockHeight, portCount) {
4106
+ const topMargin = 40;
4107
+ const bottomMargin = 30;
4108
+ const topY = blockY + topMargin;
4109
+ const bottomY = blockY + blockHeight - bottomMargin;
4110
+ const availableHeight = bottomY - topY;
4111
+ const clusterHeight = Math.max(0, (portCount - 1) * 24);
4112
+ return topY + (availableHeight - clusterHeight) / 2;
4113
+ }
4071
4114
  function placePorts(block, ports) {
4072
4115
  const inPorts = ports.filter((p) => p.direction === "in");
4073
4116
  const outPorts = ports.filter((p) => p.direction === "out");
4074
4117
  const layoutPorts = [];
4075
- const startY = block.y + 40 + (block.subLabel ? 18 : 0);
4076
4118
  const spacing = 24;
4119
+ const inStartY = startYForFace(block.y, block.height, inPorts.length);
4077
4120
  inPorts.forEach((p, i) => {
4078
4121
  layoutPorts.push({
4079
4122
  ...p,
4080
- position: { x: block.x, y: startY + i * spacing },
4123
+ position: { x: block.x, y: inStartY + i * spacing },
4081
4124
  signalType: null
4082
4125
  });
4083
4126
  });
4127
+ const outStartY = startYForFace(block.y, block.height, outPorts.length);
4084
4128
  outPorts.forEach((p, i) => {
4085
4129
  layoutPorts.push({
4086
4130
  ...p,
4087
- position: { x: block.x + block.width, y: startY + i * spacing },
4131
+ position: { x: block.x + block.width, y: outStartY + i * spacing },
4088
4132
  signalType: null
4089
4133
  });
4090
4134
  });
@@ -4127,7 +4171,7 @@ function findPortPosition(block, portId, direction) {
4127
4171
  }
4128
4172
  function layout(graph, options = {}) {
4129
4173
  const direction = options.direction ?? "LR";
4130
- const rankSep = options.rankSep ?? 200;
4174
+ const rankSep = options.rankSep ?? 120;
4131
4175
  const nodeSep = options.nodeSep ?? 40;
4132
4176
  const allBlocks = [...graph.declaredBlocks, ...graph.stubBlocks];
4133
4177
  const allConnections = [...graph.connections, ...graph.feedbackEdges];
@@ -4240,14 +4284,33 @@ function layout(graph, options = {}) {
4240
4284
  const feedbackSpace = hasFeedback ? diagramBottom - maxY + feedbackArcOffset + 30 + 16 : 0;
4241
4285
  const width = maxX + margin;
4242
4286
  const height = maxY + margin + feedbackSpace;
4287
+ const warnings = checkHeightInvariant({
4288
+ blocks: layoutBlocks,
4289
+ height,
4290
+ hasFeedback,
4291
+ feedbackBottom: diagramBottom + feedbackArcOffset
4292
+ });
4243
4293
  return {
4244
4294
  blocks: layoutBlocks,
4245
4295
  connections: layoutConnections,
4246
4296
  width,
4247
4297
  height,
4248
- signalTypeStats: graph.signalTypeStats
4298
+ signalTypeStats: graph.signalTypeStats,
4299
+ warnings
4249
4300
  };
4250
4301
  }
4302
+ function checkHeightInvariant(args) {
4303
+ const { blocks, height, hasFeedback, feedbackBottom } = args;
4304
+ const warnings = [];
4305
+ const blockBottomMax = blocks.length > 0 ? Math.max(...blocks.map((b) => b.y + b.height)) : 0;
4306
+ const contentBottom = hasFeedback ? Math.max(blockBottomMax, feedbackBottom) : blockBottomMax;
4307
+ if (height < contentBottom) {
4308
+ warnings.push(
4309
+ `layout: computed height (${height.toFixed(1)}) is below content bottom (${contentBottom.toFixed(1)}); legend/notes may overlap block content`
4310
+ );
4311
+ }
4312
+ return warnings;
4313
+ }
4251
4314
 
4252
4315
  // src/renderer.ts
4253
4316
  function genId() {
@@ -4372,7 +4435,28 @@ var SIGNAL_PILL_LABEL = {
4372
4435
  trigger: "trig",
4373
4436
  clock: "clk"
4374
4437
  };
4375
- function buildLabels(theme, blocks) {
4438
+ var UPWARD_CABLE_THRESHOLD = 20;
4439
+ function computeLabelBelowMap(connections) {
4440
+ const map = /* @__PURE__ */ new Map();
4441
+ for (const conn of connections) {
4442
+ const srcKey = `${conn.source.blockId}:${conn.source.portId}:out`;
4443
+ const srcDy = conn.targetPoint.y - conn.sourcePoint.y;
4444
+ if (srcDy < -UPWARD_CABLE_THRESHOLD) {
4445
+ map.set(srcKey, true);
4446
+ } else if (!map.has(srcKey)) {
4447
+ map.set(srcKey, false);
4448
+ }
4449
+ const tgtKey = `${conn.target.blockId}:${conn.target.portId}:in`;
4450
+ const tgtDy = conn.sourcePoint.y - conn.targetPoint.y;
4451
+ if (conn.isFeedback || tgtDy > UPWARD_CABLE_THRESHOLD) {
4452
+ map.set(tgtKey, true);
4453
+ } else if (!map.has(tgtKey)) {
4454
+ map.set(tgtKey, false);
4455
+ }
4456
+ }
4457
+ return map;
4458
+ }
4459
+ function buildLabels(theme, blocks, connections) {
4376
4460
  const parts = [];
4377
4461
  const fontFamily = sanitizeForSvg(theme.port.fontFamily);
4378
4462
  const pillShow = theme.port.pill.show;
@@ -4381,50 +4465,51 @@ function buildLabels(theme, blocks) {
4381
4465
  const pillRadius = theme.port.pill.cornerRadius;
4382
4466
  const pillPadX = 3;
4383
4467
  const pillHeight = 11;
4384
- const pillGap = 6;
4385
4468
  const charWidth = 6.5;
4469
+ const pillOffsetAbove = 20;
4470
+ const nameOffsetAbove = 32;
4471
+ const pillOffsetBelow = 20;
4472
+ const nameOffsetBelow = 32;
4473
+ const belowMap = computeLabelBelowMap(connections);
4474
+ const labelOffsetX = 6;
4386
4475
  for (const block of blocks) {
4387
4476
  for (const port of block.ports) {
4388
4477
  const { x, y } = port.position;
4389
- const isOut = port.direction === "out";
4390
- const labelX = isOut ? x + 14 : x - 14;
4391
- const labelY = y + 3;
4392
- const anchor = isOut ? "start" : "end";
4478
+ const key = `${block.id}:${port.id}:${port.direction}`;
4479
+ const below = belowMap.get(key) === true;
4393
4480
  const display = sanitizeForSvg(port.display);
4394
- const labelWidth = port.display.length * charWidth;
4481
+ const isOutput = port.direction === "out";
4482
+ const textY = below ? y + nameOffsetBelow : y - nameOffsetAbove;
4483
+ const textX = isOutput ? x + labelOffsetX : x - labelOffsetX;
4484
+ const textAnchor = isOutput ? "start" : "end";
4395
4485
  parts.push(
4396
- `<text x="${labelX}" y="${labelY}" font-family="${fontFamily}" font-size="${theme.port.fontSize}" fill="${theme.port.labelColor}" font-weight="600" text-anchor="${anchor}">${display}</text>`
4486
+ `<text x="${textX}" y="${textY}" font-family="${fontFamily}" font-size="${theme.port.fontSize}" fill="${theme.port.labelColor}" font-weight="600" text-anchor="${textAnchor}" dominant-baseline="central">${display}</text>`
4397
4487
  );
4398
4488
  if (pillShow && port.signalType) {
4399
4489
  const pillText = SIGNAL_PILL_LABEL[port.signalType];
4400
4490
  const pillWidth = pillText.length * charWidth + pillPadX * 2;
4401
4491
  const pillColor = theme.cable.colors[port.signalType].stroke;
4402
- let pillX;
4403
- if (isOut) {
4404
- pillX = labelX + labelWidth + pillGap;
4405
- } else {
4406
- pillX = labelX - labelWidth - pillGap - pillWidth;
4407
- }
4408
- const pillY = y - pillHeight / 2;
4409
- const textX = pillX + pillWidth / 2;
4410
- const textY = pillY + pillHeight / 2 + pillFontSize / 2 - 1;
4492
+ const pillCenterY = below ? y + pillOffsetBelow : y - pillOffsetAbove;
4493
+ const pillX = isOutput ? x + labelOffsetX : x - labelOffsetX - pillWidth;
4494
+ const pillY = pillCenterY - pillHeight / 2;
4495
+ const pillTextX = pillX + pillWidth / 2;
4411
4496
  parts.push(
4412
4497
  `<rect class="pf-port-pill" x="${pillX}" y="${pillY}" width="${pillWidth}" height="${pillHeight}" rx="${pillRadius}" fill="${pillColor}" data-signal="${port.signalType}"/>`
4413
4498
  );
4414
4499
  parts.push(
4415
- `<text class="pf-port-pill-text" x="${textX}" y="${textY}" text-anchor="middle" font-family="${fontFamily}" font-size="${pillFontSize}" fill="${pillTextColor}" font-weight="600">${sanitizeForSvg(pillText)}</text>`
4500
+ `<text class="pf-port-pill-text" x="${pillTextX}" y="${pillCenterY}" text-anchor="middle" dominant-baseline="central" font-family="${fontFamily}" font-size="${pillFontSize}" fill="${pillTextColor}" font-weight="600">${sanitizeForSvg(pillText)}</text>`
4416
4501
  );
4417
4502
  }
4418
4503
  }
4419
4504
  }
4420
4505
  return parts.join("");
4421
4506
  }
4422
- function buildAnnotations(theme, connections) {
4507
+ function buildAnnotations(theme, connections, layoutHeight) {
4423
4508
  const annotated = connections.filter((c) => c.annotation);
4424
4509
  if (annotated.length === 0) return "";
4425
4510
  const parts = [];
4426
4511
  const fontFamily = sanitizeForSvg(theme.annotation.fontFamily);
4427
- const noteFontSize = theme.annotation.fontSize + 1;
4512
+ const noteFontSize = theme.annotation.fontSize + 2;
4428
4513
  const markerFontFamily = fontFamily;
4429
4514
  annotated.forEach((conn, i) => {
4430
4515
  const num = i + 1;
@@ -4442,44 +4527,48 @@ function buildAnnotations(theme, connections) {
4442
4527
  mx = (sx + tx) / 2;
4443
4528
  my = (sy + ty) / 2;
4444
4529
  }
4445
- const markerStroke = conn.isFeedback ? theme.cable.colors[conn.signalType].stroke : theme.label.color;
4530
+ const markerStroke = conn.isFeedback ? theme.cable.colors[conn.signalType].stroke : theme.annotation.color;
4446
4531
  parts.push(
4447
4532
  `<circle cx="${mx}" cy="${my}" r="8" fill="${theme.panel.highlight}" stroke="${markerStroke}" stroke-width="0.5" data-annotation-marker="${num}"/>`
4448
4533
  );
4449
4534
  parts.push(
4450
- `<text x="${mx}" y="${my + 3}" text-anchor="middle" font-family="${markerFontFamily}" font-size="9" fill="${theme.label.color}">${num}</text>`
4535
+ `<text x="${mx}" y="${my + 3}" text-anchor="middle" font-family="${markerFontFamily}" font-size="9" fill="${theme.annotation.color}">${num}</text>`
4451
4536
  );
4452
4537
  });
4453
4538
  const panelX = -120;
4454
- let panelY = 20;
4455
- const lineGap = 14;
4539
+ const lineGap = 16;
4540
+ const noteCount = annotated.length;
4541
+ const bottomY = layoutHeight - 10;
4542
+ const firstNoteY = bottomY - (noteCount - 1) * lineGap;
4456
4543
  annotated.forEach((conn, i) => {
4457
4544
  const num = i + 1;
4458
4545
  const noteText = `${num}. ${sanitizeForSvg(conn.annotation)}`;
4546
+ const noteY = firstNoteY + i * lineGap;
4459
4547
  parts.push(
4460
- `<text x="${panelX}" y="${panelY}" font-family="${fontFamily}" font-size="${noteFontSize}" fill="${theme.annotation.color}">${noteText}</text>`
4548
+ `<text x="${panelX}" y="${noteY}" font-family="${fontFamily}" font-size="${noteFontSize}" font-weight="600" fill="${theme.annotation.color}">${noteText}</text>`
4461
4549
  );
4462
- panelY += lineGap;
4463
4550
  });
4464
4551
  return parts.join("");
4465
4552
  }
4466
- function buildLegend(theme, layoutResult) {
4553
+ function buildLegend(theme, layoutResult, diagramBottom) {
4467
4554
  const order = ["audio", "cv", "pitch", "gate", "trigger", "clock"];
4468
4555
  const used = order.filter((t) => (layoutResult.signalTypeStats[t] ?? 0) > 0);
4469
4556
  if (used.length === 0) return "";
4470
4557
  const parts = [];
4471
4558
  const fontFamily = sanitizeForSvg(theme.annotation.fontFamily);
4472
4559
  const itemWidth = 70;
4473
- const y = layoutResult.height - 20;
4474
- let x = 20;
4475
- for (const sig of used) {
4560
+ const totalWidth = used.length * itemWidth;
4561
+ const legendStartX = layoutResult.width - totalWidth;
4562
+ const y = diagramBottom - 20;
4563
+ for (let i = 0; i < used.length; i++) {
4564
+ const sig = used[i];
4476
4565
  const color = theme.cable.colors[sig].stroke;
4566
+ const x = legendStartX + i * itemWidth;
4477
4567
  let g = `<g transform="translate(${x}, ${y})">`;
4478
4568
  g += `<line x1="0" y1="0" x2="20" y2="0" stroke="${color}" stroke-width="3" stroke-linecap="round"/>`;
4479
4569
  g += `<text x="26" y="3" font-family="${fontFamily}" font-size="9" fill="${theme.annotation.color}">${sig}</text>`;
4480
4570
  g += `</g>`;
4481
4571
  parts.push(g);
4482
- x += itemWidth;
4483
4572
  }
4484
4573
  return parts.join("");
4485
4574
  }
@@ -4502,20 +4591,26 @@ function renderSvg(layoutResult, theme) {
4502
4591
  `<pattern id="${idPrefix}-dots" width="${spacing}" height="${spacing}" patternUnits="userSpaceOnUse"><circle cx="${spacing / 2}" cy="${spacing / 2}" r="${theme.grid.dotRadius}" fill="${theme.grid.dotColor}" opacity="${theme.grid.opacity}"/></pattern>`
4503
4592
  );
4504
4593
  }
4594
+ const labelPadX = 130;
4595
+ const vbWidth = width + labelPadX * 2;
4596
+ const topPad = 40;
4597
+ const noteCount = layoutResult.connections.filter((c) => c.annotation).length;
4598
+ const notesHeight = noteCount > 0 ? noteCount * 16 + 10 : 0;
4599
+ const bottomPad = Math.max(40, notesHeight + 10);
4600
+ const vbHeight = height + topPad + bottomPad;
4601
+ const diagramBottom = height + bottomPad;
4505
4602
  const layers = [
4506
4603
  `<g class="pf-layer-bg">${buildBackground(theme, idPrefix, width, height)}</g>`,
4507
4604
  `<g class="pf-layer-cables">${buildCables(theme, layoutResult.connections)}</g>`,
4508
4605
  `<g class="pf-layer-panels" >${buildPanels(theme, idPrefix, layoutResult.blocks)}</g>`,
4509
4606
  `<g class="pf-layer-params">${buildParams(layoutResult.blocks, theme)}</g>`,
4510
4607
  `<g class="pf-layer-jacks">${buildJacks(theme, idPrefix, layoutResult.blocks)}</g>`,
4511
- `<g class="pf-layer-labels">${buildLabels(theme, layoutResult.blocks)}</g>`,
4512
- `<g class="pf-layer-annotations">${buildAnnotations(theme, layoutResult.connections)}</g>`,
4513
- `<g class="pf-layer-legend">${buildLegend(theme, layoutResult)}</g>`
4608
+ `<g class="pf-layer-labels">${buildLabels(theme, layoutResult.blocks, layoutResult.connections)}</g>`,
4609
+ `<g class="pf-layer-annotations">${buildAnnotations(theme, layoutResult.connections, diagramBottom)}</g>`,
4610
+ `<g class="pf-layer-legend">${buildLegend(theme, layoutResult, diagramBottom)}</g>`
4514
4611
  ].join("");
4515
4612
  const style = `<style>@media print { .pf-panel, .pf-jack { filter: none; } }</style>`;
4516
- const labelPadX = 130;
4517
- const vbWidth = width + labelPadX * 2;
4518
- const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${-labelPadX} 0 ${vbWidth} ${height}" width="100%" data-pf-min-width="${minWidth + labelPadX * 2}" role="img" aria-labelledby="${idPrefix}-title ${idPrefix}-desc"><title id="${idPrefix}-title">Patch diagram</title><desc id="${idPrefix}-desc">${desc}</desc>` + style + `<defs>${defsParts.join("")}</defs>` + layers + `</svg>`;
4613
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${-labelPadX} ${-topPad} ${vbWidth} ${vbHeight}" width="100%" data-pf-min-width="${minWidth + labelPadX * 2}" role="img" aria-labelledby="${idPrefix}-title ${idPrefix}-desc"><title id="${idPrefix}-title">Patch diagram</title><desc id="${idPrefix}-desc">${desc}</desc>` + style + `<defs>${defsParts.join("")}</defs>` + layers + `</svg>`;
4519
4614
  return svg;
4520
4615
  }
4521
4616
 
@@ -4578,7 +4673,7 @@ var defaultTheme = {
4578
4673
  annotation: {
4579
4674
  fontFamily: "'SF Mono', 'Fira Code', Consolas, 'Courier New', monospace",
4580
4675
  fontSize: 9,
4581
- color: "#888888",
4676
+ color: "#4a4a4a",
4582
4677
  haloColor: "#f7f5f0"
4583
4678
  },
4584
4679
  grid: {