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
55 changes: 55 additions & 0 deletions packages/studio/src/hooks/gsapResizeIntercept.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ import type { CommitMutation } from "./gsapScriptCommitTypes";
import { computeCurrentPercentage } from "./gsapDragCommit";
import { commitSizeAtPlayhead, tryGsapResizeIntercept } from "./gsapResizeIntercept";
import { liveTween, previewWith, tween } from "./gsapParsedTween.test-helpers";
import { measurePreviewGestureStart } from "../components/editor/previewGestureStartRead";

vi.mock("../utils/studioTelemetry", () => ({ trackStudioEvent: vi.fn() }));
vi.mock("../player/lib/previewElements", async (original) => ({
...(await original<object>()),
previewGestureStart: async () => ({
elements: [{ layout: { width: 260, height: 160 }, move: null, gsap: null }],
snap: [],
}),
}));

afterEach(() => {
vi.clearAllMocks();
Expand Down Expand Up @@ -744,3 +752,50 @@ it.each([
el.remove();
document.querySelector("#__hf_color_grading_clip")?.remove();
});

it.each([
["the draft marked the element", false],
["the element is a sealed frame's stand-in, measured at the press", true],
])("a size written at the playhead backfills the box's other side when %s", async (_, sealed) => {
const selection = titleSelection();
const el = selection.element;
if (sealed) {
selection.handle = "0123456789abcdef:1";
await measurePreviewGestureStart(
document.createElement("iframe"),
document.createElement("div"),
[selection.handle],
);
} else {
el.setAttribute("data-hf-studio-original-width", "260");
el.setAttribute("data-hf-studio-original-height", "160");
}
const slide = tween({
id: "#title-to-0",
targetSelector: "#title",
propertyGroup: undefined,
method: "to",
properties: { width: 320 },
resolvedStart: 0,
duration: 2,
ease: "none",
});
usePlayerStore.setState({ currentTime: 1 });
const commitMutation = vi.fn();
const live = liveTween(el, { start: 0, duration: 2, vars: slide.properties });

const outcome = await commitSizeAtPlayhead(
selection,
slide,
{ width: 300, height: 200 },
previewWith(el, [live]),
undefined,
{ commitMutation },
);

expect(outcome.status).toBe("persisted");
const keyframes = commitMutation.mock.calls[0]![1].keyframes;
expect(
keyframes.map((kf: { properties: Record<string, number> }) => kf.properties.height),
).toEqual(keyframes.map((kf: { percentage: number }) => (kf.percentage === 50 ? 200 : 160)));
});
36 changes: 23 additions & 13 deletions packages/studio/src/hooks/gsapResizeIntercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,24 @@ function originalBoxSize(
return Number.isFinite(inline) && inline > 0 ? inline : null;
}

/** The box's width and height before the draft, for a size tween that animates only one. */
function preGestureBoxSize(el: HTMLElement): Record<string, number> {
const width = originalBoxSize(el, STUDIO_ORIGINAL_BOX_WIDTH_ATTR, "width");
const height = originalBoxSize(el, STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, "height");
return { ...(width != null && { width }), ...(height != null && { height }) };
const BOX_SIDES = [
["width", STUDIO_ORIGINAL_BOX_WIDTH_ATTR],
["height", STUDIO_ORIGINAL_BOX_HEIGHT_ATTR],
] as const;

/** The box's width and height before the draft. A sealed frame's draft marks the frame's element,
* not the stand-in, so there it is the box the press measured. */
function preGestureBoxSize(
selection: DomEditSelection,
el: HTMLElement | null = selection.element,
): Record<string, number> {
const started = selection.handle ? gestureStartLayout(selection.handle) : null;
const box: Record<string, number> = {};
for (const [side, attr] of BOX_SIDES) {
const value = originalBoxSize(el, attr, side) ?? (started?.[side] || null);
if (value != null) box[side] = value;
}
return box;
}

/** A size write at the playhead. When the same tween animates position, the resize's anchor move
Expand Down Expand Up @@ -112,7 +125,7 @@ export async function commitSizeAtPlayhead(
{
label: "Resize",
backfill: {
...preGestureBoxSize(selection.element),
...preGestureBoxSize(selection),
...(anchor && { x: anchor.baseGsapX, y: anchor.baseGsapY }),
},
...(anchor && { beforeReload: () => restoreDragOffset(selection.element, callbacks.stamp) }),
Expand Down Expand Up @@ -322,12 +335,9 @@ export async function tryGsapResizeIntercept(
// sent almost every element to the fallback below: a 630px chip scaled by
// 630/200, landing over three times the size it was dropped at, and worse
// on the next drag because the wrong scale then counted as its live one.
// A sealed preview has no element here: its gesture started from the box the frame measured.
const started = selection.handle ? gestureStartLayout(selection.handle) : null;
const cssW =
originalBoxSize(el, STUDIO_ORIGINAL_BOX_WIDTH_ATTR, "width") ?? (started?.width || 200);
const cssH =
originalBoxSize(el, STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, "height") ?? (started?.height || cssW);
const box = preGestureBoxSize(selection, el);
const cssW = box.width ?? 200;
const cssH = box.height ?? cssW;
// `size` is the draft's CSS box; on screen it is multiplied by the element's
// LIVE scale (the draft divides the cursor delta by it — see
// resolveDomEditResizeGesture). The committed keyframe REPLACES that live
Expand Down Expand Up @@ -391,7 +401,7 @@ export async function tryGsapResizeIntercept(
width: roundToLayoutPx(size.width),
height: roundToLayoutPx(size.height),
};
resizeBackfill = preGestureBoxSize(selection.element);
resizeBackfill = preGestureBoxSize(selection);
}
// Finalize a scale-route commit: tear down the gesture's inline width/height
// draft (leaving it applied compounds with the committed scale — the element
Expand Down
Loading