Skip to content

Commit

Permalink
feat(vue): fix attribute inheritance style not empty (#3868)
Browse files Browse the repository at this point in the history
  • Loading branch information
zealotchen0 authored May 21, 2024
1 parent 1966a3e commit 413e401
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
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,
};

0 comments on commit 413e401

Please sign in to comment.