Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
db2536c
Media Editor: Add bounded advanced crop controls
ramonjd May 12, 2026
2d9b094
Bound history per focus session in advanced crop controls
ramonjd May 12, 2026
b80e62c
Use image-bound draft previews for advanced crop controls
ramonjd May 13, 2026
c7a2a8c
Remove dupe tests
ramonjd May 13, 2026
0e222bc
Treat the preview rect as the crop area, not a separate overlay
ramonjd May 13, 2026
cb869f6
Implement offset normalization for fine rotation display
ramonjd May 13, 2026
f466f79
Fix crop geometry minimum size import
ramonjd May 19, 2026
e67bfaf
Fix cropper measurement cleanup on unmount
ramonjd May 19, 2026
0675758
Fix media editor geometry rebase fallout
ramonjd Jun 3, 2026
ec5aa55
Refactor useDeferredCommit to improve blur handling and add test for …
ramonjd Jun 5, 2026
875e60a
Update media editor modal styles to remove unnecessary margins and ad…
ramonjd Jun 10, 2026
31fd4ba
Fix formatting
ramonjd Jun 10, 2026
a13cd05
Remove unused prop from NumberControl in CropInput component
ramonjd Jul 15, 2026
4cafa49
Crop advanced panel: replace Flex/FlexItem with Stack and CSS
ramonjd Jul 30, 2026
0a971a7
Drop dependency group comments after rebase
ramonjd Aug 11, 2026
d319ee4
Name DOM tests for the jsdom environment
ramonjd Aug 31, 2026
ad28f90
Point the Advanced panel styles at the new sidebar pane
ramonjd Aug 31, 2026
1d9e33e
Read the cropper from the media editor session
ramonjd Oct 2, 2026
04a2a0c
Move the new tests to Vitest
ramonjd Oct 2, 2026
1b8b59c
Reformat for wp-prettier 3.9.6
ramonjd Oct 2, 2026
2a6b098
Lock the Advanced panel fields while saving
ramonjd Oct 2, 2026
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
@@ -0,0 +1,142 @@
import {
__experimentalInputControlSuffixWrapper as InputControlSuffixWrapper,
PanelBody,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { Stack } from '@wordpress/ui';
import { useCropGestureHandlers } from '../../hooks/use-crop-gesture-handlers';
import CropInput from './crop-input';
import { useAdvancedCropControls } from './use-advanced-crop-controls';

interface CropAdvancedPanelProps {
aspectRatio?: number;
freeformCrop: boolean;
onPlacementControlInteraction?: () => void;
disabled?: boolean;
}

const DEGREE_SUFFIX = <InputControlSuffixWrapper>°</InputControlSuffixWrapper>;

export default function CropAdvancedPanel( {
aspectRatio,
freeformCrop,
onPlacementControlInteraction,
disabled = false,
}: CropAdvancedPanelProps ) {
const controls = useAdvancedCropControls( {
aspectRatio,
freeformCrop,
onPlacementControlInteraction,
} );
const gestureHandlers = useCropGestureHandlers( { commitOnKeyUp: false } );

if ( ! controls.isReady ) {
return null;
}

const {
rect,
ranges,
fineRotation,
onPreview,
onEdit,
onEditEnd,
onSessionStart,
onSessionEnd,
canMoveCropRect,
} = controls;

return (
<PanelBody
title={ __( 'Advanced' ) }
initialOpen={ false }
className="media-editor-crop-advanced-panel"
>
<Stack
direction="column"
gap="sm"
role="presentation"
{ ...gestureHandlers }
>
<CropInput
label={ __( 'Fine rotation' ) }
aria-label={ __( 'Fine rotation angle' ) }
value={ fineRotation.offset }
range={ fineRotation.range }
step={ fineRotation.step }
commitStep={ fineRotation.step }
suffix={ DEGREE_SUFFIX }
disabled={ disabled }
onCommit={ fineRotation.onEdit }
onCommitEnd={ fineRotation.onEditEnd }
onSessionStart={ onSessionStart }
onSessionEnd={ onSessionEnd }
/>
<Stack
className="media-editor-crop-advanced-panel__row"
direction="row"
gap="sm"
align="flex-start"
>
<CropInput
label={ __( 'X' ) }
aria-label={ __( 'Crop horizontal position' ) }
value={ rect.left }
range={ ranges.left }
disabled={
disabled ||
! canMoveCropRect ||
! ranges.left.isEditable
}
commitOnChange={ false }
onPreview={ ( value ) => onPreview( 'left', value ) }
onCommit={ ( value ) => onEdit( 'left', value ) }
onCommitEnd={ onEditEnd }
/>
<CropInput
label={ __( 'Y' ) }
aria-label={ __( 'Crop vertical position' ) }
value={ rect.top }
range={ ranges.top }
disabled={
disabled ||
! canMoveCropRect ||
! ranges.top.isEditable
}
commitOnChange={ false }
onPreview={ ( value ) => onPreview( 'top', value ) }
onCommit={ ( value ) => onEdit( 'top', value ) }
onCommitEnd={ onEditEnd }
/>
</Stack>
<Stack
className="media-editor-crop-advanced-panel__row"
direction="row"
gap="sm"
align="flex-start"
>
<CropInput
label={ __( 'Width' ) }
value={ rect.width }
range={ ranges.width }
disabled={ disabled || ! ranges.width.isEditable }
commitOnChange={ false }
onPreview={ ( value ) => onPreview( 'width', value ) }
onCommit={ ( value ) => onEdit( 'width', value ) }
onCommitEnd={ onEditEnd }
/>
<CropInput
label={ __( 'Height' ) }
value={ rect.height }
range={ ranges.height }
disabled={ disabled || ! ranges.height.isEditable }
commitOnChange={ false }
onPreview={ ( value ) => onPreview( 'height', value ) }
onCommit={ ( value ) => onEdit( 'height', value ) }
onCommitEnd={ onEditEnd }
/>
</Stack>
</Stack>
</PanelBody>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
export interface CropInputRange {
minValue: number;
maxValue: number;
isEditable: boolean;
}

export interface CropInputBounds {
value: number;
min: number;
max: number;
}

export const INPUT_VALUE_EPSILON = 1e-6;

function getStepPrecision( step: number ): number {
const stepString = step.toString();
const decimalIndex = stepString.indexOf( '.' );
return decimalIndex === -1 ? 0 : stepString.length - decimalIndex - 1;
}

export function snapInputValueToStep( value: number, step: number ): number {
const precision = getStepPrecision( step );
const snapped = Math.round( value / step ) * step;
return Number( snapped.toFixed( precision ) );
}

function roundIfNearStep( value: number, step: number ): number {
const snapped = snapInputValueToStep( value, step );
return Math.abs( value - snapped ) < INPUT_VALUE_EPSILON ? snapped : value;
}

function ceilInputValueToStep( value: number, step: number ): number {
return snapInputValueToStep(
Math.ceil( roundIfNearStep( value, step ) / step ) * step,
step
);
}

function floorInputValueToStep( value: number, step: number ): number {
return snapInputValueToStep(
Math.floor( roundIfNearStep( value, step ) / step ) * step,
step
);
}

export function makeRange(
minValue: number,
maxValue: number,
isEditable = true
): CropInputRange {
const max = Math.max( minValue, maxValue );
return {
minValue,
maxValue: max,
isEditable: isEditable && max > minValue,
};
}

export function getInputBounds(
value: number,
range: CropInputRange,
commitStep: number
): CropInputBounds {
const snapped = snapInputValueToStep( value, commitStep );
const min = ceilInputValueToStep( range.minValue, commitStep );
const max = floorInputValueToStep( range.maxValue, commitStep );

if ( max < min ) {
return {
value: snapped,
min: snapped,
max: snapped,
};
}

// Keep the displayed value separate from the commit range. A transient
// cropper state may already be outside bounds after rotation, but arrow,
// wheel, and typed commits should still be judged against the real range.
return {
value: snapped,
min,
max,
};
}

export function getInputCommitValue(
nextValue: string,
bounds: CropInputBounds,
commitStep: number,
clampToBounds = false
): number | null {
if ( nextValue.trim() === '' ) {
return null;
}

const parsed = Number( nextValue );
if ( ! Number.isFinite( parsed ) ) {
return null;
}

const snapped = snapInputValueToStep( parsed, commitStep );
if ( snapped < bounds.min || snapped > bounds.max ) {
if ( ! clampToBounds ) {
return null;
}
return Math.min( bounds.max, Math.max( bounds.min, snapped ) );
}

return snapped;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {
__experimentalInputControlSuffixWrapper as InputControlSuffixWrapper,
__experimentalNumberControl as NumberControl,
} from '@wordpress/components';
import type { CropInputRange } from './crop-input-utils';
import { useDeferredCommit } from './use-deferred-commit';

export interface CropInputProps {
label: string;
'aria-label'?: string;
value: number;
range: CropInputRange;
disabled?: boolean;
/** Display step used by the underlying NumberControl (arrow-key increment). */
step?: number;
/** Snap granularity applied when a value is committed. Defaults to `step`. */
commitStep?: number;
suffix?: React.ReactNode;
onCommit: ( value: number ) => void;
onCommitEnd?: () => void;
commitOnChange?: boolean;
onPreview?: ( value: number | null ) => void;
onSessionStart?: () => void;
onSessionEnd?: () => void;
}

const PX_SUFFIX = <InputControlSuffixWrapper>px</InputControlSuffixWrapper>;

/**
* Numeric crop control with live preview and deferred finalization.
*
* All of the focus / draft / Enter / Escape state lives in
* `useDeferredCommit`; this component just renders.
*
* @param props Component props.
* @return Rendered number control.
*/
export default function CropInput( props: CropInputProps ) {
const {
label,
'aria-label': ariaLabel,
value,
range,
disabled = false,
step = 1,
commitStep = step,
suffix = PX_SUFFIX,
onCommit,
onCommitEnd,
commitOnChange,
onPreview,
onSessionStart,
onSessionEnd,
} = props;
const handlers = useDeferredCommit( {
value,
range,
commitStep,
onCommit,
onCommitEnd,
commitOnChange,
onPreview,
onSessionStart,
onSessionEnd,
} );

return (
<NumberControl
label={ label }
aria-label={ ariaLabel }
step={ step }
disabled={ disabled }
suffix={ suffix }
{ ...handlers }
/>
);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { SelectControl as WCSelectControl } from '@wordpress/components';
import { Stack, VisuallyHidden } from '@wordpress/ui';
import { __ } from '@wordpress/i18n';
import { resolveAspectRatio, useMediaEditor } from '../../state';
import { CROP_CONTROL_ATTR } from '../../hooks/use-crop-gesture-handlers';
import MediaEditorImageControls from '../media-editor-image-controls';
import type { AspectRatioPreset } from '../../image-editor/core/constants';
import CropAdvancedPanel from './crop-advanced-panel';

export interface MediaEditorCropPanelProps {
/**
Expand All @@ -14,6 +16,8 @@ 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[];
/** Disable every control in the panel while the edit is saving. */
Expand All @@ -27,15 +31,23 @@ export interface MediaEditorCropPanelProps {
* @param props
* @param props.aspectRatioValue
* @param props.onAspectRatioChange
* @param props.onPlacementControlInteraction
* @param props.aspectRatioOptions
* @param props.disabled
*/
export default function MediaEditorCropPanel( {
aspectRatioValue,
onAspectRatioChange,
onPlacementControlInteraction,
aspectRatioOptions,
disabled = false,
}: MediaEditorCropPanelProps ) {
const { state } = useMediaEditor().cropper;
const resolvedAspectRatio = resolveAspectRatio(
aspectRatioValue,
state.image
);

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 @@ -59,6 +71,12 @@ export default function MediaEditorCropPanel( {
value: preset.value.toString(),
} ) ) }
/>
<CropAdvancedPanel
aspectRatio={ resolvedAspectRatio }
freeformCrop
onPlacementControlInteraction={ onPlacementControlInteraction }
disabled={ disabled }
/>
</Stack>
);
}
Loading
Loading