@lynx-js/lynx-ui-common 0.0.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/LICENSE +201 -0
- package/README.md +28 -0
- package/dist/const/BaseScrollEventsMapping.d.ts +2 -0
- package/dist/const/BaseScrollEventsMapping.d.ts.map +1 -0
- package/dist/const/BaseScrollEventsMapping.jsx +13 -0
- package/dist/const/BaseScrollEventsMapping.jsx.map +1 -0
- package/dist/const/BaseUIEventsMapping.d.ts +4 -0
- package/dist/const/BaseUIEventsMapping.d.ts.map +1 -0
- package/dist/const/BaseUIEventsMapping.jsx +24 -0
- package/dist/const/BaseUIEventsMapping.jsx.map +1 -0
- package/dist/const/index.d.ts +3 -0
- package/dist/const/index.d.ts.map +1 -0
- package/dist/const/index.jsx +2 -0
- package/dist/hooks/docs/useBounce.jsx +0 -0
- package/dist/hooks/docs/useRefresh.jsx +0 -0
- package/dist/hooks/index.d.ts +19 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.jsx +73 -0
- package/dist/hooks/index.jsx.map +1 -0
- package/dist/hooks/useBounce.d.ts +26 -0
- package/dist/hooks/useBounce.d.ts.map +1 -0
- package/dist/hooks/useBounce.jsx +383 -0
- package/dist/hooks/useBounce.jsx.map +1 -0
- package/dist/hooks/useGCCollector.d.ts +18 -0
- package/dist/hooks/useGCCollector.d.ts.map +1 -0
- package/dist/hooks/useGCCollector.jsx +48 -0
- package/dist/hooks/useGCCollector.jsx.map +1 -0
- package/dist/hooks/useGlobalEventListener.d.ts +9 -0
- package/dist/hooks/useGlobalEventListener.d.ts.map +1 -0
- package/dist/hooks/useGlobalEventListener.jsx +19 -0
- package/dist/hooks/useGlobalEventListener.jsx.map +1 -0
- package/dist/hooks/useLatest.d.ts +6 -0
- package/dist/hooks/useLatest.d.ts.map +1 -0
- package/dist/hooks/useLatest.jsx +9 -0
- package/dist/hooks/useLatest.jsx.map +1 -0
- package/dist/hooks/useMemoizedFn.d.ts +8 -0
- package/dist/hooks/useMemoizedFn.d.ts.map +1 -0
- package/dist/hooks/useMemoizedFn.jsx +15 -0
- package/dist/hooks/useMemoizedFn.jsx.map +1 -0
- package/dist/hooks/usePrevious.d.ts +16 -0
- package/dist/hooks/usePrevious.d.ts.map +1 -0
- package/dist/hooks/usePrevious.jsx +11 -0
- package/dist/hooks/usePrevious.jsx.map +1 -0
- package/dist/hooks/useRefresh.d.ts +24 -0
- package/dist/hooks/useRefresh.d.ts.map +1 -0
- package/dist/hooks/useRefresh.jsx +554 -0
- package/dist/hooks/useRefresh.jsx.map +1 -0
- package/dist/hooks/useRegisteredEvents.d.ts +19 -0
- package/dist/hooks/useRegisteredEvents.d.ts.map +1 -0
- package/dist/hooks/useRegisteredEvents.jsx +24 -0
- package/dist/hooks/useRegisteredEvents.jsx.map +1 -0
- package/dist/hooks/useUnmount.d.ts +2 -0
- package/dist/hooks/useUnmount.d.ts.map +1 -0
- package/dist/hooks/useUnmount.jsx +11 -0
- package/dist/hooks/useUnmount.jsx.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.jsx +6 -0
- package/dist/reactive/hooks/useReactiveValue.d.ts +22 -0
- package/dist/reactive/hooks/useReactiveValue.d.ts.map +1 -0
- package/dist/reactive/hooks/useReactiveValue.jsx +75 -0
- package/dist/reactive/hooks/useReactiveValue.jsx.map +1 -0
- package/dist/reactive/hooks/useReactiveValueChange.d.ts +4 -0
- package/dist/reactive/hooks/useReactiveValueChange.d.ts.map +1 -0
- package/dist/reactive/hooks/useReactiveValueChange.jsx +7 -0
- package/dist/reactive/hooks/useReactiveValueChange.jsx.map +1 -0
- package/dist/reactive/hooks/useReactiveValueEvent.d.ts +5 -0
- package/dist/reactive/hooks/useReactiveValueEvent.d.ts.map +1 -0
- package/dist/reactive/hooks/useReactiveValueEvent.jsx +15 -0
- package/dist/reactive/hooks/useReactiveValueEvent.jsx.map +1 -0
- package/dist/reactive/index.d.ts +6 -0
- package/dist/reactive/index.d.ts.map +1 -0
- package/dist/reactive/index.jsx +5 -0
- package/dist/reactive/types.d.ts +17 -0
- package/dist/reactive/types.d.ts.map +1 -0
- package/dist/reactive/types.jsx +0 -0
- package/dist/reactive/updateReactive.d.ts +4 -0
- package/dist/reactive/updateReactive.d.ts.map +1 -0
- package/dist/reactive/updateReactive.jsx +8 -0
- package/dist/reactive/updateReactive.jsx.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.jsx +0 -0
- package/dist/types/interfaces/BaseScrollEvents.d.ts +40 -0
- package/dist/types/interfaces/BaseScrollEvents.d.ts.map +1 -0
- package/dist/types/interfaces/BaseScrollEvents.jsx +0 -0
- package/dist/types/interfaces/BaseUIEvents.d.ts +132 -0
- package/dist/types/interfaces/BaseUIEvents.d.ts.map +1 -0
- package/dist/types/interfaces/BaseUIEvents.jsx +0 -0
- package/dist/types/interfaces/BaseUIProps.d.ts +19 -0
- package/dist/types/interfaces/BaseUIProps.d.ts.map +1 -0
- package/dist/types/interfaces/BaseUIProps.jsx +0 -0
- package/dist/types/interfaces/BounceableInterface.d.ts +120 -0
- package/dist/types/interfaces/BounceableInterface.d.ts.map +1 -0
- package/dist/types/interfaces/BounceableInterface.jsx +0 -0
- package/dist/types/interfaces/EventMTSInterface.d.ts +1 -0
- package/dist/types/interfaces/EventMTSInterface.d.ts.map +1 -0
- package/dist/types/interfaces/EventMTSInterface.jsx +0 -0
- package/dist/types/interfaces/LazyInterface.d.ts +72 -0
- package/dist/types/interfaces/LazyInterface.d.ts.map +1 -0
- package/dist/types/interfaces/LazyInterface.jsx +0 -0
- package/dist/types/interfaces/RefreshInterface.d.ts +98 -0
- package/dist/types/interfaces/RefreshInterface.d.ts.map +1 -0
- package/dist/types/interfaces/RefreshInterface.jsx +10 -0
- package/dist/types/interfaces/RefreshInterface.jsx.map +1 -0
- package/dist/types/interfaces/index.d.ts +36 -0
- package/dist/types/interfaces/index.d.ts.map +1 -0
- package/dist/types/interfaces/index.jsx +0 -0
- package/dist/utils/common.d.ts +15 -0
- package/dist/utils/common.d.ts.map +1 -0
- package/dist/utils/common.jsx +12 -0
- package/dist/utils/common.jsx.map +1 -0
- package/dist/utils/common.test.jsx +55 -0
- package/dist/utils/common.test.jsx.map +1 -0
- package/dist/utils/convertToPx.d.ts +4 -0
- package/dist/utils/convertToPx.d.ts.map +1 -0
- package/dist/utils/convertToPx.jsx +20 -0
- package/dist/utils/convertToPx.jsx.map +1 -0
- package/dist/utils/delayFrames.d.ts +2 -0
- package/dist/utils/delayFrames.d.ts.map +1 -0
- package/dist/utils/delayFrames.jsx +12 -0
- package/dist/utils/delayFrames.jsx.map +1 -0
- package/dist/utils/getEventDetail.d.ts +23 -0
- package/dist/utils/getEventDetail.d.ts.map +1 -0
- package/dist/utils/getEventDetail.jsx +7 -0
- package/dist/utils/getEventDetail.jsx.map +1 -0
- package/dist/utils/index.d.ts +13 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.jsx +12 -0
- package/dist/utils/interpolation.d.ts +40 -0
- package/dist/utils/interpolation.d.ts.map +1 -0
- package/dist/utils/interpolation.jsx +86 -0
- package/dist/utils/interpolation.jsx.map +1 -0
- package/dist/utils/interpolationJS.d.ts +39 -0
- package/dist/utils/interpolationJS.d.ts.map +1 -0
- package/dist/utils/interpolationJS.jsx +80 -0
- package/dist/utils/interpolationJS.jsx.map +1 -0
- package/dist/utils/log.d.ts +3 -0
- package/dist/utils/log.d.ts.map +1 -0
- package/dist/utils/log.jsx +10 -0
- package/dist/utils/log.jsx.map +1 -0
- package/dist/utils/mainThreadify.d.ts +2 -0
- package/dist/utils/mainThreadify.d.ts.map +1 -0
- package/dist/utils/mainThreadify.jsx +11 -0
- package/dist/utils/mainThreadify.jsx.map +1 -0
- package/dist/utils/popoverUtils.d.ts +3 -0
- package/dist/utils/popoverUtils.d.ts.map +1 -0
- package/dist/utils/popoverUtils.jsx +12 -0
- package/dist/utils/popoverUtils.jsx.map +1 -0
- package/dist/utils/renderContentWithExtraProps.d.ts +5 -0
- package/dist/utils/renderContentWithExtraProps.d.ts.map +1 -0
- package/dist/utils/renderContentWithExtraProps.jsx +10 -0
- package/dist/utils/renderContentWithExtraProps.jsx.map +1 -0
- package/dist/utils/selector.d.ts +36 -0
- package/dist/utils/selector.d.ts.map +1 -0
- package/dist/utils/selector.jsx +85 -0
- package/dist/utils/selector.jsx.map +1 -0
- package/dist/utils/selector.test.jsx +215 -0
- package/dist/utils/selector.test.jsx.map +1 -0
- package/dist/utils/version.d.ts +6 -0
- package/dist/utils/version.d.ts.map +1 -0
- package/dist/utils/version.jsx +28 -0
- package/dist/utils/version.jsx.map +1 -0
- package/dist/utils/version.test.jsx +77 -0
- package/dist/utils/version.test.jsx.map +1 -0
- package/package.json +52 -0
- package/src/const/BaseScrollEventsMapping.ts +14 -0
- package/src/const/BaseUIEventsMapping.ts +25 -0
- package/src/const/index.ts +6 -0
- package/src/hooks/docs/useBounce.mdx +2 -0
- package/src/hooks/docs/useRefresh.mdx +2 -0
- package/src/hooks/index.ts +96 -0
- package/src/hooks/useBounce.tsx +814 -0
- package/src/hooks/useGCCollector.tsx +76 -0
- package/src/hooks/useGlobalEventListener.ts +50 -0
- package/src/hooks/useLatest.ts +16 -0
- package/src/hooks/useMemoizedFn.ts +35 -0
- package/src/hooks/usePrevious.ts +28 -0
- package/src/hooks/useRefresh.ts +1054 -0
- package/src/hooks/useRegisteredEvents.ts +69 -0
- package/src/hooks/useUnmount.ts +18 -0
- package/src/index.tsx +25 -0
- package/src/reactive/hooks/useReactiveValue.ts +132 -0
- package/src/reactive/hooks/useReactiveValueChange.ts +15 -0
- package/src/reactive/hooks/useReactiveValueEvent.ts +26 -0
- package/src/reactive/index.ts +20 -0
- package/src/reactive/types.ts +29 -0
- package/src/reactive/updateReactive.ts +16 -0
- package/src/types/index.ts +5 -0
- package/src/types/interfaces/BaseScrollEvents.ts +47 -0
- package/src/types/interfaces/BaseUIEvents.ts +142 -0
- package/src/types/interfaces/BaseUIProps.ts +22 -0
- package/src/types/interfaces/BounceableInterface.ts +125 -0
- package/src/types/interfaces/EventMTSInterface.ts +35 -0
- package/src/types/interfaces/LazyInterface.tsx +78 -0
- package/src/types/interfaces/RefreshInterface.ts +105 -0
- package/src/types/interfaces/index.ts +44 -0
- package/src/utils/common.test.ts +64 -0
- package/src/utils/common.ts +35 -0
- package/src/utils/convertToPx.ts +32 -0
- package/src/utils/delayFrames.ts +24 -0
- package/src/utils/getEventDetail.ts +36 -0
- package/src/utils/index.ts +16 -0
- package/src/utils/interpolation.ts +222 -0
- package/src/utils/interpolationJS.ts +212 -0
- package/src/utils/log.ts +16 -0
- package/src/utils/mainThreadify.ts +17 -0
- package/src/utils/popoverUtils.ts +25 -0
- package/src/utils/renderContentWithExtraProps.ts +20 -0
- package/src/utils/selector.test.ts +237 -0
- package/src/utils/selector.ts +222 -0
- package/src/utils/version.test.ts +98 -0
- package/src/utils/version.ts +50 -0
- package/tsconfig.docs.json +7 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type noop = (this: any, ...args: any[]) => any;
|
|
2
|
+
/**
|
|
3
|
+
* Hooks for persistent functions. In general, useMemoizedFn can be used instead of useCallback.
|
|
4
|
+
* In some scenarios, we need to use useCallback to cache a function, but when the second parameter deps changes, the function will be regenerated, causing the function reference to change.
|
|
5
|
+
*/
|
|
6
|
+
declare function useMemoizedFn<T extends noop>(fn: T): T;
|
|
7
|
+
export { useMemoizedFn };
|
|
8
|
+
//# sourceMappingURL=useMemoizedFn.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMemoizedFn.d.ts","sourceRoot":"","sources":["../../src/hooks/useMemoizedFn.ts"],"names":[],"mappings":"AAOA,KAAK,IAAI,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAA;AAO9C;;;GAGG;AACH,iBAAS,aAAa,CAAC,CAAC,SAAS,IAAI,EAAE,EAAE,EAAE,CAAC,GAaF,CAAC,CAC1C;AAED,OAAO,EAAE,aAAa,EAAE,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useMemo, useRef } from "@lynx-js/react";
|
|
2
|
+
function useMemoizedFn(fn) {
|
|
3
|
+
const fnRef = useRef(fn);
|
|
4
|
+
fnRef.current = useMemo(()=>fn, [
|
|
5
|
+
fn
|
|
6
|
+
]);
|
|
7
|
+
const memoizedFn = useRef();
|
|
8
|
+
memoizedFn.current ??= function(...args) {
|
|
9
|
+
return fnRef.current.apply(this, args);
|
|
10
|
+
};
|
|
11
|
+
return memoizedFn.current;
|
|
12
|
+
}
|
|
13
|
+
export { useMemoizedFn };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=useMemoizedFn.jsx.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks/useMemoizedFn.jsx","sources":["webpack://@lynx-js/lynx-ui-common/./src/hooks/useMemoizedFn.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { useMemo, useRef } from '@lynx-js/react'\n\n// biome-ignore lint/suspicious/noExplicitAny: generic type definition\ntype noop = (this: any, ...args: any[]) => any\n\ntype PickFunction<T extends noop> = (\n this: ThisParameterType<T>,\n ...args: Parameters<T>\n) => ReturnType<T>\n\n/**\n * Hooks for persistent functions. In general, useMemoizedFn can be used instead of useCallback.\n * In some scenarios, we need to use useCallback to cache a function, but when the second parameter deps changes, the function will be regenerated, causing the function reference to change.\n */\nfunction useMemoizedFn<T extends noop>(fn: T) {\n const fnRef = useRef<T>(fn)\n\n // why not write `fnRef.current = fn`?\n // https://github.com/alibaba/hooks/issues/728\n fnRef.current = useMemo<T>(() => fn, [fn])\n\n const memoizedFn = useRef<PickFunction<T>>()\n memoizedFn.current ??= function(this, ...args) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return fnRef.current.apply(this, args)\n }\n\n return memoizedFn.current as unknown as T\n}\n\nexport { useMemoizedFn }\n"],"names":["useMemoizedFn","fn","fnRef","useRef","useMemo","memoizedFn","args"],"mappings":";AAkBA,SAASA,cAA8BC,EAAK;IAC1C,MAAMC,QAAQC,OAAUF;IAIxBC,MAAM,OAAO,GAAGE,QAAW,IAAMH,IAAI;QAACA;KAAG;IAEzC,MAAMI,aAAaF;IACnBE,WAAW,OAAO,KAAK,SAAe,GAAGC,IAAI;QAE3C,OAAOJ,MAAM,OAAO,CAAC,KAAK,CAAC,IAAI,EAAEI;IACnC;IAEA,OAAOD,WAAW,OAAO;AAC3B"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tracks previous state of a value.
|
|
3
|
+
*
|
|
4
|
+
* @param value Props, state or any other calculated value.
|
|
5
|
+
* @returns Value from the previous render of the enclosing component.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* function Component() {
|
|
9
|
+
* const [count, setCount] = useState(0);
|
|
10
|
+
* const prevCount = usePrevious(count);
|
|
11
|
+
* // ...
|
|
12
|
+
* return `Now: ${count}, before: ${prevCount}`;
|
|
13
|
+
* }
|
|
14
|
+
*/
|
|
15
|
+
export declare function usePrevious<T>(value: T): T | undefined;
|
|
16
|
+
//# sourceMappingURL=usePrevious.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePrevious.d.ts","sourceRoot":"","sources":["../../src/hooks/usePrevious.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS,CAOtD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks/usePrevious.jsx","sources":["webpack://@lynx-js/lynx-ui-common/./src/hooks/usePrevious.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { useEffect, useRef } from '@lynx-js/react'\n\n/**\n * Tracks previous state of a value.\n *\n * @param value Props, state or any other calculated value.\n * @returns Value from the previous render of the enclosing component.\n *\n * @example\n * function Component() {\n * const [count, setCount] = useState(0);\n * const prevCount = usePrevious(count);\n * // ...\n * return `Now: ${count}, before: ${prevCount}`;\n * }\n */\nexport function usePrevious<T>(value: T): T | undefined {\n // Source: https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state\n const ref = useRef<T>()\n useEffect(() => {\n ref.current = value\n })\n return ref.current\n}\n"],"names":["usePrevious","value","ref","useRef","useEffect"],"mappings":";AAoBO,SAASA,YAAeC,KAAQ;IAErC,MAAMC,MAAMC;IACZC,UAAU;QACRF,IAAI,OAAO,GAAGD;IAChB;IACA,OAAOC,IAAI,OAAO;AACpB"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { NativeGesture } from '@lynx-js/gesture-runtime';
|
|
2
|
+
import type { MainThread, ScrollEvent } from '@lynx-js/types';
|
|
3
|
+
import type { BounceableBasicProps, RefreshProps } from '../types';
|
|
4
|
+
export interface useRefreshAndBounceOptions {
|
|
5
|
+
refreshOptions: RefreshProps;
|
|
6
|
+
bounceableOptions: BounceableBasicProps;
|
|
7
|
+
id: string;
|
|
8
|
+
scrollOrientation: 'vertical' | 'horizontal';
|
|
9
|
+
}
|
|
10
|
+
export interface useRefreshAndBounceReturn {
|
|
11
|
+
refreshAndBounceGesture: NativeGesture;
|
|
12
|
+
'main-thread:onLayoutChange': (e: MainThread.LayoutChangeEvent) => void;
|
|
13
|
+
'main-thread:onScroll': (e: ScrollEvent) => void;
|
|
14
|
+
'main-thread:onLayoutComplete': () => void;
|
|
15
|
+
onUpperExposure: () => void;
|
|
16
|
+
onUpperDisexposure: () => void;
|
|
17
|
+
onLowerExposure: () => void;
|
|
18
|
+
onLowerDisexposure: () => void;
|
|
19
|
+
onRefreshHeaderLayoutUpdated: (e: MainThread.LayoutChangeEvent) => void;
|
|
20
|
+
finishRefresh: () => void;
|
|
21
|
+
startRefreshMethod: () => void;
|
|
22
|
+
}
|
|
23
|
+
export declare function useRefreshAndBounce(options: useRefreshAndBounceOptions): useRefreshAndBounceReturn;
|
|
24
|
+
//# sourceMappingURL=useRefresh.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRefresh.d.ts","sourceRoot":"","sources":["../../src/hooks/useRefresh.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,aAAa,EAAc,MAAM,0BAA0B,CAAA;AAEpE,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE7D,OAAO,KAAK,EACV,oBAAoB,EAEpB,YAAY,EAKb,MAAM,UAAU,CAAA;AAKjB,MAAM,WAAW,0BAA0B;IACzC,cAAc,EAAE,YAAY,CAAA;IAC5B,iBAAiB,EAAE,oBAAoB,CAAA;IACvC,EAAE,EAAE,MAAM,CAAA;IACV,iBAAiB,EAAE,UAAU,GAAG,YAAY,CAAA;CAC7C;AAED,MAAM,WAAW,yBAAyB;IACxC,uBAAuB,EAAE,aAAa,CAAA;IACtC,4BAA4B,EAAE,CAAC,CAAC,EAAE,UAAU,CAAC,iBAAiB,KAAK,IAAI,CAAA;IACvE,sBAAsB,EAAE,CAAC,CAAC,EAAE,WAAW,KAAK,IAAI,CAAA;IAChD,8BAA8B,EAAE,MAAM,IAAI,CAAA;IAC1C,eAAe,EAAE,MAAM,IAAI,CAAA;IAC3B,kBAAkB,EAAE,MAAM,IAAI,CAAA;IAC9B,eAAe,EAAE,MAAM,IAAI,CAAA;IAC3B,kBAAkB,EAAE,MAAM,IAAI,CAAA;IAC9B,4BAA4B,EAAE,CAAC,CAAC,EAAE,UAAU,CAAC,iBAAiB,KAAK,IAAI,CAAA;IACvE,aAAa,EAAE,MAAM,IAAI,CAAA;IACzB,kBAAkB,EAAE,MAAM,IAAI,CAAA;CAC/B;AAED,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,0BAA0B,GAClC,yBAAyB,CA++B3B"}
|
|
@@ -0,0 +1,554 @@
|
|
|
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
|