UNPKG

@wordpress/block-library

Version:
55 lines (54 loc) 2.04 kB
import { store as blockEditorStore } from "@wordpress/block-editor"; import { store as coreStore } from "@wordpress/core-data"; import { useSelect } from "@wordpress/data"; import { createTemplatePartId } from "../template-part/edit/utils/create-template-part-id"; import { getTemplatePartIcon } from "../template-part/edit/utils/get-template-part-icon"; function useTemplatePartAreaLabel(clientId) { return useSelect( (select) => { if (!clientId) { return; } const { getBlock, getBlockParentsByBlockName } = select(blockEditorStore); const withAscendingResults = true; const parentTemplatePartClientIds = getBlockParentsByBlockName( clientId, "core/template-part", withAscendingResults ); if (!parentTemplatePartClientIds?.length) { return; } const { getCurrentTheme, getEditedEntityRecord } = select(coreStore); const currentTheme = getCurrentTheme(); const defaultTemplatePartAreas = currentTheme?.default_template_part_areas || []; const definedAreas = defaultTemplatePartAreas.map((item) => ({ ...item, icon: getTemplatePartIcon(item.icon) })); for (const templatePartClientId of parentTemplatePartClientIds) { const templatePartBlock = getBlock(templatePartClientId); const { theme = currentTheme?.stylesheet, slug } = templatePartBlock.attributes; const templatePartEntityId = createTemplatePartId( theme, slug ); const templatePartEntity = getEditedEntityRecord( "postType", "wp_template_part", templatePartEntityId ); if (templatePartEntity?.area) { return definedAreas.find( (definedArea) => definedArea.area !== "uncategorized" && definedArea.area === templatePartEntity.area )?.label; } } }, [clientId] ); } export { useTemplatePartAreaLabel as default }; //# sourceMappingURL=use-template-part-area-label.js.map