Skip to content

Commit 291cc48

Browse files
akasunilakasunildcalhount-hamanoyouknowriad
authored andcommitted
Add lightbox option in gallery block link control (WordPress#64014)
* Add link control as toolbar option in gallery * Add link option constant for lightbox * Add expand on lick option in link control * Add class into menugroup component for info text style * Add styles for info text in link control of popover of gallery block * Add info text for lightbox option in link control for gallery block * Add action for lightbox option in gallery block link control * Show lightbox option only when lightbox UI is enabled * Disable lightbox when other link option is selected in gallery block * Remove unneccessory constant * Conditionally apply the lightbox attribute based on defatult setting * Fix lightbox default option issue for gallery block on page load * Highlight link to option in gallery block when selected * update condition to Highlight link to option * Fix lightbox attribute issue for default state of linkTo option * fix: Set empty default `block` settings value (WordPress#64448) Currently, `__experimentalFeatures` is only partially implemented for the native mobile editor. Recent changes to the shared Gallery block component included a reference to this missing key via the `useSettings` Hook. Setting a default, empty value ensure exceptions are not thrown. Co-authored-by: dcalhoun <dpcalhoun@git.wordpress.org> Co-authored-by: akasunil <sunil25393@git.wordpress.org> * Change lightbox option lable and address other feedback * Move lightbox logic to getHrefAndDestination function * Remove highlight of LinkTo option in gallery block * Remove css for info text of lightbox option * Refactor getHrefAndDestination function to for lightbox additional attributes --------- Co-authored-by: akasunil <sunil25393@git.wordpress.org> Co-authored-by: dcalhoun <dpcalhoun@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: youknowriad <youknowriad@git.wordpress.org> Co-authored-by: madhusudhand <madhudollu@git.wordpress.org>
1 parent 1c7a7fe commit 291cc48

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

packages/block-editor/src/components/global-styles/test/use-global-styles-context.native.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ describe( 'getGlobalStyles', () => {
414414
expect( globalStyles ).toEqual(
415415
expect.objectContaining( {
416416
__experimentalFeatures: {
417+
blocks: {},
417418
color: {
418419
palette: RAW_FEATURES.color.palette,
419420
gradients,

packages/block-editor/src/components/global-styles/use-global-styles-context.native.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,9 @@ export function getColorsAndGradients(
410410
return {
411411
__experimentalGlobalStylesBaseStyles: null,
412412
__experimentalFeatures: {
413+
// Set an empty object to avoid errors from shared web components relying
414+
// upon block settings. E.g., the Gallery block.
415+
blocks: {},
413416
color: {
414417
...( ! features?.color
415418
? {
@@ -455,6 +458,9 @@ export function getGlobalStyles( rawStyles, rawFeatures ) {
455458

456459
return {
457460
__experimentalFeatures: {
461+
// Set an empty object to avoid errors from shared web components relying
462+
// upon block settings. E.g., the Gallery block.
463+
blocks: {},
458464
color: {
459465
palette: colors?.palette,
460466
gradients,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const LINK_DESTINATION_NONE = 'none';
22
export const LINK_DESTINATION_MEDIA = 'media';
3+
export const LINK_DESTINATION_LIGHTBOX = 'lightbox';
34
export const LINK_DESTINATION_ATTACHMENT = 'attachment';
45
export const LINK_DESTINATION_MEDIA_WP_CORE = 'file';
56
export const LINK_DESTINATION_ATTACHMENT_WP_CORE = 'post';

packages/block-library/src/gallery/edit.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
useInnerBlocksProps,
2626
BlockControls,
2727
MediaReplaceFlow,
28+
useSettings,
2829
} from '@wordpress/block-editor';
2930
import { Platform, useEffect, useMemo } from '@wordpress/element';
3031
import { __, _x, sprintf } from '@wordpress/i18n';
@@ -38,6 +39,7 @@ import {
3839
customLink,
3940
image as imageIcon,
4041
linkOff,
42+
fullscreen,
4143
} from '@wordpress/icons';
4244

4345
/**
@@ -55,14 +57,15 @@ import {
5557
LINK_DESTINATION_ATTACHMENT,
5658
LINK_DESTINATION_MEDIA,
5759
LINK_DESTINATION_NONE,
60+
LINK_DESTINATION_LIGHTBOX,
5861
} from './constants';
5962
import useImageSizes from './use-image-sizes';
6063
import useGetNewImages from './use-get-new-images';
6164
import useGetMedia from './use-get-media';
6265
import GapStyles from './gap-styles';
6366

6467
const MAX_COLUMNS = 8;
65-
const linkOptions = [
68+
let linkOptions = [
6669
{
6770
icon: customLink,
6871
label: __( 'Link images to attachment pages' ),
@@ -75,6 +78,13 @@ const linkOptions = [
7578
value: LINK_DESTINATION_MEDIA,
7679
noticeText: __( 'Media Files' ),
7780
},
81+
{
82+
icon: fullscreen,
83+
label: __( 'Expand on click' ),
84+
value: LINK_DESTINATION_LIGHTBOX,
85+
noticeText: __( 'Lightbox effect' ),
86+
infoText: __( 'Scale images with a lightbox effect' ),
87+
},
7888
{
7989
icon: linkOff,
8090
label: _x( 'None', 'Media item link option' ),
@@ -107,6 +117,14 @@ export default function GalleryEdit( props ) {
107117
onFocus,
108118
} = props;
109119

120+
const lightboxSetting = useSettings( 'blocks.core/image.lightbox' )[ 0 ];
121+
122+
if ( ! lightboxSetting?.allowEditing ) {
123+
linkOptions = linkOptions.filter(
124+
( option ) => option.value !== LINK_DESTINATION_LIGHTBOX
125+
);
126+
}
127+
110128
const { columns, imageCrop, randomOrder, linkTarget, linkTo, sizeSlug } =
111129
attributes;
112130

@@ -363,9 +381,13 @@ export default function GalleryEdit( props ) {
363381
const image = block.attributes.id
364382
? imageData.find( ( { id } ) => id === block.attributes.id )
365383
: null;
384+
366385
changedAttributes[ block.clientId ] = getHrefAndDestination(
367386
image,
368-
value
387+
value,
388+
false,
389+
block.attributes,
390+
lightboxSetting
369391
);
370392
} );
371393
updateBlockAttributes( blocks, changedAttributes, true );
@@ -646,6 +668,7 @@ export default function GalleryEdit( props ) {
646668
onClose();
647669
} }
648670
role="menuitemradio"
671+
info={ linkItem.infoText }
649672
>
650673
{ linkItem.label }
651674
</MenuItem>

packages/block-library/src/gallery/utils.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
LINK_DESTINATION_NONE,
88
LINK_DESTINATION_MEDIA_WP_CORE,
99
LINK_DESTINATION_ATTACHMENT_WP_CORE,
10+
LINK_DESTINATION_LIGHTBOX,
1011
} from './constants';
1112
import {
1213
LINK_DESTINATION_ATTACHMENT as IMAGE_LINK_DESTINATION_ATTACHMENT,
@@ -20,13 +21,18 @@ import {
2021
*
2122
* @param {Object} image Gallery image.
2223
* @param {string} galleryDestination Gallery's selected link destination.
23-
* @param {Object} imageDestination Image blocks attributes.
24+
* @param {Object} imageDestination Image block link destination attribute.
25+
* @param {Object} attributes Block attributes.
26+
* @param {Object} lightboxSetting Lightbox setting.
27+
*
2428
* @return {Object} New attributes to assign to image block.
2529
*/
2630
export function getHrefAndDestination(
2731
image,
2832
galleryDestination,
29-
imageDestination
33+
imageDestination,
34+
attributes,
35+
lightboxSetting
3036
) {
3137
// Gutenberg and WordPress use different constants so if image_default_link_type
3238
// option is set we need to map from the WP Core values.
@@ -36,17 +42,32 @@ export function getHrefAndDestination(
3642
return {
3743
href: image?.source_url || image?.url, // eslint-disable-line camelcase
3844
linkDestination: IMAGE_LINK_DESTINATION_MEDIA,
45+
lightbox: lightboxSetting?.enabled
46+
? { ...attributes?.lightbox, enabled: false }
47+
: undefined,
3948
};
4049
case LINK_DESTINATION_ATTACHMENT_WP_CORE:
4150
case LINK_DESTINATION_ATTACHMENT:
4251
return {
4352
href: image?.link,
4453
linkDestination: IMAGE_LINK_DESTINATION_ATTACHMENT,
54+
lightbox: lightboxSetting?.enabled
55+
? { ...attributes?.lightbox, enabled: false }
56+
: undefined,
57+
};
58+
case LINK_DESTINATION_LIGHTBOX:
59+
return {
60+
href: undefined,
61+
lightbox: ! lightboxSetting?.enabled
62+
? { ...attributes?.lightbox, enabled: true }
63+
: undefined,
64+
linkDestination: IMAGE_LINK_DESTINATION_NONE,
4565
};
4666
case LINK_DESTINATION_NONE:
4767
return {
4868
href: undefined,
4969
linkDestination: IMAGE_LINK_DESTINATION_NONE,
70+
lightbox: undefined,
5071
};
5172
}
5273

0 commit comments

Comments
 (0)