archimaid 0.0.0-stage → 0.1.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 (154) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/FOUNDATION.md +108 -0
  3. package/LICENSE +21 -0
  4. package/README.md +271 -2
  5. package/dist/archimate/catalogue.d.ts +106 -0
  6. package/dist/archimate/catalogue.js +219 -0
  7. package/dist/archimate/catalogue.js.map +1 -0
  8. package/dist/archimate/diagnostics.d.ts +48 -0
  9. package/dist/archimate/diagnostics.js +64 -0
  10. package/dist/archimate/diagnostics.js.map +1 -0
  11. package/dist/archimate/frontmatter.d.ts +13 -0
  12. package/dist/archimate/frontmatter.js +62 -0
  13. package/dist/archimate/frontmatter.js.map +1 -0
  14. package/dist/archimate/grammar.d.ts +60 -0
  15. package/dist/archimate/grammar.js +273 -0
  16. package/dist/archimate/grammar.js.map +1 -0
  17. package/dist/archimate/lexer.d.ts +91 -0
  18. package/dist/archimate/lexer.js +215 -0
  19. package/dist/archimate/lexer.js.map +1 -0
  20. package/dist/archimate/model.d.ts +46 -0
  21. package/dist/archimate/model.js +2 -0
  22. package/dist/archimate/model.js.map +1 -0
  23. package/dist/archimate/nesting.d.ts +14 -0
  24. package/dist/archimate/nesting.js +84 -0
  25. package/dist/archimate/nesting.js.map +1 -0
  26. package/dist/archimate/parser.d.ts +14 -0
  27. package/dist/archimate/parser.js +53 -0
  28. package/dist/archimate/parser.js.map +1 -0
  29. package/dist/archimate/suggestions.d.ts +1 -0
  30. package/dist/archimate/suggestions.js +44 -0
  31. package/dist/archimate/suggestions.js.map +1 -0
  32. package/dist/archimate/validate.d.ts +4 -0
  33. package/dist/archimate/validate.js +402 -0
  34. package/dist/archimate/validate.js.map +1 -0
  35. package/dist/bundle/archimaid.esm.min.mjs +1 -0
  36. package/dist/bundle/chunks/archimateDiagram-WUCF6KHR.mjs +132 -0
  37. package/dist/bundle/chunks/chunk-QBMRZFD3.mjs +1 -0
  38. package/dist/bundle/chunks/elk.bundled-2VSLVJGB.mjs +26 -0
  39. package/dist/cli/render.d.ts +10 -0
  40. package/dist/cli/render.js +102 -0
  41. package/dist/cli/render.js.map +1 -0
  42. package/dist/cli/report.d.ts +20 -0
  43. package/dist/cli/report.js +39 -0
  44. package/dist/cli/report.js.map +1 -0
  45. package/dist/cli/sources.d.ts +19 -0
  46. package/dist/cli/sources.js +86 -0
  47. package/dist/cli/sources.js.map +1 -0
  48. package/dist/cli.d.ts +2 -0
  49. package/dist/cli.js +137 -0
  50. package/dist/cli.js.map +1 -0
  51. package/dist/index.d.ts +2 -0
  52. package/dist/index.js +2 -0
  53. package/dist/index.js.map +1 -0
  54. package/dist/language.d.ts +5 -0
  55. package/dist/language.js +3 -0
  56. package/dist/language.js.map +1 -0
  57. package/dist/mermaid/archimateDetector.d.ts +12 -0
  58. package/dist/mermaid/archimateDetector.js +29 -0
  59. package/dist/mermaid/archimateDetector.js.map +1 -0
  60. package/dist/mermaid/archimateDiagram.d.ts +9 -0
  61. package/dist/mermaid/archimateDiagram.js +75 -0
  62. package/dist/mermaid/archimateDiagram.js.map +1 -0
  63. package/dist/mermaid/archimateRenderer.d.ts +6 -0
  64. package/dist/mermaid/archimateRenderer.js +40 -0
  65. package/dist/mermaid/archimateRenderer.js.map +1 -0
  66. package/dist/mermaid/architectureLayout.d.ts +50 -0
  67. package/dist/mermaid/architectureLayout.js +144 -0
  68. package/dist/mermaid/architectureLayout.js.map +1 -0
  69. package/dist/mermaid/geometry.d.ts +3 -0
  70. package/dist/mermaid/geometry.js +20 -0
  71. package/dist/mermaid/geometry.js.map +1 -0
  72. package/dist/mermaid/hierarchy.d.ts +1 -0
  73. package/dist/mermaid/hierarchy.js +8 -0
  74. package/dist/mermaid/hierarchy.js.map +1 -0
  75. package/dist/mermaid/invariant.d.ts +1 -0
  76. package/dist/mermaid/invariant.js +7 -0
  77. package/dist/mermaid/invariant.js.map +1 -0
  78. package/dist/mermaid/orthogonalRouter.d.ts +4 -0
  79. package/dist/mermaid/orthogonalRouter.js +249 -0
  80. package/dist/mermaid/orthogonalRouter.js.map +1 -0
  81. package/dist/mermaid/relationshipPlacement.d.ts +3 -0
  82. package/dist/mermaid/relationshipPlacement.js +350 -0
  83. package/dist/mermaid/relationshipPlacement.js.map +1 -0
  84. package/dist/mermaid/render/draw.d.ts +17 -0
  85. package/dist/mermaid/render/draw.js +275 -0
  86. package/dist/mermaid/render/draw.js.map +1 -0
  87. package/dist/mermaid/render/elkRoutes.d.ts +6 -0
  88. package/dist/mermaid/render/elkRoutes.js +216 -0
  89. package/dist/mermaid/render/elkRoutes.js.map +1 -0
  90. package/dist/mermaid/render/graph.d.ts +17 -0
  91. package/dist/mermaid/render/graph.js +200 -0
  92. package/dist/mermaid/render/graph.js.map +1 -0
  93. package/dist/mermaid/render/labels.d.ts +12 -0
  94. package/dist/mermaid/render/labels.js +148 -0
  95. package/dist/mermaid/render/labels.js.map +1 -0
  96. package/dist/mermaid/render/layout.d.ts +12 -0
  97. package/dist/mermaid/render/layout.js +60 -0
  98. package/dist/mermaid/render/layout.js.map +1 -0
  99. package/dist/mermaid/render/measure.d.ts +16 -0
  100. package/dist/mermaid/render/measure.js +96 -0
  101. package/dist/mermaid/render/measure.js.map +1 -0
  102. package/dist/mermaid/render/place.d.ts +20 -0
  103. package/dist/mermaid/render/place.js +63 -0
  104. package/dist/mermaid/render/place.js.map +1 -0
  105. package/dist/mermaid/render/route.d.ts +29 -0
  106. package/dist/mermaid/render/route.js +113 -0
  107. package/dist/mermaid/render/route.js.map +1 -0
  108. package/dist/mermaid/render/shared.d.ts +83 -0
  109. package/dist/mermaid/render/shared.js +52 -0
  110. package/dist/mermaid/render/shared.js.map +1 -0
  111. package/dist/mermaid/render/svg.d.ts +6 -0
  112. package/dist/mermaid/render/svg.js +33 -0
  113. package/dist/mermaid/render/svg.js.map +1 -0
  114. package/dist/mermaid/render/viewport.d.ts +14 -0
  115. package/dist/mermaid/render/viewport.js +58 -0
  116. package/dist/mermaid/render/viewport.js.map +1 -0
  117. package/dist/mermaid/routing/anchors.d.ts +14 -0
  118. package/dist/mermaid/routing/anchors.js +134 -0
  119. package/dist/mermaid/routing/anchors.js.map +1 -0
  120. package/dist/mermaid/routing/bundles.d.ts +14 -0
  121. package/dist/mermaid/routing/bundles.js +94 -0
  122. package/dist/mermaid/routing/bundles.js.map +1 -0
  123. package/dist/mermaid/routing/congestion.d.ts +8 -0
  124. package/dist/mermaid/routing/congestion.js +70 -0
  125. package/dist/mermaid/routing/congestion.js.map +1 -0
  126. package/dist/mermaid/routing/constants.d.ts +13 -0
  127. package/dist/mermaid/routing/constants.js +17 -0
  128. package/dist/mermaid/routing/constants.js.map +1 -0
  129. package/dist/mermaid/routing/primitives.d.ts +33 -0
  130. package/dist/mermaid/routing/primitives.js +110 -0
  131. package/dist/mermaid/routing/primitives.js.map +1 -0
  132. package/dist/mermaid/routing/search.d.ts +4 -0
  133. package/dist/mermaid/routing/search.js +171 -0
  134. package/dist/mermaid/routing/search.js.map +1 -0
  135. package/dist/mermaid/routing/shapes.d.ts +8 -0
  136. package/dist/mermaid/routing/shapes.js +82 -0
  137. package/dist/mermaid/routing/shapes.js.map +1 -0
  138. package/dist/mermaid/routing/types.d.ts +37 -0
  139. package/dist/mermaid/routing/types.js +14 -0
  140. package/dist/mermaid/routing/types.js.map +1 -0
  141. package/dist/mermaid/styles.d.ts +89 -0
  142. package/dist/mermaid/styles.js +166 -0
  143. package/dist/mermaid/styles.js.map +1 -0
  144. package/docs/reference.md +86 -0
  145. package/examples/cross-layer-relationships.mmd +79 -0
  146. package/examples/cross-layer-relationships.svg +33 -0
  147. package/examples/element-preview.svg +1 -0
  148. package/examples/nested-components.mmd +21 -0
  149. package/examples/nested-components.svg +4 -0
  150. package/examples/order-fulfilment.mmd +10 -0
  151. package/examples/order-management.mmd +15 -0
  152. package/examples/order-management.svg +2 -0
  153. package/package.json +91 -4
  154. package/skills/archimaid/SKILL.md +123 -0
