@wordpress/block-editor
Version:
8 lines (7 loc) • 2.94 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/block-switcher/utils.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { getBlockAttributesNamesByRole } from '@wordpress/blocks';\n\n/**\n * Try to find a matching block by a block's name in a provided\n * block. We recurse through InnerBlocks and return the reference\n * of the matched block (it could be an InnerBlock).\n * If no match is found return nothing.\n *\n * @param {WPBlock} block The block to try to find a match.\n * @param {string} selectedBlockName The block's name to use for matching condition.\n * @param {Set} consumedBlocks A set holding the previously matched/consumed blocks.\n *\n * @return {WPBlock | undefined} The matched block if found or nothing(`undefined`).\n */\nexport const getMatchingBlockByName = (\n\tblock,\n\tselectedBlockName,\n\tconsumedBlocks = new Set()\n) => {\n\tconst { clientId, name, innerBlocks = [] } = block;\n\t// Check if block has been consumed already.\n\tif ( consumedBlocks.has( clientId ) ) {\n\t\treturn;\n\t}\n\tif ( name === selectedBlockName ) {\n\t\treturn block;\n\t}\n\t// Try to find a matching block from InnerBlocks recursively.\n\tfor ( const innerBlock of innerBlocks ) {\n\t\tconst match = getMatchingBlockByName(\n\t\t\tinnerBlock,\n\t\t\tselectedBlockName,\n\t\t\tconsumedBlocks\n\t\t);\n\t\tif ( match ) {\n\t\t\treturn match;\n\t\t}\n\t}\n};\n\n/**\n * Find and return the block attributes to retain through\n * the transformation, based on Block Type's `role:content`\n * attributes. If no `role:content` attributes exist,\n * return selected block's attributes.\n *\n * @param {string} name Block type's namespaced name.\n * @param {Object} attributes Selected block's attributes.\n * @return {Object} The block's attributes to retain.\n */\nexport const getRetainedBlockAttributes = ( name, attributes ) => {\n\tconst contentAttributes = getBlockAttributesNamesByRole( name, 'content' );\n\tif ( ! contentAttributes?.length ) {\n\t\treturn attributes;\n\t}\n\n\treturn contentAttributes.reduce( ( _accumulator, attribute ) => {\n\t\tif ( attributes[ attribute ] ) {\n\t\t\t_accumulator[ attribute ] = attributes[ attribute ];\n\t\t}\n\t\treturn _accumulator;\n\t}, {} );\n};\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,oBAA8C;AAcvC,IAAM,yBAAyB,CACrC,OACA,mBACA,iBAAiB,oBAAI,IAAI,MACrB;AACJ,QAAM,EAAE,UAAU,MAAM,cAAc,CAAC,EAAE,IAAI;AAE7C,MAAK,eAAe,IAAK,QAAS,GAAI;AACrC;AAAA,EACD;AACA,MAAK,SAAS,mBAAoB;AACjC,WAAO;AAAA,EACR;AAEA,aAAY,cAAc,aAAc;AACvC,UAAM,QAAQ;AAAA,MACb;AAAA,MACA;AAAA,MACA;AAAA,IACD;AACA,QAAK,OAAQ;AACZ,aAAO;AAAA,IACR;AAAA,EACD;AACD;AAYO,IAAM,6BAA6B,CAAE,MAAM,eAAgB;AACjE,QAAM,wBAAoB,6CAA+B,MAAM,SAAU;AACzE,MAAK,CAAE,mBAAmB,QAAS;AAClC,WAAO;AAAA,EACR;AAEA,SAAO,kBAAkB,OAAQ,CAAE,cAAc,cAAe;AAC/D,QAAK,WAAY,SAAU,GAAI;AAC9B,mBAAc,SAAU,IAAI,WAAY,SAAU;AAAA,IACnD;AACA,WAAO;AAAA,EACR,GAAG,CAAC,CAAE;AACP;",
"names": []
}