| 1 |
/** |
| 2 |
* WordPress Dependencies |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { InspectorControls, BlockControls } from '@wordpress/block-editor'; |
| 6 |
import { |
| 7 |
PanelBody, |
| 8 |
PanelRow, |
| 9 |
ToolbarButton, |
| 10 |
ToolbarGroup, |
| 11 |
} from '@wordpress/components'; |
| 12 |
import { createHigherOrderComponent } from '@wordpress/compose'; |
| 13 |
import { addFilter } from '@wordpress/hooks'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Internal dependencies |
| 17 |
*/ |
| 18 |
import { isBeyondwordsSupportedBlock } from './isBeyondwordsSupportedBlock'; |
| 19 |
import Toggle from '../toggle'; |
| 20 |
import { BeyondwordsTitle } from '../icon'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Add BeyondWords controls to Gutenberg Blocks. |
| 24 |
* |
| 25 |
* @since 6.0.1 Skip internal/UI blocks to prevent breaking the block inserter. |
| 26 |
* |
| 27 |
* @param {Function} BlockEdit Block edit component. |
| 28 |
* |
| 29 |
* @return {Function} BlockEdit Modified block edit component. |
| 30 |
*/ |
| 31 |
const withBeyondwordsBlockControls = createHigherOrderComponent( |
| 32 |
( BlockEdit ) => { |
| 33 |
return ( props ) => { |
| 34 |
const { name } = props; |
| 35 |
|
| 36 |
// Check BEFORE accessing attributes to avoid unnecessary processing. |
| 37 |
if ( ! isBeyondwordsSupportedBlock( name ) ) { |
| 38 |
return <BlockEdit { ...props } />; |
| 39 |
} |
| 40 |
|
| 41 |
const { attributes, setAttributes } = props; |
| 42 |
const { beyondwordsAudio } = attributes; |
| 43 |
|
| 44 |
const icon = !! beyondwordsAudio |
| 45 |
? 'controls-volumeon' |
| 46 |
: 'controls-volumeoff'; |
| 47 |
|
| 48 |
const buttonLabel = !! beyondwordsAudio |
| 49 |
? __( 'Disable generation', 'speechkit' ) |
| 50 |
: __( 'Enable generation', 'speechkit' ); |
| 51 |
|
| 52 |
const toggleLabel = !! beyondwordsAudio |
| 53 |
? __( 'Generation enabled', 'speechkit' ) |
| 54 |
: __( 'Generation disabled', 'speechkit' ); |
| 55 |
|
| 56 |
const toggleBeyondwordsAudio = () => { |
| 57 |
setAttributes( { beyondwordsAudio: ! beyondwordsAudio } ); |
| 58 |
}; |
| 59 |
|
| 60 |
return ( |
| 61 |
<> |
| 62 |
<BlockEdit { ...props } /> |
| 63 |
|
| 64 |
<InspectorControls> |
| 65 |
<PanelBody |
| 66 |
title={ <BeyondwordsTitle /> } |
| 67 |
initialOpen={ true } |
| 68 |
> |
| 69 |
<PanelRow> |
| 70 |
<Toggle |
| 71 |
label={ toggleLabel } |
| 72 |
checked={ !! beyondwordsAudio } |
| 73 |
onChange={ toggleBeyondwordsAudio } |
| 74 |
/> |
| 75 |
</PanelRow> |
| 76 |
</PanelBody> |
| 77 |
</InspectorControls> |
| 78 |
|
| 79 |
<BlockControls> |
| 80 |
<ToolbarGroup> |
| 81 |
<ToolbarButton |
| 82 |
icon={ icon } |
| 83 |
label={ buttonLabel } |
| 84 |
className="components-toolbar__control" |
| 85 |
onClick={ toggleBeyondwordsAudio } |
| 86 |
/> |
| 87 |
</ToolbarGroup> |
| 88 |
</BlockControls> |
| 89 |
</> |
| 90 |
); |
| 91 |
}; |
| 92 |
}, |
| 93 |
'withBeyondwordsBlockControls' |
| 94 |
); |
| 95 |
|
| 96 |
addFilter( |
| 97 |
'editor.BlockEdit', |
| 98 |
'beyondwords/block-controls', |
| 99 |
withBeyondwordsBlockControls |
| 100 |
); |
| 101 |
|