-
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
Merged
westonruter
merged 15 commits into
develop
from
fix/preview-button-not-displayed-in-editor
Oct 5, 2020
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
aac2d64
Fix the AMP Preview button not being shown in the block editor
pierlon 090effd
Only render the preview button if the post type is viewable
pierlon 963993c
Bail if the 'Preview' button could not be found
pierlon af5d67d
Test with Gutenberg being disabled
pierlon a05e050
Use latest version of Gutenberg in E2E tests
pierlon 226d951
Separate AMP preview button into its own component
pierlon b4adf2f
Fix visual inconsistency between Core and Gutenberg
pierlon f61af8d
Revert "Fix visual inconsistency between Core and Gutenberg"
pierlon c2c883c
Fix style difference between Gutenberg and Core
pierlon 365a8e6
Let the user know if the current Gutenberg or WordPress version is no…
pierlon f07f291
Sanitize translation
pierlon 7a361a6
Include note as to why the WP and GB versions were chosen
pierlon dc39c3f
Reword notice
pierlon 7d9ea2b
Only show warning notice to users with `manage_options` capability
pierlon 9321696
Improve indentation
westonruter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export { default as AMPPreview } from './amp-preview'; | ||
export { default as MediaPlaceholder } from './media-placeholder'; | ||
export { default as LayoutControls } from './layout-controls'; | ||
export { default as withMediaLibraryNotice } from './with-media-library-notice'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
assets/src/block-editor/helpers/test/renderPreviewButton.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
assets/src/block-editor/plugins/wrapped-amp-preview-button.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component, createPortal } from '@wordpress/element'; | ||
import { withSelect } from '@wordpress/data'; | ||
import { ifCondition, compose, pure } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { POST_PREVIEW_CLASS } from '../constants'; | ||
import AmpPreviewButton from '../components/amp-preview-button'; | ||
|
||
/** | ||
* A wrapper for the AMP preview button that renders it immediately after the 'Post' preview button, when present. | ||
*/ | ||
class WrappedAmpPreviewButton extends Component { | ||
/** | ||
* Constructs the class. | ||
* | ||
* @param {*} args Constructor arguments. | ||
*/ | ||
constructor( ...args ) { | ||
super( ...args ); | ||
|
||
this.root = document.createElement( 'div' ); | ||
this.root.className = 'amp-wrapper-post-preview'; | ||
|
||
this.postPreviewButton = document.querySelector( `.${ POST_PREVIEW_CLASS }` ); | ||
} | ||
|
||
/** | ||
* Invoked immediately after a component is mounted (inserted into the tree). | ||
*/ | ||
componentDidMount() { | ||
if ( ! this.postPreviewButton ) { | ||
return; | ||
} | ||
|
||
// Insert the AMP preview button immediately after the post preview button. | ||
this.postPreviewButton.parentNode.insertBefore( this.root, this.postPreviewButton.nextSibling ); | ||
} | ||
|
||
/** | ||
* Invoked immediately before a component is unmounted and destroyed. | ||
*/ | ||
componentWillUnmount() { | ||
if ( ! this.postPreviewButton ) { | ||
return; | ||
} | ||
|
||
this.postPreviewButton.parentNode.removeChild( this.root ); | ||
} | ||
|
||
/** | ||
* Renders the component. | ||
*/ | ||
render() { | ||
if ( ! this.postPreviewButton ) { | ||
return null; | ||
} | ||
|
||
return createPortal( <AmpPreviewButton />, this.root ); | ||
} | ||
} | ||
|
||
export const name = 'amp-preview-button-wrapper'; | ||
|
||
export const render = pure( | ||
compose( [ | ||
withSelect( ( select ) => { | ||
const { getPostType } = select( 'core' ); | ||
const { getEditedPostAttribute } = select( 'core/editor' ); | ||
|
||
const postType = getPostType( getEditedPostAttribute( 'type' ) ); | ||
|
||
return { | ||
isViewable: get( postType, [ 'viewable' ], false ), | ||
}; | ||
} ), | ||
// This HOC creator renders the component only when the condition is true. At that point the 'Post' preview | ||
// button should have already been rendered (since it also relies on the same condition for rendering). | ||
ifCondition( ( { isViewable } ) => isViewable ), | ||
] )( WrappedAmpPreviewButton ), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.