UNPKG

@wordpress/block-library

Version:
1 lines • 8.69 kB
{"version":3,"file":"edit.mjs","names":["ToolsPanel","ToolsPanelItem","blockEditorStore","blocksStore"],"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,oBAAoB,+BAA+B;CAEzD,OACC,oBAACA,0BAAD;EACC,OAAQ,GAAI,UAAW;EACvB,gBAAiB;GAChB,cAAe,EACd,mBAAmB,KACpB,CAAE;EACH;EACoB;EAEpB,UAAA,oBAACC,8BAAD;GACC,OAAQ,GAAI,iBAAkB;GAC9B,kBAAA;GACA,gBAAiB,sBAAsB;GACvC,kBACC,cAAe,EACd,mBAAmB,KACpB,CAAE;GAGH,UAAA,oBAAC,eAAD;IACC,OAAQ,GAAI,iBAAkB;IAC9B,SAAU;IACV,gBACC,cAAe,EACd,mBAAmB,CAAE,kBACtB,CAAE;GAEH,CAAA;EACc,CAAA;CACL,CAAA;AAEd;AAEA,SAAS,qBAAsB,EAAE,YAAY,eAAe,YAAa;CACxE,MAAM,EAAE,mBAAmB,mBAAmB,iBAAiB;CAC/D,MAAM,WAAW,YAAY;CAC7B,M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