BlockControls.js
4 years ago
ComponentCSS.js
4 years ago
ContainerContentRenderer.js
4 years ago
GridItem.js
4 years ago
InspectorControls.js
4 years ago
ShapeDividers.js
4 years ago
ContainerContentRenderer.js
140 lines
| 1 | import RootElement from '../../../components/root-element'; |
| 2 | import GridItem from './GridItem'; |
| 3 | import Element from '../../../components/element'; |
| 4 | import { applyFilters } from '@wordpress/hooks'; |
| 5 | import { InnerBlocks, useBlockProps } from '@wordpress/block-editor'; |
| 6 | import { Button } from '@wordpress/components'; |
| 7 | import { __ } from '@wordpress/i18n'; |
| 8 | import getIcon from '../../../utils/get-icon'; |
| 9 | import ShapeDividers from './ShapeDividers'; |
| 10 | import classnames from 'classnames'; |
| 11 | import { useInnerBlocksCount } from '../../../hooks'; |
| 12 | import { useDispatch } from '@wordpress/data'; |
| 13 | import ComponentCSS from './ComponentCSS'; |
| 14 | import getBackgroundImageUrl from '../../../utils/get-background-image-url'; |
| 15 | |
| 16 | export default function ContainerContentRenderer( props ) { |
| 17 | const { |
| 18 | attributes, |
| 19 | clientId, |
| 20 | name, |
| 21 | filterTagName, |
| 22 | allShapes, |
| 23 | deviceType, |
| 24 | } = props; |
| 25 | |
| 26 | const { |
| 27 | uniqueId, |
| 28 | className, |
| 29 | anchor, |
| 30 | tagName, |
| 31 | backgroundColor, |
| 32 | isGrid, |
| 33 | bgOptions, |
| 34 | bgImageInline, |
| 35 | align, |
| 36 | isBlockPreview = false, |
| 37 | } = attributes; |
| 38 | |
| 39 | const { selectBlock } = useDispatch( 'core/block-editor' ); |
| 40 | const innerBlocksCount = useInnerBlocksCount( clientId ); |
| 41 | const hasChildBlocks = 0 < innerBlocksCount; |
| 42 | |
| 43 | let hasStyling = ( |
| 44 | !! backgroundColor || |
| 45 | attributes.borderSizeTop || |
| 46 | attributes.borderSizeRight || |
| 47 | attributes.borderSizeBottom || |
| 48 | attributes.borderSizeLeft |
| 49 | ); |
| 50 | |
| 51 | hasStyling = applyFilters( 'generateblocks.editor.containerHasStyling', hasStyling, props ); |
| 52 | |
| 53 | let htmlAttributes = { |
| 54 | className: classnames( { |
| 55 | 'gb-container': true, |
| 56 | [ `gb-container-${ uniqueId }` ]: true, |
| 57 | [ `${ className }` ]: undefined !== className, |
| 58 | 'gb-container-empty': ! hasChildBlocks && ! isBlockPreview, |
| 59 | 'gb-container-visual-guides': ! hasChildBlocks && ! hasStyling && ! props.isSelected && ! isBlockPreview, |
| 60 | } ), |
| 61 | id: anchor ? anchor : null, |
| 62 | 'data-align': align ? align : null, |
| 63 | }; |
| 64 | |
| 65 | const backgroundUrl = getBackgroundImageUrl( props ); |
| 66 | |
| 67 | if ( bgImageInline && backgroundUrl ) { |
| 68 | let imageAttributeName = 'background-image'; |
| 69 | |
| 70 | if ( 'element' !== bgOptions.selector ) { |
| 71 | imageAttributeName = '--' + imageAttributeName; |
| 72 | } |
| 73 | |
| 74 | htmlAttributes.style = { |
| 75 | [ imageAttributeName ]: 'url(' + backgroundUrl + ')', |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | htmlAttributes = applyFilters( |
| 80 | 'generateblocks.frontend.htmlAttributes', |
| 81 | htmlAttributes, |
| 82 | 'generateblocks/container', |
| 83 | attributes |
| 84 | ); |
| 85 | |
| 86 | const blockProps = useBlockProps( htmlAttributes ); |
| 87 | |
| 88 | return ( |
| 89 | <> |
| 90 | <ComponentCSS { ...props } deviceType={ deviceType } /> |
| 91 | |
| 92 | <RootElement name={ name } clientId={ clientId } align={ align }> |
| 93 | <GridItem isGrid={ isGrid } uniqueId={ uniqueId }> |
| 94 | <Element |
| 95 | tagName={ filterTagName( applyFilters( 'generateblocks.frontend.containerTagName', tagName, attributes ) ) } |
| 96 | htmlAttrs={ blockProps } |
| 97 | > |
| 98 | { applyFilters( 'generateblocks.frontend.afterContainerOpen', '', attributes ) } |
| 99 | <div className={ 'gb-inside-container' }> |
| 100 | { applyFilters( 'generateblocks.frontend.insideContainer', '', attributes ) } |
| 101 | <InnerBlocks |
| 102 | templateLock={ false } |
| 103 | renderAppender={ () => { |
| 104 | if ( isBlockPreview ) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | // Selected Container. |
| 109 | if ( props.isSelected ) { |
| 110 | return <InnerBlocks.ButtonBlockAppender />; |
| 111 | } |
| 112 | |
| 113 | // Empty non-selected Container. |
| 114 | if ( ! hasChildBlocks && ! props.isSelected ) { |
| 115 | return <Button |
| 116 | className="gblocks-container-selector" |
| 117 | onClick={ () => selectBlock( clientId ) } |
| 118 | aria-label={ __( 'Select Container', 'generateblocks' ) } |
| 119 | > |
| 120 | <span className="gblocks-container-selector__icon"> |
| 121 | { getIcon( 'container' ) } |
| 122 | </span> |
| 123 | </Button>; |
| 124 | } |
| 125 | |
| 126 | return false; |
| 127 | } } |
| 128 | /> |
| 129 | </div> |
| 130 | |
| 131 | <ShapeDividers attributes={ attributes } allShapes={ allShapes } /> |
| 132 | |
| 133 | { applyFilters( 'generateblocks.frontend.beforeContainerClose', '', attributes ) } |
| 134 | </Element> |
| 135 | </GridItem> |
| 136 | </RootElement> |
| 137 | </> |
| 138 | ); |
| 139 | } |
| 140 |