@bpmnkit/core 1.0.0 → 1.2.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 (73) hide show
  1. package/README.md +30 -3
  2. package/dist/bpmn/agentic.js +3 -1
  3. package/dist/bpmn/auto-layout.js +184 -15
  4. package/dist/bpmn/bpmn-builder.js +69 -7
  5. package/dist/bpmn/bpmn-model.d.ts +78 -0
  6. package/dist/bpmn/bpmn-parser.js +195 -37
  7. package/dist/bpmn/bpmn-serializer.js +121 -72
  8. package/dist/bpmn/bpmnlint.d.ts +153 -0
  9. package/dist/bpmn/bpmnlint.js +364 -0
  10. package/dist/bpmn/camunda-compat-data.d.ts +165 -0
  11. package/dist/bpmn/camunda-compat-data.js +423 -0
  12. package/dist/bpmn/camunda-compat-feel.d.ts +68 -0
  13. package/dist/bpmn/camunda-compat-feel.js +338 -0
  14. package/dist/bpmn/camunda-compat.d.ts +120 -0
  15. package/dist/bpmn/camunda-compat.js +1677 -0
  16. package/dist/bpmn/camunda7/juel.d.ts +41 -0
  17. package/dist/bpmn/camunda7/juel.js +333 -0
  18. package/dist/bpmn/camunda7/migrate.d.ts +72 -0
  19. package/dist/bpmn/camunda7/migrate.js +1152 -0
  20. package/dist/bpmn/compact.d.ts +4 -0
  21. package/dist/bpmn/compact.js +80 -0
  22. package/dist/bpmn/full-operations.js +5 -2
  23. package/dist/bpmn/label-style.d.ts +51 -0
  24. package/dist/bpmn/label-style.js +116 -0
  25. package/dist/bpmn/lint.d.ts +23 -3
  26. package/dist/bpmn/lint.js +47 -10
  27. package/dist/bpmn/optimize/bpmnlint-rules.d.ts +31 -0
  28. package/dist/bpmn/optimize/bpmnlint-rules.js +916 -0
  29. package/dist/bpmn/optimize/deploy.d.ts +8 -2
  30. package/dist/bpmn/optimize/deploy.js +16 -3
  31. package/dist/bpmn/optimize/index.js +11 -2
  32. package/dist/bpmn/optimize/patterns.js +3 -0
  33. package/dist/bpmn/optimize/types.d.ts +14 -0
  34. package/dist/bpmn/optimize/variable-flow.js +4 -1
  35. package/dist/bpmn/process-delta.d.ts +88 -0
  36. package/dist/bpmn/process-delta.js +236 -0
  37. package/dist/bpmn/process-text-writer.d.ts +53 -0
  38. package/dist/bpmn/process-text-writer.js +193 -0
  39. package/dist/bpmn/process-text.d.ts +175 -0
  40. package/dist/bpmn/process-text.js +1255 -0
  41. package/dist/bpmn/process-types.d.ts +112 -0
  42. package/dist/bpmn/process-types.js +490 -0
  43. package/dist/bpmn/svg.js +51 -22
  44. package/dist/doc/docx.d.ts +20 -0
  45. package/dist/doc/docx.js +253 -0
  46. package/dist/doc/html.d.ts +21 -0
  47. package/dist/doc/html.js +303 -0
  48. package/dist/doc/index.d.ts +7 -0
  49. package/dist/doc/index.js +5 -0
  50. package/dist/doc/markdown.d.ts +11 -0
  51. package/dist/doc/markdown.js +160 -0
  52. package/dist/doc/model.d.ts +129 -0
  53. package/dist/doc/model.js +617 -0
  54. package/dist/doc/zip.d.ts +5 -0
  55. package/dist/doc/zip.js +75 -0
  56. package/dist/index.d.ts +22 -0
  57. package/dist/index.js +11 -0
  58. package/dist/layout/annotations.d.ts +4 -2
  59. package/dist/layout/annotations.js +31 -2
  60. package/dist/layout/collaboration/ordering.d.ts +8 -0
  61. package/dist/layout/collaboration/ordering.js +57 -18
  62. package/dist/layout/orthogonal.d.ts +32 -0
  63. package/dist/layout/orthogonal.js +367 -0
  64. package/dist/layout/repair.d.ts +31 -0
  65. package/dist/layout/repair.js +220 -0
  66. package/dist/layout/semantic/route.js +45 -3
  67. package/dist/node/bpmnlint.d.ts +109 -0
  68. package/dist/node/bpmnlint.js +198 -0
  69. package/dist/node/index.d.ts +2 -0
  70. package/dist/node/index.js +1 -0
  71. package/dist/xml/xml-patch.d.ts +21 -0
  72. package/dist/xml/xml-patch.js +27 -1
  73. package/package.json +2 -2
