Skip to content
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

Add an 'AMP Preview' button to the block editor #3323

Merged
merged 35 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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 Sep 23, 2019
d86f090
Don't display the 'AMP Preview' button if the 'Enable AMP' toggle isn…
kienstra Sep 23, 2019
75e357c
Address failed PHPUnit test, though I'm not positive this is the righ…
kienstra Sep 23, 2019
7a574ef
Use the AMP slug from amp_get_slug(), apply query var to currentPostLink
kienstra Sep 23, 2019
cc1bedb
Test that the proper values are localized
kienstra Sep 23, 2019
98133f6
Correct the <AMPPreview> propTypes
kienstra Sep 23, 2019
f80dd86
Experiment with manipulating the DOM
kienstra Sep 26, 2019
cdc6e01
Merge branch 'develop' into add/block-editor-amp-preview
kienstra Sep 30, 2019
3459cbf
Try maniplating the DOM to add the 'Preview AMP' button
kienstra Sep 30, 2019
097ba95
Adjust styling of the non-AMP 'Preview' button
kienstra Sep 30, 2019
89bc7e8
Fix an issue where the 'Preview AMP' button was out of place on a new…
kienstra Oct 20, 2019
14df86f
Merge pull request #3392 from ampproject/try/dom-manipulation-amp-pre…
kienstra Oct 21, 2019
e46f3b6
Merge in develop, resolve conflict
kienstra Oct 21, 2019
c02a2d5
Fix failing unit tests
kienstra Oct 21, 2019
1dc42af
Use the 'amp/block-editor' store for the exported data
kienstra Oct 22, 2019
038b078
Move a constant into a function, as it's only used there
kienstra Oct 22, 2019
db963ac
Fix an issue with the icon display in WP 5.3
kienstra Oct 23, 2019
788fb32
Add an xmlns property to the <svg> so the build succeeds
kienstra Oct 26, 2019
32057c7
Add a unit test for the helper renderPreviewButton
kienstra Oct 26, 2019
c67f5b5
Remove extra *,and use a simply one-line comment
kienstra Oct 26, 2019
6631f44
Address failed unit tests by removing 2 expected dependencies
kienstra Oct 26, 2019
885d2f2
Remove styling for the (non-AMP) Preview button
kienstra Oct 26, 2019
e055c4d
Ensure the height of the 'Preview AMP' button matches the non-AMP pre…
kienstra Oct 27, 2019
463d75b
Ensure the height of the 'Preview AMP' button matches the non-AMP pre…
kienstra Oct 27, 2019
32e3e31
Remove an extra call in a unit test to renderPreviewButton()
kienstra Oct 27, 2019
1fc8bb6
Remove extra style rules for 'Preview AMP' button
kienstra Oct 27, 2019
cc2a307
Exit if the non-AMP 'Preview' button has no nextSibling
kienstra Oct 27, 2019
286ff79
Remove a check for whether the component exists already
kienstra Oct 27, 2019
05994f0
Change the order of the .bind calls in the constructor
kienstra Oct 27, 2019
627eca2
Merge branch 'develop' into add/block-editor-amp-preview
kienstra Oct 27, 2019
1af4466
Merge branch 'develop' into add/block-editor-amp-preview
swissspidy Nov 8, 2019
bc1b727
Address ESLint issues
kienstra Nov 8, 2019
a2e18e7
Fix an issue where clicking the non-AMP preview button reloads the AM…
kienstra Nov 8, 2019
391b9a0
Merge branch 'develop' of github.com:ampproject/amp-wp into add/block…
westonruter Nov 11, 2019
0b1a5d3
Improve specificity of JS doc
westonruter Nov 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions assets/src/block-editor/components/amp-black-icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* WordPress dependencies
*/
import { Path, SVG } from '@wordpress/components';

