@bpmnkit/core 1.16.0 → 1.17.0

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 (35) hide show
  1. package/dist/bpmn/auto-layout.d.ts +0 -8
  2. package/dist/bpmn/auto-layout.js +432 -59
  3. package/dist/bpmn/make-room.js +114 -0
  4. package/dist/bpmn/place-new-shapes.js +4 -2
  5. package/dist/layout/collaboration/crossings.d.ts +13 -0
  6. package/dist/layout/collaboration/crossings.js +37 -0
  7. package/dist/layout/collaboration/docked-labels.d.ts +21 -0
  8. package/dist/layout/collaboration/docked-labels.js +65 -0
  9. package/dist/layout/collaboration/message-tracks.d.ts +20 -0
  10. package/dist/layout/collaboration/message-tracks.js +78 -0
  11. package/dist/layout/collaboration/stack-crossings.d.ts +38 -0
  12. package/dist/layout/collaboration/stack-crossings.js +155 -0
  13. package/dist/layout/grid/edge-labels.js +21 -10
  14. package/dist/layout/layered/coords.d.ts +13 -0
  15. package/dist/layout/layered/coords.js +211 -0
  16. package/dist/layout/layered/index.d.ts +21 -0
  17. package/dist/layout/layered/index.js +288 -0
  18. package/dist/layout/layered/layers.d.ts +60 -0
  19. package/dist/layout/layered/layers.js +374 -0
  20. package/dist/layout/layered/route.d.ts +66 -0
  21. package/dist/layout/layered/route.js +369 -0
  22. package/dist/layout/layout-engine.d.ts +6 -2
  23. package/dist/layout/layout-engine.js +4 -2
  24. package/dist/layout/semantic/bands.js +8 -2
  25. package/dist/layout/semantic/graph.d.ts +33 -1
  26. package/dist/layout/semantic/graph.js +200 -4
  27. package/dist/layout/semantic/index.d.ts +8 -2
  28. package/dist/layout/semantic/index.js +114 -14
  29. package/dist/layout/semantic/lanes.d.ts +40 -0
  30. package/dist/layout/semantic/lanes.js +198 -0
  31. package/dist/layout/semantic/place.d.ts +44 -2
  32. package/dist/layout/semantic/place.js +220 -21
  33. package/dist/layout/semantic/route.js +35 -2
  34. package/dist/layout/types.d.ts +0 -8
  35. package/package.json +1 -1
