@doubao-apps/taro-runtime 0.0.27 → 0.0.28
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.
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
type ScrollViewProps
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { NodesRef } from '@lynx-js/types';
|
|
2
|
+
import type { ScrollViewProps as TaroScrollViewProps } from '@tarojs/components';
|
|
3
|
+
import { type ScrollViewEvent } from './map.js';
|
|
4
|
+
type NativeScrollViewEventProps = {
|
|
5
|
+
onContentSizeChanged?: (event: ScrollViewEvent) => void;
|
|
6
|
+
};
|
|
7
|
+
export declare const ScrollView: import("react").ForwardRefExoticComponent<Omit<TaroScrollViewProps & NativeScrollViewEventProps, "ref"> & import("react").RefAttributes<NodesRef>>;
|
|
6
8
|
export {};
|
|
7
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,4 +1,67 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { forwardRef, useCallback, useEffect, useRef } from '@byted-doubao-apps/framework';
|
|
2
|
+
import { Text } from '../Text/index.jsx';
|
|
3
|
+
import { mapScrollViewProps, toScrollViewEvent } from './map.js';
|
|
4
|
+
export const ScrollView = forwardRef(ScrollViewImpl);
|
|
5
|
+
function ScrollViewImpl(props, ref) {
|
|
6
|
+
const { children, onScrollToUpper, onScrollToLower, onScroll, onScrollEnd, onContentSizeChanged, ...nonEventProps } = props;
|
|
7
|
+
const { mapped, scrollViewId, scrollOffset, scrollWithAnimation } = mapScrollViewProps(nonEventProps);
|
|
8
|
+
const scrollViewRef = useRef(null);
|
|
9
|
+
const previousScrollOffsetRef = useRef(scrollOffset);
|
|
10
|
+
const assignRef = useCallback((instance) => {
|
|
11
|
+
scrollViewRef.current = instance;
|
|
12
|
+
assignForwardedRef(ref, instance);
|
|
13
|
+
}, [ref]);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (scrollOffset === undefined || previousScrollOffsetRef.current === scrollOffset) {
|
|
16
|
+
previousScrollOffsetRef.current = scrollOffset;
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
previousScrollOffsetRef.current = scrollOffset;
|
|
20
|
+
scrollViewRef.current
|
|
21
|
+
?.invoke({
|
|
22
|
+
method: 'scrollTo',
|
|
23
|
+
params: {
|
|
24
|
+
offset: scrollOffset,
|
|
25
|
+
smooth: scrollWithAnimation
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
.exec();
|
|
29
|
+
}, [scrollOffset, scrollWithAnimation]);
|
|
30
|
+
const handleScrollToUpper = useCallback((event) => {
|
|
31
|
+
onScrollToUpper?.(toScrollViewEvent(event, 'scrolltoupper', scrollViewId));
|
|
32
|
+
}, [onScrollToUpper, scrollViewId]);
|
|
33
|
+
const handleScrollToLower = useCallback((event) => {
|
|
34
|
+
onScrollToLower?.(toScrollViewEvent(event, 'scrolltolower', scrollViewId));
|
|
35
|
+
}, [onScrollToLower, scrollViewId]);
|
|
36
|
+
const handleScroll = useCallback((event) => {
|
|
37
|
+
onScroll?.(toScrollViewEvent(event, 'scroll', scrollViewId));
|
|
38
|
+
}, [onScroll, scrollViewId]);
|
|
39
|
+
const handleScrollEnd = useCallback((event) => {
|
|
40
|
+
onScrollEnd?.(toScrollViewEvent(event, 'scrollend', scrollViewId));
|
|
41
|
+
}, [onScrollEnd, scrollViewId]);
|
|
42
|
+
const handleContentSizeChanged = useCallback((event) => {
|
|
43
|
+
onContentSizeChanged?.(toScrollViewEvent(event, 'contentsizechanged', scrollViewId));
|
|
44
|
+
}, [onContentSizeChanged, scrollViewId]);
|
|
45
|
+
return (<scroll-view {...mapped} ref={assignRef} {...(onScrollToUpper ? { onScrollToUpper: handleScrollToUpper } : {})} {...(onScrollToLower ? { onScrollToLower: handleScrollToLower } : {})} {...(onScroll ? { onScroll: handleScroll } : {})} {...(onScrollEnd ? { onScrollEnd: handleScrollEnd } : {})} {...(onContentSizeChanged ? { onContentSizeChanged: handleContentSizeChanged } : {})}>
|
|
46
|
+
{wrapTextNode(children)}
|
|
47
|
+
</scroll-view>);
|
|
48
|
+
}
|
|
49
|
+
function wrapTextNode(child) {
|
|
50
|
+
if (typeof child === 'string' || typeof child === 'number') {
|
|
51
|
+
return <Text>{child}</Text>;
|
|
52
|
+
}
|
|
53
|
+
if (Array.isArray(child)) {
|
|
54
|
+
return child.map((item) => wrapTextNode(item));
|
|
55
|
+
}
|
|
56
|
+
return child;
|
|
57
|
+
}
|
|
58
|
+
function assignForwardedRef(ref, value) {
|
|
59
|
+
if (typeof ref === 'function') {
|
|
60
|
+
ref(value);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (ref) {
|
|
64
|
+
ref.current = value;
|
|
65
|
+
}
|
|
3
66
|
}
|
|
4
67
|
//# sourceMappingURL=index.jsx.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ContentSizeChangedEvent as LynxContentSizeChangedEvent, ScrollEndEvent as LynxScrollEndEvent, ScrollEvent as LynxScrollEvent, ScrollToLowerEvent as LynxScrollToLowerEvent, ScrollToUpperEvent as LynxScrollToUpperEvent } from '@lynx-js/types';
|
|
2
|
+
import type { ScrollViewProps as TaroScrollViewProps } from '@tarojs/components';
|
|
3
|
+
type ScrollOrientation = 'vertical' | 'horizontal';
|
|
4
|
+
type LynxScrollViewEvent = LynxScrollToUpperEvent | LynxScrollToLowerEvent | LynxScrollEvent | LynxScrollEndEvent | LynxContentSizeChangedEvent;
|
|
5
|
+
export type MappableTaroScrollViewProps = Omit<TaroScrollViewProps, 'children' | 'onScrollToUpper' | 'onScrollToLower' | 'onScroll' | 'onScrollEnd'> & {
|
|
6
|
+
enableScroll?: boolean;
|
|
7
|
+
scrollOrientation?: ScrollOrientation;
|
|
8
|
+
scrollBarEnable?: boolean;
|
|
9
|
+
initialScrollOffset?: number;
|
|
10
|
+
initialScrollToIndex?: number;
|
|
11
|
+
onContentSizeChanged?: (event: ScrollViewEvent) => void;
|
|
12
|
+
};
|
|
13
|
+
export type ScrollViewEventDetail = TaroScrollViewProps.onScrollDetail;
|
|
14
|
+
export type ScrollViewEvent = {
|
|
15
|
+
type: string;
|
|
16
|
+
timeStamp: number;
|
|
17
|
+
target: {
|
|
18
|
+
id: string;
|
|
19
|
+
tagName: string;
|
|
20
|
+
dataset: Record<string, unknown>;
|
|
21
|
+
};
|
|
22
|
+
currentTarget: {
|
|
23
|
+
id: string;
|
|
24
|
+
tagName: string;
|
|
25
|
+
dataset: Record<string, unknown>;
|
|
26
|
+
};
|
|
27
|
+
detail: ScrollViewEventDetail;
|
|
28
|
+
preventDefault: () => void;
|
|
29
|
+
stopPropagation: () => void;
|
|
30
|
+
};
|
|
31
|
+
type MapScrollViewPropsResult = {
|
|
32
|
+
mapped: Record<string, unknown>;
|
|
33
|
+
scrollViewId: string;
|
|
34
|
+
scrollOffset: number | undefined;
|
|
35
|
+
scrollWithAnimation: boolean;
|
|
36
|
+
};
|
|
37
|
+
export declare function mapScrollViewProps(props: MappableTaroScrollViewProps): MapScrollViewPropsResult;
|
|
38
|
+
export declare function toScrollViewEvent(event: LynxScrollViewEvent, type: string, scrollViewId: string): ScrollViewEvent;
|
|
39
|
+
export declare function toScrollViewEventDetail(event: LynxScrollViewEvent): ScrollViewEventDetail;
|
|
40
|
+
export {};
|
|
41
|
+
//# sourceMappingURL=map.d.ts.map
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export function mapScrollViewProps(props) {
|
|
2
|
+
const { id, className, style, scrollX, scrollY, scrollTop, scrollLeft, scrollWithAnimation, scrollOrientation, enableScroll, scrollBarEnable, showScrollbar, upperThreshold, lowerThreshold, bounces, initialScrollOffset, initialScrollToIndex } = props;
|
|
3
|
+
const orientation = scrollOrientation ?? (scrollX ? 'horizontal' : 'vertical');
|
|
4
|
+
const scrollOffset = orientation === 'horizontal' ? scrollLeft : scrollTop;
|
|
5
|
+
const resolvedInitialScrollOffset = initialScrollOffset ?? scrollOffset;
|
|
6
|
+
const mapped = {
|
|
7
|
+
id,
|
|
8
|
+
className,
|
|
9
|
+
style,
|
|
10
|
+
bounces,
|
|
11
|
+
upperThreshold,
|
|
12
|
+
lowerThreshold,
|
|
13
|
+
scrollBarEnable: scrollBarEnable ?? showScrollbar,
|
|
14
|
+
initialScrollToIndex,
|
|
15
|
+
scrollOrientation: orientation,
|
|
16
|
+
enableScroll: enableScroll ?? Boolean(scrollX || scrollY),
|
|
17
|
+
initialScrollOffset: resolvedInitialScrollOffset
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
mapped,
|
|
21
|
+
scrollViewId: String(mapped.id ?? ''),
|
|
22
|
+
scrollOffset,
|
|
23
|
+
scrollWithAnimation: scrollWithAnimation === true
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function toScrollViewEvent(event, type, scrollViewId) {
|
|
27
|
+
const eventLike = event;
|
|
28
|
+
return {
|
|
29
|
+
type,
|
|
30
|
+
timeStamp: eventLike.timeStamp ?? event.timestamp ?? Date.now(),
|
|
31
|
+
target: toEventTarget(event.target, scrollViewId),
|
|
32
|
+
currentTarget: toEventTarget(event.currentTarget, scrollViewId),
|
|
33
|
+
detail: toScrollViewEventDetail(event),
|
|
34
|
+
preventDefault: eventLike.preventDefault ?? noop,
|
|
35
|
+
stopPropagation: eventLike.stopPropagation ?? noop
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export function toScrollViewEventDetail(event) {
|
|
39
|
+
const detail = event.detail ?? {};
|
|
40
|
+
const isDrag = detail.isDrag;
|
|
41
|
+
return {
|
|
42
|
+
scrollLeft: detail.scrollLeft ?? 0,
|
|
43
|
+
scrollTop: detail.scrollTop ?? 0,
|
|
44
|
+
scrollHeight: detail.scrollHeight ?? 0,
|
|
45
|
+
scrollWidth: detail.scrollWidth ?? 0,
|
|
46
|
+
deltaX: detail.deltaX ?? 0,
|
|
47
|
+
deltaY: detail.deltaY ?? 0,
|
|
48
|
+
...(typeof isDrag === 'boolean' ? { isDrag } : {})
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function toEventTarget(target, fallbackId) {
|
|
52
|
+
return {
|
|
53
|
+
id: target.id || fallbackId,
|
|
54
|
+
tagName: 'scroll-view',
|
|
55
|
+
dataset: target.dataset ?? {}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function noop() { }
|
|
59
|
+
//# sourceMappingURL=map.js.map
|
package/package.json
CHANGED