@mpxjs/webpack-plugin 2.10.13 → 2.10.14-beta.2

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.
@@ -38,7 +38,7 @@ import Animated, { useSharedValue, withTiming, useAnimatedStyle, runOnJS } from
38
38
  import { warn, hasOwn } from '@mpxjs/utils'
39
39
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
40
40
  import useNodesRef, { HandlerRef } from './useNodesRef'
41
- import { splitProps, splitStyle, useTransformStyle, useLayout, wrapChildren, extendObject, flatGesture, GestureHandler, HIDDEN_STYLE } from './utils'
41
+ import { splitProps, splitStyle, useTransformStyle, useLayout, wrapChildren, extendObject, flatGesture, GestureHandler, HIDDEN_STYLE, useRunOnJSCallback } from './utils'
42
42
  import { IntersectionObserverContext, ScrollViewContext } from './context'
43
43
  import Portal from './mpx-portal'
44
44
 
@@ -210,6 +210,27 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
210
210
  const { textStyle, innerStyle = {} } = splitStyle(normalStyle)
211
211
 
212
212
  const scrollViewRef = useRef<ScrollView>(null)
213
+
214
+ const propsRef = useRef(props)
215
+ const refresherStateRef = useRef({
216
+ hasRefresher,
217
+ refresherTriggered
218
+ })
219
+
220
+ propsRef.current = props
221
+ refresherStateRef.current = {
222
+ hasRefresher,
223
+ refresherTriggered
224
+ }
225
+
226
+ const runOnJSCallbackRef = useRef({
227
+ setEnableScroll,
228
+ setScrollBounces,
229
+ setRefreshing,
230
+ onRefresh
231
+ })
232
+ const runOnJSCallback = useRunOnJSCallback(runOnJSCallbackRef)
233
+
213
234
  useNodesRef(props, ref, scrollViewRef, {
214
235
  style: normalStyle,
215
236
  scrollOffset: scrollOptions,
@@ -518,6 +539,7 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
518
539
 
519
540
  // 处理刷新
520
541
  function onRefresh () {
542
+ const { hasRefresher, refresherTriggered } = refresherStateRef.current
521
543
  if (hasRefresher && refresherTriggered === undefined) {
522
544
  // 处理使用了自定义刷新组件,又没设置 refresherTriggered 的情况
523
545
  setRefreshing(true)
@@ -529,10 +551,10 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
529
551
  }
530
552
  }, 500)
531
553
  }
532
- const { bindrefresherrefresh } = props
554
+ const { bindrefresherrefresh } = propsRef.current
533
555
  bindrefresherrefresh &&
534
556
  bindrefresherrefresh(
535
- getCustomEvent('refresherrefresh', {}, { layoutRef }, props)
557
+ getCustomEvent('refresherrefresh', {}, { layoutRef }, propsRef.current)
536
558
  )
537
559
  }
538
560
 
@@ -587,7 +609,7 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
587
609
  'worklet'
588
610
  if (enableScrollValue.value !== newValue) {
589
611
  enableScrollValue.value = newValue
590
- runOnJS(setEnableScroll)(newValue)
612
+ runOnJS(runOnJSCallback)('setEnableScroll', newValue)
591
613
  }
592
614
  }
593
615
 
@@ -600,7 +622,7 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
600
622
  'worklet'
601
623
  if (bouncesValue.value !== newValue) {
602
624
  bouncesValue.value = newValue
603
- runOnJS(setScrollBounces)(newValue)
625
+ runOnJS(runOnJSCallback)('setScrollBounces', newValue)
604
626
  }
605
627
  }
606
628
 
@@ -649,19 +671,19 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
649
671
  if ((event.translationY > 0 && translateY.value < refresherThreshold) || event.translationY < 0) {
650
672
  translateY.value = withTiming(0)
651
673
  updateScrollState(true)
652
- runOnJS(setRefreshing)(false)
674
+ runOnJS(runOnJSCallback)('setRefreshing', false)
653
675
  } else {
654
676
  translateY.value = withTiming(refresherHeight.value)
655
677
  }
656
678
  } else if (event.translationY >= refresherHeight.value) {
657
679
  // 触发刷新
658
680
  translateY.value = withTiming(refresherHeight.value)
659
- runOnJS(onRefresh)()
681
+ runOnJS(runOnJSCallback)('onRefresh')
660
682
  } else {
661
683
  // 回弹
662
684
  translateY.value = withTiming(0)
663
685
  updateScrollState(true)
664
- runOnJS(setRefreshing)(false)
686
+ runOnJS(runOnJSCallback)('setRefreshing', false)
665
687
  }
666
688
  })
667
689
  .simultaneousWithExternalGesture(scrollViewRef)
