UNPKG

@wordpress/block-editor

Version:
49 lines (45 loc) 1.52 kB
/** * WordPress dependencies */ import { useEffect, useState } from '@wordpress/element'; import { useSelect } from '@wordpress/data'; /** * Internal dependencies */ import { store as blockEditorStore } from '../../store'; export default function useListViewExpandSelectedItem({ firstSelectedBlockClientId, setExpandedState }) { const [selectedTreeId, setSelectedTreeId] = useState(null); const { selectedBlockParentClientIds } = useSelect(select => { const { getBlockParents } = select(blockEditorStore); return { selectedBlockParentClientIds: getBlockParents(firstSelectedBlockClientId, false) }; }, [firstSelectedBlockClientId]); const parentClientIds = Array.isArray(selectedBlockParentClientIds) && selectedBlockParentClientIds.length ? selectedBlockParentClientIds : null; // 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. if (selectedTreeId === firstSelectedBlockClientId) { 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 }); } }, [firstSelectedBlockClientId]); return { setSelectedTreeId }; } //# sourceMappingURL=use-list-view-expand-selected-item.js.map