From 6a88060eedc702923c124abdcb259afe00417c23 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 29 Jul 2022 17:29:11 +1200 Subject: [PATCH 1/5] Remove an fixed height and width from image blocks before converting to gallery --- packages/block-library/src/gallery/transforms.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/block-library/src/gallery/transforms.js b/packages/block-library/src/gallery/transforms.js index 553596f91a8cb7..fdbad6c2ff6216 100644 --- a/packages/block-library/src/gallery/transforms.js +++ b/packages/block-library/src/gallery/transforms.js @@ -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 ); } ); From 0319784e2c024843adfbcea85e97f9338bd63596 Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Fri, 29 Jul 2022 22:39:23 +0900 Subject: [PATCH 2/5] Ensure that attributes are correctly carried when converting from gallery to image --- .../block-library/src/gallery/transforms.js | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/gallery/transforms.js b/packages/block-library/src/gallery/transforms.js index fdbad6c2ff6216..1be5815c69ab17 100644 --- a/packages/block-library/src/gallery/transforms.js +++ b/packages/block-library/src/gallery/transforms.js @@ -305,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, } ) ); } From 976bd6b7102a15154f049f7714f6853c4e14c7e1 Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Fri, 29 Jul 2022 23:59:22 +0900 Subject: [PATCH 3/5] Ensure that all attributes are correctly carried when converting from image to gallery --- packages/block-library/src/gallery/edit.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/gallery/edit.js b/packages/block-library/src/gallery/edit.js index d0ba808946d7a9..5351bb7d4b87d9 100644 --- a/packages/block-library/src/gallery/edit.js +++ b/packages/block-library/src/gallery/edit.js @@ -187,10 +187,26 @@ 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 ), + ...newLinkTarget, className: newClassName, sizeSlug, caption: imageAttributes.caption || image.caption?.raw, From 8801cab9044ca5e9c493ab0941b37459c9d12638 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 17 Aug 2022 10:36:55 +1200 Subject: [PATCH 4/5] Fall back to Image block destination if gallery destination undefined --- packages/block-library/src/gallery/edit.js | 6 +++++- packages/block-library/src/gallery/utils.js | 18 +++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/gallery/edit.js b/packages/block-library/src/gallery/edit.js index 5351bb7d4b87d9..41240ac6c48be6 100644 --- a/packages/block-library/src/gallery/edit.js +++ b/packages/block-library/src/gallery/edit.js @@ -205,7 +205,11 @@ function GalleryEdit( props ) { return { ...pickRelevantMediaFiles( image, sizeSlug ), - ...getHrefAndDestination( image, linkTo ), + ...getHrefAndDestination( + image, + linkTo, + imageAttributes?.linkDestination + ), ...newLinkTarget, className: newClassName, sizeSlug, diff --git a/packages/block-library/src/gallery/utils.js b/packages/block-library/src/gallery/utils.js index 71da9db0b7fc24..ddf21689bf4b76 100644 --- a/packages/block-library/src/gallery/utils.js +++ b/packages/block-library/src/gallery/utils.js @@ -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 ( galleryDestination || imageDestination ) { case LINK_DESTINATION_MEDIA_WP_CORE: case LINK_DESTINATION_MEDIA: return { From 534d267656126fac39525180a8a6589dce377c34 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 22 Aug 2022 12:08:53 +1200 Subject: [PATCH 5/5] Use image destination to set link and fall back to gallery destination to prevent previously set image links being wiped when switching between code and editor view --- packages/block-library/src/gallery/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/gallery/utils.js b/packages/block-library/src/gallery/utils.js index ddf21689bf4b76..15f2062ea943da 100644 --- a/packages/block-library/src/gallery/utils.js +++ b/packages/block-library/src/gallery/utils.js @@ -30,7 +30,7 @@ export function getHrefAndDestination( ) { // 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 ( galleryDestination || imageDestination ) { + switch ( imageDestination ? imageDestination : galleryDestination ) { case LINK_DESTINATION_MEDIA_WP_CORE: case LINK_DESTINATION_MEDIA: return {