Skip to content

Commit

Permalink
Gallery block: ensure image attributes copy correctly between transfo…
Browse files Browse the repository at this point in the history
…rms (#42796)

Co-authored-by: Tetsuaki Hamano <[email protected]>
  • Loading branch information
glendaviesnz and t-hamano authored Aug 22, 2022
1 parent 3169cf9 commit 5bc6d33
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
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

0 comments on commit 5bc6d33

Please sign in to comment.