Update email theme via dimensions-panel
[MAILPOET-5640]
This commit is contained in:
@@ -10,6 +10,10 @@ import {
|
|||||||
__experimentalUseCustomUnits as useCustomUnits,
|
__experimentalUseCustomUnits as useCustomUnits,
|
||||||
} from '@wordpress/components';
|
} from '@wordpress/components';
|
||||||
import { __ } from '@wordpress/i18n';
|
import { __ } from '@wordpress/i18n';
|
||||||
|
import { useEntityProp } from '@wordpress/core-data';
|
||||||
|
import { useDispatch, useSelect } from '@wordpress/data';
|
||||||
|
import { isEqual } from 'lodash';
|
||||||
|
import { EmailStyles, storeName } from '../../store';
|
||||||
|
|
||||||
export function DimensionsPanel() {
|
export function DimensionsPanel() {
|
||||||
const availableUnits = useSetting('spacing.units') as string[];
|
const availableUnits = useSetting('spacing.units') as string[];
|
||||||
@@ -17,27 +21,96 @@ export function DimensionsPanel() {
|
|||||||
availableUnits,
|
availableUnits,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const [mailpoetEmailData] = useEntityProp(
|
||||||
|
'postType',
|
||||||
|
'mailpoet_email',
|
||||||
|
'mailpoet_data',
|
||||||
|
);
|
||||||
|
|
||||||
|
const { styles } = useSelect((select) => ({
|
||||||
|
styles: select(storeName).getStyles(),
|
||||||
|
}));
|
||||||
|
const defaultPadding = styles.spacing.padding ?? undefined;
|
||||||
|
const defaultBlockGap = styles.spacing.blockGap ?? undefined;
|
||||||
|
|
||||||
|
const { updateEmailMailPoetTheme } = useDispatch(storeName);
|
||||||
|
|
||||||
// Padding
|
// Padding
|
||||||
const paddingValues = {
|
const paddingValues =
|
||||||
top: '20px',
|
mailpoetEmailData.theme?.styles?.spacing?.padding ?? defaultPadding;
|
||||||
right: '20px',
|
const resetPadding = () => {
|
||||||
bottom: '20px',
|
void updateEmailMailPoetTheme({
|
||||||
left: '20px',
|
...mailpoetEmailData.theme,
|
||||||
|
styles: {
|
||||||
|
...mailpoetEmailData.theme?.styles,
|
||||||
|
spacing: {
|
||||||
|
...mailpoetEmailData.theme?.styles?.spacing,
|
||||||
|
padding: defaultPadding ?? undefined,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as EmailStyles);
|
||||||
|
};
|
||||||
|
const setPaddingValues = (value) => {
|
||||||
|
void updateEmailMailPoetTheme({
|
||||||
|
...mailpoetEmailData.theme,
|
||||||
|
styles: {
|
||||||
|
...mailpoetEmailData.theme?.styles,
|
||||||
|
spacing: {
|
||||||
|
...mailpoetEmailData.theme?.styles?.spacing,
|
||||||
|
padding: value,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as EmailStyles);
|
||||||
};
|
};
|
||||||
const resetPadding = () => {};
|
|
||||||
const setPaddingValues = () => {};
|
|
||||||
|
|
||||||
// Block spacing
|
// Block spacing
|
||||||
const blockGapValue = '16px';
|
const blockGapValue =
|
||||||
const resetBlockGap = () => {};
|
mailpoetEmailData.theme?.styles?.spacing?.blockGap ?? defaultBlockGap;
|
||||||
const setBlockGapValue = () => {};
|
const resetBlockGap = () => {
|
||||||
const resetAll = () => {};
|
void updateEmailMailPoetTheme({
|
||||||
|
...mailpoetEmailData.theme,
|
||||||
|
styles: {
|
||||||
|
...mailpoetEmailData.theme?.styles,
|
||||||
|
spacing: {
|
||||||
|
...styles.spacing,
|
||||||
|
blockGap: undefined,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as EmailStyles);
|
||||||
|
};
|
||||||
|
|
||||||
|
const setBlockGapValue = (value) => {
|
||||||
|
void updateEmailMailPoetTheme({
|
||||||
|
...mailpoetEmailData.theme,
|
||||||
|
styles: {
|
||||||
|
...mailpoetEmailData.theme?.styles,
|
||||||
|
spacing: {
|
||||||
|
...mailpoetEmailData.theme?.styles?.spacing,
|
||||||
|
blockGap: value.top || styles.spacing.blockGap,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as EmailStyles);
|
||||||
|
};
|
||||||
|
|
||||||
|
const resetAll = () => {
|
||||||
|
void updateEmailMailPoetTheme({
|
||||||
|
...mailpoetEmailData.theme,
|
||||||
|
styles: {
|
||||||
|
...mailpoetEmailData.theme?.styles,
|
||||||
|
spacing: {
|
||||||
|
...styles.spacing,
|
||||||
|
padding: defaultPadding ?? undefined,
|
||||||
|
blockGap: defaultBlockGap ?? undefined,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as EmailStyles);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ToolsPanel label={__('Dimensions', 'mailpoet')} resetAll={resetAll}>
|
<ToolsPanel label={__('Dimensions', 'mailpoet')} resetAll={resetAll}>
|
||||||
<ToolsPanelItem
|
<ToolsPanelItem
|
||||||
isShownByDefault
|
isShownByDefault
|
||||||
hasValue={() => true}
|
hasValue={() => !isEqual(paddingValues, defaultPadding)}
|
||||||
label={__('Padding')}
|
label={__('Padding')}
|
||||||
onDeselect={() => resetPadding()}
|
onDeselect={() => resetPadding()}
|
||||||
className="tools-panel-item-spacing"
|
className="tools-panel-item-spacing"
|
||||||
@@ -54,7 +127,7 @@ export function DimensionsPanel() {
|
|||||||
<ToolsPanelItem
|
<ToolsPanelItem
|
||||||
isShownByDefault
|
isShownByDefault
|
||||||
label={__('Block spacing')}
|
label={__('Block spacing')}
|
||||||
hasValue={() => true}
|
hasValue={() => blockGapValue !== defaultBlockGap}
|
||||||
onDeselect={() => resetBlockGap()}
|
onDeselect={() => resetBlockGap()}
|
||||||
className="tools-panel-item-spacing"
|
className="tools-panel-item-spacing"
|
||||||
>
|
>
|
||||||
|
Reference in New Issue
Block a user