2023-09-19 09:58:18 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
registerBlockType,
|
|
|
|
InnerBlockTemplate,
|
|
|
|
BlockAttributes,
|
|
|
|
} from '@wordpress/blocks';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
|
|
|
|
import { page } from '@wordpress/icons';
|
|
|
|
import { CHECKOUT_PAGE_ID, CART_PAGE_ID } from '@woocommerce/block-settings';
|
|
|
|
import { useEffect } from '@wordpress/element';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import metadata from './block.json';
|
|
|
|
import './editor.scss';
|
|
|
|
|
|
|
|
const Edit = ( {
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
|
|
|
}: {
|
|
|
|
attributes: BlockAttributes;
|
|
|
|
setAttributes: ( attrs: BlockAttributes ) => void;
|
|
|
|
} ) => {
|
|
|
|
const TEMPLATE: InnerBlockTemplate[] = [
|
2023-11-28 07:16:45 +00:00
|
|
|
[ 'core/post-title', { align: 'wide', level: 1 } ],
|
2023-09-19 09:58:18 +00:00
|
|
|
[ 'core/post-content', { align: 'wide' } ],
|
|
|
|
];
|
|
|
|
|
|
|
|
const blockProps = useBlockProps( {
|
|
|
|
className: 'wp-block-woocommerce-page-content-wrapper',
|
|
|
|
} );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( ! attributes.postId && attributes.page ) {
|
|
|
|
let postId = 0;
|
|
|
|
|
|
|
|
if ( attributes.page === 'checkout' ) {
|
|
|
|
postId = CHECKOUT_PAGE_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( attributes.page === 'cart' ) {
|
|
|
|
postId = CART_PAGE_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( postId ) {
|
|
|
|
setAttributes( { postId, postType: 'page' } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [ attributes, setAttributes ] );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div { ...blockProps }>
|
|
|
|
<InnerBlocks template={ TEMPLATE } />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
registerBlockType( metadata, {
|
|
|
|
icon: {
|
|
|
|
src: page,
|
|
|
|
},
|
|
|
|
edit: Edit,
|
|
|
|
save: () => <InnerBlocks.Content />,
|
|
|
|
variations: [
|
|
|
|
{
|
|
|
|
name: 'checkout-page',
|
2023-12-12 22:12:36 +00:00
|
|
|
title: __( 'Checkout Page', 'woocommerce' ),
|
2023-09-19 09:58:18 +00:00
|
|
|
attributes: {
|
|
|
|
page: 'checkout',
|
|
|
|
},
|
|
|
|
isActive: ( blockAttributes, variationAttributes ) =>
|
|
|
|
blockAttributes.page === variationAttributes.page,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'cart-page',
|
2023-12-12 22:12:36 +00:00
|
|
|
title: __( 'Cart Page', 'woocommerce' ),
|
2023-09-19 09:58:18 +00:00
|
|
|
attributes: {
|
|
|
|
page: 'cart',
|
|
|
|
},
|
|
|
|
isActive: ( blockAttributes, variationAttributes ) =>
|
|
|
|
blockAttributes.page === variationAttributes.page,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
} );
|