Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 55 additions & 35 deletions packages/block-editor/src/hooks/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,50 @@ export function addAttribute( settings ) {
return settings;
}

function BlockEditAlignmentToolbarControls( {
blockName,
attributes,
setAttributes,
} ) {
// Compute the block valid alignments by taking into account,
// if the theme supports wide alignments or not and the layout's
// availble alignments. We do that for conditionally rendering
// Slot.
const blockAllowedAlignments = getValidAlignments(
getBlockSupport( blockName, 'align' ),
hasBlockSupport( blockName, 'alignWide', true )
);

const validAlignments = useAvailableAlignments(
blockAllowedAlignments
).map( ( { name } ) => name );
const blockEditingMode = useBlockEditingMode();
if ( ! validAlignments.length || blockEditingMode !== 'default' ) {
return null;
}

const updateAlignment = ( nextAlign ) => {
if ( ! nextAlign ) {
const blockType = getBlockType( blockName );
const blockDefaultAlign = blockType?.attributes?.align?.default;
if ( blockDefaultAlign ) {
nextAlign = '';
}
}
setAttributes( { align: nextAlign } );
};

return (
<BlockControls group="block" __experimentalShareWithChildBlocks>
<BlockAlignmentControl
value={ attributes.align }
onChange={ updateAlignment }
controls={ validAlignments }
/>
</BlockControls>
);
}

/**
* Override the default edit UI to include new toolbar controls for block
* alignment, if block defines support.
Expand All @@ -118,46 +162,22 @@ export function addAttribute( settings ) {
*/
export const withToolbarControls = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const blockEdit = <BlockEdit key="edit" { ...props } />;
const { name: blockName } = props;
// Compute the block valid alignments by taking into account,
// if the theme supports wide alignments or not and the layout's
// availble alignments. We do that for conditionally rendering
// Slot.
const blockAllowedAlignments = getValidAlignments(
getBlockSupport( blockName, 'align' ),
hasBlockSupport( blockName, 'alignWide', true )
const hasAlignmentSupport = hasBlockSupport(
props.name,
'align',
false
);

const validAlignments = useAvailableAlignments(
blockAllowedAlignments
).map( ( { name } ) => name );
const blockEditingMode = useBlockEditingMode();
if ( ! validAlignments.length || blockEditingMode !== 'default' ) {
return blockEdit;
}

const updateAlignment = ( nextAlign ) => {
if ( ! nextAlign ) {
const blockType = getBlockType( props.name );
const blockDefaultAlign = blockType?.attributes?.align?.default;
if ( blockDefaultAlign ) {
nextAlign = '';
}
}
props.setAttributes( { align: nextAlign } );
};

return (
<>
<BlockControls group="block" __experimentalShareWithChildBlocks>
<BlockAlignmentControl
value={ props.attributes.align }
onChange={ updateAlignment }
controls={ validAlignments }
{ hasAlignmentSupport && (
<BlockEditAlignmentToolbarControls
blockName={ props.name }
attributes={ props.attributes }
setAttributes={ props.setAttributes }
/>
</BlockControls>
{ blockEdit }
) }
<BlockEdit key="edit" { ...props } />
</>
);
},
Expand Down