@omnigrid/core 0.1.20 → 0.1.22

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.
@@ -1,20 +1,277 @@
1
+ /**
2
+ * Dynamic row-height registry: `rowIndex -> measured px height`.
3
+ *
4
+ * Heights live in a Fenwick tree (binary indexed tree) over the row indexes.
5
+ * A single measurement is applied in O(log n) — the prefix sums are patched
6
+ * in place instead of rebuilding the whole offset table — and row offsets /
7
+ * range lookups are answered with O(log n) prefix queries and searches.
8
+ * Rebuilding the whole table is only needed when the dataset size or the
9
+ * estimate changes, which happens on structural changes only.
10
+ *
11
+ * Rows without a measurement use the resolver value, or the estimate. The
12
+ * resolver result is captured when the index is (re)built, so it must be
13
+ * stable for a given row index.
14
+ */
15
+ export class RowHeightRegistry {
16
+ constructor(estimatedHeight, resolver) {
17
+ this.heights = new Map();
18
+ this.currentVersion = 0;
19
+ /** Fenwick tree of row heights (1-based indexing). */
20
+ this.tree = new Float64Array(1);
21
+ /** Raw height of every row the tree covers — mirrors the tree leaves. */
22
+ this.leaves = new Float64Array(0);
23
+ /** Number of rows the tree currently covers. */
24
+ this.treeCount = -1;
25
+ /** Whether the tree has to be rebuilt before the next query. */
26
+ this.treeDirty = true;
27
+ this.estimatedHeight = estimatedHeight;
28
+ this.resolver = resolver;
29
+ }
30
+ /** Total number of rows with an explicit measured height. @api */
31
+ get size() {
32
+ return this.heights.size;
33
+ }
34
+ /** Monotonically increasing revision number bumped on any measurement or estimate change. @api */
35
+ get version() {
36
+ return this.currentVersion;
37
+ }
38
+ /** Whether any variable-height input exists (resolver or measured heights). @api */
39
+ get hasVariableHeights() {
40
+ return this.heights.size > 0 || this.resolver !== undefined;
41
+ }
42
+ /** Updates the fallback estimated height for unmeasured rows. @api */
43
+ setEstimatedHeight(height) {
44
+ if (height === this.estimatedHeight)
45
+ return;
46
+ this.estimatedHeight = height;
47
+ this.currentVersion += 1;
48
+ this.treeDirty = true;
49
+ }
50
+ /**
51
+ * Records a measured height. Returns `true` when the value actually
52
+ * changed (delta > 1px); sub-pixel noise is ignored to prevent
53
+ * measurement/layout feedback loops.
54
+ */
55
+ set(rowIndex, height) {
56
+ if (!Number.isFinite(rowIndex) || rowIndex < 0 || !Number.isInteger(rowIndex))
57
+ return false;
58
+ if (!Number.isFinite(height) || height <= 0)
59
+ return false;
60
+ const rounded = Math.max(1, Math.round(height));
61
+ const prev = this.heights.get(rowIndex);
62
+ if (prev !== undefined && Math.abs(rounded - prev) <= 1)
63
+ return false;
64
+ const previousHeight = this.resolveHeight(rowIndex);
65
+ this.heights.set(rowIndex, rounded);
66
+ this.currentVersion += 1;
67
+ this.applyDelta(rowIndex, rounded - previousHeight);
68
+ return true;
69
+ }
70
+ /** Removes a single measured height. Returns `true` when one existed. */
71
+ reset(rowIndex) {
72
+ if (!this.heights.has(rowIndex))
73
+ return false;
74
+ const previousHeight = this.resolveHeight(rowIndex);
75
+ this.heights.delete(rowIndex);
76
+ this.currentVersion += 1;
77
+ this.applyDelta(rowIndex, this.resolveHeight(rowIndex) - previousHeight);
78
+ return true;
79
+ }
80
+ /** Removes all measured heights. */
81
+ clear() {
82
+ if (this.heights.size === 0)
83
+ return;
84
+ this.heights.clear();
85
+ this.currentVersion += 1;
86
+ this.treeDirty = true;
87
+ }
88
+ /** Returns the measured height of a row if one has been recorded, or undefined. @api */
89
+ get(rowIndex) {
90
+ return this.heights.get(rowIndex);
91
+ }
92
+ /** Returns the resolved height of a row (measured, dynamic callback, or estimated). */
93
+ getRowHeight(rowIndex) {
94
+ return this.resolveHeight(rowIndex);
95
+ }
96
+ /** Materializes the cumulative offsets table (`offsets[i]` = top of row `i`). @api */
97
+ getOffsets(count) {
98
+ const size = Math.max(0, count);
99
+ this.ensureTree(size);
100
+ const offsets = new Array(size + 1);
101
+ let top = 0;
102
+ offsets[0] = 0;
103
+ for (let index = 0; index < size; index += 1) {
104
+ top += this.leaves[index] ?? 0;
105
+ offsets[index + 1] = top;
106
+ }
107
+ return offsets;
108
+ }
109
+ /** Total scrollable content height for `count` rows. @api */
110
+ getTotalHeight(count) {
111
+ if (count <= 0)
112
+ return 0;
113
+ this.ensureTree(count);
114
+ return RowHeightRegistry.prefixSum(this.tree, count);
115
+ }
116
+ /** Absolute top offset of a row (clamped to the dataset). @api */
117
+ getTop(rowIndex, count) {
118
+ if (count <= 0)
119
+ return 0;
120
+ const clamped = Math.max(0, Math.min(count - 1, Math.floor(rowIndex)));
121
+ this.ensureTree(count);
122
+ return RowHeightRegistry.prefixSum(this.tree, clamped);
123
+ }
124
+ /**
125
+ * First row whose bottom edge is strictly below `offset`
126
+ * (binary search over the Fenwick tree).
127
+ * @api
128
+ */
129
+ findStart(count, offset) {
130
+ if (count <= 0)
131
+ return 0;
132
+ if (offset <= 0)
133
+ return 0;
134
+ this.ensureTree(count);
135
+ return Math.min(count, RowHeightRegistry.findFloor(this.tree, count, offset).index);
136
+ }
137
+ /**
138
+ * First row whose top edge is at or past `offset`
139
+ * (binary search over the Fenwick tree). Used as the exclusive window end.
140
+ * @api
141
+ */
142
+ findEnd(count, offset) {
143
+ if (count <= 0)
144
+ return 0;
145
+ if (offset <= 0)
146
+ return 0;
147
+ this.ensureTree(count);
148
+ const floor = RowHeightRegistry.findFloor(this.tree, count, offset);
149
+ // The first row whose TOP edge is at or past the offset: the row that
150
+ // starts exactly at the offset is included, otherwise the next one.
151
+ const index = floor.sum >= offset ? floor.index : floor.index + 1;
152
+ return Math.min(count, index);
153
+ }
154
+ /** Marks the offset index dirty when the dataset identity changes. @api */
155
+ invalidate(count) {
156
+ if (count !== this.treeCount)
157
+ this.treeDirty = true;
158
+ }
159
+ /** Resolves the effective height of a row: measured, resolver, or estimate. */
160
+ resolveHeight(rowIndex) {
161
+ const measured = this.heights.get(rowIndex);
162
+ if (measured !== undefined)
163
+ return measured;
164
+ const resolved = this.resolver?.(rowIndex);
165
+ if (resolved !== undefined && Number.isFinite(resolved) && resolved > 0) {
166
+ return Math.max(1, Math.round(resolved));
167
+ }
168
+ return this.estimatedHeight;
169
+ }
170
+ /** Rebuilds the Fenwick tree when the dataset size or the estimate changed. */
171
+ ensureTree(count) {
172
+ const size = Math.max(0, count);
173
+ if (!this.treeDirty && this.treeCount === size)
174
+ return;
175
+ const tree = new Float64Array(size + 1);
176
+ const leaves = new Float64Array(size);
177
+ for (let index = 0; index < size; index += 1) {
178
+ const height = this.resolveHeight(index);
179
+ leaves[index] = height;
180
+ tree[index + 1] = height;
181
+ }
182
+ // O(n) Fenwick build: every node accumulates its children.
183
+ for (let index = 1; index <= size; index += 1) {
184
+ const parent = index + (index & -index);
185
+ if (parent <= size)
186
+ tree[parent] += tree[index];
187
+ }
188
+ this.tree = tree;
189
+ this.leaves = leaves;
190
+ this.treeCount = size;
191
+ this.treeDirty = false;
192
+ }
193
+ /** Patches a single row height into the live tree (O(log n)). */
194
+ applyDelta(rowIndex, delta) {
195
+ if (delta === 0)
196
+ return;
197
+ // A dirty tree is rebuilt from scratch on the next query — the delta
198
+ // is already part of the source data at that point.
199
+ if (this.treeDirty)
200
+ return;
201
+ if (rowIndex >= this.treeCount)
202
+ return;
203
+ this.leaves[rowIndex] = (this.leaves[rowIndex] ?? 0) + delta;
204
+ for (let position = rowIndex + 1; position < this.tree.length; position += position & -position) {
205
+ this.tree[position] += delta;
206
+ }
207
+ }
208
+ /** Sum of the first `count` row heights. */
209
+ static prefixSum(tree, count) {
210
+ let sum = 0;
211
+ for (let position = count; position > 0; position -= position & -position) {
212
+ sum += tree[position];
213
+ }
214
+ return sum;
215
+ }
216
+ /**
217
+ * Finds the largest row count whose cumulative height does not exceed
218
+ * `offset` — i.e. the number of rows fully above the offset.
219
+ */
220
+ static findFloor(tree, count, offset) {
221
+ let index = 0;
222
+ let remaining = offset;
223
+ let step = 1;
224
+ while (step * 2 <= count)
225
+ step *= 2;
226
+ for (; step >= 1; step = Math.floor(step / 2)) {
227
+ const next = index + step;
228
+ if (next <= count && tree[next] <= remaining) {
229
+ index = next;
230
+ remaining -= tree[next];
231
+ }
232
+ }
233
+ return { index, sum: offset - remaining };
234
+ }
235
+ }
1
236
  export class Virtualizer {
2
237
  constructor(options) {
3
238
  this.lastScrollTop = null;
4
239
  this.lastRowRange = null;
5
240
  this.lastRowCount = 0;
6
241
  this.lastRowViewportHeight = 0;
242
+ this.lastHeightsVersion = 0;
7
243
  this.widthsCache = new WeakMap();
8
244
  this.options = options;
245
+ this.heights = new RowHeightRegistry(options.estimatedRowHeight ?? options.rowHeight, options.getRowHeight);
246
+ }
247
+ /** Dynamic height registry backing variable-height mode. @api */
248
+ get rowHeights() {
249
+ return this.heights;
250
+ }
251
+ /** Estimated height used for rows without a measurement. @api */
252
+ get estimatedRowHeight() {
253
+ return this.options.estimatedRowHeight ?? this.options.rowHeight;
9
254
  }
10
- getRowRange(count, scrollTop, viewportHeight) {
255
+ /** Whether any variable-height input exists (resolver counts or measured heights exist). @api */
256
+ get hasVariableHeights() {
257
+ return this.heights.hasVariableHeights;
258
+ }
259
+ /**
260
+ * Visible row range calculation for fixed or variable heights.
261
+ * @api
262
+ */
263
+ getRowRange(count, scrollTop, viewportHeight, variableHeights = false) {
11
264
  if (count === 0) {
12
265
  this.lastScrollTop = null;
13
266
  this.lastRowRange = null;
14
267
  this.lastRowCount = 0;
15
268
  this.lastRowViewportHeight = 0;
269
+ this.lastHeightsVersion = this.heights.version;
16
270
  return { start: 0, end: 0 };
17
271
  }
272
+ if (variableHeights || this.hasVariableHeights) {
273
+ return this.getVariableRowRange(count, scrollTop, viewportHeight);
274
+ }
18
275
  const rowHeight = this.options.rowHeight;
19
276
  const visibleStart = Math.floor(Math.max(0, scrollTop) / rowHeight);
20
277
  const visibleEnd = Math.ceil((Math.max(0, scrollTop) + Math.max(0, viewportHeight)) / rowHeight);
@@ -63,6 +320,138 @@ export class Virtualizer {
63
320
  this.lastRowRange = corridor;
64
321
  return corridor;
65
322
  }
323
+ /**
324
+ * Computes the visible row window for variable-height mode using the
325
+ * incremental offset index (binary search over the Fenwick tree).
326
+ * @api
327
+ */
328
+ getVariableRowRange(count, scrollTop, viewportHeight) {
329
+ const visibleStart = this.heights.findStart(count, Math.max(0, scrollTop));
330
+ const visibleEnd = this.heights.findEnd(count, Math.max(0, scrollTop) + Math.max(0, viewportHeight));
331
+ const visibleRows = Math.max(1, visibleEnd - visibleStart);
332
+ const overscan = Math.max(this.options.rowOverscan, visibleRows);
333
+ const windowed = {
334
+ start: Math.max(0, visibleStart - overscan),
335
+ end: Math.min(count, visibleEnd + overscan),
336
+ };
337
+ if (scrollTop === this.lastScrollTop &&
338
+ count === this.lastRowCount &&
339
+ viewportHeight === this.lastRowViewportHeight &&
340
+ this.lastHeightsVersion === this.heights.version) {
341
+ return this.lastRowRange ?? windowed;
342
+ }
343
+ const previousTop = this.lastScrollTop;
344
+ this.lastScrollTop = scrollTop;
345
+ this.lastRowCount = count;
346
+ this.lastRowViewportHeight = viewportHeight;
347
+ this.lastHeightsVersion = this.heights.version;
348
+ if (previousTop === null) {
349
+ this.lastRowRange = windowed;
350
+ return windowed;
351
+ }
352
+ const prevVisibleStart = this.heights.findStart(count, Math.max(0, previousTop));
353
+ const prevVisibleEnd = this.heights.findEnd(count, Math.max(0, previousTop) + Math.max(0, viewportHeight));
354
+ const closeEnough = visibleStart <= prevVisibleEnd + overscan && prevVisibleStart <= visibleEnd + overscan;
355
+ if (closeEnough) {
356
+ this.lastRowRange = windowed;
357
+ return windowed;
358
+ }
359
+ const corridorCap = visibleRows * 2 + this.options.rowOverscan * 2;
360
+ const corridorStart = Math.max(0, Math.min(prevVisibleStart, visibleStart) - overscan);
361
+ const corridorEnd = Math.min(count, Math.max(prevVisibleEnd, visibleEnd) + overscan);
362
+ if (corridorEnd - corridorStart > corridorCap) {
363
+ this.lastRowRange = windowed;
364
+ return windowed;
365
+ }
366
+ const corridor = { start: corridorStart, end: corridorEnd };
367
+ this.lastRowRange = corridor;
368
+ return corridor;
369
+ }
370
+ /**
371
+ * Rows that intersect the viewport — the range WITHOUT the overscan
372
+ * buffer.
373
+ *
374
+ * The DOM pool reuses its nodes in a ring: a node that was not re-bound
375
+ * keeps the content of a completely unrelated row. The rows that
376
+ * intersect the viewport must therefore always be bound, even while a
377
+ * fast scroll defers the off-screen (overscan) rows.
378
+ * @api
379
+ */
380
+ getVisibleRowRange(count, scrollTop, viewportHeight) {
381
+ if (count <= 0)
382
+ return { start: 0, end: 0 };
383
+ if (this.hasVariableHeights) {
384
+ return {
385
+ start: this.heights.findStart(count, Math.max(0, scrollTop)),
386
+ end: this.heights.findEnd(count, Math.max(0, scrollTop) + Math.max(0, viewportHeight)),
387
+ };
388
+ }
389
+ const rowHeight = this.options.rowHeight;
390
+ const start = Math.floor(Math.max(0, scrollTop) / rowHeight);
391
+ const end = Math.ceil((Math.max(0, scrollTop) + Math.max(0, viewportHeight)) / rowHeight);
392
+ return { start: Math.min(count, start), end: Math.min(count, end) };
393
+ }
394
+ /**
395
+ * Absolute vertical offset (px) of the top of a row, accounting for variable heights.
396
+ * @api
397
+ */
398
+ getRowTop(rowIndex, count) {
399
+ if (count <= 0)
400
+ return 0;
401
+ const clamped = Math.max(0, Math.min(count - 1, Math.floor(rowIndex)));
402
+ return this.heights.getTop(clamped, count);
403
+ }
404
+ /** Current (measured, resolved, or estimated) height of a row. @api */
405
+ getRowHeight(rowIndex) {
406
+ return this.heights.getRowHeight(rowIndex);
407
+ }
408
+ /**
409
+ * Records a measured row height and returns the scrollTop correction needed
410
+ * to keep the visually anchored row stable (scroll anchoring).
411
+ * @api
412
+ */
413
+ setRowHeight(rowIndex, height, scrollTop, count) {
414
+ const rowTop = this.getRowTop(rowIndex, count);
415
+ const prevHeight = this.getRowHeight(rowIndex);
416
+ const changed = this.heights.set(rowIndex, height);
417
+ if (!changed)
418
+ return 0;
419
+ const newHeight = this.getRowHeight(rowIndex);
420
+ const delta = newHeight - prevHeight;
421
+ if (rowTop < scrollTop) {
422
+ return delta;
423
+ }
424
+ return 0;
425
+ }
426
+ /** Removes a measured row height (falls back to resolver or estimate). @api */
427
+ resetRowHeight(rowIndex, scrollTop, count) {
428
+ const hadHeight = this.heights.get(rowIndex) !== undefined;
429
+ if (!hadHeight)
430
+ return 0;
431
+ const rowTop = this.getRowTop(rowIndex, count);
432
+ const prevHeight = this.getRowHeight(rowIndex);
433
+ const removed = this.heights.reset(rowIndex);
434
+ if (!removed)
435
+ return 0;
436
+ const newHeight = this.getRowHeight(rowIndex);
437
+ const delta = newHeight - prevHeight;
438
+ if (rowTop < scrollTop) {
439
+ return delta;
440
+ }
441
+ return 0;
442
+ }
443
+ /** Removes all measured row heights (e.g. after `setColumns`). @api */
444
+ resetAllRowHeights() {
445
+ this.heights.clear();
446
+ }
447
+ /** Total scrollable content height for `count` rows, accounting for variable heights. @api */
448
+ getTotalHeight(count) {
449
+ if (count <= 0)
450
+ return 0;
451
+ if (!this.hasVariableHeights)
452
+ return count * this.options.rowHeight;
453
+ return this.heights.getTotalHeight(count);
454
+ }
66
455
  /**
67
456
  * Column geometry works on the FLAT working set of leaf columns only —
68
457
  * groups never reach the Virtualizer (see `Grid.getViewportData()`).
@@ -90,9 +479,6 @@ export class Virtualizer {
90
479
  return item;
91
480
  });
92
481
  }
93
- getTotalHeight(rowCount) {
94
- return rowCount * this.options.rowHeight;
95
- }
96
482
  getTotalWidth(columns, viewportWidth = 0) {
97
483
  return this.getColumnOffsets(columns, viewportWidth).reduce((total, item) => total + item.size, 0);
98
484
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnigrid/core",
3
- "version": "0.1.20",
3
+ "version": "0.1.22",
4
4
  "description": "Framework-agnostic core engine for OmniGrid",
5
5
  "keywords": [
6
6
  "omnigrid",