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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions packages/block-library/src/column/edit.js
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 Down Expand Up @@ -73,6 +76,17 @@ function ColumnEdit( {
: 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)' ),
innerBlocksProps[ 'aria-label' ],
currentColumnPosition,
columnsCount
);

return (
<>
<BlockControls>
Expand All @@ -97,7 +111,7 @@ function ColumnEdit( {
/>
</PanelBody>
</InspectorControls>
<div { ...innerBlocksProps } />
<div { ...innerBlocksProps } aria-label={ label } />
diegohaz marked this conversation as resolved.
Show resolved Hide resolved
</>
);
}
Expand Down