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