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

Persistent List View: Add visual support for multiple selected blocks #29878

Merged
merged 6 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 11 additions & 10 deletions packages/block-editor/src/components/block-navigation/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,19 @@ export default function BlockNavigationBlock( {
highlightBlock( clientId, false );
};

const classes = classnames( {
'is-selected': isSelected,
'is-branch-selected':
withExperimentalPersistentListViewFeatures && isBranchSelected,
'is-last-of-selected-branch':
withExperimentalPersistentListViewFeatures &&
isLastOfSelectedBranch,
'is-dragging': isDragging,
} );

return (
<BlockNavigationLeaf
className={ classnames( {
'is-selected': isSelected,
'is-branch-selected':
withExperimentalPersistentListViewFeatures &&
isBranchSelected,
'is-last-of-selected-branch':
withExperimentalPersistentListViewFeatures &&
isLastOfSelectedBranch,
'is-dragging': isDragging,
} ) }
className={ classes }
onMouseEnter={ onMouseEnter }
onMouseLeave={ onMouseLeave }
onFocus={ onMouseEnter }
Expand Down
15 changes: 10 additions & 5 deletions packages/block-editor/src/components/block-navigation/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import { Fragment } from '@wordpress/element';
*/
import BlockNavigationBlock from './block';
import BlockNavigationAppender from './appender';

import { isClientIdSelected } from './utils';
export default function BlockNavigationBranch( props ) {
const {
blocks,
selectBlock,
selectedBlockClientId,
selectedBlockClientIds,
showAppender,
showBlockMovers,
showNestedBlocks,
Expand All @@ -35,7 +35,7 @@ export default function BlockNavigationBranch( props ) {
const itemHasAppender = ( parentClientId ) =>
showAppender &&
! isTreeRoot &&
selectedBlockClientId === parentClientId;
isClientIdSelected( parentClientId, selectedBlockClientIds );
const hasAppender = itemHasAppender( parentBlockClientId );
// Add +1 to the rowCount to take the block appender into account.
const blockCount = filteredBlocks.length;
Expand All @@ -57,7 +57,10 @@ export default function BlockNavigationBranch( props ) {
const hasNestedAppender = itemHasAppender( clientId );
const hasNestedBranch = hasNestedBlocks || hasNestedAppender;

const isSelected = selectedBlockClientId === clientId;
const isSelected = isClientIdSelected(
clientId,
selectedBlockClientIds
);
const isSelectedBranch =
isBranchSelected || ( isSelected && hasNestedBranch );

Expand Down Expand Up @@ -87,7 +90,9 @@ export default function BlockNavigationBranch( props ) {
{ hasNestedBranch && (
<BlockNavigationBranch
blocks={ innerBlocks }
selectedBlockClientId={ selectedBlockClientId }
selectedBlockClientIds={
selectedBlockClientIds
}
selectBlock={ selectBlock }
isBranchSelected={ isSelectedBranch }
isLastOfBranch={ isLast }
Expand Down
20 changes: 12 additions & 8 deletions packages/block-editor/src/components/block-navigation/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { noop } from 'lodash';
import { isArray, noop } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -13,6 +13,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import BlockNavigationTree from './tree';
import { isClientIdSelected } from './utils';
import { store as blockEditorStore } from '../../store';

export default function BlockNavigation( {
Expand All @@ -30,11 +31,14 @@ export default function BlockNavigation( {

const _selectedBlockClientId = getSelectedBlockClientId();
const _rootBlocks = __unstableGetClientIdsTree();
const _rootBlock = _selectedBlockClientId
? __unstableGetClientIdWithClientIdsTree(
getBlockHierarchyRootClientId( _selectedBlockClientId )
)
: null;
const _rootBlock =
selectedBlockClientId && ! isArray( selectedBlockClientId )
? __unstableGetClientIdWithClientIdsTree(
getBlockHierarchyRootClientId(
_selectedBlockClientId
)
)
: null;

return {
rootBlock: _rootBlock,
Expand All @@ -56,7 +60,7 @@ export default function BlockNavigation( {

const hasHierarchy =
rootBlock &&
( rootBlock.clientId !== selectedBlockClientId ||
( ! isClientIdSelected( rootBlock.clientId, selectedBlockClientId ) ||
( rootBlock.innerBlocks && rootBlock.innerBlocks.length !== 0 ) );

return (
Expand All @@ -67,7 +71,7 @@ export default function BlockNavigation( {

<BlockNavigationTree
blocks={ hasHierarchy ? [ rootBlock ] : rootBlocks }
selectedBlockClientId={ selectedBlockClientId }
selectedBlockClientIds={ [ selectedBlockClientId ] }
selectBlock={ selectEditorBlock }
__experimentalFeatures={ __experimentalFeatures }
showNestedBlocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import { __experimentalTreeGrid as TreeGrid } from '@wordpress/components';
import { useMemo, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
Expand Down
19 changes: 19 additions & 0 deletions packages/block-editor/src/components/block-navigation/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { isArray } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -11,3 +16,17 @@ export const getBlockPositionDescription = ( position, siblingCount, level ) =>
siblingCount,
level
);

/**
* Returns true if the client ID occurs within the block selection or multi-selection,
* or false otherwise.
*
* @param {string} clientId Block client ID.
* @param {string|string[]} selectedBlockClientIds Selected block client ID, or an array of multi-selected blocks client IDs.
*
* @return {boolean} Whether the block is in multi-selection set.
*/
export const isClientIdSelected = ( clientId, selectedBlockClientIds ) =>
isArray( selectedBlockClientIds ) && selectedBlockClientIds.length
? selectedBlockClientIds.indexOf( clientId ) !== -1
: selectedBlockClientIds === clientId;
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function BlockNavigationList( {
return (
<__experimentalBlockNavigationTree
blocks={ blocks }
selectedBlockClientId={ selectedBlockClientId }
selectedBlockClientIds={ [ selectedBlockClientId ] }
selectBlock={ selectBlock }
__experimentalFeatures={ __experimentalFeatures }
showNestedBlocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import { ESCAPE } from '@wordpress/keycodes';
import { store as editSiteStore } from '../../store';

export default function ListViewSidebar() {
const { clientIdsTree, selectedBlockClientId } = useSelect( ( select ) => {
const { __unstableGetClientIdsTree, getSelectedBlockClientId } = select(
blockEditorStore
);
const { clientIdsTree, selectedBlockClientIds } = useSelect( ( select ) => {
const {
__unstableGetClientIdsTree,
getSelectedBlockClientIds,
} = select( blockEditorStore );
return {
clientIdsTree: __unstableGetClientIdsTree(),
selectedBlockClientId: getSelectedBlockClientId(),
selectedBlockClientIds: getSelectedBlockClientIds(),
};
} );
const { setIsListViewOpened } = useDispatch( editSiteStore );
Expand Down Expand Up @@ -74,7 +75,7 @@ export default function ListViewSidebar() {
<BlockNavigationTree
blocks={ clientIdsTree }
selectBlock={ selectEditorBlock }
selectedBlockClientId={ selectedBlockClientId }
selectedBlockClientIds={ selectedBlockClientIds }
showNestedBlocks
__experimentalPersistentListViewFeatures
/>
Expand Down