UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 2.38 kB
{ "version": 3, "sources": ["../../src/utils/is-within-overlay.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { select } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\n\n/**\n * Internal dependencies\n */\nimport { NAVIGATION_OVERLAY_TEMPLATE_PART_AREA } from '../navigation/constants';\n\n/**\n * Checks if the current editing context is within a navigation overlay template part.\n *\n * This utility exists because @wordpress/block-library cannot depend on @wordpress/editor\n * as a package dependency. Blocks can be loaded into non-post block editors, so we must\n * access the 'core/editor' store by string literal rather than importing it.\n *\n * React components should wrap this in useSelect for reactivity. Non-React contexts\n * (like filter callbacks) can call this directly.\n *\n * @return {boolean} True if editing a navigation overlay template part, false otherwise.\n */\nexport function isWithinNavigationOverlay() {\n\t// @wordpress/block-library should not depend on @wordpress/editor.\n\t// Blocks can be loaded into a *non-post* block editor, so to avoid\n\t// declaring @wordpress/editor as a dependency, we must access its\n\t// store by string.\n\t// eslint-disable-next-line @wordpress/data-no-store-string-literals\n\tconst editorStore = select( 'core/editor' );\n\n\t// Return false if the editor store is not available.\n\tif ( ! editorStore ) {\n\t\treturn false;\n\t}\n\n\tconst { getCurrentPostType, getCurrentPostId } = editorStore;\n\tconst { getEditedEntityRecord } = select( coreStore );\n\n\tconst postType = getCurrentPostType?.();\n\tconst postId = getCurrentPostId?.();\n\n\tif ( postType === 'wp_template_part' && postId ) {\n\t\tconst templatePart = getEditedEntityRecord(\n\t\t\t'postType',\n\t\t\t'wp_template_part',\n\t\t\tpostId\n\t\t);\n\n\t\treturn templatePart?.area === NAVIGATION_OVERLAY_TEMPLATE_PART_AREA;\n\t}\n\n\treturn false;\n}\n"], "mappings": ";AAGA,SAAS,cAAc;AACvB,SAAS,SAAS,iBAAiB;AAKnC,SAAS,6CAA6C;AAc/C,SAAS,4BAA4B;AAM3C,QAAM,cAAc,OAAQ,aAAc;AAG1C,MAAK,CAAE,aAAc;AACpB,WAAO;AAAA,EACR;AAEA,QAAM,EAAE,oBAAoB,iBAAiB,IAAI;AACjD,QAAM,EAAE,sBAAsB,IAAI,OAAQ,SAAU;AAEpD,QAAM,WAAW,qBAAqB;AACtC,QAAM,SAAS,mBAAmB;AAElC,MAAK,aAAa,sBAAsB,QAAS;AAChD,UAAM,eAAe;AAAA,MACpB;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAEA,WAAO,cAAc,SAAS;AAAA,EAC/B;AAEA,SAAO;AACR;", "names": [] }