-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the AMP Preview button not being shown in the block editor (#5441)
- Loading branch information
1 parent
7d9f743
commit 422a70d
Showing
13 changed files
with
214 additions
and
130 deletions.
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
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.