@@ -0,0 +1,367 @@
1
+ /** Stub every route leaves a port by before it may turn. */
2
+ const STUB = 12;
3
+ const DEFAULT_MARGIN = 16;
4
+ /** A bend costs as much as this much extra length. */
5
+ const BEND_COST = 40;
6
+ /** A crossing costs as much as this much extra length. */
7
+ const CROSSING_COST = 160;
8
+ /** Negative: running along a shape's outline counts as passing through it. */
9
+ const TOLERANCE = -1;
10
+ /** How far around the two endpoints the first, cheaper search looks. */
11
+ const REGION = 240;
12
+ const DIRS = [
13
+ [0, -1],
14
+ [1, 0],
15
+ [0, 1],
16
+ [-1, 0],
17
+ ];
18
+ const SIDE_DIR = { top: 0, right: 1, bottom: 2, left: 3 };
19
+ /**
20
+ * Obstacle-aware orthogonal routing: a shortest path, with bends and crossings
21
+ * charged as extra length, over the sparse grid of lines that run just clear of
22
+ * every obstacle edge (an orthogonal visibility graph). Used where the cheap
23
+ * candidate routes all hit something, so its cost only lands on the hard cases.
24
+ *
25
+ * Returns `null` when no clear route exists at all.
26
+ */
27
+ export function routeOrthogonal(request) {
28
+ const margin = request.margin ?? DEFAULT_MARGIN;
29
+ const near = nearObstacles(request, REGION);
30
+ return (search(request, near, margin) ??
31
+ (near.length < request.obstacles.length ? search(request, request.obstacles, margin) : null));
32
+ }
33
+ function nearObstacles(request, reach) {
34
+ let minX = Number.POSITIVE_INFINITY;
35
+ let minY = Number.POSITIVE_INFINITY;
36
+ let maxX = Number.NEGATIVE_INFINITY;
37
+ let maxY = Number.NEGATIVE_INFINITY;
38
+ for (const port of [...request.from, ...request.to]) {
39
+ minX = Math.min(minX, port.point.x);
40
+ minY = Math.min(minY, port.point.y);
41
+ maxX = Math.max(maxX, port.point.x);
42
+ maxY = Math.max(maxY, port.point.y);
43
+ }
44
+ return request.obstacles.filter((o) => o.x < maxX + reach &&
45
+ o.x + o.width > minX - reach &&
46
+ o.y < maxY + reach &&
47
+ o.y + o.height > minY - reach);
48
+ }
49
+ function stubOf(port) {
50
+ const [dx, dy] = DIRS[SIDE_DIR[port.side]] ?? [0, 0];
51
+ return { x: port.point.x + dx * STUB, y: port.point.y + dy * STUB };
52
+ }
53
+ /** Sorted, de-duplicated coordinates. */
54
+ function axis(values) {
55
+ const sorted = values.map((v) => Math.round(v * 2) / 2).sort((a, b) => a - b);
56
+ const out = [];
57
+ for (const v of sorted)
58
+ if (out[out.length - 1] !== v)
59
+ out.push(v);
60
+ return out;
61
+ }
62
+ function segmentBlocked(a, b, obstacles) {
63
+ const minX = Math.min(a.x, b.x);
64
+ const maxX = Math.max(a.x, b.x);
65
+ const minY = Math.min(a.y, b.y);
66
+ const maxY = Math.max(a.y, b.y);
67
+ for (const o of obstacles) {
68
+ if (maxX <= o.x + TOLERANCE || o.x + o.width - TOLERANCE <= minX)
69
+ continue;
70
+ if (maxY <= o.y + TOLERANCE || o.y + o.height - TOLERANCE <= minY)
71
+ continue;
72
+ return true;
73
+ }
74
+ return false;
75
+ }
76
+ /** Index of `v` in a sorted array (exact match expected). */
77
+ function indexOf(sorted, v) {
78
+ const target = Math.round(v * 2) / 2;
79
+ let lo = 0;
80
+ let hi = sorted.length - 1;
81
+ while (lo <= hi) {
82
+ const mid = (lo + hi) >> 1;
83
+ const m = sorted[mid] ?? 0;
84
+ if (m === target)
85
+ return mid;
86
+ if (m < target)
87
+ lo = mid + 1;
88
+ else
89
+ hi = mid - 1;
90
+ }
91
+ return -1;
92
+ }
93
+ /** First index whose value is greater than `v`. */
94
+ function upper(sorted, v) {
95
+ let lo = 0;
96
+ let hi = sorted.length;
97
+ while (lo < hi) {
98
+ const mid = (lo + hi) >> 1;
99
+ if ((sorted[mid] ?? 0) <= v)
100
+ lo = mid + 1;
101
+ else
102
+ hi = mid;
103
+ }
104
+ return lo;
105
+ }
106
+ function search(request, obstacles, margin) {
107
+ const starts = request.from
108
+ .map((port) => ({ port, stub: stubOf(port) }))
109
+ .filter(({ port, stub }) => !segmentBlocked(port.point, stub, request.obstacles));
110
+ const ends = request.to
111
+ .map((port) => ({ port, stub: stubOf(port) }))
112
+ .filter(({ port, stub }) => !segmentBlocked(port.point, stub, request.obstacles));
113
+ if (starts.length === 0 || ends.length === 0)
114
+ return null;
115
+ const xsRaw = [];
116
+ const ysRaw = [];
117
+ for (const { stub } of [...starts, ...ends]) {
118
+ xsRaw.push(stub.x);
119
+ ysRaw.push(stub.y);
120
+ }
121
+ for (const o of obstacles) {
122
+ xsRaw.push(o.x - margin, o.x + o.width + margin);
123
+ ysRaw.push(o.y - margin, o.y + o.height + margin);
124
+ }
125
+ const xs = axis(xsRaw);
126
+ const ys = axis(ysRaw);
127
+ const nx = xs.length;
128
+ const ny = ys.length;
129
+ // Blocked grid steps: hBlocked[j * nx + i] is the step (i, j) → (i + 1, j),
130
+ // vBlocked[j * nx + i] the step (i, j) → (i, j + 1). Every obstacle marks the
131
+ // steps that would pass through it, so no step is tested against every shape.
132
+ const hBlocked = new Uint8Array(nx * ny);
133
+ const vBlocked = new Uint8Array(nx * ny);
134
+ for (const o of request.obstacles) {
135
+ const left = o.x + TOLERANCE;
136
+ const right = o.x + o.width - TOLERANCE;
137
+ const top = o.y + TOLERANCE;
138
+ const bottom = o.y + o.height - TOLERANCE;
139
+ if (right <= left || bottom <= top)
140
+ continue;
141
+ // Rows strictly inside the obstacle's vertical span.
142
+ // Steps (i, i + 1) overlapping (left, right): from the last line at or
143
+ // before `left` up to the last line before `right`.
144
+ const iFrom = Math.max(0, upper(xs, left) - 1);
145
+ const jFrom = Math.max(0, upper(ys, top) - 1);
146
+ for (let j = upper(ys, top); j < ny && (ys[j] ?? 0) < bottom; j++) {
147
+ for (let i = iFrom; i + 1 < nx && (xs[i] ?? 0) < right; i++)
148
+ hBlocked[j * nx + i] = 1;
149
+ }
150
+ for (let i = upper(xs, left); i < nx && (xs[i] ?? 0) < right; i++) {
151
+ for (let j = jFrom; j + 1 < ny && (ys[j] ?? 0) < bottom; j++)
152
+ vBlocked[j * nx + i] = 1;
153
+ }
154
+ }
155
+ const goal = new Map();
156
+ for (const { port, stub } of ends) {
157
+ const i = indexOf(xs, stub.x);
158
+ const j = indexOf(ys, stub.y);
159
+ if (i < 0 || j < 0)
160
+ continue;
161
+ // The final move runs into the target, opposite to the port's outward side.
162
+ const inward = (SIDE_DIR[port.side] + 2) % 4;
163
+ const key = (j * nx + i) * 4 + inward;
164
+ const cost = (port.cost ?? 0) + STUB;
165
+ goal.set(key, Math.min(goal.get(key) ?? Number.POSITIVE_INFINITY, cost));
166
+ }
167
+ if (goal.size === 0)
168
+ return null;
169
+ const goalPoints = ends.map((e) => e.stub);
170
+ const heuristic = (x, y) => {
171
+ let best = Number.POSITIVE_INFINITY;
172
+ for (const p of goalPoints)
173
+ best = Math.min(best, Math.abs(p.x - x) + Math.abs(p.y - y));
174
+ return best;
175
+ };
176
+ const states = nx * ny * 4;
177
+ const dist = new Float64Array(states).fill(Number.POSITIVE_INFINITY);
178
+ const prev = new Int32Array(states).fill(-1);
179
+ const heap = new MinHeap();
180
+ for (const { port, stub } of starts) {
181
+ const i = indexOf(xs, stub.x);
182
+ const j = indexOf(ys, stub.y);
183
+ if (i < 0 || j < 0)
184
+ continue;
185
+ const state = (j * nx + i) * 4 + SIDE_DIR[port.side];
186
+ const cost = (port.cost ?? 0) + STUB;
187
+ if (cost < (dist[state] ?? 0)) {
188
+ dist[state] = cost;
189
+ prev[state] = -2 - request.from.indexOf(port);
190
+ heap.push(state, cost + heuristic(stub.x, stub.y));
191
+ }
192
+ }
193
+ let best = -1;
194
+ let bestCost = Number.POSITIVE_INFINITY;
195
+ while (heap.size > 0) {
196
+ const [state, priority] = heap.pop();
197
+ if (priority >= bestCost)
198
+ break;
199
+ const d = dist[state] ?? 0;
200
+ const dir = state & 3;
201
+ const cell = state >> 2;
202
+ const i = cell % nx;
203
+ const j = (cell - i) / nx;
204
+ const x = xs[i] ?? 0;
205
+ const y = ys[j] ?? 0;
206
+ if (d + heuristic(x, y) < priority - 1e-6)
207
+ continue;
208
+ // Arrive at a goal stub: finish, turning in if needed.
209
+ for (let finalDir = 0; finalDir < 4; finalDir++) {
210
+ const extra = goal.get(cell * 4 + finalDir);
211
+ if (extra === undefined)
212
+ continue;
213
+ if (finalDir === (dir + 2) % 4)
214
+ continue;
215
+ const total = d + extra + (finalDir === dir ? 0 : BEND_COST);
216
+ if (total < bestCost) {
217
+ bestCost = total;
218
+ best = state;
219
+ }
220
+ }
221
+ for (let nd = 0; nd < 4; nd++) {
222
+ if (nd === (dir + 2) % 4)
223
+ continue;
224
+ const [dx, dy] = DIRS[nd] ?? [0, 0];
225
+ const ni = i + dx;
226
+ const nj = j + dy;
227
+ if (ni < 0 || nj < 0 || ni >= nx || nj >= ny)
228
+ continue;
229
+ const blocked = dx !== 0
230
+ ? hBlocked[j * nx + Math.min(i, ni)] === 1
231
+ : vBlocked[Math.min(j, nj) * nx + i] === 1;
232
+ if (blocked)
233
+ continue;
234
+ const nxv = xs[ni] ?? 0;
235
+ const nyv = ys[nj] ?? 0;
236
+ let cost = d + Math.abs(nxv - x) + Math.abs(nyv - y);
237
+ if (nd !== dir)
238
+ cost += BEND_COST;
239
+ if (request.crossings) {
240
+ cost += CROSSING_COST * request.crossings({ x, y }, { x: nxv, y: nyv });
241
+ }
242
+ const next = (nj * nx + ni) * 4 + nd;
243
+ if (cost < (dist[next] ?? 0)) {
244
+ dist[next] = cost;
245
+ prev[next] = state;
246
+ heap.push(next, cost + heuristic(nxv, nyv));
247
+ }
248
+ }
249
+ }
250
+ if (best < 0)
251
+ return null;
252
+ // Walk back to the start stub, then add the ports at either end.
253
+ const points = [];
254
+ let state = best;
255
+ let startPort;
256
+ for (;;) {
257
+ const cell = state >> 2;
258
+ const i = cell % nx;
259
+ const j = (cell - i) / nx;
260
+ points.push({ x: xs[i] ?? 0, y: ys[j] ?? 0 });
261
+ const p = prev[state] ?? -1;
262
+ if (p <= -2) {
263
+ startPort = request.from[-2 - p];
264
+ break;
265
+ }
266
+ state = p;
267
+ }
268
+ points.reverse();
269
+ if (!startPort)
270
+ return null;
271
+ const last = points[points.length - 1];
272
+ const endPort = ends.find(({ stub }) => last && stub.x === last.x && stub.y === last.y);
273
+ if (!endPort)
274
+ return null;
275
+ return simplify([startPort.point, ...points, endPort.port.point]);
276
+ }
277
+ /** Drop duplicate points and the middle of straight runs. */
278
+ function simplify(points) {
279
+ const out = [];
280
+ for (const p of points) {
281
+ const last = out[out.length - 1];
282
+ if (last && last.x === p.x && last.y === p.y)
283
+ continue;
284
+ out.push(p);
285
+ while (out.length >= 3) {
286
+ const a = out[out.length - 3];
287
+ const b = out[out.length - 2];
288
+ const c = out[out.length - 1];
289
+ if (!a || !b || !c)
290
+ break;
291
+ if ((a.x === b.x && b.x === c.x) || (a.y === b.y && b.y === c.y))
292
+ out.splice(-2, 1);
293
+ else
294
+ break;
295
+ }
296
+ }
297
+ return out;
298
+ }
299
+ /** Binary min-heap of states keyed by priority. */
300
+ class MinHeap {
301
+ items = [];
302
+ keys = [];
303
+ get size() {
304
+ return this.items.length;
305
+ }
306
+ push(item, key) {
307
+ const items = this.items;
308
+ const keys = this.keys;
309
+ let i = items.length;
310
+ items.push(item);
311
+ keys.push(key);
312
+ while (i > 0) {
313
+ const parent = (i - 1) >> 1;
314
+ if ((keys[parent] ?? 0) <= key)
315
+ break;
316
+ items[i] = items[parent] ?? 0;
317
+ keys[i] = keys[parent] ?? 0;
318
+ i = parent;
319
+ }
320
+ items[i] = item;
321
+ keys[i] = key;
322
+ }
323
+ pop() {
324
+ const items = this.items;
325
+ const keys = this.keys;
326
+ const top = [items[0] ?? 0, keys[0] ?? 0];
327
+ const lastItem = items.pop() ?? 0;
328
+ const lastKey = keys.pop() ?? 0;
329
+ const n = items.length;
330
+ if (n > 0) {
331
+ let i = 0;
332
+ for (;;) {
333
+ const l = 2 * i + 1;
334
+ if (l >= n)
335
+ break;
336
+ const r = l + 1;
337
+ const c = r < n && (keys[r] ?? 0) < (keys[l] ?? 0) ? r : l;
338
+ if ((keys[c] ?? 0) >= lastKey)
339
+ break;
340
+ items[i] = items[c] ?? 0;
341
+ keys[i] = keys[c] ?? 0;
342
+ i = c;
343
+ }
344
+ items[i] = lastItem;
345
+ keys[i] = lastKey;
346
+ }
347
+ return top;
348
+ }
349
+ }
350
+ /** The four side-centre ports of a rectangle, each charged `cost[side]` when given. */
351
+ export function sidePorts(b, cost = {}) {
352
+ const cx = b.x + b.width / 2;
353
+ const cy = b.y + b.height / 2;
354
+ const ports = [
355
+ { point: { x: cx, y: b.y }, side: "top" },
356
+ { point: { x: b.x + b.width, y: cy }, side: "right" },
357
+ { point: { x: cx, y: b.y + b.height }, side: "bottom" },
358
+ { point: { x: b.x, y: cy }, side: "left" },
359
+ ];
360
+ for (const port of ports) {
361
+ const c = cost[port.side];
362
+ if (c !== undefined)
363
+ port.cost = c;
364
+ }
365
+ return ports.filter((port) => port.cost === undefined || Number.isFinite(port.cost));
366
+ }
367
+ //# sourceMappingURL=orthogonal.js.map
@@ -0,0 +1,31 @@
1
+ import { type Side } from "./orthogonal.js";
2
+ import type { Bounds, Waypoint } from "./types.js";
3
+ /** What a connection is, which decides the sides it may leave and enter by. */
4
+ export type ConnectionKind = "sequence" | "message" | "association";
5
+ export interface PlaneShape {
6
+ id: string;
7
+ bounds: Bounds;
8
+ /** Pools, lanes and groups: drawn around other shapes, so never in the way. */
9
+ container: boolean;
10
+ }
11
+ export interface PlaneEdge {
12
+ kind: ConnectionKind;
13
+ sourceRef: string;
14
+ targetRef: string;
15
+ waypoints: Waypoint[];
16
+ }
17
+ /** Shapes a route may legitimately pass through or along, beyond its two ends. */
18
+ export type RelatedShapes = (elementId: string) => Iterable<string>;
19
+ /**
20
+ * Re-route every connection on a plane that runs through a shape it has nothing
21
+ * to do with. The layout's own routers place the common cases cheaply; this pass
22
+ * is the guarantee behind them, so it only spends a search on the edges that
23
+ * actually need one — annotations packed onto a route, a message flow with no
24
+ * clear column, an association cutting across an activity.
25
+ *
26
+ * Mutates `waypoints` in place.
27
+ */
28
+ export declare function repairRoutes(shapes: PlaneShape[], edges: PlaneEdge[], related: RelatedShapes,
29
+ /** Where a boundary event sits relative to its host, when it is one. */
30
+ boundarySide: (elementId: string) => Side | undefined): void;
31
+ //# sourceMappingURL=repair.d.ts.map
@@ -0,0 +1,220 @@
1
+ import { routeOrthogonal, sidePorts } from "./orthogonal.js";
2
+ /**
3
+ * Negative: a route running along a shape's outline reads as passing through
4
+ * it, so the box is grown by a pixel rather than shrunk.
5
+ */
6
+ const TOLERANCE = -1;
7
+ /** A route's own endpoints are shrunk instead: docking on the outline is the point. */
8
+ const OWN_END_TOLERANCE = 2;
9
+ /**
10
+ * Re-route every connection on a plane that runs through a shape it has nothing
11
+ * to do with. The layout's own routers place the common cases cheaply; this pass
12
+ * is the guarantee behind them, so it only spends a search on the edges that
13
+ * actually need one — annotations packed onto a route, a message flow with no
14
+ * clear column, an association cutting across an activity.
15
+ *
16
+ * Mutates `waypoints` in place.
17
+ */
18
+ export function repairRoutes(shapes, edges, related,
19
+ /** Where a boundary event sits relative to its host, when it is one. */
20
+ boundarySide) {
21
+ const byId = new Map(shapes.map((s) => [s.id, s]));
22
+ const obstacles = shapes.filter((s) => !s.container);
23
+ for (const edge of edges) {
24
+ const exempt = new Set([edge.sourceRef, edge.targetRef]);
25
+ for (const id of related(edge.sourceRef))
26
+ exempt.add(id);
27
+ for (const id of related(edge.targetRef))
28
+ exempt.add(id);
29
+ const blocking = obstacles.filter((s) => !exempt.has(s.id));
30
+ const source = byId.get(edge.sourceRef);
31
+ const target = byId.get(edge.targetRef);
32
+ if (!source || !target)
33
+ continue;
34
+ if (!routeHits(edge.waypoints, blocking) && !throughOwnEnd(edge.waypoints, source, target)) {
35
+ continue;
36
+ }
37
+ const others = new SegmentIndex(edges.filter((e) => e !== edge));
38
+ // A boundary event overlaps its host's outline, so a port of one can sit
39
+ // inside the other; a route docking there would cross its own endpoint.
40
+ const clearOf = (other) => (port) => !inside(port.point, other.bounds);
41
+ const route = routeOrthogonal({
42
+ from: ports(edge.kind, source, target, true, boundarySide(edge.sourceRef)).filter(clearOf(target)),
43
+ to: ports(edge.kind, target, source, false, boundarySide(edge.targetRef)).filter(clearOf(source)),
44
+ obstacles: blocking.map((s) => s.bounds),
45
+ crossings: (a, b) => others.crossings(a, b),
46
+ });
47
+ if (route)
48
+ edge.waypoints = route;
49
+ }
50
+ }
51
+ function ports(kind, own, other, leaving, boundary) {
52
+ if (boundary) {
53
+ return sidePorts(own.bounds, {
54
+ top: boundary === "top" ? 0 : Number.POSITIVE_INFINITY,
55
+ bottom: boundary === "bottom" ? 0 : Number.POSITIVE_INFINITY,
56
+ left: Number.POSITIVE_INFINITY,
57
+ right: Number.POSITIVE_INFINITY,
58
+ });
59
+ }
60
+ if (own.container) {
61
+ // A pool docks under whatever it talks to rather than at its own centre.
62
+ const ob = other.bounds;
63
+ const x = Math.min(Math.max(ob.x + ob.width / 2, own.bounds.x + 40), own.bounds.x + own.bounds.width - 10);
64
+ return [
65
+ { point: { x, y: own.bounds.y }, side: "top" },
66
+ { point: { x, y: own.bounds.y + own.bounds.height }, side: "bottom" },
67
+ ];
68
+ }
69
+ if (kind === "message")
70
+ return sidePorts(own.bounds, { left: 60, right: 60 });
71
+ if (kind === "association")
72
+ return sidePorts(own.bounds);
73
+ return leaving
74
+ ? sidePorts(own.bounds, { right: 0, top: 20, bottom: 20, left: 80 })
75
+ : sidePorts(own.bounds, { left: 0, top: 20, bottom: 20, right: 80 });
76
+ }
77
+ /**
78
+ * A route may touch its own endpoints only at their outline: one that docks on
79
+ * the far side of its target, say, cuts straight across the shape to get there.
80
+ */
81
+ function throughOwnEnd(waypoints, source, target) {
82
+ for (let i = 0; i + 1 < waypoints.length; i++) {
83
+ const a = waypoints[i];
84
+ const b = waypoints[i + 1];
85
+ if (!a || !b)
86
+ continue;
87
+ for (const end of [source, target]) {
88
+ if (!end.container && segmentHits(a, b, end.bounds, OWN_END_TOLERANCE))
89
+ return true;
90
+ }
91
+ }
92
+ return false;
93
+ }
94
+ function inside(p, b) {
95
+ return p.x > b.x && p.x < b.x + b.width && p.y > b.y && p.y < b.y + b.height;
96
+ }
97
+ /** True when any segment passes through the interior of one of `shapes`. */
98
+ function routeHits(waypoints, shapes) {
99
+ for (let i = 0; i + 1 < waypoints.length; i++) {
100
+ const a = waypoints[i];
101
+ const b = waypoints[i + 1];
102
+ if (!a || !b)
103
+ continue;
104
+ for (const s of shapes)
105
+ if (segmentHits(a, b, s.bounds))
106
+ return true;
107
+ }
108
+ return false;
109
+ }
110
+ /** Liang–Barsky clip against the box shrunk by `tolerance`; handles diagonals too. */
111
+ function segmentHits(a, b, box, tolerance = TOLERANCE) {
112
+ const x1 = box.x + tolerance;
113
+ const y1 = box.y + tolerance;
114
+ const x2 = box.x + box.width - tolerance;
115
+ const y2 = box.y + box.height - tolerance;
116
+ if (x2 <= x1 || y2 <= y1)
117
+ return false;
118
+ const dx = b.x - a.x;
119
+ const dy = b.y - a.y;
120
+ const p = [-dx, dx, -dy, dy];
121
+ const q = [a.x - x1, x2 - a.x, a.y - y1, y2 - a.y];
122
+ let t0 = 0;
123
+ let t1 = 1;
124
+ for (let i = 0; i < 4; i++) {
125
+ const pi = p[i] ?? 0;
126
+ const qi = q[i] ?? 0;
127
+ if (pi === 0) {
128
+ if (qi <= 0)
129
+ return false;
130
+ continue;
131
+ }
132
+ const t = qi / pi;
133
+ if (pi < 0) {
134
+ if (t > t1)
135
+ return false;
136
+ if (t > t0)
137
+ t0 = t;
138
+ }
139
+ else {
140
+ if (t < t0)
141
+ return false;
142
+ if (t < t1)
143
+ t1 = t;
144
+ }
145
+ }
146
+ return t1 - t0 > 1e-9;
147
+ }
148
+ /** Width of one bucket of the segment index. */
149
+ const CELL = 128;
150
+ /**
151
+ * Every segment of a set of routes, flattened and bucketed by x so the search's
152
+ * many crossing queries each touch only the segments nearby.
153
+ */
154
+ class SegmentIndex {
155
+ coords = [];
156
+ cells = new Map();
157
+ constructor(edges) {
158
+ for (const edge of edges) {
159
+ const wps = edge.waypoints;
160
+ for (let i = 0; i + 1 < wps.length; i++) {
161
+ const a = wps[i];
162
+ const b = wps[i + 1];
163
+ if (!a || !b)
164
+ continue;
165
+ const index = this.coords.length;
166
+ this.coords.push(a.x, a.y, b.x, b.y);
167
+ const lo = Math.floor(Math.min(a.x, b.x) / CELL);
168
+ const hi = Math.floor(Math.max(a.x, b.x) / CELL);
169
+ for (let c = lo; c <= hi; c++) {
170
+ const cell = this.cells.get(c);
171
+ if (cell)
172
+ cell.push(index);
173
+ else
174
+ this.cells.set(c, [index]);
175
+ }
176
+ }
177
+ }
178
+ }
179
+ /** How many indexed segments the segment a→b properly crosses. */
180
+ crossings(a, b) {
181
+ const minX = Math.min(a.x, b.x);
182
+ const maxX = Math.max(a.x, b.x);
183
+ const minY = Math.min(a.y, b.y);
184
+ const maxY = Math.max(a.y, b.y);
185
+ const lo = Math.floor(minX / CELL);
186
+ const hi = Math.floor(maxX / CELL);
187
+ const seen = hi > lo ? new Set() : undefined;
188
+ const c = this.coords;
189
+ let count = 0;
190
+ for (let cell = lo; cell <= hi; cell++) {
191
+ for (const i of this.cells.get(cell) ?? []) {
192
+ if (seen) {
193
+ if (seen.has(i))
194
+ continue;
195
+ seen.add(i);
196
+ }
197
+ const x1 = c[i] ?? 0;
198
+ const y1 = c[i + 1] ?? 0;
199
+ const x2 = c[i + 2] ?? 0;
200
+ const y2 = c[i + 3] ?? 0;
201
+ if (Math.max(x1, x2) < minX || Math.min(x1, x2) > maxX)
202
+ continue;
203
+ if (Math.max(y1, y2) < minY || Math.min(y1, y2) > maxY)
204
+ continue;
205
+ if (properlyCross(a, b, { x: x1, y: y1 }, { x: x2, y: y2 }))
206
+ count++;
207
+ }
208
+ }
209
+ return count;
210
+ }
211
+ }
212
+ function properlyCross(p, q, r, t) {
213
+ const side = (o, a, b) => Math.sign((a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x));
214
+ const s1 = side(p, q, r);
215
+ const s2 = side(p, q, t);
216
+ const s3 = side(r, t, p);
217
+ const s4 = side(r, t, q);
218
+ return s1 !== 0 && s2 !== 0 && s3 !== 0 && s4 !== 0 && s1 !== s2 && s3 !== s4;
219
+ }
220
+ //# sourceMappingURL=repair.js.map
@@ -1,3 +1,4 @@
1
+ import { routeOrthogonal, sidePorts } from "../orthogonal.js";
1
2
  import { BOUNDARY_SIZE, H_GAP } from "./place.js";
