/** * Internal dependencies */ import Select from '../select'; import InputDrag from '../input-drag'; import getIcon from '../../utils/get-icon'; import ApplyFilters from '../apply-filters'; /** * WordPress dependencies */ const { Component } = wp.element; const { Tooltip, DropdownMenu, MenuGroup, MenuItem, ExternalLink } = wp.components; const { __ } = wp.i18n; const { applyFilters } = wp.hooks; const { GHOSTKIT } = window; /** * Get Default Font * @param {string} fontFamily - Current Font. * @return {*} - Current font or default label if font is empty. */ function getDefaultFont(fontFamily) { return fontFamily === '' ? __('Default Site Font', 'ghostkit') : fontFamily; } /** * Go over each fonts. * * @param {string} category - Font Family Category. * @return {*[]} - Fonts List. */ function getFonts(category = 'google-fonts') { const { fonts } = GHOSTKIT; const fontList = []; Object.keys(fonts).forEach((fontFamilyCategory) => { Object.keys(fonts[fontFamilyCategory].fonts).forEach((fontKey) => { const fontData = fonts[fontFamilyCategory].fonts[fontKey]; let fontValue = fontData.name; if (fontFamilyCategory === 'default') { fontValue = ''; } if (category === fontFamilyCategory || fontFamilyCategory === 'default') { fontList.push({ value: fontValue, label: fontData.label || fontData.name, fontFamilyCategory, }); } }); }); return fontList; } /** * Get Weight Label. * * @param {string} weight - Weight Value. * @return {string} - Weight Label. */ function getFontWeightLabel(weight) { let label = ''; switch (weight) { case '': label = __('Default', 'ghostkit'); break; case '100': label = __('Thin', 'ghostkit'); break; case '100i': label = __('Thin Italic', 'ghostkit'); break; case '200': label = __('Extra Light', 'ghostkit'); break; case '200i': label = __('Extra Light Italic', 'ghostkit'); break; case '300': label = __('Light', 'ghostkit'); break; case '300i': label = __('Light Italic', 'ghostkit'); break; case '400': label = __('Regular', 'ghostkit'); break; case '400i': label = __('Regular Italic', 'ghostkit'); break; case '500': label = __('Medium', 'ghostkit'); break; case '500i': label = __('Medium Italic', 'ghostkit'); break; case '600': label = __('Semi Bold', 'ghostkit'); break; case '600i': label = __('Semi Bold Italic', 'ghostkit'); break; case '700': label = __('Bold', 'ghostkit'); break; case '700i': label = __('Bold Italic', 'ghostkit'); break; case '800': label = __('Extra Bold', 'ghostkit'); break; case '800i': label = __('Extra Bold Italic', 'ghostkit'); break; case '900': label = __('Black', 'ghostkit'); break; case '900i': label = __('Black Italic', 'ghostkit'); break; // no default } return label; } /** * Get all font widths. * * @param {string} font - search font. * @param {string} fontFamilyCategory - font fontFamilyCategory. * @return {Array} - all font widths. */ function getFontWeights(font, fontFamilyCategory) { const { fonts } = GHOSTKIT; const fontWeights = []; if ( font !== '' && fontFamilyCategory !== '' && typeof font !== 'undefined' && typeof fontFamilyCategory !== 'undefined' && typeof fonts[fontFamilyCategory] !== 'undefined' ) { Object.keys(fonts[fontFamilyCategory].fonts).forEach((fontKey) => { if (fonts[fontFamilyCategory].fonts[fontKey].name === font) { Object.keys(fonts[fontFamilyCategory].fonts[fontKey].widths).forEach((widthKey) => { const width = fonts[fontFamilyCategory].fonts[fontKey].widths[widthKey]; fontWeights.push({ value: width, label: getFontWeightLabel(width) }); }); } }); } return fontWeights; } /** * Component Class */ export default class Typography extends Component { render() { const { fontFamilyCategory } = this.props; const { onChange, placeholders, label, fontFamily, fontWeight, fontSize, lineHeight, letterSpacing, childOf, fontWeights = getFontWeights(getDefaultFont(fontFamily), fontFamilyCategory), } = this.props; const fontsIcon = `icon-typography-${ fontFamilyCategory === 'default' ? 'google-fonts' : fontFamilyCategory }`; const allowFontSelectors = applyFilters( 'ghostkit.typography.allow.fonts', fontFamilyCategory !== 'adobe-fonts' && fontFamilyCategory !== 'custom-fonts', fontFamilyCategory ); const fontFamilyValue = { value: fontFamily, label: getDefaultFont(fontFamily), fontFamilyCategory, }; const fontWeightValue = { value: fontWeight, label: getFontWeightLabel(fontWeight) }; const fontSizeValue = typeof fontSize === 'undefined' ? '' : fontSize; let fontFamilies; if (fontFamilyCategory === 'default') { fontFamilies = getFonts('google-fonts'); } else { fontFamilies = getFonts(fontFamilyCategory); } // Find actual label. if (fontFamilyValue.value) { fontFamilies.forEach((familyData) => { if (fontFamilyValue.value === familyData.value) { fontFamilyValue.label = familyData.label; } }); } return (

{label}

{typeof fontFamilyCategory !== 'undefined' ? ( {({ onClose }) => ( { onChange({ fontFamilyCategory: 'google-fonts', fontFamily: '', fontWeight: '', }); onClose(); }} > {__('Google Fonts', 'ghostkit')} { onChange({ fontFamilyCategory: 'adobe-fonts', fontFamily: '', fontWeight: '', }); onClose(); }} > {__('Adobe Fonts', 'ghostkit')} {__('PRO', 'ghostkit')} { onChange({ fontFamilyCategory: 'custom-fonts', fontFamily: '', fontWeight: '', }); onClose(); }} > {__('Custom Fonts', 'ghostkit')} {__('PRO', 'ghostkit')} )} ) : ( '' )} {fontFamilyCategory === 'adobe-fonts' ? (
{__( 'Adobe Fonts available for Pro users only. Read more about Ghost Kit Pro plugin here - ', 'ghostkit' )} https://ghostkit.io/pricing/
) : ( '' )} {fontFamilyCategory === 'custom-fonts' ? (
{__( 'Custom Fonts available for Pro users only. Read more about Ghost Kit Pro plugin here - ', 'ghostkit' )} https://ghostkit.io/pricing/
) : ( '' )}
{typeof fontFamily !== 'undefined' && allowFontSelectors ? (
{ onChange({ fontWeight: opt && opt.value ? opt.value : '', }); }} options={fontWeights} placeholder={__('--- Select Weight ---', 'ghostkit')} className="ghostkit-typography-weight-selector" classNamePrefix="ghostkit-typography-weight-selector" menuPosition="fixed" />
) : ( '' )} {typeof fontSize !== 'undefined' ? (
{ onChange({ fontSize: value, }); }} autoComplete="off" icon={getIcon('icon-typography-font-size')} defaultUnit="px" />
) : ( '' )} {typeof lineHeight !== 'undefined' ? (
{ onChange({ lineHeight: value, }); }} autoComplete="off" icon={getIcon('icon-typography-line-height')} step={0.1} />
) : ( '' )} {typeof letterSpacing !== 'undefined' ? (
{ onChange({ letterSpacing: value, }); }} autoComplete="off" icon={getIcon('icon-typography-letter-spacing')} defaultUnit="em" step={0.01} />
) : ( '' )}
); } }