@mpxjs/webpack-plugin 2.9.67 → 2.9.70-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/README.md +1 -1
  2. package/lib/config.js +14 -0
  3. package/lib/dependencies/AddEntryDependency.js +24 -0
  4. package/lib/dependencies/ResolveDependency.js +5 -0
  5. package/lib/index.js +51 -15
  6. package/lib/json-compiler/helper.js +3 -3
  7. package/lib/loader.js +53 -0
  8. package/lib/platform/template/wx/component-config/button.js +14 -2
  9. package/lib/platform/template/wx/component-config/canvas.js +8 -0
  10. package/lib/platform/template/wx/component-config/image.js +4 -0
  11. package/lib/platform/template/wx/component-config/input.js +5 -1
  12. package/lib/platform/template/wx/component-config/rich-text.js +4 -0
  13. package/lib/platform/template/wx/component-config/scroll-view.js +4 -0
  14. package/lib/platform/template/wx/component-config/swiper.js +1 -1
  15. package/lib/platform/template/wx/component-config/switch.js +4 -0
  16. package/lib/platform/template/wx/component-config/text.js +4 -0
  17. package/lib/platform/template/wx/component-config/textarea.js +6 -1
  18. package/lib/platform/template/wx/component-config/unsupported.js +1 -1
  19. package/lib/platform/template/wx/component-config/view.js +4 -0
  20. package/lib/platform/template/wx/index.js +127 -1
  21. package/lib/react/processStyles.js +14 -4
  22. package/lib/react/processTemplate.js +3 -0
  23. package/lib/resolve-loader.js +4 -1
  24. package/lib/resolver/AddModePlugin.js +8 -8
  25. package/lib/runtime/components/react/context.ts +6 -0
  26. package/lib/runtime/components/react/dist/context.js +2 -0
  27. package/lib/runtime/components/react/dist/event.config.js +24 -24
  28. package/lib/runtime/components/react/dist/getInnerListeners.js +183 -174
  29. package/lib/runtime/components/react/dist/mpx-button.jsx +77 -49
  30. package/lib/runtime/components/react/dist/mpx-canvas/Bus.js +60 -0
  31. package/lib/runtime/components/react/dist/mpx-canvas/CanvasGradient.js +15 -0
  32. package/lib/runtime/components/react/dist/mpx-canvas/CanvasRenderingContext2D.js +84 -0
  33. package/lib/runtime/components/react/dist/mpx-canvas/Image.js +87 -0
  34. package/lib/runtime/components/react/dist/mpx-canvas/ImageData.js +15 -0
  35. package/lib/runtime/components/react/dist/mpx-canvas/constructorsRegistry.js +28 -0
  36. package/lib/runtime/components/react/dist/mpx-canvas/html.js +343 -0
  37. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +232 -0
  38. package/lib/runtime/components/react/dist/mpx-canvas/utils.jsx +89 -0
  39. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +13 -19
  40. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +29 -38
  41. package/lib/runtime/components/react/dist/mpx-form.jsx +16 -19
  42. package/lib/runtime/components/react/dist/mpx-icon.jsx +8 -16
  43. package/lib/runtime/components/react/dist/mpx-image.jsx +291 -0
  44. package/lib/runtime/components/react/dist/mpx-input.jsx +54 -27
  45. package/lib/runtime/components/react/dist/mpx-label.jsx +15 -22
  46. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +13 -16
  47. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +13 -13
  48. package/lib/runtime/components/react/dist/mpx-navigator.jsx +2 -4
  49. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +6 -2
  50. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +5 -3
  51. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +6 -2
  52. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +6 -2
  53. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +6 -2
  54. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +10 -15
  55. package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +39 -0
  56. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +160 -88
  57. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +80 -121
  58. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +11 -19
  59. package/lib/runtime/components/react/dist/mpx-radio.jsx +27 -42
  60. package/lib/runtime/components/react/dist/mpx-rich-text/html.js +39 -0
  61. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +63 -0
  62. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +6 -4
  63. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +47 -41
  64. package/lib/runtime/components/react/dist/mpx-simple-text.jsx +11 -0
  65. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +4 -2
  66. package/lib/runtime/components/react/dist/mpx-swiper.jsx +606 -0
  67. package/lib/runtime/components/react/dist/mpx-switch.jsx +20 -10
  68. package/lib/runtime/components/react/dist/mpx-text.jsx +11 -10
  69. package/lib/runtime/components/react/dist/mpx-textarea.jsx +8 -3
  70. package/lib/runtime/components/react/dist/mpx-view.jsx +65 -61
  71. package/lib/runtime/components/react/dist/mpx-web-view.jsx +112 -35
  72. package/lib/runtime/components/react/dist/pickerFaces.js +81 -0
  73. package/lib/runtime/components/react/dist/pickerVIewContext.js +9 -0
  74. package/lib/runtime/components/react/dist/pickerViewMask.jsx +18 -0
  75. package/lib/runtime/components/react/dist/pickerViewOverlay.jsx +23 -0
  76. package/lib/runtime/components/react/dist/useAnimationHooks.js +35 -9
  77. package/lib/runtime/components/react/dist/utils.jsx +70 -23
  78. package/lib/runtime/components/react/getInnerListeners.ts +36 -43
  79. package/lib/runtime/components/react/mpx-button.tsx +95 -43
  80. package/lib/runtime/components/react/mpx-canvas/Bus.ts +70 -0
  81. package/lib/runtime/components/react/mpx-canvas/CanvasGradient.ts +18 -0
  82. package/lib/runtime/components/react/mpx-canvas/CanvasRenderingContext2D.ts +87 -0
  83. package/lib/runtime/components/react/mpx-canvas/Image.ts +102 -0
  84. package/lib/runtime/components/react/mpx-canvas/ImageData.ts +23 -0
  85. package/lib/runtime/components/react/mpx-canvas/constructorsRegistry.ts +38 -0
  86. package/lib/runtime/components/react/mpx-canvas/html.ts +343 -0
  87. package/lib/runtime/components/react/mpx-canvas/index.tsx +302 -0
  88. package/lib/runtime/components/react/mpx-canvas/utils.tsx +150 -0
  89. package/lib/runtime/components/react/mpx-checkbox-group.tsx +13 -12
  90. package/lib/runtime/components/react/mpx-checkbox.tsx +28 -28
  91. package/lib/runtime/components/react/mpx-form.tsx +10 -8
  92. package/lib/runtime/components/react/mpx-icon.tsx +10 -15
  93. package/lib/runtime/components/react/mpx-image.tsx +396 -0
  94. package/lib/runtime/components/react/mpx-input.tsx +61 -33
  95. package/lib/runtime/components/react/mpx-label.tsx +14 -13
  96. package/lib/runtime/components/react/mpx-movable-area.tsx +8 -7
  97. package/lib/runtime/components/react/mpx-movable-view.tsx +1 -1
  98. package/lib/runtime/components/react/mpx-picker/date.tsx +5 -2
  99. package/lib/runtime/components/react/mpx-picker/index.tsx +3 -2
  100. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +5 -2
  101. package/lib/runtime/components/react/mpx-picker/region.tsx +5 -2
  102. package/lib/runtime/components/react/mpx-picker/selector.tsx +5 -2
  103. package/lib/runtime/components/react/mpx-picker/time.tsx +10 -15
  104. package/lib/runtime/components/react/mpx-picker/type.ts +48 -43
  105. package/lib/runtime/components/react/mpx-picker-view-column.tsx +236 -104
  106. package/lib/runtime/components/react/mpx-picker-view.tsx +132 -122
  107. package/lib/runtime/components/react/mpx-radio-group.tsx +11 -12
  108. package/lib/runtime/components/react/mpx-radio.tsx +26 -29
  109. package/lib/runtime/components/react/mpx-scroll-view.tsx +32 -30
  110. package/lib/runtime/components/react/mpx-simple-text.tsx +18 -0
  111. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +4 -2
  112. package/lib/runtime/components/react/mpx-swiper-item.tsx +3 -2
  113. package/lib/runtime/components/react/mpx-switch.tsx +10 -8
  114. package/lib/runtime/components/react/mpx-text.tsx +6 -2
  115. package/lib/runtime/components/react/mpx-view.tsx +81 -59
  116. package/lib/runtime/components/react/mpx-web-view.tsx +46 -19
  117. package/lib/runtime/components/react/pickerFaces.ts +104 -0
  118. package/lib/runtime/components/react/pickerOverlay.tsx +32 -0
  119. package/lib/runtime/components/react/types/common.ts +2 -0
  120. package/lib/runtime/components/react/types/global.d.ts +3 -16
  121. package/lib/runtime/components/react/utils.tsx +98 -27
  122. package/lib/runtime/components/tenon/getInnerListeners.js +334 -0
  123. package/lib/runtime/components/tenon/tenon-button.vue +309 -0
  124. package/lib/runtime/components/tenon/tenon-image.vue +66 -0
  125. package/lib/runtime/components/tenon/tenon-input.vue +171 -0
  126. package/lib/runtime/components/tenon/tenon-rich-text.vue +26 -0
  127. package/lib/runtime/components/tenon/tenon-scroll-view.vue +127 -0
  128. package/lib/runtime/components/tenon/tenon-switch.vue +96 -0
  129. package/lib/runtime/components/tenon/tenon-text.vue +70 -0
  130. package/lib/runtime/components/tenon/tenon-textarea.vue +86 -0
  131. package/lib/runtime/components/tenon/tenon-view.vue +93 -0
  132. package/lib/runtime/components/web/getInnerListeners.js +6 -6
  133. package/lib/runtime/components/web/mpx-movable-view.vue +334 -344
  134. package/lib/runtime/components/web/mpx-picker-view-column.vue +75 -75
  135. package/lib/runtime/components/web/mpx-picker.vue +382 -385
  136. package/lib/runtime/components/web/mpx-web-view.vue +162 -162
  137. package/lib/runtime/optionProcessor.js +7 -16
  138. package/lib/runtime/optionProcessor.tenon.js +84 -0
  139. package/lib/runtime/utils.js +2 -0
  140. package/lib/style-compiler/index.js +1 -1
  141. package/lib/style-compiler/plugins/hm.js +20 -0
  142. package/lib/template-compiler/bind-this.js +7 -2
  143. package/lib/template-compiler/compiler.js +70 -42
  144. package/lib/template-compiler/gen-node-react.js +3 -3
  145. package/lib/tenon/index.js +117 -0
  146. package/lib/tenon/processJSON.js +352 -0
  147. package/lib/tenon/processScript.js +203 -0
  148. package/lib/tenon/processStyles.js +21 -0
  149. package/lib/tenon/processTemplate.js +126 -0
  150. package/lib/tenon/script-helper.js +223 -0
  151. package/lib/utils/env.js +6 -1
  152. package/lib/utils/get-relative-path.js +25 -0
  153. package/package.json +9 -4
  154. package/LICENSE +0 -433
  155. package/lib/runtime/components/react/dist/mpx-image/index.jsx +0 -226
  156. package/lib/runtime/components/react/dist/mpx-image/svg.jsx +0 -7
  157. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +0 -478
  158. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +0 -68
  159. package/lib/runtime/components/react/dist/mpx-swiper/type.js +0 -1
  160. package/lib/runtime/components/react/mpx-image/index.tsx +0 -345
  161. package/lib/runtime/components/react/mpx-image/svg.tsx +0 -22
