-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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: Add client ID trees selectors #29902
Merged
+169
−52
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e16589b
Introduce a new getClientIdsTree selector that doesn't need block att…
Copons d34d5df
Integrate the new selector in the BlockNavigation index
Copons 44df848
Use the new selector in all BlockNavigation components
Copons 68ebe35
Add AsyncModeProvider to speed up the persistent list view
Copons 40364c9
Add missing useSelect dependency
Copons d826cf9
Add unit tests
Copons 032ac59
Move AsyncModeProvider outside ListViewSidebar
Copons File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,7 @@ import { noop } from 'lodash'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import { compose } from '@wordpress/compose'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
|
@@ -16,13 +15,41 @@ import { __ } from '@wordpress/i18n'; | |
import BlockNavigationTree from './tree'; | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
function BlockNavigation( { | ||
rootBlock, | ||
rootBlocks, | ||
selectedBlockClientId, | ||
selectBlock, | ||
export default function BlockNavigation( { | ||
onSelect = noop, | ||
__experimentalFeatures, | ||
} ) { | ||
const { rootBlock, rootBlocks, selectedBlockClientId } = useSelect( | ||
( select ) => { | ||
const { | ||
getBlockHierarchyRootClientId, | ||
getSelectedBlockClientId, | ||
__unstableGetClientIdsTree, | ||
__unstableGetClientIdWithClientIdsTree, | ||
} = select( blockEditorStore ); | ||
|
||
const _selectedBlockClientId = getSelectedBlockClientId(); | ||
const _rootBlocks = __unstableGetClientIdsTree(); | ||
const _rootBlock = _selectedBlockClientId | ||
? __unstableGetClientIdWithClientIdsTree( | ||
getBlockHierarchyRootClientId( _selectedBlockClientId ) | ||
) | ||
: null; | ||
|
||
return { | ||
rootBlock: _rootBlock, | ||
rootBlocks: _rootBlocks, | ||
selectedBlockClientId: _selectedBlockClientId, | ||
}; | ||
} | ||
); | ||
const { selectBlock } = useDispatch( blockEditorStore ); | ||
|
||
function selectEditorBlock( clientId ) { | ||
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. Any reason to use 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 really, no. I don't think there's any practical difference in this case. |
||
selectBlock( clientId ); | ||
onSelect( clientId ); | ||
} | ||
|
||
if ( ! rootBlocks || rootBlocks.length === 0 ) { | ||
return null; | ||
} | ||
|
@@ -41,39 +68,10 @@ function BlockNavigation( { | |
<BlockNavigationTree | ||
blocks={ hasHierarchy ? [ rootBlock ] : rootBlocks } | ||
selectedBlockClientId={ selectedBlockClientId } | ||
selectBlock={ selectBlock } | ||
selectBlock={ selectEditorBlock } | ||
__experimentalFeatures={ __experimentalFeatures } | ||
showNestedBlocks | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default compose( | ||
withSelect( ( select ) => { | ||
const { | ||
getSelectedBlockClientId, | ||
getBlockHierarchyRootClientId, | ||
__unstableGetBlockWithBlockTree, | ||
__unstableGetBlockTree, | ||
} = select( blockEditorStore ); | ||
const selectedBlockClientId = getSelectedBlockClientId(); | ||
return { | ||
rootBlocks: __unstableGetBlockTree(), | ||
rootBlock: selectedBlockClientId | ||
? __unstableGetBlockWithBlockTree( | ||
getBlockHierarchyRootClientId( selectedBlockClientId ) | ||
) | ||
: null, | ||
selectedBlockClientId, | ||
}; | ||
} ), | ||
withDispatch( ( dispatch, { onSelect = noop } ) => { | ||
return { | ||
selectBlock( clientId ) { | ||
dispatch( blockEditorStore ).selectBlock( clientId ); | ||
onSelect( clientId ); | ||
}, | ||
}; | ||
} ) | ||
)( BlockNavigation ); |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
General question, why the
_
prefix?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.
It's because those names are already declared in the upper scope.
The
_
is just simpler than figure out some naming switcheroo that might compromise the readability.(Also, I've seen it used plenty of times in Gutenberg, sometimes to mark a constant as "private", but often in this exact same context of select hooks.)