| 1 |
import { MenuGroup, MenuItem } from '@wordpress/components'; |
| 2 |
import { useSelect } from '@wordpress/data'; |
| 3 |
import { useEffect } from '@wordpress/element'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { |
| 6 |
customPostType, |
| 7 |
termDescription, |
| 8 |
paragraph, |
| 9 |
postContent, |
| 10 |
Icon, |
| 11 |
} from '@wordpress/icons'; |
| 12 |
import { useContentHighlight } from '@draft/hooks/useContentHighlight'; |
| 13 |
import { useSelectedText } from '@draft/hooks/useSelectedText'; |
| 14 |
import { twoLines } from '@draft/svg'; |
| 15 |
|
| 16 |
export const EditMenu = ({ disabled, setPrompt }) => { |
| 17 |
const { toggleHighlight } = useContentHighlight(); |
| 18 |
const { selectedText } = useSelectedText(); |
| 19 |
|
| 20 |
const selectedBlockClientIds = useSelect( |
| 21 |
(select) => select('core/block-editor').getSelectedBlockClientIds(), |
| 22 |
[], |
| 23 |
); |
| 24 |
|
| 25 |
useEffect(() => { |
| 26 |
return () => { |
| 27 |
toggleHighlight(selectedBlockClientIds, { isHighlighted: false }); |
| 28 |
}; |
| 29 |
}, [selectedBlockClientIds, toggleHighlight]); |
| 30 |
|
| 31 |
const handleClick = (promptType) => { |
| 32 |
setPrompt({ |
| 33 |
text: selectedText, |
| 34 |
promptType, |
| 35 |
systemMessageKey: 'edit', |
| 36 |
}); |
| 37 |
}; |
| 38 |
|
| 39 |
const actionsList = [ |
| 40 |
{ |
| 41 |
label: __('Improve writing', 'extendify-local'), |
| 42 |
promptType: 'improve-writing', |
| 43 |
systemMessageKey: 'edit', |
| 44 |
icon: <Icon icon={customPostType} />, |
| 45 |
}, |
| 46 |
{ |
| 47 |
label: __('Fix spelling & grammar', 'extendify-local'), |
| 48 |
promptType: 'fix-spelling-grammar', |
| 49 |
icon: <Icon icon={termDescription} />, |
| 50 |
}, |
| 51 |
{ |
| 52 |
label: __('Simplify language', 'extendify-local'), |
| 53 |
promptType: 'simplify-language', |
| 54 |
icon: <Icon icon={paragraph} />, |
| 55 |
}, |
| 56 |
{ |
| 57 |
label: __('Make shorter', 'extendify-local'), |
| 58 |
promptType: 'make-shorter', |
| 59 |
icon: <Icon icon={twoLines} />, |
| 60 |
}, |
| 61 |
{ |
| 62 |
label: __('Make longer', 'extendify-local'), |
| 63 |
promptType: 'make-longer', |
| 64 |
icon: <Icon icon={postContent} />, |
| 65 |
}, |
| 66 |
]; |
| 67 |
|
| 68 |
return ( |
| 69 |
<MenuGroup> |
| 70 |
{actionsList.map(({ label, promptType, icon }) => ( |
| 71 |
<MenuItem |
| 72 |
key={`${promptType}-${promptType}-edit`} |
| 73 |
onClick={() => handleClick(promptType)} |
| 74 |
onMouseEnter={() => |
| 75 |
toggleHighlight(selectedBlockClientIds, { |
| 76 |
isHighlighted: true, |
| 77 |
}) |
| 78 |
} |
| 79 |
onMouseLeave={() => |
| 80 |
toggleHighlight(selectedBlockClientIds, { |
| 81 |
isHighlighted: false, |
| 82 |
}) |
| 83 |
} |
| 84 |
icon={icon} |
| 85 |
iconPosition="left" |
| 86 |
disabled={disabled} |
| 87 |
className="group"> |
| 88 |
<span className="whitespace-normal text-left rtl:text-right"> |
| 89 |
{label} |
| 90 |
</span> |
| 91 |
</MenuItem> |
| 92 |
))} |
| 93 |
</MenuGroup> |
| 94 |
); |
| 95 |
}; |
| 96 |
|