@rdlabo/ngx-cdk-scroll-strategies 21.0.0 → 21.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/eslint.config.js +7 -0
- package/ng-package.json +7 -0
- package/package.json +3 -14
- package/src/lib/dynamic-size-virtual-scroll-strategy.spec.ts +385 -0
- package/src/lib/dynamic-size-virtual-scroll-strategy.ts +366 -0
- package/src/lib/dynamic-size-virtual-scroll.service.spec.ts +19 -0
- package/src/lib/dynamic-size-virtual-scroll.service.ts +124 -0
- package/src/public-api.ts +6 -0
- package/tsconfig.lib.json +14 -0
- package/tsconfig.lib.prod.json +10 -0
- package/tsconfig.spec.json +14 -0
- package/fesm2022/rdlabo-ngx-cdk-scroll-strategies.mjs +0 -426
- package/fesm2022/rdlabo-ngx-cdk-scroll-strategies.mjs.map +0 -1
- package/types/rdlabo-ngx-cdk-scroll-strategies.d.ts +0 -143
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* https://github.com/angular/components/blob/main/src/cdk/scrolling/fixed-size-virtual-scroll.ts
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { BooleanInput, coerceArray, coerceBooleanProperty, coerceNumberProperty, NumberInput } from '@angular/cdk/coercion';
|
|
7
|
+
import { Directive, effect, ElementRef, forwardRef, inject, input } from '@angular/core';
|
|
8
|
+
import { Observable, Subject } from 'rxjs';
|
|
9
|
+
import { distinctUntilChanged } from 'rxjs/operators';
|
|
10
|
+
import { CdkVirtualScrollViewport, VIRTUAL_SCROLL_STRATEGY, VirtualScrollStrategy } from '@angular/cdk/scrolling';
|
|
11
|
+
|
|
12
|
+
/** Virtual scrolling strategy for lists with items of known fixed size. */
|
|
13
|
+
export class DynamicSizeVirtualScrollStrategy implements VirtualScrollStrategy {
|
|
14
|
+
private readonly _scrolledIndexChange = new Subject<number>();
|
|
15
|
+
|
|
16
|
+
/** @docs-private Implemented as part of VirtualScrollStrategy. */
|
|
17
|
+
scrolledIndexChange: Observable<number> = this._scrolledIndexChange.pipe(distinctUntilChanged());
|
|
18
|
+
|
|
19
|
+
/** The attached viewport. */
|
|
20
|
+
private _viewport: CdkVirtualScrollViewport | null = null;
|
|
21
|
+
|
|
22
|
+
/** The size of the items in the virtually scrolling list. */
|
|
23
|
+
private _itemDynamicSize: itemDynamicSize[];
|
|
24
|
+
|
|
25
|
+
/** The minimum amount of buffer rendered beyond the viewport (in pixels). */
|
|
26
|
+
private _minBufferPx: number;
|
|
27
|
+
|
|
28
|
+
/** The number of buffer items to render beyond the edge of the viewport (in pixels). */
|
|
29
|
+
private _maxBufferPx: number;
|
|
30
|
+
|
|
31
|
+
/** This is added for reverse virtual scroll **/
|
|
32
|
+
private _isReverse: boolean;
|
|
33
|
+
measureScrollOffset = 0;
|
|
34
|
+
|
|
35
|
+
/** This is added for change dataLength **/
|
|
36
|
+
private _latestDataLength = 0;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param itemSize The size of the items in the virtually scrolling list.
|
|
40
|
+
* @param minBufferPx The minimum amount of buffer (in pixels) before needing to render more
|
|
41
|
+
* @param maxBufferPx The amount of buffer (in pixels) to render when rendering more.
|
|
42
|
+
* @param isReverse Added from rdlabo for reverse
|
|
43
|
+
*/
|
|
44
|
+
constructor(itemSize: itemDynamicSize[], minBufferPx: number, maxBufferPx: number, isReverse: boolean) {
|
|
45
|
+
this._itemDynamicSize = itemSize;
|
|
46
|
+
this._minBufferPx = minBufferPx;
|
|
47
|
+
this._maxBufferPx = maxBufferPx;
|
|
48
|
+
this._isReverse = isReverse;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Attaches this scroll strategy to a viewport.
|
|
53
|
+
* @param viewport The viewport to attach this strategy to.
|
|
54
|
+
*/
|
|
55
|
+
attach(viewport: CdkVirtualScrollViewport) {
|
|
56
|
+
this._viewport = viewport;
|
|
57
|
+
this._updateTotalContentSize();
|
|
58
|
+
this._updateRenderedRange();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Detaches this scroll strategy from the currently attached viewport. */
|
|
62
|
+
detach() {
|
|
63
|
+
this._scrolledIndexChange.complete();
|
|
64
|
+
this._viewport = null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Update the item size and buffer size.
|
|
69
|
+
* @param itemDynamicSize The size of the items in the virtually scrolling list.
|
|
70
|
+
* @param minBufferPx The minimum amount of buffer (in pixels) before needing to render more
|
|
71
|
+
* @param maxBufferPx The amount of buffer (in pixels) to render when rendering more.
|
|
72
|
+
* @param isReverse
|
|
73
|
+
*/
|
|
74
|
+
updateItemAndBufferSize(itemDynamicSize: itemDynamicSize[], minBufferPx: number, maxBufferPx: number, isReverse: boolean) {
|
|
75
|
+
if (maxBufferPx < minBufferPx) {
|
|
76
|
+
throw Error('CDK virtual scroll: maxBufferPx must be greater than or equal to minBufferPx');
|
|
77
|
+
}
|
|
78
|
+
this._itemDynamicSize = itemDynamicSize;
|
|
79
|
+
this._minBufferPx = minBufferPx;
|
|
80
|
+
this._maxBufferPx = maxBufferPx;
|
|
81
|
+
this._isReverse = isReverse;
|
|
82
|
+
this._updateTotalContentSize();
|
|
83
|
+
this._updateRenderedRange();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** @docs-private Implemented as part of VirtualScrollStrategy. */
|
|
87
|
+
onContentScrolled() {
|
|
88
|
+
this._updateRenderedRange();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** @docs-private Implemented as part of VirtualScrollStrategy. */
|
|
92
|
+
onDataLengthChanged() {
|
|
93
|
+
this._updateTotalContentSize();
|
|
94
|
+
this._updateRenderedRange();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** @docs-private Implemented as part of VirtualScrollStrategy. */
|
|
98
|
+
onContentRendered() {
|
|
99
|
+
/* no-op */
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** @docs-private Implemented as part of VirtualScrollStrategy. */
|
|
103
|
+
onRenderedOffsetChanged() {
|
|
104
|
+
/* no-op */
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Scroll to the offset for the given index.
|
|
109
|
+
* @param index The index of the element to scroll to.
|
|
110
|
+
* @param behavior The ScrollBehavior to use when scrolling.
|
|
111
|
+
*/
|
|
112
|
+
scrollToIndex(index: number, behavior: ScrollBehavior): void {
|
|
113
|
+
if (this._viewport) {
|
|
114
|
+
const size = sumItemSize(this._itemDynamicSize, index);
|
|
115
|
+
this._viewport.scrollToOffset(size, behavior);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Update the viewport's total content size. */
|
|
120
|
+
private _updateTotalContentSize() {
|
|
121
|
+
if (!this._viewport) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const size = sumItemSize(this._itemDynamicSize, this._viewport.getDataLength());
|
|
126
|
+
this._viewport.setTotalContentSize(size);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Update the viewport's rendered range. */
|
|
130
|
+
private _updateRenderedRange() {
|
|
131
|
+
if (!this._viewport) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (this._viewport.getViewportSize() === 0) {
|
|
135
|
+
// DOM is not ready yet. We should check again once the viewport is visible.
|
|
136
|
+
requestAnimationFrame(() => this._viewport?.checkViewportSize());
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const renderedRange = this._viewport.getRenderedRange();
|
|
141
|
+
const newRange = { start: renderedRange.start, end: renderedRange.end };
|
|
142
|
+
const viewportSize = this._viewport.getViewportSize();
|
|
143
|
+
const dataLength = this._viewport.getDataLength();
|
|
144
|
+
|
|
145
|
+
// Reverse offset if _isReverse
|
|
146
|
+
let scrollOffset = !this._isReverse
|
|
147
|
+
? this._viewport.measureScrollOffset()
|
|
148
|
+
: Math.max(0, this._viewport.getElementRef().nativeElement.scrollTop * -1);
|
|
149
|
+
|
|
150
|
+
// let firstVisibleIndex = this._itemDynamicSize > 0 ? scrollOffset / this._itemDynamicSize : 0;
|
|
151
|
+
let firstVisibleIndex = this._itemDynamicSize.length > 0 ? Math.floor(calcIndex(this._itemDynamicSize, scrollOffset)) : 0;
|
|
152
|
+
|
|
153
|
+
// If user scrolls to the bottom of the list and data changes to a smaller list
|
|
154
|
+
if (newRange.end > dataLength) {
|
|
155
|
+
// We have to recalculate the first visible index based on new data length and viewport size.
|
|
156
|
+
const maxVisibleItems = Math.ceil(calcIndex(this._itemDynamicSize, viewportSize));
|
|
157
|
+
const newVisibleIndex = Math.max(0, Math.min(firstVisibleIndex, dataLength - maxVisibleItems));
|
|
158
|
+
|
|
159
|
+
// If first visible index changed we must update scroll offset to handle start/end buffers
|
|
160
|
+
// Current range must also be adjusted to cover the new position (bottom of new list).
|
|
161
|
+
if (firstVisibleIndex != newVisibleIndex) {
|
|
162
|
+
firstVisibleIndex = newVisibleIndex;
|
|
163
|
+
// scrollOffset = newVisibleIndex * this._itemDynamicSize;
|
|
164
|
+
scrollOffset = sumItemSize(this._itemDynamicSize, newVisibleIndex);
|
|
165
|
+
newRange.start = Math.floor(firstVisibleIndex);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
newRange.end = Math.max(0, Math.min(dataLength, newRange.start + maxVisibleItems));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// const startBuffer = scrollOffset - newRange.start * this._itemDynamicSize;
|
|
172
|
+
const startBuffer = scrollOffset - sumItemSize(this._itemDynamicSize, newRange.start);
|
|
173
|
+
if (startBuffer < this._minBufferPx && newRange.start != 0) {
|
|
174
|
+
// const expandStart = Math.ceil((this._maxBufferPx - startBuffer) / this._itemDynamicSize);
|
|
175
|
+
const expandStart = Math.ceil(calcIndex(this._itemDynamicSize, this._maxBufferPx - startBuffer, newRange.start, true));
|
|
176
|
+
newRange.start = Math.max(0, newRange.start - expandStart);
|
|
177
|
+
// newRange.end = Math.min(dataLength, Math.ceil(firstVisibleIndex + (viewportSize + this._minBufferPx) / this._itemDynamicSize));
|
|
178
|
+
newRange.end = Math.min(
|
|
179
|
+
dataLength,
|
|
180
|
+
Math.ceil(firstVisibleIndex + calcIndex(this._itemDynamicSize, viewportSize + this._minBufferPx, firstVisibleIndex)) + 1, // firstVisibleIndexを削った影響
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
// console.log(
|
|
184
|
+
// 'expandStart',
|
|
185
|
+
// firstVisibleIndex,
|
|
186
|
+
// '"' + newRange.start + '-' + newRange.end + '"',
|
|
187
|
+
// dataLength,
|
|
188
|
+
// viewportSize + this._minBufferPx,
|
|
189
|
+
// Math.ceil(firstVisibleIndex + calcIndex(this._itemDynamicSize, viewportSize + this._minBufferPx, firstVisibleIndex)),
|
|
190
|
+
// );
|
|
191
|
+
} else {
|
|
192
|
+
// const endBuffer = newRange.end * this._itemSize - (scrollOffset + viewportSize);
|
|
193
|
+
const endBuffer = Math.max(sumItemSize(this._itemDynamicSize, newRange.end) - (scrollOffset + viewportSize), 0);
|
|
194
|
+
if (endBuffer < this._minBufferPx && newRange.end != dataLength) {
|
|
195
|
+
// const expandEnd = Math.ceil((this._maxBufferPx - endBuffer) / this._itemDynamicSize);
|
|
196
|
+
const expandEnd = Math.ceil(calcIndex(this._itemDynamicSize, this._maxBufferPx - endBuffer, newRange.end));
|
|
197
|
+
|
|
198
|
+
if (expandEnd > 0) {
|
|
199
|
+
newRange.end = Math.min(dataLength, newRange.end + expandEnd);
|
|
200
|
+
// Math.floor(firstVisibleIndex - this._minBufferPx / this._itemSize),
|
|
201
|
+
newRange.start = Math.max(
|
|
202
|
+
0,
|
|
203
|
+
Math.floor(firstVisibleIndex - calcIndex(this._itemDynamicSize, this._minBufferPx, firstVisibleIndex, true)),
|
|
204
|
+
);
|
|
205
|
+
// console.log(
|
|
206
|
+
// 'expandEnd',
|
|
207
|
+
// firstVisibleIndex,
|
|
208
|
+
// '"' + newRange.start + '-' + newRange.end + '"',
|
|
209
|
+
// dataLength,
|
|
210
|
+
// Math.ceil(firstVisibleIndex + calcIndex(this._itemDynamicSize, viewportSize + this._minBufferPx, firstVisibleIndex)),
|
|
211
|
+
// );
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (newRange.start === 0 && newRange.end === 0 && dataLength > 0) {
|
|
217
|
+
// have items but not rendered, set start index to 0
|
|
218
|
+
newRange.end = dataLength;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (firstVisibleIndex === 0 && newRange.start > firstVisibleIndex) {
|
|
222
|
+
// This is bug fix. If newRange.start > firstVisibleIndex, can't visible '0'
|
|
223
|
+
newRange.start = firstVisibleIndex;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (newRange.start > newRange.end) {
|
|
227
|
+
// This is bug fix. If newRange.start > newRange.end, it will cause infinite loop.
|
|
228
|
+
newRange.end = Math.min(
|
|
229
|
+
dataLength,
|
|
230
|
+
Math.ceil(newRange.start + calcIndex(this._itemDynamicSize, viewportSize + this._minBufferPx, newRange.start)),
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
this._viewport.setRenderedRange(newRange);
|
|
235
|
+
// this._viewport.setRenderedContentOffset(this._itemDynamicSize * newRange.start);
|
|
236
|
+
if (!this._isReverse) {
|
|
237
|
+
this._viewport.setRenderedContentOffset(sumItemSize(this._itemDynamicSize, newRange.start));
|
|
238
|
+
} else {
|
|
239
|
+
let offset = Math.min(0, sumItemSize(this._itemDynamicSize, newRange.start) * -1);
|
|
240
|
+
if (offset === 0) {
|
|
241
|
+
offset = 0;
|
|
242
|
+
}
|
|
243
|
+
this._viewport.setRenderedContentOffset(offset);
|
|
244
|
+
}
|
|
245
|
+
this.measureScrollOffset = scrollOffset;
|
|
246
|
+
this._latestDataLength = dataLength;
|
|
247
|
+
this._scrolledIndexChange.next(firstVisibleIndex);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Provider factory for `FixedSizeVirtualScrollStrategy` that simply extracts the already created
|
|
253
|
+
* `FixedSizeVirtualScrollStrategy` from the given directive.
|
|
254
|
+
* @param fixedSizeDir The instance of `CdkFixedSizeVirtualScroll` to extract the
|
|
255
|
+
* `FixedSizeVirtualScrollStrategy` from.
|
|
256
|
+
*/
|
|
257
|
+
export function _dynamicSizeVirtualScrollStrategyFactory(fixedSizeDir: CdkDynamicSizeVirtualScroll) {
|
|
258
|
+
return fixedSizeDir._scrollStrategy;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export type itemDynamicSize = { itemSize: number } & Record<string, string | number>;
|
|
262
|
+
|
|
263
|
+
export const sumItemSize = (dynamicSize: itemDynamicSize[], endIndex: number): number => {
|
|
264
|
+
return dynamicSize.slice(0, endIndex).reduce((acc, item) => acc + item.itemSize, 0);
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* TODO: 計算方法の見直し
|
|
269
|
+
* 0.5 = 0個目の半分はマイナスの計算が生まれるため、1個目からカウント
|
|
270
|
+
*/
|
|
271
|
+
export const calcIndex = (dynamicSize: itemDynamicSize[], itemSizeRange: number, startIndex = 0, isReverse = false): number => {
|
|
272
|
+
let sum = 0;
|
|
273
|
+
let diffIndex = 0;
|
|
274
|
+
const item = isReverse ? structuredClone(dynamicSize).reverse() : dynamicSize;
|
|
275
|
+
if (isReverse) {
|
|
276
|
+
startIndex = dynamicSize.length - startIndex;
|
|
277
|
+
}
|
|
278
|
+
const calcIndex = item.reduce((acc, currentValue, index) => {
|
|
279
|
+
if (index < startIndex) {
|
|
280
|
+
return acc;
|
|
281
|
+
}
|
|
282
|
+
if (acc !== -1) {
|
|
283
|
+
// 既に見つかった場合、何もしない
|
|
284
|
+
return acc;
|
|
285
|
+
}
|
|
286
|
+
sum += currentValue.itemSize;
|
|
287
|
+
if (sum >= itemSizeRange) {
|
|
288
|
+
if (sum === itemSizeRange) {
|
|
289
|
+
return index - startIndex;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const diff = sum - itemSizeRange;
|
|
293
|
+
const getOver = item[index].itemSize - diff;
|
|
294
|
+
diffIndex = getOver / item[index].itemSize;
|
|
295
|
+
return Math.max(0, index - 1 + diffIndex - startIndex);
|
|
296
|
+
} else if (index === item.length - 1) {
|
|
297
|
+
return index - startIndex + 1;
|
|
298
|
+
}
|
|
299
|
+
return acc;
|
|
300
|
+
}, -1);
|
|
301
|
+
return calcIndex === -1 ? 0 : calcIndex;
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
/** A virtual scroll strategy that supports fixed-size items. */
|
|
305
|
+
@Directive({
|
|
306
|
+
selector: 'cdk-virtual-scroll-viewport[itemDynamicSizes]',
|
|
307
|
+
providers: [
|
|
308
|
+
{
|
|
309
|
+
provide: VIRTUAL_SCROLL_STRATEGY,
|
|
310
|
+
useFactory: _dynamicSizeVirtualScrollStrategyFactory,
|
|
311
|
+
deps: [forwardRef(() => CdkDynamicSizeVirtualScroll)],
|
|
312
|
+
},
|
|
313
|
+
],
|
|
314
|
+
})
|
|
315
|
+
export class CdkDynamicSizeVirtualScroll {
|
|
316
|
+
private readonly el = inject(ElementRef);
|
|
317
|
+
|
|
318
|
+
readonly itemDynamicSizes = input<itemDynamicSize[], itemDynamicSize[]>([], {
|
|
319
|
+
transform: coerceArray,
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* The minimum amount of buffer rendered beyond the viewport (in pixels).
|
|
324
|
+
* If the amount of buffer dips below this number, more items will be rendered. Defaults to 100px.
|
|
325
|
+
*/
|
|
326
|
+
readonly minBufferPx = input<number, NumberInput>(100, {
|
|
327
|
+
transform: coerceNumberProperty,
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The number of pixels worth of buffer to render for when rendering new items. Defaults to 200px.
|
|
332
|
+
*/
|
|
333
|
+
readonly maxBufferPx = input<number, NumberInput>(200, {
|
|
334
|
+
transform: coerceNumberProperty,
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
readonly isReverse = input<boolean, BooleanInput>(false, {
|
|
338
|
+
transform: coerceBooleanProperty,
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
/** The scroll strategy used by this directive. */
|
|
342
|
+
readonly _scrollStrategy = new DynamicSizeVirtualScrollStrategy(
|
|
343
|
+
this.itemDynamicSizes(),
|
|
344
|
+
this.minBufferPx(),
|
|
345
|
+
this.maxBufferPx(),
|
|
346
|
+
this.isReverse(),
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
constructor() {
|
|
350
|
+
effect(() => {
|
|
351
|
+
if (this.isReverse()) {
|
|
352
|
+
this.el.nativeElement.classList.add('reverse-scroll');
|
|
353
|
+
} else {
|
|
354
|
+
this.el.nativeElement.classList.remove('reverse-scroll');
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
effect(() =>
|
|
358
|
+
this._scrollStrategy.updateItemAndBufferSize(this.itemDynamicSizes(), this.minBufferPx(), this.maxBufferPx(), this.isReverse()),
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** For isReverse scroll. Because virtualScroll.measureScrollOffset is not work. **/
|
|
363
|
+
get scrollOffset(): number {
|
|
364
|
+
return this._scrollStrategy.measureScrollOffset;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import { DynamicSizeVirtualScrollService } from './dynamic-size-virtual-scroll.service';
|
|
4
|
+
import { testConfig } from '../../../util/test.config';
|
|
5
|
+
|
|
6
|
+
describe('DynamicSizeVirtualScrollService', () => {
|
|
7
|
+
let service: DynamicSizeVirtualScrollService;
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
TestBed.configureTestingModule({
|
|
11
|
+
providers: testConfig.providers,
|
|
12
|
+
});
|
|
13
|
+
service = TestBed.inject(DynamicSizeVirtualScrollService);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should be created', () => {
|
|
17
|
+
expect(service).toBeTruthy();
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { computed, Injectable, Signal } from '@angular/core';
|
|
2
|
+
import { CdkVirtualScrollViewport } from '@angular/cdk/scrolling';
|
|
3
|
+
import { itemDynamicSize } from './dynamic-size-virtual-scroll-strategy';
|
|
4
|
+
|
|
5
|
+
@Injectable({
|
|
6
|
+
providedIn: 'root',
|
|
7
|
+
})
|
|
8
|
+
export class DynamicSizeVirtualScrollService {
|
|
9
|
+
readonly mergeScrollY = 160;
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
* iOS smooth scroll can scroll after transition end.
|
|
13
|
+
* In this case, sometimes Virtual Scroll is white screen.
|
|
14
|
+
* Scroll amount restoration prevents this.
|
|
15
|
+
*/
|
|
16
|
+
onInit(virtualScroll: CdkVirtualScrollViewport, latestScrollOffset: number) {
|
|
17
|
+
if (latestScrollOffset > 0) {
|
|
18
|
+
virtualScroll.scrollToOffset(latestScrollOffset);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Save scroll amount for next view enter.
|
|
24
|
+
*/
|
|
25
|
+
onDestroy(virtualScroll: CdkVirtualScrollViewport): number {
|
|
26
|
+
return virtualScroll.measureScrollOffset('top');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Change itemDynamicSize[] to style.height[].
|
|
31
|
+
* require itemDynamicSize has 'source' and 'itemSize'.
|
|
32
|
+
*/
|
|
33
|
+
getBindDynamicItemHeight($calcDynamicItemSize: Signal<itemDynamicSize[]>): Signal<string[]> {
|
|
34
|
+
return computed<string[]>(() => {
|
|
35
|
+
return $calcDynamicItemSize().map((item) => {
|
|
36
|
+
return item['source'] === 'temporary' ? 'auto' : item.itemSize + 'px';
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* If change the array of hidden areas during scrolling,
|
|
43
|
+
* a layout shift will occur. (If not reverse, refers to the array located above the display area.)
|
|
44
|
+
* This function confirms that it is OK to change the array.
|
|
45
|
+
*/
|
|
46
|
+
isEnableMerge(virtualScroll: CdkVirtualScrollViewport): boolean {
|
|
47
|
+
return virtualScroll.getRenderedRange().start === 0 && virtualScroll.measureScrollOffset('top') < this.mergeScrollY;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* When an array is destructively replaced,
|
|
52
|
+
* the height of the viewport may remain the same as the sum of the previous array.
|
|
53
|
+
* This force it to be initialized.
|
|
54
|
+
*/
|
|
55
|
+
refreshViewport(virtualScroll: CdkVirtualScrollViewport): void {
|
|
56
|
+
virtualScroll.scrollToOffset(0);
|
|
57
|
+
virtualScroll.setRenderedContentOffset(0);
|
|
58
|
+
virtualScroll.setRenderedRange({ start: 0, end: 0 });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* This is for smooth scrolling to the top.
|
|
63
|
+
*/
|
|
64
|
+
async scrollToTopSmooth(virtualScroll: CdkVirtualScrollViewport): Promise<void> {
|
|
65
|
+
await this.scrollToPoint(virtualScroll, 0, 0, 400);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* This is for smooth scrolling to a specific point.
|
|
70
|
+
* https://github.com/ionic-team/ionic-framework/blob/main/core/src/components/content/content.tsx#L357C3-L404C4
|
|
71
|
+
*/
|
|
72
|
+
async scrollToPoint(
|
|
73
|
+
el: CdkVirtualScrollViewport,
|
|
74
|
+
x: number | undefined | null,
|
|
75
|
+
y: number | undefined | null,
|
|
76
|
+
duration = 0,
|
|
77
|
+
): Promise<void> {
|
|
78
|
+
if (duration < 32) {
|
|
79
|
+
if (y != null) {
|
|
80
|
+
el.scrollToOffset(y);
|
|
81
|
+
}
|
|
82
|
+
if (x != null) {
|
|
83
|
+
el.scrollToOffset(x);
|
|
84
|
+
}
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let resolve!: () => void;
|
|
89
|
+
let startTime = 0;
|
|
90
|
+
const promise = new Promise<void>((r) => (resolve = r));
|
|
91
|
+
const fromY = el.measureScrollOffset('top');
|
|
92
|
+
const fromX = el.measureScrollOffset('left');
|
|
93
|
+
|
|
94
|
+
const deltaY = y != null ? y - fromY : 0;
|
|
95
|
+
const deltaX = x != null ? x - fromX : 0;
|
|
96
|
+
|
|
97
|
+
// scroll loop
|
|
98
|
+
const step = (timeStamp: number) => {
|
|
99
|
+
const linearTime = Math.min(1, (timeStamp - startTime) / duration) - 1;
|
|
100
|
+
const easedT = Math.pow(linearTime, 3) + 1;
|
|
101
|
+
|
|
102
|
+
if (deltaY !== 0) {
|
|
103
|
+
el.scrollToOffset(Math.floor(easedT * deltaY + fromY));
|
|
104
|
+
}
|
|
105
|
+
if (deltaX !== 0) {
|
|
106
|
+
el.scrollToOffset(Math.floor(easedT * deltaX + fromX));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (easedT < 1) {
|
|
110
|
+
// do not use DomController here
|
|
111
|
+
// must use nativeRaf in order to fire in the next frame
|
|
112
|
+
requestAnimationFrame(step);
|
|
113
|
+
} else {
|
|
114
|
+
resolve();
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
// chill out for a frame first
|
|
118
|
+
requestAnimationFrame((ts) => {
|
|
119
|
+
startTime = ts;
|
|
120
|
+
step(ts);
|
|
121
|
+
});
|
|
122
|
+
return promise;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
|
2
|
+
{
|
|
3
|
+
"extends": "../../tsconfig.json",
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"outDir": "../../out-tsc/lib",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"inlineSources": true,
|
|
9
|
+
"types": []
|
|
10
|
+
},
|
|
11
|
+
"exclude": [
|
|
12
|
+
"**/*.spec.ts"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
|
2
|
+
{
|
|
3
|
+
"extends": "../../tsconfig.json",
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"outDir": "../../out-tsc/spec",
|
|
6
|
+
"types": [
|
|
7
|
+
"jasmine"
|
|
8
|
+
]
|
|
9
|
+
},
|
|
10
|
+
"include": [
|
|
11
|
+
"**/*.spec.ts",
|
|
12
|
+
"**/*.d.ts"
|
|
13
|
+
]
|
|
14
|
+
}
|