@wordpress/media-editor 0.10.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/components/media-editor/index.cjs +185 -42
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +2 -26
- package/build/components/media-editor/use-crop-options.cjs.map +2 -2
- package/build/components/media-editor-canvas/index.cjs +2 -2
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +24 -21
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-fine-rotation/index.cjs +81 -0
- package/build/components/media-editor-fine-rotation/index.cjs.map +7 -0
- package/build/components/media-editor-modal/index.cjs +25 -7
- package/build/components/media-editor-modal/index.cjs.map +2 -2
- package/build/components/media-editor-transform-controls/index.cjs +84 -0
- package/build/components/media-editor-transform-controls/index.cjs.map +7 -0
- package/build/hooks/use-crop-gesture-handlers.cjs +1 -1
- package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +1 -1
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +1 -1
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +1 -3
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/state/composite-reducer.cjs +2 -14
- package/build/state/composite-reducer.cjs.map +2 -2
- package/build/state/types.cjs +1 -2
- package/build/state/types.cjs.map +2 -2
- package/build/state/use-media-editor-state.cjs +0 -11
- package/build/state/use-media-editor-state.cjs.map +2 -2
- package/build/store/actions.cjs +4 -2
- package/build/store/actions.cjs.map +2 -2
- package/build/store/reducer.cjs +5 -3
- package/build/store/reducer.cjs.map +2 -2
- package/build/store/selectors.cjs +5 -0
- package/build/store/selectors.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +191 -44
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +3 -27
- package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
- package/build-module/components/media-editor-canvas/index.mjs +2 -2
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +25 -26
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs +50 -0
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +7 -0
- package/build-module/components/media-editor-modal/index.mjs +26 -8
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-transform-controls/index.mjs +68 -0
- package/build-module/components/media-editor-transform-controls/index.mjs.map +7 -0
- package/build-module/hooks/use-crop-gesture-handlers.mjs +1 -1
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +1 -1
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +1 -1
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +1 -3
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/state/composite-reducer.mjs +2 -14
- package/build-module/state/composite-reducer.mjs.map +2 -2
- package/build-module/state/types.mjs +1 -2
- package/build-module/state/types.mjs.map +2 -2
- package/build-module/state/use-media-editor-state.mjs +0 -11
- package/build-module/state/use-media-editor-state.mjs.map +2 -2
- package/build-module/store/actions.mjs +4 -2
- package/build-module/store/actions.mjs.map +2 -2
- package/build-module/store/reducer.mjs +5 -3
- package/build-module/store/reducer.mjs.map +2 -2
- package/build-module/store/selectors.mjs +4 -0
- package/build-module/store/selectors.mjs.map +2 -2
- package/build-style/style-rtl.css +51 -35
- package/build-style/style.css +51 -35
- package/build-types/components/media-editor/index.d.ts +14 -2
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +3 -5
- package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +3 -10
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-fine-rotation/index.d.ts +14 -0
- package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-transform-controls/index.d.ts +8 -0
- package/build-types/components/media-editor-transform-controls/index.d.ts.map +1 -0
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/types.d.ts +3 -11
- package/build-types/state/types.d.ts.map +1 -1
- package/build-types/state/use-media-editor-state.d.ts +2 -5
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/build-types/store/actions.d.ts +3 -1
- package/build-types/store/actions.d.ts.map +1 -1
- package/build-types/store/reducer.d.ts +3 -0
- package/build-types/store/reducer.d.ts.map +1 -1
- package/build-types/store/selectors.d.ts +1 -0
- package/build-types/store/selectors.d.ts.map +1 -1
- package/package.json +20 -19
- package/src/components/media-editor/index.tsx +226 -57
- package/src/components/media-editor/style.scss +0 -9
- package/src/components/media-editor/test/use-crop-options.tsx +1 -55
- package/src/components/media-editor/use-crop-options.ts +6 -42
- package/src/components/media-editor-canvas/index.tsx +2 -2
- package/src/components/media-editor-crop-panel/index.tsx +35 -33
- package/src/components/media-editor-crop-panel/test/index.tsx +39 -20
- package/src/components/media-editor-fine-rotation/index.tsx +74 -0
- package/src/components/media-editor-modal/index.tsx +20 -4
- package/src/components/media-editor-modal/style.scss +80 -0
- package/src/components/media-editor-modal/test/index.tsx +3 -0
- package/src/components/media-editor-transform-controls/index.tsx +70 -0
- package/src/components/media-editor-transform-controls/style.scss +8 -0
- package/src/hooks/use-crop-gesture-handlers.ts +1 -3
- package/src/image-editor/docs/recipes.md +2 -2
- package/src/image-editor/react/components/cropper.tsx +1 -3
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +1 -3
- package/src/image-editor/react/hooks/use-aria-announcer.ts +1 -3
- package/src/image-editor/react/hooks/use-interaction.ts +2 -3
- package/src/state/composite-reducer.ts +2 -17
- package/src/state/test/use-media-editor-state.ts +3 -75
- package/src/state/types.ts +3 -9
- package/src/state/use-media-editor-state.ts +3 -19
- package/src/store/actions.ts +3 -0
- package/src/store/reducer.ts +6 -1
- package/src/store/selectors.ts +4 -0
- package/src/store/test/reducer.ts +9 -0
- package/src/style.scss +1 -1
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/build/components/media-editor-toolbar/index.cjs +0 -218
- package/build/components/media-editor-toolbar/index.cjs.map +0 -7
- package/build-module/components/media-editor-toolbar/index.mjs +0 -194
- package/build-module/components/media-editor-toolbar/index.mjs.map +0 -7
- package/build-types/components/media-editor-toolbar/index.d.ts +0 -24
- package/build-types/components/media-editor-toolbar/index.d.ts.map +0 -1
- package/src/components/media-editor-toolbar/index.tsx +0 -213
- package/src/components/media-editor-toolbar/style.scss +0 -49
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
5
|
-
RangeControl,
|
|
6
|
-
SelectControl,
|
|
7
|
-
ToggleControl,
|
|
8
|
-
} from '@wordpress/components';
|
|
4
|
+
import { RangeControl, SelectControl } from '@wordpress/components';
|
|
9
5
|
import { Stack, VisuallyHidden } from '@wordpress/ui';
|
|
10
6
|
import { __, sprintf } from '@wordpress/i18n';
|
|
11
7
|
|
|
@@ -21,6 +17,17 @@ import { MAX_ZOOM } from '../../image-editor/core/constants';
|
|
|
21
17
|
import { getMinZoom } from '../../image-editor/core/containment';
|
|
22
18
|
import type { AspectRatioPreset } from '../../image-editor/core/constants';
|
|
23
19
|
|
|
20
|
+
const ZOOM_PERCENTAGE_SCALE = 100;
|
|
21
|
+
const MAX_ZOOM_PERCENTAGE = MAX_ZOOM * ZOOM_PERCENTAGE_SCALE;
|
|
22
|
+
|
|
23
|
+
function getZoomPercentageForDisplay( zoom: number ): number {
|
|
24
|
+
return Math.round( zoom * ZOOM_PERCENTAGE_SCALE );
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function getMinZoomPercentageForDisplay( zoom: number ): number {
|
|
28
|
+
return Math.ceil( zoom * ZOOM_PERCENTAGE_SCALE );
|
|
29
|
+
}
|
|
30
|
+
|
|
24
31
|
export interface MediaEditorCropPanelProps {
|
|
25
32
|
/**
|
|
26
33
|
* Selected aspect-ratio preset value as a string (so it round-trips
|
|
@@ -30,10 +37,6 @@ export interface MediaEditorCropPanelProps {
|
|
|
30
37
|
aspectRatioValue: string;
|
|
31
38
|
/** Setter for the aspect-ratio preset value. */
|
|
32
39
|
onAspectRatioChange: ( value: string ) => void;
|
|
33
|
-
/** Whether the cropper is in freeform (resize-handle) mode. */
|
|
34
|
-
freeformCrop: boolean;
|
|
35
|
-
/** Setter for freeform mode. */
|
|
36
|
-
onFreeformChange: ( value: boolean ) => void;
|
|
37
40
|
/** Signal that a placement-oriented control is being adjusted. */
|
|
38
41
|
onPlacementControlInteraction?: () => void;
|
|
39
42
|
/** Aspect-ratio presets to display in the selector. */
|
|
@@ -41,36 +44,33 @@ export interface MediaEditorCropPanelProps {
|
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
/**
|
|
44
|
-
* Sidebar panel for crop-shape controls
|
|
45
|
-
*
|
|
46
|
-
* bottom toolbar instead.
|
|
47
|
+
* Sidebar panel for crop-shape controls. The tactile verbs (rotate, flip)
|
|
48
|
+
* live in the bottom toolbar instead.
|
|
47
49
|
* @param props
|
|
48
50
|
* @param props.aspectRatioValue
|
|
49
51
|
* @param props.onAspectRatioChange
|
|
50
|
-
* @param props.freeformCrop
|
|
51
|
-
* @param props.onFreeformChange
|
|
52
52
|
* @param props.onPlacementControlInteraction
|
|
53
53
|
* @param props.aspectRatioOptions
|
|
54
54
|
*/
|
|
55
55
|
export default function MediaEditorCropPanel( {
|
|
56
56
|
aspectRatioValue,
|
|
57
57
|
onAspectRatioChange,
|
|
58
|
-
freeformCrop,
|
|
59
|
-
onFreeformChange,
|
|
60
58
|
onPlacementControlInteraction,
|
|
61
59
|
aspectRatioOptions,
|
|
62
60
|
}: MediaEditorCropPanelProps ) {
|
|
63
61
|
const { state, setZoom } = useMediaEditor();
|
|
64
62
|
const zoomGestureHandlers = useCropGestureHandlers();
|
|
65
63
|
const minZoom = getMinZoom( state );
|
|
64
|
+
const zoomPercentage = getZoomPercentageForDisplay( state.zoom );
|
|
65
|
+
const minZoomPercentage = getMinZoomPercentageForDisplay( minZoom );
|
|
66
66
|
|
|
67
67
|
return (
|
|
68
68
|
// Tag the whole panel as a crop-control region so the modal's
|
|
69
|
-
// Cmd+Z handler doesn't mistake the SelectControl
|
|
70
|
-
//
|
|
69
|
+
// Cmd+Z handler doesn't mistake the SelectControl input for a
|
|
70
|
+
// metadata field (which would suppress undo).
|
|
71
71
|
<Stack
|
|
72
72
|
direction="column"
|
|
73
|
-
gap="
|
|
73
|
+
gap="xl"
|
|
74
74
|
{ ...{ [ CROP_CONTROL_ATTR ]: true } }
|
|
75
75
|
>
|
|
76
76
|
<VisuallyHidden render={ <h2 /> }>
|
|
@@ -86,30 +86,32 @@ export default function MediaEditorCropPanel( {
|
|
|
86
86
|
value: preset.value.toString(),
|
|
87
87
|
} ) ) }
|
|
88
88
|
/>
|
|
89
|
-
<ToggleControl
|
|
90
|
-
label={ __( 'Show resize handles' ) }
|
|
91
|
-
checked={ freeformCrop }
|
|
92
|
-
onChange={ onFreeformChange }
|
|
93
|
-
/>
|
|
94
89
|
<div role="presentation" { ...zoomGestureHandlers }>
|
|
95
90
|
<RangeControl
|
|
96
91
|
__next40pxDefaultSize
|
|
97
|
-
label={ __( 'Zoom' ) }
|
|
98
|
-
min={
|
|
99
|
-
max={
|
|
100
|
-
step={
|
|
101
|
-
|
|
92
|
+
label={ __( 'Zoom (%)' ) }
|
|
93
|
+
min={ minZoomPercentage }
|
|
94
|
+
max={ MAX_ZOOM_PERCENTAGE }
|
|
95
|
+
step={ 1 }
|
|
96
|
+
shiftStep={ 10 }
|
|
97
|
+
value={ zoomPercentage }
|
|
102
98
|
onChange={ ( value ) => {
|
|
103
99
|
onPlacementControlInteraction?.();
|
|
104
|
-
setZoom(
|
|
100
|
+
setZoom(
|
|
101
|
+
typeof value === 'number'
|
|
102
|
+
? value / ZOOM_PERCENTAGE_SCALE
|
|
103
|
+
: minZoom
|
|
104
|
+
);
|
|
105
105
|
} }
|
|
106
106
|
renderTooltipContent={ ( value ) => {
|
|
107
|
-
const
|
|
108
|
-
typeof value === 'number'
|
|
107
|
+
const percentage =
|
|
108
|
+
typeof value === 'number'
|
|
109
|
+
? value
|
|
110
|
+
: minZoomPercentage;
|
|
109
111
|
return sprintf(
|
|
110
112
|
/* translators: %d: zoom level as a percentage. */
|
|
111
113
|
__( '%d%%' ),
|
|
112
|
-
Math.round(
|
|
114
|
+
Math.round( percentage )
|
|
113
115
|
);
|
|
114
116
|
} }
|
|
115
117
|
/>
|
|
@@ -8,16 +8,16 @@ import { fireEvent, render, screen } from '@testing-library/react';
|
|
|
8
8
|
*/
|
|
9
9
|
import MediaEditorCropPanel from '..';
|
|
10
10
|
import type { MediaEditorCropPanelProps } from '..';
|
|
11
|
-
import { MediaEditorStateProvider } from '../../../state';
|
|
11
|
+
import { MediaEditorStateProvider, useMediaEditor } from '../../../state';
|
|
12
|
+
import type { CropperState } from '../../../image-editor';
|
|
12
13
|
|
|
13
14
|
function setupCropPanel(
|
|
14
|
-
overrides: Partial< MediaEditorCropPanelProps > = {}
|
|
15
|
+
overrides: Partial< MediaEditorCropPanelProps > = {},
|
|
16
|
+
initialCropperState?: Partial< CropperState >
|
|
15
17
|
) {
|
|
16
18
|
const props: MediaEditorCropPanelProps = {
|
|
17
19
|
aspectRatioValue: '1',
|
|
18
20
|
onAspectRatioChange: jest.fn(),
|
|
19
|
-
freeformCrop: false,
|
|
20
|
-
onFreeformChange: jest.fn(),
|
|
21
21
|
aspectRatioOptions: [
|
|
22
22
|
{ label: 'Free', value: 0 },
|
|
23
23
|
{ label: 'Original', value: -1 },
|
|
@@ -27,18 +27,19 @@ function setupCropPanel(
|
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
render(
|
|
30
|
-
<MediaEditorStateProvider>
|
|
30
|
+
<MediaEditorStateProvider initialCropperState={ initialCropperState }>
|
|
31
31
|
<MediaEditorCropPanel { ...props } />
|
|
32
|
+
<CurrentZoomValue />
|
|
32
33
|
</MediaEditorStateProvider>
|
|
33
34
|
);
|
|
34
35
|
|
|
35
36
|
return props;
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
function
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
function CurrentZoomValue() {
|
|
40
|
+
const { state } = useMediaEditor();
|
|
41
|
+
|
|
42
|
+
return <output data-testid="current-zoom">{ state.zoom }</output>;
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
describe( 'MediaEditorCropPanel', () => {
|
|
@@ -46,17 +47,16 @@ describe( 'MediaEditorCropPanel', () => {
|
|
|
46
47
|
setupCropPanel();
|
|
47
48
|
|
|
48
49
|
const aspectRatio = screen.getByLabelText( 'Aspect ratio' );
|
|
49
|
-
const
|
|
50
|
-
const zoom = screen.getByRole( 'slider', { name: 'Zoom' } );
|
|
50
|
+
const zoom = screen.getByRole( 'slider', { name: 'Zoom (%)' } );
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
expect( aspectRatio.compareDocumentPosition( zoom ) ).toBe(
|
|
53
|
+
Node.DOCUMENT_POSITION_FOLLOWING
|
|
54
|
+
);
|
|
54
55
|
} );
|
|
55
56
|
|
|
56
57
|
it( 'passes selected aspect ratio changes to the caller', () => {
|
|
57
58
|
const controls = setupCropPanel( {
|
|
58
59
|
aspectRatioValue: '1',
|
|
59
|
-
freeformCrop: false,
|
|
60
60
|
} );
|
|
61
61
|
|
|
62
62
|
fireEvent.change( screen.getByLabelText( 'Aspect ratio' ), {
|
|
@@ -67,17 +67,36 @@ describe( 'MediaEditorCropPanel', () => {
|
|
|
67
67
|
expect(
|
|
68
68
|
( controls.onAspectRatioChange as jest.Mock ).mock.calls[ 0 ][ 0 ]
|
|
69
69
|
).toBe( '0' );
|
|
70
|
-
expect( controls.onFreeformChange ).not.toHaveBeenCalled();
|
|
71
70
|
} );
|
|
72
71
|
|
|
73
|
-
it( '
|
|
72
|
+
it( 'displays zoom as a percentage without changing cropper state', () => {
|
|
73
|
+
setupCropPanel( {}, { zoom: 3.749999999999999 } );
|
|
74
|
+
|
|
75
|
+
const zoomInput = screen.getByRole( 'spinbutton', {
|
|
76
|
+
name: 'Zoom (%)',
|
|
77
|
+
} );
|
|
78
|
+
|
|
79
|
+
expect( zoomInput ).toHaveValue( 375 );
|
|
80
|
+
expect( screen.getByTestId( 'current-zoom' ) ).toHaveTextContent(
|
|
81
|
+
'3.749999999999999'
|
|
82
|
+
);
|
|
83
|
+
} );
|
|
84
|
+
|
|
85
|
+
it( 'converts percentage input back to the cropper zoom multiplier', () => {
|
|
74
86
|
const controls = setupCropPanel( {
|
|
75
|
-
|
|
76
|
-
freeformCrop: true,
|
|
87
|
+
onPlacementControlInteraction: jest.fn(),
|
|
77
88
|
} );
|
|
78
89
|
|
|
79
|
-
fireEvent.
|
|
90
|
+
fireEvent.change(
|
|
91
|
+
screen.getByRole( 'spinbutton', { name: 'Zoom (%)' } ),
|
|
92
|
+
{
|
|
93
|
+
target: { value: '250' },
|
|
94
|
+
}
|
|
95
|
+
);
|
|
80
96
|
|
|
81
|
-
expect(
|
|
97
|
+
expect( screen.getByTestId( 'current-zoom' ) ).toHaveTextContent(
|
|
98
|
+
'2.5'
|
|
99
|
+
);
|
|
100
|
+
expect( controls.onPlacementControlInteraction ).toHaveBeenCalled();
|
|
82
101
|
} );
|
|
83
102
|
} );
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { __ } from '@wordpress/i18n';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import { useMediaEditor } from '../../state';
|
|
10
|
+
import { useCropGestureHandlers } from '../../hooks/use-crop-gesture-handlers';
|
|
11
|
+
import { MAX_ROTATION_OFFSET } from '../../image-editor/core/constants';
|
|
12
|
+
import RotationRuler from '../rotation-ruler';
|
|
13
|
+
|
|
14
|
+
export interface MediaEditorFineRotationProps {
|
|
15
|
+
/** Signal that a placement-oriented control is being adjusted. */
|
|
16
|
+
onPlacementControlInteraction?: () => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Fine-rotation slider for the media-editor footer. Lives separately from
|
|
21
|
+
* the snap-rotate / flip buttons so the footer can place the two halves on
|
|
22
|
+
* different rows at intermediate viewport widths.
|
|
23
|
+
*
|
|
24
|
+
* @param props
|
|
25
|
+
* @param props.onPlacementControlInteraction
|
|
26
|
+
*/
|
|
27
|
+
export default function MediaEditorFineRotation( {
|
|
28
|
+
onPlacementControlInteraction,
|
|
29
|
+
}: MediaEditorFineRotationProps ) {
|
|
30
|
+
const { state, setRotation } = useMediaEditor();
|
|
31
|
+
// `commitOnKeyUp: false` lets rapid arrow-key adjustments coalesce
|
|
32
|
+
// into one undo entry via the gesture idle window. Pointer-up still
|
|
33
|
+
// closes pointer drags immediately.
|
|
34
|
+
const rotationGestureHandlers = useCropGestureHandlers( {
|
|
35
|
+
commitOnKeyUp: false,
|
|
36
|
+
} );
|
|
37
|
+
|
|
38
|
+
// `setRotation` is an absolute-angle setter. When a single flip is active
|
|
39
|
+
// the visual direction inverts, so slider input must flip sign to match
|
|
40
|
+
// what the user sees. `snapRotate90` already handles this internally.
|
|
41
|
+
const baseAngle = Math.round( state.rotation / 90 ) * 90;
|
|
42
|
+
const singleFlip = state.flip.horizontal !== state.flip.vertical;
|
|
43
|
+
const visualDir = singleFlip ? -1 : 1;
|
|
44
|
+
const fineOffset = ( state.rotation - baseAngle ) * visualDir;
|
|
45
|
+
|
|
46
|
+
const handleRotationSlider = ( value: number ) => {
|
|
47
|
+
// Clamp strictly inside [-MAX, MAX). Exactly ±MAX lands state on a
|
|
48
|
+
// 90° midpoint and flips the derived baseAngle on the next render,
|
|
49
|
+
// causing subsequent events to spiral.
|
|
50
|
+
const EPS = 0.01;
|
|
51
|
+
const clamped = Math.max(
|
|
52
|
+
-MAX_ROTATION_OFFSET + EPS,
|
|
53
|
+
Math.min( MAX_ROTATION_OFFSET - EPS, value )
|
|
54
|
+
);
|
|
55
|
+
onPlacementControlInteraction?.();
|
|
56
|
+
setRotation( baseAngle + clamped * visualDir );
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<div
|
|
61
|
+
role="presentation"
|
|
62
|
+
className="media-editor-fine-rotation"
|
|
63
|
+
{ ...rotationGestureHandlers }
|
|
64
|
+
>
|
|
65
|
+
<RotationRuler
|
|
66
|
+
label={ __( 'Fine rotation' ) }
|
|
67
|
+
min={ -MAX_ROTATION_OFFSET }
|
|
68
|
+
max={ MAX_ROTATION_OFFSET }
|
|
69
|
+
value={ fineOffset }
|
|
70
|
+
onChange={ handleRotationSlider }
|
|
71
|
+
/>
|
|
72
|
+
</div>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
@@ -37,12 +37,14 @@ export function MediaEditorModal( {
|
|
|
37
37
|
fields = [],
|
|
38
38
|
aspectRatioPresets,
|
|
39
39
|
}: MediaEditorModalProps ) {
|
|
40
|
-
const { isModalOpen, id, onUpdate } = useSelect( ( select ) => {
|
|
41
|
-
const { isOpen, getId, getOnUpdate } =
|
|
40
|
+
const { isModalOpen, id, onUpdate, onClose } = useSelect( ( select ) => {
|
|
41
|
+
const { isOpen, getId, getOnUpdate, getOnClose } =
|
|
42
|
+
select( mediaEditorStore );
|
|
42
43
|
return {
|
|
43
44
|
isModalOpen: isOpen(),
|
|
44
45
|
id: getId(),
|
|
45
46
|
onUpdate: getOnUpdate(),
|
|
47
|
+
onClose: getOnClose(),
|
|
46
48
|
};
|
|
47
49
|
}, [] );
|
|
48
50
|
|
|
@@ -66,6 +68,11 @@ export function MediaEditorModal( {
|
|
|
66
68
|
event.stopPropagation();
|
|
67
69
|
};
|
|
68
70
|
|
|
71
|
+
const handleClose = () => {
|
|
72
|
+
closeMediaEditorModal();
|
|
73
|
+
onClose?.();
|
|
74
|
+
};
|
|
75
|
+
|
|
69
76
|
return (
|
|
70
77
|
<MediaEditor
|
|
71
78
|
id={ id }
|
|
@@ -75,7 +82,7 @@ export function MediaEditorModal( {
|
|
|
75
82
|
shouldCloseOnEsc
|
|
76
83
|
noticesClassName="media-editor-modal__snackbar"
|
|
77
84
|
noticesPortalElement={ portalElement }
|
|
78
|
-
onClose={
|
|
85
|
+
onClose={ handleClose }
|
|
79
86
|
onSaved={ ( { id: savedId, url, previous } ) => {
|
|
80
87
|
if ( savedId && onUpdate ) {
|
|
81
88
|
const update: MediaEditorModalUpdate = {
|
|
@@ -84,7 +91,7 @@ export function MediaEditorModal( {
|
|
|
84
91
|
};
|
|
85
92
|
onUpdate( update );
|
|
86
93
|
}
|
|
87
|
-
|
|
94
|
+
handleClose();
|
|
88
95
|
if ( previous && savedId !== previous.id && onUpdate ) {
|
|
89
96
|
// Intentionally unscoped: the modal is closing, so the
|
|
90
97
|
// snackbar surfaces in the host editor (not the media
|
|
@@ -108,6 +115,8 @@ export function MediaEditorModal( {
|
|
|
108
115
|
renderFrame={ ( {
|
|
109
116
|
children,
|
|
110
117
|
headerActions,
|
|
118
|
+
footerActions,
|
|
119
|
+
footerLayout,
|
|
111
120
|
onRequestClose,
|
|
112
121
|
onKeyDown,
|
|
113
122
|
shouldCloseOnClickOutside,
|
|
@@ -127,6 +136,13 @@ export function MediaEditorModal( {
|
|
|
127
136
|
headerActions={ headerActions }
|
|
128
137
|
>
|
|
129
138
|
{ children }
|
|
139
|
+
<div
|
|
140
|
+
className={ `media-editor-modal__footer is-${ footerLayout }` }
|
|
141
|
+
role="region"
|
|
142
|
+
aria-label={ __( 'Editor actions' ) }
|
|
143
|
+
>
|
|
144
|
+
{ footerActions }
|
|
145
|
+
</div>
|
|
130
146
|
</Modal>
|
|
131
147
|
</ShortcutProvider>
|
|
132
148
|
) }
|
|
@@ -21,6 +21,86 @@
|
|
|
21
21
|
min-height: 0;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
// Footer chrome — three layouts driven by `is-wide` / `is-medium` /
|
|
25
|
+
// `is-narrow` modifier classes set from the media editor. Wide is a single
|
|
26
|
+
// flex row; medium and narrow stack via `flex-direction: column`. Each
|
|
27
|
+
// layout has its own JSX tree (not `order` reshuffling, which the project's
|
|
28
|
+
// stylelint rule treats as an a11y red flag), so DOM order always matches
|
|
29
|
+
// visual order.
|
|
30
|
+
.media-editor-modal.components-modal__frame .media-editor-modal__footer {
|
|
31
|
+
flex-shrink: 0;
|
|
32
|
+
display: flex;
|
|
33
|
+
gap: $grid-unit-20;
|
|
34
|
+
padding: $grid-unit-10 $grid-unit-30;
|
|
35
|
+
border-top: $border-width solid $gray-300;
|
|
36
|
+
background: $white;
|
|
37
|
+
|
|
38
|
+
&.is-wide {
|
|
39
|
+
flex-direction: row;
|
|
40
|
+
align-items: center;
|
|
41
|
+
|
|
42
|
+
// Wide-only wrapper: ruler + transform controls are grouped
|
|
43
|
+
// together, centered between History and Cancel/Save with a
|
|
44
|
+
// divider rule between them — preserving the long-standing
|
|
45
|
+
// wide-desktop look.
|
|
46
|
+
.media-editor-modal__footer-toolbar {
|
|
47
|
+
flex: 1 1 0;
|
|
48
|
+
min-width: 0;
|
|
49
|
+
display: flex;
|
|
50
|
+
align-items: center;
|
|
51
|
+
justify-content: center;
|
|
52
|
+
gap: $grid-unit-10;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.media-editor-fine-rotation {
|
|
56
|
+
flex: 1 1 280px;
|
|
57
|
+
min-width: 0;
|
|
58
|
+
max-width: 360px;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.media-editor-transform-controls {
|
|
62
|
+
border-left: $border-width solid $gray-300;
|
|
63
|
+
padding-left: $grid-unit-15;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// `margin-left: auto` guarantees right alignment whether or not
|
|
67
|
+
// History / the toolbar is rendered (handles non-image
|
|
68
|
+
// attachments where only `FooterActions` is present).
|
|
69
|
+
.media-editor__footer-actions {
|
|
70
|
+
margin-left: auto;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
&.is-medium,
|
|
75
|
+
&.is-narrow {
|
|
76
|
+
flex-direction: column;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Narrow only: transform cluster sits on its own row, centered.
|
|
80
|
+
&.is-narrow > .media-editor-transform-controls {
|
|
81
|
+
align-self: center;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Secondary row used by medium (History + Transform + Cancel/Save) and
|
|
86
|
+
// narrow (History + Cancel/Save). `margin-left: auto` on the transform
|
|
87
|
+
// cluster and on Cancel/Save splits any free space — History gets the
|
|
88
|
+
// left edge, Cancel/Save the right edge, and the transform cluster (when
|
|
89
|
+
// present, i.e. medium only) approximately centers. Exact centering only
|
|
90
|
+
// holds when History and Cancel/Save have matching widths; in practice
|
|
91
|
+
// they're close enough that the offset is imperceptible.
|
|
92
|
+
.media-editor-modal__footer-row {
|
|
93
|
+
display: flex;
|
|
94
|
+
flex-wrap: wrap;
|
|
95
|
+
align-items: center;
|
|
96
|
+
gap: $grid-unit-20;
|
|
97
|
+
|
|
98
|
+
.media-editor-transform-controls,
|
|
99
|
+
.media-editor__footer-actions {
|
|
100
|
+
margin-left: auto;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
24
104
|
.media-editor-modal {
|
|
25
105
|
display: flex;
|
|
26
106
|
flex-direction: column;
|
|
@@ -24,6 +24,7 @@ let mockSaveResult = {
|
|
|
24
24
|
},
|
|
25
25
|
};
|
|
26
26
|
const mockOnUpdate = jest.fn();
|
|
27
|
+
const mockOnClose = jest.fn();
|
|
27
28
|
const mockCloseMediaEditorModal = jest.fn();
|
|
28
29
|
const mockCreateSuccessNotice = jest.fn();
|
|
29
30
|
|
|
@@ -81,6 +82,7 @@ describe( 'MediaEditorModal', () => {
|
|
|
81
82
|
isOpen: () => true,
|
|
82
83
|
getId: () => 10,
|
|
83
84
|
getOnUpdate: () => mockOnUpdate,
|
|
85
|
+
getOnClose: () => mockOnClose,
|
|
84
86
|
} ) )
|
|
85
87
|
);
|
|
86
88
|
( useDispatch as jest.Mock ).mockImplementation( ( store ) =>
|
|
@@ -102,6 +104,7 @@ describe( 'MediaEditorModal', () => {
|
|
|
102
104
|
url: 'edited.jpg',
|
|
103
105
|
} );
|
|
104
106
|
expect( mockCloseMediaEditorModal ).toHaveBeenCalled();
|
|
107
|
+
expect( mockOnClose ).toHaveBeenCalled();
|
|
105
108
|
expect( mockCreateSuccessNotice ).toHaveBeenCalledWith(
|
|
106
109
|
'Image edited.',
|
|
107
110
|
expect.objectContaining( {
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { Button } from '@wordpress/components';
|
|
5
|
+
import { __ } from '@wordpress/i18n';
|
|
6
|
+
import {
|
|
7
|
+
rotateLeft,
|
|
8
|
+
rotateRight,
|
|
9
|
+
flipHorizontal,
|
|
10
|
+
flipVertical,
|
|
11
|
+
} from '@wordpress/icons';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Internal dependencies
|
|
15
|
+
*/
|
|
16
|
+
import { useMediaEditor } from '../../state';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Snap-rotate and flip controls for the media-editor footer. Rotation and
|
|
20
|
+
* reflection are both geometric transforms, hence "transform controls".
|
|
21
|
+
* Lives separately from the fine-rotation slider so the footer can place
|
|
22
|
+
* the two halves on different rows at intermediate viewport widths.
|
|
23
|
+
*/
|
|
24
|
+
export default function MediaEditorTransformControls() {
|
|
25
|
+
const { state, setFlip, snapRotate90 } = useMediaEditor();
|
|
26
|
+
return (
|
|
27
|
+
<div className="media-editor-transform-controls">
|
|
28
|
+
<Button
|
|
29
|
+
size="compact"
|
|
30
|
+
icon={ rotateLeft }
|
|
31
|
+
label={ __( 'Rotate 90° counter-clockwise' ) }
|
|
32
|
+
showTooltip
|
|
33
|
+
onClick={ () => snapRotate90( -1 ) }
|
|
34
|
+
/>
|
|
35
|
+
<Button
|
|
36
|
+
size="compact"
|
|
37
|
+
icon={ rotateRight }
|
|
38
|
+
label={ __( 'Rotate 90° clockwise' ) }
|
|
39
|
+
showTooltip
|
|
40
|
+
onClick={ () => snapRotate90( 1 ) }
|
|
41
|
+
/>
|
|
42
|
+
<Button
|
|
43
|
+
size="compact"
|
|
44
|
+
icon={ flipHorizontal }
|
|
45
|
+
label={ __( 'Flip horizontal' ) }
|
|
46
|
+
showTooltip
|
|
47
|
+
isPressed={ state.flip.horizontal }
|
|
48
|
+
onClick={ () =>
|
|
49
|
+
setFlip( {
|
|
50
|
+
horizontal: ! state.flip.horizontal,
|
|
51
|
+
vertical: state.flip.vertical,
|
|
52
|
+
} )
|
|
53
|
+
}
|
|
54
|
+
/>
|
|
55
|
+
<Button
|
|
56
|
+
size="compact"
|
|
57
|
+
icon={ flipVertical }
|
|
58
|
+
label={ __( 'Flip vertical' ) }
|
|
59
|
+
showTooltip
|
|
60
|
+
isPressed={ state.flip.vertical }
|
|
61
|
+
onClick={ () =>
|
|
62
|
+
setFlip( {
|
|
63
|
+
horizontal: state.flip.horizontal,
|
|
64
|
+
vertical: ! state.flip.vertical,
|
|
65
|
+
} )
|
|
66
|
+
}
|
|
67
|
+
/>
|
|
68
|
+
</div>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
@@ -50,9 +50,7 @@ export function useCropGestureHandlers(
|
|
|
50
50
|
) {
|
|
51
51
|
const { commitOnKeyUp = true } = options;
|
|
52
52
|
const { beginGesture, endGesture } = useMediaEditor();
|
|
53
|
-
const keyboardTimerRef = useRef<
|
|
54
|
-
ReturnType< typeof setTimeout > | undefined
|
|
55
|
-
>( undefined );
|
|
53
|
+
const keyboardTimerRef = useRef< ReturnType< typeof setTimeout > >();
|
|
56
54
|
|
|
57
55
|
const clearKeyboardTimer = useCallback( () => {
|
|
58
56
|
clearTimeout( keyboardTimerRef.current );
|
|
@@ -78,7 +78,7 @@ function CropperPanel( { src }: { src: string } ) {
|
|
|
78
78
|
|
|
79
79
|
## Use the Media Editor Composite Controller
|
|
80
80
|
|
|
81
|
-
Use the media editor controller when cropper state must share history with controls such as aspect ratio
|
|
81
|
+
Use the media editor controller when cropper state must share history with controls such as aspect ratio.
|
|
82
82
|
|
|
83
83
|
```tsx
|
|
84
84
|
import { resolveAspectRatio, useMediaEditor } from '../../state';
|
|
@@ -96,7 +96,7 @@ function MediaEditorCanvas( { src }: { src: string } ) {
|
|
|
96
96
|
src={ src }
|
|
97
97
|
controller={ controller }
|
|
98
98
|
aspectRatio={ aspectRatio }
|
|
99
|
-
freeformCrop
|
|
99
|
+
freeformCrop
|
|
100
100
|
onGestureStart={ controller.beginGesture }
|
|
101
101
|
onGestureEnd={ controller.endGesture }
|
|
102
102
|
/>
|
|
@@ -541,9 +541,7 @@ function CropperInner(
|
|
|
541
541
|
|
|
542
542
|
// Settling animation: brief linear transition after resize end.
|
|
543
543
|
const [ settling, setSettling ] = useState( false );
|
|
544
|
-
const settleTimerRef = useRef<
|
|
545
|
-
ReturnType< typeof setTimeout > | undefined
|
|
546
|
-
>( undefined );
|
|
544
|
+
const settleTimerRef = useRef< ReturnType< typeof setTimeout > >();
|
|
547
545
|
|
|
548
546
|
// Clear the pending settle timer on unmount so it can't fire a
|
|
549
547
|
// state update on an unmounted component.
|
|
@@ -134,9 +134,7 @@ export function RectangleStencil( {
|
|
|
134
134
|
} ),
|
|
135
135
|
[ boundsMinX, boundsMinY, boundsMaxX, boundsMaxY ]
|
|
136
136
|
);
|
|
137
|
-
const keyboardSettleTimerRef = useRef<
|
|
138
|
-
ReturnType< typeof setTimeout > | undefined
|
|
139
|
-
>( undefined );
|
|
137
|
+
const keyboardSettleTimerRef = useRef< ReturnType< typeof setTimeout > >();
|
|
140
138
|
const keyboardResizeActiveRef = useRef( false );
|
|
141
139
|
const resizeHandleDescriptionId = useId();
|
|
142
140
|
const hasLockedRatio = !! ( aspectRatio && aspectRatio > 0 );
|
|
@@ -89,9 +89,7 @@ function buildAnnouncement(
|
|
|
89
89
|
*/
|
|
90
90
|
export function useAriaAnnouncer( state: CropperState ): string {
|
|
91
91
|
const [ ariaMessage, setAriaMessage ] = useState( '' );
|
|
92
|
-
const timerRef = useRef< ReturnType< typeof setTimeout >
|
|
93
|
-
undefined
|
|
94
|
-
);
|
|
92
|
+
const timerRef = useRef< ReturnType< typeof setTimeout > >();
|
|
95
93
|
const prevMessageRef = useRef( '' );
|
|
96
94
|
const prevStateRef = useRef< CropperState | null >( null );
|
|
97
95
|
const latestStateRef = useRef( state );
|
|
@@ -108,9 +108,8 @@ export function useInteraction(
|
|
|
108
108
|
const [ isZooming, setIsZooming ] = useState( false );
|
|
109
109
|
const [ isGestureActive, setIsGestureActive ] = useState( false );
|
|
110
110
|
const [ isKeyboardPanning, setIsKeyboardPanning ] = useState( false );
|
|
111
|
-
const keyboardInteractionTimerRef =
|
|
112
|
-
ReturnType< typeof setTimeout >
|
|
113
|
-
>( undefined );
|
|
111
|
+
const keyboardInteractionTimerRef =
|
|
112
|
+
useRef< ReturnType< typeof setTimeout > >();
|
|
114
113
|
// Tracks whether a keyboard gesture (pan or zoom) is currently active so
|
|
115
114
|
// onGestureStart is only fired once per gesture, not on every key repeat.
|
|
116
115
|
const isKeyboardGestureActiveRef = useRef( false );
|