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(vue): fix attribute inheritance style not empty #3868

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 9 additions & 9 deletions driver/js/packages/hippy-vue/src/native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
deepCopy,
isTraceEnabled,
getBeforeRenderToNative,
isStyleNotEmpty,
} from '../util';
import {
EventHandlerType,
Expand All @@ -52,7 +53,6 @@ import {
} from '../util/i18n';
import {
setCacheNodeStyle,
deleteCacheNodeStyle,
getCacheNodeStyle,
} from '../util/node-style';
import { isStyleMatched, preCacheNode } from '../util/node';
Expand Down Expand Up @@ -443,24 +443,24 @@ function renderToNative(rootViewId, targetNode, refInfo = {}, notUpdateStyle = f

let style;
if (notUpdateStyle) {
// 不用更新 CSS,直接使用缓存
// No need to update CSS, use cache directly
style = getCacheNodeStyle(targetNode.nodeId);
} else {
// 重新计算 CSS 样式
// Recalculate CSS styles style
style = getElemCss(targetNode);
// 样式合并
// Merge styles style
style = { ...style, ...targetNode.style };
// CSS 预处理,该函数目前没被使用
// CSS preprocessing
getBeforeRenderToNative()();

if (targetNode.parentNode) {
// 属性继承逻辑实现
// 只继承 color 和 font属性
// Implement attribute inheritance logic
// Only inherit color and font properties
const parentNodeStyle = getCacheNodeStyle(targetNode.parentNode.nodeId);
const styleAttributes = ['color', 'fontSize', 'fontWeight', 'fontFamily', 'fontStyle', 'textAlign', 'lineHeight'];

styleAttributes.forEach((attribute) => {
if (!style[attribute] && parentNodeStyle[attribute]) {
if (!isStyleNotEmpty(style[attribute]) && isStyleNotEmpty(parentNodeStyle[attribute])) {
style[attribute] = parentNodeStyle[attribute];
}
});
Expand All @@ -471,7 +471,7 @@ function renderToNative(rootViewId, targetNode, refInfo = {}, notUpdateStyle = f
style = { ...targetNode.meta.component.defaultNativeStyle, ...style };
}

// 缓存 CSS 结果
// Cache css result
setCacheNodeStyle(targetNode.nodeId, style);
}
// Translate to native node
Expand Down
8 changes: 8 additions & 0 deletions driver/js/packages/hippy-vue/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ function isNullOrUndefined(value: NeedToTyped) {
return typeof value === 'undefined' || value === null;
}

function isStyleNotEmpty(style: string | number | null | undefined) {
if (typeof style === 'string') {
return style.trim() !== '';
}
return style !== null && style !== undefined;
}

function whitespaceFilter(str: string) {
if (typeof str !== 'string') return str;
// Adjusts template whitespace handling behavior.
Expand Down Expand Up @@ -274,4 +281,5 @@ export {
convertImageLocalPath,
deepCopy,
whitespaceFilter,
isStyleNotEmpty,
};
Loading