Skip to content

Commit 962b841

Browse files
committed
feat: Use Global Typography sizes as preset
1 parent 5d22e5f commit 962b841

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

src/block-components/typography/edit.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from '@wordpress/element'
3131
import { __ } from '@wordpress/i18n'
3232
import { applyFilters } from '@wordpress/hooks'
33+
import { useSelect } from '@wordpress/data'
3334

3435
const TYPOGRAPHY_SHADOWS = [
3536
'none',
@@ -80,6 +81,10 @@ export const Controls = props => {
8081
const attributeName = getAttrNameFunction( attrNameTemplate )
8182
const text = getAttribute( 'text' )
8283
const [ debouncedText, setDebouncedText ] = useState( text )
84+
const { useTypographyAsPresets } = useSelect( select => {
85+
const _useTypographyAsPresets = select( 'stackable/global-preset-controls.custom' )?.getUseTypographyAsPresets() ?? false
86+
return { useTypographyAsPresets: { ..._useTypographyAsPresets } }
87+
}, [] )
8388

8489
useEffect( () => {
8590
if ( text !== debouncedText ) {
@@ -100,7 +105,7 @@ export const Controls = props => {
100105

101106
const onChangeContent = useCallback( text => setDebouncedText( escapeHTMLIfInvalid( text ) ), [] )
102107

103-
const presetMarks = usePresetControls( 'fontSizes' )?.getPresetMarks() || null
108+
const presetMarks = usePresetControls( 'fontSizes' )?.getPresetMarks( useTypographyAsPresets ) || null
104109

105110
return (
106111
<>

src/global-settings.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,18 @@ public function register_global_settings() {
412412
)
413413
);
414414

415+
register_setting(
416+
'stackable_global_settings',
417+
'stackable_use_typography_as_presets',
418+
array(
419+
'type' => 'boolean',
420+
'description' => __( 'Use Global Typography font sizes as preset', STACKABLE_I18N ),
421+
'sanitize_callback' => 'sanitize_text_field',
422+
'show_in_rest' => true,
423+
'default' => '',
424+
)
425+
);
426+
415427
register_setting(
416428
'stackable_global_settings',
417429
'stackable_icon_library',

src/hooks/use-preset-controls.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ export const usePresetControls = property => {
5555
}
5656

5757
// Get the merge preset marks with the CSS Variable value
58-
const getPresetMarks = () => {
58+
// Setting customOnly to true returns the preset marks for custom presets only
59+
const getPresetMarks = ( customOnly = false ) => {
5960
const prefix = PRESET_MAPPING[ property ].prefix
60-
const mergedPresets = getMergedPresets()
61+
const presets = customOnly ? allCustomPresets[ property ] ?? [] : getMergedPresets()
6162

62-
return mergedPresets
63+
return presets
6364
.filter( preset => ! ( preset?.isDiscarded ) )
6465
.map( preset => ( {
6566
...preset,

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import FREE_FONT_PAIRS from './font-pairs.json'
1212
import {
1313
PanelAdvancedSettings, AdvancedSelectControl, ControlSeparator, FontPairPicker, ProControlButton,
1414
} from '~stackable/components'
15-
import { fetchSettings } from '~stackable/util'
15+
import { fetchSettings, getDefaultFontSize } from '~stackable/util'
1616
import {
1717
i18n, isPro, showProNotice,
1818
} from 'stackable'
@@ -29,41 +29,58 @@ import {
2929
addFilter, applyFilters, doAction,
3030
} from '@wordpress/hooks'
3131
import { __, sprintf } from '@wordpress/i18n'
32+
import { dispatch, useSelect } from '@wordpress/data'
3233

3334
export { GlobalTypographyStyles }
3435

3536
const 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 = [
7895
let saveTypographyThrottle = null
7996
let saveSelectedFontPairThrottle = null
8097
let saveCustomFontPairsThrottle = null
98+
let saveTypographyAsPresetsThrottle = null
8199

82100
addFilter( '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

Comments
 (0)