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: ensure image attributes copy correctly between transforms #42796

Merged
merged 5 commits into from
Aug 22, 2022
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
24 changes: 22 additions & 2 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,30 @@ function GalleryEdit( props ) {
? `is-style-${ preferredStyle }`
: undefined;
}

let newLinkTarget;
if ( imageAttributes.linkTarget || imageAttributes.rel ) {
// When transformed from image blocks, the link destination and rel attributes are inherited.
newLinkTarget = {
linkTarget: imageAttributes.linkTarget,
rel: imageAttributes.rel,
};
} else {
// When an image is added, update the link destination and rel attributes according to the gallery settings
newLinkTarget = getUpdatedLinkTargetSettings(
linkTarget,
attributes
);
}

return {
...pickRelevantMediaFiles( image, sizeSlug ),
...getHrefAndDestination( image, linkTo ),
...getUpdatedLinkTargetSettings( linkTarget, attributes ),
...getHrefAndDestination(
image,
linkTo,
imageAttributes?.linkDestination
),
...newLinkTarget,
className: newClassName,
sizeSlug,
caption: imageAttributes.caption || image.caption?.raw,
Expand Down
23 changes: 18 additions & 5 deletions packages/block-library/src/gallery/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ const transforms = {

if ( isGalleryV2Enabled() ) {
const innerBlocks = validImages.map( ( image ) => {
// Gallery images can't currently be resized so make sure height and width are undefined.
image.width = undefined;
image.height = undefined;
return createBlock( 'core/image', image );
} );

Expand Down Expand Up @@ -302,26 +305,36 @@ const transforms = {
return innerBlocks.map(
( {
attributes: {
id,
url,
alt,
caption,
title,
href,
rel,
linkClass,
id,
sizeSlug: imageSizeSlug,
linkDestination,
href,
linkTarget,
anchor,
className,
},
} ) =>
createBlock( 'core/image', {
id,
align,
url,
alt,
caption,
title,
href,
rel,
linkClass,
id,
sizeSlug: imageSizeSlug,
align,
linkDestination,
href,
linkTarget,
anchor,
className,
} )
);
}
Expand Down
18 changes: 11 additions & 7 deletions packages/block-library/src/gallery/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ import {
} from '../image/constants';

/**
* Determines new href and linkDestination values for an image block from the
* supplied Gallery link destination.
* Determines new href and linkDestination values for an Image block from the
* supplied Gallery link destination, or falls back to the Image blocks link.
*
* @param {Object} image Gallery image.
* @param {string} destination Gallery's selected link destination.
* @param {Object} image Gallery image.
* @param {string} galleryDestination Gallery's selected link destination.
* @param {Object} imageDestination Image blocks attributes.
* @return {Object} New attributes to assign to image block.
*/
export function getHrefAndDestination( image, destination ) {
export function getHrefAndDestination(
image,
galleryDestination,
imageDestination
) {
// Gutenberg and WordPress use different constants so if image_default_link_type
// option is set we need to map from the WP Core values.

switch ( destination ) {
switch ( imageDestination ? imageDestination : galleryDestination ) {
case LINK_DESTINATION_MEDIA_WP_CORE:
case LINK_DESTINATION_MEDIA:
return {
Expand Down