@mpxjs/webpack-plugin 2.9.66 → 2.9.69-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/lib/dependencies/RecordGlobalComponentsDependency.js +11 -12
  2. package/lib/dependencies/RecordRuntimeInfoDependency.js +1 -1
  3. package/lib/index.js +29 -8
  4. package/lib/json-compiler/index.js +2 -11
  5. package/lib/loader.js +24 -45
  6. package/lib/native-loader.js +49 -64
  7. package/lib/platform/json/wx/index.js +24 -18
  8. package/lib/platform/style/wx/index.js +49 -47
  9. package/lib/platform/template/wx/component-config/canvas.js +8 -0
  10. package/lib/platform/template/wx/component-config/fix-component-name.js +15 -12
  11. package/lib/platform/template/wx/component-config/index.js +1 -1
  12. package/lib/platform/template/wx/component-config/input.js +1 -1
  13. package/lib/platform/template/wx/component-config/rich-text.js +8 -0
  14. package/lib/platform/template/wx/component-config/swiper.js +1 -1
  15. package/lib/platform/template/wx/component-config/textarea.js +1 -1
  16. package/lib/platform/template/wx/component-config/unsupported.js +1 -1
  17. package/lib/react/index.js +4 -3
  18. package/lib/react/processJSON.js +5 -13
  19. package/lib/react/processMainScript.js +7 -3
  20. package/lib/react/processScript.js +3 -4
  21. package/lib/react/processStyles.js +14 -4
  22. package/lib/react/processTemplate.js +5 -2
  23. package/lib/resolver/AddModePlugin.js +20 -7
  24. package/lib/runtime/components/react/context.ts +6 -0
  25. package/lib/runtime/components/react/dist/context.js +2 -0
  26. package/lib/runtime/components/react/dist/event.config.js +24 -24
  27. package/lib/runtime/components/react/dist/getInnerListeners.js +183 -174
  28. package/lib/runtime/components/react/dist/mpx-button.jsx +78 -50
  29. package/lib/runtime/components/react/dist/mpx-canvas/Bus.js +60 -0
  30. package/lib/runtime/components/react/dist/mpx-canvas/CanvasGradient.js +15 -0
  31. package/lib/runtime/components/react/dist/mpx-canvas/CanvasRenderingContext2D.js +84 -0
  32. package/lib/runtime/components/react/dist/mpx-canvas/Image.js +87 -0
  33. package/lib/runtime/components/react/dist/mpx-canvas/ImageData.js +15 -0
  34. package/lib/runtime/components/react/dist/mpx-canvas/constructorsRegistry.js +28 -0
  35. package/lib/runtime/components/react/dist/mpx-canvas/html.js +343 -0
  36. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +232 -0
  37. package/lib/runtime/components/react/dist/mpx-canvas/utils.jsx +89 -0
  38. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +41 -34
  39. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +30 -39
  40. package/lib/runtime/components/react/dist/mpx-form.jsx +47 -41
  41. package/lib/runtime/components/react/dist/mpx-icon.jsx +9 -17
  42. package/lib/runtime/components/react/dist/mpx-image.jsx +291 -0
  43. package/lib/runtime/components/react/dist/mpx-input.jsx +95 -62
  44. package/lib/runtime/components/react/dist/mpx-label.jsx +24 -28
  45. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +20 -30
  46. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +377 -293
  47. package/lib/runtime/components/react/dist/mpx-navigator.jsx +3 -5
  48. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +6 -2
  49. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +5 -3
  50. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +6 -2
  51. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +6 -2
  52. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +6 -2
  53. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +10 -15
  54. package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +39 -0
  55. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +160 -88
  56. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +80 -121
  57. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +39 -34
  58. package/lib/runtime/components/react/dist/mpx-radio.jsx +28 -43
  59. package/lib/runtime/components/react/dist/mpx-rich-text/html.js +39 -0
  60. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +63 -0
  61. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +7 -5
  62. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +77 -51
  63. package/lib/runtime/components/react/dist/mpx-simple-text.jsx +11 -0
  64. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +5 -3
  65. package/lib/runtime/components/react/dist/mpx-swiper.jsx +606 -0
  66. package/lib/runtime/components/react/dist/mpx-switch.jsx +28 -11
  67. package/lib/runtime/components/react/dist/mpx-text.jsx +12 -11
  68. package/lib/runtime/components/react/dist/mpx-textarea.jsx +9 -4
  69. package/lib/runtime/components/react/dist/mpx-view.jsx +66 -62
  70. package/lib/runtime/components/react/dist/mpx-web-view.jsx +113 -36
  71. package/lib/runtime/components/react/dist/pickerFaces.js +81 -0
  72. package/lib/runtime/components/react/dist/pickerVIewContext.js +9 -0
  73. package/lib/runtime/components/react/dist/pickerViewMask.jsx +18 -0
  74. package/lib/runtime/components/react/dist/pickerViewOverlay.jsx +23 -0
  75. package/lib/runtime/components/react/dist/useAnimationHooks.js +126 -12
  76. package/lib/runtime/components/react/dist/utils.jsx +80 -24
  77. package/lib/runtime/components/react/event.config.ts +25 -26
  78. package/lib/runtime/components/react/getInnerListeners.ts +237 -198
  79. package/lib/runtime/components/react/mpx-button.tsx +105 -58
  80. package/lib/runtime/components/react/mpx-canvas/Bus.ts +70 -0
  81. package/lib/runtime/components/react/mpx-canvas/CanvasGradient.ts +18 -0
  82. package/lib/runtime/components/react/mpx-canvas/CanvasRenderingContext2D.ts +87 -0
  83. package/lib/runtime/components/react/mpx-canvas/Image.ts +102 -0
  84. package/lib/runtime/components/react/mpx-canvas/ImageData.ts +23 -0
  85. package/lib/runtime/components/react/mpx-canvas/constructorsRegistry.ts +38 -0
  86. package/lib/runtime/components/react/mpx-canvas/html.ts +343 -0
  87. package/lib/runtime/components/react/mpx-canvas/index.tsx +296 -0
  88. package/lib/runtime/components/react/mpx-canvas/utils.tsx +150 -0
  89. package/lib/runtime/components/react/mpx-checkbox-group.tsx +77 -51
  90. package/lib/runtime/components/react/mpx-checkbox.tsx +49 -50
  91. package/lib/runtime/components/react/mpx-form.tsx +62 -57
  92. package/lib/runtime/components/react/mpx-icon.tsx +13 -18
  93. package/lib/runtime/components/react/mpx-image.tsx +436 -0
  94. package/lib/runtime/components/react/mpx-input.tsx +139 -117
  95. package/lib/runtime/components/react/mpx-label.tsx +36 -34
  96. package/lib/runtime/components/react/mpx-movable-area.tsx +26 -39
  97. package/lib/runtime/components/react/mpx-movable-view.tsx +455 -337
  98. package/lib/runtime/components/react/mpx-navigator.tsx +3 -9
  99. package/lib/runtime/components/react/mpx-picker/date.tsx +5 -2
  100. package/lib/runtime/components/react/mpx-picker/index.tsx +3 -2
  101. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +5 -2
  102. package/lib/runtime/components/react/mpx-picker/region.tsx +5 -2
  103. package/lib/runtime/components/react/mpx-picker/selector.tsx +5 -2
  104. package/lib/runtime/components/react/mpx-picker/time.tsx +10 -15
  105. package/lib/runtime/components/react/mpx-picker/type.ts +48 -43
  106. package/lib/runtime/components/react/mpx-picker-view-column.tsx +236 -104
  107. package/lib/runtime/components/react/mpx-picker-view.tsx +132 -122
  108. package/lib/runtime/components/react/mpx-radio-group.tsx +77 -54
  109. package/lib/runtime/components/react/mpx-radio.tsx +46 -55
  110. package/lib/runtime/components/react/mpx-rich-text/html.ts +40 -0
  111. package/lib/runtime/components/react/mpx-rich-text/index.tsx +121 -0
  112. package/lib/runtime/components/react/mpx-root-portal.tsx +4 -6
  113. package/lib/runtime/components/react/mpx-scroll-view.tsx +122 -76
  114. package/lib/runtime/components/react/mpx-simple-text.tsx +18 -0
  115. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +6 -4
  116. package/lib/runtime/components/react/mpx-swiper/index.tsx +2 -1
  117. package/lib/runtime/components/react/mpx-swiper-item.tsx +4 -3
  118. package/lib/runtime/components/react/mpx-switch.tsx +39 -25
  119. package/lib/runtime/components/react/mpx-text.tsx +15 -19
  120. package/lib/runtime/components/react/mpx-textarea.tsx +12 -11
  121. package/lib/runtime/components/react/mpx-view.tsx +93 -77
  122. package/lib/runtime/components/react/mpx-web-view.tsx +117 -55
  123. package/lib/runtime/components/react/pickerFaces.ts +104 -0
  124. package/lib/runtime/components/react/pickerOverlay.tsx +32 -0
  125. package/lib/runtime/components/react/types/common.ts +2 -0
  126. package/lib/runtime/components/react/types/global.d.ts +5 -17
  127. package/lib/runtime/components/react/useAnimationHooks.ts +127 -18
  128. package/lib/runtime/components/react/useNodesRef.ts +1 -0
  129. package/lib/runtime/components/react/utils.tsx +113 -27
  130. package/lib/runtime/components/web/getInnerListeners.js +6 -6
  131. package/lib/runtime/components/web/mpx-movable-view.vue +334 -344
  132. package/lib/runtime/components/web/mpx-picker-view-column.vue +75 -75
  133. package/lib/runtime/components/web/mpx-picker.vue +382 -385
  134. package/lib/runtime/components/web/mpx-web-view.vue +175 -161
  135. package/lib/runtime/optionProcessor.js +7 -38
  136. package/lib/runtime/optionProcessorReact.js +0 -15
  137. package/lib/runtime/swanHelper.wxs +1 -1
  138. package/lib/runtime/utils.js +2 -0
  139. package/lib/style-compiler/index.js +1 -1
  140. package/lib/style-compiler/plugins/scope-id.js +31 -2
  141. package/lib/template-compiler/bind-this.js +7 -2
  142. package/lib/template-compiler/compiler.js +118 -56
  143. package/lib/template-compiler/gen-node-react.js +3 -3
  144. package/lib/template-compiler/index.js +4 -4
  145. package/lib/utils/pre-process-json.js +117 -0
  146. package/lib/web/index.js +5 -4
  147. package/lib/web/processJSON.js +5 -13
  148. package/lib/web/processTemplate.js +2 -2
  149. package/package.json +6 -4
  150. package/LICENSE +0 -433
  151. package/lib/runtime/components/react/dist/mpx-image/index.jsx +0 -226
  152. package/lib/runtime/components/react/dist/mpx-image/svg.jsx +0 -7
  153. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +0 -478
  154. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +0 -68
  155. package/lib/runtime/components/react/dist/mpx-swiper/type.js +0 -1
  156. package/lib/runtime/components/react/mpx-image/index.tsx +0 -346
  157. package/lib/runtime/components/react/mpx-image/svg.tsx +0 -22
  158. package/lib/runtime/components/web/event.js +0 -105
