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