Skip to content

Commit

Permalink
Move lightbox logic to getHrefAndDestination function
Browse files Browse the repository at this point in the history
  • Loading branch information
akasunil committed Aug 16, 2024
1 parent ac6c169 commit a39977f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
47 changes: 12 additions & 35 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,13 @@ export default function GalleryEdit( props ) {
onFocus,
} = props;

const lightboxSetting = useSettings( 'blocks.core/image.lightbox' );
const lightboxSetting = useSettings( 'blocks.core/image.lightbox' )[ 0 ];

useEffect( () => {
if ( ! lightboxSetting?.allowEditing ) {
linkOptions = linkOptions.filter(
( option ) => option.value !== LINK_DESTINATION_LIGHTBOX
);
}
}, [ lightboxSetting?.allowEditing ] );
if ( ! lightboxSetting?.allowEditing ) {
linkOptions = linkOptions.filter(
( option ) => option.value !== LINK_DESTINATION_LIGHTBOX
);
}

const { columns, imageCrop, randomOrder, linkTarget, linkTo, sizeSlug } =
attributes;
Expand Down Expand Up @@ -384,33 +382,12 @@ export default function GalleryEdit( props ) {
? imageData.find( ( { id } ) => id === block.attributes.id )
: null;

let hrefAndDestination = getHrefAndDestination( image, value );

if (
value === LINK_DESTINATION_LIGHTBOX &&
! lightboxSetting?.enabled
) {
hrefAndDestination = {
...hrefAndDestination,
lightbox: { enabled: true },
};
} else if (
value !== LINK_DESTINATION_NONE &&
value !== LINK_DESTINATION_LIGHTBOX &&
lightboxSetting?.enabled
) {
hrefAndDestination = {
...hrefAndDestination,
lightbox: { enabled: false },
};
} else {
hrefAndDestination = {
...hrefAndDestination,
lightbox: undefined,
};
}

changedAttributes[ block.clientId ] = hrefAndDestination;
changedAttributes[ block.clientId ] = getHrefAndDestination(
image,
value,
false,
lightboxSetting
);
} );
updateBlockAttributes( blocks, changedAttributes, true );
const linkToText = [ ...linkOptions ].find(
Expand Down
16 changes: 14 additions & 2 deletions packages/block-library/src/gallery/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ import {
* @param {Object} image Gallery image.
* @param {string} galleryDestination Gallery's selected link destination.
* @param {Object} imageDestination Image blocks attributes.
* @param {Object} lightboxSetting Lightbox setting.
*
* @return {Object} New attributes to assign to image block.
*/
export function getHrefAndDestination(
image,
galleryDestination,
imageDestination
imageDestination,
lightboxSetting
) {
// Gutenberg and WordPress use different constants so if image_default_link_type
// option is set we need to map from the WP Core values.
Expand All @@ -37,23 +40,32 @@ export function getHrefAndDestination(
return {
href: image?.source_url || image?.url, // eslint-disable-line camelcase
linkDestination: IMAGE_LINK_DESTINATION_MEDIA,
lightbox: lightboxSetting?.enabled
? { enabled: false }
: undefined,
};
case LINK_DESTINATION_ATTACHMENT_WP_CORE:
case LINK_DESTINATION_ATTACHMENT:
return {
href: image?.link,
linkDestination: IMAGE_LINK_DESTINATION_ATTACHMENT,
lightbox: lightboxSetting?.enabled
? { enabled: false }
: undefined,
};
case LINK_DESTINATION_LIGHTBOX:
return {
href: undefined,
lightbox: { enabled: true },
lightbox: ! lightboxSetting?.enabled
? { enabled: true }
: undefined,
linkDestination: IMAGE_LINK_DESTINATION_NONE,
};
case LINK_DESTINATION_NONE:
return {
href: undefined,
linkDestination: IMAGE_LINK_DESTINATION_NONE,
lightbox: undefined,
};
}

Expand Down

0 comments on commit a39977f

Please sign in to comment.