Add apply styles to all inputs button

[MAILPOET-2599]
This commit is contained in:
Rostislav Wolny
2020-03-06 10:05:38 +01:00
committed by Veljko V
parent 760aed407f
commit efdcae19c0
4 changed files with 39 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import MailPoet from 'mailpoet'; import MailPoet from 'mailpoet';
import { import {
Button,
ColorIndicator, ColorIndicator,
ColorPalette, ColorPalette,
Panel, Panel,
@@ -8,7 +9,7 @@ import {
RangeControl, RangeControl,
ToggleControl, ToggleControl,
} from '@wordpress/components'; } from '@wordpress/components';
import { useSelect } from '@wordpress/data'; import { useSelect, useDispatch } from '@wordpress/data';
import { partial } from 'underscore'; import { partial } from 'underscore';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
@@ -28,6 +29,8 @@ const InputStylesSettings = ({
[] []
); );
const { applyStylesToAllTextInputs } = useDispatch('mailpoet-form-editor');
const updateStyles = (property, value) => { const updateStyles = (property, value) => {
const updated = { ...localStyles }; const updated = { ...localStyles };
updated[property] = value; updated[property] = value;
@@ -108,6 +111,9 @@ const InputStylesSettings = ({
</div> </div>
</> </>
) : null} ) : null}
<Button isPrimary onClick={() => applyStylesToAllTextInputs(localStyles)}>
{MailPoet.I18n.t('formSettingsApplyToAll')}
</Button>
</PanelBody> </PanelBody>
</Panel> </Panel>
); );

View File

@@ -211,3 +211,10 @@ export function* blocksChangedInBlockEditor(blocks) {
blocks, blocks,
}; };
} }
export function* applyStylesToAllTextInputs(styles) {
yield {
type: 'APPLY_STYLES_TO_ALL_TEXT_INPUTS',
styles,
};
}

View File

@@ -18,6 +18,15 @@ const formatApiErrorMessage = (response) => {
return errorMessage; return errorMessage;
}; };
// Recursively apply callback on every block in blocks tree
const mapBlocks = (blocks, callback) => blocks.map((block) => {
const result = callback(block);
if (block.innerBlocks) {
result.innerBlocks = mapBlocks(block.innerBlocks, callback);
}
return result;
});
export default { export default {
SAVE_FORM() { SAVE_FORM() {
if (select('mailpoet-form-editor').getIsFormSaving()) { if (select('mailpoet-form-editor').getIsFormSaving()) {
@@ -132,6 +141,21 @@ export default {
}); });
}, },
APPLY_STYLES_TO_ALL_TEXT_INPUTS(actionData) {
const currentBlocks = select('mailpoet-form-editor').getFormBlocks();
const updatedBlocks = mapBlocks(currentBlocks, (block) => {
const updatedBlock = { ...block };
if (
['mailpoet-form/last-name-input', 'mailpoet-form/first-name-input', 'mailpoet-form/email-input'].includes(block.name)
|| block.name.startsWith('mailpoet-form/custom-text')
) {
updatedBlock.attributes.styles = actionData.styles;
}
return updatedBlock;
});
dispatch('core/block-editor').resetBlocks(updatedBlocks);
},
/** /**
* We want to ensure that email input and submit are always present. * We want to ensure that email input and submit are always present.
* @param actionData {{type: string, blocks: Object[]}} blocks property contains editor blocks * @param actionData {{type: string, blocks: Object[]}} blocks property contains editor blocks

View File

@@ -52,6 +52,7 @@
'formSettingsBorderSize': _x('Border Size', 'A label for checkbox in form style settings'), 'formSettingsBorderSize': _x('Border Size', 'A label for checkbox in form style settings'),
'formSettingsBorderRadius': _x('Border Radius', 'A label for checkbox in form style settings'), 'formSettingsBorderRadius': _x('Border Radius', 'A label for checkbox in form style settings'),
'formSettingsBorderColor': _x('Border Color', 'A label for checkbox in form style settings'), 'formSettingsBorderColor': _x('Border Color', 'A label for checkbox in form style settings'),
'formSettingsApplyToAll': __('Apply styles to all inputs'),
'customFieldSettings': _x('Custom field settings', 'A settings section heading'), 'customFieldSettings': _x('Custom field settings', 'A settings section heading'),
'customFieldsFormSettings': _x('Form settings', 'A settings section heading'), 'customFieldsFormSettings': _x('Form settings', 'A settings section heading'),
'formPlacement': _x('Form Placement', 'A settings section heading'), 'formPlacement': _x('Form Placement', 'A settings section heading'),