@nowline/layout 0.8.6 → 0.9.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 (338) hide show
  1. package/dist/axis-columns.d.ts +70 -0
  2. package/dist/axis-columns.d.ts.map +1 -0
  3. package/dist/axis-columns.js +104 -0
  4. package/dist/axis-columns.js.map +1 -0
  5. package/dist/calendar-resolver.d.ts +27 -0
  6. package/dist/calendar-resolver.d.ts.map +1 -0
  7. package/dist/calendar-resolver.js +132 -0
  8. package/dist/calendar-resolver.js.map +1 -0
  9. package/dist/calendar.d.ts +0 -19
  10. package/dist/calendar.d.ts.map +1 -1
  11. package/dist/calendar.js +4 -44
  12. package/dist/calendar.js.map +1 -1
  13. package/dist/canvas-width.d.ts +23 -0
  14. package/dist/canvas-width.d.ts.map +1 -0
  15. package/dist/canvas-width.js +26 -0
  16. package/dist/canvas-width.js.map +1 -0
  17. package/dist/capacity.d.ts +28 -0
  18. package/dist/capacity.d.ts.map +1 -1
  19. package/dist/capacity.js +64 -29
  20. package/dist/capacity.js.map +1 -1
  21. package/dist/chart-translation.d.ts +22 -0
  22. package/dist/chart-translation.d.ts.map +1 -0
  23. package/dist/chart-translation.js +83 -0
  24. package/dist/chart-translation.js.map +1 -0
  25. package/dist/container-header-geometry.d.ts +64 -0
  26. package/dist/container-header-geometry.d.ts.map +1 -0
  27. package/dist/container-header-geometry.js +89 -0
  28. package/dist/container-header-geometry.js.map +1 -0
  29. package/dist/dsl-utils.d.ts +3 -4
  30. package/dist/dsl-utils.d.ts.map +1 -1
  31. package/dist/dsl-utils.js +8 -25
  32. package/dist/dsl-utils.js.map +1 -1
  33. package/dist/edge-routing.d.ts +69 -78
  34. package/dist/edge-routing.d.ts.map +1 -1
  35. package/dist/edge-routing.js +271 -401
  36. package/dist/edge-routing.js.map +1 -1
  37. package/dist/frame-tab-geometry.d.ts +24 -14
  38. package/dist/frame-tab-geometry.d.ts.map +1 -1
  39. package/dist/frame-tab-geometry.js +69 -23
  40. package/dist/frame-tab-geometry.js.map +1 -1
  41. package/dist/header-card-geometry.d.ts +12 -0
  42. package/dist/header-card-geometry.d.ts.map +1 -1
  43. package/dist/header-card-geometry.js +14 -2
  44. package/dist/header-card-geometry.js.map +1 -1
  45. package/dist/header-logo.d.ts +28 -0
  46. package/dist/header-logo.d.ts.map +1 -0
  47. package/dist/header-logo.js +51 -0
  48. package/dist/header-logo.js.map +1 -0
  49. package/dist/i18n.d.ts +11 -0
  50. package/dist/i18n.d.ts.map +1 -1
  51. package/dist/i18n.js +11 -1
  52. package/dist/i18n.js.map +1 -1
  53. package/dist/index.d.ts +20 -8
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +17 -7
  56. package/dist/index.js.map +1 -1
  57. package/dist/inline-date-pin-geometry.d.ts +19 -11
  58. package/dist/inline-date-pin-geometry.d.ts.map +1 -1
  59. package/dist/inline-date-pin-geometry.js +45 -46
  60. package/dist/inline-date-pin-geometry.js.map +1 -1
  61. package/dist/item-bar-geometry.d.ts +192 -4
  62. package/dist/item-bar-geometry.d.ts.map +1 -1
  63. package/dist/item-bar-geometry.js +258 -5
  64. package/dist/item-bar-geometry.js.map +1 -1
  65. package/dist/item-port-geometry.d.ts +13 -0
  66. package/dist/item-port-geometry.d.ts.map +1 -0
  67. package/dist/item-port-geometry.js +63 -0
  68. package/dist/item-port-geometry.js.map +1 -0
  69. package/dist/layout-context.d.ts +115 -51
  70. package/dist/layout-context.d.ts.map +1 -1
  71. package/dist/layout-context.js +96 -5
  72. package/dist/layout-context.js.map +1 -1
  73. package/dist/layout-insights.d.ts.map +1 -1
  74. package/dist/layout-insights.js +73 -1
  75. package/dist/layout-insights.js.map +1 -1
  76. package/dist/layout.d.ts +35 -0
  77. package/dist/layout.d.ts.map +1 -1
  78. package/dist/layout.js +18 -1315
  79. package/dist/layout.js.map +1 -1
  80. package/dist/nodes/anchor-node.d.ts.map +1 -1
  81. package/dist/nodes/anchor-node.js +11 -4
  82. package/dist/nodes/anchor-node.js.map +1 -1
  83. package/dist/nodes/content-extent.d.ts +24 -0
  84. package/dist/nodes/content-extent.d.ts.map +1 -0
  85. package/dist/nodes/content-extent.js +100 -0
  86. package/dist/nodes/content-extent.js.map +1 -0
  87. package/dist/nodes/dependency-edges.d.ts +6 -0
  88. package/dist/nodes/dependency-edges.d.ts.map +1 -0
  89. package/dist/nodes/dependency-edges.js +148 -0
  90. package/dist/nodes/dependency-edges.js.map +1 -0
  91. package/dist/nodes/footnote-node.d.ts +24 -3
  92. package/dist/nodes/footnote-node.d.ts.map +1 -1
  93. package/dist/nodes/footnote-node.js +129 -25
  94. package/dist/nodes/footnote-node.js.map +1 -1
  95. package/dist/nodes/group-node.d.ts +10 -21
  96. package/dist/nodes/group-node.d.ts.map +1 -1
  97. package/dist/nodes/group-node.js +79 -152
  98. package/dist/nodes/group-node.js.map +1 -1
  99. package/dist/nodes/header-node.d.ts +56 -0
  100. package/dist/nodes/header-node.d.ts.map +1 -0
  101. package/dist/nodes/header-node.js +163 -0
  102. package/dist/nodes/header-node.js.map +1 -0
  103. package/dist/nodes/include-node.d.ts +22 -12
  104. package/dist/nodes/include-node.d.ts.map +1 -1
  105. package/dist/nodes/include-node.js +95 -62
  106. package/dist/nodes/include-node.js.map +1 -1
  107. package/dist/nodes/item-measure.d.ts +135 -0
  108. package/dist/nodes/item-measure.d.ts.map +1 -0
  109. package/dist/nodes/item-measure.js +514 -0
  110. package/dist/nodes/item-measure.js.map +1 -0
  111. package/dist/nodes/item-node.d.ts +162 -9
  112. package/dist/nodes/item-node.d.ts.map +1 -1
  113. package/dist/nodes/item-node.js +256 -69
  114. package/dist/nodes/item-node.js.map +1 -1
  115. package/dist/nodes/marker-band.d.ts +118 -0
  116. package/dist/nodes/marker-band.d.ts.map +1 -0
  117. package/dist/nodes/marker-band.js +303 -0
  118. package/dist/nodes/marker-band.js.map +1 -0
  119. package/dist/nodes/marker-geometry.js +1 -1
  120. package/dist/nodes/marker-geometry.js.map +1 -1
  121. package/dist/nodes/milestone-node.d.ts +112 -15
  122. package/dist/nodes/milestone-node.d.ts.map +1 -1
  123. package/dist/nodes/milestone-node.js +246 -95
  124. package/dist/nodes/milestone-node.js.map +1 -1
  125. package/dist/nodes/nowline-node.d.ts +8 -0
  126. package/dist/nodes/nowline-node.d.ts.map +1 -0
  127. package/dist/nodes/nowline-node.js +74 -0
  128. package/dist/nodes/nowline-node.js.map +1 -0
  129. package/dist/nodes/parallel-node.d.ts +7 -12
  130. package/dist/nodes/parallel-node.d.ts.map +1 -1
  131. package/dist/nodes/parallel-node.js +66 -30
  132. package/dist/nodes/parallel-node.js.map +1 -1
  133. package/dist/nodes/region-markers.d.ts +41 -0
  134. package/dist/nodes/region-markers.d.ts.map +1 -0
  135. package/dist/nodes/region-markers.js +130 -0
  136. package/dist/nodes/region-markers.js.map +1 -0
  137. package/dist/nodes/roadmap-node.d.ts +9 -72
  138. package/dist/nodes/roadmap-node.d.ts.map +1 -1
  139. package/dist/nodes/roadmap-node.js +38 -788
  140. package/dist/nodes/roadmap-node.js.map +1 -1
  141. package/dist/nodes/row-track.d.ts +33 -0
  142. package/dist/nodes/row-track.d.ts.map +1 -0
  143. package/dist/nodes/row-track.js +170 -0
  144. package/dist/nodes/row-track.js.map +1 -0
  145. package/dist/nodes/sequence-item.d.ts +25 -0
  146. package/dist/nodes/sequence-item.d.ts.map +1 -0
  147. package/dist/nodes/sequence-item.js +396 -0
  148. package/dist/nodes/sequence-item.js.map +1 -0
  149. package/dist/nodes/sequence-one.d.ts +5 -0
  150. package/dist/nodes/sequence-one.d.ts.map +1 -0
  151. package/dist/nodes/sequence-one.js +26 -0
  152. package/dist/nodes/sequence-one.js.map +1 -0
  153. package/dist/nodes/swimlane-node.d.ts +5 -17
  154. package/dist/nodes/swimlane-node.d.ts.map +1 -1
  155. package/dist/nodes/swimlane-node.js +62 -145
  156. package/dist/nodes/swimlane-node.js.map +1 -1
  157. package/dist/nodes/wave-node.d.ts +52 -0
  158. package/dist/nodes/wave-node.d.ts.map +1 -0
  159. package/dist/nodes/wave-node.js +483 -0
  160. package/dist/nodes/wave-node.js.map +1 -0
  161. package/dist/non-working-display.d.ts +14 -0
  162. package/dist/non-working-display.d.ts.map +1 -0
  163. package/dist/non-working-display.js +28 -0
  164. package/dist/non-working-display.js.map +1 -0
  165. package/dist/phases/date-window.d.ts +48 -0
  166. package/dist/phases/date-window.d.ts.map +1 -0
  167. package/dist/phases/date-window.js +130 -0
  168. package/dist/phases/date-window.js.map +1 -0
  169. package/dist/phases/finalize.d.ts +198 -0
  170. package/dist/phases/finalize.d.ts.map +1 -0
  171. package/dist/phases/finalize.js +1058 -0
  172. package/dist/phases/finalize.js.map +1 -0
  173. package/dist/phases/measure.d.ts +77 -0
  174. package/dist/phases/measure.d.ts.map +1 -0
  175. package/dist/phases/measure.js +222 -0
  176. package/dist/phases/measure.js.map +1 -0
  177. package/dist/phases/place.d.ts +28 -0
  178. package/dist/phases/place.d.ts.map +1 -0
  179. package/dist/phases/place.js +225 -0
  180. package/dist/phases/place.js.map +1 -0
  181. package/dist/renderable.d.ts.map +1 -1
  182. package/dist/renderable.js +23 -3
  183. package/dist/renderable.js.map +1 -1
  184. package/dist/resolve-today.d.ts +11 -1
  185. package/dist/resolve-today.d.ts.map +1 -1
  186. package/dist/resolve-today.js +26 -12
  187. package/dist/resolve-today.js.map +1 -1
  188. package/dist/row-packer.d.ts +42 -16
  189. package/dist/row-packer.d.ts.map +1 -1
  190. package/dist/row-packer.js +51 -78
  191. package/dist/row-packer.js.map +1 -1
  192. package/dist/schedule.d.ts +58 -1
  193. package/dist/schedule.d.ts.map +1 -1
  194. package/dist/schedule.js +280 -106
  195. package/dist/schedule.js.map +1 -1
  196. package/dist/start-rules.d.ts +27 -0
  197. package/dist/start-rules.d.ts.map +1 -0
  198. package/dist/start-rules.js +91 -0
  199. package/dist/start-rules.js.map +1 -0
  200. package/dist/start-terms.d.ts +23 -0
  201. package/dist/start-terms.d.ts.map +1 -0
  202. package/dist/start-terms.js +27 -0
  203. package/dist/start-terms.js.map +1 -0
  204. package/dist/status-dot.d.ts +46 -0
  205. package/dist/status-dot.d.ts.map +1 -0
  206. package/dist/status-dot.js +78 -0
  207. package/dist/status-dot.js.map +1 -0
  208. package/dist/style-resolution.d.ts.map +1 -1
  209. package/dist/style-resolution.js +23 -43
  210. package/dist/style-resolution.js.map +1 -1
  211. package/dist/text-measure.d.ts +28 -0
  212. package/dist/text-measure.d.ts.map +1 -0
  213. package/dist/text-measure.js +70 -0
  214. package/dist/text-measure.js.map +1 -0
  215. package/dist/themes/contrast.d.ts +4 -0
  216. package/dist/themes/contrast.d.ts.map +1 -0
  217. package/dist/themes/contrast.js +32 -0
  218. package/dist/themes/contrast.js.map +1 -0
  219. package/dist/themes/dark.d.ts.map +1 -1
  220. package/dist/themes/dark.js +28 -1
  221. package/dist/themes/dark.js.map +1 -1
  222. package/dist/themes/grayscale.d.ts.map +1 -1
  223. package/dist/themes/grayscale.js +28 -1
  224. package/dist/themes/grayscale.js.map +1 -1
  225. package/dist/themes/index.d.ts +4 -11
  226. package/dist/themes/index.d.ts.map +1 -1
  227. package/dist/themes/index.js +6 -4
  228. package/dist/themes/index.js.map +1 -1
  229. package/dist/themes/light.d.ts.map +1 -1
  230. package/dist/themes/light.js +28 -1
  231. package/dist/themes/light.js.map +1 -1
  232. package/dist/themes/shape.d.ts +31 -0
  233. package/dist/themes/shape.d.ts.map +1 -1
  234. package/dist/themes/shared.d.ts +44 -50
  235. package/dist/themes/shared.d.ts.map +1 -1
  236. package/dist/themes/shared.js +79 -83
  237. package/dist/themes/shared.js.map +1 -1
  238. package/dist/time-scale.d.ts +42 -6
  239. package/dist/time-scale.d.ts.map +1 -1
  240. package/dist/time-scale.js +109 -17
  241. package/dist/time-scale.js.map +1 -1
  242. package/dist/types.d.ts +528 -11
  243. package/dist/types.d.ts.map +1 -1
  244. package/dist/view-preset.d.ts +61 -6
  245. package/dist/view-preset.d.ts.map +1 -1
  246. package/dist/view-preset.js +439 -24
  247. package/dist/view-preset.js.map +1 -1
  248. package/dist/wave-barrier.d.ts +107 -0
  249. package/dist/wave-barrier.d.ts.map +1 -0
  250. package/dist/wave-barrier.js +174 -0
  251. package/dist/wave-barrier.js.map +1 -0
  252. package/dist/wave-hatch.d.ts +4 -0
  253. package/dist/wave-hatch.d.ts.map +1 -0
  254. package/dist/wave-hatch.js +21 -0
  255. package/dist/wave-hatch.js.map +1 -0
  256. package/dist/wave-layout.d.ts +99 -0
  257. package/dist/wave-layout.d.ts.map +1 -0
  258. package/dist/wave-layout.js +130 -0
  259. package/dist/wave-layout.js.map +1 -0
  260. package/dist/working-calendar.d.ts +128 -4
  261. package/dist/working-calendar.d.ts.map +1 -1
  262. package/dist/working-calendar.js +725 -51
  263. package/dist/working-calendar.js.map +1 -1
  264. package/dist/working-span.d.ts +11 -0
  265. package/dist/working-span.d.ts.map +1 -0
  266. package/dist/working-span.js +18 -0
  267. package/dist/working-span.js.map +1 -0
  268. package/package.json +4 -4
  269. package/src/axis-columns.ts +140 -0
  270. package/src/calendar-resolver.ts +161 -0
  271. package/src/calendar.ts +4 -45
  272. package/src/canvas-width.ts +36 -0
  273. package/src/capacity.ts +71 -30
  274. package/src/chart-translation.ts +88 -0
  275. package/src/container-header-geometry.ts +116 -0
  276. package/src/dsl-utils.ts +8 -26
  277. package/src/edge-routing.ts +347 -491
  278. package/src/frame-tab-geometry.ts +78 -25
  279. package/src/header-card-geometry.ts +17 -2
  280. package/src/header-logo.ts +67 -0
  281. package/src/i18n.ts +22 -1
  282. package/src/index.ts +97 -12
  283. package/src/inline-date-pin-geometry.ts +55 -58
  284. package/src/item-bar-geometry.ts +341 -5
  285. package/src/item-port-geometry.ts +69 -0
  286. package/src/layout-context.ts +210 -72
  287. package/src/layout-insights.ts +109 -2
  288. package/src/layout.ts +57 -1530
  289. package/src/nodes/anchor-node.ts +11 -4
  290. package/src/nodes/content-extent.ts +107 -0
  291. package/src/nodes/dependency-edges.ts +171 -0
  292. package/src/nodes/footnote-node.ts +171 -31
  293. package/src/nodes/group-node.ts +92 -214
  294. package/src/nodes/header-node.ts +255 -0
  295. package/src/nodes/include-node.ts +124 -110
  296. package/src/nodes/item-measure.ts +723 -0
  297. package/src/nodes/item-node.ts +358 -80
  298. package/src/nodes/marker-band.ts +406 -0
  299. package/src/nodes/marker-geometry.ts +1 -1
  300. package/src/nodes/milestone-node.ts +321 -136
  301. package/src/nodes/nowline-node.ts +87 -0
  302. package/src/nodes/parallel-node.ts +69 -38
  303. package/src/nodes/region-markers.ts +194 -0
  304. package/src/nodes/roadmap-node.ts +46 -950
  305. package/src/nodes/row-track.ts +217 -0
  306. package/src/nodes/sequence-item.ts +433 -0
  307. package/src/nodes/sequence-one.ts +40 -0
  308. package/src/nodes/swimlane-node.ts +66 -205
  309. package/src/nodes/wave-node.ts +609 -0
  310. package/src/non-working-display.ts +32 -0
  311. package/src/phases/date-window.ts +191 -0
  312. package/src/phases/finalize.ts +1303 -0
  313. package/src/phases/measure.ts +329 -0
  314. package/src/phases/place.ts +318 -0
  315. package/src/renderable.ts +23 -3
  316. package/src/resolve-today.ts +28 -25
  317. package/src/row-packer.ts +85 -84
  318. package/src/schedule.ts +362 -114
  319. package/src/start-rules.ts +113 -0
  320. package/src/start-terms.ts +38 -0
  321. package/src/status-dot.ts +81 -0
  322. package/src/style-resolution.ts +27 -57
  323. package/src/text-measure.ts +71 -0
  324. package/src/themes/contrast.ts +34 -0
  325. package/src/themes/dark.ts +28 -1
  326. package/src/themes/grayscale.ts +28 -1
  327. package/src/themes/index.ts +10 -7
  328. package/src/themes/light.ts +28 -1
  329. package/src/themes/shape.ts +38 -0
  330. package/src/themes/shared.ts +85 -93
  331. package/src/time-scale.ts +117 -19
  332. package/src/types.ts +529 -20
  333. package/src/view-preset.ts +520 -27
  334. package/src/wave-barrier.ts +242 -0
  335. package/src/wave-hatch.ts +25 -0
  336. package/src/wave-layout.ts +203 -0
  337. package/src/working-calendar.ts +875 -57
  338. package/src/working-span.ts +19 -0
