| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { SelectControl, Flex, FlexBlock } from '@wordpress/components'; |
| 6 |
import { useEntityProp } from '@wordpress/core-data'; |
| 7 |
import { useSelect } from '@wordpress/data'; |
| 8 |
import { Fragment, useMemo } from '@wordpress/element'; |
| 9 |
import { decodeEntities } from '@wordpress/html-entities'; |
| 10 |
|
| 11 |
/** |
| 12 |
* Internal dependencies |
| 13 |
*/ |
| 14 |
import SelectVoiceCheck from './check'; |
| 15 |
|
| 16 |
export function SelectVoice( { wrapper } ) { |
| 17 |
const Wrapper = wrapper || Fragment; |
| 18 |
|
| 19 |
const postType = useSelect( |
| 20 |
( select ) => select( 'core/editor' ).getCurrentPostType(), |
| 21 |
[] |
| 22 |
); |
| 23 |
|
| 24 |
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' ); |
| 25 |
|
| 26 |
const languageId = meta.beyondwords_language_id; |
| 27 |
const bodyVoiceId = meta.beyondwords_body_voice_id; |
| 28 |
|
| 29 |
const setLanguageId = ( newLanguageId ) => { |
| 30 |
setMeta( { |
| 31 |
...meta, |
| 32 |
beyondwords_language_id: newLanguageId, |
| 33 |
} ); |
| 34 |
}; |
| 35 |
|
| 36 |
const setAllVoiceIds = ( newVoiceId ) => { |
| 37 |
setMeta( { |
| 38 |
...meta, |
| 39 |
beyondwords_body_voice_id: newVoiceId, |
| 40 |
beyondwords_title_voice_id: newVoiceId, |
| 41 |
beyondwords_summary_voice_id: newVoiceId, |
| 42 |
} ); |
| 43 |
}; |
| 44 |
|
| 45 |
const { languages } = useSelect( ( select ) => { |
| 46 |
return { |
| 47 |
languages: select( 'beyondwords/settings' ).getLanguages(), |
| 48 |
} |
| 49 |
}, [] ); |
| 50 |
|
| 51 |
const { voices } = useSelect( ( select ) => { |
| 52 |
return { |
| 53 |
voices: languageId ? select( 'beyondwords/settings' ).getVoices( languageId ) : [], |
| 54 |
} |
| 55 |
}, [ languageId ] ); |
| 56 |
|
| 57 |
const languageOptions = useMemo( () => { |
| 58 |
return ( languages ?? [] ).map( ( language ) => { |
| 59 |
return { |
| 60 |
label: decodeEntities( language.name ), |
| 61 |
value: decodeEntities( language.id ), |
| 62 |
}; |
| 63 |
} ); |
| 64 |
}, [ languages ] ); |
| 65 |
|
| 66 |
const voiceOptions = useMemo( () => { |
| 67 |
return ( voices ?? [] ).map( ( voice ) => { |
| 68 |
return { |
| 69 |
label: decodeEntities( voice.name ), |
| 70 |
value: decodeEntities( voice.id ), |
| 71 |
}; |
| 72 |
} ); |
| 73 |
}, [ voices ] ); |
| 74 |
|
| 75 |
return ( |
| 76 |
<SelectVoiceCheck> |
| 77 |
<Wrapper> |
| 78 |
<Flex> |
| 79 |
<FlexBlock> |
| 80 |
<SelectControl |
| 81 |
className="beyondwords--select-language" |
| 82 |
label={ __( 'Language', 'speechkit' ) } |
| 83 |
options={ [ |
| 84 |
{ |
| 85 |
label: __( 'Project default', 'speechkit' ), |
| 86 |
value: '', |
| 87 |
}, |
| 88 |
...languageOptions |
| 89 |
] } |
| 90 |
onChange={ ( val ) => setLanguageId( val ) } |
| 91 |
value={ languageId } |
| 92 |
__nextHasNoMarginBottom |
| 93 |
/> |
| 94 |
</FlexBlock> |
| 95 |
</Flex> |
| 96 |
</Wrapper> |
| 97 |
<Wrapper> |
| 98 |
<Flex> |
| 99 |
<FlexBlock> |
| 100 |
<SelectControl |
| 101 |
className="beyondwords--select-voice" |
| 102 |
label={ __( 'Voice', 'speechkit' ) } |
| 103 |
options={ [ |
| 104 |
{ |
| 105 |
label: '', |
| 106 |
value: '', |
| 107 |
}, |
| 108 |
...voiceOptions |
| 109 |
] } |
| 110 |
onChange={ ( val ) => setAllVoiceIds( val ) } |
| 111 |
disabled={ ! voiceOptions.length } |
| 112 |
value={ bodyVoiceId } |
| 113 |
__nextHasNoMarginBottom |
| 114 |
/> |
| 115 |
</FlexBlock> |
| 116 |
</Flex> |
| 117 |
</Wrapper> |
| 118 |
</SelectVoiceCheck> |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
export default SelectVoice; |
| 123 |
|