Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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( () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be up to the editor to decide the specific time it should be initialized? Or should the prerequisite of the initialization be considered by that which is initializing it (thinking outside the specific "WP + Plugins" context).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. you mean moving this to client-assets instead. Sounds like a good idea.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that you asked it this way, it sounds reasonable 😃
I fully focused on fixing the issue with blocks loaded too late and I didn’t think about it.
We will have to make dom-ready its own module but it would happen sooner or later anyway.

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