components
2 years ago
attributes.js
3 years ago
editor.scss
3 years ago
google-fonts.json
3 years ago
index.js
2 years ago
options.js
3 years ago
index.js
208 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { useContext, useRef } from '@wordpress/element'; |
| 3 | import { applyFilters } from '@wordpress/hooks'; |
| 4 | |
| 5 | import PanelArea from '../../../../components/panel-area'; |
| 6 | import getIcon from '../../../../utils/get-icon'; |
| 7 | import ControlsContext from '../../../../block-context'; |
| 8 | import FontSize from './components/font-size'; |
| 9 | import FontWeight from './components/font-weight'; |
| 10 | import TextTransform from './components/text-transform'; |
| 11 | import LetterSpacing from './components/letter-spacing'; |
| 12 | import LineHeight from './components/line-height'; |
| 13 | import FontFamily from './components/font-family'; |
| 14 | import Alignment from './components/alignment'; |
| 15 | import getAttribute from '../../../../utils/get-attribute'; |
| 16 | import FlexControl from '../../../../components/flex-control'; |
| 17 | import getDeviceType from '../../../../utils/get-device-type'; |
| 18 | import getResponsivePlaceholder from '../../../../utils/get-responsive-placeholder'; |
| 19 | import { useStyleIndicator, useDeviceAttributes } from '../../../../hooks'; |
| 20 | import { getContentAttribute } from '../../../../utils/get-content-attribute'; |
| 21 | import './editor.scss'; |
| 22 | |
| 23 | export default function Typography( { attributes, setAttributes, computedStyles } ) { |
| 24 | const device = getDeviceType(); |
| 25 | const { id, blockName, supports: { typography: typographySupports } } = useContext( ControlsContext ); |
| 26 | const panelRef = useRef( null ); |
| 27 | const contentValue = getContentAttribute( attributes, blockName ); |
| 28 | const [ deviceAttributes ] = useDeviceAttributes( attributes, setAttributes ); |
| 29 | const { typography } = attributes; |
| 30 | const isDesktop = 'Desktop' === device; |
| 31 | const panelControls = { |
| 32 | fontFamily: false, |
| 33 | fontSize: false, |
| 34 | fontWeight: false, |
| 35 | letterSpacing: false, |
| 36 | lineHeight: false, |
| 37 | textAlign: false, |
| 38 | textTransform: false, |
| 39 | }; |
| 40 | const { |
| 41 | dispatchControlGlobalStyle, |
| 42 | styleSources, |
| 43 | hasGlobalStyle, |
| 44 | contentWasUpdated, |
| 45 | } = useStyleIndicator( computedStyles, panelControls, contentValue, deviceAttributes ); |
| 46 | |
| 47 | const { |
| 48 | fontFamily = '', |
| 49 | fontSize = '', |
| 50 | fontWeight = '', |
| 51 | letterSpacing = '', |
| 52 | lineHeight = '', |
| 53 | textAlign = '', |
| 54 | textTransform = '', |
| 55 | } = deviceAttributes.typography; |
| 56 | |
| 57 | function getLabel( defaultLabel, rules ) { |
| 58 | return applyFilters( |
| 59 | 'generateblocks.editor.control.label', |
| 60 | defaultLabel, |
| 61 | rules, |
| 62 | styleSources, |
| 63 | dispatchControlGlobalStyle, |
| 64 | contentWasUpdated, |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | const labels = { |
| 69 | fontFamily: getLabel( |
| 70 | __( 'Font Family', 'generateblocks' ), |
| 71 | { fontFamily }, |
| 72 | ), |
| 73 | fontSize: getLabel( |
| 74 | __( 'Font Size', 'generateblocks' ), |
| 75 | { fontSize }, |
| 76 | ), |
| 77 | fontWeight: getLabel( |
| 78 | __( 'Font Weight', 'generateblocks' ), |
| 79 | { fontWeight }, |
| 80 | ), |
| 81 | letterSpacing: getLabel( |
| 82 | __( 'Letter Spacing', 'generateblocks' ), |
| 83 | { letterSpacing }, |
| 84 | ), |
| 85 | lineHeight: getLabel( |
| 86 | __( 'Line Height', 'generateblocks' ), |
| 87 | { lineHeight }, |
| 88 | ), |
| 89 | textAlign: getLabel( |
| 90 | __( 'Text Alignment', 'generateblocks' ), |
| 91 | { textAlign }, |
| 92 | ), |
| 93 | textTransform: getLabel( |
| 94 | __( 'Transform', 'generateblocks' ), |
| 95 | { textTransform }, |
| 96 | ), |
| 97 | }; |
| 98 | |
| 99 | return ( |
| 100 | <PanelArea |
| 101 | title={ __( 'Typography', 'generateblocks' ) } |
| 102 | initialOpen={ false } |
| 103 | icon={ getIcon( 'typography' ) } |
| 104 | className="gblocks-panel-label" |
| 105 | ref={ panelRef } |
| 106 | hasGlobalStyle={ hasGlobalStyle } |
| 107 | id={ `${ id }Typography` } |
| 108 | > |
| 109 | { typographySupports.alignment && |
| 110 | <Alignment |
| 111 | label={ labels.textAlign } |
| 112 | value={ getAttribute( 'textAlign', { attributes: typography, deviceType: device } ) } |
| 113 | onChange={ ( value ) => { |
| 114 | setAttributes( { |
| 115 | typography: { |
| 116 | [ getAttribute( 'textAlign', { attributes: typography, deviceType: device }, true ) ]: value, |
| 117 | }, |
| 118 | } ); |
| 119 | } } |
| 120 | /> |
| 121 | } |
| 122 | |
| 123 | { !! isDesktop && ( typographySupports.fontWeight || typographySupports.textTransform ) && |
| 124 | <FlexControl> |
| 125 | <FontWeight |
| 126 | label={ labels.fontWeight } |
| 127 | value={ typography?.fontWeight } |
| 128 | onChange={ ( value ) => { |
| 129 | setAttributes( { |
| 130 | typography: { |
| 131 | [ getAttribute( 'fontWeight', { attributes: typography, deviceType: device }, true ) ]: value, |
| 132 | }, |
| 133 | } ); |
| 134 | } } |
| 135 | /> |
| 136 | |
| 137 | <TextTransform |
| 138 | label={ labels.textTransform } |
| 139 | value={ typography?.textTransform } |
| 140 | onChange={ ( value ) => { |
| 141 | setAttributes( { |
| 142 | typography: { |
| 143 | [ getAttribute( 'textTransform', { attributes: typography, deviceType: device }, true ) ]: value, |
| 144 | }, |
| 145 | } ); |
| 146 | } } |
| 147 | /> |
| 148 | </FlexControl> |
| 149 | } |
| 150 | |
| 151 | { typographySupports.fontSize && |
| 152 | <FontSize |
| 153 | label={ labels.fontSize } |
| 154 | value={ getAttribute( 'fontSize', { attributes: typography, deviceType: device } ) } |
| 155 | placeholder={ getResponsivePlaceholder( 'fontSize', typography, device, parseInt( computedStyles.fontSize ) || '' ) } |
| 156 | onChange={ ( value ) => { |
| 157 | setAttributes( { |
| 158 | typography: { |
| 159 | [ getAttribute( 'fontSize', { attributes: typography, deviceType: device }, true ) ]: value, |
| 160 | }, |
| 161 | } ); |
| 162 | } } |
| 163 | /> |
| 164 | } |
| 165 | |
| 166 | { typographySupports.lineHeight && |
| 167 | <LineHeight |
| 168 | label={ labels.lineHeight } |
| 169 | defaultUnit="em" |
| 170 | value={ getAttribute( 'lineHeight', { attributes: typography, deviceType: device } ) } |
| 171 | placeholder={ getResponsivePlaceholder( 'lineHeight', typography, device, computedStyles.lineHeight ) } |
| 172 | onChange={ ( value ) => { |
| 173 | setAttributes( { |
| 174 | typography: { |
| 175 | [ getAttribute( 'lineHeight', { attributes: typography, deviceType: device }, true ) ]: value, |
| 176 | }, |
| 177 | } ); |
| 178 | } } |
| 179 | /> |
| 180 | } |
| 181 | |
| 182 | { typographySupports.letterSpacing && |
| 183 | <LetterSpacing |
| 184 | label={ labels.letterSpacing } |
| 185 | defaultUnit="em" |
| 186 | value={ getAttribute( 'letterSpacing', { attributes: typography, deviceType: device } ) } |
| 187 | placeholder={ getResponsivePlaceholder( 'letterSpacing', typography, device, computedStyles.letterSpacing ) } |
| 188 | onChange={ ( value ) => { |
| 189 | setAttributes( { |
| 190 | typography: { |
| 191 | [ getAttribute( 'letterSpacing', { attributes: typography, deviceType: device }, true ) ]: value, |
| 192 | }, |
| 193 | } ); |
| 194 | } } |
| 195 | /> |
| 196 | } |
| 197 | |
| 198 | { typographySupports.fontFamily && isDesktop && |
| 199 | <FontFamily |
| 200 | label={ labels.fontFamily } |
| 201 | attributes={ attributes } |
| 202 | setAttributes={ setAttributes } |
| 203 | /> |
| 204 | } |
| 205 | </PanelArea> |
| 206 | ); |
| 207 | } |
| 208 |