@mpxjs/webpack-plugin 2.9.69-beta.5 → 2.9.69-beta.7

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.
@@ -1,8 +1,8 @@
1
- import { View, NativeSyntheticEvent, LayoutChangeEvent } from 'react-native'
1
+ import { AppState, View, NativeSyntheticEvent, LayoutChangeEvent } from 'react-native'
2
2
  import { GestureDetector, Gesture } from 'react-native-gesture-handler'
3
3
  import Animated, { useAnimatedStyle, useSharedValue, withTiming, Easing, runOnJS, useAnimatedReaction, cancelAnimation } from 'react-native-reanimated'
4
4
 
5
- import React, { JSX, forwardRef, useRef, useEffect, ReactNode, ReactElement, useCallback, useMemo } from 'react'
5
+ import React, { JSX, forwardRef, useRef, useEffect, ReactNode, ReactElement, useMemo } from 'react'
6
6
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
7
7
  import useNodesRef, { HandlerRef } from './useNodesRef' // 引入辅助函数
8
8
  import { useTransformStyle, splitStyle, splitProps, useLayout, wrapChildren } from './utils'
@@ -135,8 +135,8 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
135
135
  'parent-height': parentHeight,
136
136
  'external-var-context': externalVarContext,
137
137
  style = {},
138
- autoplay,
139
- circular
138
+ autoplay = false,
139
+ circular = false
140
140
  } = props
141
141
  const easeingFunc = props['easing-function'] || 'default'
142
142
  const easeDuration = props.duration || 500
@@ -160,15 +160,15 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
160
160
  })
161
161
  const { textStyle } = splitStyle(normalStyle)
162
162
  const { textProps } = splitProps(props)
163
- const preMargin = props['previous-margin'] ? parseInt(props['previous-margin']) : 0
164
- const nextMargin = props['next-margin'] ? parseInt(props['next-margin']) : 0
165
- const previousMarginShared = useSharedValue(preMargin)
163
+ const preMargin = props['previous-margin'] ? global.__formatValue(props['previous-margin']) as number : 0
164
+ const nextMargin = props['next-margin'] ? global.__formatValue(props['next-margin']) as number : 0
165
+ const preMarginShared = useSharedValue(preMargin)
166
166
  const nextMarginShared = useSharedValue(nextMargin)
167
- const autoplayShared = useSharedValue(autoplay || false)
167
+ const autoplayShared = useSharedValue(autoplay)
168
168
  // 默认前后补位的元素个数
169
169
  const patchElmNum = circular ? (preMargin ? 2 : 1) : 0
170
170
  const patchElmNumShared = useSharedValue(patchElmNum)
171
- const circularShared = useSharedValue(circular || false)
171
+ const circularShared = useSharedValue(circular)
172
172
  const children = Array.isArray(props.children) ? props.children.filter(child => child) : (props.children ? [props.children] : [])
173
173
  // 对有变化的变量,在worklet中只能使用sharedValue变量,useRef不能更新
174
174
  const childrenLength = useSharedValue(children.length)
@@ -195,6 +195,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
195
195
  const moveTime = useSharedValue(0)
196
196
  const timerId = useRef(0 as number | ReturnType<typeof setTimeout>)
197
197
  const intervalTimer = props.interval || 500
198
+ const appState = useRef(AppState.currentState)
198
199
  const {
199
200
  // 存储layout布局信息
200
201
  layoutRef,
@@ -312,9 +313,9 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
312
313
  return (<SwiperContext.Provider value={contextValue}>{arrChildren}</SwiperContext.Provider>)
313
314
  }
314
315
 
