@mpxjs/webpack-plugin 2.9.67 → 2.9.70-alpha.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 (161) hide show
  1. package/README.md +1 -1
  2. package/lib/config.js +14 -0
  3. package/lib/dependencies/AddEntryDependency.js +24 -0
  4. package/lib/dependencies/ResolveDependency.js +5 -0
  5. package/lib/index.js +51 -15
  6. package/lib/json-compiler/helper.js +3 -3
  7. package/lib/loader.js +53 -0
  8. package/lib/platform/template/wx/component-config/button.js +14 -2
  9. package/lib/platform/template/wx/component-config/canvas.js +8 -0
  10. package/lib/platform/template/wx/component-config/image.js +4 -0
  11. package/lib/platform/template/wx/component-config/input.js +5 -1
  12. package/lib/platform/template/wx/component-config/rich-text.js +4 -0
  13. package/lib/platform/template/wx/component-config/scroll-view.js +4 -0
  14. package/lib/platform/template/wx/component-config/swiper.js +1 -1
  15. package/lib/platform/template/wx/component-config/switch.js +4 -0
  16. package/lib/platform/template/wx/component-config/text.js +4 -0
  17. package/lib/platform/template/wx/component-config/textarea.js +6 -1
  18. package/lib/platform/template/wx/component-config/unsupported.js +1 -1
  19. package/lib/platform/template/wx/component-config/view.js +4 -0
  20. package/lib/platform/template/wx/index.js +127 -1
  21. package/lib/react/processStyles.js +14 -4
  22. package/lib/react/processTemplate.js +3 -0
  23. package/lib/resolve-loader.js +4 -1
  24. package/lib/resolver/AddModePlugin.js +8 -8
  25. package/lib/runtime/components/react/context.ts +6 -0
  26. package/lib/runtime/components/react/dist/context.js +2 -0
  27. package/lib/runtime/components/react/dist/event.config.js +24 -24
  28. package/lib/runtime/components/react/dist/getInnerListeners.js +183 -174
  29. package/lib/runtime/components/react/dist/mpx-button.jsx +77 -49
  30. package/lib/runtime/components/react/dist/mpx-canvas/Bus.js +60 -0
  31. package/lib/runtime/components/react/dist/mpx-canvas/CanvasGradient.js +15 -0
  32. package/lib/runtime/components/react/dist/mpx-canvas/CanvasRenderingContext2D.js +84 -0
  33. package/lib/runtime/components/react/dist/mpx-canvas/Image.js +87 -0
  34. package/lib/runtime/components/react/dist/mpx-canvas/ImageData.js +15 -0
  35. package/lib/runtime/components/react/dist/mpx-canvas/constructorsRegistry.js +28 -0
  36. package/lib/runtime/components/react/dist/mpx-canvas/html.js +343 -0
  37. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +232 -0
  38. package/lib/runtime/components/react/dist/mpx-canvas/utils.jsx +89 -0
  39. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +13 -19
  40. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +29 -38
  41. package/lib/runtime/components/react/dist/mpx-form.jsx +16 -19
  42. package/lib/runtime/components/react/dist/mpx-icon.jsx +8 -16
  43. package/lib/runtime/components/react/dist/mpx-image.jsx +291 -0
  44. package/lib/runtime/components/react/dist/mpx-input.jsx +54 -27
  45. package/lib/runtime/components/react/dist/mpx-label.jsx +15 -22
  46. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +13 -16
  47. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +13 -13
  48. package/lib/runtime/components/react/dist/mpx-navigator.jsx +2 -4
  49. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +6 -2
  50. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +5 -3
  51. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +6 -2
  52. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +6 -2
  53. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +6 -2
  54. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +10 -15
  55. package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +39 -0
  56. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +160 -88
  57. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +80 -121
  58. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +11 -19
  59. package/lib/runtime/components/react/dist/mpx-radio.jsx +27 -42
  60. package/lib/runtime/components/react/dist/mpx-rich-text/html.js +39 -0
  61. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +63 -0
  62. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +6 -4
  63. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +47 -41
  64. package/lib/runtime/components/react/dist/mpx-simple-text.jsx +11 -0
  65. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +4 -2
  66. package/lib/runtime/components/react/dist/mpx-swiper.jsx +606 -0
  67. package/lib/runtime/components/react/dist/mpx-switch.jsx +20 -10
  68. package/lib/runtime/components/react/dist/mpx-text.jsx +11 -10
  69. package/lib/runtime/components/react/dist/mpx-textarea.jsx +8 -3
  70. package/lib/runtime/components/react/dist/mpx-view.jsx +65 -61
  71. package/lib/runtime/components/react/dist/mpx-web-view.jsx +112 -35
  72. package/lib/runtime/components/react/dist/pickerFaces.js +81 -0
  73. package/lib/runtime/components/react/dist/pickerVIewContext.js +9 -0
  74. package/lib/runtime/components/react/dist/pickerViewMask.jsx +18 -0
  75. package/lib/runtime/components/react/dist/pickerViewOverlay.jsx +23 -0
  76. package/lib/runtime/components/react/dist/useAnimationHooks.js +35 -9
  77. package/lib/runtime/components/react/dist/utils.jsx +70 -23
  78. package/lib/runtime/components/react/getInnerListeners.ts +36 -43
  79. package/lib/runtime/components/react/mpx-button.tsx +95 -43
  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 +302 -0
  88. package/lib/runtime/components/react/mpx-canvas/utils.tsx +150 -0
  89. package/lib/runtime/components/react/mpx-checkbox-group.tsx +13 -12
  90. package/lib/runtime/components/react/mpx-checkbox.tsx +28 -28
  91. package/lib/runtime/components/react/mpx-form.tsx +10 -8
  92. package/lib/runtime/components/react/mpx-icon.tsx +10 -15
  93. package/lib/runtime/components/react/mpx-image.tsx +396 -0
  94. package/lib/runtime/components/react/mpx-input.tsx +61 -33
  95. package/lib/runtime/components/react/mpx-label.tsx +14 -13
  96. package/lib/runtime/components/react/mpx-movable-area.tsx +8 -7
  97. package/lib/runtime/components/react/mpx-movable-view.tsx +1 -1
  98. package/lib/runtime/components/react/mpx-picker/date.tsx +5 -2
  99. package/lib/runtime/components/react/mpx-picker/index.tsx +3 -2
  100. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +5 -2
  101. package/lib/runtime/components/react/mpx-picker/region.tsx +5 -2
  102. package/lib/runtime/components/react/mpx-picker/selector.tsx +5 -2
  103. package/lib/runtime/components/react/mpx-picker/time.tsx +10 -15
  104. package/lib/runtime/components/react/mpx-picker/type.ts +48 -43
  105. package/lib/runtime/components/react/mpx-picker-view-column.tsx +236 -104
  106. package/lib/runtime/components/react/mpx-picker-view.tsx +132 -122
  107. package/lib/runtime/components/react/mpx-radio-group.tsx +11 -12
  108. package/lib/runtime/components/react/mpx-radio.tsx +26 -29
  109. package/lib/runtime/components/react/mpx-scroll-view.tsx +32 -30
  110. package/lib/runtime/components/react/mpx-simple-text.tsx +18 -0
  111. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +4 -2
  112. package/lib/runtime/components/react/mpx-swiper-item.tsx +3 -2
  113. package/lib/runtime/components/react/mpx-switch.tsx +10 -8
  114. package/lib/runtime/components/react/mpx-text.tsx +6 -2
  115. package/lib/runtime/components/react/mpx-view.tsx +81 -59
  116. package/lib/runtime/components/react/mpx-web-view.tsx +46 -19
  117. package/lib/runtime/components/react/pickerFaces.ts +104 -0
  118. package/lib/runtime/components/react/pickerOverlay.tsx +32 -0
  119. package/lib/runtime/components/react/types/common.ts +2 -0
  120. package/lib/runtime/components/react/types/global.d.ts +3 -16
  121. package/lib/runtime/components/react/utils.tsx +98 -27
  122. package/lib/runtime/components/tenon/getInnerListeners.js +334 -0
  123. package/lib/runtime/components/tenon/tenon-button.vue +309 -0
  124. package/lib/runtime/components/tenon/tenon-image.vue +66 -0
  125. package/lib/runtime/components/tenon/tenon-input.vue +171 -0
  126. package/lib/runtime/components/tenon/tenon-rich-text.vue +26 -0
  127. package/lib/runtime/components/tenon/tenon-scroll-view.vue +127 -0
  128. package/lib/runtime/components/tenon/tenon-switch.vue +96 -0
  129. package/lib/runtime/components/tenon/tenon-text.vue +70 -0
  130. package/lib/runtime/components/tenon/tenon-textarea.vue +86 -0
  131. package/lib/runtime/components/tenon/tenon-view.vue +93 -0
  132. package/lib/runtime/components/web/getInnerListeners.js +6 -6
  133. package/lib/runtime/components/web/mpx-movable-view.vue +334 -344
  134. package/lib/runtime/components/web/mpx-picker-view-column.vue +75 -75
  135. package/lib/runtime/components/web/mpx-picker.vue +382 -385
  136. package/lib/runtime/components/web/mpx-web-view.vue +162 -162
  137. package/lib/runtime/optionProcessor.js +7 -16
  138. package/lib/runtime/optionProcessor.tenon.js +84 -0
  139. package/lib/runtime/utils.js +2 -0
  140. package/lib/style-compiler/index.js +1 -1
  141. package/lib/style-compiler/plugins/hm.js +20 -0
  142. package/lib/template-compiler/bind-this.js +7 -2
  143. package/lib/template-compiler/compiler.js +70 -42
  144. package/lib/template-compiler/gen-node-react.js +3 -3
  145. package/lib/tenon/index.js +117 -0
  146. package/lib/tenon/processJSON.js +352 -0
  147. package/lib/tenon/processScript.js +203 -0
  148. package/lib/tenon/processStyles.js +21 -0
  149. package/lib/tenon/processTemplate.js +126 -0
  150. package/lib/tenon/script-helper.js +223 -0
  151. package/lib/utils/env.js +6 -1
  152. package/lib/utils/get-relative-path.js +25 -0
  153. package/package.json +9 -4
  154. package/LICENSE +0 -433
  155. package/lib/runtime/components/react/dist/mpx-image/index.jsx +0 -226
  156. package/lib/runtime/components/react/dist/mpx-image/svg.jsx +0 -7
  157. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +0 -478
  158. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +0 -68
  159. package/lib/runtime/components/react/dist/mpx-swiper/type.js +0 -1
  160. package/lib/runtime/components/react/mpx-image/index.tsx +0 -345
  161. package/lib/runtime/components/react/mpx-image/svg.tsx +0 -22