@@ -1,550 +1,406 @@
1
- // Channel-based dependency-arrow router. Replaces the single-elbow
2
- // `routeEdge` shipped in m2g with one that:
1
+ // Date-port dependency router (specs/true-width-layout.md §3.4).
3
2
  //
4
- // - Drops the vertical leg in the cleanest inter-column gutter (item
5
- // bars are obstacles).
6
- // - Nudges the elbow X away from any visible parallel/group bracket
7
- // within `BRACKET_NUDGE_PX` so arrows breathe instead of hugging
8
- // the bracket line.
9
- // - Assigns distinct slots when multiple edges share a channel so
10
- // parallel arrows don't stack on top of one another.
11
- // - Falls back to under-bar routing (rendered behind the bars with a
12
- // thinner stroke) when no clean channel exists in the source-target
13
- // gap.
3
+ // An arrow runs on the dates. It leaves the middle of its source's last
4
+ // day through the source's bottom edge (top edge when the target is
5
+ // above), drops into a gap between rows, runs horizontally in that gap to
6
+ // the middle of the target's first day, and turns into the target's top
7
+ // edge (bottom edge when the source is below), arrowhead first. Day
8
+ // centres, never date lines: in a chain the successor starts exactly on
9
+ // its predecessor's end date, so a leg on the date line would run down the
10
+ // successor's edge.
14
11
  //
