css
6 years ago
attributes.js
6 years ago
block-controls.js
6 years ago
block.js
6 years ago
edit.js
6 years ago
editor.scss
6 years ago
save.js
6 years ago
section-tag.js
6 years ago
style.scss
6 years ago
block-controls.js
135 lines
| 1 | import getIcon from '../../utils/get-icon'; |
| 2 | |
| 3 | /** |
| 4 | * WordPress Dependencies |
| 5 | */ |
| 6 | const { |
| 7 | __, |
| 8 | } = wp.i18n; |
| 9 | |
| 10 | const { |
| 11 | addFilter, |
| 12 | } = wp.hooks; |
| 13 | |
| 14 | const { |
| 15 | Fragment, |
| 16 | } = wp.element; |
| 17 | |
| 18 | const { |
| 19 | BlockControls, |
| 20 | BlockAlignmentToolbar, |
| 21 | } = wp.blockEditor; |
| 22 | |
| 23 | const { |
| 24 | Toolbar, |
| 25 | Tooltip, |
| 26 | Button, |
| 27 | } = wp.components; |
| 28 | |
| 29 | const { |
| 30 | createHigherOrderComponent, |
| 31 | } = wp.compose; |
| 32 | |
| 33 | const { |
| 34 | cloneBlock, |
| 35 | } = wp.blocks; |
| 36 | |
| 37 | const hasWideAlignSupport = generateBlocksInfo.hasWideAlignSupport; |
| 38 | const WIDE_ALIGNMENTS = [ 'wide', 'full' ]; |
| 39 | |
| 40 | /** |
| 41 | * Add controls to the Container block toolbar. |
| 42 | * |
| 43 | * @param {function} BlockEdit Block edit component. |
| 44 | * |
| 45 | * @return {function} BlockEdit Modified block edit component. |
| 46 | */ |
| 47 | const withAdvancedControls = createHigherOrderComponent( ( BlockEdit ) => { |
| 48 | return ( props ) => { |
| 49 | const { |
| 50 | name, |
| 51 | attributes, |
| 52 | isSelected, |
| 53 | clientId, |
| 54 | setAttributes, |
| 55 | } = props; |
| 56 | |
| 57 | const { |
| 58 | isGrid, |
| 59 | align, |
| 60 | } = attributes; |
| 61 | |
| 62 | let parentGridId = false; |
| 63 | |
| 64 | if ( typeof wp.data.select( 'core/block-editor' ).getBlockParentsByBlockName === 'function' ) { |
| 65 | parentGridId = wp.data.select( 'core/block-editor' ).getBlockParentsByBlockName( clientId, 'generateblocks/grid', true )[ 0 ]; |
| 66 | } else { |
| 67 | parentGridId = wp.data.select( 'core/block-editor' ).getBlockRootClientId( clientId ); |
| 68 | } |
| 69 | |
| 70 | return ( |
| 71 | <Fragment> |
| 72 | { isSelected && isGrid && parentGridId && 'generateblocks/container' === name && |
| 73 | <BlockControls> |
| 74 | <Toolbar> |
| 75 | <Tooltip text={ __( 'Duplicate Grid Item', 'generateblocks' ) }> |
| 76 | <Button |
| 77 | className="gblocks-block-control-icon gblocks-add-grid-item" |
| 78 | icon={ getIcon( 'addContainer' ) } |
| 79 | onClick={ () => { |
| 80 | const thisBlock = wp.data.select( 'core/block-editor' ).getBlocksByClientId( clientId )[ 0 ]; |
| 81 | const clonedBlock = cloneBlock( thisBlock ); |
| 82 | |
| 83 | wp.data.dispatch( 'core/block-editor' ).insertBlocks( clonedBlock, undefined, parentGridId ); |
| 84 | } } |
| 85 | /> |
| 86 | </Tooltip> |
| 87 | </Toolbar> |
| 88 | |
| 89 | <Toolbar> |
| 90 | <Tooltip text={ __( 'Select Parent Grid', 'generateblocks' ) }> |
| 91 | <Button |
| 92 | className="gblocks-block-control-icon" |
| 93 | icon={ getIcon( 'grid' ) } |
| 94 | onClick={ () => { |
| 95 | wp.data.dispatch( 'core/block-editor' ).selectBlock( parentGridId ); |
| 96 | } } |
| 97 | /> |
| 98 | </Tooltip> |
| 99 | </Toolbar> |
| 100 | </BlockControls> |
| 101 | } |
| 102 | |
| 103 | { isSelected && hasWideAlignSupport && ! isGrid && 'generateblocks/container' === name && |
| 104 | <BlockControls> |
| 105 | <BlockAlignmentToolbar |
| 106 | value={ align } |
| 107 | onChange={ ( value ) => { |
| 108 | setAttributes( { |
| 109 | align: value, |
| 110 | } ); |
| 111 | |
| 112 | if ( 'full' === value ) { |
| 113 | setAttributes( { |
| 114 | outerContainer: 'full', |
| 115 | } ); |
| 116 | } |
| 117 | } } |
| 118 | controls={ WIDE_ALIGNMENTS } |
| 119 | /> |
| 120 | </BlockControls> |
| 121 | } |
| 122 | |
| 123 | <BlockEdit { ...props } /> |
| 124 | </Fragment> |
| 125 | ); |
| 126 | }; |
| 127 | }, 'withAdvancedControls' ); |
| 128 | |
| 129 | addFilter( |
| 130 | 'editor.BlockEdit', |
| 131 | 'generateblocks/container-block-controls', |
| 132 | withAdvancedControls |
| 133 | ); |
| 134 | |
| 135 |