315
- const { loop, pauseLoop, resumeLoop} = useMemo(() => {
316
+ const { loop, pauseLoop, resumeLoop } = useMemo(() => {
316
317
  function createAutoPlay () {
317
- if (!step.value) return
318
+ if (!step.value || appState.current !== 'active') return
318
319
  let targetOffset = 0
319
320
  let nextIndex = currentIndex.value
320
321
  if (!circularShared.value) {
@@ -324,7 +325,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
324
325
  return
325
326
  }
326
327
  nextIndex += 1
327
- // targetOffset = -nextIndex * step.value - previousMarginShared.value
328
+ // targetOffset = -nextIndex * step.value - preMarginShared.value
328
329
  targetOffset = -nextIndex * step.value
329
330
  offset.value = withTiming(targetOffset, {
330
331
  duration: easeDuration,
@@ -337,12 +338,12 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
337
338
  // 默认向右, 向下
338
339
  if (nextIndex === childrenLength.value - 1) {
339
340
  nextIndex = 0
340
- targetOffset = -(childrenLength.value + patchElmNumShared.value) * step.value + previousMarginShared.value
341
+ targetOffset = -(childrenLength.value + patchElmNumShared.value) * step.value + preMarginShared.value
341
342
  // 执行动画到下一帧
342
343
  offset.value = withTiming(targetOffset, {
343
344
  duration: easeDuration
344
345
  }, () => {
345
- const initOffset = -step.value * patchElmNumShared.value + previousMarginShared.value
346
+ const initOffset = -step.value * patchElmNumShared.value + preMarginShared.value
346
347
  // 将开始位置设置为真正的位置
347
348
  offset.value = initOffset
348
349
  currentIndex.value = nextIndex
@@ -350,7 +351,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
350
351
  })
351
352
  } else {
352
353
  nextIndex = currentIndex.value + 1
353
- targetOffset = -(nextIndex + patchElmNumShared.value) * step.value + previousMarginShared.value
354
+ targetOffset = -(nextIndex + patchElmNumShared.value) * step.value + preMarginShared.value
354
355
  // 执行动画到下一帧
355
356
  offset.value = withTiming(targetOffset, {
356
357
  duration: easeDuration,
@@ -366,7 +367,9 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
366
367
  // loop在JS线程中调用,createAutoPlay + useEffect中
367
368
  function loop () {
368
369
  timerId.current && clearTimeout(timerId.current)
369
- timerId.current = setTimeout(createAutoPlay, intervalTimer)
370
+ if (appState.current === 'active') {
371
+ timerId.current = setTimeout(createAutoPlay, intervalTimer)
372
+ }
370
373
  }
371
374
 
372
375
  function pauseLoop () {
@@ -434,16 +437,30 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
434
437
  runOnJS(handleSwiperChange)(newIndex)
435
438
  }
436
439
  })
440
+ // 监听切换前后台,默认切换到后台JS执行不会立即停止定时器还在
441
+ useEffect(() => {
442
+ const subscription = AppState.addEventListener('change', nextAppState => {
443
+ appState.current = nextAppState
444
+ if (nextAppState === 'active' && autoplayShared.value && childrenLength.value > 1) {
445
+ loop()
446
+ } else {
447
+ appState.current = nextAppState
448
+ }
449
+ })
450
+ return () => {
451
+ subscription.remove()
452
+ }
453
+ }, [])
437
454
 
438
455
  useEffect(() => {
439
456
  let patchStep = 0
440
- if (preMargin !== previousMarginShared.value) {
441
- patchStep += preMargin - previousMarginShared.value
457
+ if (preMargin !== preMarginShared.value) {
458
+ patchStep += preMargin - preMarginShared.value
442
459
  }
443
460
  if (nextMargin !== nextMarginShared.value) {
444
461
  patchStep += nextMargin - nextMarginShared.value
445
462
  }
446
- previousMarginShared.value = preMargin
463
+ preMarginShared.value = preMargin
447
464
  nextMarginShared.value = nextMargin
448
465
  const newStep = step.value - patchStep
449
466
  if (step.value !== newStep) {
@@ -454,13 +471,17 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
454
471
 
455
472
  useEffect(() => {
456
473
  childrenLength.value = children.length
474
+ pauseLoop()
457
475
  if (children.length - 1 < currentIndex.value) {
458
- pauseLoop()
459
476
  currentIndex.value = 0
460
477
  offset.value = getOffset(0, step.value)
461
- if (autoplay && children.length > 1) {
462
- loop()
463
- }
478
+ } else {
479
+ // children变化-其他属性都不变化(比如从1变化到3或者从3变到1, 在circular的情况下offset是需要更新的)
480
+ currentIndex.value = props.current || 0
481
+ offset.value = getOffset(props.current || 0, step.value)
482
+ }
483
+ if (autoplay && children.length > 1) {
484
+ loop()
464
485
  }
465
486
  }, [children.length])
466
487
 
@@ -469,7 +490,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
469
490
  }, [props.current])
470
491
 
471
492
  useEffect(() => {
472
- autoplayShared.value = autoplay || false
493
+ autoplayShared.value = autoplay
473
494
  updateAutoplay()
474
495
  return () => {
475
496
  if (autoplay) {
@@ -480,7 +501,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
480
501
 
481
502
  useEffect(() => {
482
503
  if (circular !== circularShared.value) {
483
- circularShared.value = circular || false
504
+ circularShared.value = circular
484
505
  patchElmNumShared.value = circular ? (preMargin ? 2 : 1) : 0
485
506
  offset.value = getOffset(currentIndex.value, step.value)
486
507
  }
@@ -497,7 +518,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
497
518
  let isCriticalItem = false
498
519
  // 真实滚动到的偏移量坐标
499
520
  let moveToTargetPos = 0
500
- const currentOffset = translation < 0 ? offset.value - previousMarginShared.value : offset.value + previousMarginShared.value
521
+ const currentOffset = translation < 0 ? offset.value - preMarginShared.value : offset.value + preMarginShared.value
501
522
  const computedIndex = Math.abs(currentOffset) / step.value
502
523
  const moveToIndex = translation < 0 ? Math.ceil(computedIndex) : Math.floor(computedIndex)
503
524
  // 实际应该定位的索引值
@@ -507,17 +528,17 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
507
528
  } else {
508
529
  if (moveToIndex >= childrenLength.value + patchElmNumShared.value) {
509
530
  selectedIndex = moveToIndex - (childrenLength.value + patchElmNumShared.value)
510
- resetOffsetPos = (selectedIndex + patchElmNumShared.value) * step.value - previousMarginShared.value
511
- moveToTargetPos = moveToIndex * step.value - previousMarginShared.value
531
+ resetOffsetPos = (selectedIndex + patchElmNumShared.value) * step.value - preMarginShared.value
532
+ moveToTargetPos = moveToIndex * step.value - preMarginShared.value
512
533
  isCriticalItem = true
513
534
  } else if (moveToIndex <= patchElmNumShared.value - 1) {
514
535
  selectedIndex = moveToIndex === 0 ? childrenLength.value - patchElmNumShared.value : childrenLength.value - 1
515
- resetOffsetPos = (selectedIndex + patchElmNumShared.value) * step.value - previousMarginShared.value
516
- moveToTargetPos = moveToIndex * step.value - previousMarginShared.value
536
+ resetOffsetPos = (selectedIndex + patchElmNumShared.value) * step.value - preMarginShared.value
537
+ moveToTargetPos = moveToIndex * step.value - preMarginShared.value
517
538
  isCriticalItem = true
518
539
  } else {
519
540
  selectedIndex = moveToIndex - patchElmNumShared.value
520
- moveToTargetPos = moveToIndex * step.value - previousMarginShared.value
541
+ moveToTargetPos = moveToIndex * step.value - preMarginShared.value
521
542
  }
522
543
  }
523
544
  return {
@@ -573,11 +594,11 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
573
594
  // 向右滑动的back:trans < 0, 向左滑动的back: trans < 0
574
595
  let currentOffset = Math.abs(offset.value)
575
596
  if (circularShared.value) {
576
- currentOffset += translation < 0 ? previousMarginShared.value : -previousMarginShared.value
597
+ currentOffset += translation < 0 ? preMarginShared.value : -preMarginShared.value
577
598
  }
578
599
  const curIndex = currentOffset / step.value
579
600
  const moveToIndex = (translation < 0 ? Math.floor(curIndex) : Math.ceil(curIndex)) - patchElmNumShared.value
580
- const targetOffset = -(moveToIndex + patchElmNumShared.value) * step.value + (circularShared.value ? previousMarginShared.value : 0)
601
+ const targetOffset = -(moveToIndex + patchElmNumShared.value) * step.value + (circularShared.value ? preMarginShared.value : 0)
581
602
  offset.value = withTiming(targetOffset, {
582
603
  duration: easeDuration,
583
604
  easing: easeMap[easeingFunc]
@@ -593,7 +614,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
593
614
  const currentOffset = Math.abs(offset.value)
594
615
  let preOffset = (currentIndex.value + patchElmNumShared.value) * step.value
595
616
  if (circularShared.value) {
596
- preOffset -= previousMarginShared.value
617
+ preOffset -= preMarginShared.value
597
618
  }
598
619
  // 正常事件中拿到的transition值(正向滑动<0,倒着滑>0)
599
620
  const diffOffset = preOffset - currentOffset
@@ -611,7 +632,6 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
611
632
  // 移动的距离
612
633
  const { translation } = eventData
613
634
  const elementsLength = step.value * childrenLength.value
614
-
615
635
  let isBoundary = false
616
636
  let resetOffset = 0
617
637
  // Y轴向下滚动, transDistance > 0, 向上滚动 < 0 X轴向左滚动, transDistance > 0
@@ -678,7 +698,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
678
698
  })
679
699
  .onTouchesUp((e) => {
680
700
  'worklet'
681
- if(touchfinish.value) return
701
+ if (touchfinish.value) return
682
702
  const touchEventData = e.changedTouches[0]
683
703
  const moveDistance = touchEventData[strAbso] - moveTranstion.value
684
704
  touchfinish.value = true
@@ -5,8 +5,8 @@ import { getCustomEvent } from './getInnerListeners'
5
5
  import { promisify, redirectTo, navigateTo, navigateBack, reLaunch, switchTab } from '@mpxjs/api-proxy'
6
6
  import { WebView } from 'react-native-webview'
7
7
  import useNodesRef, { HandlerRef } from './useNodesRef'
8
- import { getCurrentPage, extendObject } from './utils'
9
- import { WebViewNavigationEvent, WebViewErrorEvent, WebViewMessageEvent, WebViewNavigation, WebViewProgressEvent, WebViewSource } from 'react-native-webview/lib/WebViewTypes'
8
+ import { getCurrentPage } from './utils'
9
+ import { WebViewMessageEvent, WebViewNavigation, WebViewProgressEvent, WebViewHttpErrorEvent, WebViewEvent } from 'react-native-webview/lib/WebViewTypes'
10
10
  import { useNavigation } from '@react-navigation/native'
11
11
  import { RouteContext } from './context'
12
12
  import { BackHandler, StyleSheet, View, Text, Platform } from 'react-native'
@@ -99,6 +99,7 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
99
99
  const [pageLoadErr, setPageLoadErr] = useState<boolean>(false)
100
100
  const currentPage = useMemo(() => getCurrentPage(pageId), [pageId])
101
101
  const webViewRef = useRef<WebView>(null)
102
+ const isLoaded = useRef<boolean>(false)
102
103
  const defaultWebViewStyle = {
103
104
  position: 'absolute' as 'absolute' | 'relative' | 'static',
104
105
  left: 0 as number,
@@ -151,28 +152,6 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
151
152
  return null
152
153
  }
153
154
 
154
- const _load = function (res: WebViewNavigationEvent) {
155
- const result = {
156
- type: 'load',
157
- timeStamp: res.timeStamp,
158
- detail: {
159
- src: res.nativeEvent?.url
160
- }
161
- }
162
- bindload?.(result)
163
- }
164
- const _error = function (res: WebViewErrorEvent) {
165
- setPageLoadErr(true)
166
- const result = {
167
- type: 'error',
168
- timeStamp: res.timeStamp,
169
- detail: {
170
- src: ''
171
- }
172
- }
173
- binderror && binderror(result)
174
- }
175
-
176
155
  const _reload = function () {
177
156
  setPageLoadErr(false)
178
157
  }
@@ -307,17 +286,51 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
307
286
  }
308
287
  })
309
288
  }
310
- const events = {}
311
289
 
312
- if (bindload) {
313
- extendObject(events, {
314
- onLoad: _load
315
- })
290
+ let isLoadError = false
291
+ let fristLoaded = false
292
+ let statusCode: string | number = ''
293
+ const onLoadEnd = function (res: WebViewEvent) {
294
+ fristLoaded = true
295
+ isLoaded.current = true
296
+ const src = res.nativeEvent?.url
297
+ if (isLoadError) {
298
+ isLoadError = false
299
+ isNavigateBack.current = false
300
+ const result = {
301
+ type: 'error',
302
+ timeStamp: res.timeStamp,
303
+ detail: {
304
+ src,
305
+ statusCode
306
+ }
307
+ }
308
+ binderror && binderror(result)
309
+ } else {
310
+ const result = {
311
+ type: 'load',
312
+ timeStamp: res.timeStamp,
313
+ detail: {
314
+ src
315
+ }
316
+ }
317
+ bindload?.(result)
318
+ }
319
+ }
320
+ const onHttpError = function (res: WebViewHttpErrorEvent) {
321
+ isLoadError = true
322
+ statusCode = res.nativeEvent?.statusCode
323
+ }
324
+ const onError = function () {
325
+ statusCode = ''
326
+ isLoadError = true
327
+ if (!fristLoaded) {
328
+ setPageLoadErr(true)
329
+ }
330
+ }
331
+ const onLoadStart = function () {
332
+ isLoaded.current = false
316
333
  }
317
-
318
- extendObject(events, {
319
- onError: _error
320
- })
321
334
 
322
335
  return (
323
336
  <Portal key={pageLoadErr ? 'error' : 'webview'}>
@@ -329,17 +342,21 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
329
342
  </View>
330
343
  )
331
344
  : (<WebView
332
- style={defaultWebViewStyle}
333
- source={{ uri: src }}
334
- ref={webViewRef}
335
- javaScriptEnabled={true}
336
- onNavigationStateChange={_changeUrl}
337
- onMessage={_message}
338
- injectedJavaScript={injectedJavaScript}
339
- onLoadProgress={_onLoadProgress}
340
- allowsBackForwardNavigationGestures={true}
341
- {...events}
342
- ></WebView>)}
345
+ style={ defaultWebViewStyle }
346
+ source={{ uri: src }}
347
+ pointerEvents={ isLoaded ? 'auto' : 'none' }
348
+ ref={webViewRef}
349
+ javaScriptEnabled={true}
350
+ onNavigationStateChange={_changeUrl}
351
+ onMessage={_message}
352
+ injectedJavaScript={injectedJavaScript}
353
+ onLoadProgress={_onLoadProgress}
354
+ onLoadEnd={onLoadEnd}
355
+ onHttpError={onHttpError}
356
+ onError={onError}
357
+ onLoadStart={onLoadStart}
358
+ allowsBackForwardNavigationGestures={true}
359
+ ></WebView>)}
343
360
  </Portal>
344
361
  )
345
362
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.9.69-beta.5",
3
+ "version": "2.9.69-beta.7",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"