@bpmnkit/core 1.15.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 (60) hide show
  1. package/builder-reference.md +2 -1
  2. package/dist/bpmn/auto-layout.d.ts +0 -8
  3. package/dist/bpmn/auto-layout.js +605 -98
  4. package/dist/bpmn/bpmn-builder.d.ts +120 -14
  5. package/dist/bpmn/bpmn-builder.js +411 -152
  6. package/dist/bpmn/bpmn-model.d.ts +15 -0
  7. package/dist/bpmn/bpmn-parser.js +32 -6
  8. package/dist/bpmn/bpmn-serializer.js +9 -2
  9. package/dist/bpmn/compact-stream.d.ts +7 -0
  10. package/dist/bpmn/compact-stream.js +7 -0
  11. package/dist/bpmn/compact.d.ts +124 -9
  12. package/dist/bpmn/compact.js +295 -27
  13. package/dist/bpmn/full-operations.d.ts +19 -0
  14. package/dist/bpmn/full-operations.js +191 -7
  15. package/dist/bpmn/make-room.js +114 -0
  16. package/dist/bpmn/place-new-shapes.js +4 -2
  17. package/dist/bpmn/preserving-writer.d.ts +1 -1
  18. package/dist/bpmn/preserving-writer.js +1 -1
  19. package/dist/bpmn/process-delta.d.ts +32 -1
  20. package/dist/bpmn/process-delta.js +62 -1
  21. package/dist/bpmn/process-text-writer.d.ts +18 -7
  22. package/dist/bpmn/process-text-writer.js +105 -22
  23. package/dist/bpmn/process-text.d.ts +27 -1
  24. package/dist/bpmn/process-text.js +350 -2
  25. package/dist/bpmn/swimlanes.d.ts +178 -0
  26. package/dist/bpmn/swimlanes.js +774 -0
  27. package/dist/index.d.ts +5 -4
  28. package/dist/index.js +3 -2
  29. package/dist/layout/collaboration/crossings.d.ts +13 -0
  30. package/dist/layout/collaboration/crossings.js +37 -0
  31. package/dist/layout/collaboration/docked-labels.d.ts +21 -0
  32. package/dist/layout/collaboration/docked-labels.js +65 -0
  33. package/dist/layout/collaboration/message-tracks.d.ts +20 -0
  34. package/dist/layout/collaboration/message-tracks.js +78 -0
  35. package/dist/layout/collaboration/stack-crossings.d.ts +38 -0
  36. package/dist/layout/collaboration/stack-crossings.js +155 -0
  37. package/dist/layout/grid/edge-labels.js +21 -10
  38. package/dist/layout/layered/coords.d.ts +13 -0
  39. package/dist/layout/layered/coords.js +211 -0
  40. package/dist/layout/layered/index.d.ts +21 -0
  41. package/dist/layout/layered/index.js +288 -0
  42. package/dist/layout/layered/layers.d.ts +60 -0
  43. package/dist/layout/layered/layers.js +374 -0
  44. package/dist/layout/layered/route.d.ts +66 -0
  45. package/dist/layout/layered/route.js +369 -0
  46. package/dist/layout/layout-engine.d.ts +6 -2
  47. package/dist/layout/layout-engine.js +4 -2
  48. package/dist/layout/semantic/bands.js +8 -2
  49. package/dist/layout/semantic/graph.d.ts +33 -1
  50. package/dist/layout/semantic/graph.js +200 -4
  51. package/dist/layout/semantic/index.d.ts +8 -2
  52. package/dist/layout/semantic/index.js +114 -14
  53. package/dist/layout/semantic/lanes.d.ts +40 -0
  54. package/dist/layout/semantic/lanes.js +198 -0
  55. package/dist/layout/semantic/place.d.ts +46 -2
  56. package/dist/layout/semantic/place.js +248 -30
  57. package/dist/layout/semantic/route.js +35 -2
  58. package/dist/layout/types.d.ts +0 -8
  59. package/dist/xml/xml-patch.d.ts +1 -1
  60. package/package.json +2 -2
package/dist/index.d.ts CHANGED
@@ -50,7 +50,8 @@ export { checkDiCompleteness } from "./bpmn/di-check.js";
50
50
  export type { DiCompleteness } from "./bpmn/di-check.js";
51
51
  export { planeForElement, listPlaneElementIds } from "./bpmn/di-planes.js";
52
52
  export { DiagramBuilder, ProcessBuilder } from "./bpmn/bpmn-builder.js";
