UNPKG

@wordpress/blocks

Version:
8 lines (7 loc) 10.7 kB
{ "version": 3, "sources": ["../../src/store/private-selectors.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { createSelector, createRegistrySelector } from '@wordpress/data';\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport { getBlockType } from './selectors';\nimport { getValueFromObjectPath } from './utils';\nimport { __EXPERIMENTAL_STYLE_PROPERTY as STYLE_PROPERTY } from '../api/constants';\n\nconst ROOT_BLOCK_SUPPORTS = [\n\t'background',\n\t'backgroundColor',\n\t'color',\n\t'linkColor',\n\t'captionColor',\n\t'buttonColor',\n\t'headingColor',\n\t'fontFamily',\n\t'fontSize',\n\t'fontStyle',\n\t'fontWeight',\n\t'lineHeight',\n\t'padding',\n\t'contentSize',\n\t'wideSize',\n\t'blockGap',\n\t'textAlign',\n\t'textDecoration',\n\t'textIndent',\n\t'textTransform',\n\t'letterSpacing',\n];\n\n/**\n * Filters the list of supported styles for a given element.\n *\n * @param {string[]} blockSupports list of supported styles.\n * @param {string|undefined} name block name.\n * @param {string|undefined} element element name.\n *\n * @return {string[]} filtered list of supported styles.\n */\nfunction filterElementBlockSupports( blockSupports, name, element ) {\n\treturn blockSupports.filter( ( support ) => {\n\t\tif ( support === 'fontSize' && element === 'heading' ) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// This is only available for links\n\t\tif ( support === 'textDecoration' && ! name && element !== 'link' ) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// This is only available for heading, button, caption and text\n\t\tif (\n\t\t\tsupport === 'textTransform' &&\n\t\t\t! name &&\n\t\t\t! (\n\t\t\t\t[ 'heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ].includes(\n\t\t\t\t\telement\n\t\t\t\t) ||\n\t\t\t\telement === 'button' ||\n\t\t\t\telement === 'caption' ||\n\t\t\t\telement === 'text'\n\t\t\t)\n\t\t) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// This is only available for heading, button, caption and text\n\t\tif (\n\t\t\tsupport === 'letterSpacing' &&\n\t\t\t! name &&\n\t\t\t! (\n\t\t\t\t[ 'heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ].includes(\n\t\t\t\t\telement\n\t\t\t\t) ||\n\t\t\t\telement === 'button' ||\n\t\t\t\telement === 'caption' ||\n\t\t\t\telement === 'text'\n\t\t\t)\n\t\t) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Text indent is only available for blocks, not elements\n\t\tif ( support === 'textIndent' && ! name ) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Text columns is only available for blocks.\n\t\tif ( support === 'textColumns' && ! name ) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t} );\n}\n\n/**\n * Returns the list of supported styles for a given block name and element.\n */\nexport const getSupportedStyles = createSelector(\n\t( state, name, element ) => {\n\t\tif ( ! name ) {\n\t\t\treturn filterElementBlockSupports(\n\t\t\t\tROOT_BLOCK_SUPPORTS,\n\t\t\t\tname,\n\t\t\t\telement\n\t\t\t);\n\t\t}\n\n\t\tconst blockType = getBlockType( state, name );\n\n\t\tif ( ! blockType ) {\n\t\t\treturn [];\n\t\t}\n\n\t\tconst supportKeys = [];\n\n\t\t// Check for blockGap support.\n\t\t// Block spacing support doesn't map directly to a single style property, so needs to be handled separately.\n\t\tif ( blockType?.supports?.spacing?.blockGap ) {\n\t\t\tsupportKeys.push( 'blockGap' );\n\t\t}\n\n\t\t// check for shadow support\n\t\tif ( blockType?.supports?.shadow ) {\n\t\t\tsupportKeys.push( 'shadow' );\n\t\t}\n\n\t\tObject.keys( STYLE_PROPERTY ).forEach( ( styleName ) => {\n\t\t\tif ( ! STYLE_PROPERTY[ styleName ].support ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Opting out means that, for certain support keys like background color,\n\t\t\t// blocks have to explicitly set the support value false. If the key is\n\t\t\t// unset, we still enable it.\n\t\t\tif ( STYLE_PROPERTY[ styleName ].requiresOptOut ) {\n\t\t\t\tif (\n\t\t\t\t\tSTYLE_PROPERTY[ styleName ].support[ 0 ] in\n\t\t\t\t\t\tblockType.supports &&\n\t\t\t\t\tgetValueFromObjectPath(\n\t\t\t\t\t\tblockType.supports,\n\t\t\t\t\t\tSTYLE_PROPERTY[ styleName ].support\n\t\t\t\t\t) !== false\n\t\t\t\t) {\n\t\t\t\t\tsupportKeys.push( styleName );\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tgetValueFromObjectPath(\n\t\t\t\t\tblockType.supports,\n\t\t\t\t\tSTYLE_PROPERTY[ styleName ].support,\n\t\t\t\t\tfalse\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tsupportKeys.push( styleName );\n\t\t\t}\n\t\t} );\n\n\t\treturn filterElementBlockSupports( supportKeys, name, element );\n\t},\n\t( state, name ) => [ state.blockTypes[ name ] ]\n);\n\n/**\n * Returns the bootstrapped block type metadata for a give block name.\n *\n * @param {Object} state Data state.\n * @param {string} name Block name.\n *\n * @return {Object} Bootstrapped block type metadata for a block.\n */\nexport function getBootstrappedBlockType( state, name ) {\n\treturn state.bootstrappedBlockTypes[ name ];\n}\n\n/**\n * Returns all the unprocessed (before applying the `registerBlockType` filter)\n * block type settings as passed during block registration.\n *\n * @param {Object} state Data state.\n *\n * @return {Array} Unprocessed block type settings for all blocks.\n */\nexport function getUnprocessedBlockTypes( state ) {\n\treturn state.unprocessedBlockTypes;\n}\n\n/**\n * Returns all the block bindings sources registered.\n *\n * @param {Object} state Data state.\n *\n * @return {Object} All the registered sources and their properties.\n */\nexport function getAllBlockBindingsSources( state ) {\n\treturn state.blockBindingsSources;\n}\n\n/**\n * Returns a specific block bindings source.\n *\n * @param {Object} state Data state.\n * @param {string} sourceName Name of the source to get.\n *\n * @return {Object} The specific block binding source and its properties.\n */\nexport function getBlockBindingsSource( state, sourceName ) {\n\treturn state.blockBindingsSources[ sourceName ];\n}\n\n/**\n * Compute the fields list for a specific block bindings source.\n *\n * @param {Object} state Data state.\n * @param {Object} source Block bindings source.\n * @param {Object} blockContext Block context.\n *\n * @return {Array} List of fields for the specific source.\n */\nexport const getBlockBindingsSourceFieldsList = createRegistrySelector(\n\t( select ) =>\n\t\tcreateSelector(\n\t\t\t( state, source, blockContext ) => {\n\t\t\t\tif ( ! source.getFieldsList ) {\n\t\t\t\t\treturn [];\n\t\t\t\t}\n\n\t\t\t\tconst context = {};\n\t\t\t\tif ( source?.usesContext?.length ) {\n\t\t\t\t\tfor ( const key of source.usesContext ) {\n\t\t\t\t\t\tcontext[ key ] = blockContext[ key ];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn source.getFieldsList( { select, context } );\n\t\t\t},\n\t\t\t( state, source, blockContext ) => [\n\t\t\t\tsource.getFieldsList,\n\t\t\t\tsource.usesContext,\n\t\t\t\tblockContext,\n\t\t\t]\n\t\t)\n);\n\n/**\n * Determines if any of the block type's attributes have\n * the content role attribute.\n *\n * @param {Object} state Data state.\n * @param {string} blockTypeName Block type name.\n * @return {boolean} Whether block type has content role attribute.\n */\nexport const hasContentRoleAttribute = ( state, blockTypeName ) => {\n\tconst blockType = getBlockType( state, blockTypeName );\n\tif ( ! blockType ) {\n\t\treturn false;\n\t}\n\n\treturn Object.values( blockType.attributes ).some(\n\t\t( { role, __experimentalRole } ) => {\n\t\t\tif ( role === 'content' ) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif ( __experimentalRole === 'content' ) {\n\t\t\t\tdeprecated( '__experimentalRole attribute', {\n\t\t\t\t\tsince: '6.7',\n\t\t\t\t\tversion: '6.8',\n\t\t\t\t\talternative: 'role attribute',\n\t\t\t\t\thint: `Check the block.json of the ${ blockTypeName } block.`,\n\t\t\t\t} );\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\t);\n};\n"], "mappings": ";AAGA,SAAS,gBAAgB,8BAA8B;AACvD,OAAO,gBAAgB;AAKvB,SAAS,oBAAoB;AAC7B,SAAS,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