-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted scroll and expand logic to hook
- Loading branch information
Showing
3 changed files
with
130 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/block-editor/src/components/list-view/use-list-view-open-selected-item.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useLayoutEffect, useEffect, useState } from '@wordpress/element'; | ||
import { getScrollContainer } from '@wordpress/dom'; | ||
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 ); | ||
return { | ||
selectedBlockParentClientIds: getBlockParents( | ||
firstSelectedBlockClientId, | ||
false | ||
), | ||
}; | ||
}, | ||
[ firstSelectedBlockClientId ] | ||
); | ||
const parentClientIds = | ||
Array.isArray( selectedBlockParentClientIds ) && | ||
selectedBlockParentClientIds.length | ||
? selectedBlockParentClientIds | ||
: null; | ||
|
||
// 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 ]; | ||
} | ||
|
||
useEffect( () => { | ||
// If the selectedTreeId is the same as the selected block, | ||
// it means that the block was selected using the block list tree. | ||
if ( selectedTreeId === selectedId ) { | ||
return; | ||
} | ||
|
||
// If the selected block has parents, get the top-level parent. | ||
if ( parentClientIds ) { | ||
// If the selected block has parents, | ||
// expand the tree branch. | ||
setExpandedState( { | ||
type: 'expand', | ||
clientIds: selectedBlockParentClientIds, | ||
} ); | ||
} | ||
}, [ selectedId ] ); | ||
|
||
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 === selectedId ) { | ||
return; | ||
} | ||
|
||
if ( | ||
scrollContainer && | ||
!! selectedId && | ||
Array.isArray( clientIdsTree ) && | ||
clientIdsTree.length | ||
) { | ||
// 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 ( parentClientIds && ! expandedState[ selectedId ] ) { | ||
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; | ||
} ); | ||
|
||
const newScrollTopValue = | ||
listItemHeightFactor * blockListItemHeight; | ||
|
||
// @TODO if selected block is already visible in the list prevent scroll. | ||
scrollContainer?.scrollTo( { | ||
top: newScrollTopValue, | ||
} ); | ||
} | ||
}, [ selectedId, expandedState[ selectedId ] ] ); | ||
|
||
return { | ||
setSelectedTreeId, | ||
}; | ||
} |