BlockControls.js
4 years ago
ButtonContentRenderer.js
4 years ago
ComponentCSS.js
4 years ago
ConditionalColors.js
4 years ago
InspectorAdvancedControls.js
4 years ago
InspectorControls.js
4 years ago
BlockControls.js
148 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 } from '@wordpress/block-editor'; |
| 5 | import { useDispatch, useSelect } from '@wordpress/data'; |
| 6 | import { link, plus } from '@wordpress/icons'; |
| 7 | import { applyFilters } from '@wordpress/hooks'; |
| 8 | |
| 9 | export default ( { clientId, attributes, setAttributes } ) => { |
| 10 | const { insertBlocks } = useDispatch( 'core/block-editor' ); |
| 11 | const { |
| 12 | getBlockParentsByBlockName, |
| 13 | getBlockRootClientId, |
| 14 | getBlocksByClientId, |
| 15 | } = useSelect( ( select ) => select( 'core/block-editor' ), [] ); |
| 16 | |
| 17 | const { |
| 18 | url, |
| 19 | target, |
| 20 | relNoFollow, |
| 21 | relSponsored, |
| 22 | useDynamicData, |
| 23 | dynamicLinkType, |
| 24 | } = attributes; |
| 25 | |
| 26 | const POPOVER_PROPS = { |
| 27 | className: 'block-editor-block-settings-menu__popover', |
| 28 | position: 'bottom right', |
| 29 | }; |
| 30 | |
| 31 | const hasDynamicLink = useDynamicData && dynamicLinkType; |
| 32 | |
| 33 | return ( |
| 34 | <> |
| 35 | <BlockControls> |
| 36 | <ToolbarGroup> |
| 37 | <ToolbarButton |
| 38 | className="gblocks-add-new-button" |
| 39 | icon={ plus } |
| 40 | label={ __( 'Add Button', 'generateblocks' ) } |
| 41 | onClick={ () => { |
| 42 | let parentBlockId = false; |
| 43 | |
| 44 | if ( typeof getBlockParentsByBlockName === 'function' ) { |
| 45 | parentBlockId = getBlockParentsByBlockName( clientId, 'generateblocks/button-container', true )[ 0 ]; |
| 46 | } else { |
| 47 | parentBlockId = getBlockRootClientId( clientId ); |
| 48 | } |
| 49 | |
| 50 | const thisBlock = getBlocksByClientId( clientId )[ 0 ]; |
| 51 | |
| 52 | const clonedBlock = cloneBlock( |
| 53 | thisBlock, |
| 54 | { |
| 55 | uniqueId: '', |
| 56 | } |
| 57 | ); |
| 58 | |
| 59 | insertBlocks( clonedBlock, undefined, parentBlockId ); |
| 60 | } } |
| 61 | showTooltip |
| 62 | /> |
| 63 | </ToolbarGroup> |
| 64 | |
| 65 | <ToolbarGroup> |
| 66 | { ( ! useDynamicData || hasDynamicLink ) && |
| 67 | <Dropdown |
| 68 | contentClassName="gblocks-button-link-dropdown" |
| 69 | popoverProps={ POPOVER_PROPS } |
| 70 | renderToggle={ ( { isOpen, onToggle } ) => ( |
| 71 | <ToolbarButton |
| 72 | icon={ link } |
| 73 | label={ ! url ? __( 'Add Link', 'generateblocks' ) : __( 'Change Link', 'generateblocks' ) } |
| 74 | onClick={ onToggle } |
| 75 | aria-expanded={ isOpen } |
| 76 | isPressed={ !! url } |
| 77 | /> |
| 78 | ) } |
| 79 | renderContent={ () => ( |
| 80 | <> |
| 81 | { ! useDynamicData && |
| 82 | <URLInput |
| 83 | className={ 'gblocks-button-link' } |
| 84 | value={ url } |
| 85 | onChange={ ( value ) => { |
| 86 | setAttributes( { |
| 87 | url: value, |
| 88 | hasUrl: !! value, |
| 89 | } ); |
| 90 | } } |
| 91 | /> |
| 92 | } |
| 93 | |
| 94 | { !! useDynamicData && |
| 95 | <div style={ { |
| 96 | width: '300px', |
| 97 | 'font-style': 'italic', |
| 98 | 'margin-bottom': ( !! dynamicLinkType ? '15px' : '0' ), |
| 99 | } }> |
| 100 | { __( 'This button is using a dynamic link.', 'generateblocks' ) } |
| 101 | </div> |
| 102 | } |
| 103 | |
| 104 | { applyFilters( 'generateblocks.editor.urlInputMoreOptions', '', attributes ) } |
| 105 | |
| 106 | { ( !! url || hasDynamicLink ) && |
| 107 | <> |
| 108 | <ToggleControl |
| 109 | label={ __( 'Open link in a new tab', 'generateblocks' ) } |
| 110 | checked={ target || '' } |
| 111 | onChange={ ( value ) => { |
| 112 | setAttributes( { |
| 113 | target: value, |
| 114 | } ); |
| 115 | } } |
| 116 | /> |
| 117 | |
| 118 | <ToggleControl |
| 119 | label={ __( 'Add rel="nofollow"', 'generateblocks' ) } |
| 120 | checked={ relNoFollow || '' } |
| 121 | onChange={ ( value ) => { |
| 122 | setAttributes( { |
| 123 | relNoFollow: value, |
| 124 | } ); |
| 125 | } } |
| 126 | /> |
| 127 | |
| 128 | <ToggleControl |
| 129 | label={ __( 'Add rel="sponsored"', 'generateblocks' ) } |
| 130 | checked={ relSponsored || '' } |
| 131 | onChange={ ( value ) => { |
| 132 | setAttributes( { |
| 133 | relSponsored: value, |
| 134 | } ); |
| 135 | } } |
| 136 | /> |
| 137 | </> |
| 138 | } |
| 139 | </> |
| 140 | ) } |
| 141 | /> |
| 142 | } |
| 143 | </ToolbarGroup> |
| 144 | </BlockControls> |
| 145 | </> |
| 146 | ); |
| 147 | }; |
| 148 |