Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Add Product Gallery Thumbnails block #10442

Merged
merged 46 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
1dce567
WIP Product Gallery: Add the Thumbnails block
danieldudzic Jul 17, 2023
d4a0026
Product Gallery Thumbnails: Add block settings
danieldudzic Jul 17, 2023
b77ccba
Add template for the Product Gallery block
danieldudzic Jul 18, 2023
e486bda
Add template for the Product Gallery block. Add the rest of the files.
danieldudzic Jul 18, 2023
1395a20
Product Gallery Thumbnails: Add context settings sharing between the …
danieldudzic Jul 19, 2023
80353b6
Product Gallery Thumbnails: Add UI functionality and frontend functio…
danieldudzic Jul 20, 2023
0a7f331
Product Gallery Thumbnails: Move the static template ouside of the co…
danieldudzic Jul 20, 2023
a2b6aad
Make sure the context is set before accesing the array values
danieldudzic Jul 21, 2023
8555f9f
Product Gallery Thumbnails: Move the setGroupAttributes() function ou…
danieldudzic Jul 21, 2023
04af396
Product Gallery Thumbnails: Fix TS errors
danieldudzic Jul 23, 2023
70b3618
Product Gallery Thumbnails: Update the Features Flags and Experimenta…
danieldudzic Jul 23, 2023
e372bff
Product Gallery Thumbnails: Fix TS error
danieldudzic Jul 23, 2023
7abf06a
Product Gallery Thumbnails: Remove unused stylesheet
danieldudzic Jul 23, 2023
f7bdb0f
Product Gallery Thumbnails: Fix TS errors
danieldudzic Jul 25, 2023
7d4b1c0
Product Gallery Thumbnails: Remove unused context and fix the thumbna…
danieldudzic Jul 25, 2023
c36b0be
Product Gallery Thumbnails: Allow the user to move the horizontal thu…
danieldudzic Jul 25, 2023
5edeb65
Product Gallery Thumbnails: Add code comments and remove the incorrec…
danieldudzic Jul 26, 2023
50e8746
Product Gallery Thumbnails: Fix the eslint dependency error
danieldudzic Jul 27, 2023
5576e19
Merge branch 'trunk' into add/9943-add-product-gallery-thumbnails-block
danieldudzic Jul 27, 2023
5528087
Product Gallery Thumbnails: Refactor Product Gallery edit code and mo…
danieldudzic Jul 27, 2023
231182d
Product Gallery Thumbnails: Update the utils file
danieldudzic Jul 27, 2023
b5c13c1
Merge branch 'trunk' into add/9943-add-product-gallery-thumbnails-block
danieldudzic Jul 27, 2023
340aafa
Product Gallery Thumbnails: Update the utils file. Fix comment indent…
danieldudzic Jul 27, 2023
ff8643e
Product Gallery Thumbnails: Fix undefined variable html when only 1 p…
danieldudzic Jul 28, 2023
8d8f6cb
Product Gallery: Rename clientId to productGalleryClientId
danieldudzic Jul 28, 2023
cdc5aa0
Product Gallery Thumbnails: Combine the useEffect code having the sam…
danieldudzic Jul 28, 2023
2ba838f
Product Gallery Thumbnails: Combine all useEffect code together
danieldudzic Jul 28, 2023
11a1a7a
Product Gallery Thumbnails: Add a ThumbnailsPosition enum
danieldudzic Jul 28, 2023
fa17a84
Product Gallery Thumbnails: Update the thumbnailsPosition to an enum
danieldudzic Jul 28, 2023
d4feda2
Product Gallery Thumbnails: Fix TS errors
danieldudzic Jul 29, 2023
e88abf8
Product Gallery Thumbnails: Fix TS errors
danieldudzic Jul 29, 2023
a2171c0
Product Gallery Thumbnails: Add missing dependency
danieldudzic Jul 29, 2023
54d195b
Product Gallery Thumbnails: Uppercase the enum and fix the thumbnails…
danieldudzic Jul 29, 2023
5b23f78
Merge branch 'trunk' into add/9943-add-product-gallery-thumbnails-block
danieldudzic Jul 31, 2023
e818229
Merge branch 'trunk' into add/9943-add-product-gallery-thumbnails-block
danieldudzic Jul 31, 2023
7de1f0f
Product Gallery: Add crop, zoom and full-screen settings
danieldudzic Aug 1, 2023
69107f1
Merge branch 'trunk' into add/9943-add-product-gallery-thumbnails-block
danieldudzic Aug 1, 2023
5ed8f10
Merge branch 'add/9943-add-product-gallery-thumbnails-block' of https…
danieldudzic Aug 1, 2023
d14b460
Product Gallery Thumbnails: Replace ts-ignore with ts-expect-error
danieldudzic Aug 1, 2023
a5bdc6c
Product Gallery Thumbnails: Replace ts-ignore with ts-expect-error
danieldudzic Aug 1, 2023
3896bf0
Product Gallery Thumbnails: Revert back to ts-ignore
danieldudzic Aug 1, 2023
c1bc15d
Merge branch 'trunk' into add/9943-add-product-gallery-thumbnails-block
danieldudzic Aug 1, 2023
05d2de9
Revert "Product Gallery: Add crop, zoom and full-screen settings"
danieldudzic Aug 1, 2023
5437719
Merge branch 'trunk' into add/9943-add-product-gallery-thumbnails-block
danieldudzic Aug 1, 2023
924df88
Merge branch 'trunk' into add/9943-add-product-gallery-thumbnails-block
danieldudzic Aug 1, 2023
8c65117
Merge branch 'trunk' into add/9943-add-product-gallery-thumbnails-block
danieldudzic Aug 2, 2023
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
27 changes: 22 additions & 5 deletions assets/js/blocks/product-gallery/block.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "woocommerce/product-gallery",
"version": "1.0.0",
"title": "Product Gallery",
"description": "Showcase your products relevant images and media.",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"supports": {
"align": true,
"multiple": false
},
"keywords": [ "WooCommerce" ],
"usesContext": [ "postId", "postType", "queryId" ],
"textdomain": "woo-gutenberg-products-block",

