UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 11.1 kB
{ "version": 3, "sources": ["../../../src/components/block-switcher/index.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __, _n, sprintf, _x } from '@wordpress/i18n';\nimport {\n\tDropdownMenu,\n\tToolbarGroup,\n\tToolbarItem,\n\t__experimentalText as Text,\n\tMenuGroup,\n} from '@wordpress/components';\nimport {\n\tswitchToBlockType,\n\tstore as blocksStore,\n\tisReusableBlock,\n\tisTemplatePart,\n} from '@wordpress/blocks';\nimport { useSelect, useDispatch } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../../store';\nimport BlockTransformationsMenu from './block-transformations-menu';\nimport { useBlockVariationTransforms } from './block-variation-transformations';\nimport BlockStylesMenu from './block-styles-menu';\nimport PatternTransformationsMenu from './pattern-transformations-menu';\n\nfunction BlockSwitcherDropdownMenuContents( { onClose, clientIds } ) {\n\tconst { replaceBlocks, multiSelect, updateBlockAttributes } =\n\t\tuseDispatch( blockEditorStore );\n\tconst {\n\t\tpossibleBlockTransformations,\n\t\tpatterns,\n\t\tblocks,\n\t\tisUsingBindings,\n\t\tcanRemove,\n\t\thasBlockStyles,\n\t} = useSelect(\n\t\t( select ) => {\n\t\t\tconst {\n\t\t\t\tgetBlockAttributes,\n\t\t\t\tgetBlocksByClientId,\n\t\t\t\tgetBlockRootClientId,\n\t\t\t\tgetBlockTransformItems,\n\t\t\t\t__experimentalGetPatternTransformItems,\n\t\t\t\tcanRemoveBlocks,\n\t\t\t\tgetBlockName,\n\t\t\t} = select( blockEditorStore );\n\t\t\tconst { getBlockStyles } = select( blocksStore );\n\t\t\tconst rootClientId = getBlockRootClientId( clientIds[ 0 ] );\n\t\t\tconst _blocks = getBlocksByClientId( clientIds );\n\t\t\tconst _isSingleBlock = clientIds.length === 1;\n\t\t\tconst _blockName = _isSingleBlock && getBlockName( clientIds[ 0 ] );\n\t\t\tconst _hasBlockStyles =\n\t\t\t\t_isSingleBlock && !! getBlockStyles( _blockName )?.length;\n\t\t\treturn {\n\t\t\t\tblocks: _blocks,\n\t\t\t\tpossibleBlockTransformations: getBlockTransformItems(\n\t\t\t\t\t_blocks,\n\t\t\t\t\trootClientId\n\t\t\t\t),\n\t\t\t\tpatterns: __experimentalGetPatternTransformItems(\n\t\t\t\t\t_blocks,\n\t\t\t\t\trootClientId\n\t\t\t\t),\n\t\t\t\tisUsingBindings: clientIds.every(\n\t\t\t\t\t( clientId ) =>\n\t\t\t\t\t\t!! getBlockAttributes( clientId )?.metadata?.bindings\n\t\t\t\t),\n\t\t\t\tcanRemove: canRemoveBlocks( clientIds ),\n\t\t\t\thasBlockStyles: _hasBlockStyles,\n\t\t\t};\n\t\t},\n\t\t[ clientIds ]\n\t);\n\tconst blockVariationTransformations = useBlockVariationTransforms( {\n\t\tclientIds,\n\t\tblocks,\n\t} );\n\tfunction selectForMultipleBlocks( insertedBlocks ) {\n\t\tif ( insertedBlocks.length > 1 ) {\n\t\t\tmultiSelect(\n\t\t\t\tinsertedBlocks[ 0 ].clientId,\n\t\t\t\tinsertedBlocks[ insertedBlocks.length - 1 ].clientId\n\t\t\t);\n\t\t}\n\t}\n\t// Simple block transformation based on the `Block Transforms` API.\n\tfunction onBlockTransform( name ) {\n\t\tconst newBlocks = switchToBlockType( blocks, name );\n\t\treplaceBlocks( clientIds, newBlocks );\n\t\tselectForMultipleBlocks( newBlocks );\n\t}\n\tfunction onBlockVariationTransform( name ) {\n\t\tupdateBlockAttributes( blocks[ 0 ].clientId, {\n\t\t\t...blockVariationTransformations.find(\n\t\t\t\t( { name: variationName } ) => variationName === name\n\t\t\t).attributes,\n\t\t} );\n\t}\n\t// Pattern transformation through the `Patterns` API.\n\tfunction onPatternTransform( transformedBlocks ) {\n\t\treplaceBlocks( clientIds, transformedBlocks );\n\t\tselectForMultipleBlocks( transformedBlocks );\n\t}\n\n\t/**\n\t * The `isSynced` check is a stopgap solution here.\n\t * Ideally, the Transforms API should handle this\n\t * by allowing to exclude blocks from wildcard transformations.\n\t */\n\tconst isSingleBlock = blocks.length === 1;\n\tconst isSynced =\n\t\tisSingleBlock &&\n\t\t( isTemplatePart( blocks[ 0 ] ) || isReusableBlock( blocks[ 0 ] ) );\n\tconst hasPossibleBlockTransformations =\n\t\t!! possibleBlockTransformations?.length && canRemove && ! isSynced;\n\tconst hasPossibleBlockVariationTransformations =\n\t\t!! blockVariationTransformations?.length;\n\tconst hasPatternTransformation = !! patterns?.length && canRemove;\n\tconst hasBlockOrBlockVariationTransforms =\n\t\thasPossibleBlockTransformations ||\n\t\thasPossibleBlockVariationTransformations;\n\tconst hasContents =\n\t\thasBlockStyles ||\n\t\thasBlockOrBlockVariationTransforms ||\n\t\thasPatternTransformation;\n\tif ( ! hasContents ) {\n\t\treturn (\n\t\t\t<p className=\"block-editor-block-switcher__no-transforms\">\n\t\t\t\t{ __( 'No transforms.' ) }\n\t\t\t</p>\n\t\t);\n\t}\n\n\tconst connectedBlockDescription = isSingleBlock\n\t\t? _x(\n\t\t\t\t'This block is connected.',\n\t\t\t\t'block toolbar button label and description'\n\t\t )\n\t\t: _x(\n\t\t\t\t'These blocks are connected.',\n\t\t\t\t'block toolbar button label and description'\n\t\t );\n\n\treturn (\n\t\t<div className=\"block-editor-block-switcher__container\">\n\t\t\t{ hasPatternTransformation && (\n\t\t\t\t<PatternTransformationsMenu\n\t\t\t\t\tblocks={ blocks }\n\t\t\t\t\tpatterns={ patterns }\n\t\t\t\t\tonSelect={ ( transformedBlocks ) => {\n\t\t\t\t\t\tonPatternTransform( transformedBlocks );\n\t\t\t\t\t\tonClose();\n\t\t\t\t\t} }\n\t\t\t\t/>\n\t\t\t) }\n\t\t\t{ hasBlockOrBlockVariationTransforms && (\n\t\t\t\t<BlockTransformationsMenu\n\t\t\t\t\tclassName=\"block-editor-block-switcher__transforms__menugroup\"\n\t\t\t\t\tpossibleBlockTransformations={\n\t\t\t\t\t\tpossibleBlockTransformations\n\t\t\t\t\t}\n\t\t\t\t\tpossibleBlockVariationTransformations={\n\t\t\t\t\t\tblockVariationTransformations\n\t\t\t\t\t}\n\t\t\t\t\tblocks={ blocks }\n\t\t\t\t\tonSelect={ ( name ) => {\n\t\t\t\t\t\tonBlockTransform( name );\n\t\t\t\t\t\tonClose();\n\t\t\t\t\t} }\n\t\t\t\t\tonSelectVariation={ ( name ) => {\n\t\t\t\t\t\tonBlockVariationTransform( name );\n\t\t\t\t\t\tonClose();\n\t\t\t\t\t} }\n\t\t\t\t/>\n\t\t\t) }\n\t\t\t{ hasBlockStyles && (\n\t\t\t\t<BlockStylesMenu\n\t\t\t\t\thoveredBlock={ blocks[ 0 ] }\n\t\t\t\t\tonSwitch={ onClose }\n\t\t\t\t/>\n\t\t\t) }\n\t\t\t{ isUsingBindings && (\n\t\t\t\t<MenuGroup>\n\t\t\t\t\t<Text className=\"block-editor-block-switcher__binding-indicator\">\n\t\t\t\t\t\t{ connectedBlockDescription }\n\t\t\t\t\t</Text>\n\t\t\t\t</MenuGroup>\n\t\t\t) }\n\t\t</div>\n\t);\n}\n\nexport const BlockSwitcher = ( { children, clientIds, label, text } ) => {\n\tconst isSingleBlock = clientIds.length === 1;\n\n\tconst blockSwitcherDescription = isSingleBlock\n\t\t? __( 'Change block type or style' )\n\t\t: sprintf(\n\t\t\t\t/* translators: %d: number of blocks. */\n\t\t\t\t_n(\n\t\t\t\t\t'Change type of %d block',\n\t\t\t\t\t'Change type of %d blocks',\n\t\t\t\t\tclientIds.length\n\t\t\t\t),\n\t\t\t\tclientIds.length\n\t\t );\n\treturn (\n\t\t<ToolbarGroup>\n\t\t\t<ToolbarItem>\n\t\t\t\t{ ( toggleProps ) => (\n\t\t\t\t\t<DropdownMenu\n\t\t\t\t\t\tclassName=\"block-editor-block-switcher\"\n\t\t\t\t\t\tlabel={ label }\n\t\t\t\t\t\tpopoverProps={ {\n\t\t\t\t\t\t\tplacement: 'bottom-start',\n\t\t\t\t\t\t\tclassName: 'block-editor-block-switcher__popover',\n\t\t\t\t\t\t} }\n\t\t\t\t\t\ticon={ children }\n\t\t\t\t\t\ttext={ text }\n\t\t\t\t\t\ttoggleProps={ {\n\t\t\t\t\t\t\tdescription: blockSwitcherDescription,\n\t\t\t\t\t\t\t...toggleProps,\n\t\t\t\t\t\t} }\n\t\t\t\t\t\tmenuProps={ { orientation: 'both' } }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ ( { onClose } ) => (\n\t\t\t\t\t\t\t<BlockSwitcherDropdownMenuContents\n\t\t\t\t\t\t\t\tonClose={ onClose }\n\t\t\t\t\t\t\t\tclientIds={ clientIds }\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t) }\n\t\t\t\t\t</DropdownMenu>\n\t\t\t\t) }\n\t\t\t</ToolbarItem>\n\t\t</ToolbarGroup>\n\t);\n};\n\nexport default BlockSwitcher;\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAoC;AACpC,wBAMO;AACP,oBAKO;AACP,kBAAuC;AAKvC,mBAA0C;AAC1C,wCAAqC;AACrC,6CAA4C;AAC5C,+BAA4B;AAC5B,0CAAuC;AAwGpC;AAtGH,SAAS,kCAAmC,EAAE,SAAS,UAAU,GAAI;AACpE,QAAM,EAAE,eAAe,aAAa,sBAAsB,QACzD,yBAAa,aAAAA,KAAiB;AAC/B,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,QAAI;AAAA,IACH,CAAE,WAAY;AACb,YAAM;AAAA,QACL;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACD,IAAI,OAAQ,aAAAA,KAAiB;AAC7B,YAAM,EAAE,eAAe,IAAI,OAAQ,cAAAC,KAAY;AAC/C,YAAM,eAAe,qBAAsB,UAAW,CAAE,CAAE;AAC1D,YAAM,UAAU,oBAAqB,SAAU;AAC/C,YAAM,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AAA;AAAA,QAEA;AAAA,MACC;AAAA,MACA;AAAA,MACA,UAAU;AAAA,IACX;AAAA,IACA,UAAU;AAAA,EACV;AACH,SACC,4CAAC,kCACA,sDAAC,iCACE,WAAE,gBACH;AAAA,IAAC;AAAA;AAAA,MACA,WAAU;AAAA,MACV;AAAA,MACA,cAAe;AAAA,QACd,WAAW;AAAA,QACX,WAAW;AAAA,MACZ;AAAA,MACA,MAAO;AAAA,MACP;AAAA,MACA,aAAc;AAAA,QACb,aAAa;AAAA,QACb,GAAG;AAAA,MACJ;AAAA,MACA,WAAY,EAAE,aAAa,OAAO;AAAA,MAEhC,WAAE,EAAE,QAAQ,MACb;AAAA,QAAC;AAAA;AAAA,UACA;AAAA,UACA;AAAA;AAAA,MACD;AAAA;AAAA,EAEF,GAEF,GACD;AAEF;AAEA,IAAO,yBAAQ;", "names": ["blockEditorStore", "blocksStore", "PatternTransformationsMenu", "BlockTransformationsMenu", "BlockStylesMenu", "Text"] }