-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Reusable Blocks: Add reusable blocks effects #3377
Reusable Blocks: Add reusable blocks effects #3377
Conversation
lib/client-assets.php
Outdated
// Ensure that we've got jQuery. | ||
if ( ! wp_script_is( 'jquery', 'done' ) ) { | ||
wp_enqueue_script( 'jquery' ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't we just add a jquery
dependency, something like this 255ef19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can only pass dependencies into wp_enqueue_script
, not wp_add_inline_script
: https://developer.wordpress.org/reference/functions/wp_add_inline_script/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But wp_add_inline_script
is tied to the original handle wp-editor
, and we can define jQuery as a dependency for the enqueuing of wp-editor
.
lib/client-assets.php
Outdated
wp_add_inline_script( | ||
'wp-editor', | ||
( | ||
'jQuery.when( wp.api.init(), wp.api.init( { versionString: \'gutenberg/v1/\' } ) ).done( function() {' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is jQuery.when
the same as Promise.all
. It would be great if we can avoid jQuery
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect init
may be returning an instance of jQuery.Deferred
. Unsure if Promise.all
is capable of handling these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like that might work: https://codepen.io/noisysocks/pen/zPROqr?editors=0011
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, but we can't do this because IE 11 doesn't support promises: https://caniuse.com/#feat=promises
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use a promise polyfill :)
gutenberg/lib/client-assets.php
Line 141 in 6eec188
wp_add_inline_script( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right you are! 😄 Changed in f5cd6cd.
lib/client-assets.php
Outdated
'wp-editor', | ||
( | ||
'jQuery.when( wp.api.init(), wp.api.init( { versionString: \'gutenberg/v1/\' } ) ).done( function() {' | ||
. 'wp.editor.createEditorInstance( \'editor\', window._wpGutenbergPost, ' . json_encode( $editor_settings ) . ' ); ' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we should drop this line? Probably a merge conflict?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops! Fixed in 5367b6e.
Adds the effects necessary for supporting reusable blocks. There are 4: - FETCH_REUSABLE_BLOCKS: Loads reusable blocks from the API and inserts them into the store. - SAVE_REUSABLE_BLOCK: Persists a reusable block that's in the store to the API. - MAKE_BLOCK_STATIC: Transforms a reusable block on the page into a regular block. - MAKE_BLOCK_REUSABLE: Transforms a regular block on the page into a reusable block.
Codecov Report
@@ Coverage Diff @@
## master #3377 +/- ##
==========================================
+ Coverage 34.9% 36.92% +2.01%
==========================================
Files 263 267 +4
Lines 6727 6663 -64
Branches 1227 1203 -24
==========================================
+ Hits 2348 2460 +112
+ Misses 3694 3551 -143
+ Partials 685 652 -33
Continue to review full report at Codecov.
|
This unnecessary line probably came as a result of a bad merge conflict resolution.
`gutenberg_collect_meta_box_data` expected that `window._wpGutenbergEditor` would always be instantiated, but this is only true for when `wp.api.init()` returns quickly because of the API schema being cached. The fix is to use a Promise to store `window._wpGutenbergEditor`. (Well, a jQuery.Deferred, since we have to support IE 11 which doesn't support promises.)
Remove jQuery.Deferred and jQuery.when in our editor initialization in lieu of Promise and Promise.all.
lib/register.php
Outdated
@@ -236,7 +236,7 @@ function gutenberg_collect_meta_box_data() { | |||
*/ | |||
wp_add_inline_script( | |||
'wp-editor', | |||
'window._wpGutenbergEditor.initializeMetaBoxes( ' . wp_json_encode( $meta_box_data ) . ' )' | |||
'window._wpGutenbergEditor.then( function( editor ) { editor.initializeMetaBoxes( ' . wp_json_encode( $meta_box_data ) . ' ) } );' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch here! Potential bug fixed. Maybe we should rename the variable to make it clear that it's a promise and not the editor instance. Something like _wpLoadGutenbergEditor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 e156849
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work
Make it clear that this variable is a promise by giving it a non-noun name.
Adds the effects necessary for supporting reusable blocks. There are 4:
them into the store.
the API.
regular block.
reusable block.