2024-06-06 07:25:07 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { BlockInstance } from '@wordpress/blocks';
|
|
|
|
import { useEffect } from '@wordpress/element';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import NoBlocks from '../../../assets/images/no-blocks.png';
|
2024-07-29 10:05:29 +00:00
|
|
|
import { ENABLE_CLICK_CLASS } from '../auto-block-preview-event-listener';
|
2024-06-06 07:25:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The scope of this variable is limited to the block-placeholder folder.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export let noBlocksPlaceholderClientId: string | null;
|
|
|
|
|
|
|
|
export const useAddNoBlocksPlaceholder = ( {
|
|
|
|
blocks,
|
|
|
|
createBlock,
|
|
|
|
insertBlock,
|
|
|
|
removeBlock,
|
|
|
|
}: {
|
|
|
|
blocks: BlockInstance[];
|
|
|
|
createBlock: (
|
|
|
|
name: string,
|
|
|
|
attributes: Record< string, unknown >,
|
|
|
|
innerBlocks?: BlockInstance[]
|
|
|
|
) => BlockInstance;
|
|
|
|
insertBlock: ( block: BlockInstance, index: number ) => void;
|
|
|
|
removeBlock: ( clientId: string ) => void;
|
|
|
|
} ) => {
|
|
|
|
useEffect( () => {
|
|
|
|
if (
|
|
|
|
blocks.length === 2 &&
|
|
|
|
blocks.every( ( block ) => block.name === 'core/template-part' )
|
|
|
|
) {
|
|
|
|
const noBlocksBlock = createBlock(
|
2024-07-29 10:05:29 +00:00
|
|
|
'core/group',
|
2024-06-06 07:25:07 +00:00
|
|
|
{
|
|
|
|
__noBlocksPlaceholder: true,
|
2024-07-29 10:05:29 +00:00
|
|
|
className: ENABLE_CLICK_CLASS,
|
|
|
|
style: {
|
|
|
|
dimensions: {
|
|
|
|
minHeight: '60vh',
|
|
|
|
},
|
|
|
|
color: {
|
|
|
|
background: '#FAFAFA',
|
|
|
|
},
|
|
|
|
spacing: {
|
|
|
|
padding: {
|
|
|
|
top: '40px',
|
|
|
|
bottom: '40px',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
layout: {
|
|
|
|
type: 'flex',
|
|
|
|
orientation: 'vertical',
|
|
|
|
justifyContent: 'center',
|
|
|
|
verticalAlignment: 'center',
|
|
|
|
},
|
2024-06-06 07:25:07 +00:00
|
|
|
},
|
|
|
|
[
|
|
|
|
createBlock( 'core/image', {
|
|
|
|
url: NoBlocks,
|
|
|
|
align: 'center',
|
2024-07-29 10:05:29 +00:00
|
|
|
className: ENABLE_CLICK_CLASS,
|
2024-06-06 07:25:07 +00:00
|
|
|
} ),
|
2024-07-03 07:25:33 +00:00
|
|
|
createBlock(
|
|
|
|
'core/group',
|
|
|
|
{
|
|
|
|
layout: {
|
|
|
|
type: 'constrained',
|
|
|
|
contentSize: '350px',
|
2024-06-06 07:25:07 +00:00
|
|
|
},
|
2024-07-29 10:05:29 +00:00
|
|
|
className: ENABLE_CLICK_CLASS,
|
2024-06-06 07:25:07 +00:00
|
|
|
},
|
2024-07-03 07:25:33 +00:00
|
|
|
[
|
|
|
|
createBlock( 'core/paragraph', {
|
2024-07-29 10:05:29 +00:00
|
|
|
className: ENABLE_CLICK_CLASS,
|
2024-07-03 07:25:33 +00:00
|
|
|
align: 'center',
|
|
|
|
fontFamily: 'inter',
|
|
|
|
style: {
|
|
|
|
color: {
|
2024-07-18 14:31:19 +00:00
|
|
|
text: '#2F2F2F',
|
2024-07-03 07:25:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
content: __(
|
|
|
|
'Add one or more of our homepage patterns to create a page that welcomes shoppers.',
|
|
|
|
'woocommerce'
|
|
|
|
),
|
|
|
|
} ),
|
2024-07-29 10:05:29 +00:00
|
|
|
createBlock( 'core/button', {
|
|
|
|
align: 'center',
|
|
|
|
fontFamily: 'inter',
|
|
|
|
className: `is-style-outline ${ ENABLE_CLICK_CLASS } no-blocks-insert-pattern-button`,
|
|
|
|
style: {
|
|
|
|
border: {
|
|
|
|
radius: '2px',
|
|
|
|
color: '#007cba',
|
|
|
|
width: '1px',
|
|
|
|
},
|
|
|
|
color: {
|
|
|
|
text: '#007cba',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
text: __( 'Add patterns', 'woocommerce' ),
|
|
|
|
} ),
|
2024-07-03 07:25:33 +00:00
|
|
|
]
|
|
|
|
),
|
2024-06-06 07:25:07 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
insertBlock( noBlocksBlock, 1 );
|
|
|
|
noBlocksPlaceholderClientId = noBlocksBlock.clientId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( blocks.length > 3 && noBlocksPlaceholderClientId ) {
|
|
|
|
removeBlock( noBlocksPlaceholderClientId );
|
|
|
|
}
|
|
|
|
}, [ blocks, createBlock, insertBlock, removeBlock ] );
|
|
|
|
};
|