PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.5.3
GenerateBlocks v1.5.3
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 4 years ago google-fonts.json 4 years ago index.js 4 years ago
index.js
333 lines
1 /**
2 * Internal dependencies
3 */
4 import './editor.scss';
5 import googleFonts from './google-fonts.json';
6 import NumberControl from '../number-control';
7
8 /**
9 * WordPress dependencies
10 */
11 import { __ } from '@wordpress/i18n';
12 import { Fragment, useEffect, useState } from '@wordpress/element';
13 import {
14 BaseControl,
15 SelectControl,
16 ToggleControl,
17 TextControl,
18 } from '@wordpress/components';
19
20 export default function TypographyComponent( props ) {
21 const {
22 setAttributes,
23 attributes,
24 options = [],
25 deviceType,
26 computedStyles = {},
27 } = props;
28
29 const {
30 fontFamily,
31 fontFamilyFallback,
32 googleFont,
33 googleFontVariants,
34 fontWeight,
35 textTransform,
36 fontSizeUnit,
37 } = attributes;
38
39 const [ availableOptions, setAvailableOptions ] = useState( options );
40
41 useEffect( () => {
42 // These options don't have device values.
43 if ( 'Desktop' !== deviceType ) {
44 setAvailableOptions( availableOptions.filter(
45 ( option ) =>
46 'fontWeight' !== option &&
47 'textTransform' !== option &&
48 'fontFamily' !== option
49 ) );
50 }
51 }, [ deviceType ] );
52
53 const fonts = [
54 { value: '', label: __( 'Select font', 'generateblocks' ) },
55 { value: 'Arial', label: 'Arial' },
56 { value: 'Helvetica', label: 'Helvetica' },
57 { value: 'Times New Roman', label: 'Times New Roman' },
58 { value: 'Georgia', label: 'Georgia' },
59 ];
60
61 Object.keys( googleFonts ).slice( 0, 20 ).forEach( ( k ) => {
62 fonts.push(
63 { value: k, label: k }
64 );
65 } );
66
67 let weight = [
68 { value: '', label: __( 'Default', 'generateblocks' ) },
69 { value: 'normal', label: __( 'Normal', 'generateblocks' ) },
70 { value: 'bold', label: __( 'Bold', 'generateblocks' ) },
71 { value: '100', label: '100' },
72 { value: '200', label: '200' },
73 { value: '300', label: '300' },
74 { value: '400', label: '400' },
75 { value: '500', label: '500' },
76 { value: '600', label: '600' },
77 { value: '700', label: '700' },
78 { value: '800', label: '800' },
79 { value: '900', label: '900' },
80 ];
81
82 const transform = [
83 { value: '', label: __( 'Default', 'generateblocks' ) },
84 { value: 'uppercase', label: __( 'Uppercase', 'generateblocks' ) },
85 { value: 'lowercase', label: __( 'Lowercase', 'generateblocks' ) },
86 { value: 'capitalize', label: __( 'Capitalize', 'generateblocks' ) },
87 { value: 'initial', label: __( 'Normal', 'generateblocks' ) },
88 ];
89
90 if ( typeof googleFonts[ fontFamily ] !== 'undefined' && typeof googleFonts[ fontFamily ].weight !== 'undefined' ) {
91 weight = [
92 { value: '', label: __( 'Default', 'generateblocks' ) },
93 { value: 'normal', label: __( 'Normal', 'generateblocks' ) },
94 { value: 'bold', label: __( 'Bold', 'generateblocks' ) },
95 ];
96
97 googleFonts[ fontFamily ].weight.filter( function( k ) {
98 const hasLetters = k.match( /[a-z]/g );
99 const hasNumbers = k.match( /[0-9]/g );
100
101 if ( ( hasLetters && hasNumbers ) || 'italic' === k || 'regular' === k ) {
102 return false;
103 }
104
105 return true;
106 } ).forEach( ( k ) => {
107 weight.push(
108 { value: k, label: k }
109 );
110 } );
111 }
112
113 const onFontChange = ( value ) => {
114 if ( 'other' === value ) {
115 value = '';
116 }
117
118 setAttributes( { fontFamily: value } );
119
120 if ( typeof googleFonts[ value ] !== 'undefined' ) {
121 setAttributes( {
122 googleFont: true,
123 fontFamilyFallback: googleFonts[ value ].fallback,
124 googleFontVariants: googleFonts[ value ].weight.join( ', ' ),
125 } );
126 } else {
127 setAttributes( {
128 googleFont: false,
129 fontFamilyFallback: '',
130 googleFontVariants: '',
131 } );
132 }
133 };
134
135 const onFontShortcut = ( value ) => {
136 setAttributes( { fontFamily: value } );
137 onFontChange( value );
138 };
139
140 return (
141 <div className="gblocks-typography-component">
142 { ( availableOptions.includes( 'fontWeight' ) || availableOptions.includes( 'textTransform' ) ) &&
143 <BaseControl className="gblocks-typography-component__appearance">
144 { availableOptions.includes( 'fontWeight' ) &&
145 <SelectControl
146 label={ __( 'Weight', 'generateblocks' ) }
147 value={ fontWeight }
148 options={ weight }
149 onChange={ ( value ) => {
150 setAttributes( {
151 fontWeight: value,
152 } );
153 } }
154 />
155 }
156
157 { availableOptions.includes( 'textTransform' ) &&
158 <SelectControl
159 label={ __( 'Transform', 'generateblocks' ) }
160 value={ textTransform }
161 options={ transform }
162 onChange={ ( value ) => {
163 setAttributes( {
164 textTransform: value,
165 } );
166 } }
167 />
168 }
169 </BaseControl>
170 }
171
172 { availableOptions.includes( 'fontSize' ) &&
173 <NumberControl
174 { ...props }
175 label={ __( 'Font Size', 'generateblocks' ) }
176 attributeName="fontSize"
177 units={ [ 'px', 'em', '%' ] }
178 device={ deviceType }
179 defaultPlaceholder={
180 computedStyles.fontSize && 'px' === fontSizeUnit
181 ? computedStyles.fontSize
182 : ''
183 }
184 presets={
185 [
186 {
187 unit: 'px',
188 data: [ 13, 17, 25, 35 ],
189 },
190 ]
191 }
192 min="1"
193 />
194 }
195
196 { availableOptions.includes( 'lineHeight' ) &&
197 <NumberControl
198 { ...props }
199 label={ __( 'Line Height', 'generateblocks' ) }
200 attributeName="lineHeight"
201 units={ [ 'px', 'em', '%' ] }
202 device={ deviceType }
203 presets={
204 [
205 {
206 unit: 'em',
207 data: [
208 {
209 label: __( 'Small', 'generateblocks' ),
210 value: 0.8,
211 },
212 {
213 label: __( 'Medium', 'generateblocks' ),
214 value: 1,
215 },
216 {
217 label: __( 'Large', 'generateblocks' ),
218 value: 1.5,
219 },
220 ],
221 },
222 ]
223 }
224 min="0"
225 step={ .1 }
226 />
227 }
228
229 { availableOptions.includes( 'letterSpacing' ) &&
230 <NumberControl
231 { ...props }
232 label={ __( 'Letter Spacing', 'generateblocks' ) }
233 attributeName="letterSpacing"
234 unit="em"
235 units={ [ 'em' ] }
236 device={ deviceType }
237 presets={
238 [
239 {
240 unit: 'em',
241 data: [
242 {
243 label: __( 'Small', 'generateblocks' ),
244 value: -0.02,
245 },
246 {
247 label: __( 'Medium', 'generateblocks' ),
248 value: 0.02,
249 },
250 {
251 label: __( 'Large', 'generateblocks' ),
252 value: 0.05,
253 },
254 ],
255 },
256 ]
257 }
258 min={ -1 }
259 step={ .01 }
260 />
261 }
262
263 { availableOptions.includes( 'fontFamily' ) &&
264 <>
265 <BaseControl className="gblocks-typography-component__font-family">
266 <SelectControl
267 label={ __( 'Font Family', 'generateblocks' ) }
268 options={ fonts }
269 onChange={ onFontShortcut }
270 />
271
272 <div className="gblocks-typography-component__font-family-input">
273 <TextControl
274 value={ fontFamily }
275 placeholder={ __( 'Enter font name', 'generateblocks' ) }
276 onChange={ ( nextFontFamily ) => onFontChange( nextFontFamily ) }
277 />
278
279 { ( !! fontFamilyFallback || !! googleFont ) &&
280 <TextControl
281 value={ fontFamilyFallback }
282 placeholder={ __( 'sans-serif', 'generateblocks' ) }
283 onChange={ ( value ) => {
284 setAttributes( {
285 fontFamilyFallback: value,
286 } );
287 } }
288 />
289 }
290 </div>
291 </BaseControl>
292
293 { '' !== fontFamily &&
294 <Fragment>
295 <ToggleControl
296 label={ __( 'Use Google Fonts API', 'generateblocks' ) }
297 checked={ !! googleFont }
298 onChange={ ( value ) => {
299 setAttributes( {
300 googleFont: value,
301 } );
302
303 if ( value ) {
304 if ( typeof googleFonts[ fontFamily ] !== 'undefined' ) {
305 setAttributes( {
306 fontFamilyFallback: googleFonts[ fontFamily ].fallback,
307 googleFontVariants: googleFonts[ fontFamily ].weight.join( ', ' ),
308 } );
309 }
310 }
311 } }
312 />
313
314 { !! googleFont &&
315 <TextControl
316 label={ __( 'Variants', 'generateblocks' ) }
317 value={ googleFontVariants }
318 placeholder={ __( '300, 400, 400i', 'generateblocks' ) }
319 onChange={ ( value ) => {
320 setAttributes( {
321 googleFontVariants: value,
322 } );
323 } }
324 />
325 }
326 </Fragment>
327 }
328 </>
329 }
330 </div>
331 );
332 }
333