140 lines · 5.4 KBRaw
| 1 | import type { CodeModel, NodeId } from "./model"; |
| 2 | |
| 3 | export interface Box { |
| 4 | readonly x: number; |
| 5 | readonly y: number; |
| 6 | readonly w: number; |
| 7 | readonly h: number; |
| 8 | } |
| 9 | |
| 10 | /** Code blocks are sized to their lines, so the map at the closest level shows the code itself. */ |
| 11 | export const LINE_HEIGHT = 18; |
| 12 | export const CODE_TOP = 30; |
| 13 | const CODE_WIDTH = 440; |
| 14 | const MAX_CODE_LINES = 160; |
| 15 | const MAX_COLUMN_ITEMS = 8; |
| 16 | /** A storyline wider than this many times its height wraps into rows, read left to right, then down. */ |
| 17 | const MAX_ASPECT = 2.4; |
| 18 | |
| 19 | interface Spacing { |
| 20 | readonly pad: number; |
| 21 | readonly head: number; |
| 22 | readonly gapX: number; |
| 23 | readonly gapY: number; |
| 24 | } |
| 25 | |
| 26 | const SPACING: Record<"project" | "folder" | "file" | "class", Spacing> = { |
| 27 | project: { pad: 48, head: 0, gapX: 160, gapY: 64 }, |
| 28 | folder: { pad: 24, head: 44, gapX: 100, gapY: 28 }, |
| 29 | file: { pad: 14, head: 32, gapX: 60, gapY: 16 }, |
| 30 | class: { pad: 12, head: 30, gapX: 40, gapY: 12 }, |
| 31 | }; |
| 32 | |
| 33 | interface Sized { |
| 34 | readonly w: number; |
| 35 | readonly h: number; |
| 36 | readonly kids: readonly (readonly [NodeId, number, number])[]; |
| 37 | } |
| 38 | |
| 39 | /** Places every block. Inside each block, its children run left to right in the order they use each other, as a storyline. */ |
| 40 | export function layoutMap(model: CodeModel): ReadonlyMap<NodeId, Box> { |
| 41 | const sizes = new Map<NodeId, Sized>(); |
| 42 | size(model, "root", sizes); |
| 43 | const boxes = new Map<NodeId, Box>(); |
| 44 | place("root", 0, 0, sizes, boxes); |
| 45 | return boxes; |
| 46 | } |
| 47 | |
| 48 | export function codeLineCount(model: CodeModel, id: NodeId): number { |
| 49 | const symbol = model.symbol(id); |
| 50 | return symbol ? Math.min(MAX_CODE_LINES, symbol.end - symbol.start + 1) : 0; |
| 51 | } |
| 52 | |
| 53 | function spacingOf(model: CodeModel, id: NodeId): Spacing { |
| 54 | const kind = model.kind(id); |
| 55 | return kind === "project" ? SPACING.project : kind === "folder" ? SPACING.folder : kind === "file" ? SPACING.file : SPACING.class; |
| 56 | } |
| 57 | |
| 58 | function size(model: CodeModel, id: NodeId, sizes: Map<NodeId, Sized>): Sized { |
| 59 | const kids = model.kids(id); |
| 60 | if (model.symbol(id) && !kids.length) { |
| 61 | const sized = { w: CODE_WIDTH, h: CODE_TOP + codeLineCount(model, id) * LINE_HEIGHT + 8, kids: [] }; |
| 62 | sizes.set(id, sized); |
| 63 | return sized; |
| 64 | } |
| 65 | for (const kid of kids) size(model, kid, sizes); |
| 66 | const { pad, head, gapX, gapY } = spacingOf(model, id); |
| 67 | const columns = storyline(model, id, kids).map((column) => ({ ids: column, w: Math.max(...column.map((kid) => sizes.get(kid)!.w)), h: column.reduce((sum, kid) => sum + sizes.get(kid)!.h, 0) + gapY * (column.length - 1) })); |
| 68 | const placed: [NodeId, number, number][] = []; |
| 69 | let x = pad; |
| 70 | let y = head + pad; |
| 71 | let rowHeight = 0; |
| 72 | let widest = 0; |
| 73 | const rowWidth = wrapWidth(columns, gapX); |
| 74 | for (const column of columns) { |
| 75 | if (x > pad && x + column.w > pad + rowWidth) { |
| 76 | widest = Math.max(widest, x - gapX); |
| 77 | x = pad; |
| 78 | y += rowHeight + gapY * 3; |
| 79 | rowHeight = 0; |
| 80 | } |
| 81 | let top = y; |
| 82 | for (const kid of column.ids) { |
| 83 | placed.push([kid, x, top]); |
| 84 | top += sizes.get(kid)!.h + gapY; |
| 85 | } |
| 86 | rowHeight = Math.max(rowHeight, column.h); |
| 87 | x += column.w + gapX; |
| 88 | } |
| 89 | widest = Math.max(widest, x - gapX); |
| 90 | const height = y + rowHeight; |
| 91 | const sized = { w: Math.max(columns.length ? widest + pad : 0, 200), h: height + pad, kids: placed }; |
| 92 | sizes.set(id, sized); |
| 93 | return sized; |
| 94 | } |
| 95 | |
| 96 | /** The row width at which a storyline wraps, so a block stays close to a screen's shape. */ |
| 97 | function wrapWidth(columns: readonly { w: number; h: number }[], gapX: number): number { |
| 98 | const total = columns.reduce((sum, column) => sum + column.w + gapX, 0); |
| 99 | const tallest = Math.max(0, ...columns.map((column) => column.h)); |
| 100 | if (!tallest || total <= tallest * MAX_ASPECT) return Number.POSITIVE_INFINITY; |
| 101 | return Math.max(...columns.map((column) => column.w), Math.sqrt(total * tallest * MAX_ASPECT * 0.75)); |
| 102 | } |
| 103 | |
| 104 | /** Splits children into columns: a child that uses another sits to its left. Tall columns wrap. */ |
| 105 | function storyline(model: CodeModel, holder: NodeId, kids: readonly NodeId[]): NodeId[][] { |
| 106 | const index = new Map(kids.map((kid, i) => [kid, i])); |
| 107 | const uses = kids.map(() => new Set<number>()); |
| 108 | const childOf = (id: string): number | undefined => { |
| 109 | const chain = model.chain(id); |
| 110 | const at = chain.indexOf(holder); |
| 111 | return at >= 0 ? index.get(chain[at + 1]) : undefined; |
| 112 | }; |
| 113 | for (const edge of model.graph.edges) { |
| 114 | const from = childOf(edge.from); |
| 115 | const to = childOf(edge.to); |
| 116 | if (from !== undefined && to !== undefined && from !== to) uses[from].add(to); |
| 117 | } |
| 118 | const rank = new Array<number>(kids.length).fill(-1); |
| 119 | const visiting = new Set<number>(); |
| 120 | const depth = (i: number): number => { |
| 121 | if (rank[i] >= 0) return rank[i]; |
| 122 | visiting.add(i); |
| 123 | let best = 0; |
| 124 | for (const next of uses[i]) if (!visiting.has(next)) best = Math.max(best, depth(next) + 1); |
| 125 | visiting.delete(i); |
| 126 | rank[i] = best; |
| 127 | return best; |
| 128 | }; |
| 129 | kids.forEach((_, i) => depth(i)); |
| 130 | const top = Math.max(0, ...rank); |
| 131 | const columns: NodeId[][] = []; |
| 132 | kids.forEach((kid, i) => (columns[top - rank[i]] ??= []).push(kid)); |
| 133 | return columns.filter(Boolean).flatMap((column) => Array.from({ length: Math.ceil(column.length / MAX_COLUMN_ITEMS) }, (_, part) => column.slice(part * MAX_COLUMN_ITEMS, (part + 1) * MAX_COLUMN_ITEMS))); |
| 134 | } |
| 135 | |
| 136 | function place(id: NodeId, x: number, y: number, sizes: ReadonlyMap<NodeId, Sized>, boxes: Map<NodeId, Box>) { |
| 137 | const sized = sizes.get(id)!; |
| 138 | boxes.set(id, { x, y, w: sized.w, h: sized.h }); |
| 139 | for (const [kid, kx, ky] of sized.kids) place(kid, x + kx, y + ky, sizes, boxes); |
| 140 | } |