Skip to content

Commit

Permalink
Show content block as selected if a descendant is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Apr 24, 2023
1 parent 6cc9603 commit 5d317d8
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions packages/block-editor/src/components/content-blocks-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,24 @@ import { store as blockEditorStore } from '../../store';
import BlockIcon from '../block-icon';

export default function ContentBlocksList( { rootClientId } ) {
const { contentBlocks, selectedBlockClientId } = useSelect(
const contentBlocks = useSelect(
( select ) => {
const {
getSelectedBlockClientId,
__experimentalGetContentClientIdsTree,
getBlockName,
getSelectedBlockClientId,
} = select( blockEditorStore );
return {
contentBlocks: __experimentalGetContentClientIdsTree(
rootClientId
)
.map( ( block ) => ( {
...block,
blockName: getBlockName( block.clientId ),
} ) )
.filter(
( { blockName } ) => blockName !== 'core/list-item'
const selectedBlockClientId = getSelectedBlockClientId();
return __experimentalGetContentClientIdsTree( rootClientId )
.map( ( block ) => ( {
...block,
blockName: getBlockName( block.clientId ),
isSelected: blockHasDescendant(
block,
selectedBlockClientId
),
selectedBlockClientId: getSelectedBlockClientId(),
};
} ) )
.filter( ( { blockName } ) => blockName !== 'core/list-item' );
},
[ rootClientId ]
);
Expand All @@ -49,12 +47,12 @@ export default function ContentBlocksList( { rootClientId } ) {

return (
<VStack spacing={ 1 }>
{ contentBlocks.map( ( { clientId, blockName } ) => {
{ contentBlocks.map( ( { clientId, blockName, isSelected } ) => {
const blockType = getBlockType( blockName );
return (
<Button
key={ clientId }
isPressed={ clientId === selectedBlockClientId }
isPressed={ isSelected }
onClick={ () => selectBlock( clientId ) }
>
<HStack justify="flex-start">
Expand All @@ -67,3 +65,12 @@ export default function ContentBlocksList( { rootClientId } ) {
</VStack>
);
}

function blockHasDescendant( block, clientId ) {
return (
block.clientId === clientId ||
block.innerBlocks.some( ( child ) =>
blockHasDescendant( child, clientId )
)
);
}

0 comments on commit 5d317d8

Please sign in to comment.