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

Block Editor: Improve data selector for BlockQuickNavigationItem #51429

Merged
merged 2 commits into from
Jun 14, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
__experimentalHStack as HStack,
FlexItem,
} from '@wordpress/components';
import { getBlockType, __experimentalGetBlockLabel } from '@wordpress/blocks';
import {
__experimentalGetBlockLabel,
store as blocksStore,
} from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -33,17 +36,28 @@ export default function BlockQuickNavigation( { clientIds } ) {
}

function BlockQuickNavigationItem( { clientId } ) {
const { name, attributes, isSelected } = useSelect(
const { name, icon, isSelected } = useSelect(
( select ) => {
const {
getBlockName,
getBlockAttributes,
isBlockSelected,
hasSelectedInnerBlock,
} = select( blockEditorStore );
const { getBlockType } = select( blocksStore );

const blockType = getBlockType( getBlockName( clientId ) );
const attributes = getBlockAttributes( clientId );

return {
name: getBlockName( clientId ),
attributes: getBlockAttributes( clientId ),
name:
blockType &&
__experimentalGetBlockLabel(
blockType,
attributes,
'list-view'
),
icon: blockType?.icon,
isSelected:
isBlockSelected( clientId ) ||
hasSelectedInnerBlock( clientId, /* deep: */ true ),
Expand All @@ -52,22 +66,15 @@ function BlockQuickNavigationItem( { clientId } ) {
[ clientId ]
);
const { selectBlock } = useDispatch( blockEditorStore );
const blockType = getBlockType( name );

return (
<Button
key={ clientId }
Copy link
Member Author

Choose a reason for hiding this comment

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

A small clean-up.

isPressed={ isSelected }
onClick={ () => selectBlock( clientId ) }
>
<HStack justify="flex-start">
<BlockIcon icon={ blockType.icon } />
<FlexItem>
{ __experimentalGetBlockLabel(
blockType,
attributes,
'list-view'
) }
</FlexItem>
<BlockIcon icon={ icon } />
<FlexItem>{ name }</FlexItem>
</HStack>
</Button>
);
Expand Down