index.js
240 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 | const flexAttributes = {}; |
| 76 | |
| 77 | if ( ! attributes.display.includes( 'flex' ) ) { |
| 78 | flexAttributes.display = 'headline' === id ? 'flex' : 'inline-flex'; |
| 79 | } |
| 80 | |
| 81 | if ( ! attributes.alignItems ) { |
| 82 | flexAttributes.alignItems = 'center'; |
| 83 | } |
| 84 | |
| 85 | if ( ! attributes.columnGap ) { |
| 86 | flexAttributes.columnGap = '0.5em'; |
| 87 | } |
| 88 | |
| 89 | const styleAttributes = {}; |
| 90 | |
| 91 | if ( ! attributes.iconStyles.height ) { |
| 92 | styleAttributes.height = '1em'; |
| 93 | } |
| 94 | |
| 95 | if ( ! attributes.iconStyles.width ) { |
| 96 | styleAttributes.width = '1em'; |
| 97 | } |
| 98 | |
| 99 | return ( |
| 100 | <Fragment> |
| 101 | <BaseControl className="gb-svg-html"> |
| 102 | <TextControl |
| 103 | label={ __( 'Icon SVG HTML', 'generateblocks' ) } |
| 104 | value={ attributes[ attrIcon ] } |
| 105 | onChange={ ( value ) => { |
| 106 | setAttributes( { |
| 107 | [ this.props[ 'attrIcon' ] ]: sanitizeSVG( value ), // eslint-disable-line dot-notation |
| 108 | } ); |
| 109 | |
| 110 | if ( '' !== value ) { |
| 111 | setAttributes( { |
| 112 | hasIcon: true, |
| 113 | ...flexAttributes, |
| 114 | iconStyles: { |
| 115 | ...styleAttributes, |
| 116 | }, |
| 117 | } ); |
| 118 | } else { |
| 119 | setAttributes( { |
| 120 | hasIcon: false, |
| 121 | } ); |
| 122 | } |
| 123 | } } |
| 124 | /> |
| 125 | |
| 126 | <div className="gb-icon-preview"> |
| 127 | <span dangerouslySetInnerHTML={ { __html: sanitizeSVG( attributes[ attrIcon ] ) } } /> |
| 128 | |
| 129 | <Button |
| 130 | size="small" |
| 131 | className="reset-icon is-secondary" |
| 132 | onClick={ () => { |
| 133 | setAttributes( { |
| 134 | [ this.props[ 'attrIcon' ] ]: '', // eslint-disable-line dot-notation |
| 135 | hasIcon: false, |
| 136 | [ this.props[ 'attrRemoveText' ] ]: false, // eslint-disable-line dot-notation |
| 137 | } ); |
| 138 | } } |
| 139 | > |
| 140 | <span className="editor-block-types-list__item-icon"> |
| 141 | { __( 'Clear', 'generateblocks' ) } |
| 142 | </span> |
| 143 | </Button> |
| 144 | </div> |
| 145 | </BaseControl> |
| 146 | |
| 147 | <BaseControl className="gb-icon-chooser"> |
| 148 | { |
| 149 | Object.keys( iconSVGSets ).map( ( svg, i ) => { |
| 150 | const svgItems = iconSVGSets[ svg ].svgs; |
| 151 | |
| 152 | return ( |
| 153 | <PanelBody className="gblocks-panel-label gblocks-icon-panel" title={ iconSVGSets[ svg ].group } initialOpen={ false } key={ i }> |
| 154 | <PanelRow> |
| 155 | <BaseControl> |
| 156 | <ul className="gblocks-icon-chooser"> |
| 157 | { |
| 158 | Object.keys( svgItems ).map( ( svgItem, index ) => { |
| 159 | return ( |
| 160 | <li key={ `editor-pblock-types-list-item-${ index }` }> |
| 161 | <Tooltip text={ ( svgItems[ svgItem ].label ) }> |
| 162 | <Button |
| 163 | className="editor-block-list-item-button" |
| 164 | onClick={ () => { |
| 165 | let iconValue = svgItems[ svgItem ].icon; |
| 166 | |
| 167 | if ( 'string' !== typeof iconValue ) { |
| 168 | iconValue = renderToString( iconValue ); |
| 169 | } |
| 170 | |
| 171 | setAttributes( { |
| 172 | [ this.props.attrIcon ]: iconValue, |
| 173 | hasIcon: true, |
| 174 | ...flexAttributes, |
| 175 | iconStyles: { |
| 176 | ...styleAttributes, |
| 177 | }, |
| 178 | } ); |
| 179 | } } |
| 180 | > |
| 181 | { 'string' === typeof svgItems[ svgItem ].icon ? ( |
| 182 | <Fragment> |
| 183 | <span |
| 184 | className="editor-block-types-list__item-icon" |
| 185 | dangerouslySetInnerHTML={ { __html: sanitizeSVG( svgItems[ svgItem ].icon ) } } |
| 186 | /> |
| 187 | </Fragment> |
| 188 | ) : ( |
| 189 | <Fragment> |
| 190 | <span className="editor-block-types-list__item-icon"> |
| 191 | { svgItems[ svgItem ].icon } |
| 192 | </span> |
| 193 | </Fragment> |
| 194 | ) } |
| 195 | </Button> |
| 196 | </Tooltip> |
| 197 | </li> |
| 198 | ); |
| 199 | } ) |
| 200 | } |
| 201 | </ul> |
| 202 | </BaseControl> |
| 203 | </PanelRow> |
| 204 | </PanelBody> |
| 205 | ); |
| 206 | } ) |
| 207 | } |
| 208 | </BaseControl> |
| 209 | |
| 210 | { ( typeof attributes[ attrIconLocation ] !== 'undefined' && ! attributes[ attrRemoveText ] && !! attributes[ attrIcon ] ) && |
| 211 | <SelectControl |
| 212 | label={ __( 'Icon Location', 'generateblocks' ) } |
| 213 | value={ attributes[ attrIconLocation ] } |
| 214 | options={ locationOptions } |
| 215 | onChange={ ( value ) => { |
| 216 | setAttributes( { |
| 217 | [ this.props[ 'attrIconLocation' ] ]: value, // eslint-disable-line dot-notation |
| 218 | } ); |
| 219 | } } |
| 220 | /> |
| 221 | } |
| 222 | |
| 223 | { ( typeof attributes[ attrRemoveText ] !== 'undefined' && !! attributes[ attrIcon ] ) && |
| 224 | <ToggleControl |
| 225 | label={ __( 'Remove Text', 'generateblocks' ) } |
| 226 | checked={ !! attributes[ attrRemoveText ] } |
| 227 | onChange={ ( value ) => { |
| 228 | setAttributes( { |
| 229 | [ this.props[ 'attrRemoveText' ] ]: value, // eslint-disable-line dot-notation |
| 230 | } ); |
| 231 | } } |
| 232 | /> |
| 233 | } |
| 234 | </Fragment> |
| 235 | ); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | export default IconPicker; |
| 240 |