Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
/**
* WordPress dependencies
*/
import { RangeControl, SelectControl } from '@wordpress/components';
import { SelectControl } from '@wordpress/components';
import { Stack, VisuallyHidden } from '@wordpress/ui';
import { __, sprintf } from '@wordpress/i18n';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { useMediaEditor } from '../../state';
import {
useCropGestureHandlers,
CROP_CONTROL_ATTR,
} from '../../hooks/use-crop-gesture-handlers';
import { CROP_CONTROL_ATTR } from '../../hooks/use-crop-gesture-handlers';
import MediaEditorImageControls from '../media-editor-image-controls';
import { MAX_ZOOM } from '../../image-editor/core/constants';
import { getMinZoom } from '../../image-editor/core/containment';
import type { AspectRatioPreset } from '../../image-editor/core/constants';

const ZOOM_PERCENTAGE_SCALE = 100;
const MAX_ZOOM_PERCENTAGE = MAX_ZOOM * ZOOM_PERCENTAGE_SCALE;

function getZoomPercentageForDisplay( zoom: number ): number {
return Math.round( zoom * ZOOM_PERCENTAGE_SCALE );
}

function getMinZoomPercentageForDisplay( zoom: number ): number {
return Math.ceil( zoom * ZOOM_PERCENTAGE_SCALE );
}

export interface MediaEditorCropPanelProps {
/**
* Selected aspect-ratio preset value as a string (so it round-trips
Expand All @@ -38,41 +21,32 @@ export interface MediaEditorCropPanelProps {
aspectRatioValue: string;
/** Setter for the aspect-ratio preset value. */
onAspectRatioChange: ( value: string ) => void;
/** Signal that a placement-oriented control is being adjusted. */
onPlacementControlInteraction?: () => void;
/** Aspect-ratio presets to display in the selector. */
aspectRatioOptions: AspectRatioPreset[];
/**
* When `true`, render the rotate/flip transform controls at the top of
* When `true`, render the rotate/flip/zoom image controls at the top of
* the panel. Used on wide viewports where the footer no longer carries
* them.
*/
showTransformControls?: boolean;
}

/**
* Sidebar panel for crop-shape controls. The tactile verbs (rotate, flip)
* live in the bottom toolbar instead.
* Sidebar panel for crop controls. Renders the aspect-ratio selector, plus the
* rotate/flip and zoom controls on wide viewports (these move to the footer
* toolbar when the sidebar collapses).
* @param props
* @param props.aspectRatioValue
* @param props.onAspectRatioChange
* @param props.onPlacementControlInteraction
* @param props.aspectRatioOptions
* @param props.showTransformControls
*/
export default function MediaEditorCropPanel( {
aspectRatioValue,
onAspectRatioChange,
onPlacementControlInteraction,
aspectRatioOptions,
showTransformControls = false,
}: MediaEditorCropPanelProps ) {
const { state, setZoom } = useMediaEditor();
const zoomGestureHandlers = useCropGestureHandlers();
const minZoom = getMinZoom( state );
const zoomPercentage = getZoomPercentageForDisplay( state.zoom );
const minZoomPercentage = getMinZoomPercentageForDisplay( minZoom );

return (
// Tag the whole panel as a crop-control region so the modal's
// Cmd+Z handler doesn't mistake the SelectControl input for a
Expand All @@ -96,36 +70,6 @@ export default function MediaEditorCropPanel( {
value: preset.value.toString(),
} ) ) }
/>
<div role="presentation" { ...zoomGestureHandlers }>
<RangeControl
__next40pxDefaultSize
label={ __( 'Zoom (%)' ) }
min={ minZoomPercentage }
max={ MAX_ZOOM_PERCENTAGE }
step={ 1 }
shiftStep={ 10 }
value={ zoomPercentage }
onChange={ ( value ) => {
onPlacementControlInteraction?.();
setZoom(
typeof value === 'number'
? value / ZOOM_PERCENTAGE_SCALE
: minZoom
);
} }
renderTooltipContent={ ( value ) => {
const percentage =
typeof value === 'number'
? value
: minZoomPercentage;
return sprintf(
/* translators: %d: zoom level as a percentage. */
__( '%d%%' ),
Math.round( percentage )
);
} }
/>
</div>
</Stack>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { fireEvent, render, screen } from '@testing-library/react';
*/
import MediaEditorCropPanel from '..';
import type { MediaEditorCropPanelProps } from '..';
import { MediaEditorStateProvider, useMediaEditor } from '../../../state';
import { MediaEditorStateProvider } from '../../../state';
import type { CropperState } from '../../../image-editor';

