iter(g181): 控件高度统一三档
This commit is contained in:
@@ -41,6 +41,11 @@ export const KIND_META = [
|
||||
label: '内距字面值',
|
||||
hint: 'padding/gap/margin 写死 px,应改用 --ua-space-*',
|
||||
},
|
||||
{
|
||||
key: 'control-h-literal',
|
||||
label: '控件高度字面值',
|
||||
hint: 'height 写死控件档位值(28/34/40px),应改用 --ua-control-h-*',
|
||||
},
|
||||
];
|
||||
|
||||
/*** 作用:需要扫描的 CSS 属性白名单(其余属性本轮不动;用白名单顺带避开伪类选择器误判) ***/
|
||||
@@ -66,6 +71,9 @@ const SCAN_PROPS = new Set([
|
||||
'gap',
|
||||
'row-gap',
|
||||
'column-gap',
|
||||
'min-height',
|
||||
'max-height',
|
||||
'height',
|
||||
]);
|
||||
|
||||
/*** 作用:内距类属性集合(迁移与统计共用同一判定) ***/
|
||||
@@ -136,6 +144,13 @@ const DURATION_STEPS = [
|
||||
{ max: 0.2501, token: 'var(--ua-duration-slow)' },
|
||||
];
|
||||
|
||||
/*** 作用:控件高度三档的字面值 → 令牌(28/34/40px 三个值保留给控件高度档位,其余尺寸不入此口径) ***/
|
||||
const CONTROL_H_TOKENS = new Map([
|
||||
['28px', 'var(--ua-control-h-sm)'],
|
||||
['34px', 'var(--ua-control-h-md)'],
|
||||
['40px', 'var(--ua-control-h-lg)'],
|
||||
]);
|
||||
|
||||
/*** 作用:压缩空白,便于用稳定字符串比对(不改语义) ***/
|
||||
export function squash(value) {
|
||||
return String(value).replace(/\s+/g, ' ').trim();
|
||||
@@ -161,7 +176,7 @@ export function blankComments(src) {
|
||||
|
||||
/*** 作用:逐条产出「属性 + 值」及其在原文中的偏移;只认白名单属性且必须以分号收尾(避开伪类选择器) ***/
|
||||
export function eachDeclaration(code) {
|
||||
const re = /(padding-top|padding-right|padding-bottom|padding-left|padding|margin-top|margin-right|margin-bottom|margin-left|margin|row-gap|column-gap|gap|transition-duration|animation-duration|transition|animation|font-size|box-shadow|border-radius|outline)\s*:\s*([^;{}]*);/gi;
|
||||
const re = /(padding-top|padding-right|padding-bottom|padding-left|padding|margin-top|margin-right|margin-bottom|margin-left|margin|row-gap|column-gap|gap|min-height|max-height|height|transition-duration|animation-duration|transition|animation|font-size|box-shadow|border-radius|outline)\s*:\s*([^;{}]*);/gi;
|
||||
const list = [];
|
||||
for (const m of code.matchAll(re)) {
|
||||
const prop = m[1].toLowerCase();
|
||||
@@ -266,6 +281,13 @@ function migrateSpace(core) {
|
||||
return { kind: 'space-literal', value: next };
|
||||
}
|
||||
|
||||
/*** 作用:迁移单条控件高度:整值命中三档即换令牌(其余 height 值不动),返回 null 表示无需改动 ***/
|
||||
function migrateControlH(core) {
|
||||
const token = CONTROL_H_TOKENS.get(squash(core));
|
||||
if (!token) return null;
|
||||
return { kind: 'control-h-literal', value: token };
|
||||
}
|
||||
|
||||
/*** 作用:判定单条声明是否需要迁移,返回 { kind, value } 或 null(幂等:已是令牌就返回 null) ***/
|
||||
export function migrateDeclaration(prop, rawValue) {
|
||||
const { core, tail } = splitImportant(rawValue);
|
||||
@@ -282,6 +304,8 @@ export function migrateDeclaration(prop, rawValue) {
|
||||
result = migrateShadow(core);
|
||||
} else if (SPACE_PROPS.has(prop)) {
|
||||
result = migrateSpace(core);
|
||||
} else if (prop === 'height') {
|
||||
result = migrateControlH(core);
|
||||
}
|
||||
if (!result) return null;
|
||||
return { kind: result.kind, value: result.value + tail };
|
||||
@@ -328,6 +352,10 @@ function detect(prop, rawValue, key) {
|
||||
const tokens = String(value).match(/(^|[\s(,])(\d+(?:\.\d+)?)px\b/gi) ?? [];
|
||||
return tokens.filter((t) => !/(^|[\s(,])1px$/i.test(t)).length;
|
||||
}
|
||||
if (key === 'control-h-literal') {
|
||||
if (prop !== 'height') return 0; // min/max-height 是约束不是控件档位,不入此口径
|
||||
return CONTROL_H_TOKENS.has(squash(core)) ? 1 : 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user