| 1 |
/** |
| 2 |
* Text-Shadow reusable component. |
| 3 |
* |
| 4 |
*/ |
| 5 |
import { __ } from '@wordpress/i18n'; |
| 6 |
import Range from '@Components/range/Range.js'; |
| 7 |
import AdvancedPopColorControl from '../color-control/advanced-pop-color-control'; |
| 8 |
import { Button, Dashicon } from '@wordpress/components'; |
| 9 |
import { useState } from '@wordpress/element'; |
| 10 |
import React, { useLayoutEffect } from 'react'; |
| 11 |
import { select } from '@wordpress/data'; |
| 12 |
import getUAGEditorStateLocalStorage from '@Controls/getUAGEditorStateLocalStorage'; |
| 13 |
import { blocksAttributes } from '@Attributes/getBlocksDefaultAttributes'; |
| 14 |
|
| 15 |
const TextShadowControl = ( props ) => { |
| 16 |
const [ showAdvancedControls, toggleAdvancedControls ] = useState( false ); |
| 17 |
|
| 18 |
const { |
| 19 |
setAttributes, |
| 20 |
textShadowColor, |
| 21 |
textShadowHOffset, |
| 22 |
textShadowVOffset, |
| 23 |
textShadowBlur, |
| 24 |
label = __( 'Text Shadow', 'ultimate-addons-for-gutenberg' ), |
| 25 |
popup = false, |
| 26 |
blockId, |
| 27 |
} = props; |
| 28 |
|
| 29 |
let advancedControls; |
| 30 |
const activeClass = showAdvancedControls ? 'active' : ''; |
| 31 |
|
| 32 |
useLayoutEffect( () => { |
| 33 |
window.addEventListener( 'click', function ( e ) { |
| 34 |
const popupButton = document.querySelector( |
| 35 |
`.active.popup-${ blockId } .spectra-control-popup__options--action-button` |
| 36 |
); |
| 37 |
const popupWrap = document.querySelector( |
| 38 |
`.active.popup-${ blockId } .spectra-control-popup` |
| 39 |
); |
| 40 |
|
| 41 |
if ( |
| 42 |
popupButton && |
| 43 |
! popupButton?.contains( e.target ) && |
| 44 |
! e.target?.classList?.contains( |
| 45 |
'uagb-advanced-color-indicate' |
| 46 |
) && |
| 47 |
! e.target?.parentElement?.closest( '.uagb-popover-color' ) && |
| 48 |
popupWrap && |
| 49 |
! popupWrap?.contains( e.target ) && |
| 50 |
! e.target?.parentElement?.closest( '.uagb-reset' ) |
| 51 |
) { |
| 52 |
toggleAdvancedControls( false ); |
| 53 |
const blockName = getSelectedBlock()?.name; |
| 54 |
const uagSettingState = getUAGEditorStateLocalStorage( |
| 55 |
'uagSettingState' |
| 56 |
); |
| 57 |
|
| 58 |
const data = { |
| 59 |
...uagSettingState, |
| 60 |
[ blockName ]: { |
| 61 |
...uagSettingState?.[ blockName ], |
| 62 |
selectedSetting: false, |
| 63 |
}, |
| 64 |
}; |
| 65 |
|
| 66 |
const uagLocalStorage = getUAGEditorStateLocalStorage(); |
| 67 |
if ( uagLocalStorage ) { |
| 68 |
uagLocalStorage.setItem( |
| 69 |
'uagSettingState', |
| 70 |
JSON.stringify( data ) |
| 71 |
); |
| 72 |
} |
| 73 |
} |
| 74 |
} ); |
| 75 |
}, [] ); |
| 76 |
|
| 77 |
// Array of all the current Typography Control's Labels. |
| 78 |
const attributeNames = [ |
| 79 |
textShadowColor.label, |
| 80 |
textShadowHOffset.label, |
| 81 |
textShadowVOffset.label, |
| 82 |
textShadowBlur.label, |
| 83 |
]; |
| 84 |
|
| 85 |
const { getSelectedBlock } = select( 'core/block-editor' ); |
| 86 |
|
| 87 |
// Function to get the Block's default Text Shadow Values. |
| 88 |
const getBlockTextShadowValue = () => { |
| 89 |
const selectedBlockName = getSelectedBlock()?.name.split( '/' ).pop(); |
| 90 |
let defaultValues = false; |
| 91 |
if ( 'undefined' !== typeof blocksAttributes[ selectedBlockName ] ) { |
| 92 |
attributeNames.forEach( ( attributeName ) => { |
| 93 |
if ( attributeName ) { |
| 94 |
const blockDefaultAttributeValue = |
| 95 |
'undefined' !== |
| 96 |
typeof blocksAttributes[ selectedBlockName ][ |
| 97 |
attributeName |
| 98 |
]?.default |
| 99 |
? blocksAttributes[ selectedBlockName ][ |
| 100 |
attributeName |
| 101 |
]?.default |
| 102 |
: ''; |
| 103 |
defaultValues = { |
| 104 |
...defaultValues, |
| 105 |
[ attributeName ]: blockDefaultAttributeValue, |
| 106 |
}; |
| 107 |
} |
| 108 |
} ); |
| 109 |
} |
| 110 |
return defaultValues; |
| 111 |
}; |
| 112 |
|
| 113 |
// Function to check if any Text Shadow Setting has changed. |
| 114 |
const getUpdateState = () => { |
| 115 |
const defaultValues = getBlockTextShadowValue(); |
| 116 |
const selectedBlockAttributes = getSelectedBlock()?.attributes; |
| 117 |
let isTextShadowUpdated = false; |
| 118 |
attributeNames.forEach( ( attributeName ) => { |
| 119 |
if ( |
| 120 |
selectedBlockAttributes?.[ attributeName ] && |
| 121 |
selectedBlockAttributes?.[ attributeName ] !== |
| 122 |
defaultValues?.[ attributeName ] |
| 123 |
) { |
| 124 |
isTextShadowUpdated = true; |
| 125 |
} |
| 126 |
} ); |
| 127 |
return isTextShadowUpdated; |
| 128 |
}; |
| 129 |
|
| 130 |
// Flag to check if this control has been updated or not. |
| 131 |
const isTextShadowUpdated = popup && getUpdateState(); |
| 132 |
|
| 133 |
const overallControls = ( |
| 134 |
<> |
| 135 |
{ /* Shadow Color */ } |
| 136 |
<AdvancedPopColorControl |
| 137 |
label={ textShadowColor.title } |
| 138 |
colorValue={ textShadowColor.value } |
| 139 |
data={ { |
| 140 |
value: textShadowColor.value, |
| 141 |
label: textShadowColor.label, |
| 142 |
} } |
| 143 |
setAttributes={ setAttributes } |
| 144 |
/> |
| 145 |
{ /* Horizontal */ } |
| 146 |
<Range |
| 147 |
label={ textShadowHOffset.title } |
| 148 |
value={ textShadowHOffset.value } |
| 149 |
min={ -100 } |
| 150 |
max={ 100 } |
| 151 |
displayUnit={ false } |
| 152 |
setAttributes={ setAttributes } |
| 153 |
data={ { |
| 154 |
value: textShadowHOffset.value, |
| 155 |
label: textShadowHOffset.label, |
| 156 |
} } |
| 157 |
/> |
| 158 |
{ /* Vertical */ } |
| 159 |
<Range |
| 160 |
label={ textShadowVOffset.title } |
| 161 |
value={ textShadowVOffset.value } |
| 162 |
min={ -100 } |
| 163 |
max={ 100 } |
| 164 |
displayUnit={ false } |
| 165 |
setAttributes={ setAttributes } |
| 166 |
data={ { |
| 167 |
value: textShadowVOffset.value, |
| 168 |
label: textShadowVOffset.label, |
| 169 |
} } |
| 170 |
/> |
| 171 |
{ /* Blur */ } |
| 172 |
<Range |
| 173 |
label={ textShadowBlur.title } |
| 174 |
value={ textShadowBlur.value } |
| 175 |
min={ 0 } |
| 176 |
max={ 100 } |
| 177 |
displayUnit={ false } |
| 178 |
setAttributes={ setAttributes } |
| 179 |
data={ { |
| 180 |
value: textShadowBlur.value, |
| 181 |
label: textShadowBlur.label, |
| 182 |
} } |
| 183 |
/> |
| 184 |
</> |
| 185 |
); |
| 186 |
|
| 187 |
if ( showAdvancedControls ) { |
| 188 |
advancedControls = ( |
| 189 |
<div className="uagb-text-shadow-advanced spectra-control-popup"> |
| 190 |
{ overallControls } |
| 191 |
</div> |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
const textShadowAdvancedControls = ( |
| 196 |
<div className="spectra-control-popup__options--action-wrapper"> |
| 197 |
<span className="uag-control-label"> |
| 198 |
{ label } |
| 199 |
{ isTextShadowUpdated && ( |
| 200 |
<div className="spectra__change-indicator--dot-right" /> |
| 201 |
) } |
| 202 |
</span> |
| 203 |
<Button |
| 204 |
className="uag-text-shadow-button spectra-control-popup__options--action-button" |
| 205 |
aria-pressed={ showAdvancedControls } |
| 206 |
onClick={ () => { |
| 207 |
const allPopups = document.querySelectorAll( |
| 208 |
'.spectra-control-popup__options' |
| 209 |
); |
| 210 |
if ( allPopups && 0 < allPopups.length ) { |
| 211 |
for ( let i = 0; i < allPopups.length; i++ ) { |
| 212 |
const popupButton = allPopups[ i ]?.querySelector( |
| 213 |
'.spectra-control-popup__options.active .spectra-control-popup__options--action-button' |
| 214 |
); |
| 215 |
popupButton?.click(); |
| 216 |
} |
| 217 |
} |
| 218 |
toggleAdvancedControls( ! showAdvancedControls ); |
| 219 |
|
| 220 |
const blockName = getSelectedBlock()?.name; |
| 221 |
const uagSettingState = getUAGEditorStateLocalStorage( |
| 222 |
'uagSettingState' |
| 223 |
); |
| 224 |
let data = { |
| 225 |
...uagSettingState, |
| 226 |
[ blockName ]: { |
| 227 |
...uagSettingState?.[ blockName ], |
| 228 |
selectedSetting: '.uag-text-shadow-options', |
| 229 |
}, |
| 230 |
}; |
| 231 |
|
| 232 |
if ( showAdvancedControls ) { |
| 233 |
data = { |
| 234 |
...uagSettingState, |
| 235 |
[ blockName ]: { |
| 236 |
...uagSettingState?.[ blockName ], |
| 237 |
selectedSetting: false, |
| 238 |
}, |
| 239 |
}; |
| 240 |
} |
| 241 |
const uagLocalStorage = getUAGEditorStateLocalStorage(); |
| 242 |
if ( uagLocalStorage ) { |
| 243 |
uagLocalStorage.setItem( |
| 244 |
'uagSettingState', |
| 245 |
JSON.stringify( data ) |
| 246 |
); |
| 247 |
} |
| 248 |
} } |
| 249 |
> |
| 250 |
<Dashicon icon="edit" /> |
| 251 |
</Button> |
| 252 |
</div> |
| 253 |
); |
| 254 |
|
| 255 |
return popup ? ( |
| 256 |
<div |
| 257 |
className={ `components-base-control uag-text-shadow-options spectra-control-popup__options popup-${ blockId } ${ activeClass }` } |
| 258 |
> |
| 259 |
{ textShadowAdvancedControls } |
| 260 |
{ showAdvancedControls && advancedControls } |
| 261 |
</div> |
| 262 |
) : ( |
| 263 |
<>{ overallControls }</> |
| 264 |
); |
| 265 |
}; |
| 266 |
|
| 267 |
export default TextShadowControl; |
| 268 |
|