| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { SelectControl, Flex, FlexBlock, Spinner } from '@wordpress/components'; |
| 6 |
import { useEntityProp } from '@wordpress/core-data'; |
| 7 |
import { useSelect } from '@wordpress/data'; |
| 8 |
import { Fragment } from '@wordpress/element'; |
| 9 |
import { decodeEntities } from '@wordpress/html-entities'; |
| 10 |
|
| 11 |
export function SelectVoice( { wrapper } ) { |
| 12 |
const Wrapper = wrapper || Fragment; |
| 13 |
|
| 14 |
const postType = useSelect( |
| 15 |
( select ) => select( 'core/editor' ).getCurrentPostType(), |
| 16 |
[] |
| 17 |
); |
| 18 |
|
| 19 |
const settings = useSelect( |
| 20 |
( select ) => select( 'beyondwords/settings' ).getSettings(), |
| 21 |
[] |
| 22 |
); |
| 23 |
|
| 24 |
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' ); |
| 25 |
|
| 26 |
const languageCode = |
| 27 |
meta.beyondwords_language_code || settings.projectLanguageCode; |
| 28 |
|
| 29 |
const languages = useSelect( |
| 30 |
( select ) => select( 'beyondwords/settings' ).getLanguages(), |
| 31 |
[] |
| 32 |
); |
| 33 |
|
| 34 |
const defaultLanguage = languages?.find( |
| 35 |
( item ) => item.code === languageCode |
| 36 |
); |
| 37 |
|
| 38 |
const setLanguageCode = ( newLanguageCode ) => { |
| 39 |
setMeta( { |
| 40 |
...meta, |
| 41 |
beyondwords_language_code: newLanguageCode, |
| 42 |
} ); |
| 43 |
}; |
| 44 |
|
| 45 |
const setAllVoiceIds = ( newVoiceId ) => { |
| 46 |
setMeta( { |
| 47 |
...meta, |
| 48 |
beyondwords_body_voice_id: newVoiceId, |
| 49 |
beyondwords_title_voice_id: newVoiceId, |
| 50 |
beyondwords_summary_voice_id: newVoiceId, |
| 51 |
} ); |
| 52 |
}; |
| 53 |
|
| 54 |
const voices = useSelect( |
| 55 |
( select ) => |
| 56 |
languageCode |
| 57 |
? select( 'beyondwords/settings' ).getVoices( languageCode ) |
| 58 |
: [], |
| 59 |
[ languageCode ] |
| 60 |
); |
| 61 |
|
| 62 |
const candidates = [ |
| 63 |
meta.beyondwords_body_voice_id, |
| 64 |
settings.projectBodyVoiceId, |
| 65 |
defaultLanguage?.default_voices?.body?.id, |
| 66 |
].map( String ); |
| 67 |
|
| 68 |
const defaultVoice = |
| 69 |
candidates.find( ( candidate ) => |
| 70 |
( voices ?? [] ).some( ( { id } ) => String( id ) === candidate ) |
| 71 |
) ?? ''; |
| 72 |
|
| 73 |
const languageOptions = ( languages ?? [] ).map( ( language ) => { |
| 74 |
const { accent, code, name } = language; |
| 75 |
return { |
| 76 |
// eslint-disable-next-line prettier/prettier |
| 77 |
label: `${ decodeEntities( name ) } (${ decodeEntities( accent ) })`, |
| 78 |
value: decodeEntities( code ), |
| 79 |
}; |
| 80 |
} ); |
| 81 |
|
| 82 |
// eslint-disable-next-line @wordpress/no-unused-vars-before-return |
| 83 |
const voiceOptions = ( voices ?? [] ).map( ( voice ) => { |
| 84 |
const { id, name } = voice; |
| 85 |
return { |
| 86 |
label: decodeEntities( name ), |
| 87 |
value: `${ decodeEntities( id ) }`, |
| 88 |
}; |
| 89 |
} ); |
| 90 |
|
| 91 |
if ( ! languageOptions.length ) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
return ( |
| 96 |
<> |
| 97 |
<Wrapper> |
| 98 |
<Flex> |
| 99 |
<FlexBlock> |
| 100 |
<SelectControl |
| 101 |
className="beyondwords--select-language" |
| 102 |
label={ __( 'Language', 'speechkit' ) } |
| 103 |
options={ languageOptions } |
| 104 |
onChange={ ( val ) => setLanguageCode( val ) } |
| 105 |
value={ languageCode } |
| 106 |
__nextHasNoMarginBottom |
| 107 |
__next40pxDefaultSize |
| 108 |
/> |
| 109 |
</FlexBlock> |
| 110 |
</Flex> |
| 111 |
</Wrapper> |
| 112 |
<Wrapper> |
| 113 |
<Flex> |
| 114 |
<FlexBlock> |
| 115 |
{ ! voiceOptions?.length && ( |
| 116 |
<Spinner |
| 117 |
className="beyondwords--spinner-voices" |
| 118 |
style={ { marginTop: '1rem' } } |
| 119 |
/> |
| 120 |
) } |
| 121 |
{ !! voiceOptions?.length && ( |
| 122 |
<SelectControl |
| 123 |
className="beyondwords--select-voice" |
| 124 |
label={ __( 'Voice', 'speechkit' ) } |
| 125 |
options={ voiceOptions } |
| 126 |
onChange={ ( val ) => setAllVoiceIds( val ) } |
| 127 |
disabled={ ! voiceOptions?.length } |
| 128 |
value={ defaultVoice } |
| 129 |
__nextHasNoMarginBottom |
| 130 |
__next40pxDefaultSize |
| 131 |
/> |
| 132 |
) } |
| 133 |
</FlexBlock> |
| 134 |
</Flex> |
| 135 |
</Wrapper> |
| 136 |
</> |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
export default SelectVoice; |
| 141 |
|