-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
index.js
146 lines (135 loc) · 4.78 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* External dependencies
*/
import { has, get } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { applyFilters } from '@wordpress/hooks';
import { Button, Spinner, ResponsiveWrapper, withFilters } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { MediaUpload, MediaUploadCheck } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import PostFeaturedImageCheck from './check';
const ALLOWED_MEDIA_TYPES = [ 'image' ];
// Used when labels from post type were not yet loaded or when they are not present.
const DEFAULT_FEATURE_IMAGE_LABEL = __( 'Featured Image' );
const DEFAULT_SET_FEATURE_IMAGE_LABEL = __( 'Set featured image' );
const DEFAULT_REMOVE_FEATURE_IMAGE_LABEL = __( 'Remove image' );
function PostFeaturedImage( { currentPostId, featuredImageId, onUpdateImage, onRemoveImage, media, postType } ) {
const postLabel = get( postType, [ 'labels' ], {} );
const instructions = <p>{ __( 'To edit the featured image, you need permission to upload media.' ) }</p>;
let mediaWidth, mediaHeight, mediaSourceUrl;
if ( media ) {
const mediaSize = applyFilters( 'editor.PostFeaturedImage.imageSize', 'post-thumbnail', media.id, currentPostId );
if ( has( media, [ 'media_details', 'sizes', mediaSize ] ) ) {
mediaWidth = media.media_details.sizes[ mediaSize ].width;
mediaHeight = media.media_details.sizes[ mediaSize ].height;
mediaSourceUrl = media.media_details.sizes[ mediaSize ].source_url;
} else {
mediaWidth = media.media_details.width;
mediaHeight = media.media_details.height;
mediaSourceUrl = media.source_url;
}
}
return (
<PostFeaturedImageCheck>
<div className="editor-post-featured-image">
{ !! featuredImageId &&
<MediaUploadCheck fallback={ instructions }>
<MediaUpload
title={ postLabel.featured_image || DEFAULT_FEATURE_IMAGE_LABEL }
onSelect={ onUpdateImage }
allowedTypes={ ALLOWED_MEDIA_TYPES }
modalClass="editor-post-featured-image__media-modal"
render={ ( { open } ) => (
<Button className="editor-post-featured-image__preview" onClick={ open } aria-label={ __( 'Edit or update the image' ) }>
{ media &&
<ResponsiveWrapper
naturalWidth={ mediaWidth }
naturalHeight={ mediaHeight }
>
<img src={ mediaSourceUrl } alt="" />
</ResponsiveWrapper>
}
{ ! media && <Spinner /> }
</Button>
) }
value={ featuredImageId }
/>
</MediaUploadCheck>
}
{ !! featuredImageId && media && ! media.isLoading &&
<MediaUploadCheck>
<MediaUpload
title={ postLabel.featured_image || DEFAULT_FEATURE_IMAGE_LABEL }
onSelect={ onUpdateImage }
allowedTypes={ ALLOWED_MEDIA_TYPES }
modalClass="editor-post-featured-image__media-modal"
render={ ( { open } ) => (
<Button onClick={ open } isDefault isLarge>
{ __( 'Replace image' ) }
</Button>
) }
/>
</MediaUploadCheck>
}
{ ! featuredImageId &&
<div>
<MediaUploadCheck fallback={ instructions }>
<MediaUpload
title={ postLabel.featured_image || DEFAULT_FEATURE_IMAGE_LABEL }
onSelect={ onUpdateImage }
allowedTypes={ ALLOWED_MEDIA_TYPES }
modalClass="editor-post-featured-image__media-modal"
render={ ( { open } ) => (
<Button className="editor-post-featured-image__toggle" onClick={ open }>
{ postLabel.set_featured_image || DEFAULT_SET_FEATURE_IMAGE_LABEL }
</Button>
) }
/>
</MediaUploadCheck>
</div>
}
{ !! featuredImageId &&
<MediaUploadCheck>
<Button onClick={ onRemoveImage } isLink isDestructive>
{ postLabel.remove_featured_image || DEFAULT_REMOVE_FEATURE_IMAGE_LABEL }
</Button>
</MediaUploadCheck>
}
</div>
</PostFeaturedImageCheck>
);
}
const applyWithSelect = withSelect( ( select ) => {
const { getMedia, getPostType } = select( 'core' );
const { getCurrentPostId, getEditedPostAttribute } = select( 'core/editor' );
const featuredImageId = getEditedPostAttribute( 'featured_media' );
return {
media: featuredImageId ? getMedia( featuredImageId ) : null,
currentPostId: getCurrentPostId(),
postType: getPostType( getEditedPostAttribute( 'type' ) ),
featuredImageId,
};
} );
const applyWithDispatch = withDispatch( ( dispatch ) => {
const { editPost } = dispatch( 'core/editor' );
return {
onUpdateImage( image ) {
editPost( { featured_media: image.id } );
},
onRemoveImage() {
editPost( { featured_media: 0 } );
},
};
} );
export default compose(
applyWithSelect,
applyWithDispatch,
withFilters( 'editor.PostFeaturedImage' ),
)( PostFeaturedImage );