BlockControls.js
2 years ago
ButtonContentRenderer.js
2 years ago
ComponentCSS.js
1 year ago
ConditionalColors.js
2 years ago
InspectorAdvancedControls.js
3 years ago
BlockControls.js
176 lines
| 1 | import { ToolbarButton, ToolbarGroup, Dropdown, ToggleControl } from '@wordpress/components'; |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import { cloneBlock } from '@wordpress/blocks'; |
| 4 | import { BlockControls, URLInput, AlignmentToolbar } from '@wordpress/block-editor'; |
| 5 | import { useDispatch, useSelect } from '@wordpress/data'; |
| 6 | import { link } from '@wordpress/icons'; |
| 7 | import { applyFilters } from '@wordpress/hooks'; |
| 8 | import isFlexItem from '../../../utils/is-flex-item'; |
| 9 | import getAttribute from '../../../utils/get-attribute'; |
| 10 | import typographyOptions from '../../../extend/inspector-control/controls/typography/options'; |
| 11 | import getDeviceType from '../../../utils/get-device-type'; |
| 12 | import getIcon from '../../../utils/get-icon'; |
| 13 | |
| 14 | export default ( props ) => { |
| 15 | const { |
| 16 | attributes, |
| 17 | clientId, |
| 18 | setAttributes, |
| 19 | setButtonPreviewElement, |
| 20 | } = props; |
| 21 | |
| 22 | const { insertBlocks } = useDispatch( 'core/block-editor' ); |
| 23 | const { |
| 24 | getBlockParentsByBlockName, |
| 25 | getBlocksByClientId, |
| 26 | } = useSelect( ( select ) => select( 'core/block-editor' ), [] ); |
| 27 | |
| 28 | const { |
| 29 | url, |
| 30 | target, |
| 31 | relNoFollow, |
| 32 | relSponsored, |
| 33 | useDynamicData, |
| 34 | dynamicLinkType, |
| 35 | display, |
| 36 | displayTablet, |
| 37 | displayMobile, |
| 38 | buttonType, |
| 39 | } = attributes; |
| 40 | |
| 41 | const POPOVER_PROPS = { |
| 42 | className: 'block-editor-block-settings-menu__popover', |
| 43 | position: 'bottom right', |
| 44 | }; |
| 45 | |
| 46 | const deviceType = getDeviceType(); |
| 47 | const hasDynamicLink = useDynamicData && dynamicLinkType; |
| 48 | const showAppender = applyFilters( 'generateblocks.editor.showButtonAppender', true, props ); |
| 49 | const showButtonLinkControl = applyFilters( 'generateblocks.editor.showButtonLinkControl', 'link' === buttonType, props ); |
| 50 | const buttonContainerId = getBlockParentsByBlockName( clientId, 'generateblocks/button-container', true )[ 0 ]; |
| 51 | |
| 52 | return ( |
| 53 | <> |
| 54 | <BlockControls> |
| 55 | <ToolbarGroup> |
| 56 | { showAppender && buttonContainerId && // Add appender when using our deprecated button-container block. |
| 57 | <ToolbarButton |
| 58 | className="gblocks-add-new-button" |
| 59 | icon={ getIcon( 'add-button' ) } |
| 60 | label={ __( 'Add Button', 'generateblocks' ) } |
| 61 | onClick={ () => { |
| 62 | const thisBlock = getBlocksByClientId( clientId )[ 0 ]; |
| 63 | |
| 64 | const clonedBlock = cloneBlock( |
| 65 | thisBlock, |
| 66 | { |
| 67 | uniqueId: '', |
| 68 | } |
| 69 | ); |
| 70 | |
| 71 | insertBlocks( clonedBlock, undefined, buttonContainerId ); |
| 72 | } } |
| 73 | showTooltip |
| 74 | /> |
| 75 | } |
| 76 | </ToolbarGroup> |
| 77 | |
| 78 | { ! isFlexItem( { device: deviceType, display, displayTablet, displayMobile } ) && |
| 79 | <AlignmentToolbar |
| 80 | value={ getAttribute( 'textAlign', { attributes: attributes.typography, deviceType } ) } |
| 81 | onChange={ ( value ) => { |
| 82 | setAttributes( { |
| 83 | typography: { |
| 84 | [ getAttribute( 'textAlign', { attributes: attributes.typography, deviceType }, true ) ]: value, |
| 85 | }, |
| 86 | } ); |
| 87 | } } |
| 88 | alignmentControls={ typographyOptions.alignments } |
| 89 | /> |
| 90 | } |
| 91 | |
| 92 | <ToolbarGroup> |
| 93 | { ( ! useDynamicData || hasDynamicLink ) && showButtonLinkControl && |
| 94 | <Dropdown |
| 95 | contentClassName="gblocks-button-link-dropdown" |
| 96 | popoverProps={ POPOVER_PROPS } |
| 97 | onClose={ () => setButtonPreviewElement( url ? 'a' : 'span' ) } |
| 98 | renderToggle={ ( { isOpen, onToggle } ) => ( |
| 99 | <ToolbarButton |
| 100 | icon={ link } |
| 101 | label={ ! url ? __( 'Add Link', 'generateblocks' ) : __( 'Change Link', 'generateblocks' ) } |
| 102 | onClick={ onToggle } |
| 103 | aria-expanded={ isOpen } |
| 104 | isPressed={ !! url } |
| 105 | /> |
| 106 | ) } |
| 107 | renderContent={ () => ( |
| 108 | <> |
| 109 | { ! useDynamicData && |
| 110 | <URLInput |
| 111 | className={ 'gblocks-button-link' } |
| 112 | value={ url } |
| 113 | onChange={ ( value ) => { |
| 114 | setAttributes( { |
| 115 | url: value, |
| 116 | hasUrl: !! value, |
| 117 | } ); |
| 118 | } } |
| 119 | /> |
| 120 | } |
| 121 | |
| 122 | { !! useDynamicData && |
| 123 | <div style={ { |
| 124 | width: '300px', |
| 125 | 'font-style': 'italic', |
| 126 | 'margin-bottom': ( !! dynamicLinkType ? '15px' : '0' ), |
| 127 | } }> |
| 128 | { __( 'This button is using a dynamic link.', 'generateblocks' ) } |
| 129 | </div> |
| 130 | } |
| 131 | |
| 132 | { applyFilters( 'generateblocks.editor.urlInputMoreOptions', '', attributes ) } |
| 133 | |
| 134 | { ( !! url || hasDynamicLink ) && |
| 135 | <> |
| 136 | <ToggleControl |
| 137 | label={ __( 'Open link in a new tab', 'generateblocks' ) } |
| 138 | checked={ target || '' } |
| 139 | onChange={ ( value ) => { |
| 140 | setAttributes( { |
| 141 | target: value, |
| 142 | } ); |
| 143 | } } |
| 144 | /> |
| 145 | |
| 146 | <ToggleControl |
| 147 | label={ __( 'Add rel="nofollow"', 'generateblocks' ) } |
| 148 | checked={ relNoFollow || '' } |
| 149 | onChange={ ( value ) => { |
| 150 | setAttributes( { |
| 151 | relNoFollow: value, |
| 152 | } ); |
| 153 | } } |
| 154 | /> |
| 155 | |
| 156 | <ToggleControl |
| 157 | label={ __( 'Add rel="sponsored"', 'generateblocks' ) } |
| 158 | checked={ relSponsored || '' } |
| 159 | onChange={ ( value ) => { |
| 160 | setAttributes( { |
| 161 | relSponsored: value, |
| 162 | } ); |
| 163 | } } |
| 164 | /> |
| 165 | </> |
| 166 | } |
| 167 | </> |
| 168 | ) } |
| 169 | /> |
| 170 | } |
| 171 | </ToolbarGroup> |
| 172 | </BlockControls> |
| 173 | </> |
| 174 | ); |
| 175 | }; |
| 176 |