Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/didi/mpx
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyuki committed Oct 16, 2024
2 parents 14956e3 + e875e7f commit 1837613
Show file tree
Hide file tree
Showing 15 changed files with 443 additions and 126 deletions.
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"@docsearch/js": "^3.0.0",
"@testing-library/jest-dom": "^4.2.4",
"@types/jest": "^27.0.1",
"@typescript-eslint/eslint-plugin": "^5.2.0",
"@typescript-eslint/parser": "^5.2.0",
"@vuepress/plugin-back-to-top": "^1.8.2",
"@vuepress/plugin-pwa": "^1.8.0",
"eslint": "^7.32.0",
Expand All @@ -30,8 +32,6 @@
"eslint-plugin-jest": "^27.0.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.1",
"@typescript-eslint/eslint-plugin": "^5.2.0",
"@typescript-eslint/parser": "^5.2.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^27.2.0",
"lerna": "^8.1.8",
Expand All @@ -40,5 +40,9 @@
},
"workspaces": [
"packages/*"
]
],
"dependencies": {
"@types/react": "^18.3.11",
"react-native-linear-gradient": "^2.8.3"
}
}
6 changes: 5 additions & 1 deletion packages/api-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"expo-clipboard": "~6.0.3",
"react-native-device-info": "^10.13.2",
"react-native-get-location": "^4.0.1",
"react-native-safe-area-context": "^4.10.1"
"react-native-safe-area-context": "^4.10.1",
"react-native-haptic-feedback": "^2.3.3"
},
"peerDependenciesMeta": {
"@react-native-async-storage/async-storage": {
Expand All @@ -74,6 +75,9 @@
},
"expo-brightness": {
"optional": true
},
"react-native-haptic-feedback": {
"optional": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
hump2dash,
isArray
} from '@mpxjs/utils'
import { StyleSheet } from 'react-native'

const flushRefFns = (nodeInstances, fns, single) => {
// wx的数据格式:对于具体方法接受到的回调传参,如果获取的 nodeRef 只有一个,那么只需要返回一条数据而不是数组,但是 exec 里面统一都是数组
Expand Down Expand Up @@ -61,18 +60,18 @@ const getMeasureProps = (measureProps = []) => {
}

const getDataset = (props) => {
return wrapFn((nodeRef, resolve) => {
props = nodeRef.props.current
return wrapFn((nodeInstance, resolve) => {
props = nodeInstance.props.current
resolve({
dataset: collectDataset(props)
})
})
}

const getPlainProps = (config) => {
return wrapFn((nodeRef, resolve) => {
return wrapFn((nodeInstance, resolve) => {
const res = {}
const props = nodeRef.props.current
const props = nodeInstance.props.current
config.forEach((key) => {
// props 属性默认不转驼峰,用户写什么格式不会变化,取值做兼容
res[key] = props[key] || props[hump2dash(key)] || ''
Expand All @@ -82,12 +81,15 @@ const getPlainProps = (config) => {
}

const getComputedStyle = (config = []) => {
return wrapFn((nodeRef, resolve) => {
return wrapFn((nodeInstance, resolve) => {
config = new Set(config)
const res = {}
const styles = nodeRef.props.current.style || []
const defaultStyle = nodeRef.instance.defaultStyle || {}
const computedStyle = StyleSheet.flatten([defaultStyle, ...styles])
const styles = nodeInstance.props.current.style || {}
const defaultStyle = nodeInstance.instance.defaultStyle || {}
const computedStyle = {
...defaultStyle,
...styles
}
config.forEach((key) => {
const humpKey = dash2hump(key)
// 取 style 的 key 是根据传入的 key 来设置,传什么设置什么 key,只不过取值需要做兼容
Expand All @@ -99,8 +101,8 @@ const getComputedStyle = (config = []) => {
}

const getInstanceConfig = (config) => {
return wrapFn((nodeRef, resolve) => {
const instance = nodeRef.instance
return wrapFn((nodeInstance, resolve) => {
const instance = nodeInstance.instance
resolve({ [config]: instance[config] || {} })
})
}
Expand All @@ -113,8 +115,8 @@ const defaultScrollOffset = {
}

const getScrollOffset = () => {
return wrapFn((nodeRef, resolve) => {
const instance = nodeRef.instance
return wrapFn((nodeInstance, resolve) => {
const instance = nodeInstance.instance
resolve(
(instance.scrollOffset && instance.scrollOffset.current) ||
defaultScrollOffset
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index'
62 changes: 62 additions & 0 deletions packages/api-proxy/src/platform/api/keyboard/index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Keyboard } from 'react-native'
import { successHandle, failHandle } from '../../../common/js'
let hasListener = false
const callbacks = []

function keyboardShowListener (e) {
const endCoordinates = e.endCoordinates || {}
// eslint-disable-next-line node/no-callback-literal
callbacks.forEach(cb => cb({
height: endCoordinates.height
}))
}
function keyboardHideListener (e) {
const endCoordinates = e.endCoordinates || {}
let height
if (__mpx_mode__ === 'ios') {
height = 0
} else {
height = endCoordinates.height
}
// eslint-disable-next-line node/no-callback-literal
callbacks.forEach(cb => cb({
height
}))
}
const onKeyboardHeightChange = function (callback) {
if (!hasListener) {
Keyboard.addListener('keyboardDidShow', keyboardShowListener)
Keyboard.addListener('keyboardDidHide', keyboardHideListener)
hasListener = true
}
callbacks.push(callback)
}
const offKeyboardHeightChange = function (callback) {
const index = callbacks.indexOf(callback)
if (index > -1) {
callbacks.splice(index, 1)
}
if (callbacks.length === 0) {
Keyboard.removeAllListeners('keyboardDidShow')
Keyboard.removeAllListeners('keyboardDidHide')
hasListener = false
}
}

const hideKeyboard = function (options = {}) {
const { success, fail, complete } = options
try {
Keyboard.dismiss()
const result = { errMsg: 'hideKeyboard:ok' }
successHandle(result, success, complete)
} catch (err) {
const result = { errMsg: err.message }
failHandle(result, fail, complete)
}
}

export {
onKeyboardHeightChange,
offKeyboardHeightChange,
hideKeyboard
}
13 changes: 13 additions & 0 deletions packages/api-proxy/src/platform/api/keyboard/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ENV_OBJ, envError } from '../../../common/js'

const onKeyboardHeightChange = ENV_OBJ.onKeyboardHeightChange || envError('onKeyboardHeightChange')

const offKeyboardHeightChange = ENV_OBJ.offKeyboardHeightChange || envError('offKeyboardHeightChange')

const hideKeyboard = ENV_OBJ.hideKeyboard || envError('hideKeyboard')

export {
onKeyboardHeightChange,
offKeyboardHeightChange,
hideKeyboard
}
8 changes: 6 additions & 2 deletions packages/api-proxy/src/platform/api/modal/rnModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ const showModal = function (options = {}) {
const modalWidth = width - 60
const styles = StyleSheet.create({
modalTask: {
width,
height,
left: 0,
right: 0,
top: 0,
bottom: 0,
justifyContent: 'center',
alignItems: 'center',
display: 'flex',
backgroundColor: 'rgba(0,0,0,0.6)',
position: 'absolute'
},
Expand Down Expand Up @@ -144,6 +147,7 @@ const showModal = function (options = {}) {
height: 40,
backgroundColor: '#eeeeee',
width: '100%',
keyboardType: 'default',
paddingLeft: 10,
paddingRight: 10
}} onChangeText={text => onChangeText(text)} defaultValue={content}></TextInput></View>)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { successHandle, failHandle, getFocusedNavigation } from '../../../common/js'

import { nextTick } from '../next-tick'
function setNavigationBarTitle (options = {}) {
const { title = '', success, fail, complete } = options
const navigation = getFocusedNavigation()
if (!(navigation && navigation.setOptions)) {
failHandle({ errMsg: 'setNavigationBarTitle:fail' }, fail, complete)
} else {
navigation.setOptions({ headerTitle: title })
successHandle({ errMsg: 'setNavigationBarTitle:ok' }, success, complete)
nextTick(() => {
navigation.setOptions({ headerTitle: title })
successHandle({ errMsg: 'setNavigationBarTitle:ok' }, success, complete)
})
}
}

Expand All @@ -17,13 +19,15 @@ function setNavigationBarColor (options = {}) {
if (!(navigation && navigation.setOptions)) {
failHandle({ errMsg: 'setNavigationBarColor:fail' }, fail, complete)
} else {
navigation.setOptions({
headerStyle: {
backgroundColor: backgroundColor
},
headerTintColor: frontColor
nextTick(() => {
navigation.setOptions({
headerStyle: {
backgroundColor: backgroundColor
},
headerTintColor: frontColor
})
successHandle({ errMsg: 'setNavigationBarColor:ok' }, success, complete)
})
successHandle({ errMsg: 'setNavigationBarColor:ok' }, success, complete)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index.ios'
39 changes: 39 additions & 0 deletions packages/api-proxy/src/platform/api/vibrate/index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import ReactNativeHapticFeedback from 'react-native-haptic-feedback'
import { Vibration } from 'react-native'
import { successHandle, failHandle } from '../../../common/js'

const getType = function (type = 'light') {
return 'impact' + type[0].toUpperCase() + type.substr(1)
}
const vibrateShort = function (options = {}) {
const { type = 'light', success, fail, complete } = options
try {
ReactNativeHapticFeedback.trigger(getType(type), {
ignoreAndroidSystemSettings: true,
enableVibrateFallback: true
})
const result = {
errMsg: 'vibrateShort:ok'
}
successHandle(result, success, complete)
} catch (e) {
const result = {
errMsg: 'vibrateShort:fail'
}
failHandle(result, fail, complete)
}
}

const vibrateLong = function (options = {}) {
const { success, complete } = options
Vibration.vibrate(400)
const result = {
errMsg: 'vibrateLong:ok'
}
successHandle(result, success, complete)
}

export {
vibrateShort,
vibrateLong
}
10 changes: 10 additions & 0 deletions packages/api-proxy/src/platform/api/vibrate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ENV_OBJ, envError } from '../../../common/js'

const vibrateShort = ENV_OBJ.vibrateShort || envError('vibrateShort')

const vibrateLong = ENV_OBJ.vibrateLong || envError('vibrateLong')

export {
vibrateShort,
vibrateLong
}
6 changes: 6 additions & 0 deletions packages/api-proxy/src/platform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,9 @@ export * from './api/location'

// getExtConfig, getExtConfigSync
export * from './api/ext'

// vibrateShort, vibrateLong
export * from './api/vibrate'

// onKeyboardHeightChange, offKeyboardHeightChange, hideKeyboard
export * from './api/keyboard'
Loading

0 comments on commit 1837613

Please sign in to comment.