Skip to content

Commit

Permalink
Encode exisint values and narrow down the features
Browse files Browse the repository at this point in the history
  • Loading branch information
t-hamano committed Jul 17, 2023
1 parent 33bf984 commit c209e7f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,24 @@ export default function DimensionsPanel( {
// in global styles but not in block inspector.
includeLayoutControls = false,
} ) {
const { dimensions, spacing } = settings;

const decodeValue = ( rawValue ) => {
if ( rawValue && typeof rawValue === 'object' ) {
return Object.keys( rawValue ).reduce( ( acc, key ) => {
acc[ key ] = getValueFromVariable(
{ settings },
{ settings: { dimensions, spacing } },
'',
rawValue[ key ]
);
return acc;
}, {} );
}
return getValueFromVariable( { settings }, '', rawValue );
return getValueFromVariable(
{ settings: { dimensions, spacing } },
'',
rawValue
);
};

const showSpacingPresetsControl = useHasSpacingPresets( settings );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
* Internal dependencies
*/
import SpacingInputControl from './spacing-input-control';
import { LABELS, ICONS, hasAxisSupport } from '../utils';
import {
LABELS,
ICONS,
getPresetValueFromCustomValue,
hasAxisSupport,
} from '../utils';

const groupedSides = [ 'vertical', 'horizontal' ];

Expand All @@ -20,7 +25,17 @@ export default function AxialInputControls( {
if ( ! onChange ) {
return;
}
const nextValues = { ...values };

// Encode the existing value into the preset value if the passed in value matches the value of one of the spacingSizes.
const nextValues = {
...Object.keys( values ).reduce( ( acc, key ) => {
acc[ key ] = getPresetValueFromCustomValue(
values[ key ],
spacingSizes
);
return acc;
}, {} ),
};

if ( side === 'vertical' ) {
nextValues.top = next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
* Internal dependencies
*/
import SpacingInputControl from './spacing-input-control';
import { ALL_SIDES, LABELS, ICONS } from '../utils';
import {
ALL_SIDES,
LABELS,
ICONS,
getPresetValueFromCustomValue,
} from '../utils';

export default function SeparatedInputControls( {
minimumCustomValue,
Expand All @@ -20,7 +25,17 @@ export default function SeparatedInputControls( {
: ALL_SIDES;

const createHandleOnChange = ( side ) => ( next ) => {
const nextValues = { ...values };
// Encode the existing value into the preset value if the passed in value matches the value of one of the spacingSizes.
const nextValues = {
...Object.keys( values ).reduce( ( acc, key ) => {
acc[ key ] = getPresetValueFromCustomValue(
values[ key ],
spacingSizes
);
return acc;
}, {} ),
};

nextValues[ side ] = next;

onChange( nextValues );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import SpacingInputControl from './spacing-input-control';
import { LABELS } from '../utils';
import { LABELS, getPresetValueFromCustomValue } from '../utils';

export default function SingleInputControl( {
minimumCustomValue,
Expand All @@ -16,7 +16,17 @@ export default function SingleInputControl( {
values,
} ) {
const createHandleOnChange = ( currentSide ) => ( next ) => {
const nextValues = { ...values };
// Encode the existing value into the preset value if the passed in value matches the value of one of the spacingSizes.
const nextValues = {
...Object.keys( values ).reduce( ( acc, key ) => {
acc[ key ] = getPresetValueFromCustomValue(
values[ key ],
spacingSizes
);
return acc;
}, {} ),
};

nextValues[ currentSide ] = next;

onChange( nextValues );
Expand Down

0 comments on commit c209e7f

Please sign in to comment.