@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.
- package/LICENSE +21 -0
- package/README.md +49 -2
- package/dist/grid-BzP7CurM.d.cts +264 -0
- package/dist/grid-BzP7CurM.d.ts +264 -0
- package/dist/index-B0Ml3oq4.d.ts +133 -0
- package/dist/index-D_M9i1VO.d.cts +133 -0
- package/dist/live/index.cjs +537 -0
- package/dist/live/index.d.cts +110 -0
- package/dist/live/index.d.ts +110 -0
- package/dist/live/index.js +514 -0
- package/dist/measure-CtFm4neI.js +419 -0
- package/dist/measure-Dg1HYDei.cjs +472 -0
- package/dist/pure/headers.cjs +248 -0
- package/dist/pure/headers.d.cts +173 -0
- package/dist/pure/headers.d.ts +173 -0
- package/dist/pure/headers.js +242 -0
- package/dist/pure/index.cjs +19 -0
- package/dist/pure/index.d.cts +3 -0
- package/dist/pure/index.d.ts +3 -0
- package/dist/pure/index.js +3 -0
- package/dist/pure-WJ6pwZC6.cjs +514 -0
- package/dist/pure-jv1Ox9rG.js +443 -0
- package/package.json +48 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bevelo contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,50 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bevelo/virtual
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Windowing for long lists and grids. Given a scroll offset, a viewport size and the item
|
|
4
|
+
sizes, it answers which items to render and where they sit. Variable and measured
|
|
5
|
+
heights, lanes, sticky indices, two-axis grids, header bands and scroll anchoring are
|
|
6
|
+
covered. The maths is pure and lives in `@bevelo/virtual/core`; `createVirtualizer` binds
|
|
7
|
+
it to a runtime scroller, an element or the window.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pnpm add @bevelo/virtual
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createVirtualizer } from "@bevelo/virtual";
|
|
19
|
+
|
|
20
|
+
const list = createVirtualizer({
|
|
21
|
+
scroller: () => scrollEl,
|
|
22
|
+
count: messages.length,
|
|
23
|
+
key: (i) => messages[i].id, // sizes and the scroll anchor follow keys
|
|
24
|
+
estimate: 96,
|
|
25
|
+
anchor: "end", // start at the end, follow appends while at the end
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
list.subscribe(() => render(list.get().items)); // each item: key, index, start, size
|
|
29
|
+
// Give each rendered item data-key={item.key} and ref={list.measure}.
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The pure maths stands alone:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { computeRange, createSizeIndex, toRangeIndex } from "@bevelo/virtual/core";
|
|
36
|
+
|
|
37
|
+
const sizes = createSizeIndex({ count: 10_000, estimate: () => 32 });
|
|
38
|
+
const range = computeRange({ count: 10_000, scroll: 4_000, viewport: 600, estimate: () => 32, index: toRangeIndex(sizes) });
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Stability
|
|
42
|
+
|
|
43
|
+
The range, measure, extractor, grid and header maths are `@stable` and follow semver. Key
|
|
44
|
+
anchors, `alignedOffset`, `trackOf` and `createVirtualizer` are
|
|
45
|
+
`@experimental` and may change in a minor release.
|
|
46
|
+
|
|
47
|
+
## Docs
|
|
48
|
+
|
|
49
|
+
See the lists and trees behavior pages in the
|
|
50
|
+
[Bevelo docs](https://github.com/bevelo/bevelo/tree/main/apps/docs/src/content/docs/core/behaviors).
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/** How many extra items to render on each side of the viewport. */
|
|
2
|
+
declare const DEFAULT_OVERSCAN = 1;
|
|
3
|
+
/**
|
|
4
|
+
* Everything the range math needs. It takes accessors instead of a prepared
|
|
5
|
+
* index so a first frame — before anything has been measured — is exactly as
|
|
6
|
+
* valid as a later one: unknown sizes fall through to `estimate`.
|
|
7
|
+
*/
|
|
8
|
+
interface RangeInput {
|
|
9
|
+
readonly count: number;
|
|
10
|
+
/** Current scroll offset of the scroller, in pixels. */
|
|
11
|
+
readonly scroll: number;
|
|
12
|
+
/** Visible height of the scroller, in pixels. */
|
|
13
|
+
readonly viewport: number;
|
|
14
|
+
/** Extra items above and below the visible window; defaults to {@link DEFAULT_OVERSCAN}. */
|
|
15
|
+
readonly overscan?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Pixels of the scroller's content that precede this list: a toolbar, a page header, a
|
|
18
|
+
* hero. The scroll offset is reduced by it, so item 0 sits at the viewport's top edge when
|
|
19
|
+
* the list itself reaches it.
|
|
20
|
+
*
|
|
21
|
+
* `total` never includes the margin, and `offset` stays list-relative. Item offsets that
|
|
22
|
+
* included the margin would make every renderer subtract it again; list-relative geometry
|
|
23
|
+
* means a host places rows inside the list and never does that
|
|
24
|
+
* arithmetic.
|
|
25
|
+
*/
|
|
26
|
+
readonly scrollMargin?: number;
|
|
27
|
+
/** Size for an item that has not been measured yet. Called once per item per compute. */
|
|
28
|
+
readonly estimate: (index: number) => number;
|
|
29
|
+
/** Measured size, or `undefined` when the item is still estimated. */
|
|
30
|
+
readonly measured?: (index: number) => number | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* How many items share a row. Defaults to 1, which is a plain list.
|
|
33
|
+
*
|
|
34
|
+
* With `lanes: 3` items 0, 1 and 2 are the first row, 3, 4 and 5 the second. The row's
|
|
35
|
+
* height is the tallest of its items — the alternative, summing them, would reserve
|
|
36
|
+
* space nothing occupies. The window is still in item indices so `end - start` remains
|
|
37
|
+
* the number of rows to render, which is what a host keys on.
|
|
38
|
+
*/
|
|
39
|
+
readonly lanes?: number;
|
|
40
|
+
/**
|
|
41
|
+
* A live prefix-sum index over the same items, when the caller keeps one.
|
|
42
|
+
*
|
|
43
|
+
* The O(n) scan below is the price of accepting accessors: a first frame has no index to
|
|
44
|
+
* hand over. Once measurements start arriving the same list is scanned once per
|
|
45
|
+
* measurement, which is O(n) per row and O(n²) for a screenful — measured at 1.03ms per
|
|
46
|
+
* row for a 20,000-row list against 0.057ms for a 1,000-row one, an 18× spread on the same
|
|
47
|
+
* work. With an index the window is two binary searches, so a measurement costs O(log n)
|
|
48
|
+
* and the spread disappears.
|
|
49
|
+
*
|
|
50
|
+
* The index must answer `offsetOf` and `indexAt` for the sizes in effect; a caller that
|
|
51
|
+
* passes a stale one gets a window that disagrees with its content, which is why this is
|
|
52
|
+
* opt-in rather than inferred.
|
|
53
|
+
*/
|
|
54
|
+
readonly index?: RangeIndex;
|
|
55
|
+
}
|
|
56
|
+
/** The two lookups a range needs from a prefix-sum index. */
|
|
57
|
+
interface RangeIndex {
|
|
58
|
+
/**
|
|
59
|
+
* The number of items the index describes.
|
|
60
|
+
*
|
|
61
|
+
* Required, not inferred: the window's `end` is clamped to the count, and an index that
|
|
62
|
+
* answered `offsetOf` for an item the caller's data no longer has would otherwise produce
|
|
63
|
+
* a window over rows that do not exist. A `SizeIndex` already exposes it.
|
|
64
|
+
*/
|
|
65
|
+
readonly count: number;
|
|
66
|
+
/** Total pixel size of every item. */
|
|
67
|
+
readonly total: number;
|
|
68
|
+
/** Pixel offset of `index` from the start; `offsetOf(count)` is `total`. */
|
|
69
|
+
offsetOf(index: number): number;
|
|
70
|
+
/** The item at `offset`, clamped to the last item. */
|
|
71
|
+
indexAt(offset: number): number;
|
|
72
|
+
/** Optional O(log n) lower bound: smallest index `i` in `[0, count]` with `offsetOf(i) >= offset`. */
|
|
73
|
+
readonly lowerBound?: (offset: number) => number;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The window to render, in logical indices and pixels. `end` is exclusive, and
|
|
77
|
+
* `offset` is the absolute pixel offset of `start` — the translate to put on
|
|
78
|
+
* the first rendered item so the window lands where the scroll position says.
|
|
79
|
+
*/
|
|
80
|
+
interface Range {
|
|
81
|
+
readonly start: number;
|
|
82
|
+
/** Exclusive: `end - start` items are rendered. */
|
|
83
|
+
readonly end: number;
|
|
84
|
+
/** Prefix-sum offset of `start` in pixels; the translate for the first item. */
|
|
85
|
+
readonly offset: number;
|
|
86
|
+
/** Pixel size of the rendered window, i.e. `offsetOf(end) - offsetOf(start)`. */
|
|
87
|
+
readonly size: number;
|
|
88
|
+
/** Pixel size of the whole list. */
|
|
89
|
+
readonly total: number;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Computes the render window for a scroll position.
|
|
93
|
+
*
|
|
94
|
+
* Construction is O(n): the input hands us accessors, not a prefix sum, so the
|
|
95
|
+
* call must materialise one before it can binary search. The lookups themselves
|
|
96
|
+
* are O(log n) — two binary searches over the prefix sums, never a scan. The
|
|
97
|
+
* alternative (require a caller-owned {@link SizeIndex}) was rejected because a
|
|
98
|
+
* first render has no index to hand over, and that is exactly the frame this
|
|
99
|
+
* function exists to get right.
|
|
100
|
+
*/
|
|
101
|
+
declare function computeRange(input: RangeInput): Range;
|
|
102
|
+
/** What a size index is built from: a fixed item count and a fallback estimate. */
|
|
103
|
+
interface SizeIndexOptions {
|
|
104
|
+
readonly count: number;
|
|
105
|
+
/** Size for an index that has not been measured. Called once per index, at creation. */
|
|
106
|
+
readonly estimate: (index: number) => number;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* A live size source for one list. Prefix sums answer "where does item i start"
|
|
110
|
+
* and "which item is at offset x" in O(log n), so a list of unknown heights does
|
|
111
|
+
* not turn every scroll frame into a scan.
|
|
112
|
+
*/
|
|
113
|
+
interface SizeIndex {
|
|
114
|
+
/** Number of items; fixed for the lifetime of the index. */
|
|
115
|
+
readonly count: number;
|
|
116
|
+
/** Records a measurement; out-of-range indices and invalid sizes are ignored. */
|
|
117
|
+
set(index: number, size: number): void;
|
|
118
|
+
/** Resets a single measured index back to its initial estimate in O(log n). */
|
|
119
|
+
reset(index: number): void;
|
|
120
|
+
/** Measured size when present, else the estimate. Out-of-range indices clamp to an item. */
|
|
121
|
+
sizeOf(index: number): number;
|
|
122
|
+
/** Sum of every size, in pixels. */
|
|
123
|
+
total(): number;
|
|
124
|
+
/** Forgets every measurement and returns to the estimates. */
|
|
125
|
+
clear(): void;
|
|
126
|
+
/** Whether `index` has a real measurement (as opposed to the estimate); out-of-range is false. */
|
|
127
|
+
hasMeasured(index: number): boolean;
|
|
128
|
+
/**
|
|
129
|
+
* Pixel offset of `index` from the start of the list. `offsetOf(0)` is 0 and
|
|
130
|
+
* `offsetOf(count)` is {@link SizeIndex.total}.
|
|
131
|
+
*/
|
|
132
|
+
offsetOf(index: number): number;
|
|
133
|
+
/**
|
|
134
|
+
* Index of the item occupying `offset`; `-1` when the list is empty. An offset
|
|
135
|
+
* on an item boundary belongs to the item that starts there, an offset at or
|
|
136
|
+
* past the end belongs to the last item.
|
|
137
|
+
*/
|
|
138
|
+
indexAt(offset: number): number;
|
|
139
|
+
/**
|
|
140
|
+
* Smallest index `i` in `[0, count]` whose `offsetOf(i) >= offset`, in O(log n).
|
|
141
|
+
*/
|
|
142
|
+
lowerBound(offset: number): number;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Builds the prefix-sum index.
|
|
146
|
+
*
|
|
147
|
+
* A Fenwick tree is used instead of an array of prefix sums plus a dirty marker:
|
|
148
|
+
* a measurement must not cost O(n), because measurements arrive one per item per
|
|
149
|
+
* frame while the user is scrolling. `set` therefore touches only the O(log n)
|
|
150
|
+
* tree nodes whose ranges cover the index — the smallest suffix information the
|
|
151
|
+
* measurement can change. `offsetOf`, `total` and `indexAt` are O(log n);
|
|
152
|
+
* `sizeOf` and `hasMeasured` are O(1). `clear` is O(n) because it must rebuild
|
|
153
|
+
* every node; an epoch counter could make it O(1) but would leak the old array
|
|
154
|
+
* until the index itself is dropped, and clearing is a data change, not a frame.
|
|
155
|
+
*
|
|
156
|
+
* Estimates are snapshotted at creation, so the prefix sums and `sizeOf` can
|
|
157
|
+
* never disagree. Changing the estimate means a new index; the estimate is a
|
|
158
|
+
* property of the data model, not of the scroll position. For the same reason a
|
|
159
|
+
* list whose item count changes needs a new index: a kept index would answer for
|
|
160
|
+
* the old count and silently miss the new items.
|
|
161
|
+
*/
|
|
162
|
+
declare function createSizeIndex({ count, estimate }: SizeIndexOptions): SizeIndex;
|
|
163
|
+
/**
|
|
164
|
+
* Presents a {@link SizeIndex} as a {@link RangeIndex}.
|
|
165
|
+
*
|
|
166
|
+
* The two differ by one fact: `total()` is a method here and a property there, and that
|
|
167
|
+
* is exactly the kind of difference that silently reads a function as a size: `index.total`
|
|
168
|
+
* would be a function, the window's arithmetic would produce `NaN`, and every offset would
|
|
169
|
+
* come back zero. One adapter, exported, so the grid and the DOM layer cannot each get it subtly wrong.
|
|
170
|
+
*/
|
|
171
|
+
declare function toRangeIndex(index: SizeIndex | RangeIndex): RangeIndex;
|
|
172
|
+
/**
|
|
173
|
+
* A frozen edge: a number of leading rows or columns that stay painted while the rest
|
|
174
|
+
* scrolls. Distinct from a window's overscan, which only widens what is rendered.
|
|
175
|
+
*/
|
|
176
|
+
interface FrozenEdges {
|
|
177
|
+
/** Leading rows painted outside the vertical window. */
|
|
178
|
+
readonly rows?: number;
|
|
179
|
+
/** Leading columns painted outside the horizontal window. */
|
|
180
|
+
readonly columns?: number;
|
|
181
|
+
}
|
|
182
|
+
/** The area a grid should render. */
|
|
183
|
+
interface GridWindow {
|
|
184
|
+
/** The rows to render. `rows.offset` is the y translate for the first rendered row. */
|
|
185
|
+
readonly rows: Range;
|
|
186
|
+
/** The columns to render. `columns.offset` is the x translate for the first one. */
|
|
187
|
+
readonly columns: Range;
|
|
188
|
+
/**
|
|
189
|
+
* Leading rows that must stay painted *outside* the vertical window, ascending.
|
|
190
|
+
*
|
|
191
|
+
* These are not translated by `rows.offset`: a renderer that folded them into the window
|
|
192
|
+
* would move them with the scroll, which is the definition of not being frozen.
|
|
193
|
+
*/
|
|
194
|
+
readonly frozenRows: readonly number[];
|
|
195
|
+
/** The same for columns. */
|
|
196
|
+
readonly frozenColumns: readonly number[];
|
|
197
|
+
}
|
|
198
|
+
/** Everything {@link gridWindow} needs. */
|
|
199
|
+
interface GridWindowOptions {
|
|
200
|
+
readonly rowCount: number;
|
|
201
|
+
readonly columnCount: number;
|
|
202
|
+
/** Height of a row, or a per-row estimate. */
|
|
203
|
+
readonly rowSize: number | ((row: number) => number);
|
|
204
|
+
/** Width of a column, or a per-column estimate. */
|
|
205
|
+
readonly columnSize: number | ((column: number) => number);
|
|
206
|
+
/** Measured row height, when the host keeps one outside an index. */
|
|
207
|
+
readonly measuredRow?: (row: number) => number | undefined;
|
|
208
|
+
/** Measured column width, when the host keeps one outside an index. */
|
|
209
|
+
readonly measuredColumn?: (column: number) => number | undefined;
|
|
210
|
+
/**
|
|
211
|
+
* A live prefix-sum index over the rows, or the columns.
|
|
212
|
+
*
|
|
213
|
+
* Without these both axes scan, which is O(rows + columns) per frame — fine for the first
|
|
214
|
+
* paint and for a thousand rows, and not fine for a 50,000-column sheet. With them each
|
|
215
|
+
* axis is two binary searches. A `SizeIndex` is accepted directly and adapted here, so a
|
|
216
|
+
* host that already measures rows does not have to know about the range layer's shape.
|
|
217
|
+
*/
|
|
218
|
+
readonly rowIndex?: RangeIndex | SizeIndex;
|
|
219
|
+
readonly columnIndex?: RangeIndex | SizeIndex;
|
|
220
|
+
readonly scrollTop: number;
|
|
221
|
+
readonly scrollLeft: number;
|
|
222
|
+
readonly viewportHeight: number;
|
|
223
|
+
readonly viewportWidth: number;
|
|
224
|
+
readonly overscan?: number;
|
|
225
|
+
readonly scrollMargin?: number;
|
|
226
|
+
readonly frozen?: FrozenEdges;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Computes the band of a grid to render.
|
|
230
|
+
*
|
|
231
|
+
* The two axes are independent — a row's height does not depend on which columns are
|
|
232
|
+
* visible — so each runs its own window and the cost is the sum of the two, never the
|
|
233
|
+
* product. Frozen rows and columns are reported separately because they are painted at
|
|
234
|
+
* their natural offsets rather than at the window's: that is what "frozen" means, and it is
|
|
235
|
+
* why a renderer cannot simply union them into the window.
|
|
236
|
+
*/
|
|
237
|
+
declare function gridWindow(options: GridWindowOptions): GridWindow;
|
|
238
|
+
/**
|
|
239
|
+
* The cells of a window, row-major, with their offsets.
|
|
240
|
+
*
|
|
241
|
+
* A generator: a host that draws only what its own clipping lets through stops early and
|
|
242
|
+
* never pays for the rest. The offsets come from the caller's size source rather than from
|
|
243
|
+
* the window, because a window reports the band's edges and not every cell's position.
|
|
244
|
+
*/
|
|
245
|
+
declare function gridCells(grid: GridWindow, rows: SizeSource, columns: SizeSource): Generator<GridCell>;
|
|
246
|
+
/** One cell in the visible band. */
|
|
247
|
+
interface GridCell {
|
|
248
|
+
readonly row: number;
|
|
249
|
+
readonly column: number;
|
|
250
|
+
/** Pixel offset of the row from the grid's top. */
|
|
251
|
+
readonly y: number;
|
|
252
|
+
/** Pixel offset of the column from the grid's left. */
|
|
253
|
+
readonly x: number;
|
|
254
|
+
readonly height: number;
|
|
255
|
+
readonly width: number;
|
|
256
|
+
}
|
|
257
|
+
/** What {@link gridCells} needs from a size source. A `SizeIndex` satisfies it. */
|
|
258
|
+
interface SizeSource {
|
|
259
|
+
/** Pixel size of one index. */
|
|
260
|
+
sizeOf(index: number): number;
|
|
261
|
+
/** Pixel offset of one index from the start. */
|
|
262
|
+
offsetOf(index: number): number;
|
|
263
|
+
}
|
|
264
|
+
export { SizeSource as a, SizeIndex as c, toRangeIndex as d, DEFAULT_OVERSCAN as f, computeRange as g, RangeInput as h, GridWindowOptions as i, SizeIndexOptions as l, RangeIndex as m, GridCell as n, gridCells as o, Range as p, GridWindow as r, gridWindow as s, FrozenEdges as t, createSizeIndex as u };
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/** How many extra items to render on each side of the viewport. */
|
|
2
|
+
declare const DEFAULT_OVERSCAN = 1;
|
|
3
|
+
/**
|
|
4
|
+
* Everything the range math needs. It takes accessors instead of a prepared
|
|
5
|
+
* index so a first frame — before anything has been measured — is exactly as
|
|
6
|
+
* valid as a later one: unknown sizes fall through to `estimate`.
|
|
7
|
+
*/
|
|
8
|
+
interface RangeInput {
|
|
9
|
+
readonly count: number;
|
|
10
|
+
/** Current scroll offset of the scroller, in pixels. */
|
|
11
|
+
readonly scroll: number;
|
|
12
|
+
/** Visible height of the scroller, in pixels. */
|
|
13
|
+
readonly viewport: number;
|
|
14
|
+
/** Extra items above and below the visible window; defaults to {@link DEFAULT_OVERSCAN}. */
|
|
15
|
+
readonly overscan?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Pixels of the scroller's content that precede this list: a toolbar, a page header, a
|
|
18
|
+
* hero. The scroll offset is reduced by it, so item 0 sits at the viewport's top edge when
|
|
19
|
+
* the list itself reaches it.
|
|
20
|
+
*
|
|
21
|
+
* `total` never includes the margin, and `offset` stays list-relative. Item offsets that
|
|
22
|
+
* included the margin would make every renderer subtract it again; list-relative geometry
|
|
23
|
+
* means a host places rows inside the list and never does that
|
|
24
|
+
* arithmetic.
|
|
25
|
+
*/
|
|
26
|
+
readonly scrollMargin?: number;
|
|
27
|
+
/** Size for an item that has not been measured yet. Called once per item per compute. */
|
|
28
|
+
readonly estimate: (index: number) => number;
|
|
29
|
+
/** Measured size, or `undefined` when the item is still estimated. */
|
|
30
|
+
readonly measured?: (index: number) => number | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* How many items share a row. Defaults to 1, which is a plain list.
|
|
33
|
+
*
|
|
34
|
+
* With `lanes: 3` items 0, 1 and 2 are the first row, 3, 4 and 5 the second. The row's
|
|
35
|
+
* height is the tallest of its items — the alternative, summing them, would reserve
|
|
36
|
+
* space nothing occupies. The window is still in item indices so `end - start` remains
|
|
37
|
+
* the number of rows to render, which is what a host keys on.
|
|
38
|
+
*/
|
|
39
|
+
readonly lanes?: number;
|
|
40
|
+
/**
|
|
41
|
+
* A live prefix-sum index over the same items, when the caller keeps one.
|
|
42
|
+
*
|
|
43
|
+
* The O(n) scan below is the price of accepting accessors: a first frame has no index to
|
|
44
|
+
* hand over. Once measurements start arriving the same list is scanned once per
|
|
45
|
+
* measurement, which is O(n) per row and O(n²) for a screenful — measured at 1.03ms per
|
|
46
|
+
* row for a 20,000-row list against 0.057ms for a 1,000-row one, an 18× spread on the same
|
|
47
|
+
* work. With an index the window is two binary searches, so a measurement costs O(log n)
|
|
48
|
+
* and the spread disappears.
|
|
49
|
+
*
|
|
50
|
+
* The index must answer `offsetOf` and `indexAt` for the sizes in effect; a caller that
|
|
51
|
+
* passes a stale one gets a window that disagrees with its content, which is why this is
|
|
52
|
+
* opt-in rather than inferred.
|
|
53
|
+
*/
|
|
54
|
+
readonly index?: RangeIndex;
|
|
55
|
+
}
|
|
56
|
+
/** The two lookups a range needs from a prefix-sum index. */
|
|
57
|
+
interface RangeIndex {
|
|
58
|
+
/**
|
|
59
|
+
* The number of items the index describes.
|
|
60
|
+
*
|
|
61
|
+
* Required, not inferred: the window's `end` is clamped to the count, and an index that
|
|
62
|
+
* answered `offsetOf` for an item the caller's data no longer has would otherwise produce
|
|
63
|
+
* a window over rows that do not exist. A `SizeIndex` already exposes it.
|
|
64
|
+
*/
|
|
65
|
+
readonly count: number;
|
|
66
|
+
/** Total pixel size of every item. */
|
|
67
|
+
readonly total: number;
|
|
68
|
+
/** Pixel offset of `index` from the start; `offsetOf(count)` is `total`. */
|
|
69
|
+
offsetOf(index: number): number;
|
|
70
|
+
/** The item at `offset`, clamped to the last item. */
|
|
71
|
+
indexAt(offset: number): number;
|
|
72
|
+
/** Optional O(log n) lower bound: smallest index `i` in `[0, count]` with `offsetOf(i) >= offset`. */
|
|
73
|
+
readonly lowerBound?: (offset: number) => number;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The window to render, in logical indices and pixels. `end` is exclusive, and
|
|
77
|
+
* `offset` is the absolute pixel offset of `start` — the translate to put on
|
|
78
|
+
* the first rendered item so the window lands where the scroll position says.
|
|
79
|
+
*/
|
|
80
|
+
interface Range {
|
|
81
|
+
readonly start: number;
|
|
82
|
+
/** Exclusive: `end - start` items are rendered. */
|
|
83
|
+
readonly end: number;
|
|
84
|
+
/** Prefix-sum offset of `start` in pixels; the translate for the first item. */
|
|
85
|
+
readonly offset: number;
|
|
86
|
+
/** Pixel size of the rendered window, i.e. `offsetOf(end) - offsetOf(start)`. */
|
|
87
|
+
readonly size: number;
|
|
88
|
+
/** Pixel size of the whole list. */
|
|
89
|
+
readonly total: number;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Computes the render window for a scroll position.
|
|
93
|
+
*
|
|
94
|
+
* Construction is O(n): the input hands us accessors, not a prefix sum, so the
|
|
95
|
+
* call must materialise one before it can binary search. The lookups themselves
|
|
96
|
+
* are O(log n) — two binary searches over the prefix sums, never a scan. The
|
|
97
|
+
* alternative (require a caller-owned {@link SizeIndex}) was rejected because a
|
|
98
|
+
* first render has no index to hand over, and that is exactly the frame this
|
|
99
|
+
* function exists to get right.
|
|
100
|
+
*/
|
|
101
|
+
declare function computeRange(input: RangeInput): Range;
|
|
102
|
+
/** What a size index is built from: a fixed item count and a fallback estimate. */
|
|
103
|
+
interface SizeIndexOptions {
|
|
104
|
+
readonly count: number;
|
|
105
|
+
/** Size for an index that has not been measured. Called once per index, at creation. */
|
|
106
|
+
readonly estimate: (index: number) => number;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* A live size source for one list. Prefix sums answer "where does item i start"
|
|
110
|
+
* and "which item is at offset x" in O(log n), so a list of unknown heights does
|
|
111
|
+
* not turn every scroll frame into a scan.
|
|
112
|
+
*/
|
|
113
|
+
interface SizeIndex {
|
|
114
|
+
/** Number of items; fixed for the lifetime of the index. */
|
|
115
|
+
readonly count: number;
|
|
116
|
+
/** Records a measurement; out-of-range indices and invalid sizes are ignored. */
|
|
117
|
+
set(index: number, size: number): void;
|
|
118
|
+
/** Resets a single measured index back to its initial estimate in O(log n). */
|
|
119
|
+
reset(index: number): void;
|
|
120
|
+
/** Measured size when present, else the estimate. Out-of-range indices clamp to an item. */
|
|
121
|
+
sizeOf(index: number): number;
|
|
122
|
+
/** Sum of every size, in pixels. */
|
|
123
|
+
total(): number;
|
|
124
|
+
/** Forgets every measurement and returns to the estimates. */
|
|
125
|
+
clear(): void;
|
|
126
|
+
/** Whether `index` has a real measurement (as opposed to the estimate); out-of-range is false. */
|
|
127
|
+
hasMeasured(index: number): boolean;
|
|
128
|
+
/**
|
|
129
|
+
* Pixel offset of `index` from the start of the list. `offsetOf(0)` is 0 and
|
|
130
|
+
* `offsetOf(count)` is {@link SizeIndex.total}.
|
|
131
|
+
*/
|
|
132
|
+
offsetOf(index: number): number;
|
|
133
|
+
/**
|
|
134
|
+
* Index of the item occupying `offset`; `-1` when the list is empty. An offset
|
|
135
|
+
* on an item boundary belongs to the item that starts there, an offset at or
|
|
136
|
+
* past the end belongs to the last item.
|
|
137
|
+
*/
|
|
138
|
+
indexAt(offset: number): number;
|
|
139
|
+
/**
|
|
140
|
+
* Smallest index `i` in `[0, count]` whose `offsetOf(i) >= offset`, in O(log n).
|
|
141
|
+
*/
|
|
142
|
+
lowerBound(offset: number): number;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Builds the prefix-sum index.
|
|
146
|
+
*
|
|
147
|
+
* A Fenwick tree is used instead of an array of prefix sums plus a dirty marker:
|
|
148
|
+
* a measurement must not cost O(n), because measurements arrive one per item per
|
|
149
|
+
* frame while the user is scrolling. `set` therefore touches only the O(log n)
|
|
150
|
+
* tree nodes whose ranges cover the index — the smallest suffix information the
|
|
151
|
+
* measurement can change. `offsetOf`, `total` and `indexAt` are O(log n);
|
|
152
|
+
* `sizeOf` and `hasMeasured` are O(1). `clear` is O(n) because it must rebuild
|
|
153
|
+
* every node; an epoch counter could make it O(1) but would leak the old array
|
|
154
|
+
* until the index itself is dropped, and clearing is a data change, not a frame.
|
|
155
|
+
*
|
|
156
|
+
* Estimates are snapshotted at creation, so the prefix sums and `sizeOf` can
|
|
157
|
+
* never disagree. Changing the estimate means a new index; the estimate is a
|
|
158
|
+
* property of the data model, not of the scroll position. For the same reason a
|
|
159
|
+
* list whose item count changes needs a new index: a kept index would answer for
|
|
160
|
+
* the old count and silently miss the new items.
|
|
161
|
+
*/
|
|
162
|
+
declare function createSizeIndex({ count, estimate }: SizeIndexOptions): SizeIndex;
|
|
163
|
+
/**
|
|
164
|
+
* Presents a {@link SizeIndex} as a {@link RangeIndex}.
|
|
165
|
+
*
|
|
166
|
+
* The two differ by one fact: `total()` is a method here and a property there, and that
|
|
167
|
+
* is exactly the kind of difference that silently reads a function as a size: `index.total`
|
|
168
|
+
* would be a function, the window's arithmetic would produce `NaN`, and every offset would
|
|
169
|
+
* come back zero. One adapter, exported, so the grid and the DOM layer cannot each get it subtly wrong.
|
|
170
|
+
*/
|
|
171
|
+
declare function toRangeIndex(index: SizeIndex | RangeIndex): RangeIndex;
|
|
172
|
+
/**
|
|
173
|
+
* A frozen edge: a number of leading rows or columns that stay painted while the rest
|
|
174
|
+
* scrolls. Distinct from a window's overscan, which only widens what is rendered.
|
|
175
|
+
*/
|
|
176
|
+
interface FrozenEdges {
|
|
177
|
+
/** Leading rows painted outside the vertical window. */
|
|
178
|
+
readonly rows?: number;
|
|
179
|
+
/** Leading columns painted outside the horizontal window. */
|
|
180
|
+
readonly columns?: number;
|
|
181
|
+
}
|
|
182
|
+
/** The area a grid should render. */
|
|
183
|
+
interface GridWindow {
|
|
184
|
+
/** The rows to render. `rows.offset` is the y translate for the first rendered row. */
|
|
185
|
+
readonly rows: Range;
|
|
186
|
+
/** The columns to render. `columns.offset` is the x translate for the first one. */
|
|
187
|
+
readonly columns: Range;
|
|
188
|
+
/**
|
|
189
|
+
* Leading rows that must stay painted *outside* the vertical window, ascending.
|
|
190
|
+
*
|
|
191
|
+
* These are not translated by `rows.offset`: a renderer that folded them into the window
|
|
192
|
+
* would move them with the scroll, which is the definition of not being frozen.
|
|
193
|
+
*/
|
|
194
|
+
readonly frozenRows: readonly number[];
|
|
195
|
+
/** The same for columns. */
|
|
196
|
+
readonly frozenColumns: readonly number[];
|
|
197
|
+
}
|
|
198
|
+
/** Everything {@link gridWindow} needs. */
|
|
199
|
+
interface GridWindowOptions {
|
|
200
|
+
readonly rowCount: number;
|
|
201
|
+
readonly columnCount: number;
|
|
202
|
+
/** Height of a row, or a per-row estimate. */
|
|
203
|
+
readonly rowSize: number | ((row: number) => number);
|
|
204
|
+
/** Width of a column, or a per-column estimate. */
|
|
205
|
+
readonly columnSize: number | ((column: number) => number);
|
|
206
|
+
/** Measured row height, when the host keeps one outside an index. */
|
|
207
|
+
readonly measuredRow?: (row: number) => number | undefined;
|
|
208
|
+
/** Measured column width, when the host keeps one outside an index. */
|
|
209
|
+
readonly measuredColumn?: (column: number) => number | undefined;
|
|
210
|
+
/**
|
|
211
|
+
* A live prefix-sum index over the rows, or the columns.
|
|
212
|
+
*
|
|
213
|
+
* Without these both axes scan, which is O(rows + columns) per frame — fine for the first
|
|
214
|
+
* paint and for a thousand rows, and not fine for a 50,000-column sheet. With them each
|
|
215
|
+
* axis is two binary searches. A `SizeIndex` is accepted directly and adapted here, so a
|
|
216
|
+
* host that already measures rows does not have to know about the range layer's shape.
|
|
217
|
+
*/
|
|
218
|
+
readonly rowIndex?: RangeIndex | SizeIndex;
|
|
219
|
+
readonly columnIndex?: RangeIndex | SizeIndex;
|
|
220
|
+
readonly scrollTop: number;
|
|
221
|
+
readonly scrollLeft: number;
|
|
222
|
+
readonly viewportHeight: number;
|
|
223
|
+
readonly viewportWidth: number;
|
|
224
|
+
readonly overscan?: number;
|
|
225
|
+
readonly scrollMargin?: number;
|
|
226
|
+
readonly frozen?: FrozenEdges;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Computes the band of a grid to render.
|
|
230
|
+
*
|
|
231
|
+
* The two axes are independent — a row's height does not depend on which columns are
|
|
232
|
+
* visible — so each runs its own window and the cost is the sum of the two, never the
|
|
233
|
+
* product. Frozen rows and columns are reported separately because they are painted at
|
|
234
|
+
* their natural offsets rather than at the window's: that is what "frozen" means, and it is
|
|
235
|
+
* why a renderer cannot simply union them into the window.
|
|
236
|
+
*/
|
|
237
|
+
declare function gridWindow(options: GridWindowOptions): GridWindow;
|
|
238
|
+
/**
|
|
239
|
+
* The cells of a window, row-major, with their offsets.
|
|
240
|
+
*
|
|
241
|
+
* A generator: a host that draws only what its own clipping lets through stops early and
|
|
242
|
+
* never pays for the rest. The offsets come from the caller's size source rather than from
|
|
243
|
+
* the window, because a window reports the band's edges and not every cell's position.
|
|
244
|
+
*/
|
|
245
|
+
declare function gridCells(grid: GridWindow, rows: SizeSource, columns: SizeSource): Generator<GridCell>;
|
|
246
|
+
/** One cell in the visible band. */
|
|
247
|
+
interface GridCell {
|
|
248
|
+
readonly row: number;
|
|
249
|
+
readonly column: number;
|
|
250
|
+
/** Pixel offset of the row from the grid's top. */
|
|
251
|
+
readonly y: number;
|
|
252
|
+
/** Pixel offset of the column from the grid's left. */
|
|
253
|
+
readonly x: number;
|
|
254
|
+
readonly height: number;
|
|
255
|
+
readonly width: number;
|
|
256
|
+
}
|
|
257
|
+
/** What {@link gridCells} needs from a size source. A `SizeIndex` satisfies it. */
|
|
258
|
+
interface SizeSource {
|
|
259
|
+
/** Pixel size of one index. */
|
|
260
|
+
sizeOf(index: number): number;
|
|
261
|
+
/** Pixel offset of one index from the start. */
|
|
262
|
+
offsetOf(index: number): number;
|
|
263
|
+
}
|
|
264
|
+
export { SizeSource as a, SizeIndex as c, toRangeIndex as d, DEFAULT_OVERSCAN as f, computeRange as g, RangeInput as h, GridWindowOptions as i, SizeIndexOptions as l, RangeIndex as m, GridCell as n, gridCells as o, Range as p, GridWindow as r, gridWindow as s, FrozenEdges as t, createSizeIndex as u };
|