/** * Row geometry for table-shaped virtual windows. Pure measurement helpers — * no state kernel: selection, identity and focus stay with the existing * collection/table models. Heights are px; a row height function returning * `undefined` falls back to `estimatedRowHeight`, and every input is read on * each layout pass (prefix sums, binary search) so reactive values update the * window without a remount. * * Geometry semantics match the pinned ListLayout/TableLayout contracts: `gap` * is the spacing between rows (body rows and the trailing loader row), while * `padding` is list padding rendered as spacer rows at both ends and carries * no gap. The loader area is a reservation at the end of the row stream (after * the last row's gap and before the trailing padding): it is included in * `totalHeight` and in `window.padBottom`, so a caller rendering a physical * loader row should place it inside that reserved span. */ import { computed, shallowRef, toValue, type ComputedRef, type MaybeRefOrGetter, } from "vue"; export type VirtualRowHeight = number | ((index: number) => number | undefined); export interface UseVirtualRowsOptions { /** Number of virtualized body rows. */ count: MaybeRefOrGetter; /** Row height in px; a function maps the row index to a variable height. */ rowHeight: VirtualRowHeight; /** Fallback height in px for rows whose height function returns undefined. */ estimatedRowHeight?: number; /** Loader row reservation in px at the end of the row stream. */ loaderHeight?: number; /** Gap in px between rows (body rows and the trailing loader reservation). */ gap?: number; /** List padding in px as spacer rows at both ends. */ padding?: number; /** Extra rows rendered beyond the viewport. */ overscan?: number; } export interface VirtualRowWindow { /** First rendered body row index. */ start: number; /** Body row index after the last rendered row. */ end: number; /** Spacer height in px before the first rendered row (incl. padding). */ padTop: number; /** * Spacer height in px after the last rendered row: the unrendered rows, the * loader reservation and the trailing padding. */ padBottom: number; } export interface UseVirtualRows { window: ComputedRef; /** Top offset in px of a body row within the row stream. */ rowOffsetOf: (index: number) => number; /** Rendered extent in px of a body row (its height plus the gap after it). */ rowExtentOf: (index: number) => number; /** Total row-stream height in px (padding, rows, gaps, loader reservation). */ totalHeight: ComputedRef; /** Recompute the window from a scroll position. */ update: (scrollTop: number, viewportHeight: number) => void; /** Scroll a row into a viewport element, clamped to the row stream. */ scrollToIndex: (element: HTMLElement, index: number) => void; } export function useVirtualRows(options: UseVirtualRowsOptions): UseVirtualRows { const overscan = options.overscan ?? 8; const state = shallowRef({ scrollTop: 0, viewport: 0 }); /** Prefix sums: offsets[i] = row stream top of body row i (incl. padding). */ const geometry = computed(() => { const count = Math.max(0, toValue(options.count) ?? 0); const gap = (options.gap ?? 0) > 0 ? (options.gap ?? 0) : 0; const pad = (options.padding ?? 0) > 0 ? (options.padding ?? 0) : 0; const loader = (options.loaderHeight ?? 0) > 0 ? (options.loaderHeight ?? 0) : 0; // Rows and the loader reservation are content rows: gaps sit between // them, never between padding and content. const loaderSpan = loader > 0 ? (count > 0 ? gap : 0) + loader : 0; const offsets = new Float64Array(count + 1); let top = pad; for (let i = 0; i < count; i++) { offsets[i] = top; const raw = typeof options.rowHeight === "function" ? options.rowHeight(i) : options.rowHeight; top += raw ?? options.estimatedRowHeight ?? 50; if (i < count - 1) top += gap; } offsets[count] = top; return { count, pad, loaderSpan, offsets }; }); const rowOffsetOf = (index: number) => geometry.value.offsets[Math.min(Math.max(index, 0), geometry.value.count)]; const rowExtentOf = (index: number) => rowOffsetOf(index + 1) - rowOffsetOf(index); const totalHeight = computed( () => geometry.value.offsets[geometry.value.count]! + geometry.value.loaderSpan + geometry.value.pad, ); const window = computed(() => { const { count, offsets } = geometry.value; const { scrollTop, viewport } = state.value; // First row whose extent reaches the viewport top. let low = 0; let high = count; while (low < high) { const mid = (low + high) >> 1; if (offsets[mid + 1]! <= scrollTop) low = mid + 1; else high = mid; } const start = Math.max(0, low - overscan); // First row starting beyond the viewport bottom. let endLow = start; let endHigh = count; const limit = scrollTop + viewport; while (endLow < endHigh) { const mid = (endLow + endHigh) >> 1; if (offsets[mid]! < limit) endLow = mid + 1; else endHigh = mid; } const end = Math.min(count, endLow + overscan); return { start, end, padTop: rowOffsetOf(start), padBottom: Math.max(0, totalHeight.value - rowOffsetOf(end)), }; }); function update(scrollTop: number, viewportHeight: number) { state.value = { scrollTop, viewport: viewportHeight }; } function scrollToIndex(element: HTMLElement, index: number) { const top = rowOffsetOf(index); const extent = rowExtentOf(index); if (top < element.scrollTop) element.scrollTop = top; else if (top + extent > element.scrollTop + element.clientHeight) element.scrollTop = top + extent - element.clientHeight; update(element.scrollTop, element.clientHeight); } return { window, rowOffsetOf, rowExtentOf, totalHeight, update, scrollToIndex }; } /** * Attach the window updater to a scroll container: recomputes on scroll and on * container resize. Returns a cleanup callable. */ export function bindVirtualRows( rows: UseVirtualRows, element: HTMLElement, ): () => void { const measure = () => rows.update(element.scrollTop, element.clientHeight); const observer = typeof ResizeObserver !== "undefined" ? new ResizeObserver(measure) : null; observer?.observe(element); element.addEventListener("scroll", measure, { passive: true }); measure(); return () => { element.removeEventListener("scroll", measure); observer?.disconnect(); }; }