Skip to content

Commit

Permalink
Merge pull request #7914 from rtCamp/feature/7896-refactor-amp-previe…
Browse files Browse the repository at this point in the history
…w-button

Relocate AMP Preview Button
  • Loading branch information
westonruter authored Nov 12, 2024
2 parents e9b7e61 + 6844482 commit 2d6d2f6
Show file tree
Hide file tree
Showing 11 changed files with 5,646 additions and 5,531 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import PropTypes from 'prop-types';
/**
* WordPress dependencies
*/
import { Button, Icon, VisuallyHidden } from '@wordpress/components';
import { PluginPreviewMenuItem } from '@wordpress/editor';
import { Icon, VisuallyHidden } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { withDispatch, withSelect } from '@wordpress/data';
import { Component, createRef, renderToString } from '@wordpress/element';
Expand All @@ -16,7 +17,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import { isAMPEnabled } from '../helpers';
import { AMPFilledIcon, AMPBlackIcon } from '../../icons';
import { AMPBlackIcon } from '../../icons';

/**
* Writes the message and graphic in the new preview window that was opened.
Expand Down Expand Up @@ -95,11 +96,10 @@ function writeInterstitialMessage(targetDocument) {
}

/**
* A 'Preview AMP' button, forked from the Core 'Preview' button: <PostPreviewButton>.
* A 'Preview AMP' Menu Item.
*
* @see https://github.com/WordPress/gutenberg/blob/95e769df1f82f6b0ef587d81af65dd2f48cd1c38/packages/editor/src/components/post-preview-button/index.js#L95-L200
*/
class AmpPreviewButton extends Component {
class AmpPreviewMenuItem extends Component {
/**
* Constructs the class.
*
Expand All @@ -108,7 +108,7 @@ class AmpPreviewButton extends Component {
constructor(...args) {
super(...args);

this.buttonRef = createRef();
this.itemRef = createRef();
this.openPreviewWindow = this.openPreviewWindow.bind(this);
}

Expand Down Expand Up @@ -139,8 +139,8 @@ class AmpPreviewButton extends Component {

if (previewWindow && !previewWindow.closed) {
previewWindow.location = url;
if (this.buttonRef.current) {
this.buttonRef.current.focus();
if (this.itemRef.current) {
this.itemRef.current.focus();
}
}
}
Expand All @@ -159,7 +159,7 @@ class AmpPreviewButton extends Component {
* @param {Event} event The DOM event.
*/
openPreviewWindow(event) {
// Our Preview button has its 'href' and 'target' set correctly for a11y
// Our Preview Menu Item 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.
Expand Down Expand Up @@ -208,7 +208,6 @@ class AmpPreviewButton extends Component {
currentPostLink,
errorMessages,
isEnabled,
isSaveable,
isStandardMode,
} = this.props;

Expand All @@ -217,41 +216,44 @@ class AmpPreviewButton extends Component {
// just link to the post's URL.
const href = previewLink || currentPostLink;

if (typeof PluginPreviewMenuItem !== 'function') {
return null;
}

return (
isEnabled &&
!errorMessages.length &&
!isStandardMode && (
<Button
<PluginPreviewMenuItem
className="amp-editor-post-preview"
href={href}
title={__('Preview AMP', 'amp')}
isSecondary
disabled={!isSaveable}
onClick={this.openPreviewWindow}
ref={this.buttonRef}
href={href}
>
<AMPFilledIcon viewBox="0 0 62 62" height={18} width={18} />
{
/* translators: Button label for the AMP preview button */
__('Preview AMP', 'amp')
}
<VisuallyHidden as="span">
{
/* translators: accessibility text */
__('(opens in a new tab)', 'amp')
}
</VisuallyHidden>
</Button>
</PluginPreviewMenuItem>
)
);
}
}

AmpPreviewButton.propTypes = {
AmpPreviewMenuItem.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,
Expand Down Expand Up @@ -308,4 +310,4 @@ export default compose([
autosave: dispatch('core/editor').autosave,
savePost: dispatch('core/editor').savePost,
})),
])(AmpPreviewButton);
])(AmpPreviewMenuItem);
15 changes: 15 additions & 0 deletions assets/src/block-editor/plugins/amp-preview-item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Internal dependencies
*/
import { AMPFilledIcon } from '../../icons';
import AmpPreviewMenuItem from '../components/preview-menu-item';

export const name = 'amp-preview-menu-item';

export const icon = (
<AMPFilledIcon viewBox="0 0 62 62" height={18} width={18} />
);

export const onlyPaired = true;

export const render = AmpPreviewMenuItem;
68 changes: 0 additions & 68 deletions assets/src/block-editor/plugins/wrapped-amp-preview-button.js

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions assets/src/components/loading/test/__snapshots__/loading.js.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2d6d2f6

Please # to comment.