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

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 (126) hide show
  1. package/LICENSE +433 -0
  2. package/README.md +1 -1
  3. package/lib/config.js +0 -14
  4. package/lib/dependencies/ResolveDependency.js +0 -5
  5. package/lib/index.js +7 -38
  6. package/lib/json-compiler/helper.js +3 -3
  7. package/lib/loader.js +0 -53
  8. package/lib/parser.js +1 -1
  9. package/lib/platform/json/wx/index.js +21 -8
  10. package/lib/platform/style/wx/index.js +51 -54
  11. package/lib/platform/template/wx/component-config/button.js +2 -14
  12. package/lib/platform/template/wx/component-config/fix-component-name.js +15 -12
  13. package/lib/platform/template/wx/component-config/image.js +0 -4
  14. package/lib/platform/template/wx/component-config/index.js +1 -1
  15. package/lib/platform/template/wx/component-config/input.js +0 -4
  16. package/lib/platform/template/wx/component-config/movable-view.js +8 -1
  17. package/lib/platform/template/wx/component-config/picker-view.js +1 -5
  18. package/lib/platform/template/wx/component-config/rich-text.js +6 -2
  19. package/lib/platform/template/wx/component-config/scroll-view.js +1 -5
  20. package/lib/platform/template/wx/component-config/switch.js +0 -4
  21. package/lib/platform/template/wx/component-config/text.js +0 -4
  22. package/lib/platform/template/wx/component-config/textarea.js +0 -5
  23. package/lib/platform/template/wx/component-config/unsupported.js +1 -1
  24. package/lib/platform/template/wx/component-config/view.js +0 -4
  25. package/lib/platform/template/wx/index.js +1 -131
  26. package/lib/resolve-loader.js +1 -4
  27. package/lib/runtime/components/react/context.ts +8 -0
  28. package/lib/runtime/components/react/dist/context.js +2 -0
  29. package/lib/runtime/components/react/dist/getInnerListeners.js +39 -37
  30. package/lib/runtime/components/react/dist/mpx-button.jsx +16 -44
  31. package/lib/runtime/components/react/dist/mpx-canvas/html.js +2 -4
  32. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +5 -1
  33. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +93 -58
  34. package/lib/runtime/components/react/dist/mpx-navigator.jsx +1 -1
  35. package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +10 -14
  36. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +94 -81
  37. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +19 -16
  38. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +10 -11
  39. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +11 -4
  40. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +31 -8
  41. package/lib/runtime/components/react/dist/mpx-swiper.jsx +435 -371
  42. package/lib/runtime/components/react/dist/mpx-view.jsx +18 -53
  43. package/lib/runtime/components/react/dist/pickerFaces.js +3 -8
  44. package/lib/runtime/components/react/dist/pickerVIewContext.js +5 -0
  45. package/lib/runtime/components/react/dist/{pickerViewOverlay.jsx → pickerViewIndicator.jsx} +6 -6
  46. package/lib/runtime/components/react/dist/pickerViewMask.jsx +2 -2
  47. package/lib/runtime/components/react/dist/useAnimationHooks.js +27 -10
  48. package/lib/runtime/components/react/dist/utils.jsx +162 -70
  49. package/lib/runtime/components/react/event.config.ts +25 -26
  50. package/lib/runtime/components/react/getInnerListeners.ts +236 -182
  51. package/lib/runtime/components/react/mpx-button.tsx +27 -69
  52. package/lib/runtime/components/react/mpx-canvas/html.ts +2 -4
  53. package/lib/runtime/components/react/mpx-canvas/index.tsx +44 -46
  54. package/lib/runtime/components/react/mpx-checkbox-group.tsx +15 -13
  55. package/lib/runtime/components/react/mpx-checkbox.tsx +20 -21
  56. package/lib/runtime/components/react/mpx-form.tsx +15 -20
  57. package/lib/runtime/components/react/mpx-icon.tsx +2 -2
  58. package/lib/runtime/components/react/mpx-image.tsx +87 -47
  59. package/lib/runtime/components/react/mpx-input.tsx +24 -32
  60. package/lib/runtime/components/react/mpx-label.tsx +12 -14
  61. package/lib/runtime/components/react/mpx-movable-area.tsx +10 -16
  62. package/lib/runtime/components/react/mpx-movable-view.tsx +133 -92
  63. package/lib/runtime/components/react/mpx-navigator.tsx +3 -9
  64. package/lib/runtime/components/react/mpx-picker-view-column-item.tsx +76 -0
  65. package/lib/runtime/components/react/mpx-picker-view-column.tsx +206 -183
  66. package/lib/runtime/components/react/mpx-picker-view.tsx +49 -48
  67. package/lib/runtime/components/react/mpx-radio-group.tsx +13 -15
  68. package/lib/runtime/components/react/mpx-radio.tsx +19 -25
  69. package/lib/runtime/components/react/mpx-rich-text/html.ts +40 -0
  70. package/lib/runtime/components/react/mpx-rich-text/index.tsx +115 -0
  71. package/lib/runtime/components/react/mpx-root-portal.tsx +3 -5
  72. package/lib/runtime/components/react/mpx-scroll-view.tsx +53 -43
  73. package/lib/runtime/components/react/mpx-swiper-item.tsx +45 -11
  74. package/lib/runtime/components/react/mpx-swiper.tsx +742 -0
  75. package/lib/runtime/components/react/mpx-switch.tsx +19 -15
  76. package/lib/runtime/components/react/mpx-text.tsx +8 -16
  77. package/lib/runtime/components/react/mpx-textarea.tsx +11 -10
  78. package/lib/runtime/components/react/mpx-view.tsx +28 -77
  79. package/lib/runtime/components/react/mpx-web-view.tsx +94 -59
  80. package/lib/runtime/components/react/pickerFaces.ts +10 -7
  81. package/lib/runtime/components/react/pickerVIewContext.ts +27 -0
  82. package/lib/runtime/components/react/pickerViewIndicator.tsx +34 -0
  83. package/lib/runtime/components/react/pickerViewMask.tsx +30 -0
  84. package/lib/runtime/components/react/types/{getInnerListeners.ts → getInnerListeners.d.ts} +4 -5
  85. package/lib/runtime/components/react/types/global.d.ts +12 -1
  86. package/lib/runtime/components/react/useAnimationHooks.ts +60 -15
  87. package/lib/runtime/components/react/utils.tsx +175 -71
  88. package/lib/runtime/components/web/mpx-checkbox.vue +1 -1
  89. package/lib/runtime/components/web/mpx-picker-view-column.vue +9 -4
  90. package/lib/runtime/components/web/mpx-web-view.vue +34 -20
  91. package/lib/runtime/optionProcessor.js +0 -22
  92. package/lib/style-compiler/index.js +1 -1
  93. package/lib/style-compiler/plugins/scope-id.js +30 -2
  94. package/lib/template-compiler/compiler.js +91 -39
  95. package/lib/utils/env.js +1 -6
  96. package/lib/utils/pre-process-json.js +9 -5
  97. package/lib/wxss/loader.js +15 -2
  98. package/package.json +4 -7
  99. package/lib/dependencies/AddEntryDependency.js +0 -24
  100. package/lib/runtime/components/react/dist/types/common.js +0 -1
  101. package/lib/runtime/components/react/dist/types/getInnerListeners.js +0 -1
  102. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +0 -527
  103. package/lib/runtime/components/react/mpx-swiper/index.tsx +0 -80
  104. package/lib/runtime/components/react/mpx-swiper/type.ts +0 -87
  105. package/lib/runtime/components/react/pickerOverlay.tsx +0 -32
  106. package/lib/runtime/components/tenon/getInnerListeners.js +0 -334
  107. package/lib/runtime/components/tenon/tenon-button.vue +0 -309
  108. package/lib/runtime/components/tenon/tenon-image.vue +0 -66
  109. package/lib/runtime/components/tenon/tenon-input.vue +0 -171
  110. package/lib/runtime/components/tenon/tenon-rich-text.vue +0 -26
  111. package/lib/runtime/components/tenon/tenon-scroll-view.vue +0 -127
  112. package/lib/runtime/components/tenon/tenon-switch.vue +0 -96
  113. package/lib/runtime/components/tenon/tenon-text.vue +0 -70
  114. package/lib/runtime/components/tenon/tenon-textarea.vue +0 -86
  115. package/lib/runtime/components/tenon/tenon-view.vue +0 -93
  116. package/lib/runtime/components/web/event.js +0 -105
  117. package/lib/runtime/optionProcessor.tenon.js +0 -84
  118. package/lib/style-compiler/plugins/hm.js +0 -20
  119. package/lib/tenon/index.js +0 -117
  120. package/lib/tenon/processJSON.js +0 -352
  121. package/lib/tenon/processScript.js +0 -203
  122. package/lib/tenon/processStyles.js +0 -21
  123. package/lib/tenon/processTemplate.js +0 -126
  124. package/lib/tenon/script-helper.js +0 -223
  125. package/lib/utils/get-relative-path.js +0 -25
  126. /package/lib/runtime/components/react/types/{common.ts → common.d.ts} +0 -0
