-
Notifications
You must be signed in to change notification settings - Fork 390
Feat use page wrapper comp #1924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8010df4
feat: pageWrapper半成品
Blackgan3 56e2795
feat: pageWrapper 抽离完成初版
Blackgan3 217124c
feat: 完善pageWrapper
Blackgan3 4bd01fd
feat: 添加pageWrapper displayname
Blackgan3 716c8c9
fix: eslint error
Blackgan3 861eabb
feat: 调整pageWrapper内容,下沉page函数
Blackgan3 a46409f
feat: 调整pageWrapper 包裹逻辑
Blackgan3 50a90ef
fix: pageWrapper 验证独立组件onshow/onhide能力
Blackgan3 38159d4
feat: 删除global.__navigationHelper 的 编译注入
Blackgan3 175ae18
feat: 调整global.__navigationHelper 注入位置
Blackgan3 bfe0fe1
fix: 调整pageWrapper导出方式
Blackgan3 18c7913
feat: 调整pageWrapper包裹逻辑
Blackgan3 9c73cfa
feat: 调整navigationHelper位置
Blackgan3 1bceb7d
feat: 更改pageWrapper为HOC高阶组件
Blackgan3 3e90463
feat: add error handler
Blackgan3 6e2b8b0
Merge branch 'refs/heads/master' into feat-use-page-wrapper-comp
Blackgan3 6e33cc6
feat: merge master getDefaultOptions
Blackgan3 d4d0e1f
Merge branch 'refs/heads/master' into feat-use-page-wrapper-comp
Blackgan3 ef3ae4b
feat: 调整pagewrappper包裹逻辑
Blackgan3 959c51f
feat: error & warn utils 添加 Error 兜底创建逻辑
Blackgan3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/core/src/platform/env/navigationHelper.android.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { createStackNavigator } from '@react-navigation/stack' | ||
| import { NavigationContainer, StackActions } from '@react-navigation/native' | ||
| import PortalHost from '@mpxjs/webpack-plugin/lib/runtime/components/react/dist/mpx-portal/portal-host' | ||
| import { useHeaderHeight } from '@react-navigation/elements' | ||
| import { SafeAreaProvider, useSafeAreaInsets } from 'react-native-safe-area-context' | ||
| import { GestureHandlerRootView } from 'react-native-gesture-handler' | ||
|
|
||
| export { | ||
| createStackNavigator, | ||
| NavigationContainer, | ||
| useHeaderHeight, | ||
| StackActions, | ||
| GestureHandlerRootView, | ||
| PortalHost, | ||
| SafeAreaProvider, | ||
| useSafeAreaInsets | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { createNativeStackNavigator as createStackNavigator } from '@react-navigation/native-stack' | ||
| import { NavigationContainer, StackActions } from '@react-navigation/native' | ||
| import PortalHost from '@mpxjs/webpack-plugin/lib/runtime/components/react/dist/mpx-portal/portal-host' | ||
| import { useHeaderHeight } from '@react-navigation/elements' | ||
| import { SafeAreaProvider, useSafeAreaInsets } from 'react-native-safe-area-context' | ||
| import { GestureHandlerRootView } from 'react-native-gesture-handler' | ||
|
|
||
| export { | ||
| createStackNavigator, | ||
| NavigationContainer, | ||
| useHeaderHeight, | ||
| StackActions, | ||
| GestureHandlerRootView, | ||
| PortalHost, | ||
| SafeAreaProvider, | ||
| useSafeAreaInsets | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,15 +2,20 @@ import { useEffect, useLayoutEffect, useSyncExternalStore, useRef, useMemo, crea | |
| import * as ReactNative from 'react-native' | ||
| import { ReactiveEffect } from '../../observer/effect' | ||
| import { watch } from '../../observer/watch' | ||
| import { reactive, set, del } from '../../observer/reactive' | ||
| import { hasOwn, isFunction, noop, isObject, isArray, getByPath, collectDataset, hump2dash, dash2hump, callWithErrorHandling, wrapMethodsWithErrorHandling } from '@mpxjs/utils' | ||
| import { del, reactive, set } from '../../observer/reactive' | ||
| import { hasOwn, isFunction, noop, isObject, isArray, getByPath, collectDataset, hump2dash, dash2hump, callWithErrorHandling, wrapMethodsWithErrorHandling, error } from '@mpxjs/utils' | ||
| import MpxProxy from '../../core/proxy' | ||
| import { BEFOREUPDATE, ONLOAD, UPDATED, ONSHOW, ONHIDE, ONRESIZE, REACTHOOKSEXEC } from '../../core/innerLifecycle' | ||
| import mergeOptions from '../../core/mergeOptions' | ||
| import { queueJob, hasPendingJob } from '../../observer/scheduler' | ||
| import { createSelectorQuery, createIntersectionObserver } from '@mpxjs/api-proxy' | ||
| import { IntersectionObserverContext, RouteContext, KeyboardAvoidContext } from '@mpxjs/webpack-plugin/lib/runtime/components/react/dist/context' | ||
| import MpxKeyboardAvoidingView from '@mpxjs/webpack-plugin/lib/runtime/components/react/dist/mpx-keyboard-avoiding-view' | ||
| import { | ||
| IntersectionObserverContext, | ||
| KeyboardAvoidContext, | ||
| RouteContext | ||
| } from '@mpxjs/webpack-plugin/lib/runtime/components/react/dist/context' | ||
| import { PortalHost, useSafeAreaInsets, GestureHandlerRootView, useHeaderHeight } from '../env/navigationHelper' | ||
|
|
||
| const ProviderContext = createContext(null) | ||
| function getSystemInfo () { | ||
|
|
@@ -443,6 +448,141 @@ const checkRelation = (options) => { | |
|
|
||
| // 临时用来存储安卓底部(iOS没有这个)的高度(虚拟按键等高度)根据第一次进入推算 | ||
| let bottomVirtualHeight = null | ||
| export function PageWrapperHOC (WrappedComponent) { | ||
| return function PageWrapperCom ({ navigation, route, pageConfig = {}, ...props }) { | ||
| const rootRef = useRef(null) | ||
| const keyboardAvoidRef = useRef(null) | ||
| const intersectionObservers = useRef({}) | ||
| const currentPageId = useMemo(() => ++pageId, []) | ||
| const routeContextValRef = useRef({ | ||
| navigation, | ||
| pageId: currentPageId | ||
| }) | ||
| const currentPageConfig = Object.assign({}, global.__mpxPageConfig, pageConfig) | ||
| if (!navigation || !route) { | ||
| // 独立组件使用时要求传递navigation | ||
| error('Using pageWrapper requires passing navigation and route') | ||
| return null | ||
| } | ||
| usePageStatus(navigation, currentPageId) | ||
| useLayoutEffect(() => { | ||
| navigation.setOptions({ | ||
| title: pageConfig.navigationBarTitleText?.trim() || '', | ||
| headerStyle: { | ||
| backgroundColor: pageConfig.navigationBarBackgroundColor || '#000000' | ||
| }, | ||
| headerTintColor: pageConfig.navigationBarTextStyle || 'white' | ||
| }) | ||
|
|
||
| // TODO 此部分内容在native-stack可删除,用setOptions设置 | ||
| if (__mpx_mode__ !== 'ios') { | ||
| ReactNative.StatusBar.setBarStyle(pageConfig.barStyle || 'dark-content') | ||
| ReactNative.StatusBar.setTranslucent(true) // 控制statusbar是否占位 | ||
| ReactNative.StatusBar.setBackgroundColor('transparent') | ||
| } | ||
| }, []) | ||
|
|
||
| const headerHeight = useHeaderHeight() | ||
| const onLayout = () => { | ||
| const screenDimensions = ReactNative.Dimensions.get('screen') | ||
| if (__mpx_mode__ === 'ios') { | ||
| navigation.layout = { | ||
| x: 0, | ||
| y: headerHeight, | ||
| width: screenDimensions.width, | ||
| height: screenDimensions.height - headerHeight | ||
| } | ||
| } else { | ||
| if (bottomVirtualHeight === null) { | ||
| rootRef.current?.measureInWindow((x, y, width, height) => { | ||
| // 沉浸模式的计算方式 | ||
| bottomVirtualHeight = screenDimensions.height - height - headerHeight | ||
| // 非沉浸模式(translucent=true)计算方式, 现在默认是全用沉浸模式,所以先不算这个 | ||
| // bottomVirtualHeight = windowDimensions.height - height - headerHeight | ||
| navigation.layout = { | ||
| x: 0, | ||
| y: headerHeight, | ||
| width: screenDimensions.width, | ||
| height: height | ||
| } | ||
| }) | ||
| } else { | ||
| navigation.layout = { | ||
| x: 0, | ||
| y: headerHeight, // 这个y值 | ||
| width: screenDimensions.width, | ||
| // 后续页面的layout是通过第一次路由进入时候推算出来的底部区域来推算出来的 | ||
| height: screenDimensions.height - bottomVirtualHeight - headerHeight | ||
| } | ||
| } | ||
| } | ||
| } | ||
| const withKeyboardAvoidingView = (element) => { | ||
| return createElement(KeyboardAvoidContext.Provider, | ||
| { | ||
| value: keyboardAvoidRef | ||
| }, | ||
| createElement(MpxKeyboardAvoidingView, | ||
| { | ||
| style: { | ||
| flex: 1 | ||
| }, | ||
| contentContainerStyle: { | ||
| flex: 1 | ||
| } | ||
| }, | ||
| element | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| navigation.insets = useSafeAreaInsets() | ||
|
|
||
| return createElement(GestureHandlerRootView, | ||
| { | ||
| // https://github.com/software-mansion/react-native-reanimated/issues/6639 因存在此问题,iOS在页面上进行定宽来暂时规避 | ||
| style: __mpx_mode__ === 'ios' && currentPageConfig?.navigationStyle !== 'custom' | ||
| ? { | ||
| height: ReactNative.Dimensions.get('screen').height - useHeaderHeight() | ||
| } | ||
| : { | ||
| flex: 1 | ||
| } | ||
| }, | ||
| withKeyboardAvoidingView( | ||
| createElement(ReactNative.View, | ||
| { | ||
| style: { | ||
| flex: 1, | ||
| backgroundColor: currentPageConfig?.backgroundColor || '#fff' | ||
| }, | ||
| ref: rootRef, | ||
| onLayout | ||
| }, | ||
| createElement(RouteContext.Provider, | ||
| { | ||
| value: routeContextValRef.current | ||
| }, | ||
| createElement(IntersectionObserverContext.Provider, | ||
| { | ||
| value: intersectionObservers.current | ||
| }, | ||
| createElement(PortalHost, | ||
| null, | ||
| createElement(WrappedComponent, { | ||
| ...props, | ||
| navigation, | ||
| route, | ||
| id: currentPageId | ||
| }) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| )) | ||
| } | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const pageWrapper = (component)=>{ const page = (props)=>{ const page2 =pageWrapper(page) |
||
| export function getDefaultOptions ({ type, rawOptions = {}, currentInject }) { | ||
| rawOptions = mergeOptions(rawOptions, type, false) | ||
| const components = Object.assign({}, rawOptions.components, currentInject.getComponents()) | ||
|
|
@@ -578,139 +718,12 @@ export function getDefaultOptions ({ type, rawOptions = {}, currentInject }) { | |
| } | ||
|
|
||
| if (type === 'page') { | ||
| const { PortalHost, useSafeAreaInsets, GestureHandlerRootView, useHeaderHeight } = global.__navigationHelper | ||
| const pageConfig = Object.assign({}, global.__mpxPageConfig, currentInject.pageConfig) | ||
| const Page = ({ navigation, route }) => { | ||
| const currentPageId = useMemo(() => ++pageId, []) | ||
| const intersectionObservers = useRef({}) | ||
| const routeContextValRef = useRef({ | ||
| pageId: currentPageId, | ||
| navigation | ||
| return (props) => { | ||
| createElement(PageWrapperHOC(defaultOptions), { | ||
| pageConfig: currentInject.pageConfig, | ||
| ...props | ||
| }) | ||
| usePageStatus(navigation, currentPageId) | ||
| useLayoutEffect(() => { | ||
| navigation.setOptions({ | ||
| title: pageConfig.navigationBarTitleText?.trim() || '', | ||
| headerStyle: { | ||
| backgroundColor: pageConfig.navigationBarBackgroundColor || '#000000' | ||
| }, | ||
| headerTintColor: pageConfig.navigationBarTextStyle || 'white' | ||
| }) | ||
|
|
||
| // TODO 此部分内容在native-stack可删除,用setOptions设置 | ||
| if (__mpx_mode__ !== 'ios') { | ||
| ReactNative.StatusBar.setBarStyle(pageConfig.barStyle || 'dark-content') | ||
| ReactNative.StatusBar.setTranslucent(true) // 控制statusbar是否占位 | ||
| ReactNative.StatusBar.setBackgroundColor('transparent') | ||
| } | ||
| }, []) | ||
|
|
||
| const rootRef = useRef(null) | ||
| const keyboardAvoidRef = useRef(null) | ||
| const headerHeight = useHeaderHeight() | ||
| const onLayout = () => { | ||
| const screenDimensions = ReactNative.Dimensions.get('screen') | ||
| if (__mpx_mode__ === 'ios') { | ||
| navigation.layout = { | ||
| x: 0, | ||
| y: headerHeight, | ||
| width: screenDimensions.width, | ||
| height: screenDimensions.height - headerHeight | ||
| } | ||
| } else { | ||
| if (bottomVirtualHeight === null) { | ||
| rootRef.current?.measureInWindow((x, y, width, height) => { | ||
| // 沉浸模式的计算方式 | ||
| bottomVirtualHeight = screenDimensions.height - height - headerHeight | ||
| // 非沉浸模式(translucent=true)计算方式, 现在默认是全用沉浸模式,所以先不算这个 | ||
| // bottomVirtualHeight = windowDimensions.height - height - headerHeight | ||
| navigation.layout = { | ||
| x: 0, | ||
| y: headerHeight, | ||
| width: screenDimensions.width, | ||
| height: height | ||
| } | ||
| }) | ||
| } else { | ||
| navigation.layout = { | ||
| x: 0, | ||
| y: headerHeight, // 这个y值 | ||
| width: screenDimensions.width, | ||
| // 后续页面的layout是通过第一次路由进入时候推算出来的底部区域来推算出来的 | ||
| height: screenDimensions.height - bottomVirtualHeight - headerHeight | ||
| } | ||
| } | ||
| } | ||
| } | ||
| const withKeyboardAvoidingView = (element) => { | ||
| return createElement(KeyboardAvoidContext.Provider, | ||
| { | ||
| value: keyboardAvoidRef | ||
| }, | ||
| createElement(MpxKeyboardAvoidingView, | ||
| { | ||
| style: { | ||
| flex: 1 | ||
| }, | ||
| contentContainerStyle: { | ||
| flex: 1 | ||
| } | ||
| }, | ||
| element | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| navigation.insets = useSafeAreaInsets() | ||
|
|
||
| return createElement(GestureHandlerRootView, | ||
| { | ||
| // https://github.com/software-mansion/react-native-reanimated/issues/6639 因存在此问题,iOS在页面上进行定宽来暂时规避 | ||
| style: __mpx_mode__ === 'ios' && pageConfig.navigationStyle !== 'custom' | ||
| ? { | ||
| height: ReactNative.Dimensions.get('screen').height - useHeaderHeight() | ||
| } | ||
| : { | ||
| flex: 1 | ||
| } | ||
| }, | ||
| withKeyboardAvoidingView( | ||
| createElement(ReactNative.View, | ||
| { | ||
| style: { | ||
| flex: 1, | ||
| backgroundColor: pageConfig.backgroundColor || '#ffffff' | ||
| }, | ||
| ref: rootRef, | ||
| // 测试过了 键盘拉起后不会重新触发onLayout | ||
| onLayout | ||
| }, | ||
| createElement(RouteContext.Provider, | ||
| { | ||
| value: routeContextValRef.current | ||
| }, | ||
| createElement(IntersectionObserverContext.Provider, | ||
| { | ||
| value: intersectionObservers.current | ||
| }, | ||
| createElement(PortalHost, | ||
| null, | ||
| createElement(defaultOptions, | ||
| { | ||
| navigation, | ||
| route, | ||
| id: currentPageId | ||
| } | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| // todo custom portal host for active route | ||
| } | ||
| return Page | ||
| } | ||
| return defaultOptions | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
传递error对象,以及可以直接return