@@ -0,0 +1,198 @@
1
+ import { pathCrossings } from "../collaboration/crossings.js";
2
+ /** Most lanes whose order is searched exhaustively (n! candidates). */
3
+ const MAX_ORDERED_LANES = 6;
4
+ /** Leaf lane id per flow-node ref, in declaration order; `flat` when no lane nests another. */
5
+ export function leafLanes(laneSet) {
6
+ const leaves = [];
7
+ const leafOf = new Map();
8
+ let flat = true;
9
+ const seen = new Set();
10
+ const walk = (lanes) => {
11
+ for (const lane of lanes) {
12
+ // Same guard as the placement's lane tree: a lane set containing itself must throw there, not loop here.
13
+ if (seen.has(lane))
14
+ return;
15
+ seen.add(lane);
16
+ if (lane.childLaneSet && lane.childLaneSet.lanes.length > 0) {
17
+ flat = false;
18
+ walk(lane.childLaneSet.lanes);
19
+ continue;
20
+ }
21
+ leaves.push(lane.id);
22
+ for (const ref of lane.flowNodeRefs)
23
+ if (!leafOf.has(ref))
24
+ leafOf.set(ref, lane.id);
25
+ }
26
+ };
27
+ walk(laneSet.lanes);
28
+ return { leaves, leafOf, flat };
29
+ }
30
+ /** Lane distance summed over every flow that leaves a lane, for a given top-to-bottom order. */
31
+ function crossLaneCost(pairs, pos) {
32
+ let cost = 0;
33
+ for (const [a, b] of pairs)
34
+ cost += Math.abs((pos.get(a) ?? 0) - (pos.get(b) ?? 0));
35
+ return cost;
36
+ }
37
+ /**
38
+ * Top-to-bottom lane order that keeps the flows between lanes short: the
39
+ * declared order unless another order is strictly cheaper (ties keep what the
40
+ * author wrote). Nested lane sets and sets beyond six lanes keep their order.
41
+ */
42
+ export function laneOrder(graph, leaves, leafOf, flat) {
43
+ if (!flat || leaves.length < 3 || leaves.length > MAX_ORDERED_LANES)
44
+ return leaves;
45
+ const pairs = [];
46
+ for (const flows of graph.outgoing.values()) {
47
+ for (const f of flows) {
48
+ // A loop or a lane's own end event should not pull the lanes apart.
49
+ if (graph.backEdges.has(f.id) || graph.byId.get(f.targetRef)?.type === "endEvent")
50
+ continue;
51
+ const from = graph.byId.has(f.sourceRef) ? leafOf.get(f.sourceRef) : undefined;
52
+ const to = leafOf.get(f.targetRef);
53
+ if (from && to && from !== to)
54
+ pairs.push([from, to]);
55
+ }
56
+ }
57
+ let best = leaves;
58
+ let bestCost = crossLaneCost(pairs, new Map(leaves.map((id, i) => [id, i])));
59
+ const rest = (items) => items.length <= 1
60
+ ? [items]
61
+ : items.flatMap((x, i) => rest([...items.slice(0, i), ...items.slice(i + 1)]).map((r) => [x, ...r]));
62
+ for (const order of rest(leaves)) {
63
+ const cost = crossLaneCost(pairs, new Map(order.map((id, i) => [id, i])));
64
+ if (cost < bestCost) {
65
+ best = order;
66
+ bestCost = cost;
67
+ }
68
+ }
69
+ return best;
70
+ }
71
+ /**
72
+ * Flows into the lane directly above or below that share their source's
73
+ * column, so the hand-over is a straight vertical run. One per source and
74
+ * direction; the target takes only that flow in, and neither end carries a
75
+ * boundary event (its handler would need the column).
76
+ */
77
+ export function laneHandoffs(graph, leafOf, order,
78
+ /** Only sources with a single way on: plain hand-overs, no splits. */
79
+ plainOnly = false) {
80
+ const pos = new Map(order.map((id, i) => [id, i]));
81
+ const out = new Set();
82
+ const forwardIn = (id) => (graph.incoming.get(id) ?? []).filter((f) => !graph.backEdges.has(f.id)).length;
83
+ for (const node of graph.nodes) {
84
+ const from = leafOf.get(node.id);
85
+ if (from === undefined || graph.attachers.has(node.id))
86
+ continue;
87
+ const taken = new Set();
88
+ if (plainOnly && (graph.outgoing.get(node.id) ?? []).length !== 1)
89
+ continue;
90
+ for (const flow of graph.outgoing.get(node.id) ?? []) {
91
+ if (graph.backEdges.has(flow.id))
92
+ continue;
93
+ const to = leafOf.get(flow.targetRef);
94
+ if (to === undefined || graph.attachers.has(flow.targetRef))
95
+ continue;
96
+ const step = (pos.get(to) ?? 0) - (pos.get(from) ?? 0);
97
+ if (Math.abs(step) !== 1 || taken.has(step) || forwardIn(flow.targetRef) !== 1)
98
+ continue;
99
+ taken.add(step);
100
+ out.add(flow.id);
101
+ }
102
+ }
103
+ return out;
104
+ }
105
+ /**
106
+ * Cost of a laned layout, lower is better, compared term by term (a refinement must not lose in any): flows
107
+ * through a shape that is not theirs, crossings between flows, bends, how far
108
+ * the happy path strays from its mean row, area.
109
+ */
110
+ export function laneCost(result) {
111
+ const nodes = new Map(result.nodes.map((n) => [n.id, n.bounds]));
112
+ let through = 0;
113
+ let bends = 0;
114
+ for (const e of result.edges) {
115
+ bends += Math.max(0, e.waypoints.length - 2);
116
+ for (let i = 1; i < e.waypoints.length; i++) {
117
+ const p = e.waypoints[i - 1];
118
+ const q = e.waypoints[i];
119
+ for (const [id, b] of nodes) {
120
+ if (id === e.sourceRef || id === e.targetRef)
121
+ continue;
122
+ if (Math.max(p.x, q.x) > b.x + 1 &&
123
+ Math.min(p.x, q.x) < b.x + b.width - 1 &&
124
+ Math.max(p.y, q.y) > b.y + 1 &&
125
+ Math.min(p.y, q.y) < b.y + b.height - 1)
126
+ through++;
127
+ }
128
+ }
129
+ }
130
+ let crossings = 0;
131
+ for (let i = 0; i < result.edges.length; i++)
132
+ for (let j = i + 1; j < result.edges.length; j++)
133
+ crossings += pathCrossings(result.edges[i].waypoints, result.edges[j].waypoints);
134
+ let right = 0;
135
+ let bottom = 0;
136
+ for (const b of nodes.values()) {
137
+ right = Math.max(right, b.x + b.width);
138
+ bottom = Math.max(bottom, b.y + b.height);
139
+ }
140
+ return [through, crossings, bends, happyAxisDev(result), lengthCV(result), right * bottom];
141
+ }
142
+ /** True when `a` is no worse than `b` in any term and better in at least one. */
143
+ export function cheaper(a, b) {
144
+ let better = false;
145
+ for (let i = 0; i < a.length; i++) {
146
+ if (a[i] > b[i])
147
+ return false;
148
+ if (a[i] < b[i])
149
+ better = true;
150
+ }
151
+ return better;
152
+ }
153
+ /**
154
+ * Spread of the rows the first-declared forward path from each start event
155
+ * visits, the way `pnpm layout:score` measures it: lanes in an order that
156
+ * puts the happy path's lanes together score lower.
157
+ */
158
+ function happyAxisDev(result) {
159
+ const centreY = new Map(result.nodes.map((n) => [n.id, n.bounds.y + n.bounds.height / 2]));
160
+ const devs = [];
161
+ for (const start of result.nodes.filter((n) => n.type === "startEvent")) {
162
+ const seen = new Set([start.id]);
163
+ let cur = start.id;
164
+ for (;;) {
165
+ const next = result.edges.find((e) => e.sourceRef === cur && !seen.has(e.targetRef));
166
+ if (!next)
167
+ break;
168
+ seen.add(next.targetRef);
169
+ cur = next.targetRef;
170
+ }
171
+ const ys = [...seen].map((id) => centreY.get(id)).filter((y) => y !== undefined);
172
+ const mean = ys.reduce((a, b) => a + b, 0) / Math.max(1, ys.length);
173
+ devs.push(Math.sqrt(ys.reduce((a, y) => a + (y - mean) ** 2, 0) / Math.max(1, ys.length)));
174
+ }
175
+ return Math.round(devs.reduce((a, b) => a + b, 0) / Math.max(1, devs.length));
176
+ }
177
+ /** Lane membership, declared order and the cheaper top-to-bottom order, when there is one. */
178
+ export function lanePlan(graph, laneSet) {
179
+ const { leaves, leafOf, flat } = leafLanes(laneSet);
180
+ const order = laneOrder(graph, leaves, leafOf, flat);
181
+ return { leafOf, order: flat && order !== leaves ? order : undefined, declared: leaves, flat };
182
+ }
183
+ /** Coefficient of variation of the flows' path lengths, to two decimals. */
184
+ function lengthCV(result) {
185
+ const lengths = result.edges.map((e) => {
186
+ let sum = 0;
187
+ for (let i = 1; i < e.waypoints.length; i++) {
188
+ const p = e.waypoints[i - 1];
189
+ const q = e.waypoints[i];
190
+ sum += Math.abs(p.x - q.x) + Math.abs(p.y - q.y);
191
+ }
192
+ return sum;
193
+ });
194
+ const mean = lengths.reduce((a, b) => a + b, 0) / Math.max(1, lengths.length);
195
+ const sd = Math.sqrt(lengths.reduce((a, l) => a + (l - mean) ** 2, 0) / Math.max(1, lengths.length));
196
+ return Math.round((100 * sd) / Math.max(1, mean)) / 100;
197
+ }
198
+ //# sourceMappingURL=lanes.js.map
@@ -1,7 +1,7 @@
1
1
  import type { BpmnFlowElement, BpmnLaneSet } from "../../bpmn/bpmn-model.js";
