/**
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import { __, sprintf, _n } from '@wordpress/i18n';
import {
FlexItem,
SearchControl,
__experimentalHStack as HStack,
__experimentalText as WCText,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import {
useState,
useEffect,
useRef,
useDeferredValue,
memo,
} from '@wordpress/element';
import {
BlockIcon,
privateApis as blockEditorPrivateApis,
// @ts-expect-error: Not typed yet.
} from '@wordpress/block-editor';
import { useDebounce } from '@wordpress/compose';
import { speak } from '@wordpress/a11y';
/**
* Internal dependencies
*/
import { useBlockVariations } from './variations/variations-panel';
import { ScreenHeader } from './screen-header';
import { NavigationButtonAsItem } from './navigation-button';
import { useSetting } from './hooks';
import { unlock } from './lock-unlock';
const {
useHasDimensionsPanel,
useHasTypographyPanel,
useHasBorderPanel,
useSettingsForBlockElement,
useHasColorPanel,
useHasBackgroundPanel,
} = unlock( blockEditorPrivateApis );
function useSortedBlockTypes() {
const blockItems = useSelect(
( select ) => select( blocksStore ).getBlockTypes(),
[]
);
// Ensure core blocks are prioritized in the returned results,
// because third party blocks can be registered earlier than
// the core blocks (usually by using the `init` action),
// thus affecting the display order.
// We don't sort reusable blocks as they are handled differently.
const groupByType = ( blocks: any, block: any ) => {
const { core, noncore } = blocks;
const type = block.name.startsWith( 'core/' ) ? core : noncore;
type.push( block );
return blocks;
};
const { core: coreItems, noncore: nonCoreItems } = blockItems.reduce(
groupByType,
{ core: [], noncore: [] }
);
return [ ...coreItems, ...nonCoreItems ];
}
export function useBlockHasGlobalStyles( blockName: string ) {
const [ rawSettings ] = useSetting( '', blockName );
const settings = useSettingsForBlockElement( rawSettings, blockName );
const hasTypographyPanel = useHasTypographyPanel( settings );
const hasColorPanel = useHasColorPanel( settings );
const hasBackgroundPanel = useHasBackgroundPanel( settings );
const hasBorderPanel = useHasBorderPanel( settings );
const hasDimensionsPanel = useHasDimensionsPanel( settings );
const hasLayoutPanel = hasBorderPanel || hasDimensionsPanel;
const hasVariationsPanel = !! useBlockVariations( blockName )?.length;
const hasGlobalStyles =
hasTypographyPanel ||
hasColorPanel ||
hasBackgroundPanel ||
hasLayoutPanel ||
hasVariationsPanel;
return hasGlobalStyles;
}
interface BlockMenuItemProps {
block: any;
}
function BlockMenuItem( { block }: BlockMenuItemProps ) {
const hasBlockMenuItem = useBlockHasGlobalStyles( block.name );
if ( ! hasBlockMenuItem ) {
return null;
}
return (