-
Notifications
You must be signed in to change notification settings - Fork 381
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
[WIP] Try manipulating the DOM to add the 'Preview AMP' button #3392
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,8 @@ import PropTypes from 'prop-types'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component, renderToString } from '@wordpress/element'; | ||
import { Button } from '@wordpress/components'; | ||
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'; | ||
|
@@ -19,8 +19,10 @@ import { addQueryArgs } from '@wordpress/url'; | |
/** | ||
* Internal dependencies | ||
*/ | ||
import ampIcon from './amp-black-icon'; | ||
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. | ||
|
@@ -34,7 +36,10 @@ import { isAMPEnabled } from '../helpers'; | |
function writeInterstitialMessage( targetDocument ) { | ||
let markup = renderToString( | ||
<div className="editor-post-preview-button__interstitial-message"> | ||
{ ampIcon } | ||
<Icon | ||
icon={ ampBlackIcon } | ||
viewBox="0 0 98 98" | ||
/> | ||
<p>{ __( 'Generating AMP preview…', 'amp' ) }</p> | ||
</div> | ||
); | ||
|
@@ -73,8 +78,8 @@ function writeInterstitialMessage( targetDocument ) { | |
} | ||
} | ||
.editor-post-preview-button__interstitial-message svg { | ||
width: 192px; | ||
height: 192px; | ||
width: 198px; | ||
height: 198px; | ||
stroke: #555d66; | ||
stroke-width: 0.75; | ||
} | ||
|
@@ -100,7 +105,10 @@ function writeInterstitialMessage( targetDocument ) { | |
} | ||
|
||
/** | ||
* Forked from the Core component <PostPreviewButton>. | ||
* 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 | ||
*/ | ||
|
@@ -113,6 +121,8 @@ class AMPPreview extends Component { | |
constructor( ...args ) { | ||
super( ...args ); | ||
this.openPreviewWindow = this.openPreviewWindow.bind( this ); | ||
this.moveButton = this.moveButton.bind( this ); | ||
this.buttonRef = createRef(); | ||
} | ||
|
||
/** | ||
|
@@ -129,6 +139,25 @@ class AMPPreview extends Component { | |
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() { | ||
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. |
||
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 ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -206,18 +235,21 @@ class AMPPreview extends Component { | |
// changes that were autosaved since the post was last published. Otherwise, | ||
// just link to the post's URL. | ||
const href = previewLink || currentPostLink; | ||
const buttonIcon = <Icon viewBox="0 0 62 62" icon={ ampFilledIcon } />; | ||
|
||
return ( | ||
isEnabled && ! errorMessages.length && ! isStandardMode && ( | ||
<Button | ||
<IconButton | ||
icon={ buttonIcon } | ||
isLarge | ||
className="editor-post-preview" | ||
className="amp-editor-post-preview" | ||
href={ href } | ||
label={ __( 'Preview AMP', 'amp' ) } | ||
target={ this.getWindowTarget() } | ||
disabled={ ! isSaveable } | ||
onClick={ this.openPreviewWindow } | ||
ref={ this.buttonRef } | ||
> | ||
{ __( 'AMP Preview', 'amp' ) } | ||
<span className="screen-reader-text"> | ||
{ | ||
/* translators: accessibility text */ | ||
|
@@ -227,7 +259,7 @@ class AMPPreview extends Component { | |
<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> | ||
</IconButton> | ||
) | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ import { select } from '@wordpress/data'; | |
/** | ||
* Internal dependencies | ||
*/ | ||
import { TEXT_BLOCKS, MEDIA_BLOCKS, DEFAULT_HEIGHT, DEFAULT_WIDTH } from '../constants'; | ||
import { AMP_PREVIEW_BUTTON_WRAPPER_ID, TEXT_BLOCKS, MEDIA_BLOCKS, DEFAULT_HEIGHT, DEFAULT_WIDTH, POST_PREVIEW_CLASS } from '../constants'; | ||
import { MIN_FONT_SIZE, MAX_FONT_SIZE } from '../../common/constants'; | ||
import { AMPPreview } from '../components'; | ||
|
||
|
@@ -942,19 +942,22 @@ export const isAMPEnabled = () => { | |
}; | ||
|
||
/** | ||
* Replaces the button to preview the post with one that also previews AMP. | ||
* Renders the 'Preview AMP' button in the DOM right after the (non-AMP) 'Preview' button. | ||
*/ | ||
export const replacePreviewButton = () => { | ||
const postPreviewButton = document.querySelector( '.editor-post-preview' ); | ||
if ( ! postPreviewButton ) { | ||
export const renderPreviewButton = () => { | ||
const postPreviewButton = document.querySelector( `.${ POST_PREVIEW_CLASS }` ); | ||
if ( ! postPreviewButton || document.getElementById( AMP_PREVIEW_BUTTON_WRAPPER_ID ) ) { | ||
return; | ||
} | ||
|
||
const buttonWrapper = document.createElement( 'div' ); | ||
postPreviewButton.parentElement.insertBefore( buttonWrapper, postPreviewButton.nextSibling ); | ||
buttonWrapper.id = AMP_PREVIEW_BUTTON_WRAPPER_ID; | ||
|
||
render( | ||
<AMPPreview />, | ||
buttonWrapper | ||
); | ||
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. |
||
|
||
// Insert this after the (non-AMP) 'Preview' button. | ||
postPreviewButton.parentNode.insertBefore( buttonWrapper, postPreviewButton.nextSibling ); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
/** | ||
* External dependencies | ||
*/ | ||
import { errorMessages, isStandardMode } from 'amp-block-editor-data'; | ||
import { errorMessages } from 'amp-block-editor-data'; | ||
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. @todo: there's a console error related to Uncaught ReferenceError: ampBlockEditor is not defined
at Object.amp-block-editor-data (amp-block-validation.js:5180)
at __webpack_require__ (amp-block-validation.js:20)
at Module../assets/src/block-editor/components/amp-preview.js (amp-block-validation.js:114)
at __webpack_require__ (amp-block-validation.js:20)
at Module../assets/src/block-editor/components/index.js (amp-block-validation.js:471)
at __webpack_require__ (amp-block-validation.js:20)
at Module../assets/src/block-editor/helpers/index.js (amp-block-validation.js:907)
at __webpack_require__ (amp-block-validation.js:20)
at Module../assets/src/block-validation/index.js (amp-block-validation.js:2512)
at __webpack_require__ (amp-block-validation.js:20) |
||
import PropTypes from 'prop-types'; | ||
|
||
/** | ||
|
@@ -19,7 +19,6 @@ 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. | ||
|
@@ -61,13 +60,6 @@ function AMPToggle( { isEnabled, onChange } ) { | |
) | ||
} | ||
</PluginPostStatusInfo> | ||
{ | ||
isEnabled && ! isStandardMode && ( | ||
<PluginPostStatusInfo> | ||
<AMPPreview /> | ||
</PluginPostStatusInfo> | ||
) | ||
} | ||
</> | ||
); | ||
} | ||
|
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.
@todo: this styling for the non-AMP preview button should only apply if there is a 'Preview AMP' button: