-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
210bb1b
Add toolbar button to convert old galleries to new format
d4580f4
Change wording to Update rather than review and add better layout to …
546dc2b
Fix linting issue
c36e1c1
Remove additional wording from update dialog
04203e4
Refine the dialog text
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/block-library/src/gallery/v1/update-gallery-modal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* 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 to new gallery format' ) } | ||
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 add custom links or styles to individual images in the gallery, and makes it easier to add or move images around.' | ||
) } | ||
</p> | ||
<p> | ||
{ __( | ||
'There is no option to convert it back to the old format, so if there are problems with the new format once updated just leave the post/page without saving.' | ||
) } | ||
</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> | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tiniest of nits: the linting step is failing because of a single whitespace character on this line 😄