Repository navigation
Media Editor Modal: In the image cropper, pan when dragging the crop instead of constraining to the visible area #77899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
6ca3f31
bc7228b
1e34096
dfde8f8
0a7d202
c1061e5
745631c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import type { ViewportState, ViewportAction } from './types'; | ||
|
|
||
| export const DEFAULT_VIEWPORT_STATE: ViewportState = { | ||
| zoom: 1, | ||
| pan: { x: 0, y: 0 }, | ||
| }; | ||
|
|
||
| export function viewportReducer( | ||
| state: ViewportState, | ||
| action: ViewportAction | ||
| ): ViewportState { | ||
| switch ( action.type ) { | ||
| case 'SET_VIEWPORT_ZOOM': | ||
| return { | ||
| ...state, | ||
| zoom: Math.min( 4, Math.max( 0.1, action.payload ) ), | ||
| }; | ||
| case 'SET_VIEWPORT_PAN': | ||
| return { ...state, pan: action.payload }; | ||
| case 'RESET_VIEWPORT': | ||
| return DEFAULT_VIEWPORT_STATE; | ||
| } | ||
|
andrewserong marked this conversation as resolved.
Resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ import { useAriaAnnouncer } from '../hooks/use-aria-announcer'; | |
| import { RectangleStencil } from './stencils/rectangle-stencil'; | ||
| import { DimmingOverlay } from './overlays/dimming-overlay'; | ||
| import { GridOverlay } from './overlays/grid-overlay'; | ||
| import { ViewportProvider, useViewport } from './viewport-provider'; | ||
| import './cropper.scss'; | ||
|
|
||
| /** Threshold for comparing normalized crop rect values. */ | ||
|
|
@@ -169,6 +170,11 @@ function CropperInner( | |
| ref: React.ForwardedRef< HTMLDivElement > | ||
| ) { | ||
| const { state, setImage, setCropRect, settleCrop } = controller; | ||
| const { | ||
| viewport: viewportState, | ||
| setViewportPan, | ||
| resetViewport, | ||
| } = useViewport(); | ||
| // Canvas measurement via ResizeObserver. The canvas is the inner | ||
| // positioning context for image/stencil/handles — inset from the root | ||
| // by the handle gutter, so crop math operates on the reduced box. | ||
|
|
@@ -274,8 +280,8 @@ function CropperInner( | |
| if ( ! state.image || elementSize.width === 0 ) { | ||
| return undefined; | ||
| } | ||
| return getCropBounds( state, elementSize, visualSize, canvasSize ); | ||
| }, [ state, elementSize, visualSize, canvasSize ] ); | ||
| return getCropBounds( state, elementSize, visualSize ); | ||
| }, [ state, elementSize, visualSize ] ); | ||
| const [ isResizing, setIsResizing ] = useState( false ); | ||
| const isResizingRef = useRef( false ); | ||
|
|
||
|
|
@@ -342,14 +348,41 @@ function CropperInner( | |
| [ src, setImage, onImageLoaded ] | ||
| ); | ||
|
|
||
| /** | ||
| * Handle crop rect changes from the stencil (during drag). | ||
| */ | ||
| const handleCropChange = useCallback( | ||
| ( rect: NormalizedRect ) => { | ||
| setCropRect( rect ); | ||
| // During a resize drag, pan the viewport so the handle stays | ||
| // visible even when the crop extends beyond the canvas edge. | ||
| if ( isResizingRef.current && visualSize.width > 0 ) { | ||
|
andrewserong marked this conversation as resolved.
Resolved
andrewserong marked this conversation as resolved.
Resolved
andrewserong marked this conversation as resolved.
Resolved
|
||
| const offsetX = ( canvasSize.width - visualSize.width ) / 2; | ||
| const offsetY = ( canvasSize.height - visualSize.height ) / 2; | ||
| const rightOverflow = Math.max( | ||
| 0, | ||
| offsetX + | ||
| ( rect.x + rect.width ) * visualSize.width - | ||
| canvasSize.width | ||
| ); | ||
| const leftOverflow = Math.max( | ||
| 0, | ||
| -( offsetX + rect.x * visualSize.width ) | ||
| ); | ||
| const bottomOverflow = Math.max( | ||
| 0, | ||
| offsetY + | ||
| ( rect.y + rect.height ) * visualSize.height - | ||
| canvasSize.height | ||
| ); | ||
| const topOverflow = Math.max( | ||
| 0, | ||
| -( offsetY + rect.y * visualSize.height ) | ||
| ); | ||
| setViewportPan( { | ||
| x: -rightOverflow + leftOverflow, | ||
| y: -bottomOverflow + topOverflow, | ||
| } ); | ||
|
Comment on lines
+387
to
+390
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It also wanted an equality check here for smoother interaction/performance. const nextViewportPan = {
x: -rightOverflow + leftOverflow,
y: -bottomOverflow + topOverflow,
};
if ( ! arePointsEqual( viewportPanRef.current, nextViewportPan ) ) {
viewportPanRef.current = nextViewportPan;
setViewportPan( nextViewportPan );
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cheers. For this one, I think we've already fixed it with the feedback on updating the reducer to bail if values match, so the consumer (this callback) doesn't need to worry about performing the check and avoiding calling |
||
| } | ||
| }, | ||
| [ setCropRect ] | ||
| [ setCropRect, setViewportPan, canvasSize, visualSize ] | ||
|
andrewserong marked this conversation as resolved.
Resolved
|
||
| ); | ||
|
|
||
| // Settling animation: brief linear transition after resize end. | ||
|
|
@@ -380,23 +413,28 @@ function CropperInner( | |
| const handleResizeStart = useCallback( () => { | ||
| isResizingRef.current = true; | ||
| setIsResizing( true ); | ||
| resetViewport(); | ||
| onGestureStart?.(); | ||
|
andrewserong marked this conversation as resolved.
Resolved
|
||
| }, [ onGestureStart ] ); | ||
| }, [ onGestureStart, resetViewport ] ); | ||
|
|
||
| /** | ||
| * Handle resize end — settle the crop rect (re-center, fill height). | ||
| * Handle resize end — settle the crop rect (re-center, fill height) | ||
| * and reset the viewport pan back to neutral. | ||
| */ | ||
| const handleResizeEnd = useCallback( () => { | ||
| isResizingRef.current = false; | ||
| setIsResizing( false ); | ||
| setSettling( true ); | ||
| // Reset viewport pan first so it transitions back to zero in sync | ||
| // with the settle animation on the image. | ||
| resetViewport(); | ||
| settleCrop(); | ||
| onGestureEnd?.(); | ||
| clearTimeout( settleTimerRef.current ); | ||
| settleTimerRef.current = setTimeout( () => { | ||
| setSettling( false ); | ||
| }, 200 ); | ||
| }, [ settleCrop, onGestureEnd ] ); | ||
| }, [ settleCrop, onGestureEnd, resetViewport ] ); | ||
|
|
||
| const imageTransition = | ||
| settling || isZooming ? 'transform 150ms linear' : undefined; | ||
|
|
@@ -423,6 +461,20 @@ function CropperInner( | |
| }; | ||
| }, [ canvasSize, elementSize, transformString, imageTransition ] ); | ||
|
|
||
| // Viewport pan CSS transform for the canvas div. Applied during resize | ||
| // drags to keep handles visible when the crop extends past the canvas edge. | ||
| // Transitions back to zero during the settle animation. | ||
| const settleTransition = settling ? 'transform 200ms ease-out' : undefined; | ||
| let canvasStyle: React.CSSProperties | undefined; | ||
| if ( viewportState.pan.x !== 0 || viewportState.pan.y !== 0 ) { | ||
| canvasStyle = { | ||
| transform: `translate(${ viewportState.pan.x }px, ${ viewportState.pan.y }px)`, | ||
|
andrewserong marked this conversation as resolved.
Resolved
|
||
| transition: settleTransition, | ||
|
andrewserong marked this conversation as resolved.
Resolved
|
||
| }; | ||
| } else if ( settling ) { | ||
| canvasStyle = { transition: settleTransition }; | ||
|
andrewserong marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| // Forward the root element to the consumer's ref. | ||
| const setContainerRef = useCallback( | ||
| ( element: HTMLDivElement | null ) => { | ||
|
|
@@ -467,6 +519,7 @@ function CropperInner( | |
| showInteractiveGrid && | ||
| 'wp-media-editor-image-editor__canvas--show-grid' | ||
| ) } | ||
| style={ canvasStyle } | ||
| tabIndex={ 0 } | ||
| role="group" | ||
| aria-label={ __( 'Image editor' ) } | ||
|
|
@@ -539,6 +592,14 @@ function CropperInner( | |
| ); | ||
| } | ||
|
|
||
| export const Cropper = forwardRef< HTMLDivElement, CropperProps >( | ||
| const CropperInnerWithRef = forwardRef< HTMLDivElement, CropperProps >( | ||
| CropperInner | ||
| ); | ||
|
|
||
| export const Cropper = forwardRef< HTMLDivElement, CropperProps >( | ||
| ( props, ref ) => ( | ||
| <ViewportProvider> | ||
| <CropperInnerWithRef { ...props } ref={ ref } /> | ||
| </ViewportProvider> | ||
| ) | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { createContext, useContext } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { | ||
| useViewportState, | ||
| type UseViewportStateReturn, | ||
| } from '../hooks/use-viewport-state'; | ||
|
|
||
| type ViewportContextValue = UseViewportStateReturn; | ||
|
|
||
| const ViewportContext = createContext< ViewportContextValue | null >( null ); | ||
|
|
||
| export function ViewportProvider( { | ||
| children, | ||
| }: { | ||
| children: React.ReactNode; | ||
| } ) { | ||
| const viewportState = useViewportState(); | ||
| return ( | ||
| <ViewportContext.Provider value={ viewportState }> | ||
| { children } | ||
| </ViewportContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export function useViewport(): UseViewportStateReturn { | ||
| const context = useContext( ViewportContext ); | ||
| if ( ! context ) { | ||
| throw new Error( | ||
| 'useViewport must be used within a ViewportProvider.' | ||
| ); | ||
| } | ||
| return context; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.