@mpxjs/webpack-plugin 2.10.17-beta.2 → 2.10.17-beta.4

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 (98) hide show
  1. package/lib/config.js +60 -0
  2. package/lib/file-loader.js +4 -1
  3. package/lib/global.d.ts +16 -0
  4. package/lib/index.js +22 -2
  5. package/lib/json-compiler/index.js +13 -4
  6. package/lib/platform/json/wx/index.js +6 -0
  7. package/lib/platform/style/wx/index.js +57 -33
  8. package/lib/platform/template/wx/component-config/ad.js +5 -0
  9. package/lib/platform/template/wx/component-config/button.js +9 -2
  10. package/lib/platform/template/wx/component-config/camera.js +25 -3
  11. package/lib/platform/template/wx/component-config/canvas.js +8 -1
  12. package/lib/platform/template/wx/component-config/cover-image.js +7 -2
  13. package/lib/platform/template/wx/component-config/cover-view.js +3 -1
  14. package/lib/platform/template/wx/component-config/form.js +27 -2
  15. package/lib/platform/template/wx/component-config/image.js +5 -0
  16. package/lib/platform/template/wx/component-config/input.js +10 -0
  17. package/lib/platform/template/wx/component-config/label.js +10 -2
  18. package/lib/platform/template/wx/component-config/map.js +11 -0
  19. package/lib/platform/template/wx/component-config/movable-area.js +4 -1
  20. package/lib/platform/template/wx/component-config/movable-view.js +17 -2
  21. package/lib/platform/template/wx/component-config/navigator.js +26 -0
  22. package/lib/platform/template/wx/component-config/picker-view.js +12 -0
  23. package/lib/platform/template/wx/component-config/picker.js +3 -1
  24. package/lib/platform/template/wx/component-config/progress.js +11 -1
  25. package/lib/platform/template/wx/component-config/rich-text.js +5 -0
  26. package/lib/platform/template/wx/component-config/scroll-view.js +12 -1
  27. package/lib/platform/template/wx/component-config/slider.js +8 -0
  28. package/lib/platform/template/wx/component-config/swiper-item.js +5 -2
  29. package/lib/platform/template/wx/component-config/swiper.js +10 -0
  30. package/lib/platform/template/wx/component-config/text.js +5 -0
  31. package/lib/platform/template/wx/component-config/textarea.js +19 -2
  32. package/lib/platform/template/wx/component-config/unsupported.js +10 -1
  33. package/lib/platform/template/wx/component-config/video.js +10 -0
  34. package/lib/platform/template/wx/index.js +21 -1
  35. package/lib/react/LoadAsyncChunkModule.js +1 -1
  36. package/lib/react/processStyles.js +21 -9
  37. package/lib/react/style-helper.js +76 -13
  38. package/lib/resolver/AddModePlugin.js +23 -8
  39. package/lib/runtime/components/react/animationHooks/index.ts +75 -0
  40. package/lib/runtime/components/react/animationHooks/useAnimationAPIHooks.ts +198 -0
  41. package/lib/runtime/components/react/animationHooks/useTransitionHooks.ts +297 -0
  42. package/lib/runtime/components/react/animationHooks/utils.ts +196 -0
  43. package/lib/runtime/components/react/context.ts +7 -1
  44. package/lib/runtime/components/react/dist/animationHooks/index.d.ts +16 -0
  45. package/lib/runtime/components/react/dist/animationHooks/index.d.ts.map +1 -0
  46. package/lib/runtime/components/react/dist/animationHooks/index.js +67 -0
  47. package/lib/runtime/components/react/dist/animationHooks/useAnimationAPIHooks.d.ts +4 -0
  48. package/lib/runtime/components/react/dist/animationHooks/useAnimationAPIHooks.d.ts.map +1 -0
  49. package/lib/runtime/components/react/dist/animationHooks/useAnimationAPIHooks.js +182 -0
  50. package/lib/runtime/components/react/dist/animationHooks/useTransitionHooks.d.ts +4 -0
  51. package/lib/runtime/components/react/dist/animationHooks/useTransitionHooks.d.ts.map +1 -0
  52. package/lib/runtime/components/react/dist/animationHooks/useTransitionHooks.js +274 -0
  53. package/lib/runtime/components/react/dist/animationHooks/utils.d.ts +110 -0
  54. package/lib/runtime/components/react/dist/animationHooks/utils.d.ts.map +1 -0
  55. package/lib/runtime/components/react/dist/animationHooks/utils.js +150 -0
  56. package/lib/runtime/components/react/dist/context.d.ts +6 -1
  57. package/lib/runtime/components/react/dist/context.d.ts.map +1 -1
  58. package/lib/runtime/components/react/dist/mpx-camera.d.ts +32 -0
  59. package/lib/runtime/components/react/dist/mpx-camera.d.ts.map +1 -0
  60. package/lib/runtime/components/react/dist/mpx-camera.jsx +236 -0
  61. package/lib/runtime/components/react/dist/mpx-input.d.ts +2 -0
  62. package/lib/runtime/components/react/dist/mpx-input.d.ts.map +1 -1
  63. package/lib/runtime/components/react/dist/mpx-input.jsx +21 -10
  64. package/lib/runtime/components/react/dist/mpx-keyboard-avoiding-view.d.ts.map +1 -1
  65. package/lib/runtime/components/react/dist/mpx-keyboard-avoiding-view.jsx +3 -0
  66. package/lib/runtime/components/react/dist/mpx-portal/portal-manager.jsx +2 -2
  67. package/lib/runtime/components/react/dist/mpx-swiper.d.ts +10 -0
  68. package/lib/runtime/components/react/dist/mpx-swiper.d.ts.map +1 -1
  69. package/lib/runtime/components/react/dist/mpx-swiper.jsx +28 -16
  70. package/lib/runtime/components/react/dist/mpx-view.d.ts +3 -2
  71. package/lib/runtime/components/react/dist/mpx-view.d.ts.map +1 -1
  72. package/lib/runtime/components/react/dist/mpx-view.jsx +2 -2
  73. package/lib/runtime/components/react/dist/mpx-web-view.jsx +1 -1
  74. package/lib/runtime/components/react/dist/utils.d.ts +1 -0
  75. package/lib/runtime/components/react/dist/utils.d.ts.map +1 -1
  76. package/lib/runtime/components/react/dist/utils.jsx +34 -13
  77. package/lib/runtime/components/react/mpx-camera.tsx +327 -0
  78. package/lib/runtime/components/react/mpx-input.tsx +26 -10
  79. package/lib/runtime/components/react/mpx-keyboard-avoiding-view.tsx +3 -0
  80. package/lib/runtime/components/react/mpx-portal/portal-manager.tsx +2 -2
  81. package/lib/runtime/components/react/mpx-swiper.tsx +43 -15
  82. package/lib/runtime/components/react/mpx-view.tsx +4 -5
  83. package/lib/runtime/components/react/mpx-web-view.tsx +1 -1
  84. package/lib/runtime/components/react/types/global.d.ts +1 -0
  85. package/lib/runtime/components/react/utils.tsx +34 -16
  86. package/lib/runtime/optionProcessor.js +5 -0
  87. package/lib/runtime/optionProcessorReact.js +7 -0
  88. package/lib/runtime/stringify.wxs +2 -2
  89. package/lib/style-compiler/strip-conditional-loader/rebaseUrl.js +225 -0
  90. package/lib/style-compiler/strip-conditional-loader.js +55 -180
  91. package/lib/template-compiler/compiler.js +1 -3
  92. package/lib/utils/dom-tag-config.js +1 -1
  93. package/lib/utils/string.js +25 -1
  94. package/package.json +2 -1
  95. package/lib/runtime/components/react/dist/useAnimationHooks.d.ts +0 -33
  96. package/lib/runtime/components/react/dist/useAnimationHooks.d.ts.map +0 -1
  97. package/lib/runtime/components/react/dist/useAnimationHooks.js +0 -289
  98. package/lib/runtime/components/react/useAnimationHooks.ts +0 -320
