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 convert old galleries to new format #34606

Merged
merged 5 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
101 changes: 101 additions & 0 deletions packages/block-library/src/gallery/v1/convert-gallery-modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* WordPress dependencies
*/
import { Button, Modal } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import { createBlock } from '@wordpress/blocks';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import {
LINK_DESTINATION_ATTACHMENT,
LINK_DESTINATION_NONE,
LINK_DESTINATION_MEDIA,
} from './constants';

export const convertGallery = ( {
clientId,
getBlock,
replaceBlocks,
} ) => () => {
let link;
const {
attributes: { sizeSlug, linkTo, images, caption },
} = getBlock( clientId );

switch ( linkTo ) {
case 'post':
link = LINK_DESTINATION_ATTACHMENT;
break;
case 'file':
link = LINK_DESTINATION_MEDIA;
break;
default:
link = LINK_DESTINATION_NONE;
break;
}
const innerBlocks = images.map( ( image ) =>
createBlock( 'core/image', {
id: parseInt( image.id, 10 ),
url: image.url,
alt: image.alt,
caption: image.caption,
linkDestination: link,
} )
);

replaceBlocks(
clientId,
createBlock(
'core/gallery',
{ sizeSlug, linkTo: link, caption },
innerBlocks
)
);
};

export default function ConvertGalleryModal( { onClose, clientId } ) {
const { getBlock } = useSelect( blockEditorStore );
const { replaceBlocks } = useDispatch( blockEditorStore );
return (
<Modal
closeLabel={ __( 'Close' ) }
onRequestClose={ onClose }
title={ __( 'Convert to new gallery format' ) }
className={ 'wp-block-convert-gallery-modal' }
aria={ {
describedby: 'wp-block-convert-gallery-modal__description',
} }
>
<p id={ 'wp-block-convert-gallery-modal__description' }>
{ __(
'You can convert this gallery block to a new format which provides more flexibility, like adding custom links, or custom styles, to individual images.'
) }
</p>
<p id={ 'wp-block-convert-gallery-modal__description' }>
andrewserong marked this conversation as resolved.
Show resolved Hide resolved
{ __(
'There is no option to convert it back to the old format, so if you do not like the new format once converted just leave the post/page without saving.'
) }
</p>
<div className="wp-block-convert-gallery-modal-buttons">
<Button isTertiary onClick={ onClose }>
{ __( 'Cancel' ) }
</Button>
<Button
isPrimary
onClick={ convertGallery( {
replaceBlocks,
getBlock,
clientId,
createBlock,
} ) }
>
{ __( 'Convert' ) }
</Button>
</div>
</Modal>
);
}
27 changes: 27 additions & 0 deletions packages/block-library/src/gallery/v1/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import {
ToggleControl,
withNotices,
RangeControl,
ToolbarButton,
} from '@wordpress/components';
import {
BlockControls,
MediaPlaceholder,
InspectorControls,
useBlockProps,
Expand All @@ -51,6 +53,7 @@ import {
LINK_DESTINATION_MEDIA,
LINK_DESTINATION_NONE,
} from './constants';
import ConvertGalleryModal from './convert-gallery-modal';

const MAX_COLUMNS = 8;
const linkOptions = [
Expand Down Expand Up @@ -99,10 +102,13 @@ function GalleryEdit( props ) {
mediaUpload,
getMedia,
wasBlockJustInserted,
__unstableGalleryWithImageBlocks,
} = useSelect( ( select ) => {
const settings = select( blockEditorStore ).getSettings();

return {
__unstableGalleryWithImageBlocks:
settings.__unstableGalleryWithImageBlocks,
imageSizes: settings.imageSizes,
mediaUpload: settings.mediaUpload,
getMedia: select( coreStore ).getMedia,
Expand Down Expand Up @@ -408,6 +414,10 @@ function GalleryEdit( props ) {
/>
);

const [ isConvertOpen, setConvertOpen ] = useState( false );
const openConvertModal = () => setConvertOpen( true );
const closeConvertModal = () => setConvertOpen( false );

const blockProps = useBlockProps();

if ( ! hasImages ) {
Expand Down Expand Up @@ -456,6 +466,23 @@ function GalleryEdit( props ) {
) }
</PanelBody>
</InspectorControls>
{ __unstableGalleryWithImageBlocks && (
<BlockControls group="block">
<ToolbarButton
onClick={ openConvertModal }
title={ __( 'Convert' ) }
label={ __( 'Convert to new gallery format' ) }
>
{ __( 'Convert' ) }
</ToolbarButton>
</BlockControls>
) }
{ isConvertOpen && (
<ConvertGalleryModal
onClose={ closeConvertModal }
clientId={ clientId }
/>
) }
{ noticeUI }
<Gallery
{ ...props }
Expand Down