@@ -47,9 +47,32 @@ const InitialValue = Object.assign({
47
47
  const TransformOrigin = 'transformOrigin';
48
48
  // transform
49
49
  const isTransform = (key) => Object.keys(TransformInitial).includes(key);
50
+ // 多value解析
51
+ const parseValues = (str, char = ' ') => {
52
+ let stack = 0;
53
+ let temp = '';
54
+ const result = [];
55
+ for (let i = 0; i < str.length; i++) {
56
+ if (str[i] === '(') {
57
+ stack++;
58
+ }
59
+ else if (str[i] === ')') {
60
+ stack--;
61
+ }
62
+ // 非括号内 或者 非分隔字符且非空
63
+ if (stack !== 0 || (str[i] !== char && str[i] !== ' ')) {
64
+ temp += str[i];
65
+ }
66
+ if ((stack === 0 && str[i] === char) || i === str.length - 1) {
67
+ result.push(temp);
68
+ temp = '';
69
+ }
70
+ }
71
+ return result;
72
+ };
50
73
  // parse string transform, eg: transform: 'rotateX(45deg) rotateZ(0.785398rad)'
51
74
  const parseTransform = (transformStr) => {
52
- const values = transformStr.trim().split(/\s+/);
75
+ const values = parseValues(transformStr);
53
76
  const transform = [];
54
77
  values.forEach(item => {
55
78
  const match = item.match(/([/\w]+)\(([^)]+)\)/);
@@ -73,7 +96,7 @@ const parseTransform = (transformStr) => {
73
96
  break;
74
97
  case 'matrix':
75
98
  case 'matrix3d':
76
- transform.push({ [key]: val.split(',').map(val => +val) });
99
+ transform.push({ [key]: parseValues(val, ',').map(val => +val) });
77
100
  break;
78
101
  case 'translate':
79
102
  case 'scale':
@@ -84,8 +107,8 @@ const parseTransform = (transformStr) => {
84
107
  {
85
108
  // 2 个以上的值处理
86
109
  key = key.replace('3d', '');
87
- const vals = val.split(',', key === 'rotate' ? 4 : 3);
88
- // scale(.5) === scaleX(.5) scaleY(.5) 这里处理一下
110
+ const vals = parseValues(val, ',').splice(0, key === 'rotate' ? 4 : 3);
111
+ // scale(.5) === scaleX(.5) scaleY(.5)
89
112
  if (vals.length === 1 && key === 'scale') {
90
113
  vals.push(vals[0]);
91
114
  }
@@ -183,11 +206,14 @@ export default function useAnimationHooks(props) {
183
206
  }
184
207
  // 添加每个key的多次step动画
185
208
  animatedKeys.forEach(key => {
186
- let toVal = (rules.get(key) || transform.get(key));
187
209
  // key不存在,第一轮取shareValMap[key]value,非第一轮取上一轮的
188
- if (toVal === undefined) {
189
- toVal = index > 0 ? lastValueMap[key] : shareValMap[key].value;
190
- }
210
+ const toVal = rules.get(key) !== undefined
211
+ ? rules.get(key)
212
+ : transform.get(key) !== undefined
213
+ ? transform.get(key)
214
+ : index > 0
215
+ ? lastValueMap[key]
216
+ : shareValMap[key].value;
191
217
  const animation = getAnimation({ key, value: toVal }, { delay, duration, easing }, needSetCallback ? setTransformOrigin : undefined);
192
218
  needSetCallback = false;
193
219
  if (!sequence[key]) {
@@ -298,6 +324,6 @@ export default function useAnimationHooks(props) {
298
324
  styles[key] = shareValMap[key].value;
299
325
  }
300
326
  return styles;
301
- }, Object.assign({}, originalStyle));
327
+ }, {});
302
328
  });
303
329
  }
@@ -1,13 +1,16 @@
1
- import { useEffect, useRef, isValidElement, useContext, useState, Children, cloneElement } from 'react';
2
- import { isObject, hasOwn, diffAndCloneA, error, warn, getFocusedNavigation } from '@mpxjs/utils';
1
+ import { useEffect, useCallback, useMemo, useRef, isValidElement, useContext, useState, Children, cloneElement } from 'react';
2
+ import { Image } from 'react-native';
3
+ import { isObject, isFunction, isNumber, hasOwn, diffAndCloneA, error, warn, getFocusedNavigation } from '@mpxjs/utils';
3
4
  import { VarContext } from './context';
4
5
  import { ExpressionParser, parseFunc, ReplaceSource } from './parser';
5
6
  import { initialWindowMetrics } from 'react-native-safe-area-context';
7
+ import FastImage from '@d11/react-native-fast-image';
6
8
  export const TEXT_STYLE_REGEX = /color|font.*|text.*|letterSpacing|lineHeight|includeFontPadding|writingDirection/;
7
9
  export const PERCENT_REGEX = /^\s*-?\d+(\.\d+)?%\s*$/;
8
10
  export const URL_REGEX = /^\s*url\(["']?(.*?)["']?\)\s*$/;
11
+ export const SVG_REGEXP = /https?:\/\/.*\.(?:svg)/i;
9
12
  export const BACKGROUND_REGEX = /^background(Image|Size|Repeat|Position)$/;
10
- export const TEXT_PROPS_REGEX = /ellipsizeMode|numberOfLines/;
13
+ export const TEXT_PROPS_REGEX = /ellipsizeMode|numberOfLines|allowFontScaling/;
11
14
  export const DEFAULT_FONT_SIZE = 16;
12
15
  export const HIDDEN_STYLE = {
13
16
  opacity: 0
@@ -24,10 +27,7 @@ const safeAreaInsetMap = {
24
27
  };
25
28
  function getSafeAreaInset(name) {
26
29
  const navigation = getFocusedNavigation();
27
- const insets = {
28
- ...initialWindowMetrics?.insets,
29
- ...navigation?.insets
30
- };
30
+ const insets = extendObject({}, initialWindowMetrics?.insets, navigation?.insets);
31
31
  return insets[safeAreaInsetMap[name]];
32
32
  }
33
33
  export function omit(obj, fields) {
@@ -69,7 +69,7 @@ export const parseInlineStyle = (inlineStyle = '') => {
69
69
  if (rest.length || !v || !k)
70
70
  return styleObj;
71
71
  const key = k.trim().replace(/-./g, c => c.substring(1).toUpperCase());
72
- return Object.assign(styleObj, { [key]: v.trim() });
72
+ return Object.assign(styleObj, { [key]: global.__formatValue(v.trim()) });
73
73
  }, {});
74
74
  };
75
75
  export const parseUrl = (cssUrl = '') => {
@@ -79,23 +79,13 @@ export const parseUrl = (cssUrl = '') => {
79
79
  return match?.[1];
80
80
  };
81
81
  export const getRestProps = (transferProps = {}, originProps = {}, deletePropsKey = []) => {
82
- return {
83
- ...transferProps,
84
- ...omit(originProps, deletePropsKey)
85
- };
82
+ return extendObject({}, transferProps, omit(originProps, deletePropsKey));
86
83
  };
87
84
  export function isText(ele) {
88
85
  if (isValidElement(ele)) {
89
86
  const displayName = ele.type?.displayName;
90
87
  const isCustomText = ele.type?.isCustomText;
91
- return displayName === 'MpxText' || displayName === 'Text' || !!isCustomText;
92
- }
93
- return false;
94
- }
95
- export function isEmbedded(ele) {
96
- if (isValidElement(ele)) {
97
- const displayName = ele.type?.displayName || '';
98
- return ['mpx-checkbox', 'mpx-radio', 'mpx-switch'].includes(displayName);
88
+ return displayName === 'MpxText' || displayName === 'MpxSimpleText' || displayName === 'Text' || !!isCustomText;
99
89
  }
100
90
  return false;
101
91
  }
@@ -238,6 +228,14 @@ function transformCalc(styleObj, calcKeyPaths, formatter) {
238
228
  });
239
229
  });
240
230
  }
231
+ const stringifyProps = ['fontWeight'];
232
+ function transformStringify(styleObj) {
233
+ stringifyProps.forEach((prop) => {
234
+ if (isNumber(styleObj[prop])) {
235
+ styleObj[prop] = '' + styleObj[prop];
236
+ }
237
+ });
238
+ }
241
239
  export function useTransformStyle(styleObj = {}, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight }) {
242
240
  const varStyle = {};
243
241
  const normalStyle = {};
@@ -336,6 +334,8 @@ export function useTransformStyle(styleObj = {}, { enableVar, externalVarContext
336
334
  }
337
335
  }
338
336
  });