53
- export type { BranchBuilder, BuildOptions, BuildGateOptions, SendTaskOptions, MessageFlowOptions, ParticipantOptions, DiagramMessageOptions, SubProcessContentBuilder, ServiceTaskOptions, ScriptTaskOptions, UserTaskOptions, CallActivityOptions, BusinessRuleTaskOptions, ElementOptions, GatewayOptions, MultiInstanceOptions, SubProcessOptions, StartEventOptions, IntermediateCatchEventOptions, IntermediateThrowEventOptions, EndEventOptions, BoundaryEventOptions, ConditionVariableEvent, AdHocSubProcessOptions, AnnotateOptions, } from "./bpmn/bpmn-builder.js";
53
+ export { allLanes, assignLane, collaborationProblems, laneForNewNode, laneOf, laneProblems, leafLanes, messageEndProblem, pinPrimaryProcess, primaryProcess, } from "./bpmn/swimlanes.js";
54
+ export type { BranchBuilder, BuildOptions, BuildGateOptions, SendTaskOptions, DeclaredLaneOptions, LaneOptions, MessageFlowOptions, ParticipantOptions, DiagramMessageOptions, SubProcessContentBuilder, ServiceTaskOptions, ScriptTaskOptions, UserTaskOptions, CallActivityOptions, BusinessRuleTaskOptions, ElementOptions, GatewayOptions, MultiInstanceOptions, SubProcessOptions, StartEventOptions, IntermediateCatchEventOptions, IntermediateThrowEventOptions, EndEventOptions, BoundaryEventOptions, ConditionVariableEvent, AdHocSubProcessOptions, AnnotateOptions, } from "./bpmn/bpmn-builder.js";
54
55
  export type { BpmnDefinitions, BpmnProcess, BpmnFlowNode, BpmnFlowElement, BpmnSequenceFlow, BpmnBoundaryEvent, BpmnElementType, BpmnStartEvent, BpmnEndEvent, BpmnIntermediateCatchEvent, BpmnIntermediateThrowEvent, BpmnTask, BpmnServiceTask, BpmnScriptTask, BpmnUserTask, BpmnSendTask, BpmnReceiveTask, BpmnBusinessRuleTask, BpmnManualTask, BpmnCallActivity, BpmnSubProcess, BpmnAdHocSubProcess, BpmnEventSubProcess, BpmnTransaction, BpmnExclusiveGateway, BpmnParallelGateway, BpmnInclusiveGateway, BpmnEventBasedGateway, BpmnComplexGateway, BpmnCollaboration, BpmnParticipant, BpmnMessageFlow, BpmnLane, BpmnLaneSet, BpmnError, BpmnEscalation, BpmnMessage, BpmnSignal, BpmnTextAnnotation, BpmnAssociation, BpmnGroup, BpmnDataObject, BpmnDataObjectReference, BpmnDataStoreReference, BpmnConditionExpression, BpmnEventDefinition, BpmnTimerEventDefinition, BpmnErrorEventDefinition, BpmnEscalationEventDefinition, BpmnMessageEventDefinition, BpmnSignalEventDefinition, BpmnConditionalEventDefinition, BpmnLinkEventDefinition, BpmnCancelEventDefinition, BpmnTerminateEventDefinition, BpmnCompensateEventDefinition, BpmnMultiInstanceLoopCharacteristics, BpmnDiagram, BpmnDiPlane, BpmnDiShape, BpmnDiEdge, BpmnDiLabel, BpmnBounds, BpmnWaypoint, } from "./bpmn/bpmn-model.js";
55
56
  export type { RestConnectorConfig, RestAuthentication, HttpMethod, } from "./bpmn/rest-connector.js";
56
57
  export type { ZeebeExtensions, ZeebeTaskDefinition, ZeebeIoMapping, ZeebeIoMappingEntry, ZeebeTaskHeaders, ZeebeTaskHeaderEntry, ZeebeProperties, ZeebePropertyEntry, ZeebeFormDefinition, ZeebeCalledDecision, ZeebeLinkedResource, ZeebeCalledElement, ZeebeAssignmentDefinition, ZeebeTaskSchedule, ZeebePriorityDefinition, ZeebeSubscription, ZeebeAgentDefinition, } from "./bpmn/zeebe-extensions.js";
@@ -98,13 +99,13 @@ export { ELEMENT_SIZES, GRID_CELL_HEIGHT, GRID_CELL_WIDTH } from "./layout/index
98
99
  export { compactify, expand } from "./bpmn/compact.js";
99
100
  export { createCompactStream } from "./bpmn/compact-stream.js";
100
101
  export type { CompactStream, CompactStreamOptions } from "./bpmn/compact-stream.js";
101
- export { PROCESS_TEXT_GUIDE, conditionOrLabel, createProcessTextStream, parseProcessText, } from "./bpmn/process-text.js";
102
+ export { MESSAGE_LINE, POOL_LINE, PROCESS_TEXT_GUIDE, conditionOrLabel, createProcessTextStream, nameFromId, parseProcessText, } from "./bpmn/process-text.js";
102
103
  export type { ProcessTextProblem, ProcessTextQuestion, ProcessTextResult, ProcessTextStream, ConnectorRef, } from "./bpmn/process-text.js";
103
104
  export { parseConnectorLine } from "./bpmn/connector-line.js";
104
105
  export type { ConnectorLine } from "./bpmn/connector-line.js";
