File size: 1,539 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * Returns the editor iframe element, or null if not found.
 * @returns {HTMLElement | null}
 */
const getEditorIframe = (): HTMLIFrameElement | null => {
	const editorIframe = document.querySelector< HTMLIFrameElement >(
		'iframe[name="editor-canvas"]'
	);
	return editorIframe || null;
};

/**
 * Check if the Editor iframe is focused.
 * @returns {boolean} Whether the editor iframe has focus.
 */
export const isEditorIframeFocused = (): boolean => {
	const editorIframe = getEditorIframe();
	const iframeFocused =
		editorIframe?.contentDocument?.activeElement?.getAttribute( 'contenteditable' ) === 'true';
	return !! iframeFocused;
};

/**
 * Focus the Editor iframe if possible.
 * @returns {void}
 */
export const focusEditor = (): void => {
	if ( isEditorIframeFocused() ) {
		return;
	}

	const attemptFocus = () => {
		const editorIframe = getEditorIframe();
		const editable = editorIframe?.contentDocument?.querySelector< HTMLElement >(
			'[contenteditable="true"]'
		);

		if ( ! editable ) {
			return false;
		}
		editable.focus();
		return true;
	};

	// If immediate focus attempt fails, watch for DOM changes until
	// the editor becomes available, then focus and auto-disconnect.
	if ( ! attemptFocus() ) {
		const observer = new MutationObserver( ( _, obs ) => {
			if ( attemptFocus() ) {
				obs.disconnect();
			}
		} );
		observer.observe( document.body, { childList: true, subtree: true } );

		// Failsafe disconnect and give up after 30 seconds.
		setTimeout( () => observer.disconnect(), 30000 );
	}
};