@mpxjs/webpack-plugin 2.10.6-beta.2 → 2.10.6-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +22 -10
- package/lib/platform/index.js +2 -4
- package/lib/platform/template/wx/component-config/index.js +2 -2
- package/lib/platform/template/wx/component-config/template.js +1 -26
- package/lib/platform/template/wx/index.js +3 -10
- package/lib/react/LoadAsyncChunkModule.js +68 -0
- package/lib/react/index.js +3 -1
- package/lib/react/processJSON.js +6 -6
- package/lib/react/processScript.js +4 -3
- package/lib/react/script-helper.js +97 -18
- package/lib/runtime/components/react/AsyncContainer.tsx +189 -0
- package/lib/runtime/components/react/getInnerListeners.ts +1 -1
- package/lib/runtime/components/react/mpx-input.tsx +1 -1
- package/lib/runtime/components/react/mpx-movable-view.tsx +2 -2
- package/lib/runtime/components/react/mpx-swiper-item.tsx +2 -2
- package/lib/runtime/components/react/mpx-swiper.tsx +53 -18
- package/lib/runtime/components/react/mpx-web-view.tsx +13 -33
- package/lib/runtime/components/react/types/global.d.ts +15 -0
- package/lib/runtime/optionProcessor.js +2 -2
- package/lib/template-compiler/compiler.js +9 -59
- package/lib/utils/dom-tag-config.js +15 -1
- package/lib/web/index.js +0 -2
- package/lib/web/processScript.js +7 -29
- package/lib/web/processTemplate.js +4 -10
- package/lib/web/script-helper.js +1 -1
- package/package.json +1 -1
- package/lib/dependencies/WriteVfsDependency.js +0 -46
- package/lib/utils/get-template-content.js +0 -47
- package/lib/web/template2vue.js +0 -280
- package/lib/web/wxml-template-loader.js +0 -29
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { ComponentType, ReactNode, Component, Fragment, Suspense } from 'react'
|
|
2
|
+
import { View, Text, Image, StyleSheet, TouchableOpacity } from 'react-native'
|
|
3
|
+
import FastImage from '@d11/react-native-fast-image'
|
|
4
|
+
|
|
5
|
+
const styles = StyleSheet.create({
|
|
6
|
+
container: {
|
|
7
|
+
flex: 1,
|
|
8
|
+
padding: 20,
|
|
9
|
+
backgroundColor: '#fff'
|
|
10
|
+
},
|
|
11
|
+
loadingImage: {
|
|
12
|
+
width: 100,
|
|
13
|
+
height: 100,
|
|
14
|
+
marginTop: 220,
|
|
15
|
+
alignSelf: 'center'
|
|
16
|
+
},
|
|
17
|
+
buttonText: {
|
|
18
|
+
color: '#fff',
|
|
19
|
+
fontSize: 16,
|
|
20
|
+
fontWeight: '500',
|
|
21
|
+
textAlign: 'center'
|
|
22
|
+
},
|
|
23
|
+
errorImage: {
|
|
24
|
+
marginTop: 80,
|
|
25
|
+
width: 220,
|
|
26
|
+
aspectRatio: 1,
|
|
27
|
+
alignSelf: 'center'
|
|
28
|
+
},
|
|
29
|
+
errorText: {
|
|
30
|
+
fontSize: 16,
|
|
31
|
+
textAlign: 'center',
|
|
32
|
+
color: '#333',
|
|
33
|
+
marginBottom: 20
|
|
34
|
+
},
|
|
35
|
+
retryButton: {
|
|
36
|
+
position: 'absolute',
|
|
37
|
+
bottom: 54,
|
|
38
|
+
left: 20,
|
|
39
|
+
right: 20,
|
|
40
|
+
backgroundColor: '#fff',
|
|
41
|
+
paddingVertical: 15,
|
|
42
|
+
borderRadius: 30,
|
|
43
|
+
marginTop: 40,
|
|
44
|
+
borderWidth: 1,
|
|
45
|
+
borderColor: '#FF5F00'
|
|
46
|
+
},
|
|
47
|
+
retryButtonText: {
|
|
48
|
+
color: '#FF5F00',
|
|
49
|
+
fontSize: 16,
|
|
50
|
+
fontWeight: '500',
|
|
51
|
+
textAlign: 'center'
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
type AsyncType = 'page' | 'component'
|
|
56
|
+
|
|
57
|
+
interface PropsType<T extends AsyncType> {
|
|
58
|
+
type: T
|
|
59
|
+
props: object
|
|
60
|
+
loading: ComponentType<unknown>
|
|
61
|
+
fallback: ComponentType<unknown>
|
|
62
|
+
children: (props: unknown) => ReactNode
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface StateType {
|
|
66
|
+
hasError: boolean,
|
|
67
|
+
key: number
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface ComponentError extends Error {
|
|
71
|
+
request?: string
|
|
72
|
+
type: 'timeout' | 'fail'
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const DefaultLoading = () => {
|
|
76
|
+
return (
|
|
77
|
+
<View style={styles.container}>
|
|
78
|
+
<FastImage
|
|
79
|
+
style={styles.loadingImage}
|
|
80
|
+
source={{ uri: 'https://dpubstatic.udache.com/static/dpubimg/439jiCVOtNOnEv9F2LaDs_loading.gif' }}
|
|
81
|
+
resizeMode={FastImage.resizeMode.contain}></FastImage>
|
|
82
|
+
</View>
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface DefaultFallbackProps {
|
|
87
|
+
onReload: () => void
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const DefaultFallback = ({ onReload }: DefaultFallbackProps) => {
|
|
91
|
+
return (
|
|
92
|
+
<View style={styles.container}>
|
|
93
|
+
<Image
|
|
94
|
+
source={{ uri: 'https://dpubstatic.udache.com/static/dpubimg/Vak5mZvezPpKV5ZJI6P9b_drn-fallbak.png' }}
|
|
95
|
+
style={styles.errorImage}
|
|
96
|
+
resizeMode="contain"
|
|
97
|
+
/>
|
|
98
|
+
<Text style={styles.errorText}>网络出了点问题,请查看网络环境</Text>
|
|
99
|
+
<TouchableOpacity
|
|
100
|
+
style={styles.retryButton}
|
|
101
|
+
onPress={onReload}
|
|
102
|
+
activeOpacity={0.7}
|
|
103
|
+
>
|
|
104
|
+
<Text style={styles.retryButtonText}>点击重试</Text>
|
|
105
|
+
</TouchableOpacity>
|
|
106
|
+
</View>
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export default class AsyncContainer extends Component<PropsType<AsyncType>, StateType> {
|
|
111
|
+
private suspenseFallback: ReactNode
|
|
112
|
+
private errorFallback: ReactNode
|
|
113
|
+
|
|
114
|
+
constructor (props: PropsType<AsyncType>) {
|
|
115
|
+
super(props)
|
|
116
|
+
this.state = {
|
|
117
|
+
hasError: false,
|
|
118
|
+
key: 0
|
|
119
|
+
}
|
|
120
|
+
this.suspenseFallback = this.getSuspenseFallback()
|
|
121
|
+
this.errorFallback = this.getErrorFallback()
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// render 阶段收集到的错误
|
|
125
|
+
static getDerivedStateFromError (error: ComponentError): StateType | undefined {
|
|
126
|
+
if (error.name === 'ChunkLoadError') {
|
|
127
|
+
return {
|
|
128
|
+
hasError: true,
|
|
129
|
+
key: 0
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
// 被外层捕获
|
|
133
|
+
throw error
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
componentDidCatch (error: ComponentError): void {
|
|
138
|
+
if (error.name === 'ChunkLoadError' && this.props.type === 'component') {
|
|
139
|
+
const request = error.request || ''
|
|
140
|
+
const subpackage = request.split('/').filter((i: string) => !!i)[0]
|
|
141
|
+
global.onLazyLoadError({
|
|
142
|
+
type: 'subpackage',
|
|
143
|
+
subpackage: [subpackage],
|
|
144
|
+
errMsg: `loadSubpackage: ${error.type}`
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
reloadPage () {
|
|
150
|
+
this.setState((prevState) => {
|
|
151
|
+
return {
|
|
152
|
+
hasError: false,
|
|
153
|
+
key: prevState.key + 1
|
|
154
|
+
}
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
getSuspenseFallback () {
|
|
159
|
+
if (this.props.type === 'page') {
|
|
160
|
+
const Fallback = this.props.loading || DefaultLoading
|
|
161
|
+
return <Fallback />
|
|
162
|
+
} else {
|
|
163
|
+
const Fallback = this.props.loading
|
|
164
|
+
return <Fallback {...this.props.props}></Fallback>
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
getErrorFallback () {
|
|
169
|
+
if (this.props.type === 'page') {
|
|
170
|
+
const Fallback = this.props.fallback as ComponentType<DefaultFallbackProps> || DefaultFallback
|
|
171
|
+
return <Fallback onReload={this.reloadPage.bind(this)}></Fallback>
|
|
172
|
+
} else {
|
|
173
|
+
const Fallback = this.props.loading
|
|
174
|
+
return <Fallback {...this.props.props}></Fallback>
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
render () {
|
|
179
|
+
if (this.state.hasError) {
|
|
180
|
+
return this.errorFallback
|
|
181
|
+
} else {
|
|
182
|
+
return (
|
|
183
|
+
<Suspense fallback={this.suspenseFallback} key={this.state.key}>
|
|
184
|
+
{this.props.children(this.props.props)}
|
|
185
|
+
</Suspense>
|
|
186
|
+
)
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -24,7 +24,7 @@ const getTouchEvent = (
|
|
|
24
24
|
) => {
|
|
25
25
|
const { navigation, propsRef, layoutRef } = config
|
|
26
26
|
const props = propsRef.current
|
|
27
|
-
const {
|
|
27
|
+
const { top: navigationY = 0 } = navigation?.layout || {}
|
|
28
28
|
const nativeEvent = event.nativeEvent
|
|
29
29
|
const { timestamp, pageX, pageY, touches, changedTouches } = nativeEvent
|
|
30
30
|
const { id } = props
|
|
@@ -218,7 +218,7 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
|
|
|
218
218
|
const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
|
|
219
219
|
|
|
220
220
|
useEffect(() => {
|
|
221
|
-
if (
|
|
221
|
+
if (value !== tmpValue.current) {
|
|
222
222
|
const parsed = parseValue(value)
|
|
223
223
|
tmpValue.current = parsed
|
|
224
224
|
setInputValue(parsed)
|
|
@@ -327,7 +327,7 @@ const _MovableView = forwardRef<HandlerRef<View, MovableViewProps>, MovableViewP
|
|
|
327
327
|
setHeight(height || 0)
|
|
328
328
|
}
|
|
329
329
|
nodeRef.current?.measure((x: number, y: number, width: number, height: number) => {
|
|
330
|
-
const {
|
|
330
|
+
const { top: navigationY = 0 } = navigation?.layout || {}
|
|
331
331
|
layoutRef.current = { x, y: y - navigationY, width, height, offsetLeft: 0, offsetTop: 0 }
|
|
332
332
|
resetBoundaryAndCheck({ width, height })
|
|
333
333
|
})
|
|
@@ -335,7 +335,7 @@ const _MovableView = forwardRef<HandlerRef<View, MovableViewProps>, MovableViewP
|
|
|
335
335
|
}
|
|
336
336
|
|
|
337
337
|
const extendEvent = useCallback((e: any, type: 'start' | 'move' | 'end') => {
|
|
338
|
-
const {
|
|
338
|
+
const { top: navigationY = 0 } = navigation?.layout || {}
|
|
339
339
|
const touchArr = [e.changedTouches, e.allTouches]
|
|
340
340
|
touchArr.forEach(touches => {
|
|
341
341
|
touches && touches.forEach((item: { absoluteX: number; absoluteY: number; pageX: number; pageY: number; clientX: number; clientY: number }) => {
|
|
@@ -3,7 +3,7 @@ import Animated, { useAnimatedStyle, interpolate, SharedValue } from 'react-nati
|
|
|
3
3
|
import { ReactNode, forwardRef, useRef, useContext, createElement } from 'react'
|
|
4
4
|
import useInnerProps from './getInnerListeners'
|
|
5
5
|
import useNodesRef, { HandlerRef } from './useNodesRef' // 引入辅助函数
|
|
6
|
-
import { useTransformStyle, splitStyle, splitProps, wrapChildren, useLayout, extendObject } from './utils'
|
|
6
|
+
import { useTransformStyle, splitStyle, splitProps, wrapChildren, useLayout, extendObject, isHarmony } from './utils'
|
|
7
7
|
import { SwiperContext } from './context'
|
|
8
8
|
|
|
9
9
|
interface SwiperItemProps {
|
|
@@ -80,7 +80,7 @@ const _SwiperItem = forwardRef<HandlerRef<View, SwiperItemProps>, SwiperItemProp
|
|
|
80
80
|
],
|
|
81
81
|
{ layoutRef })
|
|
82
82
|
const itemAnimatedStyle = useAnimatedStyle(() => {
|
|
83
|
-
if (!step.value) return {}
|
|
83
|
+
if (!step.value && !isHarmony) return {}
|
|
84
84
|
const inputRange = [step.value, 0]
|
|
85
85
|
const outputRange = [0.7, 1]
|
|
86
86
|
// 实现元素的宽度跟随step从0到真实宽度,且不能触发重新渲染整个组件,通过AnimatedStyle的方式实现
|
|
@@ -23,6 +23,7 @@ import Portal from './mpx-portal'
|
|
|
23
23
|
* ✔ easing-function ="easeOutCubic"
|
|
24
24
|
* ✘ display-multiple-items
|
|
25
25
|
* ✘ snap-to-edge
|
|
26
|
+
* ✔ disableGesture
|
|
26
27
|
*/
|
|
27
28
|
type EaseType = 'default' | 'linear' | 'easeInCubic' | 'easeOutCubic' | 'easeInOutCubic'
|
|
28
29
|
type StrAbsoType = 'absoluteX' | 'absoluteY'
|
|
@@ -206,6 +207,10 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
|
|
|
206
207
|
const moveTranstion = useSharedValue(0)
|
|
207
208
|
// 记录从onBegin 到 onTouchesUp 的时间
|
|
208
209
|
const moveTime = useSharedValue(0)
|
|
210
|
+
// 记录从onBegin 到 onTouchesCancelled 另外一个方向移动的距离
|
|
211
|
+
const anotherDirectionMove = useSharedValue(0)
|
|
212
|
+
// 另一个方向的
|
|
213
|
+
const anotherAbso = 'absolute' + (dir === 'x' ? 'y' : 'x').toUpperCase() as StrAbsoType
|
|
209
214
|
const timerId = useRef(0 as number | ReturnType<typeof setTimeout>)
|
|
210
215
|
const intervalTimer = props.interval || 500
|
|
211
216
|
|
|
@@ -504,7 +509,11 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
|
|
|
504
509
|
}, [children.length])
|
|
505
510
|
|
|
506
511
|
useEffect(() => {
|
|
507
|
-
|
|
512
|
+
// 1. 如果用户在touch的过程中, 外部更新了current以外部为准(小程序表现)
|
|
513
|
+
// 2. 手指滑动过程中更新索引,外部会把current再穿进来,导致offset直接更新了
|
|
514
|
+
if (props.current !== currentIndex.value) {
|
|
515
|
+
updateCurrent(props.current || 0, step.value)
|
|
516
|
+
}
|
|
508
517
|
}, [props.current])
|
|
509
518
|
|
|
510
519
|
useEffect(() => {
|
|
@@ -566,18 +575,25 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
|
|
|
566
575
|
targetOffset: -moveToTargetPos
|
|
567
576
|
}
|
|
568
577
|
}
|
|
569
|
-
function
|
|
578
|
+
function checkUnCircular (eventData: EventDataType) {
|
|
570
579
|
'worklet'
|
|
571
580
|
const { translation } = eventData
|
|
572
581
|
const currentOffset = Math.abs(offset.value)
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
582
|
+
// 向右滑动swiper
|
|
583
|
+
if (translation < 0) {
|
|
584
|
+
const boundaryOffset = step.value * (childrenLength.value - 1)
|
|
585
|
+
const gestureMovePos = Math.abs(translation) + currentOffset
|
|
586
|
+
return {
|
|
587
|
+
// 防止快速连续向右滑动时,手势移动的距离 加 当前的offset超出边界
|
|
588
|
+
targetOffset: gestureMovePos > boundaryOffset ? -boundaryOffset : offset.value + translation,
|
|
589
|
+
canMove: currentOffset < boundaryOffset
|
|
578
590
|
}
|
|
579
591
|
} else {
|
|
580
|
-
|
|
592
|
+
const gestureMovePos = currentOffset - translation
|
|
593
|
+
return {
|
|
594
|
+
targetOffset: gestureMovePos < 0 ? 0 : offset.value + translation,
|
|
595
|
+
canMove: currentOffset > 0
|
|
596
|
+
}
|
|
581
597
|
}
|
|
582
598
|
}
|
|
583
599
|
function handleEnd (eventData: EventDataType) {
|
|
@@ -636,7 +652,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
|
|
|
636
652
|
}
|
|
637
653
|
})
|
|
638
654
|
}
|
|
639
|
-
function
|
|
655
|
+
function computeHalf () {
|
|
640
656
|
'worklet'
|
|
641
657
|
const currentOffset = Math.abs(offset.value)
|
|
642
658
|
let preOffset = (currentIndex.value + patchElmNumShared.value) * step.value
|
|
@@ -646,6 +662,14 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
|
|
|
646
662
|
// 正常事件中拿到的transition值(正向滑动<0,倒着滑>0)
|
|
647
663
|
const diffOffset = preOffset - currentOffset
|
|
648
664
|
const half = Math.abs(diffOffset) > step.value / 2
|
|
665
|
+
return {
|
|
666
|
+
diffOffset,
|
|
667
|
+
half
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
function handleLongPress () {
|
|
671
|
+
'worklet'
|
|
672
|
+
const { diffOffset, half } = computeHalf()
|
|
649
673
|
if (+diffOffset === 0) {
|
|
650
674
|
runOnJS(resumeLoop)()
|
|
651
675
|
} else if (half) {
|
|
@@ -701,18 +725,29 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
|
|
|
701
725
|
runOnJS(pauseLoop)()
|
|
702
726
|
preAbsolutePos.value = e[strAbso]
|
|
703
727
|
moveTranstion.value = e[strAbso]
|
|
728
|
+
anotherDirectionMove.value = e[anotherAbso]
|
|
704
729
|
moveTime.value = new Date().getTime()
|
|
705
730
|
})
|
|
706
|
-
.
|
|
731
|
+
.onUpdate((e) => {
|
|
707
732
|
'worklet'
|
|
708
733
|
if (touchfinish.value) return
|
|
709
|
-
const
|
|
710
|
-
const moveDistance = touchEventData[strAbso] - preAbsolutePos.value
|
|
734
|
+
const moveDistance = e[strAbso] - preAbsolutePos.value
|
|
711
735
|
const eventData = {
|
|
712
736
|
translation: moveDistance
|
|
713
737
|
}
|
|
714
|
-
//
|
|
715
|
-
|
|
738
|
+
// 1. 在Move过程中,如果手指一直没抬起来,超过一半的话也会更新索引
|
|
739
|
+
const { half } = computeHalf()
|
|
740
|
+
if (half) {
|
|
741
|
+
const { selectedIndex } = getTargetPosition(eventData)
|
|
742
|
+
currentIndex.value = selectedIndex
|
|
743
|
+
}
|
|
744
|
+
// 2. 处理用户一直拖拽到临界点的场景, 不会执行onEnd
|
|
745
|
+
const { canMove, targetOffset } = checkUnCircular(eventData)
|
|
746
|
+
if (!circularShared.value) {
|
|
747
|
+
if (canMove) {
|
|
748
|
+
offset.value = targetOffset
|
|
749
|
+
preAbsolutePos.value = e[strAbso]
|
|
750
|
+
}
|
|
716
751
|
return
|
|
717
752
|
}
|
|
718
753
|
const { isBoundary, resetOffset } = reachBoundary(eventData)
|
|
@@ -721,13 +756,12 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
|
|
|
721
756
|
} else {
|
|
722
757
|
offset.value = moveDistance + offset.value
|
|
723
758
|
}
|
|
724
|
-
preAbsolutePos.value =
|
|
759
|
+
preAbsolutePos.value = e[strAbso]
|
|
725
760
|
})
|
|
726
|
-
.
|
|
761
|
+
.onFinalize((e) => {
|
|
727
762
|
'worklet'
|
|
728
763
|
if (touchfinish.value) return
|
|
729
|
-
const
|
|
730
|
-
const moveDistance = touchEventData[strAbso] - moveTranstion.value
|
|
764
|
+
const moveDistance = e[strAbso] - moveTranstion.value
|
|
731
765
|
touchfinish.value = true
|
|
732
766
|
const eventData = {
|
|
733
767
|
translation: moveDistance
|
|
@@ -743,6 +777,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
|
|
|
743
777
|
if (Math.abs(strVelocity) < longPressRatio) {
|
|
744
778
|
handleLongPress()
|
|
745
779
|
} else {
|
|
780
|
+
// 如果触发了onTouchesCancelled,不会触发onUpdate不会更新offset值, 索引不会变更
|
|
746
781
|
handleEnd(eventData)
|
|
747
782
|
}
|
|
748
783
|
})
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { forwardRef, useRef, useContext, useMemo, useState
|
|
1
|
+
import { forwardRef, useRef, useContext, useMemo, useState } from 'react'
|
|
2
2
|
import { warn, isFunction } from '@mpxjs/utils'
|
|
3
3
|
import Portal from './mpx-portal/index'
|
|
4
|
+
import { usePreventRemove, PreventRemoveEvent } from '@react-navigation/native'
|
|
4
5
|
import { getCustomEvent } from './getInnerListeners'
|
|
5
6
|
import { promisify, redirectTo, navigateTo, navigateBack, reLaunch, switchTab } from '@mpxjs/api-proxy'
|
|
6
7
|
import { WebView } from 'react-native-webview'
|
|
@@ -75,7 +76,6 @@ const styles = StyleSheet.create({
|
|
|
75
76
|
borderRadius: 10
|
|
76
77
|
}
|
|
77
78
|
})
|
|
78
|
-
|
|
79
79
|
const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((props, ref): JSX.Element | null => {
|
|
80
80
|
const { src, bindmessage, bindload, binderror } = props
|
|
81
81
|
const mpx = global.__mpx
|
|
@@ -100,8 +100,8 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
|
|
|
100
100
|
const webViewRef = useRef<WebView>(null)
|
|
101
101
|
const fristLoaded = useRef<boolean>(false)
|
|
102
102
|
const isLoadError = useRef<boolean>(false)
|
|
103
|
+
const isNavigateBack = useRef<boolean>(false)
|
|
103
104
|
const statusCode = useRef<string|number>('')
|
|
104
|
-
const [isLoaded, setIsLoaded] = useState<boolean>(true)
|
|
105
105
|
const defaultWebViewStyle = {
|
|
106
106
|
position: 'absolute' as const,
|
|
107
107
|
left: 0,
|
|
@@ -109,30 +109,18 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
|
|
|
109
109
|
top: 0,
|
|
110
110
|
bottom: 0
|
|
111
111
|
}
|
|
112
|
-
const canGoBack = useRef<boolean>(false)
|
|
113
|
-
const isNavigateBack = useRef<boolean>(false)
|
|
114
112
|
|
|
115
|
-
const
|
|
116
|
-
|
|
113
|
+
const navigation = useNavigation()
|
|
114
|
+
const [isIntercept, setIsIntercept] = useState<boolean>(false)
|
|
115
|
+
usePreventRemove(isIntercept, (event: PreventRemoveEvent) => {
|
|
116
|
+
const { data } = event
|
|
117
|
+
if (isNavigateBack.current) {
|
|
118
|
+
navigation?.dispatch(data.action)
|
|
119
|
+
} else {
|
|
117
120
|
webViewRef.current?.goBack()
|
|
118
|
-
e.preventDefault()
|
|
119
121
|
}
|
|
120
122
|
isNavigateBack.current = false
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const navigation = useNavigation()
|
|
124
|
-
|
|
125
|
-
// useEffect(() => {
|
|
126
|
-
// let beforeRemoveSubscription:any
|
|
127
|
-
// if (__mpx_mode__ !== 'ios') {
|
|
128
|
-
// beforeRemoveSubscription = navigation?.addListener?.('beforeRemove', beforeRemoveHandle)
|
|
129
|
-
// }
|
|
130
|
-
// return () => {
|
|
131
|
-
// if (isFunction(beforeRemoveSubscription)) {
|
|
132
|
-
// beforeRemoveSubscription()
|
|
133
|
-
// }
|
|
134
|
-
// }
|
|
135
|
-
// }, [])
|
|
123
|
+
})
|
|
136
124
|
|
|
137
125
|
useNodesRef<WebView, WebViewProps>(props, ref, webViewRef, {
|
|
138
126
|
style: defaultWebViewStyle
|
|
@@ -183,14 +171,14 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
|
|
|
183
171
|
}
|
|
184
172
|
const _changeUrl = function (navState: WebViewNavigation) {
|
|
185
173
|
if (navState.navigationType) { // navigationType这个事件在页面开始加载时和页面加载完成时都会被触发所以判断这个避免其他无效触发执行该逻辑
|
|
186
|
-
canGoBack.current = navState.canGoBack
|
|
187
174
|
currentPage.__webViewUrl = navState.url
|
|
175
|
+
setIsIntercept(navState.canGoBack)
|
|
188
176
|
}
|
|
189
177
|
}
|
|
190
178
|
|
|
191
179
|
const _onLoadProgress = function (event: WebViewProgressEvent) {
|
|
192
180
|
if (__mpx_mode__ !== 'ios') {
|
|
193
|
-
|
|
181
|
+
setIsIntercept(event.nativeEvent.canGoBack)
|
|
194
182
|
}
|
|
195
183
|
}
|
|
196
184
|
const _message = function (res: WebViewMessageEvent) {
|
|
@@ -279,7 +267,6 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
|
|
|
279
267
|
}
|
|
280
268
|
const onLoadEndHandle = function (res: WebViewEvent) {
|
|
281
269
|
fristLoaded.current = true
|
|
282
|
-
setIsLoaded(true)
|
|
283
270
|
const src = res.nativeEvent?.url
|
|
284
271
|
if (isLoadError.current) {
|
|
285
272
|
isLoadError.current = false
|
|
@@ -325,11 +312,6 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
|
|
|
325
312
|
setPageLoadErr(true)
|
|
326
313
|
}
|
|
327
314
|
}
|
|
328
|
-
const onLoadStart = function () {
|
|
329
|
-
if (!fristLoaded.current) {
|
|
330
|
-
setIsLoaded(false)
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
315
|
|
|
334
316
|
return (
|
|
335
317
|
<Portal>
|
|
@@ -342,7 +324,6 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
|
|
|
342
324
|
)
|
|
343
325
|
: (<WebView
|
|
344
326
|
style={ defaultWebViewStyle }
|
|
345
|
-
pointerEvents={ isLoaded ? 'auto' : 'none' }
|
|
346
327
|
source={{ uri: src }}
|
|
347
328
|
ref={webViewRef}
|
|
348
329
|
javaScriptEnabled={true}
|
|
@@ -353,7 +334,6 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
|
|
|
353
334
|
onLoadEnd={onLoadEnd}
|
|
354
335
|
onHttpError={onHttpError}
|
|
355
336
|
onError={onError}
|
|
356
|
-
onLoadStart={onLoadStart}
|
|
357
337
|
allowsBackForwardNavigationGestures={true}
|
|
358
338
|
></WebView>)}
|
|
359
339
|
</Portal>
|
|
@@ -42,6 +42,21 @@ declare let global: {
|
|
|
42
42
|
|
|
43
43
|
declare module '@react-navigation/native' {
|
|
44
44
|
export function useNavigation (): Record<string, any>
|
|
45
|
+
export function usePreventRemove(
|
|
46
|
+
enabled: boolean,
|
|
47
|
+
callback: (e: { data: { action: any } }) => void
|
|
48
|
+
): void;
|
|
49
|
+
export interface PreventRemoveEvent {
|
|
50
|
+
data: {
|
|
51
|
+
action: NavigationAction;
|
|
52
|
+
route: {
|
|
53
|
+
key: string;
|
|
54
|
+
name: string;
|
|
55
|
+
params?: unknown;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
preventDefault(): void;
|
|
59
|
+
}
|
|
45
60
|
}
|
|
46
61
|
|
|
47
62
|
declare module '*.png'
|
|
@@ -79,7 +79,7 @@ registered in parent context!`)
|
|
|
79
79
|
transitionName: ''
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
|
-
if (!global.__mpx.config.webConfig
|
|
82
|
+
if (!global.__mpx.config.webConfig.disablePageTransition) {
|
|
83
83
|
option.watch = {
|
|
84
84
|
$route: {
|
|
85
85
|
handler () {
|
|
@@ -161,7 +161,7 @@ function createApp ({ componentsMap, Vue, pagesMap, firstPage, VueRouter, App, t
|
|
|
161
161
|
redirect: '/' + firstPage
|
|
162
162
|
})
|
|
163
163
|
}
|
|
164
|
-
const webRouteConfig = global.__mpx.config.webConfig
|
|
164
|
+
const webRouteConfig = global.__mpx.config.webConfig.routeConfig || global.__mpx.config.webRouteConfig
|
|
165
165
|
global.__mpxRouter = option.router = new VueRouter(extend({ routes }, webRouteConfig))
|
|
166
166
|
let mpxStackPath = []
|
|
167
167
|
if (isBrowser) {
|