@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
@@ -3,11 +3,11 @@
3
3
  * ✔ size
4
4
  * ✔ color
5
5
  */
6
- import { JSX, forwardRef, useRef } from 'react'
6
+ import { JSX, forwardRef, useRef, createElement } from 'react'
7
7
  import { Text, TextStyle, Image } from 'react-native'
8
8
  import useInnerProps from './getInnerListeners'
9
9
  import useNodesRef, { HandlerRef } from './useNodesRef'
10
- import { useLayout, useTransformStyle } from './utils'
10
+ import { useLayout, useTransformStyle, extendObject } from './utils'
11
11
 
12
12
  export type IconType =
13
13
  | 'success'
@@ -63,10 +63,7 @@ const Icon = forwardRef<HandlerRef<Text, IconProps>, IconProps>(
63
63
 
64
64
  const defaultStyle = { width: ~~size, height: ~~size }
65
65
 
66
- const styleObj = {
67
- ...defaultStyle,
68
- ...style
69
- }
66
+ const styleObj = extendObject({}, defaultStyle, style)
70
67
 
71
68
  const {
72
69
  hasSelfPercent,
@@ -76,32 +73,30 @@ const Icon = forwardRef<HandlerRef<Text, IconProps>, IconProps>(
76
73
  } = useTransformStyle(styleObj, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
77
74
 
78
75
  const nodeRef = useRef(null)
79
- useNodesRef(props, ref, nodeRef, { defaultStyle })
76
+ useNodesRef(props, ref, nodeRef, { style: normalStyle })
80
77
 
81
78
  const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
82
79
 
83
80
  const innerProps = useInnerProps(
84
81
  props,
85
- {
86
- ref: nodeRef,
87
- style: {
88
- ...normalStyle,
89
- ...layoutStyle,
90
- tintColor: color
82
+ extendObject(
83
+ {
84
+ ref: nodeRef,
85
+ source: { uri },
86
+ style: extendObject({}, normalStyle, layoutStyle, { tintColor: color })
91
87
  },
92
- source: { uri },
93
- ...layoutProps
94
- },
88
+ layoutProps
89
+ ),
95
90
  [],
96
91
  {
97
92
  layoutRef
98
93
  }
99
94
  )
100
95
 
101
- return <Image {...innerProps} />
96
+ return createElement(Image, innerProps)
102
97
  }
103
98
  )
104
99
 
105
- Icon.displayName = 'mpx-icon'
100
+ Icon.displayName = 'MpxIcon'
106
101
 
107
102
  export default Icon
@@ -0,0 +1,436 @@
1
+ /**
2
+ * ✔ src
3
+ * ✔ mode
4
+ * ✘ show-menu-by-longpress
5
+ * ✔ binderror
6
+ * ✔ bindload
7
+ * ✘ fade-in
8
+ * ✔ webp
9
+ * ✘ lazy-load
10
+ * ✔ bindtap
11
+ * ✔ DEFAULT_SIZE
12
+ */
13
+ import { useEffect, useMemo, useState, useRef, forwardRef, createElement } from 'react'
14
+ import {
15
+ Image as RNImage,
16
+ View,
17
+ ImageStyle,
18
+ ImageResizeMode,
19
+ NativeSyntheticEvent,
20
+ ImageErrorEventData,
21
+ LayoutChangeEvent,
22
+ DimensionValue,
23
+ ImageLoadEventData
24
+ } from 'react-native'
25
+ import { noop } from '@mpxjs/utils'
26
+ import { SvgCssUri } from 'react-native-svg/css'
27
+ import useInnerProps, { getCustomEvent } from './getInnerListeners'
28
+ import useNodesRef, { HandlerRef } from './useNodesRef'
29
+ import { SVG_REGEXP, useLayout, useTransformStyle, renderImage, extendObject } from './utils'
30
+
31
+ export type Mode =
32
+ | 'scaleToFill'
33
+ | 'aspectFit'
34
+ | 'aspectFill'
35
+ | 'widthFix'
36
+ | 'heightFix'
37
+ | 'top'
38
+ | 'bottom'
39
+ | 'center'
40
+ | 'left'
41
+ | 'right'
42
+ | 'top left'
43
+ | 'top right'
44
+ | 'bottom left'
45
+ | 'bottom right'
46
+
47
+ export interface ImageProps {
48
+ src?: string
49
+ mode?: Mode
50
+ svg?: boolean
51
+ style?: ImageStyle & Record<string, any>
52
+ 'enable-offset'?: boolean;
53
+ 'enable-var'?: boolean
54
+ 'external-var-context'?: Record<string, any>
55
+ 'parent-font-size'?: number
56
+ 'parent-width'?: number
57
+ 'parent-height'?: number
58
+ 'enable-fast-image'?: boolean
59
+ bindload?: (evt: NativeSyntheticEvent<ImageLoadEventData> | unknown) => void
60
+ binderror?: (evt: NativeSyntheticEvent<ImageErrorEventData> | unknown) => void
61
+ }
62
+
63
+ interface ImageState {
64
+ viewWidth?: number
65
+ viewHeight?: number
66
+ imageWidth?: number
67
+ imageHeight?: number
68
+ ratio?: number
69
+ }
70
+
71
+ const DEFAULT_IMAGE_WIDTH = 320
72
+ const DEFAULT_IMAGE_HEIGHT = 240
73
+
74
+ const cropMode: Mode[] = [
75
+ 'top',
76
+ 'bottom',
77
+ 'center',
78
+ 'right',
79
+ 'left',
80
+ 'top left',
81
+ 'top right',
82
+ 'bottom left',
83
+ 'bottom right'
84
+ ]
85
+
86
+ const ModeMap = new Map<Mode, ImageResizeMode | undefined>([
87
+ ['scaleToFill', 'stretch'],
88
+ ['aspectFit', 'contain'],
89
+ ['aspectFill', 'cover'],
90
+ ['widthFix', 'stretch'],
91
+ ['heightFix', 'stretch'],
92
+ ...cropMode.map<[Mode, ImageResizeMode]>(mode => [mode, 'stretch'])
93
+ ])
94
+
95
+ const isNumber = (value: DimensionValue) => typeof value === 'number'
96
+
97
+ const relativeCenteredSize = (viewSize: number, imageSize: number) => (viewSize - imageSize) / 2
98
+
99
+ function noMeetCalcRule (isSvg: boolean, mode: Mode, viewWidth: number, viewHeight: number, ratio: number) {
100
+ const isMeetSize = viewWidth && viewHeight && ratio
101
+ if (isSvg && !isMeetSize) return true
102
+ if (!isSvg && !['scaleToFill', 'aspectFit', 'aspectFill'].includes(mode) && !isMeetSize) return true
103
+ return false
104
+ }
105
+
106
+ const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, ref): JSX.Element => {
107
+ const {
108
+ src = '',
109
+ mode = 'scaleToFill',
110
+ style = {},
111
+ 'enable-var': enableVar,
112
+ 'external-var-context': externalVarContext,
113
+ 'parent-font-size': parentFontSize,
114
+ 'enable-fast-image': enableFastImage,
115
+ 'parent-width': parentWidth,
116
+ 'parent-height': parentHeight,
117
+ bindload,
118
+ binderror
119
+ } = props
120
+
121
+ const defaultStyle = {
122
+ width: DEFAULT_IMAGE_WIDTH,
123
+ height: DEFAULT_IMAGE_HEIGHT
124
+ }
125
+
126
+ const styleObj = extendObject(
127
+ {},
128
+ defaultStyle,
129
+ style,
130
+ { overflow: 'hidden' }
131
+ )
132
+
133
+ const state = useRef<ImageState>({})
134
+
135
+ const nodeRef = useRef(null)
136
+ useNodesRef(props, ref, nodeRef, {
137
+ defaultStyle
138
+ })
139
+
140
+ const isSvg = SVG_REGEXP.test(src)
141
+ const isWidthFixMode = mode === 'widthFix'
142
+ const isHeightFixMode = mode === 'heightFix'
143
+ const isCropMode = cropMode.includes(mode)
144
+ const isLayoutMode = isWidthFixMode || isHeightFixMode || isCropMode
145
+ const resizeMode: ImageResizeMode = ModeMap.get(mode) || 'stretch'
146
+
147
+ const onLayout = ({ nativeEvent: { layout: { width, height } } }: LayoutChangeEvent) => {
148
+ state.current.viewWidth = width
149
+ state.current.viewHeight = height
150
+
151
+ if (state.current.imageWidth && state.current.imageHeight && state.current.ratio) {
152
+ setViewWidth(width)
153
+ setViewHeight(height)
154
+ setRatio(state.current.ratio)
155
+ setImageWidth(state.current.imageWidth)
156
+ setImageHeight(state.current.imageHeight)
157
+ state.current = {}
158
+ setLoaded(true)
159
+ }
160
+ }
161
+
162
+ const { normalStyle, hasSelfPercent, setWidth, setHeight } = useTransformStyle(styleObj, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
163
+
164
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({
165
+ props,
166
+ hasSelfPercent,
167
+ setWidth,
168
+ setHeight,
169
+ nodeRef,
170
+ onLayout: isLayoutMode ? onLayout : noop
171
+ })
172
+
173
+ const { width, height } = normalStyle
174
+
175
+ const [viewWidth, setViewWidth] = useState(isNumber(width) ? width : 0)
176
+ const [viewHeight, setViewHeight] = useState(isNumber(height) ? height : 0)
177
+ const [imageWidth, setImageWidth] = useState(0)
178
+ const [imageHeight, setImageHeight] = useState(0)
179
+ const [ratio, setRatio] = useState(0)
180
+ const [loaded, setLoaded] = useState(!isLayoutMode)
181
+
182
+ const fixedHeight = useMemo(() => {
183
+ const fixed = viewWidth * ratio
184
+ return !fixed ? viewHeight : fixed
185
+ }, [ratio, viewWidth, viewHeight])
186
+
187
+ const fixedWidth = useMemo(() => {
188
+ if (!ratio) return viewWidth
189
+ const fixed = viewHeight / ratio
190
+ return !fixed ? viewWidth : fixed
191
+ }, [ratio, viewWidth, viewHeight])
192
+
193
+ const modeStyle: ImageStyle = useMemo(() => {
194
+ if (noMeetCalcRule(isSvg, mode, viewWidth, viewHeight, ratio)) return {}
195
+ switch (mode) {
196
+ case 'scaleToFill':
197
+ case 'aspectFit':
198
+ if (isSvg) {
199
+ const scale = ratio <= 1
200
+ ? imageWidth >= viewWidth ? viewWidth / imageWidth : imageWidth / viewWidth
201
+ : imageHeight >= viewHeight ? viewHeight / imageHeight : imageHeight / viewHeight
202
+ return {
203
+ transform: [
204
+ { scale },
205
+ ratio <= 1 ? { translateY: -(imageHeight * scale - viewHeight) / 2 / scale } : { translateX: -(imageWidth * scale - viewWidth) / 2 / scale }
206
+ ]
207
+ }
208
+ }
209
+ return {}
210
+ case 'aspectFill':
211
+ if (isSvg) {
212
+ const scale = ratio >= 1
213
+ ? imageWidth >= viewWidth ? viewWidth / imageWidth : imageWidth / viewWidth
214
+ : imageHeight >= viewHeight ? viewHeight / imageHeight : imageHeight / viewHeight
215
+ return {
216
+ transform: [
217
+ { scale },
218
+ ratio >= 1 ? { translateY: -(imageHeight * scale - viewHeight) / 2 / scale } : { translateX: -(imageWidth * scale - viewWidth) / 2 / scale }
219
+ ]
220
+ }
221
+ }
222
+ return {}
223
+ case 'widthFix':
224
+ case 'heightFix':
225
+ if (isSvg) {
226
+ const scale = ratio >= 1
227
+ ? imageWidth >= fixedWidth ? fixedWidth / imageWidth : imageWidth / fixedWidth
228
+ : imageHeight >= fixedHeight ? fixedHeight / imageHeight : imageHeight / fixedHeight
229
+ return {
230
+ transform: [{ scale }]
231
+ }
232
+ }
233
+ return {}
234
+ case 'top':
235
+ return {
236
+ transform: [
237
+ { translateX: relativeCenteredSize(viewWidth, imageWidth) }
238
+ ]
239
+ }
240
+ case 'bottom':
241
+ return {
242
+ transform: [
243
+ { translateY: viewHeight - imageHeight },
244
+ { translateX: relativeCenteredSize(viewWidth, imageWidth) }
245
+ ]
246
+ }
247
+ case 'center':
248
+ return {
249
+ transform: [
250
+ { translateY: relativeCenteredSize(viewHeight, imageHeight) },
251
+ { translateX: relativeCenteredSize(viewWidth, imageWidth) }
252
+ ]
253
+ }
254
+ case 'left':
255
+ return {
256
+ transform: [
257
+ { translateY: relativeCenteredSize(viewHeight, imageHeight) }
258
+ ]
259
+ }
260
+ case 'right':
261
+ return {
262
+ transform: [
263
+ { translateY: relativeCenteredSize(viewHeight, imageHeight) },
264
+ { translateX: viewWidth - imageWidth }
265
+ ]
266
+ }
267
+ case 'top left':
268
+ return {}
269
+ case 'top right':
270
+ return {
271
+ transform: [
272
+ { translateX: viewWidth - imageWidth }
273
+ ]
274
+ }
275
+ case 'bottom left':
276
+ return {
277
+ transform: [
278
+ { translateY: viewHeight - imageHeight }
279
+ ]
280
+ }
281
+ case 'bottom right':
282
+ return {
283
+ transform: [
284
+ { translateY: viewHeight - imageHeight },
285
+ { translateX: viewWidth - imageWidth }
286
+ ]
287
+ }
288
+ default:
289
+ return {}
290
+ }
291
+ }, [isSvg, mode, viewWidth, viewHeight, imageWidth, imageHeight, ratio, fixedWidth, fixedHeight])
292
+
293
+ const onSvgLoad = (evt: LayoutChangeEvent) => {
294
+ const { width, height } = evt.nativeEvent.layout
295
+ setRatio(!width ? 0 : height / width)
296
+ setImageWidth(width)
297
+ setImageHeight(height)
298
+
299
+ bindload && bindload(
300
+ getCustomEvent(
301
+ 'load',
302
+ evt,
303
+ {
304
+ detail: { width, height },
305
+ layoutRef
306
+ },
307
+ props
308
+ )
309
+ )
310
+ }
311
+
312
+ const onSvgError = (evt: Error) => {
313
+ binderror!(
314
+ getCustomEvent(
315
+ 'error',
316
+ evt,
317
+ {
318
+ detail: { errMsg: evt?.message },
319
+ layoutRef
320
+ },
321
+ props
322
+ )
323
+ )
324
+ }
325
+
326
+ const onImageLoad = (evt: NativeSyntheticEvent<ImageLoadEventData>) => {
327
+ evt.persist()
328
+ RNImage.getSize(src, (width: number, height: number) => {
329
+ bindload!(
330
+ getCustomEvent(
331
+ 'load',
332
+ evt,
333
+ {
334
+ detail: { width, height },
335
+ layoutRef
336
+ },
337
+ props
338
+ )
339
+ )
340
+ })
341
+ }
342
+
343
+ const onImageError = (evt: NativeSyntheticEvent<ImageErrorEventData>) => {
344
+ binderror!(
345
+ getCustomEvent(
346
+ 'error',
347
+ evt,
348
+ {
349
+ detail: { errMsg: evt.nativeEvent.error },
350
+ layoutRef
351
+ },
352
+ props
353
+ )
354
+ )
355
+ }
356
+
357
+ useEffect(() => {
358
+ if (!isSvg && isLayoutMode) {
359
+ RNImage.getSize(src, (width: number, height: number) => {
360
+ state.current.imageWidth = width
361
+ state.current.imageHeight = height
362
+ state.current.ratio = !width ? 0 : height / width
363
+
364
+ if (isWidthFixMode
365
+ ? state.current.viewWidth
366
+ : isHeightFixMode
367
+ ? state.current.viewHeight
368
+ : state.current.viewWidth && state.current.viewHeight) {
369
+ state.current.viewWidth && setViewWidth(state.current.viewWidth)
370
+ state.current.viewHeight && setViewHeight(state.current.viewHeight)
371
+ setRatio(!width ? 0 : height / width)
372
+ setImageWidth(width)
373
+ setImageHeight(height)
374
+ state.current = {}
375
+ setLoaded(true)
376
+ }
377
+ })
378
+ }
379
+ }, [src, isSvg, isLayoutMode])
380
+
381
+ const innerProps = useInnerProps(
382
+ props,
383
+ extendObject(
384
+ {
385
+ ref: nodeRef,
386
+ style: extendObject(
387
+ {},
388
+ normalStyle,
389
+ layoutStyle,
390
+ isHeightFixMode ? { width: fixedWidth } : {},
391
+ isWidthFixMode ? { height: fixedHeight } : {}
392
+ )
393
+ },
394
+ layoutProps
395
+ ),
396
+ [
397
+ 'src',
398
+ 'mode',
399
+ 'svg'
400
+ ],
401
+ {
402
+ layoutRef
403
+ }
404
+ )
405
+
406
+ return createElement(View, innerProps,
407
+ isSvg
408
+ ? createElement(SvgCssUri, {
409
+ uri: src,
410
+ onLayout: onSvgLoad,
411
+ onError: binderror && onSvgError,
412
+ style: extendObject(
413
+ { transformOrigin: 'top left' },
414
+ modeStyle
415
+ )
416
+ })
417
+ : loaded && renderImage({
418
+ source: { uri: src },
419
+ resizeMode: resizeMode,
420
+ onLoad: bindload && onImageLoad,
421
+ onError: binderror && onImageError,
422
+ style: extendObject(
423
+ {
424
+ transformOrigin: 'top left',
425
+ width: isCropMode ? imageWidth : '100%',
426
+ height: isCropMode ? imageHeight : '100%'
427
+ },
428
+ isCropMode ? modeStyle : {}
429
+ )
430
+ }, enableFastImage)
431
+ )
432
+ })
433
+
434
+ Image.displayName = 'mpx-image'
435
+
436
+ export default Image