| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { PanelBody, SelectControl } from '@wordpress/components'; |
| 6 |
import { useEntityProp } from '@wordpress/core-data'; |
| 7 |
import { useSelect } from '@wordpress/data'; |
| 8 |
import { decodeEntities } from '@wordpress/html-entities'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Internal dependencies |
| 12 |
*/ |
| 13 |
import { |
| 14 |
getSourceOptions, |
| 15 |
normalizeSource, |
| 16 |
sourceIncludesScript, |
| 17 |
projectDefaultOption, |
| 18 |
} from './helpers'; |
| 19 |
import GenerateAudio from '../generate-audio'; |
| 20 |
import Stack from '../stack'; |
| 21 |
|
| 22 |
export function ContentSection() { |
| 23 |
const postType = useSelect( |
| 24 |
( select ) => select( 'core/editor' ).getCurrentPostType(), |
| 25 |
[] |
| 26 |
); |
| 27 |
|
| 28 |
const scriptTemplates = useSelect( |
| 29 |
( select ) => select( 'beyondwords/settings' ).getScriptTemplates(), |
| 30 |
[] |
| 31 |
); |
| 32 |
|
| 33 |
// `useEntityProp` yields undefined meta until the post entity record is hydrated. |
| 34 |
const [ rawMeta, setMeta ] = useEntityProp( 'postType', postType, 'meta' ); |
| 35 |
const meta = rawMeta ?? {}; |
| 36 |
|
| 37 |
const source = normalizeSource( meta.beyondwords_source ); |
| 38 |
const scriptTemplateId = meta.beyondwords_script_template_id || ''; |
| 39 |
|
| 40 |
const setSource = ( value ) => { |
| 41 |
setMeta( { ...meta, beyondwords_source: value } ); |
| 42 |
}; |
| 43 |
|
| 44 |
const setScriptTemplateId = ( value ) => { |
| 45 |
setMeta( { ...meta, beyondwords_script_template_id: value } ); |
| 46 |
}; |
| 47 |
|
| 48 |
const hasScriptTemplates = ( scriptTemplates ?? [] ).length > 0; |
| 49 |
|
| 50 |
const scriptTemplateOptions = [ |
| 51 |
projectDefaultOption(), |
| 52 |
...( scriptTemplates ?? [] ).map( ( template ) => ( { |
| 53 |
label: decodeEntities( template.name ?? template.slug ?? '' ), |
| 54 |
value: String( template.id ), |
| 55 |
} ) ), |
| 56 |
]; |
| 57 |
|
| 58 |
return ( |
| 59 |
<PanelBody title={ __( 'Content', 'speechkit' ) } initialOpen={ true }> |
| 60 |
<Stack> |
| 61 |
<GenerateAudio /> |
| 62 |
<SelectControl |
| 63 |
className="beyondwords--source" |
| 64 |
label={ __( 'Source', 'speechkit' ) } |
| 65 |
options={ getSourceOptions() } |
| 66 |
value={ source } |
| 67 |
onChange={ setSource } |
| 68 |
__nextHasNoMarginBottom |
| 69 |
__next40pxDefaultSize |
| 70 |
/> |
| 71 |
{ sourceIncludesScript( source ) && hasScriptTemplates && ( |
| 72 |
<SelectControl |
| 73 |
className="beyondwords--script-template" |
| 74 |
label={ __( 'Script template', 'speechkit' ) } |
| 75 |
options={ scriptTemplateOptions } |
| 76 |
value={ scriptTemplateId } |
| 77 |
onChange={ setScriptTemplateId } |
| 78 |
__nextHasNoMarginBottom |
| 79 |
__next40pxDefaultSize |
| 80 |
/> |
| 81 |
) } |
| 82 |
</Stack> |
| 83 |
</PanelBody> |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
export default ContentSection; |
| 88 |
|