-
Notifications
You must be signed in to change notification settings - Fork 384
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
Fix the AMP Preview button not being shown in the block editor #5441
Fix the AMP Preview button not being shown in the block editor #5441
Conversation
Plugin builds for 9321696 are ready 🛎️!
|
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.
A couple of small pieces of feedback, @pierlon.
Also, while the approach here with createPortal
and inserting an element into the DOM on component mount is working, I think there should be a little more separation of concerns so that the component doing all the preview work doesn't have to be so aware of the DOM context. Two possible ways to achieve this:
- Create and insert the DOM element at the start of the script, and then render the component into the created element using standard
ReactDOM.render
. This would require registering this particular editor plugin outside the loop where plugins are being registered, but I don't think that's a problem. - If you still want to register the plugin inside the loop, I suggest doing the DOM manipulation work in a parent component that has that one job, and doing all the preview-related work in a child component.
this.openPreviewWindow = this.openPreviewWindow.bind( this ); | ||
|
||
this.root = document.createElement( 'div' ); | ||
this.root.id = 'amp-wrapper-post-preview'; |
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.
Maybe this should be a class instead of an ID. While unlikely, it's technically possible for this component to be rendered multiple times on a page.
@johnwatkins0 I've opted for option #2 from your review as I think it's the most applicable here. If I understand correctly, option #1 would've entailed moving the rendered component next to the 'Preview' button once the component had been mounted. I'm not entirely sure what you meant by:
|
|
||
export const name = 'amp-preview-button-wrapper'; | ||
|
||
export const render = pure( |
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've opted to make this a pure component as this does not warrant unnecessary re-rendering, unless I'm missing something. @johnwatkins0 thoughts?
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 think this is a good idea. I've seen side effects from having createPortal
inside components that re-render often. E.g., not applicable in this case, but if there were any CSS animations on any of the element inside the portal, the animation might restart on every rerender.
@pierlon I was referring to these lines: amp-wp/assets/src/block-editor/index.js Lines 25 to 29 in 5902120
All the plugin's editor plugins are expected to export the same variables to be registered in this loop. I already found this to be too limiting in a WIP branch I'm working on for the plugin sidebar, so I don't think we necessarily need to lock all editor plugins into this model. Performing DOM manipulation on the screen before registering the editor plugin is one use case that won't fit into the loop. But since your approach is working, this is a nonissue anyway. |
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!
This reverts commit b4adf2f
Co-authored-by: Weston Ruter <[email protected]>
Ah good catch. That should be resolved in c2c883c. |
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.
sprintf( | ||
'wp.domReady( | ||
function () { | ||
wp.data.dispatch( "core/notices" ).createWarningNotice( "%s" ) |
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.
wp.data.dispatch( "core/notices" ).createWarningNotice( "%s" ) | |
wp.data.dispatch( "core/notices" ).createWarningNotice( %s ) |
wp.data.dispatch( "core/notices" ).createWarningNotice( "%s" ) | ||
} | ||
);', | ||
__( 'AMP functionality is not available in the Block Editor as the current version installed is not supported. Please update the Block Editor plugin to the latest version or use the Classic Editor instead.', 'amp' ) |
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.
__( 'AMP functionality is not available in the Block Editor as the current version installed is not supported. Please update the Block Editor plugin to the latest version or use the Classic Editor instead.', 'amp' ) | |
wp_json_encode( __( 'AMP functionality is not available in the Block Editor as the current version installed is not supported. Please update the Block Editor plugin to the latest version or use the Classic Editor instead.', 'amp' ) ) |
@@ -213,6 +213,26 @@ public function enqueue_block_assets() { | |||
return; | |||
} | |||
|
|||
$gb_supported = defined( 'GUTENBERG_VERSION' ) && version_compare( GUTENBERG_VERSION, '5.3.0', '>=' ); |
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 5.3.0 specifically?
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 was the first stable release that worked without any errors. I guess I could bump it to 5.4.0 since that was the version bundled with WP 5.2.
Co-authored-by: Weston Ruter <[email protected]>
|
||
// Let the user know that block editor functionality is not available if the current Gutenberg or WordPress version is not supported. | ||
if ( ! $gb_supported && ! $wp_supported ) { | ||
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.
How about wrapping this in a current_user_can( 'manage_options' )
check? It doesn't do much good to show this warning to non-admin users.
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.
Yea that's a good idea.
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.
Done in 7d9ea2b.
Co-authored-by: Weston Ruter <[email protected]>
@pierlon my testing steps were:
This is working as expected. Did I miss something though? Want to make sure I'm testing the right thing. |
@jwold You need to switch to a Paired AMP mode (either Reader mode or Transitional mode). Then you should see an AMP-specific preview button like so: |
Summary
This PR fixes of the "Preview AMP" button no longer being shown on the edit post page (determined to be last working on Gutenberg v8.7.1). This has been tested on Gutenberg versions v6.8.0, v8.7.1, and v9.0.0.
The issue arises from the (non-AMP) "Preview" button no longer being available on DOM load (possibly due to async rendering of the component, but I have not confirmed). Previously, we would bail and not render the "Preview AMP" button if the "Preview" button was not found:
amp-wp/assets/src/block-editor/index.js
Lines 65 to 67 in 239a334
amp-wp/assets/src/block-editor/helpers/index.js
Lines 728 to 735 in 239a334
To fix this issue, the "Preview AMP" button component is no longer rendered on DOM load; the component is now registered as a block editor plugin. Once the block editor calls the component's
render
function, it will be rendered inside a DOM node we control (a portal), and not within the editor (yet). As the component is about to be mounted into the DOM node, a check is made to see if the "Preview" button is present, and if so, the component will be inserted immediately after the "Preview" button.Upgrade Notice
For versions of WP prior to 5.2 or if Gutenberg 5.4+ is not running, then a warning is displayed prompting administrators to upgrade:
Checklist