UNPKG

@wordpress/block-editor

Version:
56 lines (51 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useListViewExpandSelectedItem; var _element = require("@wordpress/element"); var _data = require("@wordpress/data"); var _store = require("../../store"); /** * WordPress dependencies */ /** * Internal dependencies */ function useListViewExpandSelectedItem({ firstSelectedBlockClientId, setExpandedState }) { const [selectedTreeId, setSelectedTreeId] = (0, _element.useState)(null); const { selectedBlockParentClientIds } = (0, _data.useSelect)(select => { const { getBlockParents } = select(_store.store); return { selectedBlockParentClientIds: getBlockParents(firstSelectedBlockClientId, false) }; }, [firstSelectedBlockClientId]); // Expand tree when a block is selected. (0, _element.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 (selectedBlockParentClientIds?.length) { // If the selected block has parents, // expand the tree branch. setExpandedState({ type: 'expand', clientIds: selectedBlockParentClientIds }); } }, [firstSelectedBlockClientId, selectedBlockParentClientIds, selectedTreeId, setExpandedState]); return { setSelectedTreeId }; } //# sourceMappingURL=use-list-view-expand-selected-item.js.map