@@ -5,7 +5,7 @@
5
5
  * ✔ color
6
6
  */
7
7
  import { Switch, SwitchProps, ViewStyle, NativeSyntheticEvent } from 'react-native'
8
- import { useRef, useEffect, forwardRef, JSX, useState, useContext } from 'react'
8
+ import { useRef, useEffect, forwardRef, JSX, useState, useContext, createElement } from 'react'
9
9
  import { warn } from '@mpxjs/utils'
10
10
  import useNodesRef, { HandlerRef } from './useNodesRef' // 引入辅助函数
11
11
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
@@ -136,22 +136,26 @@ const _Switch = forwardRef<HandlerRef<Switch, _SwitchProps>, _SwitchProps>((prop
136
136
  })
137
137
 
138
138
  if (type === 'checkbox') {
139
- return <CheckBox
140
- {...innerProps}
141
- color={color}
142
- style={normalStyle}
143
- checked={isChecked}
144
- />
139
+ return createElement(
140
+ CheckBox,
141
+ extendObject({}, innerProps, {
142
+ color: color,
143
+ style: normalStyle,
144
+ checked: isChecked
145
+ })
146
+ )
145
147
  }
146
148
 
147
- return <Switch
148
- {...innerProps}
149
- style={normalStyle}
150
- value={isChecked}
151
- trackColor={{ false: '#FFF', true: color }}
152
- thumbColor={isChecked ? '#FFF' : '#f4f3f4'}
153
- ios_backgroundColor="#FFF"
154
- />
149
+ return createElement(
150
+ Switch,
151
+ extendObject({}, innerProps, {
152
+ style: normalStyle,
153
+ value: isChecked,
154
+ trackColor: { false: '#FFF', true: color },
155
+ thumbColor: isChecked ? '#FFF' : '#f4f3f4',
156
+ ios_backgroundColor: '#FFF'
157
+ })
158
+ )
155
159
  })
