Skip to content

🎥 Make videos programmatically with React

Support unitless zero translate values

Jonny Burger7471037c2c8a
diff --git a/packages/studio/src/components/Timeline/timeline-translate-utils.ts b/packages/studio/src/components/Timeline/timeline-translate-utils.ts
index 0d19d06feb..17d02ac292 100644
--- a/packages/studio/src/components/Timeline/timeline-translate-utils.ts
+++ b/packages/studio/src/components/Timeline/timeline-translate-utils.ts
@@ -4,7 +4,7 @@ import {
} from './timeline-field-utils';
const TRANSLATE_PATTERN =
- /^(-?\d+(?:\.\d+)?)(px|%)(?:\s+(-?\d+(?:\.\d+)?)(px|%))?(?:\s+(-?\d+(?:\.\d+)?)(px))?$/i;
+ /^(-?\d+(?:\.\d+)?)(px|%)?(?:\s+(-?\d+(?:\.\d+)?)(px|%)?)?(?:\s+(-?\d+(?:\.\d+)?)(px)?)?$/i;
const translateDecimalPlaces = 1;
export type ParsedTranslate = readonly [number, number, number | null];
@@ -25,23 +25,39 @@ export const parseTranslateWithUnits = (
return null;
}
+ const x = normalizeTimelineNumber(Number(match[1]));
+ const y =
+ match[3] === undefined ? 0 : normalizeTimelineNumber(Number(match[3]));
+ const z =
+ match[5] === undefined ? null : normalizeTimelineNumber(Number(match[5]));
+
+ if (
+ (match[2] === undefined && x !== 0) ||
+ (match[3] !== undefined && match[4] === undefined && y !== 0) ||
+ (match[5] !== undefined && match[6] === undefined && z !== 0)
+ ) {
+ return null;
+ }
+
return [
{
- value: normalizeTimelineNumber(Number(match[1])),
- unit: match[2].toLowerCase() as TranslateUnit,
+ value: x,
+ unit:
+ match[2] === undefined
+ ? 'px'
+ : (match[2].toLowerCase() as TranslateUnit),
},
{
- value:
- match[3] === undefined ? 0 : normalizeTimelineNumber(Number(match[3])),
+ value: y,
unit:
match[4] === undefined
? 'px'
: (match[4].toLowerCase() as TranslateUnit),
},
- match[5] === undefined
- ? null
+ z === null
+ ? z
: {
- value: normalizeTimelineNumber(Number(match[5])),
+ value: z,
unit: 'px',
},
];
diff --git a/packages/studio/src/test/timeline-field-utils.test.ts b/packages/studio/src/test/timeline-field-utils.test.ts
index d7a65a47a1..a8f301f1ac 100644
--- a/packages/studio/src/test/timeline-field-utils.test.ts
+++ b/packages/studio/src/test/timeline-field-utils.test.ts
@@ -153,6 +153,19 @@ test('formatTimelineFieldValueForDisplay preserves translate percentages', () =>
).toBe('71.4% 2.2%');
});
+test('formatTimelineFieldValueForDisplay normalizes unitless translate zero', () => {
+ expect(
+ formatTimelineFieldValueForDisplay({
+ fieldSchema: {
+ type: 'translate',
+ default: '0px 0px',
+ step: 1,
+ },
+ value: '0 0',
+ }),
+ ).toBe('0px 0px');
+});
+
test('formatTimelineFieldValueForDisplay preserves transform-origin units', () => {
expect(
formatTimelineFieldValueForDisplay({
diff --git a/packages/studio/src/test/timeline-translate-utils.test.ts b/packages/studio/src/test/timeline-translate-utils.test.ts
index e48c7c4a63..2f64324d34 100644
--- a/packages/studio/src/test/timeline-translate-utils.test.ts
+++ b/packages/studio/src/test/timeline-translate-utils.test.ts
@@ -48,6 +48,30 @@ test('parseTranslateWithUnits preserves percentage units', () => {
]);
});
+test('parseTranslateWithUnits accepts unitless zero', () => {
+ expect(parseTranslateWithUnits('0')).toEqual([
+ {value: 0, unit: 'px'},
+ {value: 0, unit: 'px'},
+ null,
+ ]);
+ expect(parseTranslateWithUnits('0% 0')).toEqual([
+ {value: 0, unit: '%'},
+ {value: 0, unit: 'px'},
+ null,
+ ]);
+ expect(parseTranslateWithUnits('0 0 0')).toEqual([
+ {value: 0, unit: 'px'},
+ {value: 0, unit: 'px'},
+ {value: 0, unit: 'px'},
+ ]);
+});
+
+test('parseTranslateWithUnits rejects non-zero unitless coordinates', () => {
+ expect(parseTranslateWithUnits('20 0')).toBeNull();
+ expect(parseTranslateWithUnits('0 20')).toBeNull();
+ expect(parseTranslateWithUnits('0 0 20')).toBeNull();
+});
+
test('serializeTranslateWithUnits preserves percentage units', () => {
expect(
serializeTranslateWithUnits([