-
Notifications
You must be signed in to change notification settings - Fork 385
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
Conversation
Use the plugin pattern in the block-editor/ directory. Maybe the UI could be improved, especially the icon.
…'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 /> |
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 should only be displayed of the template mode is not Standard. It's only applicable to Transitional and Reader modes.
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.
Ah, right.
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 now only displays if:
isEnabled && ! isStandardMode
|
||
return { | ||
postId: getCurrentPostId(), | ||
currentPostLink: getCurrentPostAttribute( 'link' ), |
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 should get the query var added to it as well.
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.
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.
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', |
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.
These are probably from the dependencies in amp-preview.js.
Using that version of Core, the icons were too small.
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 ); |
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.
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"> |
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.
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.
Request For Review Hi @westonruter and @swissspidy, Could you please review this when you have a chance? It adds an AMP preview button, when in Transitional or Reader mode: The button is forked from the Gutenberg Thanks, and no pressure if you'd like to focus on PRs needed for 1.4. |
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.
Generally works fine.
One thing I noticed:
- Click on AMP Preview button
- AMP Preview is opened
- Click on normal Preview button
- AMP Preview tab is reloaded, but still shows AMP version
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.
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 ) { |
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.
/** | ||
* 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 ); | ||
} |
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.
Since this and setPreviewWindowLink
are unchanged from core, could the core function not be called directly here?
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.
For example, this function could be:
import { PostPreviewButton } from '@wordpress/editor';
// ...
openPreviewWindow( event ) {
PostPreviewButton.prototype.openPreviewWindow.call( this, event )
}
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.
Alas, this doesn't seem to work. It seems compose
prevents accessing the underlying functions on the prototype.
I'm a bit nervous that the underlying Gutenberg |
That's a good point. This definitely comes with technical debt. |
Update: this has changed, please see more recent comments below
Like Pascal mentioned, this is mainly forked from Gutenberg's PostPreviewButton.
Maybe the preview button, or its location, could use improvement:
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:
Closes #2934