@@ -1,5 +1,6 @@
1
- import { useRef } from 'react'
2
- import { omit } from './utils'
1
+ import { useRef, useMemo, RefObject } from 'react'
2
+ import { hasOwn, collectDataset } from '@mpxjs/utils'
3
+ import { omit, extendObject } from './utils'
3
4
  import eventConfigMap from './event.config'
4
5
  import {
5
6
  Props,
@@ -8,7 +9,6 @@ import {
8
9
  UseInnerPropsConfig,
9
10
  InnerRef,
10
11
  SetTimeoutReturnType,
11
- DataSetType,
12
12
  LayoutRef,
13
13
  NativeTouchEvent
14
14
  } from './types/getInnerListeners'
@@ -20,31 +20,38 @@ const getTouchEvent = (
20
20
  config: UseInnerPropsConfig
21
21
  ) => {
22
22
  const nativeEvent = event.nativeEvent
23
- const {
24
- timestamp,
25
- pageX,
26
- pageY,
27
- touches,
28
- changedTouches
29
- } = nativeEvent
23
+ const { timestamp, pageX, pageY, touches, changedTouches } = nativeEvent
30
24
  const { id } = props
31
25
  const { layoutRef } = config
32
- return {
33
- ...event,
26
+
27
+ const currentTarget = extendObject({}, event.currentTarget, {
28
+ id: id || '',
29
+ dataset: collectDataset(props),
30
+ offsetLeft: layoutRef?.current?.offsetLeft || 0,
31
+ offsetTop: layoutRef?.current?.offsetTop || 0
32
+ })
33
+
34
+ const pendingProps = (event as any)._targetInst?.pendingProps || {}
35
+
36
+ const target = extendObject(
37
+ {},
38
+ event.target,
39
+ {
40
+ id: pendingProps.parentId || pendingProps.nativeID || '',
41
+ dataset: collectDataset(pendingProps)
42
+ }
43
+ )
44
+
45
+ return extendObject({}, event, {
34
46
  type,
35
47
  timeStamp: timestamp,
36
- currentTarget: {
37
- ...(event.currentTarget || {}),
38
- id: id || '',
39
- dataset: getDataSet(props),
40
- offsetLeft: layoutRef?.current?.offsetLeft || 0,
41
- offsetTop: layoutRef?.current?.offsetTop || 0
42
- },
48
+ currentTarget,
49
+ target,
43
50
  detail: {
44
51
  x: pageX,
45
52
  y: pageY
46
53
  },
47
- touches: touches.map(item => {
54
+ touches: touches.map((item) => {
48
55
  return {
49
56
  identifier: item.identifier,
50
57
  pageX: item.pageX,
@@ -53,7 +60,7 @@ const getTouchEvent = (
53
60
  clientY: item.locationY
54
61
  }
55
62
  }),
56
- changedTouches: changedTouches.map(item => {
63
+ changedTouches: changedTouches.map((item) => {
57
64
  return {
58
65
  identifier: item.identifier,
59
66
  pageX: item.pageX,
@@ -65,45 +72,196 @@ const getTouchEvent = (
65
72
  persist: event.persist,
66
73
  stopPropagation: event.stopPropagation,
67
74
  preventDefault: event.preventDefault
68
- }
69
- }
70
-
71
- export const getDataSet = (props: Record<string, any>) => {
72
- const result: DataSetType = {}
73
-
74
- for (const key in props) {
75
- if (key.indexOf('data-') === 0) {
76
- const newKey = key.substr(5)
77
- result[newKey] = props[key]
78
- }
79
- }
80
-
81
- return result
75
+ })
82
76
  }
83
77
 
84
78
  export const getCustomEvent = (
85
79
  type = '',
86
80
  oe: any = {},
87
- { detail = {}, layoutRef }: { detail?: Record<string, unknown>; layoutRef: LayoutRef },
81
+ {
82
+ detail = {},
83
+ layoutRef
84
+ }: { detail?: Record<string, unknown>; layoutRef?: LayoutRef },
88
85
  props: Props = {}
89
86
  ) => {
90
- return {
91
- ...oe,
87
+ const targetInfo = extendObject({}, oe.target, {
88
+ id: props.id || '',
89
+ dataset: collectDataset(props),
90
+ offsetLeft: layoutRef?.current?.offsetLeft || 0,
91
+ offsetTop: layoutRef?.current?.offsetTop || 0
92
+ })
93
+ return extendObject({}, oe, {
92
94
  type,
93
95
  detail,
94
- target: {
95
- ...(oe.target || {}),
96
- id: props.id || '',
97
- dataset: getDataSet(props),
98
- offsetLeft: layoutRef?.current?.offsetLeft || 0,
99
- offsetTop: layoutRef?.current?.offsetTop || 0
100
- },
96
+ target: targetInfo,
101
97
  persist: oe.persist,
102
98
  stopPropagation: oe.stopPropagation,
103
99
  preventDefault: oe.preventDefault
100
+ })
101
+ }
102
+
103
+ function handleEmitEvent (
104
+ events: string[],
105
+ type: string,
106
+ oe: NativeTouchEvent,
107
+ propsRef: Record<string, any>,
108
+ config: UseInnerPropsConfig
109
+ ) {
110
+ events.forEach((event) => {
111
+ if (propsRef.current[event]) {
112
+ const match = /^(catch|capture-catch):?(.*?)(?:\.(.*))?$/.exec(event)
113
+ if (match) {
114
+ oe.stopPropagation()
115
+ }
116
+ propsRef.current[event](
117
+ getTouchEvent(type, oe, propsRef.current, config)
118
+ )
119
+ }
120
+ })
121
+ }
122
+
123
+ function checkIsNeedPress (e: NativeTouchEvent, type: 'bubble' | 'capture', ref: RefObject<InnerRef>) {
124
+ const tapDetailInfo = ref.current!.mpxPressInfo.detail || { x: 0, y: 0 }
125
+ const nativeEvent = e.nativeEvent
126
+ const currentPageX = nativeEvent.changedTouches[0].pageX
127
+ const currentPageY = nativeEvent.changedTouches[0].pageY
128
+ if (
129
+ Math.abs(currentPageX - tapDetailInfo.x) > 1 ||
130
+ Math.abs(currentPageY - tapDetailInfo.y) > 1
131
+ ) {
132
+ ref.current!.needPress[type] = false
133
+ ref.current!.startTimer[type] &&
134
+ clearTimeout(ref.current!.startTimer[type] as SetTimeoutReturnType)
135
+ ref.current!.startTimer[type] = null
104
136
  }
105
137
  }
106
138
 
139
+ function handleTouchstart (e: NativeTouchEvent, type: 'bubble' | 'capture', ref: RefObject<InnerRef>, propsRef: Record<string, any>, config: UseInnerPropsConfig) {
140
+ e.persist()
141
+ const bubbleTouchEvent = ['catchtouchstart', 'bindtouchstart']
142
+ const bubblePressEvent = ['catchlongpress', 'bindlongpress']
143
+ const captureTouchEvent = [
144
+ 'capture-catchtouchstart',
145
+ 'capture-bindtouchstart'
146
+ ]
147
+ const capturePressEvent = [
148
+ 'capture-catchlongpress',
149
+ 'capture-bindlongpress'
150
+ ]
151
+ ref.current!.startTimer[type] = null
152
+ ref.current!.needPress[type] = true
153
+ const nativeEvent = e.nativeEvent
154
+ ref.current!.mpxPressInfo.detail = {
155
+ x: nativeEvent.changedTouches[0].pageX,
156
+ y: nativeEvent.changedTouches[0].pageY
157
+ }
158
+ const currentTouchEvent =
159
+ type === 'bubble' ? bubbleTouchEvent : captureTouchEvent
160
+ const currentPressEvent =
161
+ type === 'bubble' ? bubblePressEvent : capturePressEvent
162
+ handleEmitEvent(currentTouchEvent, 'touchstart', e, propsRef, config)
163
+ const {
164
+ catchlongpress,
165
+ bindlongpress,
166
+ 'capture-catchlongpress': captureCatchlongpress,
167
+ 'capture-bindlongpress': captureBindlongpress
168
+ } = propsRef.current
169
+ if (
170
+ catchlongpress ||
171
+ bindlongpress ||
172
+ captureCatchlongpress ||
173
+ captureBindlongpress
174
+ ) {
175
+ ref.current!.startTimer[type] = setTimeout(() => {
176
+ ref.current!.needPress[type] = false
177
+ handleEmitEvent(currentPressEvent, 'longpress', e, propsRef, config)
178
+ }, 350)
179
+ }
180
+ }
181
+
182
+ function handleTouchmove (e: NativeTouchEvent, type: 'bubble' | 'capture', ref: RefObject<InnerRef>, propsRef: Record<string, any>, config: UseInnerPropsConfig) {
183
+ const bubbleTouchEvent = ['catchtouchmove', 'bindtouchmove']
184
+ const captureTouchEvent = [
185
+ 'capture-catchtouchmove',
186
+ 'capture-bindtouchmove'
187
+ ]
188
+ const currentTouchEvent =
189
+ type === 'bubble' ? bubbleTouchEvent : captureTouchEvent
190
+ handleEmitEvent(currentTouchEvent, 'touchmove', e, propsRef, config)
191
+ checkIsNeedPress(e, type, ref)
192
+ }
193
+
194
+ function handleTouchend (e: NativeTouchEvent, type: 'bubble' | 'capture', ref: RefObject<InnerRef>, propsRef: Record<string, any>, config: UseInnerPropsConfig) {
195
+ // move event may not be triggered
196
+ checkIsNeedPress(e, type, ref)
197
+ const bubbleTouchEvent = ['catchtouchend', 'bindtouchend']
198
+ const bubbleTapEvent = ['catchtap', 'bindtap']
199
+ const captureTouchEvent = [
200
+ 'capture-catchtouchend',
201
+ 'capture-bindtouchend'
202
+ ]
203
+ const captureTapEvent = ['capture-catchtap', 'capture-bindtap']
204
+ const currentTouchEvent =
205
+ type === 'bubble' ? bubbleTouchEvent : captureTouchEvent
206
+ const currentTapEvent =
207
+ type === 'bubble' ? bubbleTapEvent : captureTapEvent
208
+ ref.current!.startTimer[type] &&
209
+ clearTimeout(ref.current!.startTimer[type] as SetTimeoutReturnType)
210
+ ref.current!.startTimer[type] = null
211
+ handleEmitEvent(currentTouchEvent, 'touchend', e, propsRef, config)
212
+ if (ref.current!.needPress[type]) {
213
+ if (type === 'bubble' && config.disableTap) {
214
+ return
215
+ }
216
+ handleEmitEvent(currentTapEvent, 'tap', e, propsRef, config)
217
+ }
218
+ }
219
+
220
+ function handleTouchcancel (
221
+ e: NativeTouchEvent,
222
+ type: 'bubble' | 'capture',
223
+ ref: RefObject<InnerRef>, propsRef: Record<string, any>, config: UseInnerPropsConfig
224
+ ) {
225
+ const bubbleTouchEvent = ['catchtouchcancel', 'bindtouchcancel']
226
+ const captureTouchEvent = [
227
+ 'capture-catchtouchcancel',
228
+ 'capture-bindtouchcancel'
229
+ ]
230
+ const currentTouchEvent =
231
+ type === 'bubble' ? bubbleTouchEvent : captureTouchEvent
232
+ ref.current!.startTimer[type] &&
233
+ clearTimeout(ref.current!.startTimer[type] as SetTimeoutReturnType)
234
+ ref.current!.startTimer[type] = null
235
+ handleEmitEvent(currentTouchEvent, 'touchcancel', e, propsRef, config)
236
+ }
237
+
238
+ function createTouchEventHandler (eventName: 'onTouchStart'|'onTouchMove'|'onTouchEnd'|'onTouchCancel', type: 'bubble' | 'capture') {
239
+ return (e: NativeTouchEvent, ref: RefObject<InnerRef>, propsRef: Record<string, any>, config: UseInnerPropsConfig) => {
240
+ const handlerMap = {
241
+ onTouchStart: handleTouchstart,
242
+ onTouchMove: handleTouchmove,
243
+ onTouchEnd: handleTouchend,
244
+ onTouchCancel: handleTouchcancel
245
+ }
246
+
247
+ const handler = handlerMap[eventName]
248
+ if (handler) {
249
+ handler(e, type, ref, propsRef, config)
250
+ }
251
+ }
252
+ }
253
+
254
+ const touchEventList = [
255
+ { eventName: 'onTouchStart', handler: createTouchEventHandler('onTouchStart', 'bubble') },
256
+ { eventName: 'onTouchMove', handler: createTouchEventHandler('onTouchMove', 'bubble') },
257
+ { eventName: 'onTouchEnd', handler: createTouchEventHandler('onTouchEnd', 'bubble') },
258
+ { eventName: 'onTouchCancel', handler: createTouchEventHandler('onTouchCancel', 'bubble') },
259
+ { eventName: 'onTouchStartCapture', handler: createTouchEventHandler('onTouchStart', 'capture') },
260
+ { eventName: 'onTouchMoveCapture', handler: createTouchEventHandler('onTouchMove', 'capture') },
261
+ { eventName: 'onTouchEndCapture', handler: createTouchEventHandler('onTouchEnd', 'capture') },
262
+ { eventName: 'onTouchCancelCapture', handler: createTouchEventHandler('onTouchCancel', 'capture') }
263
+ ]
264
+
107
265
  const useInnerProps = (
108
266
  props: Props = {},
109
267
  additionalProps: AdditionalProps = {},
@@ -129,7 +287,11 @@ const useInnerProps = (
129
287
 
130
288
  const propsRef = useRef<Record<string, any>>({})
131
289
  const eventConfig: { [key: string]: string[] } = {}
132
- const config = rawConfig || { layoutRef: { current: {} }, disableTouch: false, disableTap: false }
290
+ const config = rawConfig || {
291
+ layoutRef: { current: {} },
292
+ disableTouch: false,
293
+ disableTap: false
294
+ }
133
295
  const removeProps = [
134
296
  'children',
135
297
  'enable-background',
@@ -142,170 +304,47 @@ const useInnerProps = (
142
304
  ...userRemoveProps
143
305
  ]
144
306
 
145
- propsRef.current = { ...props, ...additionalProps }
307
+ propsRef.current = extendObject({}, props, additionalProps)
308
+
309
+ let hashEventKey = ''
310
+ const rawEventKeys: Array<string> = []
146
311
 
147
312
  for (const key in eventConfigMap) {
148
- if (propsRef.current[key]) {
149
- eventConfig[key] = eventConfigMap[key]
313
+ if (hasOwn(propsRef.current, key)) {
314
+ eventConfig[key] = eventConfigMap[key].events
315
+ hashEventKey = hashEventKey + eventConfigMap[key].bitFlag
316
+ rawEventKeys.push(key)
150
317
  }
151
318
  }
152
319
 
153
- if (!(Object.keys(eventConfig).length) || config.disableTouch) {
320
+ if (!rawEventKeys.length || config.disableTouch) {
154
321
  return omit(propsRef.current, removeProps)
155
322
  }
156
323
 
157
- function handleEmitEvent (
158
- events: string[],
159
- type: string,
160
- oe: NativeTouchEvent
161
- ) {
162
- events.forEach(event => {
163
- if (propsRef.current[event]) {
164
- const match = /^(catch|capture-catch):?(.*?)(?:\.(.*))?$/.exec(event)
165
- if (match) {
166
- oe.stopPropagation()
167
- }
168
- propsRef.current[event](getTouchEvent(type, oe, propsRef.current, config))
324
+ const events = useMemo(() => {
325
+ const transformedEventKeys = rawEventKeys.reduce((acc: string[], key) => {
326
+ if (propsRef.current[key]) {
327
+ return acc.concat(eventConfig[key])
169
328
  }
170
- })
171
- }
172
-
173
- function checkIsNeedPress (e: NativeTouchEvent, type: 'bubble' | 'capture') {
174
- const tapDetailInfo = ref.current.mpxPressInfo.detail || { x: 0, y: 0 }
175
- const nativeEvent = e.nativeEvent
176
- const currentPageX = nativeEvent.changedTouches[0].pageX
177
- const currentPageY = nativeEvent.changedTouches[0].pageY
178
- if (Math.abs(currentPageX - tapDetailInfo.x) > 1 || Math.abs(currentPageY - tapDetailInfo.y) > 1) {
179
- ref.current.needPress[type] = false
180
- ref.current.startTimer[type] && clearTimeout(ref.current.startTimer[type] as SetTimeoutReturnType)
181
- ref.current.startTimer[type] = null
182
- }
183
- }
184
-
185
- function handleTouchstart (e: NativeTouchEvent, type: 'bubble' | 'capture') {
186
- e.persist()
187
- const bubbleTouchEvent = ['catchtouchstart', 'bindtouchstart']
188
- const bubblePressEvent = ['catchlongpress', 'bindlongpress']
189
- const captureTouchEvent = ['capture-catchtouchstart', 'capture-bindtouchstart']
190
- const capturePressEvent = ['capture-catchlongpress', 'capture-bindlongpress']
191
- ref.current.startTimer[type] = null
192
- ref.current.needPress[type] = true
193
- const nativeEvent = e.nativeEvent
194
- ref.current.mpxPressInfo.detail = {
195
- x: nativeEvent.changedTouches[0].pageX,
196
- y: nativeEvent.changedTouches[0].pageY
197
- }
198
- const currentTouchEvent = type === 'bubble' ? bubbleTouchEvent : captureTouchEvent
199
- const currentPressEvent = type === 'bubble' ? bubblePressEvent : capturePressEvent
200
- handleEmitEvent(currentTouchEvent, 'touchstart', e)
201
- const { catchlongpress, bindlongpress, 'capture-catchlongpress': captureCatchlongpress, 'capture-bindlongpress': captureBindlongpress } = propsRef.current
202
- if (catchlongpress || bindlongpress || captureCatchlongpress || captureBindlongpress) {
203
- ref.current.startTimer[type] = setTimeout(() => {
204
- ref.current.needPress[type] = false
205
- handleEmitEvent(currentPressEvent, 'longpress', e)
206
- }, 350)
207
- }
208
- }
209
-
210
- function handleTouchmove (e: NativeTouchEvent, type: 'bubble' | 'capture') {
211
- const bubbleTouchEvent = ['catchtouchmove', 'bindtouchmove']
212
- const captureTouchEvent = ['capture-catchtouchmove', 'capture-bindtouchmove']
213
- const currentTouchEvent = type === 'bubble' ? bubbleTouchEvent : captureTouchEvent
214
- handleEmitEvent(currentTouchEvent, 'touchmove', e)
215
- checkIsNeedPress(e, type)
216
- }
329
+ return acc
330
+ }, [])
331
+ const finalEventKeys = [...new Set(transformedEventKeys)]
332
+ const events: Record<string, (e: NativeTouchEvent) => void> = {}
217
333
 
218
- function handleTouchend (e: NativeTouchEvent, type: 'bubble' | 'capture') {
219
- // move event may not be triggered
220
- checkIsNeedPress(e, type)
221
- const bubbleTouchEvent = ['catchtouchend', 'bindtouchend']
222
- const bubbleTapEvent = ['catchtap', 'bindtap']
223
- const captureTouchEvent = ['capture-catchtouchend', 'capture-bindtouchend']
224
- const captureTapEvent = ['capture-catchtap', 'capture-bindtap']
225
- const currentTouchEvent = type === 'bubble' ? bubbleTouchEvent : captureTouchEvent
226
- const currentTapEvent = type === 'bubble' ? bubbleTapEvent : captureTapEvent
227
- ref.current.startTimer[type] && clearTimeout(ref.current.startTimer[type] as SetTimeoutReturnType)
228
- ref.current.startTimer[type] = null
229
- handleEmitEvent(currentTouchEvent, 'touchend', e)
230
- if (ref.current.needPress[type]) {
231
- if (type === 'bubble' && config.disableTap) {
232
- return
334
+ touchEventList.forEach((item) => {
335
+ if (finalEventKeys.includes(item.eventName)) {
336
+ events[item.eventName] = (e: NativeTouchEvent) =>
337
+ item.handler(e, ref, propsRef, config)
233
338
  }
234
- handleEmitEvent(currentTapEvent, 'tap', e)
235
- }
236
- }
237
-
238
- function handleTouchcancel (e: NativeTouchEvent, type: 'bubble' | 'capture') {
239
- const bubbleTouchEvent = ['catchtouchcancel', 'bindtouchcancel']
240
- const captureTouchEvent = ['capture-catchtouchcancel', 'capture-bindtouchcancel']
241
- const currentTouchEvent = type === 'bubble' ? bubbleTouchEvent : captureTouchEvent
242
- ref.current.startTimer[type] && clearTimeout(ref.current.startTimer[type] as SetTimeoutReturnType)
243
- ref.current.startTimer[type] = null
244
- handleEmitEvent(currentTouchEvent, 'touchcancel', e)
245
- }
246
-
247
- const touchEventList = [{
248
- eventName: 'onTouchStart',
249
- handler: (e: NativeTouchEvent) => {
250
- handleTouchstart(e, 'bubble')
251
- }
252
- }, {
253
- eventName: 'onTouchMove',
254
- handler: (e: NativeTouchEvent) => {
255
- handleTouchmove(e, 'bubble')
256
- }
257
- }, {
258
- eventName: 'onTouchEnd',
259
- handler: (e: NativeTouchEvent) => {
260
- handleTouchend(e, 'bubble')
261
- }
262
- }, {
263
- eventName: 'onTouchCancel',
264
- handler: (e: NativeTouchEvent) => {
265
- handleTouchcancel(e, 'bubble')
266
- }
267
- }, {
268
- eventName: 'onTouchStartCapture',
269
- handler: (e: NativeTouchEvent) => {
270
- handleTouchstart(e, 'capture')
271
- }
272
- }, {
273
- eventName: 'onTouchMoveCapture',
274
- handler: (e: NativeTouchEvent) => {
275
- handleTouchmove(e, 'capture')
276
- }
277
- }, {
278
- eventName: 'onTouchEndCapture',
279
- handler: (e: NativeTouchEvent) => {
280
- handleTouchend(e, 'capture')
281
- }
282
- }, {
283
- eventName: 'onTouchCancelCapture',
284
- handler: (e: NativeTouchEvent) => {
285
- handleTouchcancel(e, 'capture')
286
- }
287
- }]
288
-
289
- const events: Record<string, (e: NativeTouchEvent) => void> = {}
290
-
291
- const transformedEventKeys: string[] = []
292
- for (const key in eventConfig) {
293
- transformedEventKeys.push(...eventConfig[key])
294
- }
295
-
296
- const finalEventKeys = [...new Set(transformedEventKeys)]
297
-
298
- touchEventList.forEach(item => {
299
- if (finalEventKeys.includes(item.eventName)) {
300
- events[item.eventName] = item.handler
301
- }
302
- })
339
+ })
303
340
 
304
- const rawEventKeys = Object.keys(eventConfig)
341
+ return events
342
+ }, [hashEventKey])
305
343
 
306
- return {
307
- ...events,
308
- ...omit(propsRef.current, [...rawEventKeys, ...removeProps])
309
- }
344
+ return extendObject(
345
+ {},
346
+ events,
347
+ omit(propsRef.current, [...rawEventKeys, ...removeProps])
348
+ )
310
349
  }
311
350
  export default useInnerProps