PluginProbe
BeyondWords – AI audio for publishers / 4.0.6
BeyondWords – AI audio for publishers v4.0.6
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 4.0.6, at src/Component/Post/BlockAttributes/addControls.js

131 lines 3.3 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 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, isSelected } = 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 { /* Onload hack fires when block is rendered */ }
63 { /* https://wordpress.stackexchange.com/a/333125 */ }
64 <img
65 alt=""
66 className="beyondwords-block-onload-hack"
67 height="0"
68 width="0"
69 style={ { display: "none" } }
70 onLoad={ assignMarkerOnLoad }
71 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"
72 />
73
74 <BlockEdit { ...props } />
75
76 { isSelected && (
77 <BlockAttributesCheck>
78 <InspectorControls>
79 <PanelBody
80 icon="controls-volumeon"
81 title={ __( 'BeyondWords', 'speechkit' ) }
82 initialOpen={ true }
83 >
84 <PanelRow>
85 <ToggleControl
86 label={ toggleLabel }
87 checked={ !! beyondwordsAudio }
88 onChange={ toggleBeyondwordsAudio }
89 />
90 </PanelRow>
91 { !! beyondwordsAudio && (
92 <PanelRow>
93 <TextControl
94 label={ __(
95 'Segment marker',
96 'speechkit'
97 ) }
98 value={ beyondwordsMarker }
99 disabled
100 readOnly
101 />
102 </PanelRow>
103 ) }
104 </PanelBody>
105 </InspectorControls>
106
107 <BlockControls>
108 <ToolbarGroup>
109 <ToolbarButton
110 icon={ icon }
111 label={ buttonLabel }
112 className="components-toolbar__control"
113 onClick={ toggleBeyondwordsAudio }
114 />
115 </ToolbarGroup>
116 </BlockControls>
117 </BlockAttributesCheck>
118 ) }
119 </>
120 );
121 };
122 },
123 'withBeyondwordsBlockControls'
124 );
125
126 addFilter(
127 'editor.BlockEdit',
128 'beyondwords/block-controls',
129 withBeyondwordsBlockControls
130 );
131