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-05-28 08:16:25 +00:00
|
|
|
import { ToolbarGroup, Toolbar as WPToolbar } from '@wordpress/components';
|
2024-06-04 12:11:10 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { useEffect, useMemo, useState } from '@wordpress/element';
|
|
|
|
|
2024-05-28 06:55:14 +00:00
|
|
|
import {
|
|
|
|
BlockMover,
|
|
|
|
BlockPopover,
|
|
|
|
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-04 12:11:10 +00:00
|
|
|
|
|
|
|
const isHomepageUrl = ( path: string ) => {
|
|
|
|
return path === '/customize-store/assembler-hub/homepage';
|
|
|
|
};
|
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 firstBlock = useMemo( () => {
|
|
|
|
return allBlocks.find(
|
|
|
|
( block: BlockInstance ) => block.name !== 'core/template-part'
|
|
|
|
);
|
|
|
|
}, [ allBlocks ] );
|
|
|
|
|
|
|
|
const query = useQuery();
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
const path = query.path;
|
|
|
|
|
|
|
|
setIsHomepageSidebarOpen( isHomepageUrl( path ) );
|
|
|
|
}, [ query ] );
|
2024-05-28 06:55:14 +00:00
|
|
|
const { isPreviousBlockTemplatePart, isNextBlockTemplatePart } =
|
|
|
|
useMemo( () => {
|
|
|
|
return {
|
|
|
|
isPreviousBlockTemplatePart:
|
|
|
|
previousBlock?.name === 'core/template-part',
|
|
|
|
isNextBlockTemplatePart:
|
|
|
|
nextBlock?.name === 'core/template-part',
|
|
|
|
};
|
|
|
|
}, [ nextBlock?.name, previousBlock?.name ] );
|
|
|
|
|
2024-06-04 12:11:10 +00:00
|
|
|
const selectedBlockClientId =
|
|
|
|
currentBlock?.clientId ?? firstBlock?.clientId;
|
|
|
|
|
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 ] );
|
|
|
|
|
|
|
|
if (
|
|
|
|
! isHomepageSidebarOpen ||
|
|
|
|
! selectedBlockClientId ||
|
|
|
|
isNoBlocksPlaceholderPresent ||
|
|
|
|
isHeaderOrFooter
|
|
|
|
) {
|
2024-06-04 12:11:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-05-28 06:55:14 +00:00
|
|
|
return (
|
2024-06-04 12:11:10 +00:00
|
|
|
<BlockPopover clientId={ selectedBlockClientId }>
|
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={
|
|
|
|
isPreviousBlockTemplatePart
|
|
|
|
}
|
|
|
|
isBlockMoverDownButtonDisabled={
|
|
|
|
isNextBlockTemplatePart
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</ToolbarGroup>
|
2024-06-04 12:11:10 +00:00
|
|
|
<Shuffle clientId={ selectedBlockClientId } />
|
2024-06-10 12:33:32 +00:00
|
|
|
<Delete
|
|
|
|
clientId={ selectedBlockClientId }
|
|
|
|
nextBlockClientId={ nextBlock?.clientId }
|
|
|
|
/>
|
2024-05-28 06:55:14 +00:00
|
|
|
</>
|
|
|
|
</WPToolbar>
|
|
|
|
</div>
|
|
|
|
</BlockPopover>
|
|
|
|
);
|
|
|
|
};
|