block-suggestion-actions.jsx
2 weeks ago
block-suggestion-buttons.jsx
2 weeks ago
diff-view.jsx
2 weeks ago
empty-state-banner.jsx
2 weeks ago
section-generate-button.jsx
2 weeks ago
suggest-all-button.jsx
2 weeks ago
suggestion-actions.jsx
2 weeks ago
suggestion-badge.jsx
2 weeks ago
upgrade-notice.jsx
1 week ago
diff-view.jsx
56 lines
| 1 | import { useCallback, useMemo } from '@wordpress/element'; |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import { diffWords } from 'diff'; |
| 4 | |
| 5 | export default function DiffView( { original, suggestion, onAccept, height } ) { |
| 6 | const diff = useMemo( () => { |
| 7 | if ( ! suggestion ) { |
| 8 | return []; |
| 9 | } |
| 10 | return diffWords( original, suggestion ); |
| 11 | }, [ original, suggestion ] ); |
| 12 | |
| 13 | const handleKeyDown = useCallback( |
| 14 | e => { |
| 15 | if ( e.key === 'Enter' || e.key === ' ' ) { |
| 16 | e.preventDefault(); |
| 17 | onAccept(); |
| 18 | } |
| 19 | }, |
| 20 | [ onAccept ] |
| 21 | ); |
| 22 | |
| 23 | return ( |
| 24 | <div |
| 25 | className="jetpack-content-guidelines-ai__diff" |
| 26 | style={ height ? { height } : undefined } |
| 27 | role="button" |
| 28 | tabIndex={ 0 } |
| 29 | aria-label={ __( 'Click to accept suggested changes', 'jetpack' ) } |
| 30 | onClick={ onAccept } |
| 31 | onKeyDown={ handleKeyDown } |
| 32 | > |
| 33 | <span className="screen-reader-text"> |
| 34 | { __( 'Changes from current to suggested guidelines:', 'jetpack' ) } |
| 35 | </span> |
| 36 | { diff.map( ( part, i ) => { |
| 37 | if ( part.added ) { |
| 38 | return ( |
| 39 | <ins key={ i } className="jetpack-content-guidelines-ai__diff-added"> |
| 40 | { part.value } |
| 41 | </ins> |
| 42 | ); |
| 43 | } |
| 44 | if ( part.removed ) { |
| 45 | return ( |
| 46 | <del key={ i } className="jetpack-content-guidelines-ai__diff-removed"> |
| 47 | { part.value } |
| 48 | </del> |
| 49 | ); |
| 50 | } |
| 51 | return <span key={ i }>{ part.value }</span>; |
| 52 | } ) } |
| 53 | </div> |
| 54 | ); |
| 55 | } |
| 56 |