| 821 | * 5,624px that way). One width, scored on the finished canvas. |
| 822 | */ |
| 823 | const layoutAt = (lineMax: number) => { |
| 824 | const packed = new Map<string, Packed>(); |
| 825 | for (const region of regions.values()) { |
| 826 | // A region is drawn as CLUSTERS, not as rows: a step, then the steps it |
| 827 | // sets in motion on the line under it, indented. Rows-then-wrap put every |
| 828 | // step of one distance on the same rows and wrapped them at a fixed width, |
| 829 | // so a box and the thing it fires ended up seven |
| 830 | // lines apart and their line crossed everything between — 70 of 113 lines |
| 831 | // on one real screen joined boxes ONE step apart and rendered seven lines |
| 832 | // apart. Under a cluster the same line is one line long. |
| 833 | const ids = new Set(region.members.map((m) => m.id)); |
| 834 | const kidsOf = (id: string): string[] => (childrenOf.get(id) ?? []).filter((k) => ids.has(k)); |
| 835 | const starts = region.members.filter((m) => (parentsOf.get(m.id) ?? []).length === 0).map((m) => m.id); |
| 836 | |
| 837 | /** Lay the region out with its lines allowed to run this wide. */ |
| 838 | const layAt = (lineMax: number): { pos: Map<string, { x: number; line: number }>; lines: number; width: number } => { |
| 839 | /** |
| 840 | * One cluster, in its own coordinates: a step, then the steps it sets |
| 841 | * in motion on the line under it, stepped in, and a cluster of its own |
| 842 | * for anything that leads on further. |
| 843 | */ |
| 844 | const cluster = (root: string): { pos: Map<string, { x: number; line: number }>; lines: number; width: number } => { |
| 845 | const pos = new Map<string, { x: number; line: number }>(); |
| 846 | let line = 0; |
| 847 | let width = 0; |
| 848 | const spread = (list: string[], left: number): void => { |
| 849 | if (list.length === 0) return; |
| 850 | let lx = left; |
| 851 | for (const id of list) { |
| 852 | const bw = widthOf(id); |
| 853 | if (lx > left && lx + bw - left > lineMax) { |
| 854 | line += 1; |
| 855 | lx = left; |
| 856 | } |
| 857 | pos.set(id, { x: lx, line }); |
| 858 | width = Math.max(width, lx + bw); |
| 859 | lx += bw + NODE_GAP; |
| 860 | } |
| 861 | line += 1; |
| 862 | }; |
| 863 | const place = (id: string, depth: number): void => { |
| 864 | const x = Math.min(depth, CLUSTER_DEPTH_MAX) * CLUSTER_INDENT; |
| 865 | pos.set(id, { x, line }); |
| 866 | width = Math.max(width, x + widthOf(id)); |
| 867 | line += 1; |
| 868 | const kids = kidsOf(id); |
| 869 | spread(kids.filter((k) => kidsOf(k).length === 0), x + CLUSTER_INDENT); |
| 870 | for (const hub of kids.filter((k) => kidsOf(k).length > 0)) place(hub, depth + 1); |
| 871 | }; |
| 872 | place(root, 0); |
| 873 | return { pos, lines: line, width }; |
| 874 | }; |
| 875 | |
| 876 | /** The steps that fire nothing, side by side — a screen's handlers are siblings, not a hierarchy. */ |
| 877 | const flat = (list: string[]): { pos: Map<string, { x: number; line: number }>; lines: number; width: number } => { |
| 878 | const pos = new Map<string, { x: number; line: number }>(); |
| 879 | let line = 0; |
| 880 | let width = 0; |