UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 3.32 kB
{ "version": 3, "sources": ["../../../src/components/convert-to-group-buttons/use-convert-to-group-button-props.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { store as blocksStore } from '@wordpress/blocks';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../../store';\n\n/**\n * Contains the properties `ConvertToGroupButton` component needs.\n *\n * @typedef {Object} ConvertToGroupButtonProps\n * @property {string[]} clientIds An array of the selected client ids.\n * @property {boolean} isGroupable Indicates if the selected blocks can be grouped.\n * @property {boolean} isUngroupable Indicates if the selected blocks can be ungrouped.\n * @property {WPBlock[]} blocksSelection An array of the selected blocks.\n * @property {string} groupingBlockName The name of block used for handling grouping interactions.\n */\n\n/**\n * Returns the properties `ConvertToGroupButton` component needs to work properly.\n * It is used in `BlockSettingsMenuControls` to know if `ConvertToGroupButton`\n * should be rendered, to avoid ending up with an empty MenuGroup.\n *\n * @param {?string[]} selectedClientIds An optional array of clientIds to group. The selected blocks\n * from the block editor store are used if this is not provided.\n *\n * @return {ConvertToGroupButtonProps} Returns the properties needed by `ConvertToGroupButton`.\n */\nexport default function useConvertToGroupButtonProps( selectedClientIds ) {\n\treturn useSelect(\n\t\t( select ) => {\n\t\t\tconst {\n\t\t\t\tgetBlocksByClientId,\n\t\t\t\tgetSelectedBlockClientIds,\n\t\t\t\tisUngroupable,\n\t\t\t\tisGroupable,\n\t\t\t} = select( blockEditorStore );\n\t\t\tconst { getGroupingBlockName, getBlockType } =\n\t\t\t\tselect( blocksStore );\n\t\t\tconst clientIds = selectedClientIds?.length\n\t\t\t\t? selectedClientIds\n\t\t\t\t: getSelectedBlockClientIds();\n\t\t\tconst blocksSelection = getBlocksByClientId( clientIds );\n\t\t\tconst [ firstSelectedBlock ] = blocksSelection;\n\t\t\tconst _isUngroupable =\n\t\t\t\tclientIds.length === 1 && isUngroupable( clientIds[ 0 ] );\n\t\t\treturn {\n\t\t\t\tclientIds,\n\t\t\t\tisGroupable: isGroupable( clientIds ),\n\t\t\t\tisUngroupable: _isUngroupable,\n\t\t\t\tblocksSelection,\n\t\t\t\tgroupingBlockName: getGroupingBlockName(),\n\t\t\t\tonUngroup:\n\t\t\t\t\t_isUngroupable &&\n\t\t\t\t\tgetBlockType( firstSelectedBlock.name )?.transforms\n\t\t\t\t\t\t?.ungroup,\n\t\t\t};\n\t\t},\n\t\t[ selectedClientIds ]\n\t);\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,oBAAqC;AACrC,kBAA0B;AAK1B,mBAA0C;AAuB3B,SAAR,6BAA+C,mBAAoB;AACzE,aAAO;AAAA,IACN,CAAE,WAAY;AACb,YAAM;AAAA,QACL;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACD,IAAI,OAAQ,aAAAA,KAAiB;AAC7B,YAAM,EAAE,sBAAsB,aAAa,IAC1C,OAAQ,cAAAC,KAAY;AACrB,YAAM,YAAY,mBAAmB,SAClC,oBACA,0BAA0B;AAC7B,YAAM,kBAAkB,oBAAqB,SAAU;AACvD,YAAM,CAAE,kBAAmB,IAAI;AAC/B,YAAM,iBACL,UAAU,WAAW,KAAK,cAAe,UAAW,CAAE,CAAE;AACzD,aAAO;AAAA,QACN;AAAA,QACA,aAAa,YAAa,SAAU;AAAA,QACpC,eAAe;AAAA,QACf;AAAA,QACA,mBAAmB,qBAAqB;AAAA,QACxC,WACC,kBACA,aAAc,mBAAmB,IAAK,GAAG,YACtC;AAAA,MACL;AAAA,IACD;AAAA,IACA,CAAE,iBAAkB;AAAA,EACrB;AACD;", "names": ["blockEditorStore", "blocksStore"] }