/**
* Forked from AMP-Brand-Black-Icon.svg on amp.dev.
*
* Simplified, including removing the <filter> and <g> tags.
*
* @see https://amp.dev/static/files/brand-material/AMP_Logo_Rebrush.zip
*/
export default (
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 98 98" version="1.1">
<Path className="outer" d="M48 12c19.9 0 36 16.1 36 36S67.9 84 48 84 12 67.9 12 48s16.1-36 36-36" fill="none" />
<Path className="inner" transform="translate(33, 33)" d="M15,0 C23.2845834,0 30,6.71583331 30,15 C30,23.2841667 23.2845834,30 15,30 C6.71583335,30 0,23.2841667 0,15 C0,6.71583331 6.71583335,0 15,0 Z M13.8508333,24.0979167 L20.1429167,13.6258333 C20.2141667,13.5308333 20.26375,13.41875 20.26375,13.29125 C20.26375,12.9766667 20.0091667,12.7220833 19.695,12.7220833 C19.6770834,12.7220833 19.6391667,12.7229167 19.6391667,12.7229167 L16.1308334,12.7270833 L17.28625,5.89291666 L16.1270833,5.88833331 L9.85541665,16.3479167 C9.85541665,16.3479167 9.72958334,16.57625 9.72958334,16.71125 C9.72958334,17.0254167 9.98458332,17.2804167 10.29875,17.2804167 C10.3145833,17.2804167 10.3475,17.2795833 10.3475,17.2795833 L13.8379167,17.2745833 L12.7108333,24.0979167 L13.8508333,24.0979167 Z" fill="none" />
</SVG>
);
283 changes: 283 additions & 0 deletions assets/src/block-editor/components/amp-preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
/**
* External dependencies
*/
import { get } from 'lodash';
import { errorMessages } from 'amp-block-editor-data';
import PropTypes from 'prop-types';

/**
* WordPress dependencies
*/
import { Component, renderToString } from '@wordpress/element';
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { withSelect, withDispatch } from '@wordpress/data';
import { PluginPostStatusInfo } from '@wordpress/edit-post';
import { DotTip } from '@wordpress/nux';
import { ifCondition, compose } from '@wordpress/compose';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
*/
import ampIcon from './amp-black-icon';

/**
* Writes the message and graphic in the new preview window that was opened.
*
* Forked from the Core component <PostPreviewButton>.
* The errorMessages are imported via wp_localize_script().
*
* @see https://github.com/WordPress/gutenberg/blob/95e769df1f82f6b0ef587d81af65dd2f48cd1c38/packages/editor/src/components/post-preview-button/index.js#L17
* @param {Object} targetDocument The target document.
westonruter marked this conversation as resolved.
Show resolved Hide resolved
*/
function writeInterstitialMessage( targetDocument ) {
Copy link
Member

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.

let markup = renderToString(
<div className="editor-post-preview-button__interstitial-message">
{ ampIcon }
<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: 192px;
height: 192px;
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 );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the <PostPreviewButton> in Gutenberg that this is forked from, there's a filter for markup:

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();
}

/**
* Forked from the Core component <PostPreviewButton>.
*
* @see https://github.com/WordPress/gutenberg/blob/95e769df1f82f6b0ef587d81af65dd2f48cd1c38/packages/editor/src/components/post-preview-button/index.js
*/
class AMPPreview extends Component {
/**
* Constructs the class.
*
* @param {*} args Constructor arguments.
*/
constructor( ...args ) {
super( ...args );
this.openPreviewWindow = this.openPreviewWindow.bind( this );
}

/**
* Called after the component 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 );
}
}

/**
* 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 `wp-preview-${ postId }`;
}

/**
* Opens the preview window.
*
* @param {Object} 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 );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this and setPreviewWindowLink are unchanged from core, could the core function not be called directly here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, this function could be:

import { PostPreviewButton } from '@wordpress/editor';

// ...

openPreviewWindow( event ) {
    PostPreviewButton.prototype.openPreviewWindow.call( this, event )
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alas, this doesn't seem to work. It seems compose prevents accessing the underlying functions on the prototype.


/**
* Renders the component.
*/
render() {
const { previewLink, currentPostLink, isSaveable } = 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 (
! errorMessages.length && (
<PluginPostStatusInfo>
<Button
isLarge
className="editor-post-preview"
href={ href }
target={ this.getWindowTarget() }
disabled={ ! isSaveable }
onClick={ this.openPreviewWindow }
>
{ __( 'AMP Preview', 'amp' ) }
<span className="screen-reader-text">
{
/* translators: accessibility text */
__( '(opens in a new tab)', 'amp' )
}
</span>
<DotTip tipId="core/editor.preview">
{ __( 'Click “Preview” to load a preview of this page in AMP, so you can make sure you’re happy with your blocks.', 'amp' ) }
</DotTip>
</Button>
</PluginPostStatusInfo>
)
);
}
}

