@@ -12,7 +12,7 @@ import FREE_FONT_PAIRS from './font-pairs.json'
1212import {
1313 PanelAdvancedSettings , AdvancedSelectControl , ControlSeparator , FontPairPicker , ProControlButton ,
1414} from '~stackable/components'
15- import { fetchSettings } from '~stackable/util'
15+ import { fetchSettings , getDefaultFontSize } from '~stackable/util'
1616import {
1717 i18n , isPro , showProNotice ,
1818} from 'stackable'
@@ -29,41 +29,58 @@ import {
2929 addFilter , applyFilters , doAction ,
3030} from '@wordpress/hooks'
3131import { __ , sprintf } from '@wordpress/i18n'
32+ import { dispatch , useSelect } from '@wordpress/data'
3233
3334export { GlobalTypographyStyles }
3435
3536const TYPOGRAPHY_TAGS = [
3637 {
3738 label : sprintf ( __ ( 'Heading %d' , i18n ) , 1 ) ,
3839 selector : 'h1' ,
40+ presetName : '5XL' ,
41+ presetSlug : 'xxxxx-large' ,
3942 } ,
4043 {
4144 label : sprintf ( __ ( 'Heading %d' , i18n ) , 2 ) ,
4245 selector : 'h2' ,
46+ presetName : '4XL' ,
47+ presetSlug : 'xxxx-large' ,
4348 } ,
4449 {
4550 label : sprintf ( __ ( 'Heading %d' , i18n ) , 3 ) ,
4651 selector : 'h3' ,
52+ presetName : '3XL' ,
53+ presetSlug : 'xxx-large' ,
4754 } ,
4855 {
4956 label : sprintf ( __ ( 'Heading %d' , i18n ) , 4 ) ,
5057 selector : 'h4' ,
58+ presetName : '2XL' ,
59+ presetSlug : 'xx-large' ,
5160 } ,
5261 {
5362 label : sprintf ( __ ( 'Heading %d' , i18n ) , 5 ) ,
5463 selector : 'h5' ,
64+ presetName : 'XL' ,
65+ presetSlug : 'x-large' ,
5566 } ,
5667 {
5768 label : sprintf ( __ ( 'Heading %d' , i18n ) , 6 ) ,
5869 selector : 'h6' ,
70+ presetName : 'L' ,
71+ presetSlug : 'large' ,
5972 } ,
6073 {
6174 label : __ ( 'Body Text' , i18n ) ,
6275 selector : 'p' ,
76+ presetName : 'M' ,
77+ presetSlug : 'medium' ,
6378 } ,
6479 {
6580 label : __ ( 'Subtitle' , i18n ) ,
6681 selector : '.stk-subtitle' ,
82+ presetName : 'S' ,
83+ presetSlug : 'small' ,
6784 help : (
6885 < >
6986 { sprintf ( __ ( "To apply this typography style, just add `%s` in your block\'s Additional CSS classes. Also make sure that `%s` tag is set to avoid conflict with other typography styles" , i18n ) , 'stk-subtitle' , 'p' ) }
@@ -78,8 +95,15 @@ const TYPOGRAPHY_TAGS = [
7895let saveTypographyThrottle = null
7996let saveSelectedFontPairThrottle = null
8097let saveCustomFontPairsThrottle = null
98+ let saveTypographyAsPresetsThrottle = null
8199
82100addFilter ( 'stackable.global-settings.inspector' , 'stackable/global-typography' , output => {
101+ const { allCustomPresets, useTypographyAsPresets } = useSelect ( select => {
102+ const _customPresetControls = select ( 'stackable/global-preset-controls.custom' ) ?. getCustomPresetControls ( ) ?? { }
103+ const _useTypographyAsPresets = select ( 'stackable/global-preset-controls.custom' ) ?. getUseTypographyAsPresets ( ) ?? false
104+ return { allCustomPresets : { ..._customPresetControls } , useTypographyAsPresets : { ..._useTypographyAsPresets } }
105+ } , [ ] )
106+
83107 const FONT_PAIRS = applyFilters ( 'stackable.global-settings.typography.font-pairs.premium-font-pairs' , FREE_FONT_PAIRS )
84108
85109 const [ isPanelOpen , setIsPanelOpen ] = useState ( false )
@@ -104,6 +128,49 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
104128 // When typography styles are changed, trigger our editor style generator to update.
105129 useEffect ( ( ) => {
106130 doAction ( 'stackable.global-settings.typography.update-trigger' , typographySettings , applySettingsTo )
131+
132+ if ( useTypographyAsPresets ) {
133+ const fontSizePresets = TYPOGRAPHY_TAGS
134+ . filter ( ( { presetSlug } ) => ! ! presetSlug )
135+ . map ( ( {
136+ selector, presetName, presetSlug,
137+ } ) => {
138+ const size = typographySettings [ selector ] ?. fontSize ?? getDefaultFontSize ( selector ) ?? 16
139+ const unit = typographySettings [ selector ] ?. fontSizeUnit ?? 'px'
140+ return {
141+ name : presetName ,
142+ slug : presetSlug ,
143+ size : `${ size } ${ unit } ` ,
144+ }
145+ } )
146+ // Add the preset for extra small
147+ let xSmallSize = typographySettings [ '.stk-subtitle' ] ?. fontSize ?? getDefaultFontSize ( '.stk-subtitle' ) ?? 16
148+ let xSmallUnit = typographySettings [ '.stk-subtitle' ] ?. fontSizeUnit ?? 'px'
149+ if ( xSmallUnit === 'px' ) {
150+ xSmallSize = Math . pow ( xSmallSize / 16 , 2 )
151+ xSmallUnit = 'rem'
152+ } else {
153+ xSmallSize = Math . pow ( xSmallSize , 2 )
154+ }
155+
156+ fontSizePresets . push ( {
157+ name : 'XS' ,
158+ slug : 'x-small' ,
159+ size : `${ xSmallSize } ${ xSmallUnit ?? 'px' } ` ,
160+ } )
161+ // Reverse the presets so it's from smallest to biggest
162+ fontSizePresets . reverse ( )
163+
164+ const newSettings = { ...allCustomPresets , fontSizes : fontSizePresets }
165+
166+ clearTimeout ( saveTypographyAsPresetsThrottle )
167+ saveTypographyAsPresetsThrottle = setTimeout ( ( ) => {
168+ const settings = new models . Settings ( { stackable_global_custom_preset_controls : newSettings } ) // eslint-disable-line camelcase
169+ settings . save ( )
170+ } , 300 )
171+
172+ dispatch ( 'stackable/global-preset-controls.custom' ) . updateCustomPresetControls ( newSettings )
173+ }
107174 } , [ JSON . stringify ( typographySettings ) , applySettingsTo ] )
108175
109176 // Scroll to the selected font pair when Global Typography tab is toggled
0 commit comments