`layoutGraph` 把「子先于父」的提交前缀排成泳道:首父尽量继承父提交的车道与 颜色,新分支与断开的 tip 单调分配色号,空闲车道复用但活着的车道永不平移 (跨分页每一行逐字不变,宽度与悬空边也一样)。 连线几何单独放在 `edgePath` 里,一个弯只拐一次,并且拐在哪儿决定了每条线的 颜色是否统一: - **开启新线的边**(合并提交的第二父)从节点水平出线、圆角拐进自己的新车道; - **并入既有线的边**(分叉收尾)沿**自己**的车道一路向下,再在行边界横切进 目标车道——反过来画就会把这条分支的颜色涂到它并入的主线上(实测报过 「红线上仍然有一段绿」); - 横切段的中心放在行边界上方半个描边宽(`STROKE_INSET`):行是后绘制的兄弟 元素,溢出行外的半条描边会被下一行的背景吃掉,实测 8× 像素扫描只有 0.75px (正常 1.5px),内缩之后下边缘正好落在边界上,与下一行的竖线无缝相接。 布局还给每条出边记了 `join` 标记(这条边是并入还是开启),颜色与几何都由它 决定;同一条出边不会在目标车道里留下垂直线段。 测试:`tests/graph-layout.spec.ts` 覆盖空图 / 孤立根、线性历史的稳定颜色、 合并只与共同祖先汇合、octopus 与重复父提交、断开 tip 不接顺路车道、任意 分页边界逐行稳定、冻结输入与调用间无共享状态、随机 DAG 的连续性走查,以及 上面三条几何契约(含「同一行同一车道不得出现两种颜色」的全组合断言)。
197 lines
8.3 KiB
TypeScript
197 lines
8.3 KiB
TypeScript
export interface GraphNode {
|
||
readonly hashFull: string
|
||
readonly parents: readonly string[]
|
||
}
|
||
|
||
/** Corner radius of a lane-change bend, clamped per edge by its real estate. */
|
||
const CORNER_RADIUS = 4
|
||
|
||
/**
|
||
* Half of the rail stroke (`graph.module.css` paints `stroke-width: 1.5`).
|
||
*
|
||
* A join edge's crossing runs along the row's BOTTOM EDGE, and everything the
|
||
* SVG paints outside its box is covered by the NEXT row's background (siblings
|
||
* paint in order — `overflow: visible` alone cannot win that fight). Centring
|
||
* the crossing half a stroke above the boundary keeps the whole line inside the
|
||
* row while its lower edge still lands exactly ON the boundary, where the next
|
||
* row's vertical picks it up — measured at 8× zoom, the crossing renders the
|
||
* full 1.5px instead of the 0.75px half a stroke that spilled.
|
||
*/
|
||
const STROKE_INSET = 0.75
|
||
|
||
/**
|
||
* The SVG path of one segment — a "rectangular" edge with ONE rounded elbow,
|
||
* never a bare diagonal.
|
||
*
|
||
* WHERE the elbow sits is what keeps every line one colour. An edge that
|
||
* OPENS a line (a merge's second parent) leaves the node horizontally and
|
||
* rounds down into its own new lane. An edge that JOINS a line (a branch
|
||
* ending at its fork) must instead run down its OWN lane and then turn into
|
||
* the surviving lane along the row boundary: rounding into the target lane
|
||
* and dropping there would paint a piece of the leaving branch's colour
|
||
* straight onto the surviving line — the exact "there is still a green
|
||
* stretch on the red main line" defect.
|
||
*
|
||
* A full-row crossing (neither end at a node; the current layout never emits
|
||
* one) bends twice at mid-height. Coordinates stay in the row-fraction space
|
||
* of {@link GraphSegment}; callers scale by the row height.
|
||
*/
|
||
export function edgePath(segment: Pick<GraphSegment, 'fromLane' | 'toLane' | 'fromY' | 'toY' | 'join'>, laneX: (lane: number) => number, rowHeight: number): string {
|
||
const x1 = laneX(segment.fromLane)
|
||
const x2 = laneX(segment.toLane)
|
||
const y1 = segment.fromY * rowHeight
|
||
const y2 = segment.toY * rowHeight
|
||
if (x1 === x2 || y1 === y2) return `M ${x1} ${y1} L ${x2} ${y2}`
|
||
const dx = Math.sign(x2 - x1)
|
||
const dy = Math.sign(y2 - y1)
|
||
const r = Math.min(CORNER_RADIUS, Math.abs(x2 - x1) / 2, Math.abs(y2 - y1) / 2)
|
||
if (segment.join || segment.toY === 0.5) {
|
||
// The leaving line keeps its own lane all the way down, then crosses over
|
||
// along the row boundary — no vertical inside the lane it joins, and (see
|
||
// STROKE_INSET) no half-stroke for the next row's background to eat.
|
||
const yb = y2 - STROKE_INSET
|
||
// The elbow's radius is also bounded by the run left ABOVE the inset, so a
|
||
// degenerate (very short) row can never curve past the node.
|
||
const rj = Math.min(r, Math.abs(yb - y1) / 2)
|
||
return `M ${x1} ${y1} L ${x1} ${yb - dy * rj} Q ${x1} ${yb} ${x1 + dx * rj} ${yb} L ${x2} ${yb}`
|
||
}
|
||
if (segment.fromY === 0.5) {
|
||
return `M ${x1} ${y1} L ${x2 - dx * r} ${y1} Q ${x2} ${y1} ${x2} ${y1 + dy * r} L ${x2} ${y2}`
|
||
}
|
||
const ym = (y1 + y2) / 2
|
||
return `M ${x1} ${y1} L ${x1} ${ym - dy * r} Q ${x1} ${ym} ${x1 + dx * r} ${ym} L ${x2 - dx * r} ${ym} Q ${x2} ${ym} ${x2} ${ym + dy * r} L ${x2} ${y2}`
|
||
}
|
||
|
||
|
||
/** Coordinates are lane indices horizontally and fractions of one row vertically. */
|
||
export interface GraphSegment {
|
||
readonly fromLane: number
|
||
readonly toLane: number
|
||
readonly fromY: 0 | 0.5
|
||
readonly toY: 0.5 | 1
|
||
readonly colorIndex: number
|
||
/** True when this edge joins a line another child already claimed (a branch
|
||
* line ENDING at its fork point). A join stub wears the ENDING line's
|
||
* colour — the node's own — while the surviving line keeps its colour; the
|
||
* reverse convention would paint the first stretch of every fork in the
|
||
* colour of the line it joins. An edge that OPENS a parent's new line wears
|
||
* that new line's colour instead. */
|
||
readonly join: boolean
|
||
/** The ancestor this line is travelling towards; not necessarily in this page. */
|
||
readonly targetHash: string
|
||
}
|
||
|
||
export interface GraphLayoutRow {
|
||
readonly hashFull: string
|
||
readonly lane: number
|
||
readonly colorIndex: number
|
||
/** Width needed by this row, including passing lines (not the whole graph). */
|
||
readonly laneCount: number
|
||
readonly segments: readonly GraphSegment[]
|
||
}
|
||
|
||
interface Reservation {
|
||
hash: string
|
||
lane: number
|
||
colorIndex: number
|
||
}
|
||
|
||
/**
|
||
* Lay out a unique, child-before-parent commit prefix, without looking ahead.
|
||
* Render segments before the node, whose center is (lane, 0.5). Scale x by the
|
||
* lane spacing (plus padding), y by row height. A crossing is not a junction;
|
||
* only an endpoint at a commit center represents a connection to that commit.
|
||
*
|
||
* Recompute on the entire concatenated prefix when fetching another page: every
|
||
* previously returned row, including its width and dangling edges, is unchanged.
|
||
* Missing parents deliberately remain reserved through the bottom edge. Do not
|
||
* truncate them based on whether a parent is currently loaded.
|
||
*
|
||
* First-parent continuations inherit their lane/color when possible. Other new
|
||
* branches and disconnected tips get monotonically allocated color indices;
|
||
* callers may reduce these modulo a theme palette. Vacant lanes are reused but
|
||
* live lanes never shift, avoiding accidental connections and pagination jitter.
|
||
*
|
||
* Cost is O(sum of frontier widths + edges), with O(frontier width) working
|
||
* space, excluding output. Width includes vacant slots between live lanes.
|
||
*/
|
||
export function layoutGraph(nodes: readonly GraphNode[]): GraphLayoutRow[] {
|
||
const lanes: (Reservation | undefined)[] = []
|
||
const targets = new Map<string, Reservation>()
|
||
const rows: GraphLayoutRow[] = []
|
||
let nextColor = 0
|
||
|
||
for (const node of nodes) {
|
||
// One forward scan per row, even for an octopus with many new parents.
|
||
let freeCursor = 0
|
||
/** The next free lane, never the lane this row's node sits on: the node's
|
||
* consumed lane LOOKS free, but the row still draws strokes there (the
|
||
* node's own outgoing edges and its dot), so handing it to a new line
|
||
* would paint that line's colour over the node's line — the reported
|
||
* "there is still a green stretch on the red main line". `-1` means no
|
||
* exclusion (the node has not picked its lane yet). */
|
||
const freeLane = (exclude: number) => {
|
||
while (lanes[freeCursor] !== undefined || freeCursor === exclude) freeCursor++
|
||
return freeCursor++
|
||
}
|
||
const incoming = targets.get(node.hashFull)
|
||
const lane = incoming?.lane ?? freeLane(-1)
|
||
const colorIndex = incoming?.colorIndex ?? nextColor++
|
||
const segments: GraphSegment[] = []
|
||
let laneCount = Math.max(lanes.length, lane + 1)
|
||
|
||
for (const reservation of lanes) {
|
||
if (!reservation) continue
|
||
segments.push({
|
||
fromLane: reservation.lane,
|
||
toLane: reservation.lane,
|
||
fromY: 0,
|
||
toY: reservation === incoming ? 0.5 : 1,
|
||
colorIndex: reservation.colorIndex,
|
||
join: false,
|
||
targetHash: reservation.hash,
|
||
})
|
||
}
|
||
if (incoming) {
|
||
targets.delete(node.hashFull)
|
||
lanes[lane] = undefined
|
||
}
|
||
|
||
const parents = new Set(node.parents)
|
||
let firstParent = true
|
||
for (const hash of parents) {
|
||
let reservation = targets.get(hash)
|
||
const opening = reservation === undefined
|
||
if (!reservation) {
|
||
// The consumed node's lane is available even if freeCursor passed it.
|
||
const parentLane = firstParent ? lane : freeLane(lane)
|
||
reservation = {
|
||
hash,
|
||
lane: parentLane,
|
||
colorIndex: firstParent ? colorIndex : nextColor++,
|
||
}
|
||
lanes[parentLane] = reservation
|
||
targets.set(hash, reservation)
|
||
}
|
||
segments.push({
|
||
fromLane: lane,
|
||
toLane: reservation.lane,
|
||
fromY: 0.5,
|
||
toY: 1,
|
||
// An edge OPENING the parent's new line wears that line's colour; an
|
||
// edge JOINING an already-claimed parent is the tail of THIS node's
|
||
// line and wears the node's own colour all the way into the fork.
|
||
colorIndex: opening ? reservation.colorIndex : colorIndex,
|
||
join: !opening,
|
||
targetHash: hash,
|
||
})
|
||
laneCount = Math.max(laneCount, reservation.lane + 1)
|
||
firstParent = false
|
||
}
|
||
|
||
rows.push({ hashFull: node.hashFull, lane, colorIndex, laneCount, segments })
|
||
while (lanes.length && lanes[lanes.length - 1] === undefined) lanes.pop()
|
||
}
|
||
return rows
|
||
}
|