@lynx-js/lynx-ui-common 3.130.0 → 3.133.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.
Files changed (47) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/hooks/index.d.ts +1 -3
  3. package/dist/hooks/index.d.ts.map +1 -1
  4. package/dist/hooks/index.jsx +2 -3
  5. package/dist/hooks/index.jsx.map +1 -1
  6. package/dist/hooks/usePreCommit.d.ts +15 -0
  7. package/dist/hooks/usePreCommit.d.ts.map +1 -0
  8. package/dist/hooks/usePreCommit.jsx +49 -0
  9. package/dist/hooks/usePreCommit.jsx.map +1 -0
  10. package/dist/types/interfaces/BaseScrollEvents.d.ts +0 -1
  11. package/dist/types/interfaces/BaseScrollEvents.d.ts.map +1 -1
  12. package/dist/types/interfaces/LazyInterface.d.ts +0 -1
  13. package/dist/types/interfaces/LazyInterface.d.ts.map +1 -1
  14. package/dist/types/interfaces/index.d.ts +0 -2
  15. package/dist/types/interfaces/index.d.ts.map +1 -1
  16. package/dist/utils/areDepsEqual.d.ts +6 -0
  17. package/dist/utils/areDepsEqual.d.ts.map +1 -0
  18. package/dist/utils/areDepsEqual.jsx +9 -0
  19. package/dist/utils/areDepsEqual.jsx.map +1 -0
  20. package/package.json +3 -3
  21. package/src/hooks/docs/useBounce.mdx +0 -1
  22. package/src/hooks/docs/useRefresh.mdx +0 -1
  23. package/src/hooks/index.ts +1 -3
  24. package/src/hooks/usePreCommit.tsx +111 -0
  25. package/src/types/interfaces/BaseScrollEvents.ts +0 -1
  26. package/src/types/interfaces/LazyInterface.tsx +0 -1
  27. package/src/types/interfaces/index.ts +0 -2
  28. package/src/utils/areDepsEqual.ts +26 -0
  29. package/dist/hooks/useBounce.d.ts +0 -26
  30. package/dist/hooks/useBounce.d.ts.map +0 -1
  31. package/dist/hooks/useBounce.jsx +0 -383
  32. package/dist/hooks/useBounce.jsx.map +0 -1
  33. package/dist/hooks/useRefresh.d.ts +0 -24
  34. package/dist/hooks/useRefresh.d.ts.map +0 -1
  35. package/dist/hooks/useRefresh.jsx +0 -554
  36. package/dist/hooks/useRefresh.jsx.map +0 -1
  37. package/dist/types/interfaces/BounceableInterface.d.ts +0 -120
  38. package/dist/types/interfaces/BounceableInterface.d.ts.map +0 -1
  39. package/dist/types/interfaces/BounceableInterface.jsx +0 -0
  40. package/dist/types/interfaces/RefreshInterface.d.ts +0 -98
  41. package/dist/types/interfaces/RefreshInterface.d.ts.map +0 -1
  42. package/dist/types/interfaces/RefreshInterface.jsx +0 -10
  43. package/dist/types/interfaces/RefreshInterface.jsx.map +0 -1
  44. package/src/hooks/useBounce.tsx +0 -814
  45. package/src/hooks/useRefresh.ts +0 -1054
  46. package/src/types/interfaces/BounceableInterface.ts +0 -125
  47. package/src/types/interfaces/RefreshInterface.ts +0 -105
