Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gallery Block: Add toolbar button to add a caption #47325

Merged
merged 3 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';
import { compose, usePrevious } from '@wordpress/compose';
import {
BaseControl,
PanelBody,
SelectControl,
ToggleControl,
RangeControl,
Spinner,
ToolbarButton,
} from '@wordpress/components';
import {
store as blockEditorStore,
Expand All @@ -24,14 +25,21 @@ import {
BlockControls,
MediaReplaceFlow,
} from '@wordpress/block-editor';
import { Platform, useEffect, useMemo } from '@wordpress/element';
import {
Platform,
useCallback,
useEffect,
useState,
useMemo,
} from '@wordpress/element';
import { __, _x, sprintf } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { withViewportMatch } from '@wordpress/viewport';
import { View } from '@wordpress/primitives';
import { createBlock } from '@wordpress/blocks';
import { createBlobURL } from '@wordpress/blob';
import { store as noticesStore } from '@wordpress/notices';
import { caption as captionIcon } from '@wordpress/icons';

/**
* Internal dependencies
Expand Down Expand Up @@ -83,9 +91,37 @@ function GalleryEdit( props ) {
clientId,
isSelected,
insertBlocksAfter,
isContentLocked,
} = props;

const { columns, imageCrop, linkTarget, linkTo, sizeSlug } = attributes;
const { columns, imageCrop, linkTarget, linkTo, sizeSlug, caption } =
attributes;
const [ showCaption, setShowCaption ] = useState( !! caption );
const prevCaption = usePrevious( caption );

// We need to show the caption when changes come from
// history navigation(undo/redo).
useEffect( () => {
if ( caption && ! prevCaption ) {
setShowCaption( true );
}
}, [ caption, prevCaption ] );

useEffect( () => {
if ( ! isSelected && ! caption ) {
setShowCaption( false );
}
}, [ isSelected, caption ] );

// Focus the caption when we click to add one.
const captionRef = useCallback(
( node ) => {
if ( node && ! caption ) {
node.focus();
}
},
[ caption ]
);

const {
__unstableMarkNextChangeAsNotPersistent,
Expand Down Expand Up @@ -574,6 +610,25 @@ function GalleryEdit( props ) {
) }
</PanelBody>
</InspectorControls>
<BlockControls group="block">
{ ! isContentLocked && (
<ToolbarButton
onClick={ () => {
setShowCaption( ! showCaption );
if ( showCaption && caption ) {
setAttributes( { caption: undefined } );
}
} }
icon={ captionIcon }
isPressed={ showCaption }
label={
showCaption
? __( 'Remove caption' )
: __( 'Add caption' )
}
/>
) }
</BlockControls>
<BlockControls group="other">
<MediaReplaceFlow
allowedTypes={ ALLOWED_MEDIA_TYPES }
Expand All @@ -596,6 +651,8 @@ function GalleryEdit( props ) {
) }
<Gallery
{ ...props }
showCaption={ showCaption }
ref={ Platform.isWeb ? captionRef : undefined }
images={ images }
mediaPlaceholder={
! hasImages || Platform.isNative
Expand Down
71 changes: 27 additions & 44 deletions packages/block-library/src/gallery/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import {
RichText,
__experimentalGetElementClassName,
} from '@wordpress/block-editor';
import { VisuallyHidden } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { View } from '@wordpress/primitives';
import { forwardRef } from '@wordpress/element';

export const Gallery = ( props ) => {
export const Gallery = ( props, captionRef ) => {
const {
attributes,
isSelected,
Expand All @@ -24,6 +24,7 @@ export const Gallery = ( props ) => {
insertBlocksAfter,
blockProps,
__unstableLayoutClassNames: layoutClassNames,
showCaption,
} = props;

const { align, columns, caption, imageCrop } = attributes;
Expand All @@ -49,50 +50,32 @@ export const Gallery = ( props ) => {
{ mediaPlaceholder }
</View>
) }
<RichTextVisibilityHelper
isHidden={ ! isSelected && RichText.isEmpty( caption ) }
identifier="caption"
tagName="figcaption"
className={ classnames(
'blocks-gallery-caption',
__experimentalGetElementClassName( 'caption' )
{ showCaption &&
( ! RichText.isEmpty( caption ) || isSelected ) && (
<RichText
identifier="caption"
aria-label={ __( 'Gallery caption text' ) }
placeholder={ __( 'Write gallery caption…' ) }
value={ caption }
className={ classnames(
'blocks-gallery-caption',
__experimentalGetElementClassName( 'caption' )
) }
ref={ captionRef }
tagName="figcaption"
onChange={ ( value ) =>
setAttributes( { caption: value } )
}
inlineToolbar
__unstableOnSplitAtEnd={ () =>
insertBlocksAfter(
createBlock( getDefaultBlockName() )
)
}
/>
) }
aria-label={ __( 'Gallery caption text' ) }
placeholder={ __( 'Write gallery caption…' ) }
value={ caption }
onChange={ ( value ) => setAttributes( { caption: value } ) }
inlineToolbar
__unstableOnSplitAtEnd={ () =>
insertBlocksAfter( createBlock( getDefaultBlockName() ) )
}
/>
</figure>
);
};

function RichTextVisibilityHelper( {
isHidden,
className,
value,
placeholder,
tagName,
captionRef,
...richTextProps
} ) {
if ( isHidden ) {
return <VisuallyHidden as={ RichText } { ...richTextProps } />;
}

return (
<RichText
ref={ captionRef }
value={ value }
placeholder={ placeholder }
className={ className }
tagName={ tagName }
{ ...richTextProps }
/>
);
}

export default Gallery;
export default forwardRef( Gallery );
4 changes: 2 additions & 2 deletions test/e2e/specs/editor/blocks/gallery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ test.describe( 'Gallery', () => {

await expect( gallery ).toBeVisible();
await editor.selectBlocks( gallery );
await editor.clickBlockToolbarButton( 'Add caption' );

const caption = gallery.locator(
'role=textbox[name="Gallery caption text"i]'
);
await expect( caption ).toBeVisible();
await caption.click();
await expect( caption ).toBeFocused();
Comment on lines -143 to +144
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pressing the caption button brings focus to the caption textbox automatically.
Therefore, I believe This is more reasonable than tobeVisible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 👍


await page.keyboard.type( galleryCaption );

Expand Down