Skip to content

Commit 0439fc9

Browse files
committed
feat: dom-2中transition-delay的支持
1 parent 4034d79 commit 0439fc9

File tree

6 files changed

+145
-3
lines changed

6 files changed

+145
-3
lines changed

packages/uni-nvue-styler/__tests__/dom2/static/__snapshots__/transition.spec.ts.snap

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ exports[`transition-delay: 0.5s: app-harmony(nv-c) 1`] = `
1212
{
1313
"code": undefined,
1414
"messages": [],
15-
"obj": {},
15+
"obj": {
16+
"transition-delay": {
17+
"setterCode": "transitionDelay(500.0)",
18+
"valueCode": "500.0",
19+
},
20+
},
1621
}
1722
`;
1823

@@ -28,6 +33,57 @@ exports[`transition-delay: 0s: app-harmony(nv-c) 1`] = `
2833
{
2934
"code": undefined,
3035
"messages": [],
36+
"obj": {
37+
"transition-delay": {
38+
"setterCode": "transitionDelay(0.0)",
39+
"valueCode": "0.0",
40+
},
41+
},
42+
}
43+
`;
44+
45+
exports[`transition-delay: 2s, 4ms: app-harmony(dom-c) 1`] = `
46+
{
47+
"code": undefined,
48+
"messages": [
49+
Warning {
50+
"node": CustomDeclaration {
51+
"__originalProp": "transition-delay",
52+
"important": false,
53+
"prop": "transitionDelay",
54+
"remove": [Function],
55+
"replaceWith": [Function],
56+
"value": "2s, 4ms",
57+
Symbol(expanded): true,
58+
Symbol(normalized): true,
59+
},
60+
"text": "ERROR: property value \`2s, 4ms\` is not supported for \`transition-delay\` (supported values are: \`number of seconds\`|\`milliseconds\`)",
61+
"type": "warning",
62+
},
63+
],
64+
"obj": {},
65+
}
66+
`;
67+
68+
exports[`transition-delay: 2s, 4ms: app-harmony(nv-c) 1`] = `
69+
{
70+
"code": undefined,
71+
"messages": [
72+
Warning {
73+
"node": CustomDeclaration {
74+
"__originalProp": "transition-delay",
75+
"important": false,
76+
"prop": "transitionDelay",
77+
"remove": [Function],
78+
"replaceWith": [Function],
79+
"value": "2s, 4ms",
80+
Symbol(expanded): true,
81+
Symbol(normalized): true,
82+
},
83+
"text": "ERROR: property value \`2s, 4ms\` is not supported for \`transition-delay\` (supported values are: \`number of seconds\`|\`milliseconds\`)",
84+
"type": "warning",
85+
},
86+
],
3187
"obj": {},
3288
}
3389
`;
@@ -44,7 +100,12 @@ exports[`transition-delay: 200ms: app-harmony(nv-c) 1`] = `
44100
{
45101
"code": undefined,
46102
"messages": [],
47-
"obj": {},
103+
"obj": {
104+
"transition-delay": {
105+
"setterCode": "transitionDelay(200.0)",
106+
"valueCode": "200.0",
107+
},
108+
},
48109
}
49110
`;
50111

packages/uni-nvue-styler/__tests__/dom2/static/transition.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { parseDom2StaticStyle } from '../../../src/dom2'
22
import { TEST_OPTIONS_LIST } from '../utils'
33

