Skip to content
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

feat(View): add view component style. #1478

Merged
merged 54 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
68b1202
Merge branch 'feat-react-template-compiler' of https://github.com/did…
May 22, 2024
bbcbee7
fix(*): fix style.
May 23, 2024
622042a
fix(*): fix code
May 23, 2024
cbf3ac4
fix(*): merge code
May 23, 2024
c031efc
fix(*): fix code
May 23, 2024
cd10a10
fix(*): fix code
May 23, 2024
990fc86
fix(*): fix code
May 23, 2024
b0852e7
fix(*): fix code
May 23, 2024
fbd69d4
feat(text): fix text style.
May 23, 2024
d6466f2
fix(view): transform text array.
May 23, 2024
57439a8
fix(*): recursion.
May 23, 2024
4bd1f0b
fix(*): opt every.
May 24, 2024
ac30a34
fix(*): fix code
May 24, 2024
0407685
fix(*): fix merge code.
May 24, 2024
06358b1
fix(*): fix code
May 24, 2024
6d1e41d
fix(*): fix code
May 24, 2024
603f252
fix(*): fix code
May 24, 2024
bf3155a
fix(*): fix lint.
May 24, 2024
61bf815
fix(*): fix code
May 24, 2024
ee306ab
fix(*): merge code
May 27, 2024
329afcb
fix(*): fix line-height check.
May 27, 2024
3953b78
fix(*): fix code
May 27, 2024
3a0bfe6
fix(view): fix view.
May 28, 2024
7133079
fix(*): fix code.
May 28, 2024
f94c0e0
fix(*): fix code
May 28, 2024
3284a6e
fix(*): fix view reg.
May 28, 2024
93257df
fix(*): fix code
May 28, 2024
a76255b
fix(*): merge code
May 28, 2024
98e7234
fix(*): fix code
May 28, 2024
e2192f0
fix(*): merge code
May 28, 2024
9dd4bde
fix(*): fix code
May 28, 2024
4865b94
fix(*): fix code
May 28, 2024
b793e3b
fix(*): fix code
May 28, 2024
00eaca3
fix(*): fix code
May 28, 2024
39d5854
fix(*): fix code
May 29, 2024
f7b7bb6
fix(*): fix code
May 29, 2024
7c40d2f
fix(*): fix code
May 29, 2024
d70ea51
Merge branch 'feat-react-template-compiler' of https://github.com/did…
May 29, 2024
e7c8209
fix(*): add image.
May 30, 2024
1f8dd36
fix(*): fix code
May 30, 2024
593939d
fix(*): fix code
May 30, 2024
d523cf4
fix(*): fix view.
May 31, 2024
20dc607
fix(*): fix code
May 31, 2024
c5c31ec
fix(*): fix istext.
May 31, 2024
4e987ff
fix(*): fix varify.
May 31, 2024
b5124ef
fix(*): fix ts.
May 31, 2024
9aecbc4
fix(*): fix view.
May 31, 2024
7d06c2c
fix(*): fix format.
May 31, 2024
5f00d75
fix(*): fix code
Jun 1, 2024
a464717
fix(*): fix code
Jun 2, 2024
4fd0e5c
fix(*): fix code
Jun 2, 2024
9f7ffa4
fix(*): view effect.
Jun 3, 2024
15e39f5
fix(*): ts.
Jun 4, 2024
8a8b33b
fix(*): fix conflit.
Jun 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions packages/webpack-plugin/lib/platform/style/wx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,33 @@ module.exports = function getSpec ({ warn, error }) {
const numberRegExp = /^\s*(\d+(\.\d+)?)(rpx|px|%)?\s*$/
// RN 不支持的颜色格式
const colorRegExp = /^\s*(lab|lch|oklab|oklch|color-mix|color|hwb|lch|light-dark).*$/

const verifyValues = ({ prop, value, valueType }) => {
// 校验 value 枚举 是否支持
switch (valueType) {
case ValueType.color:
(numberRegExp.test(value)) && warn(`React Native property [${prop}]'s valueType is ${valueType}, we does not set type number`)
colorRegExp.test(value) && warn('React Native color does not support type [lab,lch,oklab,oklch,color-mix,color,hwb,lch,light-dark]')
return value
case ValueType.number:
(!numberRegExp.test(value)) && warn(`React Native property [${prop}] unit only supports [rpx,px,%]`)
return value
case ValueType.color: {
const isNumber = numberRegExp.test(value)
const isUnsupporttedColor = colorRegExp.test(value)
isNumber && warn(`React Native property [${prop}]'s valueType is ${valueType}, we does not set type number`)
isUnsupporttedColor && warn('React Native color does not support type [lab,lch,oklab,oklch,color-mix,color,hwb,lch,light-dark]')
return {
value,
nianxiongdi marked this conversation as resolved.
Show resolved Hide resolved
valid: !isNumber && !isUnsupporttedColor
}
}
case ValueType.number: {
const isNumber = numberRegExp.test(value)
!isNumber && warn(`React Native property [${prop}] unit only supports [rpx,px,%]`)
return {
value,
valid: isNumber
}
}
default:
return value
return {
value,
valid: true
}
}
}
// 统一校验 value type 值类型
Expand Down Expand Up @@ -133,7 +148,8 @@ module.exports = function getSpec ({ warn, error }) {
const valueType = keyMap[prop]
const dashProp = hump2dash(prop)
// 校验 value 类型
const value = verifyValues({ prop, value: values[idx], valueType })
verifyValues({ prop, value: values[idx], valueType })
const value = values[idx]
if (isIllegalValue({ prop: dashProp, value })) {
// 过滤不支持 value
unsupportedValueError({ prop: dashProp, value })
Expand Down Expand Up @@ -208,6 +224,15 @@ module.exports = function getSpec ({ warn, error }) {
})
}

const formatLineHeight = ({ prop, value }) => {
if (!verifyValues({ prop, value, valueType: ValueType.number }).valid) return false

return {
prop,
value: /\d+(\.\d+)?$/.test(value) ? `${Math.round(value * 100)}%` : value
}
}

const getFontVariant = ({ prop, value }) => {
if (/^(font-variant-caps|font-variant-numeric|font-variant-east-asian|font-variant-alternates|font-variant-ligatures)$/.test(prop)) {
error(`Property [${prop}] is not supported in React Native environment, please replace [font-variant]!`)
Expand Down Expand Up @@ -346,6 +371,11 @@ module.exports = function getSpec ({ warn, error }) {
ios: checkCommonValue(ValueType.color),
android: checkCommonValue(ValueType.color)
},
{ // color 颜色值校验
test: 'line-height',
ios: formatLineHeight,
android: formatLineHeight
},
{ // number 值校验
test: /.*width|height|left|right|top|bottom|radius|margin|padding|spacing|offset|size.*/i,
ios: checkCommonValue(ValueType.number),
Expand Down
40 changes: 30 additions & 10 deletions packages/webpack-plugin/lib/runtime/components/react/mpx-text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,60 @@
* ✘ decode
*/
import { Text, TextStyle, TextProps, StyleSheet } from 'react-native'
import React, { useRef, useEffect, forwardRef } from 'react';
import React, { useRef, useEffect, forwardRef, ReactNode, ForwardedRef } from 'react';
import useInnerProps from './getInnerListeners';
import useNodesRef from '../../useNodesRef' // 引入辅助函数
import { PERCENT_REGX } from './utils'

interface _TextProps extends TextProps {
style?: TextStyle
children?: React.ReactNode
type ExtendedTextStyle = Omit<TextStyle, 'lineHeight'> & {
lineHeight?: string | number
};
interface _TextProps extends Omit<TextProps, 'style'> {
style?: ExtendedTextStyle
children?: ReactNode
selectable?: boolean
'enable-offset'?: boolean
'user-select'?: boolean
userSelect?: boolean
useInherit?: boolean
['disable-default-style']?: boolean
}

const DEFAULT_STYLE = {
fontSize: 16
}

const _Text: React.FC<_TextProps & React.RefAttributes<any>> = forwardRef((props: _TextProps, ref: React.ForwardedRef<any>):React.JSX.Element => {

const transformStyle = (styleObj: ExtendedTextStyle) => {
let { lineHeight } = styleObj
if (typeof lineHeight === 'string' && PERCENT_REGX.test(lineHeight)) {
lineHeight = (parseFloat(lineHeight)/100) * (styleObj.fontSize || DEFAULT_STYLE.fontSize)
styleObj.lineHeight = lineHeight
}
}

const _Text = forwardRef((props: _TextProps, ref: ForwardedRef<any>): React.JSX.Element => {
const {
style = [],
children,
selectable,
'enable-offset': enableOffset,
'user-select': userSelect,
useInherit = false,
'disable-default-style': disableDefaultStyle = false,
} = props

const layoutRef = useRef({})

const styleObj = StyleSheet.flatten(style)
const styleObj = StyleSheet.flatten<ExtendedTextStyle>(style)

let defaultStyle = {}

if (!disableDefaultStyle) {
defaultStyle = DEFAULT_STYLE
transformStyle(styleObj)
}

const { nodeRef } = useNodesRef(props, ref, {
defaultStyle: DEFAULT_STYLE
defaultStyle
})

const innerProps = useInnerProps(props, {
Expand Down Expand Up @@ -74,7 +94,7 @@ const _Text: React.FC<_TextProps & React.RefAttributes<any>> = forwardRef((props

return (
<Text
style={{...useInherit && DEFAULT_STYLE, ...styleObj}}
style={{...defaultStyle, ...styleObj}}
selectable={!!selectable || !!userSelect}
{...innerProps}
>
Expand Down
Loading
Loading