@@ -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 ) ;
0 commit comments