@nowline/layout 0.0.0-dev.20260601071750.g04bdff9

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 (193) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +103 -0
  3. package/dist/band-scale.d.ts +56 -0
  4. package/dist/band-scale.d.ts.map +1 -0
  5. package/dist/band-scale.js +86 -0
  6. package/dist/band-scale.js.map +1 -0
  7. package/dist/calendar.d.ts +79 -0
  8. package/dist/calendar.d.ts.map +1 -0
  9. package/dist/calendar.js +210 -0
  10. package/dist/calendar.js.map +1 -0
  11. package/dist/capacity.d.ts +72 -0
  12. package/dist/capacity.d.ts.map +1 -0
  13. package/dist/capacity.js +163 -0
  14. package/dist/capacity.js.map +1 -0
  15. package/dist/dsl-utils.d.ts +5 -0
  16. package/dist/dsl-utils.d.ts.map +1 -0
  17. package/dist/dsl-utils.js +28 -0
  18. package/dist/dsl-utils.js.map +1 -0
  19. package/dist/edge-routing.d.ts +89 -0
  20. package/dist/edge-routing.d.ts.map +1 -0
  21. package/dist/edge-routing.js +435 -0
  22. package/dist/edge-routing.js.map +1 -0
  23. package/dist/frame-tab-geometry.d.ts +78 -0
  24. package/dist/frame-tab-geometry.d.ts.map +1 -0
  25. package/dist/frame-tab-geometry.js +115 -0
  26. package/dist/frame-tab-geometry.js.map +1 -0
  27. package/dist/header-card-geometry.d.ts +29 -0
  28. package/dist/header-card-geometry.d.ts.map +1 -0
  29. package/dist/header-card-geometry.js +41 -0
  30. package/dist/header-card-geometry.js.map +1 -0
  31. package/dist/i18n.d.ts +48 -0
  32. package/dist/i18n.d.ts.map +1 -0
  33. package/dist/i18n.js +114 -0
  34. package/dist/i18n.js.map +1 -0
  35. package/dist/include-chrome-geometry.d.ts +86 -0
  36. package/dist/include-chrome-geometry.d.ts.map +1 -0
  37. package/dist/include-chrome-geometry.js +104 -0
  38. package/dist/include-chrome-geometry.js.map +1 -0
  39. package/dist/index.d.ts +11 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +10 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/inline-date-pin-geometry.d.ts +46 -0
  44. package/dist/inline-date-pin-geometry.d.ts.map +1 -0
  45. package/dist/inline-date-pin-geometry.js +149 -0
  46. package/dist/inline-date-pin-geometry.js.map +1 -0
  47. package/dist/item-bar-geometry.d.ts +157 -0
  48. package/dist/item-bar-geometry.d.ts.map +1 -0
  49. package/dist/item-bar-geometry.js +214 -0
  50. package/dist/item-bar-geometry.js.map +1 -0
  51. package/dist/lane-utilization.d.ts +90 -0
  52. package/dist/lane-utilization.d.ts.map +1 -0
  53. package/dist/lane-utilization.js +206 -0
  54. package/dist/lane-utilization.js.map +1 -0
  55. package/dist/layout-context.d.ts +143 -0
  56. package/dist/layout-context.d.ts.map +1 -0
  57. package/dist/layout-context.js +8 -0
  58. package/dist/layout-context.js.map +1 -0
  59. package/dist/layout.d.ts +18 -0
  60. package/dist/layout.d.ts.map +1 -0
  61. package/dist/layout.js +1298 -0
  62. package/dist/layout.js.map +1 -0
  63. package/dist/nodes/anchor-node.d.ts +16 -0
  64. package/dist/nodes/anchor-node.d.ts.map +1 -0
  65. package/dist/nodes/anchor-node.js +68 -0
  66. package/dist/nodes/anchor-node.js.map +1 -0
  67. package/dist/nodes/footnote-node.d.ts +10 -0
  68. package/dist/nodes/footnote-node.d.ts.map +1 -0
  69. package/dist/nodes/footnote-node.js +41 -0
  70. package/dist/nodes/footnote-node.js.map +1 -0
  71. package/dist/nodes/group-node.d.ts +29 -0
  72. package/dist/nodes/group-node.d.ts.map +1 -0
  73. package/dist/nodes/group-node.js +195 -0
  74. package/dist/nodes/group-node.js.map +1 -0
  75. package/dist/nodes/include-node.d.ts +16 -0
  76. package/dist/nodes/include-node.d.ts.map +1 -0
  77. package/dist/nodes/include-node.js +117 -0
  78. package/dist/nodes/include-node.js.map +1 -0
  79. package/dist/nodes/item-node.d.ts +51 -0
  80. package/dist/nodes/item-node.d.ts.map +1 -0
  81. package/dist/nodes/item-node.js +108 -0
  82. package/dist/nodes/item-node.js.map +1 -0
  83. package/dist/nodes/marker-geometry.d.ts +22 -0
  84. package/dist/nodes/marker-geometry.d.ts.map +1 -0
  85. package/dist/nodes/marker-geometry.js +38 -0
  86. package/dist/nodes/marker-geometry.js.map +1 -0
  87. package/dist/nodes/milestone-node.d.ts +48 -0
  88. package/dist/nodes/milestone-node.d.ts.map +1 -0
  89. package/dist/nodes/milestone-node.js +210 -0
  90. package/dist/nodes/milestone-node.js.map +1 -0
  91. package/dist/nodes/parallel-node.d.ts +21 -0
  92. package/dist/nodes/parallel-node.d.ts.map +1 -0
  93. package/dist/nodes/parallel-node.js +88 -0
  94. package/dist/nodes/parallel-node.js.map +1 -0
  95. package/dist/nodes/roadmap-node.d.ts +76 -0
  96. package/dist/nodes/roadmap-node.d.ts.map +1 -0
  97. package/dist/nodes/roadmap-node.js +788 -0
  98. package/dist/nodes/roadmap-node.js.map +1 -0
  99. package/dist/nodes/swimlane-node.d.ts +38 -0
  100. package/dist/nodes/swimlane-node.d.ts.map +1 -0
  101. package/dist/nodes/swimlane-node.js +308 -0
  102. package/dist/nodes/swimlane-node.js.map +1 -0
  103. package/dist/renderable.d.ts +44 -0
  104. package/dist/renderable.d.ts.map +1 -0
  105. package/dist/renderable.js +21 -0
  106. package/dist/renderable.js.map +1 -0
  107. package/dist/row-packer.d.ts +125 -0
  108. package/dist/row-packer.d.ts.map +1 -0
  109. package/dist/row-packer.js +169 -0
  110. package/dist/row-packer.js.map +1 -0
  111. package/dist/style-resolution.d.ts +14 -0
  112. package/dist/style-resolution.d.ts.map +1 -0
  113. package/dist/style-resolution.js +191 -0
  114. package/dist/style-resolution.js.map +1 -0
  115. package/dist/themes/dark.d.ts +4 -0
  116. package/dist/themes/dark.d.ts.map +1 -0
  117. package/dist/themes/dark.js +241 -0
  118. package/dist/themes/dark.js.map +1 -0
  119. package/dist/themes/grayscale.d.ts +4 -0
  120. package/dist/themes/grayscale.d.ts.map +1 -0
  121. package/dist/themes/grayscale.js +237 -0
  122. package/dist/themes/grayscale.js.map +1 -0
  123. package/dist/themes/index.d.ts +19 -0
  124. package/dist/themes/index.d.ts.map +1 -0
  125. package/dist/themes/index.js +57 -0
  126. package/dist/themes/index.js.map +1 -0
  127. package/dist/themes/light.d.ts +4 -0
  128. package/dist/themes/light.d.ts.map +1 -0
  129. package/dist/themes/light.js +248 -0
  130. package/dist/themes/light.js.map +1 -0
  131. package/dist/themes/shape.d.ts +194 -0
  132. package/dist/themes/shape.d.ts.map +1 -0
  133. package/dist/themes/shape.js +6 -0
  134. package/dist/themes/shape.js.map +1 -0
  135. package/dist/themes/shared.d.ts +145 -0
  136. package/dist/themes/shared.d.ts.map +1 -0
  137. package/dist/themes/shared.js +310 -0
  138. package/dist/themes/shared.js.map +1 -0
  139. package/dist/time-scale.d.ts +39 -0
  140. package/dist/time-scale.d.ts.map +1 -0
  141. package/dist/time-scale.js +62 -0
  142. package/dist/time-scale.js.map +1 -0
  143. package/dist/types.d.ts +516 -0
  144. package/dist/types.d.ts.map +1 -0
  145. package/dist/types.js +6 -0
  146. package/dist/types.js.map +1 -0
  147. package/dist/view-preset.d.ts +23 -0
  148. package/dist/view-preset.d.ts.map +1 -0
  149. package/dist/view-preset.js +146 -0
  150. package/dist/view-preset.js.map +1 -0
  151. package/dist/working-calendar.d.ts +14 -0
  152. package/dist/working-calendar.d.ts.map +1 -0
  153. package/dist/working-calendar.js +74 -0
  154. package/dist/working-calendar.js.map +1 -0
  155. package/package.json +43 -0
  156. package/src/band-scale.ts +115 -0
  157. package/src/calendar.ts +244 -0
  158. package/src/capacity.ts +191 -0
  159. package/src/dsl-utils.ts +30 -0
  160. package/src/edge-routing.ts +550 -0
  161. package/src/frame-tab-geometry.ts +165 -0
  162. package/src/header-card-geometry.ts +48 -0
  163. package/src/i18n.ts +124 -0
  164. package/src/include-chrome-geometry.ts +156 -0
  165. package/src/index.ts +118 -0
  166. package/src/inline-date-pin-geometry.ts +196 -0
  167. package/src/item-bar-geometry.ts +271 -0
  168. package/src/lane-utilization.ts +259 -0
  169. package/src/layout-context.ts +182 -0
  170. package/src/layout.ts +1530 -0
  171. package/src/nodes/anchor-node.ts +77 -0
  172. package/src/nodes/footnote-node.ts +60 -0
  173. package/src/nodes/group-node.ts +260 -0
  174. package/src/nodes/include-node.ts +168 -0
  175. package/src/nodes/item-node.ts +171 -0
  176. package/src/nodes/marker-geometry.ts +43 -0
  177. package/src/nodes/milestone-node.ts +263 -0
  178. package/src/nodes/parallel-node.ts +110 -0
  179. package/src/nodes/roadmap-node.ts +957 -0
  180. package/src/nodes/swimlane-node.ts +423 -0
  181. package/src/renderable.ts +68 -0
  182. package/src/row-packer.ts +271 -0
  183. package/src/style-resolution.ts +243 -0
  184. package/src/themes/dark.ts +244 -0
  185. package/src/themes/grayscale.ts +240 -0
  186. package/src/themes/index.ts +66 -0
  187. package/src/themes/light.ts +251 -0
  188. package/src/themes/shape.ts +230 -0
  189. package/src/themes/shared.ts +369 -0
  190. package/src/time-scale.ts +78 -0
  191. package/src/types.ts +641 -0
  192. package/src/view-preset.ts +180 -0
  193. package/src/working-calendar.ts +91 -0