@@ -5,7 +5,7 @@ import Animated, { useAnimatedStyle, useSharedValue, withTiming, Easing, runOnJS
5
5
  import React, { JSX, forwardRef, useRef, useEffect, ReactNode, ReactElement, useMemo, createElement } from 'react'
6
6
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
7
7
  import useNodesRef, { HandlerRef } from './useNodesRef' // 引入辅助函数
8
- import { useTransformStyle, splitStyle, splitProps, useLayout, wrapChildren, extendObject, GestureHandler, flatGesture } from './utils'
8
+ import { useTransformStyle, splitStyle, splitProps, useLayout, wrapChildren, extendObject, GestureHandler, flatGesture, useRunOnJSCallback } from './utils'
9
9
  import { SwiperContext } from './context'
10
10
  import Portal from './mpx-portal'
11
11
  /**
@@ -148,6 +148,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
148
148
  autoplay = false,
149
149
  circular = false,
150
150
  disableGesture = false,
151
+ current: propCurrent = 0,
151
152
  bindchange
152
153
  } = props
153
154
  const easeingFunc = props['easing-function'] || 'default'
@@ -198,14 +199,16 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
198
199
  // 每个元素的宽度 or 高度,有固定值直接初始化无则0
199
200
  const step = useSharedValue(initStep)
200
201
  // 记录选中元素的索引值
201
- const currentIndex = useSharedValue(props.current || 0)
202
+ const currentIndex = useSharedValue(propCurrent)
202
203
  // const initOffset = getOffset(props.current || 0, initStep)
203
204
  // 记录元素的偏移量
204
- const offset = useSharedValue(getOffset(props.current || 0, initStep))
205
+ const offset = useSharedValue(getOffset(propCurrent, initStep))
205
206
  const strAbso = 'absolute' + dir.toUpperCase() as StrAbsoType
206
207
  const strVelocity = 'velocity' + dir.toUpperCase() as StrVelocityType
207
208
  // 标识手指触摸和抬起, 起点在onBegin
208
209
  const touchfinish = useSharedValue(true)
210
+ // 记录onUpdate时的方向,用于进行onFinalize中的值修正
211
+ const preUpdateTransDir = useSharedValue(0)
209
212
  // 记录上一帧的绝对定位坐标
210
213
  const preAbsolutePos = useSharedValue(0)
211
214
  // 记录从onBegin 到 onTouchesUp 时移动的距离
@@ -270,7 +273,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
270
273
  const iStep = dir === 'x' ? realWidth : realHeight
271
274
  if (iStep !== step.value) {
272
275
  step.value = iStep
273
- updateCurrent(props.current || 0, iStep)
276
+ updateCurrent(propCurrent, iStep)
274
277
  updateAutoplay()
275
278
  }
276
279
  }
@@ -374,7 +377,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
374
377
  easing: easeMap[easeingFunc]
375
378
  }, () => {
376
379
  currentIndex.value = nextIndex
377
- runOnJS(loop)()
380
+ runOnJS(runOnJSCallback)('loop')
378
381
  })
379
382
  } else {
380
383
  // 默认向右, 向下
@@ -389,7 +392,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
389
392
  // 将开始位置设置为真正的位置
390
393
  offset.value = initOffset
391
394
  currentIndex.value = nextIndex
392
- runOnJS(loop)()
395
+ runOnJS(runOnJSCallback)('loop')
393
396
  })
394
397
  } else {
395
398
  nextIndex = currentIndex.value + 1
@@ -400,7 +403,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
400
403
  easing: easeMap[easeingFunc]
401
404
  }, () => {
402
405
  currentIndex.value = nextIndex
403
- runOnJS(loop)()
406
+ runOnJS(runOnJSCallback)('loop')
404
407
  })
405
408
  }
406
409
  }
@@ -429,12 +432,18 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
429
432
  }, [])
430
433
 
431
434
  function handleSwiperChange (current: number) {
432
- if (props.current !== currentIndex.value) {
433
- const eventData = getCustomEvent('change', {}, { detail: { current, source: 'touch' }, layoutRef: layoutRef })
434
- bindchange && bindchange(eventData)
435
- }
435
+ const eventData = getCustomEvent('change', {}, { detail: { current, source: 'touch' }, layoutRef: layoutRef })
436
+ bindchange && bindchange(eventData)
436
437
  }
437
438
 
439
+ const runOnJSCallbackRef = useRef({
440
+ loop,
441
+ pauseLoop,
442
+ resumeLoop,
443
+ handleSwiperChange
444
+ })
445
+ const runOnJSCallback = useRunOnJSCallback(runOnJSCallbackRef)
446
+
438
447
  function getOffset (index: number, stepValue: number) {
439
448
  if (!stepValue) return 0
440
449
  let targetOffset = 0
@@ -451,12 +460,12 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
451
460
  const targetOffset = getOffset(index || 0, stepValue)
452
461
  if (targetOffset !== offset.value) {
453
462
  // 内部基于props.current!==currentIndex.value决定是否使用动画及更新currentIndex.value
454
- if (props.current !== undefined && props.current !== currentIndex.value) {
463
+ if (propCurrent !== undefined && propCurrent !== currentIndex.value) {
455
464
  offset.value = withTiming(targetOffset, {
456
465
  duration: easeDuration,
457
466
  easing: easeMap[easeingFunc]
458
467
  }, () => {
459
- currentIndex.value = props.current || 0
468
+ currentIndex.value = propCurrent
460
469
  })
461
470
  } else {
462
471
  offset.value = targetOffset
@@ -473,8 +482,8 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
473
482
  // 1. 用户在当前页切换选中项,动画;用户携带选中index打开到swiper页直接选中不走动画
474
483
  useAnimatedReaction(() => currentIndex.value, (newIndex: number, preIndex: number) => {
475
484
  // 这里必须传递函数名, 直接写()=> {}形式会报 访问了未sharedValue信息
476
- if (newIndex !== preIndex && bindchange) {
477
- runOnJS(handleSwiperChange)(newIndex)
485
+ if (newIndex !== preIndex && preIndex !== null && preIndex !== undefined && bindchange) {
486
+ runOnJS(runOnJSCallback)('handleSwiperChange', newIndex, propCurrent)
478
487
  }
479
488
  })
480
489
 
@@ -508,12 +517,12 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
508
517
  }, [children.length])
509
518
 
510
519
  useEffect(() => {
511
- // 1. 如果用户在touch的过程中, 外部更新了current以外部为准(小程序表现)
520
+ // 1. 如果用户在touch的过程中, 外部更新了current以内部为准(小程序表现)
512
521
  // 2. 手指滑动过程中更新索引,外部会把current再传入进来,导致offset直接更新,增加判断不同才更新
513
- if (props.current !== currentIndex.value) {
514
- updateCurrent(props.current || 0, step.value)
522
+ if (propCurrent !== currentIndex.value && touchfinish.value) {
523
+ updateCurrent(propCurrent, step.value)
515
524
  }
516
- }, [props.current])
525
+ }, [propCurrent])
517
526
 
518
527
  useEffect(() => {
519
528
  autoplayShared.value = autoplay
@@ -603,7 +612,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
603
612
  if (touchfinish.value !== false) {
604
613
  currentIndex.value = selectedIndex
605
614
  offset.value = resetOffset
606
- runOnJS(resumeLoop)()
615
+ runOnJS(runOnJSCallback)('resumeLoop')
607
616
  }
608
617
  })
609
618
  } else {
@@ -613,7 +622,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
613
622
  }, () => {
614
623
  if (touchfinish.value !== false) {
615
624
  currentIndex.value = selectedIndex
616
- runOnJS(resumeLoop)()
625
+ runOnJS(runOnJSCallback)('resumeLoop')
617
626
  }
618
627
  })
619
628
  }
@@ -635,7 +644,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
635
644
  }, () => {
636
645
  if (touchfinish.value !== false) {
637
646
  currentIndex.value = moveToIndex
638
- runOnJS(resumeLoop)()
647
+ runOnJS(runOnJSCallback)('resumeLoop')
639
648
  }
640
649
  })
641
650
  }
@@ -662,19 +671,10 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
662
671
  'worklet'
663
672
  const { diffOffset, half, isTriggerUpdateHalf } = computeHalf(eventData)
664
673
  if (+diffOffset === 0) {
665
- runOnJS(resumeLoop)()
674
+ runOnJS(runOnJSCallback)('resumeLoop')
666
675
  } else if (isTriggerUpdateHalf) {
667
- // 如果触发了onUpdate时的索引变更,则直接以update时的index为准
668
- const targetIndex = !circularShared.value ? currentIndex.value : currentIndex.value + patchElmNumShared.value - 1
669
- offset.value = withTiming(-targetIndex * step.value, {
670
- duration: easeDuration,
671
- easing: easeMap[easeingFunc]
672
- }, () => {
673
- if (touchfinish.value !== false) {
674
- currentIndex.value = targetIndex
675
- runOnJS(resumeLoop)()
676
- }
677
- })
676
+ // 如果触发了onUpdate时的索引变更
677
+ handleEnd(eventData)
678
678
  } else if (half) {
679
679
  handleEnd(eventData)
680
680
  } else {
@@ -745,18 +745,19 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
745
745
  if (!step.value) return
746
746
  touchfinish.value = false
747
747
  cancelAnimation(offset)
748
- runOnJS(pauseLoop)()
748
+ runOnJS(runOnJSCallback)('pauseLoop')
749
749
  preAbsolutePos.value = e[strAbso]
750
750
  moveTranstion.value = e[strAbso]
751
751
  })
752
752
  .onUpdate((e: GestureStateChangeEvent<PanGestureHandlerEventPayload>) => {
753
753
  'worklet'
754
- if (touchfinish.value) return
755
754
  const moveDistance = e[strAbso] - preAbsolutePos.value
755
+ if (touchfinish.value || moveDistance === 0) return
756
756
  const eventData = {
757
757
  translation: moveDistance,
758
- transdir: moveDistance !== 0 ? moveDistance : e[strAbso] - moveTranstion.value
758
+ transdir: moveDistance
759
759
  }
760
+ preUpdateTransDir.value = moveDistance
760
761
  // 1. 支持滑动中超出一半更新索引的能力:只更新索引并不会影响onFinalize依据当前offset计算的索引
761
762
  const { half } = computeHalf(eventData)
762
763
  if (childrenLength.value > 1 && half) {
@@ -794,11 +795,17 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
794
795
  'worklet'
795
796
  if (touchfinish.value) return
796
797
  touchfinish.value = true
798
+ /**
799
+ * 安卓修正
800
+ * 问题:部分安卓机型onFinalize中拿到的absoluteX 有问题
801
+ * 案例:比如手指从右向左滑的时候,onUpdate拿到的是241.64346313476562, 而onFinalize中拿到的是241.81817626953125,理论上onFinalize中应该比onUpdate小才对吧
802
+ * 解决方式:修正
803
+ */
797
804
  // 触发过onUpdate正常情况下e[strAbso] - preAbsolutePos.value=0; 未触发过onUpdate的情况下e[strAbso] - preAbsolutePos.value 不为0
798
805
  const moveDistance = e[strAbso] - preAbsolutePos.value
799
806
  const eventData = {
800
807
  translation: moveDistance,
801
- transdir: moveDistance !== 0 ? moveDistance : e[strAbso] - moveTranstion.value
808
+ transdir: Math.abs(moveDistance) > 1 ? moveDistance : preUpdateTransDir.value
802
809
  }
803
810
  // 1. 只有一个元素:循环 和 非循环状态,都走回弹效果
804
811
  if (childrenLength.value === 1) {
@@ -884,4 +891,4 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
884
891
  })
