index.js
136 lines
| 1 | import classnames from 'classnames'; |
| 2 | |
| 3 | // Import CSS |
| 4 | import './editor.scss'; |
| 5 | |
| 6 | import { |
| 7 | __, |
| 8 | } from '@wordpress/i18n'; |
| 9 | |
| 10 | import { |
| 11 | Component, |
| 12 | } from '@wordpress/element'; |
| 13 | |
| 14 | import { |
| 15 | Button, |
| 16 | ToggleControl, |
| 17 | } from '@wordpress/components'; |
| 18 | |
| 19 | import { |
| 20 | URLInput, |
| 21 | } from '@wordpress/block-editor'; |
| 22 | |
| 23 | import { |
| 24 | applyFilters, |
| 25 | } from '@wordpress/hooks'; |
| 26 | |
| 27 | export default class ButtonURLInput extends Component { |
| 28 | constructor() { |
| 29 | super( ...arguments ); |
| 30 | |
| 31 | this.state = { |
| 32 | moreOptions: false, |
| 33 | }; |
| 34 | |
| 35 | this.onChange = this.onChange.bind( this ); |
| 36 | } |
| 37 | |
| 38 | onChange( data ) { |
| 39 | const { |
| 40 | url, |
| 41 | target, |
| 42 | relNoFollow, |
| 43 | relSponsored, |
| 44 | } = this.props; |
| 45 | |
| 46 | this.props.onChange( { |
| 47 | ...{ |
| 48 | url, |
| 49 | target, |
| 50 | relNoFollow, |
| 51 | relSponsored, |
| 52 | }, |
| 53 | ...data, |
| 54 | } ); |
| 55 | } |
| 56 | |
| 57 | render() { |
| 58 | const { |
| 59 | url, |
| 60 | target, |
| 61 | relNoFollow, |
| 62 | relSponsored, |
| 63 | className, |
| 64 | autoFocus, |
| 65 | } = this.props; |
| 66 | |
| 67 | const { |
| 68 | onChange, |
| 69 | } = this; |
| 70 | |
| 71 | const { |
| 72 | moreOptions, |
| 73 | } = this.state; |
| 74 | |
| 75 | return ( |
| 76 | <div className={ classnames( 'gblocks-component-url-input', className ) }> |
| 77 | <div className="gblocks-component-url-input-flex"> |
| 78 | <URLInput |
| 79 | value={ url } |
| 80 | onChange={ ( value ) => { |
| 81 | onChange( { |
| 82 | url: value, |
| 83 | } ); |
| 84 | } } |
| 85 | autoFocus={ autoFocus } // eslint-disable-line jsx-a11y/no-autofocus |
| 86 | /> |
| 87 | <Button |
| 88 | icon={ 'ellipsis' } |
| 89 | label={ moreOptions ? __( 'Hide More Options', 'generateblocks' ) : __( 'Show More Options', 'generateblocks' ) } |
| 90 | onClick={ () => { |
| 91 | this.setState( { |
| 92 | moreOptions: ! moreOptions, |
| 93 | } ); |
| 94 | } } |
| 95 | /> |
| 96 | </div> |
| 97 | { moreOptions && |
| 98 | <div className="gblocks-component-url-input-more-options"> |
| 99 | { applyFilters( 'generateblocks.editor.urlInputMoreOptions', '', this.props, this.state ) } |
| 100 | |
| 101 | <ToggleControl |
| 102 | label={ __( 'Open link in a new tab', 'generateblocks' ) } |
| 103 | checked={ target || '' } |
| 104 | onChange={ ( value ) => { |
| 105 | onChange( { |
| 106 | target: value, |
| 107 | } ); |
| 108 | } } |
| 109 | /> |
| 110 | |
| 111 | <ToggleControl |
| 112 | label={ __( 'Add rel="nofollow"', 'generateblocks' ) } |
| 113 | checked={ relNoFollow || '' } |
| 114 | onChange={ ( value ) => { |
| 115 | onChange( { |
| 116 | relNoFollow: value, |
| 117 | } ); |
| 118 | } } |
| 119 | /> |
| 120 | |
| 121 | <ToggleControl |
| 122 | label={ __( 'Add rel="sponsored"', 'generateblocks' ) } |
| 123 | checked={ relSponsored || '' } |
| 124 | onChange={ ( value ) => { |
| 125 | onChange( { |
| 126 | relSponsored: value, |
| 127 | } ); |
| 128 | } } |
| 129 | /> |
| 130 | </div> |
| 131 | } |
| 132 | </div> |
| 133 | ); |
| 134 | } |
| 135 | } |
| 136 |