| 1 |
import { store as blockEditorStore } from '@wordpress/block-editor'; |
| 2 |
import { Button } from '@wordpress/components'; |
| 3 |
import { useDispatch } from '@wordpress/data'; |
| 4 |
import { useEffect, useState } from '@wordpress/element'; |
| 5 |
import { __, isRTL } from '@wordpress/i18n'; |
| 6 |
import { Icon, edit, trash } from '@wordpress/icons'; |
| 7 |
import { useSelectedText } from '@draft/hooks/useSelectedText'; |
| 8 |
|
| 9 |
export const SelectedText = ({ loading }) => { |
| 10 |
const [text, setText] = useState(); |
| 11 |
const { clearSelectedBlock } = useDispatch(blockEditorStore); |
| 12 |
const { selectedText } = useSelectedText(); |
| 13 |
|
| 14 |
useEffect(() => { |
| 15 |
setText(selectedText); |
| 16 |
}, [setText, selectedText]); |
| 17 |
|
| 18 |
if (!text) return; |
| 19 |
|
| 20 |
const truncatedText = () => { |
| 21 |
const preformat = text.split(' '); |
| 22 |
|
| 23 |
if (preformat.length <= 20) return text; |
| 24 |
|
| 25 |
return `${text.split(' ', 14).join(' ')}... ${text.slice( |
| 26 |
text.lastIndexOf(' ') - 14, |
| 27 |
)}`; |
| 28 |
}; |
| 29 |
|
| 30 |
return ( |
| 31 |
<div |
| 32 |
className="mb-4 flex space-x-2 overflow-hidden rounded-sm border-none bg-gray-100 p-3" |
| 33 |
data-test="existing-text-container"> |
| 34 |
<div> |
| 35 |
<Icon icon={edit} className="fill-current" /> |
| 36 |
</div> |
| 37 |
<div> |
| 38 |
<div |
| 39 |
className="mb-1 hyphens-auto text-pretty text-gray-800" |
| 40 |
dangerouslySetInnerHTML={{ |
| 41 |
__html: truncatedText(), |
| 42 |
}} |
| 43 |
/> |
| 44 |
<div className="mt-3 flex w-full justify-end"> |
| 45 |
<Button |
| 46 |
size="compact" |
| 47 |
onClick={clearSelectedBlock} |
| 48 |
disabled={loading} |
| 49 |
icon={trash} |
| 50 |
iconPosition={isRTL() ? 'right' : 'left'} |
| 51 |
className="relative flex-row-reverse rounded bg-gray-300 text-gray-800 hover:bg-gray-400" |
| 52 |
data-test="remove-selection"> |
| 53 |
{__('Remove selection', 'extendify-local')} |
| 54 |
</Button> |
| 55 |
</div> |
| 56 |
</div> |
| 57 |
</div> |
| 58 |
); |
| 59 |
}; |
| 60 |
|