Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add column count to the column block label #30248

Merged
merged 4 commits into from
Apr 12, 2021
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
36 changes: 27 additions & 9 deletions packages/block-library/src/column/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
__experimentalUnitControl as UnitControl,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { sprintf, __ } from '@wordpress/i18n';

/**
* Internal dependencies
Expand All @@ -36,15 +36,18 @@ function ColumnEdit( {
[ `is-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
} );

const { hasChildBlocks, rootClientId } = useSelect(
const { columnsIds, hasChildBlocks, rootClientId } = useSelect(
( select ) => {
const { getBlockOrder, getBlockRootClientId } = select(
blockEditorStore
);

const rootId = getBlockRootClientId( clientId );

return {
hasChildBlocks: getBlockOrder( clientId ).length > 0,
rootClientId: getBlockRootClientId( clientId ),
rootClientId: rootId,
columnsIds: getBlockOrder( rootId ),
};
},
[ clientId ]
Expand All @@ -66,12 +69,27 @@ function ColumnEdit( {
className: classes,
style: widthWithUnit ? { flexBasis: widthWithUnit } : undefined,
} );
const innerBlocksProps = useInnerBlocksProps( blockProps, {
templateLock,
renderAppender: hasChildBlocks
? undefined
: InnerBlocks.ButtonBlockAppender,
} );

const columnsCount = columnsIds.length;
const currentColumnPosition = columnsIds.indexOf( clientId ) + 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would getBlockIndex and getBlockOrder work for this? When I tried earlier this week I was not able to solve it.


const label = sprintf(
/* translators: 1: Block label (i.e. "Block: Column"), 2: Position of the selected block, 3: Total number of sibling blocks of the same type */
__( '%1$s (%2$d of %3$d)' ),
blockProps[ 'aria-label' ],
currentColumnPosition,
columnsCount
);

const innerBlocksProps = useInnerBlocksProps(
{ ...blockProps, 'aria-label': label },
{
templateLock,
renderAppender: hasChildBlocks
? undefined
: InnerBlocks.ButtonBlockAppender,
}
);

return (
<>
Expand Down