2
2
  import type { Bounds, GridHint } from "../types.js";
3
3
  import { type BandLayout } from "./bands.js";
4
- import type { SemanticGraph } from "./graph.js";
4
+ import { type SemanticGraph } from "./graph.js";
5
5
  /**
6
6
  * Geometry constants. Layout may add space beyond these; it never takes any back.
7
7
  * Hand-drawn diagrams keep about 50-60 px between neighbouring shapes; wider
@@ -49,6 +49,14 @@ export declare function sizeOf(el: BpmnFlowElement, childExtent?: Bounds): {
49
49
  * their tallest. Nodes are centred in both, so any two nodes sharing a band
50
50
  * share a centre line and the spine routes as one straight segment.
51
51
  */
52
+ export interface LaneOptions {
53
+ /** Leaf lane ids top to bottom (default: declared order). */
54
+ order?: readonly string[];
55
+ /** Pull off-row nodes up onto earlier rows of their lane. */
56
+ compact?: boolean;
57
+ /** Nodes of different lanes never push each other down a row. */
58
+ byLane?: boolean;
59
+ }
52
60
  export declare function place(graph: SemanticGraph, bandLayout: BandLayout, sizes: Map<string, {
53
61
  width: number;
54
62
  height: number;
@@ -73,5 +81,39 @@ grid?: GridHint,
73
81
  * container's top to its inner spine. The container is hung so that spine
74
82
  * continues the row's line, and the flow runs straight through it.
75
83
  */
76
- axes?: ReadonlyMap<string, number>): Placement;
84
+ axes?: ReadonlyMap<string, number>,
85
+ /** Slot flow-less data references under the activity they are wired to. */
86
+ slotArtifacts?: boolean,
87
+ /** Lane refinements; absent or all off lays lanes out the way they always were. */
88
+ laneOpts?: LaneOptions): Placement;
89
+ /**
90
+ * The gap in front of each rank. At least {@link H_GAP}; wider where a flow
91
+ * label sits between two neighbouring columns, or where events' and gateways'
92
+ * labels, centred below shapes narrower than themselves, would meet.
93
+ */
94
+ export declare function columnGaps(graph: SemanticGraph, sizes: Map<string, {
95
+ width: number;
96
+ height: number;
97
+ }>, colWidth: Map<number, number>, ranks: number[]): Map<number, number>;
98
+ /**
99
+ * Dock boundary events on their host's edge — escalation on top, everything
100
+ * else on the bottom — spread along it. Events sharing a side are ordered by
101
+ * how far out from that border their handler ended up, then how far it runs —
102
+ * farthest first, so outer slots serve outer paths and the routes nest.
103
+ * The side comes from {@link boundarySide}, the rule that also placed the
104
+ * handler's band, so a handler is never routed around its own host.
105
+ */
106
+ export declare function dockBoundaryEvents(graph: SemanticGraph, bounds: Map<string, Bounds>, titled: ReadonlySet<string>): void;
107
+ /**
108
+ * Flow-less data references mapped to the activity they are wired to (the
109
+ * first one declared), so they can be slotted next to it.
110
+ */
111
+ export declare function artifactOwnersOf(graph: SemanticGraph): Map<string, string>;
112
+ /**
113
+ * Put each artifact on its activity's centre line, below it first (the
114
+ * association is then one short vertical line), above it for a second one
115
+ * (and first when boundary events hang below). More go beside those, and a
116
+ * taken spot sends the artifact down until the area is clear.
117
+ */
118
+ export declare function placeArtifacts(graph: SemanticGraph, owners: ReadonlyMap<string, string>, bounds: Map<string, Bounds>): void;
77
119
  //# sourceMappingURL=place.d.ts.map
