PluginProbe
BeyondWords – AI audio for publishers / 4.0.0
BeyondWords – AI audio for publishers v4.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 / Component / Post / BlockAttributes / addControls.js

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

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