2021-11-19 11:47:48 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import type { ReactElement } from 'react';
|
2021-12-07 14:07:53 +00:00
|
|
|
import {
|
|
|
|
useBlockProps,
|
|
|
|
InnerBlocks,
|
|
|
|
BlockControls,
|
|
|
|
} from '@wordpress/block-editor';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Icon, filledCart, removeCart } from '@woocommerce/icons';
|
|
|
|
import { EditorProvider } from '@woocommerce/base-context';
|
|
|
|
import type { TemplateArray } from '@wordpress/blocks';
|
2021-11-19 11:47:48 +00:00
|
|
|
|
2021-12-07 14:07:53 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useViewSwitcher, useForcedLayout } from '../shared';
|
|
|
|
|
|
|
|
// Array of allowed block names.
|
|
|
|
const ALLOWED_BLOCKS = [
|
|
|
|
'woocommerce/filled-mini-cart-contents-block',
|
|
|
|
'woocommerce/empty-mini-cart-contents-block',
|
|
|
|
];
|
2021-12-14 00:46:10 +00:00
|
|
|
|
|
|
|
const views = [
|
|
|
|
{
|
|
|
|
view: 'woocommerce/filled-mini-cart-contents-block',
|
|
|
|
label: __( 'Filled Mini Cart', 'woo-gutenberg-products-block' ),
|
|
|
|
icon: <Icon srcElement={ filledCart } />,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
view: 'woocommerce/empty-mini-cart-contents-block',
|
|
|
|
label: __( 'Empty Mini Cart', 'woo-gutenberg-products-block' ),
|
|
|
|
icon: <Icon srcElement={ removeCart } />,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-12-07 14:07:53 +00:00
|
|
|
interface Props {
|
|
|
|
clientId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Edit = ( { clientId }: Props ): ReactElement => {
|
2021-11-19 11:47:48 +00:00
|
|
|
const blockProps = useBlockProps();
|
|
|
|
|
2021-12-07 14:07:53 +00:00
|
|
|
const defaultTemplate = [
|
|
|
|
[ 'woocommerce/filled-mini-cart-contents-block', {}, [] ],
|
|
|
|
[ 'woocommerce/empty-mini-cart-contents-block', {}, [] ],
|
|
|
|
] as TemplateArray;
|
|
|
|
|
|
|
|
const { currentView, component: ViewSwitcherComponent } = useViewSwitcher(
|
|
|
|
clientId,
|
2021-12-14 00:46:10 +00:00
|
|
|
views
|
2021-12-07 14:07:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
useForcedLayout( {
|
|
|
|
clientId,
|
|
|
|
registeredBlocks: ALLOWED_BLOCKS,
|
|
|
|
defaultTemplate,
|
|
|
|
} );
|
|
|
|
|
2021-11-19 11:47:48 +00:00
|
|
|
return (
|
|
|
|
<div { ...blockProps }>
|
2021-12-07 14:07:53 +00:00
|
|
|
<EditorProvider currentView={ currentView }>
|
|
|
|
<BlockControls __experimentalShareWithChildBlocks>
|
|
|
|
{ ViewSwitcherComponent }
|
|
|
|
</BlockControls>
|
|
|
|
<InnerBlocks
|
|
|
|
allowedBlocks={ ALLOWED_BLOCKS }
|
|
|
|
template={ defaultTemplate }
|
|
|
|
templateLock={ false }
|
|
|
|
/>
|
|
|
|
</EditorProvider>
|
2021-11-19 11:47:48 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Edit;
|
2021-12-07 14:07:53 +00:00
|
|
|
|
|
|
|
export const Save = (): JSX.Element => {
|
|
|
|
return (
|
|
|
|
<div { ...useBlockProps.save() }>
|
|
|
|
<InnerBlocks.Content />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|