AMPPreview.propTypes = {
autosave: PropTypes.bool.isRequired,
currentPostLink: PropTypes.func.isRequired,
postId: PropTypes.bool.isRequired,
previewLink: PropTypes.func.isRequired,
isAutosaveable: PropTypes.func.isRequired,
isDraft: PropTypes.func.isRequired,
isSaveable: PropTypes.func.isRequired,
isViewable: PropTypes.func.isRequired,
savePost: PropTypes.bool.isRequired,
};

export default compose( [
withSelect( ( select, { forcePreviewLink, forceIsAutosaveable } ) => {
const {
getCurrentPostId,
getCurrentPostAttribute,
getEditedPostAttribute,
isEditedPostSaveable,
isEditedPostAutosaveable,
getEditedPostPreviewLink,
} = select( 'core/editor' );
const {
getPostType,
} = select( 'core' );

const initialPreviewLink = getEditedPostPreviewLink();
const previewLink = initialPreviewLink ? addQueryArgs( initialPreviewLink, { amp: 1 } ) : undefined;
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
const postType = getPostType( getEditedPostAttribute( 'type' ) );

return {
postId: getCurrentPostId(),
currentPostLink: getCurrentPostAttribute( 'link' ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should get the query var added to it as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that's applied here

previewLink: forcePreviewLink !== undefined ? forcePreviewLink : previewLink,
isSaveable: isEditedPostSaveable(),
isAutosaveable: forceIsAutosaveable || isEditedPostAutosaveable(),
isViewable: get( postType, [ 'viewable' ], false ),
isDraft: [ 'draft', 'auto-draft' ].indexOf( getEditedPostAttribute( 'status' ) ) !== -1,
};
} ),
withDispatch( ( dispatch ) => ( {
autosave: dispatch( 'core/editor' ).autosave,
savePost: dispatch( 'core/editor' ).savePost,
} ) ),
ifCondition( ( { isViewable } ) => isViewable ),
] )( AMPPreview );
1 change: 1 addition & 0 deletions assets/src/block-editor/components/index.js
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';
8 changes: 8 additions & 0 deletions assets/src/block-editor/plugins/amp-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { compose, withInstanceId } from '@wordpress/compose';
* Internal dependencies
*/
import { isAMPEnabled } from '../helpers';
import { AMPPreview } from '../components';

/**
* Adds an 'Enable AMP' toggle to the block editor 'Status & Visibility' section.
Expand Down Expand Up @@ -60,6 +61,13 @@ function AMPToggle( { isEnabled, onChange } ) {
)
}
</PluginPostStatusInfo>
{
isEnabled && (
<PluginPostStatusInfo>
<AMPPreview />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only be displayed of the template mode is not Standard. It's only applicable to Transitional and Reader modes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now only displays if:

isEnabled && ! isStandardMode

</PluginPostStatusInfo>
)
}
</>
);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/php/test-class-amp-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,11 @@ public function test_enqueue_block_assets() {
'wp-element',
'wp-hooks',
'wp-i18n',
'wp-nux',
'wp-plugins',
'wp-polyfill',
'wp-server-side-render',
'wp-url',
],
$block_script->deps
);
Expand Down