@@ -1,5 +1,7 @@
1
1
  import { ELEMENT_SIZES, LABEL_CHAR_WIDTH, LABEL_HEIGHT, LABEL_MIN_WIDTH, LABEL_VERTICAL_OFFSET, } from "../types.js";
2
2
  import { boundarySide } from "./bands.js";
3
+ import { isDetour } from "./graph.js";
4
+ import { leafLanes } from "./lanes.js";
3
5
  /**
4
6
  * Geometry constants. Layout may add space beyond these; it never takes any back.
5
7
  * Hand-drawn diagrams keep about 50-60 px between neighbouring shapes; wider
@@ -7,6 +9,8 @@ import { boundarySide } from "./bands.js";
7
9
  */
8
10
  export const H_GAP = 60;
9
11
  export const V_GAP = 60;
12
+ /** Gap above a row of stacked detours (see `isDetour`): tasks on a 120 px pitch. */
13
+ const DETOUR_GAP = 40;
10
14
  /**
11
15
  * Gap beside a column holding an expanded container. The container's border,
12
16
  * its title and the notes on its children crowd that gap, so it keeps more air.
@@ -19,8 +23,13 @@ const EXTERNAL_LABEL_OFFSET = 4;
19
23
  const GRID_GAP_X = 40;
20
24
  const GRID_GAP_Y = 20;
21
25
  export const COMPONENT_GAP = 80;
26
+ const ROW_GRID = 10;
22
27
  export const LANE_PADDING = 40;
28
+ /** Vertical lane padding when lanes hand over in a shared column: 2 × 30 + 80 puts rows 140 apart, a 60 px flow. */
29
+ const HAND_OVER_PADDING = 30;
23
30
  export const BOUNDARY_SIZE = 36;
31
+ /** Air kept between two boundary events docked side by side, and before a host's corner. */
32
+ const BOUNDARY_CLEARANCE = 4;
24
33
  /** Height of a lane band with nothing in it: room for one boundary-sized row. */
25
34
  export const MIN_LANE_HEIGHT = 2 * LANE_PADDING + BOUNDARY_SIZE;
26
35
  const DEFAULT_SIZE = { width: 100, height: 80 };
@@ -50,13 +59,6 @@ export function sizeOf(el, childExtent) {
50
59
  return { width: childExtent.width, height: childExtent.height };
51
60
  return ELEMENT_SIZES[el.type] ?? DEFAULT_SIZE;
52
61
  }
