2023-07-03 14:21:31 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-08-02 07:09:51 +00:00
|
|
|
import {
|
|
|
|
InnerBlocks,
|
|
|
|
InspectorControls,
|
|
|
|
useBlockProps,
|
2023-08-28 16:32:45 +00:00
|
|
|
useInnerBlocksProps,
|
2023-08-02 07:09:51 +00:00
|
|
|
} from '@wordpress/block-editor';
|
|
|
|
import { BlockEditProps, InnerBlockTemplate } from '@wordpress/blocks';
|
|
|
|
import { useEffect } from '@wordpress/element';
|
2023-07-03 14:21:31 +00:00
|
|
|
|
2023-08-02 07:09:51 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
moveInnerBlocksToPosition,
|
|
|
|
updateGroupBlockType,
|
|
|
|
getInnerBlocksLockAttributes,
|
|
|
|
} from './utils';
|
2023-08-02 10:43:08 +00:00
|
|
|
import { ProductGalleryThumbnailsBlockSettings } from './inner-blocks/product-gallery-thumbnails/block-settings';
|
2023-08-22 19:26:46 +00:00
|
|
|
import { ProductGalleryPagerBlockSettings } from './inner-blocks/product-gallery-pager/settings';
|
2023-08-02 10:43:08 +00:00
|
|
|
import { ProductGalleryBlockSettings } from './block-settings/index';
|
2023-08-22 19:26:46 +00:00
|
|
|
import type { ProductGalleryAttributes } from './types';
|
2023-08-28 16:32:45 +00:00
|
|
|
import { ProductGalleryNextPreviousBlockSettings } from './inner-blocks/product-gallery-large-image-next-previous/settings';
|
2023-08-02 07:09:51 +00:00
|
|
|
|
|
|
|
const TEMPLATE: InnerBlockTemplate[] = [
|
|
|
|
[
|
|
|
|
'core/group',
|
|
|
|
{ layout: { type: 'flex' } },
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'woocommerce/product-gallery-thumbnails',
|
|
|
|
getInnerBlocksLockAttributes( 'lock' ),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'woocommerce/product-gallery-large-image',
|
|
|
|
getInnerBlocksLockAttributes( 'lock' ),
|
2023-08-31 16:15:31 +00:00
|
|
|
[
|
|
|
|
[
|
|
|
|
'woocommerce/product-sale-badge',
|
|
|
|
{
|
|
|
|
align: 'right',
|
|
|
|
style: {
|
|
|
|
spacing: {
|
|
|
|
margin: {
|
|
|
|
top: '4px',
|
|
|
|
right: '4px',
|
|
|
|
bottom: '4px',
|
|
|
|
left: '4px',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'woocommerce/product-gallery-large-image-next-previous',
|
|
|
|
{
|
|
|
|
layout: {
|
|
|
|
type: 'flex',
|
|
|
|
verticalAlignment: 'bottom',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2023-08-02 07:09:51 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2023-08-22 19:26:46 +00:00
|
|
|
[
|
|
|
|
'woocommerce/product-gallery-pager',
|
|
|
|
getInnerBlocksLockAttributes( 'lock' ),
|
|
|
|
],
|
2023-08-02 07:09:51 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
export const Edit = ( {
|
|
|
|
clientId,
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
2023-08-22 19:26:46 +00:00
|
|
|
}: BlockEditProps< ProductGalleryAttributes > ) => {
|
2023-07-03 14:21:31 +00:00
|
|
|
const blockProps = useBlockProps();
|
|
|
|
|
2023-08-02 07:09:51 +00:00
|
|
|
// 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 ] );
|
|
|
|
|
2023-07-03 14:21:31 +00:00
|
|
|
return (
|
|
|
|
<div { ...blockProps }>
|
2023-08-02 07:09:51 +00:00
|
|
|
<InspectorControls>
|
2023-08-22 19:26:46 +00:00
|
|
|
<ProductGalleryPagerBlockSettings
|
|
|
|
context={ {
|
|
|
|
productGalleryClientId: clientId,
|
|
|
|
pagerDisplayMode: attributes.pagerDisplayMode,
|
|
|
|
} }
|
|
|
|
/>
|
2023-08-02 10:43:08 +00:00
|
|
|
<ProductGalleryThumbnailsBlockSettings
|
2023-08-02 07:09:51 +00:00
|
|
|
attributes={ attributes }
|
|
|
|
setAttributes={ setAttributes }
|
|
|
|
context={ {
|
|
|
|
productGalleryClientId: clientId,
|
|
|
|
thumbnailsPosition: attributes.thumbnailsPosition,
|
|
|
|
thumbnailsNumberOfThumbnails:
|
|
|
|
attributes.thumbnailsNumberOfThumbnails,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
</InspectorControls>
|
2023-08-02 10:43:08 +00:00
|
|
|
<InspectorControls>
|
|
|
|
<ProductGalleryBlockSettings
|
|
|
|
attributes={ attributes }
|
|
|
|
setAttributes={ setAttributes }
|
|
|
|
/>
|
|
|
|
</InspectorControls>
|
2023-08-28 16:32:45 +00:00
|
|
|
<InspectorControls>
|
|
|
|
<ProductGalleryNextPreviousBlockSettings
|
|
|
|
context={ {
|
|
|
|
...attributes,
|
|
|
|
productGalleryClientId: clientId,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
</InspectorControls>
|
2023-07-03 14:21:31 +00:00
|
|
|
<InnerBlocks
|
2023-08-02 07:09:51 +00:00
|
|
|
allowedBlocks={ [
|
|
|
|
'woocommerce/product-gallery-large-image',
|
2023-08-22 19:26:46 +00:00
|
|
|
'woocommerce/product-gallery-pager',
|
2023-08-02 07:09:51 +00:00
|
|
|
'woocommerce/product-gallery-thumbnails',
|
|
|
|
] }
|
2023-08-22 19:26:46 +00:00
|
|
|
templateLock={ false }
|
2023-08-02 07:09:51 +00:00
|
|
|
template={ TEMPLATE }
|
2023-07-03 14:21:31 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2023-08-28 16:32:45 +00:00
|
|
|
|
|
|
|
export const Save = () => {
|
|
|
|
const blockProps = useBlockProps.save();
|
|
|
|
const innerBlocksProps = useInnerBlocksProps.save( blockProps );
|
|
|
|
return <div { ...innerBlocksProps } />;
|
|
|
|
};
|