BlockControls.js
4 years ago
ComponentCSS.js
4 years ago
ContainerContentRenderer.js
3 years ago
GridItem.js
4 years ago
InspectorControls.js
4 years ago
ShapeDividers.js
4 years ago
ContainerContentRenderer.js
148 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, store as blockEditorStore } 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, useSelect } 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 | const supportsLayout = useSelect( ( select ) => { |
| 43 | const { |
| 44 | getSettings, |
| 45 | } = select( blockEditorStore ); |
| 46 | |
| 47 | return getSettings().supportsLayout || false; |
| 48 | }, [] ); |
| 49 | |
| 50 | let hasStyling = ( |
| 51 | !! backgroundColor || |
| 52 | attributes.borderSizeTop || |
| 53 | attributes.borderSizeRight || |
| 54 | attributes.borderSizeBottom || |
| 55 | attributes.borderSizeLeft |
| 56 | ); |
| 57 | |
| 58 | hasStyling = applyFilters( 'generateblocks.editor.containerHasStyling', hasStyling, props ); |
| 59 | |
| 60 | let htmlAttributes = { |
| 61 | className: classnames( { |
| 62 | 'gb-container': true, |
| 63 | [ `gb-container-${ uniqueId }` ]: true, |
| 64 | [ `${ className }` ]: undefined !== className, |
| 65 | 'gb-container-empty': ! hasChildBlocks && ! isBlockPreview, |
| 66 | 'gb-container-visual-guides': ! hasChildBlocks && ! hasStyling && ! props.isSelected && ! isBlockPreview, |
| 67 | [ `align${ align }` ]: supportsLayout, |
| 68 | } ), |
| 69 | id: anchor ? anchor : null, |
| 70 | 'data-align': align && ! supportsLayout ? align : null, |
| 71 | }; |
| 72 | |
| 73 | const backgroundUrl = getBackgroundImageUrl( props ); |
| 74 | |
| 75 | if ( bgImageInline && backgroundUrl ) { |
| 76 | let imageAttributeName = 'background-image'; |
| 77 | |
| 78 | if ( 'element' !== bgOptions.selector ) { |
| 79 | imageAttributeName = '--' + imageAttributeName; |
| 80 | } |
| 81 | |
| 82 | htmlAttributes.style = { |
| 83 | [ imageAttributeName ]: 'url(' + backgroundUrl + ')', |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | htmlAttributes = applyFilters( |
| 88 | 'generateblocks.frontend.htmlAttributes', |
| 89 | htmlAttributes, |
| 90 | 'generateblocks/container', |
| 91 | attributes |
| 92 | ); |
| 93 | |
| 94 | const blockProps = useBlockProps( htmlAttributes ); |
| 95 | |
| 96 | return ( |
| 97 | <> |
| 98 | <ComponentCSS { ...props } deviceType={ deviceType } /> |
| 99 | |
| 100 | <RootElement name={ name } clientId={ clientId } align={ align }> |
| 101 | <GridItem isGrid={ isGrid } uniqueId={ uniqueId }> |
| 102 | <Element |
| 103 | tagName={ filterTagName( applyFilters( 'generateblocks.frontend.containerTagName', tagName, attributes ) ) } |
| 104 | htmlAttrs={ blockProps } |
| 105 | > |
| 106 | { applyFilters( 'generateblocks.frontend.afterContainerOpen', '', attributes ) } |
| 107 | <div className={ 'gb-inside-container' }> |
| 108 | { applyFilters( 'generateblocks.frontend.insideContainer', '', attributes ) } |
| 109 | <InnerBlocks |
| 110 | templateLock={ false } |
| 111 | renderAppender={ () => { |
| 112 | if ( isBlockPreview ) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | // Selected Container. |
| 117 | if ( props.isSelected ) { |
| 118 | return <InnerBlocks.ButtonBlockAppender />; |
| 119 | } |
| 120 | |
| 121 | // Empty non-selected Container. |
| 122 | if ( ! hasChildBlocks && ! props.isSelected ) { |
| 123 | return <Button |
| 124 | className="gblocks-container-selector" |
| 125 | onClick={ () => selectBlock( clientId ) } |
| 126 | aria-label={ __( 'Select Container', 'generateblocks' ) } |
| 127 | > |
| 128 | <span className="gblocks-container-selector__icon"> |
| 129 | { getIcon( 'container' ) } |
| 130 | </span> |
| 131 | </Button>; |
| 132 | } |
| 133 | |
| 134 | return false; |
| 135 | } } |
| 136 | /> |
| 137 | </div> |
| 138 | |
| 139 | <ShapeDividers attributes={ attributes } allShapes={ allShapes } /> |
| 140 | |
| 141 | { applyFilters( 'generateblocks.frontend.beforeContainerClose', '', attributes ) } |
| 142 | </Element> |
| 143 | </GridItem> |
| 144 | </RootElement> |
| 145 | </> |
| 146 | ); |
| 147 | } |
| 148 |