| 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; |
| 881 | let lx = 0; |
| 882 | for (const id of list) { |
| 883 | const bw = widthOf(id); |
| 884 | if (lx > 0 && lx + bw > lineMax) { |
| 885 | line += 1; |
| 886 | lx = 0; |
| 887 | } |
| 888 | pos.set(id, { x: lx, line }); |
| 889 | width = Math.max(width, lx + bw); |
| 890 | lx += bw + NODE_GAP; |
| 891 | } |
| 892 | return { pos, lines: list.length === 0 ? 0 : line + 1, width }; |
| 893 | }; |
| 894 | |
| 895 | // The region's blocks, in the walk's order: everything that fires |