@@ -26,6 +26,7 @@ import {
26
26
BlockIcon ,
27
27
MediaPlaceholder ,
28
28
InspectorControls ,
29
+ RichText ,
29
30
} from '@wordpress/block-editor' ;
30
31
import { Component } from '@wordpress/element' ;
31
32
import { __ , sprintf } from '@wordpress/i18n' ;
@@ -63,6 +64,7 @@ class GalleryEdit extends Component {
63
64
this . onUploadError = this . onUploadError . bind ( this ) ;
64
65
this . setImageAttributes = this . setImageAttributes . bind ( this ) ;
65
66
this . setAttributes = this . setAttributes . bind ( this ) ;
67
+ this . onFocusGalleryCaption = this . onFocusGalleryCaption . bind ( this ) ;
66
68
67
69
this . state = {
68
70
selectedImage : null ,
@@ -198,6 +200,12 @@ class GalleryEdit extends Component {
198
200
return checked ? __ ( 'Thumbnails are cropped to align.' ) : __ ( 'Thumbnails are not cropped.' ) ;
199
201
}
200
202
203
+ onFocusGalleryCaption ( ) {
204
+ this . setState ( {
205
+ selectedImage : null ,
206
+ } ) ;
207
+ }
208
+
201
209
setImageAttributes ( index , attributes ) {
202
210
const { attributes : { images } } = this . props ;
203
211
const { setAttributes } = this ;
@@ -246,10 +254,12 @@ class GalleryEdit extends Component {
246
254
className,
247
255
isSelected,
248
256
noticeUI,
257
+ setAttributes,
249
258
} = this . props ;
250
259
const {
251
260
align,
252
261
columns = defaultColumnsNumber ( attributes ) ,
262
+ caption,
253
263
imageCrop,
254
264
images,
255
265
linkTo,
@@ -283,6 +293,12 @@ class GalleryEdit extends Component {
283
293
return mediaPlaceholder ;
284
294
}
285
295
296
+ const captionClassNames = classnames (
297
+ 'blocks-gallery-caption' ,
298
+ {
299
+ 'screen-reader-text' : ! isSelected && RichText . isEmpty ( caption ) ,
300
+ }
301
+ ) ;
286
302
return (
287
303
< >
288
304
< InspectorControls >
@@ -310,42 +326,52 @@ class GalleryEdit extends Component {
310
326
</ PanelBody >
311
327
</ InspectorControls >
312
328
{ noticeUI }
313
- < ul
314
- className = { classnames (
315
- className ,
316
- {
317
- [ `align${ align } ` ] : align ,
318
- [ `columns-${ columns } ` ] : columns ,
319
- 'is-cropped' : imageCrop ,
320
- }
321
- ) }
329
+ < figure className = { classnames (
330
+ className ,
331
+ {
332
+ [ `align${ align } ` ] : align ,
333
+ [ `columns-${ columns } ` ] : columns ,
334
+ 'is-cropped' : imageCrop ,
335
+ }
336
+ ) }
322
337
>
323
- { images . map ( ( img , index ) => {
338
+ < ul className = "blocks-gallery-grid" >
339
+ { images . map ( ( img , index ) => {
324
340
/* translators: %1$d is the order number of the image, %2$d is the total number of images. */
325
- const ariaLabel = sprintf ( __ ( 'image %1$d of %2$d in gallery' ) , ( index + 1 ) , images . length ) ;
326
-
327
- return (
328
- < li className = "blocks-gallery-item" key = { img . id || img . url } >
329
- < GalleryImage
330
- url = { img . url }
331
- alt = { img . alt }
332
- id = { img . id }
333
- isFirstItem = { index === 0 }
334
- isLastItem = { ( index + 1 ) === images . length }
335
- isSelected = { isSelected && this . state . selectedImage === index }
336
- onMoveBackward = { this . onMoveBackward ( index ) }
337
- onMoveForward = { this . onMoveForward ( index ) }
338
- onRemove = { this . onRemoveImage ( index ) }
339
- onSelect = { this . onSelectImage ( index ) }
340
- setAttributes = { ( attrs ) => this . setImageAttributes ( index , attrs ) }
341
- caption = { img . caption }
342
- aria-label = { ariaLabel }
343
- />
344
- </ li >
345
- ) ;
346
- } ) }
347
- </ ul >
348
- { mediaPlaceholder }
341
+ const ariaLabel = sprintf ( __ ( 'image %1$d of %2$d in gallery' ) , ( index + 1 ) , images . length ) ;
342
+
343
+ return (
344
+ < li className = "blocks-gallery-item" key = { img . id || img . url } >
345
+ < GalleryImage
346
+ url = { img . url }
347
+ alt = { img . alt }
348
+ id = { img . id }
349
+ isFirstItem = { index === 0 }
350
+ isLastItem = { ( index + 1 ) === images . length }
351
+ isSelected = { isSelected && this . state . selectedImage === index }
352
+ onMoveBackward = { this . onMoveBackward ( index ) }
353
+ onMoveForward = { this . onMoveForward ( index ) }
354
+ onRemove = { this . onRemoveImage ( index ) }
355
+ onSelect = { this . onSelectImage ( index ) }
356
+ setAttributes = { ( attrs ) => this . setImageAttributes ( index , attrs ) }
357
+ caption = { img . caption }
358
+ aria-label = { ariaLabel }
359
+ />
360
+ </ li >
361
+ ) ;
362
+ } ) }
363
+ </ ul >
364
+ { mediaPlaceholder }
365
+ < RichText
366
+ tagName = "figcaption"
367
+ className = { captionClassNames }
368
+ placeholder = { __ ( 'Write gallery caption…' ) }
369
+ value = { caption }
370
+ unstableOnFocus = { this . onFocusGalleryCaption }
371
+ onChange = { ( value ) => setAttributes ( { caption : value } ) }
372
+ inlineToolbar
373
+ />
374
+ </ figure >
349
375
</ >
350
376
) ;
351
377
}
0 commit comments