@wordpress/block-editor
Version:
59 lines (50 loc) • 1.64 kB
JavaScript
;
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]);
const parentClientIds = Array.isArray(selectedBlockParentClientIds) && selectedBlockParentClientIds.length ? selectedBlockParentClientIds : null; // 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 (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