@mpxjs/webpack-plugin 2.10.15 → 2.10.16-beta.3
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/dependencies/AppEntryDependency.js +2 -2
- package/lib/dependencies/DynamicEntryDependency.js +1 -1
- package/lib/dependencies/ImportDependency.js +102 -0
- package/lib/dependencies/RecordModuleIdMapDependency.js +49 -0
- package/lib/dependencies/ResolveDependency.js +1 -1
- package/lib/{retry-runtime-module.js → dependencies/RetryRuntimeModule.js} +1 -1
- package/lib/helpers.js +2 -0
- package/lib/index.js +51 -25
- package/lib/json-compiler/helper.js +72 -2
- package/lib/json-compiler/index.js +14 -54
- package/lib/json-compiler/plugin.js +2 -2
- package/lib/loader.js +6 -2
- package/lib/native-loader.js +6 -3
- package/lib/platform/json/wx/index.js +24 -29
- package/lib/platform/style/wx/index.js +8 -1
- package/lib/platform/template/wx/component-config/button.js +12 -3
- package/lib/platform/template/wx/component-config/camera.js +12 -0
- package/lib/platform/template/wx/component-config/component.js +31 -33
- package/lib/platform/template/wx/component-config/slider.js +12 -0
- package/lib/platform/template/wx/component-config/unsupported.js +1 -1
- package/lib/react/processJSON.js +39 -71
- package/lib/react/processStyles.js +3 -2
- package/lib/react/processTemplate.js +6 -6
- package/lib/react/script-helper.js +6 -16
- package/lib/react/style-helper.js +10 -2
- package/lib/resolver/AddEnvPlugin.js +13 -0
- package/lib/resolver/AddModePlugin.js +18 -0
- package/lib/runtime/components/react/context.ts +2 -0
- package/lib/runtime/components/react/dist/context.js +1 -0
- package/lib/runtime/components/react/dist/mpx-camera.jsx +102 -0
- package/lib/runtime/components/react/dist/mpx-image.jsx +81 -37
- package/lib/runtime/components/react/dist/mpx-keyboard-avoiding-view.jsx +19 -4
- package/lib/runtime/components/react/dist/mpx-picker-view/index.jsx +3 -2
- package/lib/runtime/components/react/dist/mpx-picker-view-column/index.jsx +9 -6
- package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewColumnItem.jsx +8 -11
- package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewColumnItemLite.jsx +20 -0
- package/lib/runtime/components/react/dist/mpx-portal/index.jsx +5 -1
- package/lib/runtime/components/react/dist/mpx-progress.jsx +26 -22
- package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +6 -14
- package/lib/runtime/components/react/dist/mpx-slider.jsx +321 -0
- package/lib/runtime/components/react/dist/mpx-text.jsx +33 -5
- package/lib/runtime/components/react/dist/mpx-view.jsx +8 -11
- package/lib/runtime/components/react/dist/mpx-web-view.jsx +1 -1
- package/lib/runtime/components/react/dist/utils.jsx +16 -6
- package/lib/runtime/components/react/mpx-camera.tsx +167 -0
- package/lib/runtime/components/react/mpx-image.tsx +89 -42
- package/lib/runtime/components/react/mpx-keyboard-avoiding-view.tsx +31 -4
- package/lib/runtime/components/react/mpx-picker-view/index.tsx +4 -1
- package/lib/runtime/components/react/mpx-picker-view-column/index.tsx +19 -8
- package/lib/runtime/components/react/mpx-picker-view-column/pickerViewColumnItem.tsx +8 -12
- package/lib/runtime/components/react/mpx-picker-view-column/pickerViewColumnItemLite.tsx +55 -0
- package/lib/runtime/components/react/mpx-portal/index.tsx +8 -2
- package/lib/runtime/components/react/mpx-progress.tsx +26 -24
- package/lib/runtime/components/react/mpx-scroll-view.tsx +6 -17
- package/lib/runtime/components/react/mpx-slider.tsx +444 -0
- package/lib/runtime/components/react/mpx-text.tsx +38 -5
- package/lib/runtime/components/react/mpx-view.tsx +8 -11
- package/lib/runtime/components/react/mpx-web-view.tsx +1 -1
- package/lib/runtime/components/react/utils.tsx +15 -6
- package/lib/runtime/components/web/mpx-input.vue +1 -1
- package/lib/runtime/components/web/mpx-scroll-view.vue +7 -1
- package/lib/runtime/components/web/mpx-video.vue +12 -1
- package/lib/runtime/optionProcessor.js +3 -1
- package/lib/runtime/optionProcessorReact.js +4 -2
- package/lib/script-setup-compiler/index.js +2 -2
- package/lib/style-compiler/index.js +3 -2
- package/lib/style-compiler/load-postcss-config.js +1 -1
- package/lib/style-compiler/plugins/trans-special.js +10 -2
- package/lib/style-compiler/strip-conditional-loader.js +155 -15
- package/lib/template-compiler/compiler.js +262 -61
- package/lib/template-compiler/gen-node-react.js +18 -6
- package/lib/template-compiler/index.js +6 -4
- package/lib/template-compiler/parse-exps.js +1 -1
- package/lib/utils/chain-assign.js +47 -0
- package/lib/utils/check-core-version-match.js +75 -15
- package/lib/utils/const.js +2 -1
- package/lib/utils/dom-tag-config.js +1 -1
- package/lib/utils/env.js +6 -1
- package/lib/utils/get-build-tag-component.js +35 -0
- package/lib/utils/pre-process-json.js +5 -0
- package/lib/web/processJSON.js +44 -16
- package/lib/web/processScript.js +1 -1
- package/lib/web/processTemplate.js +4 -4
- package/lib/web/script-helper.js +19 -9
- package/lib/wxs/pre-loader.js +6 -6
- package/lib/wxss/loader.js +1 -9
- package/package.json +14 -5
- package/LICENSE +0 -433
- package/lib/dependencies/ImportDependencyTemplate.js +0 -50
|
@@ -17,12 +17,12 @@ import { View } from 'react-native';
|
|
|
17
17
|
import Animated, { useSharedValue, useAnimatedStyle, withTiming, Easing, runOnJS } from 'react-native-reanimated';
|
|
18
18
|
import useInnerProps from './getInnerListeners';
|
|
19
19
|
import useNodesRef from './useNodesRef';
|
|
20
|
-
import { useLayout, useTransformStyle, extendObject } from './utils';
|
|
20
|
+
import { useLayout, useTransformStyle, extendObject, useRunOnJSCallback } from './utils';
|
|
21
21
|
import Portal from './mpx-portal';
|
|
22
22
|
const Progress = forwardRef((props, ref) => {
|
|
23
|
-
const { percent = 0, 'stroke-width': strokeWidth = 6, color, activeColor = color || '#09BB07', backgroundColor = '#EBEBEB', active = false, 'active-mode': activeMode = 'backwards', duration = 30,
|
|
23
|
+
const { percent = 0, 'stroke-width': strokeWidth = 6, color, activeColor = color || '#09BB07', backgroundColor = '#EBEBEB', active = false, 'active-mode': activeMode = 'backwards', duration = 30, style = {}, 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight } = props;
|
|
24
24
|
const nodeRef = useRef(null);
|
|
25
|
-
const propsRef = useRef(
|
|
25
|
+
const propsRef = useRef(props);
|
|
26
26
|
propsRef.current = props;
|
|
27
27
|
// 进度值状态
|
|
28
28
|
const [lastPercent, setLastPercent] = useState(0);
|
|
@@ -44,6 +44,21 @@ const Progress = forwardRef((props, ref) => {
|
|
|
44
44
|
useNodesRef(props, ref, nodeRef, {
|
|
45
45
|
style: normalStyle
|
|
46
46
|
});
|
|
47
|
+
// 使用 useRunOnJSCallback 处理动画回调
|
|
48
|
+
const runOnJSCallbackRef = useRef({
|
|
49
|
+
triggerActiveEnd: (percent) => {
|
|
50
|
+
const currentProps = propsRef.current;
|
|
51
|
+
if (currentProps.bindactiveend) {
|
|
52
|
+
currentProps.bindactiveend({
|
|
53
|
+
type: 'activeend',
|
|
54
|
+
detail: {
|
|
55
|
+
percent: percent
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
const runOnJSCallback = useRunOnJSCallback(runOnJSCallbackRef);
|
|
47
62
|
// 进度条动画函数
|
|
48
63
|
const startProgressAnimation = (targetPercent, startPercent, animationDuration, onFinished) => {
|
|
49
64
|
// 根据 active-mode 设置起始位置
|
|
@@ -53,22 +68,11 @@ const Progress = forwardRef((props, ref) => {
|
|
|
53
68
|
easing: Easing.linear
|
|
54
69
|
}, (finished) => {
|
|
55
70
|
if (finished && onFinished) {
|
|
56
|
-
//
|
|
57
|
-
|
|
71
|
+
// 在动画回调中,执行传入的worklet函数
|
|
72
|
+
onFinished();
|
|
58
73
|
}
|
|
59
74
|
});
|
|
60
75
|
};
|
|
61
|
-
// 创建在主线程执行的事件回调函数
|
|
62
|
-
const triggerActiveEnd = (percent) => {
|
|
63
|
-
if (bindactiveend) {
|
|
64
|
-
bindactiveend({
|
|
65
|
-
type: 'activeend',
|
|
66
|
-
detail: {
|
|
67
|
-
percent: percent
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
76
|
// 进度变化时的动画效果
|
|
73
77
|
useEffect(() => {
|
|
74
78
|
const targetPercent = Math.max(0, Math.min(100, percent));
|
|
@@ -85,18 +89,18 @@ const Progress = forwardRef((props, ref) => {
|
|
|
85
89
|
// 计算动画持续时间
|
|
86
90
|
const percentDiff = Math.abs(targetPercent - startPercent);
|
|
87
91
|
const animationDuration = percentDiff * duration;
|
|
88
|
-
// 创建动画完成回调
|
|
89
|
-
const onAnimationFinished = () => {
|
|
90
|
-
triggerActiveEnd(targetPercent);
|
|
91
|
-
};
|
|
92
92
|
// 执行动画
|
|
93
|
-
startProgressAnimation(targetPercent, startPercent, animationDuration,
|
|
93
|
+
startProgressAnimation(targetPercent, startPercent, animationDuration, () => {
|
|
94
|
+
'worklet';
|
|
95
|
+
// 在worklet函数内部执行runOnJS调用runOnJSCallback
|
|
96
|
+
runOnJS(runOnJSCallback)('triggerActiveEnd', targetPercent);
|
|
97
|
+
});
|
|
94
98
|
}
|
|
95
99
|
else {
|
|
96
100
|
progressWidth.value = targetPercent;
|
|
97
101
|
}
|
|
98
102
|
setLastPercent(targetPercent);
|
|
99
|
-
}, [percent, active, activeMode, duration
|
|
103
|
+
}, [percent, active, activeMode, duration]);
|
|
100
104
|
// 初始化时设置进度值
|
|
101
105
|
useEffect(() => {
|
|
102
106
|
if (!active) {
|
|
@@ -48,8 +48,6 @@ const _ScrollView = forwardRef((scrollViewProps = {}, ref) => {
|
|
|
48
48
|
const scrollOffset = useRef(new RNAnimated.Value(0)).current;
|
|
49
49
|
const simultaneousHandlers = flatGesture(originSimultaneousHandlers);
|
|
50
50
|
const waitForHandlers = flatGesture(waitFor);
|
|
51
|
-
const snapScrollTop = useRef(0);
|
|
52
|
-
const snapScrollLeft = useRef(0);
|
|
53
51
|
const [refreshing, setRefreshing] = useState(false);
|
|
54
52
|
const [enableScroll, setEnableScroll] = useState(true);
|
|
55
53
|
const enableScrollValue = useSharedValue(true);
|
|
@@ -127,14 +125,12 @@ const _ScrollView = forwardRef((scrollViewProps = {}, ref) => {
|
|
|
127
125
|
warn('scroll-x and scroll-y cannot be set to true at the same time, Mpx will use the value of scroll-y as the criterion');
|
|
128
126
|
}
|
|
129
127
|
useEffect(() => {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
};
|
|
137
|
-
}
|
|
128
|
+
initialTimeout.current = setTimeout(() => {
|
|
129
|
+
scrollToOffset(scrollLeft, scrollTop);
|
|
130
|
+
}, 0);
|
|
131
|
+
return () => {
|
|
132
|
+
initialTimeout.current && clearTimeout(initialTimeout.current);
|
|
133
|
+
};
|
|
138
134
|
}, [scrollTop, scrollLeft]);
|
|
139
135
|
useEffect(() => {
|
|
140
136
|
if (scrollIntoView && __selectRef) {
|
|
@@ -316,10 +312,6 @@ const _ScrollView = forwardRef((scrollViewProps = {}, ref) => {
|
|
|
316
312
|
function scrollToOffset(x = 0, y = 0, animated = scrollWithAnimation) {
|
|
317
313
|
if (scrollViewRef.current) {
|
|
318
314
|
scrollViewRef.current.scrollTo({ x, y, animated });
|
|
319
|
-
scrollOptions.current.scrollLeft = x;
|
|
320
|
-
scrollOptions.current.scrollTop = y;
|
|
321
|
-
snapScrollLeft.current = x;
|
|
322
|
-
snapScrollTop.current = y;
|
|
323
315
|
}
|
|
324
316
|
}
|
|
325
317
|
function onScrollTouchMove(e) {
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ✔ min 最小值
|
|
3
|
+
* ✔ max 最大值
|
|
4
|
+
* ✔ step 步长
|
|
5
|
+
* ✔ disabled 是否禁用
|
|
6
|
+
* ✔ value 当前取值
|
|
7
|
+
* ✔ color 背景条的颜色(已废弃,使用 backgroundColor)
|
|
8
|
+
* ✔ selected-color 已选择的颜色(已废弃,使用 activeColor)
|
|
9
|
+
* ✔ activeColor 已选择的颜色
|
|
10
|
+
* ✔ backgroundColor 背景条的颜色
|
|
11
|
+
* ✔ block-size 滑块的大小
|
|
12
|
+
* ✔ block-color 滑块的颜色
|
|
13
|
+
* ✘ show-value 是否显示当前 value
|
|
14
|
+
* ✔ bindchange 完成一次拖动后触发的事件
|
|
15
|
+
* ✔ bindchanging 拖动过程中触发的事件
|
|
16
|
+
*/
|
|
17
|
+
import { useRef, forwardRef, useEffect, useState, createElement, useContext, useMemo } from 'react';
|
|
18
|
+
import { View } from 'react-native';
|
|
19
|
+
import { GestureDetector, Gesture } from 'react-native-gesture-handler';
|
|
20
|
+
import Animated, { useSharedValue, useAnimatedStyle, runOnJS } from 'react-native-reanimated';
|
|
21
|
+
import { warn } from '@mpxjs/utils';
|
|
22
|
+
import useInnerProps, { getCustomEvent } from './getInnerListeners';
|
|
23
|
+
import useNodesRef from './useNodesRef';
|
|
24
|
+
import { useLayout, useTransformStyle, extendObject, useRunOnJSCallback } from './utils';
|
|
25
|
+
import Portal from './mpx-portal';
|
|
26
|
+
import { FormContext } from './context';
|
|
27
|
+
const Slider = forwardRef((props, ref) => {
|
|
28
|
+
const { min: rawMin = 0, max: rawMax = 100, step: rawStep = 1, disabled = false, value: rawValue, color, 'selected-color': selectedColor, activeColor = selectedColor || color || '#1aad19', backgroundColor = color || '#e9e9e9', 'block-size': rawBlockSize = 28, 'block-color': blockColor = '#ffffff', name, style = {}, 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight } = props;
|
|
29
|
+
// 确保数值类型正确
|
|
30
|
+
const min = typeof rawMin === 'string' ? parseFloat(rawMin) : rawMin;
|
|
31
|
+
const max = typeof rawMax === 'string' ? parseFloat(rawMax) : rawMax;
|
|
32
|
+
const step = typeof rawStep === 'string' ? parseFloat(rawStep) : rawStep;
|
|
33
|
+
const value = rawValue !== undefined ? (typeof rawValue === 'string' ? parseFloat(rawValue) : rawValue) : undefined;
|
|
34
|
+
const blockSize = typeof rawBlockSize === 'string' ? parseFloat(rawBlockSize) : rawBlockSize;
|
|
35
|
+
// 如果没有提供 value,则使用 min 作为默认值
|
|
36
|
+
const defaultValue = value !== undefined ? value : min;
|
|
37
|
+
const nodeRef = useRef(null);
|
|
38
|
+
const trackRef = useRef(null);
|
|
39
|
+
const [currentValue, setCurrentValue] = useState(defaultValue);
|
|
40
|
+
const [trackWidth, setTrackWidth] = useState(0);
|
|
41
|
+
const thumbPosition = useSharedValue(0);
|
|
42
|
+
const isDragging = useSharedValue(false);
|
|
43
|
+
const startDragPosition = useSharedValue(0); // 记录拖拽开始时的位置
|
|
44
|
+
const startDragValue = useSharedValue(0); // 记录拖拽开始时的值
|
|
45
|
+
let formValuesMap;
|
|
46
|
+
const propsRef = useRef(props);
|
|
47
|
+
propsRef.current = props;
|
|
48
|
+
const formContext = useContext(FormContext);
|
|
49
|
+
if (formContext) {
|
|
50
|
+
formValuesMap = formContext.formValuesMap;
|
|
51
|
+
}
|
|
52
|
+
const { normalStyle, hasSelfPercent, setWidth, setHeight, hasPositionFixed } = useTransformStyle(style, {
|
|
53
|
+
enableVar,
|
|
54
|
+
externalVarContext,
|
|
55
|
+
parentFontSize,
|
|
56
|
+
parentWidth,
|
|
57
|
+
parentHeight
|
|
58
|
+
});
|
|
59
|
+
const { layoutRef, layoutStyle, layoutProps } = useLayout({
|
|
60
|
+
props,
|
|
61
|
+
hasSelfPercent,
|
|
62
|
+
setWidth,
|
|
63
|
+
setHeight,
|
|
64
|
+
nodeRef
|
|
65
|
+
});
|
|
66
|
+
useNodesRef(props, ref, nodeRef, {
|
|
67
|
+
style: normalStyle
|
|
68
|
+
});
|
|
69
|
+
// 使用 useRunOnJSCallback 处理手势回调
|
|
70
|
+
const runOnJSCallbackRef = useRef({
|
|
71
|
+
triggerChangeEvent: (newValue) => {
|
|
72
|
+
setCurrentValue(newValue);
|
|
73
|
+
const currentProps = propsRef.current;
|
|
74
|
+
const changeHandler = currentProps.bindchange || currentProps.catchchange;
|
|
75
|
+
if (changeHandler) {
|
|
76
|
+
changeHandler(getCustomEvent('change', {}, { layoutRef, detail: { value: newValue } }, currentProps));
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
triggerChangingEvent: (newValue) => {
|
|
80
|
+
const currentProps = propsRef.current;
|
|
81
|
+
const changingHandler = currentProps.bindchanging || currentProps.catchchanging;
|
|
82
|
+
if (changingHandler) {
|
|
83
|
+
changingHandler(getCustomEvent('changing', {}, { layoutRef, detail: { value: newValue } }, currentProps));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
const runOnJSCallback = useRunOnJSCallback(runOnJSCallbackRef);
|
|
88
|
+
// 限制步长,确保 step 大于 0,并且可被 (max - min) 整除
|
|
89
|
+
const validateStep = (step, min, max) => {
|
|
90
|
+
if (step <= 0)
|
|
91
|
+
return 1;
|
|
92
|
+
if ((max - min) % step !== 0) {
|
|
93
|
+
warn(`Step ${step} is not a divisor of range ${max - min}`);
|
|
94
|
+
}
|
|
95
|
+
return step;
|
|
96
|
+
};
|
|
97
|
+
const validStep = validateStep(step, min, max);
|
|
98
|
+
// 将值约束在 min-max 范围内,并按步长对齐
|
|
99
|
+
const constrainValue = (val, minVal = min, maxVal = max, stepVal = validStep) => {
|
|
100
|
+
const constrained = Math.max(minVal, Math.min(maxVal, val));
|
|
101
|
+
const steps = Math.round((constrained - minVal) / stepVal);
|
|
102
|
+
return minVal + steps * stepVal;
|
|
103
|
+
};
|
|
104
|
+
// 计算滑块位置
|
|
105
|
+
const getThumbPosition = (val, trackW = trackWidth, minVal = min, maxVal = max) => {
|
|
106
|
+
if (trackW === 0)
|
|
107
|
+
return 0;
|
|
108
|
+
const percentage = (val - minVal) / (maxVal - minVal);
|
|
109
|
+
const position = percentage * trackW;
|
|
110
|
+
return position;
|
|
111
|
+
};
|
|
112
|
+
// 手势处理
|
|
113
|
+
const panGesture = useMemo(() => {
|
|
114
|
+
const getThumbPositionWorklet = (val, trackW, minVal, maxVal) => {
|
|
115
|
+
'worklet';
|
|
116
|
+
if (trackW === 0)
|
|
117
|
+
return 0;
|
|
118
|
+
const percentage = (val - minVal) / (maxVal - minVal);
|
|
119
|
+
return percentage * trackW;
|
|
120
|
+
};
|
|
121
|
+
const constrainValueWorklet = (val, minVal, maxVal, stepVal) => {
|
|
122
|
+
'worklet';
|
|
123
|
+
const constrained = Math.max(minVal, Math.min(maxVal, val));
|
|
124
|
+
const steps = Math.round((constrained - minVal) / stepVal);
|
|
125
|
+
return minVal + steps * stepVal;
|
|
126
|
+
};
|
|
127
|
+
return Gesture.Pan()
|
|
128
|
+
.enabled(!disabled) // 通过手势启用状态控制是否可拖拽
|
|
129
|
+
.onBegin(() => {
|
|
130
|
+
'worklet';
|
|
131
|
+
if (trackWidth === 0)
|
|
132
|
+
return;
|
|
133
|
+
isDragging.value = true;
|
|
134
|
+
// 记录拖拽开始时的位置 - 使用当前的动画位置
|
|
135
|
+
startDragPosition.value = thumbPosition.value;
|
|
136
|
+
// 根据当前位置反推值
|
|
137
|
+
const percentage = thumbPosition.value / trackWidth;
|
|
138
|
+
const currentVal = min + percentage * (max - min);
|
|
139
|
+
startDragValue.value = constrainValueWorklet(currentVal, min, max, validStep);
|
|
140
|
+
})
|
|
141
|
+
.onUpdate((event) => {
|
|
142
|
+
'worklet';
|
|
143
|
+
if (trackWidth === 0)
|
|
144
|
+
return;
|
|
145
|
+
// 基于拖拽开始位置计算新位置
|
|
146
|
+
const newX = startDragPosition.value + event.translationX;
|
|
147
|
+
const clampedX = Math.max(0, Math.min(trackWidth, newX));
|
|
148
|
+
// 计算新值
|
|
149
|
+
const percentage = clampedX / trackWidth;
|
|
150
|
+
const rawValue = min + percentage * (max - min);
|
|
151
|
+
const newValue = constrainValueWorklet(rawValue, min, max, validStep);
|
|
152
|
+
// 更新滑块位置 - 使用约束后的值对应的位置
|
|
153
|
+
const constrainedPosition = getThumbPositionWorklet(newValue, trackWidth, min, max);
|
|
154
|
+
thumbPosition.value = constrainedPosition;
|
|
155
|
+
// 只触发 changing 事件,不更新 currentValue(避免干扰拖拽)
|
|
156
|
+
runOnJS(runOnJSCallback)('triggerChangingEvent', newValue);
|
|
157
|
+
})
|
|
158
|
+
.onEnd((event) => {
|
|
159
|
+
'worklet';
|
|
160
|
+
isDragging.value = false;
|
|
161
|
+
// 基于拖拽开始位置计算最终位置
|
|
162
|
+
const newX = startDragPosition.value + event.translationX;
|
|
163
|
+
const clampedX = Math.max(0, Math.min(trackWidth, newX));
|
|
164
|
+
const percentage = clampedX / trackWidth;
|
|
165
|
+
const rawValue = min + percentage * (max - min);
|
|
166
|
+
const finalValue = constrainValueWorklet(rawValue, min, max, validStep);
|
|
167
|
+
// 确保滑块位置与最终值匹配
|
|
168
|
+
const finalPosition = getThumbPositionWorklet(finalValue, trackWidth, min, max);
|
|
169
|
+
thumbPosition.value = finalPosition;
|
|
170
|
+
// 更新 currentValue 并触发 change 事件
|
|
171
|
+
runOnJS(runOnJSCallback)('triggerChangeEvent', finalValue);
|
|
172
|
+
});
|
|
173
|
+
}, [disabled, trackWidth, min, max, validStep, runOnJSCallback]);
|
|
174
|
+
// 当 value 属性变化时更新位置
|
|
175
|
+
useEffect(() => {
|
|
176
|
+
const newValue = constrainValue(defaultValue);
|
|
177
|
+
setCurrentValue(newValue);
|
|
178
|
+
// 同时更新动画位置
|
|
179
|
+
thumbPosition.value = getThumbPosition(newValue);
|
|
180
|
+
}, [defaultValue, min, max, validStep]);
|
|
181
|
+
// 当 trackWidth 变化时更新滑块位置
|
|
182
|
+
useEffect(() => {
|
|
183
|
+
// 只在非拖拽状态下更新位置
|
|
184
|
+
if (!isDragging.value) {
|
|
185
|
+
thumbPosition.value = getThumbPosition(currentValue);
|
|
186
|
+
}
|
|
187
|
+
}, [trackWidth, currentValue]);
|
|
188
|
+
// 动画样式
|
|
189
|
+
const animatedThumbStyle = useAnimatedStyle(() => {
|
|
190
|
+
const blockSizeNum = Math.max(12, Math.min(28, blockSize));
|
|
191
|
+
const trackHeight = 4;
|
|
192
|
+
return {
|
|
193
|
+
position: 'absolute',
|
|
194
|
+
top: -((blockSizeNum - trackHeight) / 2),
|
|
195
|
+
left: Math.max(0, Math.min(trackWidth - blockSizeNum, thumbPosition.value - (blockSizeNum / 2))),
|
|
196
|
+
width: blockSizeNum,
|
|
197
|
+
height: blockSizeNum,
|
|
198
|
+
justifyContent: 'center',
|
|
199
|
+
alignItems: 'center'
|
|
200
|
+
};
|
|
201
|
+
});
|
|
202
|
+
// 轨道布局回调
|
|
203
|
+
const onTrackLayout = (event) => {
|
|
204
|
+
const { width } = event.nativeEvent.layout;
|
|
205
|
+
setTrackWidth(width);
|
|
206
|
+
};
|
|
207
|
+
// 表单相关处理
|
|
208
|
+
const resetValue = () => {
|
|
209
|
+
const currentProps = propsRef.current;
|
|
210
|
+
const currentValue = currentProps.value !== undefined ? currentProps.value : currentProps.min || 0;
|
|
211
|
+
const parsedValue = typeof currentValue === 'string' ? parseFloat(currentValue) : currentValue;
|
|
212
|
+
const currentMin = typeof currentProps.min === 'string' ? parseFloat(currentProps.min) : (currentProps.min || 0);
|
|
213
|
+
const currentMax = typeof currentProps.max === 'string' ? parseFloat(currentProps.max) : (currentProps.max || 100);
|
|
214
|
+
const currentStep = typeof currentProps.step === 'string' ? parseFloat(currentProps.step) : (currentProps.step || 1);
|
|
215
|
+
const resetVal = parsedValue !== undefined ? parsedValue : currentMin;
|
|
216
|
+
const validatedStep = validateStep(currentStep, currentMin, currentMax);
|
|
217
|
+
const constrainedVal = constrainValue(resetVal, currentMin, currentMax, validatedStep);
|
|
218
|
+
setCurrentValue(constrainedVal);
|
|
219
|
+
thumbPosition.value = getThumbPosition(constrainedVal, trackWidth, currentMin, currentMax);
|
|
220
|
+
};
|
|
221
|
+
const getValue = () => {
|
|
222
|
+
return currentValue;
|
|
223
|
+
};
|
|
224
|
+
if (formValuesMap) {
|
|
225
|
+
if (!name) {
|
|
226
|
+
warn('If a form component is used, the name attribute is required.');
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
formValuesMap.set(name, { getValue, resetValue });
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
useEffect(() => {
|
|
233
|
+
return () => {
|
|
234
|
+
if (formValuesMap && name) {
|
|
235
|
+
formValuesMap.delete(name);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}, []);
|
|
239
|
+
// 样式定义
|
|
240
|
+
const blockSizeNum = Math.max(12, Math.min(28, blockSize));
|
|
241
|
+
const trackHeight = 4;
|
|
242
|
+
const containerStyle = extendObject({}, {
|
|
243
|
+
flexDirection: 'row',
|
|
244
|
+
alignItems: 'center',
|
|
245
|
+
minHeight: Math.max(blockSizeNum + 8, 40),
|
|
246
|
+
paddingHorizontal: 14 // 固定内边距,不受 block-size 影响
|
|
247
|
+
}, normalStyle, layoutStyle);
|
|
248
|
+
const trackStyle = {
|
|
249
|
+
flex: 1,
|
|
250
|
+
height: trackHeight,
|
|
251
|
+
backgroundColor,
|
|
252
|
+
borderRadius: trackHeight / 2,
|
|
253
|
+
position: 'relative'
|
|
254
|
+
};
|
|
255
|
+
// 动画进度条样式
|
|
256
|
+
const animatedProgressStyle = useAnimatedStyle(() => {
|
|
257
|
+
return {
|
|
258
|
+
height: '100%',
|
|
259
|
+
backgroundColor: activeColor,
|
|
260
|
+
borderRadius: trackHeight / 2,
|
|
261
|
+
width: Math.max(0, thumbPosition.value)
|
|
262
|
+
};
|
|
263
|
+
});
|
|
264
|
+
const thumbStyle = {
|
|
265
|
+
width: blockSizeNum,
|
|
266
|
+
height: blockSizeNum,
|
|
267
|
+
backgroundColor: blockColor,
|
|
268
|
+
borderRadius: blockSizeNum / 2,
|
|
269
|
+
shadowColor: '#000',
|
|
270
|
+
shadowOffset: { width: 0, height: 2 },
|
|
271
|
+
shadowOpacity: 0.2,
|
|
272
|
+
shadowRadius: 4,
|
|
273
|
+
elevation: 4
|
|
274
|
+
};
|
|
275
|
+
const innerProps = useInnerProps(extendObject({}, props, layoutProps, {
|
|
276
|
+
ref: nodeRef
|
|
277
|
+
}), [
|
|
278
|
+
'min',
|
|
279
|
+
'max',
|
|
280
|
+
'step',
|
|
281
|
+
'disabled',
|
|
282
|
+
'value',
|
|
283
|
+
'color',
|
|
284
|
+
'selected-color',
|
|
285
|
+
'activeColor',
|
|
286
|
+
'backgroundColor',
|
|
287
|
+
'block-size',
|
|
288
|
+
'block-color',
|
|
289
|
+
'bindchange',
|
|
290
|
+
'catchchange',
|
|
291
|
+
'bindchanging',
|
|
292
|
+
'catchchanging'
|
|
293
|
+
], { layoutRef });
|
|
294
|
+
const sliderContent = createElement(View, extendObject({}, innerProps, { style: containerStyle }),
|
|
295
|
+
// 轨道容器
|
|
296
|
+
createElement(View, {
|
|
297
|
+
style: trackStyle,
|
|
298
|
+
onLayout: onTrackLayout,
|
|
299
|
+
ref: trackRef
|
|
300
|
+
},
|
|
301
|
+
// 进度条 - 使用动画样式
|
|
302
|
+
createElement(Animated.View, {
|
|
303
|
+
style: animatedProgressStyle
|
|
304
|
+
}),
|
|
305
|
+
// 滑块容器
|
|
306
|
+
createElement(GestureDetector, {
|
|
307
|
+
gesture: panGesture
|
|
308
|
+
}, createElement(Animated.View, {
|
|
309
|
+
style: [animatedThumbStyle]
|
|
310
|
+
},
|
|
311
|
+
// 滑块
|
|
312
|
+
createElement(View, {
|
|
313
|
+
style: thumbStyle
|
|
314
|
+
})))));
|
|
315
|
+
if (hasPositionFixed) {
|
|
316
|
+
return createElement(Portal, null, sliderContent);
|
|
317
|
+
}
|
|
318
|
+
return sliderContent;
|
|
319
|
+
});
|
|
320
|
+
Slider.displayName = 'MpxSlider';
|
|
321
|
+
export default Slider;
|
|
@@ -1,16 +1,40 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ✔ selectable
|
|
3
3
|
* ✘ space
|
|
4
|
-
*
|
|
4
|
+
* ✔ decode
|
|
5
5
|
*/
|
|
6
6
|
import { Text } from 'react-native';
|
|
7
|
-
import { useRef, forwardRef, createElement } from 'react';
|
|
7
|
+
import { useRef, forwardRef, createElement, Children } from 'react';
|
|
8
8
|
import Portal from './mpx-portal';
|
|
9
9
|
import useInnerProps from './getInnerListeners';
|
|
10
10
|
import useNodesRef from './useNodesRef'; // 引入辅助函数
|
|
11
11
|
import { useTransformStyle, wrapChildren, extendObject } from './utils';
|
|
12
|
+
const decodeMap = {
|
|
13
|
+
'<': '<',
|
|
14
|
+
'>': '>',
|
|
15
|
+
'"': '"',
|
|
16
|
+
'&': '&',
|
|
17
|
+
''': '\'',
|
|
18
|
+
' ': ' '
|
|
19
|
+
};
|
|
20
|
+
const encodedRe = /&(?:lt|gt|quot|amp|#39|nbsp);/g;
|
|
21
|
+
function decode(value) {
|
|
22
|
+
if (value != null) {
|
|
23
|
+
return value.replace(encodedRe, function (match) {
|
|
24
|
+
return decodeMap[match];
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function getDecodedChildren(children) {
|
|
29
|
+
return Children.map(children, (child) => {
|
|
30
|
+
if (typeof child === 'string') {
|
|
31
|
+
return decode(child);
|
|
32
|
+
}
|
|
33
|
+
return child;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
12
36
|
const _Text = forwardRef((props, ref) => {
|
|
13
|
-
const { style = {}, allowFontScaling = false, selectable, 'enable-var': enableVar, 'external-var-context': externalVarContext, 'user-select': userSelect, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight } = props;
|
|
37
|
+
const { style = {}, allowFontScaling = false, selectable, 'enable-var': enableVar, 'external-var-context': externalVarContext, 'user-select': userSelect, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight, decode } = props;
|
|
14
38
|
const { normalStyle, hasVarDec, varContextRef, hasPositionFixed } = useTransformStyle(style, {
|
|
15
39
|
enableVar,
|
|
16
40
|
externalVarContext,
|
|
@@ -28,9 +52,13 @@ const _Text = forwardRef((props, ref) => {
|
|
|
28
52
|
selectable: !!selectable || !!userSelect,
|
|
29
53
|
allowFontScaling
|
|
30
54
|
}), [
|
|
31
|
-
'user-select'
|
|
55
|
+
'user-select',
|
|
56
|
+
'decode'
|
|
32
57
|
]);
|
|
33
|
-
|
|
58
|
+
const children = decode ? getDecodedChildren(props.children) : props.children;
|
|
59
|
+
let finalComponent = createElement(Text, innerProps, wrapChildren(extendObject({}, props, {
|
|
60
|
+
children
|
|
61
|
+
}), {
|
|
34
62
|
hasVarDec,
|
|
35
63
|
varContext: varContextRef.current
|
|
36
64
|
}));
|
|
@@ -203,16 +203,13 @@ function backgroundSize(imageProps, preImageInfo, imageSize, layoutInfo) {
|
|
|
203
203
|
else { // 数值类型 ImageStyle
|
|
204
204
|
// 数值类型设置为 stretch
|
|
205
205
|
imageProps.resizeMode = 'stretch';
|
|
206
|
-
if (type === 'linear') {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
height: dimensionHeight
|
|
214
|
-
};
|
|
215
|
-
}
|
|
206
|
+
if (type === 'linear' && (!layoutWidth || !layoutHeight)) {
|
|
207
|
+
// ios 上 linear 组件只要重新触发渲染,在渲染过程中外层容器 width 或者 height 被设置为 0,通过设置 % 的方式会渲染不出来,即使后面再更新为正常宽高也渲染不出来
|
|
208
|
+
// 所以 hack 手动先将 linear 宽高也设置为 0,后面再更新为正确的数值或 %。
|
|
209
|
+
dimensions = {
|
|
210
|
+
width: 0,
|
|
211
|
+
height: 0
|
|
212
|
+
};
|
|
216
213
|
}
|
|
217
214
|
else {
|
|
218
215
|
dimensions = {
|
|
@@ -401,7 +398,7 @@ function parseLinearGradient(text) {
|
|
|
401
398
|
});
|
|
402
399
|
}
|
|
403
400
|
function parseBgImage(text) {
|
|
404
|
-
if (!text)
|
|
401
|
+
if (!text || text === 'none')
|
|
405
402
|
return {};
|
|
406
403
|
const src = parseUrl(text);
|
|
407
404
|
if (src)
|
|
@@ -176,7 +176,7 @@ const _WebView = forwardRef((props, ref) => {
|
|
|
176
176
|
}
|
|
177
177
|
break;
|
|
178
178
|
case 'postMessage':
|
|
179
|
-
bindmessage && bindmessage(getCustomEvent('
|
|
179
|
+
bindmessage && bindmessage(getCustomEvent('message', {}, {
|
|
180
180
|
detail: {
|
|
181
181
|
data: params[0]?.data
|
|
182
182
|
}
|
|
@@ -169,24 +169,34 @@ function transformPercent(styleObj, percentKeyPaths, percentConfig) {
|
|
|
169
169
|
function resolveVar(input, varContext) {
|
|
170
170
|
const parsed = parseFunc(input, 'var');
|
|
171
171
|
const replaced = new ReplaceSource(input);
|
|
172
|
-
|
|
172
|
+
for (const { start, end, args } of parsed) {
|
|
173
173
|
const varName = args[0];
|
|
174
|
-
const fallback = args[1]
|
|
174
|
+
const fallback = args[1];
|
|
175
175
|
let varValue = hasOwn(varContext, varName) ? varContext[varName] : fallback;
|
|
176
|
+
if (varValue === undefined)
|
|
177
|
+
return;
|
|
176
178
|
if (varUseRegExp.test(varValue)) {
|
|
177
|
-
varValue =
|
|
179
|
+
varValue = resolveVar(varValue, varContext);
|
|
180
|
+
if (varValue === undefined)
|
|
181
|
+
return;
|
|
178
182
|
}
|
|
179
183
|
else {
|
|
180
|
-
varValue =
|
|
184
|
+
varValue = global.__formatValue(varValue);
|
|
181
185
|
}
|
|
182
186
|
replaced.replace(start, end - 1, varValue);
|
|
183
|
-
}
|
|
187
|
+
}
|
|
184
188
|
return global.__formatValue(replaced.source());
|
|
185
189
|
}
|
|
186
190
|
function transformVar(styleObj, varKeyPaths, varContext, visitOther) {
|
|
187
191
|
varKeyPaths.forEach((varKeyPath) => {
|
|
188
192
|
setStyle(styleObj, varKeyPath, ({ target, key, value }) => {
|
|
189
|
-
|
|
193
|
+
const resolved = resolveVar(value, varContext);
|
|
194
|
+
if (resolved === undefined) {
|
|
195
|
+
delete target[key];
|
|
196
|
+
error(`Can not resolve css var at ${varKeyPath.join('.')}:${value}.`);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
target[key] = resolved;
|
|
190
200
|
visitOther({ target, key, value: target[key], keyPath: varKeyPath });
|
|
191
201
|
});
|
|
192
202
|
});
|