15
- // Containers are NOT treated as obstacles. Endpoints inside a parallel
16
- // or group route directly through the items-only obstacle map and use
17
- // the under-bar fallback when needed. Looping around container edges
18
- // to avoid a single intersecting bar produced unsatisfying detours
19
- // (see `specs/handoffs/handoff-channel-routing-design.md`).
12
+ // Legs pass behind bars and cross them at right angles. The horizontal leg
13
+ // sits in a gap no bar, spilled caption or container header reaches into
14
+ // (between the source and the target, over the leg's x-range); among the
15
+ // gaps, the router takes the one whose vertical legs keep clear of every
16
+ // bar edge, then the one whose vertical legs cross the fewest bars, then
17
+ // the one nearest the source. Arrows sharing a gap take offset lanes
18
+ // `CHANNEL_LANE_PX` apart; arrows landing in the same day column share one
19
+ // lane, so they merge into one trunk over their target (§8 O1).
20
+ //
21
+ // Two shapes need no gap: a target on its source's row (a dotted leg at
22
+ // the row's midline, or no arrow when it starts where the source ends),
23
+ // and a marker source (`dependency-edges.ts`). Everything here is pure
24
+ // geometry on numbers the caller reads off the final model.
25
+
26
+ import type { BoundingBox, Point } from './types.js';
27
+
28
+ /** Spacing between the lanes of one gap (§3.4 "Channels"). */
29
+ export const CHANNEL_LANE_PX = 2.5;
30
+
31
+ /** A vertical leg keeps more than this from a bar's left or right edge over its height (§6). */
32
+ export const EDGE_CLEARANCE_PX = 1;
33
+
34
+ /** Full arrowhead width (and the length along the leg is `ARROW_HEAD_LENGTH_PX`). */
35
+ export const ARROW_HEAD_WIDTH_PX = 7;
36
+
37
+ /** Length of a full arrowhead along its leg. */
38
+ export const ARROW_HEAD_LENGTH_PX = 5;
39
+
40
+ /** The narrowest arrowhead; a target narrower than this gets a dot of this diameter (§8 O6). */
41
+ export const ARROW_HEAD_MIN_PX = 3;
42
+
43
+ /** Two x's (or y's) closer than this are the same date line. */
44
+ const SAME_PX = 0.01;
45
+
46
+ /** A gap must be at least this tall to carry a leg. */
47
+ const MIN_GAP_PX = 2;
48
+
49
+ /** Clearance kept between a horizontal leg and anything it must not touch. */
50
+ const BLOCK_MARGIN_PX = 1;
51
+
52
+ /** One end of a dependency arrow: an item bar or a group / parallel. */
53
+ export interface ArrowEnd {
54
+ /** The entity's dates in x (an item's bar is drawn inside them, a seam in from each). */
55
+ startX: number;
56
+ endX: number;
57
+ /** Painted left edge and width (the bar, or the container's box). */
58
+ paintedX: number;
59
+ paintedWidth: number;
60
+ /** Painted top and bottom (a container's box runs from its header to its body's bottom). */
61
+ top: number;
62
+ bottom: number;
63
+ /** An item's row midline (`box.y + bandwidth / 2`); undefined for a container. */
64
+ rowY?: number;
65
+ /**
66
+ * Where a horizontal leg leaves this item on its own row: the row
67
+ * midline, or the progress strip's centre when its caption spills, so
68
+ * the leg runs under the caption. Undefined for a container.
69
+ */
70
+ exitY?: number;
71
+ }
20
72
 
