Skip to content

Commit f25a136

Browse files
committed
fix(studio): retime flat tween keyframe diamonds
1 parent e4a30f6 commit f25a136

4 files changed

Lines changed: 183 additions & 32 deletions

File tree

‎packages/studio/src/components/editor/keyframeRetime.test.ts‎

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const KEYFRAMES: RetimeKeyframe[] = [
99
{ percentage: 100, properties: { x: 100 }, ease: "power2.in" },
1010
];
1111
const WINDOW = { tweenStart: 2, tweenDuration: 4 };
12+
const LEFT_BOUNDARY_DROP = { ...WINDOW, dropAbsTime: 0.5 };
1213

1314
describe("resolveKeyframeRetime — move (within the tween window)", () => {
1415
it("re-keys an interior keyframe to the tween-% of the drop", () => {
@@ -32,15 +33,30 @@ describe("resolveKeyframeRetime — move (within the tween window)", () => {
3233
expect(r.kind).toBe("noop");
3334
});
3435

35-
it("moves a flat (keyframe-less) tween without needing the keyframes array", () => {
36+
it("shortens a flat tween when its synthesized end diamond moves left", () => {
3637
const r = resolveKeyframeRetime({
3738
...WINDOW,
3839
keyframes: [],
3940
draggedTweenPct: 100,
40-
dropAbsTime: 5, // (5-2)/4 = 75%
41+
dropAbsTime: 5,
4142
});
42-
expect(r.kind).toBe("move");
43-
expect(r.toTweenPct).toBeCloseTo(75, 5);
43+
expect(r.kind).toBe("resize");
44+
expect(r.position).toBe(2);
45+
expect(r.duration).toBe(3);
46+
expect(r.pctRemap).toEqual([]);
47+
});
48+
49+
it("moves a flat tween's start while preserving its absolute end", () => {
50+
const r = resolveKeyframeRetime({
51+
...WINDOW,
52+
keyframes: [],
53+
draggedTweenPct: 0,
54+
dropAbsTime: 3,
55+
});
56+
expect(r.kind).toBe("resize");
57+
expect(r.position).toBe(3);
58+
expect(r.duration).toBe(3);
59+
expect(r.pctRemap).toEqual([]);
4460
});
4561
});
4662

@@ -67,10 +83,9 @@ describe("resolveKeyframeRetime — resize (past the tween boundary)", () => {
6783

6884
it("extends the FIRST keyframe before the start, shifting position earlier", () => {
6985
const r = resolveKeyframeRetime({
70-
...WINDOW,
86+
...LEFT_BOUNDARY_DROP,
7187
keyframes: KEYFRAMES,
7288
draggedTweenPct: 0,
73-
dropAbsTime: 0.5, // before start (2) → move position back + grow duration
7489
});
7590
expect(r.kind).toBe("resize");
7691
expect(r.position).toBeCloseTo(0.5, 5);
@@ -102,10 +117,9 @@ describe("resolveKeyframeRetime — single keyframe (both first and last)", () =
102117

103118
it("resizes left before the start", () => {
104119
const r = resolveKeyframeRetime({
105-
...WINDOW,
120+
...LEFT_BOUNDARY_DROP,
106121
keyframes: lone,
107122
draggedTweenPct: 100,
108-
dropAbsTime: 0.5,
109123
});
110124
expect(r.kind).toBe("resize");
111125
expect(r.position).toBeCloseTo(0.5, 5);
@@ -127,14 +141,16 @@ describe("resolveKeyframeRetime — guards", () => {
127141
).toBe("noop");
128142
});
129143

130-
it("no-ops a boundary drop on a flat tween (nothing to remap)", () => {
131-
expect(
132-
resolveKeyframeRetime({
133-
...WINDOW,
134-
keyframes: [],
135-
draggedTweenPct: 100,
136-
dropAbsTime: 8,
137-
}).kind,
138-
).toBe("noop");
144+
it("extends a flat tween when its synthesized end diamond moves right", () => {
145+
const r = resolveKeyframeRetime({
146+
...WINDOW,
147+
keyframes: [],
148+
draggedTweenPct: 100,
149+
dropAbsTime: 8,
150+
});
151+
expect(r.kind).toBe("resize");
152+
expect(r.position).toBe(2);
153+
expect(r.duration).toBe(6);
154+
expect(r.pctRemap).toEqual([]);
139155
});
140156
});

‎packages/studio/src/components/editor/keyframeRetime.ts‎

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@ const round3 = (n: number) => Math.round(n * 1000) / 1000;
5656
const round1 = (n: number) => Math.round(n * 10) / 10; // 0.1% precision
5757
const clamp = (n: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, n));
5858

59+
/** Resolve timing for a flat tween's synthesized start/end diamond. */
60+
function resolveFlatTweenBoundaryRetime(opts: {
61+
keyframeCount: number;
62+
draggedTweenPct: number;
63+
tweenStart: number;
64+
tweenEnd: number;
65+
dropAbsTime: number;
66+
}): KeyframeRetimeResult | null {
67+
const { keyframeCount, draggedTweenPct, tweenStart, tweenEnd, dropAbsTime } = opts;
68+
if (keyframeCount > 0 || (draggedTweenPct !== 0 && draggedTweenPct !== 100)) return null;
69+
const draggedTime = draggedTweenPct === 0 ? tweenStart : tweenEnd;
70+
if (Math.abs(dropAbsTime - draggedTime) <= EPSILON_TIME) return { kind: "noop" };
71+
const newStart = draggedTweenPct === 0 ? dropAbsTime : tweenStart;
72+
const newEnd = draggedTweenPct === 100 ? dropAbsTime : tweenEnd;
73+
if (newEnd <= newStart + EPSILON_TIME) return { kind: "noop" };
74+
return {
75+
kind: "resize",
76+
position: round3(newStart),
77+
duration: round3(newEnd - newStart),
78+
pctRemap: [],
79+
};
80+
}
81+
5982
/**
6083
* Decide move vs resize for a dragged keyframe and, for resize, return the new
6184
* tween window + remapped keyframes.
@@ -76,15 +99,30 @@ export function resolveKeyframeRetime(opts: {
7699
if (tweenDuration <= 0) return { kind: "noop" };
77100
const tweenEnd = tweenStart + tweenDuration;
78101

79-
// Within the tween window → plain move (re-key the tween-%). This branch never
80-
// touches the keyframes array, so it still works for synthesized flat tweens.
102+
// A flat tween's synthesized diamonds are its real start/end boundaries —
103+
// there is no authored keyframe node to re-key. Moving either boundary must
104+
// therefore resize the tween window while keeping the opposite endpoint at
105+
// its absolute time. Returning `resize` with an empty remap lets the caller
106+
// update the flat tween's position/duration instead of sending a keyframe
107+
// mutation that would silently no-op.
108+
const flatBoundary = resolveFlatTweenBoundaryRetime({
109+
keyframeCount: keyframes.length,
110+
draggedTweenPct,
111+
tweenStart,
112+
tweenEnd,
113+
dropAbsTime,
114+
});
115+
if (flatBoundary) return flatBoundary;
116+
117+
// Within the tween window → plain move (re-key the tween-%).
81118
if (dropAbsTime >= tweenStart - EPSILON_TIME && dropAbsTime <= tweenEnd + EPSILON_TIME) {
82119
const toTweenPct = clamp(((dropAbsTime - tweenStart) / tweenDuration) * 100, 0, 100);
83120
if (Math.abs(toTweenPct - draggedTweenPct) < NOOP_EPSILON_PCT) return { kind: "noop" };
84121
return { kind: "move", toTweenPct };
85122
}
86123

87-
// Boundary resize needs the real keyframes to remap; a flat tween has none here.
124+
// Boundary resize needs the real keyframes to remap. Flat start/end boundaries
125+
// were handled above.
88126
if (keyframes.length === 0) return { kind: "noop" };
89127

90128
const newStart = Math.min(dropAbsTime, tweenStart);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { describe, expect, it } from "vitest";
2+
import { resolveTimelineKeyframeTarget } from "./useTimelineEditCallbacks";
3+
4+
const FLAT_ANIMATION = {
5+
id: "#title-fromTo-2900",
6+
propertyGroup: null,
7+
};
8+
9+
describe("resolveTimelineKeyframeTarget", () => {
10+
it("resolves a synthesized diamond when one mixed flat tween is selected", () => {
11+
expect(
12+
resolveTimelineKeyframeTarget(
13+
8.75,
14+
[{ percentage: 8.75, tweenPercentage: 100 }],
15+
[FLAT_ANIMATION],
16+
),
17+
).toEqual({ animId: "#title-fromTo-2900", tweenPct: 100 });
18+
});
19+
20+
it("keeps multiple ungrouped flat tweens unresolved instead of guessing", () => {
21+
expect(
22+
resolveTimelineKeyframeTarget(
23+
8.75,
24+
[{ percentage: 8.75, tweenPercentage: 100 }],
25+
[FLAT_ANIMATION, { id: "#title-to-2900", propertyGroup: null }],
26+
),
27+
).toBeNull();
28+
});
29+
30+
it("keeps multiple flat tweens in the same property group unresolved", () => {
31+
expect(
32+
resolveTimelineKeyframeTarget(
33+
50,
34+
[{ percentage: 50, tweenPercentage: 100, propertyGroup: "position" }],
35+
[
36+
{ id: "position-a", propertyGroup: "position" },
37+
{ id: "position-b", propertyGroup: "position" },
38+
],
39+
),
40+
).toBeNull();
41+
});
42+
43+
it("prefers an explicit property-group match", () => {
44+
expect(
45+
resolveTimelineKeyframeTarget(
46+
50,
47+
[{ percentage: 50, tweenPercentage: 25, propertyGroup: "position" }],
48+
[
49+
{ id: "opacity", propertyGroup: "opacity" },
50+
{ id: "position", propertyGroup: "position" },
51+
],
52+
),
53+
).toEqual({ animId: "position", tweenPct: 25 });
54+
});
55+
});

‎packages/studio/src/components/nle/useTimelineEditCallbacks.ts‎

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,46 @@ export interface TimelineEditCallbackDeps {
3636
handleRazorSplitAll: (splitTime: number) => Promise<void> | void;
3737
}
3838

39+
interface TimelineKeyframeTargetAnimation {
40+
id: string;
41+
propertyGroup?: string | null;
42+
keyframes?: unknown;
43+
}
44+
45+
interface TimelineCachedKeyframe {
46+
percentage: number;
47+
tweenPercentage?: number;
48+
propertyGroup?: string;
49+
}
50+
51+
/**
52+
* Resolve a rendered timeline diamond back to the animation that authored it.
53+
* Flat tweens use synthesized diamonds, so a mixed flat tween may have neither
54+
* a property group nor real keyframes. It is safe to fall back only when that
55+
* flat tween is the selection's sole animation; multiple candidates remain
56+
* unresolved rather than retiming an arbitrary tween.
57+
*/
58+
export function resolveTimelineKeyframeTarget(
59+
pct: number,
60+
keyframes: ReadonlyArray<TimelineCachedKeyframe>,
61+
animations: ReadonlyArray<TimelineKeyframeTargetAnimation>,
62+
): { animId: string; tweenPct: number } | null {
63+
const kf = keyframes.find((item) => Math.abs(item.percentage - pct) < 0.2);
64+
const group = kf?.propertyGroup;
65+
const groupedCandidates = group
66+
? animations.filter((animation) => animation.propertyGroup === group)
67+
: [];
68+
const groupedKeyframed = groupedCandidates.find((animation) => animation.keyframes);
69+
const soleGroupedFlat =
70+
groupedCandidates.length === 1 && !groupedCandidates[0]?.keyframes
71+
? groupedCandidates[0]
72+
: undefined;
73+
const keyframed = animations.find((animation) => animation.keyframes);
74+
const soleFlat = animations.length === 1 && !animations[0]?.keyframes ? animations[0] : undefined;
75+
const animation = groupedKeyframed ?? soleGroupedFlat ?? keyframed ?? soleFlat;
76+
return animation ? { animId: animation.id, tweenPct: kf?.tweenPercentage ?? pct } : null;
77+
}
78+
3979
/**
4080
* Builds the timeline edit callback bag (move/resize/split/razor plus the
4181
* keyframe-diamond callbacks) provided to `<Timeline>` via TimelineEditProvider.
@@ -76,12 +116,7 @@ export function useTimelineEditCallbacks({
76116
// fallow-ignore-next-line complexity
77117
(pct: number): { animId: string; tweenPct: number } | null => {
78118
const cached = usePlayerStore.getState().keyframeCache.get(domEditSelection?.id ?? "");
79-
const kf = cached?.keyframes.find((k) => Math.abs(k.percentage - pct) < 0.2);
80-
const group = kf?.propertyGroup;
81-
const anim =
82-
(group ? selectedGsapAnimations.find((a) => a.propertyGroup === group) : undefined) ??
83-
selectedGsapAnimations.find((a) => a.keyframes);
84-
return anim ? { animId: anim.id, tweenPct: kf?.tweenPercentage ?? pct } : null;
119+
return resolveTimelineKeyframeTarget(pct, cached?.keyframes ?? [], selectedGsapAnimations);
85120
},
86121
[domEditSelection?.id, selectedGsapAnimations],
87122
);
@@ -154,12 +189,19 @@ export function useTimelineEditCallbacks({
154189
decision.position != null &&
155190
decision.duration != null
156191
) {
157-
handleGsapResizeKeyframedTween(
158-
target.animId,
159-
decision.position,
160-
decision.duration,
161-
decision.pctRemap,
162-
);
192+
if (anim.keyframes) {
193+
handleGsapResizeKeyframedTween(
194+
target.animId,
195+
decision.position,
196+
decision.duration,
197+
decision.pctRemap,
198+
);
199+
} else {
200+
handleGsapUpdateMeta(target.animId, {
201+
position: decision.position,
202+
duration: decision.duration,
203+
});
204+
}
163205
}
164206
},
165207
onChangeKeyframeEase: (_elId: string, _pct: number, ease: string) => {

0 commit comments

Comments
 (0)