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
21 changes: 9 additions & 12 deletions packages/studio/src/components/editor/previewGestureStarts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ export function createPreviewGestureStarts(
start: ShownStart,
draw: WaitingPressState["draw"] = null,
targets: () => DomEditSelection[] = () => [],
drawn: () => OverChannel | null = () => null,
): boolean => {
const at = playheadMoment();
const iframe = opts.iframeRef.current;
Expand Down Expand Up @@ -220,16 +219,16 @@ export function createPreviewGestureStarts(
pollNow();
});
};
const channelStep = (): "wait" | "lost" | "run" | "drawn" => {
const channelStep = (): "wait" | "lost" | "run" | "unanswered" => {
const overdue = performance.now() - since > PRESS_WAIT_MAX_MS;
if (overdue) giveUpOnPreviewChange();
if (isPreviewChanging() && !overdue) return "wait";
askShownFrame();
if (overChannel === undefined) return "wait";
if (overChannel === UNANSWERED) return "drawn";
if (overChannel === UNANSWERED) return "unanswered";
return overChannel ? "run" : "lost";
};
const waitStep = (): "wait" | "lost" | "run" | "drawn" => {
const waitStep = (): "wait" | "lost" | "run" | "unanswered" => {
if (press.after && !press.after.ended) return "wait";
if (channel) return channelStep();
const found = countFrame();
Expand All @@ -240,9 +239,8 @@ export function createPreviewGestureStarts(
if (found === 0 || settledFrames > MAX_SETTLED_WAIT_FRAMES) return "lost";
return overdue || shownFrames >= 2 ? "run" : "wait";
};
const startsFrom = (step: "lost" | "run" | "drawn"): OverChannel | undefined | false => {
if (step === "lost") return false;
if (step === "drawn") return drawn() ?? false;
const startsFrom = (step: "lost" | "run" | "unanswered"): OverChannel | undefined | false => {
if (step !== "run") return false;
return typeof overChannel === "object" && overChannel ? overChannel : undefined;
};
const poll = () => {
Expand All @@ -268,10 +266,11 @@ export function createPreviewGestureStarts(
if (press.released) releaseActiveGesture(press.released);
return true;
};
const sayWhyNotStarted = (step: "lost" | "run" | "drawn") => {
const sayWhyNotStarted = (step: "lost" | "run" | "unanswered") => {
const pressed = opts.selectionRef.current;
if (step === "drawn" && channel && pressed)
opts.onBlockedMoveRef.current(pressed, unansweredMoveReason(channel));
if (step !== "unanswered" || !channel || !pressed) return;
press.draw?.(0, 0);
opts.onBlockedMoveRef.current(pressed, unansweredMoveReason(channel));
};
pollFirst(channel, press, poll);
return true;
Expand Down Expand Up @@ -338,7 +337,6 @@ export function createPreviewGestureStarts(
items.map((item) => ({ ...item, rect: shiftedOverlayRect(item.rect, dx, dy) })),
),
() => items.map((item) => item.selection),
() => ({ shown: items.map(({ selection, rect }) => ({ selection, rect })) }),
);
};
const startGesture = (
Expand Down Expand Up @@ -382,7 +380,6 @@ export function createPreviewGestureStarts(
},
draw,
() => (target ? [target] : []),
() => (target && pressedRect ? { shown: [{ selection: target, rect: pressedRect }] } : null),
);
};

Expand Down
45 changes: 15 additions & 30 deletions packages/studio/src/components/editor/previewOverlayRects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,14 @@ describe("a press held across a preview change on an opaque frame", () => {
expect(standInHandle(started.selection.element)).toBe(started.selection.handle);
});

// fails without the fix: a press the frame never answered, with no box drawn to start from, ended with nothing.
it("says the preview didn't answer when it never does and no box was drawn", async () => {
// fails without the fix: a press the frame never answered ended with nothing said; one with a box Studio drew
// started from that box anyway, with no snap targets and no GSAP start to commit from.
it.each([
["no box was drawn", null],
["Studio drew a box for it", { left: 1, top: 2, width: 3, height: 4 } as OverlayRect],
])("refuses a press the frame never answers, saying why: %s", async (_name, overlayRect) => {
const turned = makeSelection("turned", document.getElementById("turned")!);
const { opts } = await pressDuringChange(turned, { iframe: silentFrame() });
const { opts } = await pressDuringChange(turned, { iframe: silentFrame(), overlayRect });
await vi.waitFor(() => expect(opts.onBlockedMoveRef.current).toHaveBeenCalledOnce(), {
timeout: 3000,
});
Expand All @@ -657,38 +661,19 @@ describe("a press held across a preview change on an opaque frame", () => {
);
});

it("starts from the box Studio drew when the frame shown never answers", async () => {
const turned = makeSelection("turned", document.getElementById("turned")!);
const drawn = { left: 1, top: 2, width: 3, height: 4 } as OverlayRect;
await pressDuringChange(turned, { iframe: silentFrame(), overlayRect: drawn });
await vi.waitFor(() => expect(press.start).toHaveBeenCalledOnce(), { timeout: 3000 });
const [, , , started] = press.start.mock.calls[0] as [
unknown,
unknown,
unknown,
{ selection: DomEditSelection; rect: OverlayRect },
];
expect(started).toMatchObject({ selection: turned, rect: drawn });
expect(started.selection.element).toBe(turned.element);
});

// fails without the fix: the waiting box's redraws had already moved the box the drag started from.
it("starts from the box as it was at the press, though the pointer moved while it waited", async () => {
// fails without the fix: the refused press left Studio's box where the pointer had dragged it, off the layer.
it("puts Studio's box back on the layer when it refuses a press the frame never answers", async () => {
const turned = makeSelection("turned", document.getElementById("turned")!);
const drawn = { left: 1, top: 2, width: 3, height: 4 } as OverlayRect;
await pressDuringChange(turned, {
const { opts } = await pressDuringChange(turned, {
iframe: silentFrame(),
overlayRect: drawn,
whileWaiting: (opts) => opts.waitingPressRef.current!.draw!(30, 0),
whileWaiting: (o) => o.waitingPressRef.current!.draw!(30, 0),
});
await vi.waitFor(() => expect(press.start).toHaveBeenCalledOnce(), { timeout: 3000 });
const [, , , started] = press.start.mock.calls[0] as [
unknown,
unknown,
unknown,
{ rect: OverlayRect },
];
expect(started.rect).toEqual(drawn);
await vi.waitFor(() => expect(opts.onBlockedMoveRef.current).toHaveBeenCalledOnce(), {
timeout: 3000,
});
expect(opts.overlayRectRef.current).toEqual(drawn);
});

it("asks the frame shown again when a swap comes after its first ask", async () => {
Expand Down
Loading