components
4 years ago
css
4 years ago
attributes.js
4 years ago
block-controls.js
4 years ago
block.js
4 years ago
deprecated.js
5 years ago
edit.js
4 years ago
editor.scss
4 years ago
block-controls.js
121 lines
| 1 | import getIcon from '../../utils/get-icon'; |
| 2 | |
| 3 | /** |
| 4 | * WordPress Dependencies |
| 5 | */ |
| 6 | import { __ } from '@wordpress/i18n'; |
| 7 | import { addFilter } from '@wordpress/hooks'; |
| 8 | import { Fragment } from '@wordpress/element'; |
| 9 | import { BlockControls, BlockAlignmentToolbar } from '@wordpress/block-editor'; |
| 10 | import { ToolbarGroup, ToolbarButton } from '@wordpress/components'; |
| 11 | import { createHigherOrderComponent } from '@wordpress/compose'; |
| 12 | import { cloneBlock, getBlockSupport } from '@wordpress/blocks'; |
| 13 | import { useDispatch, useSelect } from '@wordpress/data'; |
| 14 | |
| 15 | const WIDE_ALIGNMENTS = [ 'wide', 'full' ]; |
| 16 | |
| 17 | /** |
| 18 | * Add controls to the Container block toolbar. |
| 19 | * |
| 20 | * @param {Function} BlockEdit Block edit component. |
| 21 | * @return {Function} BlockEdit Modified block edit component. |
| 22 | */ |
| 23 | const withBlockControls = createHigherOrderComponent( |
| 24 | ( BlockEdit ) => ( props ) => { |
| 25 | if ( 'generateblocks/container' !== props.name ) { |
| 26 | return <BlockEdit { ...props } />; |
| 27 | } |
| 28 | |
| 29 | const { insertBlocks } = useDispatch( 'core/block-editor' ); |
| 30 | const { |
| 31 | getBlockParentsByBlockName, |
| 32 | getBlockRootClientId, |
| 33 | getBlocksByClientId, |
| 34 | } = useSelect( ( select ) => select( 'core/block-editor' ), [] ); |
| 35 | |
| 36 | const { |
| 37 | attributes, |
| 38 | clientId, |
| 39 | setAttributes, |
| 40 | } = props; |
| 41 | |
| 42 | const { |
| 43 | isGrid, |
| 44 | isQueryLoopItem, |
| 45 | align, |
| 46 | } = attributes; |
| 47 | |
| 48 | let parentGridId = false; |
| 49 | |
| 50 | if ( typeof getBlockParentsByBlockName === 'function' ) { |
| 51 | parentGridId = getBlockParentsByBlockName( clientId, 'generateblocks/grid', true )[ 0 ]; |
| 52 | } else { |
| 53 | parentGridId = getBlockRootClientId( clientId ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * We don't define "align" support in block registration as we don't want it enabled for grid items. |
| 58 | * This allows us to enable support for regular non-grid item Containers. |
| 59 | */ |
| 60 | const hasAlignmentSupport = getBlockSupport( '', 'align', true ) && ! isGrid; |
| 61 | |
| 62 | return ( |
| 63 | <Fragment> |
| 64 | { ! isQueryLoopItem && isGrid && parentGridId && |
| 65 | <BlockControls> |
| 66 | <ToolbarGroup> |
| 67 | <ToolbarButton |
| 68 | className="gblocks-block-control-icon gblocks-add-grid-item" |
| 69 | icon={ getIcon( 'addContainer' ) } |
| 70 | label={ __( 'Duplicate Grid Item', 'generateblocks' ) } |
| 71 | onClick={ () => { |
| 72 | const thisBlock = getBlocksByClientId( clientId )[ 0 ]; |
| 73 | |
| 74 | const clonedBlock = cloneBlock( |
| 75 | thisBlock, |
| 76 | { |
| 77 | uniqueId: '', |
| 78 | } |
| 79 | ); |
| 80 | |
| 81 | insertBlocks( clonedBlock, undefined, parentGridId ); |
| 82 | } } |
| 83 | showTooltip |
| 84 | /> |
| 85 | </ToolbarGroup> |
| 86 | </BlockControls> |
| 87 | } |
| 88 | |
| 89 | { hasAlignmentSupport && |
| 90 | <BlockControls> |
| 91 | <BlockAlignmentToolbar |
| 92 | value={ align } |
| 93 | onChange={ ( value ) => { |
| 94 | setAttributes( { |
| 95 | align: value, |
| 96 | } ); |
| 97 | |
| 98 | if ( 'full' === value ) { |
| 99 | setAttributes( { |
| 100 | outerContainer: 'full', |
| 101 | } ); |
| 102 | } |
| 103 | } } |
| 104 | controls={ WIDE_ALIGNMENTS } |
| 105 | /> |
| 106 | </BlockControls> |
| 107 | } |
| 108 | |
| 109 | <BlockEdit { ...props } /> |
| 110 | </Fragment> |
| 111 | ); |
| 112 | }, |
| 113 | 'withBlockControls' |
| 114 | ); |
| 115 | |
| 116 | addFilter( |
| 117 | 'editor.BlockEdit', |
| 118 | 'generateblocks/container-block-controls', |
| 119 | withBlockControls |
| 120 | ); |
| 121 |