@mpxjs/webpack-plugin 2.9.67 → 2.9.69-beta.1

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