Skip to content

Commit

Permalink
Add prop to control default expand/collapse state of ListView nodes (W…
Browse files Browse the repository at this point in the history
…ordPress#39486)

* Add prop to control default expand/collapse state

* Update windowing to account for default expanded state

* Remove implementation
  • Loading branch information
getdave authored and jostnes committed Mar 23, 2022
1 parent 2c84790 commit 2d30a6f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
52 changes: 37 additions & 15 deletions packages/block-editor/src/components/list-view/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,59 @@ import { isClientIdSelected } from './utils';
* When a block is collapsed, we do not count their children as part of that total. In the current drag
* implementation dragged blocks and their children are not counted.
*
* @param {Object} block block tree
* @param {Object} expandedState state that notes which branches are collapsed
* @param {Array} draggedClientIds a list of dragged client ids
* @param {Object} block block tree
* @param {Object} expandedState state that notes which branches are collapsed
* @param {Array} draggedClientIds a list of dragged client ids
* @param {boolean} isExpandedByDefault flag to determine the default fallback expanded state.
* @return {number} block count
*/
function countBlocks( block, expandedState, draggedClientIds ) {
function countBlocks(
block,
expandedState,
draggedClientIds,
isExpandedByDefault
) {
const isDragged = draggedClientIds?.includes( block.clientId );
if ( isDragged ) {
return 0;
}
const isExpanded = expandedState[ block.clientId ] ?? true;
const isExpanded = expandedState[ block.clientId ] ?? isExpandedByDefault;

if ( isExpanded ) {
return (
1 +
block.innerBlocks.reduce(
countReducer( expandedState, draggedClientIds ),
countReducer(
expandedState,
draggedClientIds,
isExpandedByDefault
),
0
)
);
}
return 1;
}
const countReducer = ( expandedState, draggedClientIds ) => (
count,
block
) => {
const countReducer = (
expandedState,
draggedClientIds,
isExpandedByDefault
) => ( count, block ) => {
const isDragged = draggedClientIds?.includes( block.clientId );
if ( isDragged ) {
return count;
}
const isExpanded = expandedState[ block.clientId ] ?? true;
const isExpanded = expandedState[ block.clientId ] ?? isExpandedByDefault;
if ( isExpanded && block.innerBlocks.length > 0 ) {
return count + countBlocks( block, expandedState, draggedClientIds );
return (
count +
countBlocks(
block,
expandedState,
draggedClientIds,
isExpandedByDefault
)
);
}
return count + 1;
};
Expand All @@ -72,6 +92,7 @@ function ListViewBranch( props ) {
isBranchSelected = false,
listPosition = 0,
fixedListWindow,
expandNested = true,
} = props;

const {
Expand All @@ -93,14 +114,14 @@ function ListViewBranch( props ) {
nextPosition += countBlocks(
filteredBlocks[ index - 1 ],
expandedState,
draggedClientIds
draggedClientIds,
expandNested
);
}

const usesWindowing = __experimentalPersistentListViewFeatures;

const { itemInView } = fixedListWindow;

const blockInView =
! usesWindowing || itemInView( nextPosition );

Expand All @@ -113,7 +134,7 @@ function ListViewBranch( props ) {
showNestedBlocks && !! innerBlocks && !! innerBlocks.length;

const isExpanded = hasNestedBlocks
? expandedState[ clientId ] ?? true
? expandedState[ clientId ] ?? expandNested
: undefined;

const isDragged = !! draggedClientIds?.includes( clientId );
Expand Down Expand Up @@ -165,6 +186,7 @@ function ListViewBranch( props ) {
fixedListWindow={ fixedListWindow }
isBranchSelected={ isSelectedBranch }
selectedClientIds={ selectedClientIds }
expandNested={ expandNested }
/>
) }
</AsyncModeProvider>
Expand Down
3 changes: 3 additions & 0 deletions packages/block-editor/src/components/list-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const BLOCK_LIST_ITEM_HEIGHT = 36;
* @param {boolean} props.__experimentalPersistentListViewFeatures Flag to enable features for the Persistent List View experiment.
* @param {boolean} props.__experimentalHideContainerBlockActions Flag to hide actions of top level blocks (like core/widget-area)
* @param {string} props.id Unique identifier for the root list element (primarily for a11y purposes).
* @param {boolean} props.expandNested Flag to determine whether nested levels are expanded by default.
* @param {Object} ref Forwarded ref
*/
function ListView(
Expand All @@ -71,6 +72,7 @@ function ListView(
showNestedBlocks,
showBlockMovers,
id,
expandNested,
...props
},
ref
Expand Down Expand Up @@ -223,6 +225,7 @@ function ListView(
showBlockMovers={ showBlockMovers }
fixedListWindow={ fixedListWindow }
selectedClientIds={ selectedClientIds }
expandNested={ expandNested }
{ ...props }
/>
</ListViewContext.Provider>
Expand Down

0 comments on commit 2d30a6f

Please sign in to comment.