@oxog/scrollex 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -1
- package/dist/chunk-CIB3EBBJ.js +734 -0
- package/dist/chunk-CIB3EBBJ.js.map +1 -0
- package/dist/chunk-ND5PV5MU.cjs +747 -0
- package/dist/chunk-ND5PV5MU.cjs.map +1 -0
- package/dist/index.cjs +59 -776
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -808
- package/dist/index.d.ts +5 -808
- package/dist/index.js +25 -756
- package/dist/index.js.map +1 -1
- package/dist/plugins/index.cjs +31 -743
- package/dist/plugins/index.cjs.map +1 -1
- package/dist/plugins/index.d.cts +5 -529
- package/dist/plugins/index.d.ts +5 -529
- package/dist/plugins/index.js +9 -735
- package/dist/plugins/index.js.map +1 -1
- package/dist/scroll-controller-D5AV1E2p.d.cts +809 -0
- package/dist/scroll-controller-D5AV1E2p.d.ts +809 -0
- package/package.json +19 -5
package/dist/plugins/index.js
CHANGED
|
@@ -1,733 +1,8 @@
|
|
|
1
|
+
import { normalizeColumnCount, debounce, autoColumnCount } from '../chunk-CIB3EBBJ.js';
|
|
2
|
+
export { autoMeasurerPlugin, gridRendererPlugin, infiniteLoaderPlugin, listRendererPlugin, scrollControllerPlugin } from '../chunk-CIB3EBBJ.js';
|
|
1
3
|
import { useMemo, useCallback, useState, useEffect } from 'react';
|
|
2
4
|
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
3
5
|
|
|
4
|
-
// src/plugins/core/list-renderer.ts
|
|
5
|
-
function listRendererPlugin(options = {}) {
|
|
6
|
-
const { getItemKey = (index) => index } = options;
|
|
7
|
-
let kernel = null;
|
|
8
|
-
let virtualItems = [];
|
|
9
|
-
let currentRange = {
|
|
10
|
-
startIndex: 0,
|
|
11
|
-
endIndex: 0,
|
|
12
|
-
overscanStartIndex: 0,
|
|
13
|
-
overscanEndIndex: 0
|
|
14
|
-
};
|
|
15
|
-
function buildVirtualItems(range) {
|
|
16
|
-
if (!kernel) return [];
|
|
17
|
-
const items = [];
|
|
18
|
-
range.startIndex;
|
|
19
|
-
range.endIndex;
|
|
20
|
-
const lastIndex = Math.min(range.overscanEndIndex, kernel.getItemCount() - 1);
|
|
21
|
-
for (let i = range.overscanStartIndex; i <= lastIndex; i++) {
|
|
22
|
-
const offset = kernel.getItemOffset(i);
|
|
23
|
-
const size = kernel.getCachedHeight(i) ?? kernel.getEstimatedHeight();
|
|
24
|
-
items.push({
|
|
25
|
-
index: i,
|
|
26
|
-
key: getItemKey(i),
|
|
27
|
-
start: offset,
|
|
28
|
-
end: offset + size,
|
|
29
|
-
size,
|
|
30
|
-
lane: 0
|
|
31
|
-
// For list, always lane 0
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
return items;
|
|
35
|
-
}
|
|
36
|
-
function recalculate() {
|
|
37
|
-
if (!kernel) return;
|
|
38
|
-
currentRange = kernel.getVisibleRange();
|
|
39
|
-
virtualItems = buildVirtualItems(currentRange);
|
|
40
|
-
}
|
|
41
|
-
const api = {
|
|
42
|
-
getVirtualItems() {
|
|
43
|
-
return virtualItems;
|
|
44
|
-
},
|
|
45
|
-
getTotalHeight() {
|
|
46
|
-
return kernel?.getTotalSize() ?? 0;
|
|
47
|
-
},
|
|
48
|
-
getStartOffset() {
|
|
49
|
-
if (virtualItems.length === 0) return 0;
|
|
50
|
-
return virtualItems[0]?.start ?? 0;
|
|
51
|
-
},
|
|
52
|
-
getEndOffset() {
|
|
53
|
-
if (virtualItems.length === 0) return 0;
|
|
54
|
-
const lastItem = virtualItems[virtualItems.length - 1];
|
|
55
|
-
return lastItem?.end ?? 0;
|
|
56
|
-
},
|
|
57
|
-
isItemVisible(index) {
|
|
58
|
-
if (!kernel || index >= kernel.getItemCount()) return false;
|
|
59
|
-
return index >= currentRange.startIndex && index <= currentRange.endIndex;
|
|
60
|
-
},
|
|
61
|
-
recalculate
|
|
62
|
-
};
|
|
63
|
-
return {
|
|
64
|
-
name: "list-renderer",
|
|
65
|
-
version: "1.0.0",
|
|
66
|
-
type: "core",
|
|
67
|
-
install(k) {
|
|
68
|
-
kernel = k;
|
|
69
|
-
recalculate();
|
|
70
|
-
},
|
|
71
|
-
uninstall() {
|
|
72
|
-
virtualItems = [];
|
|
73
|
-
kernel = null;
|
|
74
|
-
},
|
|
75
|
-
hooks: {
|
|
76
|
-
onScroll: () => recalculate(),
|
|
77
|
-
onResize: () => recalculate(),
|
|
78
|
-
onItemMeasured: () => recalculate(),
|
|
79
|
-
onVisibleRangeChange: (range) => {
|
|
80
|
-
currentRange = range;
|
|
81
|
-
virtualItems = buildVirtualItems(range);
|
|
82
|
-
},
|
|
83
|
-
onItemsChange: () => recalculate()
|
|
84
|
-
},
|
|
85
|
-
api
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// src/utils/array.ts
|
|
90
|
-
function normalizeColumnCount(columns) {
|
|
91
|
-
return Number.isFinite(columns) ? Math.max(1, Math.floor(columns)) : 1;
|
|
92
|
-
}
|
|
93
|
-
function autoColumnCount(containerWidth, minColumnWidth, gap) {
|
|
94
|
-
const slot = minColumnWidth + gap;
|
|
95
|
-
return slot > 0 ? normalizeColumnCount(Math.floor((containerWidth + gap) / slot)) : 1;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// src/plugins/core/grid-renderer.ts
|
|
99
|
-
function gridRendererPlugin(options) {
|
|
100
|
-
const {
|
|
101
|
-
columns,
|
|
102
|
-
minColumnWidth = 100,
|
|
103
|
-
rowHeight,
|
|
104
|
-
gap = 0,
|
|
105
|
-
getItemKey = (index) => index,
|
|
106
|
-
getContainerWidth
|
|
107
|
-
} = options;
|
|
108
|
-
let kernel = null;
|
|
109
|
-
let virtualItems = [];
|
|
110
|
-
let columnCount = typeof columns === "number" ? normalizeColumnCount(columns) : 1;
|
|
111
|
-
let columnWidth = 0;
|
|
112
|
-
let gapX = typeof gap === "number" ? gap : gap.x;
|
|
113
|
-
let gapY = typeof gap === "number" ? gap : gap.y;
|
|
114
|
-
function calculateColumns(containerWidth) {
|
|
115
|
-
if (typeof columns === "number") {
|
|
116
|
-
columnCount = normalizeColumnCount(columns);
|
|
117
|
-
} else {
|
|
118
|
-
columnCount = autoColumnCount(containerWidth, minColumnWidth, gapX);
|
|
119
|
-
}
|
|
120
|
-
columnWidth = (containerWidth - (columnCount - 1) * gapX) / columnCount;
|
|
121
|
-
}
|
|
122
|
-
function getRowCount() {
|
|
123
|
-
if (!kernel) return 0;
|
|
124
|
-
return Math.ceil(kernel.getItemCount() / columnCount);
|
|
125
|
-
}
|
|
126
|
-
function buildVirtualItems() {
|
|
127
|
-
if (!kernel) return [];
|
|
128
|
-
const viewport = kernel.getViewport();
|
|
129
|
-
const scrollPosition = kernel.getScrollPosition();
|
|
130
|
-
const scrollTop = scrollPosition.scrollTop;
|
|
131
|
-
const effectiveRowHeight = rowHeight + gapY;
|
|
132
|
-
const startRow = Math.max(0, Math.floor(scrollTop / effectiveRowHeight));
|
|
133
|
-
const visibleRows = Math.ceil(viewport.height / effectiveRowHeight);
|
|
134
|
-
const overscan = kernel.getOptions().overscan;
|
|
135
|
-
const endRow = Math.min(getRowCount() - 1, startRow + visibleRows + overscan);
|
|
136
|
-
const overscanStartRow = Math.max(0, startRow - overscan);
|
|
137
|
-
const items = [];
|
|
138
|
-
const itemCount = kernel.getItemCount();
|
|
139
|
-
for (let row = overscanStartRow; row <= endRow; row++) {
|
|
140
|
-
for (let col = 0; col < columnCount; col++) {
|
|
141
|
-
const index = row * columnCount + col;
|
|
142
|
-
if (index >= itemCount) break;
|
|
143
|
-
const top = row * effectiveRowHeight;
|
|
144
|
-
items.push({
|
|
145
|
-
index,
|
|
146
|
-
key: getItemKey(index),
|
|
147
|
-
start: top,
|
|
148
|
-
end: top + rowHeight,
|
|
149
|
-
size: rowHeight,
|
|
150
|
-
lane: col
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return items;
|
|
155
|
-
}
|
|
156
|
-
function recalculate() {
|
|
157
|
-
if (!kernel) return;
|
|
158
|
-
const viewport = kernel.getViewport();
|
|
159
|
-
calculateColumns(getContainerWidth?.() ?? viewport.width);
|
|
160
|
-
virtualItems = buildVirtualItems();
|
|
161
|
-
}
|
|
162
|
-
const api = {
|
|
163
|
-
getVirtualItems() {
|
|
164
|
-
return virtualItems;
|
|
165
|
-
},
|
|
166
|
-
getTotalHeight() {
|
|
167
|
-
const rowCount = getRowCount();
|
|
168
|
-
return rowCount === 0 ? 0 : rowCount * (rowHeight + gapY) - gapY;
|
|
169
|
-
},
|
|
170
|
-
getColumnCount() {
|
|
171
|
-
return columnCount;
|
|
172
|
-
},
|
|
173
|
-
getColumnWidth() {
|
|
174
|
-
return columnWidth;
|
|
175
|
-
},
|
|
176
|
-
getItemsPerRow() {
|
|
177
|
-
return columnCount;
|
|
178
|
-
},
|
|
179
|
-
getRowCount,
|
|
180
|
-
recalculate
|
|
181
|
-
};
|
|
182
|
-
return {
|
|
183
|
-
name: "grid-renderer",
|
|
184
|
-
version: "1.0.0",
|
|
185
|
-
type: "core",
|
|
186
|
-
install(k) {
|
|
187
|
-
kernel = k;
|
|
188
|
-
recalculate();
|
|
189
|
-
},
|
|
190
|
-
uninstall() {
|
|
191
|
-
virtualItems = [];
|
|
192
|
-
kernel = null;
|
|
193
|
-
},
|
|
194
|
-
hooks: {
|
|
195
|
-
onScroll: () => {
|
|
196
|
-
virtualItems = buildVirtualItems();
|
|
197
|
-
},
|
|
198
|
-
onResize: () => recalculate(),
|
|
199
|
-
onItemsChange: () => recalculate()
|
|
200
|
-
},
|
|
201
|
-
api
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// src/utils/debounce.ts
|
|
206
|
-
function debounce(fn, wait) {
|
|
207
|
-
let timeoutId = null;
|
|
208
|
-
let lastArgs = null;
|
|
209
|
-
let lastThis = null;
|
|
210
|
-
const debounced = function(...args) {
|
|
211
|
-
lastArgs = args;
|
|
212
|
-
lastThis = this;
|
|
213
|
-
if (timeoutId) {
|
|
214
|
-
clearTimeout(timeoutId);
|
|
215
|
-
}
|
|
216
|
-
timeoutId = setTimeout(() => {
|
|
217
|
-
timeoutId = null;
|
|
218
|
-
if (lastArgs) {
|
|
219
|
-
fn.apply(lastThis, lastArgs);
|
|
220
|
-
lastArgs = null;
|
|
221
|
-
lastThis = null;
|
|
222
|
-
}
|
|
223
|
-
}, wait);
|
|
224
|
-
};
|
|
225
|
-
debounced.cancel = () => {
|
|
226
|
-
if (timeoutId) {
|
|
227
|
-
clearTimeout(timeoutId);
|
|
228
|
-
timeoutId = null;
|
|
229
|
-
}
|
|
230
|
-
lastArgs = null;
|
|
231
|
-
lastThis = null;
|
|
232
|
-
};
|
|
233
|
-
debounced.flush = () => {
|
|
234
|
-
if (timeoutId) {
|
|
235
|
-
clearTimeout(timeoutId);
|
|
236
|
-
timeoutId = null;
|
|
237
|
-
if (lastArgs) {
|
|
238
|
-
fn.apply(lastThis, lastArgs);
|
|
239
|
-
lastArgs = null;
|
|
240
|
-
lastThis = null;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
return debounced;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
// src/plugins/core/auto-measurer.ts
|
|
248
|
-
function autoMeasurerPlugin(options = {}) {
|
|
249
|
-
const {
|
|
250
|
-
estimatedItemHeight,
|
|
251
|
-
measureOnResize = true,
|
|
252
|
-
debounceMs = 100
|
|
253
|
-
} = options;
|
|
254
|
-
let kernel = null;
|
|
255
|
-
let resizeObserver = null;
|
|
256
|
-
const observedElements = /* @__PURE__ */ new WeakMap();
|
|
257
|
-
const elementsByIndex = /* @__PURE__ */ new Map();
|
|
258
|
-
let measureQueue = /* @__PURE__ */ new Set();
|
|
259
|
-
let flushScheduled = false;
|
|
260
|
-
function sizeAlongAxis(rect) {
|
|
261
|
-
return kernel?.getOptions().direction === "horizontal" ? rect.width : rect.height;
|
|
262
|
-
}
|
|
263
|
-
const debouncedFlush = debounce(() => {
|
|
264
|
-
flush();
|
|
265
|
-
}, debounceMs);
|
|
266
|
-
function flush() {
|
|
267
|
-
if (!kernel || measureQueue.size === 0) {
|
|
268
|
-
flushScheduled = false;
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
const indicesToMeasure = Array.from(measureQueue);
|
|
272
|
-
measureQueue = /* @__PURE__ */ new Set();
|
|
273
|
-
flushScheduled = false;
|
|
274
|
-
const measurements = [];
|
|
275
|
-
for (const index of indicesToMeasure) {
|
|
276
|
-
const ref = elementsByIndex.get(index);
|
|
277
|
-
const element = ref?.deref();
|
|
278
|
-
if (element) {
|
|
279
|
-
const rect = element.getBoundingClientRect();
|
|
280
|
-
measurements.push({ index, height: sizeAlongAxis(rect) });
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
for (const { index, height } of measurements) {
|
|
284
|
-
if (height > 0) {
|
|
285
|
-
kernel.measureItem(index, height);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
function scheduleFlush() {
|
|
290
|
-
if (!flushScheduled) {
|
|
291
|
-
flushScheduled = true;
|
|
292
|
-
requestAnimationFrame(() => {
|
|
293
|
-
flush();
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
function handleResize(entries) {
|
|
298
|
-
for (const entry of entries) {
|
|
299
|
-
const index = observedElements.get(entry.target);
|
|
300
|
-
if (index !== void 0) {
|
|
301
|
-
measureQueue.add(index);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
if (measureOnResize && measureQueue.size > 0) {
|
|
305
|
-
debouncedFlush();
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
const api = {
|
|
309
|
-
measureElement(index, element) {
|
|
310
|
-
const rect = element.getBoundingClientRect();
|
|
311
|
-
const height = sizeAlongAxis(rect);
|
|
312
|
-
if (height > 0 && kernel) {
|
|
313
|
-
kernel.measureItem(index, height);
|
|
314
|
-
}
|
|
315
|
-
return height;
|
|
316
|
-
},
|
|
317
|
-
getCachedSize(index) {
|
|
318
|
-
return kernel?.getCachedHeight(index);
|
|
319
|
-
},
|
|
320
|
-
invalidate(index) {
|
|
321
|
-
kernel?.invalidateMeasurement(index);
|
|
322
|
-
},
|
|
323
|
-
invalidateAll() {
|
|
324
|
-
kernel?.invalidateAllMeasurements();
|
|
325
|
-
},
|
|
326
|
-
getMeasurementCache() {
|
|
327
|
-
const cache = /* @__PURE__ */ new Map();
|
|
328
|
-
if (!kernel) return cache;
|
|
329
|
-
const itemCount = kernel.getItemCount();
|
|
330
|
-
for (let i = 0; i < itemCount; i++) {
|
|
331
|
-
const height = kernel.getCachedHeight(i);
|
|
332
|
-
if (height !== void 0) {
|
|
333
|
-
cache.set(i, height);
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
return cache;
|
|
337
|
-
},
|
|
338
|
-
observe(index, element) {
|
|
339
|
-
const existingRef = elementsByIndex.get(index);
|
|
340
|
-
const existingElement = existingRef?.deref();
|
|
341
|
-
if (existingElement && existingElement !== element) {
|
|
342
|
-
resizeObserver?.unobserve(existingElement);
|
|
343
|
-
observedElements.delete(existingElement);
|
|
344
|
-
}
|
|
345
|
-
const previousIndex = observedElements.get(element);
|
|
346
|
-
if (previousIndex !== void 0 && previousIndex !== index) {
|
|
347
|
-
elementsByIndex.delete(previousIndex);
|
|
348
|
-
measureQueue.delete(previousIndex);
|
|
349
|
-
}
|
|
350
|
-
observedElements.set(element, index);
|
|
351
|
-
elementsByIndex.set(index, typeof WeakRef === "function" ? new WeakRef(element) : { deref: () => element });
|
|
352
|
-
resizeObserver?.observe(element);
|
|
353
|
-
measureQueue.add(index);
|
|
354
|
-
scheduleFlush();
|
|
355
|
-
},
|
|
356
|
-
unobserve(element) {
|
|
357
|
-
const index = observedElements.get(element);
|
|
358
|
-
if (index !== void 0) {
|
|
359
|
-
resizeObserver?.unobserve(element);
|
|
360
|
-
observedElements.delete(element);
|
|
361
|
-
elementsByIndex.delete(index);
|
|
362
|
-
measureQueue.delete(index);
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
};
|
|
366
|
-
return {
|
|
367
|
-
name: "auto-measurer",
|
|
368
|
-
version: "1.0.0",
|
|
369
|
-
type: "core",
|
|
370
|
-
install(k) {
|
|
371
|
-
kernel = k;
|
|
372
|
-
if (estimatedItemHeight !== void 0) {
|
|
373
|
-
kernel.configure({ estimatedItemHeight });
|
|
374
|
-
}
|
|
375
|
-
if (measureOnResize && typeof ResizeObserver !== "undefined") {
|
|
376
|
-
resizeObserver = new ResizeObserver(handleResize);
|
|
377
|
-
}
|
|
378
|
-
},
|
|
379
|
-
uninstall() {
|
|
380
|
-
if (resizeObserver) {
|
|
381
|
-
resizeObserver.disconnect();
|
|
382
|
-
resizeObserver = null;
|
|
383
|
-
}
|
|
384
|
-
debouncedFlush.cancel();
|
|
385
|
-
measureQueue.clear();
|
|
386
|
-
elementsByIndex.clear();
|
|
387
|
-
flushScheduled = false;
|
|
388
|
-
kernel = null;
|
|
389
|
-
},
|
|
390
|
-
api
|
|
391
|
-
};
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
// src/plugins/core/infinite-loader.ts
|
|
395
|
-
function initialLoaderState(direction) {
|
|
396
|
-
return { loading: false, loadGeneration: 0, hasMoreForward: true, hasMoreBackward: direction === "backward" || direction === "both" };
|
|
397
|
-
}
|
|
398
|
-
var sharedLoaderStates = /* @__PURE__ */ new WeakMap();
|
|
399
|
-
function infiniteLoaderPlugin(options) {
|
|
400
|
-
const {
|
|
401
|
-
threshold = 200,
|
|
402
|
-
thresholdItems = 5,
|
|
403
|
-
direction = "forward",
|
|
404
|
-
onLoadMore
|
|
405
|
-
} = options;
|
|
406
|
-
let kernel = null;
|
|
407
|
-
let state = initialLoaderState(direction);
|
|
408
|
-
let lastCheckTime = 0;
|
|
409
|
-
const checkDebounceMs = 100;
|
|
410
|
-
let pendingEvent = null;
|
|
411
|
-
let trailingTimer = null;
|
|
412
|
-
function checkLoadMore(event) {
|
|
413
|
-
if (state.loading || !kernel) return;
|
|
414
|
-
const now = performance.now();
|
|
415
|
-
if (now - lastCheckTime < checkDebounceMs) {
|
|
416
|
-
pendingEvent = event;
|
|
417
|
-
if (trailingTimer === null) {
|
|
418
|
-
trailingTimer = setTimeout(() => {
|
|
419
|
-
trailingTimer = null;
|
|
420
|
-
const latest = pendingEvent;
|
|
421
|
-
pendingEvent = null;
|
|
422
|
-
if (latest) runCheck(latest);
|
|
423
|
-
}, Math.max(0, checkDebounceMs - (now - lastCheckTime)));
|
|
424
|
-
}
|
|
425
|
-
return;
|
|
426
|
-
}
|
|
427
|
-
runCheck(event);
|
|
428
|
-
}
|
|
429
|
-
function runCheck(event) {
|
|
430
|
-
if (state.loading || !kernel) return;
|
|
431
|
-
lastCheckTime = performance.now();
|
|
432
|
-
const viewport = kernel.getViewport();
|
|
433
|
-
const totalSize = kernel.getTotalSize();
|
|
434
|
-
const horizontal = kernel.getOptions().direction === "horizontal";
|
|
435
|
-
const scrollTop = horizontal ? event.scrollLeft : event.scrollTop;
|
|
436
|
-
const viewportSize = horizontal ? viewport.width : viewport.height;
|
|
437
|
-
if ((direction === "forward" || direction === "both") && state.hasMoreForward) {
|
|
438
|
-
const distanceFromBottom = totalSize - scrollTop - viewportSize;
|
|
439
|
-
if (distanceFromBottom < threshold) {
|
|
440
|
-
triggerLoad("forward");
|
|
441
|
-
return;
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
if ((direction === "backward" || direction === "both") && state.hasMoreBackward) {
|
|
445
|
-
if (scrollTop < threshold) {
|
|
446
|
-
triggerLoad("backward");
|
|
447
|
-
return;
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
function checkLoadMoreByRange(range, forwardOnly = false) {
|
|
452
|
-
if (state.loading || !kernel) return;
|
|
453
|
-
const itemCount = kernel.getItemCount();
|
|
454
|
-
if ((direction === "forward" || direction === "both") && state.hasMoreForward) {
|
|
455
|
-
const itemsFromEnd = itemCount - 1 - range.endIndex;
|
|
456
|
-
if (itemsFromEnd <= thresholdItems) {
|
|
457
|
-
triggerLoad("forward");
|
|
458
|
-
return;
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
if (!forwardOnly && (direction === "backward" || direction === "both") && state.hasMoreBackward) {
|
|
462
|
-
if (range.startIndex <= thresholdItems) {
|
|
463
|
-
triggerLoad("backward");
|
|
464
|
-
return;
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
async function triggerLoad(loadDirection) {
|
|
469
|
-
if (state.loading || !kernel) return;
|
|
470
|
-
if (loadDirection === "forward" && !state.hasMoreForward) return;
|
|
471
|
-
if (loadDirection === "backward" && !state.hasMoreBackward) return;
|
|
472
|
-
state.loading = true;
|
|
473
|
-
const generation = ++state.loadGeneration;
|
|
474
|
-
kernel.emit({
|
|
475
|
-
type: "load-more",
|
|
476
|
-
direction: loadDirection
|
|
477
|
-
});
|
|
478
|
-
try {
|
|
479
|
-
await onLoadMore(loadDirection);
|
|
480
|
-
} catch (error) {
|
|
481
|
-
console.error("[Scrollex] Error in onLoadMore:", error);
|
|
482
|
-
} finally {
|
|
483
|
-
if (generation === state.loadGeneration) {
|
|
484
|
-
state.loading = false;
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
const api = {
|
|
489
|
-
isLoading() {
|
|
490
|
-
return state.loading;
|
|
491
|
-
},
|
|
492
|
-
setLoading(l) {
|
|
493
|
-
state.loading = l;
|
|
494
|
-
},
|
|
495
|
-
hasMore() {
|
|
496
|
-
return state.hasMoreForward || state.hasMoreBackward;
|
|
497
|
-
},
|
|
498
|
-
setHasMore(hasMore) {
|
|
499
|
-
state.hasMoreForward = hasMore;
|
|
500
|
-
state.hasMoreBackward = hasMore;
|
|
501
|
-
},
|
|
502
|
-
hasMoreForward() {
|
|
503
|
-
return state.hasMoreForward;
|
|
504
|
-
},
|
|
505
|
-
setHasMoreForward(hasMore) {
|
|
506
|
-
state.hasMoreForward = hasMore;
|
|
507
|
-
},
|
|
508
|
-
hasMoreBackward() {
|
|
509
|
-
return state.hasMoreBackward;
|
|
510
|
-
},
|
|
511
|
-
setHasMoreBackward(hasMore) {
|
|
512
|
-
state.hasMoreBackward = hasMore;
|
|
513
|
-
},
|
|
514
|
-
reset() {
|
|
515
|
-
state.loadGeneration++;
|
|
516
|
-
state.loading = false;
|
|
517
|
-
state.hasMoreForward = true;
|
|
518
|
-
state.hasMoreBackward = direction === "backward" || direction === "both";
|
|
519
|
-
if (trailingTimer !== null) {
|
|
520
|
-
clearTimeout(trailingTimer);
|
|
521
|
-
trailingTimer = null;
|
|
522
|
-
}
|
|
523
|
-
pendingEvent = null;
|
|
524
|
-
},
|
|
525
|
-
triggerLoad(loadDirection) {
|
|
526
|
-
triggerLoad(loadDirection);
|
|
527
|
-
}
|
|
528
|
-
};
|
|
529
|
-
return {
|
|
530
|
-
name: "infinite-loader",
|
|
531
|
-
version: "1.0.0",
|
|
532
|
-
type: "core",
|
|
533
|
-
install(k) {
|
|
534
|
-
kernel = k;
|
|
535
|
-
const shared = sharedLoaderStates.get(k);
|
|
536
|
-
if (shared) {
|
|
537
|
-
state = shared;
|
|
538
|
-
} else {
|
|
539
|
-
sharedLoaderStates.set(k, state);
|
|
540
|
-
checkLoadMoreByRange(k.getVisibleRange(), true);
|
|
541
|
-
}
|
|
542
|
-
},
|
|
543
|
-
uninstall() {
|
|
544
|
-
if (trailingTimer !== null) {
|
|
545
|
-
clearTimeout(trailingTimer);
|
|
546
|
-
trailingTimer = null;
|
|
547
|
-
}
|
|
548
|
-
pendingEvent = null;
|
|
549
|
-
kernel = null;
|
|
550
|
-
},
|
|
551
|
-
hooks: {
|
|
552
|
-
onScroll: checkLoadMore,
|
|
553
|
-
onVisibleRangeChange: (range) => checkLoadMoreByRange(range)
|
|
554
|
-
},
|
|
555
|
-
api
|
|
556
|
-
};
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
// src/utils/raf.ts
|
|
560
|
-
function raf(callback) {
|
|
561
|
-
if (typeof requestAnimationFrame !== "undefined") {
|
|
562
|
-
return requestAnimationFrame(callback);
|
|
563
|
-
}
|
|
564
|
-
return setTimeout(() => callback(performance.now()), 16);
|
|
565
|
-
}
|
|
566
|
-
function cancelRaf(id) {
|
|
567
|
-
if (typeof cancelAnimationFrame !== "undefined") {
|
|
568
|
-
cancelAnimationFrame(id);
|
|
569
|
-
} else {
|
|
570
|
-
clearTimeout(id);
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
// src/plugins/core/scroll-controller.ts
|
|
575
|
-
var easings = {
|
|
576
|
-
easeInOut: (t) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t};
|
|
577
|
-
function scrollControllerPlugin(options = {}) {
|
|
578
|
-
const {
|
|
579
|
-
defaultDuration = 300,
|
|
580
|
-
defaultEasing = easings.easeInOut
|
|
581
|
-
} = options;
|
|
582
|
-
let kernel = null;
|
|
583
|
-
let animationId = null;
|
|
584
|
-
let animating = false;
|
|
585
|
-
let animationGeneration = 0;
|
|
586
|
-
function isHorizontal(k) {
|
|
587
|
-
return k.getOptions().direction === "horizontal";
|
|
588
|
-
}
|
|
589
|
-
function getOffset(k) {
|
|
590
|
-
const position = k.getScrollPosition();
|
|
591
|
-
return isHorizontal(k) ? position.scrollLeft : position.scrollTop;
|
|
592
|
-
}
|
|
593
|
-
function setOffset(k, offset) {
|
|
594
|
-
k.setScrollPosition(isHorizontal(k) ? { scrollLeft: offset } : { scrollTop: offset });
|
|
595
|
-
}
|
|
596
|
-
function getViewportSize(k) {
|
|
597
|
-
const viewport = k.getViewport();
|
|
598
|
-
return isHorizontal(k) ? viewport.width : viewport.height;
|
|
599
|
-
}
|
|
600
|
-
function animateScroll(targetOffset, duration, easing) {
|
|
601
|
-
if (!kernel) return;
|
|
602
|
-
stopAnimation();
|
|
603
|
-
const generation = animationGeneration;
|
|
604
|
-
const startTime = performance.now();
|
|
605
|
-
const startOffset = getOffset(kernel);
|
|
606
|
-
const distance = targetOffset - startOffset;
|
|
607
|
-
if (distance === 0 || !(duration > 0)) {
|
|
608
|
-
setOffset(kernel, targetOffset);
|
|
609
|
-
return;
|
|
610
|
-
}
|
|
611
|
-
animating = true;
|
|
612
|
-
function animate(currentTime) {
|
|
613
|
-
if (!kernel) {
|
|
614
|
-
animating = false;
|
|
615
|
-
return;
|
|
616
|
-
}
|
|
617
|
-
if (generation !== animationGeneration) return;
|
|
618
|
-
const elapsed = currentTime - startTime;
|
|
619
|
-
const progress = Math.max(0, Math.min(elapsed / duration, 1));
|
|
620
|
-
const easedProgress = easing(progress);
|
|
621
|
-
const currentOffset = startOffset + distance * easedProgress;
|
|
622
|
-
setOffset(kernel, currentOffset);
|
|
623
|
-
if (generation !== animationGeneration) return;
|
|
624
|
-
if (progress < 1) {
|
|
625
|
-
animationId = raf(animate);
|
|
626
|
-
} else {
|
|
627
|
-
animationId = null;
|
|
628
|
-
animating = false;
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
animationId = raf(animate);
|
|
632
|
-
}
|
|
633
|
-
function stopAnimation() {
|
|
634
|
-
animationGeneration++;
|
|
635
|
-
if (animationId !== null) {
|
|
636
|
-
cancelRaf(animationId);
|
|
637
|
-
animationId = null;
|
|
638
|
-
}
|
|
639
|
-
animating = false;
|
|
640
|
-
}
|
|
641
|
-
const api = {
|
|
642
|
-
scrollTo(offset, options2 = {}) {
|
|
643
|
-
if (!kernel) return;
|
|
644
|
-
const {
|
|
645
|
-
behavior = "auto",
|
|
646
|
-
duration = defaultDuration,
|
|
647
|
-
easing = defaultEasing
|
|
648
|
-
} = options2;
|
|
649
|
-
if (behavior === "auto") {
|
|
650
|
-
stopAnimation();
|
|
651
|
-
setOffset(kernel, offset);
|
|
652
|
-
} else {
|
|
653
|
-
animateScroll(offset, duration, easing);
|
|
654
|
-
}
|
|
655
|
-
},
|
|
656
|
-
scrollToIndex(index, options2 = {}) {
|
|
657
|
-
if (!kernel) return;
|
|
658
|
-
const {
|
|
659
|
-
align = "auto",
|
|
660
|
-
offset: additionalOffset = 0,
|
|
661
|
-
...scrollOptions
|
|
662
|
-
} = options2;
|
|
663
|
-
const itemOffset = kernel.getItemOffset(index);
|
|
664
|
-
const itemSize = kernel.getCachedHeight(index) ?? kernel.getEstimatedHeight();
|
|
665
|
-
const viewportSize = getViewportSize(kernel);
|
|
666
|
-
let targetOffset;
|
|
667
|
-
switch (align) {
|
|
668
|
-
case "start":
|
|
669
|
-
targetOffset = itemOffset;
|
|
670
|
-
break;
|
|
671
|
-
case "center":
|
|
672
|
-
targetOffset = itemOffset - viewportSize / 2 + itemSize / 2;
|
|
673
|
-
break;
|
|
674
|
-
case "end":
|
|
675
|
-
targetOffset = itemOffset - viewportSize + itemSize;
|
|
676
|
-
break;
|
|
677
|
-
case "auto":
|
|
678
|
-
default: {
|
|
679
|
-
const visibleStart = getOffset(kernel);
|
|
680
|
-
const visibleEnd = visibleStart + viewportSize;
|
|
681
|
-
const itemEnd = itemOffset + itemSize;
|
|
682
|
-
if (itemOffset < visibleStart) {
|
|
683
|
-
targetOffset = itemOffset;
|
|
684
|
-
} else if (itemEnd > visibleEnd) {
|
|
685
|
-
targetOffset = itemEnd - viewportSize;
|
|
686
|
-
} else {
|
|
687
|
-
return;
|
|
688
|
-
}
|
|
689
|
-
break;
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
const maxScroll = Math.max(0, kernel.getTotalSize() - viewportSize);
|
|
693
|
-
targetOffset = Math.max(0, Math.min(targetOffset + additionalOffset, maxScroll));
|
|
694
|
-
api.scrollTo(targetOffset, scrollOptions);
|
|
695
|
-
},
|
|
696
|
-
scrollToTop(options2 = {}) {
|
|
697
|
-
api.scrollTo(0, options2);
|
|
698
|
-
},
|
|
699
|
-
scrollToBottom(options2 = {}) {
|
|
700
|
-
if (!kernel) return;
|
|
701
|
-
const totalSize = kernel.getTotalSize();
|
|
702
|
-
const maxScroll = Math.max(0, totalSize - getViewportSize(kernel));
|
|
703
|
-
api.scrollTo(maxScroll, options2);
|
|
704
|
-
},
|
|
705
|
-
getScrollPosition() {
|
|
706
|
-
return kernel?.getScrollPosition() ?? { scrollTop: 0, scrollLeft: 0 };
|
|
707
|
-
},
|
|
708
|
-
isScrolling() {
|
|
709
|
-
return kernel?.isScrolling() ?? false;
|
|
710
|
-
},
|
|
711
|
-
isAnimating() {
|
|
712
|
-
return animating;
|
|
713
|
-
},
|
|
714
|
-
stopAnimation
|
|
715
|
-
};
|
|
716
|
-
return {
|
|
717
|
-
name: "scroll-controller",
|
|
718
|
-
version: "1.0.0",
|
|
719
|
-
type: "core",
|
|
720
|
-
install(k) {
|
|
721
|
-
kernel = k;
|
|
722
|
-
},
|
|
723
|
-
uninstall() {
|
|
724
|
-
stopAnimation();
|
|
725
|
-
kernel = null;
|
|
726
|
-
},
|
|
727
|
-
api
|
|
728
|
-
};
|
|
729
|
-
}
|
|
730
|
-
|
|
731
6
|
// src/plugins/optional/masonry-renderer.ts
|
|
732
7
|
function masonryRenderer(options) {
|
|
733
8
|
const {
|
|
@@ -878,10 +153,8 @@ function masonryRenderer(options) {
|
|
|
878
153
|
// src/plugins/optional/sticky-headers.ts
|
|
879
154
|
function stickyHeaders(options) {
|
|
880
155
|
const {
|
|
881
|
-
headerHeight,
|
|
882
156
|
isHeader,
|
|
883
157
|
getGroupId,
|
|
884
|
-
zIndex = 10,
|
|
885
158
|
stickyOffset = 0
|
|
886
159
|
} = options;
|
|
887
160
|
let kernel = null;
|
|
@@ -1367,6 +640,10 @@ function scrollRestoration(options) {
|
|
|
1367
640
|
}
|
|
1368
641
|
}
|
|
1369
642
|
function handleScroll(_event) {
|
|
643
|
+
if (awaitingContent && kernel) {
|
|
644
|
+
awaitingContent = false;
|
|
645
|
+
restoredKernels.set(kernel, key);
|
|
646
|
+
}
|
|
1370
647
|
if (saveOnScroll && debouncedSave) {
|
|
1371
648
|
debouncedSave();
|
|
1372
649
|
}
|
|
@@ -1501,8 +778,9 @@ function bidirectional(options = {}) {
|
|
|
1501
778
|
if (scrollPosition.scrollTop > stickThreshold) ;
|
|
1502
779
|
}
|
|
1503
780
|
if (isSticking && stickToBottom) {
|
|
781
|
+
const k = kernel;
|
|
1504
782
|
requestAnimationFrame(() => {
|
|
1505
|
-
|
|
783
|
+
k.scrollTo(k.getTotalSize());
|
|
1506
784
|
});
|
|
1507
785
|
}
|
|
1508
786
|
}
|
|
@@ -1547,7 +825,6 @@ function bidirectional(options = {}) {
|
|
|
1547
825
|
type: "optional",
|
|
1548
826
|
install(k) {
|
|
1549
827
|
kernel = k;
|
|
1550
|
-
k.getItemCount();
|
|
1551
828
|
previousTotalSize = k.getTotalSize();
|
|
1552
829
|
isSticking = stickToBottom && (initialPosition === "bottom" && !positionedKernels.has(k) || checkIsAtBottom());
|
|
1553
830
|
if (initialPosition === "bottom" && !positionedKernels.has(k)) {
|
|
@@ -1589,10 +866,7 @@ function dragToReorder(options) {
|
|
|
1589
866
|
const {
|
|
1590
867
|
enabled: initialEnabled = true,
|
|
1591
868
|
axis = "vertical",
|
|
1592
|
-
handle = true,
|
|
1593
869
|
autoScroll = true,
|
|
1594
|
-
autoScrollSpeed = 10,
|
|
1595
|
-
dropIndicator = "line",
|
|
1596
870
|
onDragStart,
|
|
1597
871
|
onDragEnd,
|
|
1598
872
|
onReorder,
|
|
@@ -2377,6 +1651,6 @@ function EventsPanel({ eventLog, onClear }) {
|
|
|
2377
1651
|
] });
|
|
2378
1652
|
}
|
|
2379
1653
|
|
|
2380
|
-
export { DebugPanelOverlay,
|
|
1654
|
+
export { DebugPanelOverlay, bidirectional, debugPanel, dragToReorder, keyboardNav, masonryRenderer, scrollRestoration, stickyHeaders };
|
|
2381
1655
|
//# sourceMappingURL=index.js.map
|
|
2382
1656
|
//# sourceMappingURL=index.js.map
|