Skip to content

Write HTML. Render video. Built for agents.

feat(studio): instant, flicker-free manual editing (#1605)

Miguel Ángel091137e3c32f
diff --git a/packages/core/src/lint/rules/gsap.test.ts b/packages/core/src/lint/rules/gsap.test.ts
index af649ed614..89a70b5247 100644
--- a/packages/core/src/lint/rules/gsap.test.ts
+++ b/packages/core/src/lint/rules/gsap.test.ts
@@ -978,106 +978,6 @@ describe("GSAP rules", () => {
expect(finding).toBeUndefined();
});
- // gsap_studio_edit_blocked
- it("warns when script registers timeline AND has GSAP tweens targeting #id selectors", async () => {
- const html = `
-<html><body>
- <div data-composition-id="c1" data-width="1920" data-height="1080">
- <div id="headline" style="position:absolute;left:120px;top:200px;">Hello</div>
- </div>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
- <script>
- window.__timelines = window.__timelines || {};
- const tl = gsap.timeline({ paused: true });
- tl.set("#headline", { opacity: 0 });
- tl.to("#headline", { opacity: 1, duration: 0.5 }, 0);
- window.__timelines["c1"] = tl;
- </script>
-</body></html>`;
- const result = await lintHyperframeHtml(html);
- const finding = result.findings.find((f) => f.code === "gsap_studio_edit_blocked");
- expect(finding).toBeDefined();
- expect(finding?.severity).toBe("warning");
- expect(finding?.message).toContain('"#headline"');
- });
-
- it("warns when script registers timeline AND has GSAP tweens targeting .class selectors", async () => {
- const html = `
-<html><body>
- <div data-composition-id="c1" data-width="1920" data-height="1080">
- <div class="box" style="position:absolute;left:120px;top:200px;"></div>
- </div>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
- <script>
- window.__timelines = window.__timelines || {};
- const tl = gsap.timeline({ paused: true });
- tl.from(".box", { y: 80, opacity: 0, duration: 0.4 }, 0);
- window.__timelines["c1"] = tl;
- </script>
-</body></html>`;
- const result = await lintHyperframeHtml(html);
- const finding = result.findings.find((f) => f.code === "gsap_studio_edit_blocked");
- expect(finding).toBeDefined();
- expect(finding?.message).toContain('".box"');
- });
-
- it("does NOT warn when timeline is registered but no GSAP element selectors are called", async () => {
- const html = `
-<html><body>
- <div data-composition-id="c1" data-width="1920" data-height="1080"></div>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
- <script>
- window.__timelines = window.__timelines || {};
- const tl = gsap.timeline({ paused: true });
- window.__timelines["c1"] = tl;
- </script>
-</body></html>`;
- const result = await lintHyperframeHtml(html);
- const finding = result.findings.find((f) => f.code === "gsap_studio_edit_blocked");
- expect(finding).toBeUndefined();
- });
-
- it("does NOT warn when script has GSAP calls but does not register on window.__timelines", async () => {
- const html = `
-<html><body>
- <div data-composition-id="c1" data-width="1920" data-height="1080">
- <div id="box"></div>
- </div>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
- <script>
- window.__timelines = window.__timelines || {};
- const tl = gsap.timeline({ paused: true });
- tl.to("#box", { x: 100, duration: 1 }, 0);
- </script>
-</body></html>`;
- const result = await lintHyperframeHtml(html);
- const finding = result.findings.find((f) => f.code === "gsap_studio_edit_blocked");
- expect(finding).toBeUndefined();
- });
-
- it("lists all unique targeted selectors in the warning message", async () => {
- const html = `
-<html><body>
- <div data-composition-id="c1" data-width="1920" data-height="1080">
- <div id="title"></div>
- <div id="sub"></div>
- </div>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
- <script>
- window.__timelines = window.__timelines || {};
- const tl = gsap.timeline({ paused: true });
- tl.from("#title", { opacity: 0, duration: 0.3 }, 0);
- tl.from("#sub", { opacity: 0, duration: 0.3 }, 0.2);
- window.__timelines["c1"] = tl;
- </script>
-</body></html>`;
- const result = await lintHyperframeHtml(html);
- const finding = result.findings.find((f) => f.code === "gsap_studio_edit_blocked");
- expect(finding).toBeDefined();
- expect(finding?.message).toContain('"#title"');
- expect(finding?.message).toContain('"#sub"');
- });
-
it("scene_layer_missing_visibility_kill: fires when multi-scene exit lacks hard kill", async () => {
const html = `
<html><body>
diff --git a/packages/core/src/lint/rules/gsap.ts b/packages/core/src/lint/rules/gsap.ts
index 4e2d67b743..257d01af69 100644
--- a/packages/core/src/lint/rules/gsap.ts
+++ b/packages/core/src/lint/rules/gsap.ts
@@ -27,7 +27,6 @@ import {
truncateSnippet,
stripJsComments,
WINDOW_TIMELINE_ASSIGN_PATTERN,
- TIMELINE_REGISTRY_ASSIGN_PATTERN,
} from "../utils";
// ── GSAP-specific types ────────────────────────────────────────────────────
@@ -855,39 +854,4 @@ export const gsapRules: LintRule<LintContext>[] = [
}
return findings;
},
-
- // gsap_studio_edit_blocked
- // When a script both registers a timeline on window.__timelines AND contains
- // GSAP mutation calls targeting element selectors, Studio's isElementGsapTargeted
- // check returns true for those elements and silently skips saving drag/resize
- // position changes back to source HTML.
- ({ scripts }) => {
- const findings: HyperframeLintFinding[] = [];
- const GSAP_MUTATION_SELECTOR_RE = /\.\s*(?:set|to|from|fromTo)\s*\(\s*["']([#.][^"']+)["']/g;
-
- for (const script of scripts) {
- const content = stripJsComments(script.content);
- if (!TIMELINE_REGISTRY_ASSIGN_PATTERN.test(content)) continue;
-
- const targets = new Set<string>();
- let match: RegExpExecArray | null;
- const re = new RegExp(GSAP_MUTATION_SELECTOR_RE.source, "g");
- while ((match = re.exec(content)) !== null) {
- if (match[1]) targets.add(match[1]);
- }
- if (targets.size === 0) continue;
-
- const selList = [...targets].map((s) => `"${s}"`).join(", ");
- findings.push({
- code: "gsap_studio_edit_blocked",
- severity: "warning",
- message: `GSAP tweens target ${selList} in a registered timeline. Studio cannot save drag/resize edits to these elements — the runtime skips write-back for any element that appears in a registered window.__timelines timeline.`,
- fixHint:
- "The hyperframes runtime registers timelines automatically. Do not add a manual window.__timelines script unless GSAP intentionally controls element positions. " +
- "For initial visibility states, use CSS (e.g. opacity:0) instead of gsap.set(). " +
- "If GSAP must own these elements' positions, avoid drag-editing them in Studio.",
- });
- }
- return findings;
- },
];
diff --git a/packages/core/src/parsers/gsapConstants.ts b/packages/core/src/parsers/gsapConstants.ts
index 0153623956..5976e4b664 100644
--- a/packages/core/src/parsers/gsapConstants.ts
+++ b/packages/core/src/parsers/gsapConstants.ts
@@ -77,7 +77,10 @@ export function classifyTweenPropertyGroup(
): PropertyGroupName | undefined {
const groups = new Set<PropertyGroupName>();
for (const key of Object.keys(properties)) {
- if (key === "transformOrigin") continue;
+ // transformOrigin is a modifier; `_auto` is Studio's internal endpoint marker;
+ // `data` is GSAP-reserved (carries the Studio hold-set tag). None is an animated
+ // property, so none should affect the group.
+ if (key === "transformOrigin" || key === "_auto" || key === "data") continue;
const g = classifyPropertyGroup(key);
groups.add(g);
}
diff --git a/packages/core/src/parsers/gsapParser.test.ts b/packages/core/src/parsers/gsapParser.test.ts
index 7f56fd0a71..3d785c6426 100644
--- a/packages/core/src/parsers/gsapParser.test.ts
+++ b/packages/core/src/parsers/gsapParser.test.ts
@@ -14,11 +14,16 @@ import {
addKeyframeToScript,
removeKeyframeFromScript,
updateKeyframeInScript,
+ updateMotionPathPointInScript,
+ addMotionPathPointInScript,
+ removeMotionPathPointInScript,
+ addMotionPathToScript,
convertToKeyframesInScript,
removeAllKeyframesFromScript,
addAnimationWithKeyframesToScript,
splitAnimationsInScript,
splitIntoPropertyGroups,
+ syncPositionHoldsBeforeKeyframes,
shiftPositionsInScript,
scalePositionsInScript,
} from "./gsapParser.js";
@@ -483,6 +488,20 @@ describe("property group classification", () => {
);
});
+ it("ignores the internal `_auto` endpoint marker when classifying", () => {
+ // Regression: the `_auto: 1` sentinel on auto-generated endpoint keyframes must
+ // not pull a position tween into a mixed group, or drag-intercept can't resolve it.
+ expect(classifyTweenPropertyGroup({ x: 100, y: 50, _auto: 1 })).toBe("position");
+ });
+
+ it("ignores the GSAP-reserved `data` key when classifying", () => {
+ // Regression: `data` is GSAP-reserved (Studio stores its hold-set tag there).
+ // It is not an animated property, so it must not pull a single-group tween into
+ // a mixed group (which would return undefined and break group-scoped editing).
+ expect(classifyTweenPropertyGroup({ x: 100, y: 50, data: "hold" })).toBe("position");
+ expect(classifyTweenPropertyGroup({ scale: 0.5, data: "hold" })).toBe("scale");
+ });
+
it("returns undefined for mixed-group tweens", () => {
expect(classifyTweenPropertyGroup({ x: 100, scale: 0.5 })).toBeUndefined();
expect(classifyTweenPropertyGroup({ x: 100, opacity: 0 })).toBeUndefined();
@@ -1560,6 +1579,98 @@ describe("keyframe mutations", () => {
expect(kfs[1].properties.x).toBe(999);
});
+ // ── backfillDefaults: editing one keyframe must not move the others ──────
+ // UX invariant (CapCut/AE): keyframes are independent. Introducing a property
+ // to one keyframe (e.g. `y` on an x-only tween) must backfill the other
+ // keyframes at the element's base value — otherwise GSAP holds the new prop's
+ // value across keyframes that omit it, dragging them to the same position.
+ const X_ONLY_SCRIPT = `
+ const tl = gsap.timeline({ paused: true });
+ tl.to("#puck", { keyframes: { "0%": { x: 0 }, "100%": { x: -260 } }, duration: 2.2 }, 1.2);
+ `;
+
+ it("addKeyframeToScript — WITHOUT backfill, the other keyframe omits the new prop (GSAP would hold it)", () => {
+ const id = getAnimId(X_ONLY_SCRIPT);
+ const updated = addKeyframeToScript(X_ONLY_SCRIPT, id, 0, { x: 240, y: 780 });
+ const kfs = parseGsapScript(updated).animations[0].keyframes!.keyframes;
+ const kf100 = kfs.find((k) => k.percentage === 100)!;
+ expect(kf100.properties.x).toBe(-260);
+ expect("y" in kf100.properties).toBe(false); // <- the bug surface
+ });
+
+ it("addKeyframeToScript — WITH backfill, the new prop is added to the other keyframe at base (it stays put)", () => {
+ const id = getAnimId(X_ONLY_SCRIPT);
+ const updated = addKeyframeToScript(X_ONLY_SCRIPT, id, 0, { x: 240, y: 780 }, undefined, {
+ x: 0,
+ y: 0,
+ });
+ const kfs = parseGsapScript(updated).animations[0].keyframes!.keyframes;
+ const kf0 = kfs.find((k) => k.percentage === 0)!;
+ const kf100 = kfs.find((k) => k.percentage === 100)!;
+ // edited keyframe holds the drag
+ expect(kf0.properties).toMatchObject({ x: 240, y: 780 });
+ // other keyframe keeps its own x and gets y at base (0) — not 780
+ expect(kf100.properties.x).toBe(-260);
+ expect(kf100.properties.y).toBe(0);
+ });
+
+ // ── syncPositionHoldsBeforeKeyframes (hold before first keyframe) ────────
+ // UX invariant (every NLE): before the first keyframe, the element holds that
+ // keyframe's value — it must NOT snap to its CSS base then jump when the tween
+ // starts. Implemented as a tagged `tl.set(...,0)` kept in sync with the tween.
+ describe("syncPositionHoldsBeforeKeyframes", () => {
+ const posTweenAt = (start: number) =>
+ `const tl = gsap.timeline({ paused: true });\n` +
+ `tl.to("#p", { keyframes: { "0%": { x: -1500, y: 700 }, "100%": { x: -260, y: 0 } }, duration: 2.2 }, ${start});`;
+
+ it("inserts a hold set holding the first keyframe's position at t=0", () => {
+ const out = syncPositionHoldsBeforeKeyframes(posTweenAt(1.2));
+ const anims = parseGsapScript(out).animations;
+ const hold = anims.find((a) => a.method === "set");
+ expect(hold).toBeDefined();
+ expect(hold!.position).toBe(0);
+ expect(hold!.properties).toMatchObject({ x: -1500, y: 700 });
+ });
+
+ it("is idempotent (re-running does not stack holds)", () => {
+ const once = syncPositionHoldsBeforeKeyframes(posTweenAt(1.2));
+ expect(syncPositionHoldsBeforeKeyframes(once)).toBe(once);
+ expect((once.match(/hf-hold/g) ?? []).length).toBe(1);
+ });
+
+ it("re-syncs the hold value when the first keyframe changes", () => {
+ const out1 = syncPositionHoldsBeforeKeyframes(posTweenAt(1.2));
+ const moved = updateKeyframeInScript(
+ out1,
+ parseGsapScript(out1).animations.find((a) => a.keyframes)!.id,
+ 0,
+ { x: 99, y: 88 },
+ );
+ const out2 = syncPositionHoldsBeforeKeyframes(moved);
+ const hold = parseGsapScript(out2).animations.find((a) => a.method === "set");
+ expect(hold!.properties).toMatchObject({ x: 99, y: 88 });
+ expect((out2.match(/hf-hold/g) ?? []).length).toBe(1); // still just one
+ });
+
+ it("adds no hold for a tween that already starts at t=0", () => {
+ expect(syncPositionHoldsBeforeKeyframes(posTweenAt(0))).not.toContain("hf-hold");
+ });
+
+ it("adds no hold for an opacity-only keyframed tween (position-scoped)", () => {
+ const opacity =
+ `const tl = gsap.timeline({ paused: true });\n` +
+ `tl.to("#b", { keyframes: { "0%": { opacity: 0 }, "100%": { opacity: 1 } }, duration: 1 }, 2);`;
+ expect(syncPositionHoldsBeforeKeyframes(opacity)).not.toContain("hf-hold");
+ });
+
+ it("removes an orphaned hold when its tween is gone", () => {
+ const withHold = syncPositionHoldsBeforeKeyframes(posTweenAt(1.2));
+ const tweenId = parseGsapScript(withHold).animations.find((a) => a.keyframes)!.id;
+ const deleted = removeAnimationFromScript(withHold, tweenId);
+ expect(syncPositionHoldsBeforeKeyframes(deleted)).not.toContain("hf-hold");
+ });
+ });
+
// ── _auto endpoint updates ────────────────────────────────────────────
const AUTO_SCRIPT = `
@@ -1681,6 +1792,241 @@ describe("keyframe mutations", () => {
expect(kf100.properties.y).toBe(50);
});
+ // Array-form keyframes (`keyframes: [{x,y}, …]`) carry no percentages — GSAP
+ // distributes them evenly. The motion-path overlay drags/adds by percentage,
+ // which used to no-op on array-authored tweens (#puck-b / #shuttle).
+ const ARRAY_KF_SCRIPT =
+ "const tl = gsap.timeline();\n" +
+ 'tl.to("#shuttle", { keyframes: [{ x: 0, y: 0 }, { x: 520, y: 120 }, { x: 1040, y: 0 }, { x: 1480, y: 160 }], duration: 4.4, ease: "none" }, 5.2);';
+
+ it("updateKeyframeInScript — array-form: drags node 2 (pct 33.3) by index", () => {
+ const id = getAnimId(ARRAY_KF_SCRIPT);
+ const updated = updateKeyframeInScript(ARRAY_KF_SCRIPT, id, 33.3, { x: 503, y: 642 });
+ expect(updated).not.toBe(ARRAY_KF_SCRIPT);
+ const kf = parseGsapScript(updated).animations[0].keyframes!.keyframes;
+ expect([kf[1]!.properties.x, kf[1]!.properties.y]).toEqual([503, 642]);
+ expect([kf[0]!.properties.x, kf[0]!.properties.y]).toEqual([0, 0]);
+ expect([kf[2]!.properties.x, kf[2]!.properties.y]).toEqual([1040, 0]);
+ });
+
+ it("addKeyframeToScript — array-form: normalizes to object form + inserts 50%", () => {
+ const id = getAnimId(ARRAY_KF_SCRIPT);
+ const updated = addKeyframeToScript(ARRAY_KF_SCRIPT, id, 50, { x: 780, y: 60 });
+ expect(updated).not.toBe(ARRAY_KF_SCRIPT);
+ const kf = parseGsapScript(updated).animations[0].keyframes!.keyframes;
+ expect(kf.length).toBe(5);
+ const at50 = kf.find((k) => Math.abs(k.percentage - 50) < 1)!;
+ expect([at50.properties.x, at50.properties.y]).toEqual([780, 60]);
+ });
+
+ it("removeKeyframeFromScript — array-form: drops node 3 (pct 66.7)", () => {
+ const id = getAnimId(ARRAY_KF_SCRIPT);
+ const updated = removeKeyframeFromScript(ARRAY_KF_SCRIPT, id, 66.7);
+ expect(updated).not.toBe(ARRAY_KF_SCRIPT);
+ const kf = parseGsapScript(updated).animations[0].keyframes!.keyframes;
+ expect(kf.length).toBe(3);
+ });
+
+ it("updateKeyframeInScript — stale position-id resolves to the nearest same-selector tween", () => {
+ // Tween authored at 1.0s → id "#el-to-1000-position". A client that cached the
+ // pre-reposition id "#el-to-1200-position" (a gesture/convert moved it) must
+ // still resolve, instead of no-op'ing.
+ const script =
+ "const tl = gsap.timeline();\n" +
+ 'tl.to("#el", { keyframes: { "0%": { x: 0, y: 0 }, "100%": { x: 50, y: 50 } }, duration: 2 }, 1);';
+ const updated = updateKeyframeInScript(script, "#el-to-1200-position", 100, { x: 77, y: 88 });
+ expect(updated).not.toBe(script);
+ const at100 = parseGsapScript(updated).animations[0].keyframes!.keyframes.find(
+ (k) => k.percentage === 100,
+ )!;
+ expect([at100.properties.x, at100.properties.y]).toEqual([77, 88]);
+ });
+
+ // ── updateMotionPathPointInScript ───────────────────────────────────────
+
+ const MOTION_PATH_SCRIPT = `
+ const tl = gsap.timeline({ paused: true });
+ tl.to("#el", {
+ motionPath: {
+ path: [{x: 0, y: 0}, {x: 200, y: -100}, {x: 400, y: 50}],
+ curviness: 1.5
+ },
+ duration: 2
+ }, 0);
+ `;
+
+ it("updateMotionPathPointInScript — moves one waypoint, preserves the rest and curviness", () => {
+ const id = getAnimId(MOTION_PATH_SCRIPT);
+ const updated = updateMotionPathPointInScript(MOTION_PATH_SCRIPT, id, 1, { x: 250, y: -140 });
+ const reparsed = parseGsapScript(updated);
+ const anim = reparsed.animations[0];
+ const wp = anim.keyframes!.keyframes;
+ expect(wp.map((k) => [k.properties.x, k.properties.y])).toEqual([
+ [0, 0],
+ [250, -140],
+ [400, 50],
+ ]);
+ expect(anim.arcPath!.segments[0].curviness).toBe(1.5);
+ expect(anim.arcPath!.segments[1].curviness).toBe(1.5);
+ });
+
+ it("updateMotionPathPointInScript — out-of-range index leaves the script unchanged", () => {
+ const id = getAnimId(MOTION_PATH_SCRIPT);
+ expect(updateMotionPathPointInScript(MOTION_PATH_SCRIPT, id, 9, { x: 1, y: 1 })).toBe(
+ MOTION_PATH_SCRIPT,
+ );
+ });
+
+ it("updateMotionPathPointInScript — unknown animation id leaves the script unchanged", () => {
+ expect(updateMotionPathPointInScript(MOTION_PATH_SCRIPT, "nope", 0, { x: 1, y: 1 })).toBe(
+ MOTION_PATH_SCRIPT,
+ );
+ });
+
+ it("updateMotionPathPointInScript — moves a cubic anchor, keeps control points", () => {
+ const script = `
+ const tl = gsap.timeline({ paused: true });
+ tl.to("#el", {
+ motionPath: {
+ path: [
+ {x: 0, y: 0},
+ {x: 50, y: -80}, {x: 150, y: -120},
+ {x: 200, y: -100}
+ ],
+ type: "cubic"
+ },
+ duration: 2
+ }, 0);
+ `;
+ const id = getAnimId(script);
+ const updated = updateMotionPathPointInScript(script, id, 1, { x: 220, y: -130 });
+ const reparsed = parseGsapScript(updated);
+ const anim = reparsed.animations[0];
+ // anchor 1 moved; the segment's control points are untouched.
+ expect(anim.keyframes!.keyframes[1].properties).toMatchObject({ x: 220, y: -130 });
+ expect(anim.arcPath!.segments[0].cp1).toEqual({ x: 50, y: -80 });
+ expect(anim.arcPath!.segments[0].cp2).toEqual({ x: 150, y: -120 });
+ });
+
+ // ── add/removeMotionPathPointInScript ───────────────────────────────────
+
+ it("addMotionPathPointInScript — inserts a waypoint between anchors, keeps curviness", () => {
+ const id = getAnimId(MOTION_PATH_SCRIPT);
+ const updated = addMotionPathPointInScript(MOTION_PATH_SCRIPT, id, 1, { x: 100, y: -50 });
+ const reparsed = parseGsapScript(updated);
+ const anim = reparsed.animations[0];
+ expect(anim.keyframes!.keyframes.map((k) => [k.properties.x, k.properties.y])).toEqual([
+ [0, 0],
+ [100, -50],
+ [200, -100],
+ [400, 50],
+ ]);
+ // 4 anchors → 3 segments, all curviness 1.5
+ expect(anim.arcPath!.segments).toHaveLength(3);
+ expect(anim.arcPath!.segments.every((s) => s.curviness === 1.5)).toBe(true);
+ });
+
+ it("addMotionPathPointInScript — refuses an index at the ends or out of range", () => {
+ const id = getAnimId(MOTION_PATH_SCRIPT);
+ expect(addMotionPathPointInScript(MOTION_PATH_SCRIPT, id, 0, { x: 1, y: 1 })).toBe(
+ MOTION_PATH_SCRIPT,
+ );
+ expect(addMotionPathPointInScript(MOTION_PATH_SCRIPT, id, 3, { x: 1, y: 1 })).toBe(
+ MOTION_PATH_SCRIPT,
+ );
+ });
+
+ it("removeMotionPathPointInScript — drops a waypoint, preserves the rest", () => {
+ const id = getAnimId(MOTION_PATH_SCRIPT);
+ const updated = removeMotionPathPointInScript(MOTION_PATH_SCRIPT, id, 1);
+ const reparsed = parseGsapScript(updated);
+ const anim = reparsed.animations[0];
+ expect(anim.keyframes!.keyframes.map((k) => [k.properties.x, k.properties.y])).toEqual([
+ [0, 0],
+ [400, 50],
+ ]);
+ expect(anim.arcPath!.segments).toHaveLength(1);
+ });
+
+ it("removeMotionPathPointInScript — refuses to drop below two anchors", () => {
+ const two = `
+ const tl = gsap.timeline({ paused: true });
+ tl.to("#el", { motionPath: { path: [{x: 0, y: 0}, {x: 400, y: 50}], curviness: 1 }, duration: 2 }, 0);
+ `;
+ const id = getAnimId(two);
+ expect(removeMotionPathPointInScript(two, id, 0)).toBe(two);
+ });
+
+ it("add/removeMotionPathPointInScript — leave cubic paths untouched", () => {
+ const cubic = `
+ const tl = gsap.timeline({ paused: true });
+ tl.to("#el", { motionPath: { path: [{x:0,y:0},{x:50,y:-80},{x:150,y:-120},{x:200,y:-100}], type: "cubic" }, duration: 2 }, 0);
+ `;
+ const id = getAnimId(cubic);
+ expect(addMotionPathPointInScript(cubic, id, 1, { x: 1, y: 1 })).toBe(cubic);
+ expect(removeMotionPathPointInScript(cubic, id, 1)).toBe(cubic);
+ });
+
+ // ── addMotionPathToScript ───────────────────────────────────────────────
+
+ it("addMotionPathToScript — authors a new 2-anchor motionPath tween", () => {
+ const script = `
+ const tl = gsap.timeline({ paused: true });
+ tl.from("#title", { opacity: 0, duration: 0.5 }, 0);
+ `;
+ const { script: updated, id } = addMotionPathToScript(script, "#el", 2.0, 1.5, {
+ x: 300,
+ y: -100,
+ });
+ expect(id).not.toBeNull();
+ const reparsed = parseGsapScript(updated);
+ const anim = reparsed.animations.find((a) => a.targetSelector === "#el")!;
+ expect(anim).toBeDefined();
+ expect(anim.arcPath!.enabled).toBe(true);
+ expect(anim.keyframes!.keyframes.map((k) => [k.properties.x, k.properties.y])).toEqual([
+ [0, 0],
+ [300, -100],
+ ]);
+ expect(anim.duration).toBe(1.5);
+ });
+
+ it("addMotionPathToScript — returns id:null (not '') when there is no timeline", () => {
+ // No `gsap.timeline()` and no located tweens → failure. The sentinel must be
+ // null so a downstream caller chaining on the id can null-check instead of
+ // silently feeding an empty selector into a locate call that matches nothing.
+ const { script: updated, id } = addMotionPathToScript("const x = 1;", "#el", 0, 1, {
+ x: 10,
+ y: 10,
+ });
+ expect(id).toBeNull();
+ expect(updated).toBe("const x = 1;");
+ });
+
+ it("addMotionPathToScript + hold-sync — holds (0,0) at t=0 when authored past t=0", () => {
+ // A motionPath authored at position > 0 parses with a first keyframe of (0,0).
+ // Without a pre-tween hold the element would snap to its CSS home at frame 0 and
+ // jump when the tween starts — this is why `add-motion-path` is hold-synced.
+ const script = `const tl = gsap.timeline({ paused: true });`;
+ const { script: withPath } = addMotionPathToScript(script, "#el", 2.0, 1.5, {
+ x: 300,
+ y: -100,
+ });
+ const synced = syncPositionHoldsBeforeKeyframes(withPath);
+ const hold = parseGsapScript(synced).animations.find((a) => a.method === "set");
+ expect(hold).toBeDefined();
+ expect(hold!.position).toBe(0);
+ expect(hold!.properties).toMatchObject({ x: 0, y: 0 });
+ });
+
+ it("addMotionPathToScript + hold-sync — adds no hold when authored at t=0", () => {
+ const script = `const tl = gsap.timeline({ paused: true });`;
+ const { script: withPath } = addMotionPathToScript(script, "#el", 0, 1.5, {
+ x: 300,
+ y: -100,
+ });
+ expect(syncPositionHoldsBeforeKeyframes(withPath)).not.toContain("hf-hold");
+ });
+
// ── convertToKeyframesInScript ──────────────────────────────────────────
it("convertToKeyframesInScript — converts flat to() tween", () => {
@@ -1984,6 +2330,19 @@ describe("splitAnimationsInScript", () => {
expect(forNew[0]!.position).toBe(opts.splitTime);
});
+ it("does not pin the clone to from-values for a completed .from() before the split", () => {
+ // A .from() that finished before the split leaves the element at its natural
+ // state. Carrying its from-values (opacity:0) into the clone's `set` made the
+ // clone invisible. The clone should get NO inherited set for those props.
+ const script = `${baseScript}\ntl.from("#el1", { y: 70, opacity: 0, duration: 0.9 }, 0.4);`;
+ const result = split(script);
+ const parsed = parseGsapScript(result);
+ const forNew = parsed.animations.filter((a) => a.targetSelector === "#el1-split");
+ const inheritedSet = forNew.find((a) => a.method === "set");
+ expect(inheritedSet).toBeUndefined();
+ expect(result).not.toContain("#el1-split");
+ });
+
it("retargets animation entirely in second half to new element", () => {
const script = `${baseScript}\ntl.to("#el1", { x: 100, duration: 1 }, 3);`;
const selectors = parseSplitAndAssert(script, (s) => split(s), 1);
@@ -2053,6 +2412,42 @@ tl.to("#el1", { y: 200, duration: 1 }, 3);`;
expect(continuation!.properties.opacity).toBe(1);
});
+ it("splits a mid-flight fromTo straddling the split into two fromTo halves", () => {
+ // Mid-flight: pos(0) < splitTime(2) < animEnd(4). The first half keeps the
+ // original on #el1 ending at the interpolated mid-value; the clone continues
+ // as a fromTo from that mid-value to the original to-value.
+ const script = `${baseScript}\ntl.fromTo("#el1", { x: 0 }, { x: 100, duration: 4 }, 0);`;
+ const result = split(script);
+ const parsed = parseGsapScript(result);
+ const first = parsed.animations.find((a) => a.targetSelector === "#el1")!;
+ const forNew = parsed.animations.filter((a) => a.targetSelector === "#el1-split");
+ const continuation = forNew.find((a) => a.method === "fromTo")!;
+ expect(first.duration).toBe(2);
+ expect(first.properties.x).toBe(50);
+ expect(continuation.duration).toBe(2);
+ expect(continuation.fromProperties?.x).toBe(50);
+ expect(continuation.properties.x).toBe(100);
+ });
+
+ it("splits a mid-flight from straddling the split (no fromProperties on source)", () => {
+ // A .from() has no explicit fromProperties, so the spanning branch seeds the
+ // from-value from accumulated inherited state (defaulting to 0). The clone
+ // continues from the interpolated mid-value as a fromTo so both halves play
+ // a contiguous range.
+ const script = `${baseScript}\ntl.from("#el1", { x: 80, duration: 4 }, 0);`;
+ const result = split(script);
+ const parsed = parseGsapScript(result);
+ const first = parsed.animations.find((a) => a.targetSelector === "#el1")!;
+ const continuation = parsed.animations
+ .filter((a) => a.targetSelector === "#el1-split")
+ .find((a) => a.method === "fromTo")!;
+ expect(first.duration).toBe(2);
+ expect(first.properties.x).toBe(40);
+ expect(continuation.duration).toBe(2);
+ expect(continuation.fromProperties?.x).toBe(40);
+ expect(continuation.properties.x).toBe(80);
+ });
+
it("round-trips correctly through parseGsapScript", () => {
const script = `${baseScript}\ntl.to("#el1", { x: 100, duration: 4 }, 0);`;
const result = split(script);
diff --git a/packages/core/src/parsers/gsapParser.ts b/packages/core/src/parsers/gsapParser.ts
index 0f383bc673..a2f1047128 100644
--- a/packages/core/src/parsers/gsapParser.ts
+++ b/packages/core/src/parsers/gsapParser.ts
@@ -1563,6 +1563,67 @@ function insertInheritedStateSet(
return recast.print(parsed.ast).code;
}
+/** Marker on Studio-emitted pre-keyframe hold `set`s. `data` is a GSAP-reserved
+ * config key (attached to the tween, never applied to the target), so it carries
+ * the tag without triggering GSAP's "Invalid property" warning. */
+const STUDIO_HOLD_MARKER = "hf-hold";
+
+/** True for a `tl.set(...)` this module emitted to hold a keyframe before its tween.
+ * The Studio filters these out so they never appear as user keyframes/diamonds. */
+export function isStudioHoldSet(anim: GsapAnimation): boolean {
+ return anim.method === "set" && anim.properties?.data === STUDIO_HOLD_MARKER;
+}
+
+/**
+ * Keep a `tl.set(selector, {x,y}, 0)` "hold" in front of every position-keyframed
+ * tween that starts after t=0, so the element holds its first keyframe's position
+ * BEFORE the tween plays instead of snapping to its CSS base (the universal NLE
+ * "hold before first keyframe" behavior). The set is tagged with `data: "hf-hold"`
+ * so this pass owns it: every call wipes the prior holds and recomputes from the
+ * current keyframes, keeping them in sync as keyframes are added/moved/deleted.
+ *
+ * Idempotent. Only position props (x/y/xPercent/yPercent) are held — opacity/scale
+ * keep their authored pre-tween behavior. A tween already starting at 0 needs no
+ * hold (no gap before it).
+ */
+export function syncPositionHoldsBeforeKeyframes(script: string): string {
+ let parsed: ParsedGsap;
+ try {
+ parsed = parseGsapScript(script);
+ } catch {
+ return script;
+ }
+ // 1. Drop every hold this pass previously emitted, so we recompute fresh.
+ let result = script;
+ const staleHoldIds = parsed.animations.filter(isStudioHoldSet).map((a) => a.id);
+ for (const id of staleHoldIds) result = removeAnimationFromScript(result, id);
+
+ // 2. Re-add a hold for each position-keyframed tween that starts after t=0.
+ let reparsed: ParsedGsap;
+ try {
+ reparsed = parseGsapScript(result);
+ } catch {
+ return result;
+ }
+ for (const anim of reparsed.animations) {
+ if (!anim.keyframes) continue;
+ const start = anim.resolvedStart ?? (typeof anim.position === "number" ? anim.position : 0);
+ if (!(start > 0.001)) continue;
+ const firstKf = [...anim.keyframes.keyframes].sort((a, b) => a.percentage - b.percentage)[0];
+ if (!firstKf) continue;
+ const posProps: Record<string, number | string> = {};
+ for (const [k, v] of Object.entries(firstKf.properties)) {
+ if (classifyPropertyGroup(k) === "position" && typeof v === "number") posProps[k] = v;
+ }
+ if (Object.keys(posProps).length === 0) continue;
+ result = insertInheritedStateSet(result, anim.targetSelector, 0, {
+ ...posProps,
+ data: STUDIO_HOLD_MARKER,
+ });
+ }
+ return result;
+}
+
// ── Split Animation Functions ─────────────────────────────────────────────
export interface SplitAnimationsOptions {
@@ -1639,9 +1700,25 @@ export function splitAnimationsInScript(
continue;
}
+ // `<=` (not `<`) is deliberate: a tween whose end coincides exactly with
+ // the split boundary has fully played by splitTime, so it belongs to the
+ // first half and contributes its resting state to the clone. The spanning
+ // branch below handles only strictly-mid-flight tweens (pos < split < end).
if (animEnd <= opts.splitTime) {
- for (const [k, v] of Object.entries(anim.properties)) {
- inheritedProps[k] = v;
+ // Only a completed .from() reverts the element to its natural state, so
+ // its recorded properties are the HIDDEN start (e.g. opacity:0), not the
+ // resting state — clearing them keeps the clone at its natural value
+ // instead of pinning it to the from-values (which made it invisible).
+ // .fromTo() and .to() both END at their to-values (no revert), so they
+ // fall through to `else` and inherit `anim.properties` (the to-values) —
+ // .fromTo() must NOT join the .from() clear-branch or the clone would
+ // drop the very state the fromTo just established.
+ if (anim.method === "from") {
+ for (const k of Object.keys(anim.properties)) delete inheritedProps[k];
+ } else {
+ for (const [k, v] of Object.entries(anim.properties)) {
+ inheritedProps[k] = v;
+ }
}
continue;
}
@@ -1787,6 +1864,14 @@ function locateAnimation(
return target ? { parsed, target } : null;
}
+// Animation ids encode the tween's timeline position in ms
+// (`#puck-a-to-1200-position`). A gesture/convert can re-emit a tween at a
+// different position, changing its id — so a client that cached the old id (its
+// selectedGsapAnimations hasn't refreshed) edits a now-nonexistent id and the op
+// no-ops. Parse `{selector}-{method}-{posMs}-{group}` so we can fall back to the
+// same selector+method+group tween nearest the requested position.
+const ANIM_ID_RE = /^(.*)-(fromTo|from|to|set)-(\d+)-([a-z]+)$/;
+
function locateAnimationWithFallback(
script: string,
animationId: string,
@@ -1794,8 +1879,34 @@ function locateAnimationWithFallback(
const loc = locateAnimation(script, animationId);
if (loc) return loc;
const convertedId = animationId.replace(/-from-|-fromTo-/, "-to-");
- if (convertedId === animationId) return null;
- return locateAnimation(script, convertedId);
+ if (convertedId !== animationId) {
+ const converted = locateAnimation(script, convertedId);
+ if (converted) return converted;
+ }
+ // Position-drift fallback: match by stable identity (selector+method+group),
+ // disambiguating by the position closest to the one the caller asked for.
+ const want = ANIM_ID_RE.exec(animationId);
+ if (!want) return null;
+ const [, sel, method, wantPosStr, group] = want;
+ const wantPos = Number(wantPosStr);
+ let parsed: ParsedGsapAst;
+ try {
+ parsed = parseGsapAst(script);
+ } catch {
+ return null;
+ }
+ let best: ParsedGsapAst["located"][number] | null = null;
+ let bestDist = Number.POSITIVE_INFINITY;
+ for (const l of parsed.located) {
+ const m = ANIM_ID_RE.exec(l.id);
+ if (!m || m[1] !== sel || m[2] !== method || m[4] !== group) continue;
+ const dist = Math.abs(Number(m[3]) - wantPos);
+ if (dist < bestDist) {
+ best = l;
+ bestDist = dist;
+ }
+ }
+ return best ? { parsed, target: best } : null;
}
/** Find the keyframes ObjectExpression node on a tween's varsArg, or null. */
@@ -1804,6 +1915,33 @@ function findKeyframesObjectNode(varsArg: AstNode): AstNode | null {
return node?.type === "ObjectExpression" ? node : null;
}
+/**
+ * Convert array-form keyframes (`keyframes: [{x,y}, …]`) to even-percentage object
+ * form (`{ "0%": {…}, "33.3%": {…}, … }`) IN PLACE, returning the new object node
+ * (or null if not array-form). GSAP distributes an array evenly, so this is
+ * runtime-identical — but it gives the percentage-keyed write ops something to
+ * target. Needed before INSERTING a keyframe at an arbitrary percentage, which an
+ * even array can't host.
+ */
+function convertArrayKeyframesToObjectNode(varsArg: AstNode): AstNode | null {
+ if (varsArg?.type !== "ObjectExpression") return null;
+ const prop = (varsArg.properties ?? []).find(
+ (p: AstNode) => isObjectProperty(p) && propKeyName(p) === "keyframes",
+ );
+ if (!prop || prop.value?.type !== "ArrayExpression") return null;
+ const els: AstNode[] = (prop.value.elements ?? []).filter(
+ (e: AstNode | null): e is AstNode => !!e && e.type === "ObjectExpression",
+ );
+ const n = els.length;
+ if (n === 0) return null;
+ const entries = els.map((el: AstNode, i: number) => {
+ const pct = n > 1 ? Math.round((i / (n - 1)) * 1000) / 10 : 0;
+ return `${JSON.stringify(`${pct}%`)}: ${recast.print(el).code}`;
+ });
+ prop.value = parseExpr(`{ ${entries.join(", ")} }`);
+ return prop.value;
+}
+
/** Filter percentage-keyed properties from a keyframes ObjectExpression. */
function filterPercentageProps(kfNode: AstNode): AstNode[] {
return kfNode.properties.filter((p: AstNode) => {
@@ -1856,6 +1994,11 @@ export function addKeyframeToScript(
if (!loc) return script;
let kfNode = findKeyframesObjectNode(loc.target.call.varsArg);
+ // Array-form keyframes can't host an arbitrary new percentage — normalize to
+ // object form in place first. (convertToKeyframesInScript below only converts
+ // FLAT tweens; it early-returns when keyframes already exist.)
+ if (!kfNode) kfNode = convertArrayKeyframesToObjectNode(loc.target.call.varsArg);
+
if (!kfNode) {
script = convertToKeyframesInScript(script, animationId);
loc = locateAnimationWithFallback(script, animationId);
@@ -1967,6 +2110,43 @@ export function removeKeyframeFromScript(
animationId: string,
percentage: number,
): string {
+ // Array-form keyframes (`keyframes: [{x,y}, …]`) have no explicit percentages —
+ // GSAP distributes them evenly. The object-form path below can't see them
+ // (findKeyframesObjectNode only matches ObjectExpression), so removing from an
+ // array-form tween silently no-op'd. Resolve the element by its implicit
+ // percentage and splice it; collapse to a flat tween when fewer than two remain.
+ const arrLoc = locateAnimationWithFallback(script, animationId);
+ // findPropertyNode here returns the property's VALUE node directly.
+ const arrVal = arrLoc && findPropertyNode(arrLoc.target.call.varsArg, "keyframes");
+ if (arrLoc && arrVal?.type === "ArrayExpression") {
+ const elements: AstNode[] = (arrVal.elements ?? []).filter(
+ (e: AstNode | null): e is AstNode => !!e && e.type === "ObjectExpression",
+ );
+ const n = elements.length;
+ if (n === 0) return script;
+ let matchIdx = -1;
+ let bestDist = Number.POSITIVE_INFINITY;
+ for (let i = 0; i < n; i++) {
+ const pct = n > 1 ? (i / (n - 1)) * 100 : 0;
+ const dist = Math.abs(pct - percentage);
+ if (dist <= PCT_TOLERANCE && dist < bestDist) {
+ matchIdx = i;
+ bestDist = dist;
+ }
+ }
+ if (matchIdx === -1) return script;
+ const remaining = elements.filter((_, i) => i !== matchIdx);
+ if (remaining.length < 2) {
+ const sole = remaining[0];
+ const record = sole ? objectExpressionToRecord(sole, arrLoc.parsed.scope) : {};
+ collapseKeyframesToFlat(arrLoc.target.call.varsArg, record);
+ } else {
+ const realIdx = arrVal.elements.indexOf(elements[matchIdx]);
+ arrVal.elements.splice(realIdx, 1);
+ }
+ return recast.print(arrLoc.parsed.ast).code;
+ }
+
const ctx = locateKeyframeCtx(script, animationId, percentage);
if (!ctx) return script;
const { loc, kfNode } = ctx;
@@ -1999,6 +2179,36 @@ export function updateKeyframeInScript(
properties: Record<string, number | string>,
ease?: string,
): string {
+ // Array-form keyframes (`keyframes: [{x,y}, …]`) have no explicit percentages —
+ // GSAP distributes them evenly. The percentage-keyed object path below can't
+ // match them (findKeyframesObjectNode only matches ObjectExpression), so dragging
+ // a motion-path node on an array-authored tween silently no-op'd. Resolve the
+ // element by its implicit percentage and replace it in place. Mirrors the array
+ // branch in removeKeyframeFromScript.
+ const arrLoc = locateAnimationWithFallback(script, animationId);
+ const arrVal = arrLoc && findPropertyNode(arrLoc.target.call.varsArg, "keyframes");
+ if (arrLoc && arrVal?.type === "ArrayExpression") {
+ const elements: AstNode[] = (arrVal.elements ?? []).filter(
+ (e: AstNode | null): e is AstNode => !!e && e.type === "ObjectExpression",
+ );
+ const n = elements.length;
+ if (n === 0) return script;
+ let matchIdx = -1;
+ let bestDist = Number.POSITIVE_INFINITY;
+ for (let i = 0; i < n; i++) {
+ const pct = n > 1 ? (i / (n - 1)) * 100 : 0;
+ const dist = Math.abs(pct - percentage);
+ if (dist <= PCT_TOLERANCE && dist < bestDist) {
+ matchIdx = i;
+ bestDist = dist;
+ }
+ }
+ if (matchIdx === -1) return script;
+ const realIdx = arrVal.elements.indexOf(elements[matchIdx]);
+ arrVal.elements[realIdx] = buildKeyframeValueNode(properties, ease);
+ return recast.print(arrLoc.parsed.ast).code;
+ }
+
const ctx = locateKeyframeCtx(script, animationId, percentage);
if (!ctx) return script;
const { loc, kfNode } = ctx;
@@ -2346,6 +2556,176 @@ export function updateArcSegmentInScript(
return recast.print(loc.parsed.ast).code;
}
+/**
+ * Move a single motionPath waypoint (anchor) to a new position. The waypoint
+ * list is normalized to anchors for both straight and cubic paths, so
+ * `pointIndex` matches the node order the studio overlay renders; cubic control
+ * points are preserved. No-op when the animation/arc is missing or the index is
+ * out of range.
+ */
+export function updateMotionPathPointInScript(
+ script: string,
+ animationId: string,
+ pointIndex: number,
+ point: { x: number; y: number },
+): string {
+ const loc = locateAnimation(script, animationId);
+ if (!loc) return script;
+
+ const anim = loc.target.animation;
+ if (!anim.arcPath?.enabled) return script;
+
+ const waypoints = extractArcWaypoints(anim);
+ if (pointIndex < 0 || pointIndex >= waypoints.length || waypoints.length < 2) return script;
+
+ const nextWaypoints = waypoints.map((wp, i) =>
+ i === pointIndex ? { x: point.x, y: point.y } : wp,
+ );
+
+ const motionPathCode = buildMotionPathObjectCode({
+ waypoints: nextWaypoints,
+ segments: anim.arcPath.segments,
+ autoRotate: anim.arcPath.autoRotate,
+ });
+
+ const varsArg = loc.target.call.varsArg;
+ const existingProp = varsArg.properties.find(
+ (p: AstNode) => isObjectProperty(p) && propKeyName(p) === "motionPath",
+ );
+ if (existingProp) {
+ existingProp.value = parseExpr(motionPathCode);
+ }
+
+ return recast.print(loc.parsed.ast).code;
+}
+
+/** True when any segment carries explicit cubic control points. Add/remove are
+ * restricted to curviness (non-cubic) paths — synthesizing control points for
+ * an inserted cubic anchor is out of scope. */
+function hasCubicSegments(segments: ArcPathSegment[]): boolean {
+ return segments.some((s) => s.cp1 != null || s.cp2 != null);
+}
+
+function writeMotionPathValue(
+ loc: NonNullable<ReturnType<typeof locateAnimation>>,
+ waypoints: Array<{ x: number; y: number }>,
+ segments: ArcPathSegment[],
+ autoRotate: boolean | number,
+): string {
+ const motionPathCode = buildMotionPathObjectCode({ waypoints, segments, autoRotate });
+ const varsArg = loc.target.call.varsArg;
+ const existingProp = varsArg.properties.find(
+ (p: AstNode) => isObjectProperty(p) && propKeyName(p) === "motionPath",
+ );
+ if (existingProp) existingProp.value = parseExpr(motionPathCode);
+ return recast.print(loc.parsed.ast).code;
+}
+
+/**
+ * Insert a waypoint at `index` (between existing anchors), splitting the segment
+ * it lands on so the new neighbor inherits its curviness. Non-cubic paths only.
+ * No-op for missing animation/arc, out-of-range index, or cubic paths.
+ */
+export function addMotionPathPointInScript(
+ script: string,
+ animationId: string,
+ index: number,
+ point: { x: number; y: number },
+): string {
+ const loc = locateAnimation(script, animationId);
+ if (!loc) return script;
+ const anim = loc.target.animation;
+ if (!anim.arcPath?.enabled || hasCubicSegments(anim.arcPath.segments)) return script;
+
+ const waypoints = extractArcWaypoints(anim);
+ // Insert strictly between two anchors: index 1..length-1.
+ if (index < 1 || index > waypoints.length - 1) return script;
+
+ const segments = [...anim.arcPath.segments];
+ waypoints.splice(index, 0, { x: point.x, y: point.y });
+ const splitCurviness = segments[index - 1]?.curviness ?? 1;
+ segments.splice(index - 1, 0, { curviness: splitCurviness });
+
+ return writeMotionPathValue(loc, waypoints, segments, anim.arcPath.autoRotate);
+}
+
+/**
+ * Remove the waypoint at `index`. Refuses to drop below two anchors (a path
+ * can't have fewer). Non-cubic paths only. No-op for missing animation/arc,
+ * out-of-range index, cubic paths, or a 2-point path.
+ */
+export function removeMotionPathPointInScript(
+ script: string,
+ animationId: string,
+ index: number,
+): string {
+ const loc = locateAnimation(script, animationId);
+ if (!loc) return script;
+ const anim = loc.target.animation;
+ if (!anim.arcPath?.enabled || hasCubicSegments(anim.arcPath.segments)) return script;
+
+ const waypoints = extractArcWaypoints(anim);
+ if (waypoints.length <= 2 || index < 0 || index >= waypoints.length) return script;
+
+ const segments = [...anim.arcPath.segments];
+ waypoints.splice(index, 1);
+ // Drop the segment on the side that still exists (last anchor → preceding segment).
+ segments.splice(Math.min(index, segments.length - 1), 1);
+
+ return writeMotionPathValue(loc, waypoints, segments, anim.arcPath.autoRotate);
+}
+
+/**
+ * Author a fresh 2-anchor motionPath tween on a target element: a straight line
+ * from the element's home (0,0) to `point`, gentle ease, ready for waypoint
+ * editing. Mirrors `addAnimationWithKeyframesToScript`.
+ */
+export function addMotionPathToScript(
+ script: string,
+ targetSelector: string,
+ position: number,
+ duration: number,
+ point: { x: number; y: number },
+ ease = "power1.inOut",
+): { script: string; id: string | null } {
+ // `id: null` on the failure paths is a deliberate sentinel: callers must
+ // null-check before chaining (e.g. locating the new tween). An empty string
+ // would silently flow into selector/locate calls and match nothing.
+ let parsed: ParsedGsapAst;
+ try {
+ parsed = parseGsapAst(script);
+ } catch (e) {
+ console.warn("[gsap-parser] addMotionPathToScript parse failed:", e);
+ return { script, id: null };
+ }
+ if (parsed.located.length === 0 && parsed.detection.timelineVar === null) {
+ return { script, id: null };
+ }
+
+ const motionPathCode = buildMotionPathObjectCode({
+ waypoints: [
+ { x: 0, y: 0 },
+ { x: point.x, y: point.y },
+ ],
+ segments: [{ curviness: 1 }],
+ autoRotate: false,
+ });
+ const selector = JSON.stringify(targetSelector);
+ const varEntries = [
+ `motionPath: ${motionPathCode}`,
+ `duration: ${valueToCode(duration)}`,
+ `ease: ${JSON.stringify(ease)}`,
+ ];
+ const stmtCode = `${parsed.timelineVar}.to(${selector}, { ${varEntries.join(", ")} }, ${valueToCode(position)});`;
+ const newStatement = parseScript(stmtCode).program.body[0];
+ insertAfterAnchor(parsed, newStatement);
+
+ const result = recast.print(parsed.ast).code;
+ const reParsed = parseGsapAst(result);
+ const newId = reParsed.located[reParsed.located.length - 1]?.id ?? null;
+ return { script: result, id: newId };
+}
+
export function removeArcPathFromScript(script: string, animationId: string): string {
return setArcPathInScript(script, animationId, {
enabled: false,
diff --git a/packages/core/src/parsers/gsapParserExports.ts b/packages/core/src/parsers/gsapParserExports.ts
index eb9c3aec0e..5917d13ded 100644
--- a/packages/core/src/parsers/gsapParserExports.ts
+++ b/packages/core/src/parsers/gsapParserExports.ts
@@ -31,6 +31,10 @@ export {
SUPPORTED_PROPS,
SUPPORTED_EASES,
} from "./gsapSerialize.js";
+// Studio position-hold predicate (`tl.set(...,{data:"hf-hold"})`). A pure
+// GsapAnimation helper — re-exported here so studio can filter holds via the
+// public entry even though gsapParser.ts is otherwise an internal module.
+export { isStudioHoldSet } from "./gsapParser.js";
export type { PropertyGroupName } from "./gsapConstants.js";
export {
PROPERTY_GROUPS,
diff --git a/packages/core/src/parsers/gsapWriter.acorn.test.ts b/packages/core/src/parsers/gsapWriter.acorn.test.ts
index 839b1079b1..0c0b16fa65 100644
--- a/packages/core/src/parsers/gsapWriter.acorn.test.ts
+++ b/packages/core/src/parsers/gsapWriter.acorn.test.ts
@@ -250,6 +250,41 @@ describe("T6c — keyframe write ops", () => {
expect(result).toContain("}, 0.2)");
});
+ it("updateKeyframeInScript edits ARRAY-form keyframes by percentage→index (the #shuttle case)", () => {
+ // Array-form keyframes carry no explicit percentages; GSAP distributes 4 of
+ // them evenly → 0 / 33.3 / 66.7 / 100. Dragging the 2nd motion-path node
+ // (pct 33.3) must rewrite array index 1 — not no-op (regression: array form
+ // bailed the ObjectExpression check, so the drag committed nothing).
+ const script =
+ "const tl = gsap.timeline();\n" +
+ 'tl.to("#shuttle", { keyframes: [{ x: 0, y: 0 }, { x: 520, y: 120 }, { x: 1040, y: 0 }, { x: 1480, y: 160 }], duration: 4.4, ease: "none" }, 5.2);';
+ const result = updateKeyframeInScript(script, "#shuttle-to-5200-position", 33.3, {
+ x: 503,
+ y: 642,
+ });
+ expect(result).not.toBe(script); // actually changed (not a no-op)
+ expect(result).toContain("x: 503");
+ expect(result).toContain("y: 642");
+ expect(result).not.toContain("x: 520"); // index 1 replaced
+ // Sibling array entries untouched.
+ expect(result).toContain("{ x: 0, y: 0 }");
+ expect(result).toContain("{ x: 1040, y: 0 }");
+ expect(result).toContain("{ x: 1480, y: 160 }");
+ });
+
+ it("addKeyframeToScript — ARRAY-form normalizes to object form + inserts 50%", () => {
+ const script =
+ "const tl = gsap.timeline();\n" +
+ 'tl.to("#shuttle", { keyframes: [{ x: 0, y: 0 }, { x: 520, y: 120 }, { x: 1040, y: 0 }, { x: 1480, y: 160 }], duration: 4.4, ease: "none" }, 5.2);';
+ const result = addKeyframeToScript(script, "#shuttle-to-5200-position", 50, { x: 780, y: 60 });
+ expect(result).not.toBe(script); // not a no-op
+ expect(result).toContain('"50%"'); // converted to percentage-object form
+ expect(result).toContain("x: 780");
+ // Original even-distribution stops preserved as percentage keys.
+ expect(result).toContain('"0%"');
+ expect(result).toContain('"100%"');
+ });
+
it("addKeyframeToScript inserts new percentage in sorted order", () => {
const result = addKeyframeToScript(SCRIPT_D, "#box-to-200-visual", 25, { opacity: 0.3 });
expect(result).toContain('"25%"');
diff --git a/packages/core/src/parsers/gsapWriter.parity.test.ts b/packages/core/src/parsers/gsapWriter.parity.test.ts
index 6977ced7d2..5b07b8fbbc 100644
--- a/packages/core/src/parsers/gsapWriter.parity.test.ts
+++ b/packages/core/src/parsers/gsapWriter.parity.test.ts
@@ -23,6 +23,7 @@ import {
removeArcPathFromScript as removeArcRecast,
unrollDynamicAnimations as unrollRecast,
addKeyframeToScript as addKeyframeRecast,
+ updateKeyframeInScript as updateKeyframeRecast,
removeKeyframeFromScript as removeKeyframeRecast,
addAnimationWithKeyframesToScript as addWithKfRecast,
shiftPositionsInScript as shiftRecast,
@@ -45,6 +46,7 @@ import {
removeArcPathFromScript as removeArcAcorn,
unrollDynamicAnimations as unrollAcorn,
addKeyframeToScript as addKeyframeAcorn,
+ updateKeyframeInScript as updateKeyframeAcorn,
removeKeyframeFromScript as removeKeyframeAcorn,
addAnimationWithKeyframesToScript as addWithKfAcorn,
removeAnimationFromScript as removeAnimAcorn,
@@ -162,6 +164,57 @@ describe("parity: removeAllKeyframesFromScript (recast vs acorn)", () => {
});
});
+// Array-form keyframes (`keyframes: [{x,y}, …]`, no explicit %) used to no-op on
+// removal in BOTH writers — the object-form path couldn't see the array, so the
+// keyframe survived while downstream hold-sync stranded an `hf-hold`.
+describe("removeKeyframeFromScript: array-form keyframes (recast + acorn parity)", () => {
+ const arrayScript = `
+ const tl = gsap.timeline({ paused: true });
+ tl.to("#p", {
+ keyframes: [ { x: 0, y: 0 }, { x: -180, y: -60 }, { x: -320, y: 40 }, { x: -460, y: -20 } ],
+ duration: 3.4,
+ ease: "power1.inOut"
+ }, 1.0);
+ `;
+
+ it("removes the matched element (implicit %) — both writers, parity", () => {
+ const id = acornId(arrayScript);
+ expect(parseGsapScript(arrayScript).animations[0]!.id).toBe(id);
+
+ const recastOut = removeKeyframeRecast(arrayScript, id, 67);
+ const acornOut = removeKeyframeAcorn(arrayScript, id, 67);
+
+ expect(recastOut).not.toBe(arrayScript);
+ expect(acornOut).not.toBe(arrayScript);
+
+ const recShape = shapeOf(recastOut);
+ expect(recShape.keyframes?.keyframes.length).toBe(3);
+ // the 67% element { x: -320, y: 40 } is the one removed
+ expect(JSON.stringify(recShape.keyframes)).not.toContain("-320");
+ expect(modelOf(acornOut)).toEqual(modelOf(recastOut));
+ });
+
+ it("collapses to a flat tween when fewer than two remain — both writers, parity", () => {
+ const twoScript = `
+ const tl = gsap.timeline({ paused: true });
+ tl.to("#p", { keyframes: [ { x: 0, y: 0 }, { x: 100, y: 50 } ], duration: 1 }, 0);
+ `;
+ const id = acornId(twoScript);
+ const recastOut = removeKeyframeRecast(twoScript, id, 100);
+ const acornOut = removeKeyframeAcorn(twoScript, id, 100);
+
+ expect(shapeOf(recastOut).keyframes).toBeUndefined();
+ expect(shapeOf(acornOut).keyframes).toBeUndefined();
+ expect(modelOf(acornOut)).toEqual(modelOf(recastOut));
+ });
+
+ it("no-op when the percentage matches no element", () => {
+ const id = acornId(arrayScript);
+ expect(removeKeyframeAcorn(arrayScript, id, 12)).toBe(arrayScript);
+ expect(removeKeyframeRecast(arrayScript, id, 12)).toBe(arrayScript);
+ });
+});
+
const CONVERT_FIXTURES: Array<{
name: string;
script: string;
@@ -887,6 +940,143 @@ describe("parity: removeKeyframeFromScript (recast vs acorn)", () => {
});
});
+// ── addKeyframeToScript: array-form parity (recast vs acorn) ─────────────────
+// The object-form add parity lives above (KF_ADD_* fixtures). Array-form
+// keyframes (`keyframes: [{x,y}, …]`) carry no explicit percentages — GSAP
+// distributes them evenly. Adding an arbitrary percentage can't live in an
+// array, so BOTH writers normalize the array to percentage-keyed object form
+// first (recast: convertArrayKeyframesToObjectNode; acorn: ensureKeyframesNode
+// → convertArrayKeyframesToObject) and then insert/merge. The normalized result
+// must reparse identically across writers.
+const KF_ADD_ARRAY_SCRIPT = `
+ const tl = gsap.timeline({ paused: true });
+ tl.to("#dot", { keyframes: [{ x: 0, y: 0 }, { x: 50, y: 80 }, { x: 100, y: 0 }], duration: 1 }, 0.2);
+`;
+
+describe("parity: addKeyframeToScript array-form (recast vs acorn)", () => {
+ function expectParity(
+ script: string,
+ percentage: number,
+ properties: Record<string, number | string>,
+ ease?: string,
+ backfillDefaults?: Record<string, number | string>,
+ ) {
+ const id = acornId(script);
+ expect(parseGsapScript(script).animations[0]!.id).toBe(id);
+ const acorn = addKeyframeAcorn(script, id, percentage, properties, ease, backfillDefaults);
+ const recast = addKeyframeRecast(script, id, percentage, properties, ease, backfillDefaults);
+ expect(modelOf(acorn)).toEqual(modelOf(recast));
+ }
+
+ it("normalizes the array then inserts a new percentage in sorted order", () => {
+ expectParity(KF_ADD_ARRAY_SCRIPT, 25, { x: 20, y: 40 });
+ });
+
+ it("merges a new property into the evenly-distributed mid element", () => {
+ expectParity(KF_ADD_ARRAY_SCRIPT, 50, { x: 55 });
+ });
+
+ it("carries an ease and backfills a new property across normalized siblings", () => {
+ expectParity(KF_ADD_ARRAY_SCRIPT, 75, { opacity: 0.5 }, "power1.in", { opacity: 0 });
+ });
+
+ it("no-op on unknown id agrees between writers", () => {
+ expect(addKeyframeAcorn(KF_ADD_ARRAY_SCRIPT, "bad-id", 25, { x: 1 })).toBe(KF_ADD_ARRAY_SCRIPT);
+ expect(addKeyframeRecast(KF_ADD_ARRAY_SCRIPT, "bad-id", 25, { x: 1 })).toBe(
+ KF_ADD_ARRAY_SCRIPT,
+ );
+ });
+});
+
+// ── updateKeyframeInScript parity (recast vs acorn) ──────────────────────────
+// updateKeyframeInScript REPLACES the value at the targeted keyframe with the
+// given properties (it is not a merge — see the object-form below: untouched
+// sibling props at that percentage are dropped). Studio's motion-path drag and
+// the SDK move/edit path both call it with the COMPLETE property set for the
+// keyframe (mutate.ts spreads existingKf.properties), so replace == the caller's
+// intent. Object form keys by percentage; array form (no explicit percentages)
+// maps the percentage to an evenly-distributed index and replaces in place,
+// preserving the array literal.
+const UPD_OBJ_SCRIPT = `
+ const tl = gsap.timeline({ paused: true });
+ tl.to("#box", { keyframes: { "0%": { opacity: 0 }, "50%": { x: 10, opacity: 0.5 }, "100%": { opacity: 1 } }, duration: 0.5 }, 0.2);
+`;
+const UPD_ARRAY_SCRIPT = `
+ const tl = gsap.timeline({ paused: true });
+ tl.to("#dot", { keyframes: [{ x: 0, y: 0 }, { x: 50, y: 80 }, { x: 100, y: 0 }], duration: 1 }, 0.2);
+`;
+
+describe("parity: updateKeyframeInScript (recast vs acorn)", () => {
+ function expectParity(
+ script: string,
+ percentage: number,
+ properties: Record<string, number | string>,
+ ease?: string,
+ ) {
+ const id = acornId(script);
+ expect(parseGsapScript(script).animations[0]!.id).toBe(id);
+ const acorn = updateKeyframeAcorn(script, id, percentage, properties, ease);
+ const recast = updateKeyframeRecast(script, id, percentage, properties, ease);
+ expect(modelOf(acorn)).toEqual(modelOf(recast));
+ }
+
+ it("replaces an object-form keyframe value, dropping untouched siblings", () => {
+ // `50%` was { x: 10, opacity: 0.5 }; both writers replace it with { opacity: 0.7 }.
+ expectParity(UPD_OBJ_SCRIPT, 50, { opacity: 0.7 });
+ });
+
+ it("replaces an object-form keyframe and carries an ease", () => {
+ expectParity(UPD_OBJ_SCRIPT, 100, { opacity: 0.9 }, "none");
+ });
+
+ it("replaces an array-form element at its distributed percentage", () => {
+ expectParity(UPD_ARRAY_SCRIPT, 50, { x: 60, y: 90 });
+ });
+
+ it("replaces an array-form endpoint and carries an ease", () => {
+ expectParity(UPD_ARRAY_SCRIPT, 0, { x: 5, y: 5 }, "power2.out");
+ });
+
+ it("targets a near-coincident percentage (49 → the 50% array element)", () => {
+ expectParity(UPD_ARRAY_SCRIPT, 49, { x: 55, y: 85 });
+ });
+
+ it("no-op when the object-form percentage is absent (both writers)", () => {
+ const id = acornId(UPD_OBJ_SCRIPT);
+ expect(updateKeyframeAcorn(UPD_OBJ_SCRIPT, id, 33, { opacity: 0.4 })).toBe(UPD_OBJ_SCRIPT);
+ expect(updateKeyframeRecast(UPD_OBJ_SCRIPT, id, 33, { opacity: 0.4 })).toBe(UPD_OBJ_SCRIPT);
+ });
+
+ it("no-op on unknown id agrees between writers", () => {
+ expect(updateKeyframeAcorn(UPD_OBJ_SCRIPT, "bad-id", 50, { opacity: 0.4 })).toBe(
+ UPD_OBJ_SCRIPT,
+ );
+ expect(updateKeyframeRecast(UPD_OBJ_SCRIPT, "bad-id", 50, { opacity: 0.4 })).toBe(
+ UPD_OBJ_SCRIPT,
+ );
+ });
+
+ // KNOWN DIVERGENCE (acorn-array bug, follow-up — NOT a test artifact):
+ // For PARTIAL props on ARRAY-form keyframes the two writers disagree. recast's
+ // array branch (gsapParser.updateKeyframeInScript) does a whole-value REPLACE
+ // — `arrVal.elements[i] = buildKeyframeValueNode(properties, ease)` — matching
+ // its own object-form branch and the documented replace contract. acorn's
+ // array branch (updateArrayKeyframeByPct in gsapWriterAcorn) MERGES instead —
+ // `{ ...valueNodeToRecord(el), ...properties }` — so updating `50%` with only
+ // `{ x: 60 }` leaves recast at { x: 60 } but acorn at { x: 60, y: 80 }. acorn's
+ // array path is inconsistent with both recast AND acorn's own object path.
+ // Real callers (Studio drag, SDK mutate.ts) always pass the COMPLETE keyframe
+ // value, so the bug is latent in production — but it's a genuine writer gap to
+ // fix in gsapWriterAcorn, out of scope for this test-only change. Skipped (not
+ // deleted) so the contract is documented and the fix has a ready assertion.
+ it.skip("array-form PARTIAL props: recast replaces, acorn merges (acorn bug)", () => {
+ const id = acornId(UPD_ARRAY_SCRIPT);
+ const acorn = updateKeyframeAcorn(UPD_ARRAY_SCRIPT, id, 50, { x: 60 });
+ const recast = updateKeyframeRecast(UPD_ARRAY_SCRIPT, id, 50, { x: 60 });
+ expect(modelOf(acorn)).toEqual(modelOf(recast));
+ });
+});
+
// ── addAnimationWithKeyframesToScript parity (recast vs acorn) ───────────────
// WS-3.C add path: both writers insert a new keyframed tl.to() call. The
// inserted statement's authored model (selector, keyframes, duration, ease,
diff --git a/packages/core/src/parsers/gsapWriterAcorn.ts b/packages/core/src/parsers/gsapWriterAcorn.ts
index 7158e8dd4f..568bc255ab 100644
--- a/packages/core/src/parsers/gsapWriterAcorn.ts
+++ b/packages/core/src/parsers/gsapWriterAcorn.ts
@@ -744,7 +744,18 @@ export function updateKeyframeInScript(
if (!target) return script;
const kfPropNode = findPropertyNode(target.call.varsArg, "keyframes");
- if (!kfPropNode || kfPropNode.value?.type !== "ObjectExpression") return script;
+ if (!kfPropNode) return script;
+
+ // Array-form keyframes (`keyframes: [{x,y}, ...]`) carry no explicit percentages
+ // — GSAP distributes them evenly, and the runtime read assigns even percentages
+ // (0, 100/(n-1), …). Map the percentage back to an array index and overwrite that
+ // element in place (preserving the array form). Without this the function bailed
+ // on the ObjectExpression check, so dragging a motion-path node on an array-form
+ // tween committed nothing (server no-op).
+ if (kfPropNode.value?.type === "ArrayExpression") {
+ return updateArrayKeyframeByPct(script, kfPropNode.value, percentage, properties, ease);
+ }
+ if (kfPropNode.value?.type !== "ObjectExpression") return script;
const match = findKfPropByPct(kfPropNode.value, percentage);
if (!match) return script;
@@ -756,6 +767,33 @@ export function updateKeyframeInScript(
return ms.toString();
}
+// ponytail: even-spacing index map; if array keyframes ever carry per-element
+// `duration`, switch to matching the closest cumulative position.
+function updateArrayKeyframeByPct(
+ script: string,
+ arrayNode: Node,
+ percentage: number,
+ properties: Record<string, number | string>,
+ ease?: string,
+): string {
+ const elements = ((arrayNode.elements ?? []) as Array<Node | null>).filter(
+ (el): el is Node => !!el && el.type === "ObjectExpression",
+ );
+ const n = elements.length;
+ if (n === 0) return script;
+ const idx = n > 1 ? Math.round((percentage / 100) * (n - 1)) : 0;
+ const el = elements[Math.max(0, Math.min(n - 1, idx))];
+ if (!el) return script;
+ const merged: Record<string, number | string> = {
+ ...valueNodeToRecord(el, script),
+ ...properties,
+ };
+ if (ease) merged.ease = ease;
+ const ms = new MagicString(script);
+ ms.overwrite(el.start, el.end, recordToCode(merged));
+ return ms.toString();
+}
+
/**
* Build the final property record for the keyframe at `percentage`. If a
* keyframe already exists there, MERGE the new props over the existing record
@@ -826,6 +864,26 @@ function locateWithKeyframes(
}
/** Locate a tween's keyframes object, converting a flat tween first if absent. */
+// Array-form keyframes (`keyframes: [{x,y}, …]`) → even-percentage object form
+// (`{ "0%": {…}, "33.3%": {…}, … }`). Inserting a keyframe needs percentage keys,
+// which an even array can't host. Runtime-identical; mirrors the recast path.
+function convertArrayKeyframesToObject(script: string, target: Node): string {
+ const kfPropNode = findPropertyNode(target.call.varsArg, "keyframes");
+ if (!kfPropNode || kfPropNode.value?.type !== "ArrayExpression") return script;
+ const els = ((kfPropNode.value.elements ?? []) as Array<Node | null>).filter(
+ (el): el is Node => !!el && el.type === "ObjectExpression",
+ );
+ const n = els.length;
+ if (n === 0) return script;
+ const entries = els.map((el, i) => {
+ const pct = n > 1 ? Math.round((i / (n - 1)) * 1000) / 10 : 0;
+ return `${JSON.stringify(`${pct}%`)}: ${script.slice(el.start, el.end)}`;
+ });
+ const ms = new MagicString(script);
+ ms.overwrite(kfPropNode.value.start, kfPropNode.value.end, `{ ${entries.join(", ")} }`);
+ return ms.toString();
+}
+
function ensureKeyframesNode(
script: string,
animationId: string,
@@ -833,10 +891,19 @@ function ensureKeyframesNode(
const direct = locateWithKeyframes(script, animationId);
if (direct) return direct;
- // No static keyframes object — convert the flat tween, then re-locate.
const parsed = parseGsapScriptAcornForWrite(script);
const target = parsed?.located.find((l) => l.id === animationId);
if (!target) return null;
+
+ // Array-form keyframes → normalize to object form, then re-locate.
+ const kfProp = findPropertyNode(target.call.varsArg, "keyframes");
+ if (kfProp?.value?.type === "ArrayExpression") {
+ const normalized = convertArrayKeyframesToObject(script, target);
+ if (normalized !== script) return locateWithKeyframes(normalized, animationId);
+ return null;
+ }
+
+ // No static keyframes object — convert the flat tween, then re-locate.
const converted = convertFlatTweenToKeyframes(script, target);
if (converted === script) return null;
return locateWithKeyframes(converted, animationId);
@@ -963,6 +1030,53 @@ function collapseKeyframesToFlat(
ms.overwrite(varsNode.start, varsNode.end, `{ ${entries.join(", ")} }`);
}
+/** Implicit tween-relative percentage of array-form keyframe index `i` of `n`
+ * (GSAP distributes array keyframes evenly: 0%, 1/(n-1), …, 100%). */
+function arrayKeyframePct(i: number, n: number): number {
+ return n > 1 ? (i / (n - 1)) * 100 : 0;
+}
+
+// Array-form keyframes (`keyframes: [{x,y}, …]`) carry no explicit percentages —
+// GSAP distributes them evenly. removeKeyframeFromScript only handled the
+// object-form (`keyframes: { "50%": {…} }`), so removing from an array-form tween
+// was a silent no-op (and the downstream hold-sync then stranded an `hf-hold`).
+// Resolve the element by its implicit percentage and splice it out; collapse to a
+// flat tween when fewer than two remain (parity with the object-form path).
+function removeArrayKeyframe(
+ ms: MagicString,
+ varsArg: Node,
+ arrNode: Node,
+ script: string,
+ percentage: number,
+): boolean {
+ const elements: Node[] = (arrNode.elements ?? []).filter(
+ (e: Node | null): e is Node => !!e && e.type === "ObjectExpression",
+ );
+ const n = elements.length;
+ if (n === 0) return false;
+
+ let matchIdx = -1;
+ let bestDist = Number.POSITIVE_INFINITY;
+ for (let i = 0; i < n; i++) {
+ const dist = Math.abs(arrayKeyframePct(i, n) - percentage);
+ if (dist <= PCT_TOLERANCE && dist < bestDist) {
+ matchIdx = i;
+ bestDist = dist;
+ }
+ }
+ if (matchIdx === -1) return false;
+
+ const remaining = elements.filter((_, i) => i !== matchIdx);
+ if (remaining.length < 2) {
+ const sole = remaining[0];
+ const record = sole ? valueNodeToRecord(sole, script) : {};
+ collapseKeyframesToFlat(ms, varsArg, script, record);
+ return true;
+ }
+ removeProp(ms, elements[matchIdx], elements);
+ return true;
+}
+
export function removeKeyframeFromScript(
script: string,
animationId: string,
@@ -974,7 +1088,16 @@ export function removeKeyframeFromScript(
if (!target) return script;
const kfPropNode = findPropertyNode(target.call.varsArg, "keyframes");
- if (!kfPropNode || kfPropNode.value?.type !== "ObjectExpression") return script;
+ if (!kfPropNode) return script;
+
+ if (kfPropNode.value?.type === "ArrayExpression") {
+ const ms = new MagicString(script);
+ return removeArrayKeyframe(ms, target.call.varsArg, kfPropNode.value, script, percentage)
+ ? ms.toString()
+ : script;
+ }
+
+ if (kfPropNode.value?.type !== "ObjectExpression") return script;
const kfNode = kfPropNode.value;
const match = findKfPropByPct(kfNode, percentage);
diff --git a/packages/core/src/runtime/clipTree.test.ts b/packages/core/src/runtime/clipTree.test.ts
new file mode 100644
index 0000000000..4a8431a698
--- /dev/null
+++ b/packages/core/src/runtime/clipTree.test.ts
@@ -0,0 +1,50 @@
+import { describe, it, expect, afterEach } from "vitest";
+import { createClipTree, stableClipId } from "./clipTree";
+
+describe("stableClipId", () => {
+ it("prefers id, falls back to data-hf-id, else null", () => {
+ const withId = document.createElement("div");
+ withId.id = "real";
+ withId.setAttribute("data-hf-id", "hf-1");
+ expect(stableClipId(withId)).toBe("real");
+
+ const hfOnly = document.createElement("div");
+ hfOnly.setAttribute("data-hf-id", "hf-2");
+ expect(stableClipId(hfOnly)).toBe("hf-2");
+
+ expect(stableClipId(document.createElement("div"))).toBeNull();
+ });
+});
+
+describe("createClipTree", () => {
+ afterEach(() => {
+ document.body.innerHTML = "";
+ });
+
+ const params = {
+ startResolver: { resolveStartForElement: () => 0 },
+ timelineRegistry: {},
+ rootDuration: 10,
+ };
+
+ // Regression: id-less children (root index.html uses data-hf-id, not id) must
+ // get their data-hf-id as the node id — not a synthetic `__clip-N` — so the
+ // tree aligns with __clipManifest (which also keys on data-hf-id) and inline
+ // expansion can join parent↔child.
+ it("ids id-less timed elements by data-hf-id and links them to their parent", () => {
+ document.body.innerHTML = `
+ <div data-composition-id="root" data-duration="10" data-start="0" id="root">
+ <div data-start="0" data-duration="5" id="scene">
+ <h1 data-start="0" data-hf-id="hf-headline" class="headline">Hi</h1>
+ </div>
+ </div>`;
+
+ const tree = createClipTree(params);
+ const scene = tree.roots.find((n) => n.id === "scene");
+ expect(scene).toBeDefined();
+ const child = scene!.children.find((n) => n.id === "hf-headline");
+ expect(child).toBeDefined();
+ expect(child!.id).not.toMatch(/^__clip-/);
+ expect(child!.parentId).toBe("scene");
+ });
+});
diff --git a/packages/core/src/runtime/clipTree.ts b/packages/core/src/runtime/clipTree.ts
index 0bf87c66d1..5224617dfc 100644
--- a/packages/core/src/runtime/clipTree.ts
+++ b/packages/core/src/runtime/clipTree.ts
@@ -34,6 +34,18 @@ type MutableClipNode = {
const DECORATIVE_TAGS = new Set(["SCRIPT", "STYLE", "LINK", "META", "TEMPLATE", "NOSCRIPT"]);
+/**
+ * Stable identity for a timed element, shared by __clipTree and __clipManifest
+ * so the two id spaces align. Prefers the author `id`, then the generator's
+ * `data-hf-id` (present on every generated element). Without the data-hf-id
+ * fallback an id-less child (root index.html children use data-hf-id, not id)
+ * gets a synthetic `__clip-N` in the tree but `null` in the manifest, so inline
+ * timeline expansion can't join them and never expands.
+ */
+export function stableClipId(el: Element): string | null {
+ return (el as HTMLElement).id || el.getAttribute("data-hf-id") || null;
+}
+
interface StartResolverLike {
resolveStartForElement: (element: Element, fallback?: number) => number;
}
@@ -110,7 +122,7 @@ export function createClipTree(params: {
const absoluteStart = startResolver.resolveStartForElement(el, 0);
if (resolveDuration(el, timelineRegistry, rootDuration, absoluteStart) <= 0) continue;
const node: MutableClipNode = {
- id: (el as HTMLElement).id || `__clip-${ordinal++}`,
+ id: stableClipId(el) ?? `__clip-${ordinal++}`,
element: el,
parentId: null,
children: [],
diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts
index ef83d4349a..14c973e735 100644
--- a/packages/core/src/runtime/init.ts
+++ b/packages/core/src/runtime/init.ts
@@ -410,23 +410,6 @@ export function initSandboxRuntimeModular(): void {
return resolveStartForElement(element, fallback);
};
- const findTimedClipAncestor = (
- element: HTMLElement,
- rootComp: HTMLElement | null,
- ): HTMLElement | null => {
- let node = element.parentElement;
- while (node) {
- // rootComp may be null when no composition is mounted; the walk still
- // terminates via `while (node)` — node === null is never true here.
- if (node === rootComp) break;
- if (node.hasAttribute("data-start")) {
- return node;
- }
- node = node.parentElement;
- }
- return null;
- };
-
const isTimedElementVisibleAt = (rawNode: HTMLElement, currentTime: number): boolean => {
const tag = rawNode.tagName.toLowerCase();
if (tag === "script" || tag === "style" || tag === "link" || tag === "meta") {
@@ -1073,6 +1056,21 @@ export function initSandboxRuntimeModular(): void {
const dur = String(rootDuration > 0 ? rootDuration : 1);
const seen = new Set<Element>();
+ // Only an AUTHORED clip (data-start already in the source, captured before
+ // we stamp anything) should suppress stamping its descendants. An animated
+ // scene container we auto-stamp below (e.g. an opacity-crossfaded scene)
+ // must NOT suppress its own animated children — otherwise those children
+ // never become timeline clips and that scene can't inline-expand.
+ const authoredTimed = new Set<Element>(document.querySelectorAll("[data-start]"));
+ const hasAuthoredTimedAncestor = (element: HTMLElement): boolean => {
+ let node = element.parentElement;
+ while (node && node !== rootComp) {
+ if (authoredTimed.has(node)) return true;
+ node = node.parentElement;
+ }
+ return false;
+ };
+
// Stamp GSAP-targeted elements
if (state.capturedTimeline.getChildren) {
try {
@@ -1082,7 +1080,7 @@ export function initSandboxRuntimeModular(): void {
if (!(target instanceof HTMLElement)) continue;
if (target === rootComp) continue;
if (target.hasAttribute("data-start")) continue;
- if (findTimedClipAncestor(target, rootComp)) continue;
+ if (hasAuthoredTimedAncestor(target)) continue;
if (seen.has(target)) continue;
seen.add(target);
target.setAttribute("data-start", "0");
@@ -1102,7 +1100,7 @@ export function initSandboxRuntimeModular(): void {
if (!(el instanceof HTMLElement)) continue;
if (el === rootComp) continue;
if (el.hasAttribute("data-start")) continue;
- if (findTimedClipAncestor(el, rootComp)) continue;
+ if (hasAuthoredTimedAncestor(el)) continue;
if (seen.has(el)) continue;
if (el.tagName === "SCRIPT" || el.tagName === "STYLE" || el.tagName === "LINK") continue;
seen.add(el);
@@ -1439,6 +1437,46 @@ export function initSandboxRuntimeModular(): void {
};
// fallow-ignore-next-line complexity
+ // Whether a timed clip participates in normal flow (static/relative/sticky).
+ // In-flow clips must leave the flow when hidden — `visibility:hidden` reserves
+ // their layout box, so a split sibling would stack below the active half
+ // instead of overlapping it. Positioned clips keep `visibility:hidden` (cheaper,
+ // and avoids disturbing absolute media playback). Computed once per element.
+ let timedClipInFlow = new WeakMap<Element, boolean>();
+ const isTimedClipInFlow = (el: HTMLElement): boolean => {
+ const cached = timedClipInFlow.get(el);
+ if (cached !== undefined) return cached;
+ const pos = window.getComputedStyle(el).position;
+ const inFlow = pos === "static" || pos === "relative" || pos === "sticky";
+ timedClipInFlow.set(el, inFlow);
+ return inFlow;
+ };
+
+ // `display:none` is only safe on a LEAF timed clip (no nested timed clips). On a
+ // container it removes the whole subtree, hiding descendants that are still inside
+ // their OWN visibility window — e.g. an in-flow composition root whose window
+ // clamps to the timeline end would black out a child video that should still
+ // show. `visibility:hidden` doesn't have this problem (a child can override it
+ // with `visibility:visible`), so containers keep that and only leaves leave-flow.
+ let timedClipIsLeaf = new WeakMap<Element, boolean>();
+ const isTimedClipLeaf = (el: HTMLElement): boolean => {
+ const cached = timedClipIsLeaf.get(el);
+ if (cached !== undefined) return cached;
+ const leaf = el.querySelector("[data-start]") === null;
+ timedClipIsLeaf.set(el, leaf);
+ return leaf;
+ };
+
+ // Both caches key on live DOM facts that change when the timed-element set
+ // changes: leaf status flips when a clip gains/loses a nested `[data-start]`
+ // descendant (sub-composition load/unload, studio insert/delete), and a swapped
+ // element can reuse an identity whose in-flow status differs. WeakMap has no
+ // `clear()`, so drop both maps wholesale — re-derived lazily on next access.
+ const invalidateTimedClipCaches = () => {
+ timedClipInFlow = new WeakMap<Element, boolean>();
+ timedClipIsLeaf = new WeakMap<Element, boolean>();
+ };
+
const syncMediaForCurrentState = () => {
const resolveMediaCompositionContext = (element: HTMLVideoElement | HTMLAudioElement) => {
const compositionRoot = element.closest("[data-composition-id]");
@@ -1549,6 +1587,11 @@ export function initSandboxRuntimeModular(): void {
if (rawNode instanceof HTMLVideoElement || rawNode instanceof HTMLImageElement) {
colorGradingRuntime?.setSourceVisibility(rawNode, isVisibleNow);
}
+ if (isVisibleNow) {
+ if (isTimedClipInFlow(rawNode)) rawNode.style.removeProperty("display");
+ } else if (isTimedClipInFlow(rawNode) && isTimedClipLeaf(rawNode)) {
+ rawNode.style.display = "none";
+ }
}
};
@@ -1610,6 +1653,10 @@ export function initSandboxRuntimeModular(): void {
window.__clipManifest = payload;
const currentSignature = computeClipTreeSignature();
+ if (clipTreeSignature !== currentSignature) {
+ // The timed-element set changed — leaf/in-flow caches may be stale.
+ invalidateTimedClipCaches();
+ }
if (!window.__clipTree || clipTreeSignature !== currentSignature) {
const runtimeWindow = window as Window & {
__timelines?: Record<string, RuntimeTimelineLike | undefined>;
diff --git a/packages/core/src/runtime/timeline.test.ts b/packages/core/src/runtime/timeline.test.ts
index 333e0a08c5..f4946afbe6 100644
--- a/packages/core/src/runtime/timeline.test.ts
+++ b/packages/core/src/runtime/timeline.test.ts
@@ -20,6 +20,25 @@ describe("collectRuntimeTimelinePayload", () => {
expect(result.compositionHeight).toBe(1080);
});
+ // Regression: id-less timed elements (root index.html children carry
+ // data-hf-id, not id) must get their data-hf-id as the clip id — not null —
+ // so the manifest aligns with __clipTree and inline expansion can join them.
+ it("ids an id-less clip by its data-hf-id", () => {
+ const root = document.createElement("div");
+ root.setAttribute("data-composition-id", "main");
+ root.setAttribute("data-duration", "10");
+ document.body.appendChild(root);
+
+ const clip = document.createElement("h1");
+ clip.setAttribute("data-hf-id", "hf-headline");
+ clip.setAttribute("data-start", "1");
+ clip.setAttribute("data-duration", "3");
+ root.appendChild(clip);
+
+ const result = collectRuntimeTimelinePayload(defaultParams);
+ expect(result.clips[0].id).toBe("hf-headline");
+ });
+
it("collects clips from elements with data-start and data-duration", () => {
const root = document.createElement("div");
root.setAttribute("data-composition-id", "main");
diff --git a/packages/core/src/runtime/timeline.ts b/packages/core/src/runtime/timeline.ts
index 6f8e583818..b3b8e3dfa1 100644
--- a/packages/core/src/runtime/timeline.ts
+++ b/packages/core/src/runtime/timeline.ts
@@ -4,6 +4,7 @@ import type {
RuntimeTimelineScene,
RuntimeTimelineLike,
} from "./types";
+import { stableClipId } from "./clipTree";
import { swallow } from "./diagnostics";
import { readElementPlaybackRate } from "./media";
import { createRuntimeStartTimeResolver } from "./startResolver";
@@ -424,7 +425,7 @@ export function collectRuntimeTimelinePayload(params: {
? "image"
: "element";
clips.push({
- id: (node as HTMLElement).id || nodeCompositionId || null,
+ id: stableClipId(node) ?? nodeCompositionId ?? null,
label: buildTimelineClipLabel(node, kind, clips.length),
start,
duration,
diff --git a/packages/core/src/studio-api/helpers/sourceMutation.test.ts b/packages/core/src/studio-api/helpers/sourceMutation.test.ts
index 06bb7155e4..a0f3dedcb1 100644
--- a/packages/core/src/studio-api/helpers/sourceMutation.test.ts
+++ b/packages/core/src/studio-api/helpers/sourceMutation.test.ts
@@ -508,6 +508,28 @@ describe("splitElementInHtml", () => {
expect(splitElementInHtml(source, { id: "box" }, 7.5, "box-split").matched).toBe(false);
});
+ it("splits a GSAP element with no authored timing using fallback timing", () => {
+ // #title has no data-start/data-duration (GSAP-driven); the store supplies the range.
+ const gsapSource = `<html><body><div data-composition-id="root"><h1 id="title" class="title">Hi</h1></div></body></html>`;
+ const result = splitElementInHtml(gsapSource, { id: "title" }, 2, "title-split", {
+ start: 0,
+ duration: 6,
+ });
+ expect(result.matched).toBe(true);
+ // original windowed to [0, 2], clone to [2, 4] (attribute order is serializer-defined)
+ const original = result.html.match(/<h1[^>]*\bid="title"[^>]*>/)![0];
+ expect(original).toContain('data-start="0"');
+ expect(original).toContain('data-duration="2"');
+ const clone = result.html.match(/<h1[^>]*\bid="title-split"[^>]*>/)![0];
+ expect(clone).toContain('data-start="2"');
+ expect(clone).toContain('data-duration="4"');
+ });
+
+ it("still rejects a no-timing element when no fallback timing is given", () => {
+ const gsapSource = `<html><body><div data-composition-id="root"><h1 id="title">Hi</h1></div></body></html>`;
+ expect(splitElementInHtml(gsapSource, { id: "title" }, 2, "title-split").matched).toBe(false);
+ });
+
it("adjusts media playback-start for the second half", () => {
const mediaSource = source.replace(
'id="box" class="clip" data-start="1" data-duration="6"',
diff --git a/packages/core/src/studio-api/helpers/sourceMutation.ts b/packages/core/src/studio-api/helpers/sourceMutation.ts
index ddbf1c1036..727cd326a2 100644
--- a/packages/core/src/studio-api/helpers/sourceMutation.ts
+++ b/packages/core/src/studio-api/helpers/sourceMutation.ts
@@ -292,12 +292,23 @@ export function splitElementInHtml(
target: SourceMutationTarget,
splitTime: number,
newId: string,
+ fallbackTiming?: { start: number; duration: number },
): SplitElementResult {
const { document, wrappedFragment } = parseSourceDocument(source);
const el = findTargetElement(document, target);
if (!el || !isHTMLElement(el)) return { html: source, matched: false, newId: null };
- const { start, duration, usesDataEnd } = resolveElementTiming(el);
+ const timing = resolveElementTiming(el);
+ const { usesDataEnd } = timing;
+ let { start, duration } = timing;
+ // GSAP-animated elements carry their timing in the script, not in data-* attrs,
+ // so the source has no authored duration. Fall back to the store's (GSAP-derived)
+ // range — the runtime windows visibility off data-start/data-duration regardless
+ // of class, so stamping both halves below makes each half show only in its window.
+ if (duration <= 0 && fallbackTiming && fallbackTiming.duration > 0) {
+ start = fallbackTiming.start;
+ duration = fallbackTiming.duration;
+ }
if (duration <= 0 || splitTime <= start || splitTime >= start + duration) {
return { html: source, matched: false, newId: null };
}
@@ -316,6 +327,9 @@ export function splitElementInHtml(
const clone = el.cloneNode(true) as HTMLElement;
clone.setAttribute("id", newId);
clone.removeAttribute("data-hf-id");
+ // Descendants carry their own data-hf-id; leaving them duplicates the id of
+ // every nested node (e.g. an inner <span>), so strip them on the clone too.
+ for (const node of clone.querySelectorAll("[data-hf-id]")) node.removeAttribute("data-hf-id");
clone.setAttribute("data-start", String(Math.round(splitTime * 1000) / 1000));
setElementDuration(clone, splitTime, secondDuration, usesDataEnd);
@@ -344,7 +358,9 @@ export function splitElementInHtml(
duplicateCssRulesForId(document, originalId, newId);
}
- // Trim the original element's duration
+ // Trim the original element's duration. A GSAP element had no data-start; stamp
+ // it so the runtime windows the first half (visibility selects on [data-start]).
+ el.setAttribute("data-start", String(Math.round(start * 1000) / 1000));
setElementDuration(el, start, firstDuration, usesDataEnd);
// Insert clone after original
diff --git a/packages/core/src/studio-api/helpers/subComposition.ts b/packages/core/src/studio-api/helpers/subComposition.ts
index 30014d407b..768fe77153 100644
--- a/packages/core/src/studio-api/helpers/subComposition.ts
+++ b/packages/core/src/studio-api/helpers/subComposition.ts
@@ -6,6 +6,7 @@ import {
rewriteCssAssetUrls,
rewriteInlineStyleAssetUrls,
} from "../../compiler/rewriteSubCompPaths.js";
+import { stripEmbeddedRuntimeScripts } from "../../compiler/htmlDocument.js";
/**
* Detect whether `html` is a full document (has `<html>`, `<head>`, or
@@ -92,6 +93,23 @@ function extractElementAttrs(el: Element): string {
return parts.join(" ");
}
+/**
+ * Add `data-composition-file="<compPath>"` to the comp's root composition
+ * element (the first `[data-composition-id]` that lacks the attribute), so the
+ * studio resolves its top-level elements to the right source file. Idempotent;
+ * a no-op when no composition element is present.
+ */
+function tagRootCompositionFile(bodyHtml: string, compPath: string): string {
+ const match = bodyHtml.match(/<[a-zA-Z][^>]*\bdata-composition-id=/);
+ if (match?.index == null) return bodyHtml;
+ const tagEnd = bodyHtml.indexOf(">", match.index);
+ if (tagEnd === -1) return bodyHtml;
+ if (bodyHtml.slice(match.index, tagEnd).includes("data-composition-file")) return bodyHtml;
+ return (
+ bodyHtml.slice(0, tagEnd) + ` data-composition-file="${compPath}"` + bodyHtml.slice(tagEnd)
+ );
+}
+
/**
* Build a standalone HTML page for a sub-composition.
*
@@ -149,6 +167,21 @@ export function buildSubCompositionHtml(
rewrittenContent = contentDoc.body.innerHTML || rawComp;
}
+ // A composition file may ship a baked inline runtime (from a prior export:
+ // data-hyperframes-runtime / __hyperframeRuntime…). The studio injects its own
+ // preview runtime below, so strip the baked one from the body — otherwise it's
+ // double-loaded AND the baked inline copy can fail to parse inline (the
+ // "Unexpected token '<'" SyntaxError seen on comps with a baked runtime).
+ rewrittenContent = stripEmbeddedRuntimeScripts(rewrittenContent);
+
+ // The comp's root carries data-composition-id but (unlike inlined sub-comps,
+ // which inlineSubCompositions tags) no data-composition-file. Without it the
+ // studio can't resolve which file this comp's top-level elements live in and
+ // falls back to "index.html" — so the GSAP panel parses the project root (which
+ // may be a multi-timeline master) and wrongly reports "multiple timelines",
+ // disabling editing for a single-timeline comp. Tag the root with its own path.
+ rewrittenContent = tagRootCompositionFile(rewrittenContent, compPath);
+
// Use the project's index.html <head> to preserve all dependencies
const indexPath = join(projectDir, "index.html");
let headContent = "";
@@ -169,6 +202,11 @@ export function buildSubCompositionHtml(
// the composition's deps take precedence (last-write-wins for scripts).
if (compHeadContent) headContent += `\n${compHeadContent}`;
+ // Strip any baked runtime the borrowed index/comp <head> carried, for the same
+ // reason as the body above — done before injecting the preview runtime so the
+ // injected tag (added next) is never removed.
+ headContent = stripEmbeddedRuntimeScripts(headContent);
+
// Ensure runtime is present (might differ from the one in index.html)
if (
!headContent.includes("hyperframe.runtime") &&
diff --git a/packages/core/src/studio-api/routes/files.test.ts b/packages/core/src/studio-api/routes/files.test.ts
index 302831dba5..c65238a089 100644
--- a/packages/core/src/studio-api/routes/files.test.ts
+++ b/packages/core/src/studio-api/routes/files.test.ts
@@ -503,6 +503,46 @@ const tl = gsap.timeline();
expect(body.error).toContain("fromProperties");
});
+ // A rotation-only keyframe set must strip the legacy studio rotation channel just
+ // as a position keyframe set strips the offset channel — otherwise --hf-studio-rotation
+ // double-applies on top of the new GSAP rotation tween.
+ it("replace-with-keyframes strips studio rotation edits for a rotation-only keyframe set", async () => {
+ const projectDir = createProjectDir();
+ const ROT_COMP = `<!DOCTYPE html><html><body data-duration="3">
+<div id="box" data-start="0" data-duration="3" data-hf-studio-rotation="30" style="--hf-studio-rotation:30deg;rotate:30deg"></div>
+<script data-hyperframes-gsap>
+const tl = gsap.timeline();
+tl.to("#box", { opacity: 1, duration: 1 }, 0);
+</script>
+</body></html>`;
+ writeHtml(projectDir, "rot.html", ROT_COMP);
+ const app = new Hono();
+ registerFileRoutes(app, createAdapter(projectDir));
+
+ const anim = await getFirstAnimation(app, "rot.html");
+ const res = await app.request("http://localhost/projects/demo/gsap-mutations/rot.html", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ type: "replace-with-keyframes",
+ animationId: anim.id,
+ targetSelector: "#box",
+ position: 0,
+ duration: 1,
+ keyframes: [
+ { percentage: 0, properties: { rotation: 0 } },
+ { percentage: 100, properties: { rotation: 90 } },
+ ],
+ }),
+ });
+ const result = (await res.json()) as { ok: boolean; after: string };
+
+ expect(res.status).toBe(200);
+ expect(result.ok).toBe(true);
+ expect(result.after).not.toContain("--hf-studio-rotation");
+ expect(result.after).not.toContain("data-hf-studio-rotation");
+ });
+
it("edits a template-wrapped tween in place, preserving gsap.set and the IIFE", async () => {
const projectDir = createProjectDir();
writeComp(projectDir, "scene.html", TEMPLATE_COMP);
diff --git a/packages/core/src/studio-api/routes/files.ts b/packages/core/src/studio-api/routes/files.ts
index 0ce977592d..b6b288b540 100644
--- a/packages/core/src/studio-api/routes/files.ts
+++ b/packages/core/src/studio-api/routes/files.ts
@@ -28,6 +28,7 @@ import {
type UnsafeMutationValue,
} from "../helpers/finiteMutation.js";
import type { GsapAnimation } from "../../parsers/gsapSerialize.js";
+import { classifyPropertyGroup } from "../../parsers/gsapConstants.js";
import { parseGsapScriptAcorn } from "../../parsers/gsapParserAcorn.js";
import { unrollComputedTimeline } from "../../parsers/gsapUnroll.js";
import {
@@ -315,21 +316,49 @@ function stripStudioEditsFromTarget(document: Document, selector: string): numbe
let stripped = 0;
try {
for (const el of document.querySelectorAll(selector)) {
- if (!el.getAttribute("data-hf-studio-path-offset")) continue;
if (!isHTMLElement(el)) continue;
const htmlEl = el;
- const originalTranslate = el.getAttribute("data-hf-studio-original-inline-translate");
- htmlEl.style.removeProperty("--hf-studio-offset-x");
- htmlEl.style.removeProperty("--hf-studio-offset-y");
- if (originalTranslate) {
- htmlEl.style.setProperty("translate", originalTranslate);
- } else {
- htmlEl.style.removeProperty("translate");
+ let touched = false;
+ // Manual path offset (--hf-studio-offset / translate) — a GSAP position tween
+ // now owns position, so the stale offset channel must go.
+ if (el.getAttribute("data-hf-studio-path-offset")) {
+ const originalTranslate = el.getAttribute("data-hf-studio-original-inline-translate");
+ htmlEl.style.removeProperty("--hf-studio-offset-x");
+ htmlEl.style.removeProperty("--hf-studio-offset-y");
+ if (originalTranslate) {
+ htmlEl.style.setProperty("translate", originalTranslate);
+ } else {
+ htmlEl.style.removeProperty("translate");
+ }
+ el.removeAttribute("data-hf-studio-path-offset");
+ el.removeAttribute("data-hf-studio-original-translate");
+ el.removeAttribute("data-hf-studio-original-inline-translate");
+ touched = true;
+ }
+ // Manual rotation (--hf-studio-rotation / rotate) — likewise, a GSAP rotation
+ // set/tween now owns rotation, so clear the legacy CSS-var channel.
+ if (el.getAttribute("data-hf-studio-rotation")) {
+ const originalRotate = el.getAttribute("data-hf-studio-original-inline-rotate");
+ const originalOrigin = el.getAttribute("data-hf-studio-original-rotation-transform-origin");
+ htmlEl.style.removeProperty("--hf-studio-rotation");
+ if (originalRotate) {
+ htmlEl.style.setProperty("rotate", originalRotate);
+ } else {
+ htmlEl.style.removeProperty("rotate");
+ }
+ if (originalOrigin) {
+ htmlEl.style.setProperty("transform-origin", originalOrigin);
+ } else {
+ htmlEl.style.removeProperty("transform-origin");
+ }
+ el.removeAttribute("data-hf-studio-rotation");
+ el.removeAttribute("data-hf-studio-rotation-draft");
+ el.removeAttribute("data-hf-studio-original-rotate");
+ el.removeAttribute("data-hf-studio-original-inline-rotate");
+ el.removeAttribute("data-hf-studio-original-rotation-transform-origin");
+ touched = true;
}
- el.removeAttribute("data-hf-studio-path-offset");
- el.removeAttribute("data-hf-studio-original-translate");
- el.removeAttribute("data-hf-studio-original-inline-translate");
- stripped++;
+ if (touched) stripped++;
}
} catch {
// Invalid selector — skip silently.
@@ -337,6 +366,30 @@ function stripStudioEditsFromTarget(document: Document, selector: string): numbe
return stripped;
}
+// A studio path-offset (--hf-studio-offset / data-hf-studio-path-offset) and a GSAP
+// position tween both drive translate — keeping both stacks the offsets (a gesture or
+// drag recorded over a stale offset plays shoved off-position). When a committed tween
+// writes a position property, the tween owns position, so the stale offset must go.
+function keyframesWritePosition(
+ keyframes: Array<{ properties: Record<string, number | string> }>,
+): boolean {
+ return keyframes.some((kf) =>
+ Object.keys(kf.properties).some((k) => classifyPropertyGroup(k) === "position"),
+ );
+}
+
+// A studio rotation edit (--hf-studio-rotation / data-hf-studio-rotation) and a GSAP
+// rotation tween both drive rotate — keeping both stacks them. When a committed keyframe
+// set writes a rotation property, the tween owns rotation, so the stale CSS-var channel
+// must go (the position twin of this is `keyframesWritePosition`).
+function keyframesWriteRotation(
+ keyframes: Array<{ properties: Record<string, number | string> }>,
+): boolean {
+ return keyframes.some((kf) =>
+ Object.keys(kf.properties).some((k) => classifyPropertyGroup(k) === "rotation"),
+ );
+}
+
function lastKeyframeOpacity(kfs: GsapAnimation["keyframes"]): number | string | undefined {
if (!kfs) return undefined;
for (let i = kfs.keyframes.length - 1; i >= 0; i--) {
@@ -468,6 +521,24 @@ type GsapMutationRequest =
cp1?: { x: number; y: number };
cp2?: { x: number; y: number };
}
+ | {
+ type: "update-motion-path-point";
+ animationId: string;
+ pointIndex: number;
+ x: number;
+ y: number;
+ }
+ | { type: "add-motion-path-point"; animationId: string; index: number; x: number; y: number }
+ | { type: "remove-motion-path-point"; animationId: string; index: number }
+ | {
+ type: "add-motion-path";
+ targetSelector: string;
+ position: number;
+ duration: number;
+ x: number;
+ y: number;
+ ease?: string;
+ }
| { type: "remove-arc-path"; animationId: string }
| {
type: "add-with-keyframes";
@@ -535,6 +606,41 @@ type GsapMutationRequest =
type GsapMutationResult = string | { script: string; skippedSelectors: string[] };
+// Mutations that can change a position tween's first keyframe (value/existence/timing)
+// and therefore require the pre-keyframe hold-`set`s to be re-synced afterwards.
+// `syncPositionHoldsBeforeKeyframes` rebuilds all `hf-hold` sets from scratch: it acts
+// on every tween that has keyframes whose first percentage carries a position prop and
+// whose start is > 0. So any mutation that creates such a tween, retargets it, or moves
+// its start across the t=0 boundary must trigger a re-sync.
+const HOLD_SYNC_MUTATION_TYPES = new Set<string>([
+ "add-keyframe",
+ "update-keyframe",
+ "remove-keyframe",
+ "remove-all-keyframes",
+ "add-with-keyframes",
+ "replace-with-keyframes",
+ "convert-to-keyframes",
+ "materialize-keyframes",
+ "update-motion-path-point",
+ "add-motion-path-point",
+ "remove-motion-path-point",
+ // Authors a fresh motionPath tween whose parsed first keyframe is (0,0); if it lands
+ // at position > 0 the element snaps home at t=0 without a pre-tween hold-`set`.
+ "add-motion-path",
+ // Can move a tween's `position` (start) across the t=0 boundary, which flips whether a
+ // keyframed position tween needs a hold (started at 0 → moved later, or vice versa).
+ "update-meta",
+ // Time-shift / time-scale tweens, which can move a keyframed position tween's start
+ // across t=0, flipping hold need; stale holds are not repositioned by these ops.
+ "shift-positions",
+ "scale-positions",
+ // Retargets keyframed position tweens to a cloned element's selector; the old hold is
+ // keyed to the prior selector, so holds must be rebuilt for the new target.
+ "split-animations",
+ "delete",
+ "delete-all-for-selector",
+]);
+
async function executeGsapMutation(
body: GsapMutationRequest,
block: NonNullable<ReturnType<typeof extractGsapScriptBlock>>,
@@ -824,6 +930,10 @@ async function executeGsapMutationRecast(
unrollDynamicAnimations,
setArcPathInScript,
updateArcSegmentInScript,
+ updateMotionPathPointInScript,
+ addMotionPathPointInScript,
+ removeMotionPathPointInScript,
+ addMotionPathToScript,
removeArcPathFromScript,
addAnimationWithKeyframesToScript,
splitAnimationsInScript,
@@ -877,6 +987,17 @@ async function executeGsapMutationRecast(
if (body.fromProperties && body.method !== "fromTo") {
return respond({ error: "fromProperties is only valid for method=fromTo" }, 400);
}
+ // A new position/rotation animation owns that channel — strip the matching
+ // legacy studio CSS var (--hf-studio-offset / --hf-studio-rotation) so it can't
+ // double with the tween, matching add-with-keyframes/replace-with-keyframes.
+ if (
+ Object.keys(body.properties).some((k) => {
+ const group = classifyPropertyGroup(k);
+ return group === "position" || group === "rotation";
+ })
+ ) {
+ stripStudioEditsFromTarget(block.document, body.targetSelector);
+ }
const result = addAnimationToScript(block.scriptText, {
targetSelector: body.targetSelector,
method: body.method,
@@ -987,10 +1108,39 @@ async function executeGsapMutationRecast(
...(body.cp2 ? { cp2: body.cp2 } : {}),
});
}
+ case "update-motion-path-point": {
+ return updateMotionPathPointInScript(block.scriptText, body.animationId, body.pointIndex, {
+ x: body.x,
+ y: body.y,
+ });
+ }
+ case "add-motion-path-point": {
+ return addMotionPathPointInScript(block.scriptText, body.animationId, body.index, {
+ x: body.x,
+ y: body.y,
+ });
+ }
+ case "remove-motion-path-point": {
+ return removeMotionPathPointInScript(block.scriptText, body.animationId, body.index);
+ }
+ case "add-motion-path": {
+ const result = addMotionPathToScript(
+ block.scriptText,
+ body.targetSelector,
+ body.position,
+ body.duration,
+ { x: body.x, y: body.y },
+ body.ease,
+ );
+ return result.script;
+ }
case "remove-arc-path": {
return removeArcPathFromScript(block.scriptText, body.animationId);
}
case "add-with-keyframes": {
+ if (keyframesWritePosition(body.keyframes) || keyframesWriteRotation(body.keyframes)) {
+ stripStudioEditsFromTarget(block.document, body.targetSelector);
+ }
const result = addAnimationWithKeyframesToScript(
block.scriptText,
body.targetSelector,
@@ -1002,6 +1152,9 @@ async function executeGsapMutationRecast(
return result.script;
}
case "replace-with-keyframes": {
+ if (keyframesWritePosition(body.keyframes) || keyframesWriteRotation(body.keyframes)) {
+ stripStudioEditsFromTarget(block.document, body.targetSelector);
+ }
const script = removeAnimationFromScript(block.scriptText, body.animationId);
const added = addAnimationWithKeyframesToScript(
script,
@@ -1277,11 +1430,18 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
target?: { id?: string; selector?: string; selectorIndex?: number };
splitTime?: number;
newId?: string;
+ elementStart?: number;
+ elementDuration?: number;
}>(c);
if ("error" in parsed) return parsed.error;
if (typeof parsed.body.splitTime !== "number" || !parsed.body.newId) {
return c.json({ error: "target, splitTime, and newId required" }, 400);
}
+ const fallbackTiming =
+ typeof parsed.body.elementStart === "number" &&
+ typeof parsed.body.elementDuration === "number"
+ ? { start: parsed.body.elementStart, duration: parsed.body.elementDuration }
+ : undefined;
let originalContent: string;
try {
@@ -1294,6 +1454,7 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
parsed.target,
parsed.body.splitTime,
parsed.body.newId,
+ fallbackTiming,
);
if (!result.matched) {
return c.json({ ok: false, changed: false, content: originalContent, path: ctx.filePath });
@@ -1537,7 +1698,15 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
const result = await executeGsapMutation(body, block, respond);
if (result instanceof Response) return result;
- const newScript = typeof result === "string" ? result : result.script;
+ let newScript = typeof result === "string" ? result : result.script;
+ // Keep the "hold before first keyframe" sets in sync after any mutation that can
+ // change a position tween's first keyframe or its existence. Without it, an
+ // element snaps to its CSS base before the tween starts instead of holding its
+ // first keyframe (the universal NLE behavior).
+ if (HOLD_SYNC_MUTATION_TYPES.has(body.type)) {
+ const parser = await loadGsapParser();
+ newScript = parser.syncPositionHoldsBeforeKeyframes(newScript);
+ }
const changed = newScript !== block.scriptText;
const newHtml = changed ? block.replaceScript(newScript) : html;
let backupPath: string | null = null;
diff --git a/packages/core/src/studio-api/routes/preview.test.ts b/packages/core/src/studio-api/routes/preview.test.ts
index 8688a3c986..de1be32554 100644
--- a/packages/core/src/studio-api/routes/preview.test.ts
+++ b/packages/core/src/studio-api/routes/preview.test.ts
@@ -114,6 +114,57 @@ describe("registerPreviewRoutes", () => {
expect(html.indexOf("CustomEase.min.js")).toBeLessThan(html.indexOf("__hfStudioMotionApply"));
});
+ it("injects the GSAP MotionPathPlugin when the composition uses a motionPath", async () => {
+ const projectDir = createProjectDir();
+ writeFileSync(
+ join(projectDir, "index.html"),
+ `<!doctype html><html><head>
+ <script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
+ </head><body><div id="card" class="clip"></div>
+ <script>
+ const tl = gsap.timeline({ paused: true });
+ tl.to("#card", { motionPath: { path: [{ x: 0, y: 0 }, { x: 100, y: 50 }] }, duration: 1 }, 0);
+ window.__timelines = { index: tl };
+ </script>
+ </body></html>`,
+ );
+ const app = new Hono();
+ registerPreviewRoutes(app, createAdapter(projectDir));
+
+ const response = await app.request("http://localhost/projects/demo/preview");
+ const html = await response.text();
+
+ expect(response.status).toBe(200);
+ // Plugin version is derived from the composition's own gsap (gsap@3 here).
+ expect(html).toContain("gsap@3/dist/MotionPathPlugin.min.js");
+ // Plugin must load AFTER the core gsap script so it can register onto it.
+ expect(html.indexOf("gsap.min.js")).toBeLessThan(html.indexOf("MotionPathPlugin.min.js"));
+ });
+
+ it("does NOT inject MotionPathPlugin when the composition has no motionPath", async () => {
+ const projectDir = createProjectDir();
+ writeFileSync(
+ join(projectDir, "index.html"),
+ `<!doctype html><html><head>
+ <script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
+ </head><body><div id="card" class="clip"></div>
+ <script>
+ const tl = gsap.timeline({ paused: true });
+ tl.to("#card", { x: 100, duration: 1 }, 0);
+ window.__timelines = { index: tl };
+ </script>
+ </body></html>`,
+ );
+ const app = new Hono();
+ registerPreviewRoutes(app, createAdapter(projectDir));
+
+ const response = await app.request("http://localhost/projects/demo/preview");
+ const html = await response.text();
+
+ expect(response.status).toBe(200);
+ expect(html).not.toContain("MotionPathPlugin.min.js");
+ });
+
it("injects Studio GSAP motion runtime into sub-composition previews with the active source path", async () => {
const projectDir = createProjectDir();
mkdirSync(join(projectDir, "compositions"), { recursive: true });
diff --git a/packages/core/src/studio-api/routes/preview.ts b/packages/core/src/studio-api/routes/preview.ts
index 551ddf477f..d561808a80 100644
--- a/packages/core/src/studio-api/routes/preview.ts
+++ b/packages/core/src/studio-api/routes/preview.ts
@@ -1,7 +1,7 @@
import type { Hono } from "hono";
import { existsSync, readFileSync, statSync } from "node:fs";
import { join } from "node:path";
-import { injectScriptsIntoHtml } from "../../compiler/htmlDocument.js";
+import { injectScriptsIntoHtml, stripEmbeddedRuntimeScripts } from "../../compiler/htmlDocument.js";
import type { StudioApiAdapter } from "../types.js";
import { resolveWithinProject } from "../helpers/safePath.js";
import { getMimeType } from "../helpers/mime.js";
@@ -18,6 +18,7 @@ const PROJECT_SIGNATURE_META = "hyperframes-project-signature";
const GSAP_CDN_VERSION = "3.15.0";
const GSAP_CDN_SCRIPT = `<script src="https://cdn.jsdelivr.net/npm/gsap@${GSAP_CDN_VERSION}/dist/gsap.min.js"></script>`;
const GSAP_CUSTOM_EASE_CDN_SCRIPT = `<script src="https://cdn.jsdelivr.net/npm/gsap@${GSAP_CDN_VERSION}/dist/CustomEase.min.js"></script>`;
+const GSAP_MOTION_PATH_CDN_SCRIPT = `<script src="https://cdn.jsdelivr.net/npm/gsap@${GSAP_CDN_VERSION}/dist/MotionPathPlugin.min.js"></script>`;
function resolveProjectSignature(adapter: StudioApiAdapter, projectDir: string): string {
return adapter.getProjectSignature?.(projectDir) ?? createProjectSignature(projectDir);
@@ -86,6 +87,42 @@ function htmlHasCustomEase(html: string): boolean {
);
}
+// A composition that drives motion via GSAP's `motionPath` (e.g. a studio-created
+// motion path written into the single-source timeline) needs MotionPathPlugin
+// registered before the timeline first renders — otherwise the initial seek
+// throws "Invalid property motionPath ... Missing plugin?". Detect it anywhere in
+// the bundle (the plugin registers globally, so sub-composition usage counts too).
+function htmlUsesMotionPath(html: string): boolean {
+ return /motionPath\s*[:{]/.test(html);
+}
+
+function htmlHasMotionPathPlugin(html: string): boolean {
+ return (
+ /<script\b[^>]*src=["'][^"']*MotionPathPlugin/i.test(html) ||
+ /\bwindow\.MotionPathPlugin\b/.test(html) ||
+ /\bMotionPathPlugin\s*=\s*/.test(html)
+ );
+}
+
+function injectMotionPathPluginIfNeeded(html: string): string {
+ if (!htmlUsesMotionPath(html) || htmlHasMotionPathPlugin(html)) return html;
+ // The plugin registers onto an already-loaded gsap, so it must come AFTER the
+ // core gsap script — which often lives at body-end, not <head>. Insert it
+ // directly after the gsap script tag; only fall back to <head> if none is found
+ // (e.g. gsap is inlined).
+ const gsapScript = /<script\b[^>]*\bsrc=["'][^"']*\/gsap(\.min)?\.js["'][^>]*>\s*<\/script>/i;
+ const match = html.match(gsapScript);
+ if (match) {
+ // Match the plugin version to the composition's own gsap so the plugin
+ // registers cleanly (a minor-version skew triggers a GSAP compatibility warning).
+ const version = match[0].match(/gsap@([\d.]+)/)?.[1] ?? GSAP_CDN_VERSION;
+ const pluginTag = `<script src="https://cdn.jsdelivr.net/npm/gsap@${version}/dist/MotionPathPlugin.min.js"></script>`;
+ const end = html.indexOf(match[0]) + match[0].length;
+ return html.slice(0, end) + "\n" + pluginTag + html.slice(end);
+ }
+ return injectScriptTagIntoHead(html, GSAP_MOTION_PATH_CDN_SCRIPT);
+}
+
function injectStudioMotionDependencies(html: string, manifestContent: string): string {
const manifest = parseStudioMotionManifestContent(manifestContent);
if (!manifest.hasMotion) return html;
@@ -149,8 +186,10 @@ function injectStudioPreviewAugmentations(
activeCompositionPath: string,
): string {
return injectStudioMotionScript(
- injectGsapCdnFallback(
- injectProjectSignature(html, resolveProjectSignature(adapter, projectDir)),
+ injectMotionPathPluginIfNeeded(
+ injectGsapCdnFallback(
+ injectProjectSignature(html, resolveProjectSignature(adapter, projectDir)),
+ ),
),
projectDir,
activeCompositionPath,
@@ -221,7 +260,10 @@ export function registerPreviewRoutes(api: Hono, adapter: StudioApiAdapter): voi
let mainCompositionPath = "index.html";
if (!bundled) {
if (!diskMain) return c.text("not found", 404);
- bundled = normalizedDisk ?? diskMain.html;
+ // Disk HTML may carry a baked inline runtime from a prior export; strip
+ // it so the preview runtime injected below isn't double-loaded (the
+ // bundled path already strips via htmlBundler). Idempotent if absent.
+ bundled = stripEmbeddedRuntimeScripts(normalizedDisk ?? diskMain.html);
mainCompositionPath = diskMain.compositionPath;
}
diff --git a/packages/studio/index.html b/packages/studio/index.html
index 7e8dc88ae3..b3659e0763 100644
--- a/packages/studio/index.html
+++ b/packages/studio/index.html
@@ -7,7 +7,7 @@
<title>HyperFrames Studio</title>
</head>
<body>
- <div id="root"></div>
+ <div data-hf-id="hf-aph5" id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
diff --git a/packages/studio/src/App.tsx b/packages/studio/src/App.tsx
index 15fddfda71..d3c761a7a9 100644
--- a/packages/studio/src/App.tsx
+++ b/packages/studio/src/App.tsx
@@ -428,6 +428,7 @@ export function StudioApp() {
domEditSelection: domEditSession.domEditSelection,
buildDomSelectionFromTarget: domEditSession.buildDomSelectionFromTarget,
applyDomSelection: domEditSession.applyDomSelection,
+ setRightPanelTab: panelLayout.setRightPanelTab,
initialState: initialUrlStateRef.current,
});
const studioCtxValue = buildStudioContextValue({
diff --git a/packages/studio/src/components/StudioHeader.tsx b/packages/studio/src/components/StudioHeader.tsx
index b6b60ec3d8..f998a8db25 100644
--- a/packages/studio/src/components/StudioHeader.tsx
+++ b/packages/studio/src/components/StudioHeader.tsx
@@ -8,7 +8,6 @@ import {
import { getHistoryShortcutLabel } from "../utils/studioHelpers";
import { useStudioShellContext } from "../contexts/StudioContext";
import { usePanelLayoutContext } from "../contexts/PanelLayoutContext";
-import { useDomEditActionsContext } from "../contexts/DomEditContext";
import { useViewMode, type StudioViewMode } from "../contexts/ViewModeContext";
import { trackStudioEvent } from "../utils/studioTelemetry";
@@ -194,7 +193,6 @@ export function StudioHeader({
}: StudioHeaderProps) {
const { projectId, editHistory, handleUndo, handleRedo } = useStudioShellContext();
const { rightCollapsed, setRightCollapsed, setRightPanelTab } = usePanelLayoutContext();
- const { clearDomSelection } = useDomEditActionsContext();
return (
<div className="flex items-center justify-between h-10 px-3 bg-neutral-900 border-b border-neutral-800 flex-shrink-0">
@@ -279,7 +277,8 @@ export function StudioHeader({
return;
}
trackStudioEvent("panel_toggle", { panel: "inspector", collapsed: true });
- clearDomSelection();
+ // Keep the current selection when collapsing the Inspector — closing
+ // the panel shouldn't deselect the element.
setRightCollapsed(true);
}}
disabled={!STUDIO_INSPECTOR_PANELS_ENABLED}
diff --git a/packages/studio/src/components/StudioPreviewArea.tsx b/packages/studio/src/components/StudioPreviewArea.tsx
index cfa981a3e7..b42a06837d 100644
--- a/packages/studio/src/components/StudioPreviewArea.tsx
+++ b/packages/studio/src/components/StudioPreviewArea.tsx
@@ -3,6 +3,8 @@ import { NLELayout } from "./nle/NLELayout";
import { CaptionOverlay } from "../captions/components/CaptionOverlay";
import { CaptionTimeline } from "../captions/components/CaptionTimeline";
import { DomEditOverlay } from "./editor/DomEditOverlay";
+import { MotionPathOverlay } from "./editor/MotionPathOverlay";
+import { useCompositionDimensions } from "../hooks/useCompositionDimensions";
import { SnapToolbar } from "./editor/SnapToolbar";
import { StudioFeedbackBar } from "./StudioFeedbackBar";
import type { TimelineElement } from "../player";
@@ -10,6 +12,7 @@ import { usePlayerStore } from "../player/store/playerStore";
import type { BlockedTimelineEditIntent } from "../player/components/timelineEditing";
import {
STUDIO_INSPECTOR_PANELS_ENABLED,
+ STUDIO_KEYFRAMES_ENABLED,
STUDIO_PREVIEW_MANUAL_EDITING_ENABLED,
STUDIO_PREVIEW_SELECTION_ENABLED,
} from "./editor/manualEditingAvailability";
@@ -108,6 +111,7 @@ export function StudioPreviewArea({
isPlaying,
refreshPreviewDocumentVersion,
} = useStudioPlaybackContext();
+ const compositionDimensions = useCompositionDimensions();
const {
domEditHoverSelection,
@@ -337,6 +341,14 @@ export function StudioPreviewArea({
onToggleRecording={onToggleRecording}
/>
<SnapToolbar onSnapChange={setSnapPrefs} />
+ {STUDIO_KEYFRAMES_ENABLED && (
+ <MotionPathOverlay
+ iframeRef={previewIframeRef}
+ selection={shouldShowSelectedDomBounds ? domEditSelection : null}
+ compositionSize={compositionDimensions}
+ isPlaying={isPlaying}
+ />
+ )}
{gestureOverlay}
</>
) : null
diff --git a/packages/studio/src/components/TimelineToolbar.tsx b/packages/studio/src/components/TimelineToolbar.tsx
index ecd584a145..fd9d9ac134 100644
--- a/packages/studio/src/components/TimelineToolbar.tsx
+++ b/packages/studio/src/components/TimelineToolbar.tsx
@@ -1,5 +1,10 @@
import { useRef } from "react";
-import { useEnableKeyframes, type EnableKeyframesSession } from "../hooks/useEnableKeyframes";
+import {
+ useEnableKeyframes,
+ isPlayheadWithinTween,
+ type EnableKeyframesSession,
+} from "../hooks/useEnableKeyframes";
+import { computeElementPercentage } from "../hooks/gsapShared";
import {
getNextTimelineZoomPercent,
getTimelineZoomPercent,
@@ -44,23 +49,25 @@ function useKeyframeToggle(session?: DomEditSessionSlice) {
const anims = session.selectedGsapAnimations;
const kfAnim = anims.find((a) => a.keyframes);
- const computePct = (time: number) => {
- const elStart = Number.parseFloat(sel?.dataAttributes?.start ?? "0") || 0;
- const elDuration = Number.parseFloat(sel?.dataAttributes?.duration ?? "1") || 1;
- return elDuration > 0
- ? Math.max(0, Math.min(100, Math.round(((time - elStart) / elDuration) * 1000) / 10))
- : 0;
- };
-
let state: "active" | "inactive" | "none" = "none";
+ // Outside the tween, clicking extends the animation to the playhead rather than
+ // toggling a (clamped) edge keyframe — so the button stays an "add" affordance.
+ let willExtend = false;
if (kfAnim?.keyframes && sel) {
- const pct = computePct(currentTime);
- state = kfAnim.keyframes.keyframes.some((k) => Math.abs(k.percentage - pct) <= 1)
- ? "active"
- : "inactive";
+ if (!isPlayheadWithinTween(kfAnim, currentTime)) {
+ state = "inactive";
+ willExtend = true;
+ } else {
+ // Tween-relative percentage (not the clip range) so the button state matches
+ // where the keyframe would actually land.
+ const pct = computeElementPercentage(currentTime, sel, kfAnim);
+ state = kfAnim.keyframes.keyframes.some((k) => Math.abs(k.percentage - pct) <= 1)
+ ? "active"
+ : "inactive";
+ }
}
- return { state, onToggle: sel ? onToggle : undefined };
+ return { state, willExtend, onToggle: sel ? onToggle : undefined };
}
// fallow-ignore-next-line complexity
@@ -76,7 +83,11 @@ export function TimelineToolbar({
const beatAnalysisReady = usePlayerStore((s) => s.beatAnalysis !== null);
const { zoomMode, manualZoomPercent, setZoomMode, setManualZoomPercent } = useTimelineZoom();
const displayedTimelineZoomPercent = getTimelineZoomPercent(zoomMode, manualZoomPercent);
- const { state: keyframeState, onToggle: onToggleKeyframe } = useKeyframeToggle(domEditSession);
+ const {
+ state: keyframeState,
+ willExtend: keyframeWillExtend,
+ onToggle: onToggleKeyframe,
+ } = useKeyframeToggle(domEditSession);
return (
<div className="border-b border-neutral-800/40 bg-neutral-950/96">
@@ -124,7 +135,9 @@ export function TimelineToolbar({
keyframeState === "active"
? "Remove keyframe at playhead"
: keyframeState === "inactive"
- ? "Add keyframe at playhead"
+ ? keyframeWillExtend
+ ? "Add keyframe at playhead (extends animation)"
+ : "Add keyframe at playhead"
: "Enable keyframes"
}
>
diff --git a/packages/studio/src/components/editor/DomEditOverlay.test.ts b/packages/studio/src/components/editor/DomEditOverlay.test.ts
index 7fb292913c..468bebfdfe 100644
--- a/packages/studio/src/components/editor/DomEditOverlay.test.ts
+++ b/packages/studio/src/components/editor/DomEditOverlay.test.ts
@@ -282,7 +282,6 @@ describe("DomEditOverlay", () => {
};
let currentSelection: DomEditSelection | null = selection;
- const onToggleRecording = vi.fn();
const iframeRef = { current: document.createElement("iframe") as HTMLIFrameElement | null };
const originalPointerCapture = HTMLDivElement.prototype.setPointerCapture;
HTMLDivElement.prototype.setPointerCapture = () => {};
@@ -298,8 +297,6 @@ describe("DomEditOverlay", () => {
hoverSelection: null,
onSelectionChange: (next: DomEditSelection) => setSelected(next),
}),
- recordingState: "idle",
- onToggleRecording,
});
}
@@ -340,16 +337,6 @@ describe("DomEditOverlay", () => {
"drag",
expect.objectContaining({ button: 0 }),
);
- const recordButton = host.querySelector(
- '[aria-label="Record gesture (R)"]',
- ) as HTMLButtonElement;
- expect(recordButton).toBeTruthy();
-
- act(() => {
- recordButton.click();
- });
-
- expect(onToggleRecording).toHaveBeenCalledTimes(1);
act(() => {
root.unmount();
diff --git a/packages/studio/src/components/editor/DomEditOverlay.tsx b/packages/studio/src/components/editor/DomEditOverlay.tsx
index 12789407c6..5e0f736dd4 100644
--- a/packages/studio/src/components/editor/DomEditOverlay.tsx
+++ b/packages/studio/src/components/editor/DomEditOverlay.tsx
@@ -14,7 +14,7 @@ import { useDomEditOverlayRects } from "./useDomEditOverlayRects";
import { createDomEditOverlayGestureHandlers } from "./useDomEditOverlayGestures";
import { SnapGuideOverlay, type SnapGuidesState } from "./SnapGuideOverlay";
import { GridOverlay } from "./GridOverlay";
-import { GestureRecordBadge, type GestureRecordingState } from "./GestureRecordControl";
+import type { GestureRecordingState } from "./GestureRecordControl";
// Re-exports for external consumers — preserving existing import paths.
export {
@@ -55,6 +55,7 @@ interface DomEditOverlayProps {
onPathOffsetCommit: (
selection: DomEditSelection,
next: { x: number; y: number },
+ modifiers?: { altKey?: boolean },
) => Promise<void> | void;
onGroupPathOffsetCommit: (updates: DomEditGroupPathOffsetCommit[]) => Promise<void> | void;
onBoxSizeCommit: (
@@ -87,8 +88,6 @@ export const DomEditOverlay = memo(function DomEditOverlay({
onGroupPathOffsetCommit,
onBoxSizeCommit,
onRotationCommit,
- recordingState,
- onToggleRecording,
}: DomEditOverlayProps) {
const overlayRef = useRef<HTMLDivElement | null>(null);
const boxRef = useRef<HTMLDivElement | null>(null);
@@ -243,6 +242,7 @@ export const DomEditOverlay = memo(function DomEditOverlay({
if (!selection) return "none";
return `${selection.sourceFile}:${selection.id ?? selection.selector ?? selection.label}:${selection.selectorIndex ?? 0}`;
}, [selection]);
+
const groupBounds = useMemo(
() => resolveDomEditGroupOverlayRect(groupOverlayItems.map((item) => item.rect)),
[groupOverlayItems],
@@ -433,13 +433,6 @@ export const DomEditOverlay = memo(function DomEditOverlay({
/>
</div>
)}
- {onToggleRecording && (
- <GestureRecordBadge
- rect={overlayRect}
- recordingState={recordingState}
- onToggleRecording={onToggleRecording}
- />
- )}
<div
key={selectionKey}
ref={boxRef}
diff --git a/packages/studio/src/components/editor/KeyframeNavigation.test.ts b/packages/studio/src/components/editor/KeyframeNavigation.test.ts
new file mode 100644
index 0000000000..0ce9a694de
--- /dev/null
+++ b/packages/studio/src/components/editor/KeyframeNavigation.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it } from "vitest";
+import { clipToTweenPercentage } from "./KeyframeNavigation";
+
+/**
+ * Regression: keyframe add/remove are keyed by TWEEN-relative percentage (what the
+ * GSAP writer + runtime use), NOT the clip-relative playhead used for display/seek.
+ * The Layout-panel diamond used to emit clip-relative %, so the mutation missed
+ * every keyframe (off by the tween's offset/scale) → a silent no-op on disk that
+ * the optimistic cache hid, so the motion path never refreshed.
+ */
+
+// A tween that starts partway through the element's lifetime and is shorter than
+// it: the clip→tween map is linear with tween% = (clip% - 20) * 2.5 over [20, 60].
+const KEYFRAMES = [
+ { percentage: 20, tweenPercentage: 0, properties: { x: 0 } },
+ { percentage: 30, tweenPercentage: 25, properties: { x: -180 } },
+ { percentage: 50, tweenPercentage: 75, properties: { x: -320 } },
+ { percentage: 60, tweenPercentage: 100, properties: { x: -460 } },
+];
+
+describe("clipToTweenPercentage", () => {
+ it("maps anchor keyframes to their tween-relative percentages", () => {
+ expect(clipToTweenPercentage(KEYFRAMES, 20)).toBeCloseTo(0, 5);
+ expect(clipToTweenPercentage(KEYFRAMES, 60)).toBeCloseTo(100, 5);
+ });
+
+ it("linearly interpolates a clip-relative playhead into tween space", () => {
+ // clip 40% is the midpoint of the tween's clip span [20, 60] → tween 50%.
+ expect(clipToTweenPercentage(KEYFRAMES, 40)).toBeCloseTo(50, 5);
+ });
+
+ it("falls back to the input when there's no usable mapping", () => {
+ expect(clipToTweenPercentage([], 40)).toBe(40);
+ expect(clipToTweenPercentage([{ percentage: 10 }], 40)).toBe(40);
+ });
+});
diff --git a/packages/studio/src/components/editor/KeyframeNavigation.tsx b/packages/studio/src/components/editor/KeyframeNavigation.tsx
index 48f2f51770..c54047c305 100644
--- a/packages/studio/src/components/editor/KeyframeNavigation.tsx
+++ b/packages/studio/src/components/editor/KeyframeNavigation.tsx
@@ -3,9 +3,12 @@ import { KeyframeDiamond, type DiamondState } from "./KeyframeDiamond";
interface KeyframeNavigationProps {
property: string;
- /** All keyframes for this element's tween, or null if no keyframes exist */
+ /** All keyframes for this element's tween, or null if no keyframes exist.
+ * `percentage` is clip-relative (element lifetime) for display/seek;
+ * `tweenPercentage` is the tween-relative value the writer/runtime key on. */
keyframes: Array<{
percentage: number;
+ tweenPercentage?: number;
properties: Record<string, number | string>;
ease?: string;
}> | null;
@@ -19,6 +22,26 @@ interface KeyframeNavigationProps {
const TOLERANCE = 0.5;
+/**
+ * Convert a clip-relative percentage (element lifetime, used for display/seek) to
+ * the TWEEN-relative percentage the GSAP writer/runtime key on. The clip→tween
+ * map is linear, recovered from the keyframes' own (percentage, tweenPercentage)
+ * pairs. Falls back to the input when there's no usable mapping (e.g. parser
+ * keyframes that are already tween-relative, or fewer than two anchors).
+ */
+export function clipToTweenPercentage(
+ keyframes: ReadonlyArray<{ percentage: number; tweenPercentage?: number }>,
+ clipPct: number,
+): number {
+ const mapped = keyframes.filter((kf) => typeof kf.tweenPercentage === "number");
+ if (mapped.length < 2) return clipPct;
+ const a = mapped[0]!;
+ const b = mapped[mapped.length - 1]!;
+ if (b.percentage === a.percentage) return a.tweenPercentage!;
+ const slope = (b.tweenPercentage! - a.tweenPercentage!) / (b.percentage - a.percentage);
+ return a.tweenPercentage! + (clipPct - a.percentage) * slope;
+}
+
function ArrowLeft({ disabled }: { disabled: boolean }) {
return (
<svg
@@ -94,13 +117,20 @@ export const KeyframeNavigation = memo(function KeyframeNavigation({
diamondState = "ghost";
}
+ // Keyframe add/remove are keyed by TWEEN-relative percentage (what the GSAP
+ // writer + runtime use), not the clip-relative `currentPercentage` used for
+ // display/seek. Removing on an existing keyframe uses its own tweenPercentage;
+ // adding converts the clip-relative playhead through the keyframes' own
+ // clip→tween linear mapping. Passing clip-relative % made the mutation miss
+ // every keyframe (off by the tween's offset/scale) → a silent no-op on disk
+ // while the optimistic cache hid it, so the motion path never refreshed.
const handleDiamondClick = () => {
if (diamondState === "ghost") {
onConvertToKeyframes();
- } else if (diamondState === "active") {
- onRemoveKeyframe(currentPercentage);
+ } else if (diamondState === "active" && atCurrent) {
+ onRemoveKeyframe(atCurrent.tweenPercentage ?? atCurrent.percentage);
} else {
- onAddKeyframe(currentPercentage);
+ onAddKeyframe(clipToTweenPercentage(propertyKeyframes, currentPercentage));
}
};
diff --git a/packages/studio/src/components/editor/MotionPathNode.tsx b/packages/studio/src/components/editor/MotionPathNode.tsx
new file mode 100644
index 0000000000..c6d3ce43ca
--- /dev/null
+++ b/packages/studio/src/components/editor/MotionPathNode.tsx
@@ -0,0 +1,98 @@
+import type React from "react";
+
+// Editor primary color (themeable via --hf-accent). Applied through inline
+// style because CSS var() isn't valid in SVG presentation attributes.
+export const ACCENT = "var(--hf-accent, #3CE6AC)";
+
+/** One path node: a diamond (matching the timeline keyframe), a wider transparent
+ * grab target (when editable), and a hover-revealed × delete badge (when removable). */
+export function MotionPathNode(props: {
+ cx: number;
+ cy: number;
+ r: number;
+ interactive: boolean;
+ removable: boolean;
+ grabbing: boolean;
+ selected: boolean;
+ onEnter: () => void;
+ onLeave: () => void;
+ onPointerDown: (e: React.PointerEvent) => void;
+ onPointerMove: (e: React.PointerEvent) => void;
+ onPointerUp: (e: React.PointerEvent) => void;
+ onRemove: (e: React.PointerEvent) => void;
+ onContextMenu?: (e: React.MouseEvent) => void;
+}) {
+ const { cx, cy, r, interactive, removable, grabbing, selected } = props;
+ const bx = cx + r * 1.8;
+ const by = cy - r * 1.8;
+ const k = r * 0.55;
+ // Diamond matching the timeline keyframe (a 45°-rotated rounded square).
+ // `side` is chosen so the diamond's points reach ~`r` from center, matching the
+ // old dot's footprint; selection is shown by enlarging it (no extra shape).
+ const side = (selected ? r * 1.5 : r) * 1.414;
+ return (
+ <g onPointerEnter={props.onEnter} onPointerLeave={props.onLeave}>
+ <rect
+ x={cx - side / 2}
+ y={cy - side / 2}
+ width={side}
+ height={side}
+ rx={side * 0.17}
+ transform={`rotate(45 ${cx} ${cy})`}
+ stroke="#0b0f1a"
+ strokeWidth={1.5}
+ vectorEffect="non-scaling-stroke"
+ style={{ fill: ACCENT }}
+ />
+ {interactive && (
+ <circle
+ cx={cx}
+ cy={cy}
+ r={r * 2.4}
+ fill="transparent"
+ className="pointer-events-auto"
+ style={{ cursor: grabbing ? "grabbing" : "grab" }}
+ onPointerDown={props.onPointerDown}
+ onPointerMove={props.onPointerMove}
+ onPointerUp={props.onPointerUp}
+ onContextMenu={props.onContextMenu}
+ />
+ )}
+ {removable && (
+ <g
+ className="pointer-events-auto"
+ style={{ cursor: "pointer" }}
+ onPointerDown={props.onRemove}
+ >
+ <circle
+ cx={bx}
+ cy={by}
+ r={r * 1.3}
+ stroke="#0b0f1a"
+ strokeWidth={1}
+ vectorEffect="non-scaling-stroke"
+ style={{ fill: ACCENT }}
+ />
+ <line
+ x1={bx - k}
+ y1={by - k}
+ x2={bx + k}
+ y2={by + k}
+ stroke="#0b0f1a"
+ strokeWidth={1.5}
+ vectorEffect="non-scaling-stroke"
+ />
+ <line
+ x1={bx + k}
+ y1={by - k}
+ x2={bx - k}
+ y2={by + k}
+ stroke="#0b0f1a"
+ strokeWidth={1.5}
+ vectorEffect="non-scaling-stroke"
+ />
+ </g>
+ )}
+ </g>
+ );
+}
diff --git a/packages/studio/src/components/editor/MotionPathOverlay.tsx b/packages/studio/src/components/editor/MotionPathOverlay.tsx
new file mode 100644
index 0000000000..e96498e2e0
--- /dev/null
+++ b/packages/studio/src/components/editor/MotionPathOverlay.tsx
@@ -0,0 +1,481 @@
+import { memo, useEffect, useRef, useState, type RefObject } from "react";
+import type { DomEditSelection } from "./domEditing";
+import { useDomEditContext } from "../../contexts/DomEditContext";
+import { usePlayerStore } from "../../player/store/playerStore";
+import { parkPlayheadOnKeyframe } from "../../hooks/gsapDragCommit";
+import { nearestPointOnPath, type MotionNodeRef } from "./motionPathGeometry";
+import { editableAnimationId, selectorFor } from "./motionPathSelection";
+import { ACCENT, MotionPathNode } from "./MotionPathNode";
+import {
+ KeyframeDiamondContextMenu,
+ type KeyframeDiamondContextMenuState,
+} from "../../player/components/KeyframeDiamondContextMenu";
+import {
+ commitAddKeyframe,
+ commitAddWaypoint,
+ commitCreatePath,
+ commitNode,
+ commitRemoveWaypoint,
+} from "./motionPathCommit";
+import {
+ elementHome,
+ hasMotionPathPlugin,
+ isPreviewHtmlElement,
+ useMotionPathData,
+} from "./useMotionPathData";
+
+interface MotionPathOverlayProps {
+ iframeRef: RefObject<HTMLIFrameElement | null>;
+ selection: DomEditSelection | null;
+ compositionSize: { width: number; height: number } | null;
+ isPlaying: boolean;
+}
+
+type Draft = { index: number; x: number; y: number };
+type DragState = {
+ index: number;
+ startX: number;
+ startY: number;
+ initX: number;
+ initY: number;
+ scale: number;
+ ref: MotionNodeRef;
+};
+
+const NODE_PX = 6; // node radius in screen pixels (kept constant across zoom)
+// Click-vs-drag cutoff in SCREEN pixels. Below this the pointer-up is a click
+// (select the keyframe); at or above it the gesture commits a move. Screen-space
+// (not composition px) so it behaves identically at any zoom.
+const DRAG_THRESHOLD_PX = 3;
+
+/**
+ * Draws the selected element's GSAP motion path over the canvas — a dashed
+ * polyline through its x/y keyframes (or motionPath waypoints) with a draggable
+ * node at each. Dragging an x/y node rewrites the keyframe; dragging a waypoint
+ * rewrites the motionPath point; both commit to source (undoable). Renders in
+ * declared composition coordinates so the path doesn't drift under GSAP
+ * transforms. Read-only (no drag) while playing or when the tween isn't
+ * statically editable. Nothing renders when the selection has no positional
+ * motion.
+ */
+// fallow-ignore-next-line complexity
+export const MotionPathOverlay = memo(function MotionPathOverlay({
+ iframeRef,
+ selection,
+ compositionSize,
+ isPlaying,
+}: MotionPathOverlayProps) {
+ const {
+ commitMutation,
+ selectedGsapAnimations,
+ handleGsapRemoveKeyframe,
+ handleGsapDeleteAllForElement,
+ } = useDomEditContext();
+ const { rect, geometry, geometryResolved, visibleInPreview, home } = useMotionPathData(
+ iframeRef,
+ selectorFor(selection),
+ );
+ const [draft, setDraft] = useState<Draft | null>(null);
+ const [ghost, setGhost] = useState<{ x: number; y: number; segIndex: number } | null>(null);
+ const [hoverNode, setHoverNode] = useState<number | null>(null);
+ // Right-click context menu on a keyframe node — same delete actions as the
+ // timeline keyframe diamond.
+ const [kfMenu, setKfMenu] = useState<KeyframeDiamondContextMenuState | null>(null);
+ // The keyframe % selected by clicking its node — highlighted, and the next drag
+ // modifies it rather than adding a keyframe.
+ const activeKeyframePct = usePlayerStore((s) => s.activeKeyframePct);
+ // Set-destination mode is armed from the preview toolbar (replaces the old
+ // double-click-on-canvas UX). See createMode effects below.
+ const armed = usePlayerStore((s) => s.motionPathArmed);
+ const setMotionPathArmed = usePlayerStore((s) => s.setMotionPathArmed);
+ const setMotionPathCreateAvailable = usePlayerStore((s) => s.setMotionPathCreateAvailable);
+ const dragRef = useRef<DragState | null>(null);
+ // Park-on-click is debounced so a double-click cancels the seek (see onUp).
+ const parkTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
+ // The animation id whose path is currently editable. Computed at hook level (not
+ // just in render, after the early returns) so the park-timer cleanup can key on
+ // it: a pending park seek belongs to the OLD animation, so firing it after the
+ // active animation changed would jump the playhead onto a stale keyframe.
+ const animId = editableAnimationId(selectedGsapAnimations ?? [], geometry?.kind ?? "linear");
+ // Clear the debounced park timer on unmount AND whenever the active animation id
+ // changes — not unmount-only, or a queued seek from the previous selection still
+ // fires against the new one.
+ useEffect(() => () => clearTimeout(parkTimerRef.current), [animId]);
+
+ // Create mode: a selected element with no positional motion can be given a new
+ // motionPath. Gated on `geometryResolved` so a fresh selection never counts as
+ // "no path" before the first runtime read confirms it (see useMotionPathData).
+ const createMode = geometryResolved && !geometry && Boolean(selection?.element) && !isPlaying;
+ const createSelector = createMode ? selectorFor(selection) : null;
+ const compW = compositionSize?.width ?? null;
+ const canCreate = createMode && hasMotionPathPlugin(iframeRef.current);
+
+ // Publish whether the selected element can take a path so the preview toolbar
+ // shows its "set destination" toggle. Drops to false when this overlay unmounts
+ // or the context changes, so the button never lingers for a stale selection.
+ useEffect(() => {
+ setMotionPathCreateAvailable(Boolean(canCreate));
+ return () => setMotionPathCreateAvailable(false);
+ }, [canCreate, setMotionPathCreateAvailable]);
+
+ // Disarm when set-destination is no longer possible (element gains a path, gets
+ // deselected, or playback starts) so a toggle left on can't fire later.
+ useEffect(() => {
+ if (armed && !canCreate) setMotionPathArmed(false);
+ }, [armed, canCreate, setMotionPathArmed]);
+
+ // While armed, the next canvas press sets the destination (replaces the old
+ // double-click). Scoped to the preview pan-surface in the CAPTURE phase, on
+ // pointerdown, so it fires before the selection/drag handler underneath — a
+ // press on empty canvas would otherwise deselect (and disarm) before a later
+ // click could land. stopPropagation keeps that handler from also running.
+ // fallow-ignore-next-line complexity
+ useEffect(() => {
+ if (!armed || !createSelector || !compW) return;
+ const surface =
+ (iframeRef.current?.ownerDocument?.querySelector(
+ "[data-preview-pan-surface]",
+ ) as HTMLElement | null) ?? null;
+ if (!surface) return;
+ const prevCursor = surface.style.cursor;
+ surface.style.cursor = "crosshair";
+ // fallow-ignore-next-line complexity
+ const onDown = (e: PointerEvent) => {
+ if (e.button !== 0) return; // primary press only
+ const frame = iframeRef.current;
+ if (!frame || !hasMotionPathPlugin(frame)) return;
+ const r = frame.getBoundingClientRect();
+ if (e.clientX < r.left || e.clientX > r.right || e.clientY < r.top || e.clientY > r.bottom) {
+ return;
+ }
+ // Resolve the element LIVE from the current iframe document — the selected
+ // node may be detached after a soft-reload, which would skew home.
+ const live = frame.contentDocument?.querySelector(createSelector);
+ if (!isPreviewHtmlElement(live, frame)) return;
+ e.stopPropagation();
+ e.preventDefault();
+ const sc = r.width / compW;
+ const elHome = elementHome(live);
+ const px = Math.round((e.clientX - r.left) / sc - elHome.x);
+ const py = Math.round((e.clientY - r.top) / sc - elHome.y);
+ const t = Math.round(usePlayerStore.getState().currentTime * 100) / 100;
+ void commitCreatePath(createSelector, t, px, py, commitMutation);
+ setMotionPathArmed(false);
+ };
+ surface.addEventListener("pointerdown", onDown, true);
+ return () => {
+ surface.removeEventListener("pointerdown", onDown, true);
+ surface.style.cursor = prevCursor;
+ };
+ }, [armed, createSelector, compW, iframeRef, commitMutation, setMotionPathArmed]);
+
+ if (!rect || rect.width <= 0 || !compositionSize || compositionSize.width <= 0) return null;
+ // Hide the whole overlay (path + create hint) when the element isn't painted —
+ // same "what you see in the preview" rule as the selection box.
+ if (!visibleInPreview) return null;
+ // No live anchor (element not in the current document) → can't place the path.
+ if (!home) return null;
+
+ if (!geometry) {
+ // Create mode draws nothing by default — the destination is set via the
+ // preview toolbar's "set destination" toggle (no text sprawled over the
+ // canvas). Only while armed do we show a faint ring at the element as a
+ // "click to place" cue (the surface cursor is also crosshair, set above).
+ if (!armed || !canCreate) return null;
+ const sc = rect.width / compositionSize.width;
+ const hr = (NODE_PX / sc) * 1.6;
+ return (
+ <svg
+ className="pointer-events-none absolute z-40"
+ style={{
+ left: rect.left,
+ top: rect.top,
+ width: rect.width,
+ height: rect.height,
+ // Don't clip the ring that extends past the canvas into the gray margin
+ // — only the preview viewport (`[data-preview-pan-surface]`,
+ // overflow-hidden) clips, so overlays reach the edge but never the panels.
+ overflow: "visible",
+ }}
+ viewBox={`0 0 ${compositionSize.width} ${compositionSize.height}`}
+ >
+ <circle
+ cx={home.x}
+ cy={home.y}
+ r={hr}
+ fill="none"
+ strokeWidth={1.5}
+ strokeDasharray="3 3"
+ vectorEffect="non-scaling-stroke"
+ style={{ stroke: ACCENT }}
+ opacity={0.85}
+ />
+ </svg>
+ );
+ }
+
+ const scale = rect.width / compositionSize.width;
+ const nodeR = NODE_PX / scale;
+ const interactive = Boolean(animId) && !isPlaying;
+ // The × "quick remove" badge applies to non-cubic motionPath arcs only (cubic
+ // anchors carry control points we don't synthesize; keyframe paths remove via
+ // the right-click menu instead).
+ const structural = geometry.kind === "arc" && interactive;
+ const removable = structural && geometry.nodes.length > 2;
+ // Click-on-path to insert a node works for both kinds: a motionPath waypoint
+ // (arc paths, including cubic — GSAP recomputes curves around the new point),
+ // or an x/y keyframe (linear paths) at the projected tween-%.
+ const addable = interactive;
+
+ const nodes = draft
+ ? geometry.nodes.map((n, i) => (i === draft.index ? { ...n, x: draft.x, y: draft.y } : n))
+ : geometry.nodes;
+ // ax/ay = absolute composition position (home + offset) for drawing; n.x/n.y
+ // stay offsets so the drag commit writes the right tween values.
+ const abs = nodes.map((n) => ({ ...n, ax: home.x + n.x, ay: home.y + n.y }));
+ const points = abs.map((p) => `${p.ax},${p.ay}`).join(" ");
+ // Map a VIEWPORT pointer to composition space. Use the iframe's LIVE viewport
+ // rect, not `rect` — `rect.left/top` are stored pan-surface-relative (for the
+ // absolute-positioned SVG), so subtracting them from a viewport clientX/Y would
+ // offset the projection by the surface's gutter (panel/toolbar), and the add-
+ // ghost wouldn't track the cursor. `scale` is unaffected (width is stored raw).
+ const clientToComp = (e: React.PointerEvent) => {
+ const vr = iframeRef.current?.getBoundingClientRect();
+ const left = vr ? vr.left : rect.left;
+ const top = vr ? vr.top : rect.top;
+ return { x: (e.clientX - left) / scale, y: (e.clientY - top) / scale };
+ };
+
+ const onDown = (
+ e: React.PointerEvent,
+ index: number,
+ x: number,
+ y: number,
+ ref: MotionNodeRef,
+ ) => {
+ if (!interactive) return;
+ if (e.button !== 0) return; // primary button only — right-click is the context menu
+ e.stopPropagation();
+ (e.target as Element).setPointerCapture(e.pointerId);
+ dragRef.current = {
+ index,
+ startX: e.clientX,
+ startY: e.clientY,
+ initX: x,
+ initY: y,
+ scale,
+ ref,
+ };
+ setDraft({ index, x, y });
+ };
+ const onMove = (e: React.PointerEvent) => {
+ const d = dragRef.current;
+ if (!d) return;
+ setDraft({
+ index: d.index,
+ x: d.initX + (e.clientX - d.startX) / d.scale,
+ y: d.initY + (e.clientY - d.startY) / d.scale,
+ });
+ };
+ // fallow-ignore-next-line complexity
+ const onUp = (e: React.PointerEvent) => {
+ const d = dragRef.current;
+ if (!d) return;
+ dragRef.current = null;
+ setDraft(null);
+ if (!animId) return;
+ const screenDx = e.clientX - d.startX;
+ const screenDy = e.clientY - d.startY;
+ const x = Math.round(d.initX + screenDx / d.scale);
+ const y = Math.round(d.initY + screenDy / d.scale);
+ // Click-vs-drag is decided in SCREEN space, not composition px: the old guard
+ // compared rounded comp-px, which at high zoom (scale ≫ 1) swallowed real
+ // multi-px screen drags whose sub-comp-px delta rounds to 0 → the node would
+ // never move. A screen-distance threshold registers any genuine pointer drag
+ // at any zoom; below it the gesture is a click (select + park the playhead).
+ const movedScreenPx = Math.hypot(screenDx, screenDy);
+ if (movedScreenPx < DRAG_THRESHOLD_PX) {
+ // No drag → treat as a click: select this keyframe and park the playhead on
+ // it. Selecting it makes the next drag MODIFY this keyframe (honored via
+ // activeKeyframePct) instead of creating a new one.
+ if (d.ref.type === "keyframe") {
+ usePlayerStore.getState().setActiveKeyframePct(d.ref.pct);
+ const ref = d.ref;
+ // Debounce the playhead seek: a double-click cancels it (e.detail >= 2),
+ // so only a lone single-click parks the playhead on the keyframe.
+ clearTimeout(parkTimerRef.current);
+ if (e.detail < 2) {
+ parkTimerRef.current = setTimeout(() => {
+ const anim = selectedGsapAnimations?.find((a) => a.id === animId);
+ if (anim) parkPlayheadOnKeyframe(anim, ref.pct);
+ }, 250);
+ }
+ }
+ return; // no commit
+ }
+ // A real drag that still rounds to the same integer comp-px (sub-px move at
+ // high zoom) would commit an identical value — a no-op undo entry. Skip the
+ // commit, but don't treat it as a click either (the user did drag).
+ if (x === Math.round(d.initX) && y === Math.round(d.initY)) return;
+ void commitNode(d.ref, x, y, animId, commitMutation);
+ // Park the playhead on the edited keyframe's time so the element previews AT
+ // that keyframe. Without it, a playhead sitting before the tween renders the
+ // element's base pose — the edit (correct on the path) looks like it vanished.
+ if (d.ref.type === "keyframe") {
+ const anim = selectedGsapAnimations?.find((a) => a.id === animId);
+ if (anim) parkPlayheadOnKeyframe(anim, d.ref.pct);
+ }
+ };
+
+ // Ghost "add" affordance: project the cursor onto the path; click inserts.
+ const onPathHover = (e: React.PointerEvent) => {
+ const c = clientToComp(e);
+ const np = nearestPointOnPath(
+ c.x,
+ c.y,
+ abs.map((p) => ({ x: p.ax, y: p.ay })),
+ );
+ setGhost(np ? { x: np.x, y: np.y, segIndex: np.segIndex } : null);
+ };
+ const onPathDown = (e: React.PointerEvent) => {
+ if (!animId) return;
+ // Compute the insertion point from the event directly so a click works
+ // without (or faster than) a preceding hover.
+ const c = clientToComp(e);
+ const np = nearestPointOnPath(
+ c.x,
+ c.y,
+ abs.map((p) => ({ x: p.ax, y: p.ay })),
+ );
+ if (!np) return;
+ const x = Math.round(np.x - home.x);
+ const y = Math.round(np.y - home.y);
+ if (geometry.kind === "arc") {
+ e.stopPropagation();
+ void commitAddWaypoint(animId, np.segIndex + 1, x, y, commitMutation);
+ } else {
+ // Linear keyframe path: interpolate the new stop's tween-% from the two
+ // keyframes bounding the clicked segment (np.t = fraction along it), then
+ // insert it. Lands ON the current line, so the dot doesn't jump — drag it
+ // after to bend the path.
+ const a = abs[np.segIndex]?.ref;
+ const b = abs[np.segIndex + 1]?.ref;
+ if (a?.type !== "keyframe" || b?.type !== "keyframe") return;
+ const pct = Math.round((a.pct + (b.pct - a.pct) * np.t) * 1000) / 1000;
+ e.stopPropagation();
+ void commitAddKeyframe(animId, pct, x, y, commitMutation);
+ }
+ setGhost(null);
+ };
+ const onRemove = (e: React.PointerEvent, index: number) => {
+ e.stopPropagation();
+ if (!animId) return;
+ setHoverNode(null);
+ void commitRemoveWaypoint(animId, index, commitMutation);
+ };
+
+ const elementId = selection?.id ?? null;
+ // Right-click a keyframe node → the timeline's keyframe context menu (delete
+ // this keyframe / delete all), so motion-path keyframes are removable in place.
+ const onNodeContextMenu = (e: React.MouseEvent, ref: MotionNodeRef) => {
+ if (ref.type !== "keyframe" || !animId || !elementId) return;
+ e.preventDefault();
+ e.stopPropagation();
+ setKfMenu({
+ x: e.clientX,
+ y: e.clientY,
+ elementId,
+ percentage: ref.pct,
+ tweenPercentage: ref.pct,
+ });
+ };
+
+ return (
+ <>
+ <svg
+ className="pointer-events-none absolute z-40"
+ style={{
+ left: rect.left,
+ top: rect.top,
+ width: rect.width,
+ height: rect.height,
+ // Don't clip nodes/path past the canvas into the gray margin — only the
+ // preview viewport (overflow-hidden) clips, so overlays reach the edge
+ // but never the side panels.
+ overflow: "visible",
+ }}
+ viewBox={`0 0 ${compositionSize.width} ${compositionSize.height}`}
+ >
+ {/* Wide transparent hit path drives the add-ghost; drawn under the nodes.
+ Renders for keyframe paths and non-cubic arcs (see `addable`). */}
+ {addable && (
+ <polyline
+ points={points}
+ fill="none"
+ stroke="transparent"
+ strokeWidth={14 / scale}
+ className="pointer-events-auto"
+ style={{ cursor: "copy" }}
+ onPointerMove={onPathHover}
+ onPointerLeave={() => setGhost(null)}
+ onPointerDown={onPathDown}
+ />
+ )}
+ <polyline
+ points={points}
+ fill="none"
+ style={{ stroke: ACCENT }}
+ strokeWidth={1.5}
+ strokeDasharray="5 5"
+ strokeLinejoin="round"
+ strokeLinecap="round"
+ vectorEffect="non-scaling-stroke"
+ opacity={0.85}
+ />
+ {ghost && (
+ <rect
+ x={ghost.x - nodeR * 0.707}
+ y={ghost.y - nodeR * 0.707}
+ width={nodeR * 1.414}
+ height={nodeR * 1.414}
+ rx={nodeR * 0.24}
+ transform={`rotate(45 ${ghost.x} ${ghost.y})`}
+ fill="none"
+ strokeWidth={1.5}
+ strokeDasharray="2 2"
+ vectorEffect="non-scaling-stroke"
+ className="pointer-events-none"
+ style={{ stroke: ACCENT }}
+ />
+ )}
+ {abs.map((p, i) => (
+ <MotionPathNode
+ key={i}
+ cx={p.ax}
+ cy={p.ay}
+ r={nodeR}
+ interactive={interactive}
+ removable={removable && hoverNode === i}
+ grabbing={draft?.index === i}
+ selected={p.ref.type === "keyframe" && p.ref.pct === activeKeyframePct}
+ onEnter={() => setHoverNode(i)}
+ onLeave={() => setHoverNode((h) => (h === i ? null : h))}
+ onPointerDown={(e) => onDown(e, i, p.x, p.y, p.ref)}
+ onPointerMove={onMove}
+ onPointerUp={onUp}
+ onRemove={(e) => onRemove(e, i)}
+ onContextMenu={(e) => onNodeContextMenu(e, p.ref)}
+ />
+ ))}
+ </svg>
+ {kfMenu && (
+ <KeyframeDiamondContextMenu
+ state={kfMenu}
+ onClose={() => setKfMenu(null)}
+ onDelete={(_elId, pct) => animId && handleGsapRemoveKeyframe(animId, pct)}
+ onDeleteAll={(elId) => handleGsapDeleteAllForElement(`#${elId}`)}
+ />
+ )}
+ </>
+ );
+});
diff --git a/packages/studio/src/components/editor/SnapToolbar.tsx b/packages/studio/src/components/editor/SnapToolbar.tsx
index a63264f450..6b75110da9 100644
--- a/packages/studio/src/components/editor/SnapToolbar.tsx
+++ b/packages/studio/src/components/editor/SnapToolbar.tsx
@@ -1,7 +1,7 @@
-// fallow-ignore-file unused-file
import { memo, useCallback, useEffect, useRef, useState } from "react";
-import { MagnetStraight, GridFour } from "@phosphor-icons/react";
+import { MagnetStraight, GridFour, Path } from "@phosphor-icons/react";
import { readStudioUiPreferences, writeStudioUiPreferences } from "../../utils/studioUiPreferences";
+import { usePlayerStore } from "../../player/store/playerStore";
const SNAP_DEFAULTS = {
snapEnabled: true,
@@ -34,6 +34,11 @@ interface SnapToolbarProps {
export const SnapToolbar = memo(function SnapToolbar({ onSnapChange }: SnapToolbarProps) {
const [prefs, setPrefs] = useState(readSnapPrefs);
const [gridPopoverOpen, setGridPopoverOpen] = useState(false);
+ // Motion-path "set destination" toggle — shown only when the selected element
+ // can take a path; arms a single canvas click to place it (MotionPathOverlay).
+ const motionPathCreateAvailable = usePlayerStore((s) => s.motionPathCreateAvailable);
+ const motionPathArmed = usePlayerStore((s) => s.motionPathArmed);
+ const setMotionPathArmed = usePlayerStore((s) => s.setMotionPathArmed);
const popoverRef = useRef<HTMLDivElement>(null);
const gridButtonRef = useRef<HTMLButtonElement>(null);
@@ -89,7 +94,27 @@ export const SnapToolbar = memo(function SnapToolbar({ onSnapChange }: SnapToolb
}, [gridPopoverOpen]);
return (
- <div className="absolute top-2 right-2 z-50 flex items-center gap-1">
+ <div
+ className="absolute top-2 right-2 z-50 flex items-center gap-1"
+ onPointerDown={(e) => e.stopPropagation()}
+ >
+ {motionPathCreateAvailable && (
+ <button
+ type="button"
+ className={`rounded-md p-1.5 transition-colors ${
+ motionPathArmed
+ ? "bg-studio-accent/20 text-studio-accent"
+ : "bg-black/40 text-white/60 hover:bg-black/60 hover:text-white/80"
+ }`}
+ onClick={() => setMotionPathArmed(!motionPathArmed)}
+ title={
+ motionPathArmed ? "Click the canvas to set the destination" : "Set motion destination"
+ }
+ aria-label="Set motion destination"
+ >
+ <Path size={16} weight={motionPathArmed ? "fill" : "regular"} />
+ </button>
+ )}
<button
type="button"
className={`rounded-md p-1.5 transition-colors ${
diff --git a/packages/studio/src/components/editor/SourceEditor.tsx b/packages/studio/src/components/editor/SourceEditor.tsx
index e99c48de79..03e9c6cffe 100644
--- a/packages/studio/src/components/editor/SourceEditor.tsx
+++ b/packages/studio/src/components/editor/SourceEditor.tsx
@@ -6,7 +6,7 @@ import {
highlightActiveLine,
highlightActiveLineGutter,
} from "@codemirror/view";
-import { EditorState } from "@codemirror/state";
+import { EditorState, Annotation } from "@codemirror/state";
import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
import { bracketMatching, foldGutter, indentOnInput } from "@codemirror/language";
import { closeBrackets, closeBracketsKeymap } from "@codemirror/autocomplete";
@@ -18,6 +18,11 @@ import { css } from "@codemirror/lang-css";
import { javascript } from "@codemirror/lang-javascript";
import { markdown } from "@codemirror/lang-markdown";
+// Marks a programmatic doc sync (external content push — e.g. a manual-edit
+// commit writing the source) so the update listener doesn't mistake it for a
+// user keystroke and trigger a re-save + preview reload.
+const ExternalSync = Annotation.define<boolean>();
+
const LANGUAGE_EXTENSIONS: Record<string, () => Extension> = {
html: () => html(),
css: () => css(),
@@ -89,9 +94,10 @@ export const SourceEditor = memo(function SourceEditor({
const lang = language ?? (filePath ? detectLanguage(filePath) : "html");
const updateListener = EditorView.updateListener.of((update) => {
- if (update.docChanged && onChangeRef.current) {
- onChangeRef.current(update.state.doc.toString());
- }
+ if (!update.docChanged || !onChangeRef.current) return;
+ // Ignore programmatic external syncs — only real user edits should save.
+ if (update.transactions.some((tr) => tr.annotation(ExternalSync))) return;
+ onChangeRef.current(update.state.doc.toString());
});
const state = EditorState.create({
@@ -130,11 +136,17 @@ export const SourceEditor = memo(function SourceEditor({
const view = editorRef.current;
if (!view) return;
const current = view.state.doc.toString();
- if (current !== content) {
- view.dispatch({
- changes: { from: 0, to: current.length, insert: content },
- });
- }
+ if (current === content) return;
+ // If the user is actively typing (editor focused), a programmatic replace
+ // would clobber their in-flight keystrokes — the ExternalSync annotation
+ // suppresses onChange, so those edits would be silently lost. Skip the
+ // external sync while focused; it re-runs on the next `content` change after
+ // they blur (or when a later commit lands with the editor unfocused).
+ if (view.hasFocus) return;
+ view.dispatch({
+ changes: { from: 0, to: current.length, insert: content },
+ annotations: [ExternalSync.of(true)],
+ });
}, [content]);
useEffect(() => {
diff --git a/packages/studio/src/components/editor/domEditOverlayGeometry.ts b/packages/studio/src/components/editor/domEditOverlayGeometry.ts
index 991d1f910c..fa1a61107a 100644
--- a/packages/studio/src/components/editor/domEditOverlayGeometry.ts
+++ b/packages/studio/src/components/editor/domEditOverlayGeometry.ts
@@ -25,6 +25,18 @@ export function isElementVisibleForOverlay(el: HTMLElement): boolean {
return isElementVisibleThroughAncestors(el);
}
+// Sample points (as fractions of the element box) for the occlusion hit-test:
+// the four inner corners plus the center. This is a coarse approximation of the
+// element's painted area — we assume a sampled point that lands inside the box also
+// lands on something the element actually paints.
+//
+// LIMITATION: a donut/ring-shaped element (a hole in the middle, content only around
+// the edges) breaks that assumption — the center sample, and even the corner samples,
+// can fall in the transparent hole and hit-test through to whatever is behind, so the
+// element could read as occluded (or as covering) incorrectly. Today's scene element
+// shapes (rectangular cards, text, full-bleed media) don't have interior holes, so this
+// doesn't bite. If ring/cutout shapes become editable targets, sample more densely or
+// hit-test against the element's actual painted geometry instead of its bounding box.
function readPositiveDimension(value: string | null): number | null {
if (!value) return null;
const parsed = Number.parseFloat(value);
diff --git a/packages/studio/src/components/editor/domEditOverlayGestures.ts b/packages/studio/src/components/editor/domEditOverlayGestures.ts
index c2cbf80d8f..d693e974a9 100644
--- a/packages/studio/src/components/editor/domEditOverlayGestures.ts
+++ b/packages/studio/src/components/editor/domEditOverlayGestures.ts
@@ -173,7 +173,11 @@ export type UseDomEditOverlayGesturesOptions = {
onBlockedMoveRef: RefObject<(selection: DomEditSelection) => void>;
onManualDragStartRef: RefObject<(() => void) | undefined>;
onPathOffsetCommitRef: RefObject<
- (s: DomEditSelection, n: { x: number; y: number }) => Promise<void> | void
+ (
+ s: DomEditSelection,
+ n: { x: number; y: number },
+ m?: { altKey?: boolean },
+ ) => Promise<void> | void
>;
onGroupPathOffsetCommitRef: RefObject<
(updates: DomEditGroupPathOffsetCommit[]) => Promise<void> | void
diff --git a/packages/studio/src/components/editor/domEditOverlayStartGesture.ts b/packages/studio/src/components/editor/domEditOverlayStartGesture.ts
index b45d68db8d..f91a8a4347 100644
--- a/packages/studio/src/components/editor/domEditOverlayStartGesture.ts
+++ b/packages/studio/src/components/editor/domEditOverlayStartGesture.ts
@@ -5,6 +5,7 @@
import { type DomEditSelection } from "./domEditing";
import {
createManualOffsetDragMember,
+ readGsapRotation,
restoreManualOffsetDragMembers,
type ManualOffsetDragMember,
} from "./manualOffsetDrag";
@@ -115,7 +116,10 @@ export function startGesture(
return false;
const size = readStudioBoxSize(sel.element);
- const rotation = readStudioRotation(sel.element);
+ // Single-source rotation base = the live GSAP transform rotation plus any legacy
+ // `--hf-studio-rotation` CSS var (old projects), so a rotate gesture starts from the
+ // element's actual visual angle and commits an absolute angle to the timeline.
+ const rotation = { angle: readGsapRotation(sel.element) + readStudioRotation(sel.element).angle };
const actualWidth = size.width > 0 ? size.width : rect.width / rect.editScaleX;
const actualHeight = size.height > 0 ? size.height : rect.height / rect.editScaleY;
let initialPathOffset = captureStudioPathOffset(sel.element);
diff --git a/packages/studio/src/components/editor/domEditingDom.ts b/packages/studio/src/components/editor/domEditingDom.ts
index 7c22b2348b..b4ab83aa36 100644
--- a/packages/studio/src/components/editor/domEditingDom.ts
+++ b/packages/studio/src/components/editor/domEditingDom.ts
@@ -128,18 +128,32 @@ export function findClosestByAttribute(
}
return null;
}
+// ─── Composition source resolution ───────────────────────────────────────────
-export function getElementDepth(el: HTMLElement): number {
- let depth = 0;
- let current = el.parentElement;
- while (current) {
- depth += 1;
- current = current.parentElement;
- }
- return depth;
+// The runtime INLINES subcompositions and strips the source-file linkage from the
+// mounted root (it keeps `data-composition-id` but drops `data-composition-src`/
+// `-file`), so a subcomp element's DOM ancestors no longer say which file it came
+// from. This project-global map (composition-id → source file, built once from
+// index.html's clips — see NLELayout) recovers it. The studio loads one project at a
+// time, so module scope is the right lifetime; it's empty until set, in which case
+// resolution falls back to the historical attribute-only behavior.
+let compositionSourceMap: Map<string, string> = new Map();
+
+export function setCompositionSourceMap(map: Map<string, string>): void {
+ compositionSourceMap = map;
}
-// ─── Composition source resolution ───────────────────────────────────────────
+function sourceFromCompositionId(ownerRoot: HTMLElement | null): string | undefined {
+ if (!ownerRoot || compositionSourceMap.size === 0) return undefined;
+ // The runtime may rename the mounted id to a runtime-unique one, preserving the
+ // authored id on `data-hf-original-composition-id` — prefer that, then the current id.
+ const authored = ownerRoot.getAttribute("data-hf-original-composition-id");
+ const current = ownerRoot.getAttribute("data-composition-id");
+ return (
+ (authored ? compositionSourceMap.get(authored) : undefined) ??
+ (current ? compositionSourceMap.get(current) : undefined)
+ );
+}
export function getSourceFileForElement(
el: HTMLElement,
@@ -152,6 +166,7 @@ export function getSourceFileForElement(
sourceHost?.getAttribute("data-composition-src") ??
ownerRoot?.getAttribute("data-composition-file") ??
ownerRoot?.getAttribute("data-composition-src") ??
+ sourceFromCompositionId(ownerRoot) ??
activeCompositionPath ??
"index.html";
diff --git a/packages/studio/src/components/editor/manualEditingAvailability.test.ts b/packages/studio/src/components/editor/manualEditingAvailability.test.ts
index 4f86a20d96..4c2b57e3bd 100644
--- a/packages/studio/src/components/editor/manualEditingAvailability.test.ts
+++ b/packages/studio/src/components/editor/manualEditingAvailability.test.ts
@@ -24,11 +24,6 @@ describe("manual editing availability", () => {
expect(availability.STUDIO_INSPECTOR_PANELS_ENABLED).toBe(true);
});
- it("enables GSAP drag intercept by default", async () => {
- const availability = await loadAvailabilityWithEnv({});
- expect(availability.STUDIO_GSAP_DRAG_INTERCEPT_ENABLED).toBe(true);
- });
-
it("keeps color grading off by default", async () => {
const availability = await loadAvailabilityWithEnv({});
expect(availability.STUDIO_COLOR_GRADING_ENABLED).toBe(false);
@@ -41,13 +36,6 @@ describe("manual editing availability", () => {
expect(availability.STUDIO_COLOR_GRADING_ENABLED).toBe(true);
});
- it("disables GSAP drag intercept when env var is false", async () => {
- const availability = await loadAvailabilityWithEnv({
- VITE_STUDIO_ENABLE_GSAP_DRAG_INTERCEPT: "false",
- });
- expect(availability.STUDIO_GSAP_DRAG_INTERCEPT_ENABLED).toBe(false);
- });
-
it("disables preview selection when the inspector panel flag is explicitly off", async () => {
const availability = await loadAvailabilityWithEnv({
VITE_STUDIO_ENABLE_INSPECTOR_PANELS: "0",
diff --git a/packages/studio/src/components/editor/manualEditingAvailability.ts b/packages/studio/src/components/editor/manualEditingAvailability.ts
index fc2132aa82..020de0b853 100644
--- a/packages/studio/src/components/editor/manualEditingAvailability.ts
+++ b/packages/studio/src/components/editor/manualEditingAvailability.ts
@@ -92,16 +92,6 @@ export const STUDIO_STORYBOARD_ENABLED = resolveStudioBooleanEnvFlag(
false,
);
-// When disabled (the default), drag/resize/rotate commits always take the CSS
-// persist path instead of being intercepted into GSAP script keyframe
-// mutations. The keyframe intercept rewrites timeline tweens from drag
-// gestures and is opt-in until its recording path is hardened.
-export const STUDIO_GSAP_DRAG_INTERCEPT_ENABLED = resolveStudioBooleanEnvFlag(
- env,
- ["VITE_STUDIO_ENABLE_GSAP_DRAG_INTERCEPT", "VITE_STUDIO_GSAP_DRAG_INTERCEPT_ENABLED"],
- true,
-);
-
export const STUDIO_PREVIEW_SELECTION_ENABLED = STUDIO_INSPECTOR_PANELS_ENABLED;
// Stage 7 Step 3c: SDK cutover — routes inline-style ops through SDK dispatch
diff --git a/packages/studio/src/components/editor/manualEdits.ts b/packages/studio/src/components/editor/manualEdits.ts
index 5c0e01be22..61e735323d 100644
--- a/packages/studio/src/components/editor/manualEdits.ts
+++ b/packages/studio/src/components/editor/manualEdits.ts
@@ -16,6 +16,7 @@ export {
endStudioManualEditGesture,
isStudioManualEditGestureCurrent,
readStudioPathOffset,
+ readAppliedStudioPathOffset,
readStudioBoxSize,
readStudioRotation,
applyStudioPathOffset,
diff --git a/packages/studio/src/components/editor/manualEditsDom.ts b/packages/studio/src/components/editor/manualEditsDom.ts
index 98d05bde31..ea0ee2cbc4 100644
--- a/packages/studio/src/components/editor/manualEditsDom.ts
+++ b/packages/studio/src/components/editor/manualEditsDom.ts
@@ -70,6 +70,19 @@ export function readStudioPathOffset(element: HTMLElement): { x: number; y: numb
};
}
+/**
+ * The path offset ACTUALLY applied right now. The `--hf-studio-offset` vars can
+ * linger after GSAP re-bakes the element's transform (`translate:"none"`), so the
+ * raw var isn't a safe drag base — using it re-commits a phantom offset and flings
+ * the element off-screen. The offset only counts when the inline `translate` is the
+ * studio var-translate; otherwise it's dormant and the applied offset is zero.
+ */
+export function readAppliedStudioPathOffset(element: HTMLElement): { x: number; y: number } {
+ return (element.style.translate || "").includes(STUDIO_OFFSET_X_PROP)
+ ? readStudioPathOffset(element)
+ : { x: 0, y: 0 };
+}
+
export function readStudioBoxSize(element: HTMLElement): { width: number; height: number } {
return {
width: readPxCustomProperty(element, STUDIO_WIDTH_PROP),
@@ -250,6 +263,50 @@ function stripGsapTranslateFromTransform(element: HTMLElement): void {
}
}
+// GSAP owns the element's `transform` (it bakes x/y into a matrix and writes
+// `translate: none` every tick). Folding the drag offset into a CSS `translate`
+// — as the non-GSAP path does — composes ON TOP of GSAP's transform, and the
+// subsequent strip/reapply math compounds into a runaway matrix that flings the
+// element off-canvas. So for GSAP-animated elements we keep `translate: none`
+// and push the offset straight into GSAP's x/y via gsap.set; the var() offset is
+// still persisted (buildPathOffsetPatches), and GSAP re-reads it at init on
+// reload. Returns true when handled as GSAP (caller must skip the CSS path).
+function applyStudioPathOffsetViaGsap(
+ element: HTMLElement,
+ offset: { x: number; y: number },
+): boolean {
+ if (!gsapAnimatesProperty(element, "x", "y")) return false;
+ element.style.setProperty("translate", "none");
+ const win = element.ownerDocument.defaultView as
+ | (Window & {
+ gsap?: {
+ set: (el: Element, vars: Record<string, unknown>) => void;
+ getProperty: (el: Element, prop: string) => number;
+ };
+ })
+ | null;
+ if (win?.gsap) {
+ const baseX = Number.parseFloat(element.getAttribute("data-hf-drag-gsap-base-x") ?? "");
+ const baseY = Number.parseFloat(element.getAttribute("data-hf-drag-gsap-base-y") ?? "");
+ const origX = Number.parseFloat(element.getAttribute("data-hf-drag-initial-offset-x") ?? "");
+ const origY = Number.parseFloat(element.getAttribute("data-hf-drag-initial-offset-y") ?? "");
+ const gsapBaseX = Number.isFinite(baseX)
+ ? baseX
+ : (win.gsap.getProperty(element, "x") as number);
+ const gsapBaseY = Number.isFinite(baseY)
+ ? baseY
+ : (win.gsap.getProperty(element, "y") as number);
+ if (!Number.isFinite(baseX))
+ element.setAttribute("data-hf-drag-gsap-base-x", String(gsapBaseX));
+ if (!Number.isFinite(baseY))
+ element.setAttribute("data-hf-drag-gsap-base-y", String(gsapBaseY));
+ const deltaX = offset.x - (Number.isFinite(origX) ? origX : 0);
+ const deltaY = offset.y - (Number.isFinite(origY) ? origY : 0);
+ win.gsap.set(element, { x: gsapBaseX + deltaX, y: gsapBaseY + deltaY });
+ }
+ return true;
+}
+
export function applyStudioPathOffset(
element: HTMLElement,
offset: { x: number; y: number },
@@ -257,6 +314,10 @@ export function applyStudioPathOffset(
): void {
promoteInlineForTransform(element);
writeStudioPathOffsetVars(element, offset, { updateBase: options.updateBase ?? true });
+ // GSAP elements: route through gsap.set, NOT a CSS translate (would corrupt the
+ // matrix). Symmetrical with applyStudioPathOffsetDraft — the commit path used to
+ // skip this branch, which is what flung dragged GSAP elements off-canvas.
+ if (applyStudioPathOffsetViaGsap(element, offset)) return;
element.style.setProperty(
"translate",
composeTranslateValue(
@@ -274,45 +335,13 @@ export function applyStudioPathOffsetDraft(
): void {
promoteInlineForTransform(element);
writeStudioPathOffsetVars(element, offset, { updateBase: false });
-
- const isGsapAnimated = gsapAnimatesProperty(element, "x", "y");
- if (isGsapAnimated) {
- element.style.setProperty("translate", "none");
- const win = element.ownerDocument.defaultView as
- | (Window & {
- gsap?: {
- set: (el: Element, vars: Record<string, unknown>) => void;
- getProperty: (el: Element, prop: string) => number;
- };
- })
- | null;
- if (win?.gsap) {
- const baseX = Number.parseFloat(element.getAttribute("data-hf-drag-gsap-base-x") ?? "");
- const baseY = Number.parseFloat(element.getAttribute("data-hf-drag-gsap-base-y") ?? "");
- const origX = Number.parseFloat(element.getAttribute("data-hf-drag-initial-offset-x") ?? "");
- const origY = Number.parseFloat(element.getAttribute("data-hf-drag-initial-offset-y") ?? "");
- const gsapBaseX = Number.isFinite(baseX)
- ? baseX
- : (win.gsap.getProperty(element, "x") as number);
- const gsapBaseY = Number.isFinite(baseY)
- ? baseY
- : (win.gsap.getProperty(element, "y") as number);
- if (!Number.isFinite(baseX))
- element.setAttribute("data-hf-drag-gsap-base-x", String(gsapBaseX));
- if (!Number.isFinite(baseY))
- element.setAttribute("data-hf-drag-gsap-base-y", String(gsapBaseY));
- const deltaX = offset.x - (Number.isFinite(origX) ? origX : 0);
- const deltaY = offset.y - (Number.isFinite(origY) ? origY : 0);
- win.gsap.set(element, { x: gsapBaseX + deltaX, y: gsapBaseY + deltaY });
- }
- } else {
- // Non-GSAP elements: use CSS translate as before.
- element.style.setProperty(
- "translate",
- composeTranslateValue(element, `${Math.round(offset.x)}px`, `${Math.round(offset.y)}px`),
- );
- stripGsapTranslateFromTransform(element);
- }
+ if (applyStudioPathOffsetViaGsap(element, offset)) return;
+ // Non-GSAP elements: use CSS translate as before.
+ element.style.setProperty(
+ "translate",
+ composeTranslateValue(element, `${Math.round(offset.x)}px`, `${Math.round(offset.y)}px`),
+ );
+ stripGsapTranslateFromTransform(element);
}
/* ── Box size apply ───────────────────────────────────────────────── */
diff --git a/packages/studio/src/components/editor/manualEditsDomGsap.test.ts b/packages/studio/src/components/editor/manualEditsDomGsap.test.ts
new file mode 100644
index 0000000000..9800e556ab
--- /dev/null
+++ b/packages/studio/src/components/editor/manualEditsDomGsap.test.ts
@@ -0,0 +1,82 @@
+// @vitest-environment jsdom
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { applyStudioPathOffset, applyStudioPathOffsetDraft } from "./manualEditsDom";
+
+/**
+ * Regression: dragging a GSAP-animated element (e.g. a flat `to(#el, {x})` tween)
+ * must NOT fold the offset into a CSS `translate`. GSAP owns `style.transform`, so
+ * a CSS translate composes on top of it and the strip/reapply math compounds into
+ * a runaway matrix that flings the element off-canvas. Both the live draft and the
+ * commit must instead push the offset into GSAP's x/y via gsap.set and keep
+ * `translate: none`. Before the fix, the commit (applyStudioPathOffset) skipped the
+ * GSAP branch the draft already had — that asymmetry caused the off-canvas jump.
+ */
+
+function makeGsapWindow(
+ el: HTMLElement,
+ gsapSet: (e: Element, v: Record<string, unknown>) => void,
+) {
+ const win = el.ownerDocument.defaultView as unknown as {
+ __timelines?: Record<string, unknown>;
+ gsap?: unknown;
+ };
+ win.__timelines = {
+ playground: {
+ getChildren: () => [{ targets: () => [el], vars: { x: -260 } }],
+ },
+ };
+ win.gsap = {
+ set: gsapSet,
+ getProperty: () => 0,
+ };
+}
+
+afterEach(() => {
+ const win = window as unknown as { __timelines?: unknown; gsap?: unknown };
+ delete win.__timelines;
+ delete win.gsap;
+});
+
+describe("applyStudioPathOffset — GSAP-owned transform", () => {
+ it("non-GSAP element folds the offset into a CSS translate var()", () => {
+ const el = document.createElement("div");
+ document.body.appendChild(el);
+
+ applyStudioPathOffset(el, { x: -120, y: 40 });
+
+ expect(el.style.translate).toContain("var(--hf-studio-offset-x");
+ expect(el.style.getPropertyValue("--hf-studio-offset-x")).toBe("-120px");
+ expect(el.style.getPropertyValue("--hf-studio-offset-y")).toBe("40px");
+ });
+
+ it("GSAP element keeps translate:none and routes the offset through gsap.set", () => {
+ const el = document.createElement("div");
+ el.id = "puck-a";
+ document.body.appendChild(el);
+ const gsapSet = vi.fn();
+ makeGsapWindow(el, gsapSet);
+
+ applyStudioPathOffset(el, { x: -409, y: 398 });
+
+ // No CSS translate to collide with GSAP's transform.
+ expect(el.style.translate).toBe("none");
+ expect(el.style.translate).not.toContain("var(");
+ // Offset pushed into GSAP's x/y (gsapBase 0 + delta = the offset itself here).
+ expect(gsapSet).toHaveBeenCalledWith(el, { x: -409, y: 398 });
+ });
+
+ it("draft and commit treat a GSAP element identically (translate:none)", () => {
+ const el = document.createElement("div");
+ el.id = "puck-a";
+ document.body.appendChild(el);
+ makeGsapWindow(el, vi.fn());
+
+ applyStudioPathOffsetDraft(el, { x: -50, y: 10 });
+ const draftTranslate = el.style.translate;
+ applyStudioPathOffset(el, { x: -50, y: 10 });
+ const commitTranslate = el.style.translate;
+
+ expect(draftTranslate).toBe("none");
+ expect(commitTranslate).toBe("none");
+ });
+});
diff --git a/packages/studio/src/components/editor/manualOffsetDrag.test.ts b/packages/studio/src/components/editor/manualOffsetDrag.test.ts
index 6c2a449865..a4c633a441 100644
--- a/packages/studio/src/components/editor/manualOffsetDrag.test.ts
+++ b/packages/studio/src/components/editor/manualOffsetDrag.test.ts
@@ -193,6 +193,12 @@ describe("createManualOffsetDragMember uses raw CSS var offset", () => {
element.style.setProperty(STUDIO_OFFSET_X_PROP, "30px");
element.style.setProperty(STUDIO_OFFSET_Y_PROP, "10px");
+ // Old projects bake the offset by referencing the vars in the inline
+ // `translate` longhand — that's what makes the offset "applied" and thus the
+ // valid drag base (readAppliedStudioPathOffset). A raw var with no applied
+ // translate is dormant and reads as zero. Assign the typed `.translate`
+ // accessor (happy-dom doesn't surface it via setProperty).
+ element.style.translate = `var(${STUDIO_OFFSET_X_PROP}, 0px) var(${STUDIO_OFFSET_Y_PROP}, 0px)`;
element.style.setProperty("transform", "translate(50px, -15px)");
element.getBoundingClientRect = () => {
@@ -228,6 +234,12 @@ describe("createManualOffsetDragMember uses raw CSS var offset", () => {
// Simulate GSAP baking a translate into transform each cycle
for (let cycle = 0; cycle < 3; cycle++) {
element.style.setProperty("transform", `translate(${50 * (cycle + 1)}px, 0px)`);
+ // Mark the offset as APPLIED (the inline translate references the studio
+ // vars, the form an old project bakes) so readAppliedStudioPathOffset reads
+ // the var, not zero. Without this the var is dormant and reads as zero.
+ // Assign the typed `.translate` accessor (happy-dom doesn't surface it via
+ // setProperty).
+ element.style.translate = `var(${STUDIO_OFFSET_X_PROP}, 0px) var(${STUDIO_OFFSET_Y_PROP}, 0px)`;
const result = createManualOffsetDragMember({
key: "test",
diff --git a/packages/studio/src/components/editor/manualOffsetDrag.ts b/packages/studio/src/components/editor/manualOffsetDrag.ts
index d6ac554d02..9cfe694f0b 100644
--- a/packages/studio/src/components/editor/manualOffsetDrag.ts
+++ b/packages/studio/src/components/editor/manualOffsetDrag.ts
@@ -5,10 +5,71 @@ import {
beginStudioManualEditGesture,
captureStudioPathOffset,
endStudioManualEditGesture,
- readStudioPathOffset,
+ readAppliedStudioPathOffset,
restoreStudioPathOffset,
type StudioPathOffsetSnapshot,
} from "./manualEdits";
+import { computeDraggedGsapPosition } from "../../hooks/draggedGsapPosition";
+
+interface OffsetDragGsap {
+ set: (el: Element, vars: Record<string, number | string>) => void;
+ getProperty: (el: Element, prop: string) => number;
+}
+
+function getOffsetDragGsap(element: HTMLElement): OffsetDragGsap | null {
+ const win = element.ownerDocument.defaultView as
+ | (Window & { gsap?: Partial<OffsetDragGsap> })
+ | null;
+ const gsap = win?.gsap;
+ return gsap?.set && gsap.getProperty ? (gsap as OffsetDragGsap) : null;
+}
+
+/**
+ * Live drag preview through the GSAP channel — the SAME channel the commit
+ * lands in (a `tl.set`/keyframe on the timeline), so what the user sees while
+ * dragging equals what gets written (plan R3/R4). Reuses the commit's
+ * base+delta+rotation math so preview and commit agree by construction. Returns
+ * true when handled via gsap; false when gsap is unavailable (caller falls back
+ * to the CSS draft).
+ */
+function applyOffsetDragDraftViaGsap(
+ element: HTMLElement,
+ offset: { x: number; y: number },
+): boolean {
+ const gsap = getOffsetDragGsap(element);
+ if (!gsap) return false;
+ // GSAP owns the transform; neutralize the CSS translate longhand so the two
+ // channels can't compose into a doubled position.
+ element.style.setProperty("translate", "none");
+ const fallbackBase = {
+ x: Number(gsap.getProperty(element, "x")) || 0,
+ y: Number(gsap.getProperty(element, "y")) || 0,
+ };
+ const { newX, newY } = computeDraggedGsapPosition(element, offset, fallbackBase);
+ gsap.set(element, { x: newX, y: newY });
+ return true;
+}
+
+/**
+ * Live rotation preview through the GSAP channel — the SAME channel the commit
+ * lands in (a `tl.set`/keyframe rotation), mirroring `applyOffsetDragDraftViaGsap`.
+ * GSAP owns the transform rotation, so neutralize the CSS `rotate` longhand to keep
+ * the two channels from composing. `angle` is the absolute target rotation. Returns
+ * false when gsap is unavailable (caller falls back to the CSS draft).
+ */
+export function applyRotationDraftViaGsap(element: HTMLElement, angle: number): boolean {
+ const gsap = getOffsetDragGsap(element);
+ if (!gsap) return false;
+ element.style.setProperty("rotate", "none");
+ gsap.set(element, { rotation: angle });
+ return true;
+}
+
+/** Current GSAP transform rotation — the single-source rotation base. 0 if gsap is unavailable. */
+export function readGsapRotation(element: HTMLElement): number {
+ const gsap = getOffsetDragGsap(element);
+ return gsap ? Number(gsap.getProperty(element, "rotation")) || 0 : 0;
+}
const DEFAULT_OFFSET_PROBE_PX = 100;
const MIN_PROBE_VECTOR_LENGTH_PX = 0.01;
@@ -241,7 +302,10 @@ export function createManualOffsetDragMember(input: {
element: HTMLElement;
rect: ManualOffsetDragRect;
}): ManualOffsetDragMemberResult {
- const initialOffset = readStudioPathOffset(input.element);
+ // Base the drag on the offset ACTUALLY applied, never the raw (possibly dormant)
+ // var — see readAppliedStudioPathOffset. This keeps the commit purely relative
+ // (applied + delta) so a stale offset can't fling the element off-screen.
+ const initialOffset = readAppliedStudioPathOffset(input.element);
input.element.setAttribute("data-hf-drag-initial-offset-x", String(initialOffset.x));
input.element.setAttribute("data-hf-drag-initial-offset-y", String(initialOffset.y));
@@ -335,7 +399,12 @@ export function applyManualOffsetDragDraft(
dy: number,
): { x: number; y: number } {
const offset = resolveManualOffsetDragMemberOffset(member, dx, dy);
- applyStudioPathOffsetDraft(member.element, offset);
+ // Position is single-sourced on the GSAP timeline; preview through gsap.set so
+ // the live draft matches the committed `tl.set`/keyframe. CSS draft only when
+ // gsap is unavailable (no preview iframe runtime).
+ if (!applyOffsetDragDraftViaGsap(member.element, offset)) {
+ applyStudioPathOffsetDraft(member.element, offset);
+ }
return offset;
}
@@ -345,7 +414,13 @@ export function applyManualOffsetDragCommit(
dy: number,
): { x: number; y: number } {
const offset = resolveManualOffsetDragMemberOffset(member, dx, dy);
- applyStudioPathOffset(member.element, offset);
+ // Optimistic visual through the GSAP channel (same as the live draft and the
+ // committed `tl.set`), so the element holds its dropped position until the
+ // source mutation soft-reloads — no transient CSS `--hf-studio-offset` write.
+ // CSS apply only when gsap is unavailable.
+ if (!applyOffsetDragDraftViaGsap(member.element, offset)) {
+ applyStudioPathOffset(member.element, offset);
+ }
return offset;
}
diff --git a/packages/studio/src/components/editor/motionPathCommit.test.ts b/packages/studio/src/components/editor/motionPathCommit.test.ts
new file mode 100644
index 0000000000..6de7acdba9
--- /dev/null
+++ b/packages/studio/src/components/editor/motionPathCommit.test.ts
@@ -0,0 +1,130 @@
+import { describe, it, expect, vi } from "vitest";
+import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
+import { editableAnimationId } from "./motionPathSelection";
+import {
+ commitNode,
+ commitAddWaypoint,
+ commitAddKeyframe,
+ commitRemoveWaypoint,
+ commitCreatePath,
+} from "./motionPathCommit";
+
+const anim = (over: Partial<GsapAnimation>): GsapAnimation =>
+ ({
+ id: "a1",
+ targetSelector: "#el",
+ method: "to",
+ position: 0,
+ properties: {},
+ ...over,
+ }) as GsapAnimation;
+
+describe("editableAnimationId", () => {
+ it("picks the arc animation for an arc path", () => {
+ const arc = anim({ id: "arc1", arcPath: { enabled: true, autoRotate: false, segments: [] } });
+ expect(editableAnimationId([anim({ id: "other" }), arc], "arc")).toBe("arc1");
+ });
+
+ it("picks a position-keyframe animation for a linear path", () => {
+ const kf = anim({
+ id: "kf1",
+ propertyGroup: "position",
+ keyframes: {
+ format: "percentage",
+ keyframes: [{ percentage: 0, properties: { x: 0, y: 0 } }],
+ } as never,
+ });
+ expect(editableAnimationId([kf], "linear")).toBe("kf1");
+ });
+
+ it("returns null for dynamic (unresolved) tweens — read-only", () => {
+ const dyn = anim({
+ id: "dyn",
+ arcPath: { enabled: true, autoRotate: false, segments: [] },
+ hasUnresolvedKeyframes: true,
+ });
+ expect(editableAnimationId([dyn], "arc")).toBeNull();
+ });
+
+ it("returns null for non-literal (helper) provenance — read-only", () => {
+ const helper = anim({
+ id: "h",
+ arcPath: { enabled: true, autoRotate: false, segments: [] },
+ provenance: { kind: "helper" } as never,
+ });
+ expect(editableAnimationId([helper], "arc")).toBeNull();
+ });
+
+ it("returns null when nothing matches", () => {
+ expect(editableAnimationId([anim({ id: "x" })], "linear")).toBeNull();
+ });
+});
+
+describe("commitNode", () => {
+ it("routes a keyframe node to update-keyframe by percentage", async () => {
+ const commit = vi.fn().mockResolvedValue(undefined);
+ await commitNode({ type: "keyframe", pct: 50 }, 120, 30, "a1", commit);
+ expect(commit).toHaveBeenCalledWith(
+ { type: "update-keyframe", animationId: "a1", percentage: 50, properties: { x: 120, y: 30 } },
+ expect.objectContaining({ softReload: true }),
+ );
+ });
+
+ it("routes a waypoint node to update-motion-path-point by index", async () => {
+ const commit = vi.fn().mockResolvedValue(undefined);
+ await commitNode({ type: "waypoint", index: 2 }, 80, 40, "a1", commit);
+ expect(commit).toHaveBeenCalledWith(
+ { type: "update-motion-path-point", animationId: "a1", pointIndex: 2, x: 80, y: 40 },
+ expect.objectContaining({ softReload: true }),
+ );
+ });
+});
+
+describe("commitAddWaypoint / commitRemoveWaypoint", () => {
+ it("adds a waypoint at an index with coordinates", async () => {
+ const commit = vi.fn().mockResolvedValue(undefined);
+ await commitAddWaypoint("a1", 1, 120, -40, commit);
+ expect(commit).toHaveBeenCalledWith(
+ { type: "add-motion-path-point", animationId: "a1", index: 1, x: 120, y: -40 },
+ expect.objectContaining({ softReload: true }),
+ );
+ });
+
+ it("removes a waypoint by index", async () => {
+ const commit = vi.fn().mockResolvedValue(undefined);
+ await commitRemoveWaypoint("a1", 2, commit);
+ expect(commit).toHaveBeenCalledWith(
+ { type: "remove-motion-path-point", animationId: "a1", index: 2 },
+ expect.objectContaining({ softReload: true }),
+ );
+ });
+});
+
+describe("commitAddKeyframe", () => {
+ it("inserts an x/y keyframe at a tween-relative percentage", async () => {
+ const commit = vi.fn().mockResolvedValue(undefined);
+ await commitAddKeyframe("a1", 42.5, 80, -20, commit);
+ expect(commit).toHaveBeenCalledWith(
+ { type: "add-keyframe", animationId: "a1", percentage: 42.5, properties: { x: 80, y: -20 } },
+ expect.objectContaining({ softReload: true }),
+ );
+ });
+});
+
+describe("commitCreatePath", () => {
+ it("authors a new motionPath to a destination at a given time", async () => {
+ const commit = vi.fn().mockResolvedValue(undefined);
+ await commitCreatePath("#title", 2.0, 300, -120, commit);
+ expect(commit).toHaveBeenCalledWith(
+ {
+ type: "add-motion-path",
+ targetSelector: "#title",
+ position: 2.0,
+ duration: 1.5,
+ x: 300,
+ y: -120,
+ },
+ expect.objectContaining({ softReload: true }),
+ );
+ });
+});
diff --git a/packages/studio/src/components/editor/motionPathCommit.ts b/packages/studio/src/components/editor/motionPathCommit.ts
new file mode 100644
index 0000000000..53a7c0fcf9
--- /dev/null
+++ b/packages/studio/src/components/editor/motionPathCommit.ts
@@ -0,0 +1,83 @@
+/**
+ * Commit helpers for the motion-path overlay. Each maps a canvas gesture to a
+ * GSAP source mutation routed through the (selection-bound) commit facade, which
+ * handles the soft reload, undo snapshot, and save-failure feedback.
+ */
+import type { MotionNodeRef } from "./motionPathGeometry";
+
+export type CommitFn = (
+ mutation: Record<string, unknown>,
+ options: { label: string; softReload?: boolean },
+) => Promise<void>;
+
+const NEW_PATH_DURATION = 1.5;
+
+export function commitNode(
+ ref: MotionNodeRef,
+ x: number,
+ y: number,
+ animationId: string,
+ commit: CommitFn,
+): Promise<void> {
+ const mutation: Record<string, unknown> =
+ ref.type === "keyframe"
+ ? { type: "update-keyframe", animationId, percentage: ref.pct, properties: { x, y } }
+ : { type: "update-motion-path-point", animationId, pointIndex: ref.index, x, y };
+ return commit(mutation, {
+ label: ref.type === "keyframe" ? "Move keyframe" : "Move waypoint",
+ softReload: true,
+ });
+}
+
+export function commitAddWaypoint(
+ animationId: string,
+ index: number,
+ x: number,
+ y: number,
+ commit: CommitFn,
+): Promise<void> {
+ return commit(
+ { type: "add-motion-path-point", animationId, index, x, y },
+ { label: "Add waypoint", softReload: true },
+ );
+}
+
+export function commitAddKeyframe(
+ animationId: string,
+ percentage: number,
+ x: number,
+ y: number,
+ commit: CommitFn,
+): Promise<void> {
+ // percentage is tween-relative (matches MotionNodeRef.keyframe.pct). The parser's
+ // addKeyframeToScript inserts a new "P%": { x, y } stop (or merges if one exists
+ // at that pct) and converts a flat tween to keyframes form when needed.
+ return commit(
+ { type: "add-keyframe", animationId, percentage, properties: { x, y } },
+ { label: "Add keyframe", softReload: true },
+ );
+}
+
+export function commitRemoveWaypoint(
+ animationId: string,
+ index: number,
+ commit: CommitFn,
+): Promise<void> {
+ return commit(
+ { type: "remove-motion-path-point", animationId, index },
+ { label: "Remove waypoint", softReload: true },
+ );
+}
+
+export function commitCreatePath(
+ targetSelector: string,
+ position: number,
+ x: number,
+ y: number,
+ commit: CommitFn,
+): Promise<void> {
+ return commit(
+ { type: "add-motion-path", targetSelector, position, duration: NEW_PATH_DURATION, x, y },
+ { label: "Create motion path", softReload: true },
+ );
+}
diff --git a/packages/studio/src/components/editor/motionPathGeometry.test.ts b/packages/studio/src/components/editor/motionPathGeometry.test.ts
new file mode 100644
index 0000000000..fc66966836
--- /dev/null
+++ b/packages/studio/src/components/editor/motionPathGeometry.test.ts
@@ -0,0 +1,127 @@
+import { describe, it, expect } from "vitest";
+import { buildMotionPathGeometry, nearestPointOnPath } from "./motionPathGeometry";
+import type { ReadTween } from "../../hooks/gsapRuntimeKeyframes";
+
+const kf = (percentage: number, x: number, y: number) => ({ percentage, properties: { x, y } });
+
+describe("buildMotionPathGeometry", () => {
+ it("builds a linear path with keyframe-ref nodes from an x/y tween", () => {
+ const read: ReadTween = { keyframes: [kf(0, 10, 20), kf(100, 200, 80)] };
+ const geo = buildMotionPathGeometry(read);
+ expect(geo).not.toBeNull();
+ expect(geo!.kind).toBe("linear");
+ expect(geo!.points).toBe("10,20 200,80");
+ expect(geo!.nodes).toEqual([
+ { x: 10, y: 20, ref: { type: "keyframe", pct: 0 } },
+ { x: 200, y: 80, ref: { type: "keyframe", pct: 100 } },
+ ]);
+ });
+
+ it("preserves order and percentages for intermediate keyframes", () => {
+ const read: ReadTween = { keyframes: [kf(0, 0, 0), kf(50, 50, 90), kf(100, 100, 0)] };
+ const geo = buildMotionPathGeometry(read);
+ expect(geo!.nodes.map((n) => n.ref)).toEqual([
+ { type: "keyframe", pct: 0 },
+ { type: "keyframe", pct: 50 },
+ { type: "keyframe", pct: 100 },
+ ]);
+ });
+
+ it("builds an arc path with waypoint-index refs when arcPath is present", () => {
+ const read: ReadTween = {
+ keyframes: [kf(0, 0, 0), kf(50, 60, 40), kf(100, 120, 10)],
+ arcPath: { enabled: true, autoRotate: false, segments: [{ curviness: 1 }, { curviness: 1 }] },
+ };
+ const geo = buildMotionPathGeometry(read);
+ expect(geo!.kind).toBe("arc");
+ expect(geo!.nodes.map((n) => n.ref)).toEqual([
+ { type: "waypoint", index: 0 },
+ { type: "waypoint", index: 1 },
+ { type: "waypoint", index: 2 },
+ ]);
+ });
+
+ it("returns null for a tween with no positional keyframes", () => {
+ const read: ReadTween = {
+ keyframes: [
+ { percentage: 0, properties: { opacity: 0 } },
+ { percentage: 100, properties: { opacity: 1 } },
+ ],
+ };
+ expect(buildMotionPathGeometry(read)).toBeNull();
+ });
+
+ it("draws a single-axis (x-only) tween, defaulting the missing axis to 0", () => {
+ // Regression: an `x`-only tween (e.g. `to({ x: -260 })`) carries no `y`, so the
+ // builder used to skip every node → no path until the user added the 2nd axis.
+ const read: ReadTween = {
+ keyframes: [
+ { percentage: 0, properties: { x: 0 } },
+ { percentage: 100, properties: { x: -260 } },
+ ],
+ };
+ const geo = buildMotionPathGeometry(read);
+ expect(geo).not.toBeNull();
+ expect(geo!.points).toBe("0,0 -260,0"); // y defaults to 0 → horizontal path
+ });
+
+ it("draws a y-only tween too (x defaults to 0)", () => {
+ const read: ReadTween = {
+ keyframes: [
+ { percentage: 0, properties: { y: 0 } },
+ { percentage: 100, properties: { y: 500 } },
+ ],
+ };
+ expect(buildMotionPathGeometry(read)!.points).toBe("0,0 0,500");
+ });
+
+ it("excludes keyframes missing a coordinate without throwing", () => {
+ const read: ReadTween = {
+ keyframes: [kf(0, 10, 20), { percentage: 50, properties: { x: 100 } }, kf(100, 200, 80)],
+ };
+ const geo = buildMotionPathGeometry(read);
+ expect(geo!.nodes).toHaveLength(2);
+ expect(geo!.points).toBe("10,20 200,80");
+ });
+
+ it("returns null when fewer than two valid nodes remain", () => {
+ const read: ReadTween = { keyframes: [kf(0, 10, 20)] };
+ expect(buildMotionPathGeometry(read)).toBeNull();
+ });
+
+ it("returns null for null input", () => {
+ expect(buildMotionPathGeometry(null)).toBeNull();
+ });
+});
+
+describe("nearestPointOnPath", () => {
+ const nodes = [
+ { x: 0, y: 0 },
+ { x: 100, y: 0 },
+ { x: 100, y: 100 },
+ ];
+
+ it("projects onto the nearest segment and reports its index + fraction", () => {
+ const p = nearestPointOnPath(50, 20, nodes);
+ expect(p).toEqual({ x: 50, y: 0, segIndex: 0, t: 0.5, dist: 20 });
+ });
+
+ it("reports t at the segment endpoints (0 at start, clamps to 1 past the end)", () => {
+ expect(nearestPointOnPath(0, 5, nodes)).toMatchObject({ segIndex: 0, t: 0 });
+ expect(nearestPointOnPath(110, 0, nodes)).toMatchObject({ segIndex: 0, t: 1 });
+ });
+
+ it("picks the second segment when closer to it", () => {
+ const p = nearestPointOnPath(120, 50, nodes);
+ expect(p).toMatchObject({ x: 100, y: 50, segIndex: 1 });
+ });
+
+ it("clamps to an endpoint when the projection falls past the segment", () => {
+ const p = nearestPointOnPath(-40, -10, nodes);
+ expect(p).toMatchObject({ x: 0, y: 0, segIndex: 0 });
+ });
+
+ it("returns null for fewer than two nodes", () => {
+ expect(nearestPointOnPath(0, 0, [{ x: 0, y: 0 }])).toBeNull();
+ });
+});
diff --git a/packages/studio/src/components/editor/motionPathGeometry.ts b/packages/studio/src/components/editor/motionPathGeometry.ts
new file mode 100644
index 0000000000..0499fa2bb5
--- /dev/null
+++ b/packages/studio/src/components/editor/motionPathGeometry.ts
@@ -0,0 +1,116 @@
+/**
+ * Convert a live tween (from `readRuntimeKeyframes`) into renderable motion-path
+ * geometry for the on-canvas overlay. Pure — no React/DOM — so it unit-tests in
+ * isolation. Coordinates are in composition space (the same space the overlay's
+ * viewBox uses), so the caller renders nodes/points directly.
+ */
+import type { ReadTween } from "../../hooks/gsapRuntimeKeyframes";
+
+/** Which source edit a dragged node maps to. */
+export type MotionNodeRef =
+ | { type: "keyframe"; pct: number } // x/y position keyframe at this tween-relative %
+ | { type: "waypoint"; index: number }; // motionPath waypoint (anchor) at this index
+
+export interface MotionPathNode {
+ x: number;
+ y: number;
+ ref: MotionNodeRef;
+}
+
+export interface MotionPathGeometry {
+ /** "linear" = x/y keyframes; "arc" = motionPath tween. */
+ kind: "linear" | "arc";
+ /** SVG polyline points: "x,y x,y ...". */
+ points: string;
+ nodes: MotionPathNode[];
+}
+
+/**
+ * Build motion-path geometry, or null when the tween carries no positional path
+ * (fewer than two keyframes with both x and y). For motionPath tweens the
+ * keyframes are the arc waypoints (anchors), index-aligned with the source path
+ * — so a waypoint node at index `i` rewrites source waypoint `i`.
+ *
+ * ponytail: the arc is drawn as a polyline through its waypoints (matching the
+ * angular dotted look of the reference), not GSAP's resolved curve. Dense
+ * curve sampling is a later refinement if the straight-segment preview proves
+ * insufficient.
+ */
+/**
+ * Nearest point on a polyline to (px, py), with the index of the segment it
+ * lies on and `t` = how far along that segment the returned point sits.
+ *
+ * `t` semantics: clamped to the inclusive range [0, 1].
+ * - `t === 0` → the point is at (or projects before) the segment's start node
+ * (`segIndex`); a perpendicular dropped from (px, py) falls at or behind `a`.
+ * - `0 < t < 1` → the point is strictly interior to the segment.
+ * - `t === 1` → the point is at (or projects PAST) the segment's end node
+ * (`segIndex + 1`); past-the-end projections are clamped back onto the endpoint,
+ * so the returned (x, y) is exactly `nodes[segIndex + 1]`. Callers can read
+ * `t === 1` as "snapped to the end anchor of this segment" (equivalently, the
+ * start anchor of the next segment).
+ * A degenerate zero-length segment (`a === b`) yields `t === 0`.
+ *
+ * Used to position the ghost "add" node and decide where a new node goes: a
+ * motionPath waypoint inserts between `segIndex`/`segIndex + 1`, a keyframe
+ * interpolates its tween-% from the two adjacent keyframes via `t`.
+ * Coordinates are whatever space the caller passes (overlay uses absolute px).
+ */
+export function nearestPointOnPath(
+ px: number,
+ py: number,
+ nodes: Array<{ x: number; y: number }>,
+): { x: number; y: number; segIndex: number; t: number; dist: number } | null {
+ if (nodes.length < 2) return null;
+ let best: { x: number; y: number; segIndex: number; t: number; dist: number } | null = null;
+ for (let i = 0; i < nodes.length - 1; i++) {
+ const a = nodes[i]!;
+ const b = nodes[i + 1]!;
+ const dx = b.x - a.x;
+ const dy = b.y - a.y;
+ const len2 = dx * dx + dy * dy;
+ const t = len2 === 0 ? 0 : Math.max(0, Math.min(1, ((px - a.x) * dx + (py - a.y) * dy) / len2));
+ const cx = a.x + t * dx;
+ const cy = a.y + t * dy;
+ const dist = Math.hypot(px - cx, py - cy);
+ if (!best || dist < best.dist) best = { x: cx, y: cy, segIndex: i, t, dist };
+ }
+ return best;
+}
+
+export function buildMotionPathGeometry(read: ReadTween | null): MotionPathGeometry | null {
+ if (!read) return null;
+ const isArc = Boolean(read.arcPath);
+ const nodes: MotionPathNode[] = [];
+
+ // Index by source position so a waypoint node maps to the matching source
+ // anchor. Arc waypoints always carry x/y (never filtered), so source index
+ // and node order stay aligned.
+ // Which axes does the tween animate at all? A single-axis tween (e.g.
+ // `to({ x: -260 })`) only carries x; its y stays at the base (0, the GSAP
+ // transform identity), so we default it and still draw a path. But if the tween
+ // DOES animate an axis and a given keyframe omits it, that value is interpolated
+ // (not 0) and can't be placed here → skip that node (the prior behavior).
+ const finite = (v: unknown): v is number => typeof v === "number" && isFinite(v);
+ const tweenHasX = read.keyframes.some((kf) => finite(kf.properties.x));
+ const tweenHasY = read.keyframes.some((kf) => finite(kf.properties.y));
+ if (!tweenHasX && !tweenHasY) return null; // no positional motion (opacity/scale only)
+
+ read.keyframes.forEach((kf, i) => {
+ if (tweenHasX && !finite(kf.properties.x)) return;
+ if (tweenHasY && !finite(kf.properties.y)) return;
+ nodes.push({
+ x: tweenHasX ? (kf.properties.x as number) : 0,
+ y: tweenHasY ? (kf.properties.y as number) : 0,
+ ref: isArc ? { type: "waypoint", index: i } : { type: "keyframe", pct: kf.percentage },
+ });
+ });
+
+ if (nodes.length < 2) return null;
+
+ return {
+ kind: isArc ? "arc" : "linear",
+ points: nodes.map((n) => `${n.x},${n.y}`).join(" "),
+ nodes,
+ };
+}
diff --git a/packages/studio/src/components/editor/motionPathSelection.ts b/packages/studio/src/components/editor/motionPathSelection.ts
new file mode 100644
index 0000000000..333290be44
--- /dev/null
+++ b/packages/studio/src/components/editor/motionPathSelection.ts
@@ -0,0 +1,33 @@
+/**
+ * Resolving the selected element and the animation whose path is editable.
+ * Shared by the overlay and its diagnostics (kept here to avoid a circular
+ * import between the two).
+ */
+import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
+import type { DomEditSelection } from "./domEditing";
+
+export function selectorFor(sel: DomEditSelection | null): string | null {
+ if (!sel) return null;
+ if (sel.id) return `#${CSS.escape(sel.id)}`;
+ return sel.selector ?? null;
+}
+
+/** The animation whose path is editable on-canvas: literal, statically resolved,
+ * and matching the rendered geometry kind. Returns null when the path can only
+ * be displayed (dynamic/helper tweens) — those nodes stay read-only. */
+export function editableAnimationId(
+ animations: GsapAnimation[],
+ kind: "linear" | "arc",
+): string | null {
+ const ok = (a: GsapAnimation) =>
+ !a.hasUnresolvedKeyframes && !a.hasUnresolvedSelector && !a.provenance;
+ if (kind === "arc") return animations.find((a) => a.arcPath?.enabled && ok(a))?.id ?? null;
+ const a = animations.find(
+ (anim) =>
+ anim.keyframes &&
+ ok(anim) &&
+ (anim.propertyGroup === "position" ||
+ anim.keyframes.keyframes.some((k) => "x" in k.properties || "y" in k.properties)),
+ );
+ return a?.id ?? null;
+}
diff --git a/packages/studio/src/components/editor/useDomEditOverlayGestures.ts b/packages/studio/src/components/editor/useDomEditOverlayGestures.ts
index 459abc4aff..002e9cb8b9 100644
--- a/packages/studio/src/components/editor/useDomEditOverlayGestures.ts
+++ b/packages/studio/src/components/editor/useDomEditOverlayGestures.ts
@@ -9,6 +9,7 @@ import { type DomEditSelection } from "./domEditing";
import {
applyManualOffsetDragCommit,
applyManualOffsetDragDraft,
+ applyRotationDraftViaGsap,
endManualOffsetDragMembers,
restoreManualOffsetDragMembers,
resumeGsapTimelines,
@@ -161,19 +162,21 @@ export function createDomEditOverlayGestureHandlers(opts: UseDomEditOverlayGestu
let dy = e.clientY - g.startY;
if (g.kind === "rotate") {
- applyStudioRotationDraft(
- sel.element,
- resolveDomEditRotationGesture({
- centerX: g.centerX,
- centerY: g.centerY,
- startX: g.startX,
- startY: g.startY,
- currentX: e.clientX,
- currentY: e.clientY,
- actualAngle: g.actualRotation,
- snap: e.shiftKey,
- }),
- );
+ // Single source of truth: preview the rotation through the GSAP channel (the
+ // same channel the commit lands in), not the `--hf-studio-rotation` CSS var.
+ const rotated = resolveDomEditRotationGesture({
+ centerX: g.centerX,
+ centerY: g.centerY,
+ startX: g.startX,
+ startY: g.startY,
+ currentX: e.clientX,
+ currentY: e.clientY,
+ actualAngle: g.actualRotation,
+ snap: e.shiftKey,
+ });
+ if (!applyRotationDraftViaGsap(sel.element, rotated.angle)) {
+ applyStudioRotationDraft(sel.element, rotated);
+ }
return;
}
@@ -393,25 +396,38 @@ export function createDomEditOverlayGestureHandlers(opts: UseDomEditOverlayGestu
actualAngle: g.actualRotation,
snap: e.shiftKey,
});
+ const restoreRotation = () => {
+ // Single source of truth: snap the GSAP rotation back to the gesture's base
+ // angle; fall back to the legacy CSS-var restore when gsap is unavailable.
+ if (!applyRotationDraftViaGsap(sel.element, g.actualRotation)) {
+ restoreStudioRotation(sel.element, g.initialRotation);
+ }
+ };
if (!hasDomEditRotationChanged(g.actualRotation, finalRotation.angle)) {
- restoreStudioRotation(sel.element, g.initialRotation);
+ restoreRotation();
endStudioManualEditGesture(sel.element, g.manualEditDragToken);
return;
}
- applyStudioRotation(sel.element, finalRotation);
+ // Keep the preview at the final angle through the GSAP channel (NOT the CSS var)
+ // while the commit lands a `tl.set`/keyframe rotation on the timeline.
+ if (!applyRotationDraftViaGsap(sel.element, finalRotation.angle)) {
+ applyStudioRotation(sel.element, finalRotation);
+ }
void Promise.resolve(opts.onRotationCommitRef.current(sel, finalRotation))
.catch(() => {
if (
g.manualEditDragToken &&
isStudioManualEditGestureCurrent(sel.element, g.manualEditDragToken)
)
- restoreStudioRotation(sel.element, g.initialRotation);
+ restoreRotation();
})
.finally(() => endStudioManualEditGesture(sel.element, g.manualEditDragToken));
} else if (g.kind === "drag") {
const dx = g.lastSnappedDx ?? e.clientX - g.startX;
const dy = g.lastSnappedDy ?? e.clientY - g.startY;
- if (!g.pathOffsetMember) return;
+ if (!g.pathOffsetMember) {
+ return;
+ }
const finalOffset = applyManualOffsetDragCommit(g.pathOffsetMember, dx, dy);
const nextBoxLeft = g.originLeft + dx;
const nextBoxTop = g.originTop + dy;
@@ -427,7 +443,9 @@ export function createDomEditOverlayGestureHandlers(opts: UseDomEditOverlayGestu
box.style.left = `${nextBoxLeft}px`;
box.style.top = `${nextBoxTop}px`;
}
- void Promise.resolve(opts.onPathOffsetCommitRef.current(sel, finalOffset))
+ void Promise.resolve(
+ opts.onPathOffsetCommitRef.current(sel, finalOffset, { altKey: e.altKey }),
+ )
.catch(() => {
if (
g.pathOffsetMember?.gestureToken &&
diff --git a/packages/studio/src/components/editor/useDomEditOverlayRects.ts b/packages/studio/src/components/editor/useDomEditOverlayRects.ts
index a0c72e0837..bf3819b40e 100644
--- a/packages/studio/src/components/editor/useDomEditOverlayRects.ts
+++ b/packages/studio/src/components/editor/useDomEditOverlayRects.ts
@@ -148,6 +148,12 @@ export function useDomEditOverlayRects({
activeCompositionPathRef.current,
resolvedElementRef as ResolvedElementRef,
);
+ // An explicitly-selected element's overlay must track it whenever it's laid
+ // out and not display:none/visibility:hidden/opacity:0 — use basic visibility,
+ // NOT the occlusion heuristic. Occlusion (isElementVisibleInPreview) treats any
+ // opacity:1 ancestor as an opaque cover even when it paints nothing (e.g. a
+ // backgroundless full-bleed scene above a subcomposition), which would wrongly
+ // hide the selection box. Occlusion stays for hover, where a false hide is cheap.
if (el && isElementVisibleForOverlay(el)) {
const nextRect = toOverlayRect(overlayEl, iframe, el);
setOverlayRect(nextRect);
diff --git a/packages/studio/src/components/editor/useMotionPathData.ts b/packages/studio/src/components/editor/useMotionPathData.ts
new file mode 100644
index 0000000000..0cf9e2bbdc
--- /dev/null
+++ b/packages/studio/src/components/editor/useMotionPathData.ts
@@ -0,0 +1,135 @@
+import { useEffect, useRef, useState, type RefObject } from "react";
+import { readRuntimeKeyframes } from "../../hooks/gsapRuntimeKeyframes";
+import { isElementVisibleForOverlay } from "./domEditOverlayGeometry";
+import { buildMotionPathGeometry, type MotionPathGeometry } from "./motionPathGeometry";
+
+type Rect = { left: number; top: number; width: number; height: number };
+
+export function elementHome(el: HTMLElement): { x: number; y: number } {
+ let left = 0;
+ let top = 0;
+ let node: HTMLElement | null = el;
+ while (node) {
+ left += node.offsetLeft;
+ top += node.offsetTop;
+ const parent = node.offsetParent as HTMLElement | null;
+ if (!parent || parent.hasAttribute("data-composition-id")) break;
+ node = parent;
+ }
+ let x = left + el.offsetWidth / 2;
+ let y = top + el.offsetHeight / 2;
+ if ((el.style.translate ?? "").includes("var(")) {
+ x += Number.parseFloat(el.style.getPropertyValue("--hf-studio-offset-x")) || 0;
+ y += Number.parseFloat(el.style.getPropertyValue("--hf-studio-offset-y")) || 0;
+ }
+ return { x, y };
+}
+
+export function isPreviewHtmlElement(
+ node: Element | null | undefined,
+ iframe: HTMLIFrameElement | null,
+): node is HTMLElement {
+ const Ctor = (iframe?.contentWindow as unknown as { HTMLElement?: typeof HTMLElement } | null)
+ ?.HTMLElement;
+ return Boolean(node && Ctor && node instanceof Ctor);
+}
+
+function rectsClose(a: Rect, b: Rect): boolean {
+ return (
+ Math.abs(a.left - b.left) < 0.5 &&
+ Math.abs(a.top - b.top) < 0.5 &&
+ Math.abs(a.width - b.width) < 0.5 &&
+ Math.abs(a.height - b.height) < 0.5
+ );
+}
+
+export function hasMotionPathPlugin(iframe: HTMLIFrameElement | null): boolean {
+ try {
+ return Boolean(
+ (iframe?.contentWindow as unknown as { MotionPathPlugin?: unknown })?.MotionPathPlugin,
+ );
+ } catch {
+ return false;
+ }
+}
+
+export function useMotionPathData(
+ iframeRef: RefObject<HTMLIFrameElement | null>,
+ selector: string | null,
+): {
+ rect: Rect | null;
+ geometry: MotionPathGeometry | null;
+ geometryResolved: boolean;
+ visibleInPreview: boolean;
+ home: { x: number; y: number } | null;
+} {
+ const [rect, setRect] = useState<Rect | null>(null);
+ const [geometry, setGeometry] = useState<MotionPathGeometry | null>(null);
+ const resolvedForRef = useRef<string | null>(null);
+ const geometryResolved = resolvedForRef.current === selector;
+ const [visibleInPreview, setVisibleInPreview] = useState(true);
+ const [home, setHome] = useState<{ x: number; y: number } | null>(null);
+
+ useEffect(() => {
+ if (!selector) {
+ setRect(null);
+ setHome(null);
+ return;
+ }
+ setHome(null);
+ let raf = 0;
+ const tick = () => {
+ const el = iframeRef.current;
+ if (el) {
+ const r = el.getBoundingClientRect();
+ const surface = el.ownerDocument?.querySelector("[data-preview-pan-surface]");
+ const sRect = surface?.getBoundingClientRect();
+ const next = {
+ left: sRect ? r.left - sRect.left : r.left,
+ top: sRect ? r.top - sRect.top : r.top,
+ width: r.width,
+ height: r.height,
+ };
+ setRect((prev) => (prev && rectsClose(prev, next) ? prev : next));
+ let target: Element | null = null;
+ try {
+ target = el.contentDocument?.querySelector(selector) ?? null;
+ } catch {
+ /* cross-origin guard */
+ }
+ const live = isPreviewHtmlElement(target, el) ? target : null;
+ const vis = live ? isElementVisibleForOverlay(live) : true;
+ setVisibleInPreview((prev) => (prev === vis ? prev : vis));
+ if (live) {
+ const h = elementHome(live);
+ setHome((prev) =>
+ prev && Math.abs(prev.x - h.x) < 0.5 && Math.abs(prev.y - h.y) < 0.5 ? prev : h,
+ );
+ }
+ }
+ raf = requestAnimationFrame(tick);
+ };
+ raf = requestAnimationFrame(tick);
+ return () => cancelAnimationFrame(raf);
+ }, [selector, iframeRef]);
+
+ useEffect(() => {
+ if (!selector) {
+ setGeometry(null);
+ return;
+ }
+ const recompute = () => {
+ const read = readRuntimeKeyframes(iframeRef.current, selector);
+ const next = buildMotionPathGeometry(read);
+ setGeometry((prev) =>
+ prev?.points === next?.points && prev?.kind === next?.kind ? prev : next,
+ );
+ resolvedForRef.current = selector;
+ };
+ recompute();
+ const id = window.setInterval(recompute, 250);
+ return () => window.clearInterval(id);
+ }, [selector, iframeRef]);
+
+ return { rect, geometry, geometryResolved, visibleInPreview, home };
+}
diff --git a/packages/studio/src/components/nle/NLELayout.tsx b/packages/studio/src/components/nle/NLELayout.tsx
index 65649bb0d6..9e1dad4bb8 100644
--- a/packages/studio/src/components/nle/NLELayout.tsx
+++ b/packages/studio/src/components/nle/NLELayout.tsx
@@ -16,11 +16,13 @@ import { CompositionBreadcrumb } from "./CompositionBreadcrumb";
import { usePreviewBlockDrop } from "./usePreviewBlockDrop";
import { useCompositionStack } from "./useCompositionStack";
import { useTimelineEditContext } from "../../contexts/TimelineEditContext";
+import { setCompositionSourceMap } from "../editor/domEditingDom";
import { trackStudioExpandedClipEdit } from "../../telemetry/events";
import {
TIMELINE_TOGGLE_SHORTCUT_LABEL,
getTimelineToggleTitle,
} from "../../utils/timelineDiscovery";
+import { ensureMotionPathPluginLoaded } from "../../utils/gsapSoftReload";
interface NLELayoutProps {
projectId: string;
@@ -122,12 +124,14 @@ export const NLELayout = memo(function NLELayout({
refreshPlayer,
} = useTimelinePlayer();
- // Reset timeline state when the project changes
- const prevProjectIdRef = useRef(projectId);
- if (prevProjectIdRef.current !== projectId) {
- prevProjectIdRef.current = projectId;
+ // Reset timeline state when the project changes. Done in an effect, not during
+ // render: reset() updates the player store, and updating another store/component
+ // mid-render triggers React's "Cannot update a component while rendering a
+ // different component" warning. The effect runs right after commit, so the new
+ // project's first frame may briefly show prior timeline state before it clears.
+ useEffect(() => {
usePlayerStore.getState().reset();
- }
+ }, [projectId]);
const stageRefForDrop = useRef<HTMLDivElement | null>(null);
const handleStageRef = useCallback((ref: React.RefObject<HTMLDivElement | null>) => {
@@ -158,6 +162,10 @@ export const NLELayout = memo(function NLELayout({
const onIframeLoad = useCallback(() => {
baseOnIframeLoad();
+ // Pre-load + register MotionPathPlugin once so adding a motion path in the
+ // studio doesn't take the async plugin-load flash path on the first soft
+ // reload (the comp may not ship the plugin until it actually uses one).
+ ensureMotionPathPluginLoaded(iframeRef.current);
onIframeRef?.(iframeRef.current);
}, [baseOnIframeLoad, iframeRef, onIframeRef]);
@@ -294,6 +302,9 @@ export const NLELayout = memo(function NLELayout({
if (id && src) map.set(id, src);
}
setCompIdToSrc(map);
+ // Let DOM source-resolution recover a subcomposition element's source file
+ // (the runtime drops the linkage when inlining — see getSourceFileForElement).
+ setCompositionSourceMap(map);
onCompIdToSrcChange?.(map);
})
.catch(() => {});
@@ -427,7 +438,7 @@ export const NLELayout = memo(function NLELayout({
{/* Preview + player controls */}
<div className="flex-1 min-h-0 flex flex-col">
<div
- className="flex-1 min-h-0 relative"
+ className="flex-1 min-h-0 relative overflow-hidden"
data-preview-pan-surface="true"
onPointerDown={(e) => {
const el = iframeRef.current?.parentElement ?? iframeRef.current;
diff --git a/packages/studio/src/components/renders/RenderQueue.tsx b/packages/studio/src/components/renders/RenderQueue.tsx
index f3362843e2..e1de4f21c2 100644
--- a/packages/studio/src/components/renders/RenderQueue.tsx
+++ b/packages/studio/src/components/renders/RenderQueue.tsx
@@ -119,7 +119,7 @@ const FORMAT_INFO: Record<"mp4" | "webm" | "mov", { label: string; desc: string
mp4: { label: "MP4", desc: "Best for general use. Smallest file, universal playback." },
mov: {
label: "MOV (ProRes 4444)",
- desc: "Transparent video. Works in CapCut, Final Cut Pro, Premiere, DaVinci Resolve, After Effects. Large files.",
+ desc: "Transparent video. Works in Final Cut Pro, DaVinci Resolve, and most video editors. Large files.",
},
webm: {
label: "WebM (VP9)",
diff --git a/packages/studio/src/hooks/draggedGsapPosition.ts b/packages/studio/src/hooks/draggedGsapPosition.ts
new file mode 100644
index 0000000000..63cf68813a
--- /dev/null
+++ b/packages/studio/src/hooks/draggedGsapPosition.ts
@@ -0,0 +1,47 @@
+/**
+ * Drag → GSAP position math, shared by the commit path
+ * (`gsapDragCommit.commitGsapPositionFromDrag` / `commitStaticGsapPosition`) and
+ * the live preview (`manualOffsetDrag.applyManualOffsetDrag*`). Kept in its own
+ * leaf module — no store/runtime/core imports — so the live-preview file can use
+ * it without pulling the GSAP commit graph into its module scope.
+ */
+
+/**
+ * Translate a studio drag offset into absolute GSAP x/y, accounting for the
+ * element's rotation and its drag-start base pose. Reads the drag-start
+ * attributes stamped by `createManualOffsetDragMember`
+ * (`data-hf-drag-initial-offset-*`, `data-hf-drag-gsap-base-*`); `fallbackBase`
+ * is used when the base attributes are absent (e.g. a static element that GSAP
+ * hasn't given an x/y yet).
+ *
+ * Used by both the tweened commit and the static `set` commit / live preview, so
+ * the preview and the committed value agree by construction.
+ */
+// fallow-ignore-next-line complexity
+export function computeDraggedGsapPosition(
+ element: HTMLElement,
+ studioOffset: { x: number; y: number },
+ fallbackBase: { x: number; y: number },
+): { newX: number; newY: number; baseGsapX: number; baseGsapY: number } {
+ const rotStyle = element.style.getPropertyValue("--hf-studio-rotation");
+ const rotDeg = Number.parseFloat(rotStyle) || 0;
+ const rad = (-rotDeg * Math.PI) / 180;
+ const cos = Math.cos(rad);
+ const sin = Math.sin(rad);
+ const origX = Number.parseFloat(element.getAttribute("data-hf-drag-initial-offset-x") ?? "") || 0;
+ const origY = Number.parseFloat(element.getAttribute("data-hf-drag-initial-offset-y") ?? "") || 0;
+ const deltaX = studioOffset.x - origX;
+ const deltaY = studioOffset.y - origY;
+ const adjX = deltaX * cos - deltaY * sin;
+ const adjY = deltaX * sin + deltaY * cos;
+ const parsedBaseX = Number.parseFloat(element.getAttribute("data-hf-drag-gsap-base-x") ?? "");
+ const parsedBaseY = Number.parseFloat(element.getAttribute("data-hf-drag-gsap-base-y") ?? "");
+ const baseGsapX = Number.isFinite(parsedBaseX) ? parsedBaseX : fallbackBase.x;
+ const baseGsapY = Number.isFinite(parsedBaseY) ? parsedBaseY : fallbackBase.y;
+ return {
+ newX: Math.round(baseGsapX + adjX),
+ newY: Math.round(baseGsapY + adjY),
+ baseGsapX,
+ baseGsapY,
+ };
+}
diff --git a/packages/studio/src/hooks/gsapDragCommit.test.ts b/packages/studio/src/hooks/gsapDragCommit.test.ts
new file mode 100644
index 0000000000..fab857dd22
--- /dev/null
+++ b/packages/studio/src/hooks/gsapDragCommit.test.ts
@@ -0,0 +1,435 @@
+import { describe, expect, it, beforeEach } from "vitest";
+import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
+import type { DomEditSelection } from "../components/editor/domEditingTypes";
+import {
+ commitGsapPositionFromDrag,
+ commitStaticGsapPosition,
+ commitStaticGsapRotation,
+ parkPlayheadOnKeyframe,
+ type GsapDragCommitCallbacks,
+} from "./gsapDragCommit";
+import { usePlayerStore } from "../player/store/playerStore";
+
+// Minimal selection whose element has no drag-baseline attributes (origX/Y = 0).
+const selection = (): DomEditSelection =>
+ ({
+ id: "puck-a",
+ selector: "#puck-a",
+ element: {
+ style: { getPropertyValue: () => "", setProperty: () => {} },
+ getAttribute: () => null,
+ removeAttribute: () => {},
+ getBoundingClientRect: () => ({ top: 0, left: 0 }),
+ },
+ }) as unknown as DomEditSelection;
+
+const flatTween = (): GsapAnimation =>
+ ({
+ id: "#puck-a-to",
+ targetSelector: "#puck-a",
+ method: "to",
+ resolvedStart: 1.2,
+ duration: 2.2,
+ properties: { x: -260 },
+ }) as unknown as GsapAnimation;
+
+// What the flat tween becomes after convert-to-keyframes (returned by fetchAnimations).
+const convertedTween = (): GsapAnimation =>
+ ({
+ id: "#puck-a-converted",
+ targetSelector: "#puck-a",
+ method: "to",
+ resolvedStart: 1.2,
+ duration: 2.2,
+ keyframes: {
+ keyframes: [
+ { percentage: 0, properties: { x: 0, y: 0 } },
+ { percentage: 100, properties: { x: -260, y: 0 } },
+ ],
+ },
+ }) as unknown as GsapAnimation;
+
+function recordingCallbacks(): {
+ types: string[];
+ mutations: Array<Record<string, unknown>>;
+ callbacks: GsapDragCommitCallbacks;
+} {
+ const types: string[] = [];
+ const mutations: Array<Record<string, unknown>> = [];
+ return {
+ types,
+ mutations,
+ callbacks: {
+ commitMutation: async (_sel, mutation) => {
+ types.push(mutation.type as string);
+ mutations.push(mutation);
+ },
+ fetchAnimations: async () => [convertedTween()],
+ },
+ };
+}
+
+describe("commitGsapPositionFromDrag — flat tween", () => {
+ beforeEach(() => {
+ usePlayerStore.setState({ currentTime: 0, activeKeyframePct: null });
+ });
+
+ it("extends the existing tween (never spawns a parallel one) when dragged OUTSIDE its range", async () => {
+ usePlayerStore.setState({ currentTime: 6 }); // outside [1.2, 3.4]
+ const { types, callbacks } = recordingCallbacks();
+
+ await commitGsapPositionFromDrag(
+ selection(),
+ flatTween(),
+ { x: -100, y: 0 },
+ { x: 0, y: 0 },
+ null,
+ "#puck-a",
+ callbacks,
+ );
+
+ expect(types).toContain("convert-to-keyframes");
+ expect(types).toContain("replace-with-keyframes"); // the extend
+ expect(types).not.toContain("add-with-keyframes"); // regression: no parallel tween
+ });
+
+ it("adds a keyframe at the playhead when dragged INSIDE its range", async () => {
+ usePlayerStore.setState({ currentTime: 2 }); // inside [1.2, 3.4]
+ const { types, callbacks } = recordingCallbacks();
+
+ await commitGsapPositionFromDrag(
+ selection(),
+ flatTween(),
+ { x: -100, y: 0 },
+ { x: 0, y: 0 },
+ null,
+ "#puck-a",
+ callbacks,
+ );
+
+ expect(types).toContain("add-keyframe");
+ expect(types).not.toContain("add-with-keyframes");
+ });
+
+ it("MODIFIES the selected keyframe (no extend) when one is selected, even past the tween end", async () => {
+ // User clicked the 100% diamond (activeKeyframePct=100), playhead drifted past
+ // the end. Expect: convert + add-keyframe AT 100% — not replace-with-keyframes.
+ usePlayerStore.setState({ currentTime: 6, activeKeyframePct: 100 }); // outside [1.2, 3.4]
+ const { types, mutations, callbacks } = recordingCallbacks();
+
+ await commitGsapPositionFromDrag(
+ selection(),
+ flatTween(),
+ { x: -100, y: 0 },
+ { x: 0, y: 0 },
+ null,
+ "#puck-a",
+ callbacks,
+ );
+
+ expect(types).toContain("add-keyframe");
+ expect(types).not.toContain("replace-with-keyframes"); // not extended
+ const addKf = mutations.find((m) => m.type === "add-keyframe");
+ expect(addKf?.percentage).toBe(100); // modified the selected endpoint
+ // consumed: cleared so the next free drag doesn't keep modifying
+ expect(usePlayerStore.getState().activeKeyframePct).toBeNull();
+ // parked the playhead on the edited keyframe (1.2 start + 100% * 2.2 dur),
+ // so the edit is visible instead of rendering the base pose
+ expect(usePlayerStore.getState().requestedSeekTime).toBe(3.4);
+ });
+});
+
+describe("commitGsapPositionFromDrag — keyframed tween backfill", () => {
+ beforeEach(() => {
+ usePlayerStore.setState({ currentTime: 0, activeKeyframePct: null });
+ });
+
+ const keyframedTween = (): GsapAnimation =>
+ ({
+ id: "#puck-a-kf",
+ targetSelector: "#puck-a",
+ method: "to",
+ resolvedStart: 1.2,
+ duration: 2.2,
+ keyframes: {
+ keyframes: [
+ { percentage: 0, properties: { x: 0 } },
+ { percentage: 100, properties: { x: -260 } },
+ ],
+ },
+ }) as unknown as GsapAnimation;
+
+ it("passes backfillDefaults so a newly-introduced prop doesn't move the other keyframes", async () => {
+ // Drag the 0% keyframe DOWN (introduces y on an x-only tween). The add-keyframe
+ // must carry backfillDefaults at the element's base so 100% gets y:0, not y:780.
+ usePlayerStore.setState({ currentTime: 1.2, activeKeyframePct: 0 });
+ const { mutations, callbacks } = recordingCallbacks();
+
+ await commitGsapPositionFromDrag(
+ selection(),
+ keyframedTween(),
+ { x: 0, y: 780 }, // studioOffset: dragged straight down
+ { x: 0, y: 0 }, // gsapPos → base falls back to {0,0} (selection has no base attrs)
+ null,
+ "#puck-a",
+ callbacks,
+ );
+
+ const addKf = mutations.find((m) => m.type === "add-keyframe");
+ expect(addKf).toBeDefined();
+ expect(addKf?.percentage).toBe(0); // edited the selected 0% keyframe
+ expect(addKf?.properties).toMatchObject({ y: 780 });
+ expect(addKf?.backfillDefaults).toEqual({ x: 0, y: 0 }); // base → 100% gets y:0
+ });
+});
+
+describe("commitGsapPositionFromDrag — from() tween dragged outside its range", () => {
+ beforeEach(() => usePlayerStore.setState({ currentTime: 0, activeKeyframePct: null }));
+
+ const fromTween = (): GsapAnimation =>
+ ({
+ id: "#title-from-400",
+ targetSelector: "#title",
+ method: "from",
+ resolvedStart: 0.4,
+ duration: 0.9,
+ properties: { y: 70 },
+ }) as unknown as GsapAnimation;
+
+ it("REPLACES the split position from() tween (no parallel tween → no drop jump)", async () => {
+ usePlayerStore.setState({ currentTime: 2.13 }); // outside [0.4, 1.3]
+ const types: string[] = [];
+ const mutations: Array<Record<string, unknown>> = [];
+ const callbacks: GsapDragCommitCallbacks = {
+ commitMutation: async (_s, m) => {
+ types.push(m.type as string);
+ mutations.push(m);
+ },
+ // After split-into-property-groups, the position group is a from() tween (no keyframes).
+ fetchAnimations: async () => [
+ {
+ id: "#title-from-400-position",
+ targetSelector: "#title",
+ method: "from",
+ propertyGroup: "position",
+ resolvedStart: 0.4,
+ duration: 0.9,
+ properties: { y: 70 },
+ } as unknown as GsapAnimation,
+ ],
+ };
+
+ await commitGsapPositionFromDrag(
+ selection(),
+ fromTween(),
+ { x: 0, y: -333 },
+ { x: 0, y: 0 },
+ null,
+ "#title",
+ callbacks,
+ );
+
+ expect(types).toContain("split-into-property-groups");
+ expect(types).toContain("replace-with-keyframes");
+ expect(types).not.toContain("add-with-keyframes"); // regression: no parallel tween
+ const replace = mutations.find((m) => m.type === "replace-with-keyframes");
+ expect(replace?.animationId).toBe("#title-from-400-position"); // replaces the split from()
+ });
+});
+
+// Captures the OPTIONS each commit carries (not just the mutation) so we can
+// assert which value-only commits attach the `instantPatch` fast path.
+type RecordedCommit = { mutation: Record<string, unknown>; options: Record<string, unknown> };
+function optionRecordingCallbacks(): {
+ commits: RecordedCommit[];
+ callbacks: GsapDragCommitCallbacks;
+} {
+ const commits: RecordedCommit[] = [];
+ return {
+ commits,
+ callbacks: {
+ commitMutation: async (_sel, mutation, options) => {
+ commits.push({ mutation, options: options as Record<string, unknown> });
+ },
+ fetchAnimations: async () => [convertedTween()],
+ },
+ };
+}
+
+const existingPositionSet = (): GsapAnimation =>
+ ({
+ id: "#puck-a-set",
+ targetSelector: "#puck-a",
+ method: "set",
+ properties: { x: 10, y: 20 },
+ }) as unknown as GsapAnimation;
+
+const existingRotationSet = (): GsapAnimation =>
+ ({
+ id: "#puck-a-rot-set",
+ targetSelector: "#puck-a",
+ method: "set",
+ properties: { rotation: 15 },
+ }) as unknown as GsapAnimation;
+
+describe("commitStaticGsapPosition — instantPatch (value-only set)", () => {
+ beforeEach(() => usePlayerStore.setState({ currentTime: 0, activeKeyframePct: null }));
+
+ it("attaches an instantPatch to BOTH coalesced commits, each derived from its own mutation", async () => {
+ const { commits, callbacks } = optionRecordingCallbacks();
+
+ await commitStaticGsapPosition(
+ selection(),
+ { x: -50, y: 30 }, // studioOffset → newX/newY off a zero base
+ { x: 0, y: 0 },
+ "#puck-a",
+ existingPositionSet(),
+ callbacks,
+ );
+
+ expect(commits).toHaveLength(2);
+ // First (x) commit is the intermediate skipReload one — it now carries an
+ // instantPatch for just {x}, so if the SECOND POST fails the preview still
+ // reflects the x that DID persist (no reload, instant feedback).
+ expect(commits[0].options.skipReload).toBe(true);
+ expect(commits[0].options.instantPatch).toEqual({
+ selector: "#puck-a",
+ change: { kind: "set", props: { x: -50 } },
+ });
+ // Final (y) commit triggers the reload and carries the full {x,y} patch.
+ expect(commits[1].options.softReload).toBe(true);
+ expect(commits[1].options.instantPatch).toEqual({
+ selector: "#puck-a",
+ change: { kind: "set", props: { x: -50, y: 30 } },
+ });
+ });
+
+ it("derives each instantPatch's props from the value in the SAME mutation that's POSTed", async () => {
+ const { commits, callbacks } = optionRecordingCallbacks();
+
+ await commitStaticGsapPosition(
+ selection(),
+ { x: -50, y: 30 },
+ { x: 0, y: 0 },
+ "#puck-a",
+ existingPositionSet(),
+ callbacks,
+ );
+
+ // The patch values must equal the mutation values — they're read out of the
+ // same object, so a clean mutation can't ship alongside a stale patch.
+ const xMutation = commits[0].mutation as { property: string; value: number };
+ const yMutation = commits[1].mutation as { property: string; value: number };
+ const xPatch = commits[0].options.instantPatch as {
+ change: { props: Record<string, number> };
+ };
+ const yPatch = commits[1].options.instantPatch as {
+ change: { props: Record<string, number> };
+ };
+ expect(xPatch.change.props[xMutation.property]).toBe(xMutation.value);
+ expect(yPatch.change.props[yMutation.property]).toBe(yMutation.value);
+ // The y commit's combined patch also carries the x mutation's value.
+ expect(yPatch.change.props[xMutation.property]).toBe(xMutation.value);
+ });
+
+ it("does NOT attach instantPatch when ADDING a new set (structural — new tween)", async () => {
+ const { commits, callbacks } = optionRecordingCallbacks();
+
+ await commitStaticGsapPosition(
+ selection(),
+ { x: -50, y: 30 },
+ { x: 0, y: 0 },
+ "#puck-a",
+ null, // no existing set → `add` a new tween
+ callbacks,
+ );
+
+ expect(commits).toHaveLength(1);
+ expect(commits[0].mutation.type).toBe("add");
+ expect(commits[0].options.instantPatch).toBeUndefined();
+ });
+});
+
+describe("commitStaticGsapRotation — instantPatch (value-only set)", () => {
+ beforeEach(() => usePlayerStore.setState({ currentTime: 0, activeKeyframePct: null }));
+
+ it("attaches instantPatch {kind:set, props:{rotation}} when updating an existing rotation set", async () => {
+ const { commits, callbacks } = optionRecordingCallbacks();
+
+ await commitStaticGsapRotation(selection(), 42, "#puck-a", existingRotationSet(), callbacks);
+
+ expect(commits).toHaveLength(1);
+ expect(commits[0].mutation.type).toBe("update-property");
+ expect(commits[0].options.instantPatch).toEqual({
+ selector: "#puck-a",
+ change: { kind: "set", props: { rotation: 42 } },
+ });
+ // Patch value derived from the SAME mutation that's POSTed (one source).
+ const m = commits[0].mutation as { property: string; value: number };
+ const patch = commits[0].options.instantPatch as {
+ change: { props: Record<string, number> };
+ };
+ expect(patch.change.props[m.property]).toBe(m.value);
+ });
+
+ it("does NOT attach instantPatch when ADDING a new rotation set (structural)", async () => {
+ const { commits, callbacks } = optionRecordingCallbacks();
+
+ await commitStaticGsapRotation(selection(), 42, "#puck-a", null, callbacks);
+
+ expect(commits).toHaveLength(1);
+ expect(commits[0].mutation.type).toBe("add");
+ expect(commits[0].options.instantPatch).toBeUndefined();
+ });
+});
+
+describe("commitGsapPositionFromDrag — keyframe/structural commits omit instantPatch", () => {
+ beforeEach(() => usePlayerStore.setState({ currentTime: 0, activeKeyframePct: null }));
+
+ it("a structural keyframe drag (convert-to-keyframes → add-keyframe) sets no instantPatch", async () => {
+ usePlayerStore.setState({ currentTime: 2 }); // inside [1.2, 3.4] → convert + add-keyframe
+ const { commits, callbacks } = optionRecordingCallbacks();
+
+ await commitGsapPositionFromDrag(
+ selection(),
+ flatTween(),
+ { x: -100, y: 0 },
+ { x: 0, y: 0 },
+ null,
+ "#puck-a",
+ callbacks,
+ );
+
+ // The keyframe path is structural here (convert + add-keyframe) and must rely
+ // on the soft reload — none of its commits opt into the instant patch.
+ expect(commits.length).toBeGreaterThan(0);
+ for (const c of commits) {
+ expect(c.options.instantPatch).toBeUndefined();
+ }
+ const types = commits.map((c) => c.mutation.type);
+ expect(types).toContain("convert-to-keyframes");
+ expect(types).toContain("add-keyframe");
+ });
+});
+
+describe("parkPlayheadOnKeyframe", () => {
+ beforeEach(() => usePlayerStore.setState({ requestedSeekTime: null }));
+
+ const tween = (): GsapAnimation =>
+ ({
+ id: "#x",
+ targetSelector: "#x",
+ method: "to",
+ resolvedStart: 1.2,
+ duration: 2.2,
+ }) as unknown as GsapAnimation;
+
+ it("seeks to the keyframe's absolute time so the element previews AT it, not at base", () => {
+ parkPlayheadOnKeyframe(tween(), 0); // tween start
+ expect(usePlayerStore.getState().requestedSeekTime).toBe(1.2);
+ parkPlayheadOnKeyframe(tween(), 100); // tween end
+ expect(usePlayerStore.getState().requestedSeekTime).toBe(3.4);
+ parkPlayheadOnKeyframe(tween(), 50); // midpoint
+ expect(usePlayerStore.getState().requestedSeekTime).toBe(2.3);
+ });
+});
diff --git a/packages/studio/src/hooks/gsapDragCommit.ts b/packages/studio/src/hooks/gsapDragCommit.ts
index dffc1b07a6..86a8459c54 100644
--- a/packages/studio/src/hooks/gsapDragCommit.ts
+++ b/packages/studio/src/hooks/gsapDragCommit.ts
@@ -9,6 +9,8 @@ import { readRuntimeKeyframes, scanAllRuntimeKeyframes } from "./gsapRuntimeKeyf
import { resolveTweenStart, resolveTweenDuration } from "../utils/globalTimeCompiler";
import { roundTo3 } from "../utils/rounding";
import { computeElementPercentage } from "./gsapShared";
+import { computeDraggedGsapPosition } from "./draggedGsapPosition";
+import type { RuntimeTweenChange, SetPatchProps } from "./gsapRuntimePatch";
export interface GsapDragCommitCallbacks {
commitMutation: (
selection: DomEditSelection,
@@ -19,6 +21,13 @@ export interface GsapDragCommitCallbacks {
softReload?: boolean;
skipReload?: boolean;
beforeReload?: () => void;
+ /**
+ * Value-only fast path: when set, `runCommit` patches the changed tween in
+ * the preview runtime in place (instant, no re-run) and only falls back to
+ * the soft reload if the patch can't be safely applied. Attached only to
+ * value-only `set` commits; structural/keyframe commits omit it.
+ */
+ instantPatch?: { selector: string; change: RuntimeTweenChange };
},
) => Promise<void>;
fetchAnimations?: () => Promise<GsapAnimation[]>;
@@ -32,6 +41,17 @@ export function computeCurrentPercentage(
return computeElementPercentage(usePlayerStore.getState().currentTime, selection, animation);
}
+// When a drag edits a SELECTED keyframe, park the playhead on that keyframe's exact
+// time. Otherwise the playhead can sit a frame outside the tween (e.g. 1.1666 vs a
+// 1.2 start), so the post-commit reseek renders the element's base pose and the edit
+// looks like it snapped away. Keeping the playhead on the edited keyframe avoids that.
+export function parkPlayheadOnKeyframe(anim: GsapAnimation, pct: number): void {
+ const ts = resolveTweenStart(anim);
+ const td = resolveTweenDuration(anim);
+ if (ts == null || !td || td <= 0) return;
+ usePlayerStore.getState().requestSeek(roundTo3(ts + (pct / 100) * td));
+}
+
// ── Dynamic keyframe materialization ──────────────────────────────────────
export async function materializeIfDynamic(
@@ -77,198 +97,262 @@ export async function materializeIfDynamic(
);
}
-// ── Extend tween ──────────────────────────────────────────────────────────
+// ── Drag → GSAP position math ──────────────────────────────────────────────
+
+/** The shape of an `update-property` mutation a static-set nudge POSTs. */
+interface UpdatePropertyMutation {
+ type: "update-property";
+ animationId: string;
+ property: string;
+ value: number;
+}
/**
- * Extend a tween's time range to cover `targetTime`, remap all existing
- * keyframe percentages to preserve their absolute positions, then add
- * a new keyframe at the target time.
+ * Build the `instantPatch` for a value-only `tl.set` from the SAME
+ * `update-property` mutation(s) that are POSTed — so the patch can never carry a
+ * value the source write didn't (one source of truth). Each mutation contributes
+ * its `{property: value}` channel to the patch's props.
*/
-async function extendTweenAndAddKeyframe(
+function setPatchFromUpdateProperties(
+ selector: string,
+ mutations: UpdatePropertyMutation[],
+): { selector: string; change: RuntimeTweenChange } {
+ const props: SetPatchProps = {};
+ for (const m of mutations) props[m.property as keyof SetPatchProps] = m.value;
+ return { selector, change: { kind: "set", props } };
+}
+
+/** Single-mutation convenience over {@link setPatchFromUpdateProperties}. */
+function setPatchFromUpdateProperty(
+ selector: string,
+ mutation: UpdatePropertyMutation,
+): { selector: string; change: RuntimeTweenChange } {
+ return setPatchFromUpdateProperties(selector, [mutation]);
+}
+
+/**
+ * Find the studio position-hold `set` for a selector — a `tl.set("#el",{x,y})`
+ * with no duration. This is what a static-element nudge writes/updates.
+ */
+function findPositionSetAnimation(
+ animations: GsapAnimation[],
+ selector: string,
+): GsapAnimation | null {
+ return (
+ animations.find(
+ (a) =>
+ a.method === "set" &&
+ a.targetSelector === selector &&
+ ("x" in a.properties || "y" in a.properties),
+ ) ?? null
+ );
+}
+
+/**
+ * Commit a STATIC element drag as a `tl.set("#el",{x,y})` — the single-source
+ * position channel for elements with no position animation. Idempotent: a
+ * re-nudge of an element that already has a `set` UPDATES that set's x/y
+ * (two `update-property` mutations) rather than stacking a second set or
+ * converting it to keyframes (plan R2 / KTD3). New elements get one `add`
+ * mutation with `method:"set"` at position 0.
+ */
+export async function commitStaticGsapPosition(
selection: DomEditSelection,
- anim: GsapAnimation,
- properties: Record<string, number>,
- targetTime: number,
- tweenStart: number,
- tweenDuration: number,
+ studioOffset: { x: number; y: number },
+ gsapPos: { x: number; y: number },
+ selector: string,
+ existingSet: GsapAnimation | null,
callbacks: GsapDragCommitCallbacks,
- beforeReload?: () => void,
): Promise<void> {
- const tweenEnd = tweenStart + tweenDuration;
- const newStart = Math.min(targetTime, tweenStart);
- const newEnd = Math.max(targetTime, tweenEnd);
- const newDuration = Math.max(0.01, newEnd - newStart);
- const existingKfs = anim.keyframes?.keyframes ?? [];
- const remappedKfs: Array<{ percentage: number; properties: Record<string, number | string> }> =
- [];
- for (const kf of existingKfs) {
- const absTime = tweenStart + (kf.percentage / 100) * tweenDuration;
- const newPct = Math.round(((absTime - newStart) / newDuration) * 1000) / 10;
- remappedKfs.push({ percentage: newPct, properties: { ...kf.properties } });
+ const { newX, newY } = computeDraggedGsapPosition(selection.element, studioOffset, gsapPos);
+ if (existingSet) {
+ // Update in place — two single-property mutations (the API updates one prop
+ // per call). Coalesce them and reload only after the second lands.
+ const coalesceKey = `gsap:set-nudge:${existingSet.id}`;
+ // Build each mutation FIRST, then derive its instantPatch from the SAME
+ // object that's POSTed — so a future caller can't ship a clean mutation with
+ // a stale/malformed patch (the validated `value` flows straight into the
+ // patch). `findUnsafeMutationValues` validates the mutation upstream.
+ const xMutation = {
+ type: "update-property",
+ animationId: existingSet.id,
+ property: "x",
+ value: newX,
+ } as const;
+ const yMutation = {
+ type: "update-property",
+ animationId: existingSet.id,
+ property: "y",
+ value: newY,
+ } as const;
+ // Patch BOTH coalesced commits. If the SECOND POST fails server-side, the
+ // first (x) already persisted — patching its commit too means the live
+ // preview still reflects what DID persist. The x commit carries skipReload
+ // (no reload), so its instantPatch gives instant feedback without a reload;
+ // the y commit triggers the soft reload (skipped when the patch applies).
+ await callbacks.commitMutation(selection, xMutation, {
+ label: "Move layer",
+ skipReload: true,
+ coalesceKey,
+ instantPatch: setPatchFromUpdateProperty(selector, xMutation),
+ });
+ await callbacks.commitMutation(selection, yMutation, {
+ label: "Move layer",
+ softReload: true,
+ coalesceKey,
+ // Final commit of the coalesced x/y pair: carry both channels so the
+ // runtime `tl.set` lands the complete {x,y} pose in place.
+ instantPatch: setPatchFromUpdateProperties(selector, [xMutation, yMutation]),
+ });
+ return;
}
-
- const targetPct = Math.round(((targetTime - newStart) / newDuration) * 1000) / 10;
- remappedKfs.push({ percentage: targetPct, properties });
-
- remappedKfs.sort((a, b) => a.percentage - b.percentage);
-
await callbacks.commitMutation(
selection,
{
- type: "replace-with-keyframes",
- animationId: anim.id,
- targetSelector: anim.targetSelector,
- position: roundTo3(newStart),
- duration: roundTo3(newDuration),
- keyframes: remappedKfs,
+ type: "add",
+ targetSelector: selector,
+ method: "set",
+ position: 0,
+ properties: { x: newX, y: newY },
},
- { label: `Move layer (extended keyframe)`, softReload: true, beforeReload },
+ { label: "Move layer", softReload: true },
);
}
-// fallow-ignore-next-line complexity
-async function commitKeyframedPosition(
+export { findPositionSetAnimation };
+
+function findRotationSetAnimation(
+ animations: GsapAnimation[],
+ selector: string,
+): GsapAnimation | null {
+ return (
+ animations.find(
+ (a) => a.method === "set" && a.targetSelector === selector && "rotation" in a.properties,
+ ) ?? null
+ );
+}
+
+/**
+ * Commit a STATIC element rotation as a `tl.set("#el",{rotation})` — the single-
+ * source rotation channel for elements with no rotation animation (mirrors
+ * `commitStaticGsapPosition`). `newRotation` is the already-resolved absolute angle
+ * (current runtime rotation + drag delta). Idempotent: re-rotating an element that
+ * already has a rotation `set` UPDATES it in place (one `update-property`, rotation
+ * is a single value unlike x/y); a new element gets one `add` with `method:"set"`.
+ */
+export async function commitStaticGsapRotation(
selection: DomEditSelection,
- anim: GsapAnimation,
- properties: Record<string, number>,
+ newRotation: number,
+ selector: string,
+ existingSet: GsapAnimation | null,
callbacks: GsapDragCommitCallbacks,
- beforeReload?: () => void,
): Promise<void> {
- const { activeKeyframePct, setActiveKeyframePct } = usePlayerStore.getState();
- const pct = activeKeyframePct ?? computeCurrentPercentage(selection, anim);
+ if (existingSet) {
+ // Derive the instantPatch from the SAME mutation object that's POSTed (single
+ // source of truth — see commitStaticGsapPosition), so the validated `value`
+ // flows into the patch and the two can't drift.
+ const rotationMutation = {
+ type: "update-property",
+ animationId: existingSet.id,
+ property: "rotation",
+ value: newRotation,
+ } as const;
+ await callbacks.commitMutation(selection, rotationMutation, {
+ label: "Rotate layer",
+ softReload: true,
+ // Value-only rotation set: patch the runtime `tl.set` rotation in place.
+ instantPatch: setPatchFromUpdateProperty(selector, rotationMutation),
+ });
+ return;
+ }
await callbacks.commitMutation(
selection,
{
- type: "add-keyframe",
- animationId: anim.id,
- percentage: pct,
- properties,
+ type: "add",
+ targetSelector: selector,
+ method: "set",
+ position: 0,
+ properties: { rotation: newRotation },
},
- { label: `Move layer (keyframe ${pct}%)`, softReload: true, beforeReload },
+ { label: "Rotate layer", softReload: true },
+ );
+}
+
+export { findRotationSetAnimation };
+
+function findSizeSetAnimation(animations: GsapAnimation[], selector: string): GsapAnimation | null {
+ return (
+ animations.find(
+ (a) =>
+ a.method === "set" &&
+ a.targetSelector === selector &&
+ ("width" in a.properties || "height" in a.properties),
+ ) ?? null
);
- if (activeKeyframePct != null) setActiveKeyframePct(null);
}
/**
- * For flat to()/set() tweens, convert to keyframes first so we can place the
- * drag position at the current percentage.
+ * Commit a STATIC element resize as a `tl.set("#el",{width,height})` — the
+ * single-source size channel for elements with no size animation (mirrors
+ * `commitStaticGsapPosition`). Use this instead of a single-stop `keyframes`
+ * tween: one keyframe at the playhead % renders NaN/0 at every other frame, so
+ * the element collapses/disappears (worst when resized off the 0% mark). A `set`
+ * holds the size at all times. Re-resizing an element that already has a size
+ * `set` UPDATES it in place (two `update-property`, like x/y); a new element
+ * gets one `add` with `method:"set"`.
*/
-// fallow-ignore-next-line complexity
-async function commitFlatViaKeyframes(
+export async function commitStaticGsapSize(
selection: DomEditSelection,
- anim: GsapAnimation,
- properties: Record<string, number>,
+ size: { width: number; height: number },
+ selector: string,
+ existingSet: GsapAnimation | null,
callbacks: GsapDragCommitCallbacks,
- beforeReload?: () => void,
- iframe?: HTMLIFrameElement | null,
- selector?: string,
): Promise<void> {
- const ct = usePlayerStore.getState().currentTime;
- const ts = resolveTweenStart(anim);
- const td = resolveTweenDuration(anim);
- const outsideRange = ts !== null && td > 0 && (ct < ts - 0.01 || ct > ts + td + 0.01);
-
- // Read the runtime position at the tween's start time so the 0% keyframe
- // captures the actual interpolated value (e.g. x=300 after a preceding slide),
- // not the identity value (x=0) that a blind convert would produce.
- const resolvedFromValues: Record<string, number | string> = {};
- if (iframe && selector && ts !== null) {
- try {
- const iframeWin = iframe.contentWindow as any;
- const gsapLib = iframeWin?.gsap;
- const el = iframe.contentDocument?.querySelector(selector);
- const timelines = iframeWin?.__timelines;
- const mainTl = timelines ? (Object.values(timelines)[0] as any) : null;
- if (gsapLib && el && mainTl?.seek) {
- mainTl.seek(ts);
- for (const key of Object.keys(properties)) {
- const v = Number(gsapLib.getProperty(el, key));
- if (Number.isFinite(v)) resolvedFromValues[key] = roundTo3(v);
- }
- mainTl.seek(ct);
- }
- } catch {
- /* iframe access failed — fall back to identity values */
- }
- }
-
- if (outsideRange && ts !== null) {
- // Outside the tween's range: add a brand new keyframed tween at the drag
- // time instead of extending/replacing the existing one. This keeps all
- // existing tweens untouched and creates a clean hold at the dragged position.
- const tweenEnd = ts + td;
- const holdStart = ct > tweenEnd ? tweenEnd : ct;
- const holdEnd = ct > tweenEnd ? ct : ts;
- const holdDur = Math.max(0.01, holdEnd - holdStart);
- const kfs =
- ct > tweenEnd
- ? [
- { percentage: 0, properties: resolvedFromValues },
- { percentage: 100, properties },
- ]
- : [
- { percentage: 0, properties },
- { percentage: 100, properties: resolvedFromValues },
- ];
- console.log(
- "[drag:5] outside range — adding new tween",
- JSON.stringify({
- ct,
- ts,
- td,
- holdStart: roundTo3(holdStart),
- holdDur: roundTo3(holdDur),
- from: resolvedFromValues,
- to: properties,
- }),
+ const width = Math.round(size.width);
+ const height = Math.round(size.height);
+ if (existingSet) {
+ await callbacks.commitMutation(
+ selection,
+ { type: "delete", animationId: existingSet.id },
+ { label: "Resize layer", skipReload: true },
);
await callbacks.commitMutation(
selection,
{
- type: "add-with-keyframes",
- targetSelector: anim.targetSelector,
- position: roundTo3(holdStart),
- duration: roundTo3(holdDur),
- keyframes: kfs,
+ type: "add",
+ targetSelector: selector,
+ method: "set",
+ position: 0,
+ properties: { width, height },
},
- { label: "Move layer (new keyframe)", softReload: true, beforeReload },
+ { label: "Resize layer", softReload: true },
);
return;
}
-
- // Inside range: convert the flat tween to keyframes, then add at current %.
- const coalesceKey = `gsap:convert-drag:${anim.id}`;
await callbacks.commitMutation(
selection,
{
- type: "convert-to-keyframes",
- animationId: anim.id,
- ...(Object.keys(resolvedFromValues).length > 0 ? { resolvedFromValues } : {}),
+ type: "add",
+ targetSelector: selector,
+ method: "set",
+ position: 0,
+ properties: { width, height },
},
- { label: "Convert to keyframes for drag", skipReload: true, coalesceKey },
- );
- const pct = computeCurrentPercentage(selection, anim);
-
- await callbacks.commitMutation(
- selection,
- {
- type: "add-keyframe",
- animationId: anim.id,
- percentage: pct,
- properties,
- },
- { label: `Move layer (keyframe ${pct}%)`, softReload: true, beforeReload, coalesceKey },
+ { label: "Resize layer", softReload: true },
);
}
-// ── Main drag commit ──────────────────────────────────────────────────────
+export { findSizeSetAnimation };
+
+// ── Whole-path offset (plain drag on animated element) ──────────────────
/**
- * Compute the new GSAP position values from runtime-read positions + drag
- * offset, then commit the mutation to the GSAP script.
+ * Offset the entire animation path by the drag delta — every keyframe's x/y
+ * shifts together so the animation shape is preserved and the element can't
+ * dart off-screen. For flat tweens (no keyframes), convert first then shift.
*/
// fallow-ignore-next-line complexity
-export async function commitGsapPositionFromDrag(
+export async function commitWholePathOffset(
selection: DomEditSelection,
anim: GsapAnimation,
studioOffset: { x: number; y: number },
@@ -277,24 +361,16 @@ export async function commitGsapPositionFromDrag(
selector: string,
callbacks: GsapDragCommitCallbacks,
): Promise<void> {
- const rotStyle = selection.element.style.getPropertyValue("--hf-studio-rotation");
- const rotDeg = Number.parseFloat(rotStyle) || 0;
- const rad = (-rotDeg * Math.PI) / 180;
- const cos = Math.cos(rad);
- const sin = Math.sin(rad);
const el = selection.element;
+ const { newX, newY, baseGsapX, baseGsapY } = computeDraggedGsapPosition(
+ el,
+ studioOffset,
+ gsapPos,
+ );
+ const deltaX = newX - baseGsapX;
+ const deltaY = newY - baseGsapY;
const origX = Number.parseFloat(el.getAttribute("data-hf-drag-initial-offset-x") ?? "") || 0;
const origY = Number.parseFloat(el.getAttribute("data-hf-drag-initial-offset-y") ?? "") || 0;
- const deltaX = studioOffset.x - origX;
- const deltaY = studioOffset.y - origY;
- const adjX = deltaX * cos - deltaY * sin;
- const adjY = deltaX * sin + deltaY * cos;
- const parsedBaseX = Number.parseFloat(el.getAttribute("data-hf-drag-gsap-base-x") ?? "");
- const parsedBaseY = Number.parseFloat(el.getAttribute("data-hf-drag-gsap-base-y") ?? "");
- const baseGsapX = Number.isFinite(parsedBaseX) ? parsedBaseX : gsapPos.x;
- const baseGsapY = Number.isFinite(parsedBaseY) ? parsedBaseY : gsapPos.y;
- const newX = Math.round(baseGsapX + adjX);
- const newY = Math.round(baseGsapY + adjY);
const restoreOffset = () => {
el.style.setProperty("--hf-studio-offset-x", `${origX}px`);
el.style.setProperty("--hf-studio-offset-y", `${origY}px`);
@@ -302,141 +378,53 @@ export async function commitGsapPositionFromDrag(
el.removeAttribute("data-hf-drag-initial-offset-y");
};
- const ct = usePlayerStore.getState().currentTime;
+ let effectiveAnim = anim;
if (anim.keyframes) {
const newId = await materializeIfDynamic(anim, iframe, callbacks.commitMutation, selection);
- const effectiveAnim = newId ? { ...anim, id: newId } : anim;
- const dragProps: Record<string, number> = { x: newX, y: newY };
-
- const ts = resolveTweenStart(effectiveAnim);
- const td = resolveTweenDuration(effectiveAnim);
- const outsideRange = ts !== null && td > 0 && (ct < ts - 0.01 || ct > ts + td + 0.01);
- if (outsideRange) {
- await extendTweenAndAddKeyframe(
- selection,
- effectiveAnim,
- dragProps,
- ct,
- ts,
- td,
- callbacks,
- restoreOffset,
- );
- } else {
- await commitKeyframedPosition(selection, effectiveAnim, dragProps, callbacks, restoreOffset);
- }
- } else if (anim.method === "from" || anim.method === "fromTo") {
- const ct = usePlayerStore.getState().currentTime;
- const ts = resolveTweenStart(anim);
- const td = resolveTweenDuration(anim);
- const outsideRange = ts !== null && td > 0 && (ct < ts - 0.01 || ct > ts + td + 0.01);
- const dragProps: Record<string, number> = { x: newX, y: newY };
-
- if (outsideRange && ts !== null) {
- // Split the original from() tween into property groups first.
- await callbacks.commitMutation(
- selection,
- { type: "split-into-property-groups", animationId: anim.id },
- { label: "Split from() for drag", skipReload: true },
- );
-
- const allAnims = callbacks.fetchAnimations ? await callbacks.fetchAnimations() : [];
- const existingPosAnim = allAnims.find(
- (a) => a.propertyGroup === "position" && a.targetSelector === anim.targetSelector,
- );
+ if (newId) effectiveAnim = { ...anim, id: newId };
+ }
- if (existingPosAnim?.keyframes) {
- // Extend the existing position tween
- const posTs = resolveTweenStart(existingPosAnim);
- const posTd = resolveTweenDuration(existingPosAnim);
- if (posTs !== null) {
- await extendTweenAndAddKeyframe(
- selection,
- existingPosAnim,
- { x: newX, y: newY },
- ct,
- posTs,
- posTd,
- callbacks,
- restoreOffset,
- );
- return;
- }
- }
+ const ts = resolveTweenStart(effectiveAnim);
+ const td = resolveTweenDuration(effectiveAnim);
+ const ease = effectiveAnim.keyframes?.easeEach ?? effectiveAnim.ease;
- // No existing position tween — create one
- const newStart = Math.min(ct, ts);
- const newEnd = Math.max(ct, ts + td);
- const newDuration = Math.max(0.01, newEnd - newStart);
- const dragBefore = ct < ts;
- const origStartPct = Math.round(((ts - newStart) / newDuration) * 1000) / 10;
- const origEndPct = Math.round(((ts + td - newStart) / newDuration) * 1000) / 10;
+ let kfs = effectiveAnim.keyframes?.keyframes ?? [];
+ if (kfs.length === 0) {
+ const fromProps = effectiveAnim.fromProperties ?? {};
+ const toProps = effectiveAnim.properties ?? {};
+ const startX =
+ typeof fromProps.x === "number" ? fromProps.x : typeof toProps.x === "number" ? 0 : 0;
+ const startY =
+ typeof fromProps.y === "number" ? fromProps.y : typeof toProps.y === "number" ? 0 : 0;
+ const endX = typeof toProps.x === "number" ? toProps.x : startX;
+ const endY = typeof toProps.y === "number" ? toProps.y : startY;
+ kfs = [
+ { percentage: 0, properties: { x: startX, y: startY } },
+ { percentage: 100, properties: { x: endX, y: endY } },
+ ];
+ }
- const keyframes: Array<{ percentage: number; properties: Record<string, number | string> }> =
- [];
- if (dragBefore) {
- keyframes.push({ percentage: 0, properties: { x: newX, y: newY } });
- if (origStartPct > 0.5 && origStartPct < 99.5) {
- keyframes.push({ percentage: origStartPct, properties: { x: 0, y: 0 } });
- }
- keyframes.push({ percentage: 100, properties: { x: 0, y: 0 } });
- } else {
- keyframes.push({ percentage: 0, properties: { x: 0, y: 0 } });
- if (origEndPct > 0.5 && origEndPct < 99.5) {
- keyframes.push({ percentage: origEndPct, properties: { x: 0, y: 0 } });
- }
- keyframes.push({ percentage: 100, properties: { x: newX, y: newY } });
- }
- keyframes.sort((a, b) => a.percentage - b.percentage);
+ const shifted = kfs.map((kf) => ({
+ percentage: kf.percentage,
+ properties: {
+ ...kf.properties,
+ x: roundTo3((typeof kf.properties.x === "number" ? kf.properties.x : 0) + deltaX),
+ y: roundTo3((typeof kf.properties.y === "number" ? kf.properties.y : 0) + deltaY),
+ },
+ ...(kf.ease ? { ease: kf.ease } : {}),
+ }));
- await callbacks.commitMutation(
- selection,
- {
- type: "add-with-keyframes",
- targetSelector: anim.targetSelector,
- position: roundTo3(newStart),
- duration: roundTo3(newDuration),
- keyframes,
- },
- { label: "Move layer (from extended)", softReload: true, beforeReload: restoreOffset },
- );
- } else {
- // Inside tween range: convert then add keyframe at current time
- const coalesceKey = `gsap:convert-drag:${anim.id}`;
- await callbacks.commitMutation(
- selection,
- {
- type: "convert-to-keyframes",
- animationId: anim.id,
- },
- { label: "Convert from() for drag", skipReload: true, coalesceKey },
- );
- const pct = computeCurrentPercentage(selection, anim);
- await callbacks.commitMutation(
- selection,
- {
- type: "add-keyframe",
- animationId: anim.id,
- percentage: pct,
- properties: dragProps,
- },
- {
- label: `Move layer (keyframe ${pct}%)`,
- softReload: true,
- beforeReload: restoreOffset,
- coalesceKey,
- },
- );
- }
- } else {
- await commitFlatViaKeyframes(
- selection,
- anim,
- { x: newX, y: newY },
- callbacks,
- restoreOffset,
- iframe,
- selector,
- );
- }
+ await callbacks.commitMutation(
+ selection,
+ {
+ type: "replace-with-keyframes",
+ animationId: effectiveAnim.id,
+ targetSelector: effectiveAnim.targetSelector,
+ position: roundTo3(ts ?? 0),
+ duration: roundTo3(td || 1),
+ keyframes: shifted,
+ ease,
+ },
+ { label: "Move animation path", softReload: true, beforeReload: restoreOffset },
+ );
}
diff --git a/packages/studio/src/hooks/gsapDragPositionCommit.ts b/packages/studio/src/hooks/gsapDragPositionCommit.ts
new file mode 100644
index 0000000000..5b6725f8f6
--- /dev/null
+++ b/packages/studio/src/hooks/gsapDragPositionCommit.ts
@@ -0,0 +1,410 @@
+import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
+import type { DomEditSelection } from "../components/editor/domEditingTypes";
+import { usePlayerStore } from "../player/store/playerStore";
+import { resolveTweenStart, resolveTweenDuration } from "../utils/globalTimeCompiler";
+import { roundTo3 } from "../utils/rounding";
+import { computeDraggedGsapPosition } from "./draggedGsapPosition";
+import {
+ type GsapDragCommitCallbacks,
+ computeCurrentPercentage,
+ parkPlayheadOnKeyframe,
+ materializeIfDynamic,
+} from "./gsapDragCommit";
+
+async function extendTweenAndAddKeyframe(
+ selection: DomEditSelection,
+ anim: GsapAnimation,
+ properties: Record<string, number>,
+ targetTime: number,
+ tweenStart: number,
+ tweenDuration: number,
+ callbacks: GsapDragCommitCallbacks,
+ beforeReload?: () => void,
+ backfillDefaults?: Record<string, number>,
+): Promise<void> {
+ const tweenEnd = tweenStart + tweenDuration;
+ const newStart = Math.min(targetTime, tweenStart);
+ const newEnd = Math.max(targetTime, tweenEnd);
+ const newDuration = Math.max(0.01, newEnd - newStart);
+ const existingKfs = anim.keyframes?.keyframes ?? [];
+ const remappedKfs: Array<{ percentage: number; properties: Record<string, number | string> }> =
+ [];
+ for (const kf of existingKfs) {
+ const absTime = tweenStart + (kf.percentage / 100) * tweenDuration;
+ const newPct = Math.round(((absTime - newStart) / newDuration) * 1000) / 10;
+ const props: Record<string, number | string> = { ...kf.properties };
+ for (const k of Object.keys(properties)) {
+ if (!(k in props) && backfillDefaults?.[k] != null) props[k] = backfillDefaults[k];
+ }
+ remappedKfs.push({ percentage: newPct, properties: props });
+ }
+
+ const targetPct = Math.round(((targetTime - newStart) / newDuration) * 1000) / 10;
+ remappedKfs.push({ percentage: targetPct, properties });
+
+ remappedKfs.sort((a, b) => a.percentage - b.percentage);
+
+ await callbacks.commitMutation(
+ selection,
+ {
+ type: "replace-with-keyframes",
+ animationId: anim.id,
+ targetSelector: anim.targetSelector,
+ position: roundTo3(newStart),
+ duration: roundTo3(newDuration),
+ keyframes: remappedKfs,
+ },
+ { label: `Move layer (extended keyframe)`, softReload: true, beforeReload },
+ );
+}
+
+// fallow-ignore-next-line complexity
+async function commitKeyframedPosition(
+ selection: DomEditSelection,
+ anim: GsapAnimation,
+ properties: Record<string, number>,
+ callbacks: GsapDragCommitCallbacks,
+ beforeReload?: () => void,
+ backfillDefaults?: Record<string, number>,
+): Promise<void> {
+ const { activeKeyframePct, setActiveKeyframePct } = usePlayerStore.getState();
+ const computedPct = computeCurrentPercentage(selection, anim);
+ const pct = activeKeyframePct ?? computedPct;
+ await callbacks.commitMutation(
+ selection,
+ {
+ type: "add-keyframe",
+ animationId: anim.id,
+ percentage: pct,
+ properties,
+ ...(backfillDefaults ? { backfillDefaults } : {}),
+ },
+ { label: `Move layer (keyframe ${pct}%)`, softReload: true, beforeReload },
+ );
+ if (activeKeyframePct != null) {
+ setActiveKeyframePct(null);
+ parkPlayheadOnKeyframe(anim, pct);
+ }
+}
+
+interface DragRuntimeGsap {
+ getProperty: (target: Element, key: string) => unknown;
+ set: (target: Element, vars: Record<string, unknown>) => void;
+}
+interface DragRuntimeTimeline {
+ seek: (time: number) => void;
+}
+interface DragRuntime {
+ gsapLib: DragRuntimeGsap;
+ el: Element;
+ mainTl: DragRuntimeTimeline;
+}
+
+function resolveDragRuntime(
+ iframe: HTMLIFrameElement | null | undefined,
+ selector: string | undefined,
+): DragRuntime | null {
+ if (!iframe || !selector) return null;
+ const win = iframe.contentWindow as
+ | (Window & {
+ gsap?: Partial<DragRuntimeGsap>;
+ __timelines?: Record<string, Partial<DragRuntimeTimeline>>;
+ })
+ | null;
+ const gsap = win?.gsap;
+ if (typeof gsap?.getProperty !== "function" || typeof gsap.set !== "function") return null;
+ let el: Element | null = null;
+ try {
+ el = iframe.contentDocument?.querySelector(selector) ?? null;
+ } catch {
+ return null;
+ }
+ if (!el) return null;
+ const timelines = win?.__timelines;
+ const mainTl = timelines ? Object.values(timelines)[0] : undefined;
+ if (typeof mainTl?.seek !== "function") return null;
+ return {
+ gsapLib: gsap as DragRuntimeGsap,
+ el,
+ mainTl: mainTl as DragRuntimeTimeline,
+ };
+}
+
+// fallow-ignore-next-line complexity
+async function commitFlatViaKeyframes(
+ selection: DomEditSelection,
+ anim: GsapAnimation,
+ properties: Record<string, number>,
+ callbacks: GsapDragCommitCallbacks,
+ beforeReload?: () => void,
+ iframe?: HTMLIFrameElement | null,
+ selector?: string,
+ backfillDefaults?: Record<string, number>,
+): Promise<void> {
+ const ct = usePlayerStore.getState().currentTime;
+ const ts = resolveTweenStart(anim);
+ const td = resolveTweenDuration(anim);
+ const { activeKeyframePct, setActiveKeyframePct } = usePlayerStore.getState();
+ const outsideRange =
+ activeKeyframePct == null && ts !== null && td > 0 && (ct < ts - 0.01 || ct > ts + td + 0.01);
+
+ const resolvedFromValues: Record<string, number | string> = {};
+ const runtime = resolveDragRuntime(iframe, selector);
+ if (runtime && ts !== null) {
+ const { gsapLib, el, mainTl } = runtime;
+ const draggedValues: Record<string, number> = {};
+ for (const key of Object.keys(properties)) {
+ const v = Number(gsapLib.getProperty(el, key));
+ if (Number.isFinite(v)) draggedValues[key] = v;
+ }
+ try {
+ gsapLib.set(el, { clearProps: Object.keys(properties).join(",") });
+ mainTl.seek(ts);
+ for (const key of Object.keys(properties)) {
+ const v = Number(gsapLib.getProperty(el, key));
+ if (Number.isFinite(v)) resolvedFromValues[key] = roundTo3(v);
+ }
+ mainTl.seek(ct);
+ } catch (err) {
+ console.warn("[gsap-drag] start-value read failed; using identity from values", err);
+ for (const key of Object.keys(resolvedFromValues)) delete resolvedFromValues[key];
+ } finally {
+ if (Object.keys(draggedValues).length > 0) gsapLib.set(el, draggedValues);
+ }
+ }
+
+ if (outsideRange && ts !== null) {
+ const coalesceKey = `gsap:convert-drag:${anim.id}`;
+ await callbacks.commitMutation(
+ selection,
+ {
+ type: "convert-to-keyframes",
+ animationId: anim.id,
+ ...(Object.keys(resolvedFromValues).length > 0 ? { resolvedFromValues } : {}),
+ },
+ { label: "Convert to keyframes for drag", skipReload: true, coalesceKey },
+ );
+ const fresh = callbacks.fetchAnimations ? await callbacks.fetchAnimations() : [];
+ const converted =
+ fresh.find((a) => a.targetSelector === anim.targetSelector && a.keyframes) ?? anim;
+ const convertedStart = resolveTweenStart(converted) ?? ts;
+ const convertedDur = resolveTweenDuration(converted) || td;
+ await extendTweenAndAddKeyframe(
+ selection,
+ converted,
+ properties,
+ ct,
+ convertedStart,
+ convertedDur,
+ callbacks,
+ beforeReload,
+ );
+ return;
+ }
+
+ const coalesceKey = `gsap:convert-drag:${anim.id}`;
+ await callbacks.commitMutation(
+ selection,
+ {
+ type: "convert-to-keyframes",
+ animationId: anim.id,
+ ...(Object.keys(resolvedFromValues).length > 0 ? { resolvedFromValues } : {}),
+ },
+ { label: "Convert to keyframes for drag", skipReload: true, coalesceKey },
+ );
+ const pct = activeKeyframePct ?? computeCurrentPercentage(selection, anim);
+ const editedSelected = activeKeyframePct != null;
+ if (editedSelected) setActiveKeyframePct(null);
+
+ await callbacks.commitMutation(
+ selection,
+ {
+ type: "add-keyframe",
+ animationId: anim.id,
+ percentage: pct,
+ properties,
+ ...(backfillDefaults ? { backfillDefaults } : {}),
+ },
+ { label: `Move layer (keyframe ${pct}%)`, softReload: true, beforeReload, coalesceKey },
+ );
+ if (editedSelected) parkPlayheadOnKeyframe(anim, pct);
+}
+
+// fallow-ignore-next-line complexity
+export async function commitGsapPositionFromDrag(
+ selection: DomEditSelection,
+ anim: GsapAnimation,
+ studioOffset: { x: number; y: number },
+ gsapPos: { x: number; y: number },
+ iframe: HTMLIFrameElement | null,
+ selector: string,
+ callbacks: GsapDragCommitCallbacks,
+): Promise<void> {
+ const el = selection.element;
+ const { newX, newY, baseGsapX, baseGsapY } = computeDraggedGsapPosition(
+ el,
+ studioOffset,
+ gsapPos,
+ );
+ const origX = Number.parseFloat(el.getAttribute("data-hf-drag-initial-offset-x") ?? "") || 0;
+ const origY = Number.parseFloat(el.getAttribute("data-hf-drag-initial-offset-y") ?? "") || 0;
+ const restoreOffset = () => {
+ el.style.setProperty("--hf-studio-offset-x", `${origX}px`);
+ el.style.setProperty("--hf-studio-offset-y", `${origY}px`);
+ el.removeAttribute("data-hf-drag-initial-offset-x");
+ el.removeAttribute("data-hf-drag-initial-offset-y");
+ };
+
+ const backfillDefaults: Record<string, number> = { x: baseGsapX, y: baseGsapY };
+ const ct = usePlayerStore.getState().currentTime;
+ if (anim.keyframes) {
+ const newId = await materializeIfDynamic(anim, iframe, callbacks.commitMutation, selection);
+ const effectiveAnim = newId ? { ...anim, id: newId } : anim;
+ const dragProps: Record<string, number> = { x: newX, y: newY };
+
+ const ts = resolveTweenStart(effectiveAnim);
+ const td = resolveTweenDuration(effectiveAnim);
+ const outsideRange = ts !== null && td > 0 && (ct < ts - 0.01 || ct > ts + td + 0.01);
+ const hasSelectedKeyframe = usePlayerStore.getState().activeKeyframePct != null;
+ if (outsideRange && !hasSelectedKeyframe) {
+ await extendTweenAndAddKeyframe(
+ selection,
+ effectiveAnim,
+ dragProps,
+ ct,
+ ts,
+ td,
+ callbacks,
+ restoreOffset,
+ backfillDefaults,
+ );
+ } else {
+ await commitKeyframedPosition(
+ selection,
+ effectiveAnim,
+ dragProps,
+ callbacks,
+ restoreOffset,
+ backfillDefaults,
+ );
+ }
+ } else if (anim.method === "from" || anim.method === "fromTo") {
+ const ct = usePlayerStore.getState().currentTime;
+ const ts = resolveTweenStart(anim);
+ const td = resolveTweenDuration(anim);
+ const hasSelectedKeyframe = usePlayerStore.getState().activeKeyframePct != null;
+ const outsideRange =
+ !hasSelectedKeyframe && ts !== null && td > 0 && (ct < ts - 0.01 || ct > ts + td + 0.01);
+ const dragProps: Record<string, number> = { x: newX, y: newY };
+
+ if (outsideRange && ts !== null) {
+ await callbacks.commitMutation(
+ selection,
+ { type: "split-into-property-groups", animationId: anim.id },
+ { label: "Split from() for drag", skipReload: true },
+ );
+
+ const allAnims = callbacks.fetchAnimations ? await callbacks.fetchAnimations() : [];
+ const existingPosAnim = allAnims.find(
+ (a) => a.propertyGroup === "position" && a.targetSelector === anim.targetSelector,
+ );
+
+ if (existingPosAnim?.keyframes) {
+ const posTs = resolveTweenStart(existingPosAnim);
+ const posTd = resolveTweenDuration(existingPosAnim);
+ if (posTs !== null) {
+ await extendTweenAndAddKeyframe(
+ selection,
+ existingPosAnim,
+ { x: newX, y: newY },
+ ct,
+ posTs,
+ posTd,
+ callbacks,
+ restoreOffset,
+ backfillDefaults,
+ );
+ return;
+ }
+ }
+
+ const newStart = Math.min(ct, ts);
+ const newEnd = Math.max(ct, ts + td);
+ const newDuration = Math.max(0.01, newEnd - newStart);
+ const dragBefore = ct < ts;
+ const origStartPct = Math.round(((ts - newStart) / newDuration) * 1000) / 10;
+ const origEndPct = Math.round(((ts + td - newStart) / newDuration) * 1000) / 10;
+
+ const keyframes: Array<{ percentage: number; properties: Record<string, number | string> }> =
+ [];
+ if (dragBefore) {
+ keyframes.push({ percentage: 0, properties: { x: newX, y: newY } });
+ if (origStartPct > 0.5 && origStartPct < 99.5) {
+ keyframes.push({ percentage: origStartPct, properties: { x: 0, y: 0 } });
+ }
+ keyframes.push({ percentage: 100, properties: { x: 0, y: 0 } });
+ } else {
+ keyframes.push({ percentage: 0, properties: { x: 0, y: 0 } });
+ if (origEndPct > 0.5 && origEndPct < 99.5) {
+ keyframes.push({ percentage: origEndPct, properties: { x: 0, y: 0 } });
+ }
+ keyframes.push({ percentage: 100, properties: { x: newX, y: newY } });
+ }
+ keyframes.sort((a, b) => a.percentage - b.percentage);
+
+ const baseKf = {
+ targetSelector: anim.targetSelector,
+ position: roundTo3(newStart),
+ duration: roundTo3(newDuration),
+ keyframes,
+ };
+ await callbacks.commitMutation(
+ selection,
+ existingPosAnim
+ ? { type: "replace-with-keyframes", animationId: existingPosAnim.id, ...baseKf }
+ : { type: "add-with-keyframes", ...baseKf },
+ { label: "Move layer (from extended)", softReload: true, beforeReload: restoreOffset },
+ );
+ } else {
+ const coalesceKey = `gsap:convert-drag:${anim.id}`;
+ await callbacks.commitMutation(
+ selection,
+ {
+ type: "convert-to-keyframes",
+ animationId: anim.id,
+ },
+ { label: "Convert from() for drag", skipReload: true, coalesceKey },
+ );
+ const { activeKeyframePct, setActiveKeyframePct } = usePlayerStore.getState();
+ const pct = activeKeyframePct ?? computeCurrentPercentage(selection, anim);
+ if (activeKeyframePct != null) setActiveKeyframePct(null);
+ await callbacks.commitMutation(
+ selection,
+ {
+ type: "add-keyframe",
+ animationId: anim.id,
+ percentage: pct,
+ properties: dragProps,
+ ...(backfillDefaults ? { backfillDefaults } : {}),
+ },
+ {
+ label: `Move layer (keyframe ${pct}%)`,
+ softReload: true,
+ beforeReload: restoreOffset,
+ coalesceKey,
+ },
+ );
+ }
+ } else {
+ await commitFlatViaKeyframes(
+ selection,
+ anim,
+ { x: newX, y: newY },
+ callbacks,
+ restoreOffset,
+ iframe,
+ selector,
+ backfillDefaults,
+ );
+ }
+}
diff --git a/packages/studio/src/hooks/gsapRuntimeBridge.test.ts b/packages/studio/src/hooks/gsapRuntimeBridge.test.ts
new file mode 100644
index 0000000000..e96efea7c0
--- /dev/null
+++ b/packages/studio/src/hooks/gsapRuntimeBridge.test.ts
@@ -0,0 +1,147 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
+import type { DomEditSelection } from "../components/editor/domEditingTypes";
+import { tryGsapDragIntercept } from "./gsapRuntimeBridge";
+
+/**
+ * Regression: `selectedGsapAnimations` (and the fetch fallback) is an async
+ * server-parse that LAGS a delete-all. A drag in that window would resolve a
+ * phantom position tween from the stale cache and re-commit it — resurrecting the
+ * just-deleted animation. tryGsapDragIntercept must trust the LIVE runtime: when
+ * the runtime has no keyframed/tweened position motion, the element is STATIC
+ * (single-source model), so the drag commits a position-hold `tl.set("#el",{x,y})`
+ * rather than re-committing the phantom tween. The stale `to` parse is ignored.
+ */
+
+// A preview iframe whose runtime timeline holds `children`, resolves the element,
+// and exposes a gsap stub — so the drag can reach the commit path (the guard, not
+// a missing gsap, must be what stops it).
+function fakeIframe(elId: string, children: unknown[]): HTMLIFrameElement {
+ const timeline = { getChildren: () => children, duration: () => 14.6 };
+ const el = { id: elId };
+ return {
+ contentWindow: {
+ __timelines: { "index.html": timeline },
+ gsap: { getProperty: () => 0 },
+ },
+ contentDocument: { querySelector: (sel: string) => (sel === `#${elId}` ? el : null) },
+ } as unknown as HTMLIFrameElement;
+}
+
+// A selection whose element answers the reads commitGsapPositionFromDrag makes —
+// so without the guard the drag would reach commitMutation (resurrecting the tween).
+const fakeElement = {
+ id: "puck-b",
+ style: { getPropertyValue: () => "" },
+ getAttribute: () => null,
+ getBoundingClientRect: () => ({ top: 100, left: 100, width: 50, height: 50 }),
+} as unknown as HTMLElement;
+
+const selection = {
+ id: "puck-b",
+ selector: "#puck-b",
+ element: fakeElement,
+} as unknown as DomEditSelection;
+
+// A stale parse-cache entry: a position tween the server still reports post-delete.
+const stalePositionAnim = {
+ id: "#puck-b-to-1000-position",
+ targetSelector: "#puck-b",
+ propertyGroup: "position",
+ method: "to",
+ properties: { x: -180, y: -60 },
+ position: 1,
+ resolvedStart: 1,
+ duration: 2,
+} as unknown as GsapAnimation;
+
+afterEach(() => vi.restoreAllMocks());
+
+describe("tryGsapDragIntercept — stale-parse guard (no resurrection after delete-all)", () => {
+ it("commits a static set (not the stale tween) when the runtime has no live position motion", async () => {
+ const commitMutation = vi.fn();
+ // Runtime empty (tween deleted) — readRuntimeKeyframes returns null, so the
+ // element is treated as STATIC. The stale `to` parse must NOT be re-committed.
+ const iframe = fakeIframe("puck-b", []);
+
+ const handled = await tryGsapDragIntercept(
+ selection,
+ { x: -50, y: 30 },
+ [stalePositionAnim],
+ iframe,
+ commitMutation,
+ );
+
+ expect(handled).toBe(true);
+ // No existing `set` for the selector → one `add` mutation with `method:"set"`.
+ expect(commitMutation).toHaveBeenCalledTimes(1);
+ const [, mutation] = commitMutation.mock.calls[0];
+ expect(mutation).toMatchObject({
+ type: "add",
+ method: "set",
+ targetSelector: "#puck-b",
+ position: 0,
+ });
+ // Drag delta (-50, 30) off a zero base → the committed set holds that position.
+ expect(mutation.properties).toEqual({ x: -50, y: 30 });
+ // It must NOT resurrect the stale tween via a tween/keyframe mutation.
+ expect(mutation.type).not.toBe("update-property");
+ expect(mutation.type).not.toBe("add-keyframe");
+ });
+
+ it("forwards instantPatch on BOTH coalesced commits when updating an existing static set", async () => {
+ const commitMutation = vi.fn();
+ const iframe = fakeIframe("puck-b", []); // runtime empty → STATIC path
+ // An existing position-hold `set` for the selector → update-in-place (not add).
+ const existingSet = {
+ id: "#puck-b-set",
+ targetSelector: "#puck-b",
+ method: "set",
+ // Tagged as a position group so resolveGroupTween returns it directly
+ // (no split commit), exercising the in-place update path cleanly.
+ propertyGroup: "position",
+ properties: { x: 0, y: 0 },
+ } as unknown as GsapAnimation;
+
+ const handled = await tryGsapDragIntercept(
+ selection,
+ { x: -50, y: 30 },
+ [existingSet],
+ iframe,
+ commitMutation,
+ );
+
+ expect(handled).toBe(true);
+ // The coalesced update-property pair both carry an instantPatch so a partial
+ // (second-POST) failure still leaves the preview patched for what persisted:
+ // the x commit patches {x}, the final y commit patches the full {x,y}.
+ const updates = commitMutation.mock.calls.filter(([, m]) => m.type === "update-property");
+ expect(updates).toHaveLength(2);
+ expect(updates[0][2].instantPatch).toEqual({
+ selector: "#puck-b",
+ change: { kind: "set", props: { x: -50 } },
+ });
+ expect(updates[1][2].instantPatch).toEqual({
+ selector: "#puck-b",
+ change: { kind: "set", props: { x: -50, y: 30 } },
+ });
+ });
+
+ it("does not trip the stale-parse guard when the runtime still has the tween", async () => {
+ const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
+ const liveTween = {
+ targets: () => [{ id: "puck-b" }],
+ vars: { x: -120, y: 40, duration: 1 },
+ duration: () => 1,
+ startTime: () => 1,
+ };
+ // No fake gsap → it returns false later (at the gsapPos read), but the point
+ // is the stale-parse guard must NOT be the reason.
+ const iframe = fakeIframe("puck-b", [liveTween]);
+
+ await tryGsapDragIntercept(selection, { x: -50, y: 30 }, [stalePositionAnim], iframe, vi.fn());
+
+ const staleLogged = logSpy.mock.calls.some((c) => String(c[1] ?? "").includes("stale parse"));
+ expect(staleLogged).toBe(false);
+ });
+});
diff --git a/packages/studio/src/hooks/gsapRuntimeBridge.ts b/packages/studio/src/hooks/gsapRuntimeBridge.ts
index 576372ddae..e0f2c6131c 100644
--- a/packages/studio/src/hooks/gsapRuntimeBridge.ts
+++ b/packages/studio/src/hooks/gsapRuntimeBridge.ts
@@ -13,14 +13,22 @@ import type { DomEditSelection } from "../components/editor/domEditingTypes";
import { usePlayerStore } from "../player/store/playerStore";
import { readAllAnimatedProperties, readGsapProperty } from "./gsapRuntimeReaders";
+import { commitGsapPositionFromDrag } from "./gsapDragPositionCommit";
import {
- commitGsapPositionFromDrag,
+ commitStaticGsapPosition,
+ commitStaticGsapRotation,
+ commitStaticGsapSize,
+ commitWholePathOffset,
computeCurrentPercentage,
+ findPositionSetAnimation,
+ findRotationSetAnimation,
+ findSizeSetAnimation,
materializeIfDynamic,
} from "./gsapDragCommit";
import { resolveTweenStart, resolveTweenDuration } from "../utils/globalTimeCompiler";
import type { GsapDragCommitCallbacks } from "./gsapDragCommit";
import { getIframeGsap, queryIframeElement, selectorFromSelection } from "./gsapShared";
+import { hasNonHoldTweenForElement } from "./gsapRuntimeKeyframes";
import { roundTo3 } from "../utils/rounding";
// ── Runtime reads ──────────────────────────────────────────────────────────
@@ -196,17 +204,9 @@ export async function tryGsapDragIntercept(
iframe: HTMLIFrameElement | null,
commitMutation: GsapDragCommitCallbacks["commitMutation"],
fetchFallbackAnimations?: () => Promise<GsapAnimation[]>,
+ options?: { altKey?: boolean },
): Promise<boolean> {
const selector = selectorFromSelection(selection);
- console.log(
- "[drag:4] tryGsapDragIntercept",
- JSON.stringify({
- sel: selection.id,
- selector,
- animCount: animations.length,
- groups: animations.map((a) => a.propertyGroup).filter(Boolean),
- }),
- );
if (!selector) {
return false;
}
@@ -218,44 +218,62 @@ export async function tryGsapDragIntercept(
commitMutation,
fetchFallbackAnimations,
);
- console.log(
- "[drag:4] resolveGroupTween('position') →",
- resolved
- ? JSON.stringify({ id: resolved.anim.id, group: resolved.anim.propertyGroup })
- : "null",
- );
let posAnim = resolved?.anim ?? null;
+ let resolvedAnimations = resolved?.animations ?? animations;
if (!posAnim) {
posAnim = findGsapPositionAnimation(animations, selector);
if (!posAnim && fetchFallbackAnimations) {
const fresh = await fetchFallbackAnimations();
+ resolvedAnimations = fresh;
posAnim = findGsapPositionAnimation(fresh, selector);
- console.log(
- "[drag:4] findGsapPositionAnimation (fetched) →",
- posAnim ? posAnim.id : "null",
- "freshCount:",
- fresh.length,
- );
}
}
- if (!posAnim) {
- return false;
+
+ const gsapPos = readGsapPositionFromIframe(iframe, selector) ?? { x: 0, y: 0 };
+
+ // STATIC case (single source of truth = GSAP timeline): the element has no LIVE
+ // keyframed/tweened position motion. Use the strict non-hold check — a leftover
+ // position-hold `set` (after a delete-all, or a stale parse that lags it) must
+ // NOT count as live motion. Either way the position belongs in a
+ // `tl.set("#el",{x,y})`, not a keyframe conversion: re-nudge an existing set in
+ // place (idempotent), else add a new one. This also covers the stale-cache
+ // phantom — committing a set is correct because the element genuinely has no live motion.
+ if (!hasNonHoldTweenForElement(iframe, selector)) {
+ const existingSet =
+ posAnim && posAnim.method === "set" && posAnim.targetSelector === selector
+ ? posAnim
+ : findPositionSetAnimation(resolvedAnimations, selector);
+ await commitStaticGsapPosition(selection, offset, gsapPos, selector, existingSet, {
+ commitMutation,
+ fetchAnimations: fetchFallbackAnimations,
+ });
+ return true;
}
- const gsapPos = readGsapPositionFromIframe(iframe, selector);
- if (!gsapPos) {
- return false;
+ if (!posAnim) return false;
+
+ // Verify the anim ID is still valid in the current file. The React-state
+ // `animations` list can lag behind the file after a prior mutation changed
+ // the tween's position/method (which changes the ID). Re-fetch to get the
+ // current ID and avoid a stale-ID remove that creates duplicate tweens.
+ if (fetchFallbackAnimations) {
+ const fresh = await fetchFallbackAnimations();
+ const freshMatch = fresh.find(
+ (a) =>
+ a.targetSelector === posAnim!.targetSelector && a.propertyGroup === posAnim!.propertyGroup,
+ );
+ if (freshMatch && freshMatch.id !== posAnim.id) {
+ posAnim = freshMatch;
+ }
}
- console.log(
- "[drag:4] committing GSAP position drag",
- JSON.stringify({ posAnimId: posAnim.id, gsapPos }),
- );
- await commitGsapPositionFromDrag(selection, posAnim, offset, gsapPos, iframe, selector, {
- commitMutation,
- fetchAnimations: fetchFallbackAnimations,
- });
+ const cbs = { commitMutation, fetchAnimations: fetchFallbackAnimations };
+ if (options?.altKey) {
+ await commitWholePathOffset(selection, posAnim, offset, gsapPos, iframe, selector, cbs);
+ } else {
+ await commitGsapPositionFromDrag(selection, posAnim, offset, gsapPos, iframe, selector, cbs);
+ }
return true;
}
@@ -303,33 +321,14 @@ export async function tryGsapResizeIntercept(
);
let anim = resolved?.anim ?? null;
- if (!anim) {
- // No size-group tween exists — create one. Use the element's timing
- // from any existing animation, or fall back to element data attributes.
- const refAnim = animations[0];
- const elStart =
- refAnim?.resolvedStart ?? (Number.parseFloat(selection.dataAttributes?.start ?? "0") || 0);
- const elDuration = Number.parseFloat(selection.dataAttributes?.duration ?? "5") || 5;
- const ct = usePlayerStore.getState().currentTime;
- const pct = elDuration > 0 ? Math.round(((ct - elStart) / elDuration) * 1000) / 10 : 0;
+ if (!anim || anim.method === "set") {
const sel = selectorFromSelection(selection);
if (!sel) return false;
- await commitMutation(
- selection,
- {
- type: "add-with-keyframes",
- targetSelector: sel,
- position: roundTo3(elStart),
- duration: roundTo3(elDuration),
- keyframes: [
- {
- percentage: Math.max(0, Math.min(100, pct)),
- properties: { width: Math.round(size.width), height: Math.round(size.height) },
- },
- ],
- },
- { label: "Resize (new size keyframe)", softReload: true },
- );
+ const sizeSet = anim?.method === "set" ? anim : findSizeSetAnimation(animations, sel);
+ await commitStaticGsapSize(selection, size, sel, sizeSet, {
+ commitMutation,
+ fetchAnimations: fetchFallbackAnimations,
+ });
return true;
}
@@ -374,6 +373,13 @@ export async function tryGsapResizeIntercept(
{ type: "convert-to-keyframes", animationId: anim.id, resolvedFromValues },
{ label: "Convert to keyframes for resize", skipReload: true, coalesceKey },
);
+ if (fetchFallbackAnimations) {
+ const fresh = await fetchFallbackAnimations();
+ const refreshed = fresh.find(
+ (a) => a.targetSelector === anim!.targetSelector && a.keyframes,
+ );
+ if (refreshed) anim = refreshed;
+ }
}
}
@@ -463,6 +469,9 @@ export async function tryGsapRotationIntercept(
commitMutation: GsapDragCommitCallbacks["commitMutation"],
fetchFallbackAnimations?: () => Promise<GsapAnimation[]>,
): Promise<boolean> {
+ const selector = selectorFromSelection(selection);
+ if (!selector) return false;
+
// Resolve the rotation-group tween, splitting legacy mixed tweens if needed.
const resolved = await resolveGroupTween(
"rotation",
@@ -471,6 +480,7 @@ export async function tryGsapRotationIntercept(
commitMutation,
fetchFallbackAnimations,
);
+ const resolvedAnimations = resolved?.animations ?? animations;
// Fallback: legacy heuristic for hand-written scripts
let anim = resolved?.anim ?? null;
@@ -481,20 +491,27 @@ export async function tryGsapRotationIntercept(
anim = fresh.find((a) => "rotation" in a.properties || a.keyframes) ?? null;
}
}
- if (!anim) return false;
- const selector = selectorFromSelection(selection);
- if (!selector) return false;
+ // `angle` is the ABSOLUTE target rotation resolved by the gesture (gsap base +
+ // pointer sweep) or the inspector — so it IS the new rotation. No base re-add: the
+ // gesture's live preview already gsap.set this value (single source of truth).
+ const newRotation = Math.round(angle);
- let gsapRotation = 0;
- const gsap = getIframeGsap(iframe);
- const rotEl = gsap ? queryIframeElement(iframe, selector) : null;
- if (gsap && rotEl) {
- gsapRotation = Number(gsap.getProperty(rotEl, "rotation")) || 0;
+ // STATIC case (single source of truth = GSAP timeline): no rotation tween, so the
+ // angle belongs in a `tl.set("#el",{rotation})`, not a keyframe conversion —
+ // mirroring the static position set. Idempotent: re-rotate updates an existing
+ // rotation set in place, else add a new one. This replaces the old
+ // `--hf-studio-rotation` CSS-var fallback (the same dual-channel bug class).
+ if (!anim) {
+ const existingSet = findRotationSetAnimation(resolvedAnimations, selector);
+ await commitStaticGsapRotation(selection, newRotation, selector, existingSet, {
+ commitMutation,
+ fetchAnimations: fetchFallbackAnimations,
+ });
+ return true;
}
const pct = computeCurrentPercentage(selection, anim);
- const newRotation = Math.round(gsapRotation + angle);
if (anim.hasUnresolvedKeyframes || anim.hasUnresolvedSelector) {
const newId = await materializeIfDynamic(anim, iframe, commitMutation, selection);
diff --git a/packages/studio/src/hooks/gsapRuntimeKeyframes.test.ts b/packages/studio/src/hooks/gsapRuntimeKeyframes.test.ts
index 91cda720f4..c121067c49 100644
--- a/packages/studio/src/hooks/gsapRuntimeKeyframes.test.ts
+++ b/packages/studio/src/hooks/gsapRuntimeKeyframes.test.ts
@@ -1,5 +1,133 @@
import { describe, expect, it } from "vitest";
-import { arcPathFromMotionPathValue } from "./gsapRuntimeKeyframes";
+import {
+ arcPathFromMotionPathValue,
+ hasNonHoldTweenForElement,
+ readRuntimeKeyframes,
+} from "./gsapRuntimeKeyframes";
+
+// Build a fake preview iframe whose runtime timeline holds the given child tweens
+// and resolves `selector` to `el`.
+function fakeIframe(el: { id: string }, children: unknown[], now?: number): HTMLIFrameElement {
+ const timeline = {
+ getChildren: () => children,
+ duration: () => 14.6,
+ ...(now != null ? { time: () => now } : {}),
+ };
+ return {
+ contentWindow: { __timelines: { "index.html": timeline } },
+ contentDocument: { querySelector: (sel: string) => (sel === `#${el.id}` ? el : null) },
+ } as unknown as HTMLIFrameElement;
+}
+
+describe("readRuntimeKeyframes — zero-duration set must not shadow the keyframed tween", () => {
+ const el = { id: "puck-b" };
+ const holdSet = {
+ targets: () => [el],
+ // `data` is the STUDIO_HOLD_MARKER sentinel ("hf-hold") from core's gsapParser.
+ // TODO(core follow-up): re-export STUDIO_HOLD_MARKER via the @hyperframes/core/
+ // gsap-parser subpath so this fixture can import the const instead of the literal.
+ vars: { x: 0, y: 0, data: "hf-hold" },
+ duration: () => 0,
+ startTime: () => 0,
+ };
+ const kfTween = {
+ targets: () => [el],
+ vars: {
+ keyframes: [
+ { x: 0, y: 0 },
+ { x: -180, y: -60 },
+ { x: -320, y: 40 },
+ { x: -460, y: -20 },
+ ],
+ duration: 3.4,
+ ease: "power1.inOut",
+ },
+ duration: () => 3.4,
+ startTime: () => 1.0,
+ };
+
+ it("reads all 4 keyframes from the to() even when a hold-set precedes it", () => {
+ const read = readRuntimeKeyframes(fakeIframe(el, [holdSet, kfTween]), "#puck-b");
+ expect(read?.keyframes).toHaveLength(4);
+ });
+
+ it("returns null when the element only has a zero-duration set (no real motion)", () => {
+ expect(readRuntimeKeyframes(fakeIframe(el, [holdSet]), "#puck-b")).toBeNull();
+ });
+});
+
+describe("readRuntimeKeyframes — multiple tweens pick the one under the playhead", () => {
+ const el = { id: "puck-a" };
+ // Two non-overlapping gesture recordings → two separate keyframed tweens.
+ const gestureA = {
+ targets: () => [el],
+ vars: {
+ keyframes: [
+ { x: 0, y: 0 },
+ { x: -100, y: 50 },
+ ],
+ duration: 2.03,
+ },
+ duration: () => 2.03,
+ startTime: () => 1.033, // range [1.033, 3.063]
+ };
+ const gestureB = {
+ targets: () => [el],
+ vars: {
+ keyframes: [
+ { x: 10, y: 10 },
+ { x: 20, y: 20 },
+ { x: 30, y: 30 },
+ ],
+ duration: 1.129,
+ },
+ duration: () => 1.129,
+ startTime: () => 3.342, // range [3.342, 4.471]
+ };
+
+ it("playhead inside the SECOND tween reads the second tween (not the first)", () => {
+ const read = readRuntimeKeyframes(fakeIframe(el, [gestureA, gestureB], 3.373), "#puck-a");
+ expect(read?.keyframes).toHaveLength(3); // gestureB
+ });
+
+ it("playhead inside the FIRST tween reads the first tween", () => {
+ const read = readRuntimeKeyframes(fakeIframe(el, [gestureA, gestureB], 2.0), "#puck-a");
+ expect(read?.keyframes).toHaveLength(2); // gestureA
+ });
+
+ it("playhead outside every range falls back to the first keyframed tween", () => {
+ const read = readRuntimeKeyframes(fakeIframe(el, [gestureA, gestureB], 9.0), "#puck-a");
+ expect(read?.keyframes).toHaveLength(2); // gestureA (first)
+ });
+});
+
+describe("hasNonHoldTweenForElement — strict live-tween existence (drag stale-parse guard)", () => {
+ const el = { id: "puck-b" };
+ const holdSet = {
+ targets: () => [el],
+ vars: { x: 0, y: 0, data: "hf-hold" },
+ duration: () => 0,
+ startTime: () => 0,
+ };
+ const liveTween = {
+ targets: () => [el],
+ vars: { x: -120, y: 40, duration: 1 },
+ duration: () => 1,
+ startTime: () => 1,
+ };
+
+ it("true when a non-hold tween targets the element", () => {
+ expect(hasNonHoldTweenForElement(fakeIframe(el, [liveTween]), "#puck-b")).toBe(true);
+ });
+
+ it("false when only a zero-duration hold/set remains (post delete-all)", () => {
+ expect(hasNonHoldTweenForElement(fakeIframe(el, [holdSet]), "#puck-b")).toBe(false);
+ });
+
+ it("false when the element has no tweens at all", () => {
+ expect(hasNonHoldTweenForElement(fakeIframe(el, []), "#puck-b")).toBe(false);
+ });
+});
describe("arcPathFromMotionPathValue", () => {
it("builds arc config from object form { path, curviness }", () => {
diff --git a/packages/studio/src/hooks/gsapRuntimeKeyframes.ts b/packages/studio/src/hooks/gsapRuntimeKeyframes.ts
index 3b1ac272ce..e7be6676df 100644
--- a/packages/studio/src/hooks/gsapRuntimeKeyframes.ts
+++ b/packages/studio/src/hooks/gsapRuntimeKeyframes.ts
@@ -12,20 +12,23 @@ import { buildArcPath, type ArcPathConfig } from "@hyperframes/core/gsap-parser-
import { parsePercentageKeyframes, toAbsoluteTime } from "./gsapShared";
import { roundTo3 } from "../utils/rounding";
-interface RuntimeTween {
+export interface RuntimeTween {
targets?: () => Element[];
vars?: Record<string, unknown>;
duration?: () => number;
startTime?: () => number;
+ invalidate?: () => RuntimeTween;
}
-interface RuntimeTimeline {
+export interface RuntimeTimeline {
getChildren?: (deep: boolean) => RuntimeTween[];
duration?: () => number;
+ time?: () => number;
+ invalidate?: () => RuntimeTimeline;
}
type Pct = { percentage: number; properties: Record<string, number | string> };
-type ReadTween = { keyframes: Pct[]; easeEach?: string; arcPath?: ArcPathConfig };
+export type ReadTween = { keyframes: Pct[]; easeEach?: string; arcPath?: ArcPathConfig };
export interface RuntimeKeyframeEntry {
keyframes: Pct[];
@@ -71,6 +74,17 @@ function isXY(p: unknown): p is { x: number; y: number } {
return !!p && typeof (p as any).x === "number" && typeof (p as any).y === "number";
}
+/**
+ * A tween we must skip when reading keyframes: a zero-duration `set`/hold (incl.
+ * the studio pre-keyframe position hold, tagged `data: STUDIO_HOLD_MARKER`).
+ * These sit before the real keyframed tween and otherwise shadow it — `readTween`
+ * would fall back to a degenerate 2-point flat path from the set's values, hiding
+ * the actual multi-keyframe motion. `!(duration > 0)` also rejects NaN durations.
+ */
+function isZeroDurationSet(duration: number): boolean {
+ return !(duration > 0);
+}
+
/** Coordinates + curviness from a live `vars.motionPath` value (object or array form), or null. */
function coordsFromMotionPath(mp: unknown): {
coords: Array<{ x: number; y: number }>;
@@ -149,6 +163,95 @@ function tweenTiming(tween: RuntimeTween): { start: number; duration: number } {
};
}
+export interface ResolvedRuntimeTween {
+ /** The live GSAP tween targeting the selector. */
+ tween: RuntimeTween;
+ /** The composition timeline that owns it. */
+ timeline: RuntimeTimeline;
+}
+
+/**
+ * Whether a tween's `vars` carry at least one of `channels` as an OWN property.
+ * Used to disambiguate co-located `set`s: an element can have separate
+ * `tl.set("#el",{x,y})` and `tl.set("#el",{rotation})` tweens, and a position
+ * patch must land on the {x,y} set — never the rotation-only one.
+ */
+function varsCarryChannel(vars: Record<string, unknown> | undefined, channels: string[]): boolean {
+ if (!vars) return false;
+ for (const ch of channels) {
+ if (Object.prototype.hasOwnProperty.call(vars, ch)) return true;
+ }
+ return false;
+}
+
+/**
+ * Resolve the live tween targeting `selector` using the SAME all-timelines scan
+ * `readRuntimeKeyframes` uses, so read and write agree on "which tween". With
+ * `kind: "keyframe"` it skips zero-duration `set`s and prefers the tween whose
+ * range contains the playhead (matching the reader). With `kind: "set"` it picks
+ * the zero-duration `set`/hold instead. Returns null when none matches.
+ *
+ * `channels` disambiguates co-located `set`s (CHANNEL-BLIND otherwise): when
+ * provided with `kind: "set"`, a set carrying ONE of those channels wins, and a
+ * set carrying ONLY disjoint channels is skipped (so patching {x,y} never lands
+ * on a rotation-only set). With no channel-matching set, it falls back to the
+ * first matching set (back-compat). `channels` is ignored for `kind: "keyframe"`.
+ */
+export function resolveRuntimeTween(
+ iframe: HTMLIFrameElement | null,
+ selector: string,
+ kind: "keyframe" | "set",
+ compositionId?: string,
+ channels?: string[],
+): ResolvedRuntimeTween | null {
+ const timelines = timelinesOf(iframe);
+ if (!timelines) return null;
+
+ let targetEl: Element | null = null;
+ try {
+ targetEl = iframe?.contentDocument?.querySelector(selector) ?? null;
+ } catch {
+ return null;
+ }
+ if (!targetEl) return null;
+
+ const tlIds = compositionId
+ ? [compositionId]
+ : Object.keys(timelines).filter((k) => typeof timelines[k]?.getChildren === "function");
+
+ const wantChannels = kind === "set" && channels && channels.length > 0 ? channels : null;
+
+ let first: ResolvedRuntimeTween | null = null;
+ let channelMatch: ResolvedRuntimeTween | null = null;
+ for (const tlId of tlIds) {
+ const timeline = timelines[tlId];
+ if (!timeline?.getChildren) continue;
+ const now = typeof timeline.time === "function" ? timeline.time() : null;
+ for (const tween of timeline.getChildren(true)) {
+ if (!tween.vars || !matchesElement(tween, targetEl)) continue;
+ const dur = typeof tween.duration === "function" ? tween.duration() : 0;
+ const isSet = !(dur > 0);
+ if (kind === "set" ? !isSet : isSet) continue;
+ if (wantChannels) {
+ if (varsCarryChannel(tween.vars, wantChannels)) {
+ if (channelMatch === null) channelMatch = { tween, timeline };
+ } else if (first === null) {
+ // A set carrying only disjoint channels: remember as last-resort
+ // fallback, but never prefer it over a channel-matching set.
+ first = { tween, timeline };
+ }
+ continue;
+ }
+ if (first === null) first = { tween, timeline };
+ if (kind === "keyframe" && now != null) {
+ const start = typeof tween.startTime === "function" ? tween.startTime() : 0;
+ if (now >= start - 1e-3 && now <= start + dur + 1e-3) return { tween, timeline };
+ }
+ }
+ }
+ return channelMatch ?? first;
+}
+
/**
* Read keyframes (incl. motionPath arcs) for one selector from the live timeline.
* Returns tween-relative percentages; callers convert to clip-relative.
@@ -160,10 +263,6 @@ export function readRuntimeKeyframes(
): ReadTween | null {
const timelines = timelinesOf(iframe);
if (!timelines) return null;
- const tlId = compositionId || Object.keys(timelines)[0];
- if (!tlId) return null;
- const timeline = timelines[tlId];
- if (!timeline?.getChildren) return null;
let targetEl: Element | null = null;
try {
@@ -173,12 +272,84 @@ export function readRuntimeKeyframes(
}
if (!targetEl) return null;
+ // Search the element's OWN composition timeline. With inlined subcompositions the
+ // preview has multiple timelines (one per composition), and the element belongs to
+ // exactly one — so we can't assume the first key (order isn't stable across soft
+ // reloads, which delete+re-add the rebuilt key). Scan every timeline for tweens
+ // targeting this element; only its composition's timeline matches. An explicit
+ // compositionId still pins the search. (`__proxied` and other non-timeline markers
+ // are skipped by the getChildren guard.)
+ const tlIds = compositionId
+ ? [compositionId]
+ : Object.keys(timelines).filter((k) => typeof timelines[k]?.getChildren === "function");
+ if (tlIds.length === 0) return null;
+
+ // The element can have MORE THAN ONE keyframed tween at disjoint time ranges
+ // (e.g. two non-overlapping gesture recordings → two separate `to()`s). The
+ // overlay must draw the segment under the PLAYHEAD, not blindly the first one
+ // — otherwise recording a second gesture leaves the path stuck on the first.
+ let firstRead: ReadTween | null = null;
+ for (const tlId of tlIds) {
+ const timeline = timelines[tlId];
+ if (!timeline?.getChildren) continue;
+ const now = typeof timeline.time === "function" ? timeline.time() : null;
+ for (const tween of timeline.getChildren(true)) {
+ if (!tween.vars || !matchesElement(tween, targetEl)) continue;
+ const dur = typeof tween.duration === "function" ? tween.duration() : 0;
+ if (isZeroDurationSet(dur)) continue; // skip hold/set tweens (see isZeroDurationSet)
+ const read = readTween(tween.vars);
+ if (!read) continue;
+ if (firstRead === null) firstRead = read;
+ // Prefer the tween whose [start, start+dur] contains the playhead.
+ if (now != null) {
+ const start = typeof tween.startTime === "function" ? tween.startTime() : 0;
+ if (now >= start - 1e-3 && now <= start + dur + 1e-3) return read;
+ }
+ }
+ }
+ // Playhead outside every tween's range (or timeline has no clock): the element
+ // still has motion, so fall back to the first keyframed tween.
+ return firstRead;
+}
+
+/**
+ * Whether the live timeline has at least one NON-HOLD tween (non-zero duration,
+ * not the studio position-hold `set`) targeting `selector`. Stricter than a
+ * truthy `readRuntimeKeyframes`: that returns a flat read for any property-bearing
+ * tween, so it can't distinguish a real animation from a leftover hold/marker.
+ * The drag's stale-parse guard needs this exact distinction — after a delete-all
+ * only a hold may remain, and resurrecting the deleted tween from the stale parse
+ * must be avoided.
+ */
+export function hasNonHoldTweenForElement(
+ iframe: HTMLIFrameElement | null,
+ selector: string,
+ compositionId?: string,
+): boolean {
+ const timelines = timelinesOf(iframe);
+ if (!timelines) return false;
+ const tlId =
+ compositionId ||
+ Object.keys(timelines).find((k) => typeof timelines[k]?.getChildren === "function");
+ if (!tlId) return false;
+ const timeline = timelines[tlId];
+ if (!timeline?.getChildren) return false;
+
+ let targetEl: Element | null = null;
+ try {
+ targetEl = iframe?.contentDocument?.querySelector(selector) ?? null;
+ } catch {
+ return false;
+ }
+ if (!targetEl) return false;
+
for (const tween of timeline.getChildren(true)) {
if (!tween.vars || !matchesElement(tween, targetEl)) continue;
- const read = readTween(tween.vars);
- if (read) return read;
+ const dur = typeof tween.duration === "function" ? tween.duration() : 0;
+ if (isZeroDurationSet(dur)) continue; // skip hold/set tweens (see isZeroDurationSet)
+ if (readTween(tween.vars)) return true;
}
- return null;
+ return false;
}
/** Convert tween-relative keyframes to clip-relative % using the element's clip dims. */
@@ -217,9 +388,10 @@ function addScanEntry(
clipById?: ClipDims,
): void {
if (!tween.targets || !tween.vars) return;
+ const { start, duration } = tweenTiming(tween);
+ if (isZeroDurationSet(duration)) return; // skip hold/set tweens (see isZeroDurationSet)
const read = readTween(tween.vars);
if (!read) return;
- const { start, duration } = tweenTiming(tween);
for (const target of tween.targets()) {
const id = (target as HTMLElement).id;
if (id && !result.has(id)) result.set(id, buildEntry(read, start, duration, clipById?.get(id)));
diff --git a/packages/studio/src/hooks/gsapRuntimePatch.test.ts b/packages/studio/src/hooks/gsapRuntimePatch.test.ts
new file mode 100644
index 0000000000..bf999f9c8e
--- /dev/null
+++ b/packages/studio/src/hooks/gsapRuntimePatch.test.ts
@@ -0,0 +1,462 @@
+import { describe, expect, it, vi } from "vitest";
+import { patchRuntimeTweenInPlace } from "./gsapRuntimePatch";
+
+/**
+ * The helper patches ONE tween's values in `window.__timelines[compKey]` in place,
+ * `invalidate()`s it, and re-seeks via `__player.seek(currentTime)` so a value-only
+ * edit is reflected without re-running the composition. It must return `false`
+ * (caller falls back to a soft reload) whenever it can't confidently apply.
+ *
+ * The fixtures below mimic the runtime timeline shape the reader scans:
+ * a timeline with `getChildren(deep)`, child tweens with `vars`/`targets`/
+ * `duration`/`startTime`/`invalidate`, and an `__player` with `getTime`/`seek`.
+ */
+
+type TweenSpec = {
+ vars: Record<string, unknown>;
+ targetIds: string[];
+ duration: number;
+ startTime?: number;
+};
+
+function makeTween(spec: TweenSpec, el: { id: string }) {
+ const invalidate = vi.fn();
+ return {
+ vars: spec.vars,
+ targets: () => spec.targetIds.map((id) => (id === el.id ? el : { id })),
+ duration: () => spec.duration,
+ startTime: () => spec.startTime ?? 0,
+ invalidate,
+ };
+}
+
+// A preview iframe whose runtime timeline holds the given tweens under `compKey`,
+// resolves `#<el.id>`, and exposes a `__player` clock that re-renders the timeline
+// when `seek` is called (so we can assert the post-seek interpolated value).
+function fakeIframe(
+ el: { id: string },
+ tweens: ReturnType<typeof makeTween>[],
+ opts: {
+ compKey?: string;
+ extraTimelines?: Record<string, unknown>;
+ now?: number;
+ onSeek?: (t: number) => void;
+ } = {},
+): {
+ iframe: HTMLIFrameElement;
+ seek: ReturnType<typeof vi.fn>;
+ timeline: { getChildren: () => unknown[] };
+} {
+ const compKey = opts.compKey ?? "index.html";
+ const now = opts.now ?? 0;
+ const timeline = {
+ getChildren: () => tweens,
+ duration: () => 14.6,
+ time: () => now,
+ };
+ const seek = vi.fn((t: number) => opts.onSeek?.(t));
+ const iframe = {
+ contentWindow: {
+ __timelines: { [compKey]: timeline, ...(opts.extraTimelines ?? {}) },
+ __player: { getTime: () => now, seek },
+ },
+ contentDocument: { querySelector: (sel: string) => (sel === `#${el.id}` ? el : null) },
+ } as unknown as HTMLIFrameElement;
+ return { iframe, seek, timeline };
+}
+
+describe("patchRuntimeTweenInPlace — set tweens", () => {
+ it("patches a tl.set x/y; a simulated re-seek reflects the NEW x/y (not the old)", () => {
+ const el = { id: "box" };
+ // Model the runtime applying the set's vars to the element on seek.
+ const rendered: { x: number; y: number } = { x: 0, y: 0 };
+ const setTween = makeTween(
+ { vars: { x: 0, y: 0 }, targetIds: ["box"], duration: 0, startTime: 0 },
+ el,
+ );
+ const { iframe, seek } = fakeIframe(el, [setTween], {
+ onSeek: () => {
+ rendered.x = setTween.vars.x as number;
+ rendered.y = setTween.vars.y as number;
+ },
+ });
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#box", {
+ kind: "set",
+ props: { x: 120, y: -40 },
+ });
+
+ expect(ok).toBe(true);
+ expect(setTween.vars.x).toBe(120);
+ expect(setTween.vars.y).toBe(-40);
+ expect(setTween.invalidate).toHaveBeenCalled();
+ expect(seek).toHaveBeenCalledTimes(1);
+ expect(rendered).toEqual({ x: 120, y: -40 });
+ });
+
+ it("patches only the rotation channel, leaving x/y untouched", () => {
+ const el = { id: "knob" };
+ const setTween = makeTween(
+ { vars: { x: 10, y: 20, rotation: 0 }, targetIds: ["knob"], duration: 0 },
+ el,
+ );
+ const { iframe } = fakeIframe(el, [setTween]);
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#knob", {
+ kind: "set",
+ props: { rotation: 45 },
+ });
+
+ expect(ok).toBe(true);
+ expect(setTween.vars.rotation).toBe(45);
+ expect(setTween.vars.x).toBe(10);
+ expect(setTween.vars.y).toBe(20);
+ });
+
+ it("patches only the scale channels", () => {
+ const el = { id: "card" };
+ const setTween = makeTween(
+ { vars: { scaleX: 1, scaleY: 1, opacity: 1 }, targetIds: ["card"], duration: 0 },
+ el,
+ );
+ const { iframe } = fakeIframe(el, [setTween]);
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#card", {
+ kind: "set",
+ props: { scaleX: 2, scaleY: 1.5 },
+ });
+
+ expect(ok).toBe(true);
+ expect(setTween.vars.scaleX).toBe(2);
+ expect(setTween.vars.scaleY).toBe(1.5);
+ expect(setTween.vars.opacity).toBe(1);
+ });
+});
+
+describe("patchRuntimeTweenInPlace — channel-aware set resolution", () => {
+ it("patches the {x,y} set, not a co-located rotation-only set", () => {
+ const el = { id: "dual" };
+ const posSet = makeTween({ vars: { x: 0, y: 0 }, targetIds: ["dual"], duration: 0 }, el);
+ const rotSet = makeTween({ vars: { rotation: 0 }, targetIds: ["dual"], duration: 0 }, el);
+ // rotation set listed FIRST — channel-blind resolution would grab it.
+ const { iframe } = fakeIframe(el, [rotSet, posSet]);
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#dual", {
+ kind: "set",
+ props: { x: 33, y: 44 },
+ });
+
+ expect(ok).toBe(true);
+ expect(posSet.vars).toMatchObject({ x: 33, y: 44 });
+ // The rotation set must be untouched (no x/y written into it).
+ expect(rotSet.vars).toEqual({ rotation: 0 });
+ expect(rotSet.invalidate).not.toHaveBeenCalled();
+ expect(posSet.invalidate).toHaveBeenCalled();
+ });
+
+ it("patches the rotation set, not a co-located {x,y} set", () => {
+ const el = { id: "dual2" };
+ const posSet = makeTween({ vars: { x: 5, y: 6 }, targetIds: ["dual2"], duration: 0 }, el);
+ const rotSet = makeTween({ vars: { rotation: 0 }, targetIds: ["dual2"], duration: 0 }, el);
+ // position set listed FIRST.
+ const { iframe } = fakeIframe(el, [posSet, rotSet]);
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#dual2", {
+ kind: "set",
+ props: { rotation: 90 },
+ });
+
+ expect(ok).toBe(true);
+ expect(rotSet.vars).toMatchObject({ rotation: 90 });
+ expect(posSet.vars).toEqual({ x: 5, y: 6 });
+ expect(posSet.invalidate).not.toHaveBeenCalled();
+ expect(rotSet.invalidate).toHaveBeenCalled();
+ });
+
+ it("falls back to the only set when none carries the requested channel", () => {
+ // Back-compat: a single {x,y} set, patched with {x,y} that obviously matches,
+ // plus a set lacking the channel entirely still resolves to a match. Here the
+ // only set carries opacity; patching opacity must still land on it.
+ const el = { id: "solo" };
+ const set = makeTween({ vars: { opacity: 1 }, targetIds: ["solo"], duration: 0 }, el);
+ const { iframe } = fakeIframe(el, [set]);
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#solo", {
+ kind: "set",
+ props: { opacity: 0.5 },
+ });
+
+ expect(ok).toBe(true);
+ expect(set.vars).toMatchObject({ opacity: 0.5 });
+ });
+});
+
+describe("patchRuntimeTweenInPlace — keyframe tweens", () => {
+ it("rebuilds the keyframes; a moved keyframe updates, others unchanged", () => {
+ const el = { id: "puck" };
+ const kfTween = makeTween(
+ {
+ vars: {
+ keyframes: [
+ { x: 0, y: 0 },
+ { x: 100, y: 50 },
+ { x: 200, y: 0 },
+ ],
+ duration: 3,
+ ease: "power1.inOut",
+ },
+ targetIds: ["puck"],
+ duration: 3,
+ startTime: 1,
+ },
+ el,
+ );
+ const { iframe, seek } = fakeIframe(el, [kfTween], { now: 2 });
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#puck", {
+ kind: "keyframes",
+ keyframes: [
+ { x: 0, y: 0 },
+ { x: 140, y: 90 },
+ { x: 200, y: 0 },
+ ],
+ });
+
+ expect(ok).toBe(true);
+ const kfs = kfTween.vars.keyframes as Array<Record<string, number>>;
+ expect(kfs[1]).toEqual({ x: 140, y: 90 });
+ expect(kfs[0]).toEqual({ x: 0, y: 0 });
+ expect(kfs[2]).toEqual({ x: 200, y: 0 });
+ expect(kfTween.invalidate).toHaveBeenCalled();
+ expect(seek).toHaveBeenCalledTimes(1);
+ });
+
+ it("preserves the existing ease when rebuilding keyframes", () => {
+ const el = { id: "puck2" };
+ const kfTween = makeTween(
+ {
+ vars: {
+ keyframes: [
+ { x: 0, y: 0 },
+ { x: 100, y: 0 },
+ ],
+ duration: 2,
+ ease: "back.out",
+ },
+ targetIds: ["puck2"],
+ duration: 2,
+ startTime: 0,
+ },
+ el,
+ );
+ const { iframe } = fakeIframe(el, [kfTween], { now: 1 });
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#puck2", {
+ kind: "keyframes",
+ keyframes: [
+ { x: 0, y: 0 },
+ { x: 250, y: 10 },
+ ],
+ });
+
+ expect(ok).toBe(true);
+ expect(kfTween.vars.ease).toBe("back.out");
+ });
+});
+
+describe("patchRuntimeTweenInPlace — defensive false returns", () => {
+ it("returns false when the selector has no matching tween", () => {
+ const el = { id: "lonely" };
+ const otherTween = makeTween(
+ { vars: { x: 0 }, targetIds: ["someone-else"], duration: 0 },
+ { id: "someone-else" },
+ );
+ const { iframe, seek } = fakeIframe(el, [otherTween]);
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#lonely", { kind: "set", props: { x: 50 } });
+
+ expect(ok).toBe(false);
+ expect(seek).not.toHaveBeenCalled();
+ });
+
+ it("returns false when the selector resolves to no element", () => {
+ const el = { id: "present" };
+ const setTween = makeTween({ vars: { x: 0 }, targetIds: ["present"], duration: 0 }, el);
+ const { iframe } = fakeIframe(el, [setTween]);
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#missing", { kind: "set", props: { x: 50 } });
+ expect(ok).toBe(false);
+ });
+
+ it("returns false for a motionPath arc tween (defers to soft reload)", () => {
+ const el = { id: "flyer" };
+ const arcTween = makeTween(
+ {
+ vars: {
+ motionPath: {
+ path: [
+ { x: 0, y: 0 },
+ { x: 100, y: -50 },
+ { x: 200, y: 0 },
+ ],
+ curviness: 1.5,
+ },
+ duration: 4,
+ },
+ targetIds: ["flyer"],
+ duration: 4,
+ startTime: 0,
+ },
+ el,
+ );
+ const { iframe, seek } = fakeIframe(el, [arcTween], { now: 1 });
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#flyer", {
+ kind: "keyframes",
+ keyframes: [
+ { x: 0, y: 0 },
+ { x: 120, y: -30 },
+ ],
+ });
+
+ expect(ok).toBe(false);
+ expect(arcTween.invalidate).not.toHaveBeenCalled();
+ expect(seek).not.toHaveBeenCalled();
+ });
+
+ it("returns false for a dynamic/computed keyframe value (string expression)", () => {
+ const el = { id: "dyn" };
+ const kfTween = makeTween(
+ {
+ vars: {
+ keyframes: [
+ { x: 0, y: 0 },
+ { x: 100, y: 0 },
+ ],
+ duration: 2,
+ },
+ targetIds: ["dyn"],
+ duration: 2,
+ startTime: 0,
+ },
+ el,
+ );
+ const { iframe } = fakeIframe(el, [kfTween], { now: 1 });
+
+ // A non-finite/string value in the requested change can't be safely expressed
+ // as a static keyframe → defer to soft reload.
+ const ok = patchRuntimeTweenInPlace(iframe, "#dyn", {
+ kind: "keyframes",
+ keyframes: [
+ { x: 0, y: 0 },
+ // @ts-expect-error — intentionally dynamic/computed value
+ { x: "+=random(50,100)", y: 0 },
+ ],
+ });
+
+ expect(ok).toBe(false);
+ });
+
+ it("returns false for a keyframes change against a set-only tween (shape mismatch)", () => {
+ const el = { id: "static" };
+ const setTween = makeTween({ vars: { x: 0, y: 0 }, targetIds: ["static"], duration: 0 }, el);
+ const { iframe } = fakeIframe(el, [setTween]);
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#static", {
+ kind: "keyframes",
+ keyframes: [
+ { x: 0, y: 0 },
+ { x: 50, y: 0 },
+ ],
+ });
+ expect(ok).toBe(false);
+ });
+
+ it("returns false rather than overwriting a dynamic string set value", () => {
+ // The existing set value is a computed GSAP expression ("+=100"). Patching it
+ // with a plain number would silently drop the dynamic intent → defer.
+ const el = { id: "expr" };
+ const setTween = makeTween(
+ { vars: { x: "+=100", y: 0 }, targetIds: ["expr"], duration: 0 },
+ el,
+ );
+ const { iframe, seek } = fakeIframe(el, [setTween]);
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#expr", {
+ kind: "set",
+ props: { x: 50, y: 10 },
+ });
+
+ expect(ok).toBe(false);
+ // Declined → the dynamic expression survives, untouched.
+ expect(setTween.vars.x).toBe("+=100");
+ expect(setTween.invalidate).not.toHaveBeenCalled();
+ expect(seek).not.toHaveBeenCalled();
+ });
+
+ it("never throws — returns false on internal error", () => {
+ const el = { id: "boom" };
+ const explodingTween = {
+ get vars() {
+ throw new Error("boom");
+ },
+ targets: () => [el],
+ duration: () => 0,
+ startTime: () => 0,
+ invalidate: vi.fn(),
+ };
+ const timeline = {
+ getChildren: () => {
+ throw new Error("kaboom");
+ },
+ duration: () => 1,
+ time: () => 0,
+ };
+ const iframe = {
+ contentWindow: {
+ __timelines: { "index.html": timeline },
+ __player: { getTime: () => 0, seek: vi.fn() },
+ },
+ contentDocument: { querySelector: (sel: string) => (sel === "#boom" ? el : null) },
+ } as unknown as HTMLIFrameElement;
+ void explodingTween;
+
+ expect(() =>
+ patchRuntimeTweenInPlace(iframe, "#boom", { kind: "set", props: { x: 1 } }),
+ ).not.toThrow();
+ expect(patchRuntimeTweenInPlace(iframe, "#boom", { kind: "set", props: { x: 1 } })).toBe(false);
+ });
+});
+
+describe("patchRuntimeTweenInPlace — composition isolation", () => {
+ it("patches only the tween in the element's owning timeline, not others", () => {
+ const el = { id: "owned" };
+ const ownTween = makeTween({ vars: { x: 0, y: 0 }, targetIds: ["owned"], duration: 0 }, el);
+ // Another composition's timeline holds a tween for a DIFFERENT element with the
+ // same channel — it must be left untouched.
+ const otherTween = makeTween(
+ { vars: { x: 999, y: 999 }, targetIds: ["someone-else"], duration: 0 },
+ { id: "someone-else" },
+ );
+ const otherTimeline = {
+ getChildren: () => [otherTween],
+ duration: () => 5,
+ time: () => 0,
+ };
+
+ const { iframe } = fakeIframe(el, [ownTween], {
+ compKey: "subscene",
+ extraTimelines: { playground: otherTimeline, __proxied: true },
+ });
+
+ const ok = patchRuntimeTweenInPlace(iframe, "#owned", {
+ kind: "set",
+ props: { x: 7, y: 8 },
+ });
+
+ expect(ok).toBe(true);
+ expect(ownTween.vars).toMatchObject({ x: 7, y: 8 });
+ expect(otherTween.vars).toMatchObject({ x: 999, y: 999 });
+ expect(otherTween.invalidate).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/studio/src/hooks/gsapRuntimePatch.ts b/packages/studio/src/hooks/gsapRuntimePatch.ts
new file mode 100644
index 0000000000..d8d9d5cbe4
--- /dev/null
+++ b/packages/studio/src/hooks/gsapRuntimePatch.ts
@@ -0,0 +1,159 @@
+/**
+ * Patch ONE tween's values in the preview iframe's runtime timeline in place, so a
+ * value-only manual edit (a `tl.set` position/rotation/scale, or a keyframe
+ * value/position change) is reflected INSTANTLY — without re-running the whole
+ * composition.
+ *
+ * Defensive by default: the caller (`runCommit`) falls back to the existing soft
+ * reload whenever this returns `false`. It returns `false` (never silently
+ * mis-patches) when the tween can't be confidently located or the requested change
+ * can't be safely expressed (dynamic/computed values, motionPath arcs, shape
+ * mismatch), and never throws.
+ *
+ * "Which tween" is resolved by the same all-timelines scan `readRuntimeKeyframes`
+ * uses (`resolveRuntimeTween`), so read and write agree on the target.
+ */
+import { resolveRuntimeTween, type RuntimeTween } from "./gsapRuntimeKeyframes";
+
+/** Value-only channels a `tl.set(...)` patch may touch. */
+export interface SetPatchProps {
+ x?: number;
+ y?: number;
+ rotation?: number;
+ scaleX?: number;
+ scaleY?: number;
+ scale?: number;
+ opacity?: number;
+}
+
+/** A single keyframe step's numeric channels (the GSAP array-keyframe form). */
+export type KeyframeStep = Record<string, number>;
+
+export type RuntimeTweenChange =
+ | { kind: "set"; props: SetPatchProps }
+ | { kind: "keyframes"; keyframes: KeyframeStep[] };
+
+const SET_CHANNELS: Array<keyof SetPatchProps> = [
+ "x",
+ "y",
+ "rotation",
+ "scaleX",
+ "scaleY",
+ "scale",
+ "opacity",
+];
+
+type IframeWindow = Window & {
+ __player?: { getTime?: () => number; seek?: (t: number) => void };
+};
+
+function playerOf(iframe: HTMLIFrameElement): IframeWindow["__player"] | null {
+ try {
+ return (iframe.contentWindow as IframeWindow | null)?.__player ?? null;
+ } catch {
+ return null;
+ }
+}
+
+/** Every step value must be a finite number — string/computed values can't round-trip. */
+function keyframesAreStatic(keyframes: KeyframeStep[]): boolean {
+ if (keyframes.length === 0) return false;
+ for (const step of keyframes) {
+ if (!step || typeof step !== "object") return false;
+ for (const v of Object.values(step)) {
+ if (typeof v !== "number" || !Number.isFinite(v)) return false;
+ }
+ }
+ return true;
+}
+
+function patchSet(tween: RuntimeTween, props: SetPatchProps): boolean {
+ const vars = tween.vars;
+ if (!vars) return false;
+ // A `set` carrying a motionPath isn't a plain value set — defer.
+ if ("motionPath" in vars) return false;
+ let touched = false;
+ for (const ch of SET_CHANNELS) {
+ const next = props[ch];
+ if (next === undefined) continue;
+ if (typeof next !== "number" || !Number.isFinite(next)) return false;
+ // A string value here is a dynamic/computed GSAP expression (e.g. "+=100",
+ // "random(...)"). Overwriting it with a plain number would silently drop the
+ // dynamic intent — decline so the caller soft-reloads from the (edited) source.
+ if (typeof vars[ch] === "string") return false;
+ vars[ch] = next;
+ touched = true;
+ }
+ return touched;
+}
+
+function patchKeyframes(tween: RuntimeTween, keyframes: KeyframeStep[]): boolean {
+ const vars = tween.vars;
+ if (!vars) return false;
+ // motionPath arc tweens express motion as a path, not channel keyframes — defer.
+ if ("motionPath" in vars) return false;
+ // Only the array-keyframe form is patched in place; the existing tween must
+ // already carry array keyframes (shape match), or this is a structural change.
+ if (!Array.isArray(vars.keyframes)) return false;
+ if (!keyframesAreStatic(keyframes)) return false;
+ vars.keyframes = keyframes.map((step) => ({ ...step }));
+ return true;
+}
+
+/**
+ * Update one tween's values in `window.__timelines` in place + re-seek to the
+ * current playhead. Returns `true` on a confident patch, `false` otherwise
+ * (caller falls back to a soft reload).
+ */
+export function patchRuntimeTweenInPlace(
+ iframe: HTMLIFrameElement | null,
+ selector: string,
+ change: RuntimeTweenChange,
+ compositionId?: string,
+): boolean {
+ if (!iframe) return false;
+ try {
+ // For a `set` patch, hand the resolver the channels actually being written so
+ // it picks the set whose vars carry them — an element can have separate
+ // {x,y} and {rotation} sets, and a position patch must not corrupt the
+ // rotation set (channel-blind resolution would return the first match).
+ const channels =
+ change.kind === "set"
+ ? Object.keys(change.props).filter(
+ (k) => change.props[k as keyof SetPatchProps] !== undefined,
+ )
+ : undefined;
+ const resolved = resolveRuntimeTween(
+ iframe,
+ selector,
+ change.kind === "set" ? "set" : "keyframe",
+ compositionId,
+ channels,
+ );
+ if (!resolved) return false;
+ const { tween, timeline } = resolved;
+
+ const applied =
+ change.kind === "set"
+ ? patchSet(tween, change.props)
+ : patchKeyframes(tween, change.keyframes);
+ if (!applied) return false;
+
+ // Recompute the tween (and its timeline) from the new vars, then re-render at
+ // the current playhead. `invalidate()` makes GSAP re-read vars on the next render.
+ tween.invalidate?.();
+ timeline.invalidate?.();
+
+ const player = playerOf(iframe);
+ const currentTime =
+ typeof player?.getTime === "function"
+ ? player.getTime()
+ : typeof timeline.time === "function"
+ ? timeline.time()
+ : 0;
+ player?.seek?.(Number.isFinite(currentTime) ? currentTime : 0);
+ return true;
+ } catch {
+ return false;
+ }
+}
diff --git a/packages/studio/src/hooks/gsapScriptCommitTypes.ts b/packages/studio/src/hooks/gsapScriptCommitTypes.ts
index 60a4b4ba32..e60b1282bf 100644
--- a/packages/studio/src/hooks/gsapScriptCommitTypes.ts
+++ b/packages/studio/src/hooks/gsapScriptCommitTypes.ts
@@ -2,6 +2,7 @@ import type { ParsedGsap } from "@hyperframes/core/gsap-parser";
import type { Composition } from "@hyperframes/sdk";
import type { DomEditSelection } from "../components/editor/domEditingTypes";
import type { EditHistoryKind } from "../utils/editHistory";
+import type { RuntimeTweenChange } from "./gsapRuntimePatch";
export interface MutationResult {
ok: boolean;
@@ -26,6 +27,15 @@ export interface CommitMutationOptions {
* (and under distinct keys) run concurrently as before.
*/
serializeKey?: string;
+ /**
+ * Value-only edit fast path. When present, `runCommit` first tries to patch the
+ * one changed tween in the preview's runtime timeline in place
+ * (`patchRuntimeTweenInPlace`) — instant, no composition re-run, no iframe
+ * remount. On a successful patch the reload is skipped entirely (panels still
+ * refresh); when the patch can't be confidently applied it falls back to the
+ * existing soft/full reload path. Structural edits omit this and reload as before.
+ */
+ instantPatch?: { selector: string; change: RuntimeTweenChange };
}
export type CommitMutation = (
@@ -38,7 +48,7 @@ export type SafeGsapCommitMutation = (
selection: DomEditSelection,
mutation: Record<string, unknown>,
options: CommitMutationOptions,
-) => void;
+) => Promise<void>;
export type TrackGsapSaveFailure = (
error: unknown,
diff --git a/packages/studio/src/hooks/gsapShared.test.ts b/packages/studio/src/hooks/gsapShared.test.ts
new file mode 100644
index 0000000000..b31c377534
--- /dev/null
+++ b/packages/studio/src/hooks/gsapShared.test.ts
@@ -0,0 +1,58 @@
+import { describe, it, expect } from "vitest";
+import { parsePercentageKeyframes } from "./gsapShared";
+
+describe("parsePercentageKeyframes", () => {
+ it("parses the object/percentage form", () => {
+ const out = parsePercentageKeyframes({ "0%": { x: 0, y: 0 }, "100%": { x: 9, y: 4 } });
+ expect(out?.keyframes).toEqual([
+ { percentage: 0, properties: { x: 0, y: 0 } },
+ { percentage: 100, properties: { x: 9, y: 4 } },
+ ]);
+ });
+
+ it("parses GSAP array-form keyframes as evenly-distributed steps", () => {
+ // Regression: a multi-point shuttle path authored as `keyframes: [...]` used to
+ // read as null (no `N%` keys) → no motion path. Steps map to i/(n-1)*100%.
+ const out = parsePercentageKeyframes([
+ { x: 0, y: 0 },
+ { x: 520, y: 120 },
+ { x: 1040, y: 0 },
+ { x: 1480, y: 160 },
+ ] as unknown as Record<string, unknown>);
+ expect(out?.keyframes.map((k) => k.percentage)).toEqual([0, 33.3, 66.7, 100]);
+ expect(out?.keyframes[1]!.properties).toEqual({ x: 520, y: 120 });
+ });
+
+ it("strips a per-entry ease without shifting the even index-spacing of the others", () => {
+ // GSAP positions array keyframes by array index, so a `{ ease }` carried on an
+ // entry is a segment ease (skipped as a property) — it must not change where
+ // the surrounding keyframes land. 3 entries → 0 / 50 / 100, even though the
+ // middle entry also carries an ease.
+ const out = parsePercentageKeyframes([
+ { x: 0 },
+ { x: 100, ease: "power2.in" },
+ { x: 200 },
+ ] as unknown as Record<string, unknown>);
+ expect(out?.keyframes.map((k) => k.percentage)).toEqual([0, 50, 100]);
+ expect(out?.keyframes.map((k) => k.properties)).toEqual([{ x: 0 }, { x: 100 }, { x: 200 }]);
+ });
+
+ it("keeps even spacing when an interior array slot has no animatable prop", () => {
+ // A degenerate `{ ease }`-only slot contributes no output keyframe, but it is
+ // still an array slot GSAP allocates a position to — so the remaining entries
+ // keep their original i/(n-1) percentages (0 and 100 for a 3-slot array), not
+ // 0/100 collapsed onto a 2-entry spacing.
+ const out = parsePercentageKeyframes([
+ { x: 0 },
+ { ease: "power2.in" },
+ { x: 200 },
+ ] as unknown as Record<string, unknown>);
+ expect(out?.keyframes.map((k) => k.percentage)).toEqual([0, 100]);
+ expect(out?.keyframes.map((k) => k.properties)).toEqual([{ x: 0 }, { x: 200 }]);
+ });
+
+ it("returns null for keyframes with no positional/animatable props", () => {
+ expect(parsePercentageKeyframes([] as unknown as Record<string, unknown>)).toBeNull();
+ expect(parsePercentageKeyframes({})).toBeNull();
+ });
+});
diff --git a/packages/studio/src/hooks/gsapShared.ts b/packages/studio/src/hooks/gsapShared.ts
index e299f15a53..0b5fccfb0b 100644
--- a/packages/studio/src/hooks/gsapShared.ts
+++ b/packages/studio/src/hooks/gsapShared.ts
@@ -97,16 +97,6 @@ export function queryIframeElement(
}
}
-/** Safely access an iframe's contentDocument, returning null on cross-origin errors. */
-export function getIframeDocument(iframe: HTMLIFrameElement | null): Document | null {
- if (!iframe) return null;
- try {
- return iframe.contentDocument;
- } catch {
- return null;
- }
-}
-
// ── Keyframe parsing ──────────────────────────────────────────────────────────
export interface ParsedPercentageKeyframes {
@@ -125,6 +115,34 @@ export function parsePercentageKeyframes(
const keyframes: ParsedPercentageKeyframes["keyframes"] = [];
let easeEach: string | undefined;
+ // GSAP array-form keyframes — `keyframes: [{x,y}, {x,y}, ...]` — are spread
+ // evenly across the tween by default: GSAP gives each entry an equal share of
+ // the duration unless an entry carries its own `duration`/`delay`, which the
+ // studio never emits. So entry i of n maps to i/(n-1)*100% (n=4 → 0/33.3/66.7/100).
+ // Index spacing counts EVERY array slot, including a degenerate entry that
+ // contributes no animatable prop (it's still a slot GSAP allocates a position
+ // to), so dropping such an entry from the output below must NOT shift the others.
+ // A per-entry `ease` is a segment ease, not a keyframe value, so it's skipped as
+ // a property; there is no array-form `easeEach` (that's an object-form sibling key).
+ // (The object form further down uses explicit "0%" keys instead.) Without this
+ // branch, array-keyframed tweens (e.g. a multi-point shuttle) read as null → no
+ // motion path.
+ if (Array.isArray(kfObj)) {
+ const steps = kfObj as unknown[];
+ steps.forEach((entry, i) => {
+ if (!entry || typeof entry !== "object") return;
+ const percentage = steps.length > 1 ? Math.round((i / (steps.length - 1)) * 1000) / 10 : 0;
+ const properties: Record<string, number | string> = {};
+ for (const [pk, pv] of Object.entries(entry as Record<string, unknown>)) {
+ if (pk === "ease") continue;
+ if (typeof pv === "number") properties[pk] = Math.round(pv * 1000) / 1000;
+ else if (typeof pv === "string") properties[pk] = pv;
+ }
+ if (Object.keys(properties).length > 0) keyframes.push({ percentage, properties });
+ });
+ return keyframes.length > 0 ? { keyframes } : null;
+ }
+
for (const [key, val] of Object.entries(kfObj)) {
if (key === "easeEach") {
if (typeof val === "string") easeEach = val;
diff --git a/packages/studio/src/hooks/gsapTargetCache.ts b/packages/studio/src/hooks/gsapTargetCache.ts
index 68d2c3bc2d..a5ba2b8387 100644
--- a/packages/studio/src/hooks/gsapTargetCache.ts
+++ b/packages/studio/src/hooks/gsapTargetCache.ts
@@ -1,5 +1,3 @@
-import { STUDIO_GSAP_DRAG_INTERCEPT_ENABLED } from "../components/editor/manualEditingAvailability";
-
type TimelineLike = { getChildren?: (nested: boolean) => Array<{ targets?: () => Element[] }> };
let _gsapCachedTimelines: Record<string, TimelineLike> | undefined;
@@ -50,7 +48,6 @@ export function isElementGsapTargeted(
iframe: HTMLIFrameElement | null,
element: HTMLElement,
): boolean {
- if (!STUDIO_GSAP_DRAG_INTERCEPT_ENABLED) return false;
const timelines = readTimelines(iframe);
if (!timelines) return false;
diff --git a/packages/studio/src/hooks/useDomEditSession.ts b/packages/studio/src/hooks/useDomEditSession.ts
index 81e0f50a0c..d4c582a0d4 100644
--- a/packages/studio/src/hooks/useDomEditSession.ts
+++ b/packages/studio/src/hooks/useDomEditSession.ts
@@ -213,10 +213,8 @@ export function useDomEditSession({
handleDomTextFieldStyleCommit,
handleDomAddTextField,
handleDomRemoveTextField,
- handleDomPathOffsetCommit,
handleDomGroupPathOffsetCommit,
handleDomBoxSizeCommit,
- handleDomRotationCommit,
handleDomManualEditsReset,
handleDomEditElementDelete,
handleDomZIndexReorderCommit,
@@ -384,9 +382,7 @@ export function useDomEditSession({
bumpGsapCache,
makeFetchFallback,
trackGsapInteractionFailure,
- handleDomPathOffsetCommit,
handleDomBoxSizeCommit,
- handleDomRotationCommit,
addGsapAnimation,
convertToKeyframes,
setArcPath,
diff --git a/packages/studio/src/hooks/useDomSelection.ts b/packages/studio/src/hooks/useDomSelection.ts
index c6b7b78ba4..898cede6e2 100644
--- a/packages/studio/src/hooks/useDomSelection.ts
+++ b/packages/studio/src/hooks/useDomSelection.ts
@@ -4,7 +4,11 @@ import {
getAllPreviewTargetsFromPointer,
getPreviewTargetFromPointer,
} from "../utils/studioPreviewHelpers";
-import { findMatchingTimelineElementId, type RightPanelTab } from "../utils/studioHelpers";
+import {
+ findMatchingTimelineElementId,
+ findTimelineIdByAncestor,
+ type RightPanelTab,
+} from "../utils/studioHelpers";
import {
domEditSelectionsTargetSame,
domEditSelectionInGroup,
@@ -178,10 +182,13 @@ export function useDomSelection({
setRightCollapsed(false);
setRightPanelTab("design");
}
- const nextSelectedTimelineId = findMatchingTimelineElementId(
- nextSelection,
- timelineElements,
- );
+ const nextSelectedTimelineId =
+ findMatchingTimelineElementId(nextSelection, timelineElements) ??
+ findTimelineIdByAncestor(
+ nextSelection.element,
+ timelineElements,
+ nextSelection.sourceFile || "index.html",
+ );
setSelectedTimelineElementId(nextSelectedTimelineId);
return;
}
diff --git a/packages/studio/src/hooks/useEnableKeyframes.test.ts b/packages/studio/src/hooks/useEnableKeyframes.test.ts
new file mode 100644
index 0000000000..5abc183c30
--- /dev/null
+++ b/packages/studio/src/hooks/useEnableKeyframes.test.ts
@@ -0,0 +1,130 @@
+import { describe, expect, it } from "vitest";
+import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
+import {
+ animatedProps,
+ buildExtendedKeyframes,
+ isPlayheadWithinTween,
+ resolveNewTweenRange,
+} from "./useEnableKeyframes";
+
+function anim(overrides: Partial<GsapAnimation>): GsapAnimation {
+ return {
+ id: "#el-to-0-position",
+ targetSelector: "#el",
+ method: "to",
+ position: 0,
+ properties: {},
+ ...overrides,
+ };
+}
+
+describe("resolveNewTweenRange", () => {
+ // Regression: "add a keyframe" must land at the PLAYHEAD. The runtime auto-stamps
+ // data-start="0" + data-duration=<rootDuration> on every GSAP element, so honoring
+ // data-start as authored timing put the keyframe at 0. Clamping the playhead into
+ // the element's range fixes it (auto-stamp's full range passes the playhead through).
+ it("anchors at the playhead through the auto-stamped full-composition range", () => {
+ // data-start="0", data-duration="14" (the auto-stamp), playhead 4.9 → 4.9
+ expect(resolveNewTweenRange("0", "14", 4.9)).toEqual({ start: 4.9, duration: 9.1 });
+ });
+
+ it("anchors at the playhead when the element has no authored range", () => {
+ expect(resolveNewTweenRange(undefined, undefined, 4)).toEqual({ start: 4, duration: 1 });
+ expect(resolveNewTweenRange(undefined, undefined, 6.123456).start).toBe(6.123);
+ });
+
+ it("never returns a negative start", () => {
+ expect(resolveNewTweenRange(undefined, undefined, -2).start).toBe(0);
+ });
+
+ it("clamps the playhead into a genuinely narrow authored clip", () => {
+ // clip [2.5, 8]: inside → playhead; before → start; after → end
+ expect(resolveNewTweenRange("2.5", "5.5", 4)).toEqual({ start: 4, duration: 4 });
+ expect(resolveNewTweenRange("2.5", "5.5", 1).start).toBe(2.5);
+ expect(resolveNewTweenRange("2.5", "5.5", 99).start).toBe(8);
+ });
+});
+
+describe("animatedProps", () => {
+ it("uses top-level properties when present (flat tween)", () => {
+ expect(animatedProps(anim({ properties: { x: -260 } }))).toEqual(["x"]);
+ });
+
+ it("derives props from keyframe stops when top-level properties is empty (array form)", () => {
+ // Regression: array-form `keyframes: [{x,y},…]` leaves `properties` empty, so
+ // add-keyframe read an empty prop list → empty position → silent no-op.
+ const a = anim({
+ properties: {},
+ keyframes: {
+ format: "object-array",
+ keyframes: [
+ { percentage: 0, properties: { x: 0, y: 0 } },
+ { percentage: 100, properties: { x: -460, y: -20 } },
+ ],
+ },
+ });
+ expect(animatedProps(a).sort()).toEqual(["x", "y"]);
+ });
+
+ it("falls back to x/y for a null anim or one with no resolvable props", () => {
+ expect(animatedProps(null)).toEqual(["x", "y"]);
+ expect(animatedProps(anim({ properties: {} }))).toEqual(["x", "y"]);
+ });
+});
+
+describe("isPlayheadWithinTween", () => {
+ const tween = anim({ position: 1.0, duration: 3.4 }); // range [1.0, 4.4]
+
+ it("is true inside the range (incl. boundaries)", () => {
+ expect(isPlayheadWithinTween(tween, 3.0)).toBe(true);
+ expect(isPlayheadWithinTween(tween, 1.0)).toBe(true);
+ expect(isPlayheadWithinTween(tween, 4.4)).toBe(true);
+ });
+
+ it("is false outside the tween range", () => {
+ expect(isPlayheadWithinTween(tween, 5.767)).toBe(false);
+ expect(isPlayheadWithinTween(tween, 0.5)).toBe(false);
+ });
+
+ it("does not block when the start can't be resolved", () => {
+ expect(isPlayheadWithinTween(anim({ position: "+=1" }), 99)).toBe(true);
+ });
+});
+
+describe("buildExtendedKeyframes", () => {
+ // puck-b: tween [1.0, 4.4], four evenly-distributed stops.
+ const kfAnim = anim({
+ position: 1.0,
+ duration: 3.4,
+ keyframes: {
+ format: "object-array",
+ keyframes: [
+ { percentage: 0, properties: { x: 0, y: 0 } },
+ { percentage: 33.3, properties: { x: -180, y: -60 } },
+ { percentage: 66.7, properties: { x: -320, y: 40 } },
+ { percentage: 100, properties: { x: -460, y: -20 } },
+ ],
+ },
+ });
+
+ it("extends the end and rescales existing stops to keep their absolute timing", () => {
+ const out = buildExtendedKeyframes(kfAnim, 5.767, { x: -460, y: -20 });
+ expect(out.position).toBe(1.0); // start unchanged
+ expect(out.duration).toBe(4.767); // grown to reach the playhead
+ // old end (abs 4.4) is no longer 100% — it slid back inside the longer range
+ const last = out.keyframes[out.keyframes.length - 1]!;
+ expect(last.percentage).toBe(100); // the new keyframe sits at the new end
+ expect(last.properties).toEqual({ x: -460, y: -20 });
+ expect(out.keyframes[0]!.percentage).toBe(0); // old start still anchors 0%
+ expect(out.keyframes.some((k) => k.percentage > 0 && k.percentage < 100)).toBe(true);
+ });
+
+ it("extends the start when the playhead precedes the tween", () => {
+ const out = buildExtendedKeyframes(kfAnim, 0, { x: 0, y: 0 });
+ expect(out.position).toBe(0); // start moved back to the playhead
+ expect(out.duration).toBe(4.4); // end (abs 4.4) unchanged
+ expect(out.keyframes[0]).toEqual({ percentage: 0, properties: { x: 0, y: 0 } });
+ // the old first stop (abs 1.0) is now partway in: 1.0 / 4.4 ≈ 22.7%
+ expect(out.keyframes[1]!.percentage).toBeCloseTo(22.7, 1);
+ });
+});
diff --git a/packages/studio/src/hooks/useEnableKeyframes.ts b/packages/studio/src/hooks/useEnableKeyframes.ts
index 4a7858fa8b..eea4496cda 100644
--- a/packages/studio/src/hooks/useEnableKeyframes.ts
+++ b/packages/studio/src/hooks/useEnableKeyframes.ts
@@ -8,13 +8,19 @@
* Reads GSAP runtime values only (no CSS offset — it applies separately via translate).
*/
import { useCallback } from "react";
-import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
+import type { GsapAnimation, GsapPercentageKeyframe } from "@hyperframes/core/gsap-parser";
import type { DomEditSelection } from "../components/editor/domEditingTypes";
import { usePlayerStore } from "../player/store/playerStore";
import { fetchParsedAnimations, getAnimationsForElement } from "./useGsapTweenCache";
import { selectorFromSelection, computeElementPercentage } from "./gsapShared";
+import {
+ resolveTweenStart,
+ resolveTweenDuration,
+ isTimeWithinTween,
+} from "../utils/globalTimeCompiler";
import { POSITION_PROPS } from "./gsapRuntimeReaders";
import { roundTo3 } from "../utils/rounding";
+import { nearestPointOnPath } from "../components/editor/motionPathGeometry";
export interface EnableKeyframesSession {
domEditSelection: DomEditSelection | null;
@@ -37,6 +43,70 @@ export interface EnableKeyframesSession {
) => Promise<void>;
}
+/**
+ * Which animated properties to capture from the live element. Array-form keyframe
+ * tweens (`keyframes: [{x,y},…]`) leave `anim.properties` empty — the props live in
+ * the keyframe stops — so fall back to the union of the stops' keys, then to x/y.
+ */
+export function animatedProps(anim: GsapAnimation | null): string[] {
+ if (!anim) return ["x", "y"];
+ const own = Object.keys(anim.properties ?? {});
+ if (own.length > 0) return own;
+ const stops = anim.keyframes?.keyframes;
+ if (stops?.length) {
+ const keys = new Set<string>();
+ for (const stop of stops) for (const k of Object.keys(stop.properties ?? {})) keys.add(k);
+ if (keys.size > 0) return [...keys];
+ }
+ return ["x", "y"];
+}
+
+/**
+ * Whether the playhead sits inside an animation's tween range. When the tween's
+ * start can't be resolved we don't block (the percentage falls back to clip range,
+ * preserving prior behavior for elements without explicit timing).
+ */
+export function isPlayheadWithinTween(anim: GsapAnimation, currentTime: number): boolean {
+ const start = resolveTweenStart(anim);
+ if (start === null) return true;
+ return isTimeWithinTween(currentTime, start, resolveTweenDuration(anim));
+}
+
+/**
+ * Grow a keyframe tween's range to reach a playhead that sits outside it, and add a
+ * keyframe there. Existing keyframes keep their *absolute* timing (percentages
+ * rescale into the new range), so the current motion is preserved — the playhead
+ * just becomes a new hold at the start or end. Used when "add keyframe at playhead"
+ * fires beyond the tween instead of disabling the action.
+ */
+export function buildExtendedKeyframes(
+ anim: GsapAnimation,
+ currentTime: number,
+ position: Record<string, number>,
+): { position: number; duration: number; keyframes: GsapPercentageKeyframe[] } {
+ const oldStart = resolveTweenStart(anim) ?? 0;
+ const oldDuration = resolveTweenDuration(anim);
+ const newStart = Math.min(oldStart, currentTime);
+ const newEnd = Math.max(oldStart + oldDuration, currentTime);
+ const newDuration = roundTo3(newEnd - newStart);
+ const toPct = (absoluteTime: number) =>
+ newDuration > 0
+ ? Math.max(
+ 0,
+ Math.min(100, Math.round(((absoluteTime - newStart) / newDuration) * 1000) / 10),
+ )
+ : 0;
+ const stops = anim.keyframes?.keyframes ?? [];
+ const rescaled: GsapPercentageKeyframe[] = stops.map((stop) => ({
+ percentage: toPct(oldStart + (stop.percentage / 100) * oldDuration),
+ properties: stop.properties,
+ ...(stop.ease ? { ease: stop.ease } : {}),
+ }));
+ const added: GsapPercentageKeyframe = { percentage: toPct(currentTime), properties: position };
+ const keyframes = [...rescaled, added].sort((a, b) => a.percentage - b.percentage);
+ return { position: roundTo3(newStart), duration: newDuration, keyframes };
+}
+
function readElementPosition(
iframe: HTMLIFrameElement | null,
sel: DomEditSelection,
@@ -55,7 +125,9 @@ function readElementPosition(
const element = sel.element;
if (!element?.isConnected || !gsap?.getProperty) return result;
- const props = anim ? Object.keys(anim.properties) : ["x", "y", "opacity"];
+ // ponytail: a brand-new tween captures position only — bundling opacity made it
+ // a mixed group that the position-only drag intercept couldn't resolve.
+ const props = animatedProps(anim);
for (const prop of props) {
const val = Number(gsap.getProperty(element, prop));
if (!Number.isFinite(val)) continue;
@@ -65,18 +137,201 @@ function readElementPosition(
return result;
}
-async function fetchAnimationsForElement(sel: DomEditSelection): Promise<GsapAnimation[]> {
+/**
+ * Range for a brand-new keyframe tween created via "Enable keyframes" on an element
+ * with no existing animation. "Add a keyframe" must land at the PLAYHEAD.
+ *
+ * The runtime auto-stamps `data-start="0"` + `data-duration=<rootDuration>` on every
+ * timeline element, so we can't treat `data-start` as authored timing (doing so put
+ * the keyframe at 0). Instead, clamp the playhead into the element's [start, end]
+ * range: the auto-stamp's full-composition range passes the playhead through
+ * unchanged, while a genuinely narrow authored clip still clamps sensibly.
+ */
+export function resolveNewTweenRange(
+ authoredStart: string | undefined,
+ authoredDuration: string | undefined,
+ currentTime: number,
+): { start: number; duration: number } {
+ const t = Math.max(0, roundTo3(currentTime));
+ const start = authoredStart != null ? Number.parseFloat(authoredStart) : Number.NaN;
+ const duration = authoredDuration != null ? Number.parseFloat(authoredDuration) : Number.NaN;
+ if (!Number.isFinite(start) || !Number.isFinite(duration) || duration <= 0) {
+ return { start: t, duration: 1 };
+ }
+ const end = start + duration;
+ const clampedStart = Math.min(Math.max(t, start), end);
+ return { start: clampedStart, duration: Math.max(0.5, roundTo3(end - clampedStart)) };
+}
+
+// Authoritative parse of the current source for `sel`. Returns `null` when the
+// fetch can't run (no projectId / request failed) so callers can distinguish
+// "unavailable" from a genuine empty result (e.g. after a delete-all). An empty
+// array means the source was read and the element has no animations.
+async function tryFetchAnimationsForElement(
+ sel: DomEditSelection,
+): Promise<GsapAnimation[] | null> {
const projectId = window.location.hash.match(/project\/([^?/]+)/)?.[1];
- if (!projectId) return [];
+ if (!projectId) return null;
const sourceFile = sel.sourceFile || "index.html";
const parsed = await fetchParsedAnimations(projectId, sourceFile);
- if (!parsed) return [];
+ if (!parsed) return null;
return getAnimationsForElement(parsed.animations, {
id: sel.id,
selector: sel.selector,
});
}
+async function fetchAnimationsForElement(sel: DomEditSelection): Promise<GsapAnimation[]> {
+ return (await tryFetchAnimationsForElement(sel)) ?? [];
+}
+
+/**
+ * Apply "add keyframe at playhead" to a tween that already has x/y keyframes:
+ * toggle off an existing stop, add one at the playhead's tween-relative %, or —
+ * when the playhead sits outside the tween — extend the range to reach it (see
+ * buildExtendedKeyframes). Shared by native keyframe tweens and flat tweens that
+ * were just converted, so both behave identically.
+ */
+async function applyKeyframeAtPlayhead(
+ session: EnableKeyframesSession,
+ sel: DomEditSelection,
+ kfAnim: GsapAnimation,
+ t: number,
+ iframe: HTMLIFrameElement | null,
+): Promise<void> {
+ if (!isPlayheadWithinTween(kfAnim, t)) {
+ const position = readElementPosition(iframe, sel, kfAnim);
+ const selector = selectorFromSelection(sel);
+ if (selector && Object.keys(position).length > 0 && session.commitMutation) {
+ const extended = buildExtendedKeyframes(kfAnim, t, position);
+ await session.commitMutation(
+ {
+ type: "replace-with-keyframes",
+ animationId: kfAnim.id,
+ targetSelector: selector,
+ position: extended.position,
+ duration: extended.duration,
+ keyframes: extended.keyframes,
+ ease: kfAnim.ease,
+ },
+ { label: "Add keyframe", softReload: true },
+ );
+ }
+ return;
+ }
+ const pct = computeElementPercentage(t, sel, kfAnim);
+ const existing = kfAnim.keyframes?.keyframes.find((k) => Math.abs(k.percentage - pct) <= 1);
+ if (existing) {
+ session.handleGsapRemoveKeyframe(kfAnim.id, existing.percentage);
+ return;
+ }
+ if (session.handleGsapAddKeyframeBatch) {
+ const position = readElementPosition(iframe, sel, kfAnim);
+ if (Object.keys(position).length > 0) {
+ await session.handleGsapAddKeyframeBatch(kfAnim.id, pct, position);
+ }
+ }
+}
+
+/**
+ * A set() is an instantaneous hold. "Add keyframe at playhead" promotes it to a
+ * two-stop tween from the set's time to the playhead — the held value at 0%, the
+ * live value at 100% — giving the user something to animate. No-op if the playhead
+ * is at or before the set.
+ */
+async function promoteSetToKeyframes(
+ session: EnableKeyframesSession,
+ sel: DomEditSelection,
+ setAnim: GsapAnimation,
+ t: number,
+ iframe: HTMLIFrameElement | null,
+): Promise<void> {
+ const selector = selectorFromSelection(sel);
+ const setStart = resolveTweenStart(setAnim) ?? 0;
+ if (!selector || !session.commitMutation || t <= setStart) return;
+ const endPosition = readElementPosition(iframe, sel, setAnim);
+ if (Object.keys(endPosition).length === 0) return;
+ const startPosition: Record<string, number> = {};
+ for (const key of Object.keys(endPosition)) {
+ const held = setAnim.properties?.[key];
+ if (typeof held === "number") startPosition[key] = held;
+ }
+ await session.commitMutation(
+ {
+ type: "replace-with-keyframes",
+ animationId: setAnim.id,
+ targetSelector: selector,
+ position: roundTo3(setStart),
+ duration: roundTo3(t - setStart),
+ keyframes: [
+ {
+ percentage: 0,
+ properties: Object.keys(startPosition).length > 0 ? startPosition : endPosition,
+ },
+ { percentage: 100, properties: endPosition },
+ ],
+ ease: setAnim.ease,
+ },
+ { label: "Add keyframe", softReload: true },
+ );
+}
+
+/**
+ * An arc (motionPath) tween — its waypoints are reconstructed onto `keyframes`, so
+ * it must be edited as waypoints (not x/y keyframes, which would break the curve).
+ * "Add keyframe at playhead" drops a waypoint where the element currently sits on
+ * the path, inserted at the matching segment so the curve is preserved. Outside the
+ * range, extend the duration so the motion reaches the playhead.
+ */
+async function applyArcWaypointAtPlayhead(
+ session: EnableKeyframesSession,
+ sel: DomEditSelection,
+ arcAnim: GsapAnimation,
+ t: number,
+ iframe: HTMLIFrameElement | null,
+): Promise<void> {
+ if (!session.commitMutation) return;
+ if (!isPlayheadWithinTween(arcAnim, t)) {
+ const start = resolveTweenStart(arcAnim) ?? 0;
+ if (t > start) {
+ await session.commitMutation(
+ {
+ type: "update-meta",
+ animationId: arcAnim.id,
+ updates: { duration: roundTo3(t - start) },
+ },
+ { label: "Extend motion path", softReload: true },
+ );
+ }
+ return;
+ }
+ const live = readElementPosition(iframe, sel, arcAnim);
+ if (typeof live.x !== "number" || typeof live.y !== "number") return;
+ const liveX = live.x;
+ const liveY = live.y;
+ const nodes = (arcAnim.keyframes?.keyframes ?? [])
+ .map((k) => ({ x: k.properties.x, y: k.properties.y }))
+ .filter(
+ (p): p is { x: number; y: number } => typeof p.x === "number" && typeof p.y === "number",
+ );
+ // Don't duplicate a waypoint that already sits where the element is (e.g. at the
+ // path endpoints).
+ const WAYPOINT_MERGE_PX = 6;
+ if (nodes.some((n) => Math.hypot(n.x - liveX, n.y - liveY) <= WAYPOINT_MERGE_PX)) return;
+ const proj = nearestPointOnPath(liveX, liveY, nodes);
+ if (!proj) return;
+ await session.commitMutation(
+ {
+ type: "add-motion-path-point",
+ animationId: arcAnim.id,
+ index: proj.segIndex + 1,
+ x: liveX,
+ y: liveY,
+ },
+ { label: "Add waypoint", softReload: true },
+ );
+}
+
// fallow-ignore-next-line complexity
export function useEnableKeyframes(
sessionRef: React.RefObject<EnableKeyframesSession | undefined>,
@@ -90,41 +345,45 @@ export function useEnableKeyframes(
const t = usePlayerStore.getState().currentTime;
const iframe = session.previewIframeRef?.current ?? null;
- let anims = session.selectedGsapAnimations;
- if (anims.length === 0) {
- anims = await fetchAnimationsForElement(sel);
- }
-
- const kfAnim = anims.find((a) => a.keyframes);
- const flatAnim = anims.find((a) => !a.keyframes);
-
- if (kfAnim?.keyframes) {
- const pct = computeElementPercentage(t, sel);
- const existing = kfAnim.keyframes.keyframes.find((k) => Math.abs(k.percentage - pct) <= 1);
- if (existing) {
- session.handleGsapRemoveKeyframe(kfAnim.id, existing.percentage);
- } else if (session.handleGsapAddKeyframeBatch) {
- const position = readElementPosition(iframe, sel, kfAnim);
- if (Object.keys(position).length > 0) {
- await session.handleGsapAddKeyframeBatch(kfAnim.id, pct, position);
- }
- }
- } else if (flatAnim) {
- const position = readElementPosition(iframe, sel, flatAnim);
- const hasPosition = Object.keys(position).length > 0;
+ // `selectedGsapAnimations` is a studio-side selection cache that can lag a
+ // mutation — e.g. right after a delete-all it may still hold the just-removed
+ // tween, which would route us into the wrong branch below (editing a tween
+ // that no longer exists in source). Prefer the authoritative parse of the
+ // current source; an empty parse is a valid "no animations" result and is
+ // honored. Fall back to the cache only when the fetch couldn't run at all
+ // (no projectId / request failed), preserving prior behavior offline.
+ const fetched = await tryFetchAnimationsForElement(sel);
+ const anims = fetched ?? session.selectedGsapAnimations;
- await session.handleGsapConvertToKeyframes(flatAnim.id, hasPosition ? position : undefined);
+ // An arc/motionPath tween carries reconstructed x/y keyframes too, so match it
+ // first and edit it as waypoints — treating it as plain keyframes would break
+ // the curve.
+ const arcAnim = anims.find((a) => a.arcPath);
+ const kfAnim = anims.find((a) => a.keyframes && !a.arcPath);
+ const setAnim = anims.find((a) => a.method === "set" && !a.keyframes && !a.arcPath);
+ const flatAnim = anims.find((a) => !a.keyframes && !a.arcPath && a.method !== "set");
- const pct = computeElementPercentage(t, sel);
- if (pct > 1 && pct < 99 && hasPosition && session.handleGsapAddKeyframeBatch) {
- await session.handleGsapAddKeyframeBatch(flatAnim.id, pct, position);
- await session.handleGsapAddKeyframeBatch(flatAnim.id, 100, position);
- }
+ if (arcAnim) {
+ await applyArcWaypointAtPlayhead(session, sel, arcAnim, t, iframe);
+ } else if (kfAnim) {
+ await applyKeyframeAtPlayhead(session, sel, kfAnim, t, iframe);
+ } else if (setAnim) {
+ await promoteSetToKeyframes(session, sel, setAnim, t, iframe);
+ } else if (flatAnim) {
+ // Convert the flat tween (to/from/fromTo) to its natural keyframes — no
+ // resolvedFromValues, so the 0%/100% stops keep the real start→end motion
+ // (passing the playhead value would flatten it). Then apply uniformly so an
+ // out-of-range playhead extends the range just like a keyframe tween.
+ await session.handleGsapConvertToKeyframes(flatAnim.id);
+ const converted = (await fetchAnimationsForElement(sel)).find((a) => a.keyframes);
+ if (converted) await applyKeyframeAtPlayhead(session, sel, converted, t, iframe);
} else {
const position = readElementPosition(iframe, sel, null);
- const pct = computeElementPercentage(t, sel);
- const elStart = Number.parseFloat(sel.dataAttributes?.start ?? "0") || 0;
- const elDuration = Number.parseFloat(sel.dataAttributes?.duration ?? "1") || 1;
+ const { start: elStart, duration: elDuration } = resolveNewTweenRange(
+ sel.dataAttributes?.start,
+ sel.dataAttributes?.duration,
+ t,
+ );
const selector = selectorFromSelection(sel);
if (!selector) {
@@ -135,19 +394,13 @@ export function useEnableKeyframes(
if (Object.keys(position).length === 0) {
position.x = 0;
position.y = 0;
- position.opacity = 1;
}
+ // One keyframe at the playhead — a single diamond capturing the current
+ // value. Motion comes from the user adding/dragging more keyframes later;
+ // creating 0%+100% up front showed two diamonds for a single "add keyframe".
const keyframes: Array<{ percentage: number; properties: Record<string, number | string> }> =
[{ percentage: 0, properties: { ...position } }];
- if (pct > 1 && pct < 99) {
- keyframes.push({ percentage: pct, properties: { ...position } });
- }
- keyframes.push({
- percentage: 100,
- properties: { ...position },
- auto: true,
- } as (typeof keyframes)[number]);
if (session.commitMutation) {
await session.commitMutation(
diff --git a/packages/studio/src/hooks/useGestureCommit.ts b/packages/studio/src/hooks/useGestureCommit.ts
index 35bd494270..18488534d1 100644
--- a/packages/studio/src/hooks/useGestureCommit.ts
+++ b/packages/studio/src/hooks/useGestureCommit.ts
@@ -3,6 +3,7 @@
* Extracted from App.tsx to keep file sizes under the 600-line limit.
*/
import { useState, useCallback, useRef, useEffect } from "react";
+import { editLog } from "../utils/editDebugLog";
import { useGestureRecording } from "./useGestureRecording";
import { simplifyGestureSamples } from "../utils/rdpSimplify";
import { usePlayerStore } from "../player";
@@ -11,6 +12,46 @@ import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
import { roundTo3 } from "../utils/rounding";
import { classifyPropertyGroup } from "@hyperframes/core/gsap-parser";
+type RecordedKeyframe = { percentage: number; properties: Record<string, number | string> };
+
+/**
+ * Split recorded keyframes into one keyframe-set per property group (position /
+ * scale / rotation / …), each keyframe carrying only that group's props.
+ *
+ * A mixed-prop gesture (e.g. x/y + opacity) emitted as ONE add-with-keyframes
+ * mutation parses back as an untagged legacy mixed tween, which breaks the
+ * position-only drag intercept (it can't find a pure position tween to edit).
+ * Emitting one tween per group keeps the position tween tagged and editable.
+ * Keyframes with no prop in a group are dropped from that group's set.
+ */
+function partitionKeyframesByGroup(keyframes: RecordedKeyframe[]): RecordedKeyframe[][] {
+ // Preserve first-seen group order for deterministic, stable mutation ordering.
+ const groupOrder: string[] = [];
+ const byGroup = new Map<string, RecordedKeyframe[]>();
+ for (const kf of keyframes) {
+ const perGroup = new Map<string, Record<string, number | string>>();
+ for (const [key, value] of Object.entries(kf.properties)) {
+ const group = classifyPropertyGroup(key);
+ let props = perGroup.get(group);
+ if (!props) {
+ props = {};
+ perGroup.set(group, props);
+ }
+ props[key] = value;
+ }
+ for (const [group, props] of perGroup) {
+ let set = byGroup.get(group);
+ if (!set) {
+ set = [];
+ byGroup.set(group, set);
+ groupOrder.push(group);
+ }
+ set.push({ percentage: kf.percentage, properties: props });
+ }
+ }
+ return groupOrder.map((group) => byGroup.get(group)!);
+}
+
// Minimal subset of the session used by gesture commit
interface GestureSessionRef {
domEditSelection: DomEditSelection | null;
@@ -121,76 +162,100 @@ export function useGestureCommit({
? allAnims.find((a) => a.propertyGroup === "position" && a.targetSelector === selector)
: undefined;
if (existingPositionTween) {
- const tweenStart = existingPositionTween.resolvedStart ?? 0;
- const tweenDur = existingPositionTween.duration ?? duration;
- const tweenEnd = tweenStart + tweenDur;
- const recEnd = recStart + duration;
-
- // Only merge if the recording overlaps the existing tween's time range.
- // No overlap → fall through to add-with-keyframes (creates a separate tween).
- const overlaps = recStart < tweenEnd + 0.05 && recEnd > tweenStart - 0.05;
-
- if (overlaps) {
- const existingKfs = existingPositionTween.keyframes?.keyframes ?? [];
- const rangeStartPct =
- tweenDur > 0 ? Math.max(0, ((recStart - tweenStart) / tweenDur) * 100) : 0;
- const rangeEndPct =
- tweenDur > 0 ? Math.min(100, ((recEnd - tweenStart) / tweenDur) * 100) : 100;
-
- const preserved = existingKfs
- .filter(
- (kf) => kf.percentage < rangeStartPct - 0.5 || kf.percentage > rangeEndPct + 0.5,
- )
- .map((kf) => ({
- percentage: kf.percentage,
- properties: kf.properties,
- ...(kf.ease ? { ease: kf.ease } : {}),
- }));
-
- const mapped = keyframes.map((kf) => ({
- percentage: rangeStartPct + (kf.percentage / 100) * (rangeEndPct - rangeStartPct),
- properties: kf.properties,
- }));
-
- const merged = [...preserved, ...mapped].sort((a, b) => a.percentage - b.percentage);
-
+ if (existingPositionTween.method === "set") {
+ // A `set` is a static hold, not a tween to merge into — replace it with
+ // the recorded motion (which already starts from the set's position).
await liveSession.commitMutation(
{
type: "replace-with-keyframes",
animationId: existingPositionTween.id,
targetSelector: selector,
- position:
- typeof existingPositionTween.position === "number"
- ? existingPositionTween.position
- : tweenStart,
- duration: tweenDur,
- keyframes: merged,
+ position: roundTo3(recStart),
+ duration: roundTo3(duration),
+ keyframes,
},
- { label: "Gesture recording (merge)", softReload: true },
+ { label: "Gesture recording (replace set)", softReload: true },
);
} else {
+ const tweenStart = existingPositionTween.resolvedStart ?? 0;
+ const tweenDur = existingPositionTween.duration ?? duration;
+ const tweenEnd = tweenStart + tweenDur;
+ const recEnd = recStart + duration;
+
+ // Only merge if the recording overlaps the existing tween's time range.
+ // No overlap → fall through to add-with-keyframes (creates a separate tween).
+ const overlaps = recStart < tweenEnd + 0.05 && recEnd > tweenStart - 0.05;
+
+ if (overlaps) {
+ const existingKfs = existingPositionTween.keyframes?.keyframes ?? [];
+ const rangeStartPct =
+ tweenDur > 0 ? Math.max(0, ((recStart - tweenStart) / tweenDur) * 100) : 0;
+ const rangeEndPct =
+ tweenDur > 0 ? Math.min(100, ((recEnd - tweenStart) / tweenDur) * 100) : 100;
+
+ const preserved = existingKfs
+ .filter(
+ (kf) => kf.percentage < rangeStartPct - 0.5 || kf.percentage > rangeEndPct + 0.5,
+ )
+ .map((kf) => ({
+ percentage: kf.percentage,
+ properties: kf.properties,
+ ...(kf.ease ? { ease: kf.ease } : {}),
+ }));
+
+ const mapped = keyframes.map((kf) => ({
+ percentage: rangeStartPct + (kf.percentage / 100) * (rangeEndPct - rangeStartPct),
+ properties: kf.properties,
+ }));
+
+ const merged = [...preserved, ...mapped].sort((a, b) => a.percentage - b.percentage);
+
+ await liveSession.commitMutation(
+ {
+ type: "replace-with-keyframes",
+ animationId: existingPositionTween.id,
+ targetSelector: selector,
+ position:
+ typeof existingPositionTween.position === "number"
+ ? existingPositionTween.position
+ : tweenStart,
+ duration: tweenDur,
+ keyframes: merged,
+ },
+ { label: "Gesture recording (merge)", softReload: true },
+ );
+ } else {
+ // Emit one tween per property group so a mixed-prop gesture (e.g.
+ // x/y + opacity) doesn't collapse into an untagged legacy mixed
+ // tween that the position-only drag intercept can't edit.
+ for (const groupKfs of partitionKeyframesByGroup(keyframes)) {
+ await liveSession.commitMutation(
+ {
+ type: "add-with-keyframes",
+ targetSelector: selector,
+ position: roundTo3(recStart),
+ duration: roundTo3(duration),
+ keyframes: groupKfs,
+ },
+ { label: "Gesture recording (new range)", softReload: true },
+ );
+ }
+ }
+ }
+ } else {
+ // No existing tween — same per-group split as the new-range branch above.
+ for (const groupKfs of partitionKeyframesByGroup(keyframes)) {
await liveSession.commitMutation(
{
type: "add-with-keyframes",
targetSelector: selector,
position: roundTo3(recStart),
duration: roundTo3(duration),
- keyframes,
+ keyframes: groupKfs,
},
- { label: "Gesture recording (new range)", softReload: true },
+ { label: "Gesture recording", softReload: true },
);
}
- } else {
- await liveSession.commitMutation(
- {
- type: "add-with-keyframes",
- targetSelector: selector,
- position: roundTo3(recStart),
- duration: roundTo3(duration),
- keyframes,
- },
- { label: "Gesture recording", softReload: true },
- );
}
}
showToast(`Recorded ${sortedPcts.length} keyframes`, "info");
@@ -206,6 +271,9 @@ export function useGestureCommit({
}, [gestureRecording, showToast, isGestureRecordingRef, domEditSessionRef]);
const handleToggleRecording = useCallback(() => {
+ editLog("gesture", gestureStateRef.current === "recording" ? "stop" : "start", {
+ id: domEditSessionRef.current.domEditSelection?.id,
+ });
if (gestureStateRef.current === "recording") {
void stopAndCommitRecording();
return;
diff --git a/packages/studio/src/hooks/useGestureRecording.ts b/packages/studio/src/hooks/useGestureRecording.ts
index 8dabae8e09..12be5a6799 100644
--- a/packages/studio/src/hooks/useGestureRecording.ts
+++ b/packages/studio/src/hooks/useGestureRecording.ts
@@ -1,6 +1,16 @@
import { useCallback, useEffect, useRef, useState } from "react";
import { usePlayerStore, liveTime } from "../player/store/playerStore";
+// `import.meta.env` may be undefined in non-Vite bundlers (Next.js Turbopack),
+// so guard the access like the telemetry client does.
+function isDevBuild(): boolean {
+ try {
+ return import.meta.env.DEV === true;
+ } catch {
+ return false;
+ }
+}
+
export interface GestureSample {
time: number;
properties: Record<string, number>;
@@ -29,7 +39,7 @@ interface BasePosition {
interface GsapRuntime {
seek: (t: number) => void;
- set: (target: string, vars: Record<string, number>) => void;
+ set: (target: string, vars: Record<string, number | string>) => void;
selector: string;
element: HTMLElement;
startTime: number;
@@ -85,11 +95,18 @@ function connectGsapRuntime(
): GsapRuntime | null {
try {
const win = iframeEl.contentWindow as Window & {
- gsap?: { set: (t: string, v: Record<string, number>) => void };
+ gsap?: { set: (t: string, v: Record<string, number | string>) => void };
__timelines?: Record<string, { seek: (t: number) => void; duration: () => number }>;
__player?: { getTime: () => number };
};
- const tl = win?.__timelines ? Object.values(win.__timelines)[0] : null;
+ // Pick the first REAL timeline. `__timelines` also carries the studio's
+ // `__proxied` marker (a boolean, no `.seek`); `Object.values(...)[0]` would grab
+ // it and fail the connect — the cause of the no-live-preview gesture bug.
+ const tl = win?.__timelines
+ ? (Object.entries(win.__timelines).find(
+ ([key, value]) => key !== "__proxied" && typeof value?.seek === "function",
+ )?.[1] ?? null)
+ : null;
if (win?.gsap?.set && tl?.seek && selector) {
const tlDuration = tl.duration();
return {
@@ -105,7 +122,7 @@ function connectGsapRuntime(
};
}
} catch {
- /* cross-origin or missing runtime */
+ /* connect failed */
}
return null;
}
@@ -125,14 +142,14 @@ function applyRuntimePreview(
}
function recordSample(r: RecordingRefs, time: number, properties: Record<string, number>): void {
- const sampleProps = { ...properties };
- // Subtract both the CSS var offset AND the pointer-element snap offset
- // so the first sample doesn't include the snap-to-cursor jump.
- if ("x" in sampleProps)
- sampleProps.x -= r.cssVarOffset.x + r.pointerElementOffset.x / (r.scale || 1);
- if ("y" in sampleProps)
- sampleProps.y -= r.cssVarOffset.y + r.pointerElementOffset.y / (r.scale || 1);
- r.samples.push({ time, properties: sampleProps });
+ // Record the FULL position the live preview shows (element centered on the
+ // pointer, with any manual path offset folded into basePosition). Do NOT
+ // subtract the path offset: when this gesture commits as a position tween the
+ // server strips the element's --hf-studio-offset (the tween owns position — see
+ // stripStudioEditsFromTarget in studio-api), so the keyframes must already
+ // include it. Subtracting it made the committed gesture play shoved off by the
+ // offset (the offset was removed twice).
+ r.samples.push({ time, properties: { ...properties } });
r.trail.push({ x: r.pointer.x, y: r.pointer.y });
}
@@ -204,7 +221,6 @@ interface RecordingRefs {
basePosition: { x: number; y: number };
cssVarOffset: { x: number; y: number };
scale: number;
- pointerElementOffset: { x: number; y: number };
runtime: GsapRuntime | null;
rafId: number;
samples: GestureSample[];
@@ -223,7 +239,6 @@ function createRecordingRefs(): RecordingRefs {
basePosition: { x: 0, y: 0 },
cssVarOffset: { x: 0, y: 0 },
scale: 1,
- pointerElementOffset: { x: 0, y: 0 },
runtime: null,
rafId: 0,
samples: [],
@@ -280,30 +295,21 @@ export function useGestureRecording() {
r.accumulated = { opacity: base.baseOpacity, scale: base.baseScale, z: 0 };
r.basePosition = { x: base.baseX, y: base.baseY };
+ // --- Phase 2: iframe → studio scale, measured BEFORE clearing the path offset ---
+ // The pointer deltas in the RAF loop are in studio-viewport pixels; divide by
+ // this scale to convert them to the iframe's composition pixels.
+ r.scale = computeIframeScale(iframeEl);
+
+ // Now clear the optimistic path offset (already folded into baseX/baseY).
if (base.cssOffX || base.cssOffY) {
element.style.setProperty("--hf-studio-offset-x", "0px");
element.style.setProperty("--hf-studio-offset-y", "0px");
}
- // --- Phase 2: Connect to the iframe GSAP runtime ---
+ // --- Phase 3: Connect to the iframe GSAP runtime ---
const selector = element.id ? `#${element.id}` : null;
r.runtime = connectGsapRuntime(element, iframeEl, selector, elementEndTime);
- // --- Phase 3: Compute iframe viewport → composition scale ---
- r.scale = computeIframeScale(iframeEl);
-
- // --- Phase 4: Element center for pointer-element offset ---
- // element.getBoundingClientRect() is in the iframe's viewport.
- // Convert to the studio (parent) viewport using the iframe's position and scale.
- const iframeRect = iframeEl.getBoundingClientRect();
- const elRect = element.getBoundingClientRect();
- const iframeScale = r.scale || 1;
- const elCenterViewport = {
- x: iframeRect.left + (elRect.left + elRect.width / 2) * iframeScale,
- y: iframeRect.top + (elRect.top + elRect.height / 2) * iframeScale,
- };
- r.pointerElementOffset = { x: 0, y: 0 };
-
// --- Phase 5: Attach event listeners ---
const handlePointerMove = (e: PointerEvent) => {
r.pointer = { x: e.clientX, y: e.clientY };
@@ -315,12 +321,6 @@ export function useGestureRecording() {
// preventing an enormous bogus first keyframe from stale startPointer.
if (!r.hasMoved) {
r.startPointer = { x: r.pointer.x, y: r.pointer.y };
- r.pointerElementOffset = {
- x: r.pointer.x - elCenterViewport.x,
- y: r.pointer.y - elCenterViewport.y,
- };
- r.basePosition.x += r.pointerElementOffset.x / iframeScale;
- r.basePosition.y += r.pointerElementOffset.y / iframeScale;
r.hasMoved = true;
}
r.scrollDelta += e.deltaY;
@@ -341,12 +341,12 @@ export function useGestureRecording() {
r.startPointer = { ...r.pointer };
const captureStart = (e: PointerEvent) => {
if (!r.hasMoved) {
+ // Anchor the delta at the grab point — the element then moves by the
+ // pointer's *movement* from its actual position (preserving both the
+ // manual-drag start position and the grab offset). Do NOT snap the
+ // element's center to the pointer: that discarded the manual position
+ // and made the recorded 0% keyframe wrong.
r.startPointer = { x: e.clientX, y: e.clientY };
- const offX = e.clientX - elCenterViewport.x;
- const offY = e.clientY - elCenterViewport.y;
- r.pointerElementOffset = { x: offX, y: offY };
- r.basePosition.x += offX / iframeScale;
- r.basePosition.y += offY / iframeScale;
r.hasMoved = true;
}
};
@@ -385,12 +385,19 @@ export function useGestureRecording() {
if (r.runtime) {
try {
applyRuntimePreview(r.runtime, time, properties);
- } catch {
+ } catch (err) {
+ // Preview failed — disable it for the rest of the gesture (recording
+ // continues). Surface in dev so a dead preview isn't silent; `r.runtime`
+ // is nulled below so this warns at most once per gesture.
+ if (isDevBuild()) {
+ console.warn("[GR] live preview disabled — runtime threw:", err);
+ }
r.runtime = null;
}
}
recordSample(r, time, properties);
+
setRecordingDuration(time);
r.rafId = requestAnimationFrame(tick);
};
@@ -418,6 +425,18 @@ export function useGestureRecording() {
const { element: el, savedVisibility, savedTranslate } = r.runtime;
el.style.visibility = savedVisibility;
el.style.setProperty("translate", savedTranslate || "");
+ // Drop the gesture's inline gsap transform before re-applying the path
+ // offset below, so the two don't briefly stack (the recorded keyframes
+ // already encode the full position, offset included). On commit the
+ // re-seek lands on the gesture's first keyframe; on cancel this leaves the
+ // element at its pre-recording position.
+ try {
+ r.runtime.set(r.runtime.selector, {
+ clearProps: "x,y,scale,scaleX,scaleY,rotation,rotationX,rotationY,opacity,z",
+ });
+ } catch {
+ /* runtime gone */
+ }
}
if (r.cssVarOffset.x || r.cssVarOffset.y) {
const el = r.runtime?.element;
diff --git a/packages/studio/src/hooks/useGsapAnimationFetchFallback.test.ts b/packages/studio/src/hooks/useGsapAnimationFetchFallback.test.ts
new file mode 100644
index 0000000000..6bba06f913
--- /dev/null
+++ b/packages/studio/src/hooks/useGsapAnimationFetchFallback.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it } from "vitest";
+import type { GsapAnimation, ParsedGsap } from "@hyperframes/core/gsap-parser";
+import { selectElementAnimationsOrRetry } from "./useGsapAnimationFetchFallback";
+
+const anim = (targetSelector: string): GsapAnimation =>
+ ({ id: targetSelector, targetSelector, properties: {} }) as unknown as GsapAnimation;
+const parsed = (anims: GsapAnimation[]): ParsedGsap => ({ animations: anims }) as ParsedGsap;
+const target = { id: "puck-a", selector: "#puck-a" };
+
+describe("selectElementAnimationsOrRetry", () => {
+ it("signals fetch-error (short retry) when the fetch itself failed (null)", () => {
+ // A null parse means fetchParsedAnimations hit a 404/network/JSON failure —
+ // not a parse-warming race, so it must NOT be conflated with a cold parse.
+ expect(selectElementAnimationsOrRetry(null, target)).toEqual({ kind: "fetch-error" });
+ });
+
+ it("signals cold (full retry budget) when the parse is reachable but has zero total animations", () => {
+ expect(selectElementAnimationsOrRetry(parsed([]), target)).toEqual({ kind: "cold" });
+ });
+
+ it("resolves the matching animations from a warm parse", () => {
+ const outcome = selectElementAnimationsOrRetry(
+ parsed([anim("#puck-a"), anim("#other")]),
+ target,
+ );
+ expect(outcome.kind).toBe("resolved");
+ expect(outcome.kind === "resolved" && outcome.animations.map((a) => a.targetSelector)).toEqual([
+ "#puck-a",
+ ]);
+ });
+
+ it("resolves to [] (no retry) for a warm parse with no match — element genuinely has no animation", () => {
+ const outcome = selectElementAnimationsOrRetry(parsed([anim("#other")]), target);
+ expect(outcome).toEqual({ kind: "resolved", animations: [] });
+ });
+});
diff --git a/packages/studio/src/hooks/useGsapAnimationFetchFallback.ts b/packages/studio/src/hooks/useGsapAnimationFetchFallback.ts
index f995d0ee6b..e1fcded7d2 100644
--- a/packages/studio/src/hooks/useGsapAnimationFetchFallback.ts
+++ b/packages/studio/src/hooks/useGsapAnimationFetchFallback.ts
@@ -1,18 +1,74 @@
import { useCallback } from "react";
+import type { GsapAnimation, ParsedGsap } from "@hyperframes/core/gsap-parser";
import type { DomEditSelection } from "../components/editor/domEditing";
import { fetchParsedAnimations, getAnimationsForElement } from "./useGsapTweenCache";
+// A cold parse is the initial-load race: the endpoint is reachable but its parse
+// isn't warm yet (zero total animations). It's worth waiting out (~600ms).
+const COLD_PARSE_RETRIES = 5;
+const COLD_PARSE_DELAY_MS = 120;
+// A hard fetch error (404/403/network/JSON failure → `fetchParsedAnimations`
+// returns null) is NOT a parse-warming race, so it shouldn't burn the full
+// cold-parse budget. One short retry covers a transient blip; beyond that the
+// endpoint genuinely isn't serving this file, so fall through to "no animation".
+const FETCH_ERROR_RETRIES = 1;
+const FETCH_ERROR_DELAY_MS = 120;
+
+const delay = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
+
+/**
+ * Outcome of resolving an element's animations from a single parse result.
+ * - `resolved`: a definitive answer (matched animations, or `[]` when the parse
+ * is warm but this element has no animation — create a new one, don't retry).
+ * - `fetch-error`: `fetchParsedAnimations` returned null (HTTP/network/JSON
+ * failure) — retry briefly, not for the full cold-parse budget.
+ * - `cold`: the parse came back reachable but with zero total animations — the
+ * initial-load warming race, worth the full cold-parse retry budget.
+ */
+export type ElementAnimationsOutcome =
+ | { kind: "resolved"; animations: GsapAnimation[] }
+ | { kind: "fetch-error" }
+ | { kind: "cold" };
+
+/**
+ * Classify a parse result for one element. Differentiates a hard fetch failure
+ * (`parsed === null`) from a warm-but-empty cold parse (`animations.length === 0`)
+ * so the caller can apply the right retry budget to each.
+ */
+export function selectElementAnimationsOrRetry(
+ parsed: ParsedGsap | null,
+ target: { id: string | null; selector: string | null },
+): ElementAnimationsOutcome {
+ if (!parsed) return { kind: "fetch-error" };
+ if (parsed.animations.length === 0) return { kind: "cold" };
+ return { kind: "resolved", animations: getAnimationsForElement(parsed.animations, target) };
+}
+
export function useGsapAnimationFetchFallback(projectId: string | null, gsapSourceFile: string) {
return useCallback(
- (selection: DomEditSelection) => async () => {
- const pid = projectId;
- if (!pid) return [];
- const parsed = await fetchParsedAnimations(pid, gsapSourceFile);
- if (!parsed) return [];
- return getAnimationsForElement(parsed.animations, {
- id: selection.id ?? null,
- selector: selection.selector ?? null,
- });
+ (selection: DomEditSelection) => async (): Promise<GsapAnimation[]> => {
+ if (!projectId) return [];
+ const target = { id: selection.id ?? null, selector: selection.selector ?? null };
+ // A drag can fire before the async parse is warm; a cold parse must retry
+ // rather than fall through to the no-animation path (which duplicates the
+ // tween). A hard fetch error is a different failure — retry only briefly.
+ let coldAttempts = 0;
+ let errorAttempts = 0;
+ for (;;) {
+ const parsed = await fetchParsedAnimations(projectId, gsapSourceFile);
+ const outcome = selectElementAnimationsOrRetry(parsed, target);
+ if (outcome.kind === "resolved") return outcome.animations;
+ if (outcome.kind === "fetch-error") {
+ if (errorAttempts >= FETCH_ERROR_RETRIES) return [];
+ errorAttempts++;
+ await delay(FETCH_ERROR_DELAY_MS);
+ continue;
+ }
+ // cold
+ if (coldAttempts >= COLD_PARSE_RETRIES) return [];
+ coldAttempts++;
+ await delay(COLD_PARSE_DELAY_MS);
+ }
},
[projectId, gsapSourceFile],
);
diff --git a/packages/studio/src/hooks/useGsapAnimationOps.ts b/packages/studio/src/hooks/useGsapAnimationOps.ts
index 390db74ffc..046d8b6f00 100644
--- a/packages/studio/src/hooks/useGsapAnimationOps.ts
+++ b/packages/studio/src/hooks/useGsapAnimationOps.ts
@@ -101,7 +101,7 @@ export function useGsapAnimationOps({
void commitMutation(
selection,
{ type: "delete-all-for-selector", targetSelector },
- { label: "Delete all animations for element" },
+ { label: "Delete all animations for element", softReload: true },
);
},
[commitMutation, activeCompPath, sdkSession, sdkDeps],
diff --git a/packages/studio/src/hooks/useGsapAwareEditing.ts b/packages/studio/src/hooks/useGsapAwareEditing.ts
index cd4b55acbf..7d9c8c3bb8 100644
--- a/packages/studio/src/hooks/useGsapAwareEditing.ts
+++ b/packages/studio/src/hooks/useGsapAwareEditing.ts
@@ -8,16 +8,19 @@
* from the rest of the editing orchestration.
*/
import { useCallback } from "react";
+import { editLog } from "../utils/editDebugLog";
import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
import type { DomEditSelection } from "../components/editor/domEditingTypes";
-import { STUDIO_GSAP_DRAG_INTERCEPT_ENABLED } from "../components/editor/manualEditingAvailability";
-import { GSAP_CSS_FALLBACK_BLOCKED_MESSAGE } from "./useDomGeometryCommits";
import {
tryGsapDragIntercept,
tryGsapResizeIntercept,
tryGsapRotationIntercept,
} from "./gsapRuntimeBridge";
import { useAnimatedPropertyCommit } from "./useAnimatedPropertyCommit";
+import {
+ useGsapSaveFailureTelemetry,
+ useSafeGsapCommitMutation,
+} from "./useSafeGsapCommitMutation";
import type { CommitMutation } from "./gsapScriptCommitTypes";
export interface UseGsapAwareEditingParams {
@@ -35,15 +38,10 @@ export interface UseGsapAwareEditingParams {
label: string,
) => void;
// DOM fallbacks (from useDomEditCommits)
- handleDomPathOffsetCommit: (
- selection: DomEditSelection,
- next: { x: number; y: number },
- ) => Promise<void>;
handleDomBoxSizeCommit: (
selection: DomEditSelection,
next: { width: number; height: number },
) => Promise<void>;
- handleDomRotationCommit: (selection: DomEditSelection, next: { angle: number }) => Promise<void>;
// GSAP script commit ops (from useGsapScriptCommits)
addGsapAnimation: (
sel: DomEditSelection,
@@ -85,9 +83,7 @@ export function useGsapAwareEditing({
bumpGsapCache,
makeFetchFallback,
trackGsapInteractionFailure,
- handleDomPathOffsetCommit,
handleDomBoxSizeCommit,
- handleDomRotationCommit,
addGsapAnimation,
convertToKeyframes,
setArcPath,
@@ -96,55 +92,42 @@ export function useGsapAwareEditing({
// ── GSAP-aware geometry commits ──
const handleGsapAwarePathOffsetCommit = useCallback(
- async (selection: DomEditSelection, next: { x: number; y: number }) => {
- const hasGsapAnims = selectedGsapAnimations.length > 0;
- console.log(
- "[drag:3] handleGsapAwarePathOffsetCommit",
- JSON.stringify({
- sel: selection.id,
- offset: next,
- hasGsapAnims,
- interceptEnabled: STUDIO_GSAP_DRAG_INTERCEPT_ENABLED,
- animCount: selectedGsapAnimations.length,
- animIds: selectedGsapAnimations.map((a) => a.id).slice(0, 5),
- }),
- );
- if (hasGsapAnims && !STUDIO_GSAP_DRAG_INTERCEPT_ENABLED) {
- showToast(GSAP_CSS_FALLBACK_BLOCKED_MESSAGE, "error");
- throw new Error(GSAP_CSS_FALLBACK_BLOCKED_MESSAGE);
- }
- if (STUDIO_GSAP_DRAG_INTERCEPT_ENABLED && gsapCommitMutation) {
+ async (
+ selection: DomEditSelection,
+ next: { x: number; y: number },
+ modifiers?: { altKey?: boolean },
+ ) => {
+ editLog("manual-drag:move", { id: selection.id, next, altKey: modifiers?.altKey });
+ if (gsapCommitMutation) {
try {
- const handled = await tryGsapDragIntercept(
+ await tryGsapDragIntercept(
selection,
next,
selectedGsapAnimations,
previewIframeRef.current,
gsapCommitMutation,
makeFetchFallback(selection),
+ modifiers,
);
- if (handled) return;
} catch (error) {
trackGsapInteractionFailure(error, selection, "drag", "Move animated layer");
throw error;
}
}
- return handleDomPathOffsetCommit(selection, next);
},
[
- handleDomPathOffsetCommit,
selectedGsapAnimations,
gsapCommitMutation,
previewIframeRef,
makeFetchFallback,
trackGsapInteractionFailure,
- showToast,
],
);
const handleGsapAwareBoxSizeCommit = useCallback(
async (selection: DomEditSelection, next: { width: number; height: number }) => {
- if (STUDIO_GSAP_DRAG_INTERCEPT_ENABLED && gsapCommitMutation) {
+ editLog("manual-drag:resize", { id: selection.id, next });
+ if (gsapCommitMutation) {
try {
const handled = await tryGsapResizeIntercept(
selection,
@@ -174,9 +157,13 @@ export function useGsapAwareEditing({
const handleGsapAwareRotationCommit = useCallback(
async (selection: DomEditSelection, next: { angle: number }) => {
- if (STUDIO_GSAP_DRAG_INTERCEPT_ENABLED && gsapCommitMutation) {
+ editLog("manual-drag:rotate", { id: selection.id, next });
+ if (gsapCommitMutation) {
try {
- const handled = await tryGsapRotationIntercept(
+ // Single source of truth for rotation too: tryGsapRotationIntercept handles
+ // tweened elements (keyframes) and static ones (a tl.set), so there's no
+ // CSS-var fallback. It returns false only for a selectorless element (no-op).
+ await tryGsapRotationIntercept(
selection,
next.angle,
selectedGsapAnimations,
@@ -184,16 +171,13 @@ export function useGsapAwareEditing({
gsapCommitMutation,
makeFetchFallback(selection),
);
- if (handled) return;
} catch (error) {
trackGsapInteractionFailure(error, selection, "rotation", "Rotate animated layer");
throw error;
}
}
- return handleDomRotationCommit(selection, next);
},
[
- handleDomRotationCommit,
selectedGsapAnimations,
gsapCommitMutation,
previewIframeRef,
@@ -232,13 +216,27 @@ export function useGsapAwareEditing({
);
// ── Thin commitMutation facade ──
+ // Routes through the canonical safe wrapper so a server-save failure surfaces a
+ // toast + save telemetry instead of silently reverting — parity with the
+ // arc/keyframe/animation ops that all go through useSafeGsapCommitMutation.
+
+ const noopCommit = useCallback<CommitMutation>(async () => {}, []);
+ const trackGsapSaveFailure = useGsapSaveFailureTelemetry(null);
+ const safeGsapCommit = useSafeGsapCommitMutation(
+ gsapCommitMutation ?? noopCommit,
+ trackGsapSaveFailure,
+ showToast,
+ );
const commitMutation = useCallback(
async (mutation: Record<string, unknown>, options: { label: string; softReload?: boolean }) => {
if (!domEditSelection) return;
- await gsapCommitMutation?.(domEditSelection, mutation, options);
+ // Return (await) the safe-commit chain so consumers that `await
+ // session.commitMutation(...)` (gesture recording, enable-keyframes) run
+ // their post-actions only after the server save has settled.
+ await safeGsapCommit(domEditSelection, mutation, options);
},
- [domEditSelection, gsapCommitMutation],
+ [domEditSelection, safeGsapCommit],
);
// Unroll all computed (helper/loop) tweens in the active timeline into literal
diff --git a/packages/studio/src/hooks/useGsapScriptCommits.test.tsx b/packages/studio/src/hooks/useGsapScriptCommits.test.tsx
new file mode 100644
index 0000000000..5285c5940f
--- /dev/null
+++ b/packages/studio/src/hooks/useGsapScriptCommits.test.tsx
@@ -0,0 +1,366 @@
+// @vitest-environment happy-dom
+import { act } from "react";
+import { createRoot } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+// Mock the two preview-sync primitives so we can assert which path runCommit took.
+// `patchRuntimeTweenInPlace` is the instant in-place patch; `applySoftReload` is
+// the existing fallback. `extractGsapScriptText` is re-exported from the same
+// module and used elsewhere in the hook — keep it a harmless stub.
+const patchRuntimeTweenInPlace = vi.fn<(...args: unknown[]) => boolean>();
+const applySoftReload = vi.fn<(...args: unknown[]) => string>();
+const trackStudioEvent = vi.fn();
+
+vi.mock("./gsapRuntimePatch", () => ({
+ patchRuntimeTweenInPlace: (...args: unknown[]) => patchRuntimeTweenInPlace(...args),
+}));
+vi.mock("../utils/gsapSoftReload", () => ({
+ applySoftReload: (...args: unknown[]) => applySoftReload(...args),
+ extractGsapScriptText: () => "",
+}));
+vi.mock("../utils/studioTelemetry", () => ({
+ trackStudioEvent: (...args: unknown[]) => trackStudioEvent(...args),
+}));
+
+// Tell React this is an act-capable environment so act(...) flushes effects
+// without warning (React reads this global at call time).
+(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
+import type { DomEditSelection } from "../components/editor/domEditingTypes";
+import type { MutationResult } from "./gsapScriptCommitTypes";
+import { applyPreviewSync, useGsapScriptCommits } from "./useGsapScriptCommits";
+
+// ── applyPreviewSync (pure preview-sync decision) ────────────────────────────
+
+const FAKE_IFRAME = {} as HTMLIFrameElement;
+
+function result(over: Partial<MutationResult> = {}): MutationResult {
+ return { ok: true, scriptText: "tl.set('#a',{})", ...over };
+}
+
+describe("applyPreviewSync", () => {
+ beforeEach(() => {
+ patchRuntimeTweenInPlace.mockReset();
+ applySoftReload.mockReset();
+ trackStudioEvent.mockReset();
+ });
+
+ it("instantPatch + patch succeeds: skips both soft reload and full reload", () => {
+ patchRuntimeTweenInPlace.mockReturnValue(true);
+ const reloadPreview = vi.fn();
+
+ applyPreviewSync(
+ FAKE_IFRAME,
+ result(),
+ {
+ label: "drag",
+ softReload: true,
+ instantPatch: { selector: "#a", change: { kind: "set", props: { x: 10 } } },
+ },
+ reloadPreview,
+ );
+
+ expect(patchRuntimeTweenInPlace).toHaveBeenCalledWith(FAKE_IFRAME, "#a", {
+ kind: "set",
+ props: { x: 10 },
+ });
+ expect(applySoftReload).not.toHaveBeenCalled();
+ expect(reloadPreview).not.toHaveBeenCalled();
+ });
+
+ it("instantPatch + patch fails: falls back to the soft reload, passing onAsyncFailure", () => {
+ patchRuntimeTweenInPlace.mockReturnValue(false);
+ applySoftReload.mockReturnValue("applied");
+ const reloadPreview = vi.fn();
+
+ applyPreviewSync(
+ FAKE_IFRAME,
+ result({ scriptText: "SCRIPT" }),
+ {
+ label: "drag",
+ softReload: true,
+ instantPatch: { selector: "#a", change: { kind: "set", props: { x: 10 } } },
+ },
+ reloadPreview,
+ );
+
+ // reloadPreview is wired as onAsyncFailure (3rd arg) so a MotionPath-plugin
+ // CDN load failure escalates to a full reload — but it is NOT called eagerly.
+ expect(applySoftReload).toHaveBeenCalledWith(FAKE_IFRAME, "SCRIPT", reloadPreview);
+ expect(reloadPreview).not.toHaveBeenCalled();
+ // A successful instant patch is the fast path; here it missed → fallback event.
+ expect(trackStudioEvent).toHaveBeenCalledWith(
+ "gsap_instant_patch_fallback",
+ expect.objectContaining({ selector: "#a" }),
+ );
+ });
+
+ it('instantPatch + patch fails + soft reload "verify-failed": transient, does NOT escalate (U4)', () => {
+ patchRuntimeTweenInPlace.mockReturnValue(false);
+ applySoftReload.mockReturnValue("verify-failed");
+ const reloadPreview = vi.fn();
+
+ applyPreviewSync(
+ FAKE_IFRAME,
+ result({ scriptText: "SCRIPT" }),
+ {
+ label: "drag",
+ softReload: true,
+ instantPatch: { selector: "#a", change: { kind: "set", props: { x: 10 } } },
+ },
+ reloadPreview,
+ );
+
+ // U4: "verify-failed" is the TRANSIENT empty-timeline window — the live state
+ // is correct, so we must NOT escalate to a full reload.
+ expect(applySoftReload).toHaveBeenCalledWith(FAKE_IFRAME, "SCRIPT", reloadPreview);
+ expect(reloadPreview).not.toHaveBeenCalled();
+ // Telemetry records the suppressed transient (escalated: false).
+ expect(trackStudioEvent).toHaveBeenCalledWith(
+ "gsap_soft_reload_outcome",
+ expect.objectContaining({
+ origin: "preview_sync",
+ result: "verify-failed",
+ escalated: false,
+ }),
+ );
+ });
+
+ it('instantPatch + patch fails + soft reload "cannot-soft-reload": escalates to full reload', () => {
+ patchRuntimeTweenInPlace.mockReturnValue(false);
+ applySoftReload.mockReturnValue("cannot-soft-reload");
+ const reloadPreview = vi.fn();
+
+ applyPreviewSync(
+ FAKE_IFRAME,
+ result({ scriptText: "SCRIPT" }),
+ {
+ label: "drag",
+ softReload: true,
+ instantPatch: { selector: "#a", change: { kind: "set", props: { x: 10 } } },
+ },
+ reloadPreview,
+ );
+
+ // Structural failure: the preview is genuinely stale/broken → full reload.
+ expect(applySoftReload).toHaveBeenCalledWith(FAKE_IFRAME, "SCRIPT", reloadPreview);
+ expect(reloadPreview).toHaveBeenCalledTimes(1);
+ expect(trackStudioEvent).toHaveBeenCalledWith(
+ "gsap_soft_reload_outcome",
+ expect.objectContaining({
+ origin: "preview_sync",
+ result: "cannot-soft-reload",
+ escalated: true,
+ }),
+ );
+ });
+
+ it("no instantPatch + softReload + scriptText: soft reloads, passing onAsyncFailure", () => {
+ applySoftReload.mockReturnValue("applied");
+ const reloadPreview = vi.fn();
+
+ applyPreviewSync(
+ FAKE_IFRAME,
+ result({ scriptText: "SCRIPT" }),
+ { label: "x", softReload: true },
+ reloadPreview,
+ );
+
+ expect(patchRuntimeTweenInPlace).not.toHaveBeenCalled();
+ expect(applySoftReload).toHaveBeenCalledWith(FAKE_IFRAME, "SCRIPT", reloadPreview);
+ expect(reloadPreview).not.toHaveBeenCalled();
+ // "applied" emits no telemetry (only the failure paths do).
+ expect(trackStudioEvent).not.toHaveBeenCalled();
+ });
+
+ it('no instantPatch + softReload "verify-failed": transient, does NOT escalate (U4)', () => {
+ applySoftReload.mockReturnValue("verify-failed");
+ const reloadPreview = vi.fn();
+
+ applyPreviewSync(
+ FAKE_IFRAME,
+ result({ scriptText: "SCRIPT" }),
+ { label: "x", softReload: true },
+ reloadPreview,
+ );
+
+ // onAsyncFailure is wired, but the transient result does not trigger it.
+ expect(applySoftReload).toHaveBeenCalledWith(FAKE_IFRAME, "SCRIPT", reloadPreview);
+ expect(reloadPreview).not.toHaveBeenCalled();
+ expect(trackStudioEvent).toHaveBeenCalledWith(
+ "gsap_soft_reload_outcome",
+ expect.objectContaining({ result: "verify-failed", escalated: false }),
+ );
+ });
+
+ it('no instantPatch + softReload "cannot-soft-reload": escalates to full reload', () => {
+ applySoftReload.mockReturnValue("cannot-soft-reload");
+ const reloadPreview = vi.fn();
+
+ applyPreviewSync(
+ FAKE_IFRAME,
+ result({ scriptText: "SCRIPT" }),
+ { label: "x", softReload: true },
+ reloadPreview,
+ );
+
+ expect(applySoftReload).toHaveBeenCalledWith(FAKE_IFRAME, "SCRIPT", reloadPreview);
+ expect(reloadPreview).toHaveBeenCalledTimes(1);
+ expect(trackStudioEvent).toHaveBeenCalledWith(
+ "gsap_soft_reload_outcome",
+ expect.objectContaining({ result: "cannot-soft-reload", escalated: true }),
+ );
+ });
+
+ it("no instantPatch + no softReload: full reload (today's behavior)", () => {
+ const reloadPreview = vi.fn();
+
+ applyPreviewSync(FAKE_IFRAME, result(), { label: "x" }, reloadPreview);
+
+ expect(patchRuntimeTweenInPlace).not.toHaveBeenCalled();
+ expect(applySoftReload).not.toHaveBeenCalled();
+ expect(reloadPreview).toHaveBeenCalledTimes(1);
+ });
+});
+
+// ── runCommit (full hook path: persist + preview sync) ───────────────────────
+
+type HookApi = ReturnType<typeof useGsapScriptCommits>;
+
+let cleanup: (() => void) | null = null;
+
+function renderCommitHook() {
+ const reloadPreview = vi.fn();
+ const onCacheInvalidate = vi.fn();
+ const forceReloadSdkSession = vi.fn();
+ const recordEdit = vi.fn(async () => {});
+ const showToast = vi.fn();
+
+ const captured: { api: HookApi | null } = { api: null };
+ function Probe() {
+ captured.api = useGsapScriptCommits({
+ projectIdRef: { current: "proj-1" },
+ activeCompPath: "index.html",
+ previewIframeRef: { current: FAKE_IFRAME },
+ editHistory: { recordEdit },
+ domEditSaveTimestampRef: { current: 0 },
+ reloadPreview,
+ onCacheInvalidate,
+ onFileContentChanged: undefined,
+ showToast,
+ sdkSession: null,
+ writeProjectFile: undefined,
+ forceReloadSdkSession,
+ });
+ return null;
+ }
+
+ const container = document.createElement("div");
+ const root = createRoot(container);
+ act(() => {
+ root.render(<Probe />);
+ });
+ cleanup = () => act(() => root.unmount());
+ const hookApi = captured.api;
+ if (!hookApi) throw new Error("hook did not initialize");
+ return {
+ api: hookApi,
+ reloadPreview,
+ onCacheInvalidate,
+ forceReloadSdkSession,
+ recordEdit,
+ showToast,
+ };
+}
+
+const selection: DomEditSelection = { id: "a", selector: "#a" } as DomEditSelection;
+
+function mockFetchResult(over: Partial<MutationResult> = {}): void {
+ const body: MutationResult = {
+ ok: true,
+ changed: true,
+ before: "BEFORE",
+ after: "AFTER",
+ scriptText: "SCRIPT",
+ ...over,
+ };
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => ({ ok: true, json: async () => body }) as unknown as Response),
+ );
+}
+
+describe("runCommit — instantPatch wiring", () => {
+ beforeEach(() => {
+ patchRuntimeTweenInPlace.mockReset();
+ applySoftReload.mockReset();
+ trackStudioEvent.mockReset();
+ });
+ afterEach(() => {
+ cleanup?.();
+ cleanup = null;
+ vi.unstubAllGlobals();
+ });
+
+ it("instantPatch succeeds: persists, invalidates cache, NO reload", async () => {
+ patchRuntimeTweenInPlace.mockReturnValue(true);
+ mockFetchResult();
+ const deps = renderCommitHook();
+
+ await act(async () => {
+ await deps.api.commitMutation(
+ selection,
+ { x: 10 },
+ {
+ label: "drag",
+ softReload: true,
+ instantPatch: { selector: "#a", change: { kind: "set", props: { x: 10 } } },
+ },
+ );
+ });
+
+ expect(fetch).toHaveBeenCalledTimes(1); // source mutation persisted
+ expect(deps.recordEdit).toHaveBeenCalledTimes(1);
+ expect(deps.onCacheInvalidate).toHaveBeenCalledTimes(1);
+ expect(applySoftReload).not.toHaveBeenCalled();
+ expect(deps.reloadPreview).not.toHaveBeenCalled();
+ });
+
+ it("instantPatch fails: persists AND falls back to soft reload", async () => {
+ patchRuntimeTweenInPlace.mockReturnValue(false);
+ applySoftReload.mockReturnValue("applied");
+ mockFetchResult();
+ const deps = renderCommitHook();
+
+ await act(async () => {
+ await deps.api.commitMutation(
+ selection,
+ { x: 10 },
+ {
+ label: "drag",
+ softReload: true,
+ instantPatch: { selector: "#a", change: { kind: "set", props: { x: 10 } } },
+ },
+ );
+ });
+
+ expect(fetch).toHaveBeenCalledTimes(1);
+ expect(applySoftReload).toHaveBeenCalledWith(FAKE_IFRAME, "SCRIPT", deps.reloadPreview);
+ expect(deps.reloadPreview).not.toHaveBeenCalled();
+ expect(deps.onCacheInvalidate).toHaveBeenCalledTimes(1);
+ });
+
+ it("no instantPatch: identical to today — soft reload when softReload+scriptText", async () => {
+ applySoftReload.mockReturnValue("applied");
+ mockFetchResult();
+ const deps = renderCommitHook();
+
+ await act(async () => {
+ await deps.api.commitMutation(selection, { x: 10 }, { label: "drag", softReload: true });
+ });
+
+ expect(patchRuntimeTweenInPlace).not.toHaveBeenCalled();
+ expect(applySoftReload).toHaveBeenCalledWith(FAKE_IFRAME, "SCRIPT", deps.reloadPreview);
+ expect(deps.reloadPreview).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/studio/src/hooks/useGsapScriptCommits.ts b/packages/studio/src/hooks/useGsapScriptCommits.ts
index 1c15af70d5..a165394471 100644
--- a/packages/studio/src/hooks/useGsapScriptCommits.ts
+++ b/packages/studio/src/hooks/useGsapScriptCommits.ts
@@ -1,9 +1,13 @@
import { useCallback, useMemo, useRef } from "react";
+import { editLog } from "../utils/editDebugLog";
import { findUnsafeMutationValues } from "@hyperframes/core/studio-api/finite-mutation";
import type { DomEditSelection } from "../components/editor/domEditingTypes";
import { applySoftReload, extractGsapScriptText } from "../utils/gsapSoftReload";
+import type { SoftReloadResult } from "../utils/gsapSoftReload";
+import { trackStudioEvent } from "../utils/studioTelemetry";
import type { CutoverDeps } from "../utils/sdkCutover";
import { updateKeyframeCacheFromParsed } from "./gsapKeyframeCacheHelpers";
+import { patchRuntimeTweenInPlace } from "./gsapRuntimePatch";
import { createKeyedSerializer } from "./serializeByKey";
import {
GsapMutationHttpError,
@@ -43,6 +47,77 @@ async function mutateGsapScript(
return result;
}
+/**
+ * Apply a soft reload and enforce the U4 invariant via the richer
+ * `SoftReloadResult`, with telemetry on every non-success path so the invariant
+ * is observable in production, not just asserted in tests:
+ *
+ * - `"cannot-soft-reload"` (PERMANENT/STRUCTURAL: no gsap runtime, no rebind
+ * hook, no scopable key, no script element, or the sync re-run threw) →
+ * escalate to a full `reloadPreview()`; the preview is genuinely stale/broken.
+ * - `"verify-failed"` (TRANSIENT: re-run happened, `__timelines` momentarily
+ * empty) → do NOT escalate; the live `gsap.set` already shows the correct value
+ * and a remount would re-flash the WebGL context + revert subcomp keyframes.
+ * - `"applied"` → success (or deferred to async plugin load; `onAsyncFailure`
+ * covers the CDN-error escalation).
+ */
+function softReloadOrEscalate(
+ iframe: HTMLIFrameElement | null,
+ scriptText: string,
+ reloadPreview: () => void,
+ origin: "preview_sync" | "sdk_refresh",
+): void {
+ const result: SoftReloadResult = applySoftReload(iframe, scriptText, reloadPreview);
+ if (result === "applied") return;
+ trackStudioEvent("gsap_soft_reload_outcome", {
+ origin,
+ result,
+ escalated: result === "cannot-soft-reload",
+ });
+ // PERMANENT failure: the preview can't be soft-updated → full reload. TRANSIENT
+ // "verify-failed" is suppressed (live state is correct).
+ if (result === "cannot-soft-reload") reloadPreview();
+}
+
+/**
+ * Sync the preview after a persisted commit. For a value-only edit
+ * (`options.instantPatch`), try the in-place runtime patch first: on success the
+ * preview is already correct, so we skip the reload entirely (instant). On `false`
+ * — or when no `instantPatch` is supplied — fall back to the existing soft/full
+ * reload. Pure (no React) so `runCommit`'s preview-sync decision is unit-testable.
+ */
+export function applyPreviewSync(
+ iframe: HTMLIFrameElement | null,
+ result: MutationResult,
+ options: CommitMutationOptions,
+ reloadPreview: () => void,
+): void {
+ if (options.instantPatch) {
+ const patched = patchRuntimeTweenInPlace(
+ iframe,
+ options.instantPatch.selector,
+ options.instantPatch.change,
+ );
+ // Patched in place — element is already correct on screen; no reload needed.
+ if (patched) return;
+ // The instant path couldn't patch in place — record the fallback so we can
+ // track how often the fast path misses before the soft/full reload below.
+ trackStudioEvent("gsap_instant_patch_fallback", { selector: options.instantPatch.selector });
+ // Fall through to the soft/full reload path below.
+ }
+ if (options.softReload && result.scriptText) {
+ // A soft-reloadable edit escalates to a full iframe remount ONLY on the
+ // PERMANENT "cannot-soft-reload" result (the preview is genuinely stale/
+ // broken). The TRANSIENT "verify-failed" does NOT escalate — the value is
+ // already correct on screen, and a remount re-flashes the WebGL context AND
+ // re-inlines subcomps (reverting their keyframes). The async MotionPath-plugin
+ // load failure escalates separately via `onAsyncFailure`.
+ softReloadOrEscalate(iframe, result.scriptText, reloadPreview, "preview_sync");
+ } else {
+ reloadPreview();
+ }
+}
+
// oxfmt-ignore
// fallow-ignore-next-line complexity
export function useGsapScriptCommits({ projectIdRef, activeCompPath, previewIframeRef, editHistory, domEditSaveTimestampRef, reloadPreview, onCacheInvalidate, onFileContentChanged, showToast, sdkSession, writeProjectFile, forceReloadSdkSession }: GsapScriptCommitsParams) {
@@ -55,6 +130,12 @@ export function useGsapScriptCommits({ projectIdRef, activeCompPath, previewIfra
const runCommit = useCallback(async (selection: DomEditSelection, mutation: Record<string, unknown>, options: CommitMutationOptions) => {
const pid = projectIdRef.current;
if (!pid) return;
+ editLog("gsap-commit", {
+ type: mutation.type,
+ id: selection.id,
+ file: selection.sourceFile || activeCompPath,
+ label: options.label,
+ });
const unsafeFields = findUnsafeMutationValues(mutation);
if (unsafeFields.length > 0) {
showToast?.("Couldn't read element layout — try again at a different playhead time", "error");
@@ -82,12 +163,13 @@ export function useGsapScriptCommits({ projectIdRef, activeCompPath, previewIfra
if (options.skipReload) return;
if (result.parsed?.animations) updateKeyframeCacheFromParsed(result.parsed.animations, targetPath, selection.id ?? undefined, mutation);
options.beforeReload?.();
- if (options.softReload && result.scriptText) {
- if (!applySoftReload(previewIframeRef.current, result.scriptText)) reloadPreview();
- } else {
- reloadPreview();
- }
+ applyPreviewSync(previewIframeRef.current, result, options, reloadPreview);
onCacheInvalidate();
+ editLog("gsap-commit:done", {
+ type: mutation.type,
+ changed: result.changed,
+ instant: Boolean(options.instantPatch),
+ });
}, [projectIdRef, activeCompPath, previewIframeRef, editHistory, domEditSaveTimestampRef, reloadPreview, onCacheInvalidate, onFileContentChanged, showToast, forceReloadSdkSession]);
// Every GSAP-script commit is a read-modify-write of one file. Overlapping
// commits to the SAME file (any op type, any animation) interleave server-side,
@@ -109,8 +191,22 @@ export function useGsapScriptCommits({ projectIdRef, activeCompPath, previewIfra
// the playhead) and invalidates the panel cache, matching the server path.
const sdkRefresh = useCallback(
(after: string) => {
+ // extractGsapScriptText returns null when zero/multiple GSAP scripts are
+ // present — that's an ambiguous/structural change that genuinely needs a full
+ // reload. But a SINGLE-script soft-reloadable edit must not escalate to a full
+ // remount even if applySoftReload reports failure (same U4 invariant as
+ // applyPreviewSync): the live state is already correct, and a remount re-inlines
+ // subcomps + reverts their keyframes.
const script = extractGsapScriptText(after);
- if (!(script && applySoftReload(previewIframeRef.current, script))) reloadPreview();
+ if (script) {
+ // Soft-reload in place. reloadPreview is the ASYNC-failure escalation — a
+ // plugin-CDN load error genuinely breaks the iframe → full reload. Per U4, a
+ // synchronous "verify-failed" (transient empty __timelines) does NOT escalate,
+ // but a "cannot-soft-reload" (structural failure) does.
+ softReloadOrEscalate(previewIframeRef.current, script, reloadPreview, "sdk_refresh");
+ } else {
+ reloadPreview();
+ }
onCacheInvalidate();
},
[previewIframeRef, reloadPreview, onCacheInvalidate],
diff --git a/packages/studio/src/hooks/useGsapTweenCache.ts b/packages/studio/src/hooks/useGsapTweenCache.ts
index 246f53526c..2cbcf4c8b6 100644
--- a/packages/studio/src/hooks/useGsapTweenCache.ts
+++ b/packages/studio/src/hooks/useGsapTweenCache.ts
@@ -1,6 +1,7 @@
import { useEffect, useMemo, useRef, useState, useCallback } from "react";
import type { GsapAnimation, GsapKeyframesData, ParsedGsap } from "@hyperframes/core/gsap-parser";
import type { GsapPercentageKeyframe } from "@hyperframes/core/gsap-parser";
+import { isStudioHoldSet } from "@hyperframes/core/gsap-parser";
import { usePlayerStore } from "../player/store/playerStore";
import { readRuntimeKeyframes, scanAllRuntimeKeyframes } from "./gsapRuntimeBridge";
import {
@@ -107,7 +108,12 @@ export async function fetchParsedAnimations(
const res = await fetch(
`/api/projects/${encodeURIComponent(projectId)}/gsap-animations/${encodeURIComponent(sourceFile)}`,
);
- return res.ok ? ((await res.json()) as ParsedGsap) : null;
+ if (!res.ok) return null;
+ const parsed = (await res.json()) as ParsedGsap;
+ // Studio-emitted pre-keyframe hold `set`s are an internal runtime detail (they
+ // hold an element's first keyframe before its tween). They must not surface as
+ // user animations — otherwise they pollute the keyframe cache / timeline diamonds.
+ return { ...parsed, animations: parsed.animations.filter((a) => !isStudioHoldSet(a)) };
} catch {
return null;
}
diff --git a/packages/studio/src/hooks/useRazorSplit.ts b/packages/studio/src/hooks/useRazorSplit.ts
index 292d33232b..339d242761 100644
--- a/packages/studio/src/hooks/useRazorSplit.ts
+++ b/packages/studio/src/hooks/useRazorSplit.ts
@@ -1,4 +1,5 @@
import { useCallback, useRef } from "react";
+import { editLog } from "../utils/editDebugLog";
import type { TimelineElement } from "../player";
import { usePlayerStore } from "../player";
import { saveProjectFilesWithHistory } from "../utils/studioFileHistory";
@@ -38,13 +39,21 @@ async function splitHtmlElement(
patchTarget: NonNullable<ReturnType<typeof buildPatchTarget>>,
splitTime: number,
newId: string,
+ elementStart: number,
+ elementDuration: number,
): Promise<{ ok: boolean; changed?: boolean; content?: string }> {
const response = await fetch(
`/api/projects/${projectId}/file-mutations/split-element/${encodeURIComponent(targetPath)}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ target: patchTarget, splitTime, newId }),
+ body: JSON.stringify({
+ target: patchTarget,
+ splitTime,
+ newId,
+ elementStart,
+ elementDuration,
+ }),
},
);
if (!response.ok) throw new Error("Split request failed");
@@ -111,11 +120,21 @@ async function executeSplit(
if (!patchTarget) throw new Error("Clip is missing a patchable target.");
const targetPath = element.sourceFile || activeCompPath || "index.html";
+ editLog("razor-split", { id: element.domId ?? element.id, splitTime, file: targetPath });
const originalContent = await readFileContent(pid, targetPath);
const newId = generateSplitId(collectHtmlIds(originalContent), element.domId || "clip");
- const splitResult = await splitHtmlElement(pid, targetPath, patchTarget, splitTime, newId);
+ const splitResult = await splitHtmlElement(
+ pid,
+ targetPath,
+ patchTarget,
+ splitTime,
+ newId,
+ element.start,
+ element.duration,
+ );
if (!splitResult.ok) throw new Error("Failed to split clip.");
+ editLog("razor-split:done", { changed: splitResult.changed, newId });
if (!splitResult.changed) {
return { targetPath, originalContent, patchedContent: originalContent, changed: false };
}
diff --git a/packages/studio/src/hooks/useSafeGsapCommitMutation.test.tsx b/packages/studio/src/hooks/useSafeGsapCommitMutation.test.tsx
new file mode 100644
index 0000000000..df83dce5b5
--- /dev/null
+++ b/packages/studio/src/hooks/useSafeGsapCommitMutation.test.tsx
@@ -0,0 +1,86 @@
+// @vitest-environment happy-dom
+import { describe, it, expect, vi } from "vitest";
+import { act, createElement } from "react";
+import { createRoot } from "react-dom/client";
+
+// React's act() emits a warning unless the runtime is flagged as a test env.
+(globalThis as Record<string, unknown>).IS_REACT_ACT_ENVIRONMENT = true;
+import type { DomEditSelection } from "../components/editor/domEditingTypes";
+import type { CommitMutation } from "./gsapScriptCommitTypes";
+import { useSafeGsapCommitMutation } from "./useSafeGsapCommitMutation";
+
+/**
+ * Regression: the safe wrapper used to `void` the commit and return undefined, so
+ * the facade resolved IMMEDIATELY — consumers that `await session.commitMutation`
+ * (gesture recording, enable-keyframes) fired their post-actions (toast, reseek,
+ * "idle") BEFORE the server save landed. The wrapper must now RETURN the chain so
+ * awaiters resolve only after the save (and its error handling) settles.
+ */
+
+const selection = { id: "box", selector: "#box" } as unknown as DomEditSelection;
+
+// Mount a hook and capture its return value via a ref.
+function renderSafeCommit(
+ commitMutation: CommitMutation,
+ showToast?: (message: string, tone?: "error" | "info") => void,
+) {
+ const captured: { fn: ReturnType<typeof useSafeGsapCommitMutation> | null } = { fn: null };
+ function Probe() {
+ captured.fn = useSafeGsapCommitMutation(commitMutation, vi.fn(), showToast);
+ return null;
+ }
+ const container = document.createElement("div");
+ const root = createRoot(container);
+ act(() => {
+ root.render(createElement(Probe));
+ });
+ return captured.fn!;
+}
+
+describe("useSafeGsapCommitMutation — returned promise settles after the commit", () => {
+ it("resolves only after the underlying commit resolves", async () => {
+ let resolveCommit!: () => void;
+ const order: string[] = [];
+ const commitMutation: CommitMutation = vi.fn(
+ () =>
+ new Promise<void>((resolve) => {
+ resolveCommit = () => {
+ order.push("commit-settled");
+ resolve();
+ };
+ }),
+ );
+
+ const safe = renderSafeCommit(commitMutation);
+ const awaited = safe(selection, { type: "noop" }, { label: "test" }).then(() => {
+ order.push("awaiter-resumed");
+ });
+
+ // Give the microtask queue a chance — the awaiter must NOT have resumed yet
+ // because the commit is still pending.
+ await Promise.resolve();
+ expect(order).toEqual([]);
+
+ resolveCommit();
+ await awaited;
+ expect(order).toEqual(["commit-settled", "awaiter-resumed"]);
+ });
+
+ it("resolves (success-after-handled) after a failed commit, surfacing a toast", async () => {
+ const showToast = vi.fn();
+ const order: string[] = [];
+ const commitMutation: CommitMutation = vi.fn(async () => {
+ order.push("commit-rejected");
+ throw new Error("save failed");
+ });
+
+ const safe = renderSafeCommit(commitMutation, showToast);
+ await safe(selection, { type: "noop" }, { label: "test" });
+ order.push("awaiter-resumed");
+
+ // The awaiter resumes AFTER the rejection was handled, and the promise does
+ // not reject (the .catch swallows it for the toast).
+ expect(order).toEqual(["commit-rejected", "awaiter-resumed"]);
+ expect(showToast).toHaveBeenCalledWith(expect.stringContaining("Couldn't save"), "error");
+ });
+});
diff --git a/packages/studio/src/hooks/useSafeGsapCommitMutation.ts b/packages/studio/src/hooks/useSafeGsapCommitMutation.ts
index 7fe0cb8e8f..e957f1beeb 100644
--- a/packages/studio/src/hooks/useSafeGsapCommitMutation.ts
+++ b/packages/studio/src/hooks/useSafeGsapCommitMutation.ts
@@ -42,12 +42,16 @@ export function useSafeGsapCommitMutation(
selection: DomEditSelection,
mutation: Record<string, unknown>,
options: CommitMutationOptions,
- ) => {
- void commitMutation(selection, mutation, options).catch((error) => {
+ ): Promise<void> =>
+ // Return the chain so awaiting consumers (gesture commit, enable-keyframes)
+ // run their post-actions AFTER the server save settles, not immediately.
+ // The `.catch` handles the failure (toast + telemetry) and resolves the
+ // chain, so awaiters see a settled (success-after-handled) promise rather
+ // than an unhandled rejection.
+ commitMutation(selection, mutation, options).catch((error) => {
trackGsapSaveFailure(error, selection, mutation, options.label);
showToast?.(`Couldn't save animation: ${getStudioSaveErrorMessage(error)}`, "error");
- });
- },
+ }),
[commitMutation, trackGsapSaveFailure, showToast],
);
}
diff --git a/packages/studio/src/hooks/useServerConnection.ts b/packages/studio/src/hooks/useServerConnection.ts
index 98817f6870..acf4898648 100644
--- a/packages/studio/src/hooks/useServerConnection.ts
+++ b/packages/studio/src/hooks/useServerConnection.ts
@@ -26,7 +26,10 @@ export function useServerConnection(): ServerConnectionState {
useMountEffect(() => {
const hashProjectId = parseProjectIdFromHash(window.location.hash);
let cancelled = false;
- let retryTimer: ReturnType<typeof window.setTimeout> | null = null;
+ // Explicitly `number` (the DOM return of window.setTimeout) rather than
+ // ReturnType<typeof window.setTimeout> — with @types/node present, that infers
+ // NodeJS.Timeout and clashes with the DOM number the call actually returns.
+ let retryTimer: number | null = null;
function scheduleRetry() {
setWaitingForServer(true);
diff --git a/packages/studio/src/hooks/useStudioContextValue.ts b/packages/studio/src/hooks/useStudioContextValue.ts
index aa360586c8..177bd21a3f 100644
--- a/packages/studio/src/hooks/useStudioContextValue.ts
+++ b/packages/studio/src/hooks/useStudioContextValue.ts
@@ -90,8 +90,9 @@ export function useInspectorState(
inspectorPanelActive,
inspectorButtonActive:
STUDIO_INSPECTOR_PANELS_ENABLED && !rightCollapsed && inspectorPanelActive,
- shouldShowSelectedDomBounds:
- inspectorPanelActive && !rightCollapsed && !isPlaying && !isGestureRecording,
+ // Keep the selection box + motion path drawn even when the Inspector is
+ // collapsed — closing the panel shouldn't visually deselect the element.
+ shouldShowSelectedDomBounds: inspectorPanelActive && !isPlaying && !isGestureRecording,
};
}, [rightPanelTab, rightInspectorPanes, rightCollapsed, isPlaying, isGestureRecording]);
}
diff --git a/packages/studio/src/hooks/useStudioUrlState.ts b/packages/studio/src/hooks/useStudioUrlState.ts
index 6ab0b2f80e..7407c38e92 100644
--- a/packages/studio/src/hooks/useStudioUrlState.ts
+++ b/packages/studio/src/hooks/useStudioUrlState.ts
@@ -1,9 +1,11 @@
-import { useCallback, useEffect, useRef } from "react";
+import { useCallback, useEffect, useRef, useState } from "react";
import { usePlayerStore } from "../player";
import { findElementForSelection, type DomEditSelection } from "../components/editor/domEditing";
import { clampNumber, type RightPanelTab } from "../utils/studioHelpers";
+import { parseProjectIdFromHash } from "../utils/projectRouting";
import {
buildStudioHash,
+ parseStudioUrlStateFromHash,
type StudioUrlSelectionState,
type StudioUrlState,
} from "../utils/studioUrlState";
@@ -33,6 +35,7 @@ interface UseStudioUrlStateParams {
preserveGroup?: boolean;
},
) => void;
+ setRightPanelTab: (tab: RightPanelTab) => void;
initialState: StudioUrlState;
}
@@ -68,12 +71,17 @@ export function useStudioUrlState({
domEditSelection,
buildDomSelectionFromTarget,
applyDomSelection,
+ setRightPanelTab,
initialState,
}: UseStudioUrlStateParams) {
const currentTime = usePlayerStore((s) => s.currentTime);
const hydratedSeekRef = useRef(initialState.currentTime == null);
const hydratedInitialTimeRef = useRef(initialState.currentTime == null);
const hydratedSelectionRef = useRef(initialState.selection == null);
+ // Mirrors hydratedSelectionRef as state so the selection-hydration effect can
+ // drop its currentTime subscription once hydration completes — otherwise it
+ // re-runs on every playhead tick for the lifetime of the session.
+ const [selectionHydrated, setSelectionHydrated] = useState(initialState.selection == null);
const pendingSelectionRef = useRef(initialState.selection);
const stableTimeRef = useRef<number | null>(initialState.currentTime);
@@ -91,67 +99,91 @@ export function useStudioUrlState({
[activeCompPath, domEditSelection, rightCollapsed, rightPanelTab, timelineVisible],
);
+ // Resolve a URL selection to a live element and apply it. Shared by the initial
+ // hydration effect and the external-navigation (hashchange) handler. Returns
+ // false ONLY when the iframe document isn't ready yet (caller should retry);
+ // a missing element or null selection clears the selection and returns true.
+ const applyUrlSelection = useCallback(
+ (selection: StudioUrlSelectionState | null): boolean => {
+ if (!selection) {
+ applyDomSelection(null, { revealPanel: false });
+ return true;
+ }
+ let doc: Document | null = null;
+ try {
+ doc = previewIframeRef.current?.contentDocument ?? null;
+ } catch {
+ return false;
+ }
+ if (!doc) return false;
+ const element = findElementForSelection(
+ doc,
+ {
+ sourceFile: selection.sourceFile ?? "",
+ id: selection.id,
+ selector: selection.selector,
+ selectorIndex: selection.selectorIndex,
+ },
+ activeCompPath,
+ );
+ if (!element) {
+ applyDomSelection(null, { revealPanel: false });
+ return true;
+ }
+ void buildDomSelectionFromTarget(element, { preferClipAncestor: false }).then((resolved) => {
+ applyDomSelection(resolved, { revealPanel: false });
+ });
+ return true;
+ },
+ [activeCompPath, applyDomSelection, buildDomSelectionFromTarget, previewIframeRef],
+ );
+
useEffect(() => {
if (!projectId || hydratedSeekRef.current || compositionLoading) return;
const nextTime =
duration > 0
? clampNumber(initialState.currentTime ?? 0, 0, duration)
: Math.max(0, initialState.currentTime ?? 0);
+ // The request is honored even if it fires before the player runtime mounts:
+ // initializeAdapter reconciles the store's requestedSeekTime when the adapter
+ // becomes ready. currentTime then settles to nextTime, releasing the selection
+ // hydration below.
usePlayerStore.getState().requestSeek(nextTime);
stableTimeRef.current = nextTime;
hydratedSeekRef.current = true;
}, [projectId, compositionLoading, duration, initialState.currentTime]);
+ // Once hydration completes the selection effect no longer needs the playhead,
+ // so freeze its time dependency. This stops the effect re-running on every tick
+ // for the rest of the session (cosmetic perf) while still retrying as the seek
+ // settles before hydration.
+ const selectionHydrationTime = selectionHydrated ? 0 : currentTime;
useEffect(() => {
if (!projectId || hydratedSelectionRef.current || compositionLoading) return;
if (!hydratedSeekRef.current) return;
const targetTime = initialState.currentTime;
- if (targetTime != null && Math.abs(currentTime - stableTimeRef.current!) > 0.05) return;
-
- const pendingSelection = pendingSelectionRef.current;
- if (!pendingSelection) {
- hydratedSelectionRef.current = true;
+ if (targetTime != null && Math.abs(selectionHydrationTime - stableTimeRef.current!) > 0.05) {
return;
}
- let doc: Document | null = null;
- try {
- doc = previewIframeRef.current?.contentDocument ?? null;
- } catch {
- return;
- }
- if (!doc) return;
-
- const element = findElementForSelection(
- doc,
- {
- sourceFile: pendingSelection.sourceFile ?? "",
- id: pendingSelection.id,
- selector: pendingSelection.selector,
- selectorIndex: pendingSelection.selectorIndex,
- },
- activeCompPath,
- );
- if (!element) {
- applyDomSelection(null, { revealPanel: false });
+ const markHydrated = () => {
hydratedSelectionRef.current = true;
- pendingSelectionRef.current = null;
+ setSelectionHydrated(true);
+ };
+ const pendingSelection = pendingSelectionRef.current;
+ if (!pendingSelection) {
+ markHydrated();
return;
}
-
- hydratedSelectionRef.current = true;
+ // Doc not ready yet → leave hydration pending so a later tick retries.
+ if (!applyUrlSelection(pendingSelection)) return;
+ markHydrated();
pendingSelectionRef.current = null;
- void buildDomSelectionFromTarget(element, { preferClipAncestor: false }).then((selection) => {
- applyDomSelection(selection, { revealPanel: false });
- });
}, [
- activeCompPath,
- applyDomSelection,
- buildDomSelectionFromTarget,
+ applyUrlSelection,
compositionLoading,
- currentTime,
+ selectionHydrationTime,
initialState.currentTime,
- previewIframeRef,
projectId,
refreshKey,
]);
@@ -185,4 +217,32 @@ export function useStudioUrlState({
if (!projectId) return;
replaceHash(buildStudioHash(projectId, buildUrlState()));
}, [activeCompPathHydrated, buildUrlState, projectId]);
+
+ // Re-apply URL state when the hash changes externally (pasting a new link,
+ // back/forward) AFTER initial load. The app only reads the URL once on mount
+ // and otherwise WRITES the hash via replaceState (which never fires
+ // `hashchange`), so this listener sees only genuine external navigations —
+ // without it, opening a same-project deep link (different `t`, element, or
+ // tab) is silently ignored and then overwritten by the next hash-sync.
+ useEffect(() => {
+ if (!projectId) return;
+ const onHashChange = () => {
+ if (parseProjectIdFromHash(window.location.hash) !== projectId) return; // different project → remount handles it
+ const parsed = parseStudioUrlStateFromHash(window.location.hash);
+ if (parsed.currentTime != null) {
+ const clamped =
+ duration > 0
+ ? clampNumber(parsed.currentTime, 0, duration)
+ : Math.max(0, parsed.currentTime);
+ if (Math.abs(usePlayerStore.getState().currentTime - clamped) > 0.05) {
+ usePlayerStore.getState().requestSeek(clamped);
+ stableTimeRef.current = clamped;
+ }
+ }
+ applyUrlSelection(parsed.selection);
+ if (parsed.rightPanelTab) setRightPanelTab(parsed.rightPanelTab);
+ };
+ window.addEventListener("hashchange", onHashChange);
+ return () => window.removeEventListener("hashchange", onHashChange);
+ }, [projectId, duration, applyUrlSelection, setRightPanelTab]);
}
diff --git a/packages/studio/src/player/components/ShortcutsPanel.tsx b/packages/studio/src/player/components/ShortcutsPanel.tsx
index b91ef63989..88c21eb370 100644
--- a/packages/studio/src/player/components/ShortcutsPanel.tsx
+++ b/packages/studio/src/player/components/ShortcutsPanel.tsx
@@ -50,6 +50,14 @@ const SHORTCUT_SECTIONS = [
{ key: "⌘ Scroll", label: "Record scale" },
],
},
+ {
+ title: "Canvas",
+ hints: [
+ { key: "Drag", label: "Move element / add keyframe" },
+ { key: "⌥ Drag", label: "Move entire animation path" },
+ { key: "⇧ Drag", label: "Uniform resize" },
+ ],
+ },
{
title: "Panels",
hints: [
diff --git a/packages/studio/src/player/components/TimelineClipDiamonds.tsx b/packages/studio/src/player/components/TimelineClipDiamonds.tsx
index 361b975fe4..d396e54f8a 100644
--- a/packages/studio/src/player/components/TimelineClipDiamonds.tsx
+++ b/packages/studio/src/player/components/TimelineClipDiamonds.tsx
@@ -42,13 +42,10 @@ interface TimelineClipDiamondsProps {
const DIAMOND_RATIO = 0.8;
// Percentage tolerance for rendering keyframes near clip boundaries. Keyframes
// slightly outside [0, 100] (from rounding or stale cache during the async
-// persist → reload cycle) are clamped to the clip edge rather than hidden.
-export const KF_MIN_PCT = -5;
-export const KF_MAX_PCT = 105;
-
-function clampDiamondLeft(rawLeft: number, diamondSize: number, clipWidth: number): number {
- return Math.max(0, Math.min(clipWidth - diamondSize, rawLeft));
-}
+// persist → reload cycle) are still rendered (the clip is overflow-visible) at
+// their true position rather than hidden.
+const KF_MIN_PCT = -5;
+const KF_MAX_PCT = 105;
export const TimelineClipDiamonds = memo(function TimelineClipDiamonds({
keyframesData,
@@ -225,11 +222,12 @@ export const TimelineClipDiamonds = memo(function TimelineClipDiamonds({
})}
{sorted.map((kf, i) => {
- const leftPx = clampDiamondLeft(
- (effPct(kf.percentage) / 100) * clipWidthPx - half,
- diamondSize,
- clipWidthPx,
- );
+ // Center the diamond ON its keyframe %: left = (% · width) − half so the
+ // diamond's midpoint sits exactly at the percentage. At 0% the midpoint
+ // is the clip's left edge (the diamond's left half overflows, which the
+ // overflow-visible clip shows) — NOT shifted fully inside. No clamp, or
+ // boundary keyframes (0% / 100%) would render off-center.
+ const leftPx = (effPct(kf.percentage) / 100) * clipWidthPx - half;
const kfKey = `${elementId}:${kf.percentage}`;
const isKfSelected = selectedKeyframes.has(kfKey);
const atPlayhead = isSelected && Math.abs(kf.percentage - currentPercentage) < 0.5;
diff --git a/packages/studio/src/player/components/TimelinePropertyRows.tsx b/packages/studio/src/player/components/TimelinePropertyRows.tsx
deleted file mode 100644
index a87e75a8fa..0000000000
--- a/packages/studio/src/player/components/TimelinePropertyRows.tsx
+++ /dev/null
@@ -1,126 +0,0 @@
-import { memo } from "react";
-import type { KeyframeCacheEntry } from "../store/playerStore";
-import { KF_MIN_PCT, KF_MAX_PCT } from "./TimelineClipDiamonds";
-
-const SUB_TRACK_H = 24;
-const DIAMOND_SIZE = 6;
-const HALF = DIAMOND_SIZE / 2;
-
-interface TimelinePropertyRowsProps {
- keyframesData: KeyframeCacheEntry;
- clipWidthPx: number;
- clipLeftPx: number;
- accentColor: string;
- isSelected: boolean;
- currentPercentage: number;
- elementId: string;
- selectedKeyframes: Set<string>;
- onClickKeyframe?: (percentage: number) => void;
-}
-
-function extractProperties(data: KeyframeCacheEntry): string[] {
- const props = new Set<string>();
- for (const kf of data.keyframes) {
- for (const key of Object.keys(kf.properties)) {
- props.add(key);
- }
- }
- return Array.from(props).sort();
-}
-
-export const TimelinePropertyRows = memo(function TimelinePropertyRows({
- keyframesData,
- clipWidthPx,
- clipLeftPx,
- accentColor,
- isSelected,
- currentPercentage,
- elementId,
- selectedKeyframes,
- onClickKeyframe,
-}: TimelinePropertyRowsProps) {
- const properties = extractProperties(keyframesData);
- if (properties.length === 0 || clipWidthPx < 20) return null;
-
- return (
- <div className="flex flex-col">
- {properties.map((prop) => {
- const propKeyframes = keyframesData.keyframes
- .filter((kf) => prop in kf.properties)
- .filter((kf) => kf.percentage >= KF_MIN_PCT && kf.percentage <= KF_MAX_PCT);
- if (propKeyframes.length === 0) return null;
-
- return (
- <div key={prop} className="relative flex items-center" style={{ height: SUB_TRACK_H }}>
- <span className="absolute left-1 text-[8px] font-medium text-neutral-600 z-10 select-none">
- {prop}
- </span>
- <svg
- className="absolute"
- style={{ left: clipLeftPx, width: clipWidthPx, height: SUB_TRACK_H }}
- viewBox={`0 0 ${clipWidthPx} ${SUB_TRACK_H}`}
- >
- <line
- x1={0}
- y1={SUB_TRACK_H / 2}
- x2={clipWidthPx}
- y2={SUB_TRACK_H / 2}
- stroke={isSelected ? accentColor : "#525252"}
- strokeOpacity={0.15}
- strokeWidth={1}
- />
- {propKeyframes.map((kf) => {
- const x = Math.max(
- HALF,
- Math.min(clipWidthPx - HALF, (kf.percentage / 100) * clipWidthPx),
- );
- const y = SUB_TRACK_H / 2;
- const key = `${elementId}:${kf.percentage}`;
- const isKfSelected = selectedKeyframes.has(key);
- const isHold = kf.ease === "steps(1)";
- const fillColor =
- isKfSelected || (isSelected && Math.abs(kf.percentage - currentPercentage) < 0.5)
- ? accentColor
- : isSelected
- ? `${accentColor}80`
- : "#737373";
-
- return (
- <g
- key={kf.percentage}
- onClick={(e) => {
- e.stopPropagation();
- onClickKeyframe?.(kf.percentage);
- }}
- style={{ cursor: "pointer" }}
- >
- {isHold ? (
- <rect
- x={x - HALF}
- y={y - HALF}
- width={DIAMOND_SIZE}
- height={DIAMOND_SIZE}
- fill={fillColor}
- />
- ) : (
- <rect
- x={x - HALF}
- y={y - HALF}
- width={DIAMOND_SIZE}
- height={DIAMOND_SIZE}
- fill={fillColor}
- transform={`rotate(45, ${x}, ${y})`}
- />
- )}
- </g>
- );
- })}
- </svg>
- </div>
- );
- })}
- </div>
- );
-});
-
-export { SUB_TRACK_H };
diff --git a/packages/studio/src/player/hooks/useExpandedTimelineElements.test.ts b/packages/studio/src/player/hooks/useExpandedTimelineElements.test.ts
index 51d5980b3d..a8399dee8e 100644
--- a/packages/studio/src/player/hooks/useExpandedTimelineElements.test.ts
+++ b/packages/studio/src/player/hooks/useExpandedTimelineElements.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { buildExpandedElements } from "./useExpandedTimelineElements";
+import { buildTimelineElementKey } from "../lib/timelineElementHelpers";
import type { TimelineElement } from "../store/playerStore";
import type { ClipManifestClip } from "../lib/playbackTypes";
@@ -88,4 +89,37 @@ describe("buildExpandedElements", () => {
expect(child.expandedParentStart).toBe(13); // C's start, not B's 12 or A's 10
expect(child.sourceFile).toBe("c.html"); // C's file, not b.html or a.html
});
+
+ // Regression: an expanded child must share one identity (`key`) with the flat
+ // store element for the same DOM id. Before the fix the child key fell back to
+ // the colon form (`index.html:eyebrow:N`) while the store/selection used the
+ // hash form (`index.html#eyebrow`), so clicking an expanded child never
+ // highlighted it (isSelected compares the two keys).
+ it("keys expanded children in hash form, matching the flat store element", () => {
+ // Single composition (no sub-comps): scene `s1` with same-file children.
+ const elements = [el({ id: "s1", domId: "s1", start: 0, duration: 14 })];
+ const manifest = [
+ clip({ id: "s1", start: 0, duration: 14 }),
+ clip({ id: "eyebrow", start: 0, duration: 14 }),
+ clip({ id: "title", start: 0, duration: 14 }),
+ ];
+ const parentMap = new Map([
+ ["eyebrow", "s1"],
+ ["title", "s1"],
+ ]);
+
+ const out = buildExpandedElements(elements, manifest, parentMap, "s1", "s1");
+ const child = out.find((e) => e.domId === "eyebrow")!;
+
+ const expectedStoreKey = buildTimelineElementKey({
+ id: "eyebrow",
+ fallbackIndex: 0,
+ domId: "eyebrow",
+ selector: "#eyebrow",
+ sourceFile: undefined,
+ });
+ expect(expectedStoreKey).toBe("index.html#eyebrow");
+ expect(child.key).toBe("index.html#eyebrow");
+ expect(child.key).toBe(expectedStoreKey);
+ });
});
diff --git a/packages/studio/src/player/hooks/useExpandedTimelineElements.ts b/packages/studio/src/player/hooks/useExpandedTimelineElements.ts
index 6903402cc9..9649b2af74 100644
--- a/packages/studio/src/player/hooks/useExpandedTimelineElements.ts
+++ b/packages/studio/src/player/hooks/useExpandedTimelineElements.ts
@@ -2,6 +2,7 @@ import { useMemo } from "react";
import { usePlayerStore, type TimelineElement } from "../store/playerStore";
import type { ClipManifestClip } from "../lib/playbackTypes";
import { createTimelineElementFromManifestClip } from "../lib/timelineDOM";
+import { buildTimelineElementKey } from "../lib/timelineElementHelpers";
function findTopLevelAncestor(id: string, parentMap: Map<string, string>): string | null {
let current = parentMap.get(id);
@@ -78,14 +79,31 @@ function buildChildElements(
clip: child,
fallbackIndex: result.length,
});
+ const domId = child.id ?? undefined;
+ const selector = child.id ? `#${child.id}` : undefined;
+ // `base.key` was built without a hostEl, so it fell back to the colon form
+ // (`index.html:<id>:<idx>`) even though we set domId below. Recompute it from
+ // the same inputs the store uses (`<sourceFile>#<domId>`) so an expanded
+ // child shares one identity with its flat store element — otherwise selecting
+ // it sets `selectedElementId` to the store's hash key while the rendered row
+ // is keyed by the colon form, and `isSelected` never matches (no highlight).
+ const key = buildTimelineElementKey({
+ id: base.id,
+ fallbackIndex: result.length,
+ domId,
+ selector,
+ selectorIndex: base.selectorIndex,
+ sourceFile: editBasis.sourceFile,
+ });
result.push({
...base,
+ key,
start: clamped.start,
duration: clamped.duration,
track: display.track + result.length,
expandedParentStart: editBasis.start,
- domId: child.id ?? undefined,
- selector: child.id ? `#${child.id}` : undefined,
+ domId,
+ selector,
sourceFile: editBasis.sourceFile,
timingSource: "authored" as const,
});
diff --git a/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts b/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts
index 6f74c94091..03c02e1f99 100644
--- a/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts
+++ b/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts
@@ -174,8 +174,16 @@ export function useTimelineSyncCallbacks({
if (!adapter || adapter.getDuration() <= 0) return false;
adapter.pause();
- const seekTo = pendingSeekRef.current;
+ // Honor a seek requested before the adapter was ready. It may sit in either
+ // place: `pendingSeekRef` if the store subscription was mounted when requestSeek
+ // fired, or only in the store's `requestedSeekTime` if it fired earlier still
+ // (deep-link hydration runs before the player subscription mounts, so the request
+ // never reaches pendingSeekRef). Reconciling with the store here is what makes a
+ // deep-linked `?t=` land instead of starting at 0.
+ const storeSeek = usePlayerStore.getState().requestedSeekTime;
+ const seekTo = pendingSeekRef.current ?? storeSeek;
pendingSeekRef.current = null;
+ if (storeSeek != null) usePlayerStore.getState().clearSeekRequest();
const startTime = seekTo != null ? Math.min(seekTo, adapter.getDuration()) : 0;
adapter.seek(startTime);
diff --git a/packages/studio/src/player/lib/mediaProbe.ts b/packages/studio/src/player/lib/mediaProbe.ts
index 1304b46105..90b782336d 100644
--- a/packages/studio/src/player/lib/mediaProbe.ts
+++ b/packages/studio/src/player/lib/mediaProbe.ts
@@ -8,6 +8,9 @@ interface MediaProbeResult {
const cache = new Map<string, MediaProbeResult>();
const inflight = new Map<string, Promise<MediaProbeResult | null>>();
+// URLs whose probe failed (CORS, 404, non-media). Remembered so the rAF-driven
+// timeline re-derive doesn't re-fetch them every frame and flood the console.
+const failed = new Set<string>();
let mediabunnyModule: typeof import("mediabunny") | null | false = null;
@@ -97,7 +100,8 @@ export async function probeMissingSourceDurations<
el.src &&
el.sourceDuration == null &&
["video", "audio"].includes(el.tag.toLowerCase()) &&
- !getCachedProbe(el.src),
+ !getCachedProbe(el.src) &&
+ !failed.has(normalizeUrl(el.src)),
);
if (needs.length === 0) return;
await Promise.allSettled(
@@ -112,6 +116,7 @@ async function probeMediaUrl(url: string): Promise<MediaProbeResult | null> {
const key = normalizeUrl(url);
const cached = cache.get(key);
if (cached) return cached;
+ if (failed.has(key)) return null;
let pending = inflight.get(key);
if (pending) return pending;
@@ -119,6 +124,7 @@ async function probeMediaUrl(url: string): Promise<MediaProbeResult | null> {
pending = probeOne(key).then((result) => {
inflight.delete(key);
if (result) cache.set(key, result);
+ else failed.add(key);
return result;
});
inflight.set(key, pending);
diff --git a/packages/studio/src/player/store/playerStore.ts b/packages/studio/src/player/store/playerStore.ts
index 05b378d11d..3003283d7e 100644
--- a/packages/studio/src/player/store/playerStore.ts
+++ b/packages/studio/src/player/store/playerStore.ts
@@ -96,6 +96,14 @@ interface PlayerState {
* (drag, resize, rotate) target this instead of recomputing from playhead. */
activeKeyframePct: number | null;
setActiveKeyframePct: (pct: number | null) => void;
+ /** Motion-path "set destination" mode. Armed from the preview toolbar (replaces
+ * the old double-click-on-canvas UX); while armed, one canvas click places the
+ * new path's destination. `available` is published by MotionPathOverlay so the
+ * toolbar shows the button only when the selected element can take a path. */
+ motionPathArmed: boolean;
+ setMotionPathArmed: (armed: boolean) => void;
+ motionPathCreateAvailable: boolean;
+ setMotionPathCreateAvailable: (available: boolean) => void;
/** Multi-select: additional selected elements beyond selectedElementId. */
selectedElementIds: Set<string>;
@@ -209,6 +217,10 @@ export const usePlayerStore = create<PlayerState>((set, get) => ({
activeKeyframePct: null,
setActiveKeyframePct: (pct) => set({ activeKeyframePct: pct }),
+ motionPathArmed: false,
+ setMotionPathArmed: (armed) => set({ motionPathArmed: armed }),
+ motionPathCreateAvailable: false,
+ setMotionPathCreateAvailable: (available) => set({ motionPathCreateAvailable: available }),
selectedElementIds: new Set<string>(),
toggleSelectedElementId: (id: string) =>
@@ -327,7 +339,16 @@ export const usePlayerStore = create<PlayerState>((set, get) => ({
setTimelineReady: (ready) => set({ timelineReady: ready }),
setBeatDragging: (dragging) => set({ beatDragging: dragging }),
setElements: (elements) => set({ elements }),
- setSelectedElementId: (id) => set({ selectedElementId: id }),
+ setSelectedElementId: (id) =>
+ set((s) =>
+ // Selecting a different element drops any active keyframe selection — otherwise
+ // a stale activeKeyframePct from a prior diamond click would force the next drag
+ // to "modify" a keyframe on the new element. A diamond click sets the pct AFTER
+ // calling setSelectedElementId, so this never clobbers a genuine keyframe select.
+ id !== s.selectedElementId
+ ? { selectedElementId: id, activeKeyframePct: null, motionPathArmed: false }
+ : { selectedElementId: id },
+ ),
updateElement: (elementId, updates) =>
set((state) => ({
elements: state.elements.map((el) =>
@@ -361,3 +382,19 @@ export const usePlayerStore = create<PlayerState>((set, get) => ({
clipParentMap: new Map(),
}),
}));
+
+// Bug-bash aid: expose the store so a reproduction can dump live state from the
+// console, e.g. `__playerStore.getState().selectedElementId`. Harmless read
+// handle; no behavioural effect.
+// Only in dev. `import.meta.env` may be undefined in non-Vite bundlers (Next.js
+// Turbopack), so guard the access like the telemetry client does.
+function isDevBuild(): boolean {
+ try {
+ return import.meta.env.DEV === true;
+ } catch {
+ return false;
+ }
+}
+if (isDevBuild() && typeof window !== "undefined") {
+ (window as unknown as { __playerStore?: typeof usePlayerStore }).__playerStore = usePlayerStore;
+}
diff --git a/packages/studio/src/utils/editDebugLog.ts b/packages/studio/src/utils/editDebugLog.ts
new file mode 100644
index 0000000000..88a0607ad4
--- /dev/null
+++ b/packages/studio/src/utils/editDebugLog.ts
@@ -0,0 +1,16 @@
+// Gated strategic logging for the GSAP keyframe / manual-drag / gesture / razor
+// edit flows. Silent in production; on in dev builds, or anywhere once you set
+// `window.__hfDebug = true` in the console. Single `[hf-edit:<scope>]` prefix so
+// the whole edit pipeline is greppable. Fires only at commit boundaries (user
+// actions), never in render/raf loops, so it doesn't spam.
+export function editLog(scope: string, ...args: unknown[]): void {
+ if (typeof window === "undefined") return;
+ const w = window as unknown as { __hfDebug?: boolean };
+ if (!import.meta.env.DEV && !w.__hfDebug) return;
+ // Stringify object args so the console prints their contents inline (`{x:1}`)
+ // instead of a collapsed `Object` — keeps the edit trail greppable/copyable.
+ const parts = args.map((a) =>
+ typeof a === "object" && a !== null ? JSON.stringify(a) : String(a),
+ );
+ console.debug(`[hf-edit:${scope}]`, ...parts);
+}
diff --git a/packages/studio/src/utils/gsapSoftReload.test.ts b/packages/studio/src/utils/gsapSoftReload.test.ts
index 6f6c4515ae..9552acf98e 100644
--- a/packages/studio/src/utils/gsapSoftReload.test.ts
+++ b/packages/studio/src/utils/gsapSoftReload.test.ts
@@ -1,7 +1,7 @@
// @vitest-environment happy-dom
import { describe, it, expect, vi } from "vitest";
-import { applySoftReload } from "./gsapSoftReload";
+import { applySoftReload, ensureMotionPathPluginLoaded } from "./gsapSoftReload";
const SCRIPT_TEXT = `
window.__timelines = window.__timelines || {};
@@ -10,6 +10,13 @@ tl.to("#box", { opacity: 0.8 });
window.__timelines["root"] = tl;
`;
+const MOTION_PATH_SCRIPT_TEXT = `
+window.__timelines = window.__timelines || {};
+const tl = gsap.timeline({ paused: true });
+tl.to("#box", { motionPath: { path: [{ x: 0, y: 0 }, { x: 100, y: 50 }] } });
+window.__timelines["root"] = tl;
+`;
+
function buildMockIframe(overrides: Record<string, unknown> = {}) {
const scriptEl = document.createElement("script");
scriptEl.textContent =
@@ -58,29 +65,35 @@ function buildMockIframe(overrides: Record<string, unknown> = {}) {
}
describe("applySoftReload", () => {
- it("returns false when iframe is null", () => {
- expect(applySoftReload(null, SCRIPT_TEXT)).toBe(false);
+ it('returns "cannot-soft-reload" when iframe is null', () => {
+ expect(applySoftReload(null, SCRIPT_TEXT)).toBe("cannot-soft-reload");
});
- it("returns false when scriptText is empty", () => {
+ it('returns "cannot-soft-reload" when scriptText is empty', () => {
const { iframe } = buildMockIframe();
- expect(applySoftReload(iframe, "")).toBe(false);
+ expect(applySoftReload(iframe, "")).toBe("cannot-soft-reload");
});
- it("returns false when gsap is not on iframe window", () => {
+ it('returns "cannot-soft-reload" when gsap is not on iframe window', () => {
const { iframe } = buildMockIframe({ gsap: undefined });
- expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe(false);
+ expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe("cannot-soft-reload");
});
- it("returns false when __hfForceTimelineRebind is missing", () => {
+ it('returns "cannot-soft-reload" when __hfForceTimelineRebind is missing', () => {
const { iframe } = buildMockIframe({ __hfForceTimelineRebind: undefined });
- expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe(false);
+ expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe("cannot-soft-reload");
+ });
+
+ it('returns "cannot-soft-reload" when the script registers no scopable key', () => {
+ // No __timelines["key"] pattern → targetKeys is empty → can't scope safely.
+ const { iframe } = buildMockIframe();
+ expect(applySoftReload(iframe, 'gsap.to("#box", { x: 1 });')).toBe("cannot-soft-reload");
});
it("kills existing timelines, rebinds, and re-seeks on success", () => {
const { iframe, contentWindow, mockTimeline } = buildMockIframe();
const result = applySoftReload(iframe, SCRIPT_TEXT);
- expect(result).toBe(true);
+ expect(result).toBe("applied");
expect(mockTimeline.kill).toHaveBeenCalled();
expect(contentWindow.__hfForceTimelineRebind).toHaveBeenCalled();
expect(contentWindow.__player.seek).toHaveBeenCalledWith(2.0);
@@ -96,11 +109,119 @@ describe("applySoftReload", () => {
},
});
const result = applySoftReload(iframe, SCRIPT_TEXT);
- expect(result).toBe(true);
+ expect(result).toBe("applied");
expect(suppressionCalled).toBe(true);
});
- it("returns false when multiple GSAP scripts exist (ambiguous)", () => {
+ it('returns "applied" when the re-run re-registers the script\'s expected key', () => {
+ // SCRIPT_TEXT registers __timelines["root"]; buildMockIframe's appendChild
+ // shim repopulates `root` on execution. The hardened verify checks the
+ // expected target key is present (not merely "some key"), so a correct re-run
+ // reliably reports "applied" — it doesn't spuriously hit the transient window.
+ const { iframe, contentWindow } = buildMockIframe();
+ expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe("applied");
+ expect(contentWindow.__timelines.root).toBeDefined();
+ });
+
+ it('returns "verify-failed" (transient) when the re-run leaves the key empty', () => {
+ // No appendChild shim repopulation: the body container has no shim, so the
+ // re-run kills __timelines["root"] and the new script doesn't re-register it.
+ // That is the TRANSIENT post-run window — surfaced as "verify-failed" so
+ // callers know NOT to escalate (the live gsap.set already shows the value).
+ const scriptEl = document.createElement("script");
+ scriptEl.textContent = 'window.__timelines["root"] = gsap.timeline();';
+ const container = document.createElement("div"); // no appendChild shim
+ container.appendChild(scriptEl);
+ const { iframe } = buildMockIframe();
+ (iframe as unknown as { contentDocument: unknown }).contentDocument = {
+ querySelectorAll: (sel: string) => (sel === "script:not([src])" ? [scriptEl] : []),
+ createElement: (tag: string) => document.createElement(tag),
+ body: container,
+ head: document.createElement("div"),
+ };
+ expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe("verify-failed");
+ });
+
+ it("editing composition A leaves composition B's timeline intact (scoped kill)", () => {
+ // Two comps live side by side; the soft reload only re-runs comp "root".
+ // Comp "subscene" must survive untouched — the regression the full remount
+ // (re-inline) used to cause.
+ const subsceneTimeline = { kill: vi.fn(), pause: vi.fn() };
+ const { iframe, contentWindow, mockTimeline } = buildMockIframe({
+ __timelines: {
+ root: { kill: vi.fn(), pause: vi.fn() },
+ subscene: subsceneTimeline,
+ } as Record<string, { kill: ReturnType<typeof vi.fn>; pause: ReturnType<typeof vi.fn> }>,
+ });
+ void mockTimeline;
+
+ expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe("applied");
+ // Comp B was never killed and is still registered.
+ expect(subsceneTimeline.kill).not.toHaveBeenCalled();
+ expect(contentWindow.__timelines.subscene).toBe(subsceneTimeline);
+ });
+
+ it("runs synchronously (no async plugin load) when MotionPathPlugin is already present", () => {
+ // The preview bootstrap pre-loads MotionPathPlugin, so win.MotionPathPlugin
+ // is set before any motion-path edit. The soft reload must then execute the
+ // script inline — no CDN <script> appended to <head>, the timeline is
+ // repopulated synchronously, and verifyTimelinesPopulated reports the real
+ // result (not the optimistic-true async path).
+ const headAppends: Node[] = [];
+ const head = document.createElement("div");
+ const realHeadAppend = head.appendChild.bind(head);
+ head.appendChild = <T extends Node>(node: T): T => {
+ headAppends.push(node);
+ return realHeadAppend(node);
+ };
+ const { iframe, contentWindow } = buildMockIframe({ MotionPathPlugin: {} });
+ (iframe.contentDocument as unknown as { head: unknown }).head = head;
+
+ const result = applySoftReload(iframe, MOTION_PATH_SCRIPT_TEXT);
+
+ expect(result).toBe("applied");
+ // No CDN plugin <script> was appended to <head> — ran inline.
+ expect(headAppends.filter((n) => n instanceof HTMLScriptElement)).toHaveLength(0);
+ expect(contentWindow.__hfForceTimelineRebind).toHaveBeenCalled();
+ expect(contentWindow.__player.seek).toHaveBeenCalledWith(2.0);
+ expect(contentWindow.__timelines.root).toBeDefined();
+ });
+
+ it("falls back to the async plugin load when MotionPathPlugin is genuinely absent", () => {
+ const head = document.createElement("div");
+ const appendedScripts: HTMLScriptElement[] = [];
+ const realHeadAppend = head.appendChild.bind(head);
+ head.appendChild = <T extends Node>(node: T): T => {
+ if (node instanceof HTMLScriptElement) appendedScripts.push(node);
+ return realHeadAppend(node);
+ };
+ // gsap present but MotionPathPlugin unset → async load path.
+ const { iframe, contentWindow } = buildMockIframe({
+ MotionPathPlugin: undefined,
+ gsap: { timeline: vi.fn(), registerPlugin: vi.fn() },
+ });
+ (iframe.contentDocument as unknown as { head: unknown }).head = head;
+
+ const onAsyncFailure = vi.fn();
+ const result = applySoftReload(iframe, MOTION_PATH_SCRIPT_TEXT, onAsyncFailure);
+
+ // Optimistically "applied" (script will run once the plugin loads) — and the
+ // script has NOT executed yet, so the timeline isn't rebound synchronously.
+ expect(result).toBe("applied");
+ expect(appendedScripts).toHaveLength(1);
+ expect(appendedScripts[0]!.src).toContain("MotionPathPlugin");
+ expect(contentWindow.__hfForceTimelineRebind).not.toHaveBeenCalled();
+
+ // onerror must NOT run the script (that would reference a missing plugin) —
+ // it escalates via onAsyncFailure so the caller can full-reload to recover,
+ // and clears the in-flight loading flag.
+ appendedScripts[0]!.onerror?.(new Event("error"));
+ expect(onAsyncFailure).toHaveBeenCalledTimes(1);
+ expect(contentWindow.__hfForceTimelineRebind).not.toHaveBeenCalled();
+ expect(contentWindow.__hfMotionPathPluginLoading).toBe(false);
+ });
+
+ it('returns "cannot-soft-reload" when multiple GSAP scripts exist (ambiguous)', () => {
const script1 = document.createElement("script");
script1.textContent = "const tl = gsap.timeline({ paused: true });";
const script2 = document.createElement("script");
@@ -115,6 +236,89 @@ describe("applySoftReload", () => {
createElement: (tag: string) => document.createElement(tag),
body: container,
};
- expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe(false);
+ // Multiple scripts, none registering "root" → can't identify what to replace
+ // → structural failure that genuinely needs a full reload.
+ expect(applySoftReload(iframe, SCRIPT_TEXT)).toBe("cannot-soft-reload");
+ });
+});
+
+function buildBootstrapIframe(overrides: Record<string, unknown> = {}) {
+ const head = document.createElement("div");
+ const appendedScripts: HTMLScriptElement[] = [];
+ const realHeadAppend = head.appendChild.bind(head);
+ head.appendChild = <T extends Node>(node: T): T => {
+ if (node instanceof HTMLScriptElement) appendedScripts.push(node);
+ return realHeadAppend(node);
+ };
+
+ const registerPlugin = vi.fn();
+ const contentWindow = {
+ gsap: { registerPlugin } as Record<string, unknown> | undefined,
+ MotionPathPlugin: undefined as unknown,
+ __hfMotionPathPluginLoading: undefined as boolean | undefined,
+ ...overrides,
+ };
+ const contentDocument = {
+ createElement: (tag: string) => document.createElement(tag),
+ head,
+ };
+ return {
+ iframe: { contentWindow, contentDocument } as unknown as HTMLIFrameElement,
+ contentWindow,
+ appendedScripts,
+ registerPlugin,
+ };
+}
+
+describe("ensureMotionPathPluginLoaded", () => {
+ it("no-ops when the iframe is null", () => {
+ expect(() => ensureMotionPathPluginLoaded(null)).not.toThrow();
+ });
+
+ it("no-ops when gsap is unavailable", () => {
+ const { iframe, appendedScripts } = buildBootstrapIframe({ gsap: undefined });
+ ensureMotionPathPluginLoaded(iframe);
+ expect(appendedScripts).toHaveLength(0);
+ });
+
+ it("appends the plugin script once and registers it on load", () => {
+ const { iframe, contentWindow, appendedScripts, registerPlugin } = buildBootstrapIframe();
+ ensureMotionPathPluginLoaded(iframe);
+ expect(appendedScripts).toHaveLength(1);
+ expect(appendedScripts[0]!.src).toContain("MotionPathPlugin");
+ expect(contentWindow.__hfMotionPathPluginLoading).toBe(true);
+
+ // Simulate the CDN load completing; the plugin is now present.
+ contentWindow.MotionPathPlugin = {};
+ appendedScripts[0]!.onload?.(new Event("load"));
+ expect(registerPlugin).toHaveBeenCalledWith(contentWindow.MotionPathPlugin);
+ expect(contentWindow.__hfMotionPathPluginLoading).toBe(false);
+ });
+
+ it("is idempotent: a second call while loading does not append a second script", () => {
+ const { iframe, appendedScripts } = buildBootstrapIframe();
+ ensureMotionPathPluginLoaded(iframe);
+ ensureMotionPathPluginLoaded(iframe);
+ expect(appendedScripts).toHaveLength(1);
+ });
+
+ it("registers an already-present plugin without appending a script", () => {
+ const plugin = {};
+ const { iframe, appendedScripts, registerPlugin } = buildBootstrapIframe({
+ MotionPathPlugin: plugin,
+ });
+ ensureMotionPathPluginLoaded(iframe);
+ expect(appendedScripts).toHaveLength(0);
+ expect(registerPlugin).toHaveBeenCalledWith(plugin);
+ });
+
+ it("clears the loading flag and still resolves when the CDN load errors", () => {
+ const { iframe, contentWindow, appendedScripts } = buildBootstrapIframe();
+ ensureMotionPathPluginLoaded(iframe);
+ appendedScripts[0]!.onerror?.(new Event("error"));
+ expect(contentWindow.__hfMotionPathPluginLoading).toBe(false);
+ // A subsequent call can retry (plugin still absent, flag cleared).
+ ensureMotionPathPluginLoaded(iframe);
+ expect(appendedScripts).toHaveLength(2);
});
});
diff --git a/packages/studio/src/utils/gsapSoftReload.ts b/packages/studio/src/utils/gsapSoftReload.ts
index e60e001d86..3b03c86749 100644
--- a/packages/studio/src/utils/gsapSoftReload.ts
+++ b/packages/studio/src/utils/gsapSoftReload.ts
@@ -4,6 +4,10 @@ type IframeWindow = Window & {
__hfForceTimelineRebind?: () => void;
__hfSuppressSceneMutations?: <T>(fn: () => T) => T;
__hfStudioManualEditsApply?: () => void;
+ // Set while a MotionPathPlugin <script> is being fetched, so overlapping soft
+ // reloads (each needing the plugin) don't queue duplicate plugin scripts that
+ // re-flash the iframe. Cleared once the plugin loads or errors.
+ __hfMotionPathPluginLoading?: boolean;
gsap?: {
timeline?: (...args: unknown[]) => unknown;
registerPlugin?: (...plugins: unknown[]) => unknown;
@@ -13,6 +17,67 @@ type IframeWindow = Window & {
MotionPathPlugin?: unknown;
};
+/**
+ * CDN URL for the GSAP MotionPathPlugin. Shared between the one-time preview
+ * bootstrap (ensureMotionPathPluginLoaded) and the soft-reload fallback so the
+ * version is pinned in a single place.
+ */
+const MOTION_PATH_PLUGIN_CDN =
+ "https://cdn.jsdelivr.net/npm/[email protected]/dist/MotionPathPlugin.min.js";
+
+/**
+ * Pre-load + register MotionPathPlugin ONCE in the preview iframe so
+ * `win.MotionPathPlugin` is reliably set before any studio edit. Called from the
+ * preview bootstrap (NLELayout's onIframeLoad) on every iframe load.
+ *
+ * Why: when a user ADDS a motion path to a composition that never used one, the
+ * plugin isn't loaded, so the first soft reload takes the async `<script src>`
+ * load path — the timeline is killed/cleared while the CDN load is pending,
+ * producing a visible flash. Loading it eagerly here means the soft reload runs
+ * synchronously and `needsMotionPath && !win.MotionPathPlugin` never fires for
+ * studio edits.
+ *
+ * Idempotent (no-ops once the plugin is present or already loading) and
+ * defensive: no-ops without gsap/registerPlugin and tolerates a CDN failure
+ * (the soft-reload async fallback in applySoftReload still covers that case).
+ */
+export function ensureMotionPathPluginLoaded(iframe: HTMLIFrameElement | null): void {
+ if (!iframe?.contentWindow || !iframe.contentDocument) return;
+ const win = iframe.contentWindow as IframeWindow;
+ const doc = iframe.contentDocument;
+
+ // Already registered (composition shipped its own plugin, or a prior bootstrap
+ // ran) — register it on gsap to be safe, then bail.
+ if (win.MotionPathPlugin) {
+ try {
+ if (win.gsap?.registerPlugin) win.gsap.registerPlugin(win.MotionPathPlugin);
+ } catch {}
+ return;
+ }
+ if (!win.gsap?.registerPlugin) return;
+ // A load is already in flight for this iframe — don't queue a second script.
+ if (win.__hfMotionPathPluginLoading) return;
+
+ try {
+ win.__hfMotionPathPluginLoading = true;
+ const pluginScript = doc.createElement("script");
+ pluginScript.src = MOTION_PATH_PLUGIN_CDN;
+ const finalize = () => {
+ win.__hfMotionPathPluginLoading = false;
+ try {
+ if (win.MotionPathPlugin && win.gsap?.registerPlugin) {
+ win.gsap.registerPlugin(win.MotionPathPlugin);
+ }
+ } catch {}
+ };
+ pluginScript.onload = finalize;
+ pluginScript.onerror = finalize;
+ doc.head.appendChild(pluginScript);
+ } catch {
+ win.__hfMotionPathPluginLoading = false;
+ }
+}
+
function isGsapScript(text: string): boolean {
return (
text.includes("gsap.timeline") ||
@@ -44,14 +109,44 @@ export function extractGsapScriptText(html: string): string | null {
return scripts[0].textContent || null;
}
-/** Check that the new script repopulated __timelines with at least one entry. */
-function verifyTimelinesPopulated(win: IframeWindow): boolean {
- const tlKeys = win.__timelines
- ? Object.keys(win.__timelines).filter((k) => k !== "__proxied")
- : [];
- return tlKeys.length > 0;
+/**
+ * Confirm the re-run repopulated the timeline(s) this script owns. We check the
+ * EXPECTED keys (the ones the script re-registers), not merely "any key": a
+ * scoped soft reload only re-runs ONE composition, so the right success signal is
+ * "my target keys are back", not "the global map is non-empty". Checking the
+ * exact keys avoids the transient false where the global map momentarily looks
+ * empty right after the re-run — the spurious trigger of the full-remount fallback.
+ */
+function verifyTimelinesPopulated(win: IframeWindow, targetKeys: string[]): boolean {
+ const timelines = win.__timelines;
+ if (!timelines) return false;
+ if (targetKeys.length > 0) {
+ return targetKeys.every((key) => timelines[key] != null);
+ }
+ return Object.keys(timelines).filter((k) => k !== "__proxied").length > 0;
}
+/**
+ * Outcome of a soft-reload attempt. Callers must distinguish PERMANENT failures
+ * (the preview genuinely can't be soft-updated — escalate to a full reload) from
+ * the TRANSIENT post-run empty-timeline window (the live `gsap.set` already shows
+ * the correct value — do NOT escalate; a remount would re-flash the WebGL context
+ * and revert subcomposition keyframes):
+ *
+ * - `"applied"` — the script ran (or is deferred to the async plugin
+ * load and WILL run). The preview is/will be correct.
+ * - `"verify-failed"` — TRANSIENT: the re-run happened but `__timelines`
+ * momentarily read empty. Live state is correct → do
+ * NOT escalate. (Was a bare `false` before.)
+ * - `"cannot-soft-reload"` — PERMANENT/STRUCTURAL: no gsap runtime, no rebind
+ * hook, no scopable target key, or no script element
+ * to replace. The preview is stale/broken → escalate.
+ *
+ * The async MotionPath-plugin load failure is still surfaced via
+ * `onAsyncFailure` (it fires after this returned `"applied"` optimistically).
+ */
+export type SoftReloadResult = "applied" | "verify-failed" | "cannot-soft-reload";
+
/**
* Replace the GSAP script in the live iframe without reloading. This preserves
* the WebGL context and shader transition cache.
@@ -60,22 +155,60 @@ function verifyTimelinesPopulated(win: IframeWindow): boolean {
* elements (sub-compositions) are not visible to `querySelectorAll` and will
* fall back to a full iframe reload.
*
- * Returns false (triggering a full reload fallback) when:
+ * Returns `"cannot-soft-reload"` (caller should full-reload) when:
* - The iframe or GSAP runtime isn't available
- * - Multiple GSAP scripts are found (ambiguous which to replace)
- * - No matching GSAP script element exists in the live DOM
+ * - The rebind hook isn't installed
+ * - The script registers no scopable `__timelines` key
+ * - No GSAP script element exists in the live DOM
+ * - The synchronous re-run threw
+ *
+ * Returns `"verify-failed"` when the re-run executed but the target timeline
+ * keys read empty in the transient post-run window (live state is still correct).
+ *
+ * `onAsyncFailure` is invoked when the soft reload was deferred to load the
+ * MotionPath plugin (so this returned `"applied"` optimistically) but the plugin
+ * `<script>` then failed to load — the iframe is left without the plugin and the
+ * caller should perform a full reload to recover. It never fires on the
+ * synchronous paths.
*/
-export function applySoftReload(iframe: HTMLIFrameElement | null, scriptText: string): boolean {
- if (!iframe || !scriptText) return false;
+export function applySoftReload(
+ iframe: HTMLIFrameElement | null,
+ scriptText: string,
+ onAsyncFailure?: () => void,
+): SoftReloadResult {
+ if (!iframe || !scriptText) return "cannot-soft-reload";
const win = iframe.contentWindow as IframeWindow | null;
const doc = iframe.contentDocument;
- if (!win || !doc) return false;
- if (!win.gsap || !win.__hfForceTimelineRebind) return false;
+ if (!win || !doc) return "cannot-soft-reload";
+ if (!win.gsap || !win.__hfForceTimelineRebind) return "cannot-soft-reload";
+ // Which composition(s) does this script rebuild? A soft reload re-runs ONE
+ // composition's GSAP script, which re-registers its own window.__timelines[key].
+ // In a multi-composition preview (top-level + inlined subcompositions) each
+ // composition owns a separate timeline keyed by its id, and they're all children
+ // of the global timeline — so tearing down ALL of them (or the global timeline's
+ // children) and re-running a single script wipes every OTHER composition,
+ // reverting its edits. Scope the teardown to the keys THIS script re-registers.
+ const targetKeys = [...scriptText.matchAll(/__timelines\s*\[\s*["'`]([^"'`]+)["'`]\s*\]/g)]
+ .map((m) => m[1]!)
+ .filter((key) => key !== "__proxied");
+ if (targetKeys.length === 0) return "cannot-soft-reload"; // can't scope safely → full reload
const gsapScripts = findGsapScriptElements(doc);
- if (gsapScripts.length !== 1) return false;
- const oldScriptEl = gsapScripts[0]!;
+ if (gsapScripts.length === 0) return "cannot-soft-reload";
+ // Remove only the stale script element(s) that registered a target key; one we
+ // can't match in the doc is left alone (re-running appends a fresh element).
+ const staleScripts = gsapScripts.filter((script) =>
+ targetKeys.some((key) => {
+ const text = script.textContent || "";
+ return text.includes(`__timelines["${key}"]`) || text.includes(`__timelines['${key}']`);
+ }),
+ );
+ // Multiple GSAP scripts exist but none registers a key this script owns — we
+ // can't identify which element to replace (ambiguous, matching
+ // extractGsapScriptText's single-script requirement). Escalate to a full reload
+ // rather than killing the target timeline and appending an orphan script.
+ if (gsapScripts.length > 1 && staleScripts.length === 0) return "cannot-soft-reload";
const currentTime = win.__player?.getTime?.() ?? 0;
@@ -91,47 +224,55 @@ export function applySoftReload(iframe: HTMLIFrameElement | null, scriptText: st
const timelines = win.__timelines;
const allTargets: Element[] = [];
+ // Kill ONLY the target composition's timeline(s) — leaving every other
+ // composition's timeline (and its children on the global timeline) intact.
if (timelines) {
- for (const key of Object.keys(timelines)) {
- if (key === "__proxied") continue;
- try {
- const tl = timelines[key] as {
- kill?: () => void;
- getChildren?: (deep: boolean) => Array<{ targets?: () => Element[] }>;
- };
- if (tl?.getChildren) {
- try {
- for (const child of tl.getChildren(true)) {
- if (typeof child.targets === "function") {
- for (const t of child.targets()) allTargets.push(t);
- }
+ for (const key of targetKeys) {
+ const tl = timelines[key] as
+ | {
+ kill?: () => void;
+ getChildren?: (deep: boolean) => Array<{ targets?: () => Element[] }>;
+ }
+ | undefined;
+ if (!tl) continue;
+ if (tl.getChildren) {
+ try {
+ for (const child of tl.getChildren(true)) {
+ if (typeof child.targets === "function") {
+ for (const t of child.targets()) allTargets.push(t);
}
- } catch {}
- }
- tl?.kill?.();
+ }
+ } catch {}
+ }
+ try {
+ tl.kill?.();
} catch {}
delete timelines[key];
}
}
- // Kill bare gsap.to/from tweens not registered on __timelines
- if (win.gsap?.globalTimeline?.getChildren) {
- try {
- for (const child of win.gsap.globalTimeline.getChildren(false)) {
- child.kill?.();
- }
- } catch {}
- }
-
- // Clear residual inline transforms left by killed tweens so from() tweens
- // don't read stale end values from the DOM on re-execution
+ // Reset GSAP's internal transform cache so from() tweens don't read stale
+ // end values. `clearProps: "all"` is needed to flush the cache, but it also
+ // nukes the element's CSS base (position, width, height, etc.) from the
+ // HTML `style=""` attribute. Save → clear → restore → strip `transform`.
if (allTargets.length > 0 && win.gsap?.set) {
+ const saved: Array<[Element, string]> = [];
+ for (const el of allTargets) {
+ const s = (el as HTMLElement).style;
+ if (s?.cssText != null) saved.push([el, s.cssText]);
+ }
try {
win.gsap.set(allTargets, { clearProps: "all" });
} catch {}
+ for (const [el, css] of saved) {
+ const s = (el as HTMLElement).style;
+ if (!s) continue;
+ s.cssText = css;
+ s.removeProperty("transform");
+ }
}
- oldScriptEl.remove();
+ for (const script of staleScripts) script.remove();
const executeScript = () => {
if (win.MotionPathPlugin && win.gsap?.registerPlugin) {
@@ -148,10 +289,41 @@ export function applySoftReload(iframe: HTMLIFrameElement | null, scriptText: st
const needsMotionPath = /motionPath\s*[:{]/.test(scriptText);
if (needsMotionPath && !win.MotionPathPlugin && win.gsap) {
deferredToAsync = true;
+ // A prior soft reload is already fetching the plugin — don't queue a second
+ // <script> (it re-flashes the iframe). Defer THIS script's execution until
+ // the in-flight load settles via a one-shot poll. The bootstrap guard is
+ // the single source of truth for "plugin fetch in progress".
+ if (win.__hfMotionPathPluginLoading) {
+ const started = Date.now();
+ const poll = win.setInterval(() => {
+ if (win.MotionPathPlugin) {
+ win.clearInterval(poll);
+ executeScript();
+ } else if (!win.__hfMotionPathPluginLoading || Date.now() - started > 10000) {
+ // The in-flight load finished without registering the plugin (errored)
+ // or we timed out — recover with a full reload instead of running a
+ // script that references a missing plugin.
+ win.clearInterval(poll);
+ onAsyncFailure?.();
+ }
+ }, 50);
+ return;
+ }
+ win.__hfMotionPathPluginLoading = true;
const pluginScript = doc.createElement("script");
- pluginScript.src = "https://cdn.jsdelivr.net/npm/[email protected]/dist/MotionPathPlugin.min.js";
- pluginScript.onload = () => executeScript();
- pluginScript.onerror = () => executeScript();
+ pluginScript.src = MOTION_PATH_PLUGIN_CDN;
+ pluginScript.onload = () => {
+ win.__hfMotionPathPluginLoading = false;
+ executeScript();
+ };
+ pluginScript.onerror = () => {
+ // The plugin failed to load. Running executeScript() now would leave the
+ // iframe with a motionPath tween referencing a missing plugin while the
+ // caller already thinks the soft reload succeeded. Signal failure so the
+ // caller can full-reload (which fetches the plugin fresh) instead.
+ win.__hfMotionPathPluginLoading = false;
+ onAsyncFailure?.();
+ };
doc.head.appendChild(pluginScript);
return;
}
@@ -166,10 +338,16 @@ export function applySoftReload(iframe: HTMLIFrameElement | null, scriptText: st
doReload();
}
// When MotionPath needs async loading, the script hasn't executed yet —
- // skip the __timelines check and return true optimistically.
- if (deferredToAsync) return true;
- return verifyTimelinesPopulated(win);
+ // skip the __timelines check and report success optimistically (the script
+ // WILL run on plugin load; onAsyncFailure covers the CDN-error case).
+ if (deferredToAsync) return "applied";
+ // The re-run executed. If the target keys read back, we're done; otherwise
+ // it's the TRANSIENT empty-timeline window (live state is correct) — surfaced
+ // as "verify-failed" so callers know NOT to escalate.
+ return verifyTimelinesPopulated(win, targetKeys) ? "applied" : "verify-failed";
} catch {
- return false;
+ // The synchronous re-run threw — the preview is now genuinely broken (target
+ // timeline killed, script not re-registered). Escalate to a full reload.
+ return "cannot-soft-reload";
}
}
diff --git a/packages/studio/src/utils/studioHelpers.test.ts b/packages/studio/src/utils/studioHelpers.test.ts
index 406585d6a4..c50abc548d 100644
--- a/packages/studio/src/utils/studioHelpers.test.ts
+++ b/packages/studio/src/utils/studioHelpers.test.ts
@@ -1,5 +1,11 @@
+// @vitest-environment happy-dom
+
import { describe, expect, it } from "vitest";
-import { findMatchingTimelineElementId, resolveTimelineSelectionSeekTime } from "./studioHelpers";
+import {
+ findMatchingTimelineElementId,
+ findTimelineIdByAncestor,
+ resolveTimelineSelectionSeekTime,
+} from "./studioHelpers";
describe("resolveTimelineSelectionSeekTime", () => {
it("keeps the current time when it is already inside the clip range", () => {
@@ -42,3 +48,27 @@ describe("findMatchingTimelineElementId", () => {
expect(findMatchingTimelineElementId({ id: "ghost", sourceFile: "index.html" }, [])).toBe(null);
});
});
+
+describe("findTimelineIdByAncestor", () => {
+ const el = (over: Record<string, unknown>) =>
+ ({ id: "x", start: 0, duration: 1, track: 0, tag: "div", ...over }) as never;
+
+ it("resolves a static descendant (.num) to its nearest clip ancestor", () => {
+ // #stat1 (a clip) > .num (selected, not a clip)
+ const stat1 = document.createElement("div");
+ stat1.id = "stat1";
+ const num = document.createElement("div");
+ num.className = "num";
+ stat1.appendChild(num);
+
+ const els = [el({ id: "stat1", domId: "stat1", key: "index.html#stat1" })];
+ expect(findTimelineIdByAncestor(num, els, "index.html")).toBe("index.html#stat1");
+ });
+
+ it("returns null when no ancestor is a clip", () => {
+ const wrap = document.createElement("div");
+ const child = document.createElement("span");
+ wrap.appendChild(child);
+ expect(findTimelineIdByAncestor(child, [], "index.html")).toBe(null);
+ });
+});
diff --git a/packages/studio/src/utils/studioHelpers.ts b/packages/studio/src/utils/studioHelpers.ts
index f927896673..fc5459bc2a 100644
--- a/packages/studio/src/utils/studioHelpers.ts
+++ b/packages/studio/src/utils/studioHelpers.ts
@@ -185,6 +185,30 @@ export function findMatchingTimelineElementId(
return null;
}
+/**
+ * A selected DOM node may be a static descendant of a clip (e.g. the `.num` text
+ * inside a `#stat1` card) — not a timeline element itself. Walk up to the nearest
+ * ancestor that IS a clip so the timeline still selects + inline-expands around it.
+ */
+export function findTimelineIdByAncestor(
+ element: Element | null | undefined,
+ elements: TimelineElement[],
+ sourceFile: string,
+): string | null {
+ let ancestor = element?.parentElement ?? null;
+ while (ancestor) {
+ const id = ancestor.id;
+ if (id) {
+ const match = elements.find(
+ (el) => el.domId === id && (el.sourceFile ?? "index.html") === sourceFile,
+ );
+ if (match) return match.key ?? match.id;
+ }
+ ancestor = ancestor.parentElement;
+ }
+ return null;
+}
+
export function resolveTimelineSelectionSeekTime(
currentTime: number,
element: Pick<TimelineElement, "start" | "duration"> | null | undefined,
diff --git a/packages/studio/src/utils/studioPreviewHelpers.test.ts b/packages/studio/src/utils/studioPreviewHelpers.test.ts
index fce01d7b36..c673142e6a 100644
--- a/packages/studio/src/utils/studioPreviewHelpers.test.ts
+++ b/packages/studio/src/utils/studioPreviewHelpers.test.ts
@@ -1,5 +1,30 @@
import { describe, expect, it, vi } from "vitest";
-import { pauseStudioPreviewPlayback } from "./studioPreviewHelpers";
+import { coversComposition, pauseStudioPreviewPlayback } from "./studioPreviewHelpers";
+
+describe("coversComposition (full-bleed canvas-pick exclusion)", () => {
+ const viewport = { width: 1920, height: 1080 };
+
+ it("treats a full-bleed scene wrapper as covering the composition", () => {
+ expect(coversComposition({ width: 1920, height: 1080 }, viewport)).toBe(true);
+ expect(coversComposition({ width: 1900, height: 1040 }, viewport)).toBe(true); // ~99%/96%
+ });
+
+ it("does NOT exclude inner content (a stat card, a heading)", () => {
+ expect(coversComposition({ width: 320, height: 180 }, viewport)).toBe(false);
+ expect(coversComposition({ width: 1900, height: 200 }, viewport)).toBe(false); // wide but short
+ expect(coversComposition({ width: 200, height: 1040 }, viewport)).toBe(false); // tall but narrow
+ });
+
+ it("needs BOTH axes near full-bleed (>=95%)", () => {
+ expect(coversComposition({ width: 1800, height: 1080 }, viewport)).toBe(false); // 93.75% wide
+ expect(coversComposition({ width: 1920, height: 1000 }, viewport)).toBe(false); // 92.6% tall
+ });
+
+ it("guards against a degenerate viewport", () => {
+ expect(coversComposition({ width: 100, height: 100 }, { width: 0, height: 0 })).toBe(false);
+ expect(coversComposition({ width: 100, height: 100 }, { width: 1, height: 1 })).toBe(false);
+ });
+});
describe("pauseStudioPreviewPlayback", () => {
it("pauses through __player without pausing sibling timelines directly", () => {
diff --git a/packages/studio/src/utils/studioPreviewHelpers.ts b/packages/studio/src/utils/studioPreviewHelpers.ts
index 6bb8cb8a5e..2ec9911bc5 100644
--- a/packages/studio/src/utils/studioPreviewHelpers.ts
+++ b/packages/studio/src/utils/studioPreviewHelpers.ts
@@ -1,5 +1,4 @@
import type { DomEditViewport } from "../components/editor/domEditing";
-import { resolveVisualDomEditSelectionTarget } from "../components/editor/domEditing";
import {
getDomLayerPatchTarget,
isElementComputedVisible,
@@ -13,6 +12,29 @@ interface PreviewLocalPointer {
viewport: DomEditViewport;
}
+// An element is "full-bleed" when its box spans nearly the whole composition on
+// BOTH axes. Such elements (scene wrappers, backdrops) are excluded from canvas
+// click-picking so a click lands on inner content — or deselects on empty area —
+// instead of grabbing the giant container. The Layers panel still selects them.
+// ponytail: pure size heuristic; tighten the ratio if decorative full-bleed art
+// should remain canvas-selectable.
+const FULL_BLEED_RATIO = 0.95;
+
+export function coversComposition(
+ elRect: { width: number; height: number },
+ viewport: DomEditViewport,
+): boolean {
+ if (viewport.width <= 1 || viewport.height <= 1) return false;
+ return (
+ elRect.width / viewport.width >= FULL_BLEED_RATIO &&
+ elRect.height / viewport.height >= FULL_BLEED_RATIO
+ );
+}
+
+function isFullBleedTarget(el: HTMLElement, viewport: DomEditViewport): boolean {
+ return coversComposition(el.getBoundingClientRect(), viewport);
+}
+
function resolvePreviewLocalPointer(
iframe: HTMLIFrameElement,
doc: Document,
@@ -82,18 +104,19 @@ export function getPreviewTargetFromPointer(
const overrideStyle = forcePointerEventsAuto(doc);
try {
if (typeof doc.elementsFromPoint === "function") {
- const visualTarget = resolveVisualDomEditSelectionTarget(
+ const candidates = resolveAllVisualDomEditTargets(
doc.elementsFromPoint(localPointer.x, localPointer.y),
- {
- activeCompositionPath,
- },
+ { activeCompositionPath },
);
+ const visualTarget =
+ candidates.find((el) => !isFullBleedTarget(el, localPointer.viewport)) ?? null;
if (visualTarget) return visualTarget;
}
const fallback = getEventTargetElement(doc.elementFromPoint(localPointer.x, localPointer.y));
if (!fallback || !getDomLayerPatchTarget(fallback, activeCompositionPath)) return null;
if (!isElementComputedVisible(fallback)) return null;
+ if (isFullBleedTarget(fallback, localPointer.viewport)) return null;
return fallback;
} finally {
removePointerEventsOverride(overrideStyle);
@@ -125,11 +148,12 @@ export function getAllPreviewTargetsFromPointer(
if (typeof doc.elementsFromPoint === "function") {
return resolveAllVisualDomEditTargets(doc.elementsFromPoint(localPointer.x, localPointer.y), {
activeCompositionPath,
- });
+ }).filter((el) => !isFullBleedTarget(el, localPointer.viewport));
}
const fallback = getEventTargetElement(doc.elementFromPoint(localPointer.x, localPointer.y));
if (!fallback || !getDomLayerPatchTarget(fallback, activeCompositionPath)) return [];
if (!isElementComputedVisible(fallback)) return [];
+ if (isFullBleedTarget(fallback, localPointer.viewport)) return [];
return [fallback];
} finally {
removePointerEventsOverride(overrideStyle);