Skip to content

Commit

Permalink
Framework: Make sure block assets are always registered before wp-edi…
Browse files Browse the repository at this point in the history
…t-post script (#6448)

* Framework: Make sure block assets are always registered before wp-edit-post script

* Framework: Use domReady to make sure that all blocks are properly registered
  • Loading branch information
gziolo committed Apr 30, 2018
1 parent 1218df9 commit 4e766f1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
32 changes: 20 additions & 12 deletions edit-post/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { get, isString, some } from 'lodash';
/**
* WordPress dependencies
*/
import { registerCoreBlocks } from '@wordpress/core-blocks';
import domReady from '@wordpress/dom-ready';
import { render, unmountComponentAtNode } from '@wordpress/element';
import { deprecated } from '@wordpress/utils';

Expand Down Expand Up @@ -59,9 +61,6 @@ export function reinitializeEditor( target, settings ) {
* @return {Object} Editor interface.
*/
export function initializeEditor( id, post, settings ) {
const target = document.getElementById( id );
const reboot = reinitializeEditor.bind( null, target, settings );

if ( 'production' !== process.env.NODE_ENV ) {
// Remove with 3.0 release.
window.console.info(
Expand All @@ -84,16 +83,25 @@ export function initializeEditor( id, post, settings ) {
);
}

render(
<Editor settings={ migratedSettings || settings } onError={ reboot } post={ post } />,
target
);
return new Promise( ( resolve ) => {
domReady( () => {
const target = document.getElementById( id );
const reboot = reinitializeEditor.bind( null, target, settings );

registerCoreBlocks();

render(
<Editor settings={ migratedSettings || settings } onError={ reboot } post={ post } />,
target
);

return {
initializeMetaBoxes( metaBoxes ) {
store.dispatch( initializeMetaBoxState( metaBoxes ) );
},
};
resolve( {
initializeMetaBoxes( metaBoxes ) {
store.dispatch( initializeMetaBoxState( metaBoxes ) );
},
} );
} );
} );
}

export { default as PluginSidebar } from './components/sidebar/plugin-sidebar';
Expand Down
5 changes: 3 additions & 2 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,9 @@ function gutenberg_editor_scripts_and_styles( $hook ) {
$script = '( function() {';
$script .= sprintf( 'var editorSettings = %s;', wp_json_encode( $editor_settings ) );
$script .= <<<JS
window._wpLoadGutenbergEditor = wp.api.init().then( function() {
wp.coreBlocks.registerCoreBlocks();
window._wpLoadGutenbergEditor = new Promise( function( resolve ) {
wp.api.init().then( resolve );
} ).then( function() {
return wp.editPost.initializeEditor( 'editor', window._wpGutenbergPost, editorSettings );
} );
JS;
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"@wordpress/a11y": "1.0.6",
"@wordpress/autop": "1.0.4",
"@wordpress/dom-ready": "1.0.4",
"@wordpress/hooks": "1.1.6",
"@wordpress/i18n": "1.1.0",
"@wordpress/url": "1.0.3",
Expand Down

0 comments on commit 4e766f1

Please sign in to comment.