@tanstack/react-virtual 3.0.0-beta.9 → 3.0.1
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/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js +27 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js.map +1 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js +31 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.mjs +27 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.mjs.map +1 -0
- package/build/lib/index.d.ts +4 -0
- package/build/lib/index.esm.js +67 -0
- package/build/lib/index.esm.js.map +1 -0
- package/build/lib/index.js +97 -0
- package/build/lib/index.js.map +1 -0
- package/build/lib/index.mjs +67 -0
- package/build/lib/index.mjs.map +1 -0
- package/build/umd/index.development.js +669 -516
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +22 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +26 -12
- package/src/index.tsx +20 -8
- package/build/cjs/react-virtual/src/index.js +0 -90
- package/build/cjs/react-virtual/src/index.js.map +0 -1
- package/build/cjs/virtual-core/build/esm/index.js +0 -615
- package/build/cjs/virtual-core/build/esm/index.js.map +0 -1
- package/build/esm/index.js +0 -642
- package/build/esm/index.js.map +0 -1
- package/build/stats-html.html +0 -2689
- package/build/stats.json +0 -90
- package/build/types/react-virtual/src/index.d.ts +0 -4
- package/build/types/virtual-core/src/utils.d.ts +0 -7
|
@@ -9,10 +9,10 @@
|
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
11
11
|
(function (global, factory) {
|
|
12
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
|
|
13
|
-
typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
|
|
14
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactVirtual = {}, global.React));
|
|
15
|
-
})(this, (function (exports, React) { 'use strict';
|
|
12
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('react-dom')) :
|
|
13
|
+
typeof define === 'function' && define.amd ? define(['exports', 'react', 'react-dom'], factory) :
|
|
14
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactVirtual = {}, global.React, global.ReactDOM));
|
|
15
|
+
})(this, (function (exports, React, reactDom) { 'use strict';
|
|
16
16
|
|
|
17
17
|
function _interopNamespace(e) {
|
|
18
18
|
if (e && e.__esModule) return e;
|
|
@@ -34,6 +34,21 @@
|
|
|
34
34
|
|
|
35
35
|
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
36
36
|
|
|
37
|
+
function _extends$1() {
|
|
38
|
+
_extends$1 = Object.assign ? Object.assign.bind() : function (target) {
|
|
39
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
40
|
+
var source = arguments[i];
|
|
41
|
+
for (var key in source) {
|
|
42
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
43
|
+
target[key] = source[key];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return target;
|
|
48
|
+
};
|
|
49
|
+
return _extends$1.apply(this, arguments);
|
|
50
|
+
}
|
|
51
|
+
|
|
37
52
|
/**
|
|
38
53
|
* virtual-core
|
|
39
54
|
*
|
|
@@ -44,546 +59,678 @@
|
|
|
44
59
|
*
|
|
45
60
|
* @license MIT
|
|
46
61
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
b = {};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return props.some(function (prop) {
|
|
59
|
-
return a[prop] !== b[prop];
|
|
60
|
-
});
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
var observedNodes = /*#__PURE__*/new Map();
|
|
64
|
-
var rafId;
|
|
65
|
-
|
|
66
|
-
var run = function run() {
|
|
67
|
-
var changedStates = [];
|
|
68
|
-
observedNodes.forEach(function (state, node) {
|
|
69
|
-
var newRect = node.getBoundingClientRect();
|
|
70
|
-
|
|
71
|
-
if (rectChanged(newRect, state.rect)) {
|
|
72
|
-
state.rect = newRect;
|
|
73
|
-
changedStates.push(state);
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
changedStates.forEach(function (state) {
|
|
77
|
-
state.callbacks.forEach(function (cb) {
|
|
78
|
-
return cb(state.rect);
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
rafId = window.requestAnimationFrame(run);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
function observeRect(node, cb) {
|
|
85
|
-
return {
|
|
86
|
-
observe: function observe() {
|
|
87
|
-
var wasEmpty = observedNodes.size === 0;
|
|
88
|
-
|
|
89
|
-
if (observedNodes.has(node)) {
|
|
90
|
-
observedNodes.get(node).callbacks.push(cb);
|
|
91
|
-
} else {
|
|
92
|
-
observedNodes.set(node, {
|
|
93
|
-
rect: undefined,
|
|
94
|
-
hasRectChanged: false,
|
|
95
|
-
callbacks: [cb]
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (wasEmpty) run();
|
|
100
|
-
},
|
|
101
|
-
unobserve: function unobserve() {
|
|
102
|
-
var state = observedNodes.get(node);
|
|
103
|
-
|
|
104
|
-
if (state) {
|
|
105
|
-
// Remove the callback
|
|
106
|
-
var index = state.callbacks.indexOf(cb);
|
|
107
|
-
if (index >= 0) state.callbacks.splice(index, 1); // Remove the node reference
|
|
108
|
-
|
|
109
|
-
if (!state.callbacks.length) observedNodes["delete"](node); // Stop the loop
|
|
110
|
-
|
|
111
|
-
if (!observedNodes.size) cancelAnimationFrame(rafId);
|
|
62
|
+
function _extends() {
|
|
63
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
64
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
65
|
+
var source = arguments[i];
|
|
66
|
+
for (var key in source) {
|
|
67
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
68
|
+
target[key] = source[key];
|
|
69
|
+
}
|
|
112
70
|
}
|
|
113
71
|
}
|
|
72
|
+
return target;
|
|
114
73
|
};
|
|
74
|
+
return _extends.apply(this, arguments);
|
|
115
75
|
}
|
|
116
76
|
|
|
77
|
+
/**
|
|
78
|
+
* virtual-core
|
|
79
|
+
*
|
|
80
|
+
* Copyright (c) TanStack
|
|
81
|
+
*
|
|
82
|
+
* This source code is licensed under the MIT license found in the
|
|
83
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
84
|
+
*
|
|
85
|
+
* @license MIT
|
|
86
|
+
*/
|
|
117
87
|
function memo(getDeps, fn, opts) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
88
|
+
var _opts$initialDeps;
|
|
89
|
+
var deps = (_opts$initialDeps = opts.initialDeps) != null ? _opts$initialDeps : [];
|
|
90
|
+
var result;
|
|
91
|
+
return function () {
|
|
92
|
+
var depTime;
|
|
122
93
|
if (opts.key && opts.debug != null && opts.debug()) depTime = Date.now();
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
94
|
+
var newDeps = getDeps();
|
|
95
|
+
var depsChanged = newDeps.length !== deps.length || newDeps.some(function (dep, index) {
|
|
96
|
+
return deps[index] !== dep;
|
|
97
|
+
});
|
|
126
98
|
if (!depsChanged) {
|
|
127
99
|
return result;
|
|
128
100
|
}
|
|
129
|
-
|
|
130
101
|
deps = newDeps;
|
|
131
|
-
|
|
102
|
+
var resultTime;
|
|
132
103
|
if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now();
|
|
133
|
-
result = fn(
|
|
134
|
-
opts == null ? void 0 : opts.onChange == null ? void 0 : opts.onChange(result);
|
|
135
|
-
|
|
104
|
+
result = fn.apply(void 0, newDeps);
|
|
136
105
|
if (opts.key && opts.debug != null && opts.debug()) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const pad = (str, num) => {
|
|
106
|
+
var depEndTime = Math.round((Date.now() - depTime) * 100) / 100;
|
|
107
|
+
var resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100;
|
|
108
|
+
var resultFpsPercentage = resultEndTime / 16;
|
|
109
|
+
var pad = function pad(str, num) {
|
|
142
110
|
str = String(str);
|
|
143
|
-
|
|
144
111
|
while (str.length < num) {
|
|
145
112
|
str = ' ' + str;
|
|
146
113
|
}
|
|
147
|
-
|
|
148
114
|
return str;
|
|
149
115
|
};
|
|
150
|
-
|
|
151
116
|
console.info("%c\u23F1 " + pad(resultEndTime, 5) + " /" + pad(depEndTime, 5) + " ms", "\n font-size: .6rem;\n font-weight: bold;\n color: hsl(" + Math.max(0, Math.min(120 - 120 * resultFpsPercentage, 120)) + "deg 100% 31%);", opts == null ? void 0 : opts.key);
|
|
152
117
|
}
|
|
153
|
-
|
|
118
|
+
opts == null || opts.onChange == null || opts.onChange(result);
|
|
154
119
|
return result;
|
|
155
120
|
};
|
|
156
121
|
}
|
|
122
|
+
function notUndefined(value, msg) {
|
|
123
|
+
if (value === undefined) {
|
|
124
|
+
throw new Error("Unexpected undefined" + (msg ? ": " + msg : ''));
|
|
125
|
+
} else {
|
|
126
|
+
return value;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
var approxEqual = function approxEqual(a, b) {
|
|
130
|
+
return Math.abs(a - b) < 1;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* virtual-core
|
|
135
|
+
*
|
|
136
|
+
* Copyright (c) TanStack
|
|
137
|
+
*
|
|
138
|
+
* This source code is licensed under the MIT license found in the
|
|
139
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
140
|
+
*
|
|
141
|
+
* @license MIT
|
|
142
|
+
*/
|
|
157
143
|
|
|
158
144
|
//
|
|
159
|
-
const defaultKeyExtractor = index => index;
|
|
160
|
-
const defaultRangeExtractor = range => {
|
|
161
|
-
const start = Math.max(range.startIndex - range.overscan, 0);
|
|
162
|
-
const end = Math.min(range.endIndex + range.overscan, range.count - 1);
|
|
163
|
-
const arr = [];
|
|
164
145
|
|
|
165
|
-
|
|
166
|
-
arr.push(i);
|
|
167
|
-
}
|
|
146
|
+
//
|
|
168
147
|
|
|
148
|
+
var defaultKeyExtractor = function defaultKeyExtractor(index) {
|
|
149
|
+
return index;
|
|
150
|
+
};
|
|
151
|
+
var defaultRangeExtractor = function defaultRangeExtractor(range) {
|
|
152
|
+
var start = Math.max(range.startIndex - range.overscan, 0);
|
|
153
|
+
var end = Math.min(range.endIndex + range.overscan, range.count - 1);
|
|
154
|
+
var arr = [];
|
|
155
|
+
for (var _i = start; _i <= end; _i++) {
|
|
156
|
+
arr.push(_i);
|
|
157
|
+
}
|
|
169
158
|
return arr;
|
|
170
159
|
};
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
160
|
+
var observeElementRect = function observeElementRect(instance, cb) {
|
|
161
|
+
var element = instance.scrollElement;
|
|
162
|
+
if (!element) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
var handler = function handler(rect) {
|
|
166
|
+
var width = rect.width,
|
|
167
|
+
height = rect.height;
|
|
168
|
+
cb({
|
|
169
|
+
width: Math.round(width),
|
|
170
|
+
height: Math.round(height)
|
|
171
|
+
});
|
|
172
|
+
};
|
|
173
|
+
handler(element.getBoundingClientRect());
|
|
174
|
+
var observer = new ResizeObserver(function (entries) {
|
|
175
|
+
var entry = entries[0];
|
|
176
|
+
if (entry != null && entry.borderBoxSize) {
|
|
177
|
+
var box = entry.borderBoxSize[0];
|
|
178
|
+
if (box) {
|
|
179
|
+
handler({
|
|
180
|
+
width: box.inlineSize,
|
|
181
|
+
height: box.blockSize
|
|
182
|
+
});
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
180
185
|
}
|
|
181
|
-
|
|
182
|
-
|
|
186
|
+
handler(element.getBoundingClientRect());
|
|
187
|
+
});
|
|
188
|
+
observer.observe(element, {
|
|
189
|
+
box: 'border-box'
|
|
190
|
+
});
|
|
191
|
+
return function () {
|
|
192
|
+
observer.unobserve(element);
|
|
183
193
|
};
|
|
184
194
|
};
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const observer = observeRect(instance.scrollElement, rect => {
|
|
189
|
-
onResize(rect);
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
if (!instance.scrollElement) {
|
|
195
|
+
var observeWindowRect = function observeWindowRect(instance, cb) {
|
|
196
|
+
var element = instance.scrollElement;
|
|
197
|
+
if (!element) {
|
|
193
198
|
return;
|
|
194
199
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
+
var handler = function handler() {
|
|
201
|
+
cb({
|
|
202
|
+
width: element.innerWidth,
|
|
203
|
+
height: element.innerHeight
|
|
204
|
+
});
|
|
200
205
|
};
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const onResize = () => memoizedCallback({
|
|
206
|
-
width: instance.scrollElement.innerWidth,
|
|
207
|
-
height: instance.scrollElement.innerHeight
|
|
206
|
+
handler();
|
|
207
|
+
element.addEventListener('resize', handler, {
|
|
208
|
+
passive: true
|
|
208
209
|
});
|
|
209
|
-
|
|
210
|
-
|
|
210
|
+
return function () {
|
|
211
|
+
element.removeEventListener('resize', handler);
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
var observeElementOffset = function observeElementOffset(instance, cb) {
|
|
215
|
+
var element = instance.scrollElement;
|
|
216
|
+
if (!element) {
|
|
211
217
|
return;
|
|
212
218
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
219
|
+
var handler = function handler() {
|
|
220
|
+
cb(element[instance.options.horizontal ? 'scrollLeft' : 'scrollTop']);
|
|
221
|
+
};
|
|
222
|
+
handler();
|
|
223
|
+
element.addEventListener('scroll', handler, {
|
|
217
224
|
passive: true
|
|
218
225
|
});
|
|
219
|
-
return ()
|
|
220
|
-
|
|
226
|
+
return function () {
|
|
227
|
+
element.removeEventListener('scroll', handler);
|
|
221
228
|
};
|
|
222
229
|
};
|
|
223
|
-
|
|
224
|
-
element
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
let prevY = instance.scrollElement[propY];
|
|
238
|
-
|
|
239
|
-
const scroll = () => {
|
|
240
|
-
cb(instance.scrollElement[instance.options.horizontal ? propX : propY]);
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
scroll();
|
|
244
|
-
|
|
245
|
-
const onScroll = e => {
|
|
246
|
-
const target = e.currentTarget;
|
|
247
|
-
const scrollX = target[propX];
|
|
248
|
-
const scrollY = target[propY];
|
|
249
|
-
|
|
250
|
-
if (instance.options.horizontal ? prevX - scrollX : prevY - scrollY) {
|
|
251
|
-
scroll();
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
prevX = scrollX;
|
|
255
|
-
prevY = scrollY;
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
instance.scrollElement.addEventListener('scroll', onScroll, {
|
|
259
|
-
capture: false,
|
|
260
|
-
passive: true
|
|
261
|
-
});
|
|
262
|
-
return () => {
|
|
263
|
-
instance.scrollElement.removeEventListener('scroll', onScroll);
|
|
264
|
-
};
|
|
230
|
+
var observeWindowOffset = function observeWindowOffset(instance, cb) {
|
|
231
|
+
var element = instance.scrollElement;
|
|
232
|
+
if (!element) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
var handler = function handler() {
|
|
236
|
+
cb(element[instance.options.horizontal ? 'scrollX' : 'scrollY']);
|
|
237
|
+
};
|
|
238
|
+
handler();
|
|
239
|
+
element.addEventListener('scroll', handler, {
|
|
240
|
+
passive: true
|
|
241
|
+
});
|
|
242
|
+
return function () {
|
|
243
|
+
element.removeEventListener('scroll', handler);
|
|
265
244
|
};
|
|
266
245
|
};
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
246
|
+
var measureElement = function measureElement(element, entry, instance) {
|
|
247
|
+
if (entry != null && entry.borderBoxSize) {
|
|
248
|
+
var box = entry.borderBoxSize[0];
|
|
249
|
+
if (box) {
|
|
250
|
+
var size = Math.round(box[instance.options.horizontal ? 'inlineSize' : 'blockSize']);
|
|
251
|
+
return size;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return Math.round(element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height']);
|
|
272
255
|
};
|
|
273
|
-
|
|
274
|
-
var _instance$scrollEleme;
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
behavior
|
|
278
|
-
|
|
256
|
+
var windowScroll = function windowScroll(offset, _ref, instance) {
|
|
257
|
+
var _instance$scrollEleme, _instance$scrollEleme2;
|
|
258
|
+
var _ref$adjustments = _ref.adjustments,
|
|
259
|
+
adjustments = _ref$adjustments === void 0 ? 0 : _ref$adjustments,
|
|
260
|
+
behavior = _ref.behavior;
|
|
261
|
+
var toOffset = offset + adjustments;
|
|
262
|
+
(_instance$scrollEleme = instance.scrollElement) == null || _instance$scrollEleme.scrollTo == null || _instance$scrollEleme.scrollTo((_instance$scrollEleme2 = {}, _instance$scrollEleme2[instance.options.horizontal ? 'left' : 'top'] = toOffset, _instance$scrollEleme2.behavior = behavior, _instance$scrollEleme2));
|
|
279
263
|
};
|
|
280
|
-
|
|
281
|
-
var _instance$
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
behavior
|
|
285
|
-
|
|
264
|
+
var elementScroll = function elementScroll(offset, _ref2, instance) {
|
|
265
|
+
var _instance$scrollEleme3, _instance$scrollEleme4;
|
|
266
|
+
var _ref2$adjustments = _ref2.adjustments,
|
|
267
|
+
adjustments = _ref2$adjustments === void 0 ? 0 : _ref2$adjustments,
|
|
268
|
+
behavior = _ref2.behavior;
|
|
269
|
+
var toOffset = offset + adjustments;
|
|
270
|
+
(_instance$scrollEleme3 = instance.scrollElement) == null || _instance$scrollEleme3.scrollTo == null || _instance$scrollEleme3.scrollTo((_instance$scrollEleme4 = {}, _instance$scrollEleme4[instance.options.horizontal ? 'left' : 'top'] = toOffset, _instance$scrollEleme4.behavior = behavior, _instance$scrollEleme4));
|
|
286
271
|
};
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
rangeExtractor: defaultRangeExtractor,
|
|
314
|
-
enableSmoothScroll: true,
|
|
315
|
-
onChange: () => {},
|
|
316
|
-
measureElement,
|
|
317
|
-
initialRect: {
|
|
318
|
-
width: 0,
|
|
319
|
-
height: 0
|
|
320
|
-
},
|
|
321
|
-
...opts
|
|
322
|
-
};
|
|
323
|
-
};
|
|
324
|
-
|
|
325
|
-
this.notify = () => {
|
|
326
|
-
var _this$options$onChang, _this$options;
|
|
327
|
-
|
|
328
|
-
(_this$options$onChang = (_this$options = this.options).onChange) == null ? void 0 : _this$options$onChang.call(_this$options, this);
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
this.cleanup = () => {
|
|
332
|
-
this.unsubs.filter(Boolean).forEach(d => d());
|
|
333
|
-
this.unsubs = [];
|
|
334
|
-
this.scrollElement = null;
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
this._didMount = () => {
|
|
338
|
-
return () => {
|
|
339
|
-
this.cleanup();
|
|
340
|
-
};
|
|
341
|
-
};
|
|
342
|
-
|
|
343
|
-
this._willUpdate = () => {
|
|
344
|
-
const scrollElement = this.options.getScrollElement();
|
|
345
|
-
|
|
346
|
-
if (this.scrollElement !== scrollElement) {
|
|
347
|
-
this.cleanup();
|
|
348
|
-
this.scrollElement = scrollElement;
|
|
349
|
-
this.unsubs.push(this.options.observeElementRect(this, rect => {
|
|
350
|
-
this.scrollRect = rect;
|
|
351
|
-
this.notify();
|
|
352
|
-
}));
|
|
353
|
-
this.unsubs.push(this.options.observeElementOffset(this, offset => {
|
|
354
|
-
this.scrollOffset = offset;
|
|
355
|
-
this.notify();
|
|
356
|
-
}));
|
|
272
|
+
var Virtualizer = function Virtualizer(_opts) {
|
|
273
|
+
var _this = this;
|
|
274
|
+
this.unsubs = [];
|
|
275
|
+
this.scrollElement = null;
|
|
276
|
+
this.isScrolling = false;
|
|
277
|
+
this.isScrollingTimeoutId = null;
|
|
278
|
+
this.scrollToIndexTimeoutId = null;
|
|
279
|
+
this.measurementsCache = [];
|
|
280
|
+
this.itemSizeCache = new Map();
|
|
281
|
+
this.pendingMeasuredCacheIndexes = [];
|
|
282
|
+
this.scrollDirection = null;
|
|
283
|
+
this.scrollAdjustments = 0;
|
|
284
|
+
this.measureElementCache = new Map();
|
|
285
|
+
this.observer = function () {
|
|
286
|
+
var _ro = null;
|
|
287
|
+
var get = function get() {
|
|
288
|
+
if (_ro) {
|
|
289
|
+
return _ro;
|
|
290
|
+
} else if (typeof ResizeObserver !== 'undefined') {
|
|
291
|
+
return _ro = new ResizeObserver(function (entries) {
|
|
292
|
+
entries.forEach(function (entry) {
|
|
293
|
+
_this._measureElement(entry.target, entry);
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
} else {
|
|
297
|
+
return null;
|
|
357
298
|
}
|
|
358
299
|
};
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
const size = typeof measuredSize === 'number' ? measuredSize : this.options.estimateSize(i);
|
|
374
|
-
const end = start + size;
|
|
375
|
-
measurements[i] = {
|
|
376
|
-
index: i,
|
|
377
|
-
start,
|
|
378
|
-
size,
|
|
379
|
-
end,
|
|
380
|
-
key
|
|
381
|
-
};
|
|
300
|
+
return {
|
|
301
|
+
disconnect: function disconnect() {
|
|
302
|
+
var _get;
|
|
303
|
+
return (_get = get()) == null ? void 0 : _get.disconnect();
|
|
304
|
+
},
|
|
305
|
+
observe: function observe(target) {
|
|
306
|
+
var _get2;
|
|
307
|
+
return (_get2 = get()) == null ? void 0 : _get2.observe(target, {
|
|
308
|
+
box: 'border-box'
|
|
309
|
+
});
|
|
310
|
+
},
|
|
311
|
+
unobserve: function unobserve(target) {
|
|
312
|
+
var _get3;
|
|
313
|
+
return (_get3 = get()) == null ? void 0 : _get3.unobserve(target);
|
|
382
314
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
315
|
+
};
|
|
316
|
+
}();
|
|
317
|
+
this.range = null;
|
|
318
|
+
this.setOptions = function (opts) {
|
|
319
|
+
Object.entries(opts).forEach(function (_ref3) {
|
|
320
|
+
var key = _ref3[0],
|
|
321
|
+
value = _ref3[1];
|
|
322
|
+
if (typeof value === 'undefined') delete opts[key];
|
|
389
323
|
});
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
324
|
+
_this.options = _extends({
|
|
325
|
+
debug: false,
|
|
326
|
+
initialOffset: 0,
|
|
327
|
+
overscan: 1,
|
|
328
|
+
paddingStart: 0,
|
|
329
|
+
paddingEnd: 0,
|
|
330
|
+
scrollPaddingStart: 0,
|
|
331
|
+
scrollPaddingEnd: 0,
|
|
332
|
+
horizontal: false,
|
|
333
|
+
getItemKey: defaultKeyExtractor,
|
|
334
|
+
rangeExtractor: defaultRangeExtractor,
|
|
335
|
+
onChange: function onChange() {},
|
|
336
|
+
measureElement: measureElement,
|
|
337
|
+
initialRect: {
|
|
338
|
+
width: 0,
|
|
339
|
+
height: 0
|
|
340
|
+
},
|
|
341
|
+
scrollMargin: 0,
|
|
342
|
+
scrollingDelay: 150,
|
|
343
|
+
indexAttribute: 'data-index',
|
|
344
|
+
initialMeasurementsCache: [],
|
|
345
|
+
lanes: 1
|
|
346
|
+
}, opts);
|
|
347
|
+
};
|
|
348
|
+
this.notify = function (sync) {
|
|
349
|
+
_this.options.onChange == null || _this.options.onChange(_this, sync);
|
|
350
|
+
};
|
|
351
|
+
this.maybeNotify = memo(function () {
|
|
352
|
+
_this.calculateRange();
|
|
353
|
+
return [_this.isScrolling, _this.range ? _this.range.startIndex : null, _this.range ? _this.range.endIndex : null];
|
|
354
|
+
}, function (isScrolling) {
|
|
355
|
+
_this.notify(isScrolling);
|
|
356
|
+
}, {
|
|
357
|
+
key: 'maybeNotify',
|
|
358
|
+
debug: function debug() {
|
|
359
|
+
return _this.options.debug;
|
|
360
|
+
},
|
|
361
|
+
initialDeps: [this.isScrolling, this.range ? this.range.startIndex : null, this.range ? this.range.endIndex : null]
|
|
362
|
+
});
|
|
363
|
+
this.cleanup = function () {
|
|
364
|
+
_this.unsubs.filter(Boolean).forEach(function (d) {
|
|
365
|
+
return d();
|
|
399
366
|
});
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
367
|
+
_this.unsubs = [];
|
|
368
|
+
_this.scrollElement = null;
|
|
369
|
+
};
|
|
370
|
+
this._didMount = function () {
|
|
371
|
+
_this.measureElementCache.forEach(_this.observer.observe);
|
|
372
|
+
return function () {
|
|
373
|
+
_this.observer.disconnect();
|
|
374
|
+
_this.cleanup();
|
|
375
|
+
};
|
|
376
|
+
};
|
|
377
|
+
this._willUpdate = function () {
|
|
378
|
+
var scrollElement = _this.options.getScrollElement();
|
|
379
|
+
if (_this.scrollElement !== scrollElement) {
|
|
380
|
+
_this.cleanup();
|
|
381
|
+
_this.scrollElement = scrollElement;
|
|
382
|
+
_this._scrollToOffset(_this.scrollOffset, {
|
|
383
|
+
adjustments: undefined,
|
|
384
|
+
behavior: undefined
|
|
404
385
|
});
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
const item = this.measurementsCache[index];
|
|
413
|
-
|
|
414
|
-
if (!measurableItem) {
|
|
386
|
+
_this.unsubs.push(_this.options.observeElementRect(_this, function (rect) {
|
|
387
|
+
_this.scrollRect = rect;
|
|
388
|
+
_this.maybeNotify();
|
|
389
|
+
}));
|
|
390
|
+
_this.unsubs.push(_this.options.observeElementOffset(_this, function (offset) {
|
|
391
|
+
_this.scrollAdjustments = 0;
|
|
392
|
+
if (_this.scrollOffset === offset) {
|
|
415
393
|
return;
|
|
416
394
|
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
if (measuredItemSize !== itemSize) {
|
|
422
|
-
if (item.start < this.scrollOffset) {
|
|
423
|
-
if (this.options.debug) {
|
|
424
|
-
console.info('correction', measuredItemSize - itemSize);
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
if (!this.destinationOffset) {
|
|
428
|
-
this._scrollToOffset(this.scrollOffset + (measuredItemSize - itemSize), false);
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
this.pendingMeasuredCacheIndexes.push(index);
|
|
433
|
-
this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
|
|
434
|
-
[item.key]: measuredItemSize
|
|
435
|
-
};
|
|
436
|
-
this.notify();
|
|
395
|
+
if (_this.isScrollingTimeoutId !== null) {
|
|
396
|
+
clearTimeout(_this.isScrollingTimeoutId);
|
|
397
|
+
_this.isScrollingTimeoutId = null;
|
|
437
398
|
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
399
|
+
_this.isScrolling = true;
|
|
400
|
+
_this.scrollDirection = _this.scrollOffset < offset ? 'forward' : 'backward';
|
|
401
|
+
_this.scrollOffset = offset;
|
|
402
|
+
_this.maybeNotify();
|
|
403
|
+
_this.isScrollingTimeoutId = setTimeout(function () {
|
|
404
|
+
_this.isScrollingTimeoutId = null;
|
|
405
|
+
_this.isScrolling = false;
|
|
406
|
+
_this.scrollDirection = null;
|
|
407
|
+
_this.maybeNotify();
|
|
408
|
+
}, _this.options.scrollingDelay);
|
|
409
|
+
}));
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
this.getSize = function () {
|
|
413
|
+
return _this.scrollRect[_this.options.horizontal ? 'width' : 'height'];
|
|
414
|
+
};
|
|
415
|
+
this.memoOptions = memo(function () {
|
|
416
|
+
return [_this.options.count, _this.options.paddingStart, _this.options.scrollMargin, _this.options.getItemKey];
|
|
417
|
+
}, function (count, paddingStart, scrollMargin, getItemKey) {
|
|
418
|
+
_this.pendingMeasuredCacheIndexes = [];
|
|
419
|
+
return {
|
|
420
|
+
count: count,
|
|
421
|
+
paddingStart: paddingStart,
|
|
422
|
+
scrollMargin: scrollMargin,
|
|
423
|
+
getItemKey: getItemKey
|
|
424
|
+
};
|
|
425
|
+
}, {
|
|
426
|
+
key: false
|
|
427
|
+
});
|
|
428
|
+
this.getFurthestMeasurement = function (measurements, index) {
|
|
429
|
+
var furthestMeasurementsFound = new Map();
|
|
430
|
+
var furthestMeasurements = new Map();
|
|
431
|
+
for (var m = index - 1; m >= 0; m--) {
|
|
432
|
+
var measurement = measurements[m];
|
|
433
|
+
if (furthestMeasurementsFound.has(measurement.lane)) {
|
|
434
|
+
continue;
|
|
452
435
|
}
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
436
|
+
var previousFurthestMeasurement = furthestMeasurements.get(measurement.lane);
|
|
437
|
+
if (previousFurthestMeasurement == null || measurement.end > previousFurthestMeasurement.end) {
|
|
438
|
+
furthestMeasurements.set(measurement.lane, measurement);
|
|
439
|
+
} else if (measurement.end < previousFurthestMeasurement.end) {
|
|
440
|
+
furthestMeasurementsFound.set(measurement.lane, true);
|
|
441
|
+
}
|
|
442
|
+
if (furthestMeasurementsFound.size === _this.options.lanes) {
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
return furthestMeasurements.size === _this.options.lanes ? Array.from(furthestMeasurements.values()).sort(function (a, b) {
|
|
447
|
+
return a.end - b.end;
|
|
448
|
+
})[0] : undefined;
|
|
449
|
+
};
|
|
450
|
+
this.getMeasurements = memo(function () {
|
|
451
|
+
return [_this.memoOptions(), _this.itemSizeCache];
|
|
452
|
+
}, function (_ref4, itemSizeCache) {
|
|
453
|
+
var count = _ref4.count,
|
|
454
|
+
paddingStart = _ref4.paddingStart,
|
|
455
|
+
scrollMargin = _ref4.scrollMargin,
|
|
456
|
+
getItemKey = _ref4.getItemKey;
|
|
457
|
+
var min = _this.pendingMeasuredCacheIndexes.length > 0 ? Math.min.apply(Math, _this.pendingMeasuredCacheIndexes) : 0;
|
|
458
|
+
_this.pendingMeasuredCacheIndexes = [];
|
|
459
|
+
var measurements = _this.measurementsCache.slice(0, min);
|
|
460
|
+
for (var _i2 = min; _i2 < count; _i2++) {
|
|
461
|
+
var key = getItemKey(_i2);
|
|
462
|
+
var furthestMeasurement = _this.options.lanes === 1 ? measurements[_i2 - 1] : _this.getFurthestMeasurement(measurements, _i2);
|
|
463
|
+
var start = furthestMeasurement ? furthestMeasurement.end : paddingStart + scrollMargin;
|
|
464
|
+
var measuredSize = itemSizeCache.get(key);
|
|
465
|
+
var size = typeof measuredSize === 'number' ? measuredSize : _this.options.estimateSize(_i2);
|
|
466
|
+
var end = start + size;
|
|
467
|
+
var lane = furthestMeasurement ? furthestMeasurement.lane : _i2 % _this.options.lanes;
|
|
468
|
+
measurements[_i2] = {
|
|
469
|
+
index: _i2,
|
|
470
|
+
start: start,
|
|
471
|
+
size: size,
|
|
472
|
+
end: end,
|
|
473
|
+
key: key,
|
|
474
|
+
lane: lane
|
|
489
475
|
};
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
476
|
+
}
|
|
477
|
+
_this.measurementsCache = measurements;
|
|
478
|
+
return measurements;
|
|
479
|
+
}, {
|
|
480
|
+
key: 'getMeasurements',
|
|
481
|
+
debug: function debug() {
|
|
482
|
+
return _this.options.debug;
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
this.calculateRange = memo(function () {
|
|
486
|
+
return [_this.getMeasurements(), _this.getSize(), _this.scrollOffset];
|
|
487
|
+
}, function (measurements, outerSize, scrollOffset) {
|
|
488
|
+
return _this.range = measurements.length > 0 && outerSize > 0 ? calculateRange({
|
|
489
|
+
measurements: measurements,
|
|
490
|
+
outerSize: outerSize,
|
|
491
|
+
scrollOffset: scrollOffset
|
|
492
|
+
}) : null;
|
|
493
|
+
}, {
|
|
494
|
+
key: 'calculateRange',
|
|
495
|
+
debug: function debug() {
|
|
496
|
+
return _this.options.debug;
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
this.getIndexes = memo(function () {
|
|
500
|
+
return [_this.options.rangeExtractor, _this.calculateRange(), _this.options.overscan, _this.options.count];
|
|
501
|
+
}, function (rangeExtractor, range, overscan, count) {
|
|
502
|
+
return range === null ? [] : rangeExtractor(_extends({}, range, {
|
|
503
|
+
overscan: overscan,
|
|
504
|
+
count: count
|
|
505
|
+
}));
|
|
506
|
+
}, {
|
|
507
|
+
key: 'getIndexes',
|
|
508
|
+
debug: function debug() {
|
|
509
|
+
return _this.options.debug;
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
this.indexFromElement = function (node) {
|
|
513
|
+
var attributeName = _this.options.indexAttribute;
|
|
514
|
+
var indexStr = node.getAttribute(attributeName);
|
|
515
|
+
if (!indexStr) {
|
|
516
|
+
console.warn("Missing attribute name '" + attributeName + "={index}' on measured element.");
|
|
517
|
+
return -1;
|
|
518
|
+
}
|
|
519
|
+
return parseInt(indexStr, 10);
|
|
520
|
+
};
|
|
521
|
+
this._measureElement = function (node, entry) {
|
|
522
|
+
var item = _this.measurementsCache[_this.indexFromElement(node)];
|
|
523
|
+
if (!item || !node.isConnected) {
|
|
524
|
+
_this.measureElementCache.forEach(function (cached, key) {
|
|
525
|
+
if (cached === node) {
|
|
526
|
+
_this.observer.unobserve(node);
|
|
527
|
+
_this.measureElementCache["delete"](key);
|
|
528
|
+
}
|
|
494
529
|
});
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
} = _temp2 === void 0 ? {
|
|
502
|
-
align: 'auto'
|
|
503
|
-
} : _temp2;
|
|
504
|
-
|
|
505
|
-
const measurements = _this.getMeasurements();
|
|
506
|
-
|
|
507
|
-
const offset = _this.scrollOffset;
|
|
508
|
-
|
|
509
|
-
const size = _this.getSize();
|
|
510
|
-
|
|
511
|
-
const {
|
|
512
|
-
count
|
|
513
|
-
} = _this.options;
|
|
514
|
-
const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
|
|
515
|
-
|
|
516
|
-
if (!measurement) {
|
|
517
|
-
return;
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
var prevNode = _this.measureElementCache.get(item.key);
|
|
533
|
+
if (prevNode !== node) {
|
|
534
|
+
if (prevNode) {
|
|
535
|
+
_this.observer.unobserve(prevNode);
|
|
518
536
|
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
537
|
+
_this.observer.observe(node);
|
|
538
|
+
_this.measureElementCache.set(item.key, node);
|
|
539
|
+
}
|
|
540
|
+
var measuredItemSize = _this.options.measureElement(node, entry, _this);
|
|
541
|
+
_this.resizeItem(item, measuredItemSize);
|
|
542
|
+
};
|
|
543
|
+
this.resizeItem = function (item, size) {
|
|
544
|
+
var _this$itemSizeCache$g;
|
|
545
|
+
var itemSize = (_this$itemSizeCache$g = _this.itemSizeCache.get(item.key)) != null ? _this$itemSizeCache$g : item.size;
|
|
546
|
+
var delta = size - itemSize;
|
|
547
|
+
if (delta !== 0) {
|
|
548
|
+
if (item.start < _this.scrollOffset) {
|
|
549
|
+
if (_this.options.debug) {
|
|
550
|
+
console.info('correction', delta);
|
|
527
551
|
}
|
|
552
|
+
_this._scrollToOffset(_this.scrollOffset, {
|
|
553
|
+
adjustments: _this.scrollAdjustments += delta,
|
|
554
|
+
behavior: undefined
|
|
555
|
+
});
|
|
528
556
|
}
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
557
|
+
_this.pendingMeasuredCacheIndexes.push(item.index);
|
|
558
|
+
_this.itemSizeCache = new Map(_this.itemSizeCache.set(item.key, size));
|
|
559
|
+
_this.notify(false);
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
this.measureElement = function (node) {
|
|
563
|
+
if (!node) {
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
_this._measureElement(node, undefined);
|
|
567
|
+
};
|
|
568
|
+
this.getVirtualItems = memo(function () {
|
|
569
|
+
return [_this.getIndexes(), _this.getMeasurements()];
|
|
570
|
+
}, function (indexes, measurements) {
|
|
571
|
+
var virtualItems = [];
|
|
572
|
+
for (var k = 0, len = indexes.length; k < len; k++) {
|
|
573
|
+
var _i3 = indexes[k];
|
|
574
|
+
var measurement = measurements[_i3];
|
|
575
|
+
virtualItems.push(measurement);
|
|
576
|
+
}
|
|
577
|
+
return virtualItems;
|
|
578
|
+
}, {
|
|
579
|
+
key: 'getIndexes',
|
|
580
|
+
debug: function debug() {
|
|
581
|
+
return _this.options.debug;
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
this.getVirtualItemForOffset = function (offset) {
|
|
585
|
+
var measurements = _this.getMeasurements();
|
|
586
|
+
return notUndefined(measurements[findNearestBinarySearch(0, measurements.length - 1, function (index) {
|
|
587
|
+
return notUndefined(measurements[index]).start;
|
|
588
|
+
}, offset)]);
|
|
589
|
+
};
|
|
590
|
+
this.getOffsetForAlignment = function (toOffset, align) {
|
|
591
|
+
var size = _this.getSize();
|
|
592
|
+
if (align === 'auto') {
|
|
593
|
+
if (toOffset <= _this.scrollOffset) {
|
|
594
|
+
align = 'start';
|
|
595
|
+
} else if (toOffset >= _this.scrollOffset + size) {
|
|
596
|
+
align = 'end';
|
|
597
|
+
} else {
|
|
598
|
+
align = 'start';
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
if (align === 'start') {
|
|
602
|
+
toOffset = toOffset;
|
|
603
|
+
} else if (align === 'end') {
|
|
604
|
+
toOffset = toOffset - size;
|
|
605
|
+
} else if (align === 'center') {
|
|
606
|
+
toOffset = toOffset - size / 2;
|
|
607
|
+
}
|
|
608
|
+
var scrollSizeProp = _this.options.horizontal ? 'scrollWidth' : 'scrollHeight';
|
|
609
|
+
var scrollSize = _this.scrollElement ? 'document' in _this.scrollElement ? _this.scrollElement.document.documentElement[scrollSizeProp] : _this.scrollElement[scrollSizeProp] : 0;
|
|
610
|
+
var maxOffset = scrollSize - _this.getSize();
|
|
611
|
+
return Math.max(Math.min(maxOffset, toOffset), 0);
|
|
612
|
+
};
|
|
613
|
+
this.getOffsetForIndex = function (index, align) {
|
|
614
|
+
if (align === void 0) {
|
|
615
|
+
align = 'auto';
|
|
616
|
+
}
|
|
617
|
+
index = Math.max(0, Math.min(index, _this.options.count - 1));
|
|
618
|
+
var measurement = notUndefined(_this.getMeasurements()[index]);
|
|
619
|
+
if (align === 'auto') {
|
|
620
|
+
if (measurement.end >= _this.scrollOffset + _this.getSize() - _this.options.scrollPaddingEnd) {
|
|
621
|
+
align = 'end';
|
|
622
|
+
} else if (measurement.start <= _this.scrollOffset + _this.options.scrollPaddingStart) {
|
|
623
|
+
align = 'start';
|
|
624
|
+
} else {
|
|
625
|
+
return [_this.scrollOffset, align];
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
|
|
629
|
+
return [_this.getOffsetForAlignment(toOffset, align), align];
|
|
630
|
+
};
|
|
631
|
+
this.isDynamicMode = function () {
|
|
632
|
+
return _this.measureElementCache.size > 0;
|
|
633
|
+
};
|
|
634
|
+
this.cancelScrollToIndex = function () {
|
|
635
|
+
if (_this.scrollToIndexTimeoutId !== null) {
|
|
636
|
+
clearTimeout(_this.scrollToIndexTimeoutId);
|
|
637
|
+
_this.scrollToIndexTimeoutId = null;
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
this.scrollToOffset = function (toOffset, _temp) {
|
|
641
|
+
var _ref5 = _temp === void 0 ? {} : _temp,
|
|
642
|
+
_ref5$align = _ref5.align,
|
|
643
|
+
align = _ref5$align === void 0 ? 'start' : _ref5$align,
|
|
644
|
+
behavior = _ref5.behavior;
|
|
645
|
+
_this.cancelScrollToIndex();
|
|
646
|
+
if (behavior === 'smooth' && _this.isDynamicMode()) {
|
|
647
|
+
console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.');
|
|
648
|
+
}
|
|
649
|
+
_this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), {
|
|
650
|
+
adjustments: undefined,
|
|
651
|
+
behavior: behavior
|
|
652
|
+
});
|
|
653
|
+
};
|
|
654
|
+
this.scrollToIndex = function (index, _temp2) {
|
|
655
|
+
var _ref6 = _temp2 === void 0 ? {} : _temp2,
|
|
656
|
+
_ref6$align = _ref6.align,
|
|
657
|
+
initialAlign = _ref6$align === void 0 ? 'auto' : _ref6$align,
|
|
658
|
+
behavior = _ref6.behavior;
|
|
659
|
+
index = Math.max(0, Math.min(index, _this.options.count - 1));
|
|
660
|
+
_this.cancelScrollToIndex();
|
|
661
|
+
if (behavior === 'smooth' && _this.isDynamicMode()) {
|
|
662
|
+
console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.');
|
|
663
|
+
}
|
|
664
|
+
var _this$getOffsetForInd = _this.getOffsetForIndex(index, initialAlign),
|
|
665
|
+
toOffset = _this$getOffsetForInd[0],
|
|
666
|
+
align = _this$getOffsetForInd[1];
|
|
667
|
+
_this._scrollToOffset(toOffset, {
|
|
668
|
+
adjustments: undefined,
|
|
669
|
+
behavior: behavior
|
|
670
|
+
});
|
|
671
|
+
if (behavior !== 'smooth' && _this.isDynamicMode()) {
|
|
672
|
+
_this.scrollToIndexTimeoutId = setTimeout(function () {
|
|
673
|
+
_this.scrollToIndexTimeoutId = null;
|
|
674
|
+
var elementInDOM = _this.measureElementCache.has(_this.options.getItemKey(index));
|
|
675
|
+
if (elementInDOM) {
|
|
676
|
+
var _this$getOffsetForInd2 = _this.getOffsetForIndex(index, align),
|
|
677
|
+
_toOffset = _this$getOffsetForInd2[0];
|
|
678
|
+
if (!approxEqual(_toOffset, _this.scrollOffset)) {
|
|
679
|
+
_this.scrollToIndex(index, {
|
|
680
|
+
align: align,
|
|
681
|
+
behavior: behavior
|
|
682
|
+
});
|
|
560
683
|
}
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
684
|
+
} else {
|
|
685
|
+
_this.scrollToIndex(index, {
|
|
686
|
+
align: align,
|
|
687
|
+
behavior: behavior
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
this.scrollBy = function (delta, _temp3) {
|
|
694
|
+
var _ref7 = _temp3 === void 0 ? {} : _temp3,
|
|
695
|
+
behavior = _ref7.behavior;
|
|
696
|
+
_this.cancelScrollToIndex();
|
|
697
|
+
if (behavior === 'smooth' && _this.isDynamicMode()) {
|
|
698
|
+
console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.');
|
|
699
|
+
}
|
|
700
|
+
_this._scrollToOffset(_this.scrollOffset + delta, {
|
|
701
|
+
adjustments: undefined,
|
|
702
|
+
behavior: behavior
|
|
703
|
+
});
|
|
704
|
+
};
|
|
705
|
+
this.getTotalSize = function () {
|
|
706
|
+
var _this$getMeasurements;
|
|
707
|
+
return (((_this$getMeasurements = _this.getMeasurements()[_this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || _this.options.paddingStart) - _this.options.scrollMargin + _this.options.paddingEnd;
|
|
708
|
+
};
|
|
709
|
+
this._scrollToOffset = function (offset, _ref8) {
|
|
710
|
+
var adjustments = _ref8.adjustments,
|
|
711
|
+
behavior = _ref8.behavior;
|
|
712
|
+
_this.options.scrollToFn(offset, {
|
|
713
|
+
behavior: behavior,
|
|
714
|
+
adjustments: adjustments
|
|
715
|
+
}, _this);
|
|
716
|
+
};
|
|
717
|
+
this.measure = function () {
|
|
718
|
+
_this.itemSizeCache = new Map();
|
|
719
|
+
_this.notify(false);
|
|
720
|
+
};
|
|
721
|
+
this.setOptions(_opts);
|
|
722
|
+
this.scrollRect = this.options.initialRect;
|
|
723
|
+
this.scrollOffset = this.options.initialOffset;
|
|
724
|
+
this.measurementsCache = this.options.initialMeasurementsCache;
|
|
725
|
+
this.measurementsCache.forEach(function (item) {
|
|
726
|
+
_this.itemSizeCache.set(item.key, item.size);
|
|
727
|
+
});
|
|
728
|
+
this.maybeNotify();
|
|
729
|
+
};
|
|
730
|
+
var findNearestBinarySearch = function findNearestBinarySearch(low, high, getCurrentValue, value) {
|
|
583
731
|
while (low <= high) {
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
732
|
+
var middle = (low + high) / 2 | 0;
|
|
733
|
+
var currentValue = getCurrentValue(middle);
|
|
587
734
|
if (currentValue < value) {
|
|
588
735
|
low = middle + 1;
|
|
589
736
|
} else if (currentValue > value) {
|
|
@@ -592,82 +739,88 @@
|
|
|
592
739
|
return middle;
|
|
593
740
|
}
|
|
594
741
|
}
|
|
595
|
-
|
|
596
742
|
if (low > 0) {
|
|
597
743
|
return low - 1;
|
|
598
744
|
} else {
|
|
599
745
|
return 0;
|
|
600
746
|
}
|
|
601
747
|
};
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
|
|
614
|
-
let endIndex = startIndex;
|
|
615
|
-
|
|
748
|
+
function calculateRange(_ref9) {
|
|
749
|
+
var measurements = _ref9.measurements,
|
|
750
|
+
outerSize = _ref9.outerSize,
|
|
751
|
+
scrollOffset = _ref9.scrollOffset;
|
|
752
|
+
var count = measurements.length - 1;
|
|
753
|
+
var getOffset = function getOffset(index) {
|
|
754
|
+
return measurements[index].start;
|
|
755
|
+
};
|
|
756
|
+
var startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
|
|
757
|
+
var endIndex = startIndex;
|
|
616
758
|
while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
|
|
617
759
|
endIndex++;
|
|
618
760
|
}
|
|
619
|
-
|
|
620
761
|
return {
|
|
621
|
-
startIndex,
|
|
622
|
-
endIndex
|
|
762
|
+
startIndex: startIndex,
|
|
763
|
+
endIndex: endIndex
|
|
623
764
|
};
|
|
624
765
|
}
|
|
625
766
|
|
|
626
|
-
|
|
767
|
+
//
|
|
627
768
|
|
|
769
|
+
var useIsomorphicLayoutEffect = typeof document !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
|
|
628
770
|
function useVirtualizerBase(options) {
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
771
|
+
var rerender = React__namespace.useReducer(function () {
|
|
772
|
+
return {};
|
|
773
|
+
}, {})[1];
|
|
774
|
+
var resolvedOptions = _extends$1({}, options, {
|
|
775
|
+
onChange: function onChange(instance, sync) {
|
|
776
|
+
if (sync) {
|
|
777
|
+
reactDom.flushSync(rerender);
|
|
778
|
+
} else {
|
|
779
|
+
rerender();
|
|
780
|
+
}
|
|
781
|
+
options.onChange == null || options.onChange(instance, sync);
|
|
634
782
|
}
|
|
635
|
-
};
|
|
636
|
-
|
|
783
|
+
});
|
|
784
|
+
var _React$useState = React__namespace.useState(function () {
|
|
785
|
+
return new Virtualizer(resolvedOptions);
|
|
786
|
+
}),
|
|
787
|
+
instance = _React$useState[0];
|
|
637
788
|
instance.setOptions(resolvedOptions);
|
|
638
|
-
React__namespace.useEffect(()
|
|
789
|
+
React__namespace.useEffect(function () {
|
|
639
790
|
return instance._didMount();
|
|
640
791
|
}, []);
|
|
641
|
-
useIsomorphicLayoutEffect(()
|
|
792
|
+
useIsomorphicLayoutEffect(function () {
|
|
642
793
|
return instance._willUpdate();
|
|
643
794
|
});
|
|
644
795
|
return instance;
|
|
645
796
|
}
|
|
646
|
-
|
|
647
797
|
function useVirtualizer(options) {
|
|
648
|
-
return useVirtualizerBase({
|
|
798
|
+
return useVirtualizerBase(_extends$1({
|
|
649
799
|
observeElementRect: observeElementRect,
|
|
650
800
|
observeElementOffset: observeElementOffset,
|
|
651
|
-
scrollToFn: elementScroll
|
|
652
|
-
|
|
653
|
-
});
|
|
801
|
+
scrollToFn: elementScroll
|
|
802
|
+
}, options));
|
|
654
803
|
}
|
|
655
804
|
function useWindowVirtualizer(options) {
|
|
656
|
-
return useVirtualizerBase({
|
|
657
|
-
getScrollElement: ()
|
|
805
|
+
return useVirtualizerBase(_extends$1({
|
|
806
|
+
getScrollElement: function getScrollElement() {
|
|
807
|
+
return typeof document !== 'undefined' ? window : null;
|
|
808
|
+
},
|
|
658
809
|
observeElementRect: observeWindowRect,
|
|
659
810
|
observeElementOffset: observeWindowOffset,
|
|
660
811
|
scrollToFn: windowScroll,
|
|
661
|
-
|
|
662
|
-
});
|
|
812
|
+
initialOffset: typeof document !== 'undefined' ? window.scrollY : undefined
|
|
813
|
+
}, options));
|
|
663
814
|
}
|
|
664
815
|
|
|
665
816
|
exports.Virtualizer = Virtualizer;
|
|
817
|
+
exports.approxEqual = approxEqual;
|
|
666
818
|
exports.defaultKeyExtractor = defaultKeyExtractor;
|
|
667
819
|
exports.defaultRangeExtractor = defaultRangeExtractor;
|
|
668
820
|
exports.elementScroll = elementScroll;
|
|
669
821
|
exports.measureElement = measureElement;
|
|
670
822
|
exports.memo = memo;
|
|
823
|
+
exports.notUndefined = notUndefined;
|
|
671
824
|
exports.observeElementOffset = observeElementOffset;
|
|
672
825
|
exports.observeElementRect = observeElementRect;
|
|
673
826
|
exports.observeWindowOffset = observeWindowOffset;
|