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