* Sibling connectors out of one box that are arms of ONE fork, marked as the * choice they are: each line says only which way it is, and the condition is * said once under the box that decides it. Two lines that each carried the * whole predicate — one of them the other's negation, both truncated
(edges: Map<string, StepEdgeInfo>, layout: MapLayout)
| 342 | * before it is worth a caption. |
| 343 | */ |
| 344 | function markDecisions(edges: Map<string, StepEdgeInfo>, layout: MapLayout): StepDecision[] { |
| 345 | const groups = new Map<string, Array<{ info: StepEdgeInfo; decision: WireStepDecision }>>(); |
| 346 | for (const info of edges.values()) { |
| 347 | const decision = edgeArm(info); |
| 348 | if (decision === null) continue; |
| 349 | const key = `${info.from} ${decision.branch}`; |
| 350 | const list = groups.get(key) ?? []; |
| 351 | list.push({ info, decision }); |
| 352 | groups.set(key, list); |
| 353 | } |
| 354 | |
| 355 | const boxes = new Map(layout.nodes.map((n) => [n.id, n])); |
| 356 | const out: StepDecision[] = []; |
| 357 | /** Two decisions made in one box stack under it rather than sitting on each other. */ |
| 358 | const perBox = new Map<string, number>(); |
| 359 | for (const [key, group] of groups) { |
| 360 | if (new Set(group.map((g) => g.decision.arm)).size < 2) continue; |
| 361 | const box = boxes.get(group[0]!.info.from); |
| 362 | if (!box) continue; |
| 363 | for (const { info, decision } of group) { |
| 364 | info.arm = armWords(decision); |
| 365 | // The connector's label IS the arm now: the decision says the rest. |
| 366 | info.label = info.arm; |
| 367 | } |
| 368 | const nth = perBox.get(box.id) ?? 0; |
| 369 | perBox.set(box.id, nth + 1); |
| 370 | const on = group[0]!.decision.on; |
| 371 | const label = `${whenWords(on) || on}?`; |
| 372 | // The condition is the whole point of the caption, so it may take a |
| 373 | // little more room than the box it sits under — centred on it, and capped |
| 374 | // so a long predicate cannot reach across its neighbours. |
| 375 | const width = Math.max(box.width, Math.min(label.length * DECISION_CHAR + 8, DECISION_MAX_WIDTH)); |
| 376 | out.push({ |
| 377 | id: key, |
| 378 | label, |
| 379 | x: box.x + (box.width - width) / 2, |
| 380 | y: box.y + box.height + 4 + nth * DECISION_LINE, |
| 381 | width, |
| 382 | }); |
| 383 | } |
| 384 | return out; |
| 385 | } |
| 386 | |
| 387 | /* ---------------------------------------------------------------- build -- */ |
| 388 |