| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { Button, Dashicon } from '@wordpress/components'; |
| 6 |
import { useState } from '@wordpress/element'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Internal dependencies |
| 10 |
*/ |
| 11 |
import UAGSelectControl from '@Components/select-control'; |
| 12 |
import FontFamilyControl from './font-typography'; |
| 13 |
import RangeTypographyControl from './range-typography'; |
| 14 |
import TypographyStyles from './inline-styles'; |
| 15 |
import styles from './editor.lazy.scss'; |
| 16 |
import React, { useLayoutEffect } from 'react'; |
| 17 |
import { select } from '@wordpress/data'; |
| 18 |
import getUAGEditorStateLocalStorage from '@Controls/getUAGEditorStateLocalStorage'; |
| 19 |
import { blocksAttributes } from '@Attributes/getBlocksDefaultAttributes'; |
| 20 |
|
| 21 |
// Export for ease of importing in individual blocks. |
| 22 |
export { TypographyStyles }; |
| 23 |
|
| 24 |
const TypographyControl = ( props ) => { |
| 25 |
const [ showAdvancedControls, toggleAdvancedControls ] = useState( false ); |
| 26 |
|
| 27 |
// Add and remove the CSS on the drop and remove of the component. |
| 28 |
useLayoutEffect( () => { |
| 29 |
styles.use(); |
| 30 |
return () => { |
| 31 |
styles.unuse(); |
| 32 |
}; |
| 33 |
}, [] ); |
| 34 |
|
| 35 |
useLayoutEffect( () => { |
| 36 |
window.addEventListener( 'click', function ( e ) { |
| 37 |
const popupButton = document.querySelector( |
| 38 |
`.active.popup-${ props?.attributes?.block_id } .spectra-control-popup__options--action-button` |
| 39 |
); |
| 40 |
const popupWrap = document.querySelector( |
| 41 |
`.active.popup-${ props?.attributes?.block_id } .spectra-control-popup` |
| 42 |
); |
| 43 |
|
| 44 |
if ( |
| 45 |
popupButton && |
| 46 |
! popupButton?.contains( e.target ) && |
| 47 |
popupWrap && |
| 48 |
! popupWrap?.contains( e.target ) && |
| 49 |
! e.target?.parentElement?.parentElement?.classList?.contains( |
| 50 |
'uag-font-family-select__menu' |
| 51 |
) && |
| 52 |
! e.target?.classList?.contains( |
| 53 |
'uag-responsive-common-button' |
| 54 |
) && |
| 55 |
! e.target?.closest( '.uag-responsive-common-button' ) && |
| 56 |
! e.target?.parentElement?.closest( '.uagb-reset' ) |
| 57 |
) { |
| 58 |
toggleAdvancedControls( false ); |
| 59 |
|
| 60 |
const blockName = getSelectedBlock()?.name; |
| 61 |
const uagSettingState = getUAGEditorStateLocalStorage( |
| 62 |
'uagSettingState' |
| 63 |
); |
| 64 |
|
| 65 |
const data = { |
| 66 |
...uagSettingState, |
| 67 |
[ blockName ]: { |
| 68 |
...uagSettingState?.[ blockName ], |
| 69 |
selectedSetting: false, |
| 70 |
}, |
| 71 |
}; |
| 72 |
|
| 73 |
const uagLocalStorage = getUAGEditorStateLocalStorage(); |
| 74 |
if ( uagLocalStorage ) { |
| 75 |
uagLocalStorage.setItem( |
| 76 |
'uagSettingState', |
| 77 |
JSON.stringify( data ) |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
} ); |
| 82 |
}, [] ); |
| 83 |
|
| 84 |
let lineHeight; |
| 85 |
let fontFamily; |
| 86 |
let fontAdvancedControls; |
| 87 |
let fontTypoAdvancedControls; |
| 88 |
let showAdvancedFontControls; |
| 89 |
let transform; |
| 90 |
let decoration; |
| 91 |
let letterSpacing; |
| 92 |
const activeClass = showAdvancedControls ? 'active' : ''; |
| 93 |
|
| 94 |
const { |
| 95 |
disableFontFamily, |
| 96 |
disableFontSize, |
| 97 |
disableLineHeight, |
| 98 |
disableTransform, |
| 99 |
disableDecoration, |
| 100 |
disableAdvancedOptions = false, |
| 101 |
} = props; |
| 102 |
|
| 103 |
if ( true !== disableFontFamily ) { |
| 104 |
fontFamily = <FontFamilyControl { ...props } />; |
| 105 |
} |
| 106 |
const lineHeightStepsVal = 'em' === props.lineHeightType?.value ? 0.1 : 1; // fractional value when unit is em. |
| 107 |
const letterSpacingStepsVal = |
| 108 |
'em' === props.letterSpacingType?.value ? 0.1 : 1; // fractional value when unit is em. |
| 109 |
|
| 110 |
// Array of all the current Typography Control's Labels. |
| 111 |
const attributeNames = []; |
| 112 |
|
| 113 |
if ( ! disableFontFamily ) { |
| 114 |
attributeNames.push( |
| 115 |
props.fontFamily.label, |
| 116 |
props.fontWeight.label, |
| 117 |
props.fontStyle.label |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
if ( ! disableFontSize ) { |
| 122 |
attributeNames.push( |
| 123 |
props.fontSizeType.label, |
| 124 |
props.fontSize.label, |
| 125 |
props.fontSizeMobile.label, |
| 126 |
props.fontSizeTablet.label |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
if ( ! disableLineHeight ) { |
| 131 |
attributeNames.push( |
| 132 |
props.lineHeightType.label, |
| 133 |
props.lineHeight.label, |
| 134 |
props.lineHeightMobile.label, |
| 135 |
props.lineHeightTablet.label |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
if ( ! disableTransform ) { |
| 140 |
attributeNames.push( props.transform.label ); |
| 141 |
} |
| 142 |
|
| 143 |
if ( ! disableDecoration ) { |
| 144 |
attributeNames.push( props.decoration.label ); |
| 145 |
} |
| 146 |
|
| 147 |
if ( props.letterSpacing ) { |
| 148 |
attributeNames.push( |
| 149 |
props.letterSpacing.label, |
| 150 |
props.letterSpacingTablet.label, |
| 151 |
props.letterSpacingMobile.label, |
| 152 |
props.letterSpacingType.label |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
const { getSelectedBlock } = select( 'core/block-editor' ); |
| 157 |
|
| 158 |
// Function to get the Block's default Typography Values. |
| 159 |
const getBlockTypographyValue = () => { |
| 160 |
const selectedBlockName = getSelectedBlock()?.name.split( '/' ).pop(); |
| 161 |
let defaultValues = false; |
| 162 |
if ( 'undefined' !== typeof blocksAttributes[ selectedBlockName ] ) { |
| 163 |
attributeNames.forEach( ( attributeName ) => { |
| 164 |
if ( attributeName ) { |
| 165 |
const blockDefaultAttributeValue = |
| 166 |
'undefined' !== |
| 167 |
typeof blocksAttributes[ selectedBlockName ][ |
| 168 |
attributeName |
| 169 |
]?.default |
| 170 |
? blocksAttributes[ selectedBlockName ][ |
| 171 |
attributeName |
| 172 |
]?.default |
| 173 |
: ''; |
| 174 |
defaultValues = { |
| 175 |
...defaultValues, |
| 176 |
[ attributeName ]: blockDefaultAttributeValue, |
| 177 |
}; |
| 178 |
} |
| 179 |
} ); |
| 180 |
} |
| 181 |
return defaultValues; |
| 182 |
}; |
| 183 |
|
| 184 |
// Function to check if any Typography Setting has changed. |
| 185 |
const getUpdateState = () => { |
| 186 |
const defaultValues = getBlockTypographyValue(); |
| 187 |
const selectedBlockAttributes = getSelectedBlock()?.attributes; |
| 188 |
let isTypographyUpdated = false; |
| 189 |
attributeNames.forEach( ( attributeName ) => { |
| 190 |
if ( |
| 191 |
selectedBlockAttributes?.[ attributeName ] && |
| 192 |
selectedBlockAttributes?.[ attributeName ] !== |
| 193 |
defaultValues?.[ attributeName ] |
| 194 |
) { |
| 195 |
isTypographyUpdated = true; |
| 196 |
} |
| 197 |
} ); |
| 198 |
return isTypographyUpdated; |
| 199 |
}; |
| 200 |
|
| 201 |
// Flag to check if this control has been updated or not. |
| 202 |
const isTypographyUpdated = getUpdateState(); |
| 203 |
|
| 204 |
if ( true !== disableLineHeight ) { |
| 205 |
lineHeight = ( |
| 206 |
<RangeTypographyControl |
| 207 |
type={ props.lineHeightType } |
| 208 |
typeLabel={ props.lineHeightType.label } |
| 209 |
sizeMobile={ props.lineHeightMobile } |
| 210 |
sizeMobileLabel={ props.lineHeightMobile.label } |
| 211 |
sizeTablet={ props.lineHeightTablet } |
| 212 |
sizeTabletLabel={ props.lineHeightTablet.label } |
| 213 |
size={ props.lineHeight } |
| 214 |
sizeLabel={ props.lineHeight.label } |
| 215 |
sizeMobileText={ __( |
| 216 |
'Line Height', |
| 217 |
'ultimate-addons-for-gutenberg' |
| 218 |
) } |
| 219 |
sizeTabletText={ __( |
| 220 |
'Line Height', |
| 221 |
'ultimate-addons-for-gutenberg' |
| 222 |
) } |
| 223 |
sizeText={ __( |
| 224 |
'Line Height', |
| 225 |
'ultimate-addons-for-gutenberg' |
| 226 |
) } |
| 227 |
step={ lineHeightStepsVal } |
| 228 |
{ ...props } |
| 229 |
/> |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
if ( props.letterSpacing ) { |
| 234 |
letterSpacing = ( |
| 235 |
<RangeTypographyControl |
| 236 |
type={ props.letterSpacingType } |
| 237 |
typeLabel={ props.letterSpacingType.label } |
| 238 |
sizeMobile={ props.letterSpacingMobile } |
| 239 |
sizeMobileLabel={ props.letterSpacingMobile.label } |
| 240 |
sizeTablet={ props.letterSpacingTablet } |
| 241 |
sizeTabletLabel={ props.letterSpacingTablet.label } |
| 242 |
size={ props.letterSpacing } |
| 243 |
sizeLabel={ props.letterSpacing.label } |
| 244 |
sizeMobileText={ __( |
| 245 |
'Letter Spacing', |
| 246 |
'ultimate-addons-for-gutenberg' |
| 247 |
) } |
| 248 |
sizeTabletText={ __( |
| 249 |
'Letter Spacing', |
| 250 |
'ultimate-addons-for-gutenberg' |
| 251 |
) } |
| 252 |
sizeText={ __( |
| 253 |
'Letter Spacing', |
| 254 |
'ultimate-addons-for-gutenberg' |
| 255 |
) } |
| 256 |
step={ letterSpacingStepsVal } |
| 257 |
{ ...props } |
| 258 |
/> |
| 259 |
); |
| 260 |
} |
| 261 |
|
| 262 |
if ( ! disableTransform && props.transform ) { |
| 263 |
transform = ( |
| 264 |
<UAGSelectControl |
| 265 |
label={ __( 'Transform', 'ultimate-addons-for-gutenberg' ) } |
| 266 |
data={ { |
| 267 |
value: props.transform.value, |
| 268 |
label: props.transform.label, |
| 269 |
} } |
| 270 |
setAttributes={ props.setAttributes } |
| 271 |
options={ [ |
| 272 |
{ |
| 273 |
value: '', |
| 274 |
label: __( 'Default', 'ultimate-addons-for-gutenberg' ), |
| 275 |
}, |
| 276 |
{ |
| 277 |
value: 'normal', |
| 278 |
label: __( 'Normal', 'ultimate-addons-for-gutenberg' ), |
| 279 |
}, |
| 280 |
{ |
| 281 |
value: 'capitalize', |
| 282 |
label: __( |
| 283 |
'Capitalize', |
| 284 |
'ultimate-addons-for-gutenberg' |
| 285 |
), |
| 286 |
}, |
| 287 |
{ |
| 288 |
value: 'uppercase', |
| 289 |
label: __( |
| 290 |
'Uppercase', |
| 291 |
'ultimate-addons-for-gutenberg' |
| 292 |
), |
| 293 |
}, |
| 294 |
{ |
| 295 |
value: 'lowercase', |
| 296 |
label: __( |
| 297 |
'Lowercase', |
| 298 |
'ultimate-addons-for-gutenberg' |
| 299 |
), |
| 300 |
}, |
| 301 |
] } |
| 302 |
/> |
| 303 |
); |
| 304 |
} |
| 305 |
if ( ! disableDecoration && props.decoration ) { |
| 306 |
decoration = ( |
| 307 |
<div className="uag-typography-decoration"> |
| 308 |
<UAGSelectControl |
| 309 |
label={ __( |
| 310 |
'Decoration', |
| 311 |
'ultimate-addons-for-gutenberg' |
| 312 |
) } |
| 313 |
data={ { |
| 314 |
value: props.decoration.value, |
| 315 |
label: props.decoration.label, |
| 316 |
} } |
| 317 |
setAttributes={ props.setAttributes } |
| 318 |
options={ [ |
| 319 |
{ |
| 320 |
value: '', |
| 321 |
label: __( |
| 322 |
'Default', |
| 323 |
'ultimate-addons-for-gutenberg' |
| 324 |
), |
| 325 |
}, |
| 326 |
{ |
| 327 |
value: 'none', |
| 328 |
label: __( |
| 329 |
'None', |
| 330 |
'ultimate-addons-for-gutenberg' |
| 331 |
), |
| 332 |
}, |
| 333 |
{ |
| 334 |
value: 'underline', |
| 335 |
label: __( |
| 336 |
'Underline', |
| 337 |
'ultimate-addons-for-gutenberg' |
| 338 |
), |
| 339 |
}, |
| 340 |
{ |
| 341 |
value: 'overline', |
| 342 |
label: __( |
| 343 |
'Overline', |
| 344 |
'ultimate-addons-for-gutenberg' |
| 345 |
), |
| 346 |
}, |
| 347 |
{ |
| 348 |
value: 'line-through', |
| 349 |
label: __( |
| 350 |
'Line Through', |
| 351 |
'ultimate-addons-for-gutenberg' |
| 352 |
), |
| 353 |
}, |
| 354 |
] } |
| 355 |
/> |
| 356 |
</div> |
| 357 |
); |
| 358 |
} |
| 359 |
if ( true !== disableFontFamily && true !== disableFontSize ) { |
| 360 |
fontAdvancedControls = ( |
| 361 |
<Button |
| 362 |
className="uag-typography-button spectra-control-popup__options--action-button" |
| 363 |
aria-pressed={ showAdvancedControls } |
| 364 |
onClick={ () => { |
| 365 |
const allPopups = document.querySelectorAll( |
| 366 |
'.spectra-control-popup__options' |
| 367 |
); |
| 368 |
if ( allPopups && 0 < allPopups.length ) { |
| 369 |
for ( let i = 0; i < allPopups.length; i++ ) { |
| 370 |
const popupButton = allPopups[ i ]?.querySelector( |
| 371 |
'.spectra-control-popup__options.active .spectra-control-popup__options--action-button' |
| 372 |
); |
| 373 |
popupButton?.click(); |
| 374 |
} |
| 375 |
} |
| 376 |
toggleAdvancedControls( ! showAdvancedControls ); |
| 377 |
|
| 378 |
const blockName = getSelectedBlock()?.name; |
| 379 |
const uagSettingState = getUAGEditorStateLocalStorage( |
| 380 |
'uagSettingState' |
| 381 |
); |
| 382 |
let data = { |
| 383 |
...uagSettingState, |
| 384 |
[ blockName ]: { |
| 385 |
...uagSettingState?.[ blockName ], |
| 386 |
selectedSetting: '.uag-typography-options', |
| 387 |
}, |
| 388 |
}; |
| 389 |
|
| 390 |
if ( showAdvancedControls ) { |
| 391 |
data = { |
| 392 |
...uagSettingState, |
| 393 |
[ blockName ]: { |
| 394 |
...uagSettingState?.[ blockName ], |
| 395 |
selectedSetting: false, |
| 396 |
}, |
| 397 |
}; |
| 398 |
} |
| 399 |
|
| 400 |
const uagLocalStorage = getUAGEditorStateLocalStorage(); |
| 401 |
if ( uagLocalStorage ) { |
| 402 |
uagLocalStorage.setItem( |
| 403 |
'uagSettingState', |
| 404 |
JSON.stringify( data ) |
| 405 |
); |
| 406 |
} |
| 407 |
} } |
| 408 |
> |
| 409 |
<Dashicon icon="edit" /> |
| 410 |
</Button> |
| 411 |
); |
| 412 |
} else { |
| 413 |
showAdvancedFontControls = ( |
| 414 |
<> |
| 415 |
{ fontFamily } |
| 416 |
{ transform } |
| 417 |
{ decoration } |
| 418 |
{ lineHeight } |
| 419 |
{ letterSpacing } |
| 420 |
</> |
| 421 |
); |
| 422 |
} |
| 423 |
|
| 424 |
if ( showAdvancedControls === true ) { |
| 425 |
showAdvancedFontControls = ( |
| 426 |
<div className="uagb-typography-advanced spectra-control-popup"> |
| 427 |
{ fontFamily } |
| 428 |
{ transform } |
| 429 |
{ decoration } |
| 430 |
{ lineHeight } |
| 431 |
{ letterSpacing } |
| 432 |
</div> |
| 433 |
); |
| 434 |
} |
| 435 |
|
| 436 |
if ( true !== disableFontFamily && true !== disableFontSize ) { |
| 437 |
fontTypoAdvancedControls = ( |
| 438 |
<div className="spectra-control-popup__options--action-wrapper"> |
| 439 |
<span className="uag-control-label"> |
| 440 |
{ props.label } |
| 441 |
{ isTypographyUpdated && ( |
| 442 |
<div className="spectra__change-indicator--dot-right" /> |
| 443 |
) } |
| 444 |
</span> |
| 445 |
{ fontAdvancedControls } |
| 446 |
</div> |
| 447 |
); |
| 448 |
} |
| 449 |
|
| 450 |
return ( |
| 451 |
<div |
| 452 |
className={ `components-base-control uag-typography-options spectra-control-popup__options popup-${ props?.attributes?.block_id } ${ activeClass }` } |
| 453 |
> |
| 454 |
{ ! disableAdvancedOptions && ( |
| 455 |
<> |
| 456 |
{ fontTypoAdvancedControls } |
| 457 |
{ showAdvancedFontControls } |
| 458 |
</> |
| 459 |
) } |
| 460 |
</div> |
| 461 |
); |
| 462 |
}; |
| 463 |
|
| 464 |
export default TypographyControl; |
| 465 |
|