| 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 |
getOutputOptions, |
| 15 |
outputIncludesVideo, |
| 16 |
OUTPUT_AUDIO, |
| 17 |
projectDefaultOption, |
| 18 |
} from './helpers'; |
| 19 |
import Stack from '../stack'; |
| 20 |
|
| 21 |
export function FormatSection() { |
| 22 |
const postType = useSelect( |
| 23 |
( select ) => select( 'core/editor' ).getCurrentPostType(), |
| 24 |
[] |
| 25 |
); |
| 26 |
|
| 27 |
const projectId = useSelect( |
| 28 |
( select ) => |
| 29 |
select( 'core/editor' ).getEditedPostAttribute( 'meta' ) |
| 30 |
?.beyondwords_project_id || |
| 31 |
select( 'beyondwords/settings' ).getSettings()?.projectId, |
| 32 |
[] |
| 33 |
); |
| 34 |
|
| 35 |
const videoTemplates = useSelect( |
| 36 |
( select ) => select( 'beyondwords/settings' ).getVideoTemplates(), |
| 37 |
[] |
| 38 |
); |
| 39 |
|
| 40 |
const videoSizes = useSelect( |
| 41 |
( select ) => |
| 42 |
select( 'beyondwords/settings' ).getVideoSizes( projectId ), |
| 43 |
[ projectId ] |
| 44 |
); |
| 45 |
|
| 46 |
// `useEntityProp` yields undefined meta until the post entity record is hydrated. |
| 47 |
const [ rawMeta, setMeta ] = useEntityProp( 'postType', postType, 'meta' ); |
| 48 |
const meta = rawMeta ?? {}; |
| 49 |
|
| 50 |
const output = meta.beyondwords_output || OUTPUT_AUDIO; |
| 51 |
const videoTemplateId = meta.beyondwords_video_template_id || ''; |
| 52 |
const videoSize = meta.beyondwords_video_size || ''; |
| 53 |
|
| 54 |
const setOutput = ( value ) => { |
| 55 |
setMeta( { ...meta, beyondwords_output: value } ); |
| 56 |
}; |
| 57 |
|
| 58 |
const setVideoTemplateId = ( value ) => { |
| 59 |
setMeta( { ...meta, beyondwords_video_template_id: value } ); |
| 60 |
}; |
| 61 |
|
| 62 |
const setVideoSize = ( value ) => { |
| 63 |
setMeta( { ...meta, beyondwords_video_size: value } ); |
| 64 |
}; |
| 65 |
|
| 66 |
const videoTemplateOptions = [ |
| 67 |
projectDefaultOption(), |
| 68 |
...( videoTemplates ?? [] ).map( ( template ) => ( { |
| 69 |
label: decodeEntities( template.name ?? template.slug ?? '' ), |
| 70 |
value: String( template.id ), |
| 71 |
} ) ), |
| 72 |
]; |
| 73 |
|
| 74 |
const videoSizeOptions = [ |
| 75 |
projectDefaultOption(), |
| 76 |
...( videoSizes ?? [] ) |
| 77 |
.filter( ( size ) => size.enabled !== false ) |
| 78 |
.map( ( size ) => ( { |
| 79 |
label: decodeEntities( |
| 80 |
size.description |
| 81 |
? `${ size.name } (${ size.description })` |
| 82 |
: size.name |
| 83 |
), |
| 84 |
value: size.name, |
| 85 |
} ) ), |
| 86 |
]; |
| 87 |
|
| 88 |
return ( |
| 89 |
<PanelBody title={ __( 'Format', 'speechkit' ) } initialOpen={ true }> |
| 90 |
<Stack> |
| 91 |
<SelectControl |
| 92 |
className="beyondwords--output" |
| 93 |
label={ __( 'Output', 'speechkit' ) } |
| 94 |
options={ getOutputOptions() } |
| 95 |
value={ output } |
| 96 |
onChange={ setOutput } |
| 97 |
__nextHasNoMarginBottom |
| 98 |
__next40pxDefaultSize |
| 99 |
/> |
| 100 |
{ outputIncludesVideo( output ) && ( |
| 101 |
<> |
| 102 |
<SelectControl |
| 103 |
className="beyondwords--video-template" |
| 104 |
label={ __( 'Video template', 'speechkit' ) } |
| 105 |
options={ videoTemplateOptions } |
| 106 |
value={ videoTemplateId } |
| 107 |
onChange={ setVideoTemplateId } |
| 108 |
__nextHasNoMarginBottom |
| 109 |
__next40pxDefaultSize |
| 110 |
/> |
| 111 |
<SelectControl |
| 112 |
className="beyondwords--video-size" |
| 113 |
label={ __( 'Video size', 'speechkit' ) } |
| 114 |
options={ videoSizeOptions } |
| 115 |
value={ videoSize } |
| 116 |
onChange={ setVideoSize } |
| 117 |
__nextHasNoMarginBottom |
| 118 |
__next40pxDefaultSize |
| 119 |
/> |
| 120 |
</> |
| 121 |
) } |
| 122 |
</Stack> |
| 123 |
</PanelBody> |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
export default FormatSection; |
| 128 |
|