components
4 years ago
css
4 years ago
attributes.js
4 years ago
block.js
4 years ago
deprecated.js
5 years ago
edit.js
3 years ago
editor.scss
4 years ago
edit.js
105 lines
| 1 | import { Fragment, useEffect, useState } from '@wordpress/element'; |
| 2 | import BlockControls from './components/BlockControls'; |
| 3 | import InspectorControls from './components/InspectorControls'; |
| 4 | import InspectorAdvancedControls from '../grid/components/InspectorAdvancedControls'; |
| 5 | import ComponentCSS from './components/ComponentCSS'; |
| 6 | import { InnerBlocks, useBlockProps, BlockContextProvider } from '@wordpress/block-editor'; |
| 7 | import { useDeviceType, useInnerBlocksCount } from '../../hooks'; |
| 8 | import classnames from 'classnames'; |
| 9 | import { applyFilters } from '@wordpress/hooks'; |
| 10 | import { compose } from '@wordpress/compose'; |
| 11 | import { withButtonContainerLegacyMigration, withUniqueId } from '../../hoc'; |
| 12 | import { useDispatch } from '@wordpress/data'; |
| 13 | import RootElement from '../../components/root-element'; |
| 14 | |
| 15 | const ButtonContainerEdit = ( props ) => { |
| 16 | const { |
| 17 | attributes, |
| 18 | setAttributes, |
| 19 | clientId, |
| 20 | name, |
| 21 | context, |
| 22 | } = props; |
| 23 | |
| 24 | const { |
| 25 | uniqueId, |
| 26 | className, |
| 27 | anchor, |
| 28 | } = attributes; |
| 29 | |
| 30 | const [ buttonCount, setButtonCount ] = useState( 0 ); |
| 31 | const [ deviceType, setDeviceType ] = useDeviceType( 'Desktop' ); |
| 32 | const innerBlocksCount = useInnerBlocksCount( clientId ); |
| 33 | |
| 34 | const { removeBlock } = useDispatch( 'core/block-editor' ); |
| 35 | |
| 36 | useEffect( () => { |
| 37 | // If we've removed all of our buttons, remove the container. |
| 38 | if ( 1 === buttonCount && 0 === innerBlocksCount ) { |
| 39 | removeBlock( clientId ); |
| 40 | } |
| 41 | |
| 42 | setButtonCount( innerBlocksCount ); |
| 43 | }, [ innerBlocksCount ] ); |
| 44 | |
| 45 | let htmlAttributes = { |
| 46 | className: classnames( { |
| 47 | 'gb-button-wrapper': true, |
| 48 | [ `gb-button-wrapper-${ uniqueId }` ]: true, |
| 49 | [ `${ className }` ]: undefined !== className, |
| 50 | } ), |
| 51 | id: anchor ? anchor : null, |
| 52 | }; |
| 53 | |
| 54 | htmlAttributes = applyFilters( |
| 55 | 'generateblocks.frontend.htmlAttributes', |
| 56 | htmlAttributes, |
| 57 | 'generateblocks/button-container', |
| 58 | attributes |
| 59 | ); |
| 60 | |
| 61 | const blockProps = useBlockProps( htmlAttributes ); |
| 62 | |
| 63 | return ( |
| 64 | <Fragment> |
| 65 | <BlockControls |
| 66 | attributes={ attributes } |
| 67 | setAttributes={ setAttributes } |
| 68 | clientId={ clientId } |
| 69 | deviceType={ deviceType } |
| 70 | /> |
| 71 | |
| 72 | <InspectorControls |
| 73 | { ...props } |
| 74 | deviceType={ deviceType } |
| 75 | setDeviceType={ setDeviceType } |
| 76 | state={ { deviceType } } |
| 77 | blockDefaults={ generateBlocksDefaults.buttonContainer } |
| 78 | /> |
| 79 | |
| 80 | <InspectorAdvancedControls anchor={ anchor } setAttributes={ setAttributes } /> |
| 81 | |
| 82 | <ComponentCSS { ...props } deviceType={ deviceType } /> |
| 83 | |
| 84 | <RootElement name={ name } clientId={ clientId }> |
| 85 | <div { ...blockProps }> |
| 86 | <BlockContextProvider value={ { 'generateblocks/query': context[ 'generateblocks/query' ] } }> |
| 87 | <InnerBlocks |
| 88 | allowedBlocks={ [ 'generateblocks/button' ] } |
| 89 | renderAppender={ false } |
| 90 | template={ [ |
| 91 | [ 'generateblocks/button', generateBlocksStyling.button ], |
| 92 | ] } |
| 93 | /> |
| 94 | </BlockContextProvider> |
| 95 | </div> |
| 96 | </RootElement> |
| 97 | </Fragment> |
| 98 | ); |
| 99 | }; |
| 100 | |
| 101 | export default compose( |
| 102 | withUniqueId, |
| 103 | withButtonContainerLegacyMigration |
| 104 | )( ButtonContainerEdit ); |
| 105 |