-
Notifications
You must be signed in to change notification settings - Fork 4.7k
DRY up ContentBlocksList and BlockInspectorLockedBlocks #51281
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
Changes from all commits
e4569f5
1f46387
d154a8d
06f29ee
6006434
e4421e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useSelect, useDispatch } from '@wordpress/data'; | ||
| import { | ||
| Button, | ||
| __experimentalVStack as VStack, | ||
| __experimentalHStack as HStack, | ||
| FlexItem, | ||
| } from '@wordpress/components'; | ||
| import { getBlockType, __experimentalGetBlockLabel } from '@wordpress/blocks'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { store as blockEditorStore } from '../../store'; | ||
| import BlockIcon from '../block-icon'; | ||
|
|
||
| export default function BlockQuickNavigation( { clientIds } ) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not certain about what to name this component. |
||
| if ( ! clientIds.length ) { | ||
| return null; | ||
| } | ||
| return ( | ||
| <VStack spacing={ 1 }> | ||
| { clientIds.map( ( clientId ) => ( | ||
| <BlockQuickNavigationItem | ||
| key={ clientId } | ||
| clientId={ clientId } | ||
| /> | ||
| ) ) } | ||
| </VStack> | ||
| ); | ||
| } | ||
|
|
||
| function BlockQuickNavigationItem( { clientId } ) { | ||
| const { name, attributes, isSelected } = useSelect( | ||
| ( select ) => { | ||
| const { | ||
| getBlockName, | ||
| getBlockAttributes, | ||
| isBlockSelected, | ||
| hasSelectedInnerBlock, | ||
| } = select( blockEditorStore ); | ||
| return { | ||
| name: getBlockName( clientId ), | ||
| attributes: getBlockAttributes( clientId ), | ||
| isSelected: | ||
| isBlockSelected( clientId ) || | ||
| hasSelectedInnerBlock( clientId, /* deep: */ true ), | ||
| }; | ||
| }, | ||
| [ clientId ] | ||
| ); | ||
| const { selectBlock } = useDispatch( blockEditorStore ); | ||
| const blockType = getBlockType( name ); | ||
| return ( | ||
| <Button | ||
| key={ clientId } | ||
| isPressed={ isSelected } | ||
| onClick={ () => selectBlock( clientId ) } | ||
| > | ||
| <HStack justify="flex-start"> | ||
| <BlockIcon icon={ blockType.icon } /> | ||
| <FlexItem> | ||
| { __experimentalGetBlockLabel( | ||
| blockType, | ||
| attributes, | ||
| 'list-view' | ||
| ) } | ||
| </FlexItem> | ||
| </HStack> | ||
| </Button> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -303,10 +303,13 @@ export const __experimentalGetGlobalBlocksByName = createSelector( | |
| if ( ! blockName ) { | ||
| return EMPTY_ARRAY; | ||
| } | ||
| const blockNames = Array.isArray( blockName ) | ||
| ? blockName | ||
| : [ blockName ]; | ||
| const clientIds = getClientIdsWithDescendants( state ); | ||
| const foundBlocks = clientIds.filter( ( clientId ) => { | ||
| const block = state.blocks.byClientId.get( clientId ); | ||
| return block.name === blockName; | ||
| return blockNames.includes( block.name ); | ||
|
Comment on lines
+306
to
+312
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @noisysocks, do you remember why you introduced this change? I can't find any place that passes the block names array to this selector. |
||
| } ); | ||
| return foundBlocks.length > 0 ? foundBlocks : EMPTY_ARRAY; | ||
| }, | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { | ||
| store as blockEditorStore, | ||
| privateApis as blockEditorPrivateApis, | ||
| } from '@wordpress/block-editor'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { PAGE_CONTENT_BLOCK_TYPES } from '../../page-content-focus/constants'; | ||
| import { unlock } from '../../../private-apis'; | ||
|
|
||
| const { BlockQuickNavigation } = unlock( blockEditorPrivateApis ); | ||
|
|
||
| export default function PageContent() { | ||
| const clientIds = useSelect( | ||
| ( select ) => | ||
| select( blockEditorStore ).__experimentalGetGlobalBlocksByName( | ||
| PAGE_CONTENT_BLOCK_TYPES | ||
| ), | ||
| [] | ||
| ); | ||
| return <BlockQuickNavigation clientIds={ clientIds } />; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a nice trick to get around the array/object referential equality "requirement" and avoid excessive re-renders 🥇
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait... it is? Won't
filterreturn a new array every time? 😅Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You sent me down a rabbit hole of optimising all of this. I made some progress but will tackle it in a separate PR 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about that 😅 I left two more comments in other PR, where returned values need optimization.
This map select returns one dimensional array of client IDs, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment? #50643 (comment) Yes I've been looking into that.
Yes this is a one dimensional array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed 6006434 which reduces the number of re-renders in
PagePanelsandBlockQuickNavigationby selecting the minimal amount of data. I thinkBlockInspectorhere is as good as we can get it without creating a new selector which is not worth it right now imo.I have more performance improvements ready but they're unrelated so they'll be in a separate PR 😀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up: #51319