@react-stately/virtualizer 3.7.2-nightly.4649 → 3.7.2-nightly.4656

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 (55) hide show
  1. package/dist/Layout.main.js +2 -41
  2. package/dist/Layout.main.js.map +1 -1
  3. package/dist/Layout.mjs +2 -41
  4. package/dist/Layout.module.js +2 -41
  5. package/dist/Layout.module.js.map +1 -1
  6. package/dist/LayoutInfo.main.js.map +1 -1
  7. package/dist/LayoutInfo.module.js.map +1 -1
  8. package/dist/OverscanManager.main.js +8 -43
  9. package/dist/OverscanManager.main.js.map +1 -1
  10. package/dist/OverscanManager.mjs +8 -43
  11. package/dist/OverscanManager.module.js +8 -43
  12. package/dist/OverscanManager.module.js.map +1 -1
  13. package/dist/ReusableView.main.js +23 -0
  14. package/dist/ReusableView.main.js.map +1 -1
  15. package/dist/ReusableView.mjs +23 -0
  16. package/dist/ReusableView.module.js +23 -0
  17. package/dist/ReusableView.module.js.map +1 -1
  18. package/dist/Virtualizer.main.js +123 -710
  19. package/dist/Virtualizer.main.js.map +1 -1
  20. package/dist/Virtualizer.mjs +124 -711
  21. package/dist/Virtualizer.module.js +124 -711
  22. package/dist/Virtualizer.module.js.map +1 -1
  23. package/dist/types.d.ts +64 -225
  24. package/dist/types.d.ts.map +1 -1
  25. package/dist/useVirtualizerState.main.js +39 -40
  26. package/dist/useVirtualizerState.main.js.map +1 -1
  27. package/dist/useVirtualizerState.mjs +40 -41
  28. package/dist/useVirtualizerState.module.js +40 -41
  29. package/dist/useVirtualizerState.module.js.map +1 -1
  30. package/dist/utils.main.js +1 -27
  31. package/dist/utils.main.js.map +1 -1
  32. package/dist/utils.mjs +2 -26
  33. package/dist/utils.module.js +2 -26
  34. package/dist/utils.module.js.map +1 -1
  35. package/package.json +4 -4
  36. package/src/Layout.ts +10 -55
  37. package/src/LayoutInfo.ts +2 -2
  38. package/src/OverscanManager.ts +10 -47
  39. package/src/ReusableView.ts +36 -7
  40. package/src/Virtualizer.ts +163 -1058
  41. package/src/types.ts +16 -38
  42. package/src/useVirtualizerState.ts +40 -39
  43. package/src/utils.ts +0 -52
  44. package/dist/Transaction.main.js +0 -32
  45. package/dist/Transaction.main.js.map +0 -1
  46. package/dist/Transaction.mjs +0 -27
  47. package/dist/Transaction.module.js +0 -27
  48. package/dist/Transaction.module.js.map +0 -1
  49. package/dist/tween.main.js +0 -67
  50. package/dist/tween.main.js.map +0 -1
  51. package/dist/tween.mjs +0 -61
  52. package/dist/tween.module.js +0 -61
  53. package/dist/tween.module.js.map +0 -1
  54. package/src/Transaction.ts +0 -28
  55. package/src/tween.ts +0 -83
@@ -1,11 +1,8 @@
1
- var $57c5d36217e1f286$exports = require("./tween.main.js");
2
1
  var $abed55ea619a7a17$exports = require("./utils.main.js");
3
2
  var $191a033606d4fda1$exports = require("./OverscanManager.main.js");
4
- var $f89f15d7f52bce29$exports = require("./Point.main.js");
5
3
  var $41b7691783731623$exports = require("./Rect.main.js");
6
4
  var $197a1781bd47f5b9$exports = require("./ReusableView.main.js");
7
5
  var $064492b79924894c$exports = require("./Size.main.js");
8
- var $d288159e8347722a$exports = require("./Transaction.main.js");
9
6
 
10
7
 
