UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 8.64 kB
{ "version": 3, "sources": ["../../../src/components/block-switcher/block-transformations-menu.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { MenuGroup, MenuItem } from '@wordpress/components';\nimport {\n\tgetBlockMenuDefaultClassName,\n\tswitchToBlockType,\n} from '@wordpress/blocks';\nimport { useState, useMemo } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport BlockIcon from '../block-icon';\nimport PreviewBlockPopover from './preview-block-popover';\nimport BlockVariationTransformations from './block-variation-transformations';\n\n/**\n * Helper hook to group transformations to display them in a specific order in the UI.\n * For now we group only priority content driven transformations(ex. paragraph -> heading).\n *\n * Later on we could also group 'layout' transformations(ex. paragraph -> group) and\n * display them in different sections.\n *\n * @param {Object[]} possibleBlockTransformations The available block transformations.\n * @return {Record<string, Object[]>} The grouped block transformations.\n */\nfunction useGroupedTransforms( possibleBlockTransformations ) {\n\tconst priorityContentTransformationBlocks = {\n\t\t'core/paragraph': 1,\n\t\t'core/heading': 2,\n\t\t'core/list': 3,\n\t\t'core/quote': 4,\n\t};\n\tconst transformations = useMemo( () => {\n\t\tconst priorityTextTransformsNames = Object.keys(\n\t\t\tpriorityContentTransformationBlocks\n\t\t);\n\t\tconst groupedPossibleTransforms = possibleBlockTransformations.reduce(\n\t\t\t( accumulator, item ) => {\n\t\t\t\tconst { name } = item;\n\t\t\t\tif ( priorityTextTransformsNames.includes( name ) ) {\n\t\t\t\t\taccumulator.priorityTextTransformations.push( item );\n\t\t\t\t} else {\n\t\t\t\t\taccumulator.restTransformations.push( item );\n\t\t\t\t}\n\t\t\t\treturn accumulator;\n\t\t\t},\n\t\t\t{ priorityTextTransformations: [], restTransformations: [] }\n\t\t);\n\t\t/**\n\t\t * If there is only one priority text transformation and it's a Quote,\n\t\t * is should move to the rest transformations. This is because Quote can\n\t\t * be a container for any block type, so in multi-block selection it will\n\t\t * always be suggested, even for non-text blocks.\n\t\t */\n\t\tif (\n\t\t\tgroupedPossibleTransforms.priorityTextTransformations.length ===\n\t\t\t\t1 &&\n\t\t\tgroupedPossibleTransforms.priorityTextTransformations[ 0 ].name ===\n\t\t\t\t'core/quote'\n\t\t) {\n\t\t\tconst singleQuote =\n\t\t\t\tgroupedPossibleTransforms.priorityTextTransformations.pop();\n\t\t\tgroupedPossibleTransforms.restTransformations.push( singleQuote );\n\t\t}\n\t\treturn groupedPossibleTransforms;\n\t}, [ possibleBlockTransformations ] );\n\n\t// Order the priority text transformations.\n\ttransformations.priorityTextTransformations.sort(\n\t\t( { name: currentName }, { name: nextName } ) => {\n\t\t\treturn priorityContentTransformationBlocks[ currentName ] <\n\t\t\t\tpriorityContentTransformationBlocks[ nextName ]\n\t\t\t\t? -1\n\t\t\t\t: 1;\n\t\t}\n\t);\n\treturn transformations;\n}\n\nconst BlockTransformationsMenu = ( {\n\tclassName,\n\tpossibleBlockTransformations,\n\tpossibleBlockVariationTransformations,\n\tonSelect,\n\tonSelectVariation,\n\tblocks,\n} ) => {\n\tconst [ hoveredTransformItemName, setHoveredTransformItemName ] =\n\t\tuseState();\n\n\tconst { priorityTextTransformations, restTransformations } =\n\t\tuseGroupedTransforms( possibleBlockTransformations );\n\t// We have to check if both content transformations(priority and rest) are set\n\t// in order to create a separate MenuGroup for them.\n\tconst hasBothContentTransformations =\n\t\tpriorityTextTransformations.length && restTransformations.length;\n\tconst restTransformItems = !! restTransformations.length && (\n\t\t<RestTransformationItems\n\t\t\trestTransformations={ restTransformations }\n\t\t\tonSelect={ onSelect }\n\t\t\tsetHoveredTransformItemName={ setHoveredTransformItemName }\n\t\t/>\n\t);\n\treturn (\n\t\t<>\n\t\t\t<MenuGroup label={ __( 'Transform to' ) } className={ className }>\n\t\t\t\t{ hoveredTransformItemName && (\n\t\t\t\t\t<PreviewBlockPopover\n\t\t\t\t\t\tblocks={ switchToBlockType(\n\t\t\t\t\t\t\tblocks,\n\t\t\t\t\t\t\thoveredTransformItemName\n\t\t\t\t\t\t) }\n\t\t\t\t\t/>\n\t\t\t\t) }\n\t\t\t\t{ !! possibleBlockVariationTransformations?.length && (\n\t\t\t\t\t<BlockVariationTransformations\n\t\t\t\t\t\ttransformations={\n\t\t\t\t\t\t\tpossibleBlockVariationTransformations\n\t\t\t\t\t\t}\n\t\t\t\t\t\tblocks={ blocks }\n\t\t\t\t\t\tonSelect={ onSelectVariation }\n\t\t\t\t\t/>\n\t\t\t\t) }\n\t\t\t\t{ priorityTextTransformations.map( ( item ) => (\n\t\t\t\t\t<BlockTransformationItem\n\t\t\t\t\t\tkey={ item.name }\n\t\t\t\t\t\titem={ item }\n\t\t\t\t\t\tonSelect={ onSelect }\n\t\t\t\t\t\tsetHoveredTransformItemName={\n\t\t\t\t\t\t\tsetHoveredTransformItemName\n\t\t\t\t\t\t}\n\t\t\t\t\t/>\n\t\t\t\t) ) }\n\t\t\t\t{ ! hasBothContentTransformations && restTransformItems }\n\t\t\t</MenuGroup>\n\t\t\t{ !! hasBothContentTransformations && (\n\t\t\t\t<MenuGroup className={ className }>\n\t\t\t\t\t{ restTransformItems }\n\t\t\t\t</MenuGroup>\n\t\t\t) }\n\t\t</>\n\t);\n};\n\nfunction RestTransformationItems( {\n\trestTransformations,\n\tonSelect,\n\tsetHoveredTransformItemName,\n} ) {\n\treturn restTransformations.map( ( item ) => (\n\t\t<BlockTransformationItem\n\t\t\tkey={ item.name }\n\t\t\titem={ item }\n\t\t\tonSelect={ onSelect }\n\t\t\tsetHoveredTransformItemName={ setHoveredTransformItemName }\n\t\t/>\n\t) );\n}\n\nfunction BlockTransformationItem( {\n\titem,\n\tonSelect,\n\tsetHoveredTransformItemName,\n} ) {\n\tconst { name, icon, title, isDisabled } = item;\n\treturn (\n\t\t<MenuItem\n\t\t\tclassName={ getBlockMenuDefaultClassName( name ) }\n\t\t\tonClick={ ( event ) => {\n\t\t\t\tevent.preventDefault();\n\t\t\t\tonSelect( name );\n\t\t\t} }\n\t\t\tdisabled={ isDisabled }\n\t\t\tonMouseLeave={ () => setHoveredTransformItemName( null ) }\n\t\t\tonMouseEnter={ () => setHoveredTransformItemName( name ) }\n\t\t>\n\t\t\t<BlockIcon icon={ icon } showColors />\n\t\t\t{ title }\n\t\t</MenuItem>\n\t);\n}\n\nexport default BlockTransformationsMenu;\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAmB;AACnB,wBAAoC;AACpC,oBAGO;AACP,qBAAkC;AAKlC,wBAAsB;AACtB,mCAAgC;AAChC,6CAA0C;AAoFxC;AAxEF,SAAS,qBAAsB,8BAA+B;AAC7D,QAAM,sCAAsC;AAAA,IAC3C,kBAAkB;AAAA,IAClB,gBAAgB;AAAA,IAChB,aAAa;AAAA,IACb,cAAc;AAAA,EACf;AACA,QAAM,sBAAkB,wBAAS,MAAM;AACtC,UAAM,8BAA8B,OAAO;AAAA,MAC1C;AAAA,IACD;AACA,UAAM,4BAA4B,6BAA6B;AAAA,MAC9D,CAAE,aAAa,SAAU;AACxB,cAAM,EAAE,KAAK,IAAI;AACjB,YAAK,4BAA4B,SAAU,IAAK,GAAI;AACnD,sBAAY,4BAA4B,KAAM,IAAK;AAAA,QACpD,OAAO;AACN,sBAAY,oBAAoB,KAAM,IAAK;AAAA,QAC5C;AACA,eAAO;AAAA,MACR;AAAA,MACA,EAAE,6BAA6B,CAAC,GAAG,qBAAqB,CAAC,EAAE;AAAA,IAC5D;AAOA,QACC,0BAA0B,4BAA4B,WACrD,KACD,0BAA0B,4BAA6B,CAAE,EAAE,SAC1D,cACA;AACD,YAAM,cACL,0BAA0B,4BAA4B,IAAI;AAC3D,gCAA0B,oBAAoB,KAAM,WAAY;AAAA,IACjE;AACA,WAAO;AAAA,EACR,GAAG,CAAE,4BAA6B,CAAE;AAGpC,kBAAgB,4BAA4B;AAAA,IAC3C,CAAE,EAAE,MAAM,YAAY,GAAG,EAAE,MAAM,SAAS,MAAO;AAChD,aAAO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