Skip to content

Commit 02c5a33

Browse files
fix(studio): a drag pressed as a sealed preview takes the keyboard is no longer dropped (#9107)
The sealed frame taking keyboard focus blurs Studio's window, and the held gesture cancel read that as the person leaving the window. A blur whose focus went into a frame on the page now leaves the press held.
1 parent 26f15ca commit 02c5a33

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎packages/studio/src/components/editor/gestureHoldsPreviewReload.test.tsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,26 @@ describe("a press while a script write is out", () => {
678678
});
679679
});
680680

681+
describe("focus entering a frame on the page", () => {
682+
// fails without the fix: a sealed preview took the keyboard just after a press, the window's
683+
// blur dropped the held press, and the drag never moved or saved (seqfastkeys under load).
684+
it("keeps a held drag, which still lands where it is let go", async () => {
685+
const editor = mountEditor(false);
686+
const host = document.body.appendChild(document.createElement("div"));
687+
const frame = host.attachShadow({ mode: "open" }).appendChild(document.createElement("iframe"));
688+
pointer(editor.box, "pointerdown", 150, 150);
689+
pointer(editor.overlay, "pointermove", 170, 160);
690+
act(() => {
691+
frame.focus();
692+
window.dispatchEvent(new Event("blur"));
693+
});
694+
pointer(editor.overlay, "pointermove", 190, 170);
695+
pointer(editor.overlay, "pointerup", 190, 170);
696+
await waitFor(() => expect(file.title).toBe("translate: 40px 20px"));
697+
expect(editor.onPathOffsetCommit).toHaveBeenCalledTimes(1);
698+
});
699+
});
700+
681701
describe("Escape on a drag queued behind a write", () => {
682702
/** A released drop whose write is still out, then a second press on the box. */
683703
function secondPressBehindAWrite() {

‎packages/studio/src/components/editor/useHeldGestureCancel.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import type { RefObject } from "react";
22
import { useMountEffect } from "../../hooks/useMountEffect";
33
import { matchesKey } from "../../player/components/studioShortcuts";
44

5+
function focusMovedIntoAFrameOnThePage(): boolean {
6+
let active = document.activeElement;
7+
while (active?.shadowRoot?.activeElement) active = active.shadowRoot.activeElement;
8+
return active instanceof HTMLIFrameElement;
9+
}
10+
511
/** Cancels a held gesture that loses the window, the tab or the overlay, or meets Escape. */
612
export function useHeldGestureCancel(
713
cancelRef: RefObject<(dropQueued?: boolean) => void>,
@@ -19,11 +25,12 @@ export function useHeldGestureCancel(
1925
suppressNextClickRef.current = true;
2026
};
2127
const onHidden = () => document.hidden && cancel();
22-
window.addEventListener("blur", cancel);
28+
const onBlur = () => !focusMovedIntoAFrameOnThePage() && cancel();
29+
window.addEventListener("blur", onBlur);
2330
window.addEventListener("keydown", onEscape, true);
2431
document.addEventListener("visibilitychange", onHidden);
2532
return () => {
26-
window.removeEventListener("blur", cancel);
33+
window.removeEventListener("blur", onBlur);
2734
window.removeEventListener("keydown", onEscape, true);
2835
document.removeEventListener("visibilitychange", onHidden);
2936
cancelRef.current(true);

0 commit comments

Comments
 (0)