Refactor RichTextWithButton component and ensure it is reusable
We also removed the usage of mailpoet_data MAILPOET-6431
This commit is contained in:
committed by
Oluwaseun Olorunsola
parent
4b917dd1b3
commit
aa17e90365
@ -1,10 +1,10 @@
|
|||||||
/**
|
/**
|
||||||
* External dependencies
|
* External dependencies
|
||||||
*/
|
*/
|
||||||
|
import type { ReactNode } from 'react';
|
||||||
import { BaseControl, Button } from '@wordpress/components';
|
import { BaseControl, Button } from '@wordpress/components';
|
||||||
import { useDispatch, useSelect } from '@wordpress/data';
|
import { useSelect } from '@wordpress/data';
|
||||||
import { useCallback, useRef, useState } from '@wordpress/element';
|
import { useCallback, useRef, useState } from '@wordpress/element';
|
||||||
import { useEntityProp } from '@wordpress/core-data';
|
|
||||||
import { create, insert, toHTMLString } from '@wordpress/rich-text';
|
import { create, insert, toHTMLString } from '@wordpress/rich-text';
|
||||||
import { RichText } from '@wordpress/block-editor';
|
import { RichText } from '@wordpress/block-editor';
|
||||||
import { __ } from '@wordpress/i18n';
|
import { __ } from '@wordpress/i18n';
|
||||||
@ -17,25 +17,32 @@ import {
|
|||||||
getCursorPosition,
|
getCursorPosition,
|
||||||
replacePersonalizationTagsWithHTMLComments,
|
replacePersonalizationTagsWithHTMLComments,
|
||||||
} from './rich-text-utils';
|
} from './rich-text-utils';
|
||||||
import { storeName, editorCurrentPostType } from '../../store';
|
import { storeName } from '../../store';
|
||||||
import { PersonalizationTagsPopover } from './personalization-tags-popover';
|
import { PersonalizationTagsPopover } from './personalization-tags-popover';
|
||||||
import { recordEvent, recordEventOnce } from '../../events';
|
import { recordEvent, recordEventOnce } from '../../events';
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
label: string;
|
||||||
|
labelSuffix?: ReactNode;
|
||||||
|
help?: ReactNode;
|
||||||
|
placeholder: string;
|
||||||
|
attributeName: string;
|
||||||
|
attributeValue?: string;
|
||||||
|
updateProperty: (
|
||||||
|
theAttributeName: string,
|
||||||
|
theUpdatedValue: string
|
||||||
|
) => void;
|
||||||
|
};
|
||||||
|
|
||||||
export function RichTextWithButton( {
|
export function RichTextWithButton( {
|
||||||
label,
|
label,
|
||||||
labelSuffix,
|
labelSuffix,
|
||||||
help,
|
help,
|
||||||
placeholder,
|
placeholder,
|
||||||
attributeName,
|
attributeName,
|
||||||
} ) {
|
attributeValue,
|
||||||
const [ mailpoetEmailData ] = useEntityProp(
|
updateProperty = () => {},
|
||||||
'postType',
|
}: Props ) {
|
||||||
editorCurrentPostType,
|
|
||||||
'mailpoet_data'
|
|
||||||
);
|
|
||||||
|
|
||||||
const { updateEmailMailPoetProperty } = useDispatch( storeName );
|
|
||||||
|
|
||||||
const [ selectionRange, setSelectionRange ] = useState( null );
|
const [ selectionRange, setSelectionRange ] = useState( null );
|
||||||
const [ isModalOpened, setIsModalOpened ] = useState( false );
|
const [ isModalOpened, setIsModalOpened ] = useState( false );
|
||||||
const list = useSelect(
|
const list = useSelect(
|
||||||
@ -61,11 +68,11 @@ export function RichTextWithButton( {
|
|||||||
const updatedValue = toHTMLString( { value: richTextValue } );
|
const updatedValue = toHTMLString( { value: richTextValue } );
|
||||||
|
|
||||||
// Update the corresponding property
|
// Update the corresponding property
|
||||||
updateEmailMailPoetProperty( attributeName, updatedValue );
|
updateProperty( attributeName, updatedValue );
|
||||||
|
|
||||||
setSelectionRange( null );
|
setSelectionRange( null );
|
||||||
},
|
},
|
||||||
[ attributeName, updateEmailMailPoetProperty ]
|
[ attributeName, updateProperty ]
|
||||||
);
|
);
|
||||||
|
|
||||||
const finalLabel = (
|
const finalLabel = (
|
||||||
@ -90,7 +97,7 @@ export function RichTextWithButton( {
|
|||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( ! mailpoetEmailData ) {
|
if ( ! attributeValue ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,7 +114,7 @@ export function RichTextWithButton( {
|
|||||||
onInsert={ ( value ) => {
|
onInsert={ ( value ) => {
|
||||||
handleInsertPersonalizationTag(
|
handleInsertPersonalizationTag(
|
||||||
value,
|
value,
|
||||||
mailpoetEmailData[ attributeName ] ?? '',
|
attributeValue ?? '',
|
||||||
selectionRange
|
selectionRange
|
||||||
);
|
);
|
||||||
setIsModalOpened( false );
|
setIsModalOpened( false );
|
||||||
@ -125,17 +132,13 @@ export function RichTextWithButton( {
|
|||||||
<PersonalizationTagsPopover
|
<PersonalizationTagsPopover
|
||||||
contentRef={ richTextRef }
|
contentRef={ richTextRef }
|
||||||
onUpdate={ ( originalTag, updatedTag ) => {
|
onUpdate={ ( originalTag, updatedTag ) => {
|
||||||
const currentValue =
|
const currentValue = attributeValue ?? '';
|
||||||
mailpoetEmailData[ attributeName ] ?? '';
|
|
||||||
// When we update the tag, we need to add brackets to the tag, because the popover removes them
|
// When we update the tag, we need to add brackets to the tag, because the popover removes them
|
||||||
const updatedContent = currentValue.replace(
|
const updatedContent = currentValue.replace(
|
||||||
`<!--[${ originalTag }]-->`,
|
`<!--[${ originalTag }]-->`,
|
||||||
`<!--[${ updatedTag }]-->`
|
`<!--[${ updatedTag }]-->`
|
||||||
);
|
);
|
||||||
updateEmailMailPoetProperty(
|
updateProperty( attributeName, updatedContent );
|
||||||
attributeName,
|
|
||||||
updatedContent
|
|
||||||
);
|
|
||||||
} }
|
} }
|
||||||
/>
|
/>
|
||||||
<RichText
|
<RichText
|
||||||
@ -144,26 +147,17 @@ export function RichTextWithButton( {
|
|||||||
placeholder={ placeholder }
|
placeholder={ placeholder }
|
||||||
onFocus={ () => {
|
onFocus={ () => {
|
||||||
setSelectionRange(
|
setSelectionRange(
|
||||||
getCursorPosition(
|
getCursorPosition( richTextRef, attributeValue ?? '' )
|
||||||
richTextRef,
|
|
||||||
mailpoetEmailData[ attributeName ] ?? ''
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
} }
|
} }
|
||||||
onKeyUp={ () => {
|
onKeyUp={ () => {
|
||||||
setSelectionRange(
|
setSelectionRange(
|
||||||
getCursorPosition(
|
getCursorPosition( richTextRef, attributeValue ?? '' )
|
||||||
richTextRef,
|
|
||||||
mailpoetEmailData[ attributeName ] ?? ''
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
} }
|
} }
|
||||||
onClick={ () => {
|
onClick={ () => {
|
||||||
setSelectionRange(
|
setSelectionRange(
|
||||||
getCursorPosition(
|
getCursorPosition( richTextRef, attributeValue ?? '' )
|
||||||
richTextRef,
|
|
||||||
mailpoetEmailData[ attributeName ] ?? ''
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
} }
|
} }
|
||||||
onChange={ ( value ) => {
|
onChange={ ( value ) => {
|
||||||
@ -171,7 +165,7 @@ export function RichTextWithButton( {
|
|||||||
value ?? '',
|
value ?? '',
|
||||||
list
|
list
|
||||||
);
|
);
|
||||||
updateEmailMailPoetProperty( attributeName, value );
|
updateProperty( attributeName, value );
|
||||||
recordEventOnce(
|
recordEventOnce(
|
||||||
'rich_text_with_button_input_field_updated',
|
'rich_text_with_button_input_field_updated',
|
||||||
{
|
{
|
||||||
@ -179,7 +173,7 @@ export function RichTextWithButton( {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
} }
|
} }
|
||||||
value={ mailpoetEmailData[ attributeName ] ?? '' }
|
value={ attributeValue ?? '' }
|
||||||
data-automation-id={ `email_${ attributeName }` }
|
data-automation-id={ `email_${ attributeName }` }
|
||||||
/>
|
/>
|
||||||
</BaseControl>
|
</BaseControl>
|
||||||
|
Reference in New Issue
Block a user