index.js
265 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 | id, |
| 60 | } = this.props; |
| 61 | |
| 62 | let iconSVGSets = { |
| 63 | general: { |
| 64 | group: __( 'General', 'generateblocks' ), |
| 65 | svgs: generalSvgs, |
| 66 | }, |
| 67 | social: { |
| 68 | group: __( 'Social', 'generateblocks' ), |
| 69 | svgs: socialSvgs, |
| 70 | }, |
| 71 | }; |
| 72 | |
| 73 | iconSVGSets = applyFilters( 'generateblocks.editor.iconSVGSets', iconSVGSets, { attributes } ); |
| 74 | |
| 75 | return ( |
| 76 | <Fragment> |
| 77 | <BaseControl className="gb-svg-html"> |
| 78 | <TextControl |
| 79 | label={ __( 'Icon SVG HTML', 'generateblocks' ) } |
| 80 | value={ attributes[ attrIcon ] } |
| 81 | onChange={ ( value ) => { |
| 82 | setAttributes( { |
| 83 | [ this.props[ 'attrIcon' ] ]: sanitizeSVG( value ), // eslint-disable-line dot-notation |
| 84 | } ); |
| 85 | |
| 86 | if ( '' !== value ) { |
| 87 | setAttributes( { |
| 88 | 'hasIcon': true, // eslint-disable-line quote-props |
| 89 | display: 'headline' === id ? 'flex' : 'inline-flex', |
| 90 | alignItems: 'center', |
| 91 | } ); |
| 92 | } else { |
| 93 | setAttributes( { |
| 94 | 'hasIcon': false, // eslint-disable-line quote-props |
| 95 | } ); |
| 96 | } |
| 97 | } } |
| 98 | /> |
| 99 | |
| 100 | <div className="gb-icon-preview"> |
| 101 | <span dangerouslySetInnerHTML={ { __html: sanitizeSVG( attributes[ attrIcon ] ) } } /> |
| 102 | |
| 103 | <Button |
| 104 | isSmall |
| 105 | className="reset-icon is-secondary" |
| 106 | onClick={ () => { |
| 107 | setAttributes( { |
| 108 | [ this.props[ 'attrIcon' ] ]: '', // eslint-disable-line dot-notation |
| 109 | hasIcon: false, |
| 110 | [ this.props[ 'attrRemoveText' ] ]: false, // eslint-disable-line dot-notation |
| 111 | } ); |
| 112 | } } |
| 113 | > |
| 114 | <span className="editor-block-types-list__item-icon"> |
| 115 | { __( 'Clear', 'generateblocks' ) } |
| 116 | </span> |
| 117 | </Button> |
| 118 | </div> |
| 119 | </BaseControl> |
| 120 | |
| 121 | <BaseControl className="gb-icon-chooser"> |
| 122 | { |
| 123 | Object.keys( iconSVGSets ).map( ( svg, i ) => { |
| 124 | const svgItems = iconSVGSets[ svg ].svgs; |
| 125 | |
| 126 | return ( |
| 127 | <PanelBody className="gblocks-panel-label gblocks-icon-panel" title={ iconSVGSets[ svg ].group } initialOpen={ false } key={ i }> |
| 128 | <PanelRow> |
| 129 | <BaseControl> |
| 130 | <ul className="gblocks-icon-chooser"> |
| 131 | { |
| 132 | Object.keys( svgItems ).map( ( svgItem, index ) => { |
| 133 | return ( |
| 134 | <li key={ `editor-pblock-types-list-item-${ index }` }> |
| 135 | <Tooltip text={ ( svgItems[ svgItem ].label ) }> |
| 136 | <Button |
| 137 | className="editor-block-list-item-button" |
| 138 | onClick={ () => { |
| 139 | let iconValue = svgItems[ svgItem ].icon; |
| 140 | |
| 141 | if ( 'string' !== typeof iconValue ) { |
| 142 | iconValue = renderToString( iconValue ); |
| 143 | } |
| 144 | |
| 145 | setAttributes( { |
| 146 | [ this.props.attrIcon ]: iconValue, |
| 147 | hasIcon: true, |
| 148 | display: 'headline' === id ? 'flex' : 'inline-flex', |
| 149 | alignItems: 'center', |
| 150 | } ); |
| 151 | } } |
| 152 | > |
| 153 | { 'string' === typeof svgItems[ svgItem ].icon ? ( |
| 154 | <Fragment> |
| 155 | <span |
| 156 | className="editor-block-types-list__item-icon" |
| 157 | dangerouslySetInnerHTML={ { __html: sanitizeSVG( svgItems[ svgItem ].icon ) } } |
| 158 | /> |
| 159 | </Fragment> |
| 160 | ) : ( |
| 161 | <Fragment> |
| 162 | <span className="editor-block-types-list__item-icon"> |
| 163 | { svgItems[ svgItem ].icon } |
| 164 | </span> |
| 165 | </Fragment> |
| 166 | ) } |
| 167 | </Button> |
| 168 | </Tooltip> |
| 169 | </li> |
| 170 | ); |
| 171 | } ) |
| 172 | } |
| 173 | </ul> |
| 174 | </BaseControl> |
| 175 | </PanelRow> |
| 176 | </PanelBody> |
| 177 | ); |
| 178 | } ) |
| 179 | } |
| 180 | </BaseControl> |
| 181 | |
| 182 | { ( typeof attributes[ attrIconLocation ] !== 'undefined' && ! attributes[ attrRemoveText ] && !! attributes[ attrIcon ] ) && |
| 183 | <SelectControl |
| 184 | label={ __( 'Icon Location', 'generateblocks' ) } |
| 185 | value={ attributes[ attrIconLocation ] } |
| 186 | options={ locationOptions } |
| 187 | onChange={ ( value ) => { |
| 188 | const leftPadding = attributes.iconPaddingLeft, |
| 189 | rightPadding = attributes.iconPaddingRight, |
| 190 | rightPaddingTablet = attributes.iconPaddingRightTablet, |
| 191 | leftPaddingTablet = attributes.iconPaddingLeftTablet, |
| 192 | rightPaddingMobile = attributes.iconPaddingRightMobile, |
| 193 | leftPaddingMobile = attributes.iconPaddingLeftMobile; |
| 194 | |
| 195 | if ( 'right' === value ) { |
| 196 | if ( ! leftPadding && rightPadding ) { |
| 197 | setAttributes( { |
| 198 | iconPaddingLeft: rightPadding, |
| 199 | iconPaddingRight: '', |
| 200 | } ); |
| 201 | } |
| 202 | |
| 203 | if ( ! leftPaddingTablet && rightPaddingTablet ) { |
| 204 | setAttributes( { |
| 205 | iconPaddingLeftTablet: rightPaddingTablet, |
| 206 | iconPaddingRightTablet: '', |
| 207 | } ); |
| 208 | } |
| 209 | |
| 210 | if ( ! leftPaddingMobile && rightPaddingMobile ) { |
| 211 | setAttributes( { |
| 212 | iconPaddingLeftMobile: rightPaddingMobile, |
| 213 | iconPaddingRightMobile: '', |
| 214 | } ); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if ( 'left' === value ) { |
| 219 | if ( ! rightPadding && leftPadding ) { |
| 220 | setAttributes( { |
| 221 | iconPaddingRight: leftPadding, |
| 222 | iconPaddingLeft: '', |
| 223 | } ); |
| 224 | } |
| 225 | |
| 226 | if ( ! rightPaddingTablet && leftPaddingTablet ) { |
| 227 | setAttributes( { |
| 228 | iconPaddingRightTablet: leftPaddingTablet, |
| 229 | iconPaddingLeftTablet: '', |
| 230 | } ); |
| 231 | } |
| 232 | |
| 233 | if ( ! rightPaddingMobile && leftPaddingMobile ) { |
| 234 | setAttributes( { |
| 235 | iconPaddingRightMobile: leftPaddingMobile, |
| 236 | iconPaddingLeftMobile: '', |
| 237 | } ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | setAttributes( { |
| 242 | [ this.props[ 'attrIconLocation' ] ]: value, // eslint-disable-line dot-notation |
| 243 | } ); |
| 244 | } } |
| 245 | /> |
| 246 | } |
| 247 | |
| 248 | { ( typeof attributes[ attrRemoveText ] !== 'undefined' && !! attributes[ attrIcon ] ) && |
| 249 | <ToggleControl |
| 250 | label={ __( 'Remove Text', 'generateblocks' ) } |
| 251 | checked={ !! attributes[ attrRemoveText ] } |
| 252 | onChange={ ( value ) => { |
| 253 | setAttributes( { |
| 254 | [ this.props[ 'attrRemoveText' ] ]: value, // eslint-disable-line dot-notation |
| 255 | } ); |
| 256 | } } |
| 257 | /> |
| 258 | } |
| 259 | </Fragment> |
| 260 | ); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | export default IconPicker; |
| 265 |