156
160
 
157
161
  _Switch.displayName = 'MpxSwitch'
@@ -5,7 +5,7 @@
5
5
  * ✘ decode
6
6
  */
7
7
  import { Text, TextStyle, TextProps } from 'react-native'
8
- import { useRef, forwardRef, ReactNode, JSX } from 'react'
8
+ import { useRef, forwardRef, ReactNode, JSX, createElement } from 'react'
9
9
  import useInnerProps from './getInnerListeners'
10
10
  import useNodesRef, { HandlerRef } from './useNodesRef' // 引入辅助函数
11
11
  import { useTransformStyle, wrapChildren } from './utils'
@@ -65,21 +65,13 @@ const _Text = forwardRef<HandlerRef<Text, _TextProps>, _TextProps>((props, ref):
65
65
  layoutRef
66
66
  })
67
67
 
68
- return (
69
- <Text
70
- {...innerProps}
71
- >
72
- {
73
- wrapChildren(
74
- props,
75
- {
76
- hasVarDec,
77
- varContext: varContextRef.current
78
- }
79
- )
80
- }
81
- </Text>
82
- )
68
+ return createElement(Text, innerProps, wrapChildren(
69
+ props,
70
+ {
71
+ hasVarDec,
72
+ varContext: varContextRef.current
73
+ }
74
+ ))
83
75
  })
84
76
 
85
77
  _Text.displayName = 'MpxText'
@@ -9,10 +9,10 @@
9
9
  * ✘ show-confirm-bar
10
10
  * ✔ bindlinechange: No `heightRpx` info.
11
11
  */
12
- import { JSX, forwardRef } from 'react'
12
+ import { JSX, forwardRef, createElement } from 'react'
13
13
  import { Keyboard, TextInput } from 'react-native'
14
14
  import Input, { InputProps, PrivateInputProps } from './mpx-input'
15
- import { omit } from './utils'
15
+ import { omit, extendObject } from './utils'
16
16
  import { HandlerRef } from './useNodesRef'
17
17
 
18
18
  export type TextareProps = Omit<
@@ -29,14 +29,15 @@ const Textarea = forwardRef<HandlerRef<TextInput, TextareProps>, TextareProps>(
29
29
  'multiline',
30
30
  'confirm-hold'
31
31
  ])
32
- return (
33
- <Input
34
- ref={ref}
35
- multiline
36
- confirm-type='next'
37
- bindblur={() => Keyboard.dismiss()}
38
- {...restProps}
39
- />
32
+
33
+ return createElement(
34
+ Input,
35
+ extendObject({
36
+ ref: ref,
37
+ multiline: true,
38
+ confirmType: 'next',
39
+ bindblur: () => Keyboard.dismiss()
40
+ }, restProps)
40
41
  )
41
42
  }
42
43
  )
