UNPKG

@wordpress/block-library

Version:
1 lines • 3.33 kB
{"version":3,"file":"use-template-part-area-label.mjs","names":["blockEditorStore","coreStore"],"sources":["../../src/navigation/use-template-part-area-label.js"],"sourcesContent":["import { store as blockEditorStore } from '@wordpress/block-editor';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useSelect } from '@wordpress/data';\n// TODO: this util should perhaps be refactored somewhere like core-data.\nimport { createTemplatePartId } from '../template-part/edit/utils/create-template-part-id';\nimport { getTemplatePartIcon } from '../template-part/edit/utils/get-template-part-icon';\n\nexport default function useTemplatePartAreaLabel( clientId ) {\n\treturn useSelect(\n\t\t( select ) => {\n\t\t\t// Use the lack of a clientId as an opportunity to bypass the rest\n\t\t\t// of this hook.\n\t\t\tif ( ! clientId ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst { getBlock, getBlockParentsByBlockName } =\n\t\t\t\tselect( blockEditorStore );\n\n\t\t\tconst withAscendingResults = true;\n\t\t\tconst parentTemplatePartClientIds = getBlockParentsByBlockName(\n\t\t\t\tclientId,\n\t\t\t\t'core/template-part',\n\t\t\t\twithAscendingResults\n\t\t\t);\n\n\t\t\tif ( ! parentTemplatePartClientIds?.length ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst { getCurrentTheme, getEditedEntityRecord } =\n\t\t\t\tselect( coreStore );\n\n\t\t\tconst currentTheme = getCurrentTheme();\n\t\t\tconst defaultTemplatePartAreas =\n\t\t\t\tcurrentTheme?.default_template_part_areas || [];\n\n\t\t\tconst definedAreas = defaultTemplatePartAreas.map( ( item ) => ( {\n\t\t\t\t...item,\n\t\t\t\ticon: getTemplatePartIcon( item.icon ),\n\t\t\t} ) );\n\n\t\t\tfor ( const templatePartClientId of parentTemplatePartClientIds ) {\n\t\t\t\tconst templatePartBlock = getBlock( templatePartClientId );\n\n\t\t\t\t// The 'area' usually isn't stored on the block, but instead\n\t\t\t\t// on the entity.\n\t\t\t\tconst { theme = currentTheme?.stylesheet, slug } =\n\t\t\t\t\ttemplatePartBlock.attributes;\n\t\t\t\tconst templatePartEntityId = createTemplatePartId(\n\t\t\t\t\ttheme,\n\t\t\t\t\tslug\n\t\t\t\t);\n\t\t\t\tconst templatePartEntity = getEditedEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template_part',\n\t\t\t\t\ttemplatePartEntityId\n\t\t\t\t);\n\n\t\t\t\t// Look up the `label` for the area in the defined areas so\n\t\t\t\t// that an internationalized label can be used.\n\t\t\t\tif ( templatePartEntity?.area ) {\n\t\t\t\t\treturn definedAreas.find(\n\t\t\t\t\t\t( definedArea ) =>\n\t\t\t\t\t\t\tdefinedArea.area !== 'uncategorized' &&\n\t\t\t\t\t\t\tdefinedArea.area === templatePartEntity.area\n\t\t\t\t\t)?.label;\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t[ clientId ]\n\t);\n}\n"],"mappings":";;;;;;AAOA,SAAwB,yBAA0B,UAAW;CAC5D,OAAO,WACJ,WAAY;EAGb,IAAK,CAAE,UACN;EAGD,MAAM,EAAE,UAAU,+BACjB,OAAQA,KAAiB;EAG1B,MAAM,8BAA8B,2BACnC,UACA,sBACA,IACD;EAEA,IAAK,CAAE,6BAA6B,QACnC;EAGD,MAAM,EAAE,iBAAiB,0BACxB,OAAQC,OAAU;EAEnB,MAAM,eAAe,gBAAgB;EAIrC,MAAM,gBAFL,cAAc,+BAA+B,CAAC,EAAA,CAED,KAAO,UAAY;GAChE,GAAG;GACH,MAAM,oBAAqB,KAAK,IAAK;EACtC,EAAI;EAEJ,KAAM,MAAM,wBAAwB,6BAA8B;GAKjE,MAAM,EAAE,QAAQ,cAAc,YAAY,SAJhB,SAAU,oBAKnB,CAAC,CAAC;GAKnB,MAAM,qBAAqB,sBAC1B,YACA,oBAN4B,qBAC5B,OACA,IAKmB,CACpB;GAIA,IAAK,oBAAoB,MACxB,OAAO,aAAa,MACjB,gBACD,YAAY,SAAS,mBACrB,YAAY,SAAS,mBAAmB,IAC1C,CAAC,EAAE;EAEL;CACD,GACA,CAAE,QAAS,CACZ;AACD"}