@mpxjs/webpack-plugin 2.9.70 → 2.9.71

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 (60) hide show
  1. package/lib/platform/template/wx/component-config/movable-view.js +8 -1
  2. package/lib/platform/template/wx/component-config/picker-view.js +1 -5
  3. package/lib/platform/template/wx/component-config/scroll-view.js +1 -1
  4. package/lib/platform/template/wx/index.js +0 -4
  5. package/lib/runtime/components/react/context.ts +8 -0
  6. package/lib/runtime/components/react/dist/context.js +2 -0
  7. package/lib/runtime/components/react/dist/getInnerListeners.js +34 -31
  8. package/lib/runtime/components/react/dist/mpx-button.jsx +16 -44
  9. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +93 -58
  10. package/lib/runtime/components/react/dist/mpx-navigator.jsx +1 -1
  11. package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +35 -0
  12. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +151 -127
  13. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +38 -34
  14. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +10 -11
  15. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +11 -4
  16. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +31 -8
  17. package/lib/runtime/components/react/dist/mpx-swiper.jsx +670 -0
  18. package/lib/runtime/components/react/dist/mpx-view.jsx +15 -53
  19. package/lib/runtime/components/react/dist/pickerFaces.js +7 -6
  20. package/lib/runtime/components/react/dist/pickerVIewContext.js +14 -0
  21. package/lib/runtime/components/react/dist/pickerViewIndicator.jsx +23 -0
  22. package/lib/runtime/components/react/dist/pickerViewMask.jsx +18 -0
  23. package/lib/runtime/components/react/dist/useAnimationHooks.js +20 -2
  24. package/lib/runtime/components/react/dist/utils.jsx +74 -11
  25. package/lib/runtime/components/react/getInnerListeners.ts +43 -32
  26. package/lib/runtime/components/react/mpx-button.tsx +20 -57
  27. package/lib/runtime/components/react/mpx-movable-view.tsx +119 -74
  28. package/lib/runtime/components/react/mpx-navigator.tsx +1 -1
  29. package/lib/runtime/components/react/mpx-picker-view-column-item.tsx +76 -0
  30. package/lib/runtime/components/react/mpx-picker-view-column.tsx +206 -183
  31. package/lib/runtime/components/react/mpx-picker-view.tsx +49 -48
  32. package/lib/runtime/components/react/mpx-rich-text/index.tsx +12 -18
  33. package/lib/runtime/components/react/mpx-scroll-view.tsx +21 -10
  34. package/lib/runtime/components/react/mpx-swiper-item.tsx +45 -11
  35. package/lib/runtime/components/react/mpx-swiper.tsx +742 -0
  36. package/lib/runtime/components/react/mpx-view.tsx +18 -65
  37. package/lib/runtime/components/react/pickerFaces.ts +10 -7
  38. package/lib/runtime/components/react/pickerVIewContext.ts +27 -0
  39. package/lib/runtime/components/react/pickerViewIndicator.tsx +34 -0
  40. package/lib/runtime/components/react/pickerViewMask.tsx +30 -0
  41. package/lib/runtime/components/react/types/{getInnerListeners.ts → getInnerListeners.d.ts} +4 -5
  42. package/lib/runtime/components/react/types/global.d.ts +10 -0
  43. package/lib/runtime/components/react/useAnimationHooks.ts +24 -3
  44. package/lib/runtime/components/react/utils.tsx +85 -12
  45. package/lib/runtime/components/web/mpx-checkbox.vue +1 -1
  46. package/lib/runtime/components/web/mpx-picker-view-column.vue +9 -4
  47. package/lib/template-compiler/compiler.js +61 -13
  48. package/lib/wxss/loader.js +15 -2
  49. package/package.json +3 -3
  50. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +0 -480
  51. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +0 -68
  52. package/lib/runtime/components/react/dist/mpx-swiper/type.js +0 -1
  53. package/lib/runtime/components/react/dist/pickerOverlay.jsx +0 -21
  54. package/lib/runtime/components/react/dist/types/common.js +0 -1
  55. package/lib/runtime/components/react/dist/types/getInnerListeners.js +0 -1
  56. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +0 -527
  57. package/lib/runtime/components/react/mpx-swiper/index.tsx +0 -80
  58. package/lib/runtime/components/react/mpx-swiper/type.ts +0 -87
  59. package/lib/runtime/components/react/pickerOverlay.tsx +0 -32
  60. /package/lib/runtime/components/react/types/{common.ts → common.d.ts} +0 -0
