@wordpress/block-editor
Version:
8 lines (7 loc) • 6.14 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/hooks/allowed-blocks.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addFilter } from '@wordpress/hooks';\nimport { hasBlockSupport, getBlockType } from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport { PrivateInspectorControlsAllowedBlocks } from '../components/inspector-controls/groups';\nimport BlockAllowedBlocksControl from '../components/block-allowed-blocks/allowed-blocks-control';\nimport { useBlockEditingMode } from '../components/block-editing-mode';\n\nfunction BlockEditAllowedBlocksControlPure( { clientId } ) {\n\tconst blockEditingMode = useBlockEditingMode();\n\tconst isContentOnly = blockEditingMode === 'contentOnly';\n\n\tif ( isContentOnly ) {\n\t\treturn null;\n\t}\n\n\treturn (\n\t\t<PrivateInspectorControlsAllowedBlocks.Fill>\n\t\t\t<BlockAllowedBlocksControl clientId={ clientId } />\n\t\t</PrivateInspectorControlsAllowedBlocks.Fill>\n\t);\n}\n\nexport default {\n\tedit: BlockEditAllowedBlocksControlPure,\n\tattributeKeys: [ 'allowedBlocks' ],\n\thasSupport( name ) {\n\t\treturn hasBlockSupport( name, 'allowedBlocks' );\n\t},\n};\n\n/**\n * Filters registered block settings, extending attributes with allowedBlocks.\n *\n * @param {Object} settings Original block settings.\n *\n * @return {Object} Filtered block settings.\n */\nexport function addAttribute( settings ) {\n\t// Allow blocks to specify their own attribute definition with default values if needed.\n\tif ( settings?.attributes?.allowedBlocks?.type ) {\n\t\treturn settings;\n\t}\n\tif ( hasBlockSupport( settings, 'allowedBlocks' ) ) {\n\t\t// Gracefully handle if settings.attributes is undefined.\n\t\tsettings.attributes = {\n\t\t\t...settings.attributes,\n\t\t\tallowedBlocks: {\n\t\t\t\ttype: 'array',\n\t\t\t},\n\t\t};\n\t}\n\n\treturn settings;\n}\n\naddFilter(\n\t'blocks.registerBlockType',\n\t'core/allowedBlocks/attribute',\n\taddAttribute\n);\n\n/**\n * Add transforms to preserve allowedBlocks on block transformations.\n *\n * @param {Object} result The transformed block.\n * @param {Array} source Original blocks transformed.\n * @param {number} index Index of the transformed block.\n * @param {Array} results All blocks that resulted from the transformation.\n * @return {Object} Modified transformed block.\n */\nexport function addTransforms( result, source, index, results ) {\n\tif ( ! hasBlockSupport( result.name, 'allowedBlocks' ) ) {\n\t\treturn result;\n\t}\n\n\t// If the condition verifies we are probably in the presence of a wrapping transform\n\t// e.g: nesting paragraphs in a group or columns and in that case the attribute should not be kept.\n\tif (\n\t\tsource.length !== 1 &&\n\t\tresults.length === 1 &&\n\t\tresult.innerBlocks.length === source.length\n\t) {\n\t\treturn result;\n\t}\n\n\t// If we are transforming one block to multiple blocks or multiple blocks to one block,\n\t// we ignore the attribute during the transform.\n\tif (\n\t\t( results.length === 1 && source.length > 1 ) ||\n\t\t( results.length > 1 && source.length === 1 )\n\t) {\n\t\treturn result;\n\t}\n\n\t// If we are transforming multiple blocks to multiple blocks with different counts,\n\t// we ignore the attribute during the transform.\n\tif (\n\t\tresults.length > 1 &&\n\t\tsource.length > 1 &&\n\t\tresults.length !== source.length\n\t) {\n\t\treturn result;\n\t}\n\n\t// If the target block already has allowedBlocks, we don't need to preserve\n\t// the source allowedBlocks.\n\tif ( result.attributes.allowedBlocks ) {\n\t\treturn result;\n\t}\n\n\tconst sourceAllowedBlocks = source[ index ]?.attributes?.allowedBlocks;\n\n\tif ( ! sourceAllowedBlocks ) {\n\t\treturn result;\n\t}\n\n\tconst blockType = getBlockType( result.name );\n\tconst destinationAllowedBlocks = blockType?.allowedBlocks || [];\n\n\tif ( ! destinationAllowedBlocks.length ) {\n\t\treturn {\n\t\t\t...result,\n\t\t\tattributes: {\n\t\t\t\t...result.attributes,\n\t\t\t\tallowedBlocks: sourceAllowedBlocks,\n\t\t\t},\n\t\t};\n\t}\n\n\t// Filter out any source allowed blocks that are not defined in the destination allowed blocks.\n\tconst filteredSourceAllowedBlocks = sourceAllowedBlocks.filter( ( block ) =>\n\t\tdestinationAllowedBlocks.includes( block )\n\t);\n\n\treturn {\n\t\t...result,\n\t\tattributes: {\n\t\t\t...result.attributes,\n\t\t\tallowedBlocks: filteredSourceAllowedBlocks,\n\t\t},\n\t};\n}\n\naddFilter(\n\t'blocks.switchToBlockType.transformedBlock',\n\t'core/allowedBlocks/addTransforms',\n\taddTransforms\n);\n"],
"mappings": ";AAGA,SAAS,iBAAiB;AAC1B,SAAS,iBAAiB,oBAAoB;AAK9C,SAAS,6CAA6C;AACtD,OAAO,+BAA+B;AACtC,SAAS,2BAA2B;AAYjC;AAVH,SAAS,kCAAmC,EAAE,SAAS,GAAI;AAC1D,QAAM,mBAAmB,oBAAoB;AAC7C,QAAM,gBAAgB,qBAAqB;AAE3C,MAAK,eAAgB;AACpB,WAAO;AAAA,EACR;AAEA,SACC,oBAAC,sCAAsC,MAAtC,EACA,8BAAC,6BAA0B,UAAsB,GAClD;AAEF;AAEA,IAAO,yBAAQ;AAAA,EACd,MAAM;AAAA,EACN,eAAe,CAAE,eAAgB;AAAA,EACjC,WAAY,MAAO;AAClB,WAAO,gBAAiB,MAAM,eAAgB;AAAA,EAC/C;AACD;AASO,SAAS,aAAc,UAAW;AAExC,MAAK,UAAU,YAAY,eAAe,MAAO;AAChD,WAAO;AAAA,EACR;AACA,MAAK,gBAAiB,UAAU,eAAgB,GAAI;AAEnD,aAAS,aAAa;AAAA,MACrB,GAAG,SAAS;AAAA,MACZ,eAAe;AAAA,QACd,MAAM;AAAA,MACP;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AACR;AAEA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AACD;AAWO,SAAS,cAAe,QAAQ,QAAQ,OAAO,SAAU;AAC/D,MAAK,CAAE,gBAAiB,OAAO,MAAM,eAAgB,GAAI;AACxD,WAAO;AAAA,EACR;AAIA,MACC,OAAO,WAAW,KAClB,QAAQ,WAAW,KACnB,OAAO,YAAY,WAAW,OAAO,QACpC;AACD,WAAO;AAAA,EACR;AAIA,MACG,QAAQ,WAAW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