@@ -14,7 +14,16 @@ import { resolveTweenStart, resolveTweenDuration } from "../utils/globalTimeComp
1414import { roundTo3 } from "../utils/rounding" ;
1515import { computeElementPercentage } from "./gsapShared" ;
1616import { computeDraggedGsapPosition } from "./draggedGsapPosition" ;
17- import type { RuntimeTweenChange , SetPatchProps } from "./gsapRuntimePatch" ;
17+ import type { RuntimeTweenChange } from "./gsapRuntimePatch" ;
18+ import {
19+ setPatchFromUpdateProperties ,
20+ setPatchFromUpdateProperty ,
21+ } from "./gsapDragStaticSetHelpers" ;
22+ export {
23+ findExistingPositionWrite ,
24+ findRotationSetAnimation ,
25+ findSizeSetAnimation ,
26+ } from "./gsapDragStaticSetHelpers" ;
1827export interface GsapDragCommitCallbacks {
1928 commitMutation : (
2029 selection : DomEditSelection ,
@@ -103,86 +112,6 @@ export async function materializeIfDynamic(
103112
104113// ── Drag → GSAP position math ──────────────────────────────────────────────
105114
106- /** The shape of an `update-property` mutation a static-set nudge POSTs. */
107- interface UpdatePropertyMutation {
108- type : "update-property" ;
109- animationId : string ;
110- property : string ;
111- value : number ;
112- }
113-
114- /**
115- * Build the `instantPatch` for a value-only `tl.set` from the SAME
116- * `update-property` mutation(s) that are POSTed — so the patch can never carry a
117- * value the source write didn't (one source of truth). Each mutation contributes
118- * its `{property: value}` channel to the patch's props.
119- */
120- function setPatchFromUpdateProperties (
121- selector : string ,
122- mutations : UpdatePropertyMutation [ ] ,
123- global = false ,
124- ) : { selector : string ; change : RuntimeTweenChange } {
125- const props : SetPatchProps = { } ;
126- for ( const m of mutations ) props [ m . property as keyof SetPatchProps ] = m . value ;
127- // An off-timeline `gsap.set` has no runtime tween to patch — apply it to the
128- // element directly. An on-timeline `tl.set` mutates its tween (so a re-seek keeps it).
129- return { selector, change : { kind : global ? "global-set" : "set" , props } } ;
130- }
131-
132- /** Single-mutation convenience over {@link setPatchFromUpdateProperties}. */
133- function setPatchFromUpdateProperty (
134- selector : string ,
135- mutation : UpdatePropertyMutation ,
136- global = false ,
137- ) : { selector : string ; change : RuntimeTweenChange } {
138- return setPatchFromUpdateProperties ( selector , [ mutation ] , global ) ;
139- }
140-
141- /**
142- * Find the studio position-hold `set` for a selector — a `tl.set("#el",{x,y})`
143- * with no duration. This is what a static-element nudge writes/updates.
144- */
145- function findPositionSetAnimation (
146- animations : GsapAnimation [ ] ,
147- selector : string ,
148- ) : GsapAnimation | null {
149- return (
150- animations . find (
151- ( a ) =>
152- a . method === "set" &&
153- a . targetSelector === selector &&
154- ( "x" in a . properties || "y" in a . properties ) ,
155- ) ?? null
156- ) ;
157- }
158-
159- /**
160- * Find the EXISTING static position HOLD to update for a static-hold drag. Not
161- * just a `set`: a degenerate `tl.to("#el",{duration:0,x,y})` (what
162- * remove-all-keyframes leaves behind) is a held position too, and the next drag
163- * must UPDATE it in place rather than append a second `gsap.set` that fights it
164- * (the duplicate-position-write bug). Only zero-duration holds qualify — a
165- * live-duration `to`/`from` is NOT a static hold (and in the static path it's a
166- * stale/phantom parse: re-committing it would resurrect a just-deleted tween).
167- * Prefers a `set` (the canonical static channel) when both forms exist.
168- */
169- function findExistingPositionWrite (
170- animations : GsapAnimation [ ] ,
171- selector : string ,
172- ) : GsapAnimation | null {
173- const set = findPositionSetAnimation ( animations , selector ) ;
174- if ( set ) return set ;
175- return (
176- animations . find (
177- ( a ) =>
178- a . targetSelector === selector &&
179- a . propertyGroup === "position" &&
180- ! a . keyframes &&
181- ( a . duration ?? 0 ) === 0 ,
182- ) ?? null
183- ) ;
184- }
185-
186115/**
187116 * Commit a STATIC element drag as a `tl.set("#el",{x,y})` — the single-source
188117 * position channel for elements with no position animation. Idempotent: a
@@ -201,6 +130,34 @@ export async function commitStaticGsapPosition(
201130) : Promise < void > {
202131 const { newX, newY } = computeDraggedGsapPosition ( selection . element , studioOffset , gsapPos ) ;
203132 if ( existingSet ) {
133+ if ( existingSet . keyframes ) {
134+ // Keyframed zero-duration hold (drag-path corruption): can't update-property
135+ // into keyframes — delete it and write a clean static set instead.
136+ const coalesceKey = `gsap:heal-static:${ existingSet . id } ` ;
137+ await callbacks . commitMutation (
138+ selection ,
139+ { type : "delete" , animationId : existingSet . id } ,
140+ { label : "Move layer" , skipReload : true , coalesceKey } ,
141+ ) ;
142+ await callbacks . commitMutation (
143+ selection ,
144+ {
145+ type : "add" ,
146+ targetSelector : selector ,
147+ method : "set" ,
148+ position : 0 ,
149+ properties : { x : newX , y : newY } ,
150+ global : true ,
151+ } ,
152+ {
153+ label : "Move layer" ,
154+ softReload : true ,
155+ coalesceKey,
156+ instantPatch : { selector, change : { kind : "global-set" , props : { x : newX , y : newY } } } ,
157+ } ,
158+ ) ;
159+ return ;
160+ }
204161 // Update in place — two single-property mutations (the API updates one prop
205162 // per call). Coalesce them and reload only after the second lands.
206163 const coalesceKey = `gsap:set-nudge:${ existingSet . id } ` ;
@@ -262,19 +219,6 @@ export async function commitStaticGsapPosition(
262219 ) ;
263220}
264221
265- export { findExistingPositionWrite } ;
266-
267- function findRotationSetAnimation (
268- animations : GsapAnimation [ ] ,
269- selector : string ,
270- ) : GsapAnimation | null {
271- return (
272- animations . find (
273- ( a ) => a . method === "set" && a . targetSelector === selector && "rotation" in a . properties ,
274- ) ?? null
275- ) ;
276- }
277-
278222/**
279223 * Commit a STATIC element rotation as a `tl.set("#el",{rotation})` — the single-
280224 * source rotation channel for elements with no rotation animation (mirrors
@@ -328,19 +272,6 @@ export async function commitStaticGsapRotation(
328272 ) ;
329273}
330274
331- export { findRotationSetAnimation } ;
332-
333- function findSizeSetAnimation ( animations : GsapAnimation [ ] , selector : string ) : GsapAnimation | null {
334- return (
335- animations . find (
336- ( a ) =>
337- a . method === "set" &&
338- a . targetSelector === selector &&
339- ( "width" in a . properties || "height" in a . properties ) ,
340- ) ?? null
341- ) ;
342- }
343-
344275/**
345276 * Commit a STATIC element resize as a `tl.set("#el",{width,height})` — the
346277 * single-source size channel for elements with no size animation (mirrors
@@ -489,15 +420,14 @@ export async function commitKeyframedSizeFromResize(
489420 return true ;
490421}
491422
492- export { findSizeSetAnimation } ;
493-
494423// ── Whole-path offset (plain drag on animated element) ──────────────────
495424
496425/**
497426 * Offset the entire animation path by the drag delta — every keyframe's x/y
498427 * shifts together so the animation shape is preserved and the element can't
499428 * dart off-screen. For flat tweens (no keyframes), convert first then shift.
500429 */
430+ // fallow-ignore-next-line code-duplication
501431// fallow-ignore-next-line complexity
502432export async function commitWholePathOffset (
503433 selection : DomEditSelection ,
@@ -515,6 +445,7 @@ export async function commitWholePathOffset(
515445 gsapPos ,
516446 ) ;
517447 const deltaX = newX - baseGsapX ;
448+ // fallow-ignore-next-line code-duplication
518449 const deltaY = newY - baseGsapY ;
519450 const origX = Number . parseFloat ( el . getAttribute ( "data-hf-drag-initial-offset-x" ) ?? "" ) || 0 ;
520451 const origY = Number . parseFloat ( el . getAttribute ( "data-hf-drag-initial-offset-y" ) ?? "" ) || 0 ;
@@ -525,6 +456,7 @@ export async function commitWholePathOffset(
525456 el . removeAttribute ( "data-hf-drag-initial-offset-y" ) ;
526457 } ;
527458
459+ // fallow-ignore-next-line code-duplication
528460 let effectiveAnim = anim ;
529461 if ( anim . keyframes ) {
530462 const newId = await materializeIfDynamic ( anim , iframe , callbacks . commitMutation , selection ) ;
0 commit comments