Skip to content

Commit 270179d

Browse files
committed
fix(studio): harden flat keyframe retiming
1 parent f25a136 commit 270179d

4 files changed

Lines changed: 76 additions & 19 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ describe("resolveKeyframeRetime — move (within the tween window)", () => {
5858
expect(r.duration).toBe(3);
5959
expect(r.pctRemap).toEqual([]);
6060
});
61+
62+
it("no-ops an unexpected interior percentage on a flat tween", () => {
63+
expect(
64+
resolveKeyframeRetime({
65+
...WINDOW,
66+
keyframes: [],
67+
draggedTweenPct: 50,
68+
dropAbsTime: 5,
69+
}).kind,
70+
).toBe("noop");
71+
});
72+
73+
it("no-ops instead of rounding a sub-10ms flat tween to zero", () => {
74+
expect(
75+
resolveKeyframeRetime({
76+
tweenStart: 2,
77+
tweenDuration: 4,
78+
keyframes: [],
79+
draggedTweenPct: 100,
80+
dropAbsTime: 2.0004,
81+
}).kind,
82+
).toBe("noop");
83+
});
6184
});
6285

6386
describe("resolveKeyframeRetime — resize (past the tween boundary)", () => {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export interface KeyframeRetimeResult {
5151
const NOOP_EPSILON_PCT = 0.1;
5252
/** Slack (seconds) for the within-tween boundary test. */
5353
const EPSILON_TIME = 1e-4;
54+
/** Smallest authored tween window; avoids sub-millisecond/round-to-zero durations. */
55+
const MIN_TWEEN_DURATION = 0.01;
5456

5557
const round3 = (n: number) => Math.round(n * 1000) / 1000;
5658
const round1 = (n: number) => Math.round(n * 10) / 10; // 0.1% precision
@@ -65,16 +67,20 @@ function resolveFlatTweenBoundaryRetime(opts: {
6567
dropAbsTime: number;
6668
}): KeyframeRetimeResult | null {
6769
const { keyframeCount, draggedTweenPct, tweenStart, tweenEnd, dropAbsTime } = opts;
68-
if (keyframeCount > 0 || (draggedTweenPct !== 0 && draggedTweenPct !== 100)) return null;
70+
if (keyframeCount > 0) return null;
71+
// Flat tweens have only synthesized boundary diamonds. Never delegate an
72+
// unexpected interior percentage to the authored-keyframe move path.
73+
if (draggedTweenPct !== 0 && draggedTweenPct !== 100) return { kind: "noop" };
6974
const draggedTime = draggedTweenPct === 0 ? tweenStart : tweenEnd;
7075
if (Math.abs(dropAbsTime - draggedTime) <= EPSILON_TIME) return { kind: "noop" };
7176
const newStart = draggedTweenPct === 0 ? dropAbsTime : tweenStart;
7277
const newEnd = draggedTweenPct === 100 ? dropAbsTime : tweenEnd;
73-
if (newEnd <= newStart + EPSILON_TIME) return { kind: "noop" };
78+
const newDuration = newEnd - newStart;
79+
if (newDuration < MIN_TWEEN_DURATION) return { kind: "noop" };
7480
return {
7581
kind: "resize",
7682
position: round3(newStart),
77-
duration: round3(newEnd - newStart),
83+
duration: round3(newDuration),
7884
pctRemap: [],
7985
};
8086
}
@@ -127,7 +133,7 @@ export function resolveKeyframeRetime(opts: {
127133

128134
const newStart = Math.min(dropAbsTime, tweenStart);
129135
const newEnd = Math.max(dropAbsTime, tweenEnd);
130-
const newDuration = Math.max(0.01, newEnd - newStart);
136+
const newDuration = Math.max(MIN_TWEEN_DURATION, newEnd - newStart);
131137

132138
// The dragged keyframe is the one whose tween-% is closest to draggedTweenPct.
133139
let draggedIdx = 0;

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,37 @@ describe("resolveTimelineKeyframeTarget", () => {
4040
).toBeNull();
4141
});
4242

43-
it("prefers an explicit property-group match", () => {
43+
it("keeps a keyframed and flat tween in the same property group unresolved", () => {
44+
expect(
45+
resolveTimelineKeyframeTarget(
46+
50,
47+
[{ percentage: 50, tweenPercentage: 25, propertyGroup: "position" }],
48+
[
49+
{ id: "position-keyframed", propertyGroup: "position", keyframes: {} },
50+
{ id: "position-flat", propertyGroup: "position" },
51+
],
52+
),
53+
).toBeNull();
54+
});
55+
56+
it("keeps multiple ungrouped keyframed tweens unresolved", () => {
57+
expect(
58+
resolveTimelineKeyframeTarget(
59+
50,
60+
[{ percentage: 50, tweenPercentage: 25 }],
61+
[
62+
{ id: "ungrouped-a", keyframes: {} },
63+
{ id: "ungrouped-b", keyframes: {} },
64+
],
65+
),
66+
).toBeNull();
67+
});
68+
69+
it("does not infer an animation when the rendered diamond misses the cache", () => {
70+
expect(resolveTimelineKeyframeTarget(60, [], [FLAT_ANIMATION])).toBeNull();
71+
});
72+
73+
it("resolves the sole candidate in an explicit property group", () => {
4474
expect(
4575
resolveTimelineKeyframeTarget(
4676
50,

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,24 @@ interface TimelineCachedKeyframe {
5151
/**
5252
* Resolve a rendered timeline diamond back to the animation that authored it.
5353
* 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.
54+
* a property group nor real keyframes. The cache currently carries a property
55+
* group, not an animation id, so resolution is safe only when that group has a
56+
* single candidate. Ambiguous candidates remain unresolved rather than
57+
* retiming an arbitrary tween.
5758
*/
5859
export function resolveTimelineKeyframeTarget(
5960
pct: number,
6061
keyframes: ReadonlyArray<TimelineCachedKeyframe>,
6162
animations: ReadonlyArray<TimelineKeyframeTargetAnimation>,
6263
): { animId: string; tweenPct: number } | null {
6364
const kf = keyframes.find((item) => Math.abs(item.percentage - pct) < 0.2);
65+
if (!kf) return null;
6466
const group = kf?.propertyGroup;
65-
const groupedCandidates = group
67+
const candidates = group
6668
? 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;
69+
: animations.filter((animation) => !animation.propertyGroup);
70+
const animation = candidates.length === 1 ? candidates[0] : undefined;
71+
return animation ? { animId: animation.id, tweenPct: kf.tweenPercentage ?? pct } : null;
7772
}
7873

7974
/**
@@ -197,6 +192,9 @@ export function useTimelineEditCallbacks({
197192
decision.pctRemap,
198193
);
199194
} else {
195+
// resize-keyframed-tween requires an authored `keyframes` AST node
196+
// and intentionally no-ops for a flat tween. Update its real tween
197+
// window through the metadata writer (and SDK cutover path) instead.
200198
handleGsapUpdateMeta(target.animId, {
201199
position: decision.position,
202200
duration: decision.duration,

0 commit comments

Comments
 (0)