Skip to content

Commit 38837a9

Browse files
committed
fix(studio): static-position drag no longer freezes an element beside an animated rotation
1 parent a1abd57 commit 38837a9

8 files changed

Lines changed: 280 additions & 112 deletions

‎packages/studio/src/hooks/gsapDragCommit.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { commitGsapPositionFromDrag } from "./gsapDragPositionCommit";
55
import {
66
commitStaticGsapPosition,
77
commitStaticGsapRotation,
8+
findExistingPositionWrite,
89
parkPlayheadOnKeyframe,
910
type GsapDragCommitCallbacks,
1011
} from "./gsapDragCommit";
@@ -75,6 +76,7 @@ describe("commitGsapPositionFromDrag — flat tween", () => {
7576
});
7677

7778
it("extends the existing tween (never spawns a parallel one) when dragged OUTSIDE its range", async () => {
79+
// fallow-ignore-next-line code-duplication
7880
usePlayerStore.setState({ currentTime: 6 }); // outside [1.2, 3.4]
7981
const { types, callbacks } = recordingCallbacks();
8082

@@ -94,7 +96,9 @@ describe("commitGsapPositionFromDrag — flat tween", () => {
9496
});
9597

9698
it("adds a keyframe at the playhead when dragged INSIDE its range", async () => {
99+
// fallow-ignore-next-line code-duplication
97100
usePlayerStore.setState({ currentTime: 2 }); // inside [1.2, 3.4]
101+
// fallow-ignore-next-line code-duplication
98102
const { types, callbacks } = recordingCallbacks();
99103

100104
await commitGsapPositionFromDrag(
@@ -115,6 +119,7 @@ describe("commitGsapPositionFromDrag — flat tween", () => {
115119
// User clicked the 100% diamond (activeKeyframePct=100), playhead drifted past
116120
// the end. Expect: convert + add-keyframe AT 100% — not replace-with-keyframes.
117121
usePlayerStore.setState({ currentTime: 6, activeKeyframePct: 100 }); // outside [1.2, 3.4]
122+
// fallow-ignore-next-line code-duplication
118123
const { types, mutations, callbacks } = recordingCallbacks();
119124

120125
await commitGsapPositionFromDrag(
@@ -338,6 +343,7 @@ describe("commitStaticGsapPosition — instantPatch (value-only set)", () => {
338343
await commitStaticGsapPosition(
339344
selection(),
340345
{ x: -50, y: 30 },
346+
// fallow-ignore-next-line code-duplication
341347
{ x: 0, y: 0 },
342348
"#puck-a",
343349
null, // no existing set → `add` a new base gsap.set
@@ -352,6 +358,55 @@ describe("commitStaticGsapPosition — instantPatch (value-only set)", () => {
352358
});
353359
});
354360

361+
// A degenerate `tl.to("#el",{keyframes:{...},duration:0})` — what a pre-fix drag
362+
// left behind when it routed a STATIC position hold (sitting beside a keyframed
363+
// rotation) into the keyframe branch. A duration-0 keyframed tween renders its
364+
// final keyframe at every playhead, so the element froze and "couldn't move".
365+
const keyframedZeroDurationHold = (): GsapAnimation =>
366+
({
367+
id: "#puck-a-frozen",
368+
targetSelector: "#puck-a",
369+
method: "to",
370+
propertyGroup: "position",
371+
duration: 0,
372+
keyframes: {
373+
keyframes: [
374+
{ percentage: 0, properties: { x: 100, y: 50 } },
375+
{ percentage: 100, properties: { x: -260, y: -70 } },
376+
],
377+
},
378+
properties: {},
379+
}) as unknown as GsapAnimation;
380+
381+
describe("static position hold recognition + heal (frozen duration-0 keyframed tween)", () => {
382+
beforeEach(() => usePlayerStore.setState({ currentTime: 0, activeKeyframePct: null }));
383+
384+
it("findExistingPositionWrite recognizes a keyframed zero-duration position hold", () => {
385+
const found = findExistingPositionWrite([keyframedZeroDurationHold()], "#puck-a");
386+
expect(found?.id).toBe("#puck-a-frozen");
387+
});
388+
389+
it("commitStaticGsapPosition heals a keyframed hold by delete + clean add-set (never update-property)", async () => {
390+
const { commits, callbacks } = optionRecordingCallbacks();
391+
392+
await commitStaticGsapPosition(
393+
selection(),
394+
{ x: -50, y: 30 },
395+
{ x: 0, y: 0 },
396+
"#puck-a",
397+
keyframedZeroDurationHold(),
398+
callbacks,
399+
);
400+
401+
const types = commits.map((c) => c.mutation.type);
402+
// Can't update-property into keyframes — must delete the frozen tween and
403+
// write a clean static set, so the element becomes freely movable.
404+
expect(types).toEqual(["delete", "add"]);
405+
expect(types).not.toContain("update-property");
406+
expect((commits[1].mutation as { method?: string }).method).toBe("set");
407+
});
408+
});
409+
355410
describe("commitStaticGsapRotation — instantPatch (value-only set)", () => {
356411
beforeEach(() => usePlayerStore.setState({ currentTime: 0, activeKeyframePct: null }));
357412

@@ -377,6 +432,7 @@ describe("commitStaticGsapRotation — instantPatch (value-only set)", () => {
377432
it("ADDS a global gsap.set with a global-set instantPatch (off-timeline, no flash)", async () => {
378433
const { commits, callbacks } = optionRecordingCallbacks();
379434

435+
// fallow-ignore-next-line code-duplication
380436
await commitStaticGsapRotation(selection(), 42, "#puck-a", null, callbacks);
381437

382438
expect(commits).toHaveLength(1);

‎packages/studio/src/hooks/gsapDragCommit.ts‎

Lines changed: 41 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ import { resolveTweenStart, resolveTweenDuration } from "../utils/globalTimeComp
1414
import { roundTo3 } from "../utils/rounding";
1515
import { computeElementPercentage } from "./gsapShared";
1616
import { 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";
1827
export 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
502432
export 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);

‎packages/studio/src/hooks/gsapDragPositionCommit.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ async function commitFlatViaKeyframes(
202202
}
203203

204204
const coalesceKey = `gsap:convert-drag:${anim.id}`;
205+
// fallow-ignore-next-line code-duplication
205206
await callbacks.commitMutation(
206207
selection,
207208
{
@@ -229,6 +230,7 @@ async function commitFlatViaKeyframes(
229230
if (editedSelected) parkPlayheadOnKeyframe(anim, pct);
230231
}
231232

233+
// fallow-ignore-next-line code-duplication
232234
// fallow-ignore-next-line complexity
233235
export async function commitGsapPositionFromDrag(
234236
selection: DomEditSelection,
@@ -240,6 +242,7 @@ export async function commitGsapPositionFromDrag(
240242
callbacks: GsapDragCommitCallbacks,
241243
): Promise<void> {
242244
const el = selection.element;
245+
// fallow-ignore-next-line code-duplication
243246
const { newX, newY, baseGsapX, baseGsapY } = computeDraggedGsapPosition(
244247
el,
245248
studioOffset,

0 commit comments

Comments
 (0)