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

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented Sep 23, 2019

Update: this has changed, please see more recent comments below

Like Pascal mentioned, this is mainly forked from Gutenberg's PostPreviewButton.

amp-preview-g

Maybe the preview button, or its location, could use improvement:
amp-preview-button

Option 2 looks really good, but I didn't see a way to implement that.

For the preview tab, this uses a fork of an AMP icon from amp.dev:

svg-loading-amp

Closes #2934

Use the plugin pattern in the block-editor/ directory.
Maybe the UI could be improved,
especially the icon.
@googlebot googlebot added the cla: yes Signed the Google CLA label Sep 23, 2019
@kienstra kienstra changed the title Add an 'AMP Preview' button to the block editor [WIP] Add an 'AMP Preview' button to the block editor Sep 23, 2019
…'t selected

There's no need to preview AMP
if it's not enabled.
…t approach

I'm not sure how the tests seemeingly passed before, though they're not passing now.
{
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

assets/src/block-editor/components/amp-preview.js Outdated Show resolved Hide resolved

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

As Weston suggested,
that URL should also have the AMP query var.
As a follow-up to adding 'ampSlug' to
thie wp_localize_script() values,
this tests that the right values are present.
@kienstra kienstra changed the title [WIP] Add an 'AMP Preview' button to the block editor Add an 'AMP Preview' button to the block editor Sep 23, 2019
These were mainly copied,
so apply the correct types.
This still needs more testing.
But so far, it looks like it's working.
It inserts the button into the DOM on domReady,
and in that component's componentDidUpdate(),
it moves it back to the correct position if needed.
This allows the (non-AMP) preview button
to align with the end of the
.edit-post-sidebar.
… post

On creating a new post and making the first edit,
the 'Preview AMP' button appeared out of place.
…view

[WIP] Try manipulating the DOM to add the 'Preview AMP' button
The conflict was in
assets/src/block-editor/index.js
There are 2 tests that failed,
apparently from dependencies not being
added to the expected dependencies.
There's already a method in that for isStandardMode(),
and add 2 new methods.
This addresses a console error from ampBlockEditor
not being defined.
Delete AMP_PREVIEW_BUTTON_WRAPPER_ID,
as there's no need to store it in constants.js.
Simply define it as a const in renderPreviewButton(),
the only function where it's used.
'wp-polyfill',
'wp-url',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are probably from the dependencies in amp-preview.js.

Using that version of Core,
the icons were too small.
@westonruter westonruter dismissed their stale review October 23, 2019 12:11

Clearing my stale review

Before, there was a console error
from an <svg> property.
Add a param to that,
so that it's easier to test.
It'll accept the component that it renders
as a parameter.
This is just and if block,
and it's probably not necessary to have **.
Still, these might be needed,
so I'll have to look at this more.
This was dependent on whether the AMP Preview
button was present, so it should
be good to not need this.
…view

This is needed for WP before 5.3,
or 5.2.4 with Gutenberg active.
Without this fix, the 'Preview AMP' button
can be 1px taller than the non-AMP preview button.
That's only needed at the end of the test.
Also, change a tipId to have 'amp' instead of 'core'.
It looks like these aren't needed.
Also, clarify a comment.
</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.

This inserts the AMP preview button before
that nextSibling,
so it's probably best to exit if it doesn't exist.
The function runs on domReady,
so it should only run 1 time,
and the component shouldn't be rendered into
the DOM when it runs.
@@ -0,0 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg">
Copy link
Contributor Author

@kienstra kienstra Oct 27, 2019

Choose a reason for hiding this comment

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

Copied and modified from an .svg file from the 'Logos' link in the bottom right of https://amp.dev/

The moveButton function comes before
openPreviewWindow in the class,
so move it above it.
@kienstra
Copy link
Contributor Author

Without Gutenberg active

amp-preview-button

With Gutenberg active

amp-preview-button-without-gutenberg

@kienstra
Copy link
Contributor Author

Request For Review

Hi @westonruter and @swissspidy,
Hope you had a great weekend.

Could you please review this when you have a chance?

It adds an AMP preview button, when in Transitional or Reader mode:

previewing-amp-here

The button is forked from the Gutenberg <PostPreviewButton> component, and is rendered into the DOM on domReady.

Thanks, and no pressure if you'd like to focus on PRs needed for 1.4.

@kienstra kienstra requested a review from swissspidy October 27, 2019 23:32
@westonruter westonruter mentioned this pull request Oct 29, 2019
10 tasks
Copy link
Collaborator

@swissspidy swissspidy left a comment

Choose a reason for hiding this comment

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

Generally works fine.

One thing I noticed:

  1. Click on AMP Preview button
  2. AMP Preview is opened
  3. Click on normal Preview button
  4. AMP Preview tab is reloaded, but still shows AMP version

@kienstra
Copy link
Contributor Author

kienstra commented Nov 8, 2019

Ah, good point. I'll work on that.

There was a failed Travis build,
with ESLint issues for the lack
of a trailing comma.
…P preview

As Pascal mentioned,
on opening an AMP Preview,
clicking the non-AMP Preview button
reloads the AMP preview.
And vice versa.
@kienstra
Copy link
Contributor Author

kienstra commented Nov 8, 2019

a2e18e7 fixed the issue of the non-AMP 'Preview' link reloading the AMP preview, at least in my local.

…-editor-amp-preview

* 'develop' of github.com:ampproject/amp-wp: (30 commits)
  Fix identifying sources for validation errors coming child themes (#3708)
  Fix failing E2E tests (#3707)
  Remove amp_validate query var from Validated URL 'View' row action
  Re-factor get_html_attribute_pattern as match_element_attributes
  Quote variables added to regex pattern
  Escape instances of unescapeed output in AMP settings screen code (#3703)
  Replace incorrect usage of esc_url() with esc_url_raw()
  Remove empty alt attributes
  Add object-fit=contain to amp-youtube placeholder image
  Prevent copying empty title/alt in WP<5.2
  Extract HTML attribute parsing logic into base class, and use for YouTube embeds
  Update doc comment for `get_video_id_from_url`
  Merge separate regexes used to retrieve props into one
  Replace ambiguous `$fallback` with `$fallback_for_expected`
  Refactor `get_video_id_from_url` function
  Replace the fallback with an image placeholder for the iframe
  Remove todo
  HTML entity encoding will be handled by the DOM instead
  Bump `oembed` function deprecation version to 1.5.0
  Bump develop to v1.5.0-alpha
  ...
* @see https://github.com/WordPress/gutenberg/blob/95e769df1f82f6b0ef587d81af65dd2f48cd1c38/packages/editor/src/components/post-preview-button/index.js#L17
* @param {Object} targetDocument The target document.
*/
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.

assets/src/block-editor/components/amp-preview.js Outdated Show resolved Hide resolved
Comment on lines 184 to 225
/**
* 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.

@westonruter
Copy link
Member

I'm a bit nervous that the underlying Gutenberg PostPreviewButton will diverge from what has been copied into the plugin here, and that we'll need to maintain it as it evolves (or doesn't) change in core. But this works well now and we'll have to monitor during the 1.5 release cycle to see if any churn causes problems.

@westonruter westonruter added this to the v1.5 milestone Nov 11, 2019
@westonruter westonruter merged commit e9afd58 into develop Nov 11, 2019
@westonruter westonruter deleted the add/block-editor-amp-preview branch November 11, 2019 06:22
@kienstra
Copy link
Contributor Author

That's a good point. This definitely comes with technical debt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How can I preview AMP pages in Gutenberg?
4 participants