Skip to content

Commit 53361ff

Browse files
Merge branch 'feat/3448-global-font-pairs' of https://github.com/gambitph/Stackable into feat/3448-global-font-pairs
# Conflicts: # src/components/font-pair-picker/index.js
2 parents 181791b + 2285c7d commit 53361ff

File tree

4 files changed

+70
-16
lines changed

4 files changed

+70
-16
lines changed

src/components/font-pair-picker/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import { omit } from 'lodash'
1717
import { __ } from '@wordpress/i18n'
1818

1919
const FontPairPicker = props => {
20-
const headingStyles = props.fontPair?.typography?.h1 || {}
21-
const paragraphStyles = props.fontPair?.typography?.p || {}
22-
if ( headingStyles.fontFamily ) {
20+
const headingStyles = props.fontPair.typography.h1
21+
const paragraphStyles = props.fontPair.typography.p
22+
if ( headingStyles?.fontFamily ) {
2323
loadGoogleFont( headingStyles.fontFamily )
2424
}
25-
if ( paragraphStyles.fontFamily ) {
25+
if ( paragraphStyles?.fontFamily ) {
2626
loadGoogleFont( paragraphStyles.fontFamily )
2727
}
2828

@@ -32,7 +32,7 @@ const FontPairPicker = props => {
3232
style={ omit( { ...headingStyles }, [ 'fontSize', 'lineHeight' ] ) }
3333
className="ugb-global-settings-font-pair__label"
3434
>
35-
{ headingStyles.fontFamily ? headingStyles.fontFamily : 'Theme Heading Default' }
35+
{ headingStyles?.fontFamily ? headingStyles.fontFamily : 'Theme Heading Default' }
3636
</span>
3737
<span
3838
style={ omit( { ...paragraphStyles }, [ 'fontSize', 'lineHeight' ] ) }
@@ -67,7 +67,6 @@ const FontPairPicker = props => {
6767
className="ugb-button-icon-control__edit"
6868
label={ __( 'Edit', i18n ) }
6969
icon="edit"
70-
isSmall
7170
onClick={ event => {
7271
props.onEdit()
7372
event.stopPropagation()

src/components/typography-control/index.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,7 @@ const TypographyControl = props => {
3535
label={ props.label }
3636
popoverLabel={ props.popoverLabel }
3737
onReset={ props.onReset }
38-
allowReset={
39-
props.fontFamily ||
40-
props.fontSize || props.tabletFontSize || props.mobileFontSize ||
41-
props.fontWeight ||
42-
props.textTransform ||
43-
props.lineHeight || props.tabletLineHeight || props.mobileLineHeight ||
44-
props.letterSpacing || props.tabletLetterSpacing || props.mobileLetterSpacing
45-
}
38+
allowReset={ props?.isAllowReset }
4639
resetPopoverTitle={ props.resetPopoverTitle }
4740
resetPopoverDescription={ props.resetPopoverDescription }
4841
className={ props.className }

src/plugins/global-settings/typography/index.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import {
1414
} from '~stackable/components'
1515
import { fetchSettings } from '~stackable/util'
1616
import { i18n, isPro } from 'stackable'
17-
import { omit, head } from 'lodash'
17+
import {
18+
omit, head, isEqual,
19+
} from 'lodash'
1820

1921
/**
2022
* WordPress dependencies
@@ -183,7 +185,17 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
183185
}
184186

185187
const resetStyles = selector => {
186-
const newSettings = omit( typographySettings, [ selector ] )
188+
let newSettings
189+
const currentFontPair = [ ...FONT_PAIRS, ...customFontPairs ].find( fontPair => fontPair.name === selectedFontPairName )
190+
if ( ! editingFontPairName ) {
191+
newSettings = { ...typographySettings, [ selector ]: currentFontPair.typography[ selector ] }
192+
} else {
193+
newSettings = omit( typographySettings, [ selector ] )
194+
const updatedCustomFontPairs = customFontPairs
195+
.map( fontPair => fontPair.name === editingFontPairName ? { ...fontPair, typography: newSettings } : fontPair )
196+
updateCustomFontPairs( updatedCustomFontPairs )
197+
}
198+
187199
doAction( 'stackable.global-settings.typography-update-global-styles', newSettings )
188200

189201
updateTypography( newSettings )
@@ -197,6 +209,45 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
197209
model.save()
198210
}
199211

212+
const getIsAllowReset = selector => {
213+
const currentFontPair = [ ...FONT_PAIRS, ...customFontPairs ].find( fontPair => fontPair.name === selectedFontPairName )
214+
const fontPairStyle = currentFontPair.typography[ selector ]
215+
const typographyStyle = typographySettings[ selector ]
216+
if ( ! editingFontPairName ) {
217+
if ( ! isEqual( fontPairStyle, typographyStyle ) ) {
218+
return true
219+
}
220+
return false
221+
} else if ( typographyStyle && ( typographyStyle.fontFamily ||
222+
typographyStyle.fontSize || typographyStyle.tabletFontSize || typographyStyle.mobileFontSize ||
223+
typographyStyle.fontWeight ||
224+
typographyStyle.textTransform ||
225+
typographyStyle.lineHeight || typographyStyle.tabletLineHeight || typographyStyle.mobileLineHeight ||
226+
typographyStyle.letterSpacing || typographyStyle.tabletLetterSpacing || typographyStyle.mobileLetterSpacing ) ) {
227+
return true
228+
}
229+
return false
230+
}
231+
232+
const getIsChangeConfirmed = () => {
233+
// No need to confirm when the current font pair is custom
234+
// since changes are saved
235+
if ( customFontPairs.find( fontPair => fontPair.name === selectedFontPairName ) ) {
236+
return true
237+
}
238+
239+
const isDirty = TYPOGRAPHY_TAGS.some( ( { selector } ) => {
240+
return getIsAllowReset( selector )
241+
} )
242+
243+
if ( isDirty ) {
244+
// eslint-disable-next-line no-alert
245+
const confirmChange = window.confirm( __( 'Changing font pair will override the previous changes. Do you want to proceed?', i18n ) )
246+
return confirmChange
247+
}
248+
return true
249+
}
250+
200251
const CustomFontPairPickers = applyFilters(
201252
'stackable.global-settings.typography.font-pairs.customPicker',
202253
Fragment,
@@ -255,6 +306,9 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
255306
fontPair={ FONT_PAIRS[ 0 ] }
256307
isSelected={ selectedFontPairName === FONT_PAIRS[ 0 ].name }
257308
onClick={ () => {
309+
if ( ! getIsChangeConfirmed() ) {
310+
return
311+
}
258312
updateSelectedFontPair( FONT_PAIRS[ 0 ].name )
259313
changeStyles( FONT_PAIRS[ 0 ].typography )
260314
} }
@@ -264,6 +318,9 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
264318
customFontPairs={ customFontPairs }
265319
selected={ selectedFontPairName }
266320
onClick={ ( name, typography ) => {
321+
if ( ! getIsChangeConfirmed() ) {
322+
return
323+
}
267324
updateSelectedFontPair( name )
268325
changeStyles( typography )
269326
} }
@@ -280,6 +337,9 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
280337
fontPair={ fontPair }
281338
isSelected={ selectedFontPairName === fontPair.name }
282339
onClick={ () => {
340+
if ( ! getIsChangeConfirmed() ) {
341+
return
342+
}
283343
updateSelectedFontPair( fontPair.name )
284344
changeStyles( fontPair.typography )
285345
} }
@@ -316,6 +376,7 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
316376
label={ label }
317377
selector={ selector }
318378
value={ ( typographySettings[ selector ] ) || {} }
379+
isAllowReset={ getIsAllowReset( selector ) }
319380
onChange={ styles => changeStyles( { [ selector ]: styles } ) }
320381
onReset={ () => resetStyles( selector ) }
321382
/>

src/plugins/global-settings/typography/typography-picker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const TypographyPicker = props => {
9494
onReset={ () => props.onReset( props.selector ) }
9595
resetPopoverTitle={ sprintf( __( 'Reset %s Global Typography Style', i18n ), props.selector === 'p' ? __( 'Body Text', i18n ) : props.selector.toUpperCase() ) }
9696
resetPopoverDescription={ __( 'Resetting this typography style will revert all typography to its original style. Proceed?', i18n ) }
97+
isAllowReset={ props?.isAllowReset }
9798
/>
9899
)
99100
}

0 commit comments

Comments
 (0)