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 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
12 changes: 12 additions & 0 deletions packages/block-library/src/gallery/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,15 @@ figure.wp-block-gallery {
// Some themes give all <ul> default margin instead of padding.
margin: 0;
}

.wp-block-update-gallery-modal {
max-width: 400px;
.wp-block-update-gallery-modal-buttons {
display: flex;
justify-content: flex-end;

.components-button {
margin-left: $grid-unit-15;
}
}
}
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 UpdateGalleryModal from './update-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 [ isUpdateOpen, setUpdateOpen ] = useState( false );
const openUpdateModal = () => setUpdateOpen( true );
const closeUpdateModal = () => setUpdateOpen( false );

const blockProps = useBlockProps();

if ( ! hasImages ) {
Expand Down Expand Up @@ -456,6 +466,23 @@ function GalleryEdit( props ) {
) }
</PanelBody>
</InspectorControls>
{ __unstableGalleryWithImageBlocks && (
<BlockControls group="other">
<ToolbarButton
onClick={ openUpdateModal }
title={ __( 'Update' ) }
label={ __( 'Update to the new gallery format' ) }
>
{ __( 'Update' ) }
</ToolbarButton>
</BlockControls>
) }
{ isUpdateOpen && (
<UpdateGalleryModal
onClose={ closeUpdateModal }
clientId={ clientId }
/>
) }
{ noticeUI }
<Gallery
{ ...props }
Expand Down
97 changes: 97 additions & 0 deletions packages/block-library/src/gallery/v1/update-gallery-modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* 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 updateGallery = ( {
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 UpdateGalleryModal( { onClose, clientId } ) {
const { getBlock } = useSelect( blockEditorStore );
const { replaceBlocks } = useDispatch( blockEditorStore );
return (
<Modal
closeLabel={ __( 'Close' ) }
onRequestClose={ onClose }
title={ __( 'Update gallery' ) }
className={ 'wp-block-update-gallery-modal' }
aria={ {
describedby: 'wp-block-update-gallery-modal__description',
} }
>
<p id={ 'wp-block-update-gallery-modal__description' }>
{ __(
'Updating to the new format adds the ability to use custom links or styles on individual images in the gallery, and makes it easier to add or move them around.'
) }
</p>

<div className="wp-block-update-gallery-modal-buttons">
<Button isTertiary onClick={ onClose }>
{ __( 'Cancel' ) }
</Button>
<Button
isPrimary
onClick={ updateGallery( {
replaceBlocks,
getBlock,
clientId,
createBlock,
} ) }
>
{ __( 'Update' ) }
</Button>
</div>
</Modal>
);
}