| @@ -51,8 +51,85 @@ | ||
| 51 | 51 | } ); |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
| 55 | + * Returns the document holding the Editor canvas. | |
| 56 | + * | |
| 57 | + * The Post Editor is always iframed as of WordPress 7.1, and conditionally | |
| 58 | + * iframed before it. Falls back to the admin document when not iframed. | |
| 59 | + * | |
| 60 | + * @since 3.23.6 | |
| 61 | + * | |
| 62 | + * @return {Document} The document holding the Editor canvas. | |
| 63 | + */ | |
| 64 | +export function getEditorCanvasDocument(): Document { | |
| 65 | + // WordPress 6.5 and later name the canvas iframe. | |
| 66 | + const namedCanvas = document.querySelector<HTMLIFrameElement>( | |
| 67 | + 'iframe[name="editor-canvas"]' | |
| 68 | + ); | |
| 69 | + | |
| 70 | + if ( namedCanvas?.contentDocument ) { | |
| 71 | + return namedCanvas.contentDocument; | |
| 72 | + } | |
| 73 | + | |
| 74 | + // WordPress 6.3 and 6.4 iframe the canvas without naming the iframe, so it | |
| 75 | + // gets identified by the Editor markup it holds. | |
| 76 | + const iframes = Array.from( | |
| 77 | + document.querySelectorAll<HTMLIFrameElement>( 'iframe' ) | |
| 78 | + ); | |
| 79 | + | |
| 80 | + for ( const iframe of iframes ) { | |
| 81 | + // Reading contentDocument returns null for cross-origin iframes. | |
| 82 | + const iframeDocument = iframe.contentDocument; | |
| 83 | + | |
| 84 | + if ( iframeDocument?.querySelector( '.editor-styles-wrapper, .block-editor-block-list__layout' ) ) { | |
| 85 | + return iframeDocument; | |
| 86 | + } | |
| 87 | + } | |
| 88 | + | |
| 89 | + return document; | |
| 90 | +} | |
| 91 | + | |
| 92 | +/** | |
| 93 | + * Waits for an element to exist in the Editor canvas, which is populated | |
| 94 | + * asynchronously after the Editor reports itself as ready. | |
| 95 | + * | |
| 96 | + * @since 3.23.6 | |
| 97 | + * | |
| 98 | + * @param {string} selector The selector to wait for. | |
| 99 | + * @param {number} timeoutMs How long to wait before giving up, in milliseconds. | |
| 100 | + * | |
| 101 | + * @return {Promise<HTMLElement|null>} The matched element, or null on timeout. | |
| 102 | + */ | |
| 103 | +export function waitForEditorCanvasElement( | |
| 104 | + selector: string, | |
| 105 | + timeoutMs: number = 5000 | |
| 106 | +): Promise<HTMLElement | null> { | |
| 107 | + return new Promise( ( resolve ) => { | |
| 108 | + const deadline = Date.now() + timeoutMs; | |
| 109 | + | |
| 110 | + const poll = (): void => { | |
| 111 | + const element = | |
| 112 | + getEditorCanvasDocument().querySelector<HTMLElement>( selector ); | |
| 113 | + | |
| 114 | + if ( element ) { | |
| 115 | + resolve( element ); | |
| 116 | + return; | |
| 117 | + } | |
| 118 | + | |
| 119 | + if ( Date.now() >= deadline ) { | |
| 120 | + resolve( null ); | |
| 121 | + return; | |
| 122 | + } | |
| 123 | + | |
| 124 | + setTimeout( poll, 100 ); | |
| 125 | + }; | |
| 126 | + | |
| 127 | + poll(); | |
| 128 | + } ); | |
| 129 | +} | |
| 130 | + | |
| 131 | +/** | |
| 55 | 132 | * Adds ITM parameters to a URL. |
| 56 | 133 | * |
| 57 | 134 | * @since 3.19.0 |
| 58 | 135 | * |