11
8
  function $parcel$export(e, n, v, s) {
@@ -28,92 +25,12 @@ $parcel$export(module.exports, "Virtualizer", () => $e1bc15d49d21df0e$export$89b
28
25
 
29
26
 
30
27
 
31
-
32
-
33
-
34
28
  class $e1bc15d49d21df0e$export$89be5a243e59c4b2 {
35
- _setContentSize(size) {
36
- this._contentSize = size;
37
- this.delegate.setContentSize(size);
38
- }
39
- _setContentOffset(offset) {
40
- let rect = new (0, $41b7691783731623$exports.Rect)(offset.x, offset.y, this._visibleRect.width, this._visibleRect.height);
41
- this.delegate.setVisibleRect(rect);
42
- }
43
- /**
44
- * Get the size of the scrollable content.
45
- */ get contentSize() {
46
- return this._contentSize;
47
- }
48
- /**
49
- * Get the collection view's currently visible rectangle.
50
- */ get visibleRect() {
51
- return this._visibleRect;
52
- }
53
- /**
54
- * Set the collection view's currently visible rectangle.
55
- */ set visibleRect(rect) {
56
- this._setVisibleRect(rect);
57
- }
58
- _setVisibleRect(rect, forceUpdate = false) {
59
- let current = this._visibleRect;
60
- // Ignore if the rects are equal
61
- if (rect.equals(current)) return;
62
- if (this.shouldOverscan) this._overscanManager.setVisibleRect(rect);
63
- let shouldInvalidate = this.layout && this.layout.shouldInvalidate(rect, this._visibleRect);
64
- this._resetAnimatedContentOffset();
65
- this._visibleRect = rect;
66
- if (shouldInvalidate) // We are already in a layout effect when this method is called, so relayoutNow is appropriate.
67
- this.relayoutNow({
68
- offsetChanged: !rect.pointEquals(current),
69
- sizeChanged: !rect.sizeEquals(current)
70
- });
71
- else this.updateSubviews(forceUpdate);
72
- }
73
- get collection() {
74
- return this._collection;
75
- }
76
- set collection(data) {
77
- this._setData(data);
78
- }
79
- _setData(data) {
80
- if (data === this._collection) return;
81
- if (this._collection) this._runTransaction(()=>{
82
- this._collection = data;
83
- }, this.transitionDuration > 0);
84
- else {
85
- this._collection = data;
86
- this.reloadData();
87
- }
88
- }
89
- /**
90
- * Reloads the data from the data source and relayouts the collection view.
91
- * Does not animate any changes. Equivalent to re-assigning the same data source
92
- * to the collection view.
93
- */ reloadData() {
94
- this.relayout({
95
- contentChanged: true
96
- });
97
- }
98
- /**
99
- * Returns the item with the given key.
100
- */ getItem(key) {
101
- return this._collection ? this._collection.getItem(key) : null;
102
- }
103
- /** The set of persisted keys are always present in the DOM, even if not currently in view. */ get persistedKeys() {
104
- return this._persistedKeys;
105
- }
106
- /** The set of persisted keys are always present in the DOM, even if not currently in view. */ set persistedKeys(persistedKeys) {
107
- if (!(0, $abed55ea619a7a17$exports.isSetEqual)(persistedKeys, this._persistedKeys)) {
108
- this._persistedKeys = persistedKeys;
109
- this.updateSubviews();
110
- }
111
- }
112
29
  /** Returns whether the given key, or an ancestor, is persisted. */ isPersistedKey(key) {
113
30
  // Quick check if the key is directly in the set of persisted keys.
114
- if (this._persistedKeys.has(key)) return true;
31
+ if (this.persistedKeys.has(key)) return true;
115
32
  // If not, check if the key is an ancestor of any of the persisted keys.
116
- for (let k of this._persistedKeys)while(k != null){
33
+ for (let k of this.persistedKeys)while(k != null){
117
34
  let layoutInfo = this.layout.getLayoutInfo(k);
118
35
  if (!layoutInfo) break;
119
36
  k = layoutInfo.parentKey;
@@ -121,72 +38,16 @@ class $e1bc15d49d21df0e$export$89be5a243e59c4b2 {
121
38
  }
122
39
  return false;
123
40
  }
124
- /**
125
- * Get the collection view's layout.
126
- */ get layout() {
127
- return this._layout;
128
- }
129
- /**
130
- * Set the collection view's layout.
131
- */ set layout(layout) {
132
- this.setLayout(layout);
133
- }
134
- /**
135
- * Sets the collection view's layout, optionally with an animated transition
136
- * from the current layout to the new layout.
137
- * @param layout The layout to switch to.
138
- * @param animated Whether to animate the layout change.
139
- */ setLayout(layout, animated = false) {
140
- if (layout === this._layout) return;
141
- let applyLayout = ()=>{
142
- if (this._layout) // @ts-ignore
143
- this._layout.virtualizer = null;
144
- layout.virtualizer = this;
145
- this._layout = layout;
146
- };
147
- if (animated) // Animated layout transitions are really simple, thanks to our transaction support.
148
- // We just set the layout inside a transaction action, which runs after the initial
149
- // layout infos for the animation are retrieved from the previous layout. Then, the
150
- // final layout infos are retrieved from the new layout, and animations occur.
151
- this._runTransaction(applyLayout);
152
- else {
153
- applyLayout();
154
- this.relayout();
155
- }
156
- }
157
- _getReuseType(layoutInfo, content) {
158
- if (layoutInfo.type === 'item' && content) {
159
- let type = this.delegate.getType ? this.delegate.getType(content) : 'item';
160
- let reuseType = type === 'item' ? 'item' : layoutInfo.type + '_' + type;
161
- return {
162
- type: type,
163
- reuseType: reuseType
164
- };
165
- }
166
- return {
167
- type: layoutInfo.type,
168
- reuseType: layoutInfo.type
169
- };
170
- }
171
41
  getReusableView(layoutInfo) {
172
- let content = this.getItem(layoutInfo.key);
173
- let { reuseType: reuseType } = this._getReuseType(layoutInfo, content);
174
- if (!this._reusableViews[reuseType]) this._reusableViews[reuseType] = [];
175
- let reusable = this._reusableViews[reuseType];
176
- let view = reusable.length > 0 ? reusable.pop() : new (0, $197a1781bd47f5b9$exports.ReusableView)(this);
177
- view.viewType = reuseType;
178
- if (!this._animatedContentOffset.isOrigin()) {
179
- layoutInfo = layoutInfo.copy();
180
- layoutInfo.rect.x += this._animatedContentOffset.x;
181
- layoutInfo.rect.y += this._animatedContentOffset.y;
182
- }
42
+ let parentView = layoutInfo.parentKey != null ? this._visibleViews.get(layoutInfo.parentKey) : this._rootView;
43
+ let view = parentView.getReusableView(layoutInfo.type);
183
44
  view.layoutInfo = layoutInfo;
184
45
  this._renderView(view);
185
46
  return view;
186
47
  }
187
48
  _renderView(reusableView) {
188
49
  let { type: type, key: key } = reusableView.layoutInfo;
189
- reusableView.content = this.getItem(key);
50
+ reusableView.content = this.collection.getItem(key);
190
51
  reusableView.rendered = this._renderContent(type, reusableView.content);
191
52
  }
192
53
  _renderContent(type, content) {
@@ -197,33 +58,6 @@ class $e1bc15d49d21df0e$export$89be5a243e59c4b2 {
197
58
  return rendered;
198
59
  }
199
60
  /**
200
- * Returns an array of all currently visible views, including both
201
- * item views and supplementary views.
202
- */ get visibleViews() {
203
- return Array.from(this._visibleViews.values());
204
- }
205
- /**
206
- * Gets the visible view for the given type and key. Returns null if
207
- * the view is not currently visible.
208
- *
209
- * @param key The key of the view to retrieve.
210
- */ getView(key) {
211
- return this._visibleViews.get(key) || null;
212
- }
213
- /**
214
- * Returns an array of visible views matching the given type.
215
- * @param type The view type to find.
216
- */ getViewsOfType(type) {
217
- return this.visibleViews.filter((v)=>v.layoutInfo && v.layoutInfo.type === type);
218
- }
219
- /**
220
- * Returns the key for the given view. Returns null
221
- * if the view is not currently visible.
222
- */ keyForView(view) {
223
- if (view && view.layoutInfo) return view.layoutInfo.key;
224
- return null;
225
- }
226
- /**
227
61
  * Returns the key for the item view currently at the given point.
228
62
  */ keyAtPoint(point) {
229
63
  let rect = new (0, $41b7691783731623$exports.Rect)(point.x, point.y, 1, 1);
@@ -235,581 +69,160 @@ class $e1bc15d49d21df0e$export$89be5a243e59c4b2 {
235
69
  }
236
70
  return null;
237
71
  }
238
- /**
239
- * Cleanup for when the Virtualizer will be unmounted.
240
- */ willUnmount() {
241
- cancelAnimationFrame(this._relayoutRaf);
242
- }
243
- /**
244
- * Triggers a layout invalidation, and updates the visible subviews.
245
- */ relayout(context = {}) {
246
- // Ignore relayouts while animating the scroll position
247
- if (this._scrollAnimation || typeof requestAnimationFrame === 'undefined') return;
248
- // If we already scheduled a relayout, extend the invalidation
249
- // context so we coalesce multiple relayouts in the same frame.
250
- if (this._invalidationContext) {
251
- Object.assign(this._invalidationContext, context);
252
- return;
253
- }
254
- this._invalidationContext = context;
255
- }
256
- /**
257
- * Performs a relayout immediately. Prefer {@link relayout} over this method
258
- * where possible, since it coalesces multiple layout passes in the same tick.
259
- */ relayoutNow(context = this._invalidationContext || {}) {
260
- // Cancel the scheduled relayout, since we're doing it now.
261
- if (this._relayoutRaf) {
262
- cancelAnimationFrame(this._relayoutRaf);
263
- this._relayoutRaf = null;
264
- // Update the provided context with the current invalidationContext since we are cancelling
265
- // a scheduled relayoutNow call that has this._invalidationContext set as its default context arg (relayoutNow() in relayout)
266
- context = {
267
- ...this._invalidationContext,
268
- ...context
269
- };
270
- }
271
- // Reset the invalidation context
272
- this._invalidationContext = null;
273
- // Do nothing if we don't have a layout or content, or we are
274
- // in the middle of an animated scroll transition.
275
- if (!this.layout || !this._collection || this._scrollAnimation) return;
276
- let scrollAnchor = this._getScrollAnchor();
277
- // Trigger the beforeLayout hook, if provided
278
- if (typeof context.beforeLayout === 'function') context.beforeLayout();
72
+ relayout(context = {}) {
279
73
  // Validate the layout
280
74
  this.layout.validate(context);
281
- this._setContentSize(this.layout.getContentSize());
282
- // Trigger the afterLayout hook, if provided
283
- if (typeof context.afterLayout === 'function') context.afterLayout();
284
- // Adjust scroll position based on scroll anchor, and constrain.
75
+ this.contentSize = this.layout.getContentSize();
76
+ // Constrain scroll position.
285
77
  // If the content changed, scroll to the top.
286
- let visibleRect = this.getVisibleRect();
287
- let restoredScrollAnchor = this._restoreScrollAnchor(scrollAnchor, context);
288
- let contentOffsetX = context.contentChanged ? 0 : restoredScrollAnchor.x;
289
- let contentOffsetY = context.contentChanged ? 0 : restoredScrollAnchor.y;
78
+ let visibleRect = this.visibleRect;
79
+ let contentOffsetX = context.contentChanged ? 0 : visibleRect.x;
80
+ let contentOffsetY = context.contentChanged ? 0 : visibleRect.y;
290
81
  contentOffsetX = Math.max(0, Math.min(this.contentSize.width - visibleRect.width, contentOffsetX));
291
82
  contentOffsetY = Math.max(0, Math.min(this.contentSize.height - visibleRect.height, contentOffsetY));
292
- let hasLayoutUpdates = false;
293
83
  if (contentOffsetX !== visibleRect.x || contentOffsetY !== visibleRect.y) {
294
- // If this is an animated relayout, we do not immediately scroll because it would be jittery.
295
- // Save the difference between the current and new content offsets, and apply it to the
296
- // individual content items instead. At the end of the animation, we'll reset and set the
297
- // scroll offset for real. This ensures jitter-free animation since we don't need to sync
298
- // the scroll animation and the content animation.
299
- if (context.animated || !this._animatedContentOffset.isOrigin()) {
300
- this._animatedContentOffset.x += visibleRect.x - contentOffsetX;
301
- this._animatedContentOffset.y += visibleRect.y - contentOffsetY;
302
- hasLayoutUpdates = this.updateSubviews(context.contentChanged);
303
- } else this._setContentOffset(new (0, $f89f15d7f52bce29$exports.Point)(contentOffsetX, contentOffsetY));
304
- } else hasLayoutUpdates = this.updateSubviews(context.contentChanged);
305
- // Apply layout infos, unless this is coming from an animated transaction
306
- if (!(context.transaction && context.animated)) this._applyLayoutInfos();
307
- // Wait for animations, and apply the afterAnimation hook, if provided
308
- if (context.animated && hasLayoutUpdates) {
309
- this._enableTransitions();
310
- let done = ()=>{
311
- this._disableTransitions();
312
- // Reset scroll position after animations (see above comment).
313
- if (!this._animatedContentOffset.isOrigin()) {
314
- // Get the content offset to scroll to, taking _animatedContentOffset into account.
315
- let { x: x, y: y } = this.getVisibleRect();
316
- this._resetAnimatedContentOffset();
317
- this._setContentOffset(new (0, $f89f15d7f52bce29$exports.Point)(x, y));
318
- }
319
- if (typeof context.afterAnimation === 'function') context.afterAnimation();
320
- };
321
- // Sometimes the animation takes slightly longer than expected.
322
- setTimeout(done, this.transitionDuration + 100);
323
- return;
324
- } else if (typeof context.afterAnimation === 'function') context.afterAnimation();
325
- }
326
- /**
327
- * Corrects DOM order of visible views to match item order of collection.
328
- */ _correctItemOrder() {
329
- // Defer until after scrolling and animated transactions are complete
330
- if (this._isScrolling || this._transaction) return;
331
- for (let key of this._visibleLayoutInfos.keys()){
332
- let view = this._visibleViews.get(key);
333
- this._children.delete(view);
334
- this._children.add(view);
335
- }
336
- }
337
- _enableTransitions() {
338
- this.delegate.beginAnimations();
339
- }
340
- _disableTransitions() {
341
- this.delegate.endAnimations();
342
- }
343
- _getScrollAnchor() {
344
- if (!this.anchorScrollPosition) return null;
345
- let visibleRect = this.getVisibleRect();
346
- // Ask the delegate to provide a scroll anchor, if possible
347
- if (this.delegate.getScrollAnchor) {
348
- let key = this.delegate.getScrollAnchor(visibleRect);
349
- if (key != null) {
350
- let layoutInfo = this.layout.getLayoutInfo(key);
351
- let corner = layoutInfo.rect.getCornerInRect(visibleRect);
352
- if (corner) {
353
- let key = layoutInfo.key;
354
- let offset = layoutInfo.rect[corner].y - visibleRect.y;
355
- return {
356
- key: key,
357
- layoutInfo: layoutInfo,
358
- corner: corner,
359
- offset: offset
360
- };
361
- }
362
- }
363
- }
364
- // No need to anchor the scroll position if it is at the top
365
- if (visibleRect.y === 0 && !this.anchorScrollPositionAtTop) return null;
366
- // Find a view with a visible corner that has the smallest distance to the top of the collection view
367
- let cornerAnchor = null;
368
- for (let [key, view] of this._visibleViews){
369
- let layoutInfo = view.layoutInfo;
370
- if (layoutInfo && layoutInfo.rect.area > 0) {
371
- let corner = layoutInfo.rect.getCornerInRect(visibleRect);
372
- if (corner) {
373
- let offset = layoutInfo.rect[corner].y - visibleRect.y;
374
- if (!cornerAnchor || offset < cornerAnchor.offset) cornerAnchor = {
375
- key: key,
376
- layoutInfo: layoutInfo,
377
- corner: corner,
378
- offset: offset
379
- };
380
- }
381
- }
382
- }
383
- return cornerAnchor;
384
- }
385
- _restoreScrollAnchor(scrollAnchor, context) {
386
- let contentOffset = this.getVisibleRect();
387
- if (scrollAnchor) {
388
- var _context_transaction;
389
- let finalAnchor = ((_context_transaction = context.transaction) === null || _context_transaction === void 0 ? void 0 : _context_transaction.animated) ? context.transaction.finalMap.get(scrollAnchor.key) : this.layout.getLayoutInfo(scrollAnchor.layoutInfo.key);
390
- if (finalAnchor) {
391
- let adjustment = finalAnchor.rect[scrollAnchor.corner].y - contentOffset.y - scrollAnchor.offset;
392
- contentOffset.y += adjustment;
393
- }
394
- }
395
- return contentOffset;
396
- }
397
- getVisibleRect() {
398
- let v = this.visibleRect;
399
- let x = v.x - this._animatedContentOffset.x;
400
- let y = v.y - this._animatedContentOffset.y;
401
- return new (0, $41b7691783731623$exports.Rect)(x, y, v.width, v.height);
84
+ // If the offset changed, trigger a new re-render.
85
+ let rect = new (0, $41b7691783731623$exports.Rect)(contentOffsetX, contentOffsetY, visibleRect.width, visibleRect.height);
86
+ this.delegate.setVisibleRect(rect);
87
+ } else this.updateSubviews();
402
88
  }
403
89
  getVisibleLayoutInfos() {
404
90
  let isTestEnv = false;
405
- let isClientWidthMocked = Object.getOwnPropertyNames(window.HTMLElement.prototype).includes('clientWidth');
406
- let isClientHeightMocked = Object.getOwnPropertyNames(window.HTMLElement.prototype).includes('clientHeight');
91
+ let isClientWidthMocked = isTestEnv && typeof HTMLElement !== 'undefined' && Object.getOwnPropertyNames(HTMLElement.prototype).includes('clientWidth');
92
+ let isClientHeightMocked = isTestEnv && typeof HTMLElement !== 'undefined' && Object.getOwnPropertyNames(HTMLElement.prototype).includes('clientHeight');
407
93
  let rect;
408
- if (isTestEnv && !(isClientWidthMocked && isClientHeightMocked)) rect = this._getContentRect();
409
- else rect = this.shouldOverscan ? this._overscanManager.getOverscannedRect() : this.getVisibleRect();
410
- this._visibleLayoutInfos = this._getLayoutInfoMap(rect);
411
- return this._visibleLayoutInfos;
412
- }
413
- _getLayoutInfoMap(rect, copy = false) {
94
+ if (isTestEnv && !(isClientWidthMocked && isClientHeightMocked)) rect = new (0, $41b7691783731623$exports.Rect)(0, 0, this.contentSize.width, this.contentSize.height);
95
+ else rect = this._overscanManager.getOverscannedRect();
414
96
  let layoutInfos = this.layout.getVisibleLayoutInfos(rect);
415
97
  let map = new Map;
416
- for (let layoutInfo of layoutInfos){
417
- if (copy) layoutInfo = layoutInfo.copy();
418
- map.set(layoutInfo.key, layoutInfo);
419
- }
98
+ for (let layoutInfo of layoutInfos)map.set(layoutInfo.key, layoutInfo);
420
99
  return map;
421
100
  }
422
- updateSubviews(forceUpdate = false) {
423
- if (!this._collection) return;
101
+ updateSubviews() {
424
102
  let visibleLayoutInfos = this.getVisibleLayoutInfos();
425
- let currentlyVisible = this._visibleViews;
426
- let toAdd, toRemove, toUpdate;
427
- // If this is a force update, remove and re-add all views.
428
- // Otherwise, find and update the diff.
429
- if (forceUpdate) {
430
- toAdd = visibleLayoutInfos;
431
- toRemove = currentlyVisible;
432
- toUpdate = new Set();
433
- } else {
434
- ({ toAdd: toAdd, toRemove: toRemove, toUpdate: toUpdate } = (0, $abed55ea619a7a17$exports.difference)(currentlyVisible, visibleLayoutInfos));
435
- for (let key of toUpdate){
436
- let view = currentlyVisible.get(key);
437
- if (!view || !view.layoutInfo) continue;
438
- let item = this.getItem(visibleLayoutInfos.get(key).key);
439
- if (view.content === item) toUpdate.delete(key);
440
- else {
441
- // If the view type changes, delete and recreate the view instead of updating
442
- let { reuseType: reuseType } = this._getReuseType(view.layoutInfo, item);
443
- if (view.viewType !== reuseType) {
444
- toUpdate.delete(key);
445
- toAdd.add(key);
446
- toRemove.add(key);
447
- }
448
- }
449
- }
450
- // We are done if the sets are equal
451
- if (toAdd.size === 0 && toRemove.size === 0 && toUpdate.size === 0) {
452
- if (this._transaction) this._applyLayoutInfos();
453
- return;
454
- }
455
- }
456
- // Track views that should be removed. They are not removed from
457
- // the DOM immediately, since we may reuse and need to re-insert
458
- // them back into the DOM anyway.
459
103
  let removed = new Set();
460
- for (let key of toRemove.keys()){
461
- let view = this._visibleViews.get(key);
462
- if (view) {
463
- removed.add(view);
464
- this._visibleViews.delete(key);
465
- // If we are in the middle of a transaction, wait until the end
466
- // of the animations to remove the views from the DOM. Also means
467
- // we can't reuse those views immediately.
468
- if (this._transaction) this._transaction.toRemove.set(key, view);
469
- else this.reuseView(view);
470
- }
104
+ for (let [key, view] of this._visibleViews)if (!visibleLayoutInfos.has(key)) {
105
+ this._visibleViews.delete(key);
106
+ view.parent.reuseChild(view);
107
+ removed.add(view); // Defer removing in case we reuse this view.
471
108
  }
472
- for (let key of toAdd.keys()){
473
- let layoutInfo = visibleLayoutInfos.get(key);
474
- let view;
475
- // If we're in a transaction, and a layout change happens
476
- // during the animations such that a view that was going
477
- // to be removed is now not, we don't create a new view
478
- // since the old one is still in the DOM, marked as toRemove.
479
- if (this._transaction) {
480
- // if transaction, get initial layout attributes for the animation
481
- if (this._transaction.initialLayoutInfo.has(key)) layoutInfo = this._transaction.initialLayoutInfo.get(key);
482
- view = this._transaction.toRemove.get(key);
483
- if (view) {
484
- this._transaction.toRemove.delete(key);
485
- this._applyLayoutInfo(view, layoutInfo);
486
- }
487
- }
109
+ for (let [key, layoutInfo] of visibleLayoutInfos){
110
+ let view = this._visibleViews.get(key);
488
111
  if (!view) {
489
- // Create or reuse a view for this row
490
112
  view = this.getReusableView(layoutInfo);
491
- // Add the view to the DOM if needed
492
- if (!removed.has(view)) this._children.add(view);
113
+ view.parent.children.add(view);
114
+ this._visibleViews.set(key, view);
115
+ removed.delete(view);
116
+ } else {
117
+ view.layoutInfo = layoutInfo;
118
+ let item = this.collection.getItem(layoutInfo.key);
119
+ if (view.content !== item) {
120
+ this._renderedContent.delete(view.content);
121
+ this._renderView(view);
122
+ }
493
123
  }
494
- this._visibleViews.set(key, view);
495
- removed.delete(view);
496
- }
497
- for (let key of toUpdate){
498
- let view = currentlyVisible.get(key);
499
- this._renderedContent.delete(key);
500
- this._renderView(view);
501
124
  }
502
- // Remove the remaining rows to delete from the DOM
503
- if (!this._transaction) this.removeViews(removed);
504
- this._correctItemOrder();
505
- this._flushVisibleViews();
506
- let hasLayoutUpdates = this._transaction && (toAdd.size > 0 || toRemove.size > 0 || this._hasLayoutUpdates());
507
- if (hasLayoutUpdates) requestAnimationFrame(()=>{
508
- // If we're in a transaction, apply animations to visible views
509
- // and "to be removed" views, which animate off screen.
510
- if (this._transaction) requestAnimationFrame(()=>this._applyLayoutInfos());
125
+ // The remaining views in `removed` were not reused to render new items.
126
+ // They should be removed from the DOM. We also clear the reusable view queue
127
+ // here since there's no point holding onto views that have been removed.
128
+ // Doing so hurts performance in the future when reusing elements due to FIFO order.
129
+ for (let view of removed){
130
+ view.parent.children.delete(view);
131
+ view.parent.reusableViews.clear();
132
+ }
133
+ // Reordering DOM nodes is costly, so we defer this until scrolling stops.
134
+ // DOM order does not affect visual order (due to absolute positioning),
135
+ // but does matter for assistive technology users.
136
+ if (!this._isScrolling) // Layout infos must be in topological order (parents before children).
137
+ for (let key of visibleLayoutInfos.keys()){
138
+ let view = this._visibleViews.get(key);
139
+ view.parent.children.delete(view);
140
+ view.parent.children.add(view);
141
+ }
142
+ }
143
+ /** Performs layout and updates visible views as needed. */ render(opts) {
144
+ let mutableThis = this;
145
+ let needsLayout = false;
146
+ let offsetChanged = false;
147
+ let sizeChanged = false;
148
+ let itemSizeChanged = false;
149
+ let needsUpdate = false;
150
+ if (opts.collection !== this.collection) {
151
+ mutableThis.collection = opts.collection;
152
+ needsLayout = true;
153
+ }
154
+ if (opts.layout !== this.layout) {
155
+ if (this.layout) this.layout.virtualizer = null;
156
+ opts.layout.virtualizer = this;
157
+ mutableThis.layout = opts.layout;
158
+ needsLayout = true;
159
+ }
160
+ if (opts.persistedKeys && !(0, $abed55ea619a7a17$exports.isSetEqual)(opts.persistedKeys, this.persistedKeys)) {
161
+ mutableThis.persistedKeys = opts.persistedKeys;
162
+ needsUpdate = true;
163
+ }
164
+ if (!this.visibleRect.equals(opts.visibleRect)) {
165
+ this._overscanManager.setVisibleRect(opts.visibleRect);
166
+ let shouldInvalidate = this.layout.shouldInvalidate(opts.visibleRect, this.visibleRect);
167
+ if (shouldInvalidate) {
168
+ offsetChanged = !opts.visibleRect.pointEquals(this.visibleRect);
169
+ sizeChanged = !opts.visibleRect.sizeEquals(this.visibleRect);
170
+ needsLayout = true;
171
+ } else needsUpdate = true;
172
+ mutableThis.visibleRect = opts.visibleRect;
173
+ }
174
+ if (opts.invalidationContext !== this._invalidationContext) {
175
+ if (opts.invalidationContext) {
176
+ sizeChanged || (sizeChanged = opts.invalidationContext.sizeChanged || false);
177
+ offsetChanged || (offsetChanged = opts.invalidationContext.offsetChanged || false);
178
+ itemSizeChanged || (itemSizeChanged = opts.invalidationContext.itemSizeChanged || false);
179
+ needsLayout || (needsLayout = itemSizeChanged || sizeChanged || offsetChanged);
180
+ needsLayout || (needsLayout = opts.invalidationContext.layoutOptions !== this._invalidationContext.layoutOptions);
181
+ }
182
+ this._invalidationContext = opts.invalidationContext;
183
+ }
184
+ if (opts.isScrolling !== this._isScrolling) {
185
+ this._isScrolling = opts.isScrolling;
186
+ if (!opts.isScrolling) // Update to fix the DOM order after scrolling.
187
+ needsUpdate = true;
188
+ }
189
+ if (needsLayout) this.relayout({
190
+ offsetChanged: offsetChanged,
191
+ sizeChanged: sizeChanged,
192
+ itemSizeChanged: itemSizeChanged,
193
+ layoutOptions: this._invalidationContext.layoutOptions
511
194
  });
512
- return hasLayoutUpdates;
513
- }
514
- afterRender() {
515
- if (this._transactionQueue.length > 0) this._processTransactionQueue();
516
- else if (this._invalidationContext) this.relayoutNow();
517
- if (this.shouldOverscan) this._overscanManager.collectMetrics();
518
- }
519
- _flushVisibleViews() {
520
- // CollectionVirtualizer deals with a flattened set of LayoutInfos, but they can represent hierarchy
521
- // by referencing a parentKey. Just before rendering the visible views, we rebuild this hierarchy
522
- // by creating a mapping of views by parent key and recursively calling the delegate's renderWrapper
523
- // method to build the final tree.
524
- this._viewsByParentKey = new Map([
525
- [
526
- null,
527
- []
528
- ]
529
- ]);
530
- for (let view of this._children){
531
- var _view_layoutInfo, _this__viewsByParentKey_get, _view_layoutInfo1, _view_layoutInfo2, _view_layoutInfo3;
532
- if (((_view_layoutInfo = view.layoutInfo) === null || _view_layoutInfo === void 0 ? void 0 : _view_layoutInfo.parentKey) != null && !this._viewsByParentKey.has(view.layoutInfo.parentKey)) this._viewsByParentKey.set(view.layoutInfo.parentKey, []);
533
- (_this__viewsByParentKey_get = this._viewsByParentKey.get((_view_layoutInfo1 = view.layoutInfo) === null || _view_layoutInfo1 === void 0 ? void 0 : _view_layoutInfo1.parentKey)) === null || _this__viewsByParentKey_get === void 0 ? void 0 : _this__viewsByParentKey_get.push(view);
534
- if (!this._viewsByParentKey.has((_view_layoutInfo2 = view.layoutInfo) === null || _view_layoutInfo2 === void 0 ? void 0 : _view_layoutInfo2.key)) this._viewsByParentKey.set((_view_layoutInfo3 = view.layoutInfo) === null || _view_layoutInfo3 === void 0 ? void 0 : _view_layoutInfo3.key, []);
535
- }
536
- let children = this.getChildren(null);
537
- this.delegate.setVisibleViews(children);
195
+ else if (needsUpdate) this.updateSubviews();
196
+ return this.getChildren(null);
538
197
  }
539
198
  getChildren(key) {
540
- let buildTree = (parent, views)=>views.map((view)=>{
541
- let children = this._viewsByParentKey.get(view.layoutInfo.key);
542
- return this.delegate.renderWrapper(parent, view, children, (childViews)=>buildTree(view, childViews));
199
+ let parent = key == null ? this._rootView : this._visibleViews.get(key);
200
+ let renderChildren = (parent, views)=>views.map((view)=>{
201
+ return this.delegate.renderWrapper(parent, view, view.children ? Array.from(view.children) : [], (childViews)=>renderChildren(view, childViews));
543
202
  });
544
- let parent = this._visibleViews.get(key);
545
- return buildTree(parent, this._viewsByParentKey.get(key));
546
- }
547
- _applyLayoutInfo(view, layoutInfo) {
548
- if (view.layoutInfo === layoutInfo) return false;
549
- view.layoutInfo = layoutInfo;
550
- return true;
551
- }
552
- _applyLayoutInfos() {
553
- let updated = false;
554
- // Apply layout infos to visible views
555
- for (let view of this._visibleViews.values()){
556
- let cur = view.layoutInfo;
557
- if ((cur === null || cur === void 0 ? void 0 : cur.key) != null) {
558
- let layoutInfo = this.layout.getLayoutInfo(cur.key);
559
- if (this._applyLayoutInfo(view, layoutInfo)) updated = true;
560
- }
561
- }
562
- // Apply final layout infos for views that will be removed
563
- if (this._transaction) {
564
- for (let view of this._transaction.toRemove.values()){
565
- let cur = view.layoutInfo;
566
- if ((cur === null || cur === void 0 ? void 0 : cur.key) != null) {
567
- let layoutInfo = this.layout.getLayoutInfo(cur.key);
568
- if (this._applyLayoutInfo(view, layoutInfo)) updated = true;
569
- }
570
- }
571
- for (let view of this._transaction.removed.values()){
572
- let cur = view.layoutInfo;
573
- let layoutInfo = this._transaction.finalLayoutInfo.get(cur.key) || cur;
574
- layoutInfo = this.layout.getFinalLayoutInfo(layoutInfo.copy());
575
- if (this._applyLayoutInfo(view, layoutInfo)) updated = true;
576
- }
577
- }
578
- if (updated) this._flushVisibleViews();
579
- }
580
- _hasLayoutUpdates() {
581
- if (!this._transaction) return false;
582
- for (let view of this._visibleViews.values()){
583
- let cur = view.layoutInfo;
584
- if (!cur) return true;
585
- let layoutInfo = this.layout.getLayoutInfo(cur.key);
586
- if (// Uses equals rather than pointEquals so that width/height changes are taken into account
587
- !cur.rect.equals(layoutInfo.rect) || cur.opacity !== layoutInfo.opacity || cur.transform !== layoutInfo.transform) return true;
588
- }
589
- return false;
203
+ return renderChildren(parent, Array.from(parent.children));
590
204
  }
591
- reuseView(view) {
592
- view.prepareForReuse();
593
- this._reusableViews[view.viewType].push(view);
594
- }
595
- removeViews(toRemove) {
596
- for (let view of toRemove)this._children.delete(view);
205
+ invalidate(context) {
206
+ this.delegate.invalidate(context);
597
207
  }
598
208
  updateItemSize(key, size) {
599
- // TODO: we should be able to invalidate a single index path
600
- // @ts-ignore
601
209
  if (!this.layout.updateItemSize) return;
602
- // If the scroll position is currently animating, add the update
603
- // to a queue to be processed after the animation is complete.
604
- if (this._scrollAnimation) {
605
- this._sizeUpdateQueue.set(key, size);
606
- return;
607
- }
608
- // @ts-ignore
609
210
  let changed = this.layout.updateItemSize(key, size);
610
- if (changed) this.relayout();
611
- }
612
- startScrolling() {
613
- this._isScrolling = true;
614
- }
615
- endScrolling() {
616
- this._isScrolling = false;
617
- this._correctItemOrder();
618
- this._flushVisibleViews();
619
- }
620
- _resetAnimatedContentOffset() {
621
- // Reset the animated content offset of subviews. See comment in relayoutNow for details.
622
- if (!this._animatedContentOffset.isOrigin()) {
623
- this._animatedContentOffset = new (0, $f89f15d7f52bce29$exports.Point)(0, 0);
624
- this._applyLayoutInfos();
625
- }
626
- }
627
- /**
628
- * Scrolls the item with the given key into view, optionally with an animation.
629
- * @param key The key of the item to scroll into view.
630
- * @param duration The duration of the scroll animation.
631
- */ scrollToItem(key, options) {
632
- // key can be 0, so check if null or undefined
633
- if (key == null) return;
634
- let layoutInfo = this.layout.getLayoutInfo(key);
635
- if (!layoutInfo) return;
636
- let { duration: duration = 300, shouldScrollX: shouldScrollX = true, shouldScrollY: shouldScrollY = true, offsetX: offsetX = 0, offsetY: offsetY = 0 } = options;
637
- let x = this.visibleRect.x;
638
- let y = this.visibleRect.y;
639
- let minX = layoutInfo.rect.x - offsetX;
640
- let minY = layoutInfo.rect.y - offsetY;
641
- let maxX = x + this.visibleRect.width;
642
- let maxY = y + this.visibleRect.height;
643
- if (shouldScrollX) {
644
- if (minX <= x || maxX === 0) x = minX;
645
- else if (layoutInfo.rect.maxX > maxX) x += layoutInfo.rect.maxX - maxX;
646
- }
647
- if (shouldScrollY) {
648
- if (minY <= y || maxY === 0) y = minY;
649
- else if (layoutInfo.rect.maxY > maxY) y += layoutInfo.rect.maxY - maxY;
650
- }
651
- return this.scrollTo(new (0, $f89f15d7f52bce29$exports.Point)(x, y), duration);
652
- }
653
- /**
654
- * Performs an animated scroll to the given offset.
655
- * @param offset - The offset to scroll to.
656
- * @param duration The duration of the animation.
657
- * @returns A promise that resolves when the animation is complete.
658
- */ scrollTo(offset, duration = 300) {
659
- // Cancel the current scroll animation
660
- if (this._scrollAnimation) {
661
- this._scrollAnimation.cancel();
662
- this._scrollAnimation = null;
663
- }
664
- // Set the content offset synchronously if the duration is zero
665
- if (duration <= 0 || this.visibleRect.pointEquals(offset)) {
666
- this._setContentOffset(offset);
667
- return Promise.resolve();
668
- }
669
- this.startScrolling();
670
- this._scrollAnimation = (0, $57c5d36217e1f286$exports.tween)(this.visibleRect, offset, duration, (0, $57c5d36217e1f286$exports.easeOut), (offset)=>{
671
- this._setContentOffset(offset);
211
+ if (changed) this.invalidate({
212
+ itemSizeChanged: true
672
213
  });
673
- this._scrollAnimation.then(()=>{
674
- this._scrollAnimation = null;
675
- // Process view size updates that occurred during the animation.
676
- // Only views that are still visible will be actually updated.
677
- for (let [key, size] of this._sizeUpdateQueue)this.updateItemSize(key, size);
678
- this._sizeUpdateQueue.clear();
679
- this.relayout();
680
- this._processTransactionQueue();
681
- this.endScrolling();
682
- });
683
- return this._scrollAnimation;
684
- }
685
- _runTransaction(action, animated) {
686
- this._startTransaction();
687
- if (this._nextTransaction) this._nextTransaction.actions.push(action);
688
- this._endTransaction(animated);
689
214
  }
690
- _startTransaction() {
691
- if (!this._nextTransaction) this._nextTransaction = new (0, $d288159e8347722a$exports.Transaction);
692
- this._nextTransaction.level++;
693
- }
694
- _endTransaction(animated) {
695
- if (!this._nextTransaction) return false;
696
- // Save whether the transaction should be animated.
697
- if (animated != null) this._nextTransaction.animated = animated;
698
- // If we haven't reached level 0, we are still in a
699
- // nested transaction. Wait for the parent to end.
700
- if (--this._nextTransaction.level > 0) return false;
701
- // Do nothing for empty transactions
702
- if (this._nextTransaction.actions.length === 0) {
703
- this._nextTransaction = null;
704
- return false;
705
- }
706
- // Default animations to true
707
- if (this._nextTransaction.animated == null) this._nextTransaction.animated = true;
708
- // Enqueue the transaction
709
- this._transactionQueue.push(this._nextTransaction);
710
- this._nextTransaction = null;
711
- return true;
712
- }
713
- _processTransactionQueue() {
714
- // If the current transaction is animating, wait until the end
715
- // to process the next transaction.
716
- if (this._transaction || this._scrollAnimation) return;
717
- let next = this._transactionQueue.shift();
718
- if (next) this._performTransaction(next);
719
- }
720
- _getContentRect() {
721
- return new (0, $41b7691783731623$exports.Rect)(0, 0, this.contentSize.width, this.contentSize.height);
722
- }
723
- _performTransaction(transaction) {
724
- this._transaction = transaction;
725
- this.relayoutNow({
726
- transaction: transaction,
727
- animated: transaction.animated,
728
- beforeLayout: ()=>{
729
- // Get the initial layout infos for all views before the updates
730
- // so we can figure out which views to add and remove.
731
- if (transaction.animated) transaction.initialMap = this._getLayoutInfoMap(this._getContentRect(), true);
732
- // Apply the actions that occurred during this transaction
733
- for (let action of transaction.actions)action();
734
- },
735
- afterLayout: ()=>{
736
- // Get the final layout infos after the updates
737
- if (transaction.animated) {
738
- transaction.finalMap = this._getLayoutInfoMap(this._getContentRect());
739
- this._setupTransactionAnimations(transaction);
740
- } else this._transaction = null;
741
- },
742
- afterAnimation: ()=>{
743
- // Remove and reuse views when animations are done
744
- if (transaction.toRemove.size > 0 || transaction.removed.size > 0) for (let view of (0, $abed55ea619a7a17$exports.concatIterators)(transaction.toRemove.values(), transaction.removed.values())){
745
- this._children.delete(view);
746
- this.reuseView(view);
747
- }
748
- this._transaction = null;
749
- // Ensure DOM order is correct for accessibility after animations are complete
750
- this._correctItemOrder();
751
- this._flushVisibleViews();
752
- this._processTransactionQueue();
753
- }
754
- });
755
- }
756
- _setupTransactionAnimations(transaction) {
757
- let { initialMap: initialMap, finalMap: finalMap } = transaction;
758
- // Store initial and final layout infos for animations
759
- for (let [key, layoutInfo] of initialMap)if (finalMap.has(key)) // Store the initial layout info for use during animations.
760
- transaction.initialLayoutInfo.set(key, layoutInfo);
761
- else // This view was removed. Store the layout info for use
762
- // in Layout#getFinalLayoutInfo during animations.
763
- transaction.finalLayoutInfo.set(layoutInfo.key, layoutInfo);
764
- // Get initial layout infos for views that were added
765
- for (let [key, layoutInfo] of finalMap)if (!initialMap.has(key)) {
766
- let initialLayoutInfo = this.layout.getInitialLayoutInfo(layoutInfo.copy());
767
- transaction.initialLayoutInfo.set(key, initialLayoutInfo);
768
- }
769
- // Figure out which views were removed.
770
- for (let [key, view] of this._visibleViews)// If an item has a width of 0, there is no need to remove it from the _visibleViews.
771
- // Removing an item with width of 0 can cause a loop where the item gets added, removed,
772
- // added, removed... etc in a loop.
773
- if (!finalMap.has(key) && view.layoutInfo.rect.width > 0) {
774
- transaction.removed.set(key, view);
775
- this._visibleViews.delete(key);
776
- // In case something weird happened, where we have a view but no
777
- // initial layout info, use the one attached to the view.
778
- if (view.layoutInfo) {
779
- if (!transaction.finalLayoutInfo.has(view.layoutInfo.key)) transaction.finalLayoutInfo.set(view.layoutInfo.key, view.layoutInfo);
780
- }
781
- }
782
- }
783
- constructor(options = {}){
784
- this._contentSize = new (0, $064492b79924894c$exports.Size);
785
- this._visibleRect = new (0, $41b7691783731623$exports.Rect);
786
- this._reusableViews = {};
787
- this._visibleLayoutInfos = new Map();
215
+ constructor(delegate){
216
+ this.delegate = delegate;
217
+ this.contentSize = new (0, $064492b79924894c$exports.Size);
218
+ this.visibleRect = new (0, $41b7691783731623$exports.Rect);
219
+ this.persistedKeys = new Set();
788
220
  this._visibleViews = new Map();
789
221
  this._renderedContent = new WeakMap();
790
- this._children = new Set();
222
+ this._rootView = new (0, $197a1781bd47f5b9$exports.ReusableView)(this);
223
+ this._isScrolling = false;
791
224
  this._invalidationContext = null;
792
225
  this._overscanManager = new (0, $191a033606d4fda1$exports.OverscanManager)();
793
- this._persistedKeys = new Set();
794
- this._scrollAnimation = null;
795
- this._isScrolling = false;
796
- this._sizeUpdateQueue = new Map();
797
- this._animatedContentOffset = new (0, $f89f15d7f52bce29$exports.Point)(0, 0);
798
- this._transaction = null;
799
- this._nextTransaction = null;
800
- this._transactionQueue = [];
801
- var _options_transitionDuration;
802
- // Set options from passed object if given
803
- this.transitionDuration = (_options_transitionDuration = options.transitionDuration) !== null && _options_transitionDuration !== void 0 ? _options_transitionDuration : 500;
804
- this.anchorScrollPosition = options.anchorScrollPosition || false;
805
- this.anchorScrollPositionAtTop = options.anchorScrollPositionAtTop || false;
806
- this.shouldOverscan = options.shouldOverscan !== false;
807
- for (let key of [
808
- 'delegate',
809
- 'size',
810
- 'layout',
811
- 'collection'
812
- ])if (options[key]) this[key] = options[key];
813
226
  }
814
227
  }
815
228