Skip to content

Commit ba8df26

Browse files
committed
fix(studio): resolve keyframe fallbacks against the clicked element
Three review follow-ups on the editor-callback consolidation. The keyframe-target resolve now takes the clicked element's key and reads that element's keyframe cache. The diamond context menu and move-to-playhead pass no explicit target, so they fell through to the cache of whatever element happened to be selected: opening the menu on a non-selected element's diamond resolved against the wrong keyframes. PropertyPanelFlat opens the Motion group by adjusting state during render instead of in an effect, so the AnimationCard mounts on the same commit the focus request arrives on rather than a frame later. Both animation sections pass a module-level focus consumer instead of a fresh inline arrow, so AnimationCard's focus effect stops re-running on every parent render.
1 parent 2d1b905 commit ba8df26

6 files changed

Lines changed: 99 additions & 19 deletions

File tree

‎packages/studio/src/components/editor/GsapAnimationSection.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AnimationCard } from "./AnimationCard";
66
import {
77
type GsapAnimationEditCallbacks,
88
withTrackedGsapAnimationCallbacks,
9+
clearFocusedEaseSegment,
910
} from "./gsapAnimationCallbacks";
1011
import { useTrackDesignInput } from "../../contexts/DesignPanelInputContext";
1112
import { usePlayerStore } from "../../player";
@@ -29,7 +30,6 @@ export const GsapAnimationSection = memo(function GsapAnimationSection({
2930
const [addMenuOpen, setAddMenuOpen] = useState(false);
3031
const trackedCallbacks = withTrackedGsapAnimationCallbacks(callbacks, track);
3132
const focusedEaseSegment = usePlayerStore((s) => s.focusedEaseSegment);
32-
const setFocusedEaseSegment = usePlayerStore((s) => s.setFocusedEaseSegment);
3333

3434
return (
3535
<Section title="Animation" icon={<Film size={15} />}>
@@ -57,7 +57,7 @@ export const GsapAnimationSection = memo(function GsapAnimationSection({
5757
focusedSegment={
5858
focusedEaseSegment?.animationId === anim.id ? focusedEaseSegment : null
5959
}
60-
onFocusSegmentConsumed={() => setFocusedEaseSegment(null)}
60+
onFocusSegmentConsumed={clearFocusedEaseSegment}
6161
/>
6262
))}
6363

‎packages/studio/src/components/editor/PropertyPanelFlat.tsx‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,22 @@ export function PropertyPanelFlat({
249249
// force the Motion group open so its AnimationCard (which only mounts while
250250
// the group is expanded) can consume the focus and reveal the ease editor.
251251
const focusedEaseSegment = usePlayerStore((s) => s.focusedEaseSegment);
252-
// Identity of the element THIS panel actually renders (not the store's
253-
// selectedElementId, which flips synchronously on selection while the panel
254-
// still renders the previous element during async DOM-selection resolution):
255-
// a stale panel would otherwise consume a focus request meant for its
256-
// successor when both share a class-selector animation id.
252+
// The element THIS panel renders, not the store's selectedElementId: that
253+
// flips synchronously while the panel still renders its predecessor, so a
254+
// stale panel would consume a request meant for its successor whenever the
255+
// two share a class-selector animation id.
257256
const renderedElementId = `${element.sourceFile}#${element.id}`;
258-
useEffect(() => {
259-
if (!focusedEaseSegment || focusedEaseSegment.elementId !== renderedElementId) return;
260-
if (gsapAnimations.some((a) => a.id === focusedEaseSegment.animationId)) {
261-
setOpenGroupId("motion");
262-
}
263-
}, [focusedEaseSegment, gsapAnimations, renderedElementId]);
257+
// Adjusted during render (not an effect) so the card mounts on the same
258+
// commit the request lands on. Keyed on request identity: a group the user
259+
// closes afterwards stays closed.
260+
const [consumedFocus, setConsumedFocus] = useState(focusedEaseSegment);
261+
if (focusedEaseSegment !== consumedFocus) {
262+
setConsumedFocus(focusedEaseSegment);
263+
const focusesThisPanel =
264+
focusedEaseSegment?.elementId === renderedElementId &&
265+
gsapAnimations.some((a) => a.id === focusedEaseSegment.animationId);
266+
if (focusesThisPanel) setOpenGroupId("motion");
267+
}
264268

265269
const [justToggledIds, setJustToggledIds] = useState<string[]>([]);
266270
const justToggledTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ArcPathSegment } from "@hyperframes/parsers/gsap-parser";
2+
import { usePlayerStore } from "../../player";
23

34
/**
45
* Edit callbacks shared by GsapAnimationSection and each AnimationCard it
@@ -155,3 +156,12 @@ export function withTrackedGsapAnimationCallbacks(
155156
: undefined,
156157
};
157158
}
159+
160+
/**
161+
* Stable consumer for the store's one-shot ease-focus request. Module-level on
162+
* purpose: an inline arrow in the section components is a dep of AnimationCard's
163+
* focus effect, so a fresh identity each render re-runs that effect every render.
164+
*/
165+
export function clearFocusedEaseSegment(): void {
166+
usePlayerStore.getState().setFocusedEaseSegment(null);
167+
}

‎packages/studio/src/components/editor/propertyPanelFlatMotionSection.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { AnimationCard } from "./AnimationCard";
99
import {
1010
type GsapAnimationEditCallbacks,
1111
withTrackedGsapAnimationCallbacks,
12+
clearFocusedEaseSegment,
1213
} from "./gsapAnimationCallbacks";
1314
import { deriveElementTiming } from "./propertyPanelFlatTimingDerivation";
1415
import { usePlayerStore } from "../../player";
@@ -138,7 +139,6 @@ export function FlatMotionSection({
138139
const [addMenuOpen, setAddMenuOpen] = useState(false);
139140
const trackedCallbacks = withTrackedGsapAnimationCallbacks(callbacks, track);
140141
const focusedEaseSegment = usePlayerStore((s) => s.focusedEaseSegment);
141-
const setFocusedEaseSegment = usePlayerStore((s) => s.setFocusedEaseSegment);
142142
// Only consume a focus request aimed at the element THIS panel renders (not
143143
// the store's selectedElementId, which flips synchronously during async
144144
// selection resolution), so a shared class-selector animation id can't open
@@ -182,7 +182,7 @@ export function FlatMotionSection({
182182
defaultExpanded={index === 0}
183183
flat
184184
focusedSegment={focusedHere?.animationId === anim.id ? focusedHere : null}
185-
onFocusSegmentConsumed={() => setFocusedEaseSegment(null)}
185+
onFocusSegmentConsumed={clearFocusedEaseSegment}
186186
/>
187187
))}
188188
<GsapAddAnimationControl

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,59 @@ describe("useTimelineEditCallbacks — flat tween keyframe lanes", () => {
244244
view.unmount();
245245
});
246246

247+
// The diamond context menu opens on whatever diamond was clicked, which need
248+
// not belong to the selected element, and it passes no explicit target — so
249+
// the resolve falls back to the cache. Reading the SELECTED element's cache
250+
// there resolves against the wrong element's keyframes.
251+
it("resolves a cache fallback against the clicked element, not the selected one", () => {
252+
const circle: TimelineElement = {
253+
...element,
254+
id: "circle",
255+
key: "scenes/main.html#circle",
256+
domId: "circle",
257+
};
258+
usePlayerStore.setState({
259+
elements: [element, circle],
260+
gsapAnimations: new Map([["scenes/main.html#circle", [otherKeyframedAnimation]]]),
261+
keyframeCache: new Map([
262+
// Decoy at the same clip-% under the selected element's key.
263+
[
264+
"box",
265+
{
266+
format: "percentage",
267+
keyframes: [{ percentage: 100, tweenPercentage: 100, properties: { x: 420 } }],
268+
},
269+
],
270+
[
271+
"scenes/main.html#circle",
272+
{
273+
format: "percentage",
274+
keyframes: [
275+
{
276+
percentage: 100,
277+
tweenPercentage: 100,
278+
propertyGroup: "position",
279+
animationId: otherKeyframedAnimation.id,
280+
properties: { x: 420 },
281+
},
282+
],
283+
},
284+
],
285+
]),
286+
});
287+
const view = renderCallbacks();
288+
289+
act(() => {
290+
view.callbacks.onMoveKeyframeToPlayhead?.("scenes/main.html#circle", 100);
291+
});
292+
293+
expect(mocks.actions.handleGsapMoveKeyframeToPlayhead).toHaveBeenCalledWith(
294+
otherKeyframedAnimation.id,
295+
100,
296+
);
297+
view.unmount();
298+
});
299+
247300
it("keeps selected-element flat boundary deletion on the animation delete path", () => {
248301
const view = renderCallbacks();
249302

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,18 @@ export function useTimelineEditCallbacks({
143143
tweenPercentage?: number,
144144
animationId?: string,
145145
animations: GsapAnimation[] = selectedGsapAnimations,
146+
elementKey?: string,
146147
): { animId: string; tweenPct: number } | null => {
147148
const explicitTarget =
148149
propertyGroup !== undefined || tweenPercentage !== undefined || animationId !== undefined
149150
? [{ percentage: pct, propertyGroup, tweenPercentage, animationId }]
150151
: undefined;
151-
const cached = usePlayerStore.getState().keyframeCache.get(domEditSelection?.id ?? "");
152+
// The clicked element's own cache when the caller knows it: the diamond
153+
// context menu can open on an element that is not the selected one, and
154+
// reading the selection's cache there resolves against the wrong element.
155+
const cached = usePlayerStore
156+
.getState()
157+
.keyframeCache.get(elementKey ?? domEditSelection?.id ?? "");
152158
return resolveTimelineKeyframeTarget(
153159
pct,
154160
explicitTarget ?? cached?.keyframes ?? [],
@@ -198,7 +204,7 @@ export function useTimelineEditCallbacks({
198204
},
199205
onDeleteKeyframe: (elId, pct, group, tweenPct, animationId) => {
200206
const animations = resolveElementAnimations(elId);
201-
const target = resolveKeyframeTarget(pct, group, tweenPct, animationId, animations);
207+
const target = resolveKeyframeTarget(pct, group, tweenPct, animationId, animations, elId);
202208
if (!target) return;
203209
const element = usePlayerStore.getState().elements.find((el) => (el.key ?? el.id) === elId);
204210
if (!element) {
@@ -213,8 +219,15 @@ export function useTimelineEditCallbacks({
213219
});
214220
},
215221
// Retime the keyframe to the playhead, preserving its value + ease.
216-
onMoveKeyframeToPlayhead: (_elId, pct, group, tweenPct, animationId) => {
217-
const target = resolveKeyframeTarget(pct, group, tweenPct, animationId);
222+
onMoveKeyframeToPlayhead: (elId, pct, group, tweenPct, animationId) => {
223+
const target = resolveKeyframeTarget(
224+
pct,
225+
group,
226+
tweenPct,
227+
animationId,
228+
resolveElementAnimations(elId),
229+
elId,
230+
);
218231
if (target) handleGsapMoveKeyframeToPlayhead(target.animId, target.tweenPct);
219232
},
220233
// Drag-to-retime. The diamond reports clip-%s; resolveKeyframeTarget gives

0 commit comments

Comments
 (0)