44
describe('transition-delay:', () => {
5-
;['0s', '0.5s', '200ms'].forEach((value) => {
5+
;['0s', '0.5s', '200ms', '2s, 4ms'].forEach((value) => {
66
test(value, () => {
77
const input = `transition-delay: ${value}`
88
TEST_OPTIONS_LIST.forEach((options) => {

packages/uni-nvue-styler/lib/dom2/app-css.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,7 @@
849849
}
850850
},
851851
"transition-delay": {
852+
"type": ["UniNativeTransitionDelay", "UniNativeTransitionDelays"],
852853
"uniPlatform": {
853854
"app": {
854855
"nv-c": {

packages/uni-nvue-styler/src/dom2/processors/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ import {
4848
createSetStyleBackgroundImageValueProcessor,
4949
isBackgroundImageType,
5050
} from './backgroundImage'
51+
import {
52+
createSetStyleTransitionValueProcessor,
53+
isTransitionType,
54+
} from './transition'
5155

5256
export type { PropertyProcessor } from './utils'
5357
export { createSetStyleNativeColorValueProcessor } from './color'
@@ -172,6 +176,12 @@ export function createDom2PropertyProcessors(
172176
return createSetStyleTransformOriginValueProcessor(setter, language)
173177
} else if (isBackgroundImageType(propertyType)) {
174178
return createSetStyleBackgroundImageValueProcessor(setter, language)
179+
} else if (isTransitionType(propertyType)) {
180+
return createSetStyleTransitionValueProcessor(
181+
setter,
182+
language,
183+
propertyType
184+
)
175185
} else if (propertyType) {
176186
return createSetStyleEnumValueProcessor(
177187
setter,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { DOM2_APP_LANGUAGE } from '../types'
2+
import {
3+
type PropertyProcessor,
4+
PropertyProcessorType,
5+
createPropertyProcessor,
6+
createValueProcessorError,
7+
createValueProcessorResult,
8+
normalizeDurationToMilliseconds,
9+
} from './utils'
10+
11+
const UNINATIVETRANSITIONDELAY_TYPES = [
12+
'UniNativeTransitionDelay',
13+
'UniNativeTransitionDelays',
14+
]
15+
16+
export function isTransitionType(propertyType?: string) {
17+
return propertyType && UNINATIVETRANSITIONDELAY_TYPES.includes(propertyType)
18+
}
19+
20+
export function createSetStyleTransitionValueProcessor(
21+
setter: string,
22+
language: DOM2_APP_LANGUAGE,
23+
propertyType: string
24+
): PropertyProcessor {
25+
return createPropertyProcessor((value: string | number, propertyName) => {
26+
let code = ''
27+
if (UNINATIVETRANSITIONDELAY_TYPES.includes(propertyType)) {
28+
code = parseTransitionDurationValue(String(value), language)
29+
}
30+
if (!code) {
31+
return createValueProcessorError(value, propertyName)
32+
}
33+
return createValueProcessorResult(code, `${setter}(${code})`)
34+
}, PropertyProcessorType.Struct)
35+
}
36+
37+
function parseTransitionDurationValue(
38+
str: string,
39+
language: DOM2_APP_LANGUAGE
40+
): string {
41+
const normalized = normalizeDurationToMilliseconds(str)
42+
if (normalized === null) {
43+
return '0.0'
44+
}
45+
return normalized
46+
}

packages/uni-nvue-styler/src/dom2/processors/utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,27 @@ export function getTargetConfig(
117117

118118
return platformConfig[target] || null
119119
}
120+
121+
export function normalizeDurationToMilliseconds(
122+
enumValue: string
123+
): string | null {
124+
const match = /^(-?\d*\.?\d+)(ms|s)$/i.exec(enumValue.trim())
125+
if (!match) {
126+
return null
127+
}
128+
const numericPart = parseFloat(match[1])
129+
if (Number.isNaN(numericPart)) {
130+
return null
131+
}
132+
const unit = match[2].toLowerCase()
133+
const milliseconds = unit === 's' ? numericPart * 1000 : numericPart
134+
if (!Number.isFinite(milliseconds)) {
135+
return null
136+
}
137+
return formatFloatLiteral(milliseconds)
138+
}
139+
140+
function formatFloatLiteral(value: number): string {
141+
const str = value.toString()
142+
return str.includes('.') ? str : `${str}.0`
143+
}

0 commit comments

Comments
 (0)