@@ -5,15 +5,17 @@
5
5
  * ✔ hover-stay-time
6
6
  */
7
7
  import { View, TextStyle, NativeSyntheticEvent, ViewProps, ImageStyle, StyleSheet, Image, LayoutChangeEvent } from 'react-native'
8
- import { useRef, useState, useEffect, forwardRef, ReactNode, JSX } from 'react'
8
+ import { useRef, useState, useEffect, forwardRef, ReactNode, JSX, createElement } from 'react'
9
9
  import useInnerProps from './getInnerListeners'
10
10
  import Animated from 'react-native-reanimated'
11
11
  import useAnimationHooks from './useAnimationHooks'
12
12
  import type { AnimationProp } from './useAnimationHooks'
13
13
  import { ExtendedViewStyle } from './types/common'
14
14
  import useNodesRef, { HandlerRef } from './useNodesRef'
15
- import { parseUrl, PERCENT_REGEX, splitStyle, splitProps, useTransformStyle, wrapChildren, useLayout, renderImage, pickStyle, extendObject } from './utils'
15
+ import { parseUrl, PERCENT_REGEX, splitStyle, splitProps, useTransformStyle, wrapChildren, useLayout, renderImage, pickStyle, extendObject, useHover } from './utils'
16
+ import { error } from '@mpxjs/utils'
16
17
  import LinearGradient from 'react-native-linear-gradient'
18
+ import { GestureDetector, PanGesture } from 'react-native-gesture-handler'
17
19
 
18
20
  export interface _ViewProps extends ViewProps {
19
21
  style?: ExtendedViewStyle
@@ -550,7 +552,7 @@ function inheritStyle (innerStyle: ExtendedViewStyle = {}) {
550
552
  : undefined)
551
553
  }
552
554
 
