-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
99 lines (92 loc) · 2.99 KB
/
index.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
93
94
95
96
97
98
99
/**
* External dependencies
*/
import { connect } from 'react-redux';
import { get } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Button, Spinner, ResponsiveWrapper, withAPIData } from '@wordpress/components';
import { MediaUploadButton } from '@wordpress/blocks';
import { compose } from '@wordpress/element';
/**
* Internal dependencies
*/
import './style.scss';
import { getCurrentPostType, getEditedPostAttribute } from '../../selectors';
import { editPost } from '../../actions';
//used when labels from post tyoe were not yet loaded or when they are not present.
const DEFAULT_SET_FEATURE_IMAGE_LABEL = __( 'Set featured image' );
const DEFAULT_REMOVE_FEATURE_IMAGE_LABEL = __( 'Remove featured image' );
function PostFeaturedImage( { featuredImageId, onUpdateImage, onRemoveImage, media, postType } ) {
const postLabel = get( postType, 'data.labels', {} );
return (
<div className="editor-post-featured-image">
{ !! featuredImageId &&
<MediaUploadButton
title={ postLabel.set_featured_image }
buttonProps={ { className: 'button-link editor-post-featured-image__preview' } }
onSelect={ onUpdateImage }
type="image"
>
{ media && !! media.data &&
<ResponsiveWrapper
naturalWidth={ media.data.media_details.width }
naturalHeight={ media.data.media_details.height }
>
<img src={ media.data.source_url } alt={ __( 'Featured image' ) } />
</ResponsiveWrapper>
}
{ media && media.isLoading && <Spinner /> }
</MediaUploadButton>
}
{ !! featuredImageId && media && ! media.isLoading &&
<p className="editor-post-featured-image__howto">
{ __( 'Click the image to edit or update' ) }
</p>
}
{ ! featuredImageId &&
<MediaUploadButton
title={ postLabel.set_featured_image || DEFAULT_SET_FEATURE_IMAGE_LABEL }
buttonProps={ { className: 'editor-post-featured-image__toggle button-link' } }
onSelect={ onUpdateImage }
type="image"
>
{ postLabel.set_featured_image || DEFAULT_SET_FEATURE_IMAGE_LABEL }
</MediaUploadButton>
}
{ !! featuredImageId &&
<Button className="editor-post-featured-image__toggle button-link" onClick={ onRemoveImage }>
{ postLabel.remove_featured_image || DEFAULT_REMOVE_FEATURE_IMAGE_LABEL }
</Button>
}
</div>
);
}
const applyConnect = connect(
( state ) => {
return {
featuredImageId: getEditedPostAttribute( state, 'featured_media' ),
postTypeName: getCurrentPostType( state ),
};
},
{
onUpdateImage( image ) {
return editPost( { featured_media: image.id } );
},
onRemoveImage() {
return editPost( { featured_media: 0 } );
},
}
);
const applyWithAPIData = withAPIData( ( { featuredImageId, postTypeName } ) => {
return {
media: featuredImageId ? `/wp/v2/media/${ featuredImageId }` : undefined,
postType: postTypeName ? `/wp/v2/types/${ postTypeName }?context=edit` : undefined,
};
} );
export default compose(
applyConnect,
applyWithAPIData,
)( PostFeaturedImage );