function setupCropPanel(
Expand All @@ -29,31 +29,13 @@ function setupCropPanel(
render(
<MediaEditorStateProvider initialCropperState={ initialCropperState }>
<MediaEditorCropPanel { ...props } />
<CurrentZoomValue />
</MediaEditorStateProvider>
);

return props;
}

function CurrentZoomValue() {
const { state } = useMediaEditor();

return <output data-testid="current-zoom">{ state.zoom }</output>;
}

describe( 'MediaEditorCropPanel', () => {
it( 'renders crop shape controls before zoom controls', () => {
setupCropPanel();

const aspectRatio = screen.getByLabelText( 'Aspect ratio' );
const zoom = screen.getByRole( 'slider', { name: 'Zoom (%)' } );

expect( aspectRatio.compareDocumentPosition( zoom ) ).toBe(
Node.DOCUMENT_POSITION_FOLLOWING
);
} );

it( 'passes selected aspect ratio changes to the caller', () => {
const controls = setupCropPanel( {
aspectRatioValue: '1',
Expand All @@ -69,54 +51,36 @@ describe( 'MediaEditorCropPanel', () => {
).toBe( '0' );
} );

it( 'displays zoom as a percentage without changing cropper state', () => {
setupCropPanel( {}, { zoom: 3.749999999999999 } );

const zoomInput = screen.getByRole( 'spinbutton', {
name: 'Zoom (%)',
} );

expect( zoomInput ).toHaveValue( 375 );
expect( screen.getByTestId( 'current-zoom' ) ).toHaveTextContent(
'3.749999999999999'
);
} );

it( 'converts percentage input back to the cropper zoom multiplier', () => {
const controls = setupCropPanel( {
onPlacementControlInteraction: jest.fn(),
} );

fireEvent.change(
screen.getByRole( 'spinbutton', { name: 'Zoom (%)' } ),
{
target: { value: '250' },
}
);

expect( screen.getByTestId( 'current-zoom' ) ).toHaveTextContent(
'2.5'
);
expect( controls.onPlacementControlInteraction ).toHaveBeenCalled();
} );

it( 'omits transform controls by default', () => {
it( 'omits the image controls by default', () => {
setupCropPanel();

expect( screen.queryByText( 'Rotate' ) ).not.toBeInTheDocument();
expect( screen.queryByText( 'Flip' ) ).not.toBeInTheDocument();
expect( screen.queryByText( 'Zoom' ) ).not.toBeInTheDocument();
} );

it( 'renders rotate and flip controls when showTransformControls is set', () => {
it( 'renders rotate, flip and zoom controls when showTransformControls is set', () => {
setupCropPanel( { showTransformControls: true } );

expect( screen.getByText( 'Rotate' ) ).toBeInTheDocument();
expect( screen.getByText( 'Flip' ) ).toBeInTheDocument();
expect( screen.getByText( 'Zoom' ) ).toBeInTheDocument();
expect(
screen.getByRole( 'button', { name: 'Rotate 90° clockwise' } )
).toBeInTheDocument();
expect(
screen.getByRole( 'button', { name: 'Flip horizontal' } )
screen.getByRole( 'button', { name: 'Zoom in' } )
).toBeInTheDocument();
} );

it( 'renders the image controls above the aspect-ratio selector', () => {
setupCropPanel( { showTransformControls: true } );

const rotate = screen.getByText( 'Rotate' );
const aspectRatio = screen.getByLabelText( 'Aspect ratio' );

expect( rotate.compareDocumentPosition( aspectRatio ) ).toBe(
Node.DOCUMENT_POSITION_FOLLOWING
);
} );
} );
Loading
Loading