@bevelo/virtual 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,242 @@
1
+ import { c as normalizeLength, i as computeRange, l as sanitizeSize, n as toRangeIndex, s as normalizeCount } from "../measure-CtFm4neI.js";
2
+ // src/pure/headers.ts
3
+ /**
4
+ * @stable — follows semver.
5
+ */
6
+ /**
7
+ * Computes the header rows visible when the body has scrolled by `scrollY`.
8
+ *
9
+ * This is the vertical range maths applied to the header stack instead of the
10
+ * body: prefix sums are built once and the two edges are found with binary
11
+ * searches, so a deep grouped header costs O(n) to lay out and O(log n) per
12
+ * scroll frame. The rows come back in body coordinates, so the renderer does
13
+ * not have to keep its own running total in sync with the scroll position.
14
+ *
15
+ * Unlike {@link computeRange} the scroll is **not** clamped to
16
+ * `total - viewportHeight`. A header band is not a scroller: when the body has
17
+ * scrolled past the whole band there is nothing left to pin, and clamping would
18
+ * pin the last viewport-height of headers forever. Reporting an empty slice is
19
+ * what lets the caller unpin the band (or show a compact summary) at that
20
+ * point. A caller that wants a permanently pinned band passes `scrollY: 0` and
21
+ * lets the band's own clipping handle a header stack taller than the viewport.
22
+ *
23
+ * A row is visible when its half-open extent intersects
24
+ * `[scrollY, scrollY + viewportHeight)`: a row ending exactly at the scroll
25
+ * offset has scrolled past, and a row starting exactly at the bottom edge has
26
+ * not started yet.
27
+ */
28
+ function headerBand({ heights, scrollY, viewportHeight }) {
29
+ const count = normalizeCount(heights.length);
30
+ if (count === 0) return {
31
+ rows: [],
32
+ offset: 0
33
+ };
34
+ const ends = new Float64Array(count + 1);
35
+ let running = 0;
36
+ for (let index = 0; index < count; index += 1) {
37
+ running += sanitizeSize(heights[index] ?? 0);
38
+ ends[index + 1] = running;
39
+ }
40
+ const total = running;
41
+ const viewport = normalizeLength(viewportHeight);
42
+ const scroll = Math.min(normalizeLength(scrollY), total);
43
+ if (scroll >= total) return {
44
+ rows: [],
45
+ offset: total
46
+ };
47
+ const start = firstIndexEndingAfter(ends, count, scroll);
48
+ if (viewport === 0) return {
49
+ rows: [],
50
+ offset: ends[start] ?? 0
51
+ };
52
+ const end = firstIndexStartingAtOrAfter(ends, count, scroll + viewport);
53
+ const rows = [];
54
+ for (let index = start; index < end; index += 1) {
55
+ const y = ends[index] ?? 0;
56
+ rows.push({
57
+ index,
58
+ y,
59
+ height: (ends[index + 1] ?? 0) - y
60
+ });
61
+ }
62
+ return {
63
+ rows,
64
+ offset: ends[start] ?? 0
65
+ };
66
+ }
67
+ /**
68
+ * Resolves grouped header rows to column start indices.
69
+ *
70
+ * Spans are validated instead of coerced: a span that is not a whole number
71
+ * >= 1 is a structural bug in the column definition, not data, and silently
72
+ * rounding it would leave the header cells misaligned with the body columns in
73
+ * a way that only shows up as a visual shift. For the same reason passing
74
+ * `columnCount` makes a span total that disagrees with the grid throw: the
75
+ * renderer can then use `colSpan` and the group's `start` without a second
76
+ * consistency check of its own.
77
+ *
78
+ * `columnCount` is optional so the function can also resolve a header band
79
+ * before the grid reports its own count; the returned total span is the column
80
+ * count in that case, which is exactly the value a caller checks against.
81
+ */
82
+ function groupedHeaders(groups, columnCount) {
83
+ if (columnCount !== void 0) {
84
+ if (!Number.isInteger(columnCount) || columnCount < 0) throw new RangeError(`groupedHeaders columnCount must be a whole number >= 0, received ${String(columnCount)}`);
85
+ }
86
+ const resolved = [];
87
+ let start = 0;
88
+ for (const group of groups) {
89
+ if (!Number.isInteger(group.span) || group.span < 1) throw new RangeError(`header group '${group.id}' needs a whole span >= 1, received ${String(group.span)}`);
90
+ resolved.push({
91
+ id: group.id,
92
+ start,
93
+ span: group.span
94
+ });
95
+ start += group.span;
96
+ }
97
+ if (columnCount !== void 0 && start !== columnCount) throw new RangeError(`header groups span ${start} columns but the grid has ${columnCount}`);
98
+ return resolved;
99
+ }
100
+ /**
101
+ * Windows a wide column set for horizontal scrolling.
102
+ *
103
+ * The same shape as {@link computeRange}, transposed: prefix sums are built
104
+ * once from the widths and the two edges are found by binary search, never by
105
+ * scanning columns. That matters for a grid with tens of thousands of columns:
106
+ * a scan per scroll frame is O(n) while the prefix sums make it O(log n).
107
+ *
108
+ * The scroll offset is clamped to `total - viewportWidth`, because a real
109
+ * scroller clamps `scrollLeft`; without the clamp a stale large offset would
110
+ * render an empty window until the browser sent a corrected scroll event. A
111
+ * viewport wider than the columns therefore always starts at column zero.
112
+ */
113
+ function columnWindow({ widths, scrollX, viewportWidth, overscan }) {
114
+ const count = normalizeCount(widths.length);
115
+ if (count === 0) return {
116
+ start: 0,
117
+ end: 0,
118
+ offset: 0,
119
+ total: 0
120
+ };
121
+ const range = computeRange({
122
+ count,
123
+ scroll: scrollX,
124
+ viewport: viewportWidth,
125
+ estimate: (index) => sanitizeSize(widths[index] ?? 0),
126
+ ...overscan === void 0 ? {} : { overscan }
127
+ });
128
+ return {
129
+ start: range.start,
130
+ end: range.end,
131
+ offset: range.offset,
132
+ total: range.total
133
+ };
134
+ }
135
+ /**
136
+ * The window over a prefix-sum index, for a grid whose columns are lazily measured.
137
+ *
138
+ * The array-shaped {@link columnWindow} has to materialise every width before it can binary
139
+ * search, which is the wrong cost for a sheet with 50,000 columns: the window is 20 of them.
140
+ * This takes the index instead, so the frame costs O(log n) whether the grid has fifty
141
+ * columns or fifty thousand.
142
+ */
143
+ function columnWindowFromIndex(index, options) {
144
+ const rangeIndex = toRangeIndex(index);
145
+ const range = computeRange({
146
+ count: rangeIndex.count,
147
+ scroll: options.scrollX,
148
+ viewport: options.viewportWidth,
149
+ estimate: () => 0,
150
+ index: rangeIndex,
151
+ ...options.overscan === void 0 ? {} : { overscan: options.overscan }
152
+ });
153
+ return {
154
+ start: range.start,
155
+ end: range.end,
156
+ offset: range.offset,
157
+ total: range.total
158
+ };
159
+ }
160
+ function columnWidthCount(widths) {
161
+ return Array.isArray(widths) ? normalizeCount(widths.length) : normalizeCount(widths.count);
162
+ }
163
+ function readColumnWidth(widths, index) {
164
+ return Array.isArray(widths) ? sanitizeSize(widths[index] ?? 0) : sanitizeSize(widths.sizeOf(index));
165
+ }
166
+ /**
167
+ * Left offsets for the frozen leading columns.
168
+ *
169
+ * Frozen columns are painted outside the horizontal window, at their natural
170
+ * positions, so this is a straight prefix sum rather than range maths. Accepts
171
+ * either a plain width array or a indexed SizeSource (so a 50k-column sheet can
172
+ * read only the frozen prefix in O(frozenCount)). The frozen count is clamped to
173
+ * the column count.
174
+ */
175
+ function stickyColumnOffsets(widths, frozenCount) {
176
+ const frozen = clampFrozenCount(frozenCount, columnWidthCount(widths));
177
+ const offsets = [];
178
+ let running = 0;
179
+ for (let index = 0; index < frozen; index += 1) {
180
+ const width = readColumnWidth(widths, index);
181
+ offsets.push({
182
+ index,
183
+ offset: running,
184
+ width
185
+ });
186
+ running += width;
187
+ }
188
+ return offsets;
189
+ }
190
+ /**
191
+ * Right offsets for frozen trailing columns, returned in ascending index order.
192
+ *
193
+ * Each entry's `offset` is its distance in pixels from the trailing edge of the
194
+ * grid (0 for the last column, growing leftward).
195
+ */
196
+ function stickyTrailingColumnOffsets(widths, trailingCount) {
197
+ const count = columnWidthCount(widths);
198
+ const frozen = clampFrozenCount(trailingCount, count);
199
+ if (frozen === 0) return [];
200
+ const start = count - frozen;
201
+ const offsets = new Array(frozen);
202
+ let running = 0;
203
+ for (let index = count - 1; index >= start; index -= 1) {
204
+ const width = readColumnWidth(widths, index);
205
+ offsets[index - start] = {
206
+ index,
207
+ offset: running,
208
+ width
209
+ };
210
+ running += width;
211
+ }
212
+ return offsets;
213
+ }
214
+ /** Freeze counts are whole and non-negative; Infinity means "every column". */
215
+ function clampFrozenCount(value, count) {
216
+ if (value === Infinity) return count;
217
+ if (!Number.isFinite(value) || value <= 0) return 0;
218
+ return Math.min(Math.floor(value), count);
219
+ }
220
+ /** Smallest `index` in `[0, count]` whose end (prefix of index + 1) is past `offset`. */
221
+ function firstIndexEndingAfter(ends, count, offset) {
222
+ let low = 0;
223
+ let high = count;
224
+ while (low < high) {
225
+ const middle = low + high >>> 1;
226
+ if ((ends[middle + 1] ?? 0) <= offset) low = middle + 1;
227
+ else high = middle;
228
+ }
229
+ return low;
230
+ }
231
+ /** Smallest `index` in `[0, count]` whose start (prefix of index) is at or past `offset`. */
232
+ function firstIndexStartingAtOrAfter(ends, count, offset) {
233
+ let low = 0;
234
+ let high = count;
235
+ while (low < high) {
236
+ const middle = low + high >>> 1;
237
+ if ((ends[middle] ?? 0) < offset) low = middle + 1;
238
+ else high = middle;
239
+ }
240
+ return low;
241
+ }
242
+ export { columnWindow, columnWindowFromIndex, groupedHeaders, headerBand, stickyColumnOffsets, stickyTrailingColumnOffsets };
@@ -0,0 +1,19 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_measure = require("../measure-Dg1HYDei.cjs");
3
+ const require_pure = require("../pure-WJ6pwZC6.cjs");
4
+ exports.DEFAULT_OVERSCAN = require_measure.DEFAULT_OVERSCAN;
5
+ exports.alignedOffset = require_pure.alignedOffset;
6
+ exports.anchoredStart = require_pure.anchoredStart;
7
+ exports.captureAnchor = require_pure.captureAnchor;
8
+ exports.computeRange = require_measure.computeRange;
9
+ exports.createSheetSizeIndex = require_pure.createSheetSizeIndex;
10
+ exports.createSizeIndex = require_measure.createSizeIndex;
11
+ exports.defaultRangeExtractor = require_pure.defaultRangeExtractor;
12
+ exports.gridCells = require_pure.gridCells;
13
+ exports.gridWindow = require_pure.gridWindow;
14
+ exports.keepMounted = require_pure.keepMounted;
15
+ exports.normalizeIndices = require_pure.normalizeIndices;
16
+ exports.restoreAnchor = require_pure.restoreAnchor;
17
+ exports.toRangeIndex = require_measure.toRangeIndex;
18
+ exports.trackOf = require_pure.trackOf;
19
+ exports.withStickyIndices = require_pure.withStickyIndices;
@@ -0,0 +1,3 @@
1
+ import { a as SizeSource, c as SizeIndex, d as toRangeIndex, f as DEFAULT_OVERSCAN, g as computeRange, h as RangeInput, i as GridWindowOptions, l as SizeIndexOptions, m as RangeIndex, n as GridCell, o as gridCells, p as Range, r as GridWindow, s as gridWindow, t as FrozenEdges, u as createSizeIndex } from "../grid-BzP7CurM.cjs";
2
+ import { _ as withStickyIndices, a as anchoredStart, c as trackOf, d as createSheetSizeIndex, f as ExtractContext, g as normalizeIndices, h as keepMounted, i as alignedOffset, l as SheetSizeIndex, m as defaultRangeExtractor, n as View, o as captureAnchor, p as RangeExtractor, r as VirtualizerAlign, s as restoreAnchor, t as Anchor, u as SheetSizeIndexOptions } from "../index-D_M9i1VO.cjs";
3
+ export { type Anchor, DEFAULT_OVERSCAN, type ExtractContext, type FrozenEdges, type GridCell, type GridWindow, type GridWindowOptions, type Range, type RangeExtractor, type RangeIndex, type RangeInput, type SheetSizeIndex, type SheetSizeIndexOptions, type SizeIndex, type SizeIndexOptions, type SizeSource, type View, type VirtualizerAlign, alignedOffset, anchoredStart, captureAnchor, computeRange, createSheetSizeIndex, createSizeIndex, defaultRangeExtractor, gridCells, gridWindow, keepMounted, normalizeIndices, restoreAnchor, toRangeIndex, trackOf, withStickyIndices };
@@ -0,0 +1,3 @@
1
+ import { a as SizeSource, c as SizeIndex, d as toRangeIndex, f as DEFAULT_OVERSCAN, g as computeRange, h as RangeInput, i as GridWindowOptions, l as SizeIndexOptions, m as RangeIndex, n as GridCell, o as gridCells, p as Range, r as GridWindow, s as gridWindow, t as FrozenEdges, u as createSizeIndex } from "../grid-BzP7CurM.js";
2
+ import { _ as withStickyIndices, a as anchoredStart, c as trackOf, d as createSheetSizeIndex, f as ExtractContext, g as normalizeIndices, h as keepMounted, i as alignedOffset, l as SheetSizeIndex, m as defaultRangeExtractor, n as View, o as captureAnchor, p as RangeExtractor, r as VirtualizerAlign, s as restoreAnchor, t as Anchor, u as SheetSizeIndexOptions } from "../index-B0Ml3oq4.js";
3
+ export { type Anchor, DEFAULT_OVERSCAN, type ExtractContext, type FrozenEdges, type GridCell, type GridWindow, type GridWindowOptions, type Range, type RangeExtractor, type RangeIndex, type RangeInput, type SheetSizeIndex, type SheetSizeIndexOptions, type SizeIndex, type SizeIndexOptions, type SizeSource, type View, type VirtualizerAlign, alignedOffset, anchoredStart, captureAnchor, computeRange, createSheetSizeIndex, createSizeIndex, defaultRangeExtractor, gridCells, gridWindow, keepMounted, normalizeIndices, restoreAnchor, toRangeIndex, trackOf, withStickyIndices };
@@ -0,0 +1,3 @@
1
+ import { i as computeRange, n as toRangeIndex, r as DEFAULT_OVERSCAN, t as createSizeIndex } from "../measure-CtFm4neI.js";
2
+ import { a as trackOf, c as gridWindow, d as normalizeIndices, f as withStickyIndices, i as restoreAnchor, l as defaultRangeExtractor, n as anchoredStart, o as createSheetSizeIndex, r as captureAnchor, s as gridCells, t as alignedOffset, u as keepMounted } from "../pure-jv1Ox9rG.js";
3
+ export { DEFAULT_OVERSCAN, alignedOffset, anchoredStart, captureAnchor, computeRange, createSheetSizeIndex, createSizeIndex, defaultRangeExtractor, gridCells, gridWindow, keepMounted, normalizeIndices, restoreAnchor, toRangeIndex, trackOf, withStickyIndices };