21
- import type {
22
- BoundingBox,
23
- Point,
24
- PositionedIncludeRegion,
25
- PositionedSwimlane,
26
- PositionedTrackChild,
27
- } from './types.js';
73
+ export interface DependencyRouteRequest {
74
+ fromId: string;
75
+ toId: string;
76
+ source: ArrowEnd;
77
+ target: ArrowEnd;
78
+ }
28
79
 
29
- /** A visible parallel or group bracket stroke. Used by the
30
- * bracket-clearance nudge: arrows whose chosen elbow X falls within
31
- * `BRACKET_NUDGE_PX` of one of these lines are shifted away. Brackets
32
- * are NOT obstacles — they're aesthetic preferences, not collisions. */
33
- export interface BracketLine {
80
+ /** A vertical stroke a vertical leg must keep clear of. */
81
+ export interface VerticalEdge {
34
82
  x: number;
35
- yTop: number;
36
- yBottom: number;
83
+ top: number;
84
+ bottom: number;
37
85
  }
38
86
 
39
- /** Per-edge routing request. `from` and `to` are the (visualEdge, midY)
40
- * attach points the rest of the layout already computed; the router
41
- * produces the orthogonal polyline that connects them. */
42
- export interface EdgeRouteRequest {
43
- fromId: string;
44
- toId: string;
45
- from: Point;
46
- to: Point;
47
- /** Marker → item edges (anchor / milestone source) skip the channel
48
- * router entirely — the cut line is the visible stem, the path is
49
- * always a short horizontal stub. Set true to bypass routing. */
50
- isMarkerSource: boolean;
87
+ export interface RouteObstacles {
88
+ /**
89
+ * What a horizontal leg must stay out of: every bar with its spilled
90
+ * caption and decorations, every container header band, and every lane
91
+ * or region edge (zero-height boxes).
92
+ */
93
+ blocks: BoundingBox[];
94
+ /** The left and right edges of every bar and drawn container, over its height. */
95
+ edges: VerticalEdge[];
51
96
  }
52
97
 
53
- export interface EdgeRouteResult {
98
+ export interface RoutedDependency {
54
99
  fromId: string;
55
100
  toId: string;
101
+ /** First = source port, last = target port (the arrowhead's tip). */
56
102
  waypoints: Point[];
57
- /** True when the chosen channel intersected an item bar. The
58
- * renderer paints these edges BEFORE bar fills with a thinner
59
- * stroke so the bar still reads as the foreground. */
60
- underBar: boolean;
103
+ /** The target waits: its horizontal leg is dotted, its length the slack. */
104
+ slack: boolean;
105
+ headWidth: number;
106
+ head: 'arrow' | 'dot';
61
107
  }
62
108
 
63
- /** Distance (px) the elbow X must keep from any visible bracket. Picked
64
- * to leave a small visible gap between the arrow's vertical leg and
65
- * the bracket stroke without forcing the arrow far from its natural
66
- * mid-gutter line. */
67
- export const BRACKET_NUDGE_PX = 4;
68
-
69
- /** Minimum horizontal source stub (px) — distance from the source's
70
- * exit point to the vertical-leg elbow. Below this, the source-side
71
- * arrow body collapses into the bar's right edge with no visible
72
- * horizontal segment. Treated as a hard constraint when picking
73
- * the channel X for left-to-right edges. */
74
- export const MIN_SOURCE_STUB_PX = 6;
75
-
76
- /** Minimum horizontal target stub (px) — distance from the vertical-leg
77
- * elbow to the target's left edge. Below this, the arrowhead has no
78
- * horizontal lead-in: the leg appears to plunge directly into the bar
79
- * without a visible target-side stub. Treated as a hard constraint
80
- * when picking the channel X for left-to-right edges; conflicts with
81
- * obstacle / bracket clearance trigger the under-bar fallback. */
82
- export const MIN_TARGET_STUB_PX = 6;
109
+ /** Where an arrow leaves `end`: the middle of its last day, or its middle when shorter than a day. */
110
+ export function sourcePortX(end: ArrowEnd, pixelsPerDay: number): number {
111
+ return end.endX - Math.min(pixelsPerDay / 2, (end.endX - end.startX) / 2);
112
+ }
83
113
 
84
- /** Spacing between slots inside one channel. With a 12 px gutter and
85
- * this spacing, three slots (-1, 0, +1) fit comfortably; more than
86
- * three sharing a channel collapses to centerline. */
87
- const SLOT_SPACING_PX = 3;
114
+ /** Where an arrow lands on `end`: the middle of its first day, or its middle when shorter than a day. */
115
+ export function targetPortX(end: ArrowEnd, pixelsPerDay: number): number {
116
+ return end.startX + Math.min(pixelsPerDay / 2, (end.endX - end.startX) / 2);
117
+ }
88
118
 
89
- /** Tolerance (px) when grouping edges by channel X for slot assignment.
90
- * Edges within this distance share a channel. */
91
- const SLOT_GROUP_TOLERANCE_PX = 1;
119
+ /** Arrowhead for a leg landing across a target `width` wide (§8 O6). */
120
+ export function arrowHead(width: number): { headWidth: number; head: 'arrow' | 'dot' } {
121
+ if (width < ARROW_HEAD_MIN_PX) return { headWidth: ARROW_HEAD_MIN_PX, head: 'dot' };
122
+ return { headWidth: Math.min(ARROW_HEAD_WIDTH_PX, width), head: 'arrow' };
123
+ }
92
124
 
93
- /** Stub length leaving each endpoint before the elbow turn for
94
- * right-to-left edges. The router also inserts a quarter-arc at the
95
- * elbow corner; the renderer's `roundedOrthogonalPath` rounds the
96
- * corners. Left-to-right edges use the per-side `MIN_*_STUB_PX`
97
- * constants instead — they provide tighter, asymmetric control over
98
- * the source vs target sides. */
99
- const STUB_OUT_PX = 10;
125
+ const within = (y: number, lo: number, hi: number): boolean =>
126
+ y >= lo - SAME_PX && y <= hi + SAME_PX;
100
127
 
101
- export interface ChannelGridInput {
102
- /** Every painted item box (visual edges, including chip-spill
103
- * growth). Treated as hard obstacles: a vertical leg crossing one
104
- * triggers under-bar fallback. */
105
- itemBars: BoundingBox[];
106
- /** Every visible bracket stroke (parallel `bracket:solid|dashed`,
107
- * filled-style group chiclets are NOT brackets). Used by the
108
- * clearance nudge only — not obstacles. */
109
- brackets: BracketLine[];
128
+ /**
129
+ * True when the target sits on its source's row: each one's row midline
130
+ * crosses the other (a container source, which has no row, only needs the
131
+ * target's midline to cross it).
132
+ */
133
+ function onSameRow(s: ArrowEnd, t: ArrowEnd): boolean {
134
+ if (t.rowY === undefined || !within(t.rowY, s.top, s.bottom)) return false;
135
+ return s.rowY === undefined || within(s.rowY, t.top, t.bottom);
110
136
  }
111
137
 
112
- export class ChannelGrid {
113
- private readonly items: BoundingBox[];
114
- private readonly brackets: BracketLine[];
115
-
116
- constructor(input: ChannelGridInput) {
117
- this.items = input.itemBars;
118
- this.brackets = input.brackets;
138
+ /** The free stretches of `(lo, hi)` no block over `[xLo, xHi]` reaches into. */
139
+ function freeGaps(
140
+ blocks: BoundingBox[],
141
+ xLo: number,
142
+ xHi: number,
143
+ lo: number,
144
+ hi: number,
145
+ ): Array<[number, number]> {
146
+ const covered: Array<[number, number]> = [];
147
+ for (const b of blocks) {
148
+ if (b.x >= xHi + BLOCK_MARGIN_PX || b.x + b.width <= xLo - BLOCK_MARGIN_PX) continue;
149
+ const top = b.y - BLOCK_MARGIN_PX;
150
+ const bottom = b.y + b.height + BLOCK_MARGIN_PX;
151
+ if (bottom <= lo || top >= hi) continue;
152
+ covered.push([Math.max(lo, top), Math.min(hi, bottom)]);
119
153
  }
120
-
121
- /** True when a vertical line at `x` intersects any item bar within
122
- * the Y span `[yMin, yMax]`. */
123
- hasObstacle(x: number, yMin: number, yMax: number): boolean {
124
- const lo = Math.min(yMin, yMax);
125
- const hi = Math.max(yMin, yMax);
126
- for (const bar of this.items) {
127
- if (x <= bar.x || x >= bar.x + bar.width) continue;
128
- if (bar.y + bar.height <= lo || bar.y >= hi) continue;
129
- return true;
130
- }
131
- return false;
154
+ covered.sort((a, b) => a[0] - b[0]);
155
+ const gaps: Array<[number, number]> = [];
156
+ let y = lo;
157
+ for (const [top, bottom] of covered) {
158
+ if (top - y >= MIN_GAP_PX) gaps.push([y, top]);
159
+ y = Math.max(y, bottom);
132
160
  }
161
+ if (hi - y >= MIN_GAP_PX) gaps.push([y, hi]);
162
+ return gaps;
163
+ }
133
164
 
134
- /** Visible brackets within `radius` px of `x` whose Y span overlaps
135
- * `[yMin, yMax]`. The clearance nudge uses these to shift the
136
- * elbow X away from the bracket. */
137
- bracketsNear(x: number, yMin: number, yMax: number, radius: number): BracketLine[] {
138
- const lo = Math.min(yMin, yMax);
139
- const hi = Math.max(yMin, yMax);
140
- const out: BracketLine[] = [];
141
- for (const b of this.brackets) {
142
- if (Math.abs(b.x - x) > radius) continue;
143
- if (b.yBottom <= lo || b.yTop >= hi) continue;
144
- out.push(b);
145
- }
146
- return out;
165
+ /** Bar or container edges a vertical leg at `x` over `(y0, y1)` runs within the clearance of. */
166
+ function grazes(edges: VerticalEdge[], x: number, y0: number, y1: number): number {
167
+ const lo = Math.min(y0, y1);
168
+ const hi = Math.max(y0, y1);
169
+ let n = 0;
170
+ for (const e of edges) {
171
+ if (Math.abs(e.x - x) > EDGE_CLEARANCE_PX) continue;
172
+ if (e.bottom <= lo || e.top >= hi) continue;
173
+ n++;
147
174
  }
175
+ return n;
148
176
  }
149
177
 
150
- /**
151
- * Walk a positioned swimlane tree and collect every painted item bar
152
- * (visual edges) AND every visible bracket stroke. Output feeds
153
- * `ChannelGrid` so the router knows what to avoid.
154
- *
155
- * Swimlanes contained in include regions count too — items inside an
156
- * isolated region share the parent timeline so cross-region arrows
157
- * still need the obstacle/bracket data to route cleanly.
158
- */
159
- export function collectRoutingObstacles(
160
- swimlanes: PositionedSwimlane[],
161
- includes: PositionedIncludeRegion[],
162
- ): ChannelGridInput {
163
- const itemBars: BoundingBox[] = [];
164
- const brackets: BracketLine[] = [];
165
-
166
- const visitChild = (child: PositionedTrackChild): void => {
167
- if (child.kind === 'item') {
168
- itemBars.push(child.box);
169
- return;
170
- }
171
- if (child.kind === 'parallel') {
172
- // Parallel `bracket:solid|dashed` paints `[ ]` strokes at
173
- // the box's left/right edges with 12 px vertical padding
174
- // (matches `renderParallel`). The bracket has THREE
175
- // strokes per side — a vertical bar at the box edge, plus
176
- // top/bottom horizontal "feet" extending 4 px inward
177
- // toward the parallel's center. We model the verticals as
178
- // full-height bracket lines AND emit thin-Y-band entries
179
- // at the foot tips so the clearance nudge sees the foot
180
- // extent (otherwise an elbow nudged just past the
181
- // vertical at +4 px would land squarely on the inward
182
- // foot tip — see issue #2 in the channel-routing handoff).
183
- if (child.style.bracket === 'solid' || child.style.bracket === 'dashed') {
184
- const padding = 12;
185
- const stub = 4;
186
- const yTop = child.box.y - padding;
187
- const yBottom = child.box.y + child.box.height + padding;
188
- const lx = child.box.x;
189
- const rx = child.box.x + child.box.width;
190
- brackets.push({ x: lx, yTop, yBottom });
191
- brackets.push({ x: rx, yTop, yBottom });
192
- // Foot tips: tiny Y bands centered on each foot row
193
- // so the entries only fire when the elbow's Y span
194
- // actually crosses the foot line.
195
- brackets.push({ x: lx + stub, yTop: yTop - 1, yBottom: yTop + 1 });
196
- brackets.push({ x: lx + stub, yTop: yBottom - 1, yBottom: yBottom + 1 });
197
- brackets.push({ x: rx - stub, yTop: yTop - 1, yBottom: yTop + 1 });
198
- brackets.push({ x: rx - stub, yTop: yBottom - 1, yBottom: yBottom + 1 });
199
- }
200
- for (const sub of child.children) visitChild(sub);
201
- return;
202
- }
203
- if (child.kind === 'group') {
204
- // Bracket-style groups (no fill) paint a left-side `[`
205
- // glyph along `box.x`. Filled-style groups have no bracket
206
- // — the chiclet is the visual instead.
207
- const isFilled = child.style.bg !== 'none' && child.style.bg !== '#ffffff';
208
- if (!isFilled && child.style.bracket && child.style.bracket !== 'none') {
209
- brackets.push({
210
- x: child.box.x,
211
- yTop: child.box.y,
212
- yBottom: child.box.y + child.box.height,
213
- });
214
- }
215
- for (const sub of child.children) visitChild(sub);
216
- return;
217
- }
218
- };
219
-
220
- const visitSwimlane = (lane: PositionedSwimlane): void => {
221
- for (const child of lane.children) visitChild(child);
222
- for (const nested of lane.nested) visitSwimlane(nested);
223
- };
224
-
225
- for (const lane of swimlanes) visitSwimlane(lane);
226
- for (const region of includes) {
227
- for (const lane of region.nestedSwimlanes) visitSwimlane(lane);
178
+ /** Blocks a vertical leg at `x` over `(y0, y1)` passes behind. */
179
+ function crossings(blocks: BoundingBox[], x: number, y0: number, y1: number): number {
180
+ const lo = Math.min(y0, y1);
181
+ const hi = Math.max(y0, y1);
182
+ let n = 0;
183
+ for (const b of blocks) {
184
+ if (b.height <= 0 || x <= b.x || x >= b.x + b.width) continue;
185
+ if (b.y + b.height <= lo || b.y >= hi) continue;
186
+ n++;
228
187
  }
229
-
230
- return { itemBars, brackets };
188
+ return n;
231
189
  }
232
190
 
233
- /** Satisfiable range for a left-to-right edge's elbow X — tightest
234
- * band that still leaves `MIN_SOURCE_STUB_PX` of horizontal lead-out
235
- * on the source side and `MIN_TARGET_STUB_PX` of horizontal lead-in
236
- * on the target side. `null` means the gutter is too narrow to honor
237
- * both stubs (router pins to the target-stub line and forces
238
- * under-bar). For right-to-left and same-row edges the range is
239
- * always `null` — those branches don't apply the stub constraints
240
- * because their geometry is fundamentally different. */
241
- type StubRange = { minX: number; maxX: number } | null;
191
+ /** An arrow that needs a gap, before its lane is chosen. */
192
+ interface Pending {
193
+ index: number;
194
+ req: DependencyRouteRequest;
195
+ from: Point;
196
+ to: Point;
197
+ /** +1 when the leg runs down from the source, -1 when up. */
198
+ dir: 1 | -1;
199
+ gap: [number, number];
200
+ centre: number;
201
+ xLo: number;
202
+ xHi: number;
203
+ /** Arrows landing in one day column share a lane (O1). */
204
+ bundle: string;
205
+ }
242
206
 
243
207
  /**
244
- * Pick a channel X for one edge (no slot offset yet). Returns the
245
- * provisional X, an `underBar` flag, and the satisfiable stub range
246
- * so the bracket-clearance nudge can stay inside it. Same-row edges
247
- * (`from.y ≈ to.y`) get a degenerate "channel" at midX so the
248
- * renderer emits a straight line.
208
+ * Pick the gap for an arrow from `from` (on the source's edge) to `to` (on
209
+ * the target's edge): clear of every bar edge first, then fewest bars
210
+ * crossed, then nearest the source. With no free gap at all, the leg runs
211
+ * just past the source and passes behind whatever is there.
249
212
  */
250
- function pickChannelX(
251
- req: EdgeRouteRequest,
252
- grid: ChannelGrid,
253
- ): { x: number; underBar: boolean; range: StubRange } {
254
- const { from, to } = req;
255
- if (Math.abs(from.y - to.y) < 0.5) {
256
- return { x: (from.x + to.x) / 2, underBar: false, range: null };
257
- }
258
-
259
- const yMin = Math.min(from.y, to.y);
260
- const yMax = Math.max(from.y, to.y);
261
-
262
- if (from.x <= to.x) {
263
- // Left-to-right edge. The elbow X must satisfy both the
264
- // min-source-stub and min-target-stub constraints — when the
265
- // gutter is wider than the sum of the stubs, that's a band;
266
- // when it's tighter the band collapses or inverts and the
267
- // arrow drops to under-bar at the target-stub line.
268
- const minX = from.x + MIN_SOURCE_STUB_PX;
269
- const maxX = to.x - MIN_TARGET_STUB_PX;
270
- if (minX > maxX) {
271
- // Gutter narrower than the combined min stubs — give up
272
- // and honor the target-stub line so the arrowhead still
273
- // has its visible lead-in. Source stub absorbs the loss.
274
- return {
275
- x: to.x - MIN_TARGET_STUB_PX,
276
- underBar: true,
277
- range: null,
278
- };
279
- }
280
- const range: StubRange = { minX, maxX };
281
- // Try the natural gutter midpoint, clamped into the range.
282
- const naturalMid = (from.x + to.x) / 2;
283
- const mid = Math.max(minX, Math.min(maxX, naturalMid));
284
- if (!grid.hasObstacle(mid, yMin, yMax)) {
285
- return { x: mid, underBar: false, range };
286
- }
287
- // Walk inside the satisfiable range looking for a clear strip.
288
- const span = maxX - minX;
289
- for (let step = 1; step <= span; step++) {
290
- const left = mid - step;
291
- if (left >= minX && !grid.hasObstacle(left, yMin, yMax)) {
292
- return { x: left, underBar: false, range };
293
- }
294
- const right = mid + step;
295
- if (right <= maxX && !grid.hasObstacle(right, yMin, yMax)) {
296
- return { x: right, underBar: false, range };
297
- }
298
- }
299
- return { x: mid, underBar: true, range };
300
- }
301
-
302
- // Right-to-left edge: source ends past target's left edge. Try a
303
- // channel just right of source first; then walk leftward across
304
- // the source/target span looking for any clear vertical strip.
305
- const probes = [from.x + STUB_OUT_PX, to.x - STUB_OUT_PX];
306
- for (const probe of probes) {
307
- if (probe > 0 && !grid.hasObstacle(probe, yMin, yMax)) {
308
- return { x: probe, underBar: false, range: null };
309
- }
213
+ function pickGap(
214
+ from: Point,
215
+ to: Point,
216
+ dir: 1 | -1,
217
+ obstacles: RouteObstacles,
218
+ ): { gap: [number, number]; centre: number } {
219
+ const xLo = Math.min(from.x, to.x);
220
+ const xHi = Math.max(from.x, to.x);
221
+ const lo = Math.min(from.y, to.y);
222
+ const hi = Math.max(from.y, to.y);
223
+ let best: { gap: [number, number]; centre: number; key: number[] } | null = null;
224
+ for (const gap of freeGaps(obstacles.blocks, xLo, xHi, lo, hi)) {
225
+ const centre = (gap[0] + gap[1]) / 2;
226
+ const key = [
227
+ grazes(obstacles.edges, from.x, from.y, centre) +
228
+ grazes(obstacles.edges, to.x, centre, to.y),
229
+ crossings(obstacles.blocks, from.x, from.y, centre) +
230
+ crossings(obstacles.blocks, to.x, centre, to.y),
231
+ Math.abs(centre - from.y),
232
+ ];
233
+ if (!best || lessThan(key, best.key)) best = { gap, centre, key };
310
234
  }
311
- // Fallback to under-bar at the source-side stub.
312
- return { x: from.x + STUB_OUT_PX, underBar: true, range: null };
235
+ if (best) return { gap: best.gap, centre: best.centre };
236
+ const reach = Math.min(4, (hi - lo) / 2);
237
+ const centre = from.y + dir * reach;
238
+ return { gap: [lo, hi], centre };
313
239
  }
314
240
 
315
- /**
316
- * Apply the bracket-clearance nudge to a chosen channel X. Shifts `x`
317
- * away from the nearest visible bracket within `BRACKET_NUDGE_PX`,
318
- * preferring the direction that stays inside the satisfiable stub
319
- * range. When neither direction fits — or the candidate position is
320
- * still within nudge distance of another bracket — returns the input
321
- * X clamped to the range and signals `forceUnderBar` so the leg paints
322
- * BEHIND the bars and bracket strokes (z-order: bracket renders after
323
- * under-bar edges, so the bracket cleanly covers the colliding
324
- * portion).
325
- */
326
- function nudgeAwayFromBrackets(
327
- x: number,
328
- range: StubRange,
329
- req: EdgeRouteRequest,
330
- grid: ChannelGrid,
331
- ): { x: number; forceUnderBar: boolean } {
332
- const yMin = Math.min(req.from.y, req.to.y);
333
- const yMax = Math.max(req.from.y, req.to.y);
334
- const near = grid.bracketsNear(x, yMin, yMax, BRACKET_NUDGE_PX);
335
- if (near.length === 0) return { x, forceUnderBar: false };
336
-
337
- // Find the closest bracket — that's the one driving the nudge.
338
- let closest = near[0];
339
- let closestDist = Math.abs(near[0].x - x);
340
- for (let i = 1; i < near.length; i++) {
341
- const d = Math.abs(near[i].x - x);
342
- if (d < closestDist) {
343
- closest = near[i];
344
- closestDist = d;
345
- }
241
+ function lessThan(a: number[], b: number[]): boolean {
242
+ for (let i = 0; i < a.length; i++) {
243
+ if (a[i] !== b[i]) return a[i] < b[i];
346
244
  }
245
+ return false;
246
+ }
347
247
 
348
- // Try both sides of the closest bracket. Accept the first
349
- // candidate that stays within the satisfiable range AND clears
350
- // every other nearby bracket. The recheck radius is one epsilon
351
- // tighter than `BRACKET_NUDGE_PX` so the closest bracket itself
352
- // (now sitting at exactly the nudge distance) doesn't falsely
353
- // trip the rejection — and so a SECOND bracket exactly at the
354
- // nudge distance is also accepted as "just clear enough".
355
- const candidates = [closest.x + BRACKET_NUDGE_PX, closest.x - BRACKET_NUDGE_PX];
356
- const recheckRadius = BRACKET_NUDGE_PX - 0.01;
357
- for (const candidate of candidates) {
358
- if (range && (candidate < range.minX || candidate > range.maxX)) continue;
359
- // Clamp right-to-left candidates to the natural gap so they
360
- // don't escape past either endpoint (range is null on that path).
361
- const clamped = range
362
- ? candidate
363
- : Math.max(
364
- Math.min(req.from.x, req.to.x) + 1,
365
- Math.min(Math.max(req.from.x, req.to.x) - 1, candidate),
366
- );
367
- const stillNear = grid.bracketsNear(clamped, yMin, yMax, recheckRadius);
368
- if (stillNear.length === 0) {
369
- return { x: clamped, forceUnderBar: false };
370
- }
248
+ /** Lane offsets to try, the first one toward the source (so the arrowhead keeps its leg). */
249
+ function laneOffsets(dir: 1 | -1, halfHeight: number): number[] {
250
+ const out = [0];
251
+ for (let k = 1; k * CHANNEL_LANE_PX <= halfHeight + SAME_PX; k++) {
252
+ out.push(-dir * k * CHANNEL_LANE_PX, dir * k * CHANNEL_LANE_PX);
371
253
  }
372
-
373
- // No nudge fits. Keep the channel at its current X (clamped into
374
- // the range) and force under-bar so item bars + bracket strokes
375
- // mask the colliding portion of the leg.
376
- const fallback = range ? Math.min(Math.max(x, range.minX), range.maxX) : x;
377
- return { x: fallback, forceUnderBar: true };
254
+ return out;
378
255
  }
379
256
 
380
257
  /**
381
- * Greedy interval coloring per channel. Edges sharing a channel
382
- * (within `SLOT_GROUP_TOLERANCE_PX`) get distinct slot indices;
383
- * overlapping Y spans land on different slots. Slot 0 is the
384
- * centerline; ±1 sit `SLOT_SPACING_PX` to either side; further slots
385
- * collapse back to the centerline (rare; visual stacking accepted).
258
+ * Give each gap-routed arrow its lane: an arrow whose day column already
259
+ * has a lane in this gap joins it; otherwise the first offset no other
260
+ * horizontal leg over the same x's sits on, or, when the gap is full, the
261
+ * least crowded one.
386
262
  */
387
- function assignSlots(
388
- edges: Array<{
389
- req: EdgeRouteRequest;
390
- channelX: number;
391
- underBar: boolean;
392
- orderIndex: number;
393
- }>,
394
- ): Map<number, number> {
395
- // Group by channel X (sort first so close-X edges collapse).
396
- const byX = [...edges].sort((a, b) => {
397
- if (Math.abs(a.channelX - b.channelX) >= SLOT_GROUP_TOLERANCE_PX) {
398
- return a.channelX - b.channelX;
399
- }
400
- // Within the same channel, deterministic by orderIndex.
401
- return a.orderIndex - b.orderIndex;
402
- });
403
-
404
- const slotsByOrderIndex = new Map<number, number>();
405
- let groupStart = 0;
406
- while (groupStart < byX.length) {
407
- let groupEnd = groupStart + 1;
408
- while (
409
- groupEnd < byX.length &&
410
- Math.abs(byX[groupEnd].channelX - byX[groupStart].channelX) < SLOT_GROUP_TOLERANCE_PX
411
- ) {
412
- groupEnd++;
413
- }
414
- const group = byX.slice(groupStart, groupEnd);
415
- // Within the group, greedy color by Y span. Sort by min Y then
416
- // (deterministically) by orderIndex.
417
- const sortedGroup = [...group].sort((a, b) => {
418
- const aMin = Math.min(a.req.from.y, a.req.to.y);
419
- const bMin = Math.min(b.req.from.y, b.req.to.y);
420
- if (Math.abs(aMin - bMin) > 0.5) return aMin - bMin;
421
- return a.orderIndex - b.orderIndex;
422
- });
423
- // Track per-slot maxY assigned so far.
424
- const slotMaxY: number[] = [];
425
- for (const entry of sortedGroup) {
426
- const eMin = Math.min(entry.req.from.y, entry.req.to.y);
427
- const eMax = Math.max(entry.req.from.y, entry.req.to.y);
428
- let assigned = -1;
429
- for (let s = 0; s < slotMaxY.length; s++) {
430
- if (slotMaxY[s] <= eMin) {
431
- assigned = s;
432
- slotMaxY[s] = eMax;
433
- break;
263
+ function assignLanes(pending: Pending[]): Map<number, number> {
264
+ const placed: Array<{ y: number; xLo: number; xHi: number; bundle: string }> = [];
265
+ const out = new Map<number, number>();
266
+ const order = [...pending].sort(
267
+ (a, b) => a.centre - b.centre || a.xLo - b.xLo || a.index - b.index,
268
+ );
269
+ for (const p of order) {
270
+ const [lo, hi] = p.gap;
271
+ const mate = placed.find(
272
+ (q) => q.bundle === p.bundle && q.y >= lo - SAME_PX && q.y <= hi + SAME_PX,
273
+ );
274
+ let y = p.centre;
275
+ if (mate) {
276
+ y = mate.y;
277
+ } else {
278
+ // The gap already keeps `BLOCK_MARGIN_PX` from its neighbours.
279
+ const offsets = laneOffsets(p.dir, (hi - lo) / 2);
280
+ let bestConflicts = Number.POSITIVE_INFINITY;
281
+ for (const off of offsets) {
282
+ const cy = p.centre + off;
283
+ const conflicts = placed.filter(
284
+ (q) =>
285
+ q.bundle !== p.bundle &&
286
+ Math.abs(q.y - cy) < CHANNEL_LANE_PX - SAME_PX &&
287
+ q.xLo < p.xHi + BLOCK_MARGIN_PX &&
288
+ q.xHi > p.xLo - BLOCK_MARGIN_PX,
289
+ ).length;
290
+ if (conflicts < bestConflicts) {
291
+ bestConflicts = conflicts;
292
+ y = cy;
434
293
  }
294
+ if (conflicts === 0) break;
435
295
  }
436
- if (assigned === -1) {
437
- assigned = slotMaxY.length;
438
- slotMaxY.push(eMax);
439
- }
440
- slotsByOrderIndex.set(entry.orderIndex, assigned);
441
296
  }
442
- groupStart = groupEnd;
297
+ placed.push({ y, xLo: p.xLo, xHi: p.xHi, bundle: p.bundle });
298
+ out.set(p.index, y);
443
299
  }
444
- return slotsByOrderIndex;
445
- }
446
-
447
- /** Convert a slot index to its signed offset around the channel
448
- * centerline. Slots 0/1/2/... become 0, +SLOT, -SLOT, +2*SLOT,
449
- * -2*SLOT... so the FIRST edge sits on the centerline (no shift)
450
- * and additional edges fan out alternately. Past 2 ± slots, slots
451
- * collapse back to the centerline (visual stacking accepted). */
452
- function slotOffset(slot: number): number {
453
- if (slot === 0) return 0;
454
- if (slot > 4) return 0; // collapse beyond ±2 slots
455
- const half = Math.ceil(slot / 2);
456
- const sign = slot % 2 === 1 ? 1 : -1;
457
- return sign * half * SLOT_SPACING_PX;
458
- }
459
-
460
- /**
461
- * Marker → item stub. Source X sits on the marker's vertical cut
462
- * line; the stub is a short horizontal segment to the target's left
463
- * visual edge at the target's row mid-Y. No channel selection, no
464
- * obstacle check — the cut line is the stem.
465
- */
466
- function routeMarkerStub(req: EdgeRouteRequest): EdgeRouteResult {
467
- // When the cut line sits AT or PAST the target's left edge (item
468
- // hugs the anchor's date column), nudge the source 1 px back so
469
- // routeEdge produces a non-degenerate path with a visible
470
- // arrowhead.
471
- const from = req.from.x >= req.to.x ? { x: req.to.x - 1, y: req.from.y } : req.from;
472
- return {
473
- fromId: req.fromId,
474
- toId: req.toId,
475
- waypoints: [from, req.to],
476
- underBar: false,
477
- };
300
+ return out;
478
301
  }
479
302
 
480
303
  /**
481
- * Route every dependency edge in one batch so slot assignment can
482
- * coordinate across edges that share a channel. Marker → item edges
483
- * route as direct stubs and bypass the channel router.
304
+ * Route every item dependency of one chart (the host, or one isolated
305
+ * region) in a batch, so arrows sharing a gap can take lanes. Returns one
306
+ * entry per request; `null` where position already says it (a target on
307
+ * its source's row that starts where the source ends).
484
308
  */
485
- export function routeChannelEdges(
486
- requests: EdgeRouteRequest[],
487
- grid: ChannelGrid,
488
- ): EdgeRouteResult[] {
489
- const results: EdgeRouteResult[] = new Array(requests.length);
490
- const channelEdges: Array<{
491
- req: EdgeRouteRequest;
492
- channelX: number;
493
- underBar: boolean;
494
- orderIndex: number;
495
- }> = [];
496
-
497
- for (let i = 0; i < requests.length; i++) {
498
- const req = requests[i];
499
- if (req.isMarkerSource) {
500
- results[i] = routeMarkerStub(req);
501
- continue;
309
+ export function routeDependencies(
310
+ requests: DependencyRouteRequest[],
311
+ obstacles: RouteObstacles,
312
+ pixelsPerDay: number,
313
+ ): Array<RoutedDependency | null> {
314
+ const out: Array<RoutedDependency | null> = new Array(requests.length).fill(null);
315
+ const pending: Pending[] = [];
316
+ requests.forEach((req, index) => {
317
+ const { source: s, target: t } = req;
318
+ const slack = t.startX - s.endX > SAME_PX;
319
+ if (onSameRow(s, t) && t.startX >= s.endX - SAME_PX) {
320
+ if (!slack) return;
321
+ // A dotted leg along the row, from the source's end date to the
322
+ // target's painted start.
323
+ const y =
324
+ s.exitY !== undefined && within(s.exitY, t.top, t.bottom) ? s.exitY : (t.rowY ?? 0);
325
+ out[index] = {
326
+ fromId: req.fromId,
327
+ toId: req.toId,
328
+ waypoints: [
329
+ { x: s.endX, y },
330
+ { x: t.paintedX, y },
331
+ ],
332
+ slack: true,
333
+ ...arrowHead(t.bottom - t.top),
334
+ };
335
+ return;
502
336
  }
503
- // Same-row contiguous chains drop entirely. The caller skips
504
- // them before passing in, but defend against direct calls.
505
- if (Math.abs(req.from.y - req.to.y) < 0.5 && req.to.x - req.from.x < 20) {
506
- results[i] = {
337
+ const px = sourcePortX(s, pixelsPerDay);
338
+ const tx = targetPortX(t, pixelsPerDay);
339
+ let from: Point;
340
+ let to: Point;
341
+ let dir: 1 | -1;
342
+ if (t.top >= s.bottom - SAME_PX) {
343
+ dir = 1;
344
+ from = { x: px, y: s.bottom };
345
+ to = { x: tx, y: t.top };
346
+ } else if (t.bottom <= s.top + SAME_PX) {
347
+ dir = -1;
348
+ from = { x: px, y: s.top };
349
+ to = { x: tx, y: t.bottom };
350
+ } else {
351
+ // Overlapping but not on one row (a target that starts before
352
+ // its source ends): leave the source's bottom, run below both,
353
+ // come up into the target's bottom.
354
+ const below = Math.max(s.bottom, t.bottom) + 4;
355
+ out[index] = {
507
356
  fromId: req.fromId,
508
357
  toId: req.toId,
509
- waypoints: [req.from, req.to],
510
- underBar: false,
358
+ waypoints: [
359
+ { x: px, y: s.bottom },
360
+ { x: px, y: below },
361
+ { x: tx, y: below },
362
+ { x: tx, y: t.bottom },
363
+ ],
364
+ slack,
365
+ ...arrowHead(t.paintedWidth),
511
366
  };
512
- continue;
367
+ return;
368
+ }
369
+ if (Math.abs(px - tx) < SAME_PX) {
370
+ out[index] = {
371
+ fromId: req.fromId,
372
+ toId: req.toId,
373
+ waypoints: [from, to],
374
+ slack,
375
+ ...arrowHead(t.paintedWidth),
376
+ };
377
+ return;
513
378
  }
514
- const { x: provisionalX, underBar: provisionalUnderBar, range } = pickChannelX(req, grid);
515
- const { x: channelX, forceUnderBar } = nudgeAwayFromBrackets(
516
- provisionalX,
517
- range,
379
+ const { gap, centre } = pickGap(from, to, dir, obstacles);
380
+ pending.push({
381
+ index,
518
382
  req,
519
- grid,
520
- );
521
- const underBar = provisionalUnderBar || forceUnderBar;
522
- channelEdges.push({ req, channelX, underBar, orderIndex: i });
523
- }
524
-
525
- const slots = assignSlots(channelEdges);
526
- for (const entry of channelEdges) {
527
- const offset = slotOffset(slots.get(entry.orderIndex) ?? 0);
528
- const slotX = entry.channelX + offset;
529
- results[entry.orderIndex] = {
530
- fromId: entry.req.fromId,
531
- toId: entry.req.toId,
532
- waypoints: buildOrthogonalPath(entry.req.from, entry.req.to, slotX),
533
- underBar: entry.underBar,
383
+ from,
384
+ to,
385
+ dir,
386
+ gap,
387
+ centre,
388
+ xLo: Math.min(px, tx),
389
+ xHi: Math.max(px, tx),
390
+ bundle: `${Math.round(tx * 100)}:${dir}`,
391
+ });
392
+ });
393
+ const lanes = assignLanes(pending);
394
+ for (const p of pending) {
395
+ const y = lanes.get(p.index) ?? p.centre;
396
+ const slack = p.req.target.startX - p.req.source.endX > SAME_PX;
397
+ out[p.index] = {
398
+ fromId: p.req.fromId,
399
+ toId: p.req.toId,
400
+ waypoints: [p.from, { x: p.from.x, y }, { x: p.to.x, y }, p.to],
401
+ slack,
402
+ ...arrowHead(p.req.target.paintedWidth),
534
403
  };
535
404
  }
536
-
537
- return results;
538
- }
539
-
540
- /**
541
- * Build the orthogonal polyline through a chosen vertical channel.
542
- * Source-out-stub → vertical leg → target-in-stub. Same-row edges
543
- * collapse to two points (handled by the caller's same-row skip).
544
- */
545
- function buildOrthogonalPath(from: Point, to: Point, slotX: number): Point[] {
546
- if (Math.abs(from.y - to.y) < 0.5) {
547
- return [from, to];
548
- }
549
- return [from, { x: slotX, y: from.y }, { x: slotX, y: to.y }, to];
405
+ return out;
550
406
  }