@difizen/libro-virtualized 0.1.2

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.
Files changed (132) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +0 -0
  3. package/es/auto-sizer/auto-sizer.d.ts +56 -0
  4. package/es/auto-sizer/auto-sizer.d.ts.map +1 -0
  5. package/es/auto-sizer/auto-sizer.js +157 -0
  6. package/es/auto-sizer/index.d.ts +3 -0
  7. package/es/auto-sizer/index.d.ts.map +1 -0
  8. package/es/auto-sizer/index.js +2 -0
  9. package/es/cell-measurer/cell-measurer-cache.d.ts +52 -0
  10. package/es/cell-measurer/cell-measurer-cache.d.ts.map +1 -0
  11. package/es/cell-measurer/cell-measurer-cache.js +176 -0
  12. package/es/cell-measurer/cell-measurer.d.ts +39 -0
  13. package/es/cell-measurer/cell-measurer.d.ts.map +1 -0
  14. package/es/cell-measurer/cell-measurer.js +154 -0
  15. package/es/cell-measurer/index.d.ts +5 -0
  16. package/es/cell-measurer/index.d.ts.map +1 -0
  17. package/es/cell-measurer/index.js +4 -0
  18. package/es/cell-measurer/types.d.ts +9 -0
  19. package/es/cell-measurer/types.d.ts.map +1 -0
  20. package/es/cell-measurer/types.js +0 -0
  21. package/es/grid/accessibility-overscanIndices-getter.d.ts +11 -0
  22. package/es/grid/accessibility-overscanIndices-getter.d.ts.map +1 -0
  23. package/es/grid/accessibility-overscanIndices-getter.js +33 -0
  24. package/es/grid/default-cell-range-renderer.d.ts +10 -0
  25. package/es/grid/default-cell-range-renderer.d.ts.map +1 -0
  26. package/es/grid/default-cell-range-renderer.js +135 -0
  27. package/es/grid/default-overscanIndices-getter.d.ts +11 -0
  28. package/es/grid/default-overscanIndices-getter.d.ts.map +1 -0
  29. package/es/grid/default-overscanIndices-getter.js +28 -0
  30. package/es/grid/grid.d.ts +359 -0
  31. package/es/grid/grid.d.ts.map +1 -0
  32. package/es/grid/grid.js +1287 -0
  33. package/es/grid/index.d.ts +6 -0
  34. package/es/grid/index.d.ts.map +1 -0
  35. package/es/grid/index.js +4 -0
  36. package/es/grid/types.d.ts +87 -0
  37. package/es/grid/types.d.ts.map +1 -0
  38. package/es/grid/types.js +1 -0
  39. package/es/grid/utils/calculate-size-and-position-data-and-update-scroll-offset.d.ts +17 -0
  40. package/es/grid/utils/calculate-size-and-position-data-and-update-scroll-offset.d.ts.map +1 -0
  41. package/es/grid/utils/calculate-size-and-position-data-and-update-scroll-offset.js +25 -0
  42. package/es/grid/utils/cell-size-and-position-manager-row.d.ts +97 -0
  43. package/es/grid/utils/cell-size-and-position-manager-row.d.ts.map +1 -0
  44. package/es/grid/utils/cell-size-and-position-manager-row.js +297 -0
  45. package/es/grid/utils/cell-size-and-position-manager.d.ts +85 -0
  46. package/es/grid/utils/cell-size-and-position-manager.d.ts.map +1 -0
  47. package/es/grid/utils/cell-size-and-position-manager.js +268 -0
  48. package/es/grid/utils/max-element-size.d.ts +2 -0
  49. package/es/grid/utils/max-element-size.d.ts.map +1 -0
  50. package/es/grid/utils/max-element-size.js +17 -0
  51. package/es/grid/utils/scaling-cell-size-and-position-manager-row.d.ts +78 -0
  52. package/es/grid/utils/scaling-cell-size-and-position-manager-row.d.ts.map +1 -0
  53. package/es/grid/utils/scaling-cell-size-and-position-manager-row.js +187 -0
  54. package/es/grid/utils/scaling-cell-size-and-position-manager.d.ts +70 -0
  55. package/es/grid/utils/scaling-cell-size-and-position-manager.d.ts.map +1 -0
  56. package/es/grid/utils/scaling-cell-size-and-position-manager.js +187 -0
  57. package/es/grid/utils/update-scroll-index-helper.d.ts +24 -0
  58. package/es/grid/utils/update-scroll-index-helper.d.ts.map +1 -0
  59. package/es/grid/utils/update-scroll-index-helper.js +40 -0
  60. package/es/index.d.ts +5 -0
  61. package/es/index.d.ts.map +1 -0
  62. package/es/index.js +4 -0
  63. package/es/list/index.d.ts +3 -0
  64. package/es/list/index.d.ts.map +1 -0
  65. package/es/list/index.js +1 -0
  66. package/es/list/list.d.ts +109 -0
  67. package/es/list/list.d.ts.map +1 -0
  68. package/es/list/list.js +261 -0
  69. package/es/list/types.d.ts +22 -0
  70. package/es/list/types.d.ts.map +1 -0
  71. package/es/list/types.js +1 -0
  72. package/es/utils/animation-frame.d.ts +7 -0
  73. package/es/utils/animation-frame.d.ts.map +1 -0
  74. package/es/utils/animation-frame.js +20 -0
  75. package/es/utils/create-callback-memoizer.d.ts +5 -0
  76. package/es/utils/create-callback-memoizer.d.ts.map +1 -0
  77. package/es/utils/create-callback-memoizer.js +25 -0
  78. package/es/utils/get-updated-offset-for-index.d.ts +14 -0
  79. package/es/utils/get-updated-offset-for-index.d.ts.map +1 -0
  80. package/es/utils/get-updated-offset-for-index.js +32 -0
  81. package/es/utils/init-cell-metadata.d.ts +13 -0
  82. package/es/utils/init-cell-metadata.d.ts.map +1 -0
  83. package/es/utils/init-cell-metadata.js +32 -0
  84. package/es/utils/request-animation-timeout.d.ts +12 -0
  85. package/es/utils/request-animation-timeout.d.ts.map +1 -0
  86. package/es/utils/request-animation-timeout.js +33 -0
  87. package/es/utils/test-helper.d.ts +5 -0
  88. package/es/utils/test-helper.d.ts.map +1 -0
  89. package/es/utils/test-helper.js +31 -0
  90. package/es/vendor/binary-search-bounds.d.ts +22 -0
  91. package/es/vendor/binary-search-bounds.d.ts.map +1 -0
  92. package/es/vendor/binary-search-bounds.js +198 -0
  93. package/es/vendor/detect-element-resize.d.ts +16 -0
  94. package/es/vendor/detect-element-resize.d.ts.map +1 -0
  95. package/es/vendor/detect-element-resize.js +184 -0
  96. package/es/vendor/interval-tree.d.ts +10 -0
  97. package/es/vendor/interval-tree.d.ts.map +1 -0
  98. package/es/vendor/interval-tree.js +359 -0
  99. package/package.json +59 -0
  100. package/src/auto-sizer/auto-sizer.tsx +187 -0
  101. package/src/auto-sizer/index.ts +4 -0
  102. package/src/cell-measurer/cell-measurer-cache.ts +220 -0
  103. package/src/cell-measurer/cell-measurer.ts +151 -0
  104. package/src/cell-measurer/index.ts +5 -0
  105. package/src/cell-measurer/types.ts +8 -0
  106. package/src/grid/accessibility-overscanIndices-getter.ts +38 -0
  107. package/src/grid/default-cell-range-renderer.ts +166 -0
  108. package/src/grid/default-overscanIndices-getter.ts +32 -0
  109. package/src/grid/grid.tsx +1672 -0
  110. package/src/grid/index.ts +14 -0
  111. package/src/grid/types.ts +112 -0
  112. package/src/grid/utils/calculate-size-and-position-data-and-update-scroll-offset.ts +62 -0
  113. package/src/grid/utils/cell-size-and-position-manager-row.ts +365 -0
  114. package/src/grid/utils/cell-size-and-position-manager.ts +309 -0
  115. package/src/grid/utils/max-element-size.ts +18 -0
  116. package/src/grid/utils/scaling-cell-size-and-position-manager-row.ts +206 -0
  117. package/src/grid/utils/scaling-cell-size-and-position-manager.ts +198 -0
  118. package/src/grid/utils/update-scroll-index-helper.ts +96 -0
  119. package/src/index.spec.ts +10 -0
  120. package/src/index.ts +4 -0
  121. package/src/list/index.ts +2 -0
  122. package/src/list/list.tsx +292 -0
  123. package/src/list/types.ts +25 -0
  124. package/src/utils/animation-frame.ts +38 -0
  125. package/src/utils/create-callback-memoizer.ts +32 -0
  126. package/src/utils/get-updated-offset-for-index.ts +33 -0
  127. package/src/utils/init-cell-metadata.ts +32 -0
  128. package/src/utils/request-animation-timeout.ts +44 -0
  129. package/src/utils/test-helper.ts +20 -0
  130. package/src/vendor/binary-search-bounds.ts +203 -0
  131. package/src/vendor/detect-element-resize.ts +241 -0
  132. package/src/vendor/interval-tree.ts +406 -0