53
- /**
54
- * Turn ranks and bands into coordinates.
55
- *
56
- * Ranks become columns sized by their widest node, bands become rows sized by
57
- * their tallest. Nodes are centred in both, so any two nodes sharing a band
58
- * share a centre line and the spine routes as one straight segment.
59
- */
60
62
  export function place(graph, bandLayout, sizes, laneSet,
61
63
  /**
62
64
  * Pack nodes that have no sequence flow at all into a wrapped grid rather
@@ -78,7 +80,11 @@ grid,
78
80
  * container's top to its inner spine. The container is hung so that spine
79
81
  * continues the row's line, and the flow runs straight through it.
80
82
  */
81
- axes = new Map()) {
83
+ axes = new Map(),
84
+ /** Slot flow-less data references under the activity they are wired to. */
85
+ slotArtifacts = false,
86
+ /** Lane refinements; absent or all off lays lanes out the way they always were. */
87
+ laneOpts = {}) {
82
88
  const bounds = new Map();
83
89
  const componentOf = new Map();
84
90
  for (let i = 0; i < graph.components.length; i++) {
@@ -115,11 +121,15 @@ axes = new Map()) {
115
121
  // ── Rows: one line per band, laid out per component then stacked ──
116
122
  const loose = packLoose ? looseNodes(graph) : [];
117
123
  const looseSet = new Set(loose);
124
+ const artifactOwners = laneSet || packLoose || !slotArtifacts ? new Map() : artifactOwnersOf(graph);
118
125
  let cursorY = 0;
119
126
  for (let component = 0; component < graph.components.length; component++) {
120
127
  const members = (graph.components[component] ?? []).filter((id) => graph.byId.has(id));
121
128
  if (members.length === 0)
122
129
  continue;
130
+ // An artifact wired to one activity is slotted under it below, not stacked apart.
131
+ if (members.length === 1 && artifactOwners.has(members[0]))
132
+ continue;
123
133
  if (members.length === 1 && looseSet.has(members[0])) {
124
134
  // The whole grid takes the place of the first loose node; the rest are in it.
125
135
  if (members[0] === loose[0])
@@ -151,9 +161,10 @@ axes = new Map()) {
151
161
  }
152
162
  cursorY = bottom + dy + COMPONENT_GAP;
153
163
  }
154
- separateSameCell(graph, bandLayout, bounds);
155
- const lanes = laneSet ? applyLanes(graph, laneSet, bounds) : [];
164
+ separateSameCell(graph, bandLayout, bounds, laneSet && laneOpts.byLane ? leafLanes(laneSet).leafOf : undefined);
165
+ const lanes = laneSet ? applyLanes(graph, laneSet, bounds, laneOpts) : [];
156
166
  dockBoundaryEvents(graph, bounds, titled);
167
+ placeArtifacts(graph, artifactOwners, bounds);
157
168
  return { bounds, lanes, gutterX };
158
169
  }
159
170
  /**
@@ -161,7 +172,7 @@ axes = new Map()) {
161
172
  * label sits between two neighbouring columns, or where events' and gateways'
162
173
  * labels, centred below shapes narrower than themselves, would meet.
163
174
  */
164
- function columnGaps(graph, sizes, colWidth, ranks) {
175
+ export function columnGaps(graph, sizes, colWidth, ranks) {
165
176
  const rankOf = (id) => {
166
177
  const el = graph.byId.get(id);
167
178
  const host = el?.type === "boundaryEvent" ? el.attachedToRef : id;
@@ -311,10 +322,14 @@ function bandLines(graph, bandLayout, members, sizes, xOf, axes, titled) {
311
322
  let previous = 0;
312
323
  for (const band of list) {
313
324
  const wide = span(band);
325
+ const members = byBand.get(band) ?? [];
326
+ // A row of stacked detours only carries short vertical flows into the
327
+ // row beside it: a 120 px row pitch for tasks, as hand layouts use.
328
+ const gap = members.every((id) => isDetour(graph, id)) ? DETOUR_GAP : V_GAP;
314
329
  let y = previous + dir;
315
330
  for (const box of placed) {
316
331
  if (xOverlap(box, wide)) {
317
- y = dir === 1 ? Math.max(y, box.bottom + V_GAP) : Math.min(y, box.top - V_GAP);
332
+ y = dir === 1 ? Math.max(y, box.bottom + gap) : Math.min(y, box.top - gap);
318
333
  }
319
334
  for (const id of byBand.get(band) ?? []) {
320
335
  const b = xOf(id);
@@ -324,10 +339,14 @@ function bandLines(graph, bandLayout, members, sizes, xOf, axes, titled) {
324
339
  continue;
325
340
  y =
326
341
  dir === 1
327
- ? Math.max(y, box.bottom + V_GAP + e.above)
328
- : Math.min(y, box.top - V_GAP - e.below);
342
+ ? Math.max(y, box.bottom + gap + e.above)
343
+ : Math.min(y, box.top - gap - e.below);
329
344
  }
330
345
  }
346
+ // A row of nothing but exception handlers or stacked detours keeps the
347
+ // spine's 10 px phase, so its tasks share the main row's grid.
348
+ if (members.every((id) => graph.exceptionRow.has(id) || isDetour(graph, id)))
349
+ y = dir * Math.ceil(Math.abs(y) / ROW_GRID) * ROW_GRID;
331
350
  place(band, y);
332
351
  previous = y;
333
352
  }
@@ -393,7 +412,9 @@ function placeGrid(graph, ids, sizes, top, bounds, hint) {
393
412
  * branches were compacted onto one band, for instance. Push the later ones
394
413
  * down in declaration order until the cell is clear.
395
414
  */
396
- function separateSameCell(graph, bandLayout, bounds) {
415
+ function separateSameCell(graph, bandLayout, bounds,
416
+ /** Nodes in different lanes never share a row, so a hand-over column needs no push. */
417
+ leafOf) {
397
418
  const componentOf = new Map();
398
419
  for (let i = 0; i < graph.components.length; i++) {
399
420
  for (const id of graph.components[i] ?? [])
@@ -405,7 +426,7 @@ function separateSameCell(graph, bandLayout, bounds) {
405
426
  for (const node of graph.nodes) {
406
427
  const rank = graph.ranks.get(node.id) ?? 0;
407
428
  const band = bandLayout.bands.get(node.id) ?? 0;
408
- const key = `${componentOf.get(node.id) ?? 0}:${rank}:${band}`;
429
+ const key = `${componentOf.get(node.id) ?? 0}:${rank}:${band}:${leafOf?.get(node.id) ?? ""}`;
409
430
  const list = cells.get(key);
410
431
  if (list)
411
432
  list.push(node.id);
@@ -432,7 +453,7 @@ function separateSameCell(graph, bandLayout, bounds) {
432
453
  * Nested lane sets keep both levels: leaf lanes take a band each, and an
433
454
  * ancestor lane spans the bands of its descendants.
434
455
  */
435
- function applyLanes(graph, laneSet, bounds) {
456
+ function applyLanes(graph, laneSet, bounds, { order, compact, byLane } = {}) {
436
457
  const tree = laneTree(laneSet);
437
458
  if (tree.length === 0)
438
459
  return [];
@@ -446,6 +467,12 @@ function applyLanes(graph, laneSet, bounds) {
446
467
  }
447
468
  };
448
469
  collectLeaves(tree);
470
+ if (order && order.length === leaves.length) {
471
+ const byId = new Map(leaves.map((l) => [l.id, l]));
472
+ const sorted = order.map((id) => byId.get(id));
473
+ if (sorted.every((l) => l !== undefined))
474
+ leaves.splice(0, leaves.length, ...sorted);
475
+ }
449
476
  const laneOf = new Map();
450
477
  for (const leaf of leaves) {
451
478
  for (const ref of leaf.refs)
@@ -499,15 +526,19 @@ function applyLanes(graph, laneSet, bounds) {
499
526
  const left = minX - LANE_PADDING;
500
527
  const span = new Map();
501
528
  let top = 0;
529
+ // Tight lanes make a hand-over exactly as long as the gap between columns.
530
+ const pad = byLane ? HAND_OVER_PADDING : LANE_PADDING;
502
531
  for (const leaf of leaves) {
532
+ if (compact)
533
+ compactLane(graph, members.get(leaf.id) ?? [], bounds);
503
534
  const own = (members.get(leaf.id) ?? [])
504
535
  .map((n) => bounds.get(n))
505
536
  .filter((b) => b !== undefined);
506
537
  let height = MIN_LANE_HEIGHT;
507
538
  if (own.length > 0) {
508
539
  const lowest = Math.min(...own.map((b) => b.y));
509
- height = Math.max(...own.map((b) => b.y + b.height)) - lowest + 2 * LANE_PADDING;
510
- const shift = top + LANE_PADDING - lowest;
540
+ height = Math.max(...own.map((b) => b.y + b.height)) - lowest + 2 * pad;
541
+ const shift = top + pad - lowest;
511
542
  for (const b of own)
512
543
  b.y += shift;
513
544
  }
@@ -577,6 +608,66 @@ function placeDataUnderActivities(members, dataHome, bounds) {
577
608
  }
578
609
  }
579
610
  }
611
+ /**
612
+ * Pull a lane's off-row nodes up onto an earlier row of the same lane when
613
+ * that row has room at their column and no flow between its own nodes runs
614
+ * through it, so a lane is as tall as its busiest row rather than as the
615
+ * bands it happened to inherit. Only nodes whose lane neighbours are all on
616
+ * the target row (or that have none) move: a node never leaves its chain.
617
+ */
618
+ function compactLane(graph, ids, bounds) {
619
+ const flowNodes = ids.filter((id) => graph.byId.has(id) && bounds.has(id));
620
+ if (flowNodes.length < 2 || flowNodes.some((id) => graph.byId.get(id)?.type.startsWith("data")))
621
+ return;
622
+ const inLane = new Set(flowNodes);
623
+ const centreY = (id) => {
624
+ const b = bounds.get(id);
625
+ return b.y + b.height / 2;
626
+ };
627
+ const rowOf = new Map(flowNodes.map((id) => [id, centreY(id)]));
628
+ const rows = [...new Set(rowOf.values())].sort((a, b) => a - b);
629
+ if (rows.length < 2)
630
+ return;
631
+ const neighbours = (id) => [
632
+ ...(graph.outgoing.get(id) ?? []).map((f) => f.targetRef),
633
+ ...(graph.incoming.get(id) ?? []).map((f) => graph.byId.get(f.sourceRef)?.id ?? f.sourceRef),
634
+ ].filter((n) => inLane.has(n) && n !== id);
635
+ const members = (row) => flowNodes.filter((id) => rowOf.get(id) === row);
636
+ const fits = (id, row) => {
637
+ const b = bounds.get(id);
638
+ const on = members(row);
639
+ for (const m of on) {
640
+ const o = bounds.get(m);
641
+ if (b.x - H_GAP < o.x + o.width && o.x - H_GAP < b.x + b.width)
642
+ return false;
643
+ }
644
+ for (const f of [...graph.outgoing.values()].flat()) {
645
+ if (!on.includes(f.sourceRef) || !on.includes(f.targetRef))
646
+ continue;
647
+ const from = bounds.get(f.sourceRef);
648
+ const to = bounds.get(f.targetRef);
649
+ const left = Math.min(from.x, to.x);
650
+ const right = Math.max(from.x + from.width, to.x + to.width);
651
+ if (b.x < right && b.x + b.width > left)
652
+ return false;
653
+ }
654
+ return true;
655
+ };
656
+ for (const row of rows.slice(1)) {
657
+ for (const id of members(row)) {
658
+ const near = neighbours(id);
659
+ for (const target of rows) {
660
+ if (target >= row)
661
+ break;
662
+ if (near.some((n) => rowOf.get(n) !== target) || !fits(id, target))
663
+ continue;
664
+ bounds.get(id).y += target - row;
665
+ rowOf.set(id, target);
666
+ break;
667
+ }
668
+ }
669
+ }
670
+ }
580
671
  /**
581
672
  * The lane set as a tree of ids and refs.
582
673
  *
@@ -604,7 +695,7 @@ function laneTree(laneSet, depth = 0, seen = new Set()) {
604
695
  * The side comes from {@link boundarySide}, the rule that also placed the
605
696
  * handler's band, so a handler is never routed around its own host.
606
697
  */
607
- function dockBoundaryEvents(graph, bounds, titled) {
698
+ export function dockBoundaryEvents(graph, bounds, titled) {
608
699
  for (const [hostId, events] of graph.attachers) {
609
700
  const host = bounds.get(hostId);
610
701
  if (!host)
@@ -623,12 +714,28 @@ function dockBoundaryEvents(graph, bounds, titled) {
623
714
  handlerDepth(graph, bounds, hostId, host, side, a.id) ||
624
715
  handlerReach(graph, b.id) - handlerReach(graph, a.id));
625
716
  const n = ordered.length;
717
+ // A handler in the host's own column (see handlerSteps in graph.ts) is
718
+ // under the host's centre: its event docks there and the flow drops
719
+ // straight down. The others share the half toward their handlers, right.
720
+ const pitch = n > 1 ? (host.width / 2 - BOUNDARY_CLEARANCE) / (n - 1) : 0;
721
+ const column = n === 1 || pitch >= BOUNDARY_SIZE + BOUNDARY_CLEARANCE
722
+ ? ordered.find((e) => inHostColumn(graph, bounds, hostId, host, side, e.id))
723
+ : undefined;
724
+ const others = ordered.filter((e) => e !== column);
725
+ const slot = (i) => {
726
+ const event = ordered[i];
727
+ if (column === undefined)
728
+ return ((i + 1) * host.width) / (n + 1);
729
+ if (event === column)
730
+ return host.width / 2;
731
+ return host.width / 2 + (others.indexOf(event) + 1) * pitch;
732
+ };
626
733
  for (let i = 0; i < n; i++) {
627
734
  const event = ordered[i];
628
735
  if (!event)
629
736
  continue;
630
737
  bounds.set(event.id, {
631
- x: host.x + ((i + 1) * host.width) / (n + 1) - BOUNDARY_SIZE / 2,
738
+ x: host.x + slot(i) - BOUNDARY_SIZE / 2,
632
739
  y: (side === "top" ? host.y : host.y + host.height) - BOUNDARY_SIZE / 2,
633
740
  width: BOUNDARY_SIZE,
634
741
  height: BOUNDARY_SIZE,
@@ -660,6 +767,20 @@ function handlerDepth(graph, bounds, hostId, host, side, eventId) {
660
767
  }
661
768
  return Number.isFinite(depth) ? depth : 0;
662
769
  }
770
+ /** True when a handler of this event sits straight out from the host's centre, past the docked side. */
771
+ function inHostColumn(graph, bounds, hostId, host, side, eventId) {
772
+ const mid = host.x + host.width / 2;
773
+ for (const flow of graph.outgoing.get(hostId) ?? []) {
774
+ if (flow.sourceRef !== eventId)
775
+ continue;
776
+ const target = bounds.get(flow.targetRef);
777
+ if (!target || Math.abs(target.x + target.width / 2 - mid) >= 1)
778
+ continue;
779
+ if (side === "bottom" ? target.y > host.y + host.height : target.y + target.height < host.y)
780
+ return true;
781
+ }
782
+ return false;
783
+ }
663
784
  /** How far the handler of a boundary event runs, in ranks. */
664
785
  function handlerReach(graph, eventId) {
665
786
  let reach = 0;
@@ -672,4 +793,82 @@ function handlerReach(graph, eventId) {
672
793
  }
673
794
  return reach;
674
795
  }
796
+ const ARTIFACT_TYPES = new Set(["dataObjectReference", "dataStoreReference"]);
797
+ /** Gap between an activity and the artifact slotted beside it. */
798
+ const ARTIFACT_GAP = 40;
799
+ const ARTIFACT_CLEARANCE = 10;
800
+ const ARTIFACT_SPACING = 20;
801
+ /**
802
+ * Flow-less data references mapped to the activity they are wired to (the
803
+ * first one declared), so they can be slotted next to it.
804
+ */
805
+ export function artifactOwnersOf(graph) {
806
+ const owners = new Map();
807
+ const isArtifact = (id) => {
808
+ const el = graph.byId.get(id);
809
+ return (el !== undefined &&
810
+ ARTIFACT_TYPES.has(el.type) &&
811
+ (graph.outgoing.get(id) ?? []).length === 0 &&
812
+ (graph.incoming.get(id) ?? []).length === 0);
813
+ };
814
+ const claim = (ref, owner) => {
815
+ if (!owners.has(ref) &&
816
+ isArtifact(ref) &&
817
+ !ARTIFACT_TYPES.has(graph.byId.get(owner)?.type ?? "")) {
818
+ owners.set(ref, owner);
819
+ }
820
+ };
821
+ for (const node of graph.nodes) {
822
+ for (const a of node.dataInputAssociations ?? [])
823
+ for (const ref of a.sourceRefs)
824
+ claim(ref, node.id);
825
+ for (const a of node.dataOutputAssociations ?? []) {
826
+ if (a.targetRef !== undefined)
827
+ claim(a.targetRef, node.id);
828
+ }
829
+ }
830
+ return owners;
831
+ }
832
+ /**
833
+ * Put each artifact on its activity's centre line, below it first (the
834
+ * association is then one short vertical line), above it for a second one
835
+ * (and first when boundary events hang below). More go beside those, and a
836
+ * taken spot sends the artifact down until the area is clear.
837
+ */
838
+ export function placeArtifacts(graph, owners, bounds) {
839
+ const taken = [...bounds.values()];
840
+ const hits = (b) => taken.some((o) => b.x - ARTIFACT_CLEARANCE < o.x + o.width &&
841
+ o.x < b.x + b.width + ARTIFACT_CLEARANCE &&
842
+ b.y - ARTIFACT_CLEARANCE < o.y + o.height &&
843
+ o.y < b.y + b.height + ARTIFACT_CLEARANCE);
844
+ for (const [ref, owner] of owners) {
845
+ const host = bounds.get(owner);
846
+ if (!host)
847
+ continue;
848
+ const size = ELEMENT_SIZES[graph.byId.get(ref)?.type ?? ""] ?? DEFAULT_SIZE;
849
+ const centre = host.x + host.width / 2;
850
+ const below = host.y + host.height + ARTIFACT_GAP;
851
+ const above = host.y - ARTIFACT_GAP - size.height;
852
+ const at = (dx, y) => ({
853
+ x: centre + dx - size.width / 2,
854
+ y,
855
+ width: size.width,
856
+ height: size.height,
857
+ });
858
+ const hung = (graph.attachers.get(owner) ?? []).length > 0;
859
+ const candidates = hung ? [at(0, above), at(0, below)] : [at(0, below), at(0, above)];
860
+ const step = size.width + ARTIFACT_SPACING;
861
+ for (const k of [1, 2, 3]) {
862
+ candidates.push(at(k * step, below), at(-k * step, below));
863
+ }
864
+ let spot = candidates.find((b) => !hits(b));
865
+ if (!spot) {
866
+ spot = at(0, below);
867
+ while (hits(spot))
868
+ spot = { ...spot, y: spot.y + 10 };
869
+ }
870
+ bounds.set(ref, spot);
871
+ taken.push(spot);
872
+ }
873
+ }
675
874
  //# sourceMappingURL=place.js.map