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