@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 9.64 kB
Source Map (JSON)
{"version":3,"file":"edit.cjs","names":["useToolsPanelDropdownMenuProps","ToolsPanel","__","ToolsPanelItem","ToggleControl","useRegistry","useSelect","blockEditorStore","useDispatch","useBlockProps","useInnerBlocksProps","BlockControls","BlockVerticalAlignmentToolbar","InspectorControls","blocksStore","__experimentalBlockVariationPicker","createBlocksFromInnerBlocksTemplate"],"sources":["../../src/columns/edit.jsx"],"sourcesContent":["import clsx from 'clsx';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tToggleControl,\n\t__experimentalToolsPanel as ToolsPanel,\n\t__experimentalToolsPanelItem as ToolsPanelItem,\n} from '@wordpress/components';\nimport {\n\tInspectorControls,\n\tuseInnerBlocksProps,\n\tBlockControls,\n\tBlockVerticalAlignmentToolbar,\n\t__experimentalBlockVariationPicker,\n\tuseBlockProps,\n\tstore as blockEditorStore,\n} from '@wordpress/block-editor';\nimport { useDispatch, useSelect, useRegistry } from '@wordpress/data';\nimport {\n\tcreateBlocksFromInnerBlocksTemplate,\n\tstore as blocksStore,\n} from '@wordpress/blocks';\nimport { useToolsPanelDropdownMenuProps } from '../utils/hooks';\n\nconst DEFAULT_BLOCK = {\n\tname: 'core/column',\n};\n\nfunction ColumnInspectorControls( { setAttributes, isStackedOnMobile } ) {\n\tconst dropdownMenuProps = useToolsPanelDropdownMenuProps();\n\n\treturn (\n\t\t<ToolsPanel\n\t\t\tlabel={ __( 'Settings' ) }\n\t\t\tresetAll={ () => {\n\t\t\t\tsetAttributes( {\n\t\t\t\t\tisStackedOnMobile: true,\n\t\t\t\t} );\n\t\t\t} }\n\t\t\tdropdownMenuProps={ dropdownMenuProps }\n\t\t>\n\t\t\t<ToolsPanelItem\n\t\t\t\tlabel={ __( 'Stack on mobile' ) }\n\t\t\t\tisShownByDefault\n\t\t\t\thasValue={ () => isStackedOnMobile !== true }\n\t\t\t\tonDeselect={ () =>\n\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\tisStackedOnMobile: true,\n\t\t\t\t\t} )\n\t\t\t\t}\n\t\t\t>\n\t\t\t\t<ToggleControl\n\t\t\t\t\tlabel={ __( 'Stack on mobile' ) }\n\t\t\t\t\tchecked={ isStackedOnMobile }\n\t\t\t\t\tonChange={ () =>\n\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\tisStackedOnMobile: ! isStackedOnMobile,\n\t\t\t\t\t\t} )\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t</ToolsPanelItem>\n\t\t</ToolsPanel>\n\t);\n}\n\nfunction ColumnsEditContainer( { attributes, setAttributes, clientId } ) {\n\tconst { isStackedOnMobile, verticalAlignment, templateLock } = attributes;\n\tconst registry = useRegistry();\n\tconst { getBlockOrder } = useSelect( blockEditorStore );\n\tconst { updateBlockAttributes } = useDispatch( blockEditorStore );\n\n\tconst classes = clsx( {\n\t\t[ `are-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,\n\t\t[ `is-not-stacked-on-mobile` ]: ! isStackedOnMobile,\n\t} );\n\n\tconst blockProps = useBlockProps( {\n\t\tclassName: classes,\n\t} );\n\tconst innerBlocksProps = useInnerBlocksProps( blockProps, {\n\t\tdefaultBlock: DEFAULT_BLOCK,\n\t\tdirectInsert: true,\n\t\torientation: 'horizontal',\n\t\trenderAppender: false,\n\t\ttemplateLock,\n\t} );\n\n\t/**\n\t * Update all child Column blocks with a new vertical alignment setting\n\t * based on whatever alignment is passed in. This allows change to parent\n\t * to override anything set on a individual column basis.\n\t *\n\t * @param {string} newVerticalAlignment The vertical alignment setting.\n\t */\n\tfunction updateAlignment( newVerticalAlignment ) {\n\t\tconst innerBlockClientIds = getBlockOrder( clientId );\n\n\t\t// Update own and child Column block vertical alignments.\n\t\t// This is a single action; the batching prevents creating multiple history records.\n\t\tregistry.batch( () => {\n\t\t\tsetAttributes( { verticalAlignment: newVerticalAlignment } );\n\t\t\tupdateBlockAttributes( innerBlockClientIds, {\n\t\t\t\tverticalAlignment: newVerticalAlignment,\n\t\t\t} );\n\t\t} );\n\t}\n\n\treturn (\n\t\t<>\n\t\t\t<BlockControls>\n\t\t\t\t<BlockVerticalAlignmentToolbar\n\t\t\t\t\tonChange={ updateAlignment }\n\t\t\t\t\tvalue={ verticalAlignment }\n\t\t\t\t/>\n\t\t\t</BlockControls>\n\t\t\t<InspectorControls>\n\t\t\t\t<ColumnInspectorControls\n\t\t\t\t\tsetAttributes={ setAttributes }\n\t\t\t\t\tisStackedOnMobile={ isStackedOnMobile }\n\t\t\t\t/>\n\t\t\t</InspectorControls>\n\t\t\t<div { ...innerBlocksProps } />\n\t\t</>\n\t);\n}\n\nfunction Placeholder( { clientId, name, setAttributes } ) {\n\tconst { blockType, variations } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getBlockVariations, getBlockType } = select( blocksStore );\n\n\t\t\treturn {\n\t\t\t\tblockType: getBlockType( name ),\n\t\t\t\tvariations: getBlockVariations( name, 'block' ),\n\t\t\t};\n\t\t},\n\t\t[ name ]\n\t);\n\tconst { replaceInnerBlocks } = useDispatch( blockEditorStore );\n\tconst blockProps = useBlockProps();\n\n\treturn (\n\t\t<div { ...blockProps }>\n\t\t\t<__experimentalBlockVariationPicker\n\t\t\t\ticon={ blockType?.icon?.src }\n\t\t\t\tlabel={ blockType?.title }\n\t\t\t\tvariations={ variations }\n\t\t\t\tinstructions={ __( 'Divide into columns. Select a layout:' ) }\n\t\t\t\tonSelect={ ( nextVariation ) => {\n\t\t\t\t\tif ( nextVariation.attributes ) {\n\t\t\t\t\t\tsetAttributes( nextVariation.attributes );\n\t\t\t\t\t}\n\t\t\t\t\tif ( nextVariation.innerBlocks ) {\n\t\t\t\t\t\treplaceInnerBlocks(\n\t\t\t\t\t\t\tclientId,\n\t\t\t\t\t\t\tcreateBlocksFromInnerBlocksTemplate(\n\t\t\t\t\t\t\t\tnextVariation.innerBlocks\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\ttrue\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} }\n\t\t\t/>\n\t\t</div>\n\t);\n}\n\n/**\n * Renders the `core/columns` block in the editor.\n *\n * Until the block has inner blocks it renders a variation picker; afterwards it\n * renders the columns container. Every prop is forwarded to whichever of the two\n * is rendered.\n *\n * @param {Object} props Component props.\n * @param {string} props.clientId Client ID of the block.\n * @param {string} props.name Block name, used to look up the variations offered by the picker.\n * @param {Object} props.attributes Block attributes.\n * @param {string} [props.attributes.verticalAlignment] Vertical alignment applied to the block and to every child Column block.\n * @param {boolean} [props.attributes.isStackedOnMobile] Whether the columns stack on small viewports. Defaults to `true`.\n * @param {string|boolean} [props.attributes.templateLock] Template lock applied to the inner blocks, one of `all`, `insert`, `contentOnly` or `false`.\n * @param {Function} props.setAttributes Callback for updating block attributes.\n *\n * @return {React.JSX.Element} React element.\n */\nconst ColumnsEdit = ( props ) => {\n\tconst { clientId } = props;\n\tconst hasInnerBlocks = useSelect(\n\t\t( select ) =>\n\t\t\tselect( blockEditorStore ).getBlocks( clientId ).length > 0,\n\t\t[ clientId ]\n\t);\n\tconst Component = hasInnerBlocks ? ColumnsEditContainer : Placeholder;\n\n\treturn <Component { ...props } />;\n};\n\nexport default ColumnsEdit;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuBA,MAAM,gBAAgB,EACrB,MAAM,cACP;AAEA,SAAS,wBAAyB,EAAE,eAAe,qBAAsB;CACxE,MAAM,qBAAA,GAAoBA,kBAAAA,+BAAAA,CAA+B;CAEzD,OACC,iBAAA,GAAA,kBAAA,IAAA,CAACC,sBAAAA,0BAAD;EACC,QAAA,GAAQC,gBAAAA,GAAAA,CAAI,UAAW;EACvB,gBAAiB;GAChB,cAAe,EACd,mBAAmB,KACpB,CAAE;EACH;EACoB;EAEpB,UAAA,iBAAA,GAAA,kBAAA,IAAA,CAACC,sBAAAA,8BAAD;GACC,QAAA,GAAQD,gBAAAA,GAAAA,CAAI,iBAAkB;GAC9B,kBAAA;GACA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