Skip to content

Commit

Permalink
Merge branch 'master' into feat-image-opt
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyuki authored Dec 12, 2024
2 parents 7a3cb8a + 06df0a2 commit 3109389
Show file tree
Hide file tree
Showing 41 changed files with 1,147 additions and 1,181 deletions.
3 changes: 3 additions & 0 deletions docs-vuepress/guide/platform/rn.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

#### Webview API

#### 其他使用限制
如事件的target等




Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"tsc:build": "npm --workspace=./packages/webpack-plugin run build"
},
"devDependencies": {
"@babel/core": "^7.8.7",
"@babel/core": "^7.25.2",
"@babel/eslint-parser": "^7.18.9",
"@babel/preset-env": "^7.25.3",
"@docsearch/css": "^3.0.0",
Expand Down
43 changes: 11 additions & 32 deletions packages/core/src/convertor/convertor.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as wxLifecycle from '../platform/patch/wx/lifecycle'
import * as aliLifecycle from '../platform/patch/ali/lifecycle'
import * as webLifecycle from '../platform/patch/web/lifecycle'
import * as swanLifecycle from '../platform/patch/swan/lifecycle'
import { LIFECYCLE, lifecycleProxyMap, pageMode } from '../platform/patch/lifecycle/index'
import { mergeLifecycle } from './mergeLifecycle'
import { error } from '@mpxjs/utils'
import { error, extend } from '@mpxjs/utils'
import wxToAliRule from './wxToAli'
import wxToWebRule from './wxToWeb'
import wxToSwanRule from './wxToSwan'
Expand All @@ -13,24 +10,6 @@ import wxToDdRule from './wxToDd'
import wxToJdRule from './wxToJd'
import wxToReactRule from './wxToReact'

// 根据当前环境获取的默认生命周期信息
let lifecycleInfo
let pageMode

if (__mpx_mode__ === 'web') {
lifecycleInfo = webLifecycle
pageMode = ''
} else if (__mpx_mode__ === 'ali') {
lifecycleInfo = aliLifecycle
pageMode = ''
} else if (__mpx_mode__ === 'swan') {
lifecycleInfo = swanLifecycle
pageMode = 'blend'
} else {
lifecycleInfo = wxLifecycle
pageMode = 'blend'
}

/**
* 转换规则包含四点
* lifecycle [object] 生命周期
Expand All @@ -40,25 +19,25 @@ if (__mpx_mode__ === 'web') {
* convert [function] 自定义转换函数, 接收一个options
*/
const defaultConvertRule = {
lifecycle: mergeLifecycle(lifecycleInfo.LIFECYCLE),
lifecycleProxyMap: lifecycleInfo.lifecycleProxyMap,
lifecycle: mergeLifecycle(LIFECYCLE),
lifecycleProxyMap: lifecycleProxyMap,
pageMode,
support: !!pageMode,
convert: null
}

const rulesMap = {
local: { ...defaultConvertRule },
local: extend({}, defaultConvertRule),
default: defaultConvertRule,
wxToWeb: wxToWebRule,
wxToAli: wxToAliRule,
wxToSwan: wxToSwanRule,
wxToQq: { ...defaultConvertRule, ...wxToQqRule },
wxToTt: { ...defaultConvertRule, ...wxToTtRule },
wxToDd: { ...defaultConvertRule, ...wxToDdRule },
wxToJd: { ...defaultConvertRule, ...wxToJdRule },
wxToIos: { ...defaultConvertRule, ...wxToReactRule },
wxToAndroid: { ...defaultConvertRule, ...wxToReactRule }
wxToQq: extend({}, defaultConvertRule, wxToQqRule),
wxToTt: extend({}, defaultConvertRule, wxToTtRule),
wxToDd: extend({}, defaultConvertRule, wxToDdRule),
wxToJd: extend({}, defaultConvertRule, wxToJdRule),
wxToIos: extend({}, defaultConvertRule, wxToReactRule),
wxToAndroid: extend({}, defaultConvertRule, wxToReactRule)
}

export function getConvertRule (convertMode) {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/convertor/wxToAli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as wxLifecycle from '../platform/patch/wx/lifecycle'
import * as aliLifecycle from '../platform/patch/ali/lifecycle'
import * as wxLifecycle from '../platform/patch/lifecycle/index.wx'
import { LIFECYCLE } from '../platform/patch/lifecycle/index'
import { mergeLifecycle } from './mergeLifecycle'
import { error, hasOwn, isDev } from '@mpxjs/utils'
import { implemented } from '../core/implement'
Expand Down Expand Up @@ -38,7 +38,7 @@ function notSupportTip (options) {

export default {
lifecycle: mergeLifecycle(wxLifecycle.LIFECYCLE),
lifecycle2: mergeLifecycle(aliLifecycle.LIFECYCLE),
lifecycle2: mergeLifecycle(LIFECYCLE),
pageMode: 'blend',
support: false,
lifecycleProxyMap: wxLifecycle.lifecycleProxyMap,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/convertor/wxToSwan.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { error, isDev } from '@mpxjs/utils'
import { implemented } from '../core/implement'
import { mergeLifecycle } from './mergeLifecycle'
import * as wxLifecycle from '../platform/patch/wx/lifecycle'
import * as swanLifecycle from '../platform/patch/swan/lifecycle'
import * as wxLifecycle from '../platform/patch/lifecycle/index.wx'
import { LIFECYCLE } from '../platform/patch/lifecycle/index'

const BEHAVIORS_MAP = {
'wx://form-field': 'swan://form-field',
Expand Down Expand Up @@ -30,7 +30,7 @@ function notSupportTip (options) {

export default {
lifecycle: mergeLifecycle(wxLifecycle.LIFECYCLE),
lifecycle2: mergeLifecycle(swanLifecycle.LIFECYCLE),
lifecycle2: mergeLifecycle(LIFECYCLE),
pageMode: 'blend',
support: true,
lifecycleProxyMap: wxLifecycle.lifecycleProxyMap,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/convertor/wxToWeb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as wxLifecycle from '../platform/patch/wx/lifecycle'
import * as webLifecycle from '../platform/patch/web/lifecycle'
import * as wxLifecycle from '../platform/patch/lifecycle/index.wx'
import { LIFECYCLE } from '../platform/patch/lifecycle/index'
import { mergeLifecycle } from './mergeLifecycle'
import {
isObject,
Expand Down Expand Up @@ -32,7 +32,7 @@ function notSupportTip (options) {

export default {
lifecycle: mergeLifecycle(wxLifecycle.LIFECYCLE),
lifecycle2: mergeLifecycle(webLifecycle.LIFECYCLE),
lifecycle2: mergeLifecycle(LIFECYCLE),
pageMode: 'blend',
support: true,
lifecycleProxyMap: wxLifecycle.lifecycleProxyMap,
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/observer/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { RefKey } from '../helper/const'
import {
warn,
isPlainObject,
hasOwn
hasOwn,
extend
} from '@mpxjs/utils'

export class RefImpl {
constructor (options) {
Object.defineProperty(this, 'value', { enumerable: true, ...options })
Object.defineProperty(this, 'value', extend({ enumerable: true }, options))
}
}

Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/observer/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ import {
isArray,
remove,
callWithErrorHandling,
hasChanged
hasChanged,
extend
} from '@mpxjs/utils'

export function watchEffect (effect, options) {
return watch(effect, null, options)
}

export function watchPostEffect (effect, options) {
return watch(effect, null, { ...options, flush: 'post' })
return watch(effect, null, extend({}, options, { flush: 'post' }))
}

export function watchSyncEffect (effect, options) {
return watch(effect, null, { ...options, flush: 'sync' })
return watch(effect, null, extend({}, options, { flush: 'sync' }))
}

const warnInvalidSource = (s) => {
Expand All @@ -34,7 +35,7 @@ const warnInvalidSource = (s) => {
const shouldTrigger = (value, oldValue) => hasChanged(value, oldValue) || isObject(value)

const processWatchOptionsCompat = (options) => {
const newOptions = { ...options }
const newOptions = extend({}, options)
if (options.sync) {
newOptions.flush = 'sync'
}
Expand Down
33 changes: 22 additions & 11 deletions packages/core/src/platform/createApp.ios.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import transferOptions from '../core/transferOptions'
import builtInKeysMap from './patch/builtInKeysMap'
import { makeMap, spreadProp, parseUrlQuery, getFocusedNavigation } from '@mpxjs/utils'
import { makeMap, spreadProp, parseUrlQuery, getFocusedNavigation, hasOwn, extend } from '@mpxjs/utils'
import { mergeLifecycle } from '../convertor/mergeLifecycle'
import * as wxLifecycle from '../platform/patch/wx/lifecycle'
import { LIFECYCLE } from '../platform/patch/lifecycle/index'
import Mpx from '../index'
import { createElement, memo, useRef, useEffect } from 'react'
import * as ReactNative from 'react-native'
import { ref } from '../observer/ref'

const appHooksMap = makeMap(mergeLifecycle(wxLifecycle.LIFECYCLE).app)
const appHooksMap = makeMap(mergeLifecycle(LIFECYCLE).app)

function getOrientation (window = ReactNative.Dimensions.get('window')) {
return window.width > window.height ? 'landscape' : 'portrait'
Expand All @@ -30,11 +30,7 @@ function filterOptions (options, appData) {
}

function createAppInstance (appData) {
const instance = {
...Mpx.prototype,
...appData
}
return instance
return extend({}, Mpx.prototype, appData)
}

export default function createApp (option, config = {}) {
Expand Down Expand Up @@ -159,12 +155,17 @@ export default function createApp (option, config = {}) {
global.__mpxAppCbs.show.forEach((cb) => {
cb(options)
})
global.__mpxAppFocusedState.value = 'show'
if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
global.__mpxPageStatusMap[navigation.pageId] = 'show'
}
} else if (currentState === 'inactive') {
global.__mpxAppCbs.hide.forEach((cb) => {
cb()
})
global.__mpxAppFocusedState.value = 'hide'
const navigation = getFocusedNavigation()
if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
global.__mpxPageStatusMap[navigation.pageId] = 'hide'
}
}
})

Expand All @@ -174,7 +175,10 @@ export default function createApp (option, config = {}) {
const orientation = getOrientation(window)
if (orientation === lastOrientation) return
lastOrientation = orientation
global.__mpxAppFocusedState.value = `resize${count++}`
const navigation = getFocusedNavigation()
if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
global.__mpxPageStatusMap[navigation.pageId] = `resize${count++}`
}
})
return () => {
changeSubscription && changeSubscription.remove()
Expand Down Expand Up @@ -216,4 +220,11 @@ export default function createApp (option, config = {}) {
}
return []
}

global.setCurrentPageStatus = function (status) {
const navigation = getFocusedNavigation()
if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
global.__mpxPageStatusMap[navigation.pageId] = status
}
}
}
6 changes: 3 additions & 3 deletions packages/core/src/platform/createApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import mergeOptions from '../core/mergeOptions'
import builtInKeysMap from './patch/builtInKeysMap'
import { makeMap, spreadProp, isBrowser } from '@mpxjs/utils'
import { mergeLifecycle } from '../convertor/mergeLifecycle'
import * as webLifecycle from '../platform/patch/web/lifecycle'
import { LIFECYCLE } from '../platform/patch/lifecycle/index'
import Mpx from '../index'
import { initAppProvides } from './export/apiInject'

const webAppHooksMap = makeMap(mergeLifecycle(webLifecycle.LIFECYCLE).app)
const appHooksMap = makeMap(mergeLifecycle(LIFECYCLE).app)

function filterOptions (options, appData) {
const newOptions = {}
Object.keys(options).forEach(key => {
if (builtInKeysMap[key]) {
return
}
if (__mpx_mode__ === 'web' && !webAppHooksMap[key] && key !== 'provide') {
if (__mpx_mode__ === 'web' && !appHooksMap[key] && key !== 'provide') {
appData[key] = options[key]
} else {
newOptions[key] = options[key]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import MpxProxy from '../../../core/proxy'
import builtInKeysMap from '../builtInKeysMap'
import mergeOptions from '../../../core/mergeOptions'
import MpxProxy from '../../core/proxy'
import builtInKeysMap from './builtInKeysMap'
import mergeOptions from '../../core/mergeOptions'
import { error, diffAndCloneA, hasOwn, noop, wrapMethodsWithErrorHandling } from '@mpxjs/utils'

function transformApiForProxy (context, currentInject) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useEffect, useLayoutEffect, useSyncExternalStore, useRef, useMemo, useState, useCallback, createElement, memo, forwardRef, useImperativeHandle, useContext, Fragment, cloneElement } from 'react'
import * as ReactNative from 'react-native'
import { ReactiveEffect } from '../../../observer/effect'
import { watch } from '../../../observer/watch'
import { reactive, set, del } from '../../../observer/reactive'
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, wrapMethodsWithErrorHandling } 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 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'

Expand Down Expand Up @@ -259,6 +259,7 @@ function createInstance ({ propsRef, type, rawOptions, currentInject, validProps
if (type === 'page') {
const props = propsRef.current
instance.route = props.route.name
global.__mpxPagesMap = global.__mpxPagesMap || {}
global.__mpxPagesMap[props.route.key] = [instance, props.navigation]
}

Expand Down Expand Up @@ -337,8 +338,8 @@ function usePageEffect (mpxProxy, pageId) {
const hasHideHook = hasPageHook(mpxProxy, [ONHIDE, 'hide'])
const hasResizeHook = hasPageHook(mpxProxy, [ONRESIZE, 'resize'])
if (hasShowHook || hasHideHook || hasResizeHook) {
if (hasOwn(pageStatusContext, pageId)) {
unWatch = watch(() => pageStatusContext[pageId], (newVal) => {
if (hasOwn(pageStatusMap, pageId)) {
unWatch = watch(() => pageStatusMap[pageId], (newVal) => {
if (newVal === 'show' || newVal === 'hide') {
triggerPageStatusHook(mpxProxy, newVal)
} else if (/^resize/.test(newVal)) {
Expand All @@ -353,32 +354,28 @@ function usePageEffect (mpxProxy, pageId) {
}, [])
}

const pageStatusContext = reactive({})
let pageId = 0
const pageStatusMap = global.__mpxPageStatusMap = reactive({})

function usePageStatus (navigation, pageId) {
let isFocused = true
set(pageStatusContext, pageId, '')
navigation.pageId = pageId
set(pageStatusMap, pageId, '')
useEffect(() => {
const focusSubscription = navigation.addListener('focus', () => {
pageStatusContext[pageId] = 'show'
isFocused = true
pageStatusMap[pageId] = 'show'
})
const blurSubscription = navigation.addListener('blur', () => {
pageStatusContext[pageId] = 'hide'
isFocused = false
pageStatusMap[pageId] = 'hide'
})
const unWatchAppFocusedState = watch(global.__mpxAppFocusedState, (value) => {
if (isFocused) {
pageStatusContext[pageId] = value
}
pageStatusMap[pageId] = value
})

return () => {
focusSubscription()
blurSubscription()
unWatchAppFocusedState()
del(pageStatusContext, pageId)
del(pageStatusMap, pageId)
}
}, [navigation])
}
Expand Down
Loading

0 comments on commit 3109389

Please sign in to comment.