fix(studio): a refused sealed press always puts the box back on the layer (#8735)

* test(studio): a sealed press the frame never answers is refused, box drawn or not

* fix(studio): a sealed press the frame never answers is refused with a reason

* test(studio): a refused sealed press puts Studio's box back on the layer

* fix(studio): a refused sealed press puts Studio's box back on the layer

* test(studio): a refused sealed press puts the box back after another press or a cleared selection

* fix(studio): a refused sealed press puts the box where the press before it left the layer
This commit is contained in:
Miguel Ángel
2026-11-24 19:41:04 +00:00
committed by GitHub
parent 630409ca19
commit b3da2cf58f
3 changed files with 68 additions and 4 deletions
@@ -139,6 +139,8 @@ export interface WaitingPressState {
released: React.PointerEvent<HTMLDivElement> | null;
after: WaitingPressState | null;
ended: boolean;
refused: boolean;
drawBack: () => void;
}
export type FocusableDomEditOverlay = {
@@ -173,6 +173,7 @@ export function createPreviewGestureStarts(
// The press already holds the pointer, which may be up by the time the gesture starts.
const held = { setPointerCapture() {} };
const down = { ...e, currentTarget: held, preventDefault() {}, stopPropagation() {} };
const before = opts.waitingPressRef.current;
const press: WaitingPressState = {
pointerId: e.pointerId,
startX: e.clientX,
@@ -181,8 +182,13 @@ export function createPreviewGestureStarts(
frame: 0,
moved: null,
released: null,
after: opts.waitingPressRef.current,
after: before,
ended: false,
refused: false,
drawBack: () => {
if (before?.refused) before.drawBack();
else draw?.(0, 0);
},
};
opts.waitingPressRef.current = press;
const onSave = !!press.after || isPreviewChanging() || isStudioEditSaving();
@@ -267,10 +273,11 @@ export function createPreviewGestureStarts(
return true;
};
const sayWhyNotStarted = (step: "lost" | "run" | "unanswered") => {
if (step !== "unanswered" || !channel) return;
press.refused = true;
press.drawBack();
const pressed = opts.selectionRef.current;
if (step !== "unanswered" || !channel || !pressed) return;
press.draw?.(0, 0);
opts.onBlockedMoveRef.current(pressed, unansweredMoveReason(channel));
if (pressed) opts.onBlockedMoveRef.current(pressed, unansweredMoveReason(channel));
};
pollFirst(channel, press, poll);
return true;
@@ -676,6 +676,61 @@ describe("a press held across a preview change on an opaque frame", () => {
expect(opts.overlayRectRef.current).toEqual(drawn);
});
// fails without the fix: the second press measured its box from the first drag; refused, it went back 30 px off.
it("puts the box back on the layer when two presses in a row go unanswered", async () => {
const turned = makeSelection("turned", document.getElementById("turned")!);
const drawn = { left: 1, top: 2, width: 3, height: 4 } as OverlayRect;
const { opts, startGesture, down } = await pressDuringChange(turned, {
iframe: silentFrame(),
overlayRect: drawn,
whileWaiting: (o) => o.waitingPressRef.current!.draw!(30, 0),
});
startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
opts.waitingPressRef.current!.draw!(30, 0);
await vi.waitFor(() => expect(opts.onBlockedMoveRef.current).toHaveBeenCalledTimes(2), {
timeout: 8000,
});
expect(opts.overlayRectRef.current).toEqual(drawn);
}, 10_000);
// The first press started, so the layer goes where it dropped: the refused second press's own box, not the first's.
it("puts the box where a started press left it when the press after it goes unanswered", async () => {
const turned = makeSelection("turned", document.getElementById("turned")!);
const drawn = { left: 1, top: 2, width: 3, height: 4 } as OverlayRect;
const { opts, startGesture, down } = await pressDuringChange(turned, {
overlayRect: drawn,
whileWaiting: (o) => o.waitingPressRef.current!.draw!(30, 0),
});
press.start.mockImplementationOnce(() => {
opts.iframeRef.current = silentFrame();
return true;
});
startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
opts.waitingPressRef.current!.draw!(30, 0);
await vi.waitFor(() => expect(opts.onBlockedMoveRef.current).toHaveBeenCalledOnce(), {
timeout: 3000,
});
expect(press.start).toHaveBeenCalledOnce();
expect(opts.overlayRectRef.current).toEqual({ ...drawn, left: 31 });
});
// fails without the fix: with the selection cleared mid-wait, the refusal left the box where it was dragged.
it("puts the box back on the layer when the selection clears while an unanswered press waits", async () => {
const turned = makeSelection("turned", document.getElementById("turned")!);
const drawn = { left: 1, top: 2, width: 3, height: 4 } as OverlayRect;
const { opts } = await pressDuringChange(turned, {
iframe: silentFrame(),
overlayRect: drawn,
whileWaiting: (o) => {
o.waitingPressRef.current!.draw!(30, 0);
o.selectionRef.current = null;
},
});
await vi.waitFor(() => expect(opts.waitingPressRef.current).toBeNull(), { timeout: 3000 });
expect(opts.overlayRectRef.current).toEqual(drawn);
expect(opts.onBlockedMoveRef.current).not.toHaveBeenCalled();
});
it("asks the frame shown again when a swap comes after its first ask", async () => {
const turned = document.getElementById("turned")!;
const drawn = { left: 1, top: 2, width: 3, height: 4 } as OverlayRect;