-
Notifications
You must be signed in to change notification settings - Fork 393
样式相关优化 #2375
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
样式相关优化 #2375
Changes from 16 commits
88fe2dd
9f754c5
c06680f
224617c
6177871
6d4cfdc
01dce81
8c5413b
661131f
a061fa2
b71205d
0597ac7
2b8b302
ef01c4d
9e49ee2
dabdba6
cc1e3c2
67330d9
2fbba82
d1d9f77
5de271e
2e21b17
849c678
6a7d250
d1b48da
d73f18d
8c31a78
6d50090
a27ee7c
cfcdc94
fd8b2b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import { useEffect, useCallback, useMemo, useRef, ReactNode, ReactElement, isValidElement, useContext, useState, Dispatch, SetStateAction, Children, cloneElement, createElement, MutableRefObject } from 'react' | ||
| import { LayoutChangeEvent, TextStyle, ImageProps, Image } from 'react-native' | ||
| import { isObject, isFunction, isNumber, hasOwn, diffAndCloneA, error, warn } from '@mpxjs/utils' | ||
| import { isObject, isFunction, isNumber, hasOwn, diffAndCloneA, error, warn, isEmptyObject } from '@mpxjs/utils' | ||
| import { VarContext, ScrollViewContext, RouteContext } from './context' | ||
| import { ExpressionParser, parseFunc, ReplaceSource } from './parser' | ||
| import { initialWindowMetrics } from 'react-native-safe-area-context' | ||
|
|
@@ -238,7 +238,7 @@ function transformVar (styleObj: Record<string, any>, varKeyPaths: Array<Array<s | |
| const resolved = resolveVar(value, varContext) | ||
| if (resolved === undefined) { | ||
| delete target[key] | ||
| error(`Can not resolve css var at ${varKeyPath.join('.')}:${value}.`) | ||
| // error(`Can not resolve css var at ${varKeyPath.join('.')}:${value}.`) | ||
| return | ||
| } | ||
| target[key] = resolved | ||
|
|
@@ -388,6 +388,14 @@ function transformBoxShadow (styleObj: Record<string, any>) { | |
| }, '') | ||
| } | ||
|
|
||
| function transformZIndex (styleObj: Record<string, any>) { | ||
|
||
| if (!styleObj.zIndex || typeof styleObj.zIndex === 'number') return | ||
| if (styleObj.zIndex === 'auto') { | ||
| error('Property [z-index] does not supported [auto], please check again!') | ||
| styleObj.zIndex = 0 | ||
| } | ||
| } | ||
|
|
||
| interface TransformStyleConfig { | ||
| enableVar?: boolean | ||
| externalVarContext?: Record<string, any> | ||
|
|
@@ -444,17 +452,20 @@ export function useTransformStyle (styleObj: Record<string, any> = {}, { enableV | |
| } | ||
| } | ||
|
|
||
| function calcVisitor ({ value, keyPath }: VisitorArg) { | ||
| function calcVisitor ({ key, value, keyPath }: VisitorArg) { | ||
| if (calcUseRegExp.test(value)) { | ||
| // calc translate & border-radius 的百分比计算 | ||
| if (hasOwn(selfPercentRule, key) && /%/.test(value)) { | ||
|
||
| hasSelfPercent = true | ||
| percentKeyPaths.push(keyPath.slice()) | ||
| } | ||
| calcKeyPaths.push(keyPath.slice()) | ||
| } | ||
| } | ||
|
|
||
| function percentVisitor ({ key, value, keyPath }: VisitorArg) { | ||
| if (hasOwn(selfPercentRule, key) && PERCENT_REGEX.test(value)) { | ||
| hasSelfPercent = true | ||
| percentKeyPaths.push(keyPath.slice()) | ||
| } else if ((key === 'fontSize' || key === 'lineHeight') && PERCENT_REGEX.test(value)) { | ||
| // fixme 去掉 translate & border-radius 的百分比计算 | ||
| if ((key === 'fontSize' || key === 'lineHeight') && PERCENT_REGEX.test(value)) { | ||
| percentKeyPaths.push(keyPath.slice()) | ||
| } | ||
| } | ||
|
|
@@ -464,10 +475,10 @@ export function useTransformStyle (styleObj: Record<string, any> = {}, { enableV | |
| [envVisitor, percentVisitor, calcVisitor].forEach(visitor => visitor({ target, key, value, keyPath })) | ||
| } | ||
| } | ||
|
|
||
| // transform 字符串格式转化数组格式(先转数组再处理css var) | ||
| transformTransform(styleObj) | ||
| // traverse var & generate normalStyle | ||
| traverseStyle(styleObj, [varVisitor]) | ||
|
|
||
| hasVarDec = hasVarDec || !!externalVarContext | ||
| enableVar = enableVar || hasVarDec || hasVarUse | ||
| const enableVarRef = useRef(enableVar) | ||
|
|
@@ -531,10 +542,11 @@ export function useTransformStyle (styleObj: Record<string, any> = {}, { enableV | |
| transformStringify(normalStyle) | ||
| // transform rpx to px | ||
| transformBoxShadow(normalStyle) | ||
|
|
||
| // transform 字符串格式转化数组格式 | ||
| transformTransform(normalStyle) | ||
|
|
||
| // transform z-index auto to 0 | ||
| transformZIndex(normalStyle) | ||
| if (Array.isArray(normalStyle.transform)) { | ||
| normalStyle.transform = normalStyle.transform.filter(item => !isEmptyObject(item)) | ||
|
||
| } | ||
| return { | ||
| hasVarDec, | ||
| varContextRef, | ||
|
|
||
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.
为啥不走verifyValues来校验实现?