index.js
260 lines
| 1 | /** |
| 2 | * Internal dependencies |
| 3 | */ |
| 4 | import './editor.scss'; |
| 5 | import socialSvgs from './svgs-social'; |
| 6 | import generalSvgs from './svgs-general'; |
| 7 | import sanitizeSVG from '../../utils/sanitize-svg'; |
| 8 | |
| 9 | /** |
| 10 | * WordPress dependencies |
| 11 | */ |
| 12 | import { |
| 13 | __, |
| 14 | } from '@wordpress/i18n'; |
| 15 | |
| 16 | import { |
| 17 | Component, |
| 18 | Fragment, |
| 19 | renderToString, |
| 20 | } from '@wordpress/element'; |
| 21 | |
| 22 | import { |
| 23 | BaseControl, |
| 24 | SelectControl, |
| 25 | ToggleControl, |
| 26 | TextControl, |
| 27 | Tooltip, |
| 28 | Button, |
| 29 | PanelBody, |
| 30 | PanelRow, |
| 31 | } from '@wordpress/components'; |
| 32 | |
| 33 | import { |
| 34 | applyFilters, |
| 35 | } from '@wordpress/hooks'; |
| 36 | |
| 37 | /** |
| 38 | * Typography Component |
| 39 | */ |
| 40 | class IconPicker extends Component { |
| 41 | constructor() { |
| 42 | super( ...arguments ); |
| 43 | |
| 44 | this.state = { |
| 45 | showIcons: false, |
| 46 | showGeneralIcons: false, |
| 47 | showSocialIcons: false, |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | render() { |
| 52 | const { |
| 53 | attributes, |
| 54 | setAttributes, |
| 55 | attrIcon, |
| 56 | attrIconLocation, |
| 57 | locationOptions, |
| 58 | attrRemoveText, |
| 59 | } = this.props; |
| 60 | |
| 61 | let iconSVGSets = { |
| 62 | general: { |
| 63 | group: __( 'General', 'generateblocks' ), |
| 64 | svgs: generalSvgs, |
| 65 | }, |
| 66 | social: { |
| 67 | group: __( 'Social', 'generateblocks' ), |
| 68 | svgs: socialSvgs, |
| 69 | }, |
| 70 | }; |
| 71 | |
| 72 | iconSVGSets = applyFilters( 'generateblocks.editor.iconSVGSets', iconSVGSets ); |
| 73 | |
| 74 | return ( |
| 75 | <Fragment> |
| 76 | <BaseControl className="gb-svg-html"> |
| 77 | <TextControl |
| 78 | label={ __( 'Icon SVG HTML', 'generateblocks' ) } |
| 79 | value={ attributes[ attrIcon ] } |
| 80 | onChange={ ( value ) => { |
| 81 | setAttributes( { |
| 82 | [ this.props[ 'attrIcon' ] ]: sanitizeSVG( value ), // eslint-disable-line dot-notation |
| 83 | } ); |
| 84 | |
| 85 | if ( '' !== value ) { |
| 86 | setAttributes( { |
| 87 | 'hasIcon': true, // eslint-disable-line quote-props |
| 88 | } ); |
| 89 | } else { |
| 90 | setAttributes( { |
| 91 | 'hasIcon': false, // eslint-disable-line quote-props |
| 92 | } ); |
| 93 | } |
| 94 | } } |
| 95 | /> |
| 96 | |
| 97 | <div className="gb-icon-preview"> |
| 98 | <span dangerouslySetInnerHTML={ { __html: sanitizeSVG( attributes[ attrIcon ] ) } } /> |
| 99 | |
| 100 | <Button |
| 101 | isSmall |
| 102 | className="reset-icon is-secondary" |
| 103 | onClick={ () => { |
| 104 | setAttributes( { |
| 105 | [ this.props[ 'attrIcon' ] ]: '', // eslint-disable-line dot-notation |
| 106 | hasIcon: false, |
| 107 | [ this.props[ 'attrRemoveText' ] ]: false, // eslint-disable-line dot-notation |
| 108 | } ); |
| 109 | } } |
| 110 | > |
| 111 | <span className="editor-block-types-list__item-icon"> |
| 112 | { __( 'Clear', 'generateblocks' ) } |
| 113 | </span> |
| 114 | </Button> |
| 115 | </div> |
| 116 | </BaseControl> |
| 117 | |
| 118 | <BaseControl className="gb-icon-chooser"> |
| 119 | { |
| 120 | Object.keys( iconSVGSets ).map( ( svg, i ) => { |
| 121 | const svgItems = iconSVGSets[ svg ].svgs; |
| 122 | |
| 123 | return ( |
| 124 | <PanelBody title={ iconSVGSets[ svg ].group } initialOpen={ false } key={ i }> |
| 125 | <PanelRow> |
| 126 | <BaseControl> |
| 127 | <ul className="gblocks-icon-chooser"> |
| 128 | { |
| 129 | Object.keys( svgItems ).map( ( svgItem, index ) => { |
| 130 | return ( |
| 131 | <li key={ `editor-pblock-types-list-item-${ index }` }> |
| 132 | <Tooltip text={ ( svgItems[ svgItem ].label ) }> |
| 133 | <Button |
| 134 | className="editor-block-list-item-button" |
| 135 | onClick={ () => { |
| 136 | let iconValue = svgItems[ svgItem ].icon; |
| 137 | |
| 138 | if ( 'string' !== typeof iconValue ) { |
| 139 | iconValue = renderToString( iconValue ); |
| 140 | } |
| 141 | |
| 142 | setAttributes( { |
| 143 | [ this.props.attrIcon ]: iconValue, |
| 144 | hasIcon: true, |
| 145 | } ); |
| 146 | } } |
| 147 | > |
| 148 | { 'string' === typeof svgItems[ svgItem ].icon ? ( |
| 149 | <Fragment> |
| 150 | <span |
| 151 | className="editor-block-types-list__item-icon" |
| 152 | dangerouslySetInnerHTML={ { __html: sanitizeSVG( svgItems[ svgItem ].icon ) } } |
| 153 | /> |
| 154 | </Fragment> |
| 155 | ) : ( |
| 156 | <Fragment> |
| 157 | <span className="editor-block-types-list__item-icon"> |
| 158 | { svgItems[ svgItem ].icon } |
| 159 | </span> |
| 160 | </Fragment> |
| 161 | ) } |
| 162 | </Button> |
| 163 | </Tooltip> |
| 164 | </li> |
| 165 | ); |
| 166 | } ) |
| 167 | } |
| 168 | </ul> |
| 169 | </BaseControl> |
| 170 | </PanelRow> |
| 171 | </PanelBody> |
| 172 | ); |
| 173 | } ) |
| 174 | } |
| 175 | </BaseControl> |
| 176 | |
| 177 | { ( typeof attributes[ attrIconLocation ] !== 'undefined' && ! attributes[ attrRemoveText ] && !! attributes[ attrIcon ] ) && |
| 178 | <SelectControl |
| 179 | label={ __( 'Icon Location', 'generateblocks' ) } |
| 180 | value={ attributes[ attrIconLocation ] } |
| 181 | options={ locationOptions } |
| 182 | onChange={ ( value ) => { |
| 183 | const leftPadding = attributes.iconPaddingLeft, |
| 184 | rightPadding = attributes.iconPaddingRight, |
| 185 | rightPaddingTablet = attributes.iconPaddingRightTablet, |
| 186 | leftPaddingTablet = attributes.iconPaddingLeftTablet, |
| 187 | rightPaddingMobile = attributes.iconPaddingRightMobile, |
| 188 | leftPaddingMobile = attributes.iconPaddingLeftMobile; |
| 189 | |
| 190 | if ( 'right' === value ) { |
| 191 | if ( ! leftPadding && rightPadding ) { |
| 192 | setAttributes( { |
| 193 | iconPaddingLeft: rightPadding, |
| 194 | iconPaddingRight: '', |
| 195 | } ); |
| 196 | } |
| 197 | |
| 198 | if ( ! leftPaddingTablet && rightPaddingTablet ) { |
| 199 | setAttributes( { |
| 200 | iconPaddingLeftTablet: rightPaddingTablet, |
| 201 | iconPaddingRightTablet: '', |
| 202 | } ); |
| 203 | } |
| 204 | |
| 205 | if ( ! leftPaddingMobile && rightPaddingMobile ) { |
| 206 | setAttributes( { |
| 207 | iconPaddingLeftMobile: rightPaddingMobile, |
| 208 | iconPaddingRightMobile: '', |
| 209 | } ); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if ( 'left' === value ) { |
| 214 | if ( ! rightPadding && leftPadding ) { |
| 215 | setAttributes( { |
| 216 | iconPaddingRight: leftPadding, |
| 217 | iconPaddingLeft: '', |
| 218 | } ); |
| 219 | } |
| 220 | |
| 221 | if ( ! rightPaddingTablet && leftPaddingTablet ) { |
| 222 | setAttributes( { |
| 223 | iconPaddingRightTablet: leftPaddingTablet, |
| 224 | iconPaddingLeftTablet: '', |
| 225 | } ); |
| 226 | } |
| 227 | |
| 228 | if ( ! rightPaddingMobile && leftPaddingMobile ) { |
| 229 | setAttributes( { |
| 230 | iconPaddingRightMobile: leftPaddingMobile, |
| 231 | iconPaddingLeftMobile: '', |
| 232 | } ); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | setAttributes( { |
| 237 | [ this.props[ 'attrIconLocation' ] ]: value, // eslint-disable-line dot-notation |
| 238 | } ); |
| 239 | } } |
| 240 | /> |
| 241 | } |
| 242 | |
| 243 | { ( typeof attributes[ attrRemoveText ] !== 'undefined' && !! attributes[ attrIcon ] ) && |
| 244 | <ToggleControl |
| 245 | label={ __( 'Remove Text', 'generateblocks' ) } |
| 246 | checked={ !! attributes[ attrRemoveText ] } |
| 247 | onChange={ ( value ) => { |
| 248 | setAttributes( { |
| 249 | [ this.props[ 'attrRemoveText' ] ]: value, // eslint-disable-line dot-notation |
| 250 | } ); |
| 251 | } } |
| 252 | /> |
| 253 | } |
| 254 | </Fragment> |
| 255 | ); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | export default IconPicker; |
| 260 |