import clsx from 'clsx'; import { __ } from '@wordpress/i18n'; import { ToggleControl, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; import type { IconType } from '@wordpress/components'; import { InspectorControls, useInnerBlocksProps, BlockControls, BlockVerticalAlignmentToolbar, __experimentalBlockVariationPicker, useBlockProps, store as blockEditorStore, } from '@wordpress/block-editor'; import { useDispatch, useSelect, useRegistry } from '@wordpress/data'; import { createBlocksFromInnerBlocksTemplate, store as blocksStore, } from '@wordpress/blocks'; import type { BlockEditProps } from '@wordpress/blocks'; import { useToolsPanelDropdownMenuProps } from '../utils/hooks'; import type { ColumnInspectorControlsProps, ColumnsAttributes, ColumnsEditContainerProps, ColumnsPlaceholderProps, } from './types'; const DEFAULT_BLOCK = { name: 'core/column', }; function ColumnInspectorControls( { setAttributes, isStackedOnMobile, }: ColumnInspectorControlsProps ) { const dropdownMenuProps = useToolsPanelDropdownMenuProps(); return ( { setAttributes( { isStackedOnMobile: true, } ); } } dropdownMenuProps={ dropdownMenuProps } > isStackedOnMobile !== true } onDeselect={ () => setAttributes( { isStackedOnMobile: true, } ) } > setAttributes( { isStackedOnMobile: ! isStackedOnMobile, } ) } /> ); } function ColumnsEditContainer( { attributes, setAttributes, clientId, }: ColumnsEditContainerProps ) { const { isStackedOnMobile, verticalAlignment, templateLock } = attributes; const registry = useRegistry(); const { getBlockOrder } = useSelect( blockEditorStore ); const { updateBlockAttributes } = useDispatch( blockEditorStore ); const classes = clsx( { [ `are-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment, [ `is-not-stacked-on-mobile` ]: ! isStackedOnMobile, } ); const blockProps = useBlockProps( { className: classes, } ); const innerBlocksProps = useInnerBlocksProps( blockProps, { defaultBlock: DEFAULT_BLOCK, directInsert: true, orientation: 'horizontal', renderAppender: false, templateLock, } ); /** * Update all child Column blocks with a new vertical alignment setting * based on whatever alignment is passed in. This allows change to parent * to override anything set on a individual column basis. * * @param newVerticalAlignment The vertical alignment setting. */ function updateAlignment( newVerticalAlignment: | 'top' | 'center' | 'bottom' | 'stretch' | 'space-between' | undefined ) { const innerBlockClientIds = getBlockOrder( clientId ); // Update own and child Column block vertical alignments. // This is a single action; the batching prevents creating multiple history records. registry.batch( () => { setAttributes( { verticalAlignment: newVerticalAlignment } ); updateBlockAttributes( innerBlockClientIds, { verticalAlignment: newVerticalAlignment, } ); } ); } return ( <>
); } function Placeholder( { clientId, name, setAttributes, }: ColumnsPlaceholderProps ) { const { blockType, variations } = useSelect( ( select ) => { const { getBlockVariations, getBlockType } = select( blocksStore ); return { blockType: getBlockType( name ), variations: getBlockVariations( name, 'block' ), }; }, [ name ] ); const { replaceInnerBlocks } = useDispatch( blockEditorStore ); const blockProps = useBlockProps(); return (
<__experimentalBlockVariationPicker icon={ ( blockType?.icon as { src?: IconType } )?.src } label={ blockType?.title } variations={ variations! } instructions={ __( 'Divide into columns. Select a layout:' ) } onSelect={ ( nextVariation ) => { if ( nextVariation?.attributes ) { setAttributes( nextVariation.attributes ); } if ( nextVariation?.innerBlocks ) { replaceInnerBlocks( clientId, createBlocksFromInnerBlocksTemplate( nextVariation.innerBlocks as Parameters< typeof createBlocksFromInnerBlocksTemplate >[ 0 ] ), true ); } } } />
); } /** * Renders the `core/columns` block in the editor. * * Until the block has inner blocks it renders a variation picker; afterwards it * renders the columns container. Every prop is forwarded to whichever of the two * is rendered. * * @param props Component props. * * @return React element. */ const ColumnsEdit = ( props: BlockEditProps< ColumnsAttributes > ) => { const { clientId } = props; const hasInnerBlocks = useSelect( ( select ) => select( blockEditorStore ).getBlocks( clientId ).length > 0, [ clientId ] ); const Component = hasInnerBlocks ? ColumnsEditContainer : Placeholder; return ; }; export default ColumnsEdit;