index.js
126 lines
| 1 | import { Dropdown, ToggleControl, ToolbarButton } from '@wordpress/components'; |
| 2 | import { link } from '@wordpress/icons'; |
| 3 | import { __ } from '@wordpress/i18n'; |
| 4 | import { URLInput } from '@wordpress/block-editor'; |
| 5 | import { applyFilters } from '@wordpress/hooks'; |
| 6 | |
| 7 | const POPOVER_PROPS = { |
| 8 | className: 'block-editor-block-settings-menu__popover', |
| 9 | position: 'bottom right', |
| 10 | }; |
| 11 | |
| 12 | function DisabledInputMessage( marginBottom = false ) { |
| 13 | const styles = { |
| 14 | width: '300px', |
| 15 | 'font-style': 'italic', |
| 16 | 'margin-bottom': ( marginBottom ? '15px' : '0' ), |
| 17 | }; |
| 18 | |
| 19 | return ( |
| 20 | <div style={ styles }> |
| 21 | { __( 'This button is using a dynamic link.', 'generateblocks' ) } |
| 22 | </div> |
| 23 | ); |
| 24 | } |
| 25 | |
| 26 | function LinkDropdownContent( props ) { |
| 27 | const { |
| 28 | attributes, |
| 29 | setAttributes, |
| 30 | name, |
| 31 | } = props; |
| 32 | |
| 33 | const { |
| 34 | href, |
| 35 | relNoFollow, |
| 36 | relSponsored, |
| 37 | dynamicLinkType, |
| 38 | useDynamicData, |
| 39 | url, |
| 40 | } = attributes; |
| 41 | |
| 42 | const hasDynamicLink = useDynamicData && dynamicLinkType; |
| 43 | const targetAttribute = 'generateblocks/button' === name |
| 44 | ? 'target' |
| 45 | : 'openInNewWindow'; |
| 46 | const targetValue = attributes[ targetAttribute ]; |
| 47 | const urlValue = 'generateblocks/button' === name |
| 48 | ? url |
| 49 | : href; |
| 50 | |
| 51 | return ( |
| 52 | <> |
| 53 | { ! useDynamicData |
| 54 | ? ( |
| 55 | <URLInput |
| 56 | className={ 'gblocks-link-url' } |
| 57 | value={ urlValue } |
| 58 | onChange={ ( value ) => ( setAttributes( { href: value } ) ) } |
| 59 | /> |
| 60 | ) |
| 61 | : <DisabledInputMessage marginBottom={ !! dynamicLinkType } /> |
| 62 | } |
| 63 | |
| 64 | { applyFilters( 'generateblocks.editor.urlInputMoreOptions', '', attributes ) } |
| 65 | |
| 66 | { ( !! urlValue || hasDynamicLink ) && |
| 67 | <> |
| 68 | <ToggleControl |
| 69 | label={ __( 'Open link in a new tab', 'generateblocks' ) } |
| 70 | checked={ targetValue || '' } |
| 71 | onChange={ ( value ) => ( |
| 72 | setAttributes( { [ targetAttribute ]: value } ) |
| 73 | ) } |
| 74 | /> |
| 75 | |
| 76 | <ToggleControl |
| 77 | label={ __( 'Add rel="nofollow"', 'generateblocks' ) } |
| 78 | checked={ relNoFollow || '' } |
| 79 | onChange={ ( value ) => ( |
| 80 | setAttributes( { relNoFollow: value } ) |
| 81 | ) } |
| 82 | /> |
| 83 | |
| 84 | <ToggleControl |
| 85 | label={ __( 'Add rel="sponsored"', 'generateblocks' ) } |
| 86 | checked={ relSponsored || '' } |
| 87 | onChange={ ( value ) => ( |
| 88 | setAttributes( { relSponsored: value } ) |
| 89 | ) } |
| 90 | /> |
| 91 | </> |
| 92 | } |
| 93 | </> |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | export default function Index( props ) { |
| 98 | const { attributes, name } = props; |
| 99 | const { url, href } = attributes; |
| 100 | |
| 101 | const urlValue = 'generateblocks/button' === name |
| 102 | ? url |
| 103 | : href; |
| 104 | |
| 105 | const buttonLabel = ! urlValue |
| 106 | ? __( 'Add Link', 'generateblocks' ) |
| 107 | : __( 'Change Link', 'generateblocks' ); |
| 108 | |
| 109 | return ( |
| 110 | <Dropdown |
| 111 | popoverProps={ POPOVER_PROPS } |
| 112 | contentClassName="gblocks-link-control-dropdown" |
| 113 | renderToggle={ ( { isOpen, onToggle } ) => ( |
| 114 | <ToolbarButton |
| 115 | icon={ link } |
| 116 | label={ buttonLabel } |
| 117 | onClick={ onToggle } |
| 118 | aria-expanded={ isOpen } |
| 119 | isPressed={ !! urlValue } |
| 120 | /> |
| 121 | ) } |
| 122 | renderContent={ () => ( <LinkDropdownContent { ...props } /> ) } |
| 123 | /> |
| 124 | ); |
| 125 | } |
| 126 |