@mpxjs/webpack-plugin 2.10.17-beta.7 → 2.10.17-beta.8

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.
@@ -0,0 +1,398 @@
1
+ import React, { forwardRef, useRef, useState, useEffect, useMemo, createElement, useImperativeHandle } from 'react'
2
+ import { SectionList, FlatList, RefreshControl, NativeSyntheticEvent, NativeScrollEvent } from 'react-native'
3
+ import useInnerProps, { getCustomEvent } from './getInnerListeners'
4
+ import { extendObject, useLayout, useTransformStyle } from './utils'
5
+ interface ListItem {
6
+ isSectionHeader?: boolean;
7
+ _originalItemIndex?: number;
8
+ [key: string]: any;
9
+ }
10
+
11
+ interface Section {
12
+ headerData: ListItem | null;
13
+ data: ListItem[];
14
+ hasSectionHeader?: boolean;
15
+ _originalItemIndex?: number;
16
+ }
17
+
18
+ interface ItemHeightType {
19
+ value?: number;
20
+ getter?: (item: any, index: number) => number;
21
+ }
22
+
23
+ interface RecycleViewProps {
24
+ enhanced?: boolean;
25
+ bounces?: boolean;
26
+ scrollEventThrottle?: number;
27
+ height?: number | string;
28
+ width?: number | string;
29
+ listData?: ListItem[];
30
+ generichash?: string;
31
+ style?: Record<string, any>;
32
+ itemHeight?: ItemHeightType;
33
+ sectionHeaderHeight?: ItemHeightType;
34
+ listHeaderData?: any;
35
+ listHeaderHeight?: ItemHeightType;
36
+ useListHeader?: boolean;
37
+ 'genericrecycle-item'?: string;
38
+ 'genericsection-header'?: string;
39
+ 'genericlist-header'?: string;
40
+ 'enable-var'?: boolean;
41
+ 'external-var-context'?: any;
42
+ 'parent-font-size'?: number;
43
+ 'parent-width'?: number;
44
+ 'parent-height'?: number;
45
+ 'enable-sticky'?: boolean;
46
+ 'enable-back-to-top'?: boolean;
47
+ 'end-reached-threshold'?: number;
48
+ 'refresher-enabled'?: boolean;
49
+ 'show-scrollbar'?: boolean;
50
+ 'refresher-triggered'?: boolean;
51
+ bindrefresherrefresh?: (event: any) => void;
52
+ bindscrolltolower?: (event: any) => void;
53
+ bindscroll?: (event: any) => void;
54
+ [key: string]: any;
55
+ }
56
+
57
+ interface ScrollPositionParams {
58
+ index: number;
59
+ animated?: boolean;
60
+ viewOffset?: number;
61
+ viewPosition?: number;
62
+ }
63
+
64
+ const getGeneric = (generichash: string, generickey: string) => {
65
+ if (!generichash || !generickey) return null
66
+ const GenericComponent = global.__mpxGenericsMap?.[generichash]?.[generickey]?.()
67
+ if (!GenericComponent) return null
68
+
69
+ return forwardRef((props: any, ref: any) => {
70
+ return createElement(GenericComponent, extendObject({}, {
71
+ ref: ref
72
+ }, props))
73
+ })
74
+ }
75
+
76
+ const getListHeaderComponent = (generichash: string, generickey: string, data: any) => {
77
+ if (!generichash || !generickey) return undefined
78
+ const ListHeaderComponent = getGeneric(generichash, generickey)
79
+ return ListHeaderComponent ? createElement(ListHeaderComponent, { listHeaderData: data }) : null
80
+ }
81
+
82
+ const getSectionHeaderRenderer = (generichash: string, generickey: string) => {
83
+ if (!generichash || !generickey) return undefined
84
+ return (sectionData: { section: Section }) => {
85
+ if (!sectionData.section.hasSectionHeader) return null
86
+ const SectionHeaderComponent = getGeneric(generichash, generickey)
87
+ return SectionHeaderComponent ? createElement(SectionHeaderComponent, { itemData: sectionData.section.headerData }) : null
88
+ }
89
+ }
90
+
91
+ const getItemRenderer = (generichash: string, generickey: string) => {
92
+ if (!generichash || !generickey) return undefined
93
+ return ({ item }: { item: any }) => {
94
+ const ItemComponent = getGeneric(generichash, generickey)
95
+ return ItemComponent ? createElement(ItemComponent, { itemData: item }) : null
96
+ }
97
+ }
98
+
99
+ const RecycleView = forwardRef<any, RecycleViewProps>((props = {}, ref) => {
100
+ const {
101
+ enhanced = false,
102
+ bounces = true,
103
+ scrollEventThrottle = 0,
104
+ height,
105
+ width,
106
+ listData,
107
+ generichash,
108
+ style = {},
109
+ itemHeight = {},
110
+ sectionHeaderHeight = {},
111
+ listHeaderHeight = {},
112
+ listHeaderData = null,
113
+ useListHeader = false,
114
+ 'genericrecycle-item': genericrecycleItem,
115
+ 'genericsection-header': genericsectionHeader,
116
+ 'genericlist-header': genericListHeader,
117
+ 'enable-var': enableVar,
118
+ 'external-var-context': externalVarContext,
119
+ 'parent-font-size': parentFontSize,
120
+ 'parent-width': parentWidth,
121
+ 'parent-height': parentHeight,
122
+ 'enable-sticky': enableSticky = false,
123
+ 'enable-back-to-top': enableBackToTop = false,
124
+ 'end-reached-threshold': onEndReachedThreshold = 0.1,
125
+ 'refresher-enabled': refresherEnabled,
126
+ 'show-scrollbar': showScrollbar = true,
127
+ 'refresher-triggered': refresherTriggered
128
+ } = props
129
+
130
+ const [refreshing, setRefreshing] = useState(!!refresherTriggered)
131
+
132
+ const scrollViewRef = useRef<any>(null)
133
+
134
+ const indexMap = useRef<{ [key: string]: string | number }>({})
135
+
136
+ const reverseIndexMap = useRef<{ [key: string]: number }>({})
137
+
138
+ const {
139
+ hasSelfPercent,
140
+ setWidth,
141
+ setHeight
142
+ } = useTransformStyle(style, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
143
+
144
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: scrollViewRef })
145
+
146
+ useEffect(() => {
147
+ if (refreshing !== refresherTriggered) {
148
+ setRefreshing(!!refresherTriggered)
149
+ }
150
+ }, [refresherTriggered])
151
+
152
+ const onRefresh = () => {
153
+ const { bindrefresherrefresh } = props
154
+ bindrefresherrefresh &&
155
+ bindrefresherrefresh(
156
+ getCustomEvent('refresherrefresh', {}, { layoutRef }, props)
157
+ )
158
+ }
159
+
160
+ const onEndReached = () => {
161
+ const { bindscrolltolower } = props
162
+ bindscrolltolower &&
163
+ bindscrolltolower(
164
+ getCustomEvent('scrolltolower', {}, { layoutRef }, props)
165
+ )
166
+ }
167
+
168
+ const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
169
+ const { bindscroll } = props
170
+ bindscroll &&
171
+ bindscroll(
172
+ getCustomEvent('scroll', event.nativeEvent, { layoutRef }, props)
173
+ )
174
+ }
175
+
176
+ // 通过sectionIndex和rowIndex获取原始索引
177
+ const getOriginalIndex = (sectionIndex: number, rowIndex: number | 'header'): number => {
178
+ const key = `${sectionIndex}_${rowIndex}`
179
+ return reverseIndexMap.current[key] ?? -1 // 如果找不到,返回-1
180
+ }
181
+
182
+ const scrollToIndex = ({ index, animated, viewOffset = 0, viewPosition = 0 }: ScrollPositionParams) => {
183
+ if (scrollViewRef.current) {
184
+ // 通过索引映射表快速定位位置
185
+ const position = indexMap.current[index]
186
+ const [sectionIndex, itemIndex] = (position as string).split('_')
187
+ scrollViewRef.current.scrollToLocation?.({
188
+ itemIndex: itemIndex === 'header' ? 0 : Number(itemIndex) + 1,
189
+ sectionIndex: Number(sectionIndex) || 0,
190
+ animated,
191
+ viewOffset,
192
+ viewPosition
193
+ })
194
+ }
195
+ }
196
+
197
+ const getItemHeight = ({ sectionIndex, rowIndex }: { sectionIndex: number, rowIndex: number }) => {
198
+ if (!itemHeight) {
199
+ return 0
200
+ }
201
+ if ((itemHeight as ItemHeightType).getter) {
202
+ const item = convertedListData[sectionIndex].data[rowIndex]
203
+ // 使用getOriginalIndex获取原始索引
204
+ const originalIndex = getOriginalIndex(sectionIndex, rowIndex)
205
+ return (itemHeight as ItemHeightType).getter?.(item, originalIndex) || 0
206
+ } else {
207
+ return (itemHeight as ItemHeightType).value || 0
208
+ }
209
+ }
210
+
211
+ const getSectionHeaderHeight = ({ sectionIndex }: { sectionIndex: number }) => {
212
+ const item = convertedListData[sectionIndex]
213
+ const { hasSectionHeader } = item
214
+ // 使用getOriginalIndex获取原始索引
215
+ const originalIndex = getOriginalIndex(sectionIndex, 'header')
216
+ if (!hasSectionHeader) return 0
217
+ if ((sectionHeaderHeight as ItemHeightType).getter) {
218
+ return (sectionHeaderHeight as ItemHeightType).getter?.(item, originalIndex) || 0
219
+ } else {
220
+ return (sectionHeaderHeight as ItemHeightType).value || 0
221
+ }
222
+ }
223
+
224
+ const convertedListData = useMemo(() => {
225
+ const sections: Section[] = []
226
+ let currentSection: Section | null = null
227
+ // 清空之前的索引映射
228
+ indexMap.current = {}
229
+ // 清空反向索引映射
230
+ reverseIndexMap.current = {}
231
+ listData.forEach((item: ListItem, index: number) => {
232
+ if (item.isSectionHeader) {
233
+ // 如果已经存在一个 section,先把它添加到 sections 中
234
+ if (currentSection) {
235
+ sections.push(currentSection)
236
+ }
237
+ // 创建新的 section
238
+ currentSection = {
239
+ headerData: item,
240
+ data: [],
241
+ hasSectionHeader: true,
242
+ _originalItemIndex: index
243
+ }
244
+ // 为 section header 添加索引映射
245
+ const sectionIndex = sections.length
246
+ indexMap.current[index] = `${sectionIndex}_header`
247
+ // 添加反向索引映射
248
+ reverseIndexMap.current[`${sectionIndex}_header`] = index
249
+ } else {
250
+ // 如果没有当前 section,创建一个默认的
251
+ if (!currentSection) {
252
+ // 创建默认section (无header的section)
253
+ currentSection = {
254
+ headerData: null,
255
+ data: [],
256
+ hasSectionHeader: false,
257
+ _originalItemIndex: -1
258
+ }
259
+ }
260
+ // 将 item 添加到当前 section 的 data 中
261
+ const itemIndex = currentSection.data.length
262
+ currentSection.data.push(extendObject({}, item, {
263
+ _originalItemIndex: index
264
+ }))
265
+ let sectionIndex
266
+ // 为 item 添加索引映射 - 存储格式为: "sectionIndex_itemIndex"
267
+ if (!currentSection.hasSectionHeader && sections.length === 0) {
268
+ // 在默认section中(第一个且无header)
269
+ sectionIndex = 0
270
+ indexMap.current[index] = `${sectionIndex}_${itemIndex}`
271
+ } else {
272
+ // 在普通section中
273
+ sectionIndex = sections.length
274
+ indexMap.current[index] = `${sectionIndex}_${itemIndex}`
275
+ }
276
+ // 添加反向索引映射
277
+ reverseIndexMap.current[`${sectionIndex}_${itemIndex}`] = index
278
+ }
279
+ })
280
+ // 添加最后一个 section
281
+ if (currentSection) {
282
+ sections.push(currentSection)
283
+ }
284
+ return sections
285
+ }, [listData])
286
+
287
+ const { getItemLayout } = useMemo(() => {
288
+ const layouts: Array<{ length: number, offset: number, index: number }> = []
289
+ let offset = 0
290
+
291
+ if (useListHeader) {
292
+ // 计算列表头部的高度
293
+ offset += listHeaderHeight.getter?.() || listHeaderHeight.value || 0
294
+ }
295
+
296
+ // 遍历所有 sections
297
+ convertedListData.forEach((section: Section, sectionIndex: number) => {
298
+ // 添加 section header 的位置信息
299
+ const headerHeight = getSectionHeaderHeight({ sectionIndex })
300
+ layouts.push({
301
+ length: headerHeight,
302
+ offset,
303
+ index: layouts.length
304
+ })
305
+ offset += headerHeight
306
+
307
+ // 添加该 section 中所有 items 的位置信息
308
+ section.data.forEach((item: ListItem, itemIndex: number) => {
309
+ const contenteight = getItemHeight({ sectionIndex, rowIndex: itemIndex })
310
+ layouts.push({
311
+ length: contenteight,
312
+ offset,
313
+ index: layouts.length
314
+ })
315
+ offset += contenteight
316
+ })
317
+
318
+ // 添加该 section 尾部位置信息
319
+ // 因为即使 sectionList 没传 renderSectionFooter,getItemLayout 中的 index 的计算也会包含尾部节点
320
+ layouts.push({
321
+ length: 0,
322
+ offset,
323
+ index: layouts.length
324
+ })
325
+ })
326
+ return {
327
+ itemLayouts: layouts,
328
+ getItemLayout: (data: any, index: number) => layouts[index]
329
+ }
330
+ }, [convertedListData, useListHeader])
331
+
332
+ const scrollAdditionalProps = extendObject(
333
+ {
334
+ alwaysBounceVertical: false,
335
+ alwaysBounceHorizontal: false,
336
+ scrollEventThrottle: scrollEventThrottle,
337
+ scrollsToTop: enableBackToTop,
338
+ showsHorizontalScrollIndicator: showScrollbar,
339
+ onEndReachedThreshold,
340
+ ref: scrollViewRef,
341
+ bounces: false,
342
+ stickySectionHeadersEnabled: enableSticky,
343
+ onScroll: onScroll,
344
+ onEndReached: onEndReached
345
+ },
346
+ layoutProps
347
+ )
348
+
349
+ if (enhanced) {
350
+ Object.assign(scrollAdditionalProps, {
351
+ bounces
352
+ })
353
+ }
354
+ if (refresherEnabled) {
355
+ Object.assign(scrollAdditionalProps, {
356
+ refreshing: refreshing
357
+ })
358
+ }
359
+
360
+ useImperativeHandle(ref, () => {
361
+ return {
362
+ ...props,
363
+ scrollToIndex
364
+ }
365
+ })
366
+
367
+ const innerProps = useInnerProps(extendObject({}, props, scrollAdditionalProps), [
368
+ 'id',
369
+ 'show-scrollbar',
370
+ 'lower-threshold',
371
+ 'refresher-triggered',
372
+ 'refresher-enabled',
373
+ 'bindrefresherrefresh'
374
+ ], { layoutRef })
375
+
376
+ return createElement(
377
+ SectionList,
378
+ extendObject(
379
+ {
380
+ style: [{ height, width }, style, layoutStyle],
381
+ sections: convertedListData,
382
+ renderItem: getItemRenderer(generichash, genericrecycleItem),
383
+ getItemLayout: getItemLayout,
384
+ ListHeaderComponent: useListHeader ? getListHeaderComponent(generichash, genericListHeader, listHeaderData) : null,
385
+ renderSectionHeader: getSectionHeaderRenderer(generichash, genericsectionHeader),
386
+ refreshControl: refresherEnabled
387
+ ? React.createElement(RefreshControl, {
388
+ onRefresh: onRefresh,
389
+ refreshing: refreshing
390
+ })
391
+ : undefined
392
+ },
393
+ innerProps
394
+ )
395
+ )
396
+ })
397
+
398
+ export default RecycleView
@@ -259,6 +259,7 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
259
259
 
260
260
  // layout 完成前先隐藏,避免安卓闪烁问题
261
261
  const refresherLayoutStyle = useMemo(() => { return !hasRefresherLayoutRef.current ? HIDDEN_STYLE : {} }, [hasRefresherLayoutRef.current])
262
+
262
263
  const lastOffset = useRef(0)
263
264
 
264
265
  if (scrollX && scrollY) {
@@ -1,9 +1,9 @@
1
-
2
1
  import { useRef, forwardRef, createElement, ReactNode, useCallback, useMemo } from 'react'
3
2
  import { View, ViewStyle } from 'react-native'
4
3
  import useNodesRef, { HandlerRef } from './useNodesRef'
5
4
  import { splitProps, splitStyle, useTransformStyle, wrapChildren, useLayout, extendObject } from './utils'
6
5
  import { StickyContext } from './context'
6
+
7
7
  import useInnerProps from './getInnerListeners'
8
8
 
9
9
  interface StickySectionProps {