337
+ // transform number enum stringify
338
+ transformStringify(normalStyle);
339
339
  return {
340
340
  normalStyle,
341
341
  hasSelfPercent,
@@ -402,7 +402,7 @@ export function splitProps(props) {
402
402
  export const useLayout = ({ props, hasSelfPercent, setWidth, setHeight, onLayout, nodeRef }) => {
403
403
  const layoutRef = useRef({});
404
404
  const hasLayoutRef = useRef(false);
405
- const layoutStyle = !hasLayoutRef.current && hasSelfPercent ? HIDDEN_STYLE : {};
405
+ const layoutStyle = useMemo(() => { return !hasLayoutRef.current && hasSelfPercent ? HIDDEN_STYLE : {}; }, [hasLayoutRef.current]);
406
406
  const layoutProps = {};
407
407
  const enableOffset = props['enable-offset'];
408
408
  if (hasSelfPercent || onLayout || enableOffset) {
@@ -433,8 +433,8 @@ export function wrapChildren(props = {}, { hasVarDec, varContext, textStyle, tex
433
433
  if (textStyle || textProps) {
434
434
  children = Children.map(children, (child) => {
435
435
  if (isText(child)) {
436
- const style = { ...textStyle, ...child.props.style };
437
- return cloneElement(child, { ...textProps, style });
436
+ const style = extendObject({}, textStyle, child.props.style);
437
+ return cloneElement(child, extendObject({}, textProps, { style }));
438
438
  }
439
439
  return child;
440
440
  });
@@ -444,6 +444,34 @@ export function wrapChildren(props = {}, { hasVarDec, varContext, textStyle, tex
444
444
  }
445
445
  return children;
446
446
  }
447
+ export const debounce = (func, delay) => {
448
+ let timer;
449
+ const wrapper = (...args) => {
450
+ clearTimeout(timer);
451
+ timer = setTimeout(() => {
452
+ func(...args);
453
+ }, delay);
454
+ };
455
+ wrapper.clear = () => {
456
+ clearTimeout(timer);
457
+ };
458
+ return wrapper;
459
+ };
460
+ export const useDebounceCallback = (func, delay) => {
461
+ const debounced = useMemo(() => debounce(func, delay), [func]);
462
+ return debounced;
463
+ };
464
+ export const useStableCallback = (callback) => {
465
+ const ref = useRef(callback);
466
+ ref.current = callback;
467
+ return useCallback((...args) => ref.current?.(...args), []);
468
+ };
469
+ export const usePrevious = (value) => {
470
+ const ref = useRef(undefined);
471
+ const prev = ref.current;
472
+ ref.current = value;
473
+ return prev;
474
+ };
447
475
  export function flatGesture(gestures = []) {
448
476
  return (gestures && gestures.flatMap((gesture) => {
449
477
  if (gesture && gesture.nodeRefs) {
@@ -453,3 +481,22 @@ export function flatGesture(gestures = []) {
453
481
  return gesture?.current ? [gesture] : [];
454
482
  })) || [];
455
483
  }
484
+ export const extendObject = Object.assign;
485
+ export function getCurrentPage(pageId) {
486
+ if (!global.getCurrentPages)
487
+ return;
488
+ const pages = global.getCurrentPages();
489
+ return pages.find((page) => isFunction(page.getPageId) && page.getPageId() === pageId);
490
+ }
491
+ export function renderImage(imageProps, enableFastImage = false) {
492
+ const Component = enableFastImage ? FastImage : Image;
493
+ return <Component {...imageProps}/>;
494
+ }
495
+ export function pickStyle(styleObj = {}, pickedKeys, callback) {
496
+ return pickedKeys.reduce((acc, key) => {
497
+ if (key in styleObj) {
498
+ acc[key] = callback ? callback(key, styleObj[key]) : styleObj[key];
499
+ }
500
+ return acc;
501
+ }, {});
502
+ }
@@ -1,6 +1,6 @@
1
1
  import { useRef } from 'react'
2
- import { NativeSyntheticEvent } from 'react-native'
3
- import { omit } from './utils'
2
+ import { hasOwn, collectDataset } from '@mpxjs/utils'
3
+ import { omit, extendObject } from './utils'
4
4
  import eventConfigMap from './event.config'
5
5
  import {
6
6
  Props,
@@ -9,7 +9,6 @@ import {
9
9
  UseInnerPropsConfig,
10
10
  InnerRef,
11
11
  SetTimeoutReturnType,
12
- DataSetType,
13
12
  LayoutRef,
14
13
  NativeTouchEvent
15
14
  } from './types/getInnerListeners'
@@ -30,17 +29,22 @@ const getTouchEvent = (
30
29
  } = nativeEvent
31
30
  const { id } = props
32
31
  const { layoutRef } = config
33
- return {
34
- ...event,
35
- type,
36
- timeStamp: timestamp,
37
- currentTarget: {
38
- ...(event.currentTarget || {}),
32
+
33
+ const currentTarget = extendObject(
34
+ {},
35
+ event.currentTarget,
36
+ {
39
37
  id: id || '',
40
- dataset: getDataSet(props),
38
+ dataset: collectDataset(props),
41
39
  offsetLeft: layoutRef?.current?.offsetLeft || 0,
42
40
  offsetTop: layoutRef?.current?.offsetTop || 0
43
- },
41
+ }
42
+ )
43
+
44
+ return extendObject({}, event, {
45
+ type,
46
+ timeStamp: timestamp,
47
+ currentTarget,
44
48
  detail: {
45
49
  x: pageX,
46
50
  y: pageY
@@ -66,20 +70,7 @@ const getTouchEvent = (
66
70
  persist: event.persist,
67
71
  stopPropagation: event.stopPropagation,
68
72
  preventDefault: event.preventDefault
69
- }
70
- }
71
-
72
- export const getDataSet = (props: Record<string, any>) => {
73
- const result: DataSetType = {}
74
-
75
- for (const key in props) {
76
- if (key.indexOf('data-') === 0) {
77
- const newKey = key.substr(5)
78
- result[newKey] = props[key]
79
- }
80
- }
81
-
82
- return result
73
+ })
83
74
  }
84
75
 
85
76
  export const getCustomEvent = (
@@ -88,21 +79,20 @@ export const getCustomEvent = (
88
79
  { detail = {}, layoutRef }: { detail?: Record<string, unknown>; layoutRef: LayoutRef },
89
80
  props: Props = {}
90
81
  ) => {
91
- return {
92
- ...oe,
82
+ const targetInfo = extendObject({}, oe.target, {
83
+ id: props.id || '',
84
+ dataset: collectDataset(props),
85
+ offsetLeft: layoutRef?.current?.offsetLeft || 0,
86
+ offsetTop: layoutRef?.current?.offsetTop || 0
87
+ })
88
+ return extendObject({}, oe, {
93
89
  type,
94
90
  detail,
95
- target: {
96
- ...(oe.target || {}),
97
- id: props.id || '',
98
- dataset: getDataSet(props),
99
- offsetLeft: layoutRef?.current?.offsetLeft || 0,
100
- offsetTop: layoutRef?.current?.offsetTop || 0
101
- },
91
+ target: targetInfo,
102
92
  persist: oe.persist,
103
93
  stopPropagation: oe.stopPropagation,
104
94
  preventDefault: oe.preventDefault
105
- }
95
+ })
106
96
  }
107
97
 
108
98
  const useInnerProps = (
@@ -143,10 +133,10 @@ const useInnerProps = (
143
133
  ...userRemoveProps
144
134
  ]
145
135
 
146
- propsRef.current = { ...props, ...additionalProps }
136
+ propsRef.current = extendObject({}, props, additionalProps)
147
137
 
148
138
  for (const key in eventConfigMap) {
149
- if (propsRef.current[key]) {
139
+ if (hasOwn(propsRef.current, key)) {
150
140
  eventConfig[key] = eventConfigMap[key]
151
141
  }
152
142
  }
@@ -289,9 +279,11 @@ const useInnerProps = (
289
279
 
290
280
  const events: Record<string, (e: NativeTouchEvent) => void> = {}
291
281
 
292
- const transformedEventKeys: string[] = []
282
+ let transformedEventKeys: string[] = []
293
283
  for (const key in eventConfig) {
294
- transformedEventKeys.push(...eventConfig[key])
284
+ if (propsRef.current[key]) {
285
+ transformedEventKeys = transformedEventKeys.concat(eventConfig[key])
286
+ }
295
287
  }
296
288
 
297
289
  const finalEventKeys = [...new Set(transformedEventKeys)]
@@ -304,9 +296,10 @@ const useInnerProps = (
304
296
 
305
297
  const rawEventKeys = Object.keys(eventConfig)
306
298
 
307
- return {
308
- ...events,
309
- ...omit(propsRef.current, [...rawEventKeys, ...removeProps])
310
- }
299
+ return extendObject(
300
+ {},
301
+ events,
302
+ omit(propsRef.current, [...rawEventKeys, ...removeProps])
303
+ )
311
304
  }
312
305
  export default useInnerProps
@@ -45,10 +45,10 @@ import {
45
45
  NativeSyntheticEvent
46
46
  } from 'react-native'
47
47
  import { warn } from '@mpxjs/utils'
48
- import { splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren } from './utils'
48
+ import { getCurrentPage, splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren, extendObject } from './utils'
49
49
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
50
50
  import useNodesRef, { HandlerRef } from './useNodesRef'
51
- import { FormContext } from './context'
51
+ import { RouteContext, FormContext } from './context'
52
52
 
53
53
  export type Type = 'default' | 'primary' | 'warn'
54
54
 
@@ -128,7 +128,8 @@ const styles = StyleSheet.create({
128
128
  }
129
129
  })
130
130
 
131
- const getOpenTypeEvent = (openType: OpenType) => {
131
+ const getOpenTypeEvent = (openType?: OpenType) => {
132
+ if (!openType) return
132
133
  if (!global.__mpx?.config?.rnConfig) {
133
134
  warn('Environment not supported')
134
135
  return
@@ -148,6 +149,12 @@ const getOpenTypeEvent = (openType: OpenType) => {
148
149
  return event
149
150
  }
150
151
 
152
+ const timer = (data: any, time = 3000) => new Promise((resolve) => {
153
+ setTimeout(() => {
154
+ resolve(data)
155
+ }, time)
156
+ })
157
+
151
158
  const Loading = ({ alone = false }: { alone: boolean }): JSX.Element => {
152
159
  const image = useRef(new Animated.Value(0)).current
153
160
 
@@ -174,11 +181,14 @@ const Loading = ({ alone = false }: { alone: boolean }): JSX.Element => {
174
181
  }
175
182
  }, [])
176
183
 
177
- const loadingStyle = {
178
- ...styles.loading,
179
- transform: [{ rotate }],
180
- marginRight: alone ? 0 : 5
181
- }
184
+ const loadingStyle = extendObject(
185
+ {},
186
+ styles.loading,
187
+ {
188
+ transform: [{ rotate }],
189
+ marginRight: alone ? 0 : 5
190
+ }
191
+ )
182
192
 
183
193
  return <Animated.Image testID="loading" style={loadingStyle} source={{ uri: LOADING_IMAGE_URI }} />
184
194
  }
@@ -211,6 +221,8 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
211
221
  bindtouchend
212
222
  } = props
213
223
 
224
+ const pageId = useContext(RouteContext)
225
+
214
226
  const formContext = useContext(FormContext)
215
227
 
216
228
  let submitFn: () => void | undefined
@@ -265,28 +277,28 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
265
277
  backgroundColor: plain ? 'transparent' : normalBackgroundColor
266
278
  }
267
279
 
268
- const defaultViewStyle = {
269
- ...styles.button,
270
- ...(isMiniSize && styles.buttonMini),
271
- ...viewStyle
272
- }
280
+ const defaultViewStyle = extendObject(
281
+ {},
282
+ styles.button,
283
+ isMiniSize ? styles.buttonMini : null,
284
+ viewStyle
285
+ )
273
286
 
274
- const defaultTextStyle = {
275
- ...styles.text,
276
- ...(isMiniSize && styles.textMini),
277
- color: plain ? plainTextColor : normalTextColor
278
- }
287
+ const defaultTextStyle = extendObject(
288
+ {},
289
+ styles.text,
290
+ isMiniSize ? styles.textMini : {},
291
+ { color: plain ? plainTextColor : normalTextColor }
292
+ )
279
293
 
280
- const defaultStyle = {
281
- ...defaultViewStyle,
282
- ...defaultTextStyle
283
- }
294
+ const defaultStyle = extendObject({}, defaultViewStyle, defaultTextStyle)
284
295
 
285
- const styleObj = {
286
- ...defaultStyle,
287
- ...style,
288
- ...(applyHoverEffect && hoverStyle)
289
- }
296
+ const styleObj = extendObject(
297
+ {},
298
+ defaultStyle,
299
+ style,
300
+ applyHoverEffect ? hoverStyle : {}
301
+ )
290
302
 
291
303
  const {
292
304
  hasSelfPercent,
@@ -299,28 +311,52 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
299
311
 
300
312
  const nodeRef = useRef(null)
301
313
 
302
- useNodesRef(props, ref, nodeRef, { defaultStyle })
314
+ useNodesRef(props, ref, nodeRef, { style: normalStyle })
303
315
 
304
316
  const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
305
317
 
306
- const { textStyle, backgroundStyle, innerStyle } = splitStyle(normalStyle)
318
+ const { textStyle, backgroundStyle, innerStyle = {} } = splitStyle(normalStyle)
307
319
 
308
320
  if (backgroundStyle) {
309
321
  warn('Button does not support background image-related styles!')
310
322
  }
311
323
 
312
324
  const handleOpenTypeEvent = (evt: NativeSyntheticEvent<TouchEvent>) => {
313
- if (!openType) return
314
325
  const handleEvent = getOpenTypeEvent(openType)
326
+ if (!handleEvent) return
315
327
 
316
328
  if (openType === 'share') {
317
- handleEvent && handleEvent({
329
+ const currentPage = getCurrentPage(pageId)
330
+ const event = {
318
331
  from: 'button',
319
- target: getCustomEvent('tap', evt, { layoutRef }, props).target
320
- })
332
+ target: getCustomEvent('tap', evt, { layoutRef }, props).target,
333
+ webViewUrl: currentPage?.__webViewUrl
334
+ }
335
+ if (currentPage) {
336
+ const defaultMessage = {
337
+ title: global.__mpx.config.rnConfig.projectName || 'AwesomeProject',
338
+ path: currentPage.route || ''
339
+ }
340
+ if (currentPage.onShareAppMessage) {
341
+ const { promise, ...message } = currentPage.onShareAppMessage(event) || {}
342
+ if (promise) {
343
+ Promise.race([Promise.resolve(promise), timer(message)])
344
+ .then((msg) => {
345
+ handleEvent(Object.assign({}, defaultMessage, msg))
346
+ })
347
+ } else {
348
+ handleEvent(Object.assign({}, defaultMessage, message))
349
+ }
350
+ } else {
351
+ handleEvent(defaultMessage)
352
+ }
353
+ } else {
354
+ warn('Current page not found')
355
+ // Todo handleEvent(event)
356
+ }
321
357
  }
322
358
 
323
- if (openType === 'getUserInfo' && handleEvent && bindgetuserinfo) {
359
+ if (openType === 'getUserInfo' && bindgetuserinfo) {
324
360
  Promise.resolve(handleEvent)
325
361
  .then((userInfo) => {
326
362
  if (typeof userInfo === 'object') {
@@ -375,15 +411,31 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
375
411
 
376
412
  const innerProps = useInnerProps(
377
413
  props,
378
- {
379
- ref: nodeRef,
380
- style: { ...innerStyle, ...layoutStyle },
381
- ...layoutProps,
382
- bindtouchstart: onTouchStart,
383
- bindtouchend: onTouchEnd,
384
- bindtap: onTap
385
- },
386
- [],
414
+ extendObject(
415
+ {
416
+ ref: nodeRef,
417
+ style: extendObject({}, innerStyle, layoutStyle)
418
+ },
419
+ layoutProps,
420
+ {
421
+ bindtouchstart: (bindtouchstart || !disabled) && onTouchStart,
422
+ bindtouchend: (bindtouchend || !disabled) && onTouchEnd,
423
+ bindtap: !disabled && onTap
424
+ }
425
+ ),
426
+ [
427
+ 'disabled',
428
+ 'size',
429
+ 'type',
430
+ 'plain',
431
+ 'loading',
432
+ 'hover-class',
433
+ 'hover-style',
434
+ 'hover-start-time',
435
+ 'hover-stay-time',
436
+ 'open-type',
437
+ 'form-type'
438
+ ],
387
439
  {
388
440
  layoutRef,
389
441
  disableTap: disabled
@@ -0,0 +1,70 @@
1
+ import { warn } from '@mpxjs/utils'
2
+ interface Message {
3
+ id?: string
4
+ type: string
5
+ payload?: any
6
+ }
7
+ export default class Bus {
8
+ _paused: Boolean = false;
9
+ _messageListeners: { [key: string]: (message: Message) => void } = {}
10
+ _queue: Message[] = []
11
+ _send: (message: Message | Message[]) => void
12
+ _timeoutId: NodeJS.Timeout | null = null
13
+ constructor (send: (message: Message | Message[]) => void) {
14
+ this._send = send
15
+ }
16
+
17
+ post (message: Message): Promise<any> {
18
+ return new Promise((resolve) => {
19
+ if (message.type !== 'set' && message.id) {
20
+ this._messageListeners[message.id] = resolve
21
+ }
22
+
23
+ if (!this._paused) {
24
+ this._queue.push(message)
25
+ this.startBatching()
26
+ } else {
27
+ this._queue.push(message)
28
+ }
29
+ })
30
+ }
31
+
32
+ handle (message: Message): void {
33
+ if (!message.id) return
34
+ const handler = this._messageListeners[message.id]
35
+ delete this._messageListeners[message.id]
36
+
37
+ if (handler) {
38
+ handler(message)
39
+ } else {
40
+ warn(`Received unexpected message: ${message}`)
41
+ }
42
+ }
43
+
44
+ pause (): void {
45
+ this._paused = true
46
+ }
47
+
48
+ resume (): void {
49
+ this._paused = false
50
+ this._send(this._queue)
51
+ this._queue = []
52
+ }
53
+
54
+ startBatching (): void {
55
+ if (this._timeoutId) return
56
+
57
+ this._timeoutId = setTimeout(() => {
58
+ this._send(this._queue)
59
+ this._queue = []
60
+ this._timeoutId = null
61
+ }, 16)
62
+ }
63
+
64
+ clearBatchingTimeout (): void {
65
+ if (this._timeoutId) {
66
+ clearTimeout(this._timeoutId)
67
+ this._timeoutId = null
68
+ }
69
+ }
70
+ }
@@ -0,0 +1,18 @@
1
+ import { WebviewMessage, CanvasInstance, registerWebviewMethods } from './utils'
2
+
3
+ const METHODS = ['addColorStop']
4
+ export default class CanvasGradient {
5
+ private canvas: CanvasInstance;
6
+ [key: string]: any;
7
+ constructor (canvas: CanvasInstance, noOnConstruction = false) {
8
+ this.canvas = canvas
9
+ registerWebviewMethods(this, METHODS)
10
+ if (this.onConstruction && !noOnConstruction) {
11
+ this.onConstruction()
12
+ }
13
+ }
14
+
15
+ postMessage (message: WebviewMessage) {
16
+ return this.canvas.postMessage(message)
17
+ }
18
+ }