BlockControls.js
118 lines
| 1 | import { ToolbarButton, ToolbarGroup } from '@wordpress/components'; |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import { cloneBlock, createBlock } from '@wordpress/blocks'; |
| 4 | import { AlignmentToolbar, BlockControls } from '@wordpress/block-editor'; |
| 5 | import { useDispatch, useSelect } from '@wordpress/data'; |
| 6 | import { plus, alignLeft, alignRight, alignCenter } from '@wordpress/icons'; |
| 7 | |
| 8 | const ALIGNMENT_CONTROLS = [ |
| 9 | { |
| 10 | icon: alignLeft, |
| 11 | title: __( 'Align buttons left', 'generateblocks' ), |
| 12 | align: 'left', |
| 13 | }, |
| 14 | { |
| 15 | icon: alignCenter, |
| 16 | title: __( 'Align buttons center', 'generateblocks' ), |
| 17 | align: 'center', |
| 18 | }, |
| 19 | { |
| 20 | icon: alignRight, |
| 21 | title: __( 'Align buttons right', 'generateblocks' ), |
| 22 | align: 'right', |
| 23 | }, |
| 24 | ]; |
| 25 | |
| 26 | export default ( props ) => { |
| 27 | const { |
| 28 | attributes, |
| 29 | setAttributes, |
| 30 | clientId, |
| 31 | deviceType, |
| 32 | } = props; |
| 33 | |
| 34 | const { |
| 35 | alignment, |
| 36 | alignmentTablet, |
| 37 | alignmentMobile, |
| 38 | isPagination, |
| 39 | } = attributes; |
| 40 | |
| 41 | const { insertBlocks } = useDispatch( 'core/block-editor' ); |
| 42 | const { getBlocksByClientId } = useSelect( ( select ) => select( 'core/block-editor' ), [] ); |
| 43 | |
| 44 | return ( |
| 45 | <BlockControls> |
| 46 | |
| 47 | { ! isPagination && |
| 48 | <ToolbarGroup> |
| 49 | <ToolbarButton |
| 50 | className="gblocks-add-new-button" |
| 51 | icon={ plus } |
| 52 | label={ __( 'Add Button', 'generateblocks' ) } |
| 53 | onClick={ () => { |
| 54 | const thisBlock = getBlocksByClientId( clientId )[ 0 ]; |
| 55 | |
| 56 | if ( thisBlock ) { |
| 57 | const childBlocks = thisBlock.innerBlocks; |
| 58 | const keys = Object.keys( childBlocks ); |
| 59 | const lastKey = keys[ keys.length - 1 ]; |
| 60 | |
| 61 | if ( typeof childBlocks[ lastKey ] !== 'undefined' ) { |
| 62 | const blockToCopyId = childBlocks[ lastKey ].clientId; |
| 63 | |
| 64 | if ( blockToCopyId ) { |
| 65 | const blockToCopy = getBlocksByClientId( blockToCopyId )[ 0 ]; |
| 66 | |
| 67 | const clonedBlock = cloneBlock( |
| 68 | blockToCopy, |
| 69 | { |
| 70 | uniqueId: '', |
| 71 | } |
| 72 | ); |
| 73 | |
| 74 | insertBlocks( clonedBlock, undefined, clientId ); |
| 75 | } |
| 76 | } else if ( 0 === childBlocks.length ) { |
| 77 | insertBlocks( createBlock( 'generateblocks/button', generateBlocksStyling.button ), undefined, clientId ); |
| 78 | } |
| 79 | } |
| 80 | } } |
| 81 | showTooltip |
| 82 | /> |
| 83 | </ToolbarGroup> |
| 84 | } |
| 85 | |
| 86 | { 'Desktop' === deviceType && ( |
| 87 | <AlignmentToolbar |
| 88 | value={ alignment } |
| 89 | alignmentControls={ ALIGNMENT_CONTROLS } |
| 90 | onChange={ ( nextAlign ) => { |
| 91 | setAttributes( { alignment: nextAlign } ); |
| 92 | } } |
| 93 | /> |
| 94 | ) } |
| 95 | |
| 96 | { 'Tablet' === deviceType && ( |
| 97 | <AlignmentToolbar |
| 98 | value={ alignmentTablet } |
| 99 | alignmentControls={ ALIGNMENT_CONTROLS } |
| 100 | onChange={ ( value ) => { |
| 101 | setAttributes( { alignmentTablet: value } ); |
| 102 | } } |
| 103 | /> |
| 104 | ) } |
| 105 | |
| 106 | { 'Mobile' === deviceType && ( |
| 107 | <AlignmentToolbar |
| 108 | value={ alignmentMobile } |
| 109 | alignmentControls={ ALIGNMENT_CONTROLS } |
| 110 | onChange={ ( value ) => { |
| 111 | setAttributes( { alignmentMobile: value } ); |
| 112 | } } |
| 113 | /> |
| 114 | ) } |
| 115 | </BlockControls> |
| 116 | ); |
| 117 | }; |
| 118 |