-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
List View: Simplify the BlockNavigation component #31290
Changes from all commits
b9e59f3
6238fe0
6f96374
06a0d01
91acff1
ef1b5fa
757f20b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
|
||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { isClientIdSelected } from './utils'; | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
const useBlockNavigationSelectedClientIds = ( | ||
__experimentalPersistentListViewFeatures | ||
) => | ||
useSelect( | ||
( select ) => { | ||
const { | ||
getSelectedBlockClientId, | ||
getSelectedBlockClientIds, | ||
} = select( blockEditorStore ); | ||
|
||
if ( __experimentalPersistentListViewFeatures ) { | ||
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. Does this make any difference somewhere? 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. This is a confusing function 😬 I'd expect it to return an We should just 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. Well what do you know, I think you're right! 😄 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. Or actually... The problem here is that some consumers (Dropdown to be specific) really expect the selected client ID to be single and string, or null otherwise, whereas the Persistent List View wants the whole multiselection array. For example, Using those two distinct functions basically saves a bunch of additional checks and conversions to make sure everything keeps receiving the parameters with the expected types. This said, I think the major cause of confusion here is this: const isSingleBlockSelected =
selectedClientIds && ! Array.isArray( selectedClientIds ); Single selections can be arrays as well, and the naming here doesn't make it clear. |
||
return getSelectedBlockClientIds(); | ||
} | ||
|
||
return getSelectedBlockClientId(); | ||
}, | ||
[ __experimentalPersistentListViewFeatures ] | ||
); | ||
|
||
const useBlockNavigationClientIdsTree = ( | ||
blocks, | ||
selectedClientIds, | ||
showOnlyCurrentHierarchy | ||
) => | ||
useSelect( | ||
( select ) => { | ||
const { | ||
getBlockHierarchyRootClientId, | ||
__unstableGetClientIdsTree, | ||
__unstableGetClientIdWithClientIdsTree, | ||
} = select( blockEditorStore ); | ||
|
||
if ( blocks ) { | ||
return blocks; | ||
} | ||
|
||
const isSingleBlockSelected = | ||
selectedClientIds && ! Array.isArray( selectedClientIds ); | ||
if ( ! showOnlyCurrentHierarchy || ! isSingleBlockSelected ) { | ||
return __unstableGetClientIdsTree(); | ||
} | ||
|
||
const rootBlock = __unstableGetClientIdWithClientIdsTree( | ||
getBlockHierarchyRootClientId( selectedClientIds ) | ||
); | ||
if ( ! rootBlock ) { | ||
return __unstableGetClientIdsTree(); | ||
} | ||
Addison-Stavlo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const hasHierarchy = | ||
! isClientIdSelected( rootBlock.clientId, selectedClientIds ) || | ||
( rootBlock.innerBlocks && rootBlock.innerBlocks.length !== 0 ); | ||
if ( hasHierarchy ) { | ||
return [ rootBlock ]; | ||
} | ||
|
||
return __unstableGetClientIdsTree(); | ||
}, | ||
[ blocks, selectedClientIds, showOnlyCurrentHierarchy ] | ||
); | ||
|
||
export default function useBlockNavigationClientIds( | ||
blocks, | ||
showOnlyCurrentHierarchy, | ||
__experimentalPersistentListViewFeatures | ||
) { | ||
const selectedClientIds = useBlockNavigationSelectedClientIds( | ||
__experimentalPersistentListViewFeatures | ||
); | ||
const clientIdsTree = useBlockNavigationClientIdsTree( | ||
blocks, | ||
selectedClientIds, | ||
showOnlyCurrentHierarchy | ||
); | ||
return { clientIdsTree, selectedClientIds }; | ||
} |
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.
Should we expose/handle this property explicitly and set it as default to
true
? Currently we have four usages of this component where all pass this prop withtrue
.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.
Even though we almost have this enabled everywhere, personally I'd still prefer to
opt-in
rather thanopt-out
.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 don't have a strong opinion either way, to be honest.
I haven't paid much attention to that prop; it has been there for quite a while now, and I have no idea why it was introduced instead of just defaulting it to true. 🤔