diff --git a/packages/block-library/src/block/block.json b/packages/block-library/src/block/block.json index 006c85c0b5db6..87725221803ff 100644 --- a/packages/block-library/src/block/block.json +++ b/packages/block-library/src/block/block.json @@ -8,6 +8,9 @@ "attributes": { "ref": { "type": "number" + }, + "inheritedAlignment": { + "type": "string" } }, "supports": { diff --git a/packages/block-library/src/block/edit.js b/packages/block-library/src/block/edit.js index bf4f933773d6b..51d76c736bab4 100644 --- a/packages/block-library/src/block/edit.js +++ b/packages/block-library/src/block/edit.js @@ -28,11 +28,17 @@ import { } from '@wordpress/block-editor'; import { store as reusableBlocksStore } from '@wordpress/reusable-blocks'; import { ungroup } from '@wordpress/icons'; +import { useEffect } from '@wordpress/element'; -export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) { +export default function ReusableBlockEdit( { + attributes: { ref, inheritedAlignment }, + setAttributes, + clientId, +} ) { const [ hasAlreadyRendered, RecursionProvider ] = useNoRecursiveRenders( ref ); + const { isMissing, hasResolved } = useSelect( ( select ) => { const persistedBlock = select( coreStore ).getEntityRecord( @@ -71,7 +77,29 @@ export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) { ref ); - const blockProps = useBlockProps(); + useEffect( () => { + const alignments = [ 'wide', 'full' ]; + + // Determine the widest setting of all the contained blocks. + const widestAlignment = blocks.reduce( ( accumulator, block ) => { + const { align } = block.attributes; + return alignments.indexOf( align ) > + alignments.indexOf( accumulator ) + ? align + : accumulator; + }, undefined ); + + // Set the attribute of the Reusable block to match the widest + // alignment. + + setAttributes( { + inheritedAlignment: widestAlignment ?? '', + } ); + }, [ blocks ] ); + + const blockProps = useBlockProps( { + 'data-align': inheritedAlignment, + } ); const innerBlocksProps = useInnerBlocksProps( {}, diff --git a/packages/reusable-blocks/src/store/controls.js b/packages/reusable-blocks/src/store/controls.js index e430688684305..56c0df73c855a 100644 --- a/packages/reusable-blocks/src/store/controls.js +++ b/packages/reusable-blocks/src/store/controls.js @@ -90,6 +90,24 @@ const controls = { CONVERT_BLOCKS_TO_REUSABLE: createRegistryControl( ( registry ) => async function ( { clientIds, title } ) { + const blocks = registry + .select( 'core/block-editor' ) + .getBlocksByClientId( clientIds ); + + const alignments = [ 'wide', 'full' ]; + + // Determine the widest setting of all the blocks to be grouped + const widestAlignment = blocks.reduce( + ( accumulator, block ) => { + const { align } = block.attributes; + return alignments.indexOf( align ) > + alignments.indexOf( accumulator ) + ? align + : accumulator; + }, + undefined + ); + const reusableBlock = { title: title || __( 'Untitled Reusable block' ), content: serialize( @@ -106,6 +124,7 @@ const controls = { const newBlock = createBlock( 'core/block', { ref: updatedRecord.id, + align: widestAlignment, } ); registry .dispatch( blockEditorStore )