BlockAppender.js
3 years ago
BlockControls.js
2 years ago
ComponentCSS.js
1 year ago
ContainerContentRenderer.js
2 years ago
GridItem.js
4 years ago
InspectorAdvancedControls.js
2 years ago
LegacyLayoutControls.js
3 years ago
ShapeDividers.js
4 years ago
TagName.js
3 years ago
BlockAppender.js
86 lines
| 1 | import { useInnerBlocksCount } from '../../../hooks'; |
| 2 | import { Inserter } from '@wordpress/block-editor'; |
| 3 | import { useDispatch } from '@wordpress/data'; |
| 4 | import { __, sprintf, _x } from '@wordpress/i18n'; |
| 5 | import { Button, Icon, Tooltip } from '@wordpress/components'; |
| 6 | import getIcon from '../../../utils/get-icon'; |
| 7 | import { applyFilters } from '@wordpress/hooks'; |
| 8 | import { plus } from '@wordpress/icons'; |
| 9 | |
| 10 | export default ( { clientId, isSelected, attributes } ) => { |
| 11 | const { isBlockPreview } = attributes; |
| 12 | const innerBlocksCount = useInnerBlocksCount( clientId ); |
| 13 | const hasChildBlocks = 0 < innerBlocksCount; |
| 14 | const { selectBlock } = useDispatch( 'core/block-editor' ); |
| 15 | |
| 16 | let appender = false; |
| 17 | |
| 18 | if ( isBlockPreview ) { |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | function ButtonBlockAppender() { |
| 23 | return ( |
| 24 | <Inserter |
| 25 | position="bottom right" |
| 26 | rootClientId={ clientId } |
| 27 | __experimentalIsQuick |
| 28 | renderToggle={ ( { |
| 29 | onToggle, |
| 30 | disabled, |
| 31 | isOpen, |
| 32 | blockTitle, |
| 33 | hasSingleBlockType, |
| 34 | } ) => { |
| 35 | const label = hasSingleBlockType |
| 36 | ? sprintf( |
| 37 | // translators: %s: the name of the block when there is only one |
| 38 | _x( 'Add %s', 'directly add the only allowed block', 'generateblocks' ), |
| 39 | blockTitle |
| 40 | ) : _x( 'Add block', 'Generic label for block inserter button', 'generateblocks' ); |
| 41 | |
| 42 | return ( |
| 43 | <Tooltip text={ label }> |
| 44 | <Button |
| 45 | className={ 'block-editor-button-block-appender' } |
| 46 | onClick={ onToggle } |
| 47 | aria-haspopup={ ! hasSingleBlockType ? 'true' : undefined } |
| 48 | aria-expanded={ ! hasSingleBlockType ? isOpen : undefined } |
| 49 | disabled={ disabled } |
| 50 | label={ label } |
| 51 | > |
| 52 | <Icon icon={ plus } /> |
| 53 | </Button> |
| 54 | </Tooltip> |
| 55 | ); |
| 56 | } } |
| 57 | isAppender |
| 58 | /> |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | // Selected Container. |
| 63 | if ( isSelected ) { |
| 64 | appender = <ButtonBlockAppender />; |
| 65 | } |
| 66 | |
| 67 | // Empty non-selected Container. |
| 68 | if ( ! hasChildBlocks && ! isSelected ) { |
| 69 | appender = <Button |
| 70 | className="gblocks-container-selector" |
| 71 | onClick={ () => selectBlock( clientId ) } |
| 72 | aria-label={ __( 'Select Container', 'generateblocks' ) } |
| 73 | > |
| 74 | <span className="gblocks-container-selector__icon"> |
| 75 | { getIcon( 'container' ) } |
| 76 | </span> |
| 77 | </Button>; |
| 78 | } |
| 79 | |
| 80 | return applyFilters( |
| 81 | 'generateblocks.editor.containerAppender', |
| 82 | appender, |
| 83 | { clientId, isSelected, attributes } |
| 84 | ); |
| 85 | }; |
| 86 |