@gechiui/block-editor
Version:
130 lines (125 loc) • 2.67 kB
JavaScript
/**
* GeChiUI dependencies
*/
import { check, aspectRatio as aspectRatioIcon } from '@gechiui/icons';
import { DropdownMenu, MenuGroup, MenuItem } from '@gechiui/components';
import { __ } from '@gechiui/i18n';
/**
* Internal dependencies
*/
import { POPOVER_PROPS } from './constants';
import { useImageEditingContext } from './context';
function AspectGroup( { aspectRatios, isDisabled, label, onClick, value } ) {
return (
<MenuGroup label={ label }>
{ aspectRatios.map( ( { title, aspect } ) => (
<MenuItem
key={ aspect }
disabled={ isDisabled }
onClick={ () => {
onClick( aspect );
} }
role="menuitemradio"
isSelected={ aspect === value }
icon={ aspect === value ? check : undefined }
>
{ title }
</MenuItem>
) ) }
</MenuGroup>
);
}
export default function AspectRatioDropdown( { toggleProps } ) {
const {
isInProgress,
aspect,
setAspect,
defaultAspect,
} = useImageEditingContext();
return (
<DropdownMenu
icon={ aspectRatioIcon }
label={ __( '长宽比' ) }
popoverProps={ POPOVER_PROPS }
toggleProps={ toggleProps }
className="gc-block-image__aspect-ratio"
>
{ ( { onClose } ) => (
<>
<AspectGroup
isDisabled={ isInProgress }
onClick={ ( newAspect ) => {
setAspect( newAspect );
onClose();
} }
value={ aspect }
aspectRatios={ [
{
title: __( '原始' ),
aspect: defaultAspect,
},
{
title: __( '实心方块' ),
aspect: 1,
},
] }
/>
<AspectGroup
label={ __( '横向' ) }
isDisabled={ isInProgress }
onClick={ ( newAspect ) => {
setAspect( newAspect );
onClose();
} }
value={ aspect }
aspectRatios={ [
{
title: __( '16:10' ),
aspect: 16 / 10,
},
{
title: __( '16:9' ),
aspect: 16 / 9,
},
{
title: __( '4:3' ),
aspect: 4 / 3,
},
{
title: __( '3:2' ),
aspect: 3 / 2,
},
] }
/>
<AspectGroup
label={ __( '肖像' ) }
isDisabled={ isInProgress }
onClick={ ( newAspect ) => {
setAspect( newAspect );
onClose();
} }
value={ aspect }
aspectRatios={ [
{
title: __( '10:16' ),
aspect: 10 / 16,
},
{
title: __( '9:16' ),
aspect: 9 / 16,
},
{
title: __( '3:4' ),
aspect: 3 / 4,
},
{
title: __( '2:3' ),
aspect: 2 / 3,
},
] }
/>
</>
) }
</DropdownMenu>
);
}