@mpxjs/webpack-plugin 2.8.25-alpha.21 → 2.8.25-alpha.22
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.
- package/lib/runtime/components/react/dist/KeyboardAvoidingView.jsx +89 -0
- package/lib/runtime/components/react/dist/context.js +14 -0
- package/lib/runtime/components/react/dist/event.config.js +27 -0
- package/lib/runtime/components/react/dist/getInnerListeners.js +262 -0
- package/lib/runtime/components/react/dist/mpx-button.jsx +271 -0
- package/lib/runtime/components/react/dist/mpx-canvas/Bus.js +60 -0
- package/lib/runtime/components/react/dist/mpx-canvas/CanvasGradient.js +15 -0
- package/lib/runtime/components/react/dist/mpx-canvas/CanvasRenderingContext2D.js +84 -0
- package/lib/runtime/components/react/dist/mpx-canvas/Image.js +87 -0
- package/lib/runtime/components/react/dist/mpx-canvas/ImageData.js +15 -0
- package/lib/runtime/components/react/dist/mpx-canvas/constructorsRegistry.js +28 -0
- package/lib/runtime/components/react/dist/mpx-canvas/html.js +341 -0
- package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +236 -0
- package/lib/runtime/components/react/dist/mpx-canvas/utils.jsx +89 -0
- package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +90 -0
- package/lib/runtime/components/react/dist/mpx-checkbox.jsx +131 -0
- package/lib/runtime/components/react/dist/mpx-form.jsx +68 -0
- package/lib/runtime/components/react/dist/mpx-icon/icons/cancel.png +0 -0
- package/lib/runtime/components/react/dist/mpx-icon/icons/clear.png +0 -0
- package/lib/runtime/components/react/dist/mpx-icon/icons/download.png +0 -0
- package/lib/runtime/components/react/dist/mpx-icon/icons/info.png +0 -0
- package/lib/runtime/components/react/dist/mpx-icon/icons/search.png +0 -0
- package/lib/runtime/components/react/dist/mpx-icon/icons/success.png +0 -0
- package/lib/runtime/components/react/dist/mpx-icon/icons/success_no_circle.png +0 -0
- package/lib/runtime/components/react/dist/mpx-icon/icons/waiting.png +0 -0
- package/lib/runtime/components/react/dist/mpx-icon/icons/warn.png +0 -0
- package/lib/runtime/components/react/dist/mpx-icon/index.jsx +50 -0
- package/lib/runtime/components/react/dist/mpx-image.jsx +292 -0
- package/lib/runtime/components/react/dist/mpx-input.jsx +292 -0
- package/lib/runtime/components/react/dist/mpx-label.jsx +52 -0
- package/lib/runtime/components/react/dist/mpx-movable-area.jsx +32 -0
- package/lib/runtime/components/react/dist/mpx-movable-view.jsx +468 -0
- package/lib/runtime/components/react/dist/mpx-navigator.jsx +33 -0
- package/lib/runtime/components/react/dist/mpx-picker/date.jsx +74 -0
- package/lib/runtime/components/react/dist/mpx-picker/index.jsx +141 -0
- package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +147 -0
- package/lib/runtime/components/react/dist/mpx-picker/region.jsx +99 -0
- package/lib/runtime/components/react/dist/mpx-picker/regionData.js +6099 -0
- package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +81 -0
- package/lib/runtime/components/react/dist/mpx-picker/time.jsx +242 -0
- package/lib/runtime/components/react/dist/mpx-picker/type.js +1 -0
- package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +35 -0
- package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +193 -0
- package/lib/runtime/components/react/dist/mpx-picker-view.jsx +125 -0
- package/lib/runtime/components/react/dist/mpx-portal/index.jsx +30 -0
- package/lib/runtime/components/react/dist/mpx-portal/portal-host.jsx +112 -0
- package/lib/runtime/components/react/dist/mpx-portal/portal-manager.jsx +41 -0
- package/lib/runtime/components/react/dist/mpx-radio-group.jsx +86 -0
- package/lib/runtime/components/react/dist/mpx-radio.jsx +140 -0
- package/lib/runtime/components/react/dist/mpx-rich-text/html.js +39 -0
- package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +62 -0
- package/lib/runtime/components/react/dist/mpx-root-portal.jsx +17 -0
- package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +372 -0
- package/lib/runtime/components/react/dist/mpx-simple-text.jsx +11 -0
- package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +59 -0
- package/lib/runtime/components/react/dist/mpx-swiper.jsx +671 -0
- package/lib/runtime/components/react/dist/mpx-switch.jsx +97 -0
- package/lib/runtime/components/react/dist/mpx-text.jsx +41 -0
- package/lib/runtime/components/react/dist/mpx-textarea.jsx +40 -0
- package/lib/runtime/components/react/dist/mpx-video.jsx +248 -0
- package/lib/runtime/components/react/dist/mpx-view.jsx +611 -0
- package/lib/runtime/components/react/dist/mpx-web-view.jsx +289 -0
- package/lib/runtime/components/react/dist/parser.js +218 -0
- package/lib/runtime/components/react/dist/pickerFaces.js +76 -0
- package/lib/runtime/components/react/dist/pickerVIewContext.js +14 -0
- package/lib/runtime/components/react/dist/pickerViewIndicator.jsx +23 -0
- package/lib/runtime/components/react/dist/pickerViewMask.jsx +18 -0
- package/lib/runtime/components/react/dist/useAnimationHooks.js +346 -0
- package/lib/runtime/components/react/dist/useNodesRef.js +16 -0
- package/lib/runtime/components/react/dist/utils.jsx +599 -0
- package/package.json +6 -3
- package/LICENSE +0 -433
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ✔ hover-class
|
|
3
|
+
* ✘ hover-stop-propagation
|
|
4
|
+
* ✔ hover-start-time
|
|
5
|
+
* ✔ hover-stay-time
|
|
6
|
+
*/
|
|
7
|
+
import { View, StyleSheet, Image } from 'react-native';
|
|
8
|
+
import { useRef, useState, useEffect, forwardRef, createElement } from 'react';
|
|
9
|
+
import useInnerProps from './getInnerListeners';
|
|
10
|
+
import Animated from 'react-native-reanimated';
|
|
11
|
+
import useAnimationHooks from './useAnimationHooks';
|
|
12
|
+
import useNodesRef from './useNodesRef';
|
|
13
|
+
import { parseUrl, PERCENT_REGEX, splitStyle, splitProps, useTransformStyle, wrapChildren, useLayout, renderImage, pickStyle, extendObject, useHover } from './utils';
|
|
14
|
+
import { error } from '@mpxjs/utils';
|
|
15
|
+
import LinearGradient from 'react-native-linear-gradient';
|
|
16
|
+
import { GestureDetector } from 'react-native-gesture-handler';
|
|
17
|
+
const linearMap = new Map([
|
|
18
|
+
['top', 0],
|
|
19
|
+
['bottom', 180],
|
|
20
|
+
['left', 270],
|
|
21
|
+
['right', 90]
|
|
22
|
+
]);
|
|
23
|
+
// 对角线角度
|
|
24
|
+
const diagonalAngleMap = {
|
|
25
|
+
'top right': (width, height) => {
|
|
26
|
+
return Math.acos((width / 2) /
|
|
27
|
+
(Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)) / 2));
|
|
28
|
+
},
|
|
29
|
+
'right top': (width, height) => { return diagonalAngleMap['top right'](width, height); },
|
|
30
|
+
'bottom right': (width, height) => Math.PI - diagonalAngleMap['top right'](width, height),
|
|
31
|
+
'right bottom': (width, height) => { return diagonalAngleMap['bottom right'](width, height); },
|
|
32
|
+
'bottom left': (width, height) => Math.PI + diagonalAngleMap['top right'](width, height),
|
|
33
|
+
'left bottom': (width, height) => { return diagonalAngleMap['bottom left'](width, height); },
|
|
34
|
+
'top left': (width, height) => (2 * Math.PI) - diagonalAngleMap['top right'](width, height),
|
|
35
|
+
'left top': (width, height) => { return diagonalAngleMap['top left'](width, height); }
|
|
36
|
+
};
|
|
37
|
+
// 弧度转化为角度的公式
|
|
38
|
+
function radToAngle(r) {
|
|
39
|
+
return r * 180 / Math.PI;
|
|
40
|
+
}
|
|
41
|
+
const applyHandlers = (handlers, args) => {
|
|
42
|
+
for (const handler of handlers) {
|
|
43
|
+
handler(...args);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const normalizeStyle = (style = {}) => {
|
|
47
|
+
['backgroundSize', 'backgroundPosition'].forEach(name => {
|
|
48
|
+
if (style[name] && typeof style[name] === 'string') {
|
|
49
|
+
if (style[name].trim()) {
|
|
50
|
+
style[name] = style[name].split(' ');
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return style;
|
|
55
|
+
};
|
|
56
|
+
const isPercent = (val) => typeof val === 'string' && PERCENT_REGEX.test(val);
|
|
57
|
+
const isBackgroundSizeKeyword = (val) => typeof val === 'string' && /^cover|contain$/.test(val);
|
|
58
|
+
const isNeedLayout = (preImageInfo) => {
|
|
59
|
+
const { sizeList, backgroundPosition, linearInfo } = preImageInfo;
|
|
60
|
+
const [width, height] = sizeList;
|
|
61
|
+
const bp = backgroundPosition;
|
|
62
|
+
// 含有百分号,center 需计算布局
|
|
63
|
+
return isBackgroundSizeKeyword(width) ||
|
|
64
|
+
(isPercent(height) && width === 'auto') ||
|
|
65
|
+
(isPercent(width) && height === 'auto') ||
|
|
66
|
+
isPercent(bp[1]) ||
|
|
67
|
+
isPercent(bp[3]) ||
|
|
68
|
+
isDiagonalAngle(linearInfo);
|
|
69
|
+
};
|
|
70
|
+
const checkNeedLayout = (preImageInfo) => {
|
|
71
|
+
const { sizeList } = preImageInfo;
|
|
72
|
+
const [width] = sizeList;
|
|
73
|
+
// 在渐变的时候,background-size的cover,contain, auto属性值,转化为100%, needLayout计算逻辑和原来保持一致,needImageSize始终为false
|
|
74
|
+
return {
|
|
75
|
+
// 是否开启layout的计算
|
|
76
|
+
needLayout: isNeedLayout(preImageInfo),
|
|
77
|
+
// 是否开启原始宽度的计算
|
|
78
|
+
needImageSize: isBackgroundSizeKeyword(width) || sizeList.includes('auto')
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* h - 用户设置的高度
|
|
83
|
+
* lh - 容器的高度
|
|
84
|
+
* ratio - 原始图片的宽高比
|
|
85
|
+
* **/
|
|
86
|
+
function calculateSize(h, ratio, lh, reverse = false) {
|
|
87
|
+
let height = 0;
|
|
88
|
+
let width = 0;
|
|
89
|
+
if (typeof lh === 'boolean') {
|
|
90
|
+
reverse = lh;
|
|
91
|
+
}
|
|
92
|
+
if (isPercent(h)) { // auto px/rpx
|
|
93
|
+
if (!lh)
|
|
94
|
+
return null;
|
|
95
|
+
height = (parseFloat(h) / 100) * lh;
|
|
96
|
+
width = height * ratio;
|
|
97
|
+
}
|
|
98
|
+
else { // 2. auto px/rpx - 根据比例计算
|
|
99
|
+
height = h;
|
|
100
|
+
width = height * ratio;
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
width: reverse ? height : width,
|
|
104
|
+
height: reverse ? width : height
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 用户设置百分比后,转换为偏移量
|
|
109
|
+
* h - 用户设置图片的高度
|
|
110
|
+
* ch - 容器的高度
|
|
111
|
+
* val - 用户设置的百分比
|
|
112
|
+
* **/
|
|
113
|
+
function calculateSizePosition(h, ch, val) {
|
|
114
|
+
if (!h || !ch)
|
|
115
|
+
return 0;
|
|
116
|
+
// 百分比需要单独的计算
|
|
117
|
+
if (isPercent(h)) {
|
|
118
|
+
h = ch * parseFloat(h) / 100;
|
|
119
|
+
}
|
|
120
|
+
// (container width - image width) * (position x%) = (x offset value)
|
|
121
|
+
return (ch - h) * parseFloat(val) / 100;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* 获取图片的展示宽高
|
|
125
|
+
* h - 用户设置的高度
|
|
126
|
+
* lh - 容器的高度
|
|
127
|
+
* **/
|
|
128
|
+
const calcPercent = (h, lh) => {
|
|
129
|
+
return isPercent(h) ? parseFloat(h) / 100 * lh : +h;
|
|
130
|
+
};
|
|
131
|
+
function backgroundPosition(imageProps, preImageInfo, imageSize, layoutInfo) {
|
|
132
|
+
const bps = preImageInfo.backgroundPosition;
|
|
133
|
+
if (bps.length === 0)
|
|
134
|
+
return;
|
|
135
|
+
const style = {};
|
|
136
|
+
const imageStyle = imageProps.style || {};
|
|
137
|
+
for (let i = 0; i < bps.length; i += 2) {
|
|
138
|
+
const key = bps[i];
|
|
139
|
+
const val = bps[i + 1];
|
|
140
|
+
// 需要获取 图片宽度 和 容器的宽度 进行计算
|
|
141
|
+
if (isPercent(val)) {
|
|
142
|
+
if (i === 0) {
|
|
143
|
+
style[key] = calculateSizePosition(imageStyle.width, layoutInfo?.width, val);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
style[key] = calculateSizePosition(imageStyle.height, layoutInfo?.height, val);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
style[key] = val;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
extendObject(imageProps.style, style);
|
|
154
|
+
}
|
|
155
|
+
// background-size 转换
|
|
156
|
+
function backgroundSize(imageProps, preImageInfo, imageSize, layoutInfo) {
|
|
157
|
+
const sizeList = preImageInfo.sizeList;
|
|
158
|
+
if (!sizeList)
|
|
159
|
+
return;
|
|
160
|
+
const { width: layoutWidth, height: layoutHeight } = layoutInfo || {};
|
|
161
|
+
const { width: imageSizeWidth, height: imageSizeHeight } = imageSize || {};
|
|
162
|
+
const [width, height] = sizeList;
|
|
163
|
+
let dimensions = { width: 0, height: 0 };
|
|
164
|
+
// 枚举值
|
|
165
|
+
if (typeof width === 'string' && ['cover', 'contain'].includes(width)) {
|
|
166
|
+
if (layoutInfo && imageSize) {
|
|
167
|
+
const layoutRatio = layoutWidth / imageSizeWidth;
|
|
168
|
+
const eleRatio = imageSizeWidth / imageSizeHeight;
|
|
169
|
+
// 容器宽高比 大于 图片的宽高比,依据宽度作为基准,否则以高度为基准
|
|
170
|
+
if ((layoutRatio <= eleRatio && width === 'contain') || (layoutRatio >= eleRatio && width === 'cover')) {
|
|
171
|
+
dimensions = calculateSize(layoutWidth, imageSizeHeight / imageSizeWidth, true);
|
|
172
|
+
}
|
|
173
|
+
else if ((layoutRatio > eleRatio && width === 'contain') || (layoutRatio < eleRatio && width === 'cover')) {
|
|
174
|
+
dimensions = calculateSize(layoutHeight, imageSizeWidth / imageSizeHeight);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
if (width === 'auto' && height === 'auto') { // 均为auto
|
|
180
|
+
if (!imageSize)
|
|
181
|
+
return;
|
|
182
|
+
dimensions = {
|
|
183
|
+
width: imageSizeWidth,
|
|
184
|
+
height: imageSizeHeight
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
else if (width === 'auto') { // auto px/rpx/%
|
|
188
|
+
if (!imageSize)
|
|
189
|
+
return;
|
|
190
|
+
dimensions = calculateSize(height, imageSizeWidth / imageSizeHeight, layoutInfo?.height);
|
|
191
|
+
if (!dimensions)
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
else if (height === 'auto') { // auto px/rpx/%
|
|
195
|
+
if (!imageSize)
|
|
196
|
+
return;
|
|
197
|
+
dimensions = calculateSize(width, imageSizeHeight / imageSizeWidth, layoutInfo?.width, true);
|
|
198
|
+
if (!dimensions)
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
else { // 数值类型 ImageStyle
|
|
202
|
+
// 数值类型设置为 stretch
|
|
203
|
+
imageProps.resizeMode = 'stretch';
|
|
204
|
+
dimensions = {
|
|
205
|
+
width: isPercent(width) ? width : +width,
|
|
206
|
+
height: isPercent(height) ? height : +height
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
// 样式合并
|
|
211
|
+
extendObject(imageProps.style, dimensions);
|
|
212
|
+
}
|
|
213
|
+
// background-image转换为source
|
|
214
|
+
function backgroundImage(imageProps, preImageInfo) {
|
|
215
|
+
const src = preImageInfo.src;
|
|
216
|
+
if (src) {
|
|
217
|
+
imageProps.source = { uri: src };
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// 渐变的转换
|
|
221
|
+
function linearGradient(imageProps, preImageInfo, imageSize, layoutInfo) {
|
|
222
|
+
const { type, linearInfo } = preImageInfo;
|
|
223
|
+
const { colors = [], locations, direction = '' } = linearInfo || {};
|
|
224
|
+
const { width, height } = imageSize || {};
|
|
225
|
+
if (type !== 'linear')
|
|
226
|
+
return;
|
|
227
|
+
// 角度计算
|
|
228
|
+
let angle = +(linearMap.get(direction) || direction.match(/(-?\d+(\.\d+)?)deg/)?.[1] || 180) % 360;
|
|
229
|
+
// 对角线角度计算
|
|
230
|
+
if (layoutInfo && diagonalAngleMap[direction] && imageSize && linearInfo) {
|
|
231
|
+
angle = radToAngle(diagonalAngleMap[direction](width, height)) || 180;
|
|
232
|
+
}
|
|
233
|
+
// 赋值
|
|
234
|
+
imageProps.colors = colors;
|
|
235
|
+
imageProps.locations = locations;
|
|
236
|
+
imageProps.angle = angle;
|
|
237
|
+
}
|
|
238
|
+
const imageStyleToProps = (preImageInfo, imageSize, layoutInfo) => {
|
|
239
|
+
// 初始化
|
|
240
|
+
const imageProps = {
|
|
241
|
+
resizeMode: 'cover',
|
|
242
|
+
style: {
|
|
243
|
+
position: 'absolute'
|
|
244
|
+
// ...StyleSheet.absoluteFillObject
|
|
245
|
+
},
|
|
246
|
+
colors: []
|
|
247
|
+
};
|
|
248
|
+
applyHandlers([backgroundSize, backgroundImage, backgroundPosition, linearGradient], [imageProps, preImageInfo, imageSize, layoutInfo]);
|
|
249
|
+
return imageProps;
|
|
250
|
+
};
|
|
251
|
+
function isHorizontal(val) {
|
|
252
|
+
return typeof val === 'string' && /^(left|right)$/.test(val);
|
|
253
|
+
}
|
|
254
|
+
function isVertical(val) {
|
|
255
|
+
return typeof val === 'string' && /^(top|bottom)$/.test(val);
|
|
256
|
+
}
|
|
257
|
+
function normalizeBackgroundPosition(parts) {
|
|
258
|
+
if (parts.length === 0)
|
|
259
|
+
return [];
|
|
260
|
+
// 定义默认值
|
|
261
|
+
let hStart = 'left';
|
|
262
|
+
let hOffset = 0;
|
|
263
|
+
let vStart = 'top';
|
|
264
|
+
let vOffset = 0;
|
|
265
|
+
if (parts.length === 4)
|
|
266
|
+
return parts;
|
|
267
|
+
// 归一化
|
|
268
|
+
if (parts.length === 1) {
|
|
269
|
+
// 1. center
|
|
270
|
+
// 2. 2px - hOffset, vOffset(center) - center为50%
|
|
271
|
+
// 3. 10% - hOffset, vOffset(center) - center为50%
|
|
272
|
+
// 4. left - hStart, vOffset(center) - center为50%
|
|
273
|
+
// 5. top - hOffset(center), vStart - center为50%
|
|
274
|
+
if (isHorizontal(parts[0])) {
|
|
275
|
+
hStart = parts[0];
|
|
276
|
+
vOffset = '50%';
|
|
277
|
+
}
|
|
278
|
+
else if (isVertical(parts[0])) {
|
|
279
|
+
vStart = parts[0];
|
|
280
|
+
hOffset = '50%';
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
hOffset = parts[0];
|
|
284
|
+
vOffset = '50%';
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
else if (parts.length === 2) {
|
|
288
|
+
// 1. center center - hOffset, vOffset
|
|
289
|
+
// 2. 10px center - hOffset, vStart
|
|
290
|
+
// 3. left center - hStart, vOffset
|
|
291
|
+
// 4. right center - hStart, vOffset
|
|
292
|
+
// 5. 第一位是 left right 覆盖的是 hStart
|
|
293
|
+
// center, 100% 正常的px 覆盖的是 hOffset
|
|
294
|
+
// 第二位是 top bottom 覆盖的是 vStart
|
|
295
|
+
// center, 100% 覆盖的是 vOffset
|
|
296
|
+
//
|
|
297
|
+
// 水平方向
|
|
298
|
+
if (isHorizontal(parts[0])) {
|
|
299
|
+
hStart = parts[0];
|
|
300
|
+
}
|
|
301
|
+
else { // center, 100% 正常的px 覆盖的是 hOffset
|
|
302
|
+
hOffset = parts[0];
|
|
303
|
+
}
|
|
304
|
+
// 垂直方向
|
|
305
|
+
if (isVertical(parts[1])) {
|
|
306
|
+
vStart = parts[1];
|
|
307
|
+
}
|
|
308
|
+
else { // center, 100% 正常的px 覆盖的是 hOffset
|
|
309
|
+
vOffset = parts[1];
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
else if (parts.length === 3) {
|
|
313
|
+
// 1. center top 10px / top 10px center 等价 - center为50%
|
|
314
|
+
// 2. right 10px center / center right 10px 等价 - center为50%
|
|
315
|
+
// 2. bottom 50px right
|
|
316
|
+
if (typeof parts[0] === 'string' && typeof parts[1] === 'string' && /^left|bottom|right|top$/.test(parts[0]) && /^left|bottom|right|top$/.test(parts[1])) {
|
|
317
|
+
[hStart, vStart, vOffset] = parts;
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
[hStart, hOffset, vStart] = parts;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return [hStart, hOffset, vStart, vOffset];
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
*
|
|
327
|
+
* calcSteps - 计算起始位置和终点位置之间的差值
|
|
328
|
+
* startVal - 起始位置距离
|
|
329
|
+
* endVal - 终点位置距离
|
|
330
|
+
* len - 数量
|
|
331
|
+
* **/
|
|
332
|
+
function calcSteps(startVal, endVal, len) {
|
|
333
|
+
const diffVal = endVal - startVal;
|
|
334
|
+
const step = diffVal / len;
|
|
335
|
+
const newArr = [];
|
|
336
|
+
for (let i = 1; i < len; i++) {
|
|
337
|
+
const val = startVal + step * i;
|
|
338
|
+
newArr.push(+val.toFixed(2));
|
|
339
|
+
}
|
|
340
|
+
return newArr;
|
|
341
|
+
}
|
|
342
|
+
function parseLinearGradient(text) {
|
|
343
|
+
let linearText = text.trim().match(/linear-gradient\((.*)\)/)?.[1];
|
|
344
|
+
if (!linearText)
|
|
345
|
+
return;
|
|
346
|
+
// 添加默认的角度
|
|
347
|
+
if (!/^to|^-?\d+deg/.test(linearText)) {
|
|
348
|
+
linearText = '180deg ,' + linearText;
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
linearText = linearText.replace('to', '');
|
|
352
|
+
}
|
|
353
|
+
// 把 0deg, red 10%, blue 20% 解析为 ['0deg', 'red, 10%', 'blue, 20%']
|
|
354
|
+
const [direction, ...colorList] = linearText.split(/,(?![^(#]*\))/);
|
|
355
|
+
// 记录需要填充起点的起始位置
|
|
356
|
+
let startIdx = 0;
|
|
357
|
+
let startVal = 0;
|
|
358
|
+
// 把 ['red, 10%', 'blue, 20%']解析为 [[red, 10%], [blue, 20%]]
|
|
359
|
+
const linearInfo = colorList.map(item => item.trim().split(/(?<!,)\s+/))
|
|
360
|
+
.reduce((prev, cur, idx, self) => {
|
|
361
|
+
const { colors, locations } = prev;
|
|
362
|
+
const [color, val] = cur;
|
|
363
|
+
let numberVal = parseFloat(val) / 100;
|
|
364
|
+
// 处理渐变默认值
|
|
365
|
+
if (idx === 0) {
|
|
366
|
+
numberVal = numberVal || 0;
|
|
367
|
+
}
|
|
368
|
+
else if (self.length - 1 === idx) {
|
|
369
|
+
numberVal = numberVal || 1;
|
|
370
|
+
}
|
|
371
|
+
// 出现缺省值时进行填充
|
|
372
|
+
if (idx - startIdx > 1 && !isNaN(numberVal)) {
|
|
373
|
+
locations.push(...calcSteps(startVal, numberVal, idx - startIdx));
|
|
374
|
+
}
|
|
375
|
+
if (!isNaN(numberVal)) {
|
|
376
|
+
startIdx = idx;
|
|
377
|
+
startVal = numberVal;
|
|
378
|
+
}
|
|
379
|
+
// 添加color的数组
|
|
380
|
+
colors.push(color.trim());
|
|
381
|
+
!isNaN(numberVal) && locations.push(numberVal);
|
|
382
|
+
return prev;
|
|
383
|
+
}, { colors: [], locations: [] });
|
|
384
|
+
return extendObject({}, linearInfo, {
|
|
385
|
+
direction: direction.trim()
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
function parseBgImage(text) {
|
|
389
|
+
if (!text)
|
|
390
|
+
return {};
|
|
391
|
+
const src = parseUrl(text);
|
|
392
|
+
if (src)
|
|
393
|
+
return { src, type: 'image' };
|
|
394
|
+
const linearInfo = parseLinearGradient(text);
|
|
395
|
+
if (!linearInfo)
|
|
396
|
+
return {};
|
|
397
|
+
return {
|
|
398
|
+
linearInfo,
|
|
399
|
+
type: 'linear'
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
function normalizeBackgroundSize(backgroundSize, type) {
|
|
403
|
+
const sizeList = backgroundSize.slice();
|
|
404
|
+
if (sizeList.length === 1)
|
|
405
|
+
sizeList.push('auto');
|
|
406
|
+
if (type === 'linear') {
|
|
407
|
+
// 处理当使用渐变的时候,background-size出现cover, contain, auto,当作100%处理
|
|
408
|
+
for (const i in sizeList) {
|
|
409
|
+
const val = sizeList[i];
|
|
410
|
+
sizeList[i] = /^cover|contain|auto$/.test(val) ? '100%' : val;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return sizeList;
|
|
414
|
+
}
|
|
415
|
+
function preParseImage(imageStyle) {
|
|
416
|
+
const { backgroundImage = '', backgroundSize = ['auto'], backgroundPosition = [0, 0] } = normalizeStyle(imageStyle) || {};
|
|
417
|
+
const { type, src, linearInfo } = parseBgImage(backgroundImage);
|
|
418
|
+
return {
|
|
419
|
+
src,
|
|
420
|
+
linearInfo,
|
|
421
|
+
type,
|
|
422
|
+
sizeList: normalizeBackgroundSize(backgroundSize, type),
|
|
423
|
+
backgroundPosition: normalizeBackgroundPosition(backgroundPosition)
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
function isDiagonalAngle(linearInfo) {
|
|
427
|
+
return !!(linearInfo?.direction && diagonalAngleMap[linearInfo.direction]);
|
|
428
|
+
}
|
|
429
|
+
function inheritStyle(innerStyle = {}) {
|
|
430
|
+
const { borderWidth, borderRadius } = innerStyle;
|
|
431
|
+
const borderStyles = ['borderRadius', 'borderTopLeftRadius', 'borderTopRightRadius', 'borderBottomRightRadius', 'borderBottomLeftRadius'];
|
|
432
|
+
return pickStyle(innerStyle, borderStyles, borderWidth && borderRadius
|
|
433
|
+
? (key, val) => {
|
|
434
|
+
// 盒子内圆角borderWith与borderRadius的关系
|
|
435
|
+
// 当borderRadius 小于 当borderWith 内边框为直角
|
|
436
|
+
// 当borderRadius 大于等于 当borderWith 内边框为圆角
|
|
437
|
+
if (borderStyles.includes(key)) {
|
|
438
|
+
const borderVal = +val - borderWidth;
|
|
439
|
+
return borderVal > 0 ? borderVal : 0;
|
|
440
|
+
}
|
|
441
|
+
return val;
|
|
442
|
+
}
|
|
443
|
+
: undefined);
|
|
444
|
+
}
|
|
445
|
+
function useWrapImage(imageStyle, innerStyle, enableFastImage) {
|
|
446
|
+
// 预处理数据
|
|
447
|
+
const preImageInfo = preParseImage(imageStyle);
|
|
448
|
+
// 预解析
|
|
449
|
+
const { src, sizeList, type } = preImageInfo;
|
|
450
|
+
// 判断是否可挂载onLayout
|
|
451
|
+
const { needLayout, needImageSize } = checkNeedLayout(preImageInfo);
|
|
452
|
+
const [show, setShow] = useState(((type === 'image' && !!src) || type === 'linear') && !needLayout && !needImageSize);
|
|
453
|
+
const [, setImageSizeWidth] = useState(null);
|
|
454
|
+
const [, setImageSizeHeight] = useState(null);
|
|
455
|
+
const [, setLayoutInfoWidth] = useState(null);
|
|
456
|
+
const [, setLayoutInfoHeight] = useState(null);
|
|
457
|
+
const sizeInfo = useRef(null);
|
|
458
|
+
const layoutInfo = useRef(null);
|
|
459
|
+
useEffect(() => {
|
|
460
|
+
sizeInfo.current = null;
|
|
461
|
+
if (type === 'linear') {
|
|
462
|
+
if (!needLayout)
|
|
463
|
+
setShow(true);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
if (!src) {
|
|
467
|
+
setShow(false);
|
|
468
|
+
return;
|
|
469
|
+
// 一开始未出现,数据改变时出现
|
|
470
|
+
}
|
|
471
|
+
else if (!(needLayout || needImageSize)) {
|
|
472
|
+
setShow(true);
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
if (needImageSize) {
|
|
476
|
+
Image.getSize(src, (width, height) => {
|
|
477
|
+
sizeInfo.current = {
|
|
478
|
+
width,
|
|
479
|
+
height
|
|
480
|
+
};
|
|
481
|
+
// 1. 当需要绑定onLayout 2. 获取到布局信息
|
|
482
|
+
if (!needLayout || layoutInfo.current) {
|
|
483
|
+
setImageSizeWidth(width);
|
|
484
|
+
setImageSizeHeight(height);
|
|
485
|
+
if (layoutInfo.current) {
|
|
486
|
+
setLayoutInfoWidth(layoutInfo.current.width);
|
|
487
|
+
setLayoutInfoHeight(layoutInfo.current.height);
|
|
488
|
+
}
|
|
489
|
+
setShow(true);
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
// type 添加type 处理无渐变 有渐变的场景
|
|
494
|
+
}, [src, type]);
|
|
495
|
+
if (!type)
|
|
496
|
+
return null;
|
|
497
|
+
const onLayout = (res) => {
|
|
498
|
+
const { width, height } = res?.nativeEvent?.layout || {};
|
|
499
|
+
layoutInfo.current = {
|
|
500
|
+
width,
|
|
501
|
+
height
|
|
502
|
+
};
|
|
503
|
+
if (!needImageSize) {
|
|
504
|
+
setLayoutInfoWidth(width);
|
|
505
|
+
setLayoutInfoHeight(height);
|
|
506
|
+
// 有渐变角度的时候,才触发渲染组件
|
|
507
|
+
if (type === 'linear') {
|
|
508
|
+
sizeInfo.current = {
|
|
509
|
+
width: calcPercent(sizeList[0], width),
|
|
510
|
+
height: calcPercent(sizeList[1], height)
|
|
511
|
+
};
|
|
512
|
+
setImageSizeWidth(sizeInfo.current.width);
|
|
513
|
+
setImageSizeHeight(sizeInfo.current.height);
|
|
514
|
+
}
|
|
515
|
+
setShow(true);
|
|
516
|
+
}
|
|
517
|
+
else if (sizeInfo.current) {
|
|
518
|
+
setLayoutInfoWidth(width);
|
|
519
|
+
setLayoutInfoHeight(height);
|
|
520
|
+
setImageSizeWidth(sizeInfo.current.width);
|
|
521
|
+
setImageSizeHeight(sizeInfo.current.height);
|
|
522
|
+
setShow(true);
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
return <View key='backgroundImage' {...needLayout ? { onLayout } : null} style={{ ...inheritStyle(innerStyle), ...StyleSheet.absoluteFillObject, overflow: 'hidden' }}>
|
|
526
|
+
{show && type === 'linear' && <LinearGradient useAngle={true} {...imageStyleToProps(preImageInfo, sizeInfo.current, layoutInfo.current)}/>}
|
|
527
|
+
{show && type === 'image' && (renderImage(imageStyleToProps(preImageInfo, sizeInfo.current, layoutInfo.current), enableFastImage))}
|
|
528
|
+
</View>;
|
|
529
|
+
}
|
|
530
|
+
function wrapWithChildren(props, { hasVarDec, enableBackground, textStyle, backgroundStyle, varContext, textProps, innerStyle, enableFastImage }) {
|
|
531
|
+
const children = wrapChildren(props, {
|
|
532
|
+
hasVarDec,
|
|
533
|
+
varContext,
|
|
534
|
+
textStyle,
|
|
535
|
+
textProps
|
|
536
|
+
});
|
|
537
|
+
return [
|
|
538
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
539
|
+
enableBackground ? useWrapImage(backgroundStyle, innerStyle, enableFastImage) : null,
|
|
540
|
+
children
|
|
541
|
+
];
|
|
542
|
+
}
|
|
543
|
+
const _View = forwardRef((viewProps, ref) => {
|
|
544
|
+
const { textProps, innerProps: props = {} } = splitProps(viewProps);
|
|
545
|
+
let { style = {}, 'hover-style': hoverStyle, 'hover-start-time': hoverStartTime = 50, 'hover-stay-time': hoverStayTime = 400, 'enable-var': enableVar, 'external-var-context': externalVarContext, 'enable-background': enableBackground, 'enable-fast-image': enableFastImage, 'enable-animation': enableAnimation, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight, animation } = props;
|
|
546
|
+
// 默认样式
|
|
547
|
+
const defaultStyle = style.display === 'flex'
|
|
548
|
+
? {
|
|
549
|
+
flexDirection: 'row',
|
|
550
|
+
flexBasis: 'auto',
|
|
551
|
+
flexShrink: 1,
|
|
552
|
+
flexWrap: 'nowrap'
|
|
553
|
+
}
|
|
554
|
+
: {};
|
|
555
|
+
const enableHover = !!hoverStyle;
|
|
556
|
+
const { isHover, gesture } = useHover({ enableHover, hoverStartTime, hoverStayTime });
|
|
557
|
+
const styleObj = extendObject({}, defaultStyle, style, isHover ? hoverStyle : {});
|
|
558
|
+
const { normalStyle, hasSelfPercent, hasVarDec, varContextRef, setWidth, setHeight } = useTransformStyle(styleObj, {
|
|
559
|
+
enableVar,
|
|
560
|
+
externalVarContext,
|
|
561
|
+
parentFontSize,
|
|
562
|
+
parentWidth,
|
|
563
|
+
parentHeight
|
|
564
|
+
});
|
|
565
|
+
const { textStyle, backgroundStyle, innerStyle = {} } = splitStyle(normalStyle);
|
|
566
|
+
enableBackground = enableBackground || !!backgroundStyle;
|
|
567
|
+
const enableBackgroundRef = useRef(enableBackground);
|
|
568
|
+
if (enableBackgroundRef.current !== enableBackground) {
|
|
569
|
+
error('[Mpx runtime error]: background use should be stable in the component lifecycle, or you can set [enable-background] with true.');
|
|
570
|
+
}
|
|
571
|
+
const nodeRef = useRef(null);
|
|
572
|
+
useNodesRef(props, ref, nodeRef, {
|
|
573
|
+
style: normalStyle
|
|
574
|
+
});
|
|
575
|
+
const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef });
|
|
576
|
+
const viewStyle = extendObject({}, innerStyle, layoutStyle);
|
|
577
|
+
const { enableStyleAnimation, animationStyle } = useAnimationHooks({
|
|
578
|
+
enableAnimation,
|
|
579
|
+
animation,
|
|
580
|
+
style: viewStyle
|
|
581
|
+
});
|
|
582
|
+
const innerProps = useInnerProps(props, extendObject({
|
|
583
|
+
ref: nodeRef,
|
|
584
|
+
style: enableStyleAnimation ? [viewStyle, animationStyle] : viewStyle
|
|
585
|
+
}, layoutProps), [
|
|
586
|
+
'hover-start-time',
|
|
587
|
+
'hover-stay-time',
|
|
588
|
+
'hover-style',
|
|
589
|
+
'hover-class'
|
|
590
|
+
], {
|
|
591
|
+
layoutRef
|
|
592
|
+
});
|
|
593
|
+
const childNode = wrapWithChildren(props, {
|
|
594
|
+
hasVarDec,
|
|
595
|
+
enableBackground: enableBackgroundRef.current,
|
|
596
|
+
textStyle,
|
|
597
|
+
backgroundStyle,
|
|
598
|
+
varContext: varContextRef.current,
|
|
599
|
+
textProps,
|
|
600
|
+
innerStyle,
|
|
601
|
+
enableFastImage
|
|
602
|
+
});
|
|
603
|
+
const BaseComponent = enableStyleAnimation
|
|
604
|
+
? createElement(Animated.View, innerProps, childNode)
|
|
605
|
+
: createElement(View, innerProps, childNode);
|
|
606
|
+
return enableHover
|
|
607
|
+
? createElement(GestureDetector, { gesture: gesture }, BaseComponent)
|
|
608
|
+
: BaseComponent;
|
|
609
|
+
});
|
|
610
|
+
_View.displayName = 'MpxView';
|
|
611
|
+
export default _View;
|