-
Notifications
You must be signed in to change notification settings - Fork 378
Add an 'AMP Preview' button to the block editor #3323
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
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
4558a05
Add an 'AMP Preview' button to the block editor
kienstra d86f090
Don't display the 'AMP Preview' button if the 'Enable AMP' toggle isn…
kienstra 75e357c
Address failed PHPUnit test, though I'm not positive this is the righ…
kienstra 7a574ef
Use the AMP slug from amp_get_slug(), apply query var to currentPostLink
kienstra cc1bedb
Test that the proper values are localized
kienstra 98133f6
Correct the <AMPPreview> propTypes
kienstra f80dd86
Experiment with manipulating the DOM
kienstra cdc6e01
Merge branch 'develop' into add/block-editor-amp-preview
kienstra 3459cbf
Try maniplating the DOM to add the 'Preview AMP' button
kienstra 097ba95
Adjust styling of the non-AMP 'Preview' button
kienstra 89bc7e8
Fix an issue where the 'Preview AMP' button was out of place on a new…
kienstra 14df86f
Merge pull request #3392 from ampproject/try/dom-manipulation-amp-pre…
kienstra e46f3b6
Merge in develop, resolve conflict
kienstra c02a2d5
Fix failing unit tests
kienstra 1dc42af
Use the 'amp/block-editor' store for the exported data
kienstra 038b078
Move a constant into a function, as it's only used there
kienstra db963ac
Fix an issue with the icon display in WP 5.3
kienstra 788fb32
Add an xmlns property to the <svg> so the build succeeds
kienstra 32057c7
Add a unit test for the helper renderPreviewButton
kienstra c67f5b5
Remove extra *,and use a simply one-line comment
kienstra 6631f44
Address failed unit tests by removing 2 expected dependencies
kienstra 885d2f2
Remove styling for the (non-AMP) Preview button
kienstra e055c4d
Ensure the height of the 'Preview AMP' button matches the non-AMP pre…
kienstra 463d75b
Ensure the height of the 'Preview AMP' button matches the non-AMP pre…
kienstra 32e3e31
Remove an extra call in a unit test to renderPreviewButton()
kienstra 1fc8bb6
Remove extra style rules for 'Preview AMP' button
kienstra cc2a307
Exit if the non-AMP 'Preview' button has no nextSibling
kienstra 286ff79
Remove a check for whether the component exists already
kienstra 05994f0
Change the order of the .bind calls in the constructor
kienstra 627eca2
Merge branch 'develop' into add/block-editor-amp-preview
kienstra 1af4466
Merge branch 'develop' into add/block-editor-amp-preview
swissspidy bc1b727
Address ESLint issues
kienstra a2e18e7
Fix an issue where clicking the non-AMP preview button reloads the AM…
kienstra 391b9a0
Merge branch 'develop' of github.com:ampproject/amp-wp into add/block…
westonruter 0b1a5d3
Improve specificity of JS doc
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 hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,318 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { get } from 'lodash'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Component, createRef, renderToString } from '@wordpress/element'; | ||
| import { Icon, IconButton } from '@wordpress/components'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { withSelect, withDispatch } from '@wordpress/data'; | ||
| import { DotTip } from '@wordpress/nux'; | ||
| import { compose } from '@wordpress/compose'; | ||
| import { addQueryArgs } from '@wordpress/url'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import ampBlackIcon from '../../../images/amp-black-icon.svg'; | ||
| import ampFilledIcon from '../../../images/amp-icon.svg'; | ||
| import { isAMPEnabled } from '../helpers'; | ||
| import { POST_PREVIEW_CLASS } from '../constants'; | ||
|
|
||
| /** | ||
| * Writes the message and graphic in the new preview window that was opened. | ||
| * | ||
| * Forked from the Core component <PostPreviewButton>. | ||
| * | ||
| * @see https://github.com/WordPress/gutenberg/blob/95e769df1f82f6b0ef587d81af65dd2f48cd1c38/packages/editor/src/components/post-preview-button/index.js#L17-L93 | ||
| * @param {Document} targetDocument The target document. | ||
| */ | ||
| function writeInterstitialMessage( targetDocument ) { | ||
| let markup = renderToString( | ||
| <div className="editor-post-preview-button__interstitial-message"> | ||
| <Icon | ||
| icon={ ampBlackIcon( { viewBox: '0 0 98 98' } ) } | ||
| /> | ||
| <p> | ||
| { __( 'Generating AMP preview…', 'amp' ) } | ||
| </p> | ||
| </div>, | ||
| ); | ||
|
|
||
| markup += ` | ||
| <style> | ||
| body { | ||
| margin: 0; | ||
| } | ||
| .editor-post-preview-button__interstitial-message { | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| justify-content: center; | ||
| height: 100vh; | ||
| width: 100vw; | ||
| } | ||
| @-webkit-keyframes paint { | ||
| 0% { | ||
| stroke-dashoffset: 0; | ||
| } | ||
| } | ||
| @-moz-keyframes paint { | ||
| 0% { | ||
| stroke-dashoffset: 0; | ||
| } | ||
| } | ||
| @-o-keyframes paint { | ||
| 0% { | ||
| stroke-dashoffset: 0; | ||
| } | ||
| } | ||
| @keyframes paint { | ||
| 0% { | ||
| stroke-dashoffset: 0; | ||
| } | ||
| } | ||
| .editor-post-preview-button__interstitial-message svg { | ||
| width: 198px; | ||
| height: 198px; | ||
| stroke: #555d66; | ||
| stroke-width: 0.75; | ||
| } | ||
| .editor-post-preview-button__interstitial-message svg .outer, | ||
| .editor-post-preview-button__interstitial-message svg .inner { | ||
| stroke-dasharray: 280; | ||
| stroke-dashoffset: 280; | ||
| -webkit-animation: paint 1.5s ease infinite alternate; | ||
| -moz-animation: paint 1.5s ease infinite alternate; | ||
| -o-animation: paint 1.5s ease infinite alternate; | ||
| animation: paint 1.5s ease infinite alternate; | ||
| } | ||
| p { | ||
| text-align: center; | ||
| font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; | ||
| } | ||
| </style> | ||
| `; | ||
|
|
||
| targetDocument.write( markup ); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the markup = applyFilters( 'editor.PostPreview.interstitialMarkup', markup );I decided not to add one here, but feel free to comment if you'd like one. |
||
| targetDocument.title = __( 'Generating AMP preview…', 'amp' ); | ||
| targetDocument.close(); | ||
| } | ||
|
|
||
| /** | ||
| * A 'Preview AMP' button, forked from the Core 'Preview' button: <PostPreviewButton>. | ||
| * | ||
| * Rendered into the DOM with renderPreviewButton() in helpers/index.js. | ||
| * This also moves the (non-AMP) 'Preview' button to before this, if it's not already there. | ||
| * | ||
| * @see https://github.com/WordPress/gutenberg/blob/95e769df1f82f6b0ef587d81af65dd2f48cd1c38/packages/editor/src/components/post-preview-button/index.js#L95-L200 | ||
| */ | ||
| class AMPPreview extends Component { | ||
| /** | ||
| * Constructs the class. | ||
| * | ||
| * @param {*} args Constructor arguments. | ||
| */ | ||
| constructor( ...args ) { | ||
| super( ...args ); | ||
| this.moveButton = this.moveButton.bind( this ); | ||
| this.openPreviewWindow = this.openPreviewWindow.bind( this ); | ||
| this.buttonRef = createRef(); | ||
| } | ||
|
|
||
| /** | ||
| * Called after the component is updated. | ||
| * | ||
| * @param {Object} prevProps The previous props. | ||
| */ | ||
| componentDidUpdate( prevProps ) { | ||
| const { previewLink } = this.props; | ||
|
|
||
| // This relies on the window being responsible to unset itself when | ||
| // navigation occurs or a new preview window is opened, to avoid | ||
| // unintentional forceful redirects. | ||
| if ( previewLink && ! prevProps.previewLink ) { | ||
| this.setPreviewWindowLink( previewLink ); | ||
| } | ||
|
|
||
| this.moveButton(); | ||
| } | ||
|
|
||
| /** | ||
| * Moves the (non-AMP) 'Preview' button to before this 'Preview AMP' button, if it's not there already. | ||
| */ | ||
| moveButton() { | ||
| const buttonWrapper = get( this, [ 'buttonRef', 'current', 'parentNode' ], false ); | ||
| if ( ! buttonWrapper ) { | ||
| return; | ||
| } | ||
|
|
||
| if ( ! buttonWrapper.previousSibling || ! buttonWrapper.previousSibling.classList.contains( POST_PREVIEW_CLASS ) ) { | ||
| const postPreviewButton = document.querySelector( `.${ POST_PREVIEW_CLASS }` ); | ||
| if ( get( postPreviewButton, 'nextSibling' ) ) { | ||
| buttonWrapper.parentNode.insertBefore( buttonWrapper, postPreviewButton.nextSibling ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets the preview window's location to the given URL, if a preview window | ||
| * exists and is not closed. | ||
| * | ||
| * @param {string} url URL to assign as preview window location. | ||
| */ | ||
| setPreviewWindowLink( url ) { | ||
| const { previewWindow } = this; | ||
|
|
||
| if ( previewWindow && ! previewWindow.closed ) { | ||
| previewWindow.location = url; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the window target. | ||
| */ | ||
| getWindowTarget() { | ||
| const { postId } = this.props; | ||
| return `amp-preview-${ postId }`; | ||
| } | ||
|
|
||
| /** | ||
| * Opens the preview window. | ||
| * | ||
| * @param {Event} event The DOM event. | ||
| */ | ||
| openPreviewWindow( event ) { | ||
| // Our Preview button has its 'href' and 'target' set correctly for a11y | ||
| // purposes. Unfortunately, though, we can't rely on the default 'click' | ||
| // handler since sometimes it incorrectly opens a new tab instead of reusing | ||
| // the existing one. | ||
| // https://github.com/WordPress/gutenberg/pull/8330 | ||
| event.preventDefault(); | ||
|
|
||
| // Open up a Preview tab if needed. This is where we'll show the preview. | ||
| if ( ! this.previewWindow || this.previewWindow.closed ) { | ||
| this.previewWindow = window.open( '', this.getWindowTarget() ); | ||
| } | ||
|
|
||
| // Focus the Preview tab. This might not do anything, depending on the browser's | ||
| // and user's preferences. | ||
| // https://html.spec.whatwg.org/multipage/interaction.html#dom-window-focus | ||
| this.previewWindow.focus(); | ||
|
|
||
| // If we don't need to autosave the post before previewing, then we simply | ||
| // load the Preview URL in the Preview tab. | ||
| if ( ! this.props.isAutosaveable ) { | ||
| this.setPreviewWindowLink( event.target.href ); | ||
| return; | ||
| } | ||
|
|
||
| // Request an autosave. This happens asynchronously and causes the component | ||
| // to update when finished. | ||
| if ( this.props.isDraft ) { | ||
| this.props.savePost( { isPreview: true } ); | ||
| } else { | ||
| this.props.autosave( { isPreview: true } ); | ||
| } | ||
|
|
||
| // Display a 'Generating preview' message in the Preview tab while we wait for the | ||
| // autosave to finish. | ||
| writeInterstitialMessage( this.previewWindow.document ); | ||
| } | ||
|
|
||
| /** | ||
| * Renders the component. | ||
| */ | ||
| render() { | ||
| const { previewLink, currentPostLink, errorMessages, isEnabled, isSaveable, isStandardMode } = this.props; | ||
|
|
||
| // Link to the `?preview=true` URL if we have it, since this lets us see | ||
| // changes that were autosaved since the post was last published. Otherwise, | ||
| // just link to the post's URL. | ||
| const href = previewLink || currentPostLink; | ||
|
|
||
| return ( | ||
| isEnabled && ! errorMessages.length && ! isStandardMode && ( | ||
| <IconButton | ||
| icon={ ampFilledIcon( { viewBox: '0 0 62 62' } ) } | ||
| isLarge | ||
| className="amp-editor-post-preview" | ||
| href={ href } | ||
| label={ __( 'Preview AMP', 'amp' ) } | ||
| target={ this.getWindowTarget() } | ||
| disabled={ ! isSaveable } | ||
| onClick={ this.openPreviewWindow } | ||
| ref={ this.buttonRef } | ||
| > | ||
| <span className="screen-reader-text"> | ||
| { | ||
| /* translators: accessibility text */ | ||
| __( '(opens in a new tab)', 'amp' ) | ||
| } | ||
| </span> | ||
| <DotTip tipId="amp/editor.preview"> | ||
| { __( 'Click “Preview” to load a preview of this page in AMP, so you can make sure you are happy with your blocks.', 'amp' ) } | ||
| </DotTip> | ||
| </IconButton> | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| AMPPreview.propTypes = { | ||
| autosave: PropTypes.func.isRequired, | ||
| currentPostLink: PropTypes.string.isRequired, | ||
| postId: PropTypes.number.isRequired, | ||
| previewLink: PropTypes.string, | ||
| isAutosaveable: PropTypes.bool.isRequired, | ||
| isDraft: PropTypes.bool.isRequired, | ||
| isEnabled: PropTypes.bool.isRequired, | ||
| isSaveable: PropTypes.bool.isRequired, | ||
| savePost: PropTypes.func.isRequired, | ||
| errorMessages: PropTypes.array, | ||
| isStandardMode: PropTypes.bool, | ||
| }; | ||
|
|
||
| export default compose( [ | ||
| withSelect( ( select, { forcePreviewLink, forceIsAutosaveable } ) => { | ||
| const { | ||
| getCurrentPostId, | ||
| getCurrentPostAttribute, | ||
| getEditedPostAttribute, | ||
| isEditedPostSaveable, | ||
| isEditedPostAutosaveable, | ||
| getEditedPostPreviewLink, | ||
| } = select( 'core/editor' ); | ||
|
|
||
| const { | ||
| getAmpSlug, | ||
| getErrorMessages, | ||
| isStandardMode, | ||
| } = select( 'amp/block-editor' ); | ||
|
|
||
| const queryArgs = {}; | ||
| queryArgs[ getAmpSlug() ] = 1; | ||
| const initialPreviewLink = getEditedPostPreviewLink(); | ||
| const previewLink = initialPreviewLink ? addQueryArgs( initialPreviewLink, queryArgs ) : undefined; | ||
|
|
||
| return { | ||
| postId: getCurrentPostId(), | ||
| currentPostLink: addQueryArgs( getCurrentPostAttribute( 'link' ), queryArgs ), | ||
| previewLink: forcePreviewLink !== undefined ? forcePreviewLink : previewLink, | ||
| isSaveable: isEditedPostSaveable(), | ||
| isAutosaveable: forceIsAutosaveable || isEditedPostAutosaveable(), | ||
| isDraft: [ 'draft', 'auto-draft' ].indexOf( getEditedPostAttribute( 'status' ) ) !== -1, | ||
| isEnabled: isAMPEnabled(), | ||
| errorMessages: getErrorMessages(), | ||
| isStandardMode: isStandardMode(), | ||
| }; | ||
| } ), | ||
| withDispatch( ( dispatch ) => ( { | ||
| autosave: dispatch( 'core/editor' ).autosave, | ||
| savePost: dispatch( 'core/editor' ).savePost, | ||
| } ) ), | ||
| ] )( AMPPreview ); | ||
This file contains hidden or 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,3 +1,4 @@ | ||
| 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 hidden or 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.
This is a nice touch.