2
3
  /** Clearance kept between a routed segment and the shapes it passes. */
3
4
  const ROUTING_MARGIN = 20;
@@ -119,7 +120,10 @@ export function routeFlows(graph, flows, bounds, bandLayout, gutterX) {
119
120
  // Going around costs bends and length, so it has to save more than one
120
121
  // crossing to be worth taking.
121
122
  const cost = crossingCount(around, ctx) + DETOUR_PENALTY;
122
- const waypoints = item.direct && item.direct.crossings <= cost ? item.direct.waypoints : around;
123
+ const chosen = item.direct && item.direct.crossings <= cost ? item.direct.waypoints : around;
124
+ const waypoints = blocked(ctx, chosen, item.flow.sourceRef, item.flow.targetRef)
125
+ ? (searchRoute(ctx, item.flow, item.source, item.target) ?? chosen)
126
+ : chosen;
123
127
  commit(ctx, routed, item.flow.id, waypoints);
124
128
  }
125
129
  // Emit in declaration order, whichever pass produced the route.
@@ -140,6 +144,41 @@ export function routeFlows(graph, flows, bounds, bandLayout, gutterX) {
140
144
  }
141
145
  return edges;
142
146
  }
147
+ /**
148
+ * Last resort for an edge every candidate route runs through a shape: a full
149
+ * obstacle-aware search. Leaving right and entering left stay preferred, so the
150
+ * result still reads left to right where the diagram has room for it.
151
+ */
152
+ function searchRoute(ctx, flow, source, target) {
153
+ const fromBoundary = ctx.graph.byId.get(flow.sourceRef)?.type === "boundaryEvent";
154
+ let from;
155
+ if (fromBoundary) {
156
+ // A boundary event never leaves into its host: only the outward side.
157
+ const host = ctx.bounds.get(hostOf(ctx.graph, flow.sourceRef));
158
+ const onTop = host !== undefined && source.y + source.height / 2 <= host.y + 1;
159
+ from = sidePorts(source, {
160
+ top: onTop ? 0 : Number.POSITIVE_INFINITY,
161
+ bottom: onTop ? Number.POSITIVE_INFINITY : 0,
162
+ left: Number.POSITIVE_INFINITY,
163
+ right: Number.POSITIVE_INFINITY,
164
+ });
165
+ }
166
+ else {
167
+ from = sidePorts(source, { right: 0, top: 20, bottom: 20, left: 80 });
168
+ }
169
+ const to = sidePorts(target, { left: 0, top: 20, bottom: 20, right: 80 });
170
+ const obstacles = [];
171
+ for (const [id, b] of ctx.bounds) {
172
+ if (id !== flow.sourceRef && id !== flow.targetRef)
173
+ obstacles.push(b);
174
+ }
175
+ return routeOrthogonal({
176
+ from,
177
+ to,
178
+ obstacles,
179
+ crossings: (a, b) => crossingCount([a, b], ctx),
180
+ });
181
+ }
143
182
  /** Record a chosen route so later edges can steer around it. */
144
183
  function commit(ctx, routed, id, waypoints) {
145
184
  routed.set(id, waypoints);
@@ -382,8 +421,11 @@ isLoop = false, sourceId = "", targetId = "") {
382
421
  }
383
422
  const goesDown = base > middle;
384
423
  const corridorY = reserve(ctx, left, right, base, goesDown ? 1 : -1);
385
- const exitY = goesDown ? source.y + source.height : source.y;
386
- const entryY = goesDown ? target.y + target.height : target.y;
424
+ // Each end faces the corridor on its own: a corridor between the two runs
425
+ // below one shape and above the other, and docking on the far side would
426
+ // cut straight across the shape.
427
+ const exitY = corridorY > from.y ? source.y + source.height : source.y;
428
+ const entryY = corridorY > to.y ? target.y + target.height : target.y;
387
429
  // Leaving through the source's own top or bottom keeps the route out of the
388
430
  // horizontal corridor its neighbours flow along; the gutter variant is the
389
431
  // fallback for when that vertical is occupied.