-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
313 lines (297 loc) · 8.94 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { applyFilters } from '@wordpress/hooks';
import {
DropZone,
Button,
Spinner,
withNotices,
withFilters,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { isBlobURL } from '@wordpress/blob';
import { useState, useRef } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { useSelect, withDispatch, withSelect } from '@wordpress/data';
import {
MediaUpload,
MediaUploadCheck,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import PostFeaturedImageCheck from './check';
import { store as editorStore } from '../../store';
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 = __( 'Add a featured image' );
const instructions = (
<p>
{ __(
'To edit the featured image, you need permission to upload media.'
) }
</p>
);
function getMediaDetails( media, postId ) {
if ( ! media ) {
return {};
}
const defaultSize = applyFilters(
'editor.PostFeaturedImage.imageSize',
'large',
media.id,
postId
);
if ( defaultSize in ( media?.media_details?.sizes ?? {} ) ) {
return {
mediaWidth: media.media_details.sizes[ defaultSize ].width,
mediaHeight: media.media_details.sizes[ defaultSize ].height,
mediaSourceUrl: media.media_details.sizes[ defaultSize ].source_url,
};
}
// Use fallbackSize when defaultSize is not available.
const fallbackSize = applyFilters(
'editor.PostFeaturedImage.imageSize',
'thumbnail',
media.id,
postId
);
if ( fallbackSize in ( media?.media_details?.sizes ?? {} ) ) {
return {
mediaWidth: media.media_details.sizes[ fallbackSize ].width,
mediaHeight: media.media_details.sizes[ fallbackSize ].height,
mediaSourceUrl:
media.media_details.sizes[ fallbackSize ].source_url,
};
}
// Use full image size when fallbackSize and defaultSize are not available.
return {
mediaWidth: media.media_details.width,
mediaHeight: media.media_details.height,
mediaSourceUrl: media.source_url,
};
}
function PostFeaturedImage( {
currentPostId,
featuredImageId,
onUpdateImage,
onRemoveImage,
media,
postType,
noticeUI,
noticeOperations,
} ) {
const toggleRef = useRef();
const [ isLoading, setIsLoading ] = useState( false );
const { getSettings } = useSelect( blockEditorStore );
const { mediaSourceUrl } = getMediaDetails( media, currentPostId );
function onDropFiles( filesList ) {
getSettings().mediaUpload( {
allowedTypes: ALLOWED_MEDIA_TYPES,
filesList,
onFileChange( [ image ] ) {
if ( isBlobURL( image?.url ) ) {
setIsLoading( true );
return;
}
if ( image ) {
onUpdateImage( image );
}
setIsLoading( false );
},
onError( message ) {
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice( message );
},
} );
}
/**
* Generates the featured image alt text for this editing context.
*
* @param {Object} imageMedia The image media object.
* @param {string} imageMedia.alt_text The alternative text of the image.
* @param {Object} imageMedia.media_details The media details of the image.
* @param {Object} imageMedia.media_details.sizes The sizes of the image.
* @param {Object} imageMedia.media_details.sizes.full The full size details of the image.
* @param {string} imageMedia.media_details.sizes.full.file The file name of the full size image.
* @param {string} imageMedia.slug The slug of the image.
* @return {string} The featured image alt text.
*/
function getImageDescription( imageMedia ) {
if ( imageMedia.alt_text ) {
return sprintf(
// Translators: %s: The selected image alt text.
__( 'Current image: %s' ),
imageMedia.alt_text
);
}
return sprintf(
// Translators: %s: The selected image filename.
__(
'The current image has no alternative text. The file name is: %s'
),
imageMedia.media_details.sizes?.full?.file || imageMedia.slug
);
}
return (
<PostFeaturedImageCheck>
{ noticeUI }
<div className="editor-post-featured-image">
{ media && (
<div
id={ `editor-post-featured-image-${ featuredImageId }-describedby` }
className="hidden"
>
{ getImageDescription( media ) }
</div>
) }
<MediaUploadCheck fallback={ instructions }>
<MediaUpload
title={
postType?.labels?.featured_image ||
DEFAULT_FEATURE_IMAGE_LABEL
}
onSelect={ onUpdateImage }
unstableFeaturedImageFlow
allowedTypes={ ALLOWED_MEDIA_TYPES }
modalClass="editor-post-featured-image__media-modal"
render={ ( { open } ) => (
<div className="editor-post-featured-image__container">
<Button
__next40pxDefaultSize
ref={ toggleRef }
className={
! featuredImageId
? 'editor-post-featured-image__toggle'
: 'editor-post-featured-image__preview'
}
onClick={ open }
aria-label={
! featuredImageId
? null
: __(
'Edit or replace the featured image'
)
}
aria-describedby={
! featuredImageId
? null
: `editor-post-featured-image-${ featuredImageId }-describedby`
}
aria-haspopup="dialog"
disabled={ isLoading }
accessibleWhenDisabled
>
{ !! featuredImageId && media && (
<img
className="editor-post-featured-image__preview-image"
src={ mediaSourceUrl }
alt={ getImageDescription( media ) }
/>
) }
{ isLoading && <Spinner /> }
{ ! featuredImageId &&
! isLoading &&
( postType?.labels
?.set_featured_image ||
DEFAULT_SET_FEATURE_IMAGE_LABEL ) }
</Button>
{ !! featuredImageId && (
<HStack className="editor-post-featured-image__actions">
<Button
__next40pxDefaultSize
className="editor-post-featured-image__action"
onClick={ open }
aria-haspopup="dialog"
>
{ __( 'Replace' ) }
</Button>
<Button
__next40pxDefaultSize
className="editor-post-featured-image__action"
onClick={ () => {
onRemoveImage();
toggleRef.current.focus();
} }
>
{ __( 'Remove' ) }
</Button>
</HStack>
) }
<DropZone onFilesDrop={ onDropFiles } />
</div>
) }
value={ featuredImageId }
/>
</MediaUploadCheck>
</div>
</PostFeaturedImageCheck>
);
}
const applyWithSelect = withSelect( ( select ) => {
const { getMedia, getPostType } = select( coreStore );
const { getCurrentPostId, getEditedPostAttribute } = select( editorStore );
const featuredImageId = getEditedPostAttribute( 'featured_media' );
return {
media: featuredImageId
? getMedia( featuredImageId, { context: 'view' } )
: null,
currentPostId: getCurrentPostId(),
postType: getPostType( getEditedPostAttribute( 'type' ) ),
featuredImageId,
};
} );
const applyWithDispatch = withDispatch(
( dispatch, { noticeOperations }, { select } ) => {
const { editPost } = dispatch( editorStore );
return {
onUpdateImage( image ) {
editPost( { featured_media: image.id } );
},
onDropImage( filesList ) {
select( blockEditorStore )
.getSettings()
.mediaUpload( {
allowedTypes: [ 'image' ],
filesList,
onFileChange( [ image ] ) {
editPost( { featured_media: image.id } );
},
onError( message ) {
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice( message );
},
} );
},
onRemoveImage() {
editPost( { featured_media: 0 } );
},
};
}
);
/**
* Renders the component for managing the featured image of a post.
*
* @param {Object} props Props.
* @param {number} props.currentPostId ID of the current post.
* @param {number} props.featuredImageId ID of the featured image.
* @param {Function} props.onUpdateImage Function to call when the image is updated.
* @param {Function} props.onRemoveImage Function to call when the image is removed.
* @param {Object} props.media The media object representing the featured image.
* @param {string} props.postType Post type.
* @param {Element} props.noticeUI UI for displaying notices.
* @param {Object} props.noticeOperations Operations for managing notices.
*
* @return {Element} Component to be rendered .
*/
export default compose(
withNotices,
applyWithSelect,
applyWithDispatch,
withFilters( 'editor.PostFeaturedImage' )
)( PostFeaturedImage );