virtua 0.46.7 → 0.47.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.
@@ -105,19 +105,18 @@ const getItemOffset = (cache, index) => {
105
105
  */
106
106
  const findIndex = (cache, offset, low = 0, high = cache._length - 1) => {
107
107
  // Find with binary search
108
+ let found = low;
108
109
  while (low <= high) {
109
110
  const mid = floor((low + high) / 2);
110
111
  if (getItemOffset(cache, mid) <= offset) {
111
- if (getItemOffset(cache, mid + 1) > offset) {
112
- return mid;
113
- }
112
+ found = mid;
114
113
  low = mid + 1;
115
114
  }
116
115
  else {
117
116
  high = mid - 1;
118
117
  }
119
118
  }
120
- return clamp(low, 0, cache._length - 1);
119
+ return clamp(found, 0, cache._length - 1);
121
120
  };
122
121
  /**
123
122
  * @internal
@@ -168,12 +167,12 @@ const estimateDefaultItemSize = (cache, startIndex) => {
168
167
  /**
169
168
  * @internal
170
169
  */
171
- const initCache = (length, itemSize, snapshot) => {
170
+ const initCache = (length, itemSize, sizes) => {
172
171
  return {
173
- _defaultItemSize: snapshot ? snapshot[1] : itemSize,
174
- _sizes: snapshot && snapshot[0]
172
+ _defaultItemSize: itemSize,
173
+ _sizes: sizes
175
174
  ? // https://github.com/inokawa/virtua/issues/441
176
- fill(snapshot[0].slice(0, min(length, snapshot[0].length)), max(0, length - snapshot[0].length))
175
+ fill(sizes.slice(0, min(length, sizes.length)), max(0, length - sizes.length))
177
176
  : fill([], length),
178
177
  _length: length,
179
178
  _computedOffsetIndex: -1,
@@ -306,7 +305,9 @@ const createVirtualStore = (elementsCount, itemSize = 40, ssrCount = 0, cacheSna
306
305
  let _frozenRange = NULL;
307
306
  let _prevRange = [0, isSSR ? max(ssrCount - 1, 0) : -1];
308
307
  let _totalMeasuredSize = 0;
309
- const cache = initCache(elementsCount, itemSize, cacheSnapshot);
308
+ const cache = initCache(elementsCount, cacheSnapshot
309
+ ? cacheSnapshot[1]
310
+ : itemSize, cacheSnapshot && cacheSnapshot[0]);
310
311
  const subscribers = new Set();
311
312
  const getRelativeScrollOffset = () => scrollOffset - startSpacerSize;
312
313
  const getVisibleOffset = () => getRelativeScrollOffset() + pendingJump + jump;
@@ -381,8 +382,7 @@ const createVirtualStore = (elementsCount, itemSize = 40, ssrCount = 0, cacheSna
381
382
  }
382
383
  return [max(startIndex, 0), min(endIndex, cache._length - 1)];
383
384
  },
384
- $findStartIndex: () => findIndex(cache, getVisibleOffset()),
385
- $findEndIndex: () => findIndex(cache, getVisibleOffset() + viewportSize),
385
+ $findItemIndex: (offset) => findIndex(cache, offset - startSpacerSize),
386
386
  $isUnmeasuredItem: isSizeEqual,
387
387
  $getItemOffset: getItemOffset$1,
388
388
  $getItemSize: getItemSize$1,
@@ -994,32 +994,44 @@ const createWindowScroller = (store, isHorizontal) => {
994
994
  * @internal
995
995
  */
996
996
  const createGridScroller = (rowStore, colStore) => {
997
- const vScroller = createScroller(rowStore, false);
998
- const hScroller = createScroller(colStore, true);
997
+ const rowScroller = createScroller(rowStore, false);
998
+ const colScroller = createScroller(colStore, true);
999
999
  return {
1000
1000
  $observe(viewportElement) {
1001
- vScroller.$observe(viewportElement);
1002
- hScroller.$observe(viewportElement);
1001
+ rowScroller.$observe(viewportElement);
1002
+ colScroller.$observe(viewportElement);
1003
1003
  },
1004
1004
  $dispose() {
1005
- vScroller.$dispose();
1006
- hScroller.$dispose();
1005
+ rowScroller.$dispose();
1006
+ colScroller.$dispose();
1007
1007
  },
1008
- $scrollTo(offsetX, offsetY) {
1009
- vScroller.$scrollTo(offsetY);
1010
- hScroller.$scrollTo(offsetX);
1008
+ $scrollTo(row, col) {
1009
+ if (row != null) {
1010
+ rowScroller.$scrollTo(row);
1011
+ }
1012
+ if (col != null) {
1013
+ colScroller.$scrollTo(col);
1014
+ }
1011
1015
  },
1012
- $scrollBy(offsetX, offsetY) {
1013
- vScroller.$scrollBy(offsetY);
1014
- hScroller.$scrollBy(offsetX);
1016
+ $scrollBy(row, col) {
1017
+ if (row != null) {
1018
+ rowScroller.$scrollBy(row);
1019
+ }
1020
+ if (col != null) {
1021
+ colScroller.$scrollBy(col);
1022
+ }
1015
1023
  },
1016
- $scrollToIndex(indexX, indexY) {
1017
- vScroller.$scrollToIndex(indexY);
1018
- hScroller.$scrollToIndex(indexX);
1024
+ $scrollToIndex(row, col) {
1025
+ if (row != null) {
1026
+ rowScroller.$scrollToIndex(row);
1027
+ }
1028
+ if (col != null) {
1029
+ colScroller.$scrollToIndex(col);
1030
+ }
1019
1031
  },
1020
1032
  $fixScrollJump() {
1021
- vScroller.$fixScrollJump();
1022
- hScroller.$fixScrollJump();
1033
+ rowScroller.$fixScrollJump();
1034
+ colScroller.$fixScrollJump();
1023
1035
  },
1024
1036
  };
1025
1037
  };
@@ -1345,8 +1357,7 @@ const Virtualizer = (props) => {
1345
1357
  get viewportSize() {
1346
1358
  return store.$getViewportSize();
1347
1359
  },
1348
- findStartIndex: store.$findStartIndex,
1349
- findEndIndex: store.$findEndIndex,
1360
+ findItemIndex: store.$findItemIndex,
1350
1361
  getItemOffset: store.$getItemOffset,
1351
1362
  getItemSize: store.$getItemSize,
1352
1363
  scrollToIndex: scroller.$scrollToIndex,
@@ -1511,8 +1522,15 @@ const WindowVirtualizer = (props) => {
1511
1522
  get cache() {
1512
1523
  return store.$getCacheSnapshot();
1513
1524
  },
1514
- findStartIndex: store.$findStartIndex,
1515
- findEndIndex: store.$findEndIndex,
1525
+ get scrollOffset() {
1526
+ return store.$getScrollOffset();
1527
+ },
1528
+ get viewportSize() {
1529
+ return store.$getViewportSize();
1530
+ },
1531
+ findItemIndex: store.$findItemIndex,
1532
+ getItemOffset: store.$getItemOffset,
1533
+ getItemSize: store.$getItemSize,
1516
1534
  scrollToIndex: scroller.$scrollToIndex,
1517
1535
  });
1518
1536
  }