105
106
  export { PROCESS_DELTA_GUIDE, parseProcessDelta } from "./bpmn/process-delta.js";
106
107
  export type { DeltaFlow, DeltaNode, ProcessDelta } from "./bpmn/process-delta.js";
107
- export { writableCondition, writableLabel, writableName, writeProcessText, } from "./bpmn/process-text-writer.js";
108
+ export { writableCondition, writableLabel, writableMessageLabel, writableName, writeProcessText, } from "./bpmn/process-text-writer.js";
108
109
  export type { WriteProcessTextOptions, WrittenProcessText } from "./bpmn/process-text-writer.js";
109
110
  export { ELEMENT_GROUP_ORDER, ELEMENT_TYPE_GROUPS, allElementTypes, elementTypesInGroup, } from "./bpmn/element-catalog.js";
110
111
  export type { ElementTypeGroup } from "./bpmn/element-catalog.js";
@@ -127,7 +128,7 @@ export { exportDrdSvg, exportDecisionTableSvg } from "./dmn/svg.js";
127
128
  export type { DecisionTableSvgOptions, DmnSvgOptions } from "./dmn/svg.js";
128
129
  export { buildProcessDocumentation, documentationToDocx, documentationToHtml, documentationToMarkdown, renderDocumentationDocx, renderDocumentationHtml, renderDocumentationMarkdown, } from "./doc/index.js";
129
130
  export type { DocumentationLink, DocumentationOptions, DocumentedDecision, DocumentedElement, DocumentedForm, DocumentedFormField, DocumentedLane, DocumentedMessageFlow, DocumentedProcess, DocumentedProperty, DocxDocumentationOptions, ProcessDocumentation, } from "./doc/index.js";
130
- export type { CompactDiagram, CompactElement, CompactFlow, CompactNote, CompactProcess, ExpandOptions, } from "./bpmn/compact.js";
131
+ export type { CompactDiagram, CompactElement, CompactFlow, CompactLane, CompactMessageFlow, CompactNote, CompactParticipant, CompactProcess, ExpandOptions, } from "./bpmn/compact.js";
131
132
  export { compilePlan, extractPlan, mergePlan, slugify, uniqueId, } from "./plan/index.js";
132
133
  export type { CompilePlanOptions, ConnectorApplyResult, ConnectorResolver, ExtractResult, PlanAgentTool, PlanAiAgentStep, PlanBranch, PlanBusinessRuleTaskStep, PlanCallActivityStep, PlanConnectorRef, PlanConnectorStep, PlanEndStep, PlanErrorBoundary, PlanGatewayStep, PlanInputVariable, PlanProblem, PlanRawStep, PlanReceiveTaskStep, PlanScenario, PlanScriptTaskStep, PlanSendTaskStep, PlanServiceTaskStep, PlanStartStep, PlanStep, PlanSubProcessStep, PlanTimerBoundary, PlanUserTaskStep, PlanWaitStep, ProcessPlan, SynthResult, UnsupportedElement, } from "./plan/index.js";
133
134
  export { listSecrets } from "./bpmn/secrets.js";
package/dist/index.js CHANGED
@@ -31,6 +31,7 @@ export { compositeKey, stableToken } from "./types/stable-key.js";
31
31
  export { checkDiCompleteness } from "./bpmn/di-check.js";
32
32
  export { planeForElement, listPlaneElementIds } from "./bpmn/di-planes.js";
33
33
  export { DiagramBuilder, ProcessBuilder } from "./bpmn/bpmn-builder.js";
34
+ export { allLanes, assignLane, collaborationProblems, laneForNewNode, laneOf, laneProblems, leafLanes, messageEndProblem, pinPrimaryProcess, primaryProcess, } from "./bpmn/swimlanes.js";
34
35
  export { assertZeebePlacement, bpmnElementName, ensureZeebeExtension, isZeebePlacementAllowed, ZeebePlacementError, zeebeExtensionsToXmlElements, } from "./bpmn/zeebe-extensions.js";
35
36
  export { ZEEBE_PLACEMENT } from "./bpmn/zeebe-placement.js";
36
37
  export { buildAiAgentSubProcess, AI_AGENT_JOB_WORKER_TASK_TYPE, AI_AGENT_DEFAULT_OUTPUT_ELEMENT, } from "./bpmn/agentic.js";
