2023-07-03 14:21:31 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-08-02 07:09:51 +00:00
|
|
|
import {
|
|
|
|
InnerBlocks,
|
|
|
|
InspectorControls,
|
|
|
|
useBlockProps,
|
|
|
|
} 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';
|
|
|
|
import { ProductGalleryBlockSettings } from './block-settings/index';
|
|
|
|
import type {
|
|
|
|
ProductGalleryThumbnailsBlockAttributes,
|
|
|
|
ProductGalleryBlockAttributes,
|
|
|
|
} from './types';
|
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' ),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
export const Edit = ( {
|
|
|
|
clientId,
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
2023-08-02 10:43:08 +00:00
|
|
|
}: BlockEditProps<
|
|
|
|
ProductGalleryThumbnailsBlockAttributes & ProductGalleryBlockAttributes
|
|
|
|
> ) => {
|
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-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-07-03 14:21:31 +00:00
|
|
|
<InnerBlocks
|
2023-08-02 07:09:51 +00:00
|
|
|
allowedBlocks={ [
|
|
|
|
'woocommerce/product-gallery-large-image',
|
|
|
|
'woocommerce/product-gallery-thumbnails',
|
|
|
|
] }
|
|
|
|
template={ TEMPLATE }
|
2023-07-03 14:21:31 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|