Skip to content

Commit e6442ac

Browse files
committed
Fix FM module loader in Gutenberg context
This backports the fix in 0bf49fb to the 1.3 branch. See #815, #819.
1 parent 280bc23 commit e6442ac

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

js/fieldmanager-loader.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,53 @@
55
* @param {function} callback - The callback function to execute when the DOM is ready.
66
*/
77
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+
840
if ( 'object' === typeof wp && 'function' === typeof wp.domReady ) {
9-
wp.domReady( callback );
41+
wp.domReady( wrappedCallback );
1042
} else if ( jQuery ) {
11-
jQuery( document ).ready( callback );
43+
jQuery( document ).ready( wrappedCallback );
1244
} else {
1345
// Shim wp.domReady.
1446
if (
1547
document.readyState === 'complete' || // DOMContentLoaded + Images/Styles/etc loaded, so we call directly.
1648
document.readyState === 'interactive' // DOMContentLoaded fires at this point, so we call directly.
1749
) {
18-
callback();
50+
wrappedCallback();
1951
return;
2052
}
2153

2254
// DOMContentLoaded has not fired yet, delay callback until then.
23-
document.addEventListener( 'DOMContentLoaded', callback );
55+
document.addEventListener( 'DOMContentLoaded', wrappedCallback );
2456
}
2557
}

0 commit comments

Comments
 (0)