PluginProbe
BeyondWords – AI audio for publishers / 7.0.0
BeyondWords – AI audio for publishers v7.0.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 / editor / components / block-attributes / addControls.js

addControls.js in BeyondWords – AI audio for publishers 7.0.0, at src/editor/components/block-attributes/addControls.js

101 lines 2.4 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 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