Reusable Blocks: Add reusable blocks effects#3377
Reusable Blocks: Add reusable blocks effects#3377noisysocks merged 5 commits intoWordPress:masterfrom noisysocks:add/reusable-blocks-effects
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.
Why can't we just add a jquery dependency, something like this 255ef19
There was a problem hiding this comment.
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.
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.
Is jQuery.when the same as Promise.all. It would be great if we can avoid jQuery
There was a problem hiding this comment.
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.
It looks like that might work: https://codepen.io/noisysocks/pen/zPROqr?editors=0011
There was a problem hiding this comment.
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.
We use a promise polyfill :)
gutenberg/lib/client-assets.php
Line 141 in 6eec188
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.
I guess we should drop this line? Probably a merge conflict?
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
| 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.
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
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.