@@ -0,0 +1,302 @@
1
+ /**
2
+ * ✘ type
3
+ * ✘ canvas-id
4
+ * ✘ disable-scroll
5
+ * ✔ bindtouchstart
6
+ * ✔ bindtouchmove
7
+ * ✔ bindtouchend
8
+ * ✔ bindtouchcancel
9
+ * ✔ bindlongtap
10
+ * ✔ binderror
11
+ */
12
+ import React, { useRef, useState, useCallback, useEffect, forwardRef, JSX, TouchEvent, MutableRefObject } from 'react'
13
+ import { View, Platform, StyleSheet, NativeSyntheticEvent } from 'react-native'
14
+ import { WebView } from 'react-native-webview'
15
+ import useNodesRef, { HandlerRef } from '../useNodesRef'
16
+ import { useLayout, useTransformStyle, extendObject } from '../utils'
17
+ import useInnerProps, { getCustomEvent } from '../getInnerListeners'
18
+ import Bus from './Bus'
19
+ import {
20
+ useWebviewBinding,
21
+ constructors,
22
+ WEBVIEW_TARGET,
23
+ WebviewMessage,
24
+ ID,
25
+ CanvasInstance,
26
+ registerWebviewConstructor
27
+ } from './utils'
28
+ import CanvasRenderingContext2D from './CanvasRenderingContext2D'
29
+ import html from './html'
30
+ import './CanvasGradient'
31
+ import { createImage as canvasCreateImage } from './Image'
32
+ import { createImageData as canvasCreateImageData } from './ImageData'
33
+ import { useConstructorsRegistry } from './constructorsRegistry'
34
+
35
+ const stylesheet = StyleSheet.create({
36
+ container: { overflow: 'hidden', flex: 0 },
37
+ webview: {
38
+ overflow: 'hidden',
39
+ backgroundColor: 'transparent',
40
+ flex: 0
41
+ },
42
+ webviewAndroid9: {
43
+ overflow: 'hidden',
44
+ backgroundColor: 'transparent',
45
+ flex: 0,
46
+ opacity: 0.99
47
+ }
48
+ })
49
+
50
+ interface CanvasProps {
51
+ style?: Record<string, any>;
52
+ originWhitelist?: Array<string>;
53
+ 'enable-var'?: boolean
54
+ 'parent-font-size'?: number
55
+ 'parent-width'?: number
56
+ 'parent-height'?: number
57
+ 'external-var-context'?: Record<string, any>
58
+ bindtouchstart?: (event: NativeSyntheticEvent<TouchEvent>) => void;
59
+ bindtouchmove?: (event: NativeSyntheticEvent<TouchEvent>) => void;
60
+ bindtouchend?: (event: NativeSyntheticEvent<TouchEvent>) => void;
61
+ bindtouchcancel?: (event: NativeSyntheticEvent<TouchEvent>) => void;
62
+ bindlongtap?: (event: NativeSyntheticEvent<TouchEvent>) => void;
63
+ binderror?: (event: NativeSyntheticEvent<ErrorEvent>) => void;
64
+ }
65
+
66
+ const _Canvas = forwardRef<HandlerRef<CanvasProps & View, CanvasProps>, CanvasProps>((props: CanvasProps = {}, ref): JSX.Element => {
67
+ const { style = {}, originWhitelist = ['*'], 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight } = props
68
+ const [isLoaded, setIsLoaded] = useState(false)
69
+ const nodeRef = useRef(null)
70
+
71
+ const {
72
+ normalStyle,
73
+ hasSelfPercent,
74
+ setWidth,
75
+ setHeight
76
+ } = useTransformStyle(extendObject({}, style, stylesheet.container), {
77
+ enableVar,
78
+ externalVarContext,
79
+ parentFontSize,
80
+ parentWidth,
81
+ parentHeight
82
+ })
83
+
84
+ const { width, height } = normalStyle
85
+ const canvasRef = useWebviewBinding({
86
+ targetName: 'canvas',
87
+ properties: { width, height },
88
+ methods: ['toDataURL']
89
+ }) as MutableRefObject<CanvasInstance>
90
+
91
+ const { register } = useConstructorsRegistry()
92
+
93
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
94
+ const innerProps = useInnerProps(props, {
95
+ ref: nodeRef,
96
+ style: extendObject({}, normalStyle, layoutStyle, { opacity: isLoaded ? 1 : 0 }),
97
+ ...layoutProps
98
+ }, [], {
99
+ layoutRef
100
+ })
101
+
102
+ const context2D = new CanvasRenderingContext2D(canvasRef.current) as any
103
+
104
+ register(registerWebviewConstructor)
105
+ // 初始化bus和context2D
106
+ useEffect(() => {
107
+ const webviewPostMessage = (message: WebviewMessage) => {
108
+ if (canvasRef.current.webview) {
109
+ canvasRef.current.webview.postMessage(JSON.stringify(message))
110
+ }
111
+ }
112
+
113
+ // 设置bus
114
+ canvasRef.current.bus = new Bus(webviewPostMessage)
115
+ canvasRef.current.bus.pause()
116
+
117
+ // 设置 context 2D
118
+ canvasRef.current.context2D = context2D
119
+
120
+ // 设置 getContext 方法
121
+ canvasRef.current.getContext = getContext
122
+
123
+ // 设置 createImage 方法
124
+ canvasRef.current.createImage = createImage
125
+
126
+ // 设置 postMessage 方法
127
+ canvasRef.current.postMessage = postMessage
128
+
129
+ // 设置 listeners
130
+ canvasRef.current.listeners = []
131
+
132
+ canvasRef.current.addMessageListener = addMessageListener
133
+
134
+ canvasRef.current.removeMessageListener = removeMessageListener
135
+
136
+ canvasRef.current.createImageData = createImageData
137
+ return () => {
138
+ canvasRef.current.bus?.clearBatchingTimeout()
139
+ }
140
+ }, [])
141
+
142
+ const createImageData = (dataArray: Array<number>, width: number, height: number) => {
143
+ return canvasCreateImageData(canvasRef.current, dataArray, width, height)
144
+ }
145
+ const createImage = (width?: number, height?: number) => {
146
+ return canvasCreateImage(canvasRef.current, width, height)
147
+ }
148
+ const getContext = useCallback((contextType: string) => {
149
+ if (contextType === '2d') {
150
+ return context2D
151
+ }
152
+ return null
153
+ }, [])
154
+
155
+ const postMessage = useCallback(async (message: WebviewMessage) => {
156
+ if (!canvasRef.current?.bus) return
157
+ const { type, payload } = await canvasRef.current.bus.post({
158
+ id: ID(),
159
+ ...message
160
+ })
161
+
162
+ switch (type) {
163
+ case 'error': {
164
+ const { binderror } = props
165
+ binderror &&
166
+ binderror(
167
+ getCustomEvent('error', {}, {
168
+ detail: {
169
+ errMsg: payload.message
170
+ },
171
+ layoutRef
172
+ }, props)
173
+ )
174
+ break
175
+ }
176
+ case 'json': {
177
+ return payload
178
+ }
179
+ case 'blob': {
180
+ return atob(payload)
181
+ }
182
+ }
183
+ }, [])
184
+
185
+ const addMessageListener = (listener: any) => {
186
+ canvasRef.current.listeners.push(listener)
187
+ return () => canvasRef.current.removeMessageListener(listener)
188
+ }
189
+
190
+ const removeMessageListener = (listener: any) => {
191
+ canvasRef.current.listeners.splice(canvasRef.current.listeners.indexOf(listener), 1)
192
+ }
193
+
194
+ const onMessage = useCallback((e: { nativeEvent: { data: string } }) => {
195
+ let data = JSON.parse(e.nativeEvent.data)
196
+ switch (data.type) {
197
+ case 'error': {
198
+ const { binderror } = props
199
+ binderror &&
200
+ binderror(
201
+ getCustomEvent('error', e, {
202
+ detail: {
203
+ errMsg: data.payload.message
204
+ },
205
+ layoutRef
206
+ }, props)
207
+ )
208
+ break
209
+ }
210
+ default: {
211
+ if (data.payload) {
212
+ // createLinearGradient 方法调用需要在 constructors 中需要注册 CanvasGradient
213
+ const constructor = constructors[data.meta.constructor]
214
+ if (constructor) {
215
+ const { args, payload } = data
216
+ // RN 端同步生成一个 CanvasGradient 的实例
217
+ const object = constructor.constructLocally(canvasRef.current, ...args)
218
+ Object.assign(object, payload, {
219
+ [WEBVIEW_TARGET]: data.meta.target
220
+ })
221
+ data = {
222
+ ...data,
223
+ payload: object
224
+ }
225
+ }
226
+ for (const listener of canvasRef.current.listeners) {
227
+ listener(data.payload)
228
+ }
229
+ }
230
+ if (canvasRef.current.bus) {
231
+ canvasRef.current.bus.handle(data)
232
+ }
233
+ }
234
+ }
235
+ }, [])
236
+
237
+ const onLoad = useCallback(() => {
238
+ setIsLoaded(true)
239
+ if (canvasRef.current?.bus) {
240
+ canvasRef.current.bus.resume()
241
+ }
242
+ }, [])
243
+
244
+ useNodesRef(props, ref, nodeRef, {
245
+ style: normalStyle,
246
+ node: canvasRef.current,
247
+ context: context2D
248
+ })
249
+
250
+ if (Platform.OS === 'android') {
251
+ const isAndroid9 = Platform.Version >= 28
252
+ return (
253
+ <View {...innerProps}>
254
+ <WebView
255
+ ref={(element) => {
256
+ if (canvasRef.current) {
257
+ canvasRef.current.webview = element
258
+ }
259
+ }}
260
+ style={[
261
+ isAndroid9 ? stylesheet.webviewAndroid9 : stylesheet.webview,
262
+ { height, width }
263
+ ]}
264
+ source={{ html }}
265
+ originWhitelist={originWhitelist}
266
+ onMessage={onMessage}
267
+ onLoad={onLoad}
268
+ overScrollMode="never"
269
+ mixedContentMode="always"
270
+ scalesPageToFit={false}
271
+ javaScriptEnabled
272
+ domStorageEnabled
273
+ thirdPartyCookiesEnabled
274
+ allowUniversalAccessFromFileURLs
275
+ />
276
+ </View>
277
+ )
278
+ }
279
+
280
+ return (
281
+ <View
282
+ {...innerProps}
283
+ >
284
+ <WebView
285
+ ref={(element) => {
286
+ if (canvasRef.current) {
287
+ canvasRef.current.webview = element
288
+ }
289
+ }}
290
+ style={[stylesheet.webview, { height, width }]}
291
+ source={{ html }}
292
+ originWhitelist={originWhitelist}
293
+ onMessage={onMessage}
294
+ onLoad={onLoad}
295
+ scrollEnabled={false}
296
+ />
297
+ </View>
298
+ )
299
+ })
300
+ _Canvas.displayName = 'mpxCanvas'
301
+
302
+ export default _Canvas
@@ -0,0 +1,150 @@
1
+ import { useEffect, useRef } from 'react'
2
+ import { WebView } from 'react-native-webview'
3
+ import Bus from './Bus'
4
+
5
+ export const WEBVIEW_TARGET = '@@WEBVIEW_TARGET'
6
+
7
+ export const constructors: Record<string, any> = {}
8
+
9
+ export const ID = () => Math.random().toString(32).slice(2)
10
+
11
+ const SPECIAL_CONSTRUCTOR: Record<string, { className: string, paramNum: number }> = {
12
+ ImageData: {
13
+ className: 'Uint8ClampedArray',
14
+ paramNum: 0
15
+ }
16
+ }
17
+
18
+ export interface Instance {
19
+ postMessage: (...args: any[]) => void;
20
+ [WEBVIEW_TARGET]?: string;
21
+ [key: string]: any;
22
+ }
23
+
24
+ export interface WebviewConstructor {
25
+ new (...args: any[]): Instance;
26
+ constructLocally?: (...args: unknown[]) => Instance;
27
+ }
28
+
29
+ export interface WebviewMessage {
30
+ type: 'set' | 'exec' | 'listen' | 'event' | 'construct'
31
+ payload: {
32
+ target?: string | { [key: string]: any }
33
+ key?: string
34
+ value?: any
35
+ method?: string
36
+ args?: any[]
37
+ types?: string[]
38
+ type?: string
39
+ constructor?: string | Function
40
+ id?: string
41
+ }
42
+ }
43
+
44
+ export interface CanvasInstance {
45
+ webview: WebView | null;
46
+ bus: Bus | null;
47
+ context2D: CanvasRenderingContext2D;
48
+ getContext: (contextType: string) => CanvasRenderingContext2D | null;
49
+ createImage: (width?: number, height?: number) => any;
50
+ postMessage: (message: WebviewMessage) => Promise<any>;
51
+ listeners: Array<(payload: any) => void>;
52
+ addMessageListener: (listener: (payload: any) => void) => () => void;
53
+ removeMessageListener: (listener: (payload: any) => void) => void;
54
+ createImageData: (dataArray: number[], width?: number, height?: number) => any;
55
+ }
56
+
57
+ export const registerWebviewTarget = (instance: Instance, targetName: string): void => {
58
+ instance[WEBVIEW_TARGET] = targetName
59
+ }
60
+
61
+ export const registerWebviewProperties = (instance: Instance, properties: Record<string, any>): void => {
62
+ Object.entries(properties).forEach(([key, initialValue]) => {
63
+ const privateKey = `__${key}__`
64
+ instance[privateKey] = initialValue
65
+ Object.defineProperty(instance, key, {
66
+ configurable: true,
67
+ enumerable: true,
68
+ get () {
69
+ return instance[privateKey]
70
+ },
71
+ set (value) {
72
+ instance.postMessage({
73
+ type: 'set',
74
+ payload: {
75
+ target: instance[WEBVIEW_TARGET],
76
+ key,
77
+ value
78
+ }
79
+ })
80
+
81
+ if (instance.forceUpdate) {
82
+ instance.forceUpdate()
83
+ }
84
+ return (instance[privateKey] = value)
85
+ }
86
+ })
87
+ })
88
+ }
89
+
90
+ export const registerWebviewMethods = (instance: Instance, methods: string[]): void => {
91
+ methods.forEach(method => {
92
+ instance[method] = (...args: any[]) => {
93
+ return instance.postMessage({
94
+ type: 'exec',
95
+ payload: {
96
+ target: instance[WEBVIEW_TARGET],
97
+ method,
98
+ args
99
+ }
100
+ })
101
+ }
102
+ })
103
+ }
104
+
105
+ export const registerWebviewConstructor = (constructor: WebviewConstructor, constructorName: string): void => {
106
+ constructors[constructorName] = constructor
107
+ constructor.constructLocally = function (...args: unknown[]): Instance {
108
+ return new (constructor as any)(...args, true)
109
+ }
110
+
111
+ constructor.prototype.onConstruction = function (...args: any[]): void {
112
+ if (SPECIAL_CONSTRUCTOR[constructorName] !== undefined) {
113
+ const { className, paramNum } = SPECIAL_CONSTRUCTOR[constructorName]
114
+ args[paramNum] = { className, classArgs: [args[paramNum]] }
115
+ }
116
+ this[WEBVIEW_TARGET] = ID()
117
+ this.postMessage({
118
+ type: 'construct',
119
+ payload: {
120
+ constructor: constructorName,
121
+ id: this[WEBVIEW_TARGET],
122
+ args
123
+ }
124
+ })
125
+ }
126
+ constructor.prototype.toJSON = function () {
127
+ return { __ref__: this[WEBVIEW_TARGET] }
128
+ }
129
+ }
130
+ export const useWebviewBinding = ({
131
+ targetName,
132
+ properties = {},
133
+ methods = []
134
+ }: {
135
+ targetName: string;
136
+ properties?: Record<string, any>;
137
+ methods?: string[]
138
+ }) => {
139
+ const instanceRef = useRef({})
140
+
141
+ useEffect(() => {
142
+ if (instanceRef.current) {
143
+ registerWebviewTarget(instanceRef.current as Instance, targetName)
144
+ registerWebviewProperties(instanceRef.current as Instance, properties)
145
+ registerWebviewMethods(instanceRef.current as Instance, methods)
146
+ }
147
+ }, [])
148
+
149
+ return instanceRef
150
+ }
@@ -19,7 +19,7 @@ import { warn } from '@mpxjs/utils'
19
19
  import { FormContext, FormFieldValue, CheckboxGroupContext, GroupValue } from './context'
