Skip to content

Commit

Permalink
Revert scroll to logic to be performed in another PR.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Feb 1, 2022
1 parent 23c762a commit 5869aef
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 86 deletions.
3 changes: 0 additions & 3 deletions packages/block-editor/src/components/list-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ function ListView(
const isMounted = useRef( false );
const { setSelectedTreeId } = useListViewOpenSelectedItem( {
firstSelectedBlockClientId: selectedClientIds[ 0 ],
clientIdsTree,
scrollContainerElement: elementRef?.current,
expandedState,
setExpandedState,
} );
const selectEditorBlock = useCallback(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
/**
* WordPress dependencies
*/
import { useLayoutEffect, useEffect, useState } from '@wordpress/element';
import { getScrollContainer } from '@wordpress/dom';
import { useEffect, useState } from '@wordpress/element';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { BLOCK_LIST_ITEM_HEIGHT } from './';
import { countBlocks } from './branch';
import { store as blockEditorStore } from '../../store';

export default function useListViewOpenSelectedItem( {
firstSelectedBlockClientId,
clientIdsTree,
blockListItemHeight = BLOCK_LIST_ITEM_HEIGHT,
scrollContainerElement,
expandedState,
setExpandedState,
} ) {
const [ selectedTreeId, setSelectedTreeId ] = useState( null );
const scrollContainer = getScrollContainer( scrollContainerElement );
const { selectedBlockParentClientIds } = useSelect(
( select ) => {
const { getBlockParents } = select( blockEditorStore );
Expand All @@ -41,13 +33,7 @@ export default function useListViewOpenSelectedItem( {
? selectedBlockParentClientIds
: null;

// Track the expanded state of any parents.
// To calculate the number of expanded items correctly.
let parentExpandedState = null;
if ( parentClientIds ) {
parentExpandedState = expandedState[ parentClientIds[ 0 ] ];
}

// Expand tree when a block is selected.
useEffect( () => {
// If the selectedTreeId is the same as the selected block,
// it means that the block was selected using the block list tree.
Expand All @@ -66,73 +52,6 @@ export default function useListViewOpenSelectedItem( {
}
}, [ firstSelectedBlockClientId ] );

useLayoutEffect( () => {
// If the selectedTreeId is the same as the selected block,
// it means that the block was selected using the block list tree.
if ( selectedTreeId === firstSelectedBlockClientId ) {
return;
}
if (
scrollContainer &&
!! firstSelectedBlockClientId &&
Array.isArray( clientIdsTree ) &&
clientIdsTree.length
) {
// Grab the selected id. This is the point at which we can
// stop counting blocks in the tree.
let selectedId = firstSelectedBlockClientId;

// If the selected block has parents, get the top-level parent.
if ( parentClientIds ) {
selectedId = parentClientIds[ 0 ];
// If the selected block has parents,
// check to see if the selected tree is expanded
// so we can accurately calculate the scroll container top value.
if ( ! parentExpandedState ) {
return;
}
}

// Count expanded blocks in the tree up until the selected block,
// so we can calculate the scroll container top value.
let listItemHeightFactor = 0;
clientIdsTree.every( ( item ) => {
if ( item?.clientId === selectedId ) {
return false;
}
listItemHeightFactor += countBlocks( item, expandedState, [] );
return true;
} );

// New scroll value is the number of expanded items
// multiplied by the item height
// plus the number of expanded children in the selected block
// multiplied by the item height.
const newScrollTopValue =
listItemHeightFactor * blockListItemHeight +
( parentClientIds ? parentClientIds.length : 1 ) *
blockListItemHeight;

const shouldScrollDown =
newScrollTopValue >
scrollContainer.scrollTop + scrollContainer.clientHeight;

const shouldScrollUp =
newScrollTopValue < scrollContainer.scrollTop;

if ( ! shouldScrollUp && ! shouldScrollDown ) {
return;
}

// @TODO This doesn't yet work when nested blocks are selected.
// We're still using the top parent block to calculate/trigger redraw.
// If selected block is already visible in the list prevent scroll.
scrollContainer?.scrollTo( {
top: newScrollTopValue,
} );
}
}, [ firstSelectedBlockClientId, parentExpandedState ] );

return {
setSelectedTreeId,
};
Expand Down

0 comments on commit 5869aef

Please sign in to comment.