|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { __, sprintf } from '@wordpress/i18n'; |
| 5 | +import { useState } from '@wordpress/element'; |
| 6 | +import { RichTextToolbarButton } from '@wordpress/block-editor'; |
| 7 | +import { |
| 8 | + Modal, |
| 9 | + Button, |
| 10 | + __experimentalText as Text, |
| 11 | + __experimentalHStack as HStack, |
| 12 | +} from '@wordpress/components'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Internal dependencies |
| 16 | + */ |
| 17 | +import { clearFormatsIcon } from './icon'; |
| 18 | +import { useClearFormats } from './hooks'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Edit component for clearing all formats. |
| 22 | + * |
| 23 | + * @param {Object} props - The component properties. |
| 24 | + * @param {Object} props.value - The current value of the rich text. |
| 25 | + * @param {Function} props.onChange - Function to update the rich text value. |
| 26 | + * @return {JSX.Element} - The rendered clear formats button. |
| 27 | + */ |
| 28 | +export function Edit( { value, onChange } ) { |
| 29 | + const [ isModalOpen, setIsModalOpen ] = useState( false ); |
| 30 | + const { hasSelection, clearFormats } = useClearFormats( { |
| 31 | + value, |
| 32 | + onChange, |
| 33 | + } ); |
| 34 | + |
| 35 | + const handleClearFormats = () => { |
| 36 | + clearFormats(); |
| 37 | + setIsModalOpen( false ); |
| 38 | + }; |
| 39 | + |
| 40 | + // Dynamic confirmation message based on selection |
| 41 | + const selectionType = hasSelection |
| 42 | + ? __( 'text', 'blablablocks-formats' ) |
| 43 | + : __( 'block', 'blablablocks-formats' ); |
| 44 | + |
| 45 | + const confirmationMessage = sprintf( |
| 46 | + /* translators: %s: selection type (text/block) */ |
| 47 | + __( |
| 48 | + 'Are you sure you want to remove all formatting (bold, italic, links, etc.) from the selected %s?', |
| 49 | + 'blablablocks-formats' |
| 50 | + ), |
| 51 | + selectionType |
| 52 | + ); |
| 53 | + |
| 54 | + return ( |
| 55 | + <> |
| 56 | + <RichTextToolbarButton |
| 57 | + icon={ clearFormatsIcon } |
| 58 | + title={ __( 'Clear formatting', 'blablablocks-formats' ) } |
| 59 | + onClick={ () => setIsModalOpen( true ) } |
| 60 | + /> |
| 61 | + |
| 62 | + { isModalOpen && ( |
| 63 | + <Modal |
| 64 | + title={ __( 'Clear All Formats', 'blablablocks-formats' ) } |
| 65 | + onRequestClose={ () => setIsModalOpen( false ) } |
| 66 | + size="small" |
| 67 | + > |
| 68 | + <Text as="p" highlightWords={ [ 'text', 'block' ] }> |
| 69 | + { confirmationMessage } |
| 70 | + </Text> |
| 71 | + <HStack |
| 72 | + spacing={ 2 } |
| 73 | + justify="flex-end" |
| 74 | + style={ { marginTop: '24px' } } |
| 75 | + > |
| 76 | + <Button |
| 77 | + variant="tertiary" |
| 78 | + onClick={ () => setIsModalOpen( false ) } |
| 79 | + > |
| 80 | + { __( 'Cancel', 'blablablocks-formats' ) } |
| 81 | + </Button> |
| 82 | + <Button |
| 83 | + variant="primary" |
| 84 | + onClick={ handleClearFormats } |
| 85 | + isDestructive |
| 86 | + > |
| 87 | + { __( 'Clear', 'blablablocks-formats' ) } |
| 88 | + </Button> |
| 89 | + </HStack> |
| 90 | + </Modal> |
| 91 | + ) } |
| 92 | + </> |
| 93 | + ); |
| 94 | +} |
0 commit comments