@@ -0,0 +1,58 @@
1
+ import { edgeLabelPadding, layoutPadding, markerReach, strokeGutter, titleGap } from './shared.js';
2
+ export function computeViewport(layout, edges, title, diagramPadding) {
3
+ // ELK's fixed gutter keeps routing independent of the requested outer margin.
4
+ let left = Math.min(layoutPadding, layout.width / 2);
5
+ let top = Math.min(layoutPadding, layout.height / 2);
6
+ let right = layout.width - left;
7
+ let bottom = layout.height - top;
8
+ const markerAnchors = [];
9
+ for (const { appearance, points, labelPosition } of edges) {
10
+ for (const { x, y } of points) {
11
+ left = Math.min(left, x);
12
+ top = Math.min(top, y);
13
+ right = Math.max(right, x);
14
+ bottom = Math.max(bottom, y);
15
+ }
16
+ if (appearance.label && labelPosition) {
17
+ left = Math.min(left, labelPosition.x);
18
+ top = Math.min(top, labelPosition.y);
19
+ right = Math.max(right, labelPosition.x + appearance.label.width + edgeLabelPadding);
20
+ bottom = Math.max(bottom, labelPosition.y + appearance.label.height);
21
+ }
22
+ const start = points[0];
23
+ const end = points.at(-1);
24
+ if (start && appearance.notation.start !== 'none')
25
+ markerAnchors.push(start);
26
+ if (end && appearance.notation.end !== 'none')
27
+ markerAnchors.push(end);
28
+ }
29
+ const padding = Math.max(strokeGutter, diagramPadding);
30
+ left -= padding;
31
+ top -= padding;
32
+ right += padding;
33
+ bottom += padding;
34
+ for (const { x, y } of markerAnchors) {
35
+ left = Math.min(left, x - markerReach);
36
+ top = Math.min(top, y - markerReach);
37
+ right = Math.max(right, x + markerReach);
38
+ bottom = Math.max(bottom, y + markerReach);
39
+ }
40
+ const graphWidth = right - left;
41
+ const width = Math.max(graphWidth, (title?.width ?? 0) + 2 * padding);
42
+ const titleOffset = title ? title.height + titleGap : 0;
43
+ const height = Math.max(2 * padding, bottom - top + titleOffset);
44
+ return { width, height, graphWidth, left, top, titleOffset, padding };
45
+ }
46
+ export function applyViewport(svg, { width, height }, useMaxWidth) {
47
+ svg.setAttribute('viewBox', `0 0 ${width} ${height}`);
48
+ if (useMaxWidth) {
49
+ svg.setAttribute('width', '100%');
50
+ svg.style.maxWidth = `${width}px`;
51
+ svg.style.height = 'auto';
52
+ }
53
+ else {
54
+ svg.setAttribute('width', String(width));
55
+ svg.setAttribute('height', String(height));
56
+ }
57
+ }
58
+ //# sourceMappingURL=viewport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"viewport.js","sourceRoot":"","sources":["../../../src/mermaid/render/viewport.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,WAAW,EAAE,YAAY,EAAE,QAAQ,EAAoB,MAAM,aAAa,CAAC;AAarH,MAAM,UAAU,eAAe,CAC7B,MAAqB,EACrB,KAA4B,EAC5B,KAA8B,EAC9B,cAAsB;IAEtB,8EAA8E;IAC9E,IAAI,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,EAAE,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;IACrD,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,EAAE,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACrD,IAAI,KAAK,GAAG,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC;IAChC,IAAI,MAAM,GAAG,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC;IACjC,MAAM,aAAa,GAAe,EAAE,CAAC;IACrC,KAAK,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,aAAa,EAAE,IAAI,KAAK,EAAE,CAAC;QAC1D,KAAK,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,IAAI,MAAM,EAAE,CAAC;YAC9B,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;YACzB,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;YACvB,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;YAC3B,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,MAA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CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QACzC,GAAG,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7C,CAAC;AACH,CAAC","sourcesContent":["import type { ElkPoint } from 'elkjs/lib/elk.bundled.js';\nimport type { DiagramLayout } from './layout.js';\nimport type { PlacedEdge } from './labels.js';\nimport { edgeLabelPadding, layoutPadding, markerReach, strokeGutter, titleGap, type TextMetrics } from './shared.js';\n\nexport interface Viewport {\n readonly width: number;\n readonly height: number;\n // The drawing's own extent, centred within the width when the title is wider.\n readonly graphWidth: number;\n readonly left: number;\n readonly top: number;\n readonly titleOffset: number;\n readonly padding: number;\n}\n\nexport function computeViewport(\n layout: DiagramLayout,\n edges: readonly PlacedEdge[],\n title: TextMetrics | undefined,\n diagramPadding: number,\n): Viewport {\n // ELK's fixed gutter keeps routing independent of the requested outer margin.\n let left = Math.min(layoutPadding, layout.width / 2);\n let top = Math.min(layoutPadding, layout.height / 2);\n let right = layout.width - left;\n let bottom = layout.height - top;\n const markerAnchors: ElkPoint[] = [];\n for (const { appearance, points, labelPosition } of edges) {\n for (const { x, y } of points) {\n left = Math.min(left, x);\n top = Math.min(top, y);\n right = Math.max(right, x);\n bottom = Math.max(bottom, y);\n }\n if (appearance.label && labelPosition) {\n left = Math.min(left, labelPosition.x);\n top = Math.min(top, labelPosition.y);\n right = Math.max(right, labelPosition.x + appearance.label.width + edgeLabelPadding);\n bottom = Math.max(bottom, labelPosition.y + appearance.label.height);\n }\n const start = points[0];\n const end = points.at(-1);\n if (start && appearance.notation.start !== 'none') markerAnchors.push(start);\n if (end && appearance.notation.end !== 'none') markerAnchors.push(end);\n }\n const padding = Math.max(strokeGutter, diagramPadding);\n left -= padding;\n top -= padding;\n right += padding;\n bottom += padding;\n for (const { x, y } of markerAnchors) {\n left = Math.min(left, x - markerReach);\n top = Math.min(top, y - markerReach);\n right = Math.max(right, x + markerReach);\n bottom = Math.max(bottom, y + markerReach);\n }\n const graphWidth = right - left;\n const width = Math.max(graphWidth, (title?.width ?? 0) + 2 * padding);\n const titleOffset = title ? title.height + titleGap : 0;\n const height = Math.max(2 * padding, bottom - top + titleOffset);\n return { width, height, graphWidth, left, top, titleOffset, padding };\n}\n\nexport function applyViewport(svg: SVGSVGElement, { width, height }: Viewport, useMaxWidth: boolean): void {\n svg.setAttribute('viewBox', `0 0 ${width} ${height}`);\n if (useMaxWidth) {\n svg.setAttribute('width', '100%');\n svg.style.maxWidth = `${width}px`;\n svg.style.height = 'auto';\n } else {\n svg.setAttribute('width', String(width));\n svg.setAttribute('height', String(height));\n }\n}\n"]}
@@ -0,0 +1,14 @@
1
+ import type { Box, Point } from '../architectureLayout.js';
2
+ import type { BundleGroup } from './bundles.js';
3
+ import { type Port } from './primitives.js';
4
+ import { type RoutingNode } from './types.js';
5
+ export interface AnchorRegistry {
6
+ readonly clear: (id: string, box: Box, point: Point, marker: boolean) => boolean;
7
+ readonly reserve: (id: string, box: Box, point: Point, marker: boolean) => void;
8
+ }
9
+ export declare function createAnchorRegistry(): AnchorRegistry;
10
+ export interface SharedAnchors {
11
+ readonly ports: ReadonlyMap<string, Port>;
12
+ readonly order: readonly BundleGroup[];
13
+ }
14
+ export declare function placeSharedAnchors(nodes: ReadonlyMap<string, RoutingNode>, groups: ReadonlyMap<string, BundleGroup>, anchors: AnchorRegistry): SharedAnchors;
@@ -0,0 +1,134 @@
1
+ import { leadLength, markerGap, trackGap } from './constants.js';
2
+ import { required } from '../invariant.js';
3
+ import { centre, pointDistance, portSide, sidePort } from './primitives.js';
4
+ import { boxOf } from './types.js';
5
+ export function createAnchorRegistry() {
6
+ const anchors = new Map();
7
+ function clear(id, box, point, marker) {
8
+ return !(anchors.get(id)?.get(portSide(box, point)) ?? []).some((other) => Math.abs(other.point.x - point.x) + Math.abs(other.point.y - point.y) <
9
+ (marker || other.marker ? markerGap : trackGap));
10
+ }
11
+ function reserve(id, box, point, marker) {
12
+ let sides = anchors.get(id);
13
+ if (!sides) {
14
+ sides = new Map();
15
+ anchors.set(id, sides);
16
+ }
17
+ const side = portSide(box, point);
18
+ const points = sides.get(side);
19
+ if (points?.some((other) => other.point.x === point.x && other.point.y === point.y))
20
+ return;
21
+ if (points)
22
+ points.push({ point, marker });
23
+ else
24
+ sides.set(side, [{ point, marker }]);
25
+ }
26
+ return { clear, reserve };
27
+ }
28
+ // Spread anchors evenly along a side, in the order given. They stay clear of rounded corners, but trunks
29
+ // get at least a track gap between them where the side allows it.
30
+ function spreadAnchors(start, length, count) {
31
+ const inset = Math.max(0, Math.min(leadLength, (length - (count + 1) * trackGap) / 2));
32
+ return Array.from({ length: count }, (_, index) => start + inset + ((index + 1) * (length - 2 * inset)) / (count + 1));
33
+ }
34
+ // Order anchors so trunks nest: the anchor nearest a side's end serves the nearest far elements.
35
+ export function placeSharedAnchors(nodes, groups, anchors) {
36
+ const sharedPorts = new Map();
37
+ const routingOrder = [];
38
+ const contains = (outer, inner) => outer.x <= inner.x &&
39
+ outer.y <= inner.y &&
40
+ outer.x + outer.width >= inner.x + inner.width &&
41
+ outer.y + outer.height >= inner.y + inner.height;
42
+ const bySide = new Map();
43
+ for (const group of groups.values()) {
44
+ const key = JSON.stringify([group.element, group.side]);
45
+ bySide.set(key, [...(bySide.get(key) ?? []), group]);
46
+ }
47
+ for (const sideGroups of bySide.values()) {
48
+ const [first] = sideGroups;
49
+ if (!first)
50
+ continue;
51
+ const box = boxOf(nodes, first.element);
52
+ const middle = centre(box);
53
+ const farBoxes = (group) => group.members.map((edge) => boxOf(nodes, group.sharedEnd === 'start' ? edge.target : edge.source));
54
+ // A branch to a farther row must rise through the gap beside the element in front of it,
55
+ // so that element's near edge, not the target's centre, decides how far out the branch turns.
56
+ const sideX = first.side === 'left' ? box.x : box.x + box.width;
57
+ const turn = (far) => {
58
+ const between = [...nodes.values()]
59
+ .map((node) => node.box)
60
+ .filter((other) => other !== far &&
61
+ other !== box &&
62
+ !contains(other, far) &&
63
+ !contains(other, box) &&
64
+ other.x < far.x + far.width &&
65
+ other.x + other.width > far.x &&
66
+ (far.y < middle.y
67
+ ? other.y >= far.y + far.height && other.y + other.height <= middle.y
68
+ : other.y + other.height <= far.y && other.y >= middle.y));
69
+ if (!between.length)
70
+ return centre(far).x;
71
+ return first.side === 'left'
72
+ ? Math.max(...between.map((other) => other.x + other.width))
73
+ : Math.min(...between.map((other) => other.x));
74
+ };
75
+ const reach = (group) => Math.max(...farBoxes(group).map((far) => Math.abs(turn(far) - sideX)));
76
+ const vertical = first.side === 'top' || first.side === 'bottom';
77
+ const axis = vertical ? 'x' : 'y';
78
+ const size = vertical ? box.width : box.height;
79
+ const inset = Math.min(leadLength, size / 4);
80
+ // A far element straight across is best served by an anchor within the overlap, so the line stays straight.
81
+ const straightAt = (group) => {
82
+ const [far] = farBoxes(group).toSorted((a, b) => pointDistance(centre(a), middle) - pointDistance(centre(b), middle));
83
+ if (!far)
84
+ return undefined;
85
+ const low = Math.max(box[axis] + inset, far[axis] + inset);
86
+ const high = Math.min(box[axis] + size - inset, far[axis] + (vertical ? far.width : far.height) - inset);
87
+ return low <= high ? (low + high) / 2 : undefined;
88
+ };
89
+ let ordered;
90
+ let positions;
91
+ if (vertical) {
92
+ ordered = sideGroups.toSorted((a, b) => Math.min(...farBoxes(a).map((far) => centre(far).x)) - Math.min(...farBoxes(b).map((far) => centre(far).x)));
93
+ const even = spreadAnchors(box[axis], size, ordered.length);
94
+ positions = ordered.map((group, index) => straightAt(group) ?? required(even[index], 'an evenly spread anchor'));
95
+ }
96
+ else {
97
+ // Trunks that turn upward take the upper half of the side and those that turn downward the lower half,
98
+ // leaving the middle for straight connections to the same row, including ones that arrive there.
99
+ const level = sideGroups
100
+ .filter((group) => straightAt(group) !== undefined)
101
+ .toSorted((a, b) => required(straightAt(a), 'a straight anchor') - required(straightAt(b), 'a straight anchor'));
102
+ const turning = sideGroups.filter((group) => !level.includes(group));
103
+ const rising = turning
104
+ .filter((group) => farBoxes(group).filter((far) => centre(far).y < middle.y).length * 2 >= group.members.length)
105
+ .toSorted((a, b) => reach(a) - reach(b));
106
+ const falling = turning.filter((group) => !rising.includes(group)).toSorted((a, b) => reach(b) - reach(a));
107
+ ordered = [...rising, ...level, ...falling];
108
+ const half = size / 2;
109
+ const upper = spreadAnchors(box.y, half, rising.length);
110
+ const lower = spreadAnchors(middle.y, half, falling.length);
111
+ positions = [...upper, ...level.map((group) => required(straightAt(group), 'a straight anchor')), ...lower];
112
+ }
113
+ // Keep the planned order, at least a marker gap apart where the side is long enough.
114
+ const lowest = box[axis] + inset;
115
+ const highest = box[axis] + size - inset;
116
+ const spacing = Math.min(markerGap, (highest - lowest) / Math.max(1, ordered.length - 1));
117
+ for (let index = 0; index < positions.length; index++) {
118
+ positions[index] = Math.max(required(positions[index], 'an anchor position'), lowest, (positions[index - 1] ?? -Infinity) + spacing);
119
+ }
120
+ for (let index = positions.length - 1; index >= 0; index--) {
121
+ positions[index] = Math.min(required(positions[index], 'an anchor position'), highest, (positions[index + 1] ?? Infinity) - spacing);
122
+ }
123
+ for (const [index, group] of ordered.entries()) {
124
+ const port = sidePort(box, group.side, required(positions[index], 'an anchor position'));
125
+ sharedPorts.set(group.id, port);
126
+ anchors.reserve(group.element, box, port.anchor, group.marker);
127
+ }
128
+ // Inner trunks go first, so outer trunks find them in place and pass outside them.
129
+ const inward = first.side === 'left' || first.side === 'right' ? ordered.toSorted((a, b) => reach(a) - reach(b)) : ordered;
130
+ routingOrder.push(...inward);
131
+ }
132
+ return { ports: sharedPorts, order: routingOrder };
133
+ }
134
+ //# sourceMappingURL=anchors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anchors.js","sourceRoot":"","sources":["../../../src/mermaid/routing/anchors.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAA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AK,EAAE,YAAY,EAAE,CAAC;AACrD,CAAC","sourcesContent":["import type { Box, Point } from '../architectureLayout.js';\nimport { leadLength, markerGap, trackGap } from './constants.js';\nimport { required } from '../invariant.js';\nimport type { BundleGroup } from './bundles.js';\nimport { centre, pointDistance, portSide, sidePort, type Port } from './primitives.js';\nimport { boxOf, type RoutingNode } from './types.js';\n\nexport interface AnchorRegistry {\n // Whether a new anchor keeps the spacing rule against the anchors already on that side.\n readonly clear: (id: string, box: Box, point: Point, marker: boolean) => boolean;\n readonly reserve: (id: string, box: Box, point: Point, marker: boolean) => void;\n}\n\nexport function createAnchorRegistry(): AnchorRegistry {\n const anchors = new Map<string, Map<string, { point: Point; marker: boolean }[]>>();\n function clear(id: string, box: Box, point: Point, marker: boolean): boolean {\n return !(anchors.get(id)?.get(portSide(box, point)) ?? []).some(\n (other) =>\n Math.abs(other.point.x - point.x) + Math.abs(other.point.y - point.y) <\n (marker || other.marker ? markerGap : trackGap),\n );\n }\n function reserve(id: string, box: Box, point: Point, marker: boolean): void {\n let sides = anchors.get(id);\n if (!sides) {\n sides = new Map();\n anchors.set(id, sides);\n }\n const side = portSide(box, point);\n const points = sides.get(side);\n if (points?.some((other) => other.point.x === point.x && other.point.y === point.y)) return;\n if (points) points.push({ point, marker });\n else sides.set(side, [{ point, marker }]);\n }\n return { clear, reserve };\n}\n\n// Spread anchors evenly along a side, in the order given. They stay clear of rounded corners, but trunks\n// get at least a track gap between them where the side allows it.\nfunction spreadAnchors(start: number, length: number, count: number): number[] {\n const inset = Math.max(0, Math.min(leadLength, (length - (count + 1) * trackGap) / 2));\n return Array.from(\n { length: count },\n (_, index) => start + inset + ((index + 1) * (length - 2 * inset)) / (count + 1),\n );\n}\n\nexport interface SharedAnchors {\n readonly ports: ReadonlyMap<string, Port>;\n // Bundles in the order they must be routed.\n readonly order: readonly BundleGroup[];\n}\n\n// Order anchors so trunks nest: the anchor nearest a side's end serves the nearest far elements.\nexport function placeSharedAnchors(\n nodes: ReadonlyMap<string, RoutingNode>,\n groups: ReadonlyMap<string, BundleGroup>,\n anchors: AnchorRegistry,\n): SharedAnchors {\n const sharedPorts = new Map<string, Port>();\n const routingOrder: BundleGroup[] = [];\n const contains = (outer: Box, inner: Box) =>\n outer.x <= inner.x &&\n outer.y <= inner.y &&\n outer.x + outer.width >= inner.x + inner.width &&\n outer.y + outer.height >= inner.y + inner.height;\n const bySide = new Map<string, BundleGroup[]>();\n for (const group of groups.values()) {\n const key = JSON.stringify([group.element, group.side]);\n bySide.set(key, [...(bySide.get(key) ?? []), group]);\n }\n for (const sideGroups of bySide.values()) {\n const [first] = sideGroups;\n if (!first) continue;\n const box = boxOf(nodes, first.element);\n const middle = centre(box);\n const farBoxes = (group: BundleGroup) =>\n group.members.map((edge) => boxOf(nodes, group.sharedEnd === 'start' ? edge.target : edge.source));\n // A branch to a farther row must rise through the gap beside the element in front of it,\n // so that element's near edge, not the target's centre, decides how far out the branch turns.\n const sideX = first.side === 'left' ? box.x : box.x + box.width;\n const turn = (far: Box) => {\n const between = [...nodes.values()]\n .map((node) => node.box)\n .filter(\n (other) =>\n other !== far &&\n other !== box &&\n !contains(other, far) &&\n !contains(other, box) &&\n other.x < far.x + far.width &&\n other.x + other.width > far.x &&\n (far.y < middle.y\n ? other.y >= far.y + far.height && other.y + other.height <= middle.y\n : other.y + other.height <= far.y && other.y >= middle.y),\n );\n if (!between.length) return centre(far).x;\n return first.side === 'left'\n ? Math.max(...between.map((other) => other.x + other.width))\n : Math.min(...between.map((other) => other.x));\n };\n const reach = (group: BundleGroup) => Math.max(...farBoxes(group).map((far) => Math.abs(turn(far) - sideX)));\n const vertical = first.side === 'top' || first.side === 'bottom';\n const axis = vertical ? 'x' : 'y';\n const size = vertical ? box.width : box.height;\n const inset = Math.min(leadLength, size / 4);\n // A far element straight across is best served by an anchor within the overlap, so the line stays straight.\n const straightAt = (group: BundleGroup): number | undefined => {\n const [far] = farBoxes(group).toSorted(\n (a, b) => pointDistance(centre(a), middle) - pointDistance(centre(b), middle),\n );\n if (!far) return undefined;\n const low = Math.max(box[axis] + inset, far[axis] + inset);\n const high = Math.min(box[axis] + size - inset, far[axis] + (vertical ? far.width : far.height) - inset);\n return low <= high ? (low + high) / 2 : undefined;\n };\n let ordered: BundleGroup[];\n let positions: number[];\n if (vertical) {\n ordered = sideGroups.toSorted(\n (a, b) =>\n Math.min(...farBoxes(a).map((far) => centre(far).x)) - Math.min(...farBoxes(b).map((far) => centre(far).x)),\n );\n const even = spreadAnchors(box[axis], size, ordered.length);\n positions = ordered.map((group, index) => straightAt(group) ?? required(even[index], 'an evenly spread anchor'));\n } else {\n // Trunks that turn upward take the upper half of the side and those that turn downward the lower half,\n // leaving the middle for straight connections to the same row, including ones that arrive there.\n const level = sideGroups\n .filter((group) => straightAt(group) !== undefined)\n .toSorted(\n (a, b) => required(straightAt(a), 'a straight anchor') - required(straightAt(b), 'a straight anchor'),\n );\n const turning = sideGroups.filter((group) => !level.includes(group));\n const rising = turning\n .filter((group) => farBoxes(group).filter((far) => centre(far).y < middle.y).length * 2 >= group.members.length)\n .toSorted((a, b) => reach(a) - reach(b));\n const falling = turning.filter((group) => !rising.includes(group)).toSorted((a, b) => reach(b) - reach(a));\n ordered = [...rising, ...level, ...falling];\n const half = size / 2;\n const upper = spreadAnchors(box.y, half, rising.length);\n const lower = spreadAnchors(middle.y, half, falling.length);\n positions = [...upper, ...level.map((group) => required(straightAt(group), 'a straight anchor')), ...lower];\n }\n // Keep the planned order, at least a marker gap apart where the side is long enough.\n const lowest = box[axis] + inset;\n const highest = box[axis] + size - inset;\n const spacing = Math.min(markerGap, (highest - lowest) / Math.max(1, ordered.length - 1));\n for (let index = 0; index < positions.length; index++) {\n positions[index] = Math.max(\n required(positions[index], 'an anchor position'),\n lowest,\n (positions[index - 1] ?? -Infinity) + spacing,\n );\n }\n for (let index = positions.length - 1; index >= 0; index--) {\n positions[index] = Math.min(\n required(positions[index], 'an anchor position'),\n highest,\n (positions[index + 1] ?? Infinity) - spacing,\n );\n }\n for (const [index, group] of ordered.entries()) {\n const port = sidePort(box, group.side, required(positions[index], 'an anchor position'));\n sharedPorts.set(group.id, port);\n anchors.reserve(group.element, box, port.anchor, group.marker);\n }\n // Inner trunks go first, so outer trunks find them in place and pass outside them.\n const inward =\n first.side === 'left' || first.side === 'right' ? ordered.toSorted((a, b) => reach(a) - reach(b)) : ordered;\n routingOrder.push(...inward);\n }\n return { ports: sharedPorts, order: routingOrder };\n}\n"]}
@@ -0,0 +1,14 @@
1
+ import type { Box, Point } from '../architectureLayout.js';
2
+ import { type Side } from './primitives.js';
3
+ import { type RoutingEdge, type RoutingNode } from './types.js';
4
+ export interface BundleGroup {
5
+ readonly id: string;
6
+ readonly element: string;
7
+ readonly side: Side;
8
+ readonly sharedEnd: 'start' | 'end';
9
+ readonly marker: boolean;
10
+ readonly members: readonly RoutingEdge[];
11
+ }
12
+ export declare function bundleSide(shared: Box, far: Box): Side;
13
+ export declare function farSide(group: Side, anchor: Point, far: Box): Side;
14
+ export declare function planBundles(nodes: ReadonlyMap<string, RoutingNode>, edges: readonly RoutingEdge[]): ReadonlyMap<string, BundleGroup>;
@@ -0,0 +1,94 @@
1
+ import { busyDegree } from './constants.js';
2
+ import { centre, oppositeSide } from './primitives.js';
3
+ import { ancestorIds, boxOf } from './types.js';
4
+ // A far element directly above or below gets a straight line from the facing side. Anything else
5
+ // joins a trunk on the left or right side, which then runs along the gap between rows.
6
+ export function bundleSide(shared, far) {
7
+ if (far.x < shared.x + shared.width && far.x + far.width > shared.x) {
8
+ return far.y < shared.y ? 'top' : 'bottom';
9
+ }
10
+ return centre(far).x < centre(shared).x ? 'left' : 'right';
11
+ }
12
+ export function farSide(group, anchor, far) {
13
+ if (group === 'top')
14
+ return 'bottom';
15
+ if (group === 'bottom')
16
+ return 'top';
17
+ if (far.y + far.height <= anchor.y)
18
+ return 'bottom';
19
+ if (far.y >= anchor.y)
20
+ return 'top';
21
+ return oppositeSide[group];
22
+ }
23
+ // Relationships of one kind that leave, or arrive at, the same element share an anchor and a trunk.
24
+ // The groups keep the order in which they were found, which decides routing order.
25
+ export function planBundles(nodes, edges) {
26
+ const eligible = edges.filter((edge) => {
27
+ if (edge.bundling === undefined || edge.source === edge.target)
28
+ return false;
29
+ return (!ancestorIds(edge.source, nodes).includes(edge.target) && !ancestorIds(edge.target, nodes).includes(edge.source));
30
+ });
31
+ const candidates = new Map();
32
+ const kindKey = ({ bundling }) => [bundling?.type, bundling?.access, bundling?.directed];
33
+ const keys = (edge) => [
34
+ {
35
+ key: JSON.stringify([edge.source, ...kindKey(edge), 'start']),
36
+ element: edge.source,
37
+ sharedEnd: 'start',
38
+ },
39
+ { key: JSON.stringify([edge.target, ...kindKey(edge), 'end']), element: edge.target, sharedEnd: 'end' },
40
+ ];
41
+ for (const edge of eligible) {
42
+ for (const { key } of keys(edge))
43
+ candidates.set(key, [...(candidates.get(key) ?? []), edge]);
44
+ }
45
+ const eligibleDegrees = new Map();
46
+ for (const { source, target } of eligible) {
47
+ for (const id of [source, target])
48
+ eligibleDegrees.set(id, (eligibleDegrees.get(id) ?? 0) + 1);
49
+ }
50
+ const groups = new Map();
51
+ for (const edge of eligible) {
52
+ const [atSource, atTarget] = keys(edge);
53
+ if (!atSource || !atTarget)
54
+ continue;
55
+ const sourceSize = candidates.get(atSource.key)?.length ?? 0;
56
+ const targetSize = candidates.get(atTarget.key)?.length ?? 0;
57
+ const sourceDegree = eligibleDegrees.get(edge.source) ?? 0;
58
+ const targetDegree = eligibleDegrees.get(edge.target) ?? 0;
59
+ // A lone relationship still gets a planned anchor at a busy element, so its trunk nests with the bundles there.
60
+ const chosen = Math.max(sourceSize, targetSize) >= 2
61
+ ? sourceSize >= targetSize
62
+ ? atSource
63
+ : atTarget
64
+ : Math.max(sourceDegree, targetDegree) >= busyDegree
65
+ ? sourceDegree >= targetDegree
66
+ ? atSource
67
+ : atTarget
68
+ : undefined;
69
+ if (!chosen)
70
+ continue;
71
+ const far = chosen.sharedEnd === 'start' ? edge.target : edge.source;
72
+ const side = bundleSide(boxOf(nodes, chosen.element), boxOf(nodes, far));
73
+ const id = JSON.stringify([chosen.key, side]);
74
+ const group = groups.get(id);
75
+ // Two relationships of one kind between the same pair would coincide completely, so the copy routes alone.
76
+ const farOf = (member) => (chosen.sharedEnd === 'start' ? member.target : member.source);
77
+ if (group?.members.some((member) => farOf(member) === far))
78
+ continue;
79
+ if (group)
80
+ group.members.push(edge);
81
+ else {
82
+ groups.set(id, {
83
+ id,
84
+ element: chosen.element,
85
+ side,
86
+ sharedEnd: chosen.sharedEnd,
87
+ members: [edge],
88
+ marker: (chosen.sharedEnd === 'start' ? edge.sourceMarker : edge.targetMarker) ?? false,
89
+ });
90
+ }
91
+ }
92
+ return groups;
93
+ }
94
+ //# sourceMappingURL=bundles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bundles.js","sourceRoot":"","sources":["../../../src/mermaid/routing/bundles.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,YAAY,EAAa,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,KAAK,EAAsC,MAAM,YAAY,CAAC;AAWpF,iGAAiG;AACjG,uFAAuF;AACvF,MAAM,UAAU,UAAU,CAAC,MAAW,EAAE,GAAQ;IAC9C,IAAI,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,IAAI,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,KAAK,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC;QACpE,OAAO,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC;IAC7C,CAAC;IACD,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAA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AAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC7B,2GAA2G;QAC3G,MAAM,KAAK,GAAG,CAAC,MAAmB,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACtG,IAAI,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC;YAAE,SAAS;QACrE,IAAI,KAAK;YAAE,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aAC/B,CAAC;YACJ,MAAM,CAAC,GAAG,CAAC,EAAE,EAAE;gBACb,EAAE;gBACF,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,IAAI;gBACJ,SAAS,EAAE,MAAM,CAAC,SAAS;gBAC3B,OAAO,EAAE,CAAC,IAAI,CAAC;gBACf,MAAM,EAAE,CAAC,MAAM,CAAC,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,KAAK;aACxF,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { Box, Point } from '../architectureLayout.js';\nimport { busyDegree } from './constants.js';\nimport { centre, oppositeSide, type Side } from './primitives.js';\nimport { ancestorIds, boxOf, type RoutingEdge, type RoutingNode } from './types.js';\n\nexport interface BundleGroup {\n readonly id: string;\n readonly element: string;\n readonly side: Side;\n readonly sharedEnd: 'start' | 'end';\n readonly marker: boolean;\n readonly members: readonly RoutingEdge[];\n}\n\n// A far element directly above or below gets a straight line from the facing side. Anything else\n// joins a trunk on the left or right side, which then runs along the gap between rows.\nexport function bundleSide(shared: Box, far: Box): Side {\n if (far.x < shared.x + shared.width && far.x + far.width > shared.x) {\n return far.y < shared.y ? 'top' : 'bottom';\n }\n return centre(far).x < centre(shared).x ? 'left' : 'right';\n}\n\nexport function farSide(group: Side, anchor: Point, far: Box): Side {\n if (group === 'top') return 'bottom';\n if (group === 'bottom') return 'top';\n if (far.y + far.height <= anchor.y) return 'bottom';\n if (far.y >= anchor.y) return 'top';\n return oppositeSide[group];\n}\n\n// Relationships of one kind that leave, or arrive at, the same element share an anchor and a trunk.\n// The groups keep the order in which they were found, which decides routing order.\nexport function planBundles(\n nodes: ReadonlyMap<string, RoutingNode>,\n edges: readonly RoutingEdge[],\n): ReadonlyMap<string, BundleGroup> {\n const eligible = edges.filter((edge) => {\n if (edge.bundling === undefined || edge.source === edge.target) return false;\n return (\n !ancestorIds(edge.source, nodes).includes(edge.target) && !ancestorIds(edge.target, nodes).includes(edge.source)\n );\n });\n const candidates = new Map<string, RoutingEdge[]>();\n const kindKey = ({ bundling }: RoutingEdge) => [bundling?.type, bundling?.access, bundling?.directed];\n const keys = (edge: RoutingEdge) => [\n {\n key: JSON.stringify([edge.source, ...kindKey(edge), 'start']),\n element: edge.source,\n sharedEnd: 'start' as const,\n },\n { key: JSON.stringify([edge.target, ...kindKey(edge), 'end']), element: edge.target, sharedEnd: 'end' as const },\n ];\n for (const edge of eligible) {\n for (const { key } of keys(edge)) candidates.set(key, [...(candidates.get(key) ?? []), edge]);\n }\n const eligibleDegrees = new Map<string, number>();\n for (const { source, target } of eligible) {\n for (const id of [source, target]) eligibleDegrees.set(id, (eligibleDegrees.get(id) ?? 0) + 1);\n }\n const groups = new Map<string, BundleGroup & { readonly members: RoutingEdge[] }>();\n for (const edge of eligible) {\n const [atSource, atTarget] = keys(edge);\n if (!atSource || !atTarget) continue;\n const sourceSize = candidates.get(atSource.key)?.length ?? 0;\n const targetSize = candidates.get(atTarget.key)?.length ?? 0;\n const sourceDegree = eligibleDegrees.get(edge.source) ?? 0;\n const targetDegree = eligibleDegrees.get(edge.target) ?? 0;\n // A lone relationship still gets a planned anchor at a busy element, so its trunk nests with the bundles there.\n const chosen =\n Math.max(sourceSize, targetSize) >= 2\n ? sourceSize >= targetSize\n ? atSource\n : atTarget\n : Math.max(sourceDegree, targetDegree) >= busyDegree\n ? sourceDegree >= targetDegree\n ? atSource\n : atTarget\n : undefined;\n if (!chosen) continue;\n const far = chosen.sharedEnd === 'start' ? edge.target : edge.source;\n const side = bundleSide(boxOf(nodes, chosen.element), boxOf(nodes, far));\n const id = JSON.stringify([chosen.key, side]);\n const group = groups.get(id);\n // Two relationships of one kind between the same pair would coincide completely, so the copy routes alone.\n const farOf = (member: RoutingEdge) => (chosen.sharedEnd === 'start' ? member.target : member.source);\n if (group?.members.some((member) => farOf(member) === far)) continue;\n if (group) group.members.push(edge);\n else {\n groups.set(id, {\n id,\n element: chosen.element,\n side,\n sharedEnd: chosen.sharedEnd,\n members: [edge],\n marker: (chosen.sharedEnd === 'start' ? edge.sourceMarker : edge.targetMarker) ?? false,\n });\n }\n }\n return groups;\n}\n"]}
@@ -0,0 +1,8 @@
1
+ import type { Point } from '../architectureLayout.js';
2
+ import { type Axis } from './primitives.js';
3
+ export type RouteCost = (axis: Axis, start: Point, end: Point) => number;
4
+ export interface Congestion {
5
+ readonly cost: (axis: Axis, start: Point, end: Point, owner?: string) => number;
6
+ readonly add: (axis: Axis, across: number, from: number, to: number, owner?: string) => void;
7
+ }
8
+ export declare function createCongestion(): Congestion;
@@ -0,0 +1,70 @@
1
+ import { crossingCost, trackGap } from './constants.js';
2
+ import { firstAtLeast, otherAxis } from './primitives.js';
3
+ // Lines are cached for the whole diagram and updated as routes are recorded, because rebuilding them
4
+ // for every search dominated routing time on tall diagrams.
5
+ export function createCongestion() {
6
+ const tracks = { x: new Map(), y: new Map() };
7
+ const lines = { x: new Map(), y: new Map() };
8
+ function line(axis, coordinate) {
9
+ const cached = lines[axis].get(coordinate);
10
+ if (cached)
11
+ return cached;
12
+ const parallel = [];
13
+ const crossings = [];
14
+ for (const [offset, ranges] of tracks[axis]) {
15
+ const distance = Math.abs(coordinate - offset);
16
+ if (distance >= trackGap)
17
+ continue;
18
+ for (const [from, to, owner] of ranges) {
19
+ parallel.push({ from, to, weight: 1 - distance / trackGap, owner, shared: distance === 0 });
20
+ }
21
+ }
22
+ for (const [offset, ranges] of tracks[otherAxis(axis)]) {
23
+ for (const [from, to] of ranges) {
24
+ if (from < coordinate && coordinate < to)
25
+ crossings.push(offset);
26
+ }
27
+ }
28
+ crossings.sort((a, b) => a - b);
29
+ const created = { parallel, crossings };
30
+ lines[axis].set(coordinate, created);
31
+ return created;
32
+ }
33
+ return {
34
+ cost(axis, start, end, owner) {
35
+ const { parallel, crossings } = line(axis, start[otherAxis(axis)]);
36
+ const from = Math.min(start[axis], end[axis]);
37
+ const to = Math.max(start[axis], end[axis]);
38
+ let penalty = 0;
39
+ let reused = 0;
40
+ for (const track of parallel) {
41
+ const overlap = Math.max(0, Math.min(to, track.to) - Math.max(from, track.from));
42
+ if (owner === undefined || track.owner !== owner)
43
+ penalty += 2 * track.weight * overlap;
44
+ else if (track.shared)
45
+ reused = Math.max(reused, overlap);
46
+ }
47
+ // Following a bundle's own trunk is cheaper than a fresh line, so members share it. The discount
48
+ // stays below the step length, which keeps every step cost positive for the search.
49
+ return penalty - reused / 2 + crossingCost * (firstAtLeast(crossings, to) - firstAtLeast(crossings, from));
50
+ },
51
+ add(axis, across, from, to, owner) {
52
+ const occupied = tracks[axis].get(across);
53
+ if (occupied)
54
+ occupied.push([from, to, owner]);
55
+ else
56
+ tracks[axis].set(across, [[from, to, owner]]);
57
+ for (const [coordinate, { parallel }] of lines[axis]) {
58
+ const distance = Math.abs(coordinate - across);
59
+ if (distance < trackGap) {
60
+ parallel.push({ from, to, weight: 1 - distance / trackGap, owner, shared: distance === 0 });
61
+ }
62
+ }
63
+ for (const [coordinate, { crossings }] of lines[otherAxis(axis)]) {
64
+ if (from < coordinate && coordinate < to)
65
+ crossings.splice(firstAtLeast(crossings, across), 0, across);
66
+ }
67
+ },
68
+ };
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xB,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC,GAAG,QAAQ,GAAG,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,KAAK,CAAC,EAAE,CAAC,CAAC;gBAC9F,CAAC;YACH,CAAC;YACD,KAAK,MAAM,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,CAAC,IAAI,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;gBACjE,IAAI,IAAI,GAAG,UAAU,IAAI,UAAU,GAAG,EAAE;oBAAE,SAAS,CAAC,MAAM,CAAC,YAAY,CAAC,SAAS,EAAE,MAAM,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC;YACzG,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import type { Point } from '../architectureLayout.js';\nimport { crossingCost, trackGap } from './constants.js';\nimport { firstAtLeast, otherAxis, type Axis } from './primitives.js';\n\ntype Track = readonly [from: number, to: number, owner: string | undefined];\ntype Tracks = Record<Axis, Map<number, Track[]>>;\n\nexport type RouteCost = (axis: Axis, start: Point, end: Point) => number;\n\nexport interface Congestion {\n readonly cost: (axis: Axis, start: Point, end: Point, owner?: string) => number;\n readonly add: (axis: Axis, across: number, from: number, to: number, owner?: string) => void;\n}\n\n// Lines are cached for the whole diagram and updated as routes are recorded, because rebuilding them\n// for every search dominated routing time on tall diagrams.\nexport function createCongestion(): Congestion {\n type Line = {\n parallel: { from: number; to: number; weight: number; owner: string | undefined; shared: boolean }[];\n crossings: number[];\n };\n const tracks: Tracks = { x: new Map(), y: new Map() };\n const lines: Record<Axis, Map<number, Line>> = { x: new Map(), y: new Map() };\n function line(axis: Axis, coordinate: number): Line {\n const cached = lines[axis].get(coordinate);\n if (cached) return cached;\n const parallel: Line['parallel'] = [];\n const crossings: Line['crossings'] = [];\n for (const [offset, ranges] of tracks[axis]) {\n const distance = Math.abs(coordinate - offset);\n if (distance >= trackGap) continue;\n for (const [from, to, owner] of ranges) {\n parallel.push({ from, to, weight: 1 - distance / trackGap, owner, shared: distance === 0 });\n }\n }\n for (const [offset, ranges] of tracks[otherAxis(axis)]) {\n for (const [from, to] of ranges) {\n if (from < coordinate && coordinate < to) crossings.push(offset);\n }\n }\n crossings.sort((a, b) => a - b);\n const created = { parallel, crossings };\n lines[axis].set(coordinate, created);\n return created;\n }\n return {\n cost(axis, start, end, owner) {\n const { parallel, crossings } = line(axis, start[otherAxis(axis)]);\n const from = Math.min(start[axis], end[axis]);\n const to = Math.max(start[axis], end[axis]);\n let penalty = 0;\n let reused = 0;\n for (const track of parallel) {\n const overlap = Math.max(0, Math.min(to, track.to) - Math.max(from, track.from));\n if (owner === undefined || track.owner !== owner) penalty += 2 * track.weight * overlap;\n else if (track.shared) reused = Math.max(reused, overlap);\n }\n // Following a bundle's own trunk is cheaper than a fresh line, so members share it. The discount\n // stays below the step length, which keeps every step cost positive for the search.\n return penalty - reused / 2 + crossingCost * (firstAtLeast(crossings, to) - firstAtLeast(crossings, from));\n },\n add(axis, across, from, to, owner) {\n const occupied = tracks[axis].get(across);\n if (occupied) occupied.push([from, to, owner]);\n else tracks[axis].set(across, [[from, to, owner]]);\n for (const [coordinate, { parallel }] of lines[axis]) {\n const distance = Math.abs(coordinate - across);\n if (distance < trackGap) {\n parallel.push({ from, to, weight: 1 - distance / trackGap, owner, shared: distance === 0 });\n }\n }\n for (const [coordinate, { crossings }] of lines[otherAxis(axis)]) {\n if (from < coordinate && coordinate < to) crossings.splice(firstAtLeast(crossings, across), 0, across);\n }\n },\n };\n}\n"]}
@@ -0,0 +1,13 @@
1
+ export declare const leadLength = 14;
2
+ export declare const bendCost = 20;
3
+ export declare const trackGap = 8;
4
+ export declare const markerGap = 14;
5
+ export declare const crossingCost = 32;
6
+ export declare const loopReach = 24;
7
+ export declare const heuristicWeight = 1.3;
8
+ export declare const clearances: {
9
+ readonly comfortable: 8;
10
+ readonly tight: 2;
11
+ };
12
+ export declare const searchMargin = 160;
13
+ export declare const busyDegree = 3;
@@ -0,0 +1,17 @@
1
+ import { markerExtent } from '../geometry.js';
2
+ export const leadLength = 14;
3
+ export const bendCost = 20;
4
+ export const trackGap = 8;
5
+ // Neighbouring anchors with markers need room for a marker and its outline.
6
+ export const markerGap = markerExtent;
7
+ export const crossingCost = 32;
8
+ export const loopReach = 24;
9
+ // Weighting the distance still to go above 1 makes the search greedy: it gives up guaranteed shortest
10
+ // routes for far fewer expanded states on dense diagrams.
11
+ export const heuristicWeight = 1.3;
12
+ // Routes first try to stay a comfortable distance from elements, then accept a tight squeeze.
13
+ export const clearances = { comfortable: 8, tight: 2 };
14
+ // Wide enough for a detour around one neighbouring element and its gap; anything longer searches the full grid.
15
+ export const searchMargin = 160;
16
+ export const busyDegree = 3;
17
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/mermaid/routing/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,MAAM,CAAC,MAAM,UAAU,GAAG,EAAE,CAAC;AAC7B,MAAM,CAAC,MAAM,QAAQ,GAAG,EAAE,CAAC;AAC3B,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,CAAC;AAC1B,4EAA4E;AAC5E,MAAM,CAAC,MAAM,SAAS,GAAG,YAAY,CAAC;AACtC,MAAM,CAAC,MAAM,YAAY,GAAG,EAAE,CAAC;AAC/B,MAAM,CAAC,MAAM,SAAS,GAAG,EAAE,CAAC;AAC5B,sGAAsG;AACtG,0DAA0D;AAC1D,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,CAAC;AACnC,8FAA8F;AAC9F,MAAM,CAAC,MAAM,UAAU,GAAG,EAAE,WAAW,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAW,CAAC;AAChE,gHAAgH;AAChH,MAAM,CAAC,MAAM,YAAY,GAAG,GAAG,CAAC;AAChC,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAC","sourcesContent":["import { markerExtent } from '../geometry.js';\n\nexport const leadLength = 14;\nexport const bendCost = 20;\nexport const trackGap = 8;\n// Neighbouring anchors with markers need room for a marker and its outline.\nexport const markerGap = markerExtent;\nexport const crossingCost = 32;\nexport const loopReach = 24;\n// Weighting the distance still to go above 1 makes the search greedy: it gives up guaranteed shortest\n// routes for far fewer expanded states on dense diagrams.\nexport const heuristicWeight = 1.3;\n// Routes first try to stay a comfortable distance from elements, then accept a tight squeeze.\nexport const clearances = { comfortable: 8, tight: 2 } as const;\n// Wide enough for a detour around one neighbouring element and its gap; anything longer searches the full grid.\nexport const searchMargin = 160;\nexport const busyDegree = 3;\n"]}
@@ -0,0 +1,33 @@
1
+ import type { Box, Point } from '../architectureLayout.js';
2
+ export type Axis = 'x' | 'y';
3
+ export interface Port {
4
+ anchor: Point;
5
+ lead: Point;
6
+ axis: Axis;
7
+ cost: number;
8
+ }
9
+ export interface Bounds {
10
+ left: number;
11
+ right: number;
12
+ top: number;
13
+ bottom: number;
14
+ }
15
+ export declare function otherAxis(axis: Axis): Axis;
16
+ export declare function bounds(box: Box, padding?: number): Bounds;
17
+ export declare function crosses(start: Point, end: Point, obstacles: readonly Bounds[]): boolean;
18
+ export declare function ports(box: Box, fraction: number, inside: boolean): Port[];
19
+ export declare function firstAtLeast(sorted: ArrayLike<number>, value: number, strictlyAbove?: boolean): number;
20
+ export declare function axisSize(box: Box, axis: Axis): number;
21
+ export declare function pointDistance(a: Point, b: Point): number;
22
+ export type Side = 'top' | 'bottom' | 'left' | 'right';
23
+ export declare const oppositeSide: {
24
+ top: "bottom";
25
+ bottom: "top";
26
+ left: "right";
27
+ right: "left";
28
+ };
29
+ export declare function beyondSides(box: Box, other: Box): Record<Side, boolean>;
30
+ export declare function facing(candidates: readonly Port[], box: Box, other: Box): Port[];
31
+ export declare function portSide(box: Box, point: Point): Side;
32
+ export declare function centre(box: Box): Point;
33
+ export declare function sidePort(box: Box, side: Side, position: number): Port;