Skip to content

Commit

Permalink
List View: Add a rootClientId prop (#49475)
Browse files Browse the repository at this point in the history
* List View: Add a rootClientId prop

* update readme

* Add a deprecation for the blocks prop

* Update packages/block-editor/src/components/list-view/index.js

Co-authored-by: Daniel Richards <[email protected]>

* Update packages/block-editor/src/components/list-view/index.js

Co-authored-by: Andrew Serong <[email protected]>

* pacify linter

* Remove changes to list-view-sidebar

---------

Co-authored-by: Daniel Richards <[email protected]>
Co-authored-by: Andrew Serong <[email protected]>
  • Loading branch information
3 people authored Apr 6, 2023
1 parent 71acaa6 commit 274cde3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
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 ]
);
}

1 comment on commit 274cde3

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 274cde3.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4625439552
📝 Reported issues:

Please sign in to comment.