Skip to content

Commit 9fc0011

Browse files
committed
fix(studio): give every keyframe callback one mutation frame
Each timeline keyframe callback now reads and writes through the SAME element: the clicked element's animations resolve the target, its selection commits the mutation, and its animation computes the playhead percentage. onMoveKeyframeToPlayhead previously took the percentage from the clicked element and the animation plus selection from the current one, so a context menu on a non-selected diamond retimed against one tween and wrote into another file. onChangeKeyframeEase and onToggleKeyframeAtPlayhead had the same split. An explicit null selection override now aborts the write instead of falling back to the current selection.
1 parent a423c93 commit 9fc0011

4 files changed

Lines changed: 158 additions & 60 deletions

File tree

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,16 @@ describe("useTimelineEditCallbacks — flat tween keyframe lanes", () => {
255255
// not belong to the selected element, and it passes no explicit target — so
256256
// the resolve falls back to the cache. Reading the SELECTED element's cache
257257
// there resolves against the wrong element's keyframes.
258-
it("resolves a cache fallback against the clicked element, not the selected one", () => {
258+
it("resolves a cache fallback against the clicked element, not the selected one", async () => {
259259
const circle: TimelineElement = {
260260
...element,
261261
id: "circle",
262262
key: "scenes/main.html#circle",
263263
domId: "circle",
264+
sourceFile: "scenes/main.html",
264265
};
266+
const circleSelection = { id: "circle", selector: "#circle", sourceFile: "scenes/main.html" };
267+
mocks.actions.buildDomSelectionForTimelineElement.mockResolvedValue(circleSelection);
265268
usePlayerStore.setState({
266269
elements: [element, circle],
267270
gsapAnimations: new Map([["scenes/main.html#circle", [otherKeyframedAnimation]]]),
@@ -293,13 +296,18 @@ describe("useTimelineEditCallbacks — flat tween keyframe lanes", () => {
293296
});
294297
const view = renderCallbacks();
295298

296-
act(() => {
299+
await act(async () => {
297300
view.callbacks.onMoveKeyframeToPlayhead?.("scenes/main.html#circle", { percentage: 100 });
301+
await Promise.resolve();
298302
});
299303

304+
// The retime target, the selection it commits through, and the animation the
305+
// playhead percentage is computed against all come from the CLICKED element.
300306
expect(mocks.actions.handleGsapMoveKeyframeToPlayhead).toHaveBeenCalledWith(
301307
otherKeyframedAnimation.id,
302308
100,
309+
circleSelection,
310+
otherKeyframedAnimation,
303311
);
304312
view.unmount();
305313
});
@@ -316,7 +324,10 @@ describe("useTimelineEditCallbacks — flat tween keyframe lanes", () => {
316324
});
317325
});
318326

319-
expect(mocks.actions.handleGsapDeleteAnimation).toHaveBeenCalledWith(flatAnimation.id);
327+
expect(mocks.actions.handleGsapDeleteAnimation).toHaveBeenCalledWith(
328+
flatAnimation.id,
329+
undefined,
330+
);
320331
expect(mocks.actions.handleGsapRemoveKeyframe).not.toHaveBeenCalled();
321332
view.unmount();
322333
});
@@ -391,7 +402,12 @@ describe("useTimelineEditCallbacks — flat tween keyframe lanes", () => {
391402
});
392403
});
393404

394-
expect(mocks.actions.handleGsapRemoveKeyframe).toHaveBeenCalledWith(flatAnimation.id, 50);
405+
expect(mocks.actions.handleGsapRemoveKeyframe).toHaveBeenCalledWith(
406+
flatAnimation.id,
407+
50,
408+
undefined,
409+
undefined,
410+
);
395411
expect(mocks.actions.handleGsapDeleteAnimation).not.toHaveBeenCalled();
396412
view.unmount();
397413
});

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

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,10 @@ export function useTimelineEditCallbacks({
171171
) => {
172172
const animation = animations.find((candidate) => candidate.id === animationId);
173173
if (animation && !animation.keyframes) {
174-
if (selectionOverride === undefined) handleGsapDeleteAnimation(animationId);
175-
else handleGsapDeleteAnimation(animationId, selectionOverride);
174+
handleGsapDeleteAnimation(animationId, selectionOverride);
176175
return;
177176
}
178-
if (selectionOverride === undefined) handleGsapRemoveKeyframe(animationId, percentage);
179-
else handleGsapRemoveKeyframe(animationId, percentage, undefined, selectionOverride);
177+
handleGsapRemoveKeyframe(animationId, percentage, undefined, selectionOverride);
180178
},
181179
[handleGsapDeleteAnimation, handleGsapRemoveKeyframe],
182180
);
@@ -216,10 +214,25 @@ export function useTimelineEditCallbacks({
216214
removeKeyframeTarget(target.animId, target.tweenPct, animations, selection);
217215
});
218216
},
219-
// Retime the keyframe to the playhead, preserving its value + ease.
217+
// Retime the keyframe to the playhead, preserving its value + ease. The
218+
// clicked element owns the whole write: its animations resolve the target,
219+
// its selection commits it, and its animation computes the playhead
220+
// percentage. Mixing frames here retimed against the selected element's
221+
// tween and wrote the result into the clicked element's file.
220222
onMoveKeyframeToPlayhead: (elId, keyframe) => {
221-
const target = resolveKeyframeTarget(keyframe, resolveElementAnimations(elId), elId);
222-
if (target) handleGsapMoveKeyframeToPlayhead(target.animId, target.tweenPct);
223+
const animations = resolveElementAnimations(elId);
224+
const target = resolveKeyframeTarget(keyframe, animations, elId);
225+
const animation = target
226+
? animations.find((candidate) => candidate.id === target.animId)
227+
: undefined;
228+
if (!target || !animation) return;
229+
const element = usePlayerStore.getState().elements.find((el) => (el.key ?? el.id) === elId);
230+
if (!element) return;
231+
void buildDomSelectionForTimelineElement(element).then((selection) => {
232+
if (selection) {
233+
handleGsapMoveKeyframeToPlayhead(target.animId, target.tweenPct, selection, animation);
234+
}
235+
});
223236
},
224237
// Drag-to-retime. The diamond reports clip-%s; resolveKeyframeTarget gives
225238
// the dragged keyframe's anim + tween-%. We convert the clip-% drop to an
@@ -294,10 +307,19 @@ export function useTimelineEditCallbacks({
294307
}
295308
return true;
296309
},
297-
onChangeKeyframeEase: (_elId: string, _pct: number, ease: string) => {
298-
for (const anim of selectedGsapAnimations) {
299-
if (anim.keyframes) handleGsapUpdateMeta(anim.id, { ease });
300-
}
310+
onChangeKeyframeEase: (elId: string, _pct: number, ease: string) => {
311+
// The edited element's own animations + selection, not the selection's:
312+
// an ease change on a non-selected lane otherwise rewrote whichever
313+
// element happened to be selected, in whichever file it lives.
314+
const animations = resolveElementAnimations(elId);
315+
const element = usePlayerStore.getState().elements.find((el) => (el.key ?? el.id) === elId);
316+
if (!element) return;
317+
void buildDomSelectionForTimelineElement(element).then((selection) => {
318+
if (!selection) return;
319+
for (const anim of animations) {
320+
if (anim.keyframes) handleGsapUpdateMeta(anim.id, { ease }, selection);
321+
}
322+
});
301323
},
302324
// fallow-ignore-next-line complexity
303325
onToggleKeyframeAtPlayhead: (el: TimelineElement) => {
@@ -306,18 +328,34 @@ export function useTimelineEditCallbacks({
306328
el.duration > 0
307329
? Math.max(0, Math.min(100, Math.round(((currentTime - el.start) / el.duration) * 100)))
308330
: 0;
309-
const anim = selectedGsapAnimations.find((a) => a.keyframes);
310-
if (anim?.keyframes) {
311-
const existing = anim.keyframes.keyframes.find((k) => Math.abs(k.percentage - pct) <= 1);
312-
if (existing) {
313-
handleGsapRemoveKeyframe(anim.id, existing.percentage);
331+
// Same frame for read and write: the toggled element's animations decide
332+
// add-vs-remove, and its selection is what the mutation commits through.
333+
const animations = resolveElementAnimations(el.key ?? el.id);
334+
void buildDomSelectionForTimelineElement(el).then((selection) => {
335+
if (!selection) return;
336+
const anim = animations.find((a) => a.keyframes);
337+
if (anim?.keyframes) {
338+
const existing = anim.keyframes.keyframes.find(
339+
(k) => Math.abs(k.percentage - pct) <= 1,
340+
);
341+
if (existing) {
342+
handleGsapRemoveKeyframe(anim.id, existing.percentage, undefined, selection);
343+
} else {
344+
handleGsapAddKeyframe(anim.id, pct, "x", 0, selection);
345+
}
314346
} else {
315-
handleGsapAddKeyframe(anim.id, pct, "x", 0);
347+
const flatAnim = animations.find((a) => !a.keyframes);
348+
if (flatAnim) {
349+
void handleGsapConvertToKeyframes(
350+
flatAnim.id,
351+
undefined,
352+
undefined,
353+
undefined,
354+
selection,
355+
);
356+
}
316357
}
317-
} else {
318-
const flatAnim = selectedGsapAnimations.find((a) => !a.keyframes);
319-
if (flatAnim) handleGsapConvertToKeyframes(flatAnim.id);
320-
}
358+
});
321359
},
322360
onTogglePropertyGroupKeyframe: async (element, target) => {
323361
const selection = await buildDomSelectionForTimelineElement(element);

‎packages/studio/src/hooks/useGsapSelectionHandlers.test.tsx‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { act } from "react";
33
import { createRoot } from "react-dom/client";
44
import { describe, expect, it, vi } from "vitest";
5+
import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
56
import type { DomEditSelection } from "../components/editor/domEditingTypes";
67
import { useGsapSelectionHandlers } from "./useGsapSelectionHandlers";
78

@@ -114,3 +115,32 @@ describe("useGsapSelectionHandlers save failures", () => {
114115
rendered.unmount();
115116
});
116117
});
118+
119+
describe("useGsapSelectionHandlers selection override", () => {
120+
it("aborts on an explicit null override instead of writing to the current selection", () => {
121+
const removeKeyframe = vi.fn();
122+
const rendered = renderHandlers(makeParams({ removeKeyframe }));
123+
124+
// Explicit null: the caller resolved a selection for its own element and
125+
// found none, so the write must not land on the selected element.
126+
rendered.handlers().handleGsapRemoveKeyframe("anim-1", 50, undefined, null);
127+
expect(removeKeyframe).not.toHaveBeenCalled();
128+
129+
// Omitted override: falls back to the current selection as before.
130+
rendered.handlers().handleGsapRemoveKeyframe("anim-1", 50);
131+
expect(removeKeyframe).toHaveBeenCalledOnce();
132+
rendered.unmount();
133+
});
134+
135+
it("computes the playhead percentage from the passed animation, not the selection's", () => {
136+
const moveKeyframe = vi.fn();
137+
const selection = makeSelection();
138+
const animation = { id: "anim-1", keyframes: { keyframes: [] } } as unknown as GsapAnimation;
139+
const rendered = renderHandlers(makeParams({ moveKeyframe, selectedGsapAnimations: [] }));
140+
141+
rendered.handlers().handleGsapMoveKeyframeToPlayhead("anim-1", 50, selection, animation);
142+
143+
expect(moveKeyframe).toHaveBeenCalledWith(selection, "anim-1", 50, expect.any(Number));
144+
rendered.unmount();
145+
});
146+
});

0 commit comments

Comments
 (0)