From 248ce49742d5be31ead54a51149e11afab6a4818 Mon Sep 17 00:00:00 2001 From: David Ichim Date: Fri, 28 Oct 2022 19:58:34 +0300 Subject: [PATCH] change(textAlign): modeled TextAlign widget to code from Align widget - Added clear selection to Align widget --- src/Widgets/Align.jsx | 13 +++++++- src/Widgets/TextAlign.jsx | 70 ++++++++++++++++++++++++++++++--------- 2 files changed, 67 insertions(+), 16 deletions(-) diff --git a/src/Widgets/Align.jsx b/src/Widgets/Align.jsx index 334509e..3c4d020 100644 --- a/src/Widgets/Align.jsx +++ b/src/Widgets/Align.jsx @@ -15,8 +15,9 @@ import imageFitSVG from '@plone/volto/icons/image-fit.svg'; import imageNarrowSVG from '@eeacms/volto-block-style/icons/image-narrow.svg'; import imageWideSVG from '@plone/volto/icons/image-wide.svg'; import imageFullSVG from '@plone/volto/icons/image-full.svg'; +import clearSVG from '@plone/volto/icons/clear.svg'; -const messages = defineMessages({ +export const messages = defineMessages({ left: { id: 'Left', defaultMessage: 'Left', @@ -41,6 +42,10 @@ const messages = defineMessages({ id: 'Full', defaultMessage: 'Full', }, + '': { + id: 'Clear selection', + defaultMessage: 'Clear selection', + }, }); export const defaultActionsInfo = ({ intl }) => ({ @@ -50,6 +55,7 @@ export const defaultActionsInfo = ({ intl }) => ({ narrow: [imageNarrowSVG, intl.formatMessage(messages.narrow)], wide: [imageWideSVG, intl.formatMessage(messages.wide)], full: [imageFullSVG, intl.formatMessage(messages.full)], + '': [clearSVG, intl.formatMessage(messages[''])], }); const AlignWidget = (props) => { @@ -69,6 +75,11 @@ const AlignWidget = (props) => { } }); + // add clear selection button to the actions mapping if it's not already present + if (actions[actions.length - 1] !== '') { + actions.push(''); + } + const actionsInfo = { ...defaultActionsInfo({ intl }), ...actionsInfoMap, diff --git a/src/Widgets/TextAlign.jsx b/src/Widgets/TextAlign.jsx index 88c9761..6319f1e 100644 --- a/src/Widgets/TextAlign.jsx +++ b/src/Widgets/TextAlign.jsx @@ -7,37 +7,77 @@ import alignRightSVG from '@plone/volto/icons/align-right.svg'; import alignJustifySVG from '@plone/volto/icons/align-justify.svg'; import alignCenterSVG from '@plone/volto/icons/align-center.svg'; import clearSVG from '@plone/volto/icons/clear.svg'; +import { defineMessages, useIntl } from 'react-intl'; -export const TEXT_ALIGN_VALUE_MAP = [ - ['left', alignLeftSVG, 'Left align'], - ['right', alignRightSVG, 'Right align'], - ['center', alignCenterSVG, 'Center align'], - ['justify', alignJustifySVG, 'Justify align'], -]; +export const messages = defineMessages({ + left: { + id: 'Left', + defaultMessage: 'Left', + }, + right: { + id: 'Right', + defaultMessage: 'Right', + }, + center: { + id: 'Center', + defaultMessage: 'Center', + }, + justify: { + id: 'Justify', + defaultMessage: 'Justify', + }, + '': { + id: 'Clear selection', + defaultMessage: 'Clear selection', + }, +}); +const defaultActionsInfo = ({ intl }) => ({ + left: [alignLeftSVG, intl.formatMessage(messages.left)], + right: [alignRightSVG, intl.formatMessage(messages.right)], + center: [alignCenterSVG, intl.formatMessage(messages.center)], + justify: [alignJustifySVG, intl.formatMessage(messages.justify)], + '': [clearSVG, intl.formatMessage(messages[''])], +}); export default (props) => { - const { value, onChange, id, actions = TEXT_ALIGN_VALUE_MAP } = props; + const intl = useIntl(); + const { + onChange, + id, + actions = ['left', 'right', 'center', 'justify'], + actionsInfoMap = {}, + value, + } = props; // add clear selection button to the actions mapping if it's not already present - if (actions[actions.length - 1][0] !== '') { - actions.push(['', clearSVG, 'Clear selection']); + if (actions[actions.length - 1] !== '') { + actions.push(''); } + const actionsInfo = { + ...defaultActionsInfo({ intl }), + ...actionsInfoMap, + }; + return (
- {actions.map(([name, icon, title], index) => ( - + {actions.map((action, index) => ( + ))}