885
892
  SwiperWrapper.displayName = 'MpxSwiperWrapper'
886
893
 
887
- export default SwiperWrapper
894
+ export default SwiperWrapper
@@ -287,16 +287,13 @@ function backgroundSize (imageProps: ImageProps, preImageInfo: PreImageInfo, ima
287
287
  } else { // 数值类型 ImageStyle
288
288
  // 数值类型设置为 stretch
289
289
  imageProps.resizeMode = 'stretch'
290
- if (type === 'linear') {
291
- const dimensionWidth = calcPercent(width as NumberVal, layoutWidth) || 0
292
- const dimensionHeight = calcPercent(height as NumberVal, layoutHeight) || 0
293
- // ios 上 linear 组件只要重新触发渲染,在渲染过程中 width 或者 height 被设置为 0,即使后面再更新为正常宽高,也会渲染不出来
294
- if (dimensionWidth && dimensionHeight) {
295
- dimensions = {
296
- width: dimensionWidth,
297
- height: dimensionHeight
298
- } as { width: NumberVal, height: NumberVal }
299
- }
290
+ if (type === 'linear' && (!layoutWidth || !layoutHeight)) {
291
+ // ios linear 组件只要重新触发渲染,在渲染过程中外层容器 width 或者 height 被设置为 0,通过设置 % 的方式会渲染不出来,即使后面再更新为正常宽高也渲染不出来
292
+ // 所以 hack 手动先将 linear 宽高也设置为 0,后面再更新为正确的数值或 %。
293
+ dimensions = {
294
+ width: 0,
295
+ height: 0
296
+ } as { width: NumberVal, height: NumberVal }
300
297
  } else {
301
298
  dimensions = {
302
299
  width: isPercent(width) ? width : +width,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.10.13",
3
+ "version": "2.10.14-beta.2",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
@@ -100,5 +100,5 @@
100
100
  "engines": {
101
101
  "node": ">=14.14.0"
102
102
  },
103
- "gitHead": "1b4a2d4765341ef6c6b74e501f72a0f856f247f9"
103
+ "gitHead": "2d37697869b9bdda3efab92dda8c910b68fd05c0"
104
104
  }