Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ it("ends a sealed drag on GSAP's route without its drawn translate, so the saved
await vi.waitFor(() => expect(document.getElementById("box")!.style.translate).toBe(""));
});

// fails without the fix: a sealed drag on GSAP's route left its offset on Studio's stand-in, the next gesture counted
// from it, and a second scale resize of the layer saved its position that far off.
it("leaves no Studio offset on the stand-in once a sealed drag ends on GSAP's route", async () => {
mount(SCRIPT);
const { standIn } = await dragBoxOnSealedFrame(SCRIPT);

expect((standIn as HTMLElement).style.getPropertyValue("--hf-studio-offset-x")).toBe("");
});

// fails without the fix: ending the draft on GSAP's route took back the size a resize had drawn too.
it("keeps the size a sealed resize drew when its anchor ends on GSAP's route", async () => {
mount(SCRIPT);
Expand Down
6 changes: 5 additions & 1 deletion packages/studio/src/components/editor/manualOffsetDrag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface ManualOffsetDragMember {
initialOffset: { x: number; y: number };
/** The move writes the element's own inline `translate`; offsets are then absolute translate px. */
plainTranslate: boolean;
gsapOwnsPosition: boolean;
sizeSavedAsScale?: boolean;
/**
* The element's GSAP x/y at gesture start, captured in JS so a mid-drag
Expand Down Expand Up @@ -356,6 +357,7 @@ export function createManualOffsetDragMember(input: {
const gsapY = (!plainTranslate && win?.gsap?.getProperty?.(input.element, "y")) || 0;
const baseGsap = start?.gsap ? { x: start.gsap.x, y: start.gsap.y } : { x: gsapX, y: gsapY };
const moving = start?.gsap?.moving === true;
const gsapOwnsPosition = !plainTranslate && Boolean(start ?? getOffsetDragGsap(input.element));
if (!plainTranslate) stampGestureBase(input.element, initialOffset, baseGsap, moving);

if (win?.__timelines) {
Expand Down Expand Up @@ -401,6 +403,7 @@ export function createManualOffsetDragMember(input: {
element: input.element,
initialOffset,
plainTranslate,
gsapOwnsPosition,
baseGsap,
...(moving && { moving }),
initialPathOffset,
Expand All @@ -419,6 +422,7 @@ export function createManualOffsetDragMember(input: {
element: input.element,
initialOffset,
plainTranslate,
gsapOwnsPosition,
baseGsap,
...(moving && { moving }),
initialPathOffset,
Expand Down Expand Up @@ -546,7 +550,7 @@ export function endManualOffsetDragMembers(members: ManualOffsetDragMember[]): v
member.element.removeAttribute(DRAG_MOVING_ATTR);
// Do NOT clearProps:"transform" — that nukes the committed GSAP position
// and causes a visual snap-back before the soft reload re-applies it.
const gsapOwnsPosition = !member.plainTranslate && Boolean(getOffsetDragGsap(member.element));
const { gsapOwnsPosition } = member;
// Migration: when GSAP owns the position (the committed value lives in the
// GSAP transform), the legacy `--hf-studio-offset` CSS channel is obsolete.
// Clear it on the LIVE element — otherwise the leftover `translate:
Expand Down
13 changes: 8 additions & 5 deletions packages/studio/src/hooks/gsapFrameSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ function refreshGsapSnapshot(iframe: HTMLIFrameElement): Promise<void> {
return pending;
}

export function rereadGsapSnapshot(iframe: HTMLIFrameElement): void {
const client = previewEditingClient(iframe);
held.delete(client);
asking.delete(client);
void refreshGsapSnapshot(iframe);
}

/** Reads the frame again unless the held read is at the paused playhead; false if an opaque frame holds none. */
export async function settleGsapSnapshot(iframe: HTMLIFrameElement | null): Promise<boolean> {
if (!iframe || !previewFrameOpaque(iframe)) return true;
Expand All @@ -91,11 +98,7 @@ async function read(iframe: HTMLIFrameElement, client: EditingPortClient): Promi
watchPlayhead();
if (!watched.has(client)) {
watched.add(client);
const stopOnUndoRerun = onPreviewRestoredInPlace(iframe, () => {
held.delete(client);
asking.delete(client);
void refreshGsapSnapshot(iframe);
});
const stopOnUndoRerun = onPreviewRestoredInPlace(iframe, () => rereadGsapSnapshot(iframe));
const stop = onPreviewContentReplaced(iframe, () => {
stop();
stopOnUndoRerun();
Expand Down
7 changes: 6 additions & 1 deletion packages/studio/src/hooks/gsapResizeDropPoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { DomEditSelection } from "../components/editor/domEditingTypes";
import { usePlayerStore } from "../player/store/playerStore";
import { tryGsapResizeIntercept } from "./gsapResizeIntercept";
import { drawPreviewDraft, previewDraftsSent } from "../components/editor/previewDrafts";
import { applyStudioBoxSize, readStudioBoxSize } from "../components/editor/manualEdits";
import { measurePreviewGestureStart } from "../components/editor/previewGestureStartRead";
import {
findPreviewElementForSelection,
Expand Down Expand Up @@ -439,7 +440,9 @@ it("lands on the drop point when undo paints the box back while its animations l
expect(settled.w).toBeCloseTo(dropPoint.w, 0);
});

it("lands on the drop point in a sealed preview, measured by the frame, with no second move", async () => {
// fails without the fix: a sealed scale drop kept the dropped size on Studio's stand-in, so the next resize of the
// layer started from that size on top of the saved scale and jumped, saving the jump.
it("lands on the drop point in a sealed preview, measured by the frame, and leaves no box size behind", async () => {
const box = { w: 630, h: 252 };
const base = { x: 489, y: 195 };
const drop = { w: 900, h: 360, x: 300, y: 100 };
Expand Down Expand Up @@ -487,6 +490,7 @@ it("lands on the drop point in a sealed preview, measured by the frame, with no
});
await previewDraftsSent(iframe);
const dropPoint = el.getBoundingClientRect();
applyStudioBoxSize(selection.element, { width: drop.w, height: drop.h });
const animations = [scaleTween(false), positionAnimation("static-set", base)!];
const commitMutation = vi.fn();

Expand Down Expand Up @@ -517,4 +521,5 @@ it("lands on the drop point in a sealed preview, measured by the frame, with no
expect(shown.x).toBeCloseTo(dropPoint.x, 0);
expect(shown.y).toBeCloseTo(dropPoint.y, 0);
}
expect(readStudioBoxSize(selection.element)).toEqual({ width: 0, height: 0 });
});
2 changes: 2 additions & 0 deletions packages/studio/src/hooks/gsapResizeIntercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ export async function tryGsapResizeIntercept(
return false;
}
const dropPoint = scaleDraftDropPoint;
const nextResizeReadsItsSizeFrom = draftEl ?? selection.element;
draw(() => clearStudioBoxSize(nextResizeReadsItsSizeFrom));
// Measure from the pre-gesture position: the scale route never saves the draft's translation, and
// the position write composes the residual onto that same base.
const preGesture = () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/studio/src/hooks/scaleDropPoint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { clearStudioBoxSize } from "../components/editor/manualEdits";
import { setElementGsapPosition, setElementGsapScale } from "../utils/elementGsap";
import { roundTo3 } from "../utils/rounding";
import { logResize } from "../utils/resizeDebug";
Expand Down Expand Up @@ -54,7 +53,6 @@ export function correctOnElement(
dropPoint: Point,
{ base, gsapPos }: { base: Point; gsapPos: Point },
): ScaleDrop | "settled" | null {
clearStudioBoxSize(draftEl);
// Draw the committed scale before measuring: on a first resize the timeline has not re-seeked yet,
// so the box would read at its natural size and the correction would be skipped.
if (committedScale) setElementGsapScale(draftEl, committedScale.x, committedScale.y);
Expand Down
7 changes: 3 additions & 4 deletions packages/studio/src/hooks/useGsapScriptCommits.sealed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,9 @@ it("re-runs the saved script in place when the frame refuses a patch of a soft-r
);

await vi.waitFor(() =>
expect(previewRequest.mock.calls.map(([, kind]) => kind)).toEqual([
"patch-tweens",
"soft-reload",
]),
expect(
previewRequest.mock.calls.map(([, kind]) => kind).filter((kind) => kind !== "gsap-snapshot"),
).toEqual(["patch-tweens", "soft-reload"]),
);
expect(reloadPreview).not.toHaveBeenCalled();
});
44 changes: 44 additions & 0 deletions packages/studio/src/utils/gsapSoftReload.sealed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ vi.mock("../player/lib/previewChannel", async (importOriginal) => ({
}));

import { applySoftReload } from "./gsapSoftReload";
import { frameGsap, frameQuery, settleGsapSnapshot } from "../hooks/gsapFrameSnapshot";
import { usePlayerStore } from "../player/store/playerStore";

const iframe = document.createElement("iframe");
const SAVED = `window.__timelines["main"] = gsap.timeline(); gsap.set("#b", { x: 5 });`;
Expand Down Expand Up @@ -97,3 +99,45 @@ it("asks for a full reload without sending anything when the file before the wri
expect(result).toBe("cannot-soft-reload");
expect(previewRequest).not.toHaveBeenCalled();
});

// fails without the fix: Studio's copy of a sealed frame's GSAP values was read again only on a seek, a reload or an
// undo, so after a save's in-place rerun a second resize read the scale from before the first and saved it wrong.
it("reads the frame's GSAP values again once a saved script reruns in place", async () => {
const shown = (scaleX: number) => ({
time: 2,
defaultEase: null,
timelines: [{ id: "main", time: 2, defaultEase: null, tweens: [] }],
targets: [{ handle: "0123456789abcdef:1", id: "b", className: "", values: { scaleX } }],
});
let scaleX = 1.125;
previewRequest.mockImplementation(async (_frame: unknown, kind: string) =>
kind === "gsap-snapshot" ? shown(scaleX) : { result: "applied" },
);
usePlayerStore.setState({ currentTime: 2, isPlaying: false });
await settleGsapSnapshot(iframe);

scaleX = 1.542;
applySoftReload(iframe, SAVED, {
authoredHtml: file(SAVED),
previousHtml: PREVIOUS,
currentTimeOverride: 2,
});
await settle();
await settleGsapSnapshot(iframe);

expect(frameGsap(iframe)?.getProperty(frameQuery(iframe, "#b")!, "scaleX")).toBe(1.542);
const reads = () =>
previewRequest.mock.calls.filter(([, kind]) => kind === "gsap-snapshot").length;
expect(reads()).toBe(2);

previewRequest.mockImplementation(async (_frame: unknown, kind: string) =>
kind === "gsap-snapshot" ? shown(scaleX) : { result: "cannot-soft-reload" },
);
applySoftReload(iframe, SAVED, {
authoredHtml: file(SAVED),
previousHtml: PREVIOUS,
currentTimeOverride: 2,
});
await settle();
expect(reads()).toBe(2);
});
6 changes: 6 additions & 0 deletions packages/studio/src/utils/gsapSoftReload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { authoredStyles, findAuthoredElement, savedTiming } from "./authoredSour
import { fileDocs, standaloneSets } from "./softReloadTargets";
import type { EditingParams } from "@hyperframes/core/runtime/editing-port";
import { PREVIEW_REJECTED, PREVIEW_STALE, previewFrameOpaque } from "../player/lib/previewChannel";
import { rereadGsapSnapshot } from "../hooks/gsapFrameSnapshot";
import {
SAVED_STATE_DEADLINE_MS,
sendPreviewWrite,
Expand Down Expand Up @@ -202,6 +203,10 @@ export function sealedRerun(
}

// Everything sent comes from the files Studio saved, never from the frame; the reply only says whether it applied.
function rereadOnceRerun(iframe: HTMLIFrameElement, result: SoftReloadResult): void {
if (result !== "cannot-soft-reload" && result !== "superseded") rereadGsapSnapshot(iframe);
}

function softReloadSealed(
iframe: HTMLIFrameElement,
scriptText: string,
Expand Down Expand Up @@ -236,6 +241,7 @@ function softReloadSealed(
answer === PREVIEW_REJECTED || answer === PREVIEW_STALE
? "cannot-soft-reload"
: sealedSoftReloadResult(answer);
rereadOnceRerun(iframe, result);
if (!ticketNeedsRecovery(ticket)) return;
if (result === "cannot-soft-reload") onAsyncFailure?.();
else if (result !== "superseded") onShownInPlace?.();
Expand Down
2 changes: 1 addition & 1 deletion packages/studio/src/utils/gsapUndoRestore.sealed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const settled = () => new Promise((resolve) => setTimeout(resolve, 0));
const sentToFrame = () =>
channel.request.mock.calls
.map(([, request, params]) => [request, params] as const)
.filter(([request]) => request !== "mark-scenes-stale");
.filter(([request]) => request !== "mark-scenes-stale" && request !== "gsap-snapshot");

beforeEach(() => {
channel.request.mockReset();
Expand Down
11 changes: 11 additions & 0 deletions packages/studio/tests/e2e/edit-accuracy/drags.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const GSAP_SEQUENCES = {
resizedrag: [{ do: "drag", gesture: "resize" }, MOVE],
};

/** On a GSAP-scaled box: a second resize must start from the box the first one saved. */
const SCALE_TWICE = [
{ do: "drag", gesture: "resize" },
{ do: "drag", gesture: "resize" },
];

/** A box a timeline set holds: a second resize and drag after the first pair must save too. */
const HELD_TWICE = [
{ do: "drag", gesture: "resize" },
Expand Down Expand Up @@ -125,6 +131,10 @@ export function dragCases() {
settle: true,
})),
);
const scaleTwice = ["root", "nested"].map((nesting) => ({
...row("seqscaletwice", pxRoot({ nesting }), SCALE_TWICE, "scale"),
settle: true,
}));
// Rotate on the common centring idiom, a transform rather than the translate property.
const centred = [0, 30].flatMap((rotation) =>
["root", "nested"].map((nesting) => ({
Expand All @@ -138,6 +148,7 @@ export function dragCases() {
...sequences,
...plainToKeys,
...fastKeys,
...scaleTwice,
...heldTwice,
...gsapSequences,
...texts,
Expand Down
8 changes: 4 additions & 4 deletions packages/studio/tests/e2e/edit-accuracy/grid.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ describe("grid fixtures", () => {
});

describe("grid size", () => {
it("holds 747 cases, 271 of them in the pull request slice, each id once", () => {
it("holds 749 cases, 273 of them in the pull request slice, each id once", () => {
const full = buildGrid("full");
expect(full).toHaveLength(747);
expect(new Set(full.map((c) => c.id)).size).toBe(747);
expect(buildGrid("pr")).toHaveLength(271);
expect(full).toHaveLength(749);
expect(new Set(full.map((c) => c.id)).size).toBe(749);
expect(buildGrid("pr")).toHaveLength(273);
});
});

Expand Down
2 changes: 2 additions & 0 deletions packages/studio/tests/e2e/edit-accuracy/timings.json
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,8 @@
"seqrotatenudge-none-pct-r0-root-z100": 32.8,
"seqrotatenudge-none-px-r0-nested-z100": 15.7,
"seqrotatenudge-none-px-r0-root-z100": 22.2,
"seqscaletwice-scale-px-r0-nested-z100": 21,
"seqscaletwice-scale-px-r0-root-z100": 21,
"seqseek-none-center-r0-nested-z100": 19.6,
"seqseek-none-center-r0-root-z100": 30.2,
"seqseek-none-pct-r0-nested-z100": 44.7,
Expand Down
Loading