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