Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -14,6 +14,11 @@ export interface MediaEditorCanvasProps {
onGestureStart?: () => void;
/** Fires when a canvas cropper gesture ends. */
onGestureEnd?: () => void;
/**
* Disable crop interaction on the canvas. Set while the edit is saving,
* so a drag cannot change the crop after the request was built.
*/
disabled?: boolean;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the thing can be disabled at a higher level, by consumers

}

/**
Expand All @@ -28,11 +33,13 @@ export interface MediaEditorCanvasProps {
* @param props.isPlacementActive
* @param props.onGestureStart
* @param props.onGestureEnd
* @param props.disabled
*/
export default function MediaEditorCanvas( {
isPlacementActive = false,
onGestureStart,
onGestureEnd,
disabled = false,
}: MediaEditorCanvasProps ) {
const { media } = useMediaEditorContext();
const controller = useMediaEditor();
Expand Down Expand Up @@ -159,6 +166,7 @@ export default function MediaEditorCanvas( {
isPlacementActive={ isPlacementActive }
onGestureStart={ handleGestureStart }
onGestureEnd={ handleGestureEnd }
disabled={ disabled }
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface MediaEditorCropPanelProps {
onAspectRatioChange: ( value: string ) => void;
/** Aspect-ratio presets to display in the selector. */
aspectRatioOptions: AspectRatioPreset[];
/** Disable every control in the panel while the edit is saving. */
disabled?: boolean;
}

/**
Expand All @@ -26,11 +28,13 @@ export interface MediaEditorCropPanelProps {
* @param props.aspectRatioValue
* @param props.onAspectRatioChange
* @param props.aspectRatioOptions
* @param props.disabled
*/
export default function MediaEditorCropPanel( {
aspectRatioValue,
onAspectRatioChange,
aspectRatioOptions,
disabled = false,
}: MediaEditorCropPanelProps ) {
return (
// Tag the whole panel as a crop-control region so the modal's
Expand All @@ -44,11 +48,12 @@ export default function MediaEditorCropPanel( {
<VisuallyHidden render={ <h2 /> }>
{ __( 'Crop options' ) }
</VisuallyHidden>
<MediaEditorImageControls withLabels />
<MediaEditorImageControls withLabels disabled={ disabled } />
<SelectControl
label={ __( 'Aspect ratio' ) }
value={ aspectRatioValue }
onChange={ onAspectRatioChange }
disabled={ disabled }
options={ aspectRatioOptions.map( ( preset ) => ( {
label: preset.label,
value: preset.value.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import RotationRuler from '../rotation-ruler';
export interface MediaEditorFineRotationProps {
/** Signal that a placement-oriented control is being adjusted. */
onPlacementControlInteraction?: () => void;
/**
* Disable the ruler. Set while the edit is saving; `RotationRuler`
* cancels an in-flight drag when this flips.
*/
disabled?: boolean;
}

/**
Expand All @@ -17,9 +22,11 @@ export interface MediaEditorFineRotationProps {
*
* @param props
* @param props.onPlacementControlInteraction
* @param props.disabled
*/
export default function MediaEditorFineRotation( {
onPlacementControlInteraction,
disabled = false,
}: MediaEditorFineRotationProps ) {
const { state, setRotation } = useMediaEditor();
// `commitOnKeyUp: false` lets rapid arrow-key adjustments coalesce
Expand Down Expand Up @@ -62,6 +69,7 @@ export default function MediaEditorFineRotation( {
max={ MAX_ROTATION_OFFSET }
value={ fineOffset }
onChange={ handleRotationSlider }
disabled={ disabled }
Comment thread
ramonjd marked this conversation as resolved.
Resolved
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export interface MediaEditorImageControlsProps {
* `DEFAULT_ZOOM_FACTOR` (1.2).
*/
zoomFactor?: number;
/**
* Disable every control. Set while the edit is saving, so the crop the
* request was built from cannot change under it.
*/
disabled?: boolean;
}

/**
Expand All @@ -67,12 +72,14 @@ export interface MediaEditorImageControlsProps {
* @param props.showAspectRatioControl
* @param props.aspectRatioPresets
* @param props.zoomFactor
* @param props.disabled
*/
export default function MediaEditorImageControls( {
withLabels = false,
showAspectRatioControl = false,
aspectRatioPresets,
zoomFactor = DEFAULT_ZOOM_FACTOR,
disabled = false,
}: MediaEditorImageControlsProps ) {
const { state, setFlip, snapRotate90, setZoom } = useMediaEditor();
const { aspectRatioValue, setAspectRatioValue, aspectRatioOptions } =
Expand All @@ -92,13 +99,17 @@ export default function MediaEditorImageControls( {
icon={ rotateLeft }
label={ __( 'Rotate 90° counter-clockwise' ) }
showTooltip
disabled={ disabled }
accessibleWhenDisabled
onClick={ () => snapRotate90( -1 ) }
/>
<Button
size="compact"
icon={ rotateRight }
label={ __( 'Rotate 90° clockwise' ) }
showTooltip
disabled={ disabled }
accessibleWhenDisabled
onClick={ () => snapRotate90( 1 ) }
/>
</>
Expand All @@ -112,6 +123,8 @@ export default function MediaEditorImageControls( {
label={ __( 'Flip horizontal' ) }
showTooltip
isPressed={ state.flip.horizontal }
disabled={ disabled }
accessibleWhenDisabled
onClick={ () =>
setFlip( {
horizontal: ! state.flip.horizontal,
Expand All @@ -125,6 +138,8 @@ export default function MediaEditorImageControls( {
label={ __( 'Flip vertical' ) }
showTooltip
isPressed={ state.flip.vertical }
disabled={ disabled }
accessibleWhenDisabled
onClick={ () =>
setFlip( {
horizontal: state.flip.horizontal,
Expand All @@ -142,7 +157,7 @@ export default function MediaEditorImageControls( {
icon={ plus }
label={ __( 'Zoom in' ) }
showTooltip
disabled={ state.zoom >= MAX_ZOOM }
disabled={ disabled || state.zoom >= MAX_ZOOM }
accessibleWhenDisabled
onClick={ () => zoomByFactor( zoomFactor ) }
/>
Expand All @@ -151,7 +166,7 @@ export default function MediaEditorImageControls( {
icon={ lineSolid }
label={ __( 'Zoom out' ) }
showTooltip
disabled={ state.zoom <= minZoom }
disabled={ disabled || state.zoom <= minZoom }
accessibleWhenDisabled
onClick={ () => zoomByFactor( 1 / zoomFactor ) }
/>
Expand All @@ -163,7 +178,7 @@ export default function MediaEditorImageControls( {
icon={ aspectRatioIcon }
label={ __( 'Aspect ratio' ) }
popoverProps={ { placement: 'top' } }
toggleProps={ { size: 'compact' } }
toggleProps={ { size: 'compact', disabled } }
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
>
{ ( { onClose } ) => (
<MenuGroup label={ __( 'Aspect ratio' ) }>
Expand All @@ -176,7 +191,11 @@ export default function MediaEditorImageControls( {
role="menuitemradio"
isSelected={ isSelected }
icon={ isSelected ? check : undefined }
disabled={ disabled }
onClick={ () => {
if ( disabled ) {
return;
}
setAspectRatioValue( value );
onClose();
} }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { useState } from '@wordpress/element';
import MediaEditorImageControls from '..';
import type { MediaEditorImageControlsProps } from '..';
import { MediaEditorStateProvider, useMediaEditor } from '../../../state';
Expand Down Expand Up @@ -130,6 +131,41 @@ describe( 'MediaEditorImageControls', () => {
);
} );

it( 'ignores an aspect ratio chosen from a menu left open when disabled', async () => {
function Harness() {
const [ locked, setLocked ] = useState( false );
return (
<MediaEditorStateProvider>
<MediaEditorImageControls
showAspectRatioControl
disabled={ locked }
/>
<button onClick={ () => setLocked( true ) }>lock</button>
<CurrentState />
</MediaEditorStateProvider>
);
}
render( <Harness /> );

fireEvent.click(
screen.getByRole( 'button', { name: 'Aspect ratio' } )
);
const before = screen.getByTestId( 'current-aspect-ratio' ).textContent;

// The popover stays mounted when the toggle becomes disabled, so
// the items have to refuse the change themselves.
fireEvent.click( screen.getByRole( 'button', { name: 'lock' } ) );
fireEvent.click(
screen.getByRole( 'menuitemradio', { name: 'Square (1:1)' } )
);

await waitFor( () =>
expect(
screen.getByTestId( 'current-aspect-ratio' )
).toHaveTextContent( before ?? '' )
);
} );

it( 'omits the aspect ratio dropdown from the labelled panel layout', () => {
setup( {
withLabels: true,
Expand Down
45 changes: 40 additions & 5 deletions packages/media-editor/src/components/media-editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,15 @@ interface MediaEditorSidebarProps {
/** The open panel's id. Always one of `tabs`. */
activeTab: string;
onSelectTab: ( tab: string ) => void;
/** Freeze the open panel while the edit is saving. */
disabled?: boolean;
}

function MediaEditorSidebar( {
tabs,
activeTab,
onSelectTab,
disabled = false,
}: MediaEditorSidebarProps ) {
return (
<NavigableRegion
Expand All @@ -146,7 +149,11 @@ function MediaEditorSidebar( {
<div className="media-editor__tablist">
<Tabs.List variant="minimal">
{ tabs.map( ( tab ) => (
<Tabs.Tab key={ tab.id } value={ tab.id }>
<Tabs.Tab
key={ tab.id }
value={ tab.id }
disabled={ disabled }
>
{ tab.title }
</Tabs.Tab>
) ) }
Expand Down Expand Up @@ -320,6 +327,9 @@ function HistoryActions() {
redoCrop();
};
const handleReset = () => {
if ( isUndoRedoDisabled ) {
return;
}
beginGesture();
reset();
onReset();
Expand All @@ -334,7 +344,7 @@ function HistoryActions() {
<Button
size="compact"
variant="tertiary"
disabled={ ! isDirty }
disabled={ isUndoRedoDisabled || ! isDirty }
accessibleWhenDisabled
onClick={ handleReset }
>
Expand Down Expand Up @@ -600,6 +610,14 @@ function MediaEditorContent( {
} );

const handleChange = ( updates: Partial< Media > ) => {
// A save reads the pending edits once and then waits. An edit
// arriving after that read misses the request, and a crop save
// then clears the edits for this id, so it would be lost rather
// than merely late. It would also land on the attachment being
// replaced, not the new one the modal moves to.
if ( isSaving ) {
return;
}
editEntityRecord( 'postType', 'attachment', id, updates );
};

Expand Down Expand Up @@ -634,7 +652,7 @@ function MediaEditorContent( {
! target.closest( `[${ CROP_CONTROL_ATTR }]` );
if ( ! isMetadataField ) {
event.preventDefault();
if ( isCropInteractionActive ) {
if ( isCropInteractionActive || isSaving ) {
return;
}
if ( isRedoShortcut ) {
Expand Down Expand Up @@ -677,6 +695,7 @@ function MediaEditorContent( {
<MediaEditorImageControls
showAspectRatioControl
aspectRatioPresets={ aspectRatioPresets }
disabled={ isSaving }
/>
) : null;

Expand All @@ -694,6 +713,7 @@ function MediaEditorContent( {
aspectRatioValue={ aspectRatioValue }
onAspectRatioChange={ setAspectRatioValue }
aspectRatioOptions={ aspectRatioOptions }
disabled={ isSaving }
/>
),
},
Expand All @@ -709,14 +729,24 @@ function MediaEditorContent( {
const ruler = isImage ? (
<MediaEditorFineRotation
onPlacementControlInteraction={ signalPlacementControlInteraction }
disabled={ isSaving }
/>
) : null;

const children = (
<MediaEditorProvider
value={ media ?? undefined }
onChange={ handleChange }
settings={ { fields } }
settings={ {
// Show the fields as read-only while saving, so the guard
// in `handleChange` is not silently swallowing typing.
fields: isSaving
? fields.map( ( field ) => ( {
...field,
readOnly: true,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to mutate the fields while saving, I think we should probably is isDisabled instead of readOnly, otherwise it switches the layout rendering of the field during save, rather than setting the field to disabled. What do you reckon?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You bet. Thanks for reviewing. Much better idea

} ) )
: fields,
} }
>
<div className="media-editor">
{ ! media ? (
Expand Down Expand Up @@ -745,6 +775,7 @@ function MediaEditorContent( {
handleCanvasGestureStart
}
onGestureEnd={ handleCanvasGestureEnd }
disabled={ isSaving }
/>
) : (
<MediaPreview />
Expand Down Expand Up @@ -773,6 +804,7 @@ function MediaEditorContent( {
: DETAILS_PANEL
}
onSelectTab={ selectPanel }
disabled={ isSaving }
/>
) }
</div>
Expand Down Expand Up @@ -805,7 +837,10 @@ function MediaEditorContent( {
isSaving,
hasMedia: !! media,
hasChanges,
isUndoRedoDisabled: isCropInteractionActive,
// Saving freezes the whole edit surface: `save()` reads the
// modifiers once and then awaits, so anything changed after that
// would be silently dropped when the save resolves.
isUndoRedoDisabled: isCropInteractionActive || isSaving,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
aspectRatioPresets,
isWide,
activePanel,
Expand Down
Loading
Loading