| 1 |
/** |
| 2 |
* Styles |
| 3 |
*/ |
| 4 |
import './style.scss'; |
| 5 |
|
| 6 |
import { |
| 7 |
BaseControl, |
| 8 |
Button, |
| 9 |
SelectControl, |
| 10 |
Spinner, |
| 11 |
} from '@wordpress/components'; |
| 12 |
import { Component, Fragment } from '@wordpress/element'; |
| 13 |
import { __ } from '@wordpress/i18n'; |
| 14 |
|
| 15 |
import Select from '../select'; |
| 16 |
|
| 17 |
const { fonts } = window.GHOSTKIT; |
| 18 |
|
| 19 |
const defaultFont = |
| 20 |
typeof fonts['google-fonts'].fonts !== 'undefined' |
| 21 |
? fonts['google-fonts'].fonts[0].name |
| 22 |
: ''; |
| 23 |
|
| 24 |
function getGoogleFontFamilyOptions() { |
| 25 |
const fontFamilies = []; |
| 26 |
|
| 27 |
if ( |
| 28 |
typeof fonts['google-fonts'] !== 'undefined' && |
| 29 |
typeof fonts['google-fonts'].fonts !== 'undefined' |
| 30 |
) { |
| 31 |
Object.keys(fonts['google-fonts'].fonts).forEach((key) => { |
| 32 |
fontFamilies.push({ |
| 33 |
value: fonts['google-fonts'].fonts[key].name, |
| 34 |
label: fonts['google-fonts'].fonts[key].name, |
| 35 |
}); |
| 36 |
}); |
| 37 |
} |
| 38 |
|
| 39 |
return fontFamilies; |
| 40 |
} |
| 41 |
|
| 42 |
function getGoogleFontWeightsByFamily(fontFamily) { |
| 43 |
const fontWeights = { |
| 44 |
normal: [], |
| 45 |
italic: [], |
| 46 |
}; |
| 47 |
|
| 48 |
if ( |
| 49 |
typeof fonts['google-fonts'] !== 'undefined' && |
| 50 |
typeof fonts['google-fonts'].fonts !== 'undefined' |
| 51 |
) { |
| 52 |
const font = fonts['google-fonts'].fonts.find( |
| 53 |
(item) => item.name === fontFamily |
| 54 |
); |
| 55 |
const { widths } = font; |
| 56 |
|
| 57 |
Object.keys(widths).forEach((key) => { |
| 58 |
const weight = { |
| 59 |
value: widths[key].replace('i', ''), |
| 60 |
label: widths[key].replace('i', ''), |
| 61 |
}; |
| 62 |
|
| 63 |
if (widths[key].indexOf('i') === -1) { |
| 64 |
fontWeights.normal.push(weight); |
| 65 |
} else { |
| 66 |
fontWeights.italic.push(weight); |
| 67 |
} |
| 68 |
}); |
| 69 |
} |
| 70 |
|
| 71 |
return fontWeights; |
| 72 |
} |
| 73 |
|
| 74 |
class GoogleFonts extends Component { |
| 75 |
constructor(props) { |
| 76 |
super(props); |
| 77 |
|
| 78 |
const { fontWeightOptions, styleOptions } = |
| 79 |
this.getFontWeightAndStyleOptions(defaultFont); |
| 80 |
|
| 81 |
this.state = { |
| 82 |
isLoading: true, |
| 83 |
// isEdit: false, |
| 84 |
name: defaultFont, |
| 85 |
weight: ['400'], |
| 86 |
style: 'normal', |
| 87 |
fontFamilyOptions: getGoogleFontFamilyOptions(), |
| 88 |
fontWeightOptions, |
| 89 |
styleOptions, |
| 90 |
error: '', |
| 91 |
isEdit: false, |
| 92 |
editKey: null, |
| 93 |
notice: '', |
| 94 |
}; |
| 95 |
} |
| 96 |
|
| 97 |
componentDidMount() { |
| 98 |
this.setState({ isLoading: false }); |
| 99 |
} |
| 100 |
|
| 101 |
getFontWeightAndStyleOptions(fontFamily, fontStyle = 'normal') { |
| 102 |
const styleOptions = []; |
| 103 |
const fontWeights = getGoogleFontWeightsByFamily(fontFamily); |
| 104 |
const fontWeightOptions = |
| 105 |
fontStyle === 'normal' ? fontWeights.normal : fontWeights.italic; |
| 106 |
if (fontWeights.normal.length > 0) { |
| 107 |
styleOptions.push({ |
| 108 |
value: 'normal', |
| 109 |
label: __('Normal', 'ghostkit'), |
| 110 |
}); |
| 111 |
} |
| 112 |
|
| 113 |
if (fontWeights.italic.length > 0) { |
| 114 |
styleOptions.push({ |
| 115 |
value: 'italic', |
| 116 |
label: __('Italic', 'ghostkit'), |
| 117 |
}); |
| 118 |
} |
| 119 |
return { |
| 120 |
fontWeightOptions, |
| 121 |
styleOptions, |
| 122 |
}; |
| 123 |
} |
| 124 |
|
| 125 |
renderEditor() { |
| 126 |
const { |
| 127 |
fontFamilyOptions, |
| 128 |
fontWeightOptions, |
| 129 |
styleOptions, |
| 130 |
style, |
| 131 |
name, |
| 132 |
weight, |
| 133 |
error, |
| 134 |
notice, |
| 135 |
isEdit, |
| 136 |
editKey, |
| 137 |
} = this.state; |
| 138 |
|
| 139 |
const { customFonts, updateFonts } = this.props; |
| 140 |
|
| 141 |
return ( |
| 142 |
<div className="editor-styles-wrapper"> |
| 143 |
<div className="ghostkit-settings-fonts-google-form"> |
| 144 |
{!isEdit ? ( |
| 145 |
<BaseControl |
| 146 |
id={__('Font', 'ghostkit')} |
| 147 |
label={__('Font', 'ghostkit')} |
| 148 |
> |
| 149 |
<Select |
| 150 |
value={{ |
| 151 |
value: name, |
| 152 |
label: name, |
| 153 |
}} |
| 154 |
onChange={(opt) => { |
| 155 |
let options = |
| 156 |
this.getFontWeightAndStyleOptions( |
| 157 |
opt.value, |
| 158 |
style |
| 159 |
); |
| 160 |
const newWeight = []; |
| 161 |
let newStyle = false; |
| 162 |
|
| 163 |
if ( |
| 164 |
options.fontWeightOptions.length === 0 |
| 165 |
) { |
| 166 |
newStyle = |
| 167 |
style === 'normal' |
| 168 |
? 'italic' |
| 169 |
: 'normal'; |
| 170 |
options = |
| 171 |
this.getFontWeightAndStyleOptions( |
| 172 |
opt.value, |
| 173 |
newStyle |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
Object.keys(weight).forEach((key) => { |
| 178 |
const findWeight = |
| 179 |
options.fontWeightOptions.find( |
| 180 |
(el) => el.value === weight |
| 181 |
); |
| 182 |
|
| 183 |
if (typeof findWeight !== 'undefined') { |
| 184 |
newWeight.push(weight[key]); |
| 185 |
} |
| 186 |
}); |
| 187 |
|
| 188 |
if ( |
| 189 |
newWeight.length === 0 && |
| 190 |
options.fontWeightOptions.length !== 0 |
| 191 |
) { |
| 192 |
newWeight.push( |
| 193 |
options.fontWeightOptions[0].value |
| 194 |
); |
| 195 |
} |
| 196 |
|
| 197 |
this.setState({ |
| 198 |
name: opt.value, |
| 199 |
style: newStyle || style, |
| 200 |
weight: newWeight, |
| 201 |
fontWeightOptions: |
| 202 |
options.fontWeightOptions, |
| 203 |
styleOptions: options.styleOptions, |
| 204 |
}); |
| 205 |
}} |
| 206 |
options={fontFamilyOptions} |
| 207 |
placeholder={__('--- Select ---', 'ghostkit')} |
| 208 |
/> |
| 209 |
</BaseControl> |
| 210 |
) : null} |
| 211 |
<SelectControl |
| 212 |
multiple |
| 213 |
label={__('Weight', 'ghostkit')} |
| 214 |
value={weight} |
| 215 |
onChange={(val) => { |
| 216 |
this.setState({ |
| 217 |
weight: val, |
| 218 |
}); |
| 219 |
}} |
| 220 |
options={fontWeightOptions} |
| 221 |
/> |
| 222 |
<SelectControl |
| 223 |
label={__('Style', 'ghostkit')} |
| 224 |
value={style} |
| 225 |
onChange={(val) => { |
| 226 |
this.setState({ |
| 227 |
style: val, |
| 228 |
}); |
| 229 |
}} |
| 230 |
options={styleOptions} |
| 231 |
/> |
| 232 |
{error ? ( |
| 233 |
<div className="ghostkit-settings-fonts-google-form-error"> |
| 234 |
{error} |
| 235 |
</div> |
| 236 |
) : null} |
| 237 |
{notice ? ( |
| 238 |
<div className="ghostkit-settings-fonts-google-form-notice"> |
| 239 |
{notice} |
| 240 |
</div> |
| 241 |
) : null} |
| 242 |
{isEdit ? ( |
| 243 |
<Button |
| 244 |
isPrimary |
| 245 |
onClick={() => { |
| 246 |
let findFont = false; |
| 247 |
Object.keys(customFonts.google).forEach( |
| 248 |
(fontKey) => { |
| 249 |
if ( |
| 250 |
customFonts.google[fontKey].name === |
| 251 |
name && |
| 252 |
customFonts.google[fontKey] |
| 253 |
.style === style && |
| 254 |
fontKey !== editKey |
| 255 |
) { |
| 256 |
findFont = true; |
| 257 |
} |
| 258 |
} |
| 259 |
); |
| 260 |
if (name && editKey && !findFont) { |
| 261 |
updateFonts({ |
| 262 |
google: { |
| 263 |
...customFonts.google, |
| 264 |
[editKey]: { |
| 265 |
name: this.state.name, |
| 266 |
weight: this.state.weight, |
| 267 |
style: this.state.style, |
| 268 |
}, |
| 269 |
}, |
| 270 |
}); |
| 271 |
|
| 272 |
const options = |
| 273 |
this.getFontWeightAndStyleOptions( |
| 274 |
defaultFont |
| 275 |
); |
| 276 |
|
| 277 |
this.setState({ |
| 278 |
name: defaultFont, |
| 279 |
weight: ['400'], |
| 280 |
style: 'normal', |
| 281 |
fontWeightOptions: |
| 282 |
options.fontWeightOptions, |
| 283 |
styleOptions: options.styleOptions, |
| 284 |
error: '', |
| 285 |
isEdit: false, |
| 286 |
editKey: null, |
| 287 |
notice: __( |
| 288 |
'The font has been successfully edited', |
| 289 |
'ghostkit' |
| 290 |
), |
| 291 |
}); |
| 292 |
} else if (!this.state.name) { |
| 293 |
this.setState({ |
| 294 |
error: __( |
| 295 |
'You should specify the `Name` to add new font.', |
| 296 |
'ghostkit' |
| 297 |
), |
| 298 |
}); |
| 299 |
} else if (findFont) { |
| 300 |
this.setState({ |
| 301 |
error: __( |
| 302 |
'The font has already been added with style.', |
| 303 |
'ghostkit' |
| 304 |
), |
| 305 |
}); |
| 306 |
} |
| 307 |
}} |
| 308 |
> |
| 309 |
{__('Edit Font', 'ghostkit')} |
| 310 |
</Button> |
| 311 |
) : ( |
| 312 |
<Button |
| 313 |
isPrimary |
| 314 |
onClick={() => { |
| 315 |
let findFont = false; |
| 316 |
Object.keys(customFonts.google).forEach( |
| 317 |
(fontKey) => { |
| 318 |
if ( |
| 319 |
customFonts.google[fontKey].name === |
| 320 |
name && |
| 321 |
customFonts.google[fontKey] |
| 322 |
.style === style |
| 323 |
) { |
| 324 |
findFont = true; |
| 325 |
} |
| 326 |
} |
| 327 |
); |
| 328 |
if (name && !findFont) { |
| 329 |
updateFonts({ |
| 330 |
google: { |
| 331 |
...customFonts.google, |
| 332 |
[Math.random() |
| 333 |
.toString(36) |
| 334 |
.substr(2, 9)]: { |
| 335 |
name: this.state.name, |
| 336 |
weight: this.state.weight, |
| 337 |
style: this.state.style, |
| 338 |
}, |
| 339 |
}, |
| 340 |
}); |
| 341 |
|
| 342 |
const options = |
| 343 |
this.getFontWeightAndStyleOptions( |
| 344 |
defaultFont |
| 345 |
); |
| 346 |
|
| 347 |
this.setState({ |
| 348 |
name: defaultFont, |
| 349 |
weight: ['400'], |
| 350 |
style: 'normal', |
| 351 |
fontWeightOptions: |
| 352 |
options.fontWeightOptions, |
| 353 |
styleOptions: options.styleOptions, |
| 354 |
error: '', |
| 355 |
notice: '', |
| 356 |
isEdit: false, |
| 357 |
}); |
| 358 |
} else if (!this.state.name) { |
| 359 |
this.setState({ |
| 360 |
error: __( |
| 361 |
'You should specify the `Name` to add new font.', |
| 362 |
'ghostkit' |
| 363 |
), |
| 364 |
}); |
| 365 |
} else if (findFont) { |
| 366 |
this.setState({ |
| 367 |
error: __( |
| 368 |
'The font has already been added. To edit, use the font edit button in the table.', |
| 369 |
'ghostkit' |
| 370 |
), |
| 371 |
}); |
| 372 |
} |
| 373 |
}} |
| 374 |
> |
| 375 |
{__('Add Font', 'ghostkit')} |
| 376 |
</Button> |
| 377 |
)} |
| 378 |
</div> |
| 379 |
</div> |
| 380 |
); |
| 381 |
} |
| 382 |
|
| 383 |
render() { |
| 384 |
const { isLoading, isEdit, editKey } = this.state; |
| 385 |
|
| 386 |
const { customFonts, updateFonts } = this.props; |
| 387 |
|
| 388 |
return ( |
| 389 |
<div className={isLoading ? 'ghostkit-settings-fonts-loading' : ''}> |
| 390 |
{isLoading ? <Spinner /> : ''} |
| 391 |
{!isEdit ? this.renderEditor() : ''} |
| 392 |
{customFonts.google && |
| 393 |
Object.keys(customFonts.google).length ? ( |
| 394 |
<> |
| 395 |
<br /> |
| 396 |
<table className="widefat fixed striped"> |
| 397 |
<thead> |
| 398 |
<tr> |
| 399 |
<td>{__('Font Family', 'ghostkit')}</td> |
| 400 |
<td>{__('Font Weights', 'ghostkit')}</td> |
| 401 |
<td>{__('Font Style', 'ghostkit')}</td> |
| 402 |
<td>{__('Actions', 'ghostkit')}</td> |
| 403 |
</tr> |
| 404 |
</thead> |
| 405 |
<tbody> |
| 406 |
{Object.keys(customFonts.google).map((key) => ( |
| 407 |
<Fragment |
| 408 |
key={customFonts.google[key].name + key} |
| 409 |
> |
| 410 |
<tr> |
| 411 |
<td> |
| 412 |
{customFonts.google[key].name} |
| 413 |
</td> |
| 414 |
<td> |
| 415 |
{Object.keys( |
| 416 |
customFonts.google[key] |
| 417 |
.weight |
| 418 |
).map((weightKey) => { |
| 419 |
weightKey = |
| 420 |
Number(weightKey); |
| 421 |
return ( |
| 422 |
<Fragment |
| 423 |
key={ |
| 424 |
customFonts |
| 425 |
.google[key] |
| 426 |
.name + |
| 427 |
key + |
| 428 |
weightKey |
| 429 |
} |
| 430 |
> |
| 431 |
{ |
| 432 |
customFonts |
| 433 |
.google[key] |
| 434 |
.weight[ |
| 435 |
weightKey |
| 436 |
] |
| 437 |
} |
| 438 |
{Number( |
| 439 |
customFonts |
| 440 |
.google[key] |
| 441 |
.weight |
| 442 |
.length |
| 443 |
) === |
| 444 |
weightKey + 1 |
| 445 |
? '' |
| 446 |
: ', '} |
| 447 |
</Fragment> |
| 448 |
); |
| 449 |
})} |
| 450 |
</td> |
| 451 |
<td> |
| 452 |
{customFonts.google[key].style} |
| 453 |
</td> |
| 454 |
<td> |
| 455 |
<Button |
| 456 |
isLink |
| 457 |
onClick={() => { |
| 458 |
const result = { |
| 459 |
...customFonts.google, |
| 460 |
}; |
| 461 |
|
| 462 |
delete result[key]; |
| 463 |
|
| 464 |
updateFonts({ |
| 465 |
google: { |
| 466 |
...result, |
| 467 |
}, |
| 468 |
}); |
| 469 |
}} |
| 470 |
> |
| 471 |
{__('Remove', 'ghostkit')} |
| 472 |
</Button> |
| 473 |
| |
| 474 |
<Button |
| 475 |
isLink |
| 476 |
onClick={() => { |
| 477 |
const result = { |
| 478 |
...customFonts.google, |
| 479 |
}; |
| 480 |
|
| 481 |
const options = |
| 482 |
this.getFontWeightAndStyleOptions( |
| 483 |
result[key] |
| 484 |
.name, |
| 485 |
result[key] |
| 486 |
.style |
| 487 |
); |
| 488 |
|
| 489 |
this.setState({ |
| 490 |
name: result[key] |
| 491 |
.name, |
| 492 |
style: result[key] |
| 493 |
.style, |
| 494 |
weight: result[key] |
| 495 |
.weight, |
| 496 |
fontWeightOptions: |
| 497 |
options.fontWeightOptions, |
| 498 |
styleOptions: |
| 499 |
options.styleOptions, |
| 500 |
isEdit: true, |
| 501 |
editKey: key, |
| 502 |
}); |
| 503 |
}} |
| 504 |
> |
| 505 |
{__('Edit', 'ghostkit')} |
| 506 |
</Button> |
| 507 |
</td> |
| 508 |
</tr> |
| 509 |
{isEdit && editKey === key ? ( |
| 510 |
<tr> |
| 511 |
<td colSpan={4}> |
| 512 |
{this.renderEditor()} |
| 513 |
</td> |
| 514 |
</tr> |
| 515 |
) : null} |
| 516 |
</Fragment> |
| 517 |
))} |
| 518 |
</tbody> |
| 519 |
</table> |
| 520 |
</> |
| 521 |
) : null} |
| 522 |
</div> |
| 523 |
); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
export default GoogleFonts; |
| 528 |
|