@@ -0,0 +1,182 @@
1
+ import { useMemo, useRef, useEffect } from 'react';
2
+ import { error } from '@mpxjs/utils';
3
+ import { Easing, withSequence, makeMutable, runOnJS, useSharedValue, useAnimatedStyle, cancelAnimation } from 'react-native-reanimated';
4
+ import { easingKey, animationAPIInitialValue, percentExp, isTransform, getInitialVal, getAnimation, getTransformObj, formatAnimatedKeys } from './utils';
5
+ import { useRunOnJSCallback } from '../utils';
6
+ export default function useAnimationAPIHooks(props) {
7
+ // console.log(`useAnimationAPIHooks, props=`, props)
8
+ const { style: originalStyle = {}, animation, transitionend } = props;
9
+ // style变更标识(首次render不执行)
10
+ const animationDeps = useRef(-1);
11
+ // animation API 使用 animation.id 为依赖
12
+ if (animation?.id) {
13
+ animationDeps.current = animation.id;
14
+ }
15
+ // 有动画样式的 style key(useAnimatedStyle使用)
16
+ const animatedStyleKeys = useSharedValue([]);
17
+ // 记录需要执行动画的 propName
18
+ const animatedKeys = useRef([]);
19
+ // 记录上次style map
20
+ const lastStyleRef = useRef({});
21
+ // ** 全量 style prop sharedValue
22
+ const shareValMap = useMemo(() => {
23
+ return Object.keys(animationAPIInitialValue).reduce((valMap, key) => {
24
+ const defaultVal = getInitialVal(originalStyle, key);
25
+ valMap[key] = makeMutable(defaultVal);
26
+ return valMap;
27
+ }, {});
28
+ }, []);
29
+ const runOnJSCallbackRef = useRef({});
30
+ if (transitionend) {
31
+ runOnJSCallbackRef.current = {
32
+ transitionend
33
+ };
34
+ }
35
+ const runOnJSCallback = useRunOnJSCallback(runOnJSCallbackRef);
36
+ // 设置 lastShareValRef & shareValMap
37
+ function updateStyleVal() {
38
+ Object.keys(shareValMap).forEach(key => {
39
+ let value = originalStyle[key];
40
+ if (isTransform(key)) {
41
+ value = originalStyle.transform;
42
+ Object.entries(getTransformObj(value)).forEach(([key, value]) => {
43
+ if (value !== lastStyleRef.current[key]) {
44
+ lastStyleRef.current[key] = value;
45
+ shareValMap[key].value = value;
46
+ }
47
+ });
48
+ }
49
+ else {
50
+ if (value !== lastStyleRef.current[key]) {
51
+ lastStyleRef.current[key] = value;
52
+ shareValMap[key].value = value;
53
+ }
54
+ }
55
+ });
56
+ }
57
+ // 根据 animation action 创建&驱动动画
58
+ function createAnimation(animatedKeys = []) {
59
+ const actions = animation?.actions || [];
60
+ const sequence = {};
61
+ const lastValueMap = {};
62
+ actions.forEach(({ animatedOption, rules, transform }, index) => {
63
+ const { delay, duration, timingFunction, transformOrigin } = animatedOption;
64
+ const easing = timingFunction ? easingKey[timingFunction] : Easing.inOut(Easing.quad);
65
+ let needSetCallback = true;
66
+ const callback = (finished, current) => {
67
+ 'worklet';
68
+ // 动画结束后设置下一次transformOrigin
69
+ if (finished) {
70
+ if (index < actions.length - 1) {
71
+ const transformOrigin = actions[index + 1].animatedOption?.transformOrigin;
72
+ transformOrigin && (shareValMap.transformOrigin.value = transformOrigin);
73
+ }
74
+ transitionend && runOnJS(runOnJSCallback)('transitionend', finished, current, duration);
75
+ }
76
+ };
77
+ if (index === 0 && transformOrigin) {
78
+ // 设置当次中心
79
+ shareValMap.transformOrigin.value = transformOrigin;
80
+ }
81
+ // 添加每个key的多次step动画
82
+ animatedKeys.forEach(key => {
83
+ const shareVal = shareValMap[key].value;
84
+ const ruleV = isTransform(key) ? transform.get(key) : rules.get(key);
85
+ // color 设置为 1
86
+ // key不存在,第一轮取shareValMap[key]value,非第一轮取上一轮的
87
+ let toVal = ruleV !== undefined
88
+ ? ruleV
89
+ : index > 0
90
+ ? lastValueMap[key]
91
+ : shareVal;
92
+ if (percentExp.test(`${toVal}`) && !percentExp.test(shareVal) && !isNaN(+shareVal)) {
93
+ // 获取到的toVal为百分比格式化shareValMap为百分比
94
+ shareValMap[key].value = `${shareVal * 100}%`;
95
+ }
96
+ else if (percentExp.test(shareVal) && !percentExp.test(toVal) && !isNaN(+toVal)) {
97
+ // 初始值为百分比则格式化toVal为百分比
98
+ toVal = `${toVal * 100}%`;
99
+ }
100
+ else if (typeof toVal !== typeof shareVal) {
101
+ // 动画起始值和终态值类型不一致报错提示一下
102
+ error(`[Mpx runtime error]: Value types of property ${key} must be consistent during the animation`);
103
+ }
104
+ // Todo 对齐wx
105
+ const animation = getAnimation({ key, value: toVal }, { delay, duration, easing }, needSetCallback ? callback : undefined);
106
+ needSetCallback = false;
107
+ if (!sequence[key]) {
108
+ sequence[key] = [animation];
109
+ }
110
+ else {
111
+ sequence[key].push(animation);
112
+ }
113
+ // 更新一下 lastValueMap
114
+ lastValueMap[key] = toVal;
115
+ });
116
+ // 赋值驱动动画
117
+ animatedKeys.forEach((key) => {
118
+ const animations = sequence[key];
119
+ shareValMap[key].value = animations.length > 1 ? withSequence(...animations) : animations[0];
120
+ });
121
+ });
122
+ }
123
+ // 循环 animation actions 获取所有有动画的 style prop name
124
+ function getAnimatedStyleKeys() {
125
+ return (animation?.actions || []).reduce((keyMap, action) => {
126
+ const { rules, transform } = action;
127
+ const ruleArr = [...rules.keys(), ...transform.keys()];
128
+ ruleArr.forEach(key => {
129
+ keyMap.push(key);
130
+ });
131
+ // console.log('getAnimatedStyleKeys keyMap=', keyMap)
132
+ return keyMap;
133
+ }, []);
134
+ }
135
+ // 获取动画样式&驱动动画
136
+ function startAnimation() {
137
+ // 更新动画样式 key map
138
+ animatedKeys.current = getAnimatedStyleKeys();
139
+ animatedStyleKeys.value = formatAnimatedKeys(['transformOrigin', ...animatedKeys.current]);
140
+ // 驱动动画
141
+ createAnimation(animatedKeys.current);
142
+ }
143
+ // ** style 更新
144
+ useEffect(() => {
145
+ // animation api style 更新同步更新 shareVal(默认)
146
+ updateStyleVal();
147
+ }, [originalStyle]);
148
+ // ** 获取动画样式prop & 驱动动画
149
+ useEffect(() => {
150
+ if (animationDeps.current <= 0)
151
+ return;
152
+ startAnimation();
153
+ }, [animationDeps.current]);
154
+ // ** 清空动画
155
+ useEffect(() => {
156
+ return () => {
157
+ Object.values(shareValMap).forEach((value) => {
158
+ cancelAnimation(value);
159
+ });
160
+ };
161
+ }, []);
162
+ // ** 生成动画样式
163
+ return useAnimatedStyle(() => {
164
+ // console.info(`useAnimatedStyle styles=`, originalStyle)
165
+ return animatedStyleKeys.value.reduce((styles, key) => {
166
+ if (Array.isArray(key)) {
167
+ const transformStyle = getTransformObj(originalStyle.transform || []);
168
+ key.forEach((transformKey) => {
169
+ transformStyle[transformKey] = shareValMap[transformKey].value;
170
+ });
171
+ styles.transform = Object.entries(transformStyle).map(([key, value]) => {
172
+ return { [key]: value };
173
+ });
174
+ }
175
+ else {
176
+ styles[key] = shareValMap[key].value;
177
+ }
178
+ // console.log('animationStyle', styles)
179
+ return styles;
180
+ }, {});
181
+ });
182
+ }
@@ -0,0 +1,4 @@
1
+ import type { ExtendedViewStyle } from '../types/common';
2
+ import type { AnimationHooksPropsType } from './utils';
3
+ export default function useTransitionHooks<T, P>(props: AnimationHooksPropsType): ExtendedViewStyle;
4
+ //# sourceMappingURL=useTransitionHooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTransitionHooks.d.ts","sourceRoot":"","sources":["../../animationHooks/useTransitionHooks.ts"],"names":[],"mappings":"AA0BA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AACxD,OAAO,KAAK,EAAE,uBAAuB,EAAiC,MAAM,SAAS,CAAA;AAoIrF,MAAM,CAAC,OAAO,UAAU,kBAAkB,CAAC,CAAC,EAAE,CAAC,EAAG,KAAK,EAAE,uBAAuB,qBAyI/E"}
@@ -0,0 +1,274 @@
1
+ import { hasOwn, dash2hump, error } from '@mpxjs/utils';
2
+ import { useMemo, useRef, useEffect } from 'react';
3
+ import { Easing, makeMutable, runOnJS, useSharedValue, useAnimatedStyle, cancelAnimation } from 'react-native-reanimated';
4
+ import { easingKey, transitionSupportedProperty, transformInitial, cubicBezierExp, secondRegExp, percentExp, getTransformObj, getUnit, getInitialVal, getAnimation, isTransform } from './utils';
5
+ import { parseValues, useRunOnJSCallback } from '../utils';
6
+ const propName = {
7
+ transition: '',
8
+ transitionDuration: 'duration',
9
+ transitionProperty: 'property',
10
+ transitionTimingFunction: 'easing',
11
+ transitionDelay: 'delay'
12
+ };
13
+ const behaviorExp = /^(allow-discrete|normal)$/;
14
+ const defaultValueExp = /^(inherit|initial|revert|revert-layer|unset)$/;
15
+ const timingFunctionExp = /^(step-start|step-end|steps)/;
16
+ // cubic-bezier 参数解析
17
+ function getBezierParams(str) {
18
+ // ease 0.25, 0.1, 0.25, 1.0
19
+ return str.match(cubicBezierExp)?.[1]?.split(',').map(item => +item);
20
+ }
21
+ // 解析 transition-prop
22
+ function parseTransitionSingleProp(vals, property) {
23
+ let setDuration = false;
24
+ property = propName[property];
25
+ return vals.map(val => {
26
+ // transition-property all
27
+ if (val === 'all') {
28
+ error('[Mpx runtime error]: the value of transition-property is not supported \'all\'');
29
+ return undefined;
30
+ }
31
+ // behavior
32
+ if (behaviorExp.test(val)) {
33
+ error('[Mpx runtime error]: transition-behavior is not supported');
34
+ return undefined;
35
+ }
36
+ // global values
37
+ if (defaultValueExp.test(val)) {
38
+ error('[Mpx runtime error]: global values is not supported');
39
+ return undefined;
40
+ }
41
+ if (timingFunctionExp.test(val)) {
42
+ error('[Mpx runtime error]: the timingFunction in step-start,step-end,steps() is not supported');
43
+ return undefined;
44
+ }
45
+ // timingFunction
46
+ if (Object.keys(easingKey).includes(val) || cubicBezierExp.test(val)) {
47
+ const bezierParams = getBezierParams(val);
48
+ return {
49
+ easing: bezierParams?.length ? Easing.bezier(bezierParams[0], bezierParams[1], bezierParams[2], bezierParams[3]) : easingKey[val] || Easing.inOut(Easing.ease)
50
+ };
51
+ }
52
+ // duration & delay
53
+ if (secondRegExp.test(val)) {
54
+ const newProperty = property || (!setDuration ? 'duration' : 'delay');
55
+ setDuration = true;
56
+ // console.log('parseTransitionSingleProp val=', val, property, setDuration)
57
+ return {
58
+ [newProperty]: getUnit(val)
59
+ };
60
+ }
61
+ // property
62
+ return {
63
+ property: dash2hump(val)
64
+ };
65
+ }).filter(item => item !== undefined);
66
+ }
67
+ // transition 解析
68
+ function parseTransitionStyle(originalStyle) {
69
+ let transitionData = [];
70
+ Object.entries(originalStyle).filter(arr => arr[0].includes('transition')).forEach(([prop, value]) => {
71
+ if (prop === 'transition') {
72
+ const vals = parseValues(value, ',').map(item => {
73
+ return parseTransitionSingleProp(parseValues(item), prop).reduce((map, subItem) => {
74
+ return Object.assign(map, subItem);
75
+ }, {});
76
+ });
77
+ // console.log(`parseTransitionStyle ${prop}=${value} formatVal=`, vals)
78
+ if (transitionData.length) {
79
+ transitionData = (vals.length > transitionData.length ? vals : transitionData).map((transitionItem, i) => {
80
+ const valItem = vals[i] || {};
81
+ const current = transitionData[i] || {};
82
+ // console.log('parseTransitionStyle current=', current)
83
+ // console.log('parseTransitionStyle valItem=', valItem)
84
+ // console.log('parseTransitionStyle mergeObj=', Object.assign({}, current, valItem))
85
+ return Object.assign({}, current, valItem);
86
+ });
87
+ // console.log(`parseTransitionStyle ${prop}=${value}, transitionData=`, transitionData)
88
+ }
89
+ else {
90
+ transitionData = vals;
91
+ }
92
+ }
93
+ else {
94
+ const vals = parseTransitionSingleProp(parseValues(value, ','), prop);
95
+ // console.log(`parseTransitionStyle ${prop}=${value} formatVal=`, vals)
96
+ // formatVal [{"property": "transform"}, {"property": "marginLeft"}]
97
+ if (transitionData.length) {
98
+ transitionData = (vals.length > transitionData.length ? vals : transitionData).map((transitionItem, i) => {
99
+ const valItem = vals[i] || vals[vals.length - 1];
100
+ const current = transitionData[i] || transitionData[transitionData.length - 1];
101
+ // console.log('parseTransitionStyle current=', current)
102
+ // console.log('parseTransitionStyle valItem=', valItem)
103
+ // console.log('parseTransitionStyle mergeObj=', Object.assign({}, current, valItem))
104
+ return Object.assign({}, current, valItem);
105
+ });
106
+ // console.log(`parseTransitionStyle ${prop}=${value}, transitionData=`, transitionData)
107
+ }
108
+ else {
109
+ transitionData = vals;
110
+ }
111
+ // transitionData.push(...vals)
112
+ }
113
+ });
114
+ // console.log(`parseTransitionStyle transitionData=`, transitionData)
115
+ // 从style 中解析的动画数据,结构如下:
116
+ // transitionMap= {"marginLeft": {"delay": 0, "duration": 3000, "easing": []}, "transform": {"delay": 0, "duration": 3000, "easing": []}}
117
+ const transitionMap = transitionData.reduce((acc, cur) => {
118
+ // hasOwn(transitionSupportedProperty, dash2hump(val)) || val === Transform
119
+ const { property = '', duration = 0, delay = 0, easing = Easing.inOut(Easing.ease) } = cur;
120
+ if ((hasOwn(transitionSupportedProperty, dash2hump(property)) || property === 'transform') && duration > 0) {
121
+ acc[property] = {
122
+ duration,
123
+ delay,
124
+ easing
125
+ };
126
+ }
127
+ return acc;
128
+ }, {});
129
+ // console.log(`parseTransitionStyle transitionMap=`, transitionMap)
130
+ return transitionMap;
131
+ }
132
+ export default function useTransitionHooks(props) {
133
+ // console.log(`useTransitionHooks, props=`, props)
134
+ const { style: originalStyle = {}, transitionend } = props;
135
+ // style变更标识(首次render不执行),初始值为0,首次渲染后为1
136
+ const animationDeps = useRef(0);
137
+ // 记录上次style map
138
+ // const lastStyleRef = useRef({} as {[propName: keyof ExtendedViewStyle]: number|string})
139
+ // ** 从 style 中获取动画数据
140
+ const transitionMap = useMemo(() => {
141
+ return parseTransitionStyle(originalStyle);
142
+ }, []);
143
+ // ** style prop sharedValue interpolateOutput: SharedValue<InterpolateOutput>
144
+ const { shareValMap, animatedKeys, animatedKeysShareVal } = useMemo(() => {
145
+ // 记录需要执行动画的 propName
146
+ const animatedKeys = [];
147
+ const animatedKeysShareVal = [];
148
+ const transforms = [];
149
+ const shareValMap = Object.keys(transitionMap).reduce((valMap, property) => {
150
+ // const { property } = transition || {}
151
+ if (property === 'transform') {
152
+ Object.keys(originalStyle.transform ? getTransformObj(originalStyle.transform) : transformInitial).forEach((key) => {
153
+ const defaultVal = getInitialVal(originalStyle, key);
154
+ // console.log(`shareValMap property=${key} defaultVal=${defaultVal}`)
155
+ valMap[key] = makeMutable(defaultVal);
156
+ animatedKeys.push(key);
157
+ transforms.push(key);
158
+ });
159
+ }
160
+ else if (hasOwn(transitionSupportedProperty, property)) {
161
+ const defaultVal = getInitialVal(originalStyle, property);
162
+ // console.log(`shareValMap property=${property} defaultVal=${defaultVal}`)
163
+ valMap[property] = makeMutable(defaultVal);
164
+ animatedKeys.push(property);
165
+ animatedKeysShareVal.push(property);
166
+ }
167
+ // console.log('shareValMap = ', valMap)
168
+ return valMap;
169
+ }, {});
170
+ if (transforms.length)
171
+ animatedKeysShareVal.push(transforms);
172
+ return {
173
+ shareValMap,
174
+ animatedKeys,
175
+ animatedKeysShareVal
176
+ };
177
+ }, []);
178
+ // 有动画样式的 style key(useAnimatedStyle使用)
179
+ const animatedStyleKeys = useSharedValue(animatedKeysShareVal);
180
+ const runOnJSCallbackRef = useRef({});
181
+ const runOnJSCallback = useRunOnJSCallback(runOnJSCallbackRef);
182
+ // 根据 animation action 创建&驱动动画
183
+ function createAnimation() {
184
+ let transformTransitionendDone = false;
185
+ animatedKeys.forEach(key => {
186
+ // console.log(`createAnimation key=${key} originalStyle=`, originalStyle)
187
+ const isTransformKey = isTransform(key);
188
+ let ruleV = originalStyle[key];
189
+ if (isTransformKey) {
190
+ const transform = getTransformObj(originalStyle.transform);
191
+ ruleV = transform[key];
192
+ }
193
+ let toVal = ruleV !== undefined
194
+ ? ruleV
195
+ : transitionSupportedProperty[key];
196
+ const shareVal = shareValMap[key].value;
197
+ if (percentExp.test(`${toVal}`) && !percentExp.test(shareVal) && !isNaN(+shareVal)) {
198
+ // 获取到的toVal为百分比格式化shareValMap为百分比
199
+ shareValMap[key].value = `${shareVal * 100}%`;
200
+ }
201
+ else if (percentExp.test(shareVal) && !percentExp.test(toVal) && !isNaN(+toVal)) {
202
+ // 初始值为百分比则格式化toVal为百分比
203
+ toVal = `${toVal * 100}%`;
204
+ }
205
+ else if (typeof toVal !== typeof shareVal) {
206
+ // 动画起始值和终态值类型不一致报错提示一下
207
+ error(`[Mpx runtime error]: Value types of property ${key} must be consistent during the animation`);
208
+ }
209
+ // console.log(`key=${key} oldVal=${shareValMap[key].value} newVal=${toVal}`)
210
+ const { delay = 0, duration, easing } = transitionMap[isTransformKey ? 'transform' : key];
211
+ // console.log('animationOptions=', { delay, duration, easing })
212
+ let callback;
213
+ if (transitionend && (!isTransformKey || !transformTransitionendDone)) {
214
+ runOnJSCallbackRef.current = {
215
+ animationCallback: (duration, finished, current) => {
216
+ transitionend(finished, current, duration);
217
+ }
218
+ };
219
+ callback = (finished, current) => {
220
+ 'worklet';
221
+ // 动画结束后设置下一次transformOrigin
222
+ if (finished) {
223
+ runOnJS(runOnJSCallback)('animationCallback', duration, finished, current);
224
+ }
225
+ };
226
+ }
227
+ const animation = getAnimation({ key, value: toVal }, { delay, duration, easing }, callback);
228
+ // Todo transform 有多个属性时也仅执行一次 transitionend(对齐wx)
229
+ if (isTransformKey) {
230
+ transformTransitionendDone = true;
231
+ }
232
+ shareValMap[key].value = animation;
233
+ // console.log(`useTransitionHooks, ${key}=`, animation)
234
+ });
235
+ }
236
+ // ** style 更新
237
+ useEffect(() => {
238
+ // console.log('useEffect originalStyle animationDeps=', animationDeps.current, originalStyle)
239
+ // 首次不执行
240
+ if (!animationDeps.current) {
241
+ animationDeps.current = 1;
242
+ return;
243
+ }
244
+ createAnimation();
245
+ }, [originalStyle]);
246
+ // ** 清空动画
247
+ useEffect(() => {
248
+ return () => {
249
+ Object.values(shareValMap).forEach((value) => {
250
+ cancelAnimation(value);
251
+ });
252
+ };
253
+ }, []);
254
+ // ** 生成动画样式
255
+ return useAnimatedStyle(() => {
256
+ // console.info(`useAnimatedStyle styles=`, originalStyle)
257
+ return animatedStyleKeys.value.reduce((styles, key) => {
258
+ if (Array.isArray(key)) {
259
+ const transformStyle = getTransformObj(originalStyle.transform || []);
260
+ key.forEach((transformKey) => {
261
+ transformStyle[transformKey] = shareValMap[transformKey].value;
262
+ });
263
+ styles.transform = Object.entries(transformStyle).map(([key, value]) => {
264
+ return { [key]: value };
265
+ });
266
+ }
267
+ else {
268
+ styles[key] = shareValMap[key].value;
269
+ }
270
+ // console.log('animationStyle', styles)
271
+ return styles;
272
+ }, {});
273
+ });
274
+ }
@@ -0,0 +1,110 @@
1
+ import type { AnimatableValue, WithTimingConfig, AnimationCallback, EasingFunction } from 'react-native-reanimated';
2
+ import type { ExtendedViewStyle } from '../types/common';
3
+ import type { _ViewProps } from '../mpx-view';
4
+ export type TimingFunction = 'linear' | 'ease' | 'ease-in' | 'ease-in-out' | 'ease-out';
5
+ export type AnimatedOption = {
6
+ duration: number;
7
+ delay?: number;
8
+ useNativeDriver?: boolean;
9
+ timingFunction?: TimingFunction;
10
+ transformOrigin?: string;
11
+ };
12
+ export type ExtendWithTimingConfig = WithTimingConfig & {
13
+ delay?: number;
14
+ };
15
+ export type AnimationStepItem = {
16
+ animatedOption: AnimatedOption;
17
+ rules: Map<string, number | string>;
18
+ transform: Map<string, number>;
19
+ };
20
+ export type AnimationProp = {
21
+ id: number;
22
+ actions: AnimationStepItem[];
23
+ };
24
+ export type CustomAnimationCallback = (finished?: boolean, current?: AnimatableValue, duration?: number) => void;
25
+ export type TransitionMap = {
26
+ [propName: string]: {
27
+ duration: number;
28
+ delay?: number;
29
+ easing: EasingFunction;
30
+ };
31
+ };
32
+ export type InterpolateOutput = {
33
+ [propName: string]: string[];
34
+ };
35
+ export type AnimationHooksPropsType = _ViewProps & {
36
+ transitionend?: CustomAnimationCallback;
37
+ };
38
+ export declare const secondRegExp: RegExp;
39
+ export declare const cubicBezierExp: RegExp;
40
+ export declare const percentExp: RegExp;
41
+ export declare const easingKey: {
42
+ linear: (t: number) => number;
43
+ ease: EasingFunction;
44
+ 'ease-in': EasingFunction;
45
+ 'ease-in-out': EasingFunction;
46
+ 'ease-out': EasingFunction;
47
+ };
48
+ export declare const transformInitial: ExtendedViewStyle;
49
+ export declare const animationAPIInitialValue: ExtendedViewStyle;
50
+ export declare const transitionSupportedProperty: {
51
+ color: string;
52
+ borderColor: string;
53
+ borderBottomColor: string;
54
+ borderLeftColor: string;
55
+ borderRightColor: string;
56
+ borderTopColor: string;
57
+ borderTopLeftRadius: number;
58
+ borderTopRightRadius: number;
59
+ borderBottomLeftRadius: number;
60
+ borderBottomRightRadius: number;
61
+ borderRadius: number;
62
+ borderBottomWidth: number;
63
+ borderLeftWidth: number;
64
+ borderRightWidth: number;
65
+ borderTopWidth: number;
66
+ borderWidth: number;
67
+ margin: number;
68
+ marginBottom: number;
69
+ marginLeft: number;
70
+ marginRight: number;
71
+ marginTop: number;
72
+ marginHorizontal: number;
73
+ marginVertical: number;
74
+ maxHeight: number;
75
+ maxWidth: number;
76
+ minHeight: number;
77
+ minWidth: number;
78
+ padding: number;
79
+ paddingBottom: number;
80
+ paddingLeft: number;
81
+ paddingRight: number;
82
+ paddingTop: number;
83
+ paddingHorizontal: number;
84
+ paddingVertical: number;
85
+ fontSize: number;
86
+ letterSpacing: number;
87
+ } & import("react-native/types").ViewStyle & {
88
+ [key: string]: any;
89
+ backgroundImage?: string | undefined;
90
+ backgroundSize?: ("auto" | "contain" | "cover" | import("../types/common").NumberVal)[] | undefined;
91
+ borderRadius?: string | number | undefined;
92
+ backgroundPosition?: import("../types/common").backgroundPositionList | undefined;
93
+ transform?: {
94
+ [key: string]: string | number;
95
+ }[] | undefined;
96
+ };
97
+ export declare const isTransform: (key: string) => boolean;
98
+ export declare function getTransformObj(transforms: {
99
+ [propName: string]: string | number;
100
+ }[]): {
101
+ [propName: string]: string | number;
102
+ };
103
+ export declare function getInitialVal(style: ExtendedViewStyle, key: string): any;
104
+ export declare function formatAnimatedKeys(keys: string[]): (string | string[])[];
105
+ export declare function getUnit(duration: string): number;
106
+ export declare function getAnimation({ key, value }: {
107
+ key: string;
108
+ value: string | number;
109
+ }, { delay, duration, easing }: ExtendWithTimingConfig, callback?: AnimationCallback): string | number;
110
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../animationHooks/utils.ts"],"names":[],"mappings":"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"}