@@ -1,554 +0,0 @@
1
- import { runOnBackground, useMainThreadRef } from "@lynx-js/react";
2
- import { NativeGesture, useGesture } from "@lynx-js/gesture-runtime";
3
- import { RefreshState } from "../types/interfaces/RefreshInterface.jsx";
4
- import { selectorMT } from "../utils/selector.jsx";
5
- import { mtsNativeLynxSDKVersionLessThan } from "../utils/version.jsx";
6
- function useRefreshAndBounce(options) {
7
- const rubberC = useMainThreadRef(0.55);
8
- const flingDeceleratingRate = useMainThreadRef(0.98);
9
- const beta = useMainThreadRef(15);
10
- const startTouchEvent = useMainThreadRef(null);
11
- const prevTouchEvent = useMainThreadRef(null);
12
- const scrollVelocity = useMainThreadRef(0);
13
- const containerID = options.id ?? 'bounceableContainer';
14
- const bouncingPositionInfo = useMainThreadRef({
15
- currentID: containerID
16
- });
17
- const startBouncingTouchEvent = useMainThreadRef(null);
18
- const startTouchBouncingDelta = useMainThreadRef({});
19
- const bouncingTouchStartPosition = useMainThreadRef({});
20
- const prevScroll = useMainThreadRef({});
21
- const height = useMainThreadRef(0);
22
- const width = useMainThreadRef(0);
23
- const scrollX = useMainThreadRef(0);
24
- const scrollY = useMainThreadRef(0);
25
- const touchEndFrameEnableFlag = useMainThreadRef(false);
26
- const touchingEndBouncingBackEnableFlag = useMainThreadRef(false);
27
- const flingEndWithBouncingEnableFlag = useMainThreadRef(false);
28
- const toUpper = useMainThreadRef(false);
29
- const toLower = useMainThreadRef(false);
30
- const { enableBounces, onScrollToBounces } = options.bounceableOptions;
31
- const bounceableDebugLog = useMainThreadRef((enableBounces && options.bounceableOptions.debugLog) ?? false);
32
- const alwaysBouncing = options.bounceableOptions.alwaysBouncing ?? options.refreshOptions.enableRefresh ?? false;
33
- const scrollOrientation = options.scrollOrientation ?? 'vertical';
34
- const enableBounceEventInFling = options.bounceableOptions.enableBounceEventInFling ?? options.bounceableOptions.enableBounceEventInFling ?? true;
35
- const startBounceTriggerDistance = options.bounceableOptions.startBounceTriggerDistance ?? 0;
36
- const endBounceTriggerDistance = options.bounceableOptions.endBounceTriggerDistance ?? 0;
37
- const temporaryValidVersionCheckForRAF = useMainThreadRef(options.bounceableOptions.validAnimationVersion ?? options.refreshOptions.validAnimationVersion);
38
- const { enableRefresh, onStartRefresh, onRefreshOffsetChange, onRefreshStateChange, onHeaderReleased } = options.refreshOptions;
39
- const refreshHeaderSize = useMainThreadRef(0);
40
- const refreshDebugLog = useMainThreadRef(options.refreshOptions.debugLog ?? false);
41
- const isDuringRefresh = useMainThreadRef(false);
42
- const currentRefreshState = useMainThreadRef(RefreshState.IDLE);
43
- const bouncingStatus = {
44
- inScrollingRange: 0,
45
- upperBouncing: 1,
46
- lowerBouncing: 2,
47
- noBouncing: 3,
48
- alwaysBouncing: 4
49
- };
50
- const bouncingBackMode = {
51
- resetToBoundary: 0,
52
- clampToHeader: 1
53
- };
54
- function isVertical() {
55
- 'main thread';
56
- return 'vertical' === scrollOrientation;
57
- }
58
- function threshold() {
59
- 'main thread';
60
- return 1.0 / Number(SystemInfo.pixelRatio);
61
- }
62
- function bouncingBackThreshold(mode) {
63
- 'main thread';
64
- switch(mode){
65
- case bouncingBackMode.resetToBoundary:
66
- return threshold();
67
- case bouncingBackMode.clampToHeader:
68
- return refreshHeaderSize.current + threshold();
69
- default:
70
- return threshold();
71
- }
72
- }
73
- function bouncingBackTargetPosition(mode) {
74
- 'main thread';
75
- switch(mode){
76
- case bouncingBackMode.resetToBoundary:
77
- return 0;
78
- case bouncingBackMode.clampToHeader:
79
- return refreshHeaderSize.current;
80
- default:
81
- return 0;
82
- }
83
- }
84
- function isAndroid() {
85
- 'main thread';
86
- return 'Android' === SystemInfo.platform;
87
- }
88
- function isEmpty(obj) {
89
- 'main thread';
90
- return !obj || 0 === Object.keys(obj).length;
91
- }
92
- function enableScroll(enable) {
93
- 'main thread';
94
- if (isAndroid() || isDuringRefresh.current) selectorMT(containerID)?.setAttribute('enable-scroll', enable);
95
- }
96
- function clearTouchInfo() {
97
- 'main thread';
98
- startTouchEvent.current = null;
99
- bouncingTouchStartPosition.current = 0;
100
- prevTouchEvent.current = null;
101
- startBouncingTouchEvent.current = null;
102
- startTouchBouncingDelta.current = 0;
103
- }
104
- function customRequestAnimationFrame(animationFunc) {
105
- 'main thread';
106
- if (temporaryValidVersionCheckForRAF.current) requestAnimationFrame(animationFunc);
107
- else setTimeout(animationFunc, 8);
108
- }
109
- function getCurrentDelta(event) {
110
- 'main thread';
111
- if (null === startTouchEvent.current) return 0;
112
- if (null === startBouncingTouchEvent.current) return 'vertical' === scrollOrientation ? event.params.pageY - startTouchEvent.current.params.pageY : event.params.pageX - startTouchEvent.current.params.pageX;
113
- return 'vertical' === scrollOrientation ? event.params.pageY - startBouncingTouchEvent.current.params.pageY : event.params.pageX - startBouncingTouchEvent.current.params.pageX;
114
- }
115
- function onRefreshStateChangeJS(info) {
116
- if (enableRefresh && onRefreshStateChange) onRefreshStateChange(info);
117
- }
118
- function sendRefreshStateChangeEvent(state) {
119
- 'main thread';
120
- const refreshStateChangeInfo = {
121
- state
122
- };
123
- runOnBackground(onRefreshStateChangeJS)(refreshStateChangeInfo);
124
- }
125
- function setRefreshState(state) {
126
- 'main thread';
127
- if (currentRefreshState.current === state) return;
128
- currentRefreshState.current = state;
129
- sendRefreshStateChangeEvent(state);
130
- }
131
- function onStartRefreshJS(info) {
132
- if (enableRefresh && onStartRefresh) onStartRefresh(info);
133
- }
134
- function sendStartRefreshEvent(fromStartRefreshMethod) {
135
- 'main thread';
136
- const refreshInfo = {
137
- triggeredBy: fromStartRefreshMethod ? 'startRefresh' : 'drag'
138
- };
139
- runOnBackground(onStartRefreshJS)(refreshInfo);
140
- }
141
- function onHeaderReleasedJS(info) {
142
- if (enableRefresh && onHeaderReleased) onHeaderReleased(info);
143
- }
144
- function sendHeaderReleasedEvent() {
145
- 'main thread';
146
- if (bouncingPositionInfo.current.bouncingOffset > 0) {
147
- if (refreshHeaderSize.current < bouncingPositionInfo.current.bouncingOffset) setRefreshState(RefreshState.OVER_DRAG_RELEASE);
148
- const refreshReleaseInfo = {
149
- offset: bouncingPositionInfo.current.bouncingOffset ?? 0,
150
- headerSize: refreshHeaderSize.current
151
- };
152
- runOnBackground(onHeaderReleasedJS)(refreshReleaseInfo);
153
- }
154
- }
155
- function onRefreshOffsetChangeJS(info) {
156
- if (enableRefresh && onRefreshOffsetChange) onRefreshOffsetChange(info);
157
- }
158
- function sendHeaderOffsetChangeEvent() {
159
- 'main thread';
160
- if (bouncingPositionInfo.current.bouncingOffset > 0) {
161
- const refreshOffsetChangeInfo = {
162
- offset: bouncingPositionInfo.current.bouncingOffset ?? 0,
163
- headerSize: refreshHeaderSize.current,
164
- isDragging: null !== startTouchEvent.current
165
- };
166
- runOnBackground(onRefreshOffsetChangeJS)(refreshOffsetChangeInfo);
167
- }
168
- }
169
- function getBouncingStatus() {
170
- 'main thread';
171
- const currentPosition = bouncingPositionInfo.current?.bouncingOffset ?? 0;
172
- if (Boolean(toUpper.current) && Boolean(toLower.current)) return alwaysBouncing ? bouncingStatus.alwaysBouncing : bouncingStatus.noBouncing;
173
- const initialLayoutPosition = 0;
174
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('getBouncingStatus', initialLayoutPosition, 'currentPosition', currentPosition);
175
- if (currentPosition > initialLayoutPosition) return bouncingStatus.upperBouncing;
176
- if (currentPosition < initialLayoutPosition) return bouncingStatus.lowerBouncing;
177
- return bouncingStatus.inScrollingRange;
178
- }
179
- function shouldBounceWhenTouchEnd(status) {
180
- 'main thread';
181
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('shouldBounceWhenTouchEnd', status);
182
- if (status === bouncingStatus.inScrollingRange || status === bouncingStatus.noBouncing) return false;
183
- if (status === bouncingStatus.upperBouncing || status === bouncingStatus.lowerBouncing) return true;
184
- if (status === bouncingStatus.alwaysBouncing) return alwaysBouncing;
185
- return false;
186
- }
187
- function releasedOverHeader() {
188
- 'main thread';
189
- if (bouncingPositionInfo.current.bouncingOffset > refreshHeaderSize.current) return true;
190
- }
191
- function shouldBounceBackWhenRefresh(status) {
192
- 'main thread';
193
- if (!enableRefresh) return false;
194
- if ((status === bouncingStatus.upperBouncing || status === bouncingStatus.alwaysBouncing) && releasedOverHeader()) return true;
195
- return false;
196
- }
197
- function bouncingSetStyle(offset) {
198
- 'main thread';
199
- if (isNaN(Number(offset))) return void console.error('ERROR! bouncingOffset is NaN');
200
- if (isEmpty(bouncingPositionInfo.current)) bouncingPositionInfo.current.velocity = 0;
201
- else {
202
- const timeDuration = Date.now() - bouncingPositionInfo.current.timeStamp;
203
- const offsetDiff = offset - Number(bouncingPositionInfo.current?.bouncingOffset ?? 0);
204
- if (timeDuration > 0) bouncingPositionInfo.current.velocity = offsetDiff / timeDuration;
205
- }
206
- bouncingPositionInfo.current.bouncingOffset = offset;
207
- bouncingPositionInfo.current.timeStamp = Date.now();
208
- if (0 === offset && null !== prevTouchEvent.current) {
209
- startTouchEvent.current = prevTouchEvent.current;
210
- bouncingTouchStartPosition.current = {};
211
- }
212
- if (isVertical()) {
213
- selectorMT(containerID)?.setStyleProperty('transform', `translateY(${offset}px)`);
214
- if (refreshDebugLog.current) console.info('bouncingSetStyle', `translateY(${offset}px)`);
215
- if (enableRefresh) selectorMT(`${containerID}-refreshHeaderWrapper`)?.setStyleProperty('transform', `translateY(${offset}px)`);
216
- selectorMT(`${containerID}-refreshFooterWrapper`)?.setStyleProperty('transform', `translateY(${offset}px)`);
217
- selectorMT(`${containerID}-lowerBounceWrapper`)?.setStyleProperty('transform', `translateY(${offset}px)`);
218
- } else {
219
- selectorMT(containerID)?.setStyleProperty('transform', `translateX(${offset}px)`);
220
- selectorMT(`${containerID}-upperBounceWrapper`)?.setStyleProperty('transform', `translateX(${offset}px)`);
221
- selectorMT(`${containerID}-lowerBounceWrapper`)?.setStyleProperty('transform', `translateX(${offset}px)`);
222
- }
223
- if (isVertical() && enableRefresh) sendHeaderOffsetChangeEvent();
224
- }
225
- function onScrollToBouncesJS(info) {
226
- if (enableBounces && onScrollToBounces) onScrollToBounces(info);
227
- }
228
- function triggerScrollToBouncesEvent(isUpper) {
229
- 'main thread';
230
- if (bounceableDebugLog.current) console.info('triggerScrollToBouncesEvent', isUpper ? 'upper' : 'lower');
231
- const info = {
232
- direction: isUpper ? 'upper' : 'lower'
233
- };
234
- runOnBackground(onScrollToBouncesJS)(info);
235
- }
236
- function rubberEffect(isNegative, delta) {
237
- 'main thread';
238
- const scrollViewFrameSize = isVertical() ? height.current : width.current;
239
- const touchStartPosition = Math.abs(Number(isEmpty(startTouchBouncingDelta.current) ? bouncingTouchStartPosition.current : startTouchBouncingDelta.current));
240
- const deltaYForTouchStartPosition = scrollViewFrameSize * touchStartPosition / ((scrollViewFrameSize - touchStartPosition) * rubberC.current) || 0;
241
- const deltaYForNextPosition = bouncingTouchStartPosition.current ? deltaYForTouchStartPosition + isNegative * delta : isNegative * delta;
242
- const rubberBandingDistance = Math.max(0, (1.0 - 1.0 / (deltaYForNextPosition * rubberC.current / scrollViewFrameSize + 1.0)) * scrollViewFrameSize);
243
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('rubberEffect', 'scrollContainerFrameSize', scrollViewFrameSize, 'touchStartPosition', touchStartPosition, 'deltaYForTouchStartPosition', deltaYForTouchStartPosition, 'deltaYForNextPosition', deltaYForNextPosition, 'rubberBandingDistance', rubberBandingDistance);
244
- setRefreshState(RefreshState.DRAGGING);
245
- bouncingSetStyle(isNegative * rubberBandingDistance);
246
- }
247
- function onRefreshHeaderLayoutUpdated(event) {
248
- 'main thread';
249
- if (refreshDebugLog.current) console.info('onRefreshHeaderLayoutUpdated', event.detail);
250
- refreshHeaderSize.current = isAndroid() ? event.params?.height : event.detail?.height;
251
- }
252
- function triggerRubberEffectIfCrossingEdge(event) {
253
- 'main thread';
254
- const delta = getCurrentDelta(event) || 0;
255
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('isCrossingEdge', 'delta', delta, 'toUpper', toUpper.current, 'toLower', toLower.current);
256
- if (true === toUpper.current && delta > 0) {
257
- enableScroll(false);
258
- rubberEffect(1, delta);
259
- }
260
- if (true === toLower.current && delta < 0) {
261
- enableScroll(false);
262
- rubberEffect(-1, delta);
263
- }
264
- enableScroll(true);
265
- }
266
- function isOverTriggerDistance() {
267
- 'main thread';
268
- const bouncingTop = bouncingPositionInfo.current.bouncingOffset ?? 0;
269
- const triggerDistance = bouncingPositionInfo.current?.bouncingOffset > 0 ? startBounceTriggerDistance : endBounceTriggerDistance;
270
- const delta = Math.abs(bouncingTop) - Math.abs(triggerDistance);
271
- return delta > threshold();
272
- }
273
- function bouncingBack(mode) {
274
- 'main thread';
275
- if (bounceableDebugLog.current || refreshDebugLog.current) {
276
- console.info('fling forward end');
277
- console.info('bounce back starts');
278
- }
279
- const C1 = bouncingPositionInfo.current.bouncingOffset ?? 0;
280
- const startTime = Date.now();
281
- const touchingEndBouncingBackFrame = ()=>{
282
- if (false === Boolean(touchingEndBouncingBackEnableFlag.current)) return;
283
- const currentTime = Date.now() - startTime;
284
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('touchingEndBouncingBackFrame');
285
- const C2 = beta.current * C1;
286
- const easedDistance = (C1 + currentTime / 1000 * C2) * Math.pow(Math.E, -beta.current * (currentTime / 1000));
287
- bouncingSetStyle(easedDistance);
288
- if (Math.abs(easedDistance) < bouncingBackThreshold(mode)) {
289
- if (mode === bouncingBackMode.clampToHeader) {
290
- isDuringRefresh.current = true;
291
- sendStartRefreshEvent(false);
292
- setRefreshState(RefreshState.REFRESHING);
293
- }
294
- bouncingSetStyle(bouncingBackTargetPosition(mode));
295
- if (mode === bouncingBackMode.resetToBoundary) enableScroll(true);
296
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('bounce back ends');
297
- } else customRequestAnimationFrame(touchingEndBouncingBackFrame);
298
- };
299
- customRequestAnimationFrame(touchingEndBouncingBackFrame);
300
- touchingEndBouncingBackEnableFlag.current = true;
301
- }
302
- function bounceableTouchStart(event) {
303
- 'main thread';
304
- startTouchEvent.current = event;
305
- prevTouchEvent.current = event;
306
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('bounceableTouchStart');
307
- bouncingTouchStartPosition.current = bouncingPositionInfo.current?.bouncingOffset ?? 0;
308
- touchEndFrameEnableFlag.current = false;
309
- touchingEndBouncingBackEnableFlag.current = false;
310
- flingEndWithBouncingEnableFlag.current = false;
311
- }
312
- function bounceableTouchMove(event) {
313
- 'main thread';
314
- if (null === startTouchEvent.current) return void console.error('ERROR! touch not started');
315
- prevTouchEvent.current = event;
316
- const delta = getCurrentDelta(event);
317
- const currentBouncingStatus = getBouncingStatus();
318
- switch(currentBouncingStatus){
319
- case bouncingStatus.upperBouncing:
320
- enableScroll(false);
321
- rubberEffect(1, delta);
322
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('bounceableTouchMove upperBouncing');
323
- break;
324
- case bouncingStatus.lowerBouncing:
325
- enableScroll(false);
326
- rubberEffect(-1, delta);
327
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('bounceableTouchMove lowerBouncing');
328
- break;
329
- case bouncingStatus.alwaysBouncing:
330
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('bounceableTouchMove alwaysBouncing');
331
- if (alwaysBouncing) triggerRubberEffectIfCrossingEdge(event);
332
- break;
333
- case bouncingStatus.inScrollingRange:
334
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('bounceableTouchMove inScrollingRange');
335
- triggerRubberEffectIfCrossingEdge(event);
336
- break;
337
- default:
338
- break;
339
- }
340
- }
341
- function bouncingBackEntrance(mode) {
342
- 'main thread';
343
- const startTime = Date.now();
344
- const dragEndVelocity = bouncingPositionInfo.current?.velocity ?? 0;
345
- const dragEndPosition = bouncingPositionInfo.current?.bouncingOffset ?? 0;
346
- if (isOverTriggerDistance()) triggerScrollToBouncesEvent(bouncingPositionInfo.current?.bouncingOffset > 0);
347
- let currentVelocity = dragEndVelocity;
348
- const touchEndProcessFrame = ()=>{
349
- if (false === Boolean(touchEndFrameEnableFlag.current)) return;
350
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('touchEndProcessFrame');
351
- if (Math.abs(currentVelocity) <= threshold()) bouncingBack(mode);
352
- else {
353
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('fling down start');
354
- const distance = dragEndPosition + dragEndVelocity * (Math.pow(flingDeceleratingRate.current, Date.now() - startTime) - 1) / Math.log(flingDeceleratingRate.current);
355
- bouncingSetStyle(distance);
356
- currentVelocity = dragEndVelocity * Math.pow(flingDeceleratingRate.current, Date.now() - startTime);
357
- if (touchEndFrameEnableFlag.current) customRequestAnimationFrame(touchEndProcessFrame);
358
- }
359
- };
360
- customRequestAnimationFrame(touchEndProcessFrame);
361
- touchEndFrameEnableFlag.current = true;
362
- }
363
- function bounceableTouchEnd() {
364
- 'main thread';
365
- if (enableRefresh) sendHeaderReleasedEvent();
366
- clearTouchInfo();
367
- enableScroll(true);
368
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('bounceableTouchEnd');
369
- if (shouldBounceBackWhenRefresh(getBouncingStatus())) bouncingBackEntrance(bouncingBackMode.clampToHeader);
370
- else if (shouldBounceWhenTouchEnd(getBouncingStatus()) && !shouldBounceBackWhenRefresh(getBouncingStatus())) bouncingBackEntrance(bouncingBackMode.resetToBoundary);
371
- }
372
- function bounceableHandleScroll(event) {
373
- 'main thread';
374
- scrollX.current = event.detail.scrollLeft;
375
- scrollY.current = event.detail.scrollTop;
376
- if (!isEmpty(prevScroll.current)) {
377
- const timeDuration = Date.now() - prevScroll.current.timestamp;
378
- if (timeDuration > 0) {
379
- const deltaY = isVertical() ? event.detail.scrollTop - prevScroll.current.detail.scrollTop : event.detail.scrollLeft - prevScroll.current.detail.scrollLeft;
380
- const velocity = deltaY / timeDuration;
381
- if (Number.isFinite(velocity)) scrollVelocity.current = velocity;
382
- }
383
- }
384
- prevScroll.current = event;
385
- }
386
- function finishRefresh() {
387
- 'main thread';
388
- if (enableRefresh) {
389
- isDuringRefresh.current = false;
390
- bouncingBack(bouncingBackMode.resetToBoundary);
391
- setRefreshState(RefreshState.IDLE);
392
- }
393
- }
394
- function startRefreshMethod() {
395
- 'main thread';
396
- if (enableRefresh) {
397
- isDuringRefresh.current = true;
398
- const scrollToZero = selectorMT(containerID)?.invoke('scrollToPosition', {
399
- position: 0,
400
- smooth: false,
401
- alignTo: 'top',
402
- useScroller: true
403
- });
404
- scrollToZero?.then(()=>{
405
- bouncingSetStyle(refreshHeaderSize.current);
406
- setRefreshState(RefreshState.OVER_DRAG_RELEASE);
407
- sendStartRefreshEvent(true);
408
- });
409
- }
410
- }
411
- function handleBounceEventInFling() {
412
- 'main thread';
413
- const startVelocity = scrollVelocity.current;
414
- let currentVelocity = startVelocity;
415
- const startTime = Date.now();
416
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('fling to edge event start with velocity', startVelocity);
417
- let sentScrollToBouncesEvent = false;
418
- const flingEndWithBouncingFrame = ()=>{
419
- if (false === Boolean(flingEndWithBouncingEnableFlag.current)) {
420
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('flingEndWithBouncingEnableFlag early return');
421
- return;
422
- }
423
- if (Math.abs(currentVelocity) <= threshold()) {
424
- flingEndWithBouncingEnableFlag.current = false;
425
- if (bounceableDebugLog.current || refreshDebugLog.current) {
426
- console.info('fling event end');
427
- console.info('bouncing event start');
428
- }
429
- bouncingBack(bouncingBackMode.resetToBoundary);
430
- } else {
431
- const distance = currentVelocity * (Math.pow(flingDeceleratingRate.current, Date.now() - startTime) - 1) / Math.log(flingDeceleratingRate.current);
432
- bouncingSetStyle(-distance);
433
- if (!sentScrollToBouncesEvent && isOverTriggerDistance()) {
434
- triggerScrollToBouncesEvent(bouncingPositionInfo.current?.bouncingOffset > 0);
435
- sentScrollToBouncesEvent = true;
436
- }
437
- currentVelocity = startVelocity * Math.pow(flingDeceleratingRate.current, Date.now() - startTime);
438
- if (flingEndWithBouncingEnableFlag.current) customRequestAnimationFrame(flingEndWithBouncingFrame);
439
- }
440
- };
441
- customRequestAnimationFrame(flingEndWithBouncingFrame);
442
- flingEndWithBouncingEnableFlag.current = true;
443
- }
444
- function onUpperDisexposure() {
445
- 'main thread';
446
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('upper disexposure');
447
- toUpper.current = false;
448
- }
449
- function onUpperExposure() {
450
- 'main thread';
451
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('upper exposure');
452
- toUpper.current = true;
453
- if (prevTouchEvent.current) {
454
- startBouncingTouchEvent.current = prevTouchEvent.current;
455
- startTouchBouncingDelta.current = bouncingPositionInfo.current?.bouncingOffset ?? 0;
456
- } else if (enableBounceEventInFling && Math.abs(scrollVelocity.current) > threshold()) handleBounceEventInFling();
457
- scrollVelocity.current = 0;
458
- }
459
- function onLowerDisexposure() {
460
- 'main thread';
461
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('lower disexposure', scrollVelocity.current);
462
- toLower.current = false;
463
- }
464
- function onLowerExposure() {
465
- 'main thread';
466
- if (bounceableDebugLog.current || refreshDebugLog.current) console.info('lower exposure');
467
- toLower.current = true;
468
- if (prevTouchEvent.current) {
469
- startBouncingTouchEvent.current = prevTouchEvent.current;
470
- startTouchBouncingDelta.current = bouncingPositionInfo.current?.bouncingOffset ?? 0;
471
- } else if (enableBounceEventInFling && Math.abs(scrollVelocity.current) > threshold()) handleBounceEventInFling();
472
- scrollVelocity.current = 0;
473
- }
474
- function bounceableLayoutComplete() {
475
- 'main thread';
476
- clearTouchInfo();
477
- }
478
- function bounceableLayoutChange(event) {
479
- 'main thread';
480
- height.current = (isAndroid() ? event.params?.height : event.detail?.height) ?? 0;
481
- width.current = (isAndroid() ? event.params?.width : event.detail?.width) ?? 0;
482
- }
483
- const isHorizontalTouchMove = (event)=>{
484
- 'main thread';
485
- if (!prevTouchEvent.current) return;
486
- const absDeltaX = Math.abs(event.params.clientX - prevTouchEvent.current.params.clientX);
487
- const absDeltaY = Math.abs(event.params.clientY - prevTouchEvent.current.params.clientY);
488
- if (absDeltaX > absDeltaY) return true;
489
- return false;
490
- };
491
- const refreshAndBounceGesture = useGesture(NativeGesture);
492
- const isFirstJudge = useMainThreadRef(true);
493
- refreshAndBounceGesture.onBegin((event, gestureManager)=>{
494
- 'main thread';
495
- isFirstJudge.current = true;
496
- bounceableTouchStart(event);
497
- if (mtsNativeLynxSDKVersionLessThan('3.3')) gestureManager.consumeGesture(true);
498
- });
499
- refreshAndBounceGesture.onEnd(()=>{
500
- 'main thread';
501
- isFirstJudge.current = true;
502
- bounceableTouchEnd();
503
- });
504
- refreshAndBounceGesture.onTouchesCancel(()=>{
505
- 'main thread';
506
- isFirstJudge.current = true;
507
- bounceableTouchEnd();
508
- });
509
- refreshAndBounceGesture.onUpdate((event, gestureManager)=>{
510
- 'main thread';
511
- if (enableRefresh || enableBounces && 'vertical' === scrollOrientation) {
512
- if (isFirstJudge.current) if (mtsNativeLynxSDKVersionLessThan('3.3')) if (isHorizontalTouchMove(event)) {
513
- gestureManager.consumeGesture(false);
514
- gestureManager.end();
515
- } else gestureManager.consumeGesture(true);
516
- else {
517
- const deltaY = prevTouchEvent.current ? event.params.clientY - prevTouchEvent.current.params.clientY : 0;
518
- if (!isHorizontalTouchMove(event) && toUpper.current && deltaY > 0) gestureManager.interceptGesture(true);
519
- else gestureManager.interceptGesture(false);
520
- }
521
- else if (!isHorizontalTouchMove(event)) bounceableTouchMove(event);
522
- } else if (enableBounces && 'horizontal' === scrollOrientation) {
523
- if (mtsNativeLynxSDKVersionLessThan('3.3')) {
524
- if (isFirstJudge.current) if (isHorizontalTouchMove(event)) gestureManager.consumeGesture(true);
525
- else {
526
- gestureManager.consumeGesture(false);
527
- gestureManager.end();
528
- }
529
- else if (isHorizontalTouchMove(event)) bounceableTouchMove(event);
530
- } else if (isHorizontalTouchMove(event)) {
531
- gestureManager.interceptGesture(false);
532
- bounceableTouchMove(event);
533
- }
534
- }
535
- prevTouchEvent.current = event;
536
- isFirstJudge.current = false;
537
- });
538
- return {
539
- refreshAndBounceGesture,
540
- 'main-thread:onLayoutChange': bounceableLayoutChange,
541
- 'main-thread:onScroll': bounceableHandleScroll,
542
- 'main-thread:onLayoutComplete': bounceableLayoutComplete,
543
- onUpperExposure,
544
- onUpperDisexposure,
545
- onLowerExposure,
546
- onLowerDisexposure,
547
- onRefreshHeaderLayoutUpdated,
548
- finishRefresh,
549
- startRefreshMethod
550
- };
551
- }
552
- export { useRefreshAndBounce };
553
-
554
- //# sourceMappingURL=useRefresh.jsx.map