553
- function wrapImage (imageStyle?: ExtendedViewStyle, innerStyle?: Record<string, any>, enableFastImage?: boolean) {
555
+ function useWrapImage (imageStyle?: ExtendedViewStyle, innerStyle?: Record<string, any>, enableFastImage?: boolean) {
554
556
  // 预处理数据
555
557
  const preImageInfo: PreImageInfo = preParseImage(imageStyle)
556
558
  // 预解析
@@ -641,7 +643,7 @@ function wrapImage (imageStyle?: ExtendedViewStyle, innerStyle?: Record<string,
641
643
 
642
644
  interface WrapChildrenConfig {
643
645
  hasVarDec: boolean
644
- enableBackground: boolean
646
+ enableBackground?: boolean
645
647
  textStyle?: TextStyle
646
648
  backgroundStyle?: ExtendedViewStyle
647
649
  varContext?: Record<string, any>
@@ -659,7 +661,8 @@ function wrapWithChildren (props: _ViewProps, { hasVarDec, enableBackground, tex
659
661
  })
660
662
 
661
663
  return [
662
- enableBackground ? wrapImage(backgroundStyle, innerStyle, enableFastImage) : null,
664
+ // eslint-disable-next-line react-hooks/rules-of-hooks
665
+ enableBackground ? useWrapImage(backgroundStyle, innerStyle, enableFastImage) : null,
663
666
  children
664
667
  ]
665
668
  }
@@ -682,8 +685,6 @@ const _View = forwardRef<HandlerRef<View, _ViewProps>, _ViewProps>((viewProps, r
682
685
  animation
683
686
  } = props
684
687
 
685
- const [isHover, setIsHover] = useState(false)
686
-
687
688
  // 默认样式
688
689
  const defaultStyle: ExtendedViewStyle = style.display === 'flex'
689
690
  ? {
@@ -694,6 +695,9 @@ const _View = forwardRef<HandlerRef<View, _ViewProps>, _ViewProps>((viewProps, r
694
695
  }
695
696
  : {}
696
697
 
698
+ const enableHover = !!hoverStyle
699
+ const { isHover, gesture } = useHover({ enableHover, hoverStartTime, hoverStayTime })
700
+
697
701
  const styleObj: ExtendedViewStyle = extendObject({}, defaultStyle, style, isHover ? hoverStyle as ExtendedViewStyle : {})
698
702
 
699
703
  const {
@@ -716,7 +720,7 @@ const _View = forwardRef<HandlerRef<View, _ViewProps>, _ViewProps>((viewProps, r
716
720
  enableBackground = enableBackground || !!backgroundStyle
717
721
  const enableBackgroundRef = useRef(enableBackground)
718
722
  if (enableBackgroundRef.current !== enableBackground) {
719
- throw new Error('[Mpx runtime error]: background use should be stable in the component lifecycle, or you can set [enable-background] with true.')
723
+ error('[Mpx runtime error]: background use should be stable in the component lifecycle, or you can set [enable-background] with true.')
720
724
  }
721
725
 
722
726
  const nodeRef = useRef(null)
@@ -724,45 +728,6 @@ const _View = forwardRef<HandlerRef<View, _ViewProps>, _ViewProps>((viewProps, r
724
728
  style: normalStyle
725
729
  })
726
730
 
727
- const dataRef = useRef<{
728
- startTimer?: ReturnType<typeof setTimeout>
729
- stayTimer?: ReturnType<typeof setTimeout>
730
- }>({})
731
-
732
- useEffect(() => {
733
- return () => {
734
- dataRef.current.startTimer && clearTimeout(dataRef.current.startTimer)
735
- dataRef.current.stayTimer && clearTimeout(dataRef.current.stayTimer)
736
- }
737
- }, [])
738
-
739
- const setStartTimer = () => {
740
- dataRef.current.startTimer && clearTimeout(dataRef.current.startTimer)
741
- dataRef.current.startTimer = setTimeout(() => {
742
- setIsHover(true)
743
- }, +hoverStartTime)
744
- }
745
-
746
- const setStayTimer = () => {
747
- dataRef.current.stayTimer && clearTimeout(dataRef.current.stayTimer)
748
- dataRef.current.startTimer && clearTimeout(dataRef.current.startTimer)
749
- dataRef.current.stayTimer = setTimeout(() => {
750
- setIsHover(false)
751
- }, +hoverStayTime)
752
- }
753
-
754
- function onTouchStart (e: NativeSyntheticEvent<TouchEvent>) {
755
- const { bindtouchstart } = props
756
- bindtouchstart && bindtouchstart(e)
757
- setStartTimer()
758
- }
759
-
760
- function onTouchEnd (e: NativeSyntheticEvent<TouchEvent>) {
761
- const { bindtouchend } = props
762
- bindtouchend && bindtouchend(e)
763
- setStayTimer()
764
- }
765
-
766
731
  const {
767
732
  layoutRef,
768
733
  layoutStyle,
@@ -771,30 +736,19 @@ const _View = forwardRef<HandlerRef<View, _ViewProps>, _ViewProps>((viewProps, r
771
736
 
772
737
  const viewStyle = extendObject({}, innerStyle, layoutStyle)
773
738
 
774
- enableAnimation = enableAnimation || !!animation
775
- const enableAnimationRef = useRef(enableAnimation)
776
- if (enableAnimationRef.current !== enableAnimation) {
777
- throw new Error('[Mpx runtime error]: animation use should be stable in the component lifecycle, or you can set [enable-animation] with true.')
778
- }
779
- const finalStyle = enableAnimation
780
- ? useAnimationHooks({
781
- animation,
782
- style: viewStyle
783
- })
784
- : viewStyle
739
+ const { enableStyleAnimation, animationStyle } = useAnimationHooks({
740
+ enableAnimation,
741
+ animation,
742
+ style: viewStyle
743
+ })
744
+
785
745
  const innerProps = useInnerProps(
786
746
  props,
787
747
  extendObject({
788
748
  ref: nodeRef,
789
- style: finalStyle
749
+ style: enableStyleAnimation ? [viewStyle, animationStyle] : viewStyle
790
750
  },
791
- layoutProps,
792
- hoverStyle
793
- ? {
794
- bindtouchstart: onTouchStart,
795
- bindtouchend: onTouchEnd
796
- }
797
- : {}
751
+ layoutProps
798
752
  ), [
799
753
  'hover-start-time',
800
754
  'hover-stay-time',
@@ -814,17 +768,14 @@ const _View = forwardRef<HandlerRef<View, _ViewProps>, _ViewProps>((viewProps, r
814
768
  innerStyle,
815
769
  enableFastImage
816
770
  })
817
- return enableAnimation
818
- ? (<Animated.View
819
- {...innerProps}
820
- >
821
- {childNode}
822
- </Animated.View>)
823
- : (<View
824
- {...innerProps}
825
- >
826
- {childNode}
827
- </View>)
771
+
772
+ const BaseComponent = enableStyleAnimation
773
+ ? createElement(Animated.View, innerProps, childNode)
774
+ : createElement(View, innerProps, childNode)
775
+
776
+ return enableHover
777
+ ? createElement(GestureDetector, { gesture: gesture as PanGesture }, BaseComponent)
778
+ : BaseComponent
828
779
  })
829
780
 
830
781
  _View.displayName = 'MpxView'
@@ -1,6 +1,5 @@
1
- import { forwardRef, JSX, useEffect, useRef, useContext, useMemo } from 'react'
2
- import { noop, warn } from '@mpxjs/utils'
3
- import { View } from 'react-native'
1
+ import { forwardRef, JSX, useRef, useContext, useMemo, createElement } from 'react'
2
+ import { warn, getFocusedNavigation, isFunction } from '@mpxjs/utils'
4
3
  import { Portal } from '@ant-design/react-native'
5
4
  import { getCustomEvent } from './getInnerListeners'
6
5
  import { promisify, redirectTo, navigateTo, navigateBack, reLaunch, switchTab } from '@mpxjs/api-proxy'
@@ -31,26 +30,25 @@ interface WebViewProps {
31
30
  }
32
31
 
33
32
  interface PayloadData {
34
- data?: Record<string, any>
33
+ [x: string]: any
35
34
  }
36
35
 
37
36
  type MessageData = {
38
37
  payload?: PayloadData,
38
+ args?: Array<any>,
39
39
  type?: string,
40
40
  callbackId?: number
41
41
  }
42
42
 
43
- const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((props, ref): JSX.Element => {
44
- const { src, bindmessage = noop, bindload = noop, binderror = noop } = props
45
- if (!src) {
46
- return (<View></View>)
47
- }
43
+ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((props, ref): JSX.Element | null => {
44
+ const { src, bindmessage, bindload, binderror } = props
45
+ const mpx = global.__mpx
48
46
  if (props.style) {
49
47
  warn('The web-view component does not support the style prop.')
50
48
  }
51
49
  const pageId = useContext(RouteContext)
52
50
  const currentPage = useMemo(() => getCurrentPage(pageId), [pageId])
53
-
51
+ const webViewRef = useRef<WebView>(null)
54
52
  const defaultWebViewStyle = {
55
53
  position: 'absolute' as 'absolute' | 'relative' | 'static',
56
54
  left: 0 as number,
@@ -59,34 +57,14 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
59
57
  bottom: 0 as number
60
58
  }
61
59
 
62
- const webViewRef = useRef<WebView>(null)
63
60
  useNodesRef<WebView, WebViewProps>(props, ref, webViewRef, {
64
61
  style: defaultWebViewStyle
65
62
  })
66
63
 
67
- const _messageList = useRef<any[]>([])
68
- const handleUnload = () => {
69
- // 这里是 WebView 销毁前执行的逻辑
70
- bindmessage(getCustomEvent('messsage', {}, {
71
- detail: {
72
- data: _messageList.current
73
- },
74
- layoutRef: webViewRef
75
- }))
64
+ if (!src) {
65
+ return null
76
66
  }
77
67
 
78
- useEffect(() => {
79
- if (currentPage) {
80
- currentPage.__webViewUrl = src
81
- }
82
- }, [src, currentPage])
83
-
84
- useEffect(() => {
85
- // 组件卸载时执行
86
- return () => {
87
- handleUnload()
88
- }
89
- }, [])
90
68
  const _load = function (res: WebViewNavigationEvent) {
91
69
  const result = {
92
70
  type: 'load',
@@ -107,8 +85,33 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
107
85
  }
108
86
  binderror(result)
109
87
  }
88
+ const injectedJavaScript = `
89
+ if (window.ReactNativeWebView && window.ReactNativeWebView.postMessage) {
90
+ var _documentTitle = document.title;
91
+ window.ReactNativeWebView.postMessage(JSON.stringify({
92
+ type: 'setTitle',
93
+ payload: {
94
+ _documentTitle: _documentTitle
95
+ }
96
+ }))
97
+ Object.defineProperty(document, 'title', {
98
+ set (val) {
99
+ _documentTitle = val
100
+ window.ReactNativeWebView.postMessage(JSON.stringify({
101
+ type: 'setTitle',
102
+ payload: {
103
+ _documentTitle: _documentTitle
104
+ }
105
+ }))
106
+ },
107
+ get () {
108
+ return _documentTitle
109
+ }
110
+ });
111
+ }
112
+ `
110
113
  const _changeUrl = function (navState: WebViewNavigation) {
111
- if (currentPage) {
114
+ if (navState.navigationType) { // navigationType这个事件在页面开始加载时和页面加载完成时都会被触发所以判断这个避免其他无效触发执行该逻辑
112
115
  currentPage.__webViewUrl = navState.url
113
116
  }
114
117
  }
@@ -121,43 +124,79 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
121
124
  if (typeof nativeEventData === 'string') {
122
125
  data = JSON.parse(nativeEventData)
123
126
  }
124
- } catch (e) {
125
- data = {}
126
- }
127
+ } catch (e) {}
128
+ const args = data.args
127
129
  const postData: PayloadData = data.payload || {}
128
- switch (data.type) {
130
+ const params = Array.isArray(args) ? args : [postData]
131
+ const type = data.type
132
+ switch (type) {
133
+ case 'setTitle':
134
+ { // case下不允许直接声明,包个块解决该问题
135
+ const title = postData._documentTitle
136
+ if (title) {
137
+ const navigation = getFocusedNavigation()
138
+ navigation && navigation.setOptions({ title })
139
+ }
140
+ }
141
+ break
129
142
  case 'postMessage':
130
- _messageList.current.push(postData.data)
143
+ bindmessage && bindmessage(getCustomEvent('messsage', {}, { // RN组件销毁顺序与小程序不一致,所以改成和支付宝消息一致
144
+ detail: {
145
+ data: params[0]?.data
146
+ }
147
+ }))
131
148
  asyncCallback = Promise.resolve({
132
149
  errMsg: 'invokeWebappApi:ok'
133
150
  })
134
151
  break
135
152
  case 'navigateTo':
136
- asyncCallback = navObj.navigateTo(postData)
153
+ asyncCallback = navObj.navigateTo(...params)
137
154
  break
138
155
  case 'navigateBack':
139
- asyncCallback = navObj.navigateBack(postData)
156
+ asyncCallback = navObj.navigateBack(...params)
140
157
  break
141
158
  case 'redirectTo':
142
- asyncCallback = navObj.redirectTo(postData)
159
+ asyncCallback = navObj.redirectTo(...params)
143
160
  break
144
161
  case 'switchTab':
145
- asyncCallback = navObj.switchTab(postData)
162
+ asyncCallback = navObj.switchTab(...params)
146
163
  break
147
164
  case 'reLaunch':
148
- asyncCallback = navObj.reLaunch(postData)
165
+ asyncCallback = navObj.reLaunch(...params)
166
+ break
167
+ default:
168
+ if (type) {
169
+ const implement = mpx.config.webviewConfig.apiImplementations && mpx.config.webviewConfig.apiImplementations[type]
170
+ if (isFunction(implement)) {
171
+ asyncCallback = Promise.resolve(implement(...params))
172
+ } else {
173
+ /* eslint-disable prefer-promise-reject-errors */
174
+ asyncCallback = Promise.reject({
175
+ errMsg: `未在apiImplementations中配置${type}方法`
176
+ })
177
+ }
178
+ }
149
179
  break
150
180
  }
151
181
 
152
182
  asyncCallback && asyncCallback.then((res: any) => {
153
183
  if (webViewRef.current?.postMessage) {
154
184
  const test = JSON.stringify({
155
- type: data.type,
185
+ type,
156
186
  callbackId: data.callbackId,
157
187
  result: res
158
188
  })
159
189
  webViewRef.current.postMessage(test)
160
190
  }
191
+ }).catch((error: any) => {
192
+ if (webViewRef.current?.postMessage) {
193
+ const test = JSON.stringify({
194
+ type,
195
+ callbackId: data.callbackId,
196
+ error
197
+ })
198
+ webViewRef.current.postMessage(test)
199
+ }
161
200
  })
162
201
  }
163
202
  const events = {}
@@ -172,21 +211,17 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
172
211
  onError: _error
173
212
  })
174
213
  }
175
- if (bindmessage) {
176
- extendObject(events, {
177
- onMessage: _message
178
- })
179
- }
180
- return (<Portal>
181
- <WebView
182
- style={defaultWebViewStyle}
183
- source={{ uri: src }}
184
- ref={webViewRef}
185
- {...events}
186
- onNavigationStateChange={_changeUrl}
187
- javaScriptEnabled={true}
188
- ></WebView>
189
- </Portal>)
214
+ extendObject(events, {
215
+ onMessage: _message
216
+ })
217
+
218
+ return createElement(Portal, null, createElement(WebView, extendObject({
219
+ style: defaultWebViewStyle,
220
+ source: { uri: src },
221
+ ref: webViewRef,
222
+ javaScriptEnabled: true,
223
+ onNavigationStateChange: _changeUrl
224
+ }, events)))
190
225
  })
191
226
 
192
227
  _WebView.displayName = 'MpxWebview'
@@ -8,6 +8,7 @@ export type Faces = {
8
8
  deg: number
9
9
  offsetY: number
10
10
  opacity: number
11
+ scale: number
11
12
  screenHeight: number
12
13
  }
13
14
 
@@ -33,7 +34,7 @@ export const createFaces = (
33
34
  const maxStep = Math.trunc((visibleCount + 2) / 2) // + 2 because there are 2 more faces at 90 degrees
34
35
  const stepDegree = 90 / maxStep
35
36
 
36
- const result = []
37
+ const result: number[] = []
37
38
  for (let i = 1; i <= maxStep; i++) {
38
39
  result.push(i * stepDegree)
39
40
  }
@@ -62,17 +63,17 @@ export const createFaces = (
62
63
  const getOpacity = (index: number) => {
63
64
  const map: Record<number, number> = {
64
65
  0: 0,
65
- 1: 0.2,
66
- 2: 0.35,
67
- 3: 0.45,
68
- 4: 0.5
66
+ 1: 0.8,
67
+ 2: 0.9
69
68
  }
70
- return map[index] ?? Math.min(1, map[4] + index * 0.5)
69
+ return map[index] ?? Math.min(1, map[2] + index * 0.05)
71
70
  }
72
71
 
73
72
  const degrees = getDegreesRelativeCenter()
74
73
  const [screenHeight, offsets] = getScreenHeightsAndOffsets(degrees)
75
74
 
75
+ const scales = [0.973, 0.9, 0.8]
76
+
76
77
  return [
77
78
  // top items
78
79
  ...degrees
@@ -82,13 +83,14 @@ export const createFaces = (
82
83
  deg: degree,
83
84
  opacity: getOpacity(degrees.length - 1 - index),
84
85
  offsetY: -1 * offsets[index],
86
+ scale: scales[index],
85
87
  screenHeight: screenHeight[index]
86
88
  }
87
89
  })
88
90
  .reverse(),
89
91
 
90
92
  // center item
91
- { index: 0, deg: 0, opacity: 1, offsetY: 0, screenHeight: itemHeight },
93
+ { index: 0, deg: 0, opacity: 1, offsetY: 0, scale: 1, screenHeight: itemHeight },
92
94
 
93
95
  // bottom items
94
96
  ...degrees.map<Faces>((degree, index) => {
@@ -97,6 +99,7 @@ export const createFaces = (
97
99
  deg: -1 * degree,
98
100
  opacity: getOpacity(degrees.length - 1 - index),
99
101
  offsetY: offsets[index],
102
+ scale: scales[index],
100
103
  screenHeight: screenHeight[index]
101
104
  }
102
105
  })
@@ -0,0 +1,27 @@
1
+ import { createContext, useContext } from 'react'
2
+ import { SharedValue } from 'react-native-reanimated'
3
+
4
+ type ContextValue = SharedValue<number>
5
+
6
+ export const PickerViewColumnAnimationContext = createContext<
7
+ ContextValue | undefined
8
+ >(undefined)
9
+
10
+ export const usePickerViewColumnAnimationContext = () => {
11
+ const value = useContext(PickerViewColumnAnimationContext)
12
+ if (value === undefined) {
13
+ throw new Error(
14
+ 'usePickerViewColumnAnimationContext must be called from within PickerViewColumnAnimationContext.Provider!'
15
+ )
16
+ }
17
+ return value
18
+ }
19
+
20
+ export const PickerViewStyleContext = createContext<
21
+ Record<string, any> | undefined
22
+ >(undefined)
23
+
24
+ export const usePickerViewStyleContext = () => {
25
+ const value = useContext(PickerViewStyleContext)
26
+ return value
27
+ }
@@ -0,0 +1,34 @@
1
+ import React from 'react'
2
+ import { StyleProp, StyleSheet, View, ViewStyle } from 'react-native'
3
+
4
+ type IndicatorProps = {
5
+ itemHeight: number
6
+ indicatorItemStyle?: StyleProp<ViewStyle>
7
+ indicatorContainerStyle?: StyleProp<ViewStyle>
8
+ }
9
+
10
+ const _PickerViewIndicator = ({ itemHeight, indicatorItemStyle, indicatorContainerStyle }: IndicatorProps) => {
11
+ return (
12
+ <View style={[styles.indicatorContainer, indicatorContainerStyle]} pointerEvents={'none'}>
13
+ <View style={[styles.selection, { height: itemHeight }, indicatorItemStyle]} />
14
+ </View>
15
+ )
16
+ }
17
+
18
+ const styles = StyleSheet.create({
19
+ indicatorContainer: {
20
+ ...StyleSheet.absoluteFillObject,
21
+ justifyContent: 'center',
22
+ alignItems: 'center',
23
+ zIndex: 200
24
+ },
25
+ selection: {
26
+ borderTopWidth: 1,
27
+ borderBottomWidth: 1,
28
+ borderColor: 'rgba(0, 0, 0, 0.05)',
29
+ alignSelf: 'stretch'
30
+ }
31
+ })
32
+
33
+ _PickerViewIndicator.displayName = 'MpxPickerViewIndicator'
34
+ export default _PickerViewIndicator