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: explore opening to focused block #35967

Closed
wants to merge 5 commits into from
Closed
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
46 changes: 34 additions & 12 deletions packages/block-editor/src/components/list-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ import ListViewDropIndicator from './drop-indicator';
import useListViewClientIds from './use-list-view-client-ids';
import useListViewDropZone from './use-list-view-drop-zone';
import { store as blockEditorStore } from '../../store';
import { hasFocusWithin } from './utils';

const noop = () => {};
const expanded = ( state, action ) => {
switch ( action.type ) {
case 'expand':
return { ...state, ...{ [ action.clientId ]: true } };
case 'collapse':
return { ...state, ...{ [ action.clientId ]: false } };
default:
return state;
if ( Array.isArray( action.clientIds ) ) {
return {
...state,
...action.clientIds.reduce(
( newState, id ) => ( {
...newState,
[ id ]: action.type === 'expand',
} ),
{}
),
};
}
return state;
};

/**
Expand Down Expand Up @@ -67,7 +73,11 @@ function ListView(
},
ref
) {
const { clientIdsTree, draggedClientIds } = useListViewClientIds( blocks );
const {
clientIdsTree,
draggedClientIds,
selectedBlockParentClientIds,
} = useListViewClientIds( blocks );
const { selectBlock } = useDispatch( blockEditorStore );
const { visibleBlockCount } = useSelect(
( select ) => {
Expand All @@ -92,12 +102,12 @@ function ListView(
[ selectBlock, onSelect ]
);
const [ expandedState, setExpandedState ] = useReducer( expanded, {} );

const { ref: dropZoneRef, target: blockDropTarget } = useListViewDropZone();
const elementRef = useRef();
const treeGridRef = useMergeRefs( [ elementRef, dropZoneRef, ref ] );

const isMounted = useRef( false );
const hasFocus = hasFocusWithin( elementRef?.current );

useEffect( () => {
isMounted.current = true;
}, [] );
Expand All @@ -119,7 +129,7 @@ function ListView(
if ( ! clientId ) {
return;
}
setExpandedState( { type: 'expand', clientId } );
setExpandedState( { type: 'expand', clientIds: [ clientId ] } );
},
[ setExpandedState ]
);
Expand All @@ -128,7 +138,7 @@ function ListView(
if ( ! clientId ) {
return;
}
setExpandedState( { type: 'collapse', clientId } );
setExpandedState( { type: 'collapse', clientIds: [ clientId ] } );
},
[ setExpandedState ]
);
Expand Down Expand Up @@ -168,6 +178,18 @@ function ListView(
]
);

// If a selection is made outside the block list,
// for example, in the Block Editor,
// try to expand the block list tree.
useEffect( () => {
if ( ! hasFocus ) {
setExpandedState( {
type: 'expand',
clientIds: selectedBlockParentClientIds,
} );
}
}, [ hasFocus, selectedBlockParentClientIds ] );

return (
<AsyncModeProvider value={ true }>
<ListViewDropIndicator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ export default function useListViewClientIds( blocks ) {
const {
getDraggedBlockClientIds,
__unstableGetClientIdsTree,
getSelectedBlockClientIds,
getBlockParents,
} = select( blockEditorStore );
const selectedBlockClientIds = getSelectedBlockClientIds();

return {
draggedClientIds: getDraggedBlockClientIds(),
clientIdsTree: blocks ? blocks : __unstableGetClientIdsTree(),
selectedBlockParentClientIds: getBlockParents(
selectedBlockClientIds[ 0 ],
false
),
};
},
[ blocks ]
Expand Down
11 changes: 11 additions & 0 deletions packages/block-editor/src/components/list-view/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ export const isClientIdSelected = ( clientId, selectedBlockClientIds ) =>
isArray( selectedBlockClientIds ) && selectedBlockClientIds.length
? selectedBlockClientIds.indexOf( clientId ) !== -1
: selectedBlockClientIds === clientId;

/**
* Returns true if the container contains the document active element.
*
* @param {HTMLElement} container An HTML element.
*
* @return {boolean} Whether the container contains the currently document active element.
*/
export const hasFocusWithin = ( container ) => {
return !! container?.contains( container?.ownerDocument?.activeElement );
};
6 changes: 4 additions & 2 deletions packages/e2e-tests/specs/performance/post-editor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { sum } from 'lodash';
import {
createNewPost,
saveDraft,
insertBlock,
openGlobalBlockInserter,
closeGlobalBlockInserter,
openListView,
Expand Down Expand Up @@ -121,7 +120,9 @@ describe( 'Post Editor Performance', () => {

it( 'Typing', async () => {
// Measuring typing performance
await insertBlock( 'Paragraph' );
await openListView();
await page.click( '.edit-post-visual-editor__post-title-wrapper' );
await page.keyboard.press( 'Enter' );
let i = 20;
await page.tracing.start( {
path: traceFile,
Expand Down Expand Up @@ -160,6 +161,7 @@ describe( 'Post Editor Performance', () => {
it( 'Selecting blocks', async () => {
// Measuring block selection performance
await createNewPost();
await openListView();
await page.evaluate( () => {
const { createBlock } = window.wp.blocks;
const { dispatch } = window.wp.data;
Expand Down
10 changes: 8 additions & 2 deletions packages/e2e-tests/specs/performance/site-editor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
canvas,
createNewPost,
saveDraft,
insertBlock,
openListView,
} from '@wordpress/e2e-test-utils';

/**
Expand Down Expand Up @@ -118,7 +118,13 @@ describe( 'Site Editor Performance', () => {
await canvas().click(
'[data-type="core/post-content"] [data-type="core/paragraph"]'
);
await insertBlock( 'Paragraph' );
await openListView();
await canvas().click(
'[data-type="core/post-content"] [data-type="core/paragraph"]'
);
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'ArrowUp' );
i = 200;
const traceFile = __dirname + '/trace.json';
await page.tracing.start( {
Expand Down