| 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 |
TextControl, |
| 10 |
ToggleControl, |
| 11 |
ToolbarButton, |
| 12 |
ToolbarGroup, |
| 13 |
} from '@wordpress/components'; |
| 14 |
import { createHigherOrderComponent } from '@wordpress/compose'; |
| 15 |
import { addFilter } from '@wordpress/hooks'; |
| 16 |
|
| 17 |
/** |
| 18 |
* External dependencies |
| 19 |
*/ |
| 20 |
import getBlockMarkerAttribute from './helpers/getBlockMarkerAttribute'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Internal dependencies |
| 24 |
*/ |
| 25 |
import BlockAttributesCheck from './check'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Add BeyondWords controls to Gutenberg Blocks. |
| 29 |
* |
| 30 |
* @param {Function} BlockEdit Block edit component. |
| 31 |
* |
| 32 |
* @return {Function} BlockEdit Modified block edit component. |
| 33 |
*/ |
| 34 |
const withBeyondwordsBlockControls = createHigherOrderComponent( |
| 35 |
( BlockEdit ) => { |
| 36 |
return ( props ) => { |
| 37 |
const { attributes, setAttributes } = props; |
| 38 |
|
| 39 |
const { beyondwordsAudio, beyondwordsMarker } = attributes; |
| 40 |
|
| 41 |
const icon = !! beyondwordsAudio |
| 42 |
? 'controls-volumeon' |
| 43 |
: 'controls-volumeoff'; |
| 44 |
const buttonLabel = !! beyondwordsAudio |
| 45 |
? __( 'Disable audio processing', 'speechkit' ) |
| 46 |
: __( 'Enable audio processing', 'speechkit' ); |
| 47 |
const toggleLabel = !! beyondwordsAudio |
| 48 |
? __( 'Audio processing enabled', 'speechkit' ) |
| 49 |
: __( 'Audio processing disabled', 'speechkit' ); |
| 50 |
|
| 51 |
const toggleBeyondwordsAudio = () => |
| 52 |
setAttributes( { beyondwordsAudio: ! beyondwordsAudio } ); |
| 53 |
|
| 54 |
const assignMarkerOnLoad = () => { |
| 55 |
const marker = getBlockMarkerAttribute( attributes ); |
| 56 |
|
| 57 |
setAttributes( { beyondwordsMarker: marker } ); |
| 58 |
}; |
| 59 |
|
| 60 |
return ( |
| 61 |
<> |
| 62 |
<BlockEdit { ...props } /> |
| 63 |
|
| 64 |
<BlockAttributesCheck> |
| 65 |
{ /* Onload hack fires when block is rendered */ } |
| 66 |
{ /* https://wordpress.stackexchange.com/a/333125 */ } |
| 67 |
<img |
| 68 |
alt="" |
| 69 |
className="beyondwords-block-onload-hack" |
| 70 |
height="0" |
| 71 |
width="0" |
| 72 |
style={ { display: "none" } } |
| 73 |
onLoad={ assignMarkerOnLoad } |
| 74 |
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1' %3E%3Cpath d=''/%3E%3C/svg%3E" |
| 75 |
/> |
| 76 |
|
| 77 |
<InspectorControls> |
| 78 |
<PanelBody |
| 79 |
icon="controls-volumeon" |
| 80 |
title={ __( 'BeyondWords', 'speechkit' ) } |
| 81 |
initialOpen={ true } |
| 82 |
> |
| 83 |
<PanelRow> |
| 84 |
<ToggleControl |
| 85 |
label={ toggleLabel } |
| 86 |
checked={ !! beyondwordsAudio } |
| 87 |
onChange={ toggleBeyondwordsAudio } |
| 88 |
/> |
| 89 |
</PanelRow> |
| 90 |
{ !! beyondwordsAudio && ( |
| 91 |
<PanelRow> |
| 92 |
<TextControl |
| 93 |
label={ __( |
| 94 |
'Segment marker', |
| 95 |
'speechkit' |
| 96 |
) } |
| 97 |
value={ beyondwordsMarker } |
| 98 |
disabled |
| 99 |
readOnly |
| 100 |
/> |
| 101 |
</PanelRow> |
| 102 |
) } |
| 103 |
</PanelBody> |
| 104 |
</InspectorControls> |
| 105 |
|
| 106 |
<BlockControls> |
| 107 |
<ToolbarGroup> |
| 108 |
<ToolbarButton |
| 109 |
icon={ icon } |
| 110 |
label={ buttonLabel } |
| 111 |
className="components-toolbar__control" |
| 112 |
onClick={ toggleBeyondwordsAudio } |
| 113 |
/> |
| 114 |
</ToolbarGroup> |
| 115 |
</BlockControls> |
| 116 |
</BlockAttributesCheck> |
| 117 |
</> |
| 118 |
); |
| 119 |
}; |
| 120 |
}, |
| 121 |
'withBeyondwordsBlockControls' |
| 122 |
); |
| 123 |
|
| 124 |
addFilter( |
| 125 |
'editor.BlockEdit', |
| 126 |
'beyondwords/block-controls', |
| 127 |
withBeyondwordsBlockControls |
| 128 |
); |
| 129 |
|