20
20
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
21
21
  import useNodesRef, { HandlerRef } from './useNodesRef'
22
- import { useLayout, useTransformStyle, wrapChildren } from './utils'
22
+ import { useLayout, useTransformStyle, wrapChildren, extendObject } from './utils'
23
23
 
24
24
  export interface CheckboxGroupProps {
25
25
  name: string
@@ -64,10 +64,7 @@ const CheckboxGroup = forwardRef<
64
64
  flexWrap: 'wrap'
65
65
  }
66
66
 
67
- const styleObj = {
68
- ...defaultStyle,
69
- ...style
70
- }
67
+ const styleObj = extendObject({}, defaultStyle, style)
71
68
 
72
69
  const {
73
70
  hasSelfPercent,
@@ -80,7 +77,7 @@ const CheckboxGroup = forwardRef<
80
77
 
81
78
  const nodeRef = useRef(null)
82
79
 
83
- useNodesRef(props, ref, nodeRef, { defaultStyle })
80
+ useNodesRef(props, ref, nodeRef, { style: normalStyle })
84
81
 
85
82
  const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
86
83
 
@@ -119,12 +116,16 @@ const CheckboxGroup = forwardRef<
119
116
 
120
117
  const innerProps = useInnerProps(
121
118
  props,
122
- {
123
- ref: nodeRef,
124
- style: { ...normalStyle, ...layoutStyle },
125
- ...layoutProps
126
- },
127
- [],
119
+ extendObject(
120
+ {
121
+ ref: nodeRef,
122
+ style: extendObject({}, normalStyle, layoutStyle)
123
+ },
124
+ layoutProps
125
+ ),
126
+ [
127
+ 'name'
128
+ ],
128
129
  {
129
130
  layoutRef
130
131
  }
@@ -25,7 +25,7 @@ import { warn } from '@mpxjs/utils'
25
25
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
26
26
  import useNodesRef, { HandlerRef } from './useNodesRef'
27
27
  import Icon from './mpx-icon'
28
- import { splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren } from './utils'
28
+ import { splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren, extendObject } from './utils'
29
29
  import { CheckboxGroupContext, LabelContext } from './context'
30
30
 
31
31
  interface Selection {
@@ -46,7 +46,7 @@ export interface CheckboxProps extends Selection {
46
46
  'parent-height'?: number;
47
47
  children?: ReactNode
48
48
  bindtap?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
49
- catchtap?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
49
+ _onChange?: (evt: NativeSyntheticEvent<TouchEvent> | unknown, { checked }: { checked: boolean }) => void
50
50
  }
51
51
 
52
52
  const styles = StyleSheet.create({
@@ -92,7 +92,7 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
92
92
  'parent-width': parentWidth,
93
93
  'parent-height': parentHeight,
94
94
  bindtap,
95
- catchtap
95
+ _onChange
96
96
  } = props
97
97
 
98
98
  const [isChecked, setIsChecked] = useState<boolean>(!!checked)
@@ -101,15 +101,13 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
101
101
  let groupValue: { [key: string]: { checked: boolean; setValue: Dispatch<SetStateAction<boolean>>; } } | undefined
102
102
  let notifyChange: (evt: NativeSyntheticEvent<TouchEvent>) => void | undefined
103
103
 
104
- const defaultStyle = {
105
- ...styles.wrapper,
106
- ...(disabled && styles.wrapperDisabled)
107
- }
104
+ const defaultStyle = extendObject(
105
+ {},
106
+ styles.wrapper,
107
+ disabled ? styles.wrapperDisabled : null
108
+ )
108
109
 
109
- const styleObj = {
110
- ...styles.container,
111
- ...style
112
- }
110
+ const styleObj = extendObject({}, styles.container, style)
113
111
 
114
112
  const onChange = (evt: NativeSyntheticEvent<TouchEvent>) => {
115
113
  if (disabled) return
@@ -119,20 +117,15 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
119
117
  groupValue[value].checked = checked
120
118
  }
121
119
  notifyChange && notifyChange(evt)
120
+ // Called when the switch type attribute is checkbox
121
+ _onChange && _onChange(evt, { checked })
122
122
  }
123
123
 
124
124
  const onTap = (evt: NativeSyntheticEvent<TouchEvent>) => {
125
- if (disabled) return
126
125
  bindtap && bindtap(getCustomEvent('tap', evt, { layoutRef }, props))
127
126
  onChange(evt)
128
127
  }
129
128
 
130
- const catchTap = (evt: NativeSyntheticEvent<TouchEvent>) => {
131
- if (disabled) return
132
- catchtap && catchtap(getCustomEvent('tap', evt, { layoutRef }, props))
133
- onChange(evt)
134
- }
135
-
136
129
  const {
137
130
  hasSelfPercent,
138
131
  normalStyle,
@@ -145,13 +138,13 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
145
138
  const nodeRef = useRef(null)
146
139
 
147
140
  useNodesRef(props, ref, nodeRef, {
148
- defaultStyle,
141
+ style: extendObject({}, defaultStyle, normalStyle),
149
142
  change: onChange
150
143
  })
151
144
 
152
145
  const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
153
146
 
154
- const { textStyle, backgroundStyle, innerStyle } = splitStyle(normalStyle)
147
+ const { textStyle, backgroundStyle, innerStyle = {} } = splitStyle(normalStyle)
155
148
 
156
149
  if (backgroundStyle) {
157
150
  warn('Checkbox does not support background image-related styles!')
@@ -170,14 +163,21 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
170
163
 
171
164
  const innerProps = useInnerProps(
172
165
  props,
173
- {
174
- ref: nodeRef,
175
- style: { ...innerStyle, ...layoutStyle },
176
- ...layoutProps,
177
- bindtap: onTap,
178
- catchtap: catchTap
179
- },
180
- [],
166
+ extendObject(
167
+ {
168
+ ref: nodeRef,
169
+ style: extendObject({}, innerStyle, layoutStyle)
170
+ },
171
+ layoutProps,
172
+ {
173
+ bindtap: !disabled && onTap
174
+ }
175
+ ),
176
+ [
177
+ 'value',
178
+ 'disabled',
179
+ 'checked'
180
+ ],
181
181
  {
182
182
  layoutRef
183
183
  }
@@ -9,7 +9,7 @@ import { JSX, useRef, forwardRef, ReactNode, useMemo, useCallback } from 'react'
9
9
  import useNodesRef, { HandlerRef } from './useNodesRef'
10
10
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
11
11
  import { FormContext } from './context'
12
- import { useTransformStyle, splitProps, splitStyle, useLayout, wrapChildren } from './utils'
12
+ import { useTransformStyle, splitProps, splitStyle, useLayout, wrapChildren, extendObject } from './utils'
13
13
  interface FormProps {
14
14
  style?: Record<string, any>;
15
15
  children?: ReactNode;
@@ -47,21 +47,23 @@ const _Form = forwardRef<HandlerRef<View, FormProps>, FormProps>((fromProps: For
47
47
  setHeight
48
48
  } = useTransformStyle(style, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
49
49
 
50
- const { textStyle, innerStyle } = splitStyle(normalStyle)
50
+ const { textStyle, innerStyle = {} } = splitStyle(normalStyle)
51
51
 
52
52
  const formRef = useRef(null)
53
- useNodesRef(props, ref, formRef)
53
+ useNodesRef(props, ref, formRef, {
54
+ style: normalStyle
55
+ })
54
56
 
55
57
  const propsRef = useRef<FormProps>({})
56
58
  propsRef.current = props
57
59
 
58
60
  const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: formRef })
59
61
 
60
- const innerProps = useInnerProps(props, {
61
- style: { ...innerStyle, ...layoutStyle },
62
- ref: formRef,
63
- ...layoutProps
64
- }, [
62
+ const innerProps = useInnerProps(props, extendObject({
63
+ style: extendObject({}, innerStyle, layoutStyle),
64
+ ref: formRef
65
+ }, layoutProps)
66
+ , [
65
67
  'bindsubmit',
66
68
  'bindreset'
67
69
  ], { layoutRef })