-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DRY up the two places we show a list of content blocks
- Loading branch information
1 parent
62249b7
commit 6cc9603
Showing
5 changed files
with
89 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/block-editor/src/components/content-blocks-list/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { | ||
Button, | ||
__experimentalVStack as VStack, | ||
__experimentalHStack as HStack, | ||
FlexItem, | ||
} from '@wordpress/components'; | ||
import { getBlockType } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../store'; | ||
import BlockIcon from '../block-icon'; | ||
|
||
export default function ContentBlocksList( { rootClientId } ) { | ||
const { contentBlocks, selectedBlockClientId } = useSelect( | ||
( select ) => { | ||
const { | ||
__experimentalGetContentClientIdsTree, | ||
getBlockName, | ||
getSelectedBlockClientId, | ||
} = select( blockEditorStore ); | ||
return { | ||
contentBlocks: __experimentalGetContentClientIdsTree( | ||
rootClientId | ||
) | ||
.map( ( block ) => ( { | ||
...block, | ||
blockName: getBlockName( block.clientId ), | ||
} ) ) | ||
.filter( | ||
( { blockName } ) => blockName !== 'core/list-item' | ||
), | ||
selectedBlockClientId: getSelectedBlockClientId(), | ||
}; | ||
}, | ||
[ rootClientId ] | ||
); | ||
|
||
const { selectBlock } = useDispatch( blockEditorStore ); | ||
|
||
if ( ! contentBlocks.length ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<VStack spacing={ 1 }> | ||
{ contentBlocks.map( ( { clientId, blockName } ) => { | ||
const blockType = getBlockType( blockName ); | ||
return ( | ||
<Button | ||
key={ clientId } | ||
isPressed={ clientId === selectedBlockClientId } | ||
onClick={ () => selectBlock( clientId ) } | ||
> | ||
<HStack justify="flex-start"> | ||
<BlockIcon icon={ blockType.icon } /> | ||
<FlexItem>{ blockType.title }</FlexItem> | ||
</HStack> | ||
</Button> | ||
); | ||
} ) } | ||
</VStack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters