@bpmnkit/ascii 0.0.22 → 0.0.23

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.
Files changed (2) hide show
  1. package/dist/render.js +22 -10
  2. package/package.json +2 -2
package/dist/render.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Bpmn, GRID_CELL_HEIGHT, layoutFlowNodes } from "@bpmnkit/core";
1
+ import { Bpmn, GRID_CELL_HEIGHT, GRID_CELL_WIDTH, layoutFlowNodes } from "@bpmnkit/core";
2
2
  import { drawPortedEdge } from "./edges.js";
3
3
  import { AsciiGrid } from "./grid.js";
4
4
  import { CELL_H, CELL_W, drawElement, entryCol, exitCol, midCol } from "./shapes.js";
@@ -18,6 +18,17 @@ function nodeMidRow(node) {
18
18
  // midRow = cell top + vertical padding + 1 (same formula as the old elemRow+1)
19
19
  return asciiCellRow + Math.floor((CELL_H - 3) / 2) + 1;
20
20
  }
21
+ /**
22
+ * Derive the layer (column index) from a node's pixel x-coordinate.
23
+ *
24
+ * The block-based layout engine does not assign meaningful `layer` values
25
+ * (all nodes get layer=0), but positions elements correctly in pixel space.
26
+ * We recover the column index from the x-center of the element's bounds.
27
+ */
28
+ function nodeLayer(node) {
29
+ const centerX = node.bounds.x + node.bounds.width / 2;
30
+ return Math.round(centerX / GRID_CELL_WIDTH);
31
+ }
21
32
  const GATEWAY_TYPES = new Set([
22
33
  "exclusiveGateway",
23
34
  "parallelGateway",
@@ -44,7 +55,7 @@ export function renderBpmnAscii(xml, options) {
44
55
  if (nodes.length === 0)
45
56
  return "(empty)";
46
57
  // Compute grid dimensions from the layout's layer extents and pixel bounds
47
- const maxLayer = Math.max(...nodes.map((n) => n.layer));
58
+ const maxLayer = Math.max(...nodes.map(nodeLayer));
48
59
  const maxMidRow = Math.max(...nodes.map(nodeMidRow));
49
60
  const gridCols = (maxLayer + 1) * CELL_W + 4;
50
61
  // Element bottom is at maxMidRow+1; leave CELL_H rows of padding below.
@@ -63,11 +74,11 @@ export function renderBpmnAscii(xml, options) {
63
74
  if (!src || !dst)
64
75
  continue;
65
76
  const { srcPort, dstPort } = ports.get(edge.id) ?? { srcPort: "right", dstPort: "left" };
66
- drawPortedEdge(grid, srcPort === "right" ? exitCol(src.type, src.layer) : midCol(src.type, src.layer), nodeMidRow(src), srcPort, dstPort === "left" ? entryCol(dst.type, dst.layer) : midCol(dst.type, dst.layer), nodeMidRow(dst), dstPort, edge.label);
77
+ drawPortedEdge(grid, srcPort === "right" ? exitCol(src.type, nodeLayer(src)) : midCol(src.type, nodeLayer(src)), nodeMidRow(src), srcPort, dstPort === "left" ? entryCol(dst.type, nodeLayer(dst)) : midCol(dst.type, nodeLayer(dst)), nodeMidRow(dst), dstPort, edge.label);
67
78
  }
68
79
  // Draw element boxes on top
69
80
  for (const node of nodes) {
70
- drawElement(grid, node.type, node.layer, nodeMidRow(node) - 1, node.label);
81
+ drawElement(grid, node.type, nodeLayer(node), nodeMidRow(node) - 1, node.label);
71
82
  }
72
83
  const diagram = grid.toString();
73
84
  // Optional title header
@@ -112,7 +123,7 @@ function computeEdgePorts(edges, nodeById) {
112
123
  const src = nodeById.get(edge.sourceRef);
113
124
  const dst = nodeById.get(edge.targetRef);
114
125
  // Back-edges (dst layer ≤ src layer) and non-gateway sources always exit right
115
- if (!src || !dst || !GATEWAY_TYPES.has(src.type) || dst.layer <= src.layer) {
126
+ if (!src || !dst || !GATEWAY_TYPES.has(src.type) || nodeLayer(dst) <= nodeLayer(src)) {
116
127
  srcPorts.set(edge.id, "right");
117
128
  continue;
118
129
  }
@@ -129,9 +140,8 @@ function computeEdgePorts(edges, nodeById) {
129
140
  }
130
141
  // Sort outgoing edges by their target's row position (ascending = topmost first)
131
142
  const sorted = [...outgoing].sort((a, b) => {
132
- const pa = nodeById.get(a.targetRef)?.position ?? 0;
133
- const pb = nodeById.get(b.targetRef)?.position ?? 0;
134
- return pa - pb;
143
+ // biome-ignore lint/style/noNonNullAssertion: edges reference existing nodes
144
+ return nodeMidRow(nodeById.get(a.targetRef)) - nodeMidRow(nodeById.get(b.targetRef));
135
145
  });
136
146
  for (let i = 0; i < sorted.length; i++) {
137
147
  // biome-ignore lint/style/noNonNullAssertion: loop bounds guarantee element
@@ -160,9 +170,11 @@ function computeEdgePorts(edges, nodeById) {
160
170
  const src = nodeById.get(edge.sourceRef);
161
171
  const dst = nodeById.get(edge.targetRef);
162
172
  if (src && dst && GATEWAY_TYPES.has(dst.type)) {
163
- if (src.position < dst.position)
173
+ const srcRow = nodeMidRow(src);
174
+ const dstRow = nodeMidRow(dst);
175
+ if (srcRow < dstRow)
164
176
  dstPort = "top";
165
- else if (src.position > dst.position)
177
+ else if (srcRow > dstRow)
166
178
  dstPort = "bottom";
167
179
  // else same row → left (default)
168
180
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bpmnkit/ascii",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -17,7 +17,7 @@
17
17
  "dist/**/*.d.ts"
18
18
  ],
19
19
  "dependencies": {
20
- "@bpmnkit/core": "0.0.22"
20
+ "@bpmnkit/core": "0.0.23"
21
21
  },
22
22
  "description": "Render BPMN diagrams as Unicode box-drawing ASCII art — perfect for terminals and docs",
23
23
  "keywords": [