|
| 1 | +import { warn } from '@mpxjs/utils' |
| 2 | +import { CREATED } from '../../core/innerLifecycle' |
| 3 | + |
| 4 | +/** |
| 5 | + * React Native 页面滚动 Mixin |
| 6 | + * 提供页面级别的 pageScrollTo 方法 |
| 7 | + * 使用该功能需在页面的 scroll-view 组件上声明 wx:ref="pageScrollView" |
| 8 | + */ |
| 9 | +export default function pageScrollMixin (mixinType) { |
| 10 | + if (mixinType !== 'page') { |
| 11 | + return |
| 12 | + } |
| 13 | + |
| 14 | + return { |
| 15 | + [CREATED] () { |
| 16 | + this.__registerPageScrollTo() |
| 17 | + }, |
| 18 | + beforeUnmount () { |
| 19 | + const navigation = this.__props?.navigation |
| 20 | + if (navigation && navigation.pageScrollTo) { |
| 21 | + delete navigation.pageScrollTo |
| 22 | + } |
| 23 | + }, |
| 24 | + methods: { |
| 25 | + /** |
| 26 | + * 注册 pageScrollTo 方法到 navigation 对象 |
| 27 | + */ |
| 28 | + __registerPageScrollTo () { |
| 29 | + const navigation = this.__props?.navigation |
| 30 | + |
| 31 | + // navigation.pageScrollTo 不存在时才注册,避免重复 |
| 32 | + if (navigation && !navigation.pageScrollTo) { |
| 33 | + navigation.pageScrollTo = (options) => { |
| 34 | + this.__pageScrollTo(options) |
| 35 | + } |
| 36 | + } |
| 37 | + }, |
| 38 | + |
| 39 | + /** |
| 40 | + * 获取页面滚动视图节点(通过固定 ref 名称 pageScrollView) |
| 41 | + * @returns {Object|null} 滚动视图的节点实例 |
| 42 | + */ |
| 43 | + __findScrollableNode () { |
| 44 | + const ref = this.__refs?.pageScrollView?.[0] |
| 45 | + if (!ref || ref.type !== 'node' || !ref.instance?.getNodeInstance) return null |
| 46 | + return ref.instance.getNodeInstance() |
| 47 | + }, |
| 48 | + |
| 49 | + /** |
| 50 | + * 页面滚动到指定位置 |
| 51 | + * @param {Object} options - 配置选项 |
| 52 | + * @param {number} options.scrollTop - 滚动到页面的目标位置(单位 px) |
| 53 | + * @param {number} options.duration - 滚动动画的时长(单位 ms) |
| 54 | + * @param {string} options.selector - 选择器 |
| 55 | + * @param {number} options.offsetTop - 偏移距离 |
| 56 | + * @param {Function} options.onSuccess - 成功回调 |
| 57 | + * @param {Function} options.onFail - 失败回调 |
| 58 | + */ |
| 59 | + __pageScrollTo (options = {}) { |
| 60 | + const { |
| 61 | + scrollTop, |
| 62 | + duration = 300, |
| 63 | + selector, |
| 64 | + offsetTop = 0, |
| 65 | + onSuccess, |
| 66 | + onFail |
| 67 | + } = options |
| 68 | + |
| 69 | + try { |
| 70 | + const nodeInstance = this.__findScrollableNode() |
| 71 | + |
| 72 | + if (nodeInstance) { |
| 73 | + this.__executeScroll(nodeInstance, scrollTop, duration, selector, offsetTop, onSuccess, onFail) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + // 没找到可滚动视图 |
| 78 | + const errMsg = 'pageScrollTo:fail scrollable view not found. Please add wx:ref="pageScrollView" to the scroll-view component in your page' |
| 79 | + warn(errMsg) |
| 80 | + onFail && onFail(errMsg) |
| 81 | + } catch (e) { |
| 82 | + const errMsg = `pageScrollTo:fail ${e.message}` |
| 83 | + warn(errMsg) |
| 84 | + onFail && onFail(errMsg) |
| 85 | + } |
| 86 | + }, |
| 87 | + |
| 88 | + /** |
| 89 | + * 执行滚动操作 |
| 90 | + */ |
| 91 | + __executeScroll (scrollViewNodeInstance, scrollTop, duration, selector, offsetTop, onSuccess, onFail) { |
| 92 | + try { |
| 93 | + const scrollViewNode = scrollViewNodeInstance.instance.node |
| 94 | + |
| 95 | + // 如果提供了 selector,使用 scrollIntoView |
| 96 | + if (selector) { |
| 97 | + if (scrollViewNode.scrollIntoView) { |
| 98 | + scrollViewNode.scrollIntoView(selector, { |
| 99 | + offset: offsetTop, |
| 100 | + animated: duration > 0, |
| 101 | + duration |
| 102 | + }) |
| 103 | + onSuccess && onSuccess() |
| 104 | + } else { |
| 105 | + const errMsg = 'pageScrollTo:fail scrollIntoView method not available' |
| 106 | + warn(errMsg) |
| 107 | + onFail && onFail(errMsg) |
| 108 | + } |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + // 使用 scrollTop 进行滚动 |
| 113 | + if (scrollTop !== undefined) { |
| 114 | + if (scrollViewNode.scrollTo) { |
| 115 | + scrollViewNode.scrollTo({ |
| 116 | + top: scrollTop, |
| 117 | + left: 0, |
| 118 | + animated: duration > 0, |
| 119 | + duration |
| 120 | + }) |
| 121 | + onSuccess && onSuccess() |
| 122 | + } else { |
| 123 | + const errMsg = 'pageScrollTo:fail scrollTo method not available' |
| 124 | + warn(errMsg) |
| 125 | + onFail && onFail(errMsg) |
| 126 | + } |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + // 既没有 scrollTop 也没有 selector |
| 131 | + const errMsg = 'pageScrollTo:fail scrollTop or selector is required' |
| 132 | + warn(errMsg) |
| 133 | + onFail && onFail(errMsg) |
| 134 | + } catch (e) { |
| 135 | + const errMsg = `pageScrollTo:fail ${e.message}` |
| 136 | + warn(errMsg) |
| 137 | + onFail && onFail(errMsg) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments