PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.4.0
GenerateBlocks v1.4.0
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / components / typography / index.js
generateblocks / src / components / typography Last commit date
editor.scss 6 years ago google-fonts.json 4 years ago index.js 4 years ago
index.js
480 lines
1 /**
2 * Internal dependencies
3 */
4 import './editor.scss';
5 import googleFonts from './google-fonts.json';
6 import UnitPicker from '../unit-picker';
7 import getResponsivePlaceholder from '../../utils/get-responsive-placeholder';
8
9 /**
10 * WordPress dependencies
11 */
12 import {
13 __,
14 } from '@wordpress/i18n';
15
16 import {
17 Component,
18 Fragment,
19 } from '@wordpress/element';
20
21 import {
22 BaseControl,
23 SelectControl,
24 ToggleControl,
25 TextControl,
26 Button,
27 } from '@wordpress/components';
28
29 /**
30 * Typography Component
31 */
32 class TypographyControls extends Component {
33 constructor() {
34 super( ...arguments );
35
36 this.state = {
37 showAdvancedTypography: 'true' === localStorage.getItem( 'generateblocksShowAdvancedTypography' ) || false,
38 };
39 }
40
41 render() {
42 const {
43 setAttributes,
44 attributes,
45 device = '',
46 showFontSize = false,
47 showFontFamily = false,
48 showFontWeight = false,
49 showTextTransform = false,
50 showLineHeight = false,
51 showLetterSpacing = false,
52 disableAdvancedToggle = false,
53 fontSizePlaceholder = '17',
54 } = this.props;
55
56 const fonts = [
57 { value: '', label: __( 'Select font', 'generateblocks' ) },
58 { value: 'Arial', label: 'Arial' },
59 { value: 'Helvetica', label: 'Helvetica' },
60 { value: 'Times New Roman', label: 'Times New Roman' },
61 { value: 'Georgia', label: 'Georgia' },
62 ];
63
64 Object.keys( googleFonts ).slice( 0, 20 ).forEach( ( k ) => {
65 fonts.push(
66 { value: k, label: k }
67 );
68 } );
69
70 fonts.push(
71 { value: 'other', label: __( 'Other', 'generateblocks' ) }
72 );
73
74 let weight = [
75 { value: '', label: __( 'Default', 'generateblocks' ) },
76 { value: 'normal', label: __( 'Normal', 'generateblocks' ) },
77 { value: 'bold', label: __( 'Bold', 'generateblocks' ) },
78 { value: '100', label: '100' },
79 { value: '200', label: '200' },
80 { value: '300', label: '300' },
81 { value: '400', label: '400' },
82 { value: '500', label: '500' },
83 { value: '600', label: '600' },
84 { value: '700', label: '700' },
85 { value: '800', label: '800' },
86 { value: '900', label: '900' },
87 ];
88
89 const transform = [
90 { value: '', label: __( 'Default', 'generateblocks' ) },
91 { value: 'uppercase', label: __( 'Uppercase', 'generateblocks' ) },
92 { value: 'lowercase', label: __( 'Lowercase', 'generateblocks' ) },
93 { value: 'capitalize', label: __( 'Capitalize', 'generateblocks' ) },
94 { value: 'initial', label: __( 'Normal', 'generateblocks' ) },
95 ];
96
97 if ( typeof googleFonts[ attributes.fontFamily ] !== 'undefined' && typeof googleFonts[ attributes.fontFamily ].weight !== 'undefined' ) {
98 weight = [
99 { value: '', label: __( 'Default', 'generateblocks' ) },
100 { value: 'normal', label: __( 'Normal', 'generateblocks' ) },
101 { value: 'bold', label: __( 'Bold', 'generateblocks' ) },
102 ];
103
104 googleFonts[ attributes.fontFamily ].weight.filter( function( k ) {
105 const hasLetters = k.match( /[a-z]/g );
106 const hasNumbers = k.match( /[0-9]/g );
107
108 if ( ( hasLetters && hasNumbers ) || 'italic' === k || 'regular' === k ) {
109 return false;
110 }
111
112 return true;
113 } ).forEach( ( k ) => {
114 weight.push(
115 { value: k, label: k }
116 );
117 } );
118 }
119
120 const onFontChange = ( value ) => {
121 if ( 'other' === value ) {
122 value = '';
123 }
124
125 let fontWeight = attributes.fontWeight; // eslint-disable-line no-unused-vars
126
127 setAttributes( { fontFamily: value } );
128
129 if ( attributes.fontWeight && Object.values( weight ).indexOf( attributes.fontWeight ) < 0 ) {
130 fontWeight = ''; // eslint-disable-line no-unused-vars
131 }
132
133 if ( typeof googleFonts[ value ] !== 'undefined' ) {
134 setAttributes( {
135 'googleFont': true, // eslint-disable-line quote-props
136 'fontFamilyFallback': googleFonts[ value ].fallback, // eslint-disable-line quote-props
137 'googleFontVariants': googleFonts[ value ].weight.join( ', ' ), // eslint-disable-line quote-props
138 } );
139 } else {
140 setAttributes( {
141 'googleFont': false, // eslint-disable-line quote-props
142 'fontFamilyFallback': '', // eslint-disable-line quote-props
143 'googleFontVariants': '', // eslint-disable-line quote-props
144 } );
145 }
146 };
147
148 const onFontShortcut = ( event ) => {
149 setAttributes( { 'fontFamily': event.target.value } ); // eslint-disable-line quote-props
150 onFontChange( event.target.value );
151 };
152
153 const getValue = ( value, setDevice ) => {
154 const valueName = value + setDevice;
155
156 return attributes[ valueName ];
157 };
158
159 const getAttributeName = ( name, setDevice ) => {
160 const attributeName = name + setDevice;
161
162 return attributeName;
163 };
164
165 let showAdvancedToggle = this.state.showAdvancedTypography;
166
167 if ( disableAdvancedToggle ) {
168 showAdvancedToggle = true;
169 }
170
171 return (
172 <Fragment>
173 <div className={ 'components-gblocks-typography-weight-transform' }>
174 { showFontWeight &&
175 <SelectControl
176 label={ __( 'Weight', 'generateblocks' ) }
177 value={ attributes.fontWeight }
178 options={ weight }
179 onChange={ ( value ) => {
180 setAttributes( {
181 'fontWeight': value, // eslint-disable-line quote-props
182 } );
183 } }
184 className="components-base-control"
185 />
186 }
187
188 { showTextTransform &&
189 <SelectControl
190 label={ __( 'Transform', 'generateblocks' ) }
191 value={ attributes.textTransform }
192 options={ transform }
193 onChange={ ( value ) => {
194 setAttributes( {
195 'textTransform': value, // eslint-disable-line quote-props
196 } );
197 } }
198 className="components-base-control"
199 />
200 }
201 </div>
202
203 { ! disableAdvancedToggle &&
204 <ToggleControl
205 label={ __( 'Show Advanced Typography', 'generateblocks' ) }
206 checked={ !! this.state.showAdvancedTypography }
207 onChange={ ( value ) => {
208 localStorage.setItem( 'generateblocksShowAdvancedTypography', value );
209
210 this.setState( {
211 showAdvancedTypography: value,
212 } );
213 } }
214 />
215 }
216
217 { showFontFamily && showAdvancedToggle &&
218 <BaseControl className={ 'gblocks-font-family-shortcuts' }>
219 <span className="components-base-control__label">{ __( 'Font Family', 'generateblocks' ) }</span>
220
221 <select
222 className="components-select-control__input components-select-control__input--gblocks-fontfamily"
223 onChange={ onFontShortcut }
224 onBlur={ onFontShortcut }
225 >
226 { fonts.map( ( option, index ) =>
227 <option
228 key={ `${ option.label }-${ option.value }-${ index }` }
229 value={ option.value }
230 >
231 { option.label }
232 </option>
233 ) }
234 </select>
235 </BaseControl>
236 }
237
238 { showFontFamily && showAdvancedToggle &&
239 <TextControl
240 value={ attributes.fontFamily }
241 placeholder={ __( 'Enter font name', 'generateblocks' ) }
242 onChange={ ( nextFontFamily ) => onFontChange( nextFontFamily ) }
243 />
244 }
245
246 { showFontFamily && '' !== attributes.fontFamily && showAdvancedToggle &&
247 <Fragment>
248 <ToggleControl
249 label={ __( 'Google Font', 'generateblocks' ) }
250 checked={ !! attributes.googleFont }
251 onChange={ ( value ) => {
252 setAttributes( {
253 'googleFont': value, // eslint-disable-line quote-props
254 } );
255
256 if ( value ) {
257 if ( typeof googleFonts[ attributes.fontFamily ] !== 'undefined' ) {
258 setAttributes( {
259 'fontFamilyFallback': googleFonts[ attributes.fontFamily ].fallback, // eslint-disable-line quote-props
260 'googleFontVariants': googleFonts[ attributes.fontFamily ].weight.join( ', ' ), // eslint-disable-line quote-props
261 } );
262 }
263 }
264 } }
265 />
266
267 { !! attributes.googleFont &&
268 <TextControl
269 label={ __( 'Variants', 'generateblocks' ) }
270 value={ attributes.googleFontVariants }
271 placeholder={ __( '300, 400, 400i', 'generateblocks' ) }
272 onChange={ ( value ) => {
273 setAttributes( {
274 'googleFontVariants': value, // eslint-disable-line quote-props
275 } );
276 } }
277 />
278 }
279 </Fragment>
280 }
281
282 { showFontFamily && showAdvancedToggle &&
283 <TextControl
284 label={ __( 'Font Family Fallback', 'generateblocks' ) }
285 value={ attributes.fontFamilyFallback }
286 placeholder={ __( 'sans-serif', 'generateblocks' ) }
287 onChange={ ( value ) => {
288 setAttributes( {
289 'fontFamilyFallback': value, // eslint-disable-line quote-props
290 } );
291 } }
292 />
293 }
294
295 { showFontSize && showAdvancedToggle &&
296 <BaseControl>
297 <UnitPicker
298 label={ __( 'Font Size', 'generateblocks' ) }
299 value={ attributes.fontSizeUnit }
300 units={ [ 'px', 'em', '%' ] }
301 onClick={ ( value ) => {
302 setAttributes( {
303 fontSizeUnit: value,
304 } );
305 } }
306 />
307
308 <div className="components-gblocks-typography-control__inputs">
309 <TextControl
310 type={ 'number' }
311 value={ getValue( 'fontSize', device ) || '' }
312 placeholder={ getResponsivePlaceholder( 'fontSize', attributes, device, fontSizePlaceholder ) }
313 onChange={ ( value ) => {
314 const name = getAttributeName( 'fontSize', device );
315
316 setAttributes( {
317 [ name ]: value,
318 } );
319 } }
320 onBlur={ () => {
321 const name = getAttributeName( 'fontSize', device );
322
323 if ( '' !== getValue( 'fontSize', device ) ) {
324 setAttributes( {
325 [ name ]: parseFloat( getValue( 'fontSize', device ) ),
326 } );
327 }
328 } }
329 onClick={ ( e ) => {
330 // Make sure onBlur fires in Firefox.
331 e.currentTarget.focus();
332 } }
333 min={ 1 }
334 autoComplete="off"
335 />
336
337 <Button
338 isSmall
339 isSecondary
340 className="components-gblocks-default-number"
341 onClick={ () => {
342 const name = getAttributeName( 'fontSize', device );
343
344 setAttributes( {
345 [ name ]: this.props.defaultFontSize,
346 } );
347 } }
348 >
349 { __( 'Reset', 'generateblocks' ) }
350 </Button>
351 </div>
352 </BaseControl>
353 }
354
355 { showLineHeight && showAdvancedToggle &&
356 <BaseControl>
357 <UnitPicker
358 label={ __( 'Line Height', 'generateblocks' ) }
359 value={ attributes.lineHeightUnit }
360 units={ [ 'px', 'em', '%' ] }
361 onClick={ ( value ) => {
362 setAttributes( {
363 lineHeightUnit: value,
364 } );
365 } }
366 />
367
368 <div className="components-gblocks-typography-control__inputs">
369 <TextControl
370 type={ 'number' }
371 value={ getValue( 'lineHeight', device ) || 0 === getValue( 'lineHeight', device ) ? getValue( 'lineHeight', device ) : '' }
372 placeholder={ getResponsivePlaceholder( 'lineHeight', attributes, device, '' ) }
373 onChange={ ( value ) => {
374 const name = getAttributeName( 'lineHeight', device );
375
376 setAttributes( {
377 [ name ]: value,
378 } );
379 } }
380 onBlur={ () => {
381 const name = getAttributeName( 'lineHeight', device );
382
383 if ( '' !== getValue( 'lineHeight', device ) ) {
384 setAttributes( {
385 [ name ]: parseFloat( getValue( 'lineHeight', device ) ),
386 } );
387 }
388 } }
389 onClick={ ( e ) => {
390 // Make sure onBlur fires in Firefox.
391 e.currentTarget.focus();
392 } }
393 min={ 0 }
394 step={ .1 }
395 autoComplete="off"
396 />
397
398 <Button
399 isSmall
400 isSecondary
401 className="components-gblocks-default-number"
402 onClick={ () => {
403 const name = getAttributeName( 'lineHeight', device );
404
405 setAttributes( {
406 [ name ]: this.props.defaultLineHeight,
407 } );
408 } }
409 >
410 { __( 'Reset', 'generateblocks' ) }
411 </Button>
412 </div>
413 </BaseControl>
414 }
415
416 { showLetterSpacing && showAdvancedToggle &&
417 <BaseControl>
418 <UnitPicker
419 label={ __( 'Letter Spacing', 'generateblocks' ) }
420 value={ 'em' }
421 units={ [ 'em' ] }
422 onClick={ () => {
423 return false;
424 } }
425 />
426
427 <div className="components-gblocks-typography-control__inputs">
428 <TextControl
429 type={ 'number' }
430 value={ getValue( 'letterSpacing', device ) || '' }
431 placeholder={ getResponsivePlaceholder( 'letterSpacing', attributes, device, '0.01' ) }
432 onChange={ ( value ) => {
433 const name = getAttributeName( 'letterSpacing', device );
434
435 setAttributes( {
436 [ name ]: value,
437 } );
438 } }
439 onBlur={ () => {
440 const name = getAttributeName( 'letterSpacing', device );
441
442 if ( '' !== getValue( 'letterSpacing', device ) ) {
443 setAttributes( {
444 [ name ]: parseFloat( getValue( 'letterSpacing', device ) ),
445 } );
446 }
447 } }
448 onClick={ ( e ) => {
449 // Make sure onBlur fires in Firefox.
450 e.currentTarget.focus();
451 } }
452 min={ -1 }
453 step={ .01 }
454 autoComplete="off"
455 />
456
457 <Button
458 isSmall
459 isSecondary
460 className="components-gblocks-default-number"
461 onClick={ () => {
462 const name = getAttributeName( 'letterSpacing', device );
463
464 setAttributes( {
465 [ name ]: this.props.defaultLetterSpacing,
466 } );
467 } }
468 >
469 { __( 'Reset', 'generateblocks' ) }
470 </Button>
471 </div>
472 </BaseControl>
473 }
474 </Fragment>
475 );
476 }
477 }
478
479 export default TypographyControls;
480