|
5 | 5 | * @param {function} callback - The callback function to execute when the DOM is ready. |
6 | 6 | */ |
7 | 7 | function fmLoadModule( callback ) { |
| 8 | + /** |
| 9 | + * Wraps the provided callback to add check for the Gutenberg editor. If this |
| 10 | + * is called within the Gutenberg editor, then the callback function will trigger |
| 11 | + * after the metaboxes are initialized. |
| 12 | + */ |
| 13 | + const wrappedCallback = () => { |
| 14 | + if ( document.querySelector( '.block-editor-page' ) ) { |
| 15 | + const unsubscribeListener = wp.data.subscribe( () => { |
| 16 | + /** |
| 17 | + * `areMetaBoxesInitialized` is called immediately before the |
| 18 | + * `MetaBoxesArea` component is rendered, which is where the metabox |
| 19 | + * HTML is moved from the hidden div and into the main form element. |
| 20 | + * |
| 21 | + * This means we need to checks for the existence of the markup in the |
| 22 | + * DOM before we run our callbacks and then unsubscribe our listener. |
| 23 | + * |
| 24 | + * @link https://github.com/WordPress/gutenberg/blob/019d0a1b1883a5c3e5c9cdecc60bd5e546b60a1b/packages/edit-post/src/components/meta-boxes/index.js#L38-L45 |
| 25 | + * @link https://github.com/WordPress/gutenberg/blob/d39949a3b9dc8e12d5f5d33b9091f14b93b37c8a/packages/edit-post/src/components/meta-boxes/meta-boxes-area/index.js#L34-L36 |
| 26 | + */ |
| 27 | + if ( |
| 28 | + wp.data.select( 'core/edit-post' ).areMetaBoxesInitialized() |
| 29 | + && document.querySelector( '.edit-post-meta-boxes-area__container' ) |
| 30 | + ) { |
| 31 | + callback(); |
| 32 | + unsubscribeListener(); |
| 33 | + } |
| 34 | + } ); |
| 35 | + } else { |
| 36 | + callback(); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
8 | 40 | if ( 'object' === typeof wp && 'function' === typeof wp.domReady ) { |
9 | | - wp.domReady( callback ); |
| 41 | + wp.domReady( wrappedCallback ); |
10 | 42 | } else if ( jQuery ) { |
11 | | - jQuery( document ).ready( callback ); |
| 43 | + jQuery( document ).ready( wrappedCallback ); |
12 | 44 | } else { |
13 | 45 | // Shim wp.domReady. |
14 | 46 | if ( |
15 | 47 | document.readyState === 'complete' || // DOMContentLoaded + Images/Styles/etc loaded, so we call directly. |
16 | 48 | document.readyState === 'interactive' // DOMContentLoaded fires at this point, so we call directly. |
17 | 49 | ) { |
18 | | - callback(); |
| 50 | + wrappedCallback(); |
19 | 51 | return; |
20 | 52 | } |
21 | 53 |
|
22 | 54 | // DOMContentLoaded has not fired yet, delay callback until then. |
23 | | - document.addEventListener( 'DOMContentLoaded', callback ); |
| 55 | + document.addEventListener( 'DOMContentLoaded', wrappedCallback ); |
24 | 56 | } |
25 | 57 | } |
0 commit comments