Skip to content
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: Add a rootClientId prop #49475

Merged
merged 7 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/list-view/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

The ListView component provides an overview of the hierarchical structure of all blocks in the editor. The blocks are presented vertically one below the other.

By providing the `rootClientId` prop you can restrict the blocks that are shown to only children of the block with that client id.

Blocks that have child blocks (such as group or column blocks) are presented with the parent at the top and the nested children below.

In addition to presenting the structure of the blocks in the editor, the ListView component lets users navigate to each block by clicking on its line in the hierarchy tree. Multiple blocks at the same level of nesting can be selected by holding down the `SHIFT` key and clicking blocks within the list.
Expand Down
20 changes: 18 additions & 2 deletions packages/block-editor/src/components/list-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@wordpress/compose';
import { __experimentalTreeGrid as TreeGrid } from '@wordpress/components';
import { AsyncModeProvider, useSelect } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import {
useCallback,
useEffect,
Expand Down Expand Up @@ -56,11 +57,12 @@ export const BLOCK_LIST_ITEM_HEIGHT = 36;
*
* @param {Object} props Components props.
* @param {string} props.id An HTML element id for the root element of ListView.
* @param {Array} props.blocks Custom subset of block client IDs to be used instead of the default hierarchy.
* @param {Array} props.blocks _deprecated_ Custom subset of block client IDs to be used instead of the default hierarchy.
* @param {?boolean} props.showBlockMovers Flag to enable block movers. Defaults to `false`.
* @param {?boolean} props.isExpanded Flag to determine whether nested levels are expanded by default. Defaults to `false`.
* @param {?boolean} props.showAppender Flag to show or hide the block appender. Defaults to `false`.
* @param {?ComponentType} props.blockSettingsMenu Optional more menu substitution. Defaults to the standard `BlockSettingsDropdown` component.
* @param {string} props.rootClientId The client id of the root block from which we determine the blocks to show in the list.
* @param {Ref} ref Forwarded ref
*/
function ListViewComponent(
Expand All @@ -71,11 +73,23 @@ function ListViewComponent(
isExpanded = false,
showAppender = false,
blockSettingsMenu: BlockSettingsMenu = BlockSettingsDropdown,
rootClientId,
},
ref
) {
// This can be removed once we no longer need to support the blocks prop.
if ( blocks ) {
deprecated(
'`blocks` property in `wp.blockEditor.__experimentalListView`',
{
since: '6.3',
alternative: '`rootClientId` property',
}
);
}

const { clientIdsTree, draggedClientIds, selectedClientIds } =
useListViewClientIds( blocks );
useListViewClientIds( { blocks, rootClientId } );

const { visibleBlockCount, shouldShowInnerBlocks } = useSelect(
( select ) => {
Expand Down Expand Up @@ -219,6 +233,7 @@ function ListViewComponent(
<ListViewContext.Provider value={ contextValue }>
<ListViewBranch
blocks={ clientIdsTree }
parentId={ rootClientId }
selectBlock={ selectEditorBlock }
showBlockMovers={ showBlockMovers }
fixedListWindow={ fixedListWindow }
Expand All @@ -241,6 +256,7 @@ export default forwardRef( ( props, ref ) => {
{ ...props }
showAppender={ false }
blockSettingsMenu={ BlockSettingsDropdown }
rootClientId={ null }
/>
);
} );
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useSelect } from '@wordpress/data';
*/
import { store as blockEditorStore } from '../../store';

export default function useListViewClientIds( blocks ) {
export default function useListViewClientIds( { blocks, rootClientId } ) {
return useSelect(
( select ) => {
const {
Expand All @@ -21,9 +21,11 @@ export default function useListViewClientIds( blocks ) {
return {
selectedClientIds: getSelectedBlockClientIds(),
draggedClientIds: getDraggedBlockClientIds(),
clientIdsTree: blocks ? blocks : __unstableGetClientIdsTree(),
clientIdsTree: blocks
? blocks
: __unstableGetClientIdsTree( rootClientId ),
};
},
[ blocks ]
[ blocks, rootClientId ]
);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the changes in this file must be reverted before merge

Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/**
* WordPress dependencies
*/
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import {
privateApis as blockEditorPrivateApis,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { Button } from '@wordpress/components';
import {
useFocusOnMount,
useFocusReturn,
useInstanceId,
useMergeRefs,
} from '@wordpress/compose';
import { useDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { closeSmall } from '@wordpress/icons';
import { ESCAPE } from '@wordpress/keycodes';
Expand All @@ -35,6 +38,10 @@ export default function ListViewSidebar() {
const instanceId = useInstanceId( ListViewSidebar );
const labelId = `edit-site-editor__list-view-panel-label-${ instanceId }`;
const { PrivateListView } = unlock( blockEditorPrivateApis );
const clientIdsTree = useSelect( ( select ) => {
const { __unstableGetClientIdsTree } = select( blockEditorStore );
return __unstableGetClientIdsTree();
} );
return (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div
Expand All @@ -60,7 +67,11 @@ export default function ListViewSidebar() {
focusOnMountRef,
] ) }
>
<PrivateListView />
<PrivateListView
rootClientId={
clientIdsTree[ 0 ].innerBlocks[ 0 ].clientId
}
/>
</div>
</div>
);
Expand Down