Skip to content

Commit 203d057

Browse files
authored
Merge pull request #6881 from ampproject/update/gutenberg-v12.5.0-packages
Update Gutenberg packages after v12.5.0 release
2 parents 36edf69 + 15c713c commit 203d057

File tree

31 files changed

+6216
-4883
lines changed

31 files changed

+6216
-4883
lines changed

assets/src/admin/amp-plugin-install.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
/**
2-
* WordPress dependencies
3-
*/
4-
import domReady from '@wordpress/dom-ready';
5-
import { __ } from '@wordpress/i18n';
6-
71
/**
82
* External dependencies
93
*/
104
import { AMP_PLUGINS, AMP_COMPATIBLE } from 'amp-plugins'; // From WP inline script.
115
import { debounce } from 'lodash';
126

7+
/**
8+
* WordPress dependencies
9+
*/
10+
import domReady from '@wordpress/dom-ready';
11+
import { __ } from '@wordpress/i18n';
12+
1313
const ampPluginInstall = {
1414

1515
/**

assets/src/admin/amp-theme-install.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
* WordPress dependencies
2+
* External dependencies
33
*/
4-
import { __ } from '@wordpress/i18n';
5-
import domReady from '@wordpress/dom-ready';
4+
import { AMP_COMPATIBLE } from 'amp-themes'; // From WP inline script.
65

76
/**
8-
* External dependencies
7+
* WordPress dependencies
98
*/
10-
import { AMP_COMPATIBLE } from 'amp-themes'; // From WP inline script.
9+
import { __ } from '@wordpress/i18n';
10+
import domReady from '@wordpress/dom-ready';
1111

1212
/**
1313
* Internal dependencies

assets/src/admin/site-scan-notice/notice.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/**
2-
* WordPress dependencies
3-
*/
4-
import { useContext, useEffect } from '@wordpress/element';
5-
import { __ } from '@wordpress/i18n';
6-
71
/**
82
* External dependencies
93
*/
@@ -12,6 +6,12 @@ import {
126
SETTINGS_LINK,
137
} from 'amp-site-scan-notice'; // From WP inline script.
148

9+
/**
10+
* WordPress dependencies
11+
*/
12+
import { useContext, useEffect } from '@wordpress/element';
13+
import { __ } from '@wordpress/i18n';
14+
1515
/**
1616
* Internal dependencies
1717
*/

assets/src/admin/site-scan-notice/plugins-with-amp-incompatibility.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
* WordPress dependencies
2+
* External dependencies
33
*/
4-
import { useContext } from '@wordpress/element';
5-
import { _n, sprintf } from '@wordpress/i18n';
4+
import PropTypes from 'prop-types';
65

76
/**
8-
* External dependencies
7+
* WordPress dependencies
98
*/
10-
import PropTypes from 'prop-types';
9+
import { useContext } from '@wordpress/element';
10+
import { _n, sprintf } from '@wordpress/i18n';
1111

1212
/**
1313
* Internal dependencies

assets/src/admin/theme-install/view/theme.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* WordPress dependencies
2+
* External dependencies
33
*/
4-
import { __, sprintf } from '@wordpress/i18n';
4+
import { AMP_THEMES, AMP_COMPATIBLE, NONE_WPORG_THEMES } from 'amp-themes'; // From WP inline script.
55

66
/**
7-
* External dependencies
7+
* WordPress dependencies
88
*/
9-
import { AMP_THEMES, AMP_COMPATIBLE, NONE_WPORG_THEMES } from 'amp-themes'; // From WP inline script.
9+
import { __, sprintf } from '@wordpress/i18n';
1010

1111
const wpThemeView = wp.themes.view.Theme;
1212

assets/src/block-editor/constants.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@ export const TEXT_BLOCKS = [
55
'core/quote',
66
'core/subhead',
77
];
8-
9-
export const POST_PREVIEW_CLASS = 'editor-post-preview';
Lines changed: 47 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,69 @@
1-
/**
2-
* External dependencies
3-
*/
4-
import { get } from 'lodash';
5-
61
/**
72
* WordPress dependencies
83
*/
9-
import { Component, createPortal } from '@wordpress/element';
10-
import { withSelect } from '@wordpress/data';
11-
import { ifCondition, compose, pure } from '@wordpress/compose';
4+
import { createPortal, useEffect, useRef } from '@wordpress/element';
5+
import { useSelect } from '@wordpress/data';
126

137
/**
148
* Internal dependencies
159
*/
16-
import { POST_PREVIEW_CLASS } from '../constants';
1710
import AmpPreviewButton from '../components/amp-preview-button';
1811

1912
/**
2013
* A wrapper for the AMP preview button that renders it immediately after the 'Post' preview button, when present.
2114
*/
22-
class WrappedAmpPreviewButton extends Component {
23-
/**
24-
* Constructs the class.
25-
*
26-
* @param {*} args Constructor arguments.
27-
*/
28-
constructor( ...args ) {
29-
super( ...args );
30-
31-
this.root = document.createElement( 'div' );
32-
this.root.className = 'amp-wrapper-post-preview';
33-
34-
this.postPreviewButton = document.querySelector( `.${ POST_PREVIEW_CLASS }` );
35-
}
36-
37-
/**
38-
* Invoked immediately after a component is mounted (inserted into the tree).
39-
*/
40-
componentDidMount() {
41-
if ( ! this.postPreviewButton ) {
42-
return;
43-
}
44-
45-
// Insert the AMP preview button immediately after the post preview button.
46-
this.postPreviewButton.parentNode.insertBefore( this.root, this.postPreviewButton.nextSibling );
47-
}
48-
49-
/**
50-
* Invoked immediately before a component is unmounted and destroyed.
51-
*/
52-
componentWillUnmount() {
53-
if ( ! this.postPreviewButton ) {
54-
return;
15+
function WrappedAmpPreviewButton() {
16+
const root = useRef( null );
17+
const referenceNode = useRef( null );
18+
19+
const isViewable = useSelect(
20+
( select ) => select( 'core' ).getPostType( select( 'core/editor' ).getEditedPostAttribute( 'type' ) )?.viewable,
21+
[],
22+
);
23+
24+
useEffect( () => {
25+
if ( ! root.current && ! referenceNode.current ) {
26+
// At first, we try finding the post preview button that is visible only on small screens.
27+
// If found, we will use its next sibling so that `insertBefore` gets us to the exact location
28+
// we are looking for.
29+
referenceNode.current = document.querySelector( '.editor-post-preview' )?.nextSibling;
30+
31+
// Since the mobile post preview button is rendered with a delay, we are using the post publish/update
32+
// button as a fallback. Because it is rendered early, our AMP preview button will be visible immediately.
33+
if ( ! referenceNode.current ) {
34+
referenceNode.current = document.querySelector( '.editor-post-publish-button' );
35+
}
36+
37+
if ( referenceNode.current ) {
38+
root.current = document.createElement( 'div' );
39+
root.current.className = 'amp-wrapper-post-preview';
40+
referenceNode.current.parentNode.insertBefore( root.current, referenceNode.current );
41+
}
5542
}
5643

57-
this.postPreviewButton.parentNode.removeChild( this.root );
44+
return () => {
45+
if ( referenceNode.current && root.current ) {
46+
referenceNode.current.parentNode.removeChild( root.current );
47+
root.current = null;
48+
referenceNode.current = null;
49+
}
50+
};
51+
// We use `isViewable` as a dependency in order to reposition the preview button once the block editor is fully loaded.
52+
}, [ isViewable ] );
53+
54+
// It is unlikely that AMP would be enabled for a non-viewable post type. This is why the Preview button will
55+
// always be displayed initially (when `isViewable` is undefined), preventing horizontal layout shift.
56+
// Once the `isViewable` value is defined (which is after the initial block editor load) and it is `false`,
57+
// the Preview button will be hidden causing a minor layout shift.
58+
if ( ! root.current || isViewable === false ) {
59+
return null;
5860
}
5961

60-
/**
61-
* Renders the component.
62-
*/
63-
render() {
64-
if ( ! this.postPreviewButton ) {
65-
return null;
66-
}
67-
68-
return createPortal( <AmpPreviewButton />, this.root );
69-
}
62+
return createPortal( <AmpPreviewButton />, root.current );
7063
}
7164

7265
export const name = 'amp-preview-button-wrapper';
7366

7467
export const onlyPaired = true;
7568

76-
export const render = pure(
77-
compose( [
78-
withSelect( ( select ) => {
79-
const { getPostType } = select( 'core' );
80-
const { getEditedPostAttribute } = select( 'core/editor' );
81-
82-
const postType = getPostType( getEditedPostAttribute( 'type' ) );
83-
84-
return {
85-
isViewable: get( postType, [ 'viewable' ], false ),
86-
};
87-
} ),
88-
// This HOC creator renders the component only when the condition is true. At that point the 'Post' preview
89-
// button should have already been rendered (since it also relies on the same condition for rendering).
90-
ifCondition( ( { isViewable } ) => isViewable ),
91-
] )( WrappedAmpPreviewButton ),
92-
);
69+
export const render = WrappedAmpPreviewButton;

0 commit comments

Comments
 (0)