Open History to see the last commit
1let latestOperationId = 0;
2
3const getMeaningfulActiveElement = (): HTMLElement | null => {
4 if (typeof document === "undefined" || typeof HTMLElement === "undefined") return null;
5
6 const activeElement = document.activeElement;
7 if (
8 !(activeElement instanceof HTMLElement) ||
9 !activeElement.isConnected ||
10 activeElement === document.body ||
11 activeElement === document.documentElement
12 ) {
13 return null;
14 }
15 return activeElement;
16};
17
18const isDocumentFocused = (): boolean => {
19 if (typeof document === "undefined") return false;
20 return typeof document.hasFocus !== "function" || document.hasFocus();
21};
22
23/**
24 * Keeps preview automation from changing focus in the shared renderer.
25 */
26export async function withPreviewAutomationFocus<T>(operation: () => Promise<T>): Promise<T> {
27 const operationId = ++latestOperationId;
28 const previouslyFocused = getMeaningfulActiveElement();
29 const wasDocumentFocused = isDocumentFocused();
30 let userFocusObserved = false;
31 let pendingUserFocus = false;
32 let pendingVersion = 0;
33 let nativeFocusElement: HTMLElement | null = null;
34 let windowBlurred = false;
35 let windowRefocused = false;
36
37 const markPendingUserFocus = (event: Event): void => {
38 if (!event.isTrusted) return;
39 pendingUserFocus = true;
40 const version = ++pendingVersion;
41 queueMicrotask(() => {
42 if (pendingVersion === version) pendingUserFocus = false;
43 });
44 };
45 const onFocusIn = (event: Event): void => {
46 const nativeFocusTransfer = windowBlurred && windowRefocused;
47 const target = event.target instanceof HTMLElement ? event.target : null;
48 if (nativeFocusTransfer) nativeFocusElement = target;
49 if (event.isTrusted && (pendingUserFocus || (!nativeFocusTransfer && target?.isConnected))) {
50 userFocusObserved = true;
51 }
52 pendingUserFocus = false;
53 pendingVersion += 1;
54 windowBlurred = false;
55 windowRefocused = false;
56 };
57 const onWindowBlur = (): void => {
58 windowBlurred = true;
59 windowRefocused = false;
60 };
61 const onWindowFocus = (): void => {
62 if (windowBlurred) windowRefocused = true;
63 };
64
65 if (typeof document !== "undefined") {
66 document.addEventListener("pointerdown", markPendingUserFocus, true);
67 document.addEventListener("keydown", markPendingUserFocus, true);
68 document.addEventListener("focusin", onFocusIn, true);
69 }
70 if (typeof window !== "undefined") {
71 window.addEventListener("blur", onWindowBlur);
72 window.addEventListener("focus", onWindowFocus);
73 }
74
75 try {
76 return await operation();
77 } finally {
78 if (typeof document !== "undefined") {
79 document.removeEventListener("pointerdown", markPendingUserFocus, true);
80 document.removeEventListener("keydown", markPendingUserFocus, true);
81 document.removeEventListener("focusin", onFocusIn, true);
82 }
83 if (typeof window !== "undefined") {
84 window.removeEventListener("blur", onWindowBlur);
85 window.removeEventListener("focus", onWindowFocus);
86 }
87
88 const activeElement = getMeaningfulActiveElement();
89 const activeFocusIsExpected =
90 !activeElement || activeElement === previouslyFocused || activeElement === nativeFocusElement;
91 if (
92 operationId === latestOperationId &&
93 !userFocusObserved &&
94 wasDocumentFocused &&
95 !windowBlurred &&
96 isDocumentFocused() &&
97 previouslyFocused?.isConnected &&
98 activeFocusIsExpected &&
99 activeElement !== previouslyFocused
100 ) {
101 try {
102 previouslyFocused.focus({ preventScroll: true });
103 } catch {
104 // Focus restoration is best effort; never mask the automation result.
105 }
106 }
107 }
108}