Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Centralize timeline field display formatting (#8528)

Jonny Burger302a48cda786
diff --git a/packages/studio/src/components/Timeline/EasingEditorModal.tsx b/packages/studio/src/components/Timeline/EasingEditorModal.tsx
index 44f82329c3..022f17c97e 100644
--- a/packages/studio/src/components/Timeline/EasingEditorModal.tsx
+++ b/packages/studio/src/components/Timeline/EasingEditorModal.tsx
@@ -12,7 +12,6 @@ import React, {
} from 'react';
import type {InteractivitySchemaField} from 'remotion';
import {Easing, Internals} from 'remotion';
-import {NoReactInternals} from 'remotion/no-react';
import {StudioServerConnectionCtx} from '../../helpers/client-id';
import {
BACKGROUND,
@@ -26,16 +25,8 @@ import {INSPECTOR_PANEL_HORIZONTAL_PADDING} from '../InspectorPanelLayout';
import {InputDragger} from '../NewComposition/InputDragger';
import type {SegmentedControlItem} from '../SegmentedControl';
import {SegmentedControl} from '../SegmentedControl';
-import {
- formatTimelineNumber,
- getDecimalPlaces,
- getTimelineDisplayDecimalPlaces,
- normalizeTimelineNumber,
-} from './timeline-field-utils';
-import {parseCssRotationToDegrees} from './timeline-rotation-utils';
-import {parseTranslate, serializeTranslate} from './timeline-translate-utils';
+import {formatTimelineFieldValueForDisplay} from './timeline-field-display-utils';
import type {TimelineSelection} from './TimelineSelection';
-import {parseTransformOrigin} from './transform-origin-utils';
import type {
SelectedEasingUpdate,
TimelineEasingValue,
@@ -379,311 +370,6 @@ const getEasingUpdateTargetKey = (update: SelectedEasingUpdate) => {
return `effect:${nodePathKey}:${update.effectIndex}:${update.fieldKey}:${update.segmentIndex}`;
};
-const EASING_GRAPH_FALLBACK_DECIMAL_PLACES = 3;
-
-const formatGenericNumberEasingGraphLabel = (value: number) => {
- if (!Number.isFinite(value)) {
- return String(value);
- }
-
- return formatTimelineNumber({
- decimalPlaces: EASING_GRAPH_FALLBACK_DECIMAL_PLACES,
- fixed: false,
- value: normalizeTimelineNumber(value),
- });
-};
-
-const normalizeUnknownForEasingGraphLabel = (value: unknown): unknown => {
- if (typeof value === 'number' && Number.isFinite(value)) {
- return roundToDecimalPlaces(
- normalizeTimelineNumber(value),
- EASING_GRAPH_FALLBACK_DECIMAL_PLACES,
- );
- }
-
- if (Array.isArray(value)) {
- return value.map(normalizeUnknownForEasingGraphLabel);
- }
-
- if (value && typeof value === 'object') {
- return Object.fromEntries(
- Object.entries(value).map(([key, item]) => [
- key,
- normalizeUnknownForEasingGraphLabel(item),
- ]),
- );
- }
-
- return value;
-};
-
-const formatUnknownEasingGraphLabel = (value: unknown): string => {
- if (typeof value === 'number') {
- return formatGenericNumberEasingGraphLabel(value);
- }
-
- if (
- value === null ||
- typeof value === 'string' ||
- typeof value === 'boolean' ||
- typeof value === 'bigint'
- ) {
- return String(value);
- }
-
- if (value === undefined) {
- return 'undefined';
- }
-
- try {
- return (
- JSON.stringify(normalizeUnknownForEasingGraphLabel(value)) ??
- String(value)
- );
- } catch {
- return String(value);
- }
-};
-
-const formatRotationEasingGraphLabel = ({
- fieldSchema,
- value,
-}: {
- readonly fieldSchema: Extract<
- InteractivitySchemaField,
- {type: 'rotation-css'} | {type: 'rotation-degrees'}
- >;
- readonly value: unknown;
-}) => {
- const configuredStep =
- fieldSchema.type === 'rotation-css' ||
- fieldSchema.type === 'rotation-degrees'
- ? fieldSchema.step
- : undefined;
- const decimalPlaces = getTimelineDisplayDecimalPlaces({
- defaultDecimalPlaces: 1,
- step: configuredStep,
- });
- const degrees =
- fieldSchema.type === 'rotation-css'
- ? parseCssRotationToDegrees(String(value ?? '0deg'))
- : typeof value === 'number'
- ? value
- : Number(value);
-
- if (!Number.isFinite(degrees)) {
- return null;
- }
-
- return `${formatTimelineNumber({
- decimalPlaces,
- fixed: false,
- value: normalizeTimelineNumber(degrees),
- })}\u00B0`;
-};
-
-const formatNumberEasingGraphLabel = ({
- fieldSchema,
- value,
-}: {
- readonly fieldSchema: Extract<InteractivitySchemaField, {type: 'number'}>;
- readonly value: unknown;
-}) => {
- if (typeof value !== 'number') {
- return null;
- }
-
- const stepDecimals =
- fieldSchema.step === undefined ? null : getDecimalPlaces(fieldSchema.step);
-
- if (stepDecimals === null) {
- const digits = getDecimalPlaces(value);
- return digits === 0 ? String(value) : value.toFixed(digits);
- }
-
- return formatTimelineNumber({
- decimalPlaces: stepDecimals,
- fixed: true,
- value,
- });
-};
-
-const formatScaleEasingGraphLabel = ({
- fieldSchema,
- value,
-}: {
- readonly fieldSchema: Extract<InteractivitySchemaField, {type: 'scale'}>;
- readonly value: unknown;
-}) => {
- const decimalPlaces = getTimelineDisplayDecimalPlaces({
- defaultDecimalPlaces: 3,
- step: fieldSchema.step,
- });
- const formatScalePart = (part: number) =>
- formatTimelineNumber({
- decimalPlaces,
- fixed: true,
- value: part,
- });
- const [x, y, z] = NoReactInternals.parseScaleValue(value);
- const parts = x === y && z === 1 ? [x] : z === 1 ? [x, y] : [x, y, z];
-
- return parts.map(formatScalePart).join(' ');
-};
-
-const formatTranslateEasingGraphLabel = ({
- fieldSchema,
- value,
-}: {
- readonly fieldSchema: Extract<InteractivitySchemaField, {type: 'translate'}>;
- readonly value: unknown;
-}) => {
- const decimalPlaces = getTimelineDisplayDecimalPlaces({
- defaultDecimalPlaces: 1,
- step: fieldSchema.step,
- });
- const [x, y] = parseTranslate(String(value ?? '0px 0px'));
-
- return serializeTranslate(x, y, decimalPlaces);
-};
-
-const formatTransformOriginAxisValue = ({
- decimalPlaces,
- unit,
- value,
-}: {
- readonly decimalPlaces: number;
- readonly unit: '%' | 'px';
- readonly value: number;
-}) => {
- return `${formatTimelineNumber({
- decimalPlaces,
- fixed: false,
- value,
- })}${unit}`;
-};
-
-const formatTransformOriginEasingGraphLabel = ({
- fieldSchema,
- value,
-}: {
- readonly fieldSchema: Extract<
- InteractivitySchemaField,
- {type: 'transform-origin'}
- >;
- readonly value: unknown;
-}) => {
- const parsed = parseTransformOrigin(value);
- if (parsed === null) {
- return null;
- }
-
- const decimalPlaces = getTimelineDisplayDecimalPlaces({
- defaultDecimalPlaces: 2,
- step: fieldSchema.step,
- });
- const xy = `${formatTransformOriginAxisValue({
- decimalPlaces,
- unit: parsed.x.unit,
- value: parsed.x.value,
- })} ${formatTransformOriginAxisValue({
- decimalPlaces,
- unit: parsed.y.unit,
- value: parsed.y.value,
- })}`;
-
- return parsed.z === null ? xy : `${xy} ${parsed.z}`;
-};
-
-const formatUvCoordinateEasingGraphLabel = ({
- fieldSchema,
- value,
-}: {
- readonly fieldSchema: Extract<
- InteractivitySchemaField,
- {type: 'uv-coordinate'}
- >;
- readonly value: unknown;
-}) => {
- if (
- !Array.isArray(value) ||
- value.length !== 2 ||
- !value.every((item) => typeof item === 'number' && Number.isFinite(item))
- ) {
- return null;
- }
-
- const decimalPlaces = getTimelineDisplayDecimalPlaces({
- defaultDecimalPlaces: 2,
- step: fieldSchema.step,
- });
- const formatPart = (part: number) =>
- formatTimelineNumber({
- decimalPlaces,
- fixed: true,
- value: part,
- });
-
- return `${formatPart(value[0])}, ${formatPart(value[1])}`;
-};
-
-const formatSchemaEasingGraphLabel = ({
- fieldSchema,
- value,
-}: {
- readonly fieldSchema: InteractivitySchemaField | undefined;
- readonly value: unknown;
-}) => {
- if (!fieldSchema) {
- return formatUnknownEasingGraphLabel(value);
- }
-
- switch (fieldSchema.type) {
- case 'number': {
- return (
- formatNumberEasingGraphLabel({fieldSchema, value}) ??
- formatUnknownEasingGraphLabel(value)
- );
- }
-
- case 'rotation-css':
- case 'rotation-degrees': {
- return (
- formatRotationEasingGraphLabel({fieldSchema, value}) ??
- formatUnknownEasingGraphLabel(value)
- );
- }
-
- case 'scale':
- return formatScaleEasingGraphLabel({fieldSchema, value});
-
- case 'translate':
- return formatTranslateEasingGraphLabel({fieldSchema, value});
-
- case 'transform-origin':
- return (
- formatTransformOriginEasingGraphLabel({fieldSchema, value}) ??
- formatUnknownEasingGraphLabel(value)
- );
-
- case 'uv-coordinate':
- return (
- formatUvCoordinateEasingGraphLabel({fieldSchema, value}) ??
- formatUnknownEasingGraphLabel(value)
- );
-
- case 'array':
- case 'boolean':
- case 'color':
- case 'enum':
- case 'hidden':
- return formatUnknownEasingGraphLabel(value);
-
- default:
- return formatUnknownEasingGraphLabel(value);
- }
-};
-
const getBuiltInVisualStyleFieldSchema = (
fieldKey: string,
): InteractivitySchemaField | undefined => {
@@ -714,7 +400,7 @@ const formatEasingGraphLabel = (
value: unknown,
update: SelectedEasingUpdate,
): string =>
- formatSchemaEasingGraphLabel({
+ formatTimelineFieldValueForDisplay({
fieldSchema: getEasingGraphFieldSchema(update),
value,
});
diff --git a/packages/studio/src/components/Timeline/TimelineNumberField.tsx b/packages/studio/src/components/Timeline/TimelineNumberField.tsx
index 33555eddf8..2055eb408d 100644
--- a/packages/studio/src/components/Timeline/TimelineNumberField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineNumberField.tsx
@@ -1,4 +1,4 @@
-import React, {useCallback, useMemo, useState} from 'react';
+import React, {useCallback, useState} from 'react';
import type {CanUpdateSequencePropStatusStatic} from 'remotion';
import type {
SchemaFieldInfo,
@@ -6,11 +6,8 @@ import type {
TimelineFieldOnSave,
} from '../../helpers/timeline-layout';
import {InputDragger} from '../NewComposition/InputDragger';
-import {
- draggerStyle,
- formatTimelineNumber,
- getDecimalPlaces,
-} from './timeline-field-utils';
+import {formatTimelineFieldValueForDisplay} from './timeline-field-display-utils';
+import {draggerStyle} from './timeline-field-utils';
export const TimelineNumberField: React.FC<{
readonly field: SchemaFieldInfo;
@@ -69,27 +66,14 @@ export const TimelineNumberField: React.FC<{
field.fieldSchema.type === 'number' ? field.fieldSchema.step : undefined;
const step = configuredStep ?? 1;
- const stepDecimals = useMemo(
- () =>
- configuredStep === undefined ? null : getDecimalPlaces(configuredStep),
- [configuredStep],
- );
-
const formatter = useCallback(
(v: number | string) => {
- const num = Number(v);
- if (stepDecimals === null) {
- const digits = getDecimalPlaces(num);
- return digits === 0 ? String(num) : num.toFixed(digits);
- }
-
- return formatTimelineNumber({
- decimalPlaces: stepDecimals,
- fixed: true,
- value: num,
+ return formatTimelineFieldValueForDisplay({
+ fieldSchema: field.fieldSchema,
+ value: v,
});
},
- [stepDecimals],
+ [field.fieldSchema],
);
return (
diff --git a/packages/studio/src/components/Timeline/TimelineRotationField.tsx b/packages/studio/src/components/Timeline/TimelineRotationField.tsx
index 25942c4d8a..0fd3b4c9b1 100644
--- a/packages/studio/src/components/Timeline/TimelineRotationField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineRotationField.tsx
@@ -6,9 +6,9 @@ import type {
TimelineFieldOnSave,
} from '../../helpers/timeline-layout';
import {InputDragger} from '../NewComposition/InputDragger';
+import {formatTimelineFieldValueForDisplay} from './timeline-field-display-utils';
import {
draggerStyle,
- formatTimelineNumber,
getTimelineDisplayDecimalPlaces,
normalizeTimelineNumber,
} from './timeline-field-utils';
@@ -118,14 +118,12 @@ export const TimelineRotationField: React.FC<{
const formatter = useCallback(
(v: number | string) => {
- const formatted = formatTimelineNumber({
- decimalPlaces,
- fixed: false,
- value: normalizeTimelineNumber(Number(v)),
+ return formatTimelineFieldValueForDisplay({
+ fieldSchema: field.fieldSchema,
+ value: v,
});
- return `${formatted}\u00B0`;
},
- [decimalPlaces],
+ [field.fieldSchema],
);
return (
diff --git a/packages/studio/src/components/Timeline/TimelineScaleField.tsx b/packages/studio/src/components/Timeline/TimelineScaleField.tsx
index b4b4ba0391..72268bc7e0 100644
--- a/packages/studio/src/components/Timeline/TimelineScaleField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineScaleField.tsx
@@ -12,11 +12,8 @@ import type {
} from '../../helpers/timeline-layout';
import {ScaleLockContext} from '../../state/scale-lock';
import {InputDragger} from '../NewComposition/InputDragger';
-import {
- formatTimelineNumber,
- getTimelineDisplayDecimalPlaces,
- normalizeTimelineNumber,
-} from './timeline-field-utils';
+import {formatTimelineFieldValueForDisplay} from './timeline-field-display-utils';
+import {normalizeTimelineNumber} from './timeline-field-utils';
import {timelineLayerIconContainer} from './TimelineLayerEye';
const leftDraggerStyle: React.CSSProperties = {
@@ -190,24 +187,14 @@ export const TimelineScaleField: React.FC<{
? (field.fieldSchema.max ?? Infinity)
: Infinity;
- const decimalPlaces = useMemo(
- () =>
- getTimelineDisplayDecimalPlaces({
- defaultDecimalPlaces: 3,
- step: configuredStep,
- }),
- [configuredStep],
- );
-
const formatter = useCallback(
(v: number | string) => {
- return formatTimelineNumber({
- decimalPlaces,
- fixed: true,
+ return formatTimelineFieldValueForDisplay({
+ fieldSchema: field.fieldSchema,
value: v,
});
},
- [decimalPlaces],
+ [field.fieldSchema],
);
const getDragStart = useCallback((): readonly [number, number] => {
diff --git a/packages/studio/src/components/Timeline/TimelineTransformOriginField.tsx b/packages/studio/src/components/Timeline/TimelineTransformOriginField.tsx
index 79bd9d642f..9117c5a4ad 100644
--- a/packages/studio/src/components/Timeline/TimelineTransformOriginField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineTransformOriginField.tsx
@@ -6,10 +6,8 @@ import type {
TimelineFieldOnSave,
} from '../../helpers/timeline-layout';
import {InputDragger} from '../NewComposition/InputDragger';
-import {
- formatTimelineNumber,
- getTimelineDisplayDecimalPlaces,
-} from './timeline-field-utils';
+import {formatTimelineFieldValueForDisplay} from './timeline-field-display-utils';
+import {getTimelineDisplayDecimalPlaces} from './timeline-field-utils';
import {UnsupportedStatus} from './TimelineSchemaField';
import {
parseTransformOrigin,
@@ -75,14 +73,12 @@ export const TimelineTransformOriginField: React.FC<{
const formatter = useCallback(
(v: number | string) => {
- const formatted = formatTimelineNumber({
- decimalPlaces,
- fixed: false,
+ return formatTimelineFieldValueForDisplay({
+ fieldSchema: field.fieldSchema,
value: v,
});
- return `${formatted}%`;
},
- [decimalPlaces],
+ [field.fieldSchema],
);
const serialize = useCallback(
diff --git a/packages/studio/src/components/Timeline/TimelineTranslateField.tsx b/packages/studio/src/components/Timeline/TimelineTranslateField.tsx
index f9280b8b01..f306702ac1 100644
--- a/packages/studio/src/components/Timeline/TimelineTranslateField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineTranslateField.tsx
@@ -6,10 +6,8 @@ import type {
TimelineFieldOnSave,
} from '../../helpers/timeline-layout';
import {InputDragger} from '../NewComposition/InputDragger';
-import {
- formatTimelineNumber,
- getTimelineDisplayDecimalPlaces,
-} from './timeline-field-utils';
+import {formatTimelineFieldValueForDisplay} from './timeline-field-display-utils';
+import {getTimelineDisplayDecimalPlaces} from './timeline-field-utils';
import {parseTranslate, serializeTranslate} from './timeline-translate-utils';
const leftDraggerStyle: React.CSSProperties = {
@@ -63,14 +61,12 @@ export const TimelineTranslateField: React.FC<{
const formatter = useCallback(
(v: number | string) => {
- const formatted = formatTimelineNumber({
- decimalPlaces,
- fixed: false,
+ return formatTimelineFieldValueForDisplay({
+ fieldSchema: field.fieldSchema,
value: v,
});
- return `${formatted}px`;
},
- [decimalPlaces],
+ [field.fieldSchema],
);
// --- X callbacks ---
diff --git a/packages/studio/src/components/Timeline/TimelineUvCoordinateField.tsx b/packages/studio/src/components/Timeline/TimelineUvCoordinateField.tsx
index d9e5fc3fae..ea70a544a4 100644
--- a/packages/studio/src/components/Timeline/TimelineUvCoordinateField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineUvCoordinateField.tsx
@@ -6,10 +6,8 @@ import type {
TimelineFieldOnSave,
} from '../../helpers/timeline-layout';
import {InputDragger} from '../NewComposition/InputDragger';
-import {
- formatTimelineNumber,
- getTimelineDisplayDecimalPlaces,
-} from './timeline-field-utils';
+import {formatTimelineFieldValueForDisplay} from './timeline-field-display-utils';
+import {getTimelineDisplayDecimalPlaces} from './timeline-field-utils';
const leftDraggerStyle: React.CSSProperties = {
paddingLeft: 0,
@@ -97,13 +95,12 @@ export const TimelineUvCoordinateField: React.FC<{
const formatter = useCallback(
(v: number | string) => {
- return formatTimelineNumber({
- decimalPlaces,
- fixed: true,
+ return formatTimelineFieldValueForDisplay({
+ fieldSchema: field.fieldSchema,
value: v,
});
},
- [decimalPlaces],
+ [field.fieldSchema],
);
const onXChange = useCallback(
diff --git a/packages/studio/src/components/Timeline/timeline-field-display-utils.ts b/packages/studio/src/components/Timeline/timeline-field-display-utils.ts
new file mode 100644
index 0000000000..458a69df9a
--- /dev/null
+++ b/packages/studio/src/components/Timeline/timeline-field-display-utils.ts
@@ -0,0 +1,396 @@
+import type {InteractivitySchemaField} from 'remotion';
+import {
+ formatTimelineNumber,
+ getDecimalPlaces,
+ getTimelineDisplayDecimalPlaces,
+ normalizeTimelineNumber,
+ roundToDecimalPlaces,
+} from './timeline-field-utils';
+import {parseCssRotationToDegrees} from './timeline-rotation-utils';
+import {parseTranslate, serializeTranslate} from './timeline-translate-utils';
+import {parseTransformOrigin} from './transform-origin-utils';
+
+const DISPLAY_FALLBACK_DECIMAL_PLACES = 3;
+const DEFAULT_SCALE_VALUE: readonly [number, number, number] = [1, 1, 1];
+
+const getFiniteNumericValue = (value: unknown): number | null => {
+ if (typeof value !== 'number' && typeof value !== 'string') {
+ return null;
+ }
+
+ const numericValue = Number(value);
+ return Number.isFinite(numericValue) ? numericValue : null;
+};
+
+const parseScaleStringForDisplay = (
+ value: string,
+): readonly [number, number, number] | null => {
+ const parts = value.trim().split(/\s+/);
+ if (parts.length < 1 || parts.length > 3 || parts[0] === '') {
+ return null;
+ }
+
+ const parsed = parts.map((part) => Number(part));
+ if (!parsed.every((part) => Number.isFinite(part))) {
+ return null;
+ }
+
+ const x = parsed[0];
+ const y = parsed[1] ?? x;
+ const z = parsed[2] ?? 1;
+
+ return [x, y, z];
+};
+
+const parseScaleValueForDisplay = (
+ value: unknown,
+): readonly [number, number, number] => {
+ if (typeof value === 'number') {
+ return Number.isFinite(value) ? [value, value, 1] : DEFAULT_SCALE_VALUE;
+ }
+
+ if (typeof value === 'string') {
+ return parseScaleStringForDisplay(value) ?? DEFAULT_SCALE_VALUE;
+ }
+
+ return DEFAULT_SCALE_VALUE;
+};
+
+const formatUnknownNumberForDisplay = (value: number): string => {
+ if (!Number.isFinite(value)) {
+ return String(value);
+ }
+
+ return formatTimelineNumber({
+ decimalPlaces: DISPLAY_FALLBACK_DECIMAL_PLACES,
+ fixed: false,
+ value: normalizeTimelineNumber(value),
+ });
+};
+
+const normalizeUnknownForDisplay = (value: unknown): unknown => {
+ if (typeof value === 'number' && Number.isFinite(value)) {
+ return roundToDecimalPlaces(
+ normalizeTimelineNumber(value),
+ DISPLAY_FALLBACK_DECIMAL_PLACES,
+ );
+ }
+
+ if (Array.isArray(value)) {
+ return value.map(normalizeUnknownForDisplay);
+ }
+
+ if (value && typeof value === 'object') {
+ return Object.fromEntries(
+ Object.entries(value).map(([key, item]) => [
+ key,
+ normalizeUnknownForDisplay(item),
+ ]),
+ );
+ }
+
+ return value;
+};
+
+const formatUnknownTimelineValueForDisplay = (value: unknown): string => {
+ if (typeof value === 'number') {
+ return formatUnknownNumberForDisplay(value);
+ }
+
+ if (
+ value === null ||
+ typeof value === 'string' ||
+ typeof value === 'boolean' ||
+ typeof value === 'bigint'
+ ) {
+ return String(value);
+ }
+
+ if (value === undefined) {
+ return 'undefined';
+ }
+
+ try {
+ return JSON.stringify(normalizeUnknownForDisplay(value)) ?? String(value);
+ } catch {
+ return String(value);
+ }
+};
+
+const formatNumberTimelineFieldValueForDisplay = ({
+ fieldSchema,
+ value,
+}: {
+ readonly fieldSchema: Extract<InteractivitySchemaField, {type: 'number'}>;
+ readonly value: unknown;
+}): string | null => {
+ const numericValue = getFiniteNumericValue(value);
+ if (numericValue === null) {
+ return null;
+ }
+
+ const stepDecimals =
+ fieldSchema.step === undefined ? null : getDecimalPlaces(fieldSchema.step);
+
+ if (stepDecimals === null) {
+ const digits = getDecimalPlaces(numericValue);
+ return digits === 0 ? String(numericValue) : numericValue.toFixed(digits);
+ }
+
+ return formatTimelineNumber({
+ decimalPlaces: stepDecimals,
+ fixed: true,
+ value: numericValue,
+ });
+};
+
+const formatRotationTimelineFieldValueForDisplay = ({
+ fieldSchema,
+ value,
+}: {
+ readonly fieldSchema: Extract<
+ InteractivitySchemaField,
+ {type: 'rotation-css'} | {type: 'rotation-degrees'}
+ >;
+ readonly value: unknown;
+}): string | null => {
+ const configuredStep =
+ fieldSchema.type === 'rotation-css' ||
+ fieldSchema.type === 'rotation-degrees'
+ ? fieldSchema.step
+ : undefined;
+ const decimalPlaces = getTimelineDisplayDecimalPlaces({
+ defaultDecimalPlaces: 1,
+ step: configuredStep,
+ });
+ const degrees =
+ fieldSchema.type === 'rotation-css'
+ ? parseCssRotationToDegrees(String(value ?? '0deg'))
+ : getFiniteNumericValue(value);
+
+ if (degrees === null || !Number.isFinite(degrees)) {
+ return null;
+ }
+
+ return `${formatTimelineNumber({
+ decimalPlaces,
+ fixed: false,
+ value: normalizeTimelineNumber(degrees),
+ })}\u00B0`;
+};
+
+const formatScaleTimelineFieldValueForDisplay = ({
+ fieldSchema,
+ value,
+}: {
+ readonly fieldSchema: Extract<InteractivitySchemaField, {type: 'scale'}>;
+ readonly value: unknown;
+}): string => {
+ const decimalPlaces = getTimelineDisplayDecimalPlaces({
+ defaultDecimalPlaces: 3,
+ step: fieldSchema.step,
+ });
+ const formatScalePart = (part: number) =>
+ formatTimelineNumber({
+ decimalPlaces,
+ fixed: true,
+ value: part,
+ });
+ const [x, y, z] = parseScaleValueForDisplay(value);
+ const parts = x === y && z === 1 ? [x] : z === 1 ? [x, y] : [x, y, z];
+
+ return parts.map(formatScalePart).join(' ');
+};
+
+const formatTranslateCoordinateForDisplay = (
+ value: number,
+ decimalPlaces: number,
+) =>
+ `${formatTimelineNumber({
+ decimalPlaces,
+ fixed: false,
+ value,
+ })}px`;
+
+const formatTranslateTimelineFieldValueForDisplay = ({
+ fieldSchema,
+ value,
+}: {
+ readonly fieldSchema: Extract<InteractivitySchemaField, {type: 'translate'}>;
+ readonly value: unknown;
+}): string | null => {
+ const decimalPlaces = getTimelineDisplayDecimalPlaces({
+ defaultDecimalPlaces: 1,
+ step: fieldSchema.step,
+ });
+ const numericValue = getFiniteNumericValue(value);
+
+ if (numericValue !== null) {
+ return formatTranslateCoordinateForDisplay(numericValue, decimalPlaces);
+ }
+
+ const [x, y] = parseTranslate(String(value ?? '0px 0px'));
+ return serializeTranslate(x, y, decimalPlaces);
+};
+
+const formatTransformOriginAxisValueForDisplay = ({
+ decimalPlaces,
+ unit,
+ value,
+}: {
+ readonly decimalPlaces: number;
+ readonly unit: '%' | 'px';
+ readonly value: number;
+}) => {
+ return `${formatTimelineNumber({
+ decimalPlaces,
+ fixed: false,
+ value,
+ })}${unit}`;
+};
+
+const formatTransformOriginTimelineFieldValueForDisplay = ({
+ fieldSchema,
+ value,
+}: {
+ readonly fieldSchema: Extract<
+ InteractivitySchemaField,
+ {type: 'transform-origin'}
+ >;
+ readonly value: unknown;
+}): string | null => {
+ const decimalPlaces = getTimelineDisplayDecimalPlaces({
+ defaultDecimalPlaces: 2,
+ step: fieldSchema.step,
+ });
+ const numericValue = getFiniteNumericValue(value);
+
+ if (numericValue !== null) {
+ return formatTransformOriginAxisValueForDisplay({
+ decimalPlaces,
+ unit: '%',
+ value: numericValue,
+ });
+ }
+
+ const parsed = parseTransformOrigin(value);
+ if (parsed === null) {
+ return null;
+ }
+
+ const xy = `${formatTransformOriginAxisValueForDisplay({
+ decimalPlaces,
+ unit: parsed.x.unit,
+ value: parsed.x.value,
+ })} ${formatTransformOriginAxisValueForDisplay({
+ decimalPlaces,
+ unit: parsed.y.unit,
+ value: parsed.y.value,
+ })}`;
+
+ return parsed.z === null ? xy : `${xy} ${parsed.z}`;
+};
+
+const formatUvCoordinatePartForDisplay = (
+ value: number,
+ decimalPlaces: number,
+) =>
+ formatTimelineNumber({
+ decimalPlaces,
+ fixed: true,
+ value,
+ });
+
+const formatUvCoordinateTimelineFieldValueForDisplay = ({
+ fieldSchema,
+ value,
+}: {
+ readonly fieldSchema: Extract<
+ InteractivitySchemaField,
+ {type: 'uv-coordinate'}
+ >;
+ readonly value: unknown;
+}): string | null => {
+ const decimalPlaces = getTimelineDisplayDecimalPlaces({
+ defaultDecimalPlaces: 2,
+ step: fieldSchema.step,
+ });
+ const numericValue = getFiniteNumericValue(value);
+
+ if (numericValue !== null) {
+ return formatUvCoordinatePartForDisplay(numericValue, decimalPlaces);
+ }
+
+ if (
+ !Array.isArray(value) ||
+ value.length !== 2 ||
+ !value.every((item) => typeof item === 'number' && Number.isFinite(item))
+ ) {
+ return null;
+ }
+
+ return `${formatUvCoordinatePartForDisplay(
+ value[0],
+ decimalPlaces,
+ )}, ${formatUvCoordinatePartForDisplay(value[1], decimalPlaces)}`;
+};
+
+export const formatTimelineFieldValueForDisplay = ({
+ fieldSchema,
+ value,
+}: {
+ readonly fieldSchema: InteractivitySchemaField | undefined;
+ readonly value: unknown;
+}): string => {
+ if (!fieldSchema) {
+ return formatUnknownTimelineValueForDisplay(value);
+ }
+
+ switch (fieldSchema.type) {
+ case 'number':
+ return (
+ formatNumberTimelineFieldValueForDisplay({fieldSchema, value}) ??
+ formatUnknownTimelineValueForDisplay(value)
+ );
+
+ case 'rotation-css':
+ case 'rotation-degrees':
+ return (
+ formatRotationTimelineFieldValueForDisplay({fieldSchema, value}) ??
+ formatUnknownTimelineValueForDisplay(value)
+ );
+
+ case 'scale':
+ return formatScaleTimelineFieldValueForDisplay({fieldSchema, value});
+
+ case 'translate':
+ return (
+ formatTranslateTimelineFieldValueForDisplay({fieldSchema, value}) ??
+ formatUnknownTimelineValueForDisplay(value)
+ );
+
+ case 'transform-origin':
+ return (
+ formatTransformOriginTimelineFieldValueForDisplay({
+ fieldSchema,
+ value,
+ }) ?? formatUnknownTimelineValueForDisplay(value)
+ );
+
+ case 'uv-coordinate':
+ return (
+ formatUvCoordinateTimelineFieldValueForDisplay({fieldSchema, value}) ??
+ formatUnknownTimelineValueForDisplay(value)
+ );
+
+ case 'array':
+ case 'boolean':
+ case 'color':
+ case 'enum':
+ case 'hidden':
+ return formatUnknownTimelineValueForDisplay(value);
+
+ default:
+ return formatUnknownTimelineValueForDisplay(value);
+ }
+};
diff --git a/packages/studio/src/test/timeline-field-utils.test.ts b/packages/studio/src/test/timeline-field-utils.test.ts
index 131543d865..3c807b698a 100644
--- a/packages/studio/src/test/timeline-field-utils.test.ts
+++ b/packages/studio/src/test/timeline-field-utils.test.ts
@@ -1,4 +1,5 @@
import {expect, test} from 'bun:test';
+import {formatTimelineFieldValueForDisplay} from '../components/Timeline/timeline-field-display-utils';
import {
formatTimelineNumber,
getDecimalPlaces,
@@ -48,3 +49,91 @@ test('formatTimelineNumber can omit unnecessary trailing zeroes', () => {
}),
).toBe('10');
});
+
+test('formatTimelineFieldValueForDisplay formats number fields based on step', () => {
+ expect(
+ formatTimelineFieldValueForDisplay({
+ fieldSchema: {
+ type: 'number',
+ default: 0,
+ hiddenFromList: false,
+ step: 0.01,
+ },
+ value: 1.2000000000000002,
+ }),
+ ).toBe('1.20');
+});
+
+test('formatTimelineFieldValueForDisplay formats scale fields with fixed decimals', () => {
+ expect(
+ formatTimelineFieldValueForDisplay({
+ fieldSchema: {
+ type: 'scale',
+ default: 1,
+ step: 0.01,
+ },
+ value: 2.044585,
+ }),
+ ).toBe('2.045');
+});
+
+test('formatTimelineFieldValueForDisplay formats rotation fields as degrees', () => {
+ expect(
+ formatTimelineFieldValueForDisplay({
+ fieldSchema: {
+ type: 'rotation-css',
+ default: '0deg',
+ step: 1,
+ },
+ value: '0.7853981633974483rad',
+ }),
+ ).toBe('45°');
+});
+
+test('formatTimelineFieldValueForDisplay formats translate fields as pixels', () => {
+ expect(
+ formatTimelineFieldValueForDisplay({
+ fieldSchema: {
+ type: 'translate',
+ default: '0px 0px',
+ step: 1,
+ },
+ value: '12.345px 0px',
+ }),
+ ).toBe('12.3px 0px');
+});
+
+test('formatTimelineFieldValueForDisplay preserves transform-origin units', () => {
+ expect(
+ formatTimelineFieldValueForDisplay({
+ fieldSchema: {
+ type: 'transform-origin',
+ default: '50% 50%',
+ step: 0.01,
+ },
+ value: '10.1234px 20.5678%',
+ }),
+ ).toBe('10.12px 20.57%');
+});
+
+test('formatTimelineFieldValueForDisplay formats UV coordinate tuples', () => {
+ expect(
+ formatTimelineFieldValueForDisplay({
+ fieldSchema: {
+ type: 'uv-coordinate',
+ default: [0.5, 0.5],
+ step: 0.01,
+ },
+ value: [0.123456, 0.5],
+ }),
+ ).toBe('0.12, 0.50');
+});
+
+test('formatTimelineFieldValueForDisplay rounds unknown numeric fallbacks', () => {
+ expect(
+ formatTimelineFieldValueForDisplay({
+ fieldSchema: undefined,
+ value: 10.000000000000002,
+ }),
+ ).toBe('10');
+});