2024-06-04 12:11:10 +00:00
|
|
|
/* eslint-disable @woocommerce/dependency-group */
|
2024-05-28 06:55:14 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { BlockInstance } from '@wordpress/blocks';
|
2024-06-20 11:26:06 +00:00
|
|
|
import {
|
|
|
|
ToolbarGroup,
|
|
|
|
Toolbar as WPToolbar,
|
|
|
|
Popover,
|
|
|
|
} from '@wordpress/components';
|
2024-06-04 12:11:10 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
2024-06-20 11:26:06 +00:00
|
|
|
import {
|
|
|
|
useContext,
|
|
|
|
useEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from '@wordpress/element';
|
2024-06-04 12:11:10 +00:00
|
|
|
|
2024-05-28 06:55:14 +00:00
|
|
|
import {
|
|
|
|
BlockMover,
|
|
|
|
store as blockEditorStore,
|
2024-06-04 12:11:10 +00:00
|
|
|
// @ts-expect-error missing type
|
2024-05-28 06:55:14 +00:00
|
|
|
} from '@wordpress/block-editor';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2024-06-04 12:11:10 +00:00
|
|
|
import { useQuery } from '@woocommerce/navigation';
|
2024-05-28 07:32:26 +00:00
|
|
|
import Shuffle from './shuffle';
|
2024-06-06 07:25:07 +00:00
|
|
|
import Delete from './delete';
|
2024-06-04 12:11:10 +00:00
|
|
|
import './style.scss';
|
2024-06-06 07:25:07 +00:00
|
|
|
import { useIsNoBlocksPlaceholderPresent } from '../hooks/block-placeholder/use-is-no-blocks-placeholder-present';
|
2024-06-20 11:26:06 +00:00
|
|
|
import { SelectedBlockContext } from '../context/selected-block-ref-context';
|
2024-06-04 12:11:10 +00:00
|
|
|
|
|
|
|
const isHomepageUrl = ( path: string ) => {
|
2024-06-18 12:16:16 +00:00
|
|
|
return path.includes( '/customize-store/assembler-hub/homepage' );
|
2024-06-04 12:11:10 +00:00
|
|
|
};
|
2024-05-28 06:55:14 +00:00
|
|
|
|
|
|
|
export const Toolbar = () => {
|
2024-06-04 12:11:10 +00:00
|
|
|
const [ isHomepageSidebarOpen, setIsHomepageSidebarOpen ] =
|
|
|
|
useState( false );
|
|
|
|
|
2024-05-28 06:55:14 +00:00
|
|
|
const {
|
|
|
|
currentBlock,
|
|
|
|
nextBlock,
|
|
|
|
previousBlock,
|
2024-06-04 12:11:10 +00:00
|
|
|
allBlocks,
|
2024-05-28 06:55:14 +00:00
|
|
|
}: {
|
|
|
|
currentBlock: BlockInstance | undefined;
|
|
|
|
nextBlock: BlockInstance | undefined;
|
|
|
|
previousBlock: BlockInstance | undefined;
|
2024-06-04 12:11:10 +00:00
|
|
|
allBlocks: BlockInstance[];
|
2024-05-28 06:55:14 +00:00
|
|
|
} = useSelect( ( select ) => {
|
|
|
|
const selectedBlockId =
|
|
|
|
// @ts-expect-error missing type
|
|
|
|
select( blockEditorStore ).getSelectedBlockClientId();
|
|
|
|
const nextBlockClientId =
|
|
|
|
// @ts-expect-error missing type
|
|
|
|
select( blockEditorStore ).getNextBlockClientId();
|
|
|
|
const previousBlockClientId =
|
|
|
|
// @ts-expect-error missing type
|
|
|
|
select( blockEditorStore ).getPreviousBlockClientId();
|
|
|
|
|
|
|
|
// @ts-expect-error missing type
|
|
|
|
const [ current ] = select( blockEditorStore ).getBlocksByClientId( [
|
|
|
|
selectedBlockId,
|
|
|
|
] );
|
|
|
|
|
|
|
|
// @ts-expect-error missing type
|
|
|
|
const [ next ] = select( blockEditorStore ).getBlocksByClientId( [
|
|
|
|
nextBlockClientId,
|
|
|
|
] );
|
|
|
|
|
|
|
|
const [ previous ] = select(
|
|
|
|
blockEditorStore
|
|
|
|
// @ts-expect-error missing type
|
|
|
|
).getBlocksByClientId( [ previousBlockClientId ] );
|
|
|
|
|
2024-06-04 12:11:10 +00:00
|
|
|
const blocks = select(
|
|
|
|
blockEditorStore
|
|
|
|
// @ts-expect-error missing type
|
|
|
|
).getBlocks();
|
|
|
|
|
2024-05-28 06:55:14 +00:00
|
|
|
return {
|
|
|
|
currentBlock: current,
|
|
|
|
nextBlock: next,
|
|
|
|
previousBlock: previous,
|
2024-06-04 12:11:10 +00:00
|
|
|
allBlocks: blocks,
|
2024-05-28 06:55:14 +00:00
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
2024-06-04 12:11:10 +00:00
|
|
|
const query = useQuery();
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
const path = query.path;
|
2024-06-18 12:16:16 +00:00
|
|
|
if ( ! path ) {
|
|
|
|
return;
|
|
|
|
}
|
2024-06-04 12:11:10 +00:00
|
|
|
setIsHomepageSidebarOpen( isHomepageUrl( path ) );
|
|
|
|
}, [ query ] );
|
2024-05-28 06:55:14 +00:00
|
|
|
|
2024-06-27 08:22:32 +00:00
|
|
|
const selectedBlockClientId = currentBlock?.clientId ?? null;
|
2024-06-04 12:11:10 +00:00
|
|
|
|
2024-06-17 08:56:45 +00:00
|
|
|
const { isBlockMoverUpButtonDisabled, isBlockMoverDownButtonDisabled } =
|
|
|
|
useMemo( () => {
|
|
|
|
const isPreviousBlockTemplatePart =
|
|
|
|
previousBlock?.name === 'core/template-part';
|
|
|
|
const isNextBlockTemplatePart =
|
|
|
|
nextBlock?.name === 'core/template-part';
|
|
|
|
|
|
|
|
return {
|
|
|
|
isBlockMoverUpButtonDisabled:
|
|
|
|
isPreviousBlockTemplatePart ||
|
|
|
|
// The first block is the header, which is not movable.
|
|
|
|
allBlocks[ 1 ]?.clientId === selectedBlockClientId,
|
|
|
|
isBlockMoverDownButtonDisabled:
|
|
|
|
isNextBlockTemplatePart ||
|
|
|
|
// The last block is the footer, which is not movable.
|
|
|
|
allBlocks[ allBlocks.length - 2 ]?.clientId ===
|
|
|
|
selectedBlockClientId,
|
|
|
|
};
|
|
|
|
}, [
|
|
|
|
allBlocks,
|
|
|
|
nextBlock?.name,
|
|
|
|
previousBlock?.name,
|
|
|
|
selectedBlockClientId,
|
|
|
|
] );
|
|
|
|
|
2024-06-06 07:25:07 +00:00
|
|
|
const isNoBlocksPlaceholderPresent =
|
|
|
|
useIsNoBlocksPlaceholderPresent( allBlocks );
|
|
|
|
|
|
|
|
const isHeaderOrFooter = useMemo( () => {
|
|
|
|
const selectedBlock = allBlocks.find( ( { clientId } ) => {
|
|
|
|
return clientId === selectedBlockClientId;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return selectedBlock?.name === 'core/template-part';
|
|
|
|
}, [ allBlocks, selectedBlockClientId ] );
|
|
|
|
|
2024-06-20 11:26:06 +00:00
|
|
|
const { selectedBlockRef } = useContext( SelectedBlockContext );
|
|
|
|
|
|
|
|
const blockPopoverRef = useRef< HTMLDivElement | null >( null );
|
|
|
|
|
|
|
|
const popoverAnchor = useMemo( () => {
|
|
|
|
if ( ! selectedBlockRef || ! selectedBlockClientId ) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
getBoundingClientRect() {
|
|
|
|
const { top, width, height } =
|
|
|
|
selectedBlockRef.getBoundingClientRect();
|
|
|
|
|
|
|
|
const rect = window.document
|
|
|
|
.querySelector(
|
|
|
|
'.woocommerce-customize-store-assembler > iframe[name="editor-canvas"]'
|
|
|
|
)
|
|
|
|
?.getBoundingClientRect();
|
|
|
|
|
|
|
|
if ( ! rect ) {
|
|
|
|
return new window.DOMRect( 0, 0, 0, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return new window.DOMRect(
|
|
|
|
rect?.left + 10,
|
2024-07-03 07:25:23 +00:00
|
|
|
Math.max( top + 70 + rect.top, 100 ),
|
2024-06-20 11:26:06 +00:00
|
|
|
width,
|
|
|
|
height
|
|
|
|
);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}, [ selectedBlockRef, selectedBlockClientId ] );
|
|
|
|
|
2024-06-06 07:25:07 +00:00
|
|
|
if (
|
|
|
|
! isHomepageSidebarOpen ||
|
|
|
|
! selectedBlockClientId ||
|
|
|
|
isNoBlocksPlaceholderPresent ||
|
2024-06-20 11:26:06 +00:00
|
|
|
isHeaderOrFooter ||
|
|
|
|
! popoverAnchor
|
2024-06-06 07:25:07 +00:00
|
|
|
) {
|
2024-06-04 12:11:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-05-28 06:55:14 +00:00
|
|
|
return (
|
2024-06-20 11:26:06 +00:00
|
|
|
<Popover
|
|
|
|
as="div"
|
2024-06-27 08:22:32 +00:00
|
|
|
animate={ false }
|
2024-06-20 11:26:06 +00:00
|
|
|
className="components-tooltip woocommerce-customize-store_block-toolbar-popover"
|
|
|
|
// @ts-expect-error missing type
|
|
|
|
variant="unstyled"
|
|
|
|
resize={ false }
|
|
|
|
flip={ false }
|
|
|
|
shift={ true }
|
|
|
|
anchor={ popoverAnchor }
|
|
|
|
placement="top-start"
|
|
|
|
ref={ blockPopoverRef }
|
|
|
|
>
|
2024-05-28 06:55:14 +00:00
|
|
|
<div className="woocommerce-customize-store-block-toolbar">
|
|
|
|
<WPToolbar label="Options">
|
|
|
|
<>
|
|
|
|
<ToolbarGroup>
|
|
|
|
<BlockMover
|
2024-06-04 12:11:10 +00:00
|
|
|
clientIds={ [ selectedBlockClientId ] }
|
2024-05-28 06:55:14 +00:00
|
|
|
isBlockMoverUpButtonDisabled={
|
2024-06-17 08:56:45 +00:00
|
|
|
isBlockMoverUpButtonDisabled
|
2024-05-28 06:55:14 +00:00
|
|
|
}
|
|
|
|
isBlockMoverDownButtonDisabled={
|
2024-06-17 08:56:45 +00:00
|
|
|
isBlockMoverDownButtonDisabled
|
2024-05-28 06:55:14 +00:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</ToolbarGroup>
|
2024-06-04 12:11:10 +00:00
|
|
|
<Shuffle clientId={ selectedBlockClientId } />
|
2024-06-10 12:33:32 +00:00
|
|
|
<Delete
|
|
|
|
clientId={ selectedBlockClientId }
|
2024-07-16 11:24:55 +00:00
|
|
|
currentBlockName={ currentBlock?.name }
|
2024-06-10 12:33:32 +00:00
|
|
|
nextBlockClientId={ nextBlock?.clientId }
|
|
|
|
/>
|
2024-05-28 06:55:14 +00:00
|
|
|
</>
|
|
|
|
</WPToolbar>
|
|
|
|
</div>
|
2024-06-20 11:26:06 +00:00
|
|
|
</Popover>
|
2024-05-28 06:55:14 +00:00
|
|
|
);
|
|
|
|
};
|