Skip to content

Commit 3b6e30e

Browse files
committed
feat: add use size preset by default option
1 parent 43159bb commit 3b6e30e

File tree

4 files changed

+81
-14
lines changed

4 files changed

+81
-14
lines changed

src/components/advanced-range-control/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { useControlHandlers } from '../base-control2/hooks'
66
import AdvancedControl, { extractControlProps } from '../base-control2'
77
import DynamicContentControl, { useDynamicContentControlProps } from '../dynamic-content-control'
88
import { ResetButton } from '../base-control2/reset-button'
9+
/**
10+
* External dependencies
11+
*/
12+
import { isEqual } from 'lodash'
913
import {
1014
useAttributeName,
1115
useBlockAttributesContext,
@@ -14,11 +18,7 @@ import {
1418
useDeviceType,
1519
} from '~stackable/hooks'
1620
import { extractNumbersAndUnits, getCSSVarName } from '~stackable/util'
17-
18-
/**
19-
* External dependencies
20-
*/
21-
import { isEqual } from 'lodash'
21+
import { settings as stackableSettings } from 'stackable'
2222

2323
/**
2424
* WordPress dependencies
@@ -57,6 +57,8 @@ const AdvancedRangeControl = props => {
5757
? ( props.unit || props.units?.[ 0 ] || 'px' )
5858
: ( unitAttribute || '' )
5959

60+
const isMarkModeDefault = !! ( stackableSettings?.stackable_use_size_presets_by_default ?? true )
61+
6062
// Change the min, max & step values depending on the unit used.
6163
if ( hasUnits ) {
6264
const i = props.units.indexOf( unit ) < 0 ? 0 : props.units.indexOf( unit )
@@ -109,7 +111,8 @@ const AdvancedRangeControl = props => {
109111

110112
// Is value at first render the same as a step value? If so, do mark mode
111113
// at the start, or show custom
112-
let isMarkValue = !! props.marks
114+
// If no initial value, use the given default from the settings
115+
let isMarkValue = !! props.marks && isMarkModeDefault
113116
if ( props.marks && derivedValue ) {
114117
// Check if the current value exists in the marks only by their CSS variable name
115118
// to match in case the fallback size changes.
@@ -285,6 +288,7 @@ AdvancedRangeControl.defaultProps = {
285288
marks: undefined, // [{ value: 'var(--stk-preset-font-size-small', name: 'S' }]
286289
allowCustom: true,
287290
isCustomPreset: false,
291+
isMarkModeDefault: true,
288292
}
289293

290294
export default memo( AdvancedRangeControl, isEqual )

src/components/four-range-control/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { dispatch } from '@wordpress/data'
3535
*/
3636
import { isEqual } from 'lodash'
3737
import classnames from 'classnames'
38-
import { i18n } from 'stackable'
38+
import { i18n, settings as stackableSettings } from 'stackable'
3939
import { Button } from '~stackable/components'
4040
import {
4141
useAttributeName,
@@ -122,6 +122,8 @@ const FourRangeControl = memo( props => {
122122
}
123123
} )
124124

125+
const isMarkModeDefault = !! ( stackableSettings?.stackable_use_size_presets_by_default ?? true )
126+
125127
// Change the min, max & step values depending on the unit used.
126128
if ( hasUnits ) {
127129
const i = props.units.indexOf( unit ) < 0 ? 0 : props.units.indexOf( unit )
@@ -187,12 +189,13 @@ const FourRangeControl = memo( props => {
187189

188190
// Is value at first render the same as a step value? If so, do mark mode
189191
// at the start, or show custom
192+
// If no initial value, use the given default from the settings
190193
const isMarkValue = {
191-
first: !! props.marks,
192-
top: !! props.marks,
193-
right: !! props.marks,
194-
bottom: !! props.marks,
195-
left: !! props.marks,
194+
first: !! props.marks && isMarkModeDefault,
195+
top: !! props.marks && isMarkModeDefault,
196+
right: !! props.marks && isMarkModeDefault,
197+
bottom: !! props.marks && isMarkModeDefault,
198+
left: !! props.marks && isMarkModeDefault,
196199
}
197200
if ( props.marks && firstValue ) {
198201
// Check if the current value exists in the marks only by their CSS variable name

src/plugins/global-settings/preset-controls/index.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,54 @@ class Stackable_Size_And_Spacing_Preset_Controls {
4242
* Initialize
4343
*/
4444
function __construct() {
45+
add_action( 'register_stackable_global_settings', array( $this, 'register_use_size_presets_by_default' ) );
46+
add_action( 'stackable_early_version_upgraded', array( $this, 'use_size_presets_by_default_set_default' ), 10, 2 );
47+
add_filter( 'stackable_js_settings', array( $this, 'add_setting' ) );
48+
4549
add_filter( 'stackable_inline_styles_nodep', array( $this, 'add_preset_controls_styles' ) );
4650
add_filter( 'stackable_inline_editor_styles', array( $this, 'add_preset_controls_styles' ) );
4751
}
4852

53+
// Register the setting for using presets by default
54+
function register_use_size_presets_by_default() {
55+
register_setting(
56+
'stackable_global_settings',
57+
'stackable_use_size_presets_by_default',
58+
array(
59+
'type' => 'boolean',
60+
'description' => __( 'If enabled, range controls will be on preset mode by default', STACKABLE_I18N ),
61+
'sanitize_callback' => 'sanitize_text_field',
62+
'show_in_rest' => true,
63+
'default' => true,
64+
)
65+
);
66+
}
67+
68+
/**
69+
* When upgrading to v3.16.0 and above, set option to false.
70+
* If new installation, set option to true.
71+
*
72+
* @since 3.16.0
73+
*/
74+
public function use_size_presets_by_default_set_default( $old_version, $new_version ) {
75+
if ( ! empty( $old_version ) && version_compare( $old_version, "3.16.0", "<" ) ) {
76+
if ( ! get_option( 'stackable_use_size_presets_by_default' ) ) {
77+
update_option( 'stackable_use_size_presets_by_default', '' );
78+
}
79+
}
80+
}
81+
82+
// Make the setting available in the editor
83+
public function add_setting( $settings ) {
84+
$settings['stackable_use_size_presets_by_default'] = get_option( 'stackable_use_size_presets_by_default' );
85+
return $settings;
86+
}
87+
88+
/**
89+
* Loads the different preset values.
90+
*
91+
* @return void
92+
*/
4993
public function load_presets() {
5094
$this->custom_presets = get_option( 'stackable_global_custom_preset_controls' );
5195
$this->theme_presets = WP_Theme_JSON_Resolver::get_theme_data()->get_settings();
@@ -57,6 +101,12 @@ public static function sanitize_array_setting( $input ) {
57101
return ! is_array( $input ) ? array( array() ) : $input;
58102
}
59103

104+
/**
105+
* Loads and decodes a JSON file, returning the settings array if available.
106+
*
107+
* @param string $json_path Absolute path to the JSON file.
108+
* @return array The settings array from the decoded JSON file, or an empty array.
109+
*/
60110
private function load_json_file( $json_path ) {
61111
if ( file_exists( $json_path ) ) {
62112
$decoded_data = wp_json_file_decode( $json_path, [

src/welcome/admin.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ const SEARCH_TREE = [
7777
children: [
7878
__( 'Nested Block Width', i18n ),
7979
__( 'Nested Wide Block Width', i18n ),
80-
__( 'Stackable Text as Default Block', i18n ),
8180
],
8281
},
8382
{
8483
id: 'editor',
8584
children: [
85+
__( 'Stackable Text as Default Block', i18n ),
8686
__( 'Design Library', i18n ),
8787
__( 'Stackable Settings', i18n ),
8888
__( 'Block Linking (Beta)', i18n ),
@@ -155,6 +155,7 @@ const SEARCH_TREE = [
155155
id: 'global-settings',
156156
children: [
157157
__( 'Force Typography Styles', i18n ),
158+
__( 'Use Size Presets by Default', i18n ),
158159
],
159160
},
160161
],
@@ -672,7 +673,7 @@ const EditorSettings = props => {
672673
<p className="s-settings-subtitle">{ __( 'You can customize some of the features and behavior of Stackable in the editor here.' ) } </p>
673674
<AdminToggleSetting
674675
label={ __( 'Stackable Text as Default Block', i18n ) }
675-
searchedSettings={ blocks.children }
676+
searchedSettings={ editor.children }
676677
value={ settings.stackable_enable_text_default_block }
677678
onChange={ value => {
678679
handleSettingsChange( { stackable_enable_text_default_block: value } ) // eslint-disable-line camelcase
@@ -1162,6 +1163,15 @@ const GlobalSettings = props => {
11621163
disabled={ __( 'Not forced', i18n ) }
11631164
enabled={ __( 'Force styles', i18n ) }
11641165
/>
1166+
<AdminToggleSetting
1167+
label={ __( 'Use Size Presets by Default', i18n ) }
1168+
searchedSettings={ globalSettings.children }
1169+
value={ props.settings.stackable_use_size_presets_by_default }
1170+
onChange={ value => {
1171+
props.handleSettingsChange( { stackable_use_size_presets_by_default: value } ) // eslint-disable-line camelcase
1172+
} }
1173+
help={ __( 'If enabled, range controls will be on preset mode by default.', i18n ) }
1174+
/>
11651175
</div>
11661176
}
11671177
</>

0 commit comments

Comments
 (0)