@@ -0,0 +1,670 @@
1
+ import { View } from 'react-native';
2
+ import { GestureDetector, Gesture } from 'react-native-gesture-handler';
3
+ import Animated, { useAnimatedStyle, useSharedValue, withTiming, Easing, runOnJS, useAnimatedReaction, cancelAnimation } from 'react-native-reanimated';
4
+ import React, { forwardRef, useRef, useEffect, useMemo } from 'react';
5
+ import useInnerProps, { getCustomEvent } from './getInnerListeners';
6
+ import useNodesRef from './useNodesRef'; // 引入辅助函数
7
+ import { useTransformStyle, splitStyle, splitProps, useLayout, wrapChildren } from './utils';
8
+ import { SwiperContext } from './context';
9
+ /**
10
+ * 默认的Style类型
11
+ */
12
+ const styles = {
13
+ pagination_x: {
14
+ position: 'absolute',
15
+ bottom: 25,
16
+ left: 0,
17
+ right: 0,
18
+ flexDirection: 'row',
19
+ flex: 1,
20
+ justifyContent: 'center',
21
+ alignItems: 'center'
22
+ },
23
+ pagination_y: {
24
+ position: 'absolute',
25
+ right: 15,
26
+ top: 0,
27
+ bottom: 0,
28
+ flexDirection: 'column',
29
+ flex: 1,
30
+ justifyContent: 'center',
31
+ alignItems: 'center'
32
+ },
33
+ pagerWrapperx: {
34
+ position: 'absolute',
35
+ flexDirection: 'row',
36
+ justifyContent: 'center',
37
+ alignItems: 'center'
38
+ },
39
+ pagerWrappery: {
40
+ position: 'absolute',
41
+ flexDirection: 'column',
42
+ justifyContent: 'center',
43
+ alignItems: 'center'
44
+ },
45
+ swiper: {
46
+ overflow: 'scroll',
47
+ display: 'flex',
48
+ justifyContent: 'flex-start'
49
+ }
50
+ };
51
+ const dotCommonStyle = {
52
+ width: 8,
53
+ height: 8,
54
+ borderRadius: 4,
55
+ marginLeft: 3,
56
+ marginRight: 3,
57
+ marginTop: 3,
58
+ marginBottom: 3,
59
+ zIndex: 98
60
+ };
61
+ const activeDotStyle = {
62
+ zIndex: 99
63
+ };
64
+ const longPressRatio = 100;
65
+ const easeMap = {
66
+ default: Easing.linear,
67
+ linear: Easing.linear,
68
+ easeInCubic: Easing.in(Easing.cubic),
69
+ easeOutCubic: Easing.out(Easing.cubic),
70
+ easeInOutCubic: Easing.inOut(Easing.cubic)
71
+ };
72
+ const SwiperWrapper = forwardRef((props, ref) => {
73
+ const { 'indicator-dots': showsPagination, 'indicator-color': dotColor = 'rgba(0, 0, 0, .3)', 'indicator-active-color': activeDotColor = '#000000', 'enable-var': enableVar = false, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight, 'external-var-context': externalVarContext, style = {}, autoplay = false, circular = false } = props;
74
+ const easeingFunc = props['easing-function'] || 'default';
75
+ const easeDuration = props.duration || 500;
76
+ const horizontal = props.vertical !== undefined ? !props.vertical : true;
77
+ const nodeRef = useRef(null);
78
+ useNodesRef(props, ref, nodeRef, {});
79
+ // 计算transfrom之类的
80
+ const { normalStyle, hasVarDec, varContextRef, hasSelfPercent, setWidth, setHeight } = useTransformStyle(style, {
81
+ enableVar,
82
+ externalVarContext,
83
+ parentFontSize,
84
+ parentWidth,
85
+ parentHeight
86
+ });
87
+ const { textStyle } = splitStyle(normalStyle);
88
+ const { textProps } = splitProps(props);
89
+ const preMargin = props['previous-margin'] ? global.__formatValue(props['previous-margin']) : 0;
90
+ const nextMargin = props['next-margin'] ? global.__formatValue(props['next-margin']) : 0;
91
+ const preMarginShared = useSharedValue(preMargin);
92
+ const nextMarginShared = useSharedValue(nextMargin);
93
+ const autoplayShared = useSharedValue(autoplay);
94
+ // 默认前后补位的元素个数
95
+ const patchElmNum = circular ? (preMargin ? 2 : 1) : 0;
96
+ const patchElmNumShared = useSharedValue(patchElmNum);
97
+ const circularShared = useSharedValue(circular);
98
+ const children = Array.isArray(props.children) ? props.children.filter(child => child) : (props.children ? [props.children] : []);
99
+ // 对有变化的变量,在worklet中只能使用sharedValue变量,useRef不能更新
100
+ const childrenLength = useSharedValue(children.length);
101
+ const initWidth = typeof normalStyle?.width === 'number' ? normalStyle.width - preMargin - nextMargin : normalStyle.width;
102
+ const initHeight = typeof normalStyle?.height === 'number' ? normalStyle.height - preMargin - nextMargin : normalStyle.height;
103
+ const dir = horizontal === false ? 'y' : 'x';
104
+ const pstep = dir === 'x' ? initWidth : initHeight;
105
+ const initStep = isNaN(pstep) ? 0 : pstep;
106
+ // 每个元素的宽度 or 高度,有固定值直接初始化无则0
107
+ const step = useSharedValue(initStep);
108
+ // 记录选中元素的索引值
109
+ const currentIndex = useSharedValue(props.current || 0);
110
+ // const initOffset = getOffset(props.current || 0, initStep)
111
+ // 记录元素的偏移量
112
+ const offset = useSharedValue(getOffset(props.current || 0, initStep));
113
+ const strAbso = 'absolute' + dir.toUpperCase();
114
+ // 标识手指触摸和抬起, 起点在onBegin
115
+ const touchfinish = useSharedValue(true);
116
+ // 记录上一帧的绝对定位坐标
117
+ const preAbsolutePos = useSharedValue(0);
118
+ // 记录从onBegin 到 onTouchesUp 时移动的距离
119
+ const moveTranstion = useSharedValue(0);
120
+ // 记录从onBegin 到 onTouchesUp 的时间
121
+ const moveTime = useSharedValue(0);
122
+ const timerId = useRef(0);
123
+ const intervalTimer = props.interval || 500;
124
+ const {
125
+ // 存储layout布局信息
126
+ layoutRef, layoutProps, layoutStyle } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef, onLayout: onWrapperLayout });
127
+ const innerProps = useInnerProps(props, {
128
+ ref: nodeRef
129
+ }, [
130
+ 'style',
131
+ 'indicator-dots',
132
+ 'indicator-color',
133
+ 'indicator-active-color',
134
+ 'previous-margin',
135
+ 'vertical',
136
+ 'previous-margin',
137
+ 'next-margin',
138
+ 'easing-function',
139
+ 'autoplay',
140
+ 'circular',
141
+ 'interval',
142
+ 'easing-function'
143
+ ], { layoutRef: layoutRef });
144
+ function onWrapperLayout(e) {
145
+ const { width, height } = e.nativeEvent.layout;
146
+ const realWidth = dir === 'x' ? width - preMargin - nextMargin : width;
147
+ const realHeight = dir === 'y' ? height - preMargin - nextMargin : height;
148
+ const iStep = dir === 'x' ? realWidth : realHeight;
149
+ if (iStep !== step.value) {
150
+ step.value = iStep;
151
+ updateCurrent(props.current || 0, iStep);
152
+ updateAutoplay();
153
+ }
154
+ }
155
+ const dotAnimatedStyle = useAnimatedStyle(() => {
156
+ if (!step.value)
157
+ return {};
158
+ const dotStep = dotCommonStyle.width + dotCommonStyle.marginRight + dotCommonStyle.marginLeft;
159
+ if (dir === 'x') {
160
+ return { transform: [{ translateX: currentIndex.value * dotStep }] };
161
+ }
162
+ else {
163
+ return { transform: [{ translateY: currentIndex.value * dotStep }] };
164
+ }
165
+ });
166
+ function renderPagination() {
167
+ if (children.length <= 1)
168
+ return null;
169
+ const activeColor = activeDotColor || '#007aff';
170
+ const unActionColor = dotColor || 'rgba(0,0,0,.2)';
171
+ // 正常渲染所有dots
172
+ const dots = [];
173
+ for (let i = 0; i < children.length; i++) {
174
+ dots.push(<View style={[dotCommonStyle, { backgroundColor: unActionColor }]} key={i}></View>);
175
+ }
176
+ return (<View pointerEvents="none" style={styles['pagination_' + dir]}>
177
+ <View style={[styles['pagerWrapper' + dir]]}>
178
+ <Animated.View style={[
179
+ dotCommonStyle,
180
+ activeDotStyle,
181
+ {
182
+ backgroundColor: activeColor,
183
+ position: 'absolute',
184
+ left: 0,
185
+ top: 0
186
+ },
187
+ dotAnimatedStyle
188
+ ]}/>
189
+ {dots}
190
+ </View>
191
+ </View>);
192
+ }
193
+ function renderItems() {
194
+ const intLen = children.length;
195
+ let renderChild = children.slice();
196
+ if (circular && intLen > 1) {
197
+ // 最前面加最后一个元素
198
+ const lastChild = React.cloneElement(children[intLen - 1], { key: 'clone0' });
199
+ // 最后面加第一个元素
200
+ const firstChild = React.cloneElement(children[0], { key: 'clone1' });
201
+ if (preMargin) {
202
+ const lastChild1 = React.cloneElement(children[intLen - 2], { key: 'clone2' });
203
+ const firstChild1 = React.cloneElement(children[1], { key: 'clone3' });
204
+ renderChild = [lastChild1, lastChild].concat(renderChild).concat([firstChild, firstChild1]);
205
+ }
206
+ else {
207
+ renderChild = [lastChild].concat(renderChild).concat([firstChild]);
208
+ }
209
+ }
210
+ const arrChildren = renderChild.map((child, index) => {
211
+ const extraStyle = {};
212
+ if (index === 0 && !circular) {
213
+ preMargin && dir === 'x' && (extraStyle.marginLeft = preMargin);
214
+ preMargin && dir === 'y' && (extraStyle.marginTop = preMargin);
215
+ }
216
+ if (index === intLen - 1 && !circular) {
217
+ nextMargin && dir === 'x' && (extraStyle.marginRight = nextMargin);
218
+ nextMargin && dir === 'y' && (extraStyle.marginBottom = nextMargin);
219
+ }
220
+ // 业务swiper-item自己生成key,内部添加的元素自定义key
221
+ const newChild = React.cloneElement(child, {
222
+ itemIndex: index,
223
+ customStyle: extraStyle
224
+ });
225
+ return newChild;
226
+ });
227
+ const contextValue = {
228
+ offset,
229
+ step,
230
+ scale: props.scale,
231
+ dir
232
+ };
233
+ return (<SwiperContext.Provider value={contextValue}>{arrChildren}</SwiperContext.Provider>);
234
+ }
235
+ const { loop, pauseLoop, resumeLoop } = useMemo(() => {
236
+ function createAutoPlay() {
237
+ if (!step.value)
238
+ return;
239
+ let targetOffset = 0;
240
+ let nextIndex = currentIndex.value;
241
+ if (!circularShared.value) {
242
+ // 获取下一个位置的坐标, 循环到最后一个元素,直接停止, 取消定时器
243
+ if (currentIndex.value === childrenLength.value - 1) {
244
+ pauseLoop();
245
+ return;
246
+ }
247
+ nextIndex += 1;
248
+ // targetOffset = -nextIndex * step.value - preMarginShared.value
249
+ targetOffset = -nextIndex * step.value;
250
+ offset.value = withTiming(targetOffset, {
251
+ duration: easeDuration,
252
+ easing: easeMap[easeingFunc]
253
+ }, () => {
254
+ currentIndex.value = nextIndex;
255
+ runOnJS(loop)();
256
+ });
257
+ }
258
+ else {
259
+ // 默认向右, 向下
260
+ if (nextIndex === childrenLength.value - 1) {
261
+ nextIndex = 0;
262
+ targetOffset = -(childrenLength.value + patchElmNumShared.value) * step.value + preMarginShared.value;
263
+ // 执行动画到下一帧
264
+ offset.value = withTiming(targetOffset, {
265
+ duration: easeDuration
266
+ }, () => {
267
+ const initOffset = -step.value * patchElmNumShared.value + preMarginShared.value;
268
+ // 将开始位置设置为真正的位置
269
+ offset.value = initOffset;
270
+ currentIndex.value = nextIndex;
271
+ runOnJS(loop)();
272
+ });
273
+ }
274
+ else {
275
+ nextIndex = currentIndex.value + 1;
276
+ targetOffset = -(nextIndex + patchElmNumShared.value) * step.value + preMarginShared.value;
277
+ // 执行动画到下一帧
278
+ offset.value = withTiming(targetOffset, {
279
+ duration: easeDuration,
280
+ easing: easeMap[easeingFunc]
281
+ }, () => {
282
+ currentIndex.value = nextIndex;
283
+ runOnJS(loop)();
284
+ });
285
+ }
286
+ }
287
+ }
288
+ // loop在JS线程中调用,createAutoPlay + useEffect中
289
+ function loop() {
290
+ timerId.current && clearTimeout(timerId.current);
291
+ timerId.current = setTimeout(createAutoPlay, intervalTimer);
292
+ }
293
+ function pauseLoop() {
294
+ timerId.current && clearTimeout(timerId.current);
295
+ }
296
+ // resumeLoop在worklet中调用
297
+ function resumeLoop() {
298
+ if (autoplayShared.value && childrenLength.value > 1) {
299
+ loop();
300
+ }
301
+ }
302
+ return {
303
+ loop,
304
+ pauseLoop,
305
+ resumeLoop
306
+ };
307
+ }, []);
308
+ function handleSwiperChange(current) {
309
+ if (props.current !== currentIndex.value) {
310
+ const eventData = getCustomEvent('change', {}, { detail: { current, source: 'touch' }, layoutRef: layoutRef });
311
+ props.bindchange && props.bindchange(eventData);
312
+ }
313
+ }
314
+ function getOffset(index, stepValue) {
315
+ if (!stepValue)
316
+ return 0;
317
+ let targetOffset = 0;
318
+ if (circular && children.length > 1) {
319
+ const targetIndex = index + patchElmNum;
320
+ targetOffset = -(stepValue * targetIndex - preMargin);
321
+ }
322
+ else {
323
+ targetOffset = -index * stepValue;
324
+ }
325
+ return targetOffset;
326
+ }
327
+ function updateCurrent(index, stepValue) {
328
+ const targetOffset = getOffset(index || 0, stepValue);
329
+ if (targetOffset !== offset.value) {
330
+ // 内部基于props.current!==currentIndex.value决定是否使用动画及更新currentIndex.value
331
+ if (props.current !== undefined && props.current !== currentIndex.value) {
332
+ offset.value = withTiming(targetOffset, {
333
+ duration: easeDuration,
334
+ easing: easeMap[easeingFunc]
335
+ }, () => {
336
+ currentIndex.value = props.current || 0;
337
+ });
338
+ }
339
+ else {
340
+ offset.value = targetOffset;
341
+ }
342
+ }
343
+ }
344
+ function updateAutoplay() {
345
+ if (autoplay && children.length > 1) {
346
+ loop();
347
+ }
348
+ else {
349
+ pauseLoop();
350
+ }
351
+ }
352
+ // 1. 用户在当前页切换选中项,动画;用户携带选中index打开到swiper页直接选中不走动画
353
+ useAnimatedReaction(() => currentIndex.value, (newIndex, preIndex) => {
354
+ // 这里必须传递函数名, 直接写()=> {}形式会报 访问了未sharedValue信息
355
+ if (newIndex !== preIndex && props.bindchange) {
356
+ runOnJS(handleSwiperChange)(newIndex);
357
+ }
358
+ });
359
+ useEffect(() => {
360
+ let patchStep = 0;
361
+ if (preMargin !== preMarginShared.value) {
362
+ patchStep += preMargin - preMarginShared.value;
363
+ }
364
+ if (nextMargin !== nextMarginShared.value) {
365
+ patchStep += nextMargin - nextMarginShared.value;
366
+ }
367
+ preMarginShared.value = preMargin;
368
+ nextMarginShared.value = nextMargin;
369
+ const newStep = step.value - patchStep;
370
+ if (step.value !== newStep) {
371
+ step.value = newStep;
372
+ offset.value = getOffset(currentIndex.value, newStep);
373
+ }
374
+ }, [preMargin, nextMargin]);
375
+ useEffect(() => {
376
+ childrenLength.value = children.length;
377
+ if (children.length - 1 < currentIndex.value) {
378
+ pauseLoop();
379
+ currentIndex.value = 0;
380
+ offset.value = getOffset(0, step.value);
381
+ if (autoplay && children.length > 1) {
382
+ loop();
383
+ }
384
+ }
385
+ }, [children.length]);
386
+ useEffect(() => {
387
+ updateCurrent(props.current || 0, step.value);
388
+ }, [props.current]);
389
+ useEffect(() => {
390
+ autoplayShared.value = autoplay;
391
+ updateAutoplay();
392
+ return () => {
393
+ if (autoplay) {
394
+ pauseLoop();
395
+ }
396
+ };
397
+ }, [autoplay]);
398
+ useEffect(() => {
399
+ if (circular !== circularShared.value) {
400
+ circularShared.value = circular;
401
+ patchElmNumShared.value = circular ? (preMargin ? 2 : 1) : 0;
402
+ offset.value = getOffset(currentIndex.value, step.value);
403
+ }
404
+ }, [circular, preMargin]);
405
+ const { gestureHandler } = useMemo(() => {
406
+ function getTargetPosition(eventData) {
407
+ 'worklet';
408
+ // 移动的距离
409
+ const { translation } = eventData;
410
+ let resetOffsetPos = 0;
411
+ let selectedIndex = currentIndex.value;
412
+ // 是否临界点
413
+ let isCriticalItem = false;
414
+ // 真实滚动到的偏移量坐标
415
+ let moveToTargetPos = 0;
416
+ const currentOffset = translation < 0 ? offset.value - preMarginShared.value : offset.value + preMarginShared.value;
417
+ const computedIndex = Math.abs(currentOffset) / step.value;
418
+ const moveToIndex = translation < 0 ? Math.ceil(computedIndex) : Math.floor(computedIndex);
419
+ // 实际应该定位的索引值
420
+ if (!circularShared.value) {
421
+ selectedIndex = moveToIndex;
422
+ moveToTargetPos = selectedIndex * step.value;
423
+ }
424
+ else {
425
+ if (moveToIndex >= childrenLength.value + patchElmNumShared.value) {
426
+ selectedIndex = moveToIndex - (childrenLength.value + patchElmNumShared.value);
427
+ resetOffsetPos = (selectedIndex + patchElmNumShared.value) * step.value - preMarginShared.value;
428
+ moveToTargetPos = moveToIndex * step.value - preMarginShared.value;
429
+ isCriticalItem = true;
430
+ }
431
+ else if (moveToIndex <= patchElmNumShared.value - 1) {
432
+ selectedIndex = moveToIndex === 0 ? childrenLength.value - patchElmNumShared.value : childrenLength.value - 1;
433
+ resetOffsetPos = (selectedIndex + patchElmNumShared.value) * step.value - preMarginShared.value;
434
+ moveToTargetPos = moveToIndex * step.value - preMarginShared.value;
435
+ isCriticalItem = true;
436
+ }
437
+ else {
438
+ selectedIndex = moveToIndex - patchElmNumShared.value;
439
+ moveToTargetPos = moveToIndex * step.value - preMarginShared.value;
440
+ }
441
+ }
442
+ return {
443
+ selectedIndex,
444
+ isCriticalItem,
445
+ resetOffset: -resetOffsetPos,
446
+ targetOffset: -moveToTargetPos
447
+ };
448
+ }
449
+ function canMove(eventData) {
450
+ 'worklet';
451
+ const { translation } = eventData;
452
+ const currentOffset = Math.abs(offset.value);
453
+ if (!circularShared.value) {
454
+ if (translation < 0) {
455
+ return currentOffset < step.value * (childrenLength.value - 1);
456
+ }
457
+ else {
458
+ return currentOffset > 0;
459
+ }
460
+ }
461
+ else {
462
+ return true;
463
+ }
464
+ }
465
+ function handleEnd(eventData) {
466
+ 'worklet';
467
+ const { isCriticalItem, targetOffset, resetOffset, selectedIndex } = getTargetPosition(eventData);
468
+ if (isCriticalItem) {
469
+ offset.value = withTiming(targetOffset, {
470
+ duration: easeDuration,
471
+ easing: easeMap[easeingFunc]
472
+ }, () => {
473
+ if (touchfinish.value !== false) {
474
+ currentIndex.value = selectedIndex;
475
+ offset.value = resetOffset;
476
+ runOnJS(resumeLoop)();
477
+ }
478
+ });
479
+ }
480
+ else {
481
+ offset.value = withTiming(targetOffset, {
482
+ duration: easeDuration,
483
+ easing: easeMap[easeingFunc]
484
+ }, () => {
485
+ if (touchfinish.value !== false) {
486
+ currentIndex.value = selectedIndex;
487
+ runOnJS(resumeLoop)();
488
+ }
489
+ });
490
+ }
491
+ }
492
+ function handleBack(eventData) {
493
+ 'worklet';
494
+ const { translation } = eventData;
495
+ // 向右滑动的back:trans < 0, 向左滑动的back: trans < 0
496
+ let currentOffset = Math.abs(offset.value);
497
+ if (circularShared.value) {
498
+ currentOffset += translation < 0 ? preMarginShared.value : -preMarginShared.value;
499
+ }
500
+ const curIndex = currentOffset / step.value;
501
+ const moveToIndex = (translation < 0 ? Math.floor(curIndex) : Math.ceil(curIndex)) - patchElmNumShared.value;
502
+ const targetOffset = -(moveToIndex + patchElmNumShared.value) * step.value + (circularShared.value ? preMarginShared.value : 0);
503
+ offset.value = withTiming(targetOffset, {
504
+ duration: easeDuration,
505
+ easing: easeMap[easeingFunc]
506
+ }, () => {
507
+ if (touchfinish.value !== false) {
508
+ currentIndex.value = moveToIndex;
509
+ runOnJS(resumeLoop)();
510
+ }
511
+ });
512
+ }
513
+ function handleLongPress() {
514
+ 'worklet';
515
+ const currentOffset = Math.abs(offset.value);
516
+ let preOffset = (currentIndex.value + patchElmNumShared.value) * step.value;
517
+ if (circularShared.value) {
518
+ preOffset -= preMarginShared.value;
519
+ }
520
+ // 正常事件中拿到的transition值(正向滑动<0,倒着滑>0)
521
+ const diffOffset = preOffset - currentOffset;
522
+ const half = Math.abs(diffOffset) > step.value / 2;
523
+ if (+diffOffset === 0) {
524
+ runOnJS(resumeLoop)();
525
+ }
526
+ else if (half) {
527
+ handleEnd({ translation: diffOffset });
528
+ }
529
+ else {
530
+ handleBack({ translation: diffOffset });
531
+ }
532
+ }
533
+ function reachBoundary(eventData) {
534
+ 'worklet';
535
+ // 移动的距离
536
+ const { translation } = eventData;
537
+ const elementsLength = step.value * childrenLength.value;
538
+ let isBoundary = false;
539
+ let resetOffset = 0;
540
+ // Y轴向下滚动, transDistance > 0, 向上滚动 < 0 X轴向左滚动, transDistance > 0
541
+ const currentOffset = offset.value;
542
+ const moveStep = Math.ceil(translation / elementsLength);
543
+ if (translation < 0) {
544
+ const posEnd = (childrenLength.value + patchElmNumShared.value + 1) * step.value;
545
+ const posReverseEnd = (patchElmNumShared.value - 1) * step.value;
546
+ if (currentOffset < -posEnd + step.value) {
547
+ isBoundary = true;
548
+ resetOffset = Math.abs(moveStep) === 0 ? patchElmNumShared.value * step.value + translation : moveStep * elementsLength;
549
+ }
550
+ if (currentOffset > -posReverseEnd) {
551
+ isBoundary = true;
552
+ resetOffset = moveStep * elementsLength;
553
+ }
554
+ }
555
+ else if (translation > 0) {
556
+ const posEnd = (patchElmNumShared.value - 1) * step.value;
557
+ const posReverseEnd = (patchElmNumShared.value + childrenLength.value) * step.value;
558
+ if (currentOffset > -posEnd) {
559
+ isBoundary = true;
560
+ resetOffset = moveStep * elementsLength + step.value + (moveStep === 1 ? translation : 0);
561
+ }
562
+ if (currentOffset < -posReverseEnd) {
563
+ isBoundary = true;
564
+ resetOffset = moveStep * elementsLength + patchElmNumShared.value * step.value;
565
+ }
566
+ }
567
+ return {
568
+ isBoundary,
569
+ resetOffset: -resetOffset
570
+ };
571
+ }
572
+ const gesturePan = Gesture.Pan()
573
+ .onBegin((e) => {
574
+ 'worklet';
575
+ if (!step.value)
576
+ return;
577
+ touchfinish.value = false;
578
+ cancelAnimation(offset);
579
+ runOnJS(pauseLoop)();
580
+ preAbsolutePos.value = e[strAbso];
581
+ moveTranstion.value = e[strAbso];
582
+ moveTime.value = new Date().getTime();
583
+ })
584
+ .onTouchesMove((e) => {
585
+ 'worklet';
586
+ if (touchfinish.value)
587
+ return;
588
+ const touchEventData = e.changedTouches[0];
589
+ const moveDistance = touchEventData[strAbso] - preAbsolutePos.value;
590
+ const eventData = {
591
+ translation: moveDistance
592
+ };
593
+ // 处理用户一直拖拽到临界点的场景, 不会执行onEnd
594
+ if (!circularShared.value && !canMove(eventData)) {
595
+ return;
596
+ }
597
+ const { isBoundary, resetOffset } = reachBoundary(eventData);
598
+ if (isBoundary && circularShared.value) {
599
+ offset.value = resetOffset;
600
+ }
601
+ else {
602
+ offset.value = moveDistance + offset.value;
603
+ }
604
+ preAbsolutePos.value = touchEventData[strAbso];
605
+ })
606
+ .onTouchesUp((e) => {
607
+ 'worklet';
608
+ if (touchfinish.value)
609
+ return;
610
+ const touchEventData = e.changedTouches[0];
611
+ const moveDistance = touchEventData[strAbso] - moveTranstion.value;
612
+ touchfinish.value = true;
613
+ const eventData = {
614
+ translation: moveDistance
615
+ };
616
+ // 用户手指按下起来, 需要计算正确的位置, 比如在滑动过程中突然按下然后起来,需要计算到正确的位置
617
+ if (!circularShared.value && !canMove(eventData)) {
618
+ return;
619
+ }
620
+ const strVelocity = moveDistance / (new Date().getTime() - moveTime.value) * 1000;
621
+ if (Math.abs(strVelocity) < longPressRatio) {
622
+ handleLongPress();
623
+ }
624
+ else {
625
+ handleEnd(eventData);
626
+ }
627
+ });
628
+ return {
629
+ gestureHandler: gesturePan
630
+ };
631
+ }, []);
632
+ const animatedStyles = useAnimatedStyle(() => {
633
+ if (dir === 'x') {
634
+ return { transform: [{ translateX: offset.value }], opacity: step.value > 0 ? 1 : 0 };
635
+ }
636
+ else {
637
+ return { transform: [{ translateY: offset.value }], opacity: step.value > 0 ? 1 : 0 };
638
+ }
639
+ });
640
+ function renderSwiper() {
641
+ const arrPages = renderItems();
642
+ return (<View style={[normalStyle, layoutStyle, styles.swiper]} {...layoutProps} {...innerProps}>
643
+ <Animated.View style={[{
644
+ flexDirection: dir === 'x' ? 'row' : 'column',
645
+ width: '100%',
646
+ height: '100%'
647
+ }, animatedStyles]}>
648
+ {wrapChildren({
649
+ children: arrPages
650
+ }, {
651
+ hasVarDec,
652
+ varContext: varContextRef.current,
653
+ textStyle,
654
+ textProps
655
+ })}
656
+ </Animated.View>
657
+ {showsPagination && renderPagination()}
658
+ </View>);
659
+ }
660
+ if (children.length === 1) {
661
+ return renderSwiper();
662
+ }
663
+ else {
664
+ return (<GestureDetector gesture={gestureHandler}>
665
+ {renderSwiper()}
666
+ </GestureDetector>);
667
+ }
668
+ });
669
+ SwiperWrapper.displayName = 'MpxSwiperWrapper';
670
+ export default SwiperWrapper;