@@ -54,10 +55,10 @@ export { benchmarkLayout, compareLayouts, formatBenchmarkResult, generateAutoLay
54
55
  export { ELEMENT_SIZES, GRID_CELL_HEIGHT, GRID_CELL_WIDTH } from "./layout/index.js";
55
56
  export { compactify, expand } from "./bpmn/compact.js";
56
57
  export { createCompactStream } from "./bpmn/compact-stream.js";
57
- export { PROCESS_TEXT_GUIDE, conditionOrLabel, createProcessTextStream, parseProcessText, } from "./bpmn/process-text.js";
58
+ export { MESSAGE_LINE, POOL_LINE, PROCESS_TEXT_GUIDE, conditionOrLabel, createProcessTextStream, nameFromId, parseProcessText, } from "./bpmn/process-text.js";
58
59
  export { parseConnectorLine } from "./bpmn/connector-line.js";
59
60
  export { PROCESS_DELTA_GUIDE, parseProcessDelta } from "./bpmn/process-delta.js";
60
- export { writableCondition, writableLabel, writableName, writeProcessText, } from "./bpmn/process-text-writer.js";
61
+ export { writableCondition, writableLabel, writableMessageLabel, writableName, writeProcessText, } from "./bpmn/process-text-writer.js";
61
62
  export { ELEMENT_GROUP_ORDER, ELEMENT_TYPE_GROUPS, allElementTypes, elementTypesInGroup, } from "./bpmn/element-catalog.js";
62
63
  export { createFlowElement, retypeElement } from "./bpmn/element-shape.js";
63
64
  export { applyOperations } from "./bpmn/operations.js";
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Crossings between orthogonal polylines, counted the way `pnpm layout:score`
3
+ * counts them: a horizontal and a vertical segment meeting strictly inside
4
+ * both, with 1 px of slack, so a shared dock or a touching corner is no
5
+ * crossing. Collinear overlaps are not crossings either.
6
+ */
7
+ export interface CrossingPoint {
8
+ x: number;
9
+ y: number;
10
+ }
11
+ /** How many times polyline `a` crosses polyline `b`. */
12
+ export declare function pathCrossings(a: readonly CrossingPoint[], b: readonly CrossingPoint[]): number;
13
+ //# sourceMappingURL=crossings.d.ts.map
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Crossings between orthogonal polylines, counted the way `pnpm layout:score`
3
+ * counts them: a horizontal and a vertical segment meeting strictly inside
4
+ * both, with 1 px of slack, so a shared dock or a touching corner is no
5
+ * crossing. Collinear overlaps are not crossings either.
6
+ */
7
+ function segmentsCross(a, b, c, d) {
8
+ const firstFlat = Math.abs(a.y - b.y) < 0.5;
9
+ const secondFlat = Math.abs(c.y - d.y) < 0.5;
10
+ if (firstFlat === secondFlat)
11
+ return false;
12
+ const [h1, h2, v1, v2] = firstFlat ? [a, b, c, d] : [c, d, a, b];
13
+ const x = v1.x;
14
+ const y = h1.y;
15
+ return (x > Math.min(h1.x, h2.x) + 1 &&
16
+ x < Math.max(h1.x, h2.x) - 1 &&
17
+ y > Math.min(v1.y, v2.y) + 1 &&
18
+ y < Math.max(v1.y, v2.y) - 1);
19
+ }
20
+ /** How many times polyline `a` crosses polyline `b`. */
21
+ export function pathCrossings(a, b) {
22
+ let n = 0;
23
+ for (let i = 1; i < a.length; i++) {
24
+ const p = a[i - 1];
25
+ const q = a[i];
26
+ if (!p || !q)
27
+ continue;
28
+ for (let j = 1; j < b.length; j++) {
29
+ const r = b[j - 1];
30
+ const s = b[j];
31
+ if (r && s && segmentsCross(p, q, r, s))
32
+ n++;
33
+ }
34
+ }
35
+ return n;
36
+ }
37
+ //# sourceMappingURL=crossings.js.map
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Labels that sit where a message flow docks.
3
+ *
4
+ * The process layout puts an event's or gateway's name below it by default,
5
+ * before any message flow exists. A message that then docks on that same side
6
+ * runs straight through the name. Mirroring the label to the far side of its
7
+ * element keeps both readable, as long as nothing already sits there.
8
+ */
9
+ import type { BpmnDiEdge, BpmnDiShape } from "../../bpmn/bpmn-model.js";
10
+ /** One end of a message flow: the element it docks on and where. */
11
+ export interface MessageDock {
12
+ element: string;
13
+ y: number;
14
+ }
15
+ /**
16
+ * Move each docked element's label to the side no message docks on. An
17
+ * element docked on both sides, or whose far side is taken by a shape, a label
18
+ * or an edge, keeps its label where it is.
19
+ */
20
+ export declare function flipDockedLabels(shapes: BpmnDiShape[], edges: readonly BpmnDiEdge[], docks: readonly MessageDock[], containers: ReadonlySet<string>): void;
21
+ //# sourceMappingURL=docked-labels.d.ts.map
@@ -0,0 +1,65 @@
1
+ const overlaps = (a, b) => a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
2
+ function edgeHits(edge, r) {
3
+ const points = edge.waypoints;
4
+ for (let i = 1; i < points.length; i++) {
5
+ const p = points[i - 1];
6
+ const q = points[i];
7
+ if (!p || !q)
8
+ continue;
9
+ if (Math.max(p.x, q.x) > r.x &&
10
+ Math.min(p.x, q.x) < r.x + r.width &&
11
+ Math.max(p.y, q.y) > r.y &&
12
+ Math.min(p.y, q.y) < r.y + r.height)
13
+ return true;
14
+ }
15
+ return false;
16
+ }
17
+ /**
18
+ * Move each docked element's label to the side no message docks on. An
19
+ * element docked on both sides, or whose far side is taken by a shape, a label
20
+ * or an edge, keeps its label where it is.
21
+ */
22
+ export function flipDockedLabels(shapes, edges, docks, containers) {
23
+ const byElement = new Map(shapes.map((s) => [s.bpmnElement, s]));
24
+ const sides = new Map();
25
+ for (const dock of docks) {
26
+ const shape = byElement.get(dock.element);
27
+ if (!shape)
28
+ continue;
29
+ const b = shape.bounds;
30
+ const side = Math.abs(dock.y - b.y) <= 1
31
+ ? "top"
32
+ : Math.abs(dock.y - (b.y + b.height)) <= 1
33
+ ? "bottom"
34
+ : undefined;
35
+ if (!side)
36
+ continue;
37
+ const set = sides.get(dock.element) ?? new Set();
38
+ set.add(side);
39
+ sides.set(dock.element, set);
40
+ }
41
+ for (const [element, set] of sides) {
42
+ if (set.size !== 1)
43
+ continue;
44
+ const shape = byElement.get(element);
45
+ const label = shape?.label?.bounds;
46
+ if (!shape || !label)
47
+ continue;
48
+ const b = shape.bounds;
49
+ const below = label.y >= b.y + b.height - 1;
50
+ const above = label.y + label.height <= b.y + 1;
51
+ const docked = set.has("bottom") ? "bottom" : "top";
52
+ if (!((docked === "bottom" && below) || (docked === "top" && above)))
53
+ continue;
54
+ const y = below
55
+ ? b.y - (label.y - (b.y + b.height)) - label.height
56
+ : b.y + b.height + (b.y - (label.y + label.height));
57
+ const spot = { x: label.x, y, width: label.width, height: label.height };
58
+ const taken = shapes.some((s) => s !== shape &&
59
+ !containers.has(s.bpmnElement) &&
60
+ (overlaps(s.bounds, spot) || (s.label?.bounds ? overlaps(s.label.bounds, spot) : false))) || edges.some((e) => edgeHits(e, spot));
61
+ if (!taken)
62
+ label.y = y;
63
+ }
64
+ }
65
+ //# sourceMappingURL=docked-labels.js.map
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Horizontal tracks for the message flows that cross one gap between pools.
3
+ *
4
+ * A slanted message runs down from the pool above, along the gap, and on down
5
+ * into the pool below. Two such runs cross when one's riser lands inside the
6
+ * other's horizontal stretch on the wrong side of it, so the order of the
7
+ * tracks decides the crossings, not just their spacing. Runs whose stretches
8
+ * do not overlap share a track, which leaves more room between the others.
9
+ */
10
+ /** One message flow in a gap: x where it comes in from above and leaves below. */
11
+ export interface GapRun {
12
+ upper: number;
13
+ lower: number;
14
+ }
15
+ /**
16
+ * The y of each run's horizontal stretch inside the gap from `top` to
17
+ * `bottom`, or undefined for a vertical run, which needs no track.
18
+ */
19
+ export declare function assignTracks(runs: readonly GapRun[], top: number, bottom: number): Array<number | undefined>;
20
+ //# sourceMappingURL=message-tracks.d.ts.map
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Horizontal tracks for the message flows that cross one gap between pools.
3
+ *
4
+ * A slanted message runs down from the pool above, along the gap, and on down
5
+ * into the pool below. Two such runs cross when one's riser lands inside the
6
+ * other's horizontal stretch on the wrong side of it, so the order of the
7
+ * tracks decides the crossings, not just their spacing. Runs whose stretches
8
+ * do not overlap share a track, which leaves more room between the others.
9
+ */
10
+ /** Stretches closer than this on one track would read as one line. */
11
+ const TRACK_CLEARANCE = 10;
12
+ /**
13
+ * The y of each run's horizontal stretch inside the gap from `top` to
14
+ * `bottom`, or undefined for a vertical run, which needs no track.
15
+ */
16
+ export function assignTracks(runs, top, bottom) {
17
+ const lo = runs.map((r) => Math.min(r.upper, r.lower));
18
+ const hi = runs.map((r) => Math.max(r.upper, r.lower));
19
+ const slanted = runs.flatMap((_, i) => ((hi[i] ?? 0) - (lo[i] ?? 0) >= 1 ? [i] : []));
20
+ const inside = (x, j) => x > (lo[j] ?? 0) + 1 && x < (hi[j] ?? 0) - 1;
21
+ // below[i] holds every run that must sit below run i: i's riser from above
22
+ // lands inside it, or its riser from below lands inside i.
23
+ const below = new Map(slanted.map((i) => [i, []]));
24
+ const blockers = new Map(slanted.map((i) => [i, 0]));
25
+ for (const i of slanted) {
26
+ for (const j of slanted) {
27
+ if (i === j)
28
+ continue;
29
+ const run = runs[i];
30
+ if (!run)
31
+ continue;
32
+ if (inside(run.upper, j) || inside(runs[j]?.lower ?? 0, i)) {
33
+ below.get(i)?.push(j);
34
+ blockers.set(j, (blockers.get(j) ?? 0) + 1);
35
+ }
36
+ }
37
+ }
38
+ // Topological order, leftmost first among the free; a cycle cannot be
39
+ // satisfied, so the leftmost remaining run breaks it.
40
+ const key = (a, b) => (lo[a] ?? 0) - (lo[b] ?? 0) || (hi[a] ?? 0) - (hi[b] ?? 0) || a - b;
41
+ const order = [];
42
+ const left = new Set(slanted);
43
+ while (left.size > 0) {
44
+ const free = [...left].filter((i) => (blockers.get(i) ?? 0) === 0).sort(key);
45
+ const next = free[0] ?? [...left].sort(key)[0];
46
+ if (next === undefined)
47
+ break;
48
+ left.delete(next);
49
+ order.push(next);
50
+ for (const j of below.get(next) ?? [])
51
+ blockers.set(j, (blockers.get(j) ?? 0) - 1);
52
+ }
53
+ const track = [];
54
+ const tracks = [];
55
+ for (const i of order) {
56
+ let t = 0;
57
+ for (const p of order) {
58
+ if (p === i)
59
+ break;
60
+ if ((below.get(p) ?? []).includes(i))
61
+ t = Math.max(t, (track[p] ?? 0) + 1);
62
+ }
63
+ const a = (lo[i] ?? 0) - TRACK_CLEARANCE;
64
+ const b = (hi[i] ?? 0) + TRACK_CLEARANCE;
65
+ while ((tracks[t] ?? []).some(([x1, x2]) => x1 < b && a < x2))
66
+ t++;
67
+ track[i] = t;
68
+ while (tracks.length <= t)
69
+ tracks.push([]);
70
+ tracks[t]?.push([lo[i] ?? 0, hi[i] ?? 0]);
71
+ }
72
+ const count = tracks.length;
73
+ return runs.map((_, i) => {
74
+ const t = track[i];
75
+ return t === undefined ? undefined : Math.round(top + ((t + 1) * (bottom - top)) / (count + 1));
76
+ });
77
+ }
78
+ //# sourceMappingURL=message-tracks.js.map
@@ -0,0 +1,38 @@
1
+ export interface StackPoint {
2
+ x: number;
3
+ y: number;
4
+ }
5
+ export interface StackPool {
6
+ height: number;
7
+ /** Sequence-flow polylines, y measured from the pool's top. */
8
+ paths: StackPoint[][];
9
+ /** Shapes, same frame: a riser through one has to jog round it. */
10
+ boxes?: Array<{
11
+ x: number;
12
+ y: number;
13
+ width: number;
14
+ height: number;
15
+ }>;
16
+ }
17
+ /** One message; an end with no element (a black box) docks under the other end. */
18
+ export interface StackMessage {
19
+ from: number;
20
+ to: number;
21
+ fromEnd?: {
22
+ x: number;
23
+ top: number;
24
+ bottom: number;
25
+ };
26
+ toEnd?: {
27
+ x: number;
28
+ top: number;
29
+ bottom: number;
30
+ };
31
+ }
32
+ export declare function stackCrossings(order: readonly number[], pools: readonly StackPool[], messages: readonly StackMessage[]): number;
33
+ /**
34
+ * Remove-and-reinsert from `start` on the crossing sketch; the first strictly
35
+ * better move wins, so `start` survives every tie.
36
+ */
37
+ export declare function orderByCrossings(start: readonly number[], pools: readonly StackPool[], messages: readonly StackMessage[]): number[];
38
+ //# sourceMappingURL=stack-crossings.d.ts.map
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Pool order by message-flow crossings.
3
+ *
4
+ * Distance alone (ordering.ts) keeps talking pools close, but on a
5
+ * hub-and-spoke collaboration many orders tie on distance and differ a lot in
6
+ * how often the messages cross each other and the processes they pass. This
7
+ * scores an order on a sketch of the final drawing: every pool stacked at its
8
+ * real height and horizontal offset (alignment does not depend on the order),
9
+ * each message drawn as riser, run in the gap nearest its midpoint (tracks as
10
+ * the router assigns them), riser. Risers through shapes count too.
11
+ */
12
+ import { pathCrossings } from "./crossings.js";
13
+ import { assignTracks } from "./message-tracks.js";
14
+ const GAP = 30;
15
+ /**
16
+ * A riser through a shape costs what a crossing does: the router jogs round
17
+ * it (two bends), and an order that sends risers through processes trades
18
+ * crossings for slanted messages.
19
+ */
20
+ const JOG_COST = 1;
21
+ export function stackCrossings(order, pools, messages) {
22
+ const top = [];
23
+ const gaps = [];
24
+ let y = 0;
25
+ for (const index of order) {
26
+ if (y > 0)
27
+ gaps.push({ top: y - GAP, bottom: y });
28
+ top[index] = y;
29
+ y += (pools[index]?.height ?? 0) + GAP;
30
+ }
31
+ const runs = [];
32
+ for (const m of messages) {
33
+ const fromTop = top[m.from];
34
+ const toTop = top[m.to];
35
+ if (fromTop === undefined || toTop === undefined || m.from === m.to)
36
+ continue;
37
+ const fx = m.fromEnd?.x ?? m.toEnd?.x;
38
+ const tx = m.toEnd?.x ?? m.fromEnd?.x;
39
+ if (fx === undefined || tx === undefined)
40
+ continue;
41
+ const down = fromTop < toTop;
42
+ const end = (t, e, pool, leaving) => {
43
+ const lowSide = down === leaving;
44
+ if (!e)
45
+ return lowSide ? t + (pools[pool]?.height ?? 0) : t;
46
+ return t + (lowSide ? e.bottom : e.top);
47
+ };
48
+ const sy = end(fromTop, m.fromEnd, m.from, true);
49
+ const ty = end(toTop, m.toEnd, m.to, false);
50
+ const mid = (sy + ty) / 2;
51
+ let gap = -1;
52
+ for (let g = 0; g < gaps.length; g++) {
53
+ const c = ((gaps[g]?.top ?? 0) + (gaps[g]?.bottom ?? 0)) / 2;
54
+ if (c < Math.min(sy, ty) || c > Math.max(sy, ty))
55
+ continue;
56
+ const best = gaps[gap];
57
+ if (gap < 0 || (best && Math.abs(c - mid) < Math.abs((best.top + best.bottom) / 2 - mid)))
58
+ gap = g;
59
+ }
60
+ runs.push({ a: { x: fx, y: sy }, b: { x: tx, y: ty }, ax: fx, bx: tx, gap });
61
+ }
62
+ const paths = runs.map((r) => [r.a, r.b]);
63
+ const byGap = new Map();
64
+ runs.forEach((r, i) => {
65
+ if (r.gap < 0)
66
+ return;
67
+ const list = byGap.get(r.gap);
68
+ if (list)
69
+ list.push(i);
70
+ else
71
+ byGap.set(r.gap, [i]);
72
+ });
73
+ for (const [g, list] of byGap) {
74
+ const gap = gaps[g];
75
+ if (!gap)
76
+ continue;
77
+ const ys = assignTracks(list.map((i) => {
78
+ const r = runs[i];
79
+ const fromAbove = r.a.y < gap.top;
80
+ return { upper: fromAbove ? r.ax : r.bx, lower: fromAbove ? r.bx : r.ax };
81
+ }), gap.top, gap.bottom);
82
+ list.forEach((i, k) => {
83
+ const r = runs[i];
84
+ const ty = ys[k];
85
+ paths[i] =
86
+ ty === undefined
87
+ ? [r.a, { x: r.ax, y: r.b.y }]
88
+ : [r.a, { x: r.ax, y: ty }, { x: r.bx, y: ty }, r.b];
89
+ });
90
+ }
91
+ let n = 0;
92
+ const drawn = [];
93
+ const boxes = [];
94
+ for (const index of order) {
95
+ const t = top[index] ?? 0;
96
+ for (const p of pools[index]?.paths ?? [])
97
+ drawn.push(p.map((q) => ({ x: q.x, y: q.y + t })));
98
+ for (const b of pools[index]?.boxes ?? [])
99
+ boxes.push({ ...b, y: b.y + t });
100
+ }
101
+ for (const p of paths) {
102
+ for (let i = 1; i < p.length; i++) {
103
+ const a = p[i - 1];
104
+ const b = p[i];
105
+ if (a.x !== b.x)
106
+ continue;
107
+ const lo = Math.min(a.y, b.y) + 2;
108
+ const hi = Math.max(a.y, b.y) - 2;
109
+ for (const r of boxes)
110
+ if (a.x > r.x + 1 && a.x < r.x + r.width - 1 && r.y < hi && lo < r.y + r.height)
111
+ n += JOG_COST;
112
+ }
113
+ }
114
+ for (let i = 0; i < paths.length; i++) {
115
+ const p = paths[i] ?? [];
116
+ for (let j = i + 1; j < paths.length; j++)
117
+ n += pathCrossings(p, paths[j] ?? []);
118
+ for (const d of drawn)
119
+ n += pathCrossings(p, d);
120
+ }
121
+ return n;
122
+ }
123
+ /**
124
+ * Remove-and-reinsert from `start` on the crossing sketch; the first strictly
125
+ * better move wins, so `start` survives every tie.
126
+ */
127
+ export function orderByCrossings(start, pools, messages) {
128
+ let order = [...start];
129
+ let current = stackCrossings(order, pools, messages);
130
+ for (let sweep = 0; sweep < order.length; sweep++) {
131
+ let improved = false;
132
+ for (let from = 0; from < order.length; from++) {
133
+ const pool = order[from];
134
+ if (pool === undefined)
135
+ continue;
136
+ const without = [...order.slice(0, from), ...order.slice(from + 1)];
137
+ for (let to = 0; to <= without.length; to++) {
138
+ if (to === from)
139
+ continue;
140
+ const candidate = [...without.slice(0, to), pool, ...without.slice(to)];
141
+ const n = stackCrossings(candidate, pools, messages);
142
+ if (n < current) {
143
+ order = candidate;
144
+ current = n;
145
+ improved = true;
146
+ break;
147
+ }
148
+ }
149
+ }
150
+ if (!improved)
151
+ break;
152
+ }
153
+ return order;
154
+ }
155
+ //# sourceMappingURL=stack-crossings.js.map
@@ -57,19 +57,25 @@ nearSource = new Set()) {
57
57
  const { segStart, segEnd } = findLongestSegment(edge.waypoints);
58
58
  // Generate candidate positions along the segment
59
59
  const candidates = generateLabelCandidates(segStart, segEnd, labelWidth, labelHeight);
60
- // Pick the first non-overlapping candidate
60
+ // A spot no other flow crosses wins; only when none exists is a crossed one accepted.
61
+ const crossed = (c) => segments.some((s) => s.edge !== edge && segmentThrough(s.a, s.b, c));
62
+ const clear = (c) => !occupied.some((ob) => boundsOverlap(c, ob));
61
63
  let placed = false;
62
- for (const candidate of candidates) {
63
- if (!occupied.some((ob) => boundsOverlap(candidate, ob))) {
64
- edge.labelBounds = candidate;
65
- occupied.push(candidate);
66
- placed = true;
67
- break;
64
+ for (const strict of [true, false]) {
65
+ for (const candidate of candidates) {
66
+ if (clear(candidate) && !(strict && crossed(candidate))) {
67
+ edge.labelBounds = candidate;
68
+ occupied.push(candidate);
69
+ placed = true;
70
+ break;
71
+ }
68
72
  }
73
+ if (placed)
74
+ break;
69
75
  }
70
76
  // Fallback: slide along segment to find clear space
71
77
  if (!placed) {
72
- const fallback = slideLabelAlongSegment(segStart, segEnd, labelWidth, labelHeight, occupied);
78
+ const fallback = slideLabelAlongSegment(segStart, segEnd, labelWidth, labelHeight, occupied, crossed);
73
79
  if (fallback) {
74
80
  edge.labelBounds = fallback;
75
81
  occupied.push(fallback);
@@ -158,8 +164,9 @@ function generateLabelCandidates(segStart, segEnd, labelWidth, labelHeight) {
158
164
  }
159
165
  return candidates;
160
166
  }
161
- function slideLabelAlongSegment(segStart, segEnd, labelWidth, labelHeight, occupied) {
167
+ function slideLabelAlongSegment(segStart, segEnd, labelWidth, labelHeight, occupied, crossed) {
162
168
  const isHorizontal = Math.abs(segEnd.y - segStart.y) < 1;
169
+ let crossedSpot;
163
170
  for (let step = 0; step <= LABEL_SLIDE_STEPS; step++) {
164
171
  const t = step / LABEL_SLIDE_STEPS;
165
172
  const px = segStart.x + (segEnd.x - segStart.x) * t;
@@ -178,9 +185,13 @@ function slideLabelAlongSegment(segStart, segEnd, labelWidth, labelHeight, occup
178
185
  height: labelHeight,
179
186
  };
180
187
  if (!occupied.some((ob) => boundsOverlap(candidate, ob))) {
181
- return candidate;
188
+ if (!crossed(candidate))
189
+ return candidate;
190
+ crossedSpot ??= candidate;
182
191
  }
183
192
  }
193
+ if (crossedSpot)
194
+ return crossedSpot;
184
195
  // No clear position found — skip label placement rather than forcing an overlap
185
196
  return undefined;
186
197
  }
@@ -0,0 +1,13 @@
1
+ import type { Layering } from "./layers.js";
2
+ /** How far a node reaches above and below its centre line, labels and docked events included. */
3
+ export interface Extent {
4
+ above: number;
5
+ below: number;
6
+ }
7
+ /**
8
+ * Brandes–Köpf centre lines (the cross-column axis here is y): the balanced
9
+ * mean, then the four aligned variants, each kept only while it holds every
10
+ * column's spacing. The caller picks one by how the flows would route.
11
+ */
12
+ export declare function yCandidates(lg: Layering, extent: (id: string) => Extent): Map<string, number>[];
13
+ //# sourceMappingURL=coords.d.ts.map