"apiVersion": 2,
"$schema": "https://schemas.wp.org/trunk/block.json"
"providesContext": {
"thumbnailsPosition": "thumbnailsPosition",
"thumbnailsNumberOfThumbnails": "thumbnailsNumberOfThumbnails",
"productGalleryClientId": "productGalleryClientId"
},
"attributes": {
"thumbnailsPosition": {
"type": "string",
"default": "left"
},
"thumbnailsNumberOfThumbnails": {
"type": "number",
"default": 3
},
"productGalleryClientId": {
"type": "string",
"default": ""
}
}
}
81 changes: 69 additions & 12 deletions assets/js/blocks/product-gallery/edit.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,82 @@
/**
* External dependencies
*/
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
import {
InnerBlocks,
InspectorControls,
useBlockProps,
} from '@wordpress/block-editor';
import { BlockEditProps, InnerBlockTemplate } from '@wordpress/blocks';
import { useEffect } from '@wordpress/element';

export const Edit = (): JSX.Element => {
/**
* Internal dependencies
*/
import {
moveInnerBlocksToPosition,
updateGroupBlockType,
getInnerBlocksLockAttributes,
} from './utils';
import { BlockSettings } from './inner-blocks/product-gallery-thumbnails/block-settings';
import type { BlockAttributes } from './types';

const TEMPLATE: InnerBlockTemplate[] = [
[
'core/group',
{ layout: { type: 'flex' } },
[
[
'woocommerce/product-gallery-thumbnails',
getInnerBlocksLockAttributes( 'lock' ),
],
[
'woocommerce/product-gallery-large-image',
getInnerBlocksLockAttributes( 'lock' ),
],
],
],
];

export const Edit = ( {
clientId,
attributes,
setAttributes,
}: BlockEditProps< BlockAttributes > ) => {
const blockProps = useBlockProps();

// Update the Group block type when the thumbnailsPosition attribute changes.
updateGroupBlockType( attributes, clientId );

useEffect( () => {
setAttributes( {
...attributes,
productGalleryClientId: clientId,
} );
// Move the Thumbnails block to the correct above or below the Large Image based on the thumbnailsPosition attribute.
moveInnerBlocksToPosition( attributes, clientId );
}, [ setAttributes, attributes, clientId ] );

return (
<div { ...blockProps }>
<InspectorControls>
<BlockSettings
attributes={ attributes }
setAttributes={ setAttributes }
context={ {
productGalleryClientId: clientId,
thumbnailsPosition: attributes.thumbnailsPosition,
thumbnailsNumberOfThumbnails:
attributes.thumbnailsNumberOfThumbnails,
} }
/>
</InspectorControls>
<InnerBlocks
allowedBlocks={ [ 'woocommerce/product-gallery-large-image' ] }
templateLock={ false }
allowedBlocks={ [
'woocommerce/product-gallery-large-image',
'woocommerce/product-gallery-thumbnails',
] }
template={ TEMPLATE }
/>
</div>
);
};

export const Save = (): JSX.Element => {
return (
<div { ...useBlockProps.save() }>
<InnerBlocks.Content />
</div>
);
};
6 changes: 5 additions & 1 deletion assets/js/blocks/product-gallery/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ import { isExperimentalBuild } from '@woocommerce/block-settings';
/**
* Internal dependencies
*/
import { Edit, Save } from './edit';
import { Edit } from './edit';
import { Save } from './save';
import metadata from './block.json';
import icon from './icon';
import './inner-blocks/product-gallery-large-image';
import './inner-blocks/product-gallery-thumbnails';

if ( isExperimentalBuild() ) {
registerBlockSingleProductTemplate( {
blockName: metadata.name,
// @ts-expect-error: `metadata` currently does not have a type definition in WordPress core
blockMetadata: metadata,
blockSettings: {
icon,
// @ts-expect-error `edit` can be extended to include other attributes
edit: Edit,
save: Save,
ancestor: [ 'woocommerce/single-product' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { WC_BLOCKS_IMAGE_URL } from '@woocommerce/block-settings';
import { useBlockProps } from '@wordpress/block-editor';
import { Disabled } from '@wordpress/components';

/**
* Internal dependencies
*/
import './editor.scss';

export const Edit = (): JSX.Element => {
const blockProps = useBlockProps( {
className: 'wc-block-editor-product-gallery_large-image',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.wc-block-editor-product-gallery-large-image img {
max-width: 500px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* External dependencies
*/
import {
InspectorControls,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { Icon } from '@wordpress/icons';
import {
thumbnailsPositionLeft,
thumbnailsPositionBottom,
thumbnailsPositionRight,
} from '@woocommerce/icons';
import { useDispatch } from '@wordpress/data';
import {
PanelBody,
RangeControl,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Ignoring because `__experimentalToggleGroupControlOption` is not yet in the type definitions.
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Ignoring because `__experimentalToggleGroupControl` is not yet in the type definitions.
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalToggleGroupControl as ToggleGroupControl,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import { ThumbnailsPosition } from '../constants';
import type { ThumbnailsSettingProps } from '../../../types';

const positionHelp: Record< ThumbnailsPosition, string > = {
[ ThumbnailsPosition.OFF ]: __(
'No thumbnails will be displayed.',
'woo-gutenberg-products-block'
),
[ ThumbnailsPosition.LEFT ]: __(
'A strip of small images will appear to the left of the main gallery image.',
'woo-gutenberg-products-block'
),
[ ThumbnailsPosition.BOTTOM ]: __(
'A strip of small images will appear below the main gallery image.',
'woo-gutenberg-products-block'
),
[ ThumbnailsPosition.RIGHT ]: __(
'A strip of small images will appear to the right of the main gallery image.',
'woo-gutenberg-products-block'
),
};

export const BlockSettings = ( { context }: ThumbnailsSettingProps ) => {
const maxNumberOfThumbnails = 8;
const minNumberOfThumbnails = 2;
const { productGalleryClientId } = context;
// @ts-expect-error @wordpress/block-editor/store types not provided
const { updateBlockAttributes } = useDispatch( blockEditorStore );

return (
<InspectorControls>
<PanelBody
title={ __( 'Settings', 'woo-gutenberg-products-block' ) }
>
<ToggleGroupControl
className="wc-block-editor-product-gallery-thumbnails__position-toggle"
isBlock={ true }
label={ __( 'Thumbnails', 'woo-gutenberg-products-block' ) }
value={ context.thumbnailsPosition }
help={
positionHelp[
context.thumbnailsPosition as ThumbnailsPosition
]
}
onChange={ ( value: string ) =>
updateBlockAttributes( productGalleryClientId, {
thumbnailsPosition: value,
} )
}
>
<ToggleGroupControlOption
value={ ThumbnailsPosition.OFF }
label={ __( 'Off', 'woo-gutenberg-products-block' ) }
/>
<ToggleGroupControlOption
value={ ThumbnailsPosition.LEFT }
label={
<Icon size={ 32 } icon={ thumbnailsPositionLeft } />
}
/>
<ToggleGroupControlOption
value={ ThumbnailsPosition.BOTTOM }
label={
<Icon
size={ 32 }
icon={ thumbnailsPositionBottom }
/>
}
/>
<ToggleGroupControlOption
value={ ThumbnailsPosition.RIGHT }
label={
<Icon
size={ 32 }
icon={ thumbnailsPositionRight }
/>
}
/>
</ToggleGroupControl>
{ context.thumbnailsPosition !== ThumbnailsPosition.OFF && (
<RangeControl
label={ __(
'Number of Thumbnails',
'woo-gutenberg-products-block'
) }
value={ context.thumbnailsNumberOfThumbnails }
onChange={ ( value: number ) =>
updateBlockAttributes( productGalleryClientId, {
thumbnailsNumberOfThumbnails: value,
} )
}
help={ __(
'Choose how many thumbnails (2-8) will display. If more images exist, a “View all” button will display.',
'woo-gutenberg-products-block'
) }
max={ maxNumberOfThumbnails }
min={ minNumberOfThumbnails }
/>
) }
</PanelBody>
</InspectorControls>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "woocommerce/product-gallery-thumbnails",
"version": "1.0.0",
"title": "Thumbnails",
"description": "Display the Thumbnails of a product.",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"usesContext": [ "postId", "thumbnailsPosition", "thumbnailsNumberOfThumbnails", "productGalleryClientId" ],
"textdomain": "woo-gutenberg-products-block",
"ancestor": [ "woocommerce/product-gallery" ],
"supports": {
"spacing": {
"margin": true,
"__experimentalDefaultControls": {
"margin": true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum ThumbnailsPosition {
OFF = 'off',
LEFT = 'left',
BOTTOM = 'bottom',
RIGHT = 'right',
}
Loading