| 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 } from '@wordpress/element'; |
| 9 |
|
| 10 |
export function PlayerStyle( { wrapper } ) { |
| 11 |
const Wrapper = wrapper || Fragment; |
| 12 |
|
| 13 |
const postType = useSelect( |
| 14 |
( select ) => select( 'core/editor' ).getCurrentPostType(), |
| 15 |
[] |
| 16 |
); |
| 17 |
|
| 18 |
const postProjectId = useSelect( |
| 19 |
( select ) => |
| 20 |
select( 'core/editor' ).getEditedPostAttribute( 'meta' ) |
| 21 |
?.beyondwords_project_id, |
| 22 |
[] |
| 23 |
); |
| 24 |
|
| 25 |
const settingsProjectId = useSelect( |
| 26 |
( select ) => select( 'beyondwords/settings' ).getSettings()?.projectId, |
| 27 |
[] |
| 28 |
); |
| 29 |
|
| 30 |
const playerStyles = useSelect( |
| 31 |
( select ) => { |
| 32 |
const projectId = postProjectId || settingsProjectId; |
| 33 |
return projectId |
| 34 |
? select( 'beyondwords/settings' ).getPlayerStyles( |
| 35 |
projectId |
| 36 |
) || [] |
| 37 |
: []; |
| 38 |
}, |
| 39 |
[ postProjectId, settingsProjectId ] |
| 40 |
); |
| 41 |
|
| 42 |
const defaultPlayerStyle = playerStyles.find( ( style ) => style.default ); |
| 43 |
|
| 44 |
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' ); |
| 45 |
|
| 46 |
const playerStyle = |
| 47 |
meta.beyondwords_player_style || defaultPlayerStyle?.value; |
| 48 |
|
| 49 |
const setPlayerStyle = ( newPlayerStyle ) => { |
| 50 |
setMeta( { |
| 51 |
...meta, |
| 52 |
beyondwords_player_style: newPlayerStyle, |
| 53 |
} ); |
| 54 |
}; |
| 55 |
|
| 56 |
if ( ! playerStyles.length ) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
return ( |
| 61 |
<Wrapper> |
| 62 |
<Flex> |
| 63 |
<FlexBlock> |
| 64 |
<SelectControl |
| 65 |
className="beyondwords--player-style" |
| 66 |
label={ __( 'Player style', 'speechkit' ) } |
| 67 |
options={ [ |
| 68 |
{ |
| 69 |
label: '', |
| 70 |
value: '', |
| 71 |
}, |
| 72 |
...playerStyles, |
| 73 |
] } |
| 74 |
onChange={ ( val ) => setPlayerStyle( val ) } |
| 75 |
value={ playerStyle } |
| 76 |
__nextHasNoMarginBottom |
| 77 |
__next40pxDefaultSize |
| 78 |
/> |
| 79 |
</FlexBlock> |
| 80 |
</Flex> |
| 81 |
</Wrapper> |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
export default PlayerStyle; |
| 86 |
|