PluginProbe
BeyondWords – AI audio for publishers / 6.3.0
BeyondWords – AI audio for publishers v6.3.0
7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.7.0 All 43 releases
speechkit / src / Component / Post / BlockAttributes / addControls.js

addControls.js in BeyondWords – AI audio for publishers 6.3.0, at src/Component/Post/BlockAttributes/addControls.js

103 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ToggleControl,
10 ToolbarButton,
11 ToolbarGroup,
12 } from '@wordpress/components';
13 import { createHigherOrderComponent } from '@wordpress/compose';
14 import { addFilter } from '@wordpress/hooks';
15
16 /**
17 * Internal dependencies
18 */
19 import { isBeyondwordsSupportedBlock } from './isBeyondwordsSupportedBlock';
20
21 /**
22 * Add BeyondWords controls to Gutenberg Blocks.
23 *
24 * @since 6.0.1 Skip internal/UI blocks to prevent breaking the block inserter.
25 *
26 * @param {Function} BlockEdit Block edit component.
27 *
28 * @return {Function} BlockEdit Modified block edit component.
29 */
30 const withBeyondwordsBlockControls = createHigherOrderComponent(
31 ( BlockEdit ) => {
32 return ( props ) => {
33 const { name } = props;
34
35 // Skip blocks that shouldn't have controls
36 // Do this 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 audio processing', 'speechkit' )
50 : __( 'Enable audio processing', 'speechkit' );
51
52 const toggleLabel = !! beyondwordsAudio
53 ? __( 'Audio processing enabled', 'speechkit' )
54 : __( 'Audio processing disabled', 'speechkit' );
55
56 const toggleBeyondwordsAudio = () => {
57 setAttributes( { beyondwordsAudio: ! beyondwordsAudio } );
58 };
59
60 return (
61 <>
62 <BlockEdit { ...props } />
63
64 <InspectorControls>
65 <PanelBody
66 icon="controls-volumeon"
67 title={ __( 'BeyondWords', 'speechkit' ) }
68 initialOpen={ true }
69 >
70 <PanelRow>
71 <ToggleControl
72 label={ toggleLabel }
73 checked={ !! beyondwordsAudio }
74 onChange={ toggleBeyondwordsAudio }
75 __nextHasNoMarginBottom
76 />
77 </PanelRow>
78 </PanelBody>
79 </InspectorControls>
80
81 <BlockControls>
82 <ToolbarGroup>
83 <ToolbarButton
84 icon={ icon }
85 label={ buttonLabel }
86 className="components-toolbar__control"
87 onClick={ toggleBeyondwordsAudio }
88 />
89 </ToolbarGroup>
90 </BlockControls>
91 </>
92 );
93 };
94 },
95 'withBeyondwordsBlockControls'
96 );
97
98 addFilter(
99 'editor.BlockEdit',
100 'beyondwords/block-controls',
101 withBeyondwordsBlockControls
102 );
103