package/dist/layout.js ADDED
@@ -0,0 +1,1298 @@
1
+ import { isGroupBlock, isItemDeclaration, isParallelBlock } from '@nowline/core';
2
+ import { addDays, daysBetween, deriveItemDurationDays, deriveTotalEffortDays, resolveDuration, } from './calendar.js';
3
+ import { estimateCapacitySuffixWidth, formatCapacityNumber, parseCapacityValue, resolveCapacityIcon, } from './capacity.js';
4
+ import { parseDate, propValue, propValues } from './dsl-utils.js';
5
+ import { ChannelGrid, collectRoutingObstacles, routeChannelEdges, } from './edge-routing.js';
6
+ import { HEADER_AUTHOR_FONT_SIZE_PX, HEADER_AUTHOR_LINE_HEIGHT_PX, HEADER_CARD_PADDING_BOTTOM, HEADER_CARD_PADDING_TOP, HEADER_CARD_PADDING_X, HEADER_TITLE_FONT_SIZE_PX, HEADER_TITLE_LINE_HEIGHT_PX, HEADER_TITLE_TO_AUTHOR_GAP_PX, } from './header-card-geometry.js';
7
+ import { localeStrings } from './i18n.js';
8
+ import { computeItemInlineDatePins, pickInlineDate } from './inline-date-pin-geometry.js';
9
+ import { ITEM_CAPTION_INSET_X_PX, ITEM_CAPTION_META_BASELINE_OFFSET_PX, ITEM_CAPTION_SPILL_GAP_PX, ITEM_CAPTION_TITLE_FONT_SIZE_PX, ITEM_DECORATION_SPILL_GAP_PX, ITEM_FOOTNOTE_INDICATOR_STEP_PX, ITEM_LINK_ICON_TILE_SIZE_PX, ITEM_STATUS_DOT_RADIUS_PX, LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX, LABEL_CHIP_GAP_BETWEEN_PX, LABEL_CHIP_HEIGHT_PX, LABEL_CHIP_ROW_STEP_PX, MIN_BAR_WIDTH_FOR_DOT_PX, MIN_BAR_WIDTH_FOR_FOOTNOTE_PX, MIN_BAR_WIDTH_FOR_LINK_AND_DOT_PX, packSpillChips, } from './item-bar-geometry.js';
10
+ import { newCursor } from './layout-context.js';
11
+ import { GroupNode } from './nodes/group-node.js';
12
+ import { ItemNode } from './nodes/item-node.js';
13
+ import { ParallelNode } from './nodes/parallel-node.js';
14
+ import { RoadmapNode } from './nodes/roadmap-node.js';
15
+ import { SwimlaneNode } from './nodes/swimlane-node.js';
16
+ import { resolveLabelChipStyle, resolveStyle } from './style-resolution.js';
17
+ import { GUTTER_PX, HEADER_BESIDE_MAX_WIDTH_PX, HEADER_BESIDE_MIN_WIDTH_PX, ITEM_INSET_PX, MIN_ITEM_WIDTH, NOW_PILL_LABEL_FONT_SIZE_PX, NOW_PILL_LABEL_INSET_X_PX, NOW_PILL_WIDTH_PX, PADDING_PX, PROGRESS_STRIP_HEIGHT_PX, } from './themes/shared.js';
18
+ import { daysPerUnit } from './working-calendar.js';
19
+ function statusFromProp(raw) {
20
+ switch (raw) {
21
+ case 'done':
22
+ case 'completed':
23
+ return 'done';
24
+ case 'in-progress':
25
+ case 'active':
26
+ return 'in-progress';
27
+ case 'at-risk':
28
+ case 'blocked':
29
+ case 'planned':
30
+ return raw;
31
+ case undefined:
32
+ return 'planned';
33
+ default:
34
+ return 'neutral';
35
+ }
36
+ }
37
+ function parseProgressFraction(raw) {
38
+ if (!raw)
39
+ return 0;
40
+ const m = /^(\d{1,3})%$/.exec(raw);
41
+ if (!m)
42
+ return 0;
43
+ return Math.max(0, Math.min(100, parseInt(m[1], 10))) / 100;
44
+ }
45
+ // Resolve a `size:NAME` value to its size declaration's effort literal, used
46
+ // by displays that want the literal duration string (not days). Returns the
47
+ // original string for raw literals (`1w`, `3d`) and undefined for missing
48
+ // values. m5 will adjust callers to apply capacity-aware derivation when the
49
+ // caller wants the calendar duration; this helper continues to expose the
50
+ // raw effort/duration literal for chips, captions, and tooltips.
51
+ function resolveDurationLiteral(raw, ctx) {
52
+ if (!raw)
53
+ return undefined;
54
+ if (/^\d+(?:\.\d+)?[dwmqy]$/.test(raw) || /^\d+%$/.test(raw))
55
+ return raw;
56
+ return ctx.sizes.get(raw)?.effortLiteral ?? raw;
57
+ }
58
+ // Resolve a person/team id to its declared title when present (id otherwise).
59
+ function resolveActorDisplay(raw, ctx) {
60
+ if (!raw)
61
+ return undefined;
62
+ return ctx.teams.get(raw)?.title ?? ctx.persons.get(raw)?.title ?? raw;
63
+ }
64
+ function parseLinkIcon(link) {
65
+ if (!link)
66
+ return { icon: 'none' };
67
+ const lower = link.toLowerCase();
68
+ if (lower.includes('linear.app'))
69
+ return { icon: 'linear', href: link };
70
+ if (lower.includes('github.com'))
71
+ return { icon: 'github', href: link };
72
+ if (lower.includes('atlassian.net') || lower.includes('jira.')) {
73
+ return { icon: 'jira', href: link };
74
+ }
75
+ return { icon: 'generic', href: link };
76
+ }
77
+ // Bake a named/hex color from a label's `bg:` or `fg:` into the chip style.
78
+ function buildLabelChip(label, ctx, x, y, maxWidth) {
79
+ const style = resolveLabelChipStyle(label, ctx);
80
+ // Prefer the short name (id) when it exists — chips inside an item bar
81
+ // are tight; the long title risks overflowing.
82
+ const text = label.name ?? label.title ?? '';
83
+ const padKey = style.padding === 'none' ? 'xs' : style.padding;
84
+ const pad = PADDING_PX[padKey];
85
+ let width = Math.max(20, Math.round(text.length * 5.5 + pad * 2));
86
+ if (maxWidth !== undefined)
87
+ width = Math.min(width, maxWidth);
88
+ return {
89
+ text,
90
+ style,
91
+ box: { x, y, width, height: LABEL_CHIP_HEIGHT_PX },
92
+ };
93
+ }
94
+ // Sequence a set of nodes into a single horizontal track. `parallelInside`
95
+ // indicates the caller is inside a ParallelBlock and each child occupies a
96
+ // fresh sub-track (caller passes a new cursor per call).
97
+ function sequenceItem(node, cursor, ctx, ownerOverride) {
98
+ const props = node.properties;
99
+ const style = resolveStyle('item', props, ctx.styleCtx);
100
+ // Sizing precedence (specs/dsl.md § "Sizing precedence"):
101
+ // 1. `duration:LITERAL` wins as the calendar duration — `size:NAME`
102
+ // becomes a pure annotation (chip only).
103
+ // 2. Otherwise, `size:NAME` derives `effort ÷ capacity` (default
104
+ // capacity = 1).
105
+ // The validator requires one of the two on every item.
106
+ const sizeRef = propValue(props, 'size');
107
+ const sizeResolved = sizeRef ? (ctx.sizes.get(sizeRef) ?? null) : null;
108
+ const durationDays = deriveItemDurationDays(props, ctx.sizes, ctx.cal);
109
+ // Total work in single-engineer days, used below to normalize a literal
110
+ // `remaining:` value into a progress fraction. Stays per-engineer
111
+ // regardless of the item's `capacity:` so a `remaining:1w` always means
112
+ // "one engineer-week of work left".
113
+ const totalEffortDays = deriveTotalEffortDays(props, ctx.sizes, ctx.cal);
114
+ const afterRaw = propValues(props, 'after');
115
+ const beforeRaw = propValues(props, 'before');
116
+ const dateRaw = propValue(props, 'date');
117
+ const remainingDays = resolveDuration(propValue(props, 'remaining'), ctx.sizes, ctx.cal);
118
+ // Resolve start x: explicit date > after-chain > cursor position.
119
+ // `after:` accepts both entity ids (looked up in entityRightEdges) and
120
+ // inline ISO date literals (resolved through the time scale). The
121
+ // validator enforces "at most one inline date per direction" so at most
122
+ // one element will hit the date path per item.
123
+ let startX = cursor.x;
124
+ const explicitDate = parseDate(dateRaw);
125
+ if (explicitDate) {
126
+ const xd = ctx.scale.forwardWithinDomain(explicitDate);
127
+ if (xd !== null)
128
+ startX = xd;
129
+ }
130
+ else if (afterRaw.length > 0) {
131
+ let maxEnd = cursor.x;
132
+ for (const ref of afterRaw) {
133
+ const inlineDate = parseDate(ref);
134
+ if (inlineDate) {
135
+ const xd = ctx.scale.forwardWithinDomain(inlineDate);
136
+ if (xd !== null)
137
+ maxEnd = Math.max(maxEnd, xd);
138
+ continue;
139
+ }
140
+ const endX = ctx.entityRightEdges.get(ref);
141
+ if (endX !== undefined)
142
+ maxEnd = Math.max(maxEnd, endX);
143
+ }
144
+ startX = Math.max(cursor.x, maxEnd);
145
+ }
146
+ const naturalWidth = Math.max(MIN_ITEM_WIDTH, durationDays * ctx.timeline.pixelsPerDay);
147
+ // Logical extent — what the item "owns" in time (used for chaining,
148
+ // `after:` lookups, dependency-arrow attach points).
149
+ const logicalLeft = startX;
150
+ const logicalRight = startX + naturalWidth;
151
+ const linkRaw = propValue(props, 'link');
152
+ const linkInfo = parseLinkIcon(linkRaw);
153
+ const hasLinkIcon = linkInfo.icon !== 'none';
154
+ // Pre-compute the chip row geometry — every chip renders at its
155
+ // NATURAL text-fit width on a single horizontal row, never
156
+ // truncated. We only need the total row width here so we can
157
+ // decide whether the row fits inside the bar; concrete chip
158
+ // (x, y) placement comes after ItemNode resolves the visible
159
+ // bar box below.
160
+ //
161
+ // Chips sit at the bar's bottom (just above the progress strip)
162
+ // and the link icon (when present) sits in the bar's UPPER-LEFT
163
+ // corner, so they no longer share a vertical band — chips have
164
+ // the full caption-inset-bounded inner width regardless of
165
+ // whether a link icon is rendered. The link-icon column's
166
+ // horizontal cost is borne by the caption (title/meta) inset
167
+ // instead, see `ItemNode`.
168
+ const visualWidthPredict = Math.max(MIN_ITEM_WIDTH, naturalWidth - 2 * ITEM_INSET_PX);
169
+ const labelIds = propValues(props, 'labels');
170
+ const chipSamples = [];
171
+ for (const labelId of labelIds) {
172
+ const label = ctx.labels.get(labelId);
173
+ if (!label)
174
+ continue;
175
+ const sample = buildLabelChip(label, ctx.styleCtx, 0, 0);
176
+ chipSamples.push({ id: label, width: sample.box.width });
177
+ }
178
+ let chipRowWidth = 0;
179
+ for (let i = 0; i < chipSamples.length; i += 1) {
180
+ if (i > 0)
181
+ chipRowWidth += LABEL_CHIP_GAP_BETWEEN_PX;
182
+ chipRowWidth += chipSamples[i].width;
183
+ }
184
+ const chipInsideAvailWidth = Math.max(0, visualWidthPredict - 2 * ITEM_CAPTION_INSET_X_PX);
185
+ const chipsOutside = chipSamples.length > 0 && chipRowWidth > chipInsideAvailWidth;
186
+ // Handle `before:` — item must end by the earliest cap. Each cap is
187
+ // either an entity id (looked up in entityLeftEdges) or an inline ISO
188
+ // date literal (resolved through the time scale). The validator enforces
189
+ // "at most one inline date per direction" so at most one element per
190
+ // before-list will hit the date path.
191
+ let hasOverflow = false;
192
+ let overflowBox;
193
+ let overflowAnchorId;
194
+ if (beforeRaw.length > 0) {
195
+ let earliestCapX;
196
+ let earliestCapRef;
197
+ for (const ref of beforeRaw) {
198
+ const inlineDate = parseDate(ref);
199
+ const capX = inlineDate
200
+ ? (ctx.scale.forwardWithinDomain(inlineDate) ?? undefined)
201
+ : ctx.entityLeftEdges.get(ref);
202
+ if (capX === undefined)
203
+ continue;
204
+ if (earliestCapX === undefined || capX < earliestCapX) {
205
+ earliestCapX = capX;
206
+ earliestCapRef = ref;
207
+ }
208
+ }
209
+ if (earliestCapX !== undefined && logicalRight > earliestCapX) {
210
+ hasOverflow = true;
211
+ overflowBox = {
212
+ x: earliestCapX,
213
+ y: cursor.y,
214
+ width: logicalRight - earliestCapX,
215
+ height: ctx.bandScale.bandwidth(),
216
+ };
217
+ overflowAnchorId = earliestCapRef;
218
+ }
219
+ }
220
+ // Progress fraction
221
+ const statusRaw = propValue(props, 'status');
222
+ const status = statusFromProp(statusRaw);
223
+ let progress = parseProgressFraction(statusRaw);
224
+ if (progress === 0 && status === 'done')
225
+ progress = 1;
226
+ const remainingPctMatch = /^(\d{1,3})%$/.exec(propValue(props, 'remaining') ?? '');
227
+ if (progress === 0 && status === 'in-progress' && remainingPctMatch) {
228
+ const pct = Math.max(0, Math.min(100, parseInt(remainingPctMatch[1], 10))) / 100;
229
+ progress = 1 - pct;
230
+ }
231
+ if (progress === 0 && status === 'in-progress' && remainingDays > 0 && totalEffortDays > 0) {
232
+ // `remaining:` literal is single-engineer days; `totalEffortDays`
233
+ // is also single-engineer days, so the ratio is unit-correct.
234
+ // Clamp to [0, 1] — the renderer paints 100% remaining when the
235
+ // author overshot, matching the spec's "warn-and-clamp" overflow
236
+ // behavior. (Validation defers the warn to layout-time today; a
237
+ // future diagnostics channel can surface it back to the user.)
238
+ progress = Math.max(0, Math.min(1, 1 - remainingDays / totalEffortDays));
239
+ }
240
+ // Apply the status-tinted item background when the resolved bg is still
241
+ // theme-default. Authors who set explicit `bg:` keep their override.
242
+ // Per m2d handoff Resolution 3: layout owns this so the renderer stays
243
+ // palette-dumb.
244
+ const STATUS_TINT_LIGHT = {
245
+ done: '#ecfdf5',
246
+ 'in-progress': '#eff6ff',
247
+ 'at-risk': '#fffbeb',
248
+ blocked: '#fee2e2',
249
+ planned: '#f8fafc',
250
+ neutral: '#f8fafc',
251
+ };
252
+ const STATUS_TINT_DARK = {
253
+ done: '#052e16',
254
+ 'in-progress': '#172554',
255
+ 'at-risk': '#422006',
256
+ blocked: '#7f1d1d',
257
+ planned: '#1e293b',
258
+ neutral: '#1e293b',
259
+ };
260
+ const STATUS_TINT_GREY = {
261
+ done: '#ebebeb',
262
+ 'in-progress': '#e4e4e4',
263
+ 'at-risk': '#eeeeee',
264
+ blocked: '#dcdcdc',
265
+ planned: '#f5f5f5',
266
+ neutral: '#f5f5f5',
267
+ };
268
+ const STATUS_BORDER = {
269
+ done: ctx.styleCtx.theme.status.done,
270
+ 'in-progress': ctx.styleCtx.theme.status.inProgress,
271
+ 'at-risk': ctx.styleCtx.theme.status.atRisk,
272
+ blocked: ctx.styleCtx.theme.status.blocked,
273
+ planned: ctx.styleCtx.theme.status.planned,
274
+ neutral: ctx.styleCtx.theme.status.neutral,
275
+ };
276
+ const themeName = ctx.styleCtx.theme.name;
277
+ const isDark = themeName === 'dark';
278
+ const themeDefaultBg = isDark ? '#0f172a' : '#ffffff';
279
+ const themeDefaultFg = themeName === 'grayscale' ? '#9e9e9e' : '#94a3b8';
280
+ if (style.bg === themeDefaultBg) {
281
+ style.bg =
282
+ themeName === 'grayscale'
283
+ ? STATUS_TINT_GREY[status]
284
+ : isDark
285
+ ? STATUS_TINT_DARK[status]
286
+ : STATUS_TINT_LIGHT[status];
287
+ }
288
+ if (style.fg === themeDefaultFg) {
289
+ style.fg = STATUS_BORDER[status];
290
+ }
291
+ // Pre-format the secondary line shown inside the item bar.
292
+ //
293
+ // Driver-only meta line (`rendering.md` § Item size chip): exactly one
294
+ // leading token — the explicit non-empty `duration:LITERAL` when set,
295
+ // otherwise the size chip when `size:` drives. Never both; bar width
296
+ // already encodes derived calendar span for sized items.
297
+ const explicitDurationLiteral = propValue(props, 'duration');
298
+ const durationDrives = !!explicitDurationLiteral && /^\d+(?:\.\d+)?[dwmqy]$/.test(explicitDurationLiteral);
299
+ const sizeChipText = sizeResolved ? (sizeResolved.title ?? sizeResolved.name) : '';
300
+ const driverToken = durationDrives
301
+ ? explicitDurationLiteral
302
+ : sizeChipText || undefined;
303
+ const remainingRaw = propValue(props, 'remaining');
304
+ const remainingLiteral = resolveDurationLiteral(remainingRaw, ctx);
305
+ const ownerDisplay = resolveActorDisplay(ownerOverride ?? propValue(props, 'owner'), ctx);
306
+ const metaHead = () => [driverToken, ownerDisplay].filter(Boolean).join(' ');
307
+ let metaText;
308
+ if (status === 'in-progress' && remainingLiteral) {
309
+ const head = metaHead();
310
+ metaText = head
311
+ ? `${head} — ${remainingLiteral} remaining`
312
+ : `${remainingLiteral} remaining`;
313
+ }
314
+ else if (status === 'in-progress' && progress > 0 && progress < 1) {
315
+ const pct = Math.round((1 - progress) * 100);
316
+ const head = metaHead();
317
+ metaText = head ? `${head} — ${pct}% remaining` : `${pct}% remaining`;
318
+ }
319
+ else if (ownerDisplay || driverToken) {
320
+ metaText = metaHead() || undefined;
321
+ }
322
+ // Capacity suffix — appended after metaText at render time. Layout's
323
+ // job here is to (a) parse the value out of `capacity:`, (b) format
324
+ // the display number, (c) resolve `capacity-icon` to either a
325
+ // built-in name or a literal string the renderer can paint directly,
326
+ // and (d) feed the suffix's estimated width into ItemNode so spill
327
+ // detection accounts for `2w 5×` rather than just `2w`. The suffix
328
+ // disappears entirely when capacity is missing or non-positive.
329
+ const capacityRaw = propValue(props, 'capacity');
330
+ const capacityValue = parseCapacityValue(capacityRaw);
331
+ let capacity = null;
332
+ let capacityTrailingWidth = 0;
333
+ if (capacityValue !== null) {
334
+ const capacityText = formatCapacityNumber(capacityValue);
335
+ const capacityIcon = resolveCapacityIcon(style.capacityIcon, ctx.symbols);
336
+ capacity = { value: capacityValue, text: capacityText, icon: capacityIcon };
337
+ const META_FONT_SIZE_PX_LOCAL = 11;
338
+ // Add a small leading separator (a single space's worth) only when
339
+ // the suffix sits next to existing meta text, so `m 5×` has air
340
+ // between the driver token and the count. Standalone suffix needs no
341
+ // leading separator.
342
+ const separatorWidth = metaText ? estimateTextWidth(' ', META_FONT_SIZE_PX_LOCAL) : 0;
343
+ capacityTrailingWidth =
344
+ separatorWidth +
345
+ estimateCapacitySuffixWidth(capacityText, capacityIcon, META_FONT_SIZE_PX_LOCAL);
346
+ }
347
+ // Visual bar + caption-spill decision delegated to ItemNode. Logical
348
+ // extent (used by chaining and `after:` lookups) stays on
349
+ // logicalLeft/logicalRight; ItemNode computes the inset visual box and
350
+ // whether the title+meta line overflows the bar's inner padded width.
351
+ const titleStr = node.title ?? node.name ?? '';
352
+ const placed = new ItemNode({
353
+ id: node.name ?? '',
354
+ title: titleStr,
355
+ logicalLeftX: logicalLeft,
356
+ logicalRightX: logicalRight,
357
+ metaText,
358
+ metaTrailingWidth: capacityTrailingWidth,
359
+ hasLinkIcon,
360
+ }).place({ x: logicalLeft, y: cursor.y }, { time: ctx.scale, bands: ctx.bandScale, style });
361
+ const itemBox = placed.box;
362
+ const bandwidth = ctx.bandScale.bandwidth();
363
+ // Narrow-bar decoration spill — when a bar is too narrow to host
364
+ // the dot, link icon, or footnote with its full inset, those
365
+ // glyphs render in the same spill column as the (already-
366
+ // spilling) caption text, in reading order
367
+ // `[bar][dot][icon][title][footnote#…][meta]`. Each decoration's
368
+ // threshold is independent (a 20-px-wide bar can host the dot
369
+ // but not the icon, etc.); see the `MIN_BAR_WIDTH_FOR_*`
370
+ // constants in `item-bar-geometry`.
371
+ //
372
+ // Forcing `textSpills` when `iconSpills` keeps the icon and
373
+ // title visually adjacent — otherwise a spilled icon would
374
+ // float at `bar.right + 6` while the title stayed inside the
375
+ // bar, breaking the icon→title affordance.
376
+ const dotSpills = itemBox.width < MIN_BAR_WIDTH_FOR_DOT_PX;
377
+ const iconSpills = hasLinkIcon && itemBox.width < MIN_BAR_WIDTH_FOR_LINK_AND_DOT_PX;
378
+ const footnoteSpillsForNarrow = itemBox.width < MIN_BAR_WIDTH_FOR_FOOTNOTE_PX;
379
+ const textSpills = placed.textSpills || iconSpills;
380
+ // Label chips lay out left → right at natural text width.
381
+ //
382
+ // INSIDE the bar (chipsOutside === false): single row,
383
+ // left-aligned at the caption inset, anchored just above the
384
+ // bottom progress strip.
385
+ //
386
+ // OUTSIDE the bar (chipsOutside === true): the whole chip set
387
+ // moves to the spill column at `bar.right + 6`. Within the
388
+ // column, chips pack into rows capped at the bar's visual
389
+ // width — see `packSpillChips`. Row 0 sits at the same y the
390
+ // single-row would have used; subsequent rows stack DOWNWARD by
391
+ // one `LABEL_CHIP_ROW_STEP_PX`.
392
+ //
393
+ // When chips spill, the BAR ITSELF GROWS DOWNWARD so the chip
394
+ // column reads as enclosed by the bar — the painted footprint
395
+ // of the bar is `bandwidth + chipBarExtra` and the bottom
396
+ // progress strip moves with the new bottom edge. Chip Y is
397
+ // anchored to the ORIGINAL bandwidth (relative to the bar's
398
+ // top), not to the grown box.height, so row 0 stays where a
399
+ // single-row chip would naturally render and rows 1..N grow
400
+ // downward into the new bar area.
401
+ //
402
+ // When the caption ALSO spills (`textSpills && chipsOutside`),
403
+ // row 0's y drops below the meta baseline so the spilled stack
404
+ // reads `title → meta → chip-row-0 → chip-row-1 → ...` at a
405
+ // single column inside the (now-taller) bar.
406
+ let chipPack = null;
407
+ if (chipsOutside) {
408
+ chipPack = packSpillChips(chipSamples, itemBox.width);
409
+ }
410
+ const chipRowCount = chipPack ? chipPack.rows.length : chipSamples.length > 0 ? 1 : 0;
411
+ // Capacity suffix renders on the same line as metaText. Treat the meta
412
+ // line as present whenever EITHER metaText OR a capacity suffix will
413
+ // paint, so chip-row pitch reserves the right amount of vertical space.
414
+ const hasMeta = metaText !== undefined || capacity !== null;
415
+ const chipBarExtra = computeChipBarExtra(chipsOutside, textSpills, chipRowCount, bandwidth, hasMeta);
416
+ if (chipBarExtra > 0) {
417
+ itemBox.height = bandwidth + chipBarExtra;
418
+ }
419
+ const labelChips = [];
420
+ const baseChipY = itemBox.y +
421
+ bandwidth -
422
+ PROGRESS_STRIP_HEIGHT_PX -
423
+ LABEL_CHIP_HEIGHT_PX -
424
+ LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX;
425
+ const captionStackChipY = itemBox.y + ITEM_CAPTION_META_BASELINE_OFFSET_PX + LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX;
426
+ // Inside-bar chips with meta need to clear the meta baseline —
427
+ // the natural `baseChipY` (anchored to bar bottom) sits above
428
+ // the meta line at typical bandwidths, so the chip rect would
429
+ // overlap the meta text vertically. Use whichever Y is lower.
430
+ // Outside-bar chips already reuse `captionStackChipY` when the
431
+ // caption ALSO spills; with caption inside we don't need to
432
+ // shift them since they're horizontally separated from the meta.
433
+ const stackBelowMeta = (chipsOutside && textSpills) || (!chipsOutside && hasMeta && chipSamples.length > 0);
434
+ const chipRow0Y = stackBelowMeta ? Math.max(baseChipY, captionStackChipY) : baseChipY;
435
+ const chipStartX = chipsOutside
436
+ ? itemBox.x + itemBox.width + ITEM_CAPTION_SPILL_GAP_PX
437
+ : itemBox.x + ITEM_CAPTION_INSET_X_PX;
438
+ let chipsRightX = chipStartX;
439
+ if (chipPack) {
440
+ for (let r = 0; r < chipPack.rows.length; r += 1) {
441
+ const rowY = chipRow0Y + r * LABEL_CHIP_ROW_STEP_PX;
442
+ let rowCursorX = chipStartX;
443
+ for (const sample of chipPack.rows[r]) {
444
+ const chip = buildLabelChip(sample.id, ctx.styleCtx, rowCursorX, rowY);
445
+ labelChips.push(chip);
446
+ rowCursorX += chip.box.width + LABEL_CHIP_GAP_BETWEEN_PX;
447
+ }
448
+ const rowRight = rowCursorX - LABEL_CHIP_GAP_BETWEEN_PX;
449
+ if (rowRight > chipsRightX)
450
+ chipsRightX = rowRight;
451
+ }
452
+ }
453
+ else {
454
+ let rowCursorX = chipStartX;
455
+ for (const sample of chipSamples) {
456
+ const chip = buildLabelChip(sample.id, ctx.styleCtx, rowCursorX, chipRow0Y);
457
+ labelChips.push(chip);
458
+ rowCursorX += chip.box.width + LABEL_CHIP_GAP_BETWEEN_PX;
459
+ }
460
+ chipsRightX = chipSamples.length > 0 ? rowCursorX - LABEL_CHIP_GAP_BETWEEN_PX : chipStartX;
461
+ }
462
+ // Footnote superscript indicators. Per `specs/dsl.md`, footnotes
463
+ // attach via the `on:` property on the footnote declaration only —
464
+ // there is no forward `footnote:` property on the host entity. Walk
465
+ // `footnoteHosts` (built from each footnote's `on:` list) and emit
466
+ // a superscript for every footnote that names this item.
467
+ const footnoteIndicatorSet = new Set();
468
+ if (node.name) {
469
+ for (const [fid, hosts] of ctx.footnoteHosts.entries()) {
470
+ if (hosts.includes(node.name)) {
471
+ const n = ctx.footnoteIndex.get(fid);
472
+ if (n !== undefined)
473
+ footnoteIndicatorSet.add(n);
474
+ }
475
+ }
476
+ }
477
+ const footnoteIndicators = [...footnoteIndicatorSet].sort((a, b) => a - b);
478
+ const owner = ownerDisplay ?? ownerOverride ?? propValue(props, 'owner');
479
+ const description = node.description?.text;
480
+ // Footnote glyphs only need to spill when there's at least one
481
+ // indicator AND the bar is too narrow to host them at the inset-
482
+ // right anchor. Compute the final boolean here once we know the
483
+ // indicator count.
484
+ const footnoteSpills = footnoteIndicators.length > 0 && footnoteSpillsForNarrow;
485
+ // Spill-column x positions for the decorations. The cluster
486
+ // mirrors the in-bar reading order so users see the same visual
487
+ // hierarchy whether everything fits inside or trails off to the
488
+ // right:
489
+ //
490
+ // In-bar (default): [icon] [title] [¹²] [dot]
491
+ // Spilled (narrow): [bar] [icon?] [title][¹²?] [dot?]
492
+ //
493
+ // The dot lives at the trailing edge in BOTH cases — pushing it
494
+ // to the LEFT of the title (with the title trailing it) read as
495
+ // the dot belonging to the next item, not this one. A missing
496
+ // decoration just collapses out of the row; e.g. an item with
497
+ // no link AND a too-narrow bar gives `[bar] [title] [dot]`.
498
+ //
499
+ // `decorationsRightX` is the furthest right edge any spilled
500
+ // glyph reaches; the row-packer uses it (alongside spilled-chip
501
+ // width) to reserve x-extent so the next chained item bumps to
502
+ // a fresh row instead of landing under the spilled cluster.
503
+ const SPILL_COLUMN_X0 = itemBox.x + itemBox.width + ITEM_CAPTION_SPILL_GAP_PX;
504
+ let spillCursor = SPILL_COLUMN_X0;
505
+ // Advance the cursor by `gap` IFF something has already been
506
+ // placed in the column — keeps the cluster from leaving a
507
+ // dangling gap past its final glyph (which would over-reserve
508
+ // x-extent and shift downstream items).
509
+ let needGap = false;
510
+ let iconSpillX = null;
511
+ if (iconSpills) {
512
+ if (needGap)
513
+ spillCursor += ITEM_DECORATION_SPILL_GAP_PX;
514
+ iconSpillX = spillCursor;
515
+ spillCursor = iconSpillX + ITEM_LINK_ICON_TILE_SIZE_PX;
516
+ needGap = true;
517
+ }
518
+ let captionSpillWidth = 0;
519
+ if (textSpills) {
520
+ if (needGap)
521
+ spillCursor += ITEM_DECORATION_SPILL_GAP_PX;
522
+ const titleW = estimateTextWidth(titleStr, ITEM_CAPTION_TITLE_FONT_SIZE_PX);
523
+ // Spill column width is the wider of the title and the *full* meta
524
+ // line (text + capacity suffix). `capacityTrailingWidth` is 0 when
525
+ // no capacity suffix is rendered, so this stays a no-op for items
526
+ // without `capacity:`.
527
+ const metaW = (metaText ? estimateTextWidth(metaText, 11) : 0) + capacityTrailingWidth;
528
+ captionSpillWidth = Math.max(titleW, metaW);
529
+ spillCursor += captionSpillWidth;
530
+ needGap = true;
531
+ }
532
+ let footnoteSpillStartX = null;
533
+ if (footnoteSpills) {
534
+ if (needGap)
535
+ spillCursor += ITEM_DECORATION_SPILL_GAP_PX;
536
+ footnoteSpillStartX = spillCursor;
537
+ spillCursor += footnoteIndicators.length * ITEM_FOOTNOTE_INDICATOR_STEP_PX;
538
+ needGap = true;
539
+ }
540
+ let dotSpillCx = null;
541
+ if (dotSpills) {
542
+ if (needGap)
543
+ spillCursor += ITEM_DECORATION_SPILL_GAP_PX;
544
+ dotSpillCx = spillCursor + ITEM_STATUS_DOT_RADIUS_PX;
545
+ spillCursor = dotSpillCx + ITEM_STATUS_DOT_RADIUS_PX;
546
+ needGap = true;
547
+ }
548
+ const decorationsRightX = Math.max(itemBox.x + itemBox.width, spillCursor);
549
+ const id = node.name;
550
+ if (id) {
551
+ // Entity edges live in LOGICAL space so chained items / `after:`
552
+ // references / dependency-arrow attach points sit on the column
553
+ // boundary, not on the visually inset bar edge. The visible 12 px
554
+ // gutter between bars then becomes a clean attach corridor.
555
+ ctx.entityLeftEdges.set(id, logicalLeft);
556
+ ctx.entityRightEdges.set(id, logicalRight);
557
+ ctx.entityMidpoints.set(id, {
558
+ x: (logicalLeft + logicalRight) / 2,
559
+ y: itemBox.y + itemBox.height / 2,
560
+ });
561
+ // Visual edges — where dependency arrows actually attach. These
562
+ // sit ITEM_INSET_PX inside the column boundaries so the arrows
563
+ // emerge from the painted bar edge instead of the inter-column
564
+ // gutter. See LayoutContext.entityVisualLeftX/RightX.
565
+ ctx.entityVisualLeftX.set(id, itemBox.x);
566
+ ctx.entityVisualRightX.set(id, itemBox.x + itemBox.width);
567
+ // Dependency-arrow source point. Default = the bar's right
568
+ // edge at row midpoint. When the caption spills past the
569
+ // bar's right edge (`textSpills`), the spilled title /
570
+ // meta occupy the area immediately right of the bar at
571
+ // row midline. Keep X on the bar's right edge so the
572
+ // arrow visually leaves the bar's side, but drop Y to the
573
+ // vertical center of the bottom progress strip so the
574
+ // arrow runs UNDERNEATH the spilled text rather than
575
+ // through it. Mirrors the slack-arrow attach below.
576
+ const arrowSource = textSpills
577
+ ? {
578
+ x: itemBox.x + itemBox.width,
579
+ y: itemBox.y + itemBox.height - PROGRESS_STRIP_HEIGHT_PX / 2,
580
+ }
581
+ : {
582
+ x: itemBox.x + itemBox.width,
583
+ y: itemBox.y + itemBox.height / 2,
584
+ };
585
+ ctx.itemArrowSource.set(id, arrowSource);
586
+ ctx.itemFlowKey.set(id, ctx.currentFlowKey);
587
+ // Slack-arrow attach Y. Defaults to the bar's row midpoint; when
588
+ // the caption spills past the bar's right edge, drop to the
589
+ // progress-strip's vertical center so the arrow aligns with the
590
+ // bottom-edge progress bar instead of running through the
591
+ // adjacent title/meta text. The `/ 2` keeps the attach point on
592
+ // the strip's vertical center if `PROGRESS_STRIP_HEIGHT_PX` is
593
+ // ever bumped.
594
+ const slackAttachY = textSpills
595
+ ? itemBox.y + itemBox.height - PROGRESS_STRIP_HEIGHT_PX / 2
596
+ : itemBox.y + itemBox.height / 2;
597
+ ctx.itemSlackAttachY.set(id, slackAttachY);
598
+ }
599
+ cursor.x = logicalRight;
600
+ cursor.maxX = Math.max(cursor.maxX, cursor.x);
601
+ // The next row in a parallel/group/lane starts at
602
+ // `cursor.y + cursor.height`. Default pitch is `bandScale.step()`
603
+ // (bandwidth + inter-row gap). When the bar grew to enclose a
604
+ // spilled chip column, the pitch grows by the SAME amount so the
605
+ // inter-row gap stays constant — the next row's bar starts
606
+ // `step − bandwidth` px below the (now-taller) bar bottom.
607
+ cursor.height = Math.max(cursor.height, ctx.bandScale.step() + chipBarExtra);
608
+ const inlineDatePins = computeItemInlineDatePins({
609
+ box: itemBox,
610
+ afterDate: pickInlineDate(afterRaw),
611
+ beforeDate: pickInlineDate(beforeRaw),
612
+ hasLinkIcon,
613
+ footnoteCount: footnoteIndicators.length,
614
+ });
615
+ const result = {
616
+ kind: 'item',
617
+ id,
618
+ title: titleStr,
619
+ box: itemBox,
620
+ status,
621
+ progressFraction: progress,
622
+ footnoteIndicators,
623
+ labelChips,
624
+ chipsOutside,
625
+ chipsRightX,
626
+ linkIcon: linkInfo.icon,
627
+ linkHref: linkInfo.href,
628
+ hasOverflow,
629
+ overflowBox,
630
+ overflowAnchorId,
631
+ owner,
632
+ description,
633
+ metaText,
634
+ textSpills,
635
+ dotSpills,
636
+ iconSpills,
637
+ footnoteSpills,
638
+ dotSpillCx,
639
+ iconSpillX,
640
+ footnoteSpillStartX,
641
+ decorationsRightX,
642
+ capacity,
643
+ size: sizeResolved,
644
+ style,
645
+ inlineDatePins: inlineDatePins.length > 0 ? inlineDatePins : undefined,
646
+ };
647
+ return result;
648
+ }
649
+ function sequenceParallel(node, cursor, ctx) {
650
+ return new ParallelNode(node, { sequenceOne, newCursor }).place(cursor, ctx);
651
+ }
652
+ function sequenceGroup(node, cursor, ctx) {
653
+ return new GroupNode(node, {
654
+ sequenceItem,
655
+ sequenceOne,
656
+ resolveChildStart,
657
+ newCursor,
658
+ estimateTextWidth,
659
+ predictItemChipExtraHeight,
660
+ }).place(cursor, ctx);
661
+ }
662
+ function sequenceOne(node, cursor, ctx) {
663
+ if (isItemDeclaration(node))
664
+ return sequenceItem(node, cursor, ctx);
665
+ if (isParallelBlock(node))
666
+ return sequenceParallel(node, cursor, ctx);
667
+ if (isGroupBlock(node))
668
+ return sequenceGroup(node, cursor, ctx);
669
+ throw new Error(`Unknown swimlane child type: ${node.$type ?? 'unknown'}`);
670
+ }
671
+ // Rough px-width estimate for sans-serif text. Intentionally pessimistic
672
+ // (uses ~0.58 em per char) so we err toward "doesn't fit" and trigger a
673
+ // row bump rather than draw an item with a clipped title.
674
+ function estimateTextWidth(text, fontSize) {
675
+ return text.length * fontSize * 0.58;
676
+ }
677
+ /**
678
+ * Compute the extra vertical px the bar grows when its spilled chip
679
+ * column would otherwise extend below the (single-row) bottom. The
680
+ * bar's painted footprint becomes `bandwidth + chipBarExtra`, the
681
+ * progress strip rides the new bottom, and chip rows pack inside
682
+ * the taller bar (anchored from the bar TOP so row 0 doesn't shift
683
+ * when the bar grows).
684
+ *
685
+ * Returns 0 when chips fit inside the bar, when there are no chips,
686
+ * or when the spilled column happens to fit inside `bandwidth` (a
687
+ * single row with the caption inside, for instance).
688
+ *
689
+ * The same number is the row-pitch increase the swimlane / group
690
+ * row-packer needs to reserve so the next row clears the taller
691
+ * bar — `cursor.height = step + chipBarExtra` and the predict
692
+ * helper returns this verbatim.
693
+ */
694
+ function computeChipBarExtra(chipsOutside, captionSpills, chipRowCount, bandwidth, hasMeta) {
695
+ if (chipRowCount === 0)
696
+ return 0;
697
+ // Row 0 anchor relative to the bar's TOP — three regimes:
698
+ //
699
+ // 1. chipsOutside + captionSpills → chips stack below the
700
+ // spilled meta line (`captionStackTop`).
701
+ // 2. chips INSIDE the bar AND meta is present → chip top must
702
+ // clear the meta baseline; the natural `baseTop` sits
703
+ // ABOVE the meta line at default bandwidth (=56), so we
704
+ // take whichever is lower of base/captionStack.
705
+ // 3. otherwise (in-bar w/o meta, or chipsOutside w/o caption
706
+ // spill) → row 0 hugs the bar bottom at `baseTop`.
707
+ //
708
+ // Cases (2) and (3-with-multi-row-spill) can both grow the bar;
709
+ // case (3-with-single-row-inside-no-meta) never grows.
710
+ const baseTop = bandwidth -
711
+ PROGRESS_STRIP_HEIGHT_PX -
712
+ LABEL_CHIP_HEIGHT_PX -
713
+ LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX;
714
+ const captionStackTop = ITEM_CAPTION_META_BASELINE_OFFSET_PX + LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX;
715
+ let chipRow0Top;
716
+ if (chipsOutside && captionSpills) {
717
+ chipRow0Top = captionStackTop;
718
+ }
719
+ else if (!chipsOutside && hasMeta) {
720
+ chipRow0Top = Math.max(baseTop, captionStackTop);
721
+ }
722
+ else {
723
+ chipRow0Top = baseTop;
724
+ }
725
+ const lastRowBottomTop = chipRow0Top + (chipRowCount - 1) * LABEL_CHIP_ROW_STEP_PX + LABEL_CHIP_HEIGHT_PX;
726
+ // The bar must be tall enough to fit `lastRowBottomTop` plus a
727
+ // GAP above the progress strip plus the progress strip itself.
728
+ const requiredHeight = lastRowBottomTop + LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX + PROGRESS_STRIP_HEIGHT_PX;
729
+ return Math.max(0, requiredHeight - bandwidth);
730
+ }
731
+ /**
732
+ * Predict an item's bar growth (and therefore row-pitch growth) for
733
+ * a multi-row spilled chip column BEFORE the bar is sequenced. Used
734
+ * by the swimlane / group row-packer so neighboring rows on later
735
+ * rows are positioned correctly without a retroactive shift.
736
+ *
737
+ * Mirrors the chip-pack + caption-spill arithmetic in
738
+ * `sequenceItem` so prediction and placement agree byte-for-byte.
739
+ */
740
+ function predictItemChipExtraHeight(item, ctx) {
741
+ const props = item.properties;
742
+ const labelIds = propValues(props, 'labels');
743
+ if (labelIds.length === 0)
744
+ return 0;
745
+ const durationDays = deriveItemDurationDays(props, ctx.sizes, ctx.cal);
746
+ const naturalWidth = Math.max(MIN_ITEM_WIDTH, durationDays * ctx.timeline.pixelsPerDay);
747
+ const visualWidth = Math.max(MIN_ITEM_WIDTH, naturalWidth - 2 * ITEM_INSET_PX);
748
+ const samples = [];
749
+ for (const labelId of labelIds) {
750
+ const label = ctx.labels.get(labelId);
751
+ if (!label)
752
+ continue;
753
+ const sample = buildLabelChip(label, ctx.styleCtx, 0, 0);
754
+ samples.push({ id: label, width: sample.box.width });
755
+ }
756
+ if (samples.length === 0)
757
+ return 0;
758
+ let chipRowWidth = 0;
759
+ for (let i = 0; i < samples.length; i += 1) {
760
+ if (i > 0)
761
+ chipRowWidth += LABEL_CHIP_GAP_BETWEEN_PX;
762
+ chipRowWidth += samples[i].width;
763
+ }
764
+ const insideAvail = Math.max(0, visualWidth - 2 * ITEM_CAPTION_INSET_X_PX);
765
+ const chipsOutside = chipRowWidth > insideAvail;
766
+ // `hasMeta` mirrors `metaText !== undefined` in `sequenceItem`,
767
+ // plus the capacity suffix (which renders on the same meta line).
768
+ // metaText is set whenever an item declares a duration, owner,
769
+ // or remaining — so we just check those four props. Status
770
+ // strings (in-progress) only matter when paired with one of
771
+ // these, so this is an upper bound (false-positives still grow
772
+ // the bar by exactly the same amount as the renderer would, so
773
+ // they stay byte-stable).
774
+ const hasMeta = propValue(props, 'duration') !== undefined ||
775
+ propValue(props, 'size') !== undefined ||
776
+ propValue(props, 'owner') !== undefined ||
777
+ propValue(props, 'remaining') !== undefined ||
778
+ propValue(props, 'capacity') !== undefined;
779
+ const titleStr = item.title ?? item.name ?? '';
780
+ const titleW = titleStr ? estimateTextWidth(titleStr, ITEM_CAPTION_TITLE_FONT_SIZE_PX) : 0;
781
+ const captionSpills = titleW > insideAvail;
782
+ const pack = chipsOutside ? packSpillChips(samples, visualWidth) : null;
783
+ const chipRowCount = pack ? pack.rows.length : samples.length > 0 ? 1 : 0;
784
+ return computeChipBarExtra(chipsOutside, captionSpills, chipRowCount, ctx.bandScale.bandwidth(), hasMeta);
785
+ }
786
+ // Resolve the desired startX for a swimlane child, honoring `date:` (fixed
787
+ // pin) > `start:` (fixed pin) > `after:` (chain after refs) > sequential
788
+ // default (continue from `seqDefault`, which is the lane's rightmost time
789
+ // cursor across all rows).
790
+ //
791
+ // `after:` accepts both entity ids (looked up in `ctx.entityRightEdges`) and
792
+ // inline ISO date literals (looked up via `ctx.scale.forwardWithinDomain`).
793
+ // The validator already enforces "at most one inline date per direction" so
794
+ // at most one element in the list will hit the date path.
795
+ function resolveChildStart(props, seqDefault, laneLeftX, ctx) {
796
+ const explicitDate = parseDate(propValue(props, 'date')) ?? parseDate(propValue(props, 'start'));
797
+ if (explicitDate) {
798
+ const xd = ctx.scale.forwardWithinDomain(explicitDate);
799
+ if (xd !== null)
800
+ return xd;
801
+ }
802
+ const afterRefs = propValues(props, 'after');
803
+ if (afterRefs.length > 0) {
804
+ let maxEnd = laneLeftX;
805
+ for (const ref of afterRefs) {
806
+ const inlineDate = parseDate(ref);
807
+ if (inlineDate) {
808
+ const xd = ctx.scale.forwardWithinDomain(inlineDate);
809
+ if (xd !== null)
810
+ maxEnd = Math.max(maxEnd, xd);
811
+ continue;
812
+ }
813
+ const endX = ctx.entityRightEdges.get(ref);
814
+ if (endX !== undefined)
815
+ maxEnd = Math.max(maxEnd, endX);
816
+ }
817
+ return Math.max(laneLeftX, maxEnd);
818
+ }
819
+ return seqDefault;
820
+ }
821
+ function _buildSwimlane(lane, y, bandIndex, ctx) {
822
+ return new SwimlaneNode({ lane, bandIndex }, {
823
+ sequenceItem,
824
+ sequenceOne,
825
+ resolveChildStart,
826
+ newCursor,
827
+ estimateTextWidth,
828
+ predictItemChipExtraHeight,
829
+ }).place({ x: ctx.timeline.originX, y }, ctx);
830
+ }
831
+ // Card-sizing constants for beside-mode headers live in
832
+ // `header-card-geometry.ts` so the renderer can paint with the same
833
+ // numbers the layout sized against. Title and author both wrap at
834
+ // MAX_CONTENT_WIDTH (= MAX header width minus 2 * padding). The card
835
+ // hugs its content in the MIN..MAX range and grows vertically when
836
+ // wrapping is needed.
837
+ //
838
+ // Left margin between the canvas's left edge and the visible card. The
839
+ // matching right-side breathing room is owned by `GUTTER_PX` (the canonical
840
+ // content gutter, applied between `chartLeftX` and `originX`), so the gap
841
+ // from the card's right edge to the timeline strip is the same as the gap
842
+ // between two adjacent items.
843
+ const HEADER_CARD_OUTER_PAD = 6;
844
+ // Word-wrap `text` so that no line wider than `maxWidth` (in px). Long single
845
+ // words are kept on their own line even if they overflow — we never split a
846
+ // word in the middle.
847
+ function wrapText(text, maxWidth, fontSize) {
848
+ if (!text)
849
+ return [];
850
+ const words = text.split(/\s+/).filter((w) => w.length > 0);
851
+ if (words.length === 0)
852
+ return [];
853
+ const lines = [];
854
+ let cur = '';
855
+ for (const word of words) {
856
+ const trial = cur ? `${cur} ${word}` : word;
857
+ if (cur && estimateTextWidth(trial, fontSize) > maxWidth) {
858
+ lines.push(cur);
859
+ cur = word;
860
+ }
861
+ else {
862
+ cur = trial;
863
+ }
864
+ }
865
+ if (cur)
866
+ lines.push(cur);
867
+ return lines;
868
+ }
869
+ function sizeBesideHeader(title, author) {
870
+ const maxContentWidth = HEADER_BESIDE_MAX_WIDTH_PX - 2 * HEADER_CARD_PADDING_X;
871
+ const titleLines = wrapText(title, maxContentWidth, HEADER_TITLE_FONT_SIZE_PX);
872
+ const authorLines = wrapText(author ?? '', maxContentWidth, HEADER_AUTHOR_FONT_SIZE_PX);
873
+ let widest = 0;
874
+ for (const line of titleLines)
875
+ widest = Math.max(widest, estimateTextWidth(line, HEADER_TITLE_FONT_SIZE_PX));
876
+ for (const line of authorLines)
877
+ widest = Math.max(widest, estimateTextWidth(line, HEADER_AUTHOR_FONT_SIZE_PX));
878
+ const naturalCardWidth = widest + 2 * HEADER_CARD_PADDING_X;
879
+ // `HEADER_BESIDE_{MIN,MAX}_WIDTH_PX` bound the **boxWidth** (= cardWidth
880
+ // + left outer pad). Subtract one outer pad to derive the cardWidth
881
+ // bounds.
882
+ const cardWidth = Math.max(HEADER_BESIDE_MIN_WIDTH_PX - HEADER_CARD_OUTER_PAD, Math.min(HEADER_BESIDE_MAX_WIDTH_PX - HEADER_CARD_OUTER_PAD, naturalCardWidth));
883
+ const titleBlockHeight = titleLines.length > 0 ? (titleLines.length - 1) * HEADER_TITLE_LINE_HEIGHT_PX : 0;
884
+ const authorBlockHeight = authorLines.length > 0
885
+ ? HEADER_TITLE_TO_AUTHOR_GAP_PX +
886
+ (authorLines.length - 1) * HEADER_AUTHOR_LINE_HEIGHT_PX
887
+ : 0;
888
+ const cardHeight = HEADER_CARD_PADDING_TOP + titleBlockHeight + authorBlockHeight + HEADER_CARD_PADDING_BOTTOM;
889
+ // `boxWidth` only includes the LEFT outer pad — the right-side breathing
890
+ // room between the card and the chart is owned by `GUTTER_PX` in
891
+ // `RoadmapNode`. So `boxWidth` doubles as `chartLeftX` (the card's right
892
+ // edge in canvas coordinates).
893
+ const boxWidth = cardWidth + HEADER_CARD_OUTER_PAD;
894
+ return { titleLines, authorLines, cardWidth, cardHeight, boxWidth };
895
+ }
896
+ // Compute a sensible [startDate, endDate] window.
897
+ //
898
+ // Precedence:
899
+ // 1. Explicit `length:` on the roadmap declaration wins.
900
+ // 2. Otherwise we derive the end day from the actual content extent
901
+ // (latest item end, anchor date, milestone date/after, and today's
902
+ // now-line if it falls past the content). This keeps the rendered
903
+ // chart from defaulting to a 180-day desert when the content only
904
+ // spans a few weeks.
905
+ // 3. As a last resort (no content + no length), fall back to a small
906
+ // 4-week placeholder so an empty roadmap still draws a sensible axis.
907
+ function computeDateWindow(file, ctx, resolved, today, scale) {
908
+ const roadmap = file.roadmapDecl;
909
+ const props = roadmap?.properties ?? [];
910
+ const startRaw = propValue(props, 'start');
911
+ // Spec (`specs/dsl.md`): "A roadmap with no `start:` and no dates is
912
+ // purely relative — renderers choose their own reference date (e.g.
913
+ // the day of rendering)." Use the caller's `today` (the resolved
914
+ // `--now`) when present so the start lines up with the now-line, and
915
+ // fall back to actual today's UTC midnight otherwise. Either default
916
+ // is dangerous — output drifts day-to-day — but it's strictly better
917
+ // than the legacy "Jan 1 of the current year" fallback that drifted
918
+ // every January 1 by 365 days at once. Authors should set `start:`
919
+ // for any roadmap they want to be reproducible.
920
+ const startDate = parseDate(startRaw) ?? defaultStartDate(today);
921
+ const lengthRaw = propValue(props, 'length');
922
+ if (lengthRaw) {
923
+ const days = literalDays(lengthRaw, ctx.cal);
924
+ if (days > 0) {
925
+ return { startDate, endDate: addDays(startDate, days) };
926
+ }
927
+ }
928
+ const contentDays = computeContentEndDay(resolved, ctx, startDate, today);
929
+ const tickDays = daysPerUnit(scale.unit, ctx.cal);
930
+ // Round up to the smallest tick boundary that is `>= contentDays`. When
931
+ // the latest content lands exactly on a tick boundary the chart ends
932
+ // exactly there (no extra trailing tick); otherwise we extend to the
933
+ // next tick so the right edge always sits on a labelled column.
934
+ const padded = contentDays > 0 ? Math.ceil(contentDays / tickDays) * tickDays : 4 * ctx.cal.daysPerWeek;
935
+ return { startDate, endDate: addDays(startDate, Math.max(1, padded)) };
936
+ }
937
+ /**
938
+ * Reference date used when a roadmap omits `start:`. Prefers the
939
+ * caller-supplied `today` (UTC midnight already, when it comes from
940
+ * the CLI); falls back to actual today's UTC midnight so the layout
941
+ * still produces a valid window for direct API callers that don't pass
942
+ * a `today`. Date components are taken in UTC to match `parseDate`.
943
+ */
944
+ function defaultStartDate(today) {
945
+ const ref = today ?? new Date();
946
+ return new Date(Date.UTC(ref.getUTCFullYear(), ref.getUTCMonth(), ref.getUTCDate()));
947
+ }
948
+ function literalDays(literal, cal) {
949
+ const m = /^(\d+(?:\.\d+)?)([dwmqy])$/.exec(literal);
950
+ if (!m)
951
+ return 0;
952
+ const n = parseFloat(m[1]);
953
+ switch (m[2]) {
954
+ case 'd':
955
+ return n;
956
+ case 'w':
957
+ return n * cal.daysPerWeek;
958
+ case 'm':
959
+ return n * cal.daysPerMonth;
960
+ case 'q':
961
+ return n * cal.daysPerQuarter;
962
+ case 'y':
963
+ return n * cal.daysPerYear;
964
+ default:
965
+ return 0;
966
+ }
967
+ }
968
+ // Walk every dated/sequenced entity in the resolved content and return the
969
+ // latest day-offset from `startDate`. Mirrors the sequencer's start-rules
970
+ // (date: > start: > after: > previous-in-lane) without producing positions.
971
+ function computeContentEndDay(resolved, ctx, startDate, today) {
972
+ const itemEnd = new Map();
973
+ const anchorEnd = new Map();
974
+ const milestoneEnd = new Map();
975
+ let maxDay = 0;
976
+ // Resolve a single `after:` element to a day-offset from `startDate`.
977
+ // The element is either an entity id (looked up in itemEnd / anchorEnd /
978
+ // milestoneEnd) or an inline ISO date literal (converted directly via
979
+ // `daysBetween`). The validator already enforces "at most one inline date
980
+ // per direction", so at most one element per list will hit the date path.
981
+ const resolveAfterDay = (ref) => {
982
+ const inlineDate = parseDate(ref);
983
+ if (inlineDate)
984
+ return daysBetween(startDate, inlineDate);
985
+ if (itemEnd.has(ref))
986
+ return itemEnd.get(ref);
987
+ if (anchorEnd.has(ref))
988
+ return anchorEnd.get(ref);
989
+ if (milestoneEnd.has(ref))
990
+ return milestoneEnd.get(ref);
991
+ return 0;
992
+ };
993
+ // Pre-seed anchors fixed by `date:` so items that reference them get a
994
+ // valid end-day during the lane walk.
995
+ for (const anchor of resolved.content.anchors.values()) {
996
+ const d = parseDate(propValue(anchor.properties, 'date'));
997
+ if (d && anchor.name) {
998
+ const day = daysBetween(startDate, d);
999
+ anchorEnd.set(anchor.name, day);
1000
+ maxDay = Math.max(maxDay, day);
1001
+ }
1002
+ }
1003
+ const walkLane = (children, baselineEnd) => {
1004
+ let prevEnd = baselineEnd;
1005
+ for (const child of children) {
1006
+ if (child.$type === 'DescriptionDirective')
1007
+ continue;
1008
+ prevEnd = walkNode(child, prevEnd);
1009
+ maxDay = Math.max(maxDay, prevEnd);
1010
+ }
1011
+ return prevEnd;
1012
+ };
1013
+ const walkNode = (node, prevEnd) => {
1014
+ if (isItemDeclaration(node)) {
1015
+ const dur = deriveItemDurationDays(node.properties, ctx.sizes, ctx.cal);
1016
+ const dateProp = parseDate(propValue(node.properties, 'date'));
1017
+ const startProp = parseDate(propValue(node.properties, 'start'));
1018
+ const afterRefs = propValues(node.properties, 'after');
1019
+ let start = prevEnd;
1020
+ if (dateProp) {
1021
+ start = daysBetween(startDate, dateProp);
1022
+ }
1023
+ else if (startProp) {
1024
+ start = daysBetween(startDate, startProp);
1025
+ }
1026
+ else if (afterRefs.length > 0) {
1027
+ start = Math.max(prevEnd, ...afterRefs.map(resolveAfterDay));
1028
+ }
1029
+ const end = start + dur;
1030
+ if (node.name)
1031
+ itemEnd.set(node.name, end);
1032
+ return end;
1033
+ }
1034
+ if (isParallelBlock(node)) {
1035
+ // All children share the parallel's start; the block's effective
1036
+ // end is the maximum child end. The parallel's own `after:`
1037
+ // (including inline-date pins) widens that shared start.
1038
+ const afterRefs = propValues(node.properties, 'after');
1039
+ const containerStart = afterRefs.length > 0
1040
+ ? Math.max(prevEnd, ...afterRefs.map(resolveAfterDay))
1041
+ : prevEnd;
1042
+ let parallelEnd = containerStart;
1043
+ for (const child of node.content) {
1044
+ if (child.$type === 'DescriptionDirective')
1045
+ continue;
1046
+ const childEnd = walkNode(child, containerStart);
1047
+ parallelEnd = Math.max(parallelEnd, childEnd);
1048
+ }
1049
+ return parallelEnd;
1050
+ }
1051
+ if (isGroupBlock(node)) {
1052
+ // The group's own `after:` (including inline-date pins) widens
1053
+ // the baseline before walking the inner sequential lane.
1054
+ const afterRefs = propValues(node.properties, 'after');
1055
+ const containerStart = afterRefs.length > 0
1056
+ ? Math.max(prevEnd, ...afterRefs.map(resolveAfterDay))
1057
+ : prevEnd;
1058
+ return walkLane(node.content, containerStart);
1059
+ }
1060
+ return prevEnd;
1061
+ };
1062
+ for (const lane of resolved.content.swimlanes.values()) {
1063
+ walkLane(lane.content, 0);
1064
+ }
1065
+ // Milestones (after items so `after:` references can resolve).
1066
+ for (const ms of resolved.content.milestones.values()) {
1067
+ const d = parseDate(propValue(ms.properties, 'date'));
1068
+ if (d) {
1069
+ const day = daysBetween(startDate, d);
1070
+ if (ms.name)
1071
+ milestoneEnd.set(ms.name, day);
1072
+ maxDay = Math.max(maxDay, day);
1073
+ continue;
1074
+ }
1075
+ const after = propValues(ms.properties, 'after');
1076
+ if (after.length > 0) {
1077
+ // Milestones disallow inline date literals at the validator
1078
+ // level (rule 24a — milestones already have `date:`). The
1079
+ // shared `resolveAfterDay` helper still handles such input
1080
+ // gracefully if it slips past, treating the date as the
1081
+ // milestone's effective day.
1082
+ const day = Math.max(0, ...after.map(resolveAfterDay));
1083
+ if (ms.name)
1084
+ milestoneEnd.set(ms.name, day);
1085
+ maxDay = Math.max(maxDay, day);
1086
+ }
1087
+ }
1088
+ // Isolated includes contribute their own content extent against the
1089
+ // shared timeline.
1090
+ for (const region of resolved.content.isolatedRegions) {
1091
+ const nestedMax = computeContentEndDay({
1092
+ config: region.config,
1093
+ content: region.content,
1094
+ diagnostics: [],
1095
+ processedFiles: new Set(),
1096
+ }, ctx, startDate, undefined);
1097
+ maxDay = Math.max(maxDay, nestedMax);
1098
+ }
1099
+ if (today) {
1100
+ const t = daysBetween(startDate, today);
1101
+ if (t > 0)
1102
+ maxDay = Math.max(maxDay, t);
1103
+ }
1104
+ return maxDay;
1105
+ }
1106
+ function buildDependencies(items, swimlanes, includes, ctx) {
1107
+ // m2g+: collect every painted item bar + every visible parallel /
1108
+ // group bracket once, hand to the channel router so it can drop
1109
+ // vertical legs in clean inter-column gutters and nudge away from
1110
+ // bracket strokes that would otherwise be hugged.
1111
+ const grid = new ChannelGrid(collectRoutingObstacles(swimlanes, includes));
1112
+ const requests = [];
1113
+ const pending = [];
1114
+ for (const [id, item] of items) {
1115
+ const afters = propValues(item.properties, 'after');
1116
+ const targetMid = ctx.entityMidpoints.get(id);
1117
+ const targetVisualLeftX = ctx.entityVisualLeftX.get(id);
1118
+ if (!targetMid || targetVisualLeftX === undefined)
1119
+ continue;
1120
+ // The arrow always TERMINATES at the target item's left
1121
+ // visual edge, vertically centered on the bar.
1122
+ const targetPoint = { x: targetVisualLeftX, y: targetMid.y };
1123
+ for (const pred of afters) {
1124
+ // Source point depends on what kind of predecessor `pred`
1125
+ // is. For ITEMS we use the per-item arrow source point
1126
+ // — (visualRight, midY) by default, dropping to
1127
+ // (visualRight, bar.bottom - PROGRESS_STRIP_HEIGHT/2) when
1128
+ // the caption spilled past the right edge so the arrow
1129
+ // exits below the spilled title / meta text. For ANCHORS
1130
+ // / MILESTONES we attach to the marker's vertical CUT
1131
+ // LINE at the TARGET item's row mid-Y — the dashed/solid
1132
+ // cut line already drops through the chart and reads as
1133
+ // the arrow's stem, so a short horizontal stub from the
1134
+ // line into the bar's left edge is the cleanest
1135
+ // connection.
1136
+ const itemSource = ctx.itemArrowSource.get(pred);
1137
+ let from;
1138
+ const isMarkerPred = itemSource === undefined;
1139
+ if (itemSource) {
1140
+ from = itemSource;
1141
+ }
1142
+ else {
1143
+ const markerMid = ctx.entityMidpoints.get(pred);
1144
+ if (!markerMid)
1145
+ continue;
1146
+ from = { x: markerMid.x, y: targetMid.y };
1147
+ }
1148
+ // Skip same-row contiguous chains for ITEM → ITEM only:
1149
+ // when the target sits immediately to the right of the
1150
+ // source on the same row, the spatial flow already
1151
+ // conveys ordering and an arrow is redundant noise.
1152
+ // MARKER → ITEM stubs always draw (the cut line is the
1153
+ // stem; the short stub completes the visual connection
1154
+ // even when the bar is right next to the cut line).
1155
+ if (!isMarkerPred &&
1156
+ Math.abs(from.y - targetPoint.y) < 0.5 &&
1157
+ targetPoint.x - from.x < 20) {
1158
+ continue;
1159
+ }
1160
+ requests.push({
1161
+ fromId: pred,
1162
+ toId: id,
1163
+ from,
1164
+ to: targetPoint,
1165
+ isMarkerSource: isMarkerPred,
1166
+ });
1167
+ pending.push({ fromId: pred, toId: id });
1168
+ }
1169
+ }
1170
+ const routed = routeChannelEdges(requests, grid);
1171
+ const out = [];
1172
+ for (let i = 0; i < routed.length; i++) {
1173
+ const r = routed[i];
1174
+ out.push({
1175
+ fromId: pending[i].fromId,
1176
+ toId: pending[i].toId,
1177
+ waypoints: r.waypoints,
1178
+ kind: r.underBar ? 'underBar' : 'normal',
1179
+ style: resolveStyle('item', [], ctx.styleCtx),
1180
+ });
1181
+ }
1182
+ return out;
1183
+ }
1184
+ function buildNowline(today, ctx, locale) {
1185
+ if (!today)
1186
+ return null;
1187
+ const x = ctx.scale.forwardWithinDomain(today);
1188
+ if (x === null)
1189
+ return null;
1190
+ // Pill row is the band reserved at the very top of the timeline area.
1191
+ // The line drops from the bottom of the pill (top of the date headers)
1192
+ // through any marker row and into the chart, so the pill and line stay
1193
+ // visually connected.
1194
+ const pillTopY = ctx.timeline.box.y;
1195
+ const lineTopY = ctx.timeline.tickPanelY;
1196
+ // Pill width is locale-aware: en-US's `'now'` (3 chars) fits the
1197
+ // 36 px default trivially; longer strings like fr's `'maint.'`
1198
+ // (6 chars) need a wider pill to avoid clipping. Floor at the
1199
+ // default so en-US output stays byte-stable; grow when the
1200
+ // measured label needs it. The 2× inset matches the renderer's
1201
+ // flag-mode label inset (`NOW_PILL_LABEL_INSET_X_PX`) and gives
1202
+ // the same visual padding to centered-mode strings.
1203
+ const label = localeStrings(locale).nowLabel;
1204
+ const labelTextWidth = estimateTextWidth(label, NOW_PILL_LABEL_FONT_SIZE_PX);
1205
+ const pillWidth = Math.max(NOW_PILL_WIDTH_PX, Math.ceil(labelTextWidth + 2 * NOW_PILL_LABEL_INSET_X_PX));
1206
+ // The "chart's left edge" the pill must clear is `chartLeftX` (in
1207
+ // beside-mode, the right edge of the header card; in above-mode,
1208
+ // the canvas left edge at x=0). originX = chartLeftX + GUTTER_PX,
1209
+ // so we recover chartLeftX as `originX - GUTTER_PX`.
1210
+ const chartLeftX = ctx.timeline.originX - GUTTER_PX;
1211
+ const halfPill = pillWidth / 2;
1212
+ let pillMode;
1213
+ if (x - halfPill < chartLeftX) {
1214
+ // Centered pill would intrude into the header card / past the
1215
+ // canvas left edge — anchor the pill's LEFT side to the line
1216
+ // and let it extend right into the chart.
1217
+ pillMode = 'flag-right';
1218
+ }
1219
+ else if (x + halfPill > ctx.chartRightX) {
1220
+ // Centered pill would clip past the canvas right edge — anchor
1221
+ // the pill's RIGHT side to the line and let it extend left.
1222
+ pillMode = 'flag-left';
1223
+ }
1224
+ else {
1225
+ pillMode = 'center';
1226
+ }
1227
+ // Bottom-most Y the now-line should reach. When a mirrored bottom
1228
+ // tick panel exists, thread the line through it (so the line ties
1229
+ // the two date strips together visually). Otherwise stop at the
1230
+ // last swimlane — never extend into the footnote area below.
1231
+ const bottomTickPanelY = ctx.timeline.bottomTickPanelY;
1232
+ const bottomTickPanelHeight = ctx.timeline.bottomTickPanelHeight ?? 0;
1233
+ const lineBottomY = bottomTickPanelY !== undefined && bottomTickPanelHeight > 0
1234
+ ? bottomTickPanelY + bottomTickPanelHeight
1235
+ : ctx.swimlaneBottomY;
1236
+ return {
1237
+ x,
1238
+ topY: lineTopY,
1239
+ bottomY: lineBottomY,
1240
+ pillTopY,
1241
+ pillMode,
1242
+ label,
1243
+ pillWidth,
1244
+ style: resolveStyle('item', [], ctx.styleCtx),
1245
+ };
1246
+ }
1247
+ // Mutable layout-time context shared across helpers.
1248
+ // Traverse the full content tree to build an `items` map keyed by id.
1249
+ function collectItems(swimlanes) {
1250
+ const out = new Map();
1251
+ const walk = (node) => {
1252
+ if (isItemDeclaration(node)) {
1253
+ if (node.name)
1254
+ out.set(node.name, node);
1255
+ return;
1256
+ }
1257
+ if (isParallelBlock(node)) {
1258
+ for (const child of node.content) {
1259
+ if (child.$type === 'DescriptionDirective')
1260
+ continue;
1261
+ walk(child);
1262
+ }
1263
+ return;
1264
+ }
1265
+ if (isGroupBlock(node)) {
1266
+ for (const child of node.content) {
1267
+ if (child.$type === 'DescriptionDirective')
1268
+ continue;
1269
+ walk(child);
1270
+ }
1271
+ return;
1272
+ }
1273
+ };
1274
+ for (const lane of swimlanes) {
1275
+ for (const child of lane.content) {
1276
+ if (child.$type === 'DescriptionDirective')
1277
+ continue;
1278
+ walk(child);
1279
+ }
1280
+ }
1281
+ return out;
1282
+ }
1283
+ export function layoutRoadmap(file, resolved, options = {}) {
1284
+ return new RoadmapNode().place(file, resolved, options, {
1285
+ sequenceItem,
1286
+ sequenceOne,
1287
+ resolveChildStart,
1288
+ newCursor,
1289
+ estimateTextWidth,
1290
+ predictItemChipExtraHeight,
1291
+ computeDateWindow,
1292
+ sizeBesideHeader,
1293
+ collectItems,
1294
+ buildDependencies,
1295
+ buildNowline,
1296
+ });
1297
+ }
1298
+ //# sourceMappingURL=layout.js.map