@@ -0,0 +1,309 @@
1
+ /* eslint-disable no-param-reassign */
2
+ /* eslint-disable prefer-const */
3
+ import type { Alignment, CellSizeGetter, VisibleCellRange } from '../types.js';
4
+
5
+ type CellSizeAndPositionManagerParams = {
6
+ cellCount: number;
7
+ cellSizeGetter: CellSizeGetter;
8
+ estimatedCellSize: number;
9
+ };
10
+
11
+ type ConfigureParams = {
12
+ cellCount: number;
13
+ estimatedCellSize: number;
14
+ cellSizeGetter: CellSizeGetter;
15
+ };
16
+
17
+ type GetUpdatedOffsetForIndex = {
18
+ align: Alignment;
19
+ containerSize: number;
20
+ currentOffset: number;
21
+ targetIndex: number;
22
+ };
23
+
24
+ type GetVisibleCellRangeParams = {
25
+ containerSize: number;
26
+ offset: number;
27
+ };
28
+
29
+ type SizeAndPositionData = {
30
+ offset: number;
31
+ size: number;
32
+ };
33
+
34
+ /**
35
+ * Just-in-time calculates and caches size and position information for a collection of cells.
36
+ */
37
+
38
+ export default class CellSizeAndPositionManager {
39
+ // Cache of size and position data for cells, mapped by cell index.
40
+ // Note that invalid values may exist in this map so only rely on cells up to this._lastMeasuredIndex
41
+ _cellSizeAndPositionData: any = {};
42
+
43
+ // Measurements for cells up to this index can be trusted; cells afterward should be estimated.
44
+ _lastMeasuredIndex = -1;
45
+
46
+ // Used in deferred mode to track which cells have been queued for measurement.
47
+ _lastBatchedIndex = -1;
48
+
49
+ _cellCount: number;
50
+ _cellSizeGetter: CellSizeGetter;
51
+ _estimatedCellSize: number;
52
+
53
+ constructor({
54
+ cellCount,
55
+ cellSizeGetter,
56
+ estimatedCellSize,
57
+ }: CellSizeAndPositionManagerParams) {
58
+ this._cellSizeGetter = cellSizeGetter;
59
+ this._cellCount = cellCount;
60
+ this._estimatedCellSize = estimatedCellSize;
61
+ }
62
+
63
+ areOffsetsAdjusted() {
64
+ return false;
65
+ }
66
+
67
+ configure({ cellCount, estimatedCellSize, cellSizeGetter }: ConfigureParams) {
68
+ this._cellCount = cellCount;
69
+ this._estimatedCellSize = estimatedCellSize;
70
+ this._cellSizeGetter = cellSizeGetter;
71
+ }
72
+
73
+ getCellCount(): number {
74
+ return this._cellCount;
75
+ }
76
+
77
+ getEstimatedCellSize(): number {
78
+ return this._estimatedCellSize;
79
+ }
80
+
81
+ getLastMeasuredIndex(): number {
82
+ return this._lastMeasuredIndex;
83
+ }
84
+
85
+ getOffsetAdjustment() {
86
+ return 0;
87
+ }
88
+
89
+ /**
90
+ * This method returns the size and position for the cell at the specified index.
91
+ * It just-in-time calculates (or used cached values) for cells leading up to the index.
92
+ */
93
+ getSizeAndPositionOfCell(index: number): SizeAndPositionData {
94
+ if (index < 0 || index >= this._cellCount) {
95
+ throw Error(`Requested index ${index} is outside of range 0..${this._cellCount}`);
96
+ }
97
+
98
+ if (index > this._lastMeasuredIndex) {
99
+ const lastMeasuredCellSizeAndPosition =
100
+ this.getSizeAndPositionOfLastMeasuredCell();
101
+ let offset =
102
+ lastMeasuredCellSizeAndPosition.offset + lastMeasuredCellSizeAndPosition.size;
103
+
104
+ for (let i = this._lastMeasuredIndex + 1; i <= index; i++) {
105
+ const size = this._cellSizeGetter({ index: i });
106
+
107
+ // undefined or NaN probably means a logic error in the size getter.
108
+ // null means we're using CellMeasurer and haven't yet measured a given index.
109
+ if (size === undefined || isNaN(size)) {
110
+ throw Error(`Invalid size returned for cell ${i} of value ${size}`);
111
+ } else if (size === null) {
112
+ this._cellSizeAndPositionData[i] = {
113
+ offset,
114
+ size: 0,
115
+ };
116
+
117
+ this._lastBatchedIndex = index;
118
+ } else {
119
+ this._cellSizeAndPositionData[i] = {
120
+ offset,
121
+ size,
122
+ };
123
+
124
+ offset += size;
125
+
126
+ this._lastMeasuredIndex = index;
127
+ }
128
+ }
129
+ }
130
+
131
+ return this._cellSizeAndPositionData[index];
132
+ }
133
+
134
+ getSizeAndPositionOfLastMeasuredCell(): SizeAndPositionData {
135
+ return this._lastMeasuredIndex >= 0
136
+ ? this._cellSizeAndPositionData[this._lastMeasuredIndex]
137
+ : {
138
+ offset: 0,
139
+ size: 0,
140
+ };
141
+ }
142
+
143
+ /**
144
+ * Total size of all cells being measured.
145
+ * This value will be completely estimated initially.
146
+ * As cells are measured, the estimate will be updated.
147
+ */
148
+ getTotalSize(): number {
149
+ const lastMeasuredCellSizeAndPosition = this.getSizeAndPositionOfLastMeasuredCell();
150
+ const totalSizeOfMeasuredCells =
151
+ lastMeasuredCellSizeAndPosition.offset + lastMeasuredCellSizeAndPosition.size;
152
+ const numUnmeasuredCells = this._cellCount - this._lastMeasuredIndex - 1;
153
+ const totalSizeOfUnmeasuredCells = numUnmeasuredCells * this._estimatedCellSize;
154
+ return totalSizeOfMeasuredCells + totalSizeOfUnmeasuredCells;
155
+ }
156
+
157
+ /**
158
+ * Determines a new offset that ensures a certain cell is visible, given the current offset.
159
+ * If the cell is already visible then the current offset will be returned.
160
+ * If the current offset is too great or small, it will be adjusted just enough to ensure the specified index is visible.
161
+ *
162
+ * @param align Desired alignment within container; one of "auto" (default), "start", or "end"
163
+ * @param containerSize Size (width or height) of the container viewport
164
+ * @param currentOffset Container's current (x or y) offset
165
+ * @param totalSize Total size (width or height) of all cells
166
+ * @return Offset to use to ensure the specified cell is visible
167
+ */
168
+ getUpdatedOffsetForIndex({
169
+ align = 'auto',
170
+ containerSize,
171
+ currentOffset,
172
+ targetIndex,
173
+ }: GetUpdatedOffsetForIndex): number {
174
+ if (containerSize <= 0) {
175
+ return 0;
176
+ }
177
+
178
+ const datum = this.getSizeAndPositionOfCell(targetIndex);
179
+ const maxOffset = datum.offset;
180
+ const minOffset = maxOffset - containerSize + datum.size;
181
+
182
+ let idealOffset;
183
+
184
+ switch (align) {
185
+ case 'start':
186
+ idealOffset = maxOffset;
187
+ break;
188
+ case 'end':
189
+ idealOffset = minOffset;
190
+ break;
191
+ case 'center':
192
+ idealOffset = maxOffset - (containerSize - datum.size) / 2;
193
+ break;
194
+ default:
195
+ idealOffset = Math.max(minOffset, Math.min(maxOffset, currentOffset));
196
+ break;
197
+ }
198
+
199
+ const totalSize = this.getTotalSize();
200
+
201
+ return Math.max(0, Math.min(totalSize - containerSize, idealOffset));
202
+ }
203
+
204
+ getVisibleCellRange(params: GetVisibleCellRangeParams): VisibleCellRange {
205
+ let { containerSize, offset } = params;
206
+
207
+ const totalSize = this.getTotalSize();
208
+
209
+ if (totalSize === 0) {
210
+ return {};
211
+ }
212
+
213
+ const maxOffset = offset + containerSize;
214
+ const start = this._findNearestCell(offset);
215
+
216
+ const datum = this.getSizeAndPositionOfCell(start);
217
+ offset = datum.offset + datum.size;
218
+
219
+ let stop = start;
220
+
221
+ while (offset < maxOffset && stop < this._cellCount - 1) {
222
+ stop++;
223
+
224
+ offset += this.getSizeAndPositionOfCell(stop).size;
225
+ }
226
+
227
+ return {
228
+ start,
229
+ stop,
230
+ };
231
+ }
232
+
233
+ /**
234
+ * Clear all cached values for cells after the specified index.
235
+ * This method should be called for any cell that has changed its size.
236
+ * It will not immediately perform any calculations; they'll be performed the next time getSizeAndPositionOfCell() is called.
237
+ */
238
+ resetCell(index: number): void {
239
+ this._lastMeasuredIndex = Math.min(this._lastMeasuredIndex, index - 1);
240
+ }
241
+
242
+ _binarySearch(high: number, low: number, offset: number): number {
243
+ while (low <= high) {
244
+ const middle = low + Math.floor((high - low) / 2);
245
+ const currentOffset = this.getSizeAndPositionOfCell(middle).offset;
246
+
247
+ if (currentOffset === offset) {
248
+ return middle;
249
+ } else if (currentOffset < offset) {
250
+ low = middle + 1;
251
+ } else if (currentOffset > offset) {
252
+ high = middle - 1;
253
+ }
254
+ }
255
+
256
+ if (low > 0) {
257
+ return low - 1;
258
+ } else {
259
+ return 0;
260
+ }
261
+ }
262
+
263
+ _exponentialSearch(index: number, offset: number): number {
264
+ let interval = 1;
265
+
266
+ while (
267
+ index < this._cellCount &&
268
+ this.getSizeAndPositionOfCell(index).offset < offset
269
+ ) {
270
+ index += interval;
271
+ interval *= 2;
272
+ }
273
+
274
+ return this._binarySearch(
275
+ Math.min(index, this._cellCount - 1),
276
+ Math.floor(index / 2),
277
+ offset,
278
+ );
279
+ }
280
+
281
+ /**
282
+ * Searches for the cell (index) nearest the specified offset.
283
+ *
284
+ * If no exact match is found the next lowest cell index will be returned.
285
+ * This allows partially visible cells (with offsets just before/above the fold) to be visible.
286
+ */
287
+ _findNearestCell(offset: number): number {
288
+ if (isNaN(offset)) {
289
+ throw Error(`Invalid offset ${offset} specified`);
290
+ }
291
+
292
+ // Our search algorithms find the nearest match at or below the specified offset.
293
+ // So make sure the offset is at least 0 or no match will be found.
294
+ offset = Math.max(0, offset);
295
+
296
+ const lastMeasuredCellSizeAndPosition = this.getSizeAndPositionOfLastMeasuredCell();
297
+ const lastMeasuredIndex = Math.max(0, this._lastMeasuredIndex);
298
+
299
+ if (lastMeasuredCellSizeAndPosition.offset >= offset) {
300
+ // If we've already measured cells within this range just use a binary search as it's faster.
301
+ return this._binarySearch(lastMeasuredIndex, 0, offset);
302
+ } else {
303
+ // If we haven't yet measured this high, fallback to an exponential search with an inner binary search.
304
+ // The exponential search avoids pre-computing sizes for the full set of cells as a binary search would.
305
+ // The overall complexity for this approach is O(log n).
306
+ return this._exponentialSearch(lastMeasuredIndex, offset);
307
+ }
308
+ }
309
+ }
@@ -0,0 +1,18 @@
1
+ const DEFAULT_MAX_ELEMENT_SIZE = 1500000;
2
+ const CHROME_MAX_ELEMENT_SIZE = 1.67771e7;
3
+
4
+ const isBrowser = () => typeof window !== 'undefined';
5
+
6
+ const isChrome = () => {
7
+ const userAgent = navigator.userAgent;
8
+ return userAgent.indexOf('Chrome') >= 0;
9
+ };
10
+
11
+ export const getMaxElementSize = (): number => {
12
+ if (isBrowser()) {
13
+ if (isChrome()) {
14
+ return CHROME_MAX_ELEMENT_SIZE;
15
+ }
16
+ }
17
+ return DEFAULT_MAX_ELEMENT_SIZE;
18
+ };
@@ -0,0 +1,206 @@
1
+ /* eslint-disable no-param-reassign */
2
+ import type { Alignment, CellSizeGetter, VisibleCellRange } from '../types.js';
3
+
4
+ import RowCellSizeAndPositionManager from './cell-size-and-position-manager-row.js';
5
+ import { getMaxElementSize } from './max-element-size.js';
6
+
7
+ type ContainerSizeAndOffset = {
8
+ containerSize: number;
9
+ offset: number;
10
+ };
11
+
12
+ /**
13
+ * Browsers have scroll offset limitations (eg Chrome stops scrolling at ~33.5M pixels where as Edge tops out at ~1.5M pixels).
14
+ * After a certain position, the browser won't allow the user to scroll further (even via JavaScript scroll offset adjustments).
15
+ * This util picks a lower ceiling for max size and artificially adjusts positions within to make it transparent for users.
16
+ */
17
+
18
+ type Params = {
19
+ maxScrollSize?: number;
20
+ cellCount: number;
21
+ cellSizeGetter: CellSizeGetter;
22
+ estimatedCellSize: number;
23
+ cellsHeight: number[];
24
+ editorAreaHeight: number[];
25
+ totalSize: number;
26
+ editorsOffset: number[];
27
+ };
28
+
29
+ /**
30
+ * Extends CellSizeAndPositionManager and adds scaling behavior for lists that are too large to fit within a browser's native limits.
31
+ */
32
+ export default class RowScalingCellSizeAndPositionManager {
33
+ _cellSizeAndPositionManager: RowCellSizeAndPositionManager;
34
+ _maxScrollSize: number;
35
+
36
+ constructor({ maxScrollSize = getMaxElementSize(), ...params }: Params) {
37
+ // Favor composition over inheritance to simplify IE10 support
38
+ this._cellSizeAndPositionManager = new RowCellSizeAndPositionManager(params);
39
+ this._maxScrollSize = maxScrollSize;
40
+ }
41
+
42
+ areOffsetsAdjusted(): boolean {
43
+ return this._cellSizeAndPositionManager.getTotalSize() > this._maxScrollSize;
44
+ }
45
+
46
+ configure(params: {
47
+ cellCount: number;
48
+ estimatedCellSize: number;
49
+ cellSizeGetter: CellSizeGetter;
50
+ cellsHeight: number[];
51
+ editorAreaHeight: number[];
52
+ totalSize: number;
53
+ editorsOffset: number[];
54
+ }) {
55
+ this._cellSizeAndPositionManager.configure(params);
56
+ }
57
+
58
+ getCellCount(): number {
59
+ return this._cellSizeAndPositionManager.getCellCount();
60
+ }
61
+
62
+ getEstimatedCellSize(): number {
63
+ return this._cellSizeAndPositionManager.getEstimatedCellSize();
64
+ }
65
+
66
+ getLastMeasuredIndex(): number {
67
+ return this._cellSizeAndPositionManager.getLastMeasuredIndex();
68
+ }
69
+
70
+ /**
71
+ * Number of pixels a cell at the given position (offset) should be shifted in order to fit within the scaled container.
72
+ * The offset passed to this function is scaled (safe) as well.
73
+ */
74
+ getOffsetAdjustment({
75
+ containerSize,
76
+ offset, // safe
77
+ }: ContainerSizeAndOffset): number {
78
+ const totalSize = this._cellSizeAndPositionManager.getTotalSize();
79
+ const safeTotalSize = this.getTotalSize();
80
+ const offsetPercentage = this._getOffsetPercentage({
81
+ containerSize,
82
+ offset,
83
+ totalSize: safeTotalSize,
84
+ });
85
+
86
+ return Math.round(offsetPercentage * (safeTotalSize - totalSize));
87
+ }
88
+
89
+ getSizeAndPositionOfCell(index: number) {
90
+ return this._cellSizeAndPositionManager.getSizeAndPositionOfCell(index);
91
+ }
92
+
93
+ getSizeAndPositionOfLastMeasuredCell() {
94
+ return this._cellSizeAndPositionManager.getSizeAndPositionOfLastMeasuredCell();
95
+ }
96
+
97
+ /** See CellSizeAndPositionManager#getTotalSize */
98
+ getTotalSize(): number {
99
+ return Math.min(
100
+ this._maxScrollSize,
101
+ this._cellSizeAndPositionManager.getTotalSize(),
102
+ );
103
+ }
104
+
105
+ /** See CellSizeAndPositionManager#getUpdatedOffsetForIndex */
106
+ getUpdatedOffsetForIndex({
107
+ align = 'auto',
108
+ containerSize,
109
+ currentOffset, // safe
110
+ targetIndex,
111
+ }: {
112
+ align: Alignment;
113
+ containerSize: number;
114
+ currentOffset: number;
115
+ targetIndex: number;
116
+ }) {
117
+ currentOffset = this._safeOffsetToOffset({
118
+ containerSize,
119
+ offset: currentOffset,
120
+ });
121
+
122
+ const offset = this._cellSizeAndPositionManager.getUpdatedOffsetForIndex({
123
+ align,
124
+ containerSize,
125
+ currentOffset,
126
+ targetIndex,
127
+ });
128
+
129
+ return this._offsetToSafeOffset({
130
+ containerSize,
131
+ offset,
132
+ });
133
+ }
134
+
135
+ /** See CellSizeAndPositionManager#getVisibleCellRange */
136
+ getVisibleCellRange({
137
+ containerSize,
138
+ offset, // safe
139
+ }: ContainerSizeAndOffset): VisibleCellRange {
140
+ offset = this._safeOffsetToOffset({
141
+ containerSize,
142
+ offset,
143
+ });
144
+
145
+ return this._cellSizeAndPositionManager.getVisibleCellRange({
146
+ containerSize,
147
+ offset,
148
+ });
149
+ }
150
+
151
+ resetCell(index: number): void {
152
+ this._cellSizeAndPositionManager.resetCell(index);
153
+ }
154
+
155
+ _getOffsetPercentage({
156
+ containerSize,
157
+ offset, // safe
158
+ totalSize,
159
+ }: {
160
+ containerSize: number;
161
+ offset: number;
162
+ totalSize: number;
163
+ }) {
164
+ return totalSize <= containerSize ? 0 : offset / (totalSize - containerSize);
165
+ }
166
+
167
+ _offsetToSafeOffset({
168
+ containerSize,
169
+ offset, // unsafe
170
+ }: ContainerSizeAndOffset): number {
171
+ const totalSize = this._cellSizeAndPositionManager.getTotalSize();
172
+ const safeTotalSize = this.getTotalSize();
173
+
174
+ if (totalSize === safeTotalSize) {
175
+ return offset;
176
+ } else {
177
+ const offsetPercentage = this._getOffsetPercentage({
178
+ containerSize,
179
+ offset,
180
+ totalSize,
181
+ });
182
+
183
+ return Math.round(offsetPercentage * (safeTotalSize - containerSize));
184
+ }
185
+ }
186
+
187
+ _safeOffsetToOffset({
188
+ containerSize,
189
+ offset, // safe
190
+ }: ContainerSizeAndOffset): number {
191
+ const totalSize = this._cellSizeAndPositionManager.getTotalSize();
192
+ const safeTotalSize = this.getTotalSize();
193
+
194
+ if (totalSize === safeTotalSize) {
195
+ return offset;
196
+ } else {
197
+ const offsetPercentage = this._getOffsetPercentage({
198
+ containerSize,
199
+ offset,
200
+ totalSize: safeTotalSize,
201
+ });
202
+
203
+ return Math.round(offsetPercentage * (totalSize - containerSize));
204
+ }
205
+ }
206
+ }