-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathwrapped-amp-preview-button.js
92 lines (76 loc) · 2.31 KB
/
wrapped-amp-preview-button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* External dependencies
*/
import { get } from 'lodash';
/**
* WordPress dependencies
*/
import { Component, createPortal } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
import { ifCondition, compose, pure } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { POST_PREVIEW_CLASS } from '../constants';
import AmpPreviewButton from '../components/amp-preview-button';
/**
* A wrapper for the AMP preview button that renders it immediately after the 'Post' preview button, when present.
*/
class WrappedAmpPreviewButton extends Component {
/**
* Constructs the class.
*
* @param {*} args Constructor arguments.
*/
constructor( ...args ) {
super( ...args );
this.root = document.createElement( 'div' );
this.root.className = 'amp-wrapper-post-preview';
this.postPreviewButton = document.querySelector( `.${ POST_PREVIEW_CLASS }` );
}
/**
* Invoked immediately after a component is mounted (inserted into the tree).
*/
componentDidMount() {
if ( ! this.postPreviewButton ) {
return;
}
// Insert the AMP preview button immediately after the post preview button.
this.postPreviewButton.parentNode.insertBefore( this.root, this.postPreviewButton.nextSibling );
}
/**
* Invoked immediately before a component is unmounted and destroyed.
*/
componentWillUnmount() {
if ( ! this.postPreviewButton ) {
return;
}
this.postPreviewButton.parentNode.removeChild( this.root );
}
/**
* Renders the component.
*/
render() {
if ( ! this.postPreviewButton ) {
return null;
}
return createPortal( <AmpPreviewButton />, this.root );
}
}
export const name = 'amp-preview-button-wrapper';
export const onlyPaired = true;
export const render = pure(
compose( [
withSelect( ( select ) => {
const { getPostType } = select( 'core' );
const { getEditedPostAttribute } = select( 'core/editor' );
const postType = getPostType( getEditedPostAttribute( 'type' ) );
return {
isViewable: get( postType, [ 'viewable' ], false ),
};
} ),
// This HOC creator renders the component only when the condition is true. At that point the 'Post' preview
// button should have already been rendered (since it also relies on the same condition for rendering).
ifCondition( ( { isViewable } ) => isViewable ),
] )( WrappedAmpPreviewButton ),
);