-
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.
List View: Simplify the BlockNavigation component (#31290)
Simplify the `BlockNavigation` component, moving most of the logic inside its `BlockNavigationTree` sub-component.
- Loading branch information
1 parent
6499d70
commit 640eb99
Showing
7 changed files
with
147 additions
and
137 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
81 changes: 0 additions & 81 deletions
81
packages/block-editor/src/components/block-navigation/index.js
This file was deleted.
Oops, something went wrong.
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
88 changes: 88 additions & 0 deletions
88
packages/block-editor/src/components/block-navigation/use-block-navigation-client-ids.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,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 ) { | ||
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(); | ||
} | ||
|
||
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 }; | ||
} |
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