@wordpress/media-editor 0.17.2-next.v.202608281122.0 → 0.18.0
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/CHANGELOG.md +1 -1
- package/build/components/media-editor/index.cjs +43 -11
- package/build/components/media-editor/index.cjs.map +2 -2
- package/build/components/media-editor-canvas/index.cjs +4 -2
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +4 -2
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-fine-rotation/index.cjs +4 -2
- package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
- package/build/components/media-editor-image-controls/index.cjs +17 -4
- package/build/components/media-editor-image-controls/index.cjs.map +2 -2
- package/build/components/rotation-ruler/index.cjs +1 -0
- package/build/components/rotation-ruler/index.cjs.map +2 -2
- package/build/components/rotation-ruler/use-ruler-drag.cjs +7 -2
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs +3 -0
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +5 -3
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +12 -5
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs +31 -0
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +43 -11
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor-canvas/index.mjs +4 -2
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +4 -2
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs +4 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
- package/build-module/components/media-editor-image-controls/index.mjs +17 -4
- package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/index.mjs +1 -0
- package/build-module/components/rotation-ruler/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs +7 -2
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs +3 -0
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +5 -3
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +12 -5
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs +31 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-style/style-rtl.css +5 -12
- package/build-style/style.css +5 -12
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor-canvas/index.d.ts +7 -1
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +4 -1
- 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 +7 -1
- package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts +7 -1
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +7 -0
- 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-interaction.d.ts +5 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/global.d.ts +1 -0
- package/package.json +23 -22
- package/src/components/media-editor/index.tsx +42 -5
- package/src/components/media-editor/test/{use-crop-options.tsx → use-crop-options.jsdom.test.tsx} +1 -0
- package/src/components/media-editor-canvas/index.tsx +8 -0
- package/src/components/media-editor-crop-panel/index.tsx +6 -1
- package/src/components/media-editor-crop-panel/test/{index.tsx → index.jsdom.test.tsx} +5 -2
- package/src/components/media-editor-fine-rotation/index.tsx +8 -0
- package/src/components/media-editor-image-controls/index.tsx +22 -3
- package/src/components/media-editor-image-controls/test/{index.tsx → index.jsdom.test.tsx} +37 -0
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +24 -14
- package/src/components/media-editor-modal/test/{index.tsx → index.jsdom.test.tsx} +65 -35
- package/src/components/rotation-ruler/index.tsx +1 -0
- package/src/components/rotation-ruler/style.scss +1 -1
- package/src/components/rotation-ruler/test/{index.tsx → index.jsdom.test.tsx} +42 -4
- package/src/components/rotation-ruler/use-ruler-drag.ts +10 -2
- package/src/image-editor/core/export/test/{canvas-renderer.ts → canvas-renderer.jsdom.test.ts} +76 -63
- package/src/image-editor/core/interaction-controller.ts +8 -0
- package/src/image-editor/core/math/test/sanitize.ts +1 -0
- package/src/image-editor/core/test/camera.ts +2 -3
- package/src/image-editor/core/test/interaction-controller.ts +50 -40
- package/src/image-editor/core/test/preview-export-parity.ts +31 -23
- package/src/image-editor/core/test/render-camera-bridge.ts +1 -0
- package/src/image-editor/core/test/state.ts +1 -0
- package/src/image-editor/core/test/stencil-math.ts +1 -0
- package/src/image-editor/core/transforms/test/pipeline.ts +1 -0
- package/src/image-editor/react/components/cropper.tsx +12 -1
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +22 -6
- package/src/image-editor/react/components/stencils/test/{rectangle-stencil.tsx → rectangle-stencil.jsdom.test.tsx} +54 -36
- package/src/image-editor/react/components/test/{cropper.tsx → cropper.jsdom.test.tsx} +304 -0
- package/src/image-editor/react/hooks/test/{use-aria-announcer.ts → use-aria-announcer.jsdom.test.ts} +57 -44
- package/src/image-editor/react/hooks/test/{use-cropper-reducer.ts → use-cropper-reducer.jsdom.test.ts} +1 -0
- package/src/image-editor/react/hooks/test/{use-interaction.ts → use-interaction.jsdom.test.ts} +17 -16
- package/src/image-editor/react/hooks/test/{use-transform-style.ts → use-transform-style.jsdom.test.ts} +1 -0
- package/src/image-editor/react/hooks/use-interaction.ts +47 -0
- package/src/state/test/{use-media-editor-state.ts → use-media-editor-state.jsdom.test.ts} +1 -0
- package/src/store/test/reducer.ts +9 -8
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.json +8 -3
|
@@ -1,16 +1,19 @@
|
|
|
1
|
+
import { describe, expect, it, vi, type Mock } from 'vitest';
|
|
1
2
|
import { fireEvent, render, screen } from '@testing-library/react';
|
|
2
3
|
import MediaEditorCropPanel from '..';
|
|
3
4
|
import type { MediaEditorCropPanelProps } from '..';
|
|
4
5
|
import { MediaEditorStateProvider } from '../../../state';
|
|
5
6
|
import type { CropperState } from '../../../image-editor';
|
|
6
7
|
|
|
8
|
+
globalThis.wpVitest.mockMatchMedia();
|
|
9
|
+
|
|
7
10
|
function setupCropPanel(
|
|
8
11
|
overrides: Partial< MediaEditorCropPanelProps > = {},
|
|
9
12
|
initialCropperState?: Partial< CropperState >
|
|
10
13
|
) {
|
|
11
14
|
const props: MediaEditorCropPanelProps = {
|
|
12
15
|
aspectRatioValue: '1',
|
|
13
|
-
onAspectRatioChange:
|
|
16
|
+
onAspectRatioChange: vi.fn(),
|
|
14
17
|
aspectRatioOptions: [
|
|
15
18
|
{ label: 'Free', value: 0 },
|
|
16
19
|
{ label: 'Original', value: -1 },
|
|
@@ -40,7 +43,7 @@ describe( 'MediaEditorCropPanel', () => {
|
|
|
40
43
|
|
|
41
44
|
expect( controls.onAspectRatioChange ).toHaveBeenCalled();
|
|
42
45
|
expect(
|
|
43
|
-
( controls.onAspectRatioChange as
|
|
46
|
+
( controls.onAspectRatioChange as Mock ).mock.calls[ 0 ][ 0 ]
|
|
44
47
|
).toBe( '0' );
|
|
45
48
|
} );
|
|
46
49
|
|
|
@@ -7,6 +7,11 @@ import RotationRuler from '../rotation-ruler';
|
|
|
7
7
|
export interface MediaEditorFineRotationProps {
|
|
8
8
|
/** Signal that a placement-oriented control is being adjusted. */
|
|
9
9
|
onPlacementControlInteraction?: () => void;
|
|
10
|
+
/**
|
|
11
|
+
* Disable the ruler. Set while the edit is saving; `RotationRuler`
|
|
12
|
+
* cancels an in-flight drag when this flips.
|
|
13
|
+
*/
|
|
14
|
+
disabled?: boolean;
|
|
10
15
|
}
|
|
11
16
|
|
|
12
17
|
/**
|
|
@@ -17,9 +22,11 @@ export interface MediaEditorFineRotationProps {
|
|
|
17
22
|
*
|
|
18
23
|
* @param props
|
|
19
24
|
* @param props.onPlacementControlInteraction
|
|
25
|
+
* @param props.disabled
|
|
20
26
|
*/
|
|
21
27
|
export default function MediaEditorFineRotation( {
|
|
22
28
|
onPlacementControlInteraction,
|
|
29
|
+
disabled = false,
|
|
23
30
|
}: MediaEditorFineRotationProps ) {
|
|
24
31
|
const { state, setRotation } = useMediaEditor();
|
|
25
32
|
// `commitOnKeyUp: false` lets rapid arrow-key adjustments coalesce
|
|
@@ -62,6 +69,7 @@ export default function MediaEditorFineRotation( {
|
|
|
62
69
|
max={ MAX_ROTATION_OFFSET }
|
|
63
70
|
value={ fineOffset }
|
|
64
71
|
onChange={ handleRotationSlider }
|
|
72
|
+
disabled={ disabled }
|
|
65
73
|
/>
|
|
66
74
|
</div>
|
|
67
75
|
);
|
|
@@ -54,6 +54,11 @@ export interface MediaEditorImageControlsProps {
|
|
|
54
54
|
* `DEFAULT_ZOOM_FACTOR` (1.2).
|
|
55
55
|
*/
|
|
56
56
|
zoomFactor?: number;
|
|
57
|
+
/**
|
|
58
|
+
* Disable every control. Set while the edit is saving, so the crop the
|
|
59
|
+
* request was built from cannot change under it.
|
|
60
|
+
*/
|
|
61
|
+
disabled?: boolean;
|
|
57
62
|
}
|
|
58
63
|
|
|
59
64
|
/**
|
|
@@ -67,12 +72,14 @@ export interface MediaEditorImageControlsProps {
|
|
|
67
72
|
* @param props.showAspectRatioControl
|
|
68
73
|
* @param props.aspectRatioPresets
|
|
69
74
|
* @param props.zoomFactor
|
|
75
|
+
* @param props.disabled
|
|
70
76
|
*/
|
|
71
77
|
export default function MediaEditorImageControls( {
|
|
72
78
|
withLabels = false,
|
|
73
79
|
showAspectRatioControl = false,
|
|
74
80
|
aspectRatioPresets,
|
|
75
81
|
zoomFactor = DEFAULT_ZOOM_FACTOR,
|
|
82
|
+
disabled = false,
|
|
76
83
|
}: MediaEditorImageControlsProps ) {
|
|
77
84
|
const { state, setFlip, snapRotate90, setZoom } = useMediaEditor();
|
|
78
85
|
const { aspectRatioValue, setAspectRatioValue, aspectRatioOptions } =
|
|
@@ -92,6 +99,8 @@ export default function MediaEditorImageControls( {
|
|
|
92
99
|
icon={ rotateLeft }
|
|
93
100
|
label={ __( 'Rotate 90° counter-clockwise' ) }
|
|
94
101
|
showTooltip
|
|
102
|
+
disabled={ disabled }
|
|
103
|
+
accessibleWhenDisabled
|
|
95
104
|
onClick={ () => snapRotate90( -1 ) }
|
|
96
105
|
/>
|
|
97
106
|
<Button
|
|
@@ -99,6 +108,8 @@ export default function MediaEditorImageControls( {
|
|
|
99
108
|
icon={ rotateRight }
|
|
100
109
|
label={ __( 'Rotate 90° clockwise' ) }
|
|
101
110
|
showTooltip
|
|
111
|
+
disabled={ disabled }
|
|
112
|
+
accessibleWhenDisabled
|
|
102
113
|
onClick={ () => snapRotate90( 1 ) }
|
|
103
114
|
/>
|
|
104
115
|
</>
|
|
@@ -112,6 +123,8 @@ export default function MediaEditorImageControls( {
|
|
|
112
123
|
label={ __( 'Flip horizontal' ) }
|
|
113
124
|
showTooltip
|
|
114
125
|
isPressed={ state.flip.horizontal }
|
|
126
|
+
disabled={ disabled }
|
|
127
|
+
accessibleWhenDisabled
|
|
115
128
|
onClick={ () =>
|
|
116
129
|
setFlip( {
|
|
117
130
|
horizontal: ! state.flip.horizontal,
|
|
@@ -125,6 +138,8 @@ export default function MediaEditorImageControls( {
|
|
|
125
138
|
label={ __( 'Flip vertical' ) }
|
|
126
139
|
showTooltip
|
|
127
140
|
isPressed={ state.flip.vertical }
|
|
141
|
+
disabled={ disabled }
|
|
142
|
+
accessibleWhenDisabled
|
|
128
143
|
onClick={ () =>
|
|
129
144
|
setFlip( {
|
|
130
145
|
horizontal: state.flip.horizontal,
|
|
@@ -142,7 +157,7 @@ export default function MediaEditorImageControls( {
|
|
|
142
157
|
icon={ plus }
|
|
143
158
|
label={ __( 'Zoom in' ) }
|
|
144
159
|
showTooltip
|
|
145
|
-
disabled={ state.zoom >= MAX_ZOOM }
|
|
160
|
+
disabled={ disabled || state.zoom >= MAX_ZOOM }
|
|
146
161
|
accessibleWhenDisabled
|
|
147
162
|
onClick={ () => zoomByFactor( zoomFactor ) }
|
|
148
163
|
/>
|
|
@@ -151,7 +166,7 @@ export default function MediaEditorImageControls( {
|
|
|
151
166
|
icon={ lineSolid }
|
|
152
167
|
label={ __( 'Zoom out' ) }
|
|
153
168
|
showTooltip
|
|
154
|
-
disabled={ state.zoom <= minZoom }
|
|
169
|
+
disabled={ disabled || state.zoom <= minZoom }
|
|
155
170
|
accessibleWhenDisabled
|
|
156
171
|
onClick={ () => zoomByFactor( 1 / zoomFactor ) }
|
|
157
172
|
/>
|
|
@@ -163,7 +178,7 @@ export default function MediaEditorImageControls( {
|
|
|
163
178
|
icon={ aspectRatioIcon }
|
|
164
179
|
label={ __( 'Aspect ratio' ) }
|
|
165
180
|
popoverProps={ { placement: 'top' } }
|
|
166
|
-
toggleProps={ { size: 'compact' } }
|
|
181
|
+
toggleProps={ { size: 'compact', disabled } }
|
|
167
182
|
>
|
|
168
183
|
{ ( { onClose } ) => (
|
|
169
184
|
<MenuGroup label={ __( 'Aspect ratio' ) }>
|
|
@@ -176,7 +191,11 @@ export default function MediaEditorImageControls( {
|
|
|
176
191
|
role="menuitemradio"
|
|
177
192
|
isSelected={ isSelected }
|
|
178
193
|
icon={ isSelected ? check : undefined }
|
|
194
|
+
disabled={ disabled }
|
|
179
195
|
onClick={ () => {
|
|
196
|
+
if ( disabled ) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
180
199
|
setAspectRatioValue( value );
|
|
181
200
|
onClose();
|
|
182
201
|
} }
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
1
2
|
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
3
|
+
import { useState } from '@wordpress/element';
|
|
2
4
|
import MediaEditorImageControls from '..';
|
|
3
5
|
import type { MediaEditorImageControlsProps } from '..';
|
|
4
6
|
import { MediaEditorStateProvider, useMediaEditor } from '../../../state';
|
|
@@ -130,6 +132,41 @@ describe( 'MediaEditorImageControls', () => {
|
|
|
130
132
|
);
|
|
131
133
|
} );
|
|
132
134
|
|
|
135
|
+
it( 'ignores an aspect ratio chosen from a menu left open when disabled', async () => {
|
|
136
|
+
function Harness() {
|
|
137
|
+
const [ locked, setLocked ] = useState( false );
|
|
138
|
+
return (
|
|
139
|
+
<MediaEditorStateProvider>
|
|
140
|
+
<MediaEditorImageControls
|
|
141
|
+
showAspectRatioControl
|
|
142
|
+
disabled={ locked }
|
|
143
|
+
/>
|
|
144
|
+
<button onClick={ () => setLocked( true ) }>lock</button>
|
|
145
|
+
<CurrentState />
|
|
146
|
+
</MediaEditorStateProvider>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
render( <Harness /> );
|
|
150
|
+
|
|
151
|
+
fireEvent.click(
|
|
152
|
+
screen.getByRole( 'button', { name: 'Aspect ratio' } )
|
|
153
|
+
);
|
|
154
|
+
const before = screen.getByTestId( 'current-aspect-ratio' ).textContent;
|
|
155
|
+
|
|
156
|
+
// The popover stays mounted when the toggle becomes disabled, so
|
|
157
|
+
// the items have to refuse the change themselves.
|
|
158
|
+
fireEvent.click( screen.getByRole( 'button', { name: 'lock' } ) );
|
|
159
|
+
fireEvent.click(
|
|
160
|
+
screen.getByRole( 'menuitemradio', { name: 'Square (1:1)' } )
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
await waitFor( () =>
|
|
164
|
+
expect(
|
|
165
|
+
screen.getByTestId( 'current-aspect-ratio' )
|
|
166
|
+
).toHaveTextContent( before ?? '' )
|
|
167
|
+
);
|
|
168
|
+
} );
|
|
169
|
+
|
|
133
170
|
it( 'omits the aspect ratio dropdown from the labelled panel layout', () => {
|
|
134
171
|
setup( {
|
|
135
172
|
withLabels: true,
|
|
@@ -52,6 +52,8 @@
|
|
|
52
52
|
* `serverSourcePixel` to undo it, and add values to the grid in
|
|
53
53
|
* `buildRows`. The probes don't need to change.
|
|
54
54
|
*/
|
|
55
|
+
|
|
56
|
+
import { describe, expect, it } from 'vitest';
|
|
55
57
|
import { mat2d, vec2 } from 'gl-matrix';
|
|
56
58
|
import { buildModifiers } from '../build-modifiers';
|
|
57
59
|
import type { Modifier } from '../build-modifiers';
|
|
@@ -329,21 +331,29 @@ function outputSizeFor( modifiers: Modifier[], imageSize: Size ): Size {
|
|
|
329
331
|
describe( 'buildModifiers ↔ Export-camera parity', () => {
|
|
330
332
|
const rows = buildRows();
|
|
331
333
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
+
for ( const { label, state, u, v } of rows ) {
|
|
335
|
+
it( label, () => {
|
|
336
|
+
const modifiers = buildModifiers( state, IMAGE );
|
|
334
337
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
338
|
+
// Output canvas is whatever the server would emit: the crop rect
|
|
339
|
+
// if present, otherwise the full rotated AABB. This makes (u, v)
|
|
340
|
+
// map to the same *output* pixel on both paths.
|
|
341
|
+
const outputSize = outputSizeFor( modifiers, IMAGE );
|
|
339
342
|
|
|
340
|
-
|
|
341
|
-
|
|
343
|
+
const exported = exportSourcePixel(
|
|
344
|
+
state,
|
|
345
|
+
IMAGE,
|
|
346
|
+
outputSize,
|
|
347
|
+
u,
|
|
348
|
+
v
|
|
349
|
+
);
|
|
350
|
+
const server = serverSourcePixel( modifiers, IMAGE, u, v );
|
|
342
351
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
352
|
+
// Tolerance of 1 source pixel. The two paths compose rotations
|
|
353
|
+
// and scales in different orders, so sub-pixel disagreement is
|
|
354
|
+
// expected from floating-point. Anything larger is a real bug.
|
|
355
|
+
expect( server.x ).toBeCloseTo( exported.x, 0 );
|
|
356
|
+
expect( server.y ).toBeCloseTo( exported.y, 0 );
|
|
357
|
+
} );
|
|
358
|
+
}
|
|
349
359
|
} );
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
|
1
2
|
import { fireEvent, render, screen } from '@testing-library/react';
|
|
3
|
+
import type { ReactNode } from 'react';
|
|
2
4
|
import { useDispatch, useSelect } from '@wordpress/data';
|
|
3
5
|
import { store as noticesStore } from '@wordpress/notices';
|
|
4
6
|
import { MediaEditorModal } from '../index';
|
|
5
7
|
|
|
6
|
-
let mockSaveResult
|
|
8
|
+
let mockSaveResult: {
|
|
9
|
+
id: number;
|
|
10
|
+
url: string;
|
|
11
|
+
media: { id: number; source_url: string };
|
|
12
|
+
previous?: { id: number; url: string };
|
|
13
|
+
} = {
|
|
7
14
|
id: 11,
|
|
8
15
|
url: 'edited.jpg',
|
|
9
16
|
media: { id: 11, source_url: 'edited.jpg' },
|
|
@@ -12,50 +19,73 @@ let mockSaveResult = {
|
|
|
12
19
|
url: 'original.jpg',
|
|
13
20
|
},
|
|
14
21
|
};
|
|
15
|
-
const mockOnUpdate =
|
|
16
|
-
const mockOnClose =
|
|
17
|
-
const mockCloseMediaEditorModal =
|
|
18
|
-
const mockCreateSuccessNotice =
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
22
|
+
const mockOnUpdate = vi.fn();
|
|
23
|
+
const mockOnClose = vi.fn();
|
|
24
|
+
const mockCloseMediaEditorModal = vi.fn();
|
|
25
|
+
const mockCreateSuccessNotice = vi.fn();
|
|
26
|
+
|
|
27
|
+
vi.mock(
|
|
28
|
+
import( '@wordpress/data' ),
|
|
29
|
+
() =>
|
|
30
|
+
( {
|
|
31
|
+
useDispatch: vi.fn(),
|
|
32
|
+
useSelect: vi.fn(),
|
|
33
|
+
} ) as unknown as typeof import('@wordpress/data')
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
vi.mock(
|
|
37
|
+
import( '@wordpress/components' ),
|
|
38
|
+
() =>
|
|
39
|
+
( {
|
|
40
|
+
Modal: ( { children }: { children: ReactNode } ) => children,
|
|
41
|
+
} ) as unknown as typeof import('@wordpress/components')
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
vi.mock(
|
|
45
|
+
import( '@wordpress/keyboard-shortcuts' ),
|
|
46
|
+
() =>
|
|
47
|
+
( {
|
|
48
|
+
ShortcutProvider: ( { children }: { children: ReactNode } ) =>
|
|
49
|
+
children,
|
|
50
|
+
} ) as unknown as typeof import('@wordpress/keyboard-shortcuts')
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
vi.mock(
|
|
54
|
+
import( '@wordpress/notices' ),
|
|
55
|
+
() =>
|
|
56
|
+
( {
|
|
57
|
+
store: { name: 'notices' },
|
|
58
|
+
} ) as unknown as typeof import('@wordpress/notices')
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
vi.mock(
|
|
62
|
+
import( '../../../store' ),
|
|
63
|
+
() =>
|
|
64
|
+
( {
|
|
65
|
+
store: { name: 'media-editor' },
|
|
66
|
+
} ) as unknown as typeof import('../../../store')
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
vi.mock( import( '../../media-editor' ), async () => {
|
|
70
|
+
const { createElement } =
|
|
71
|
+
await vi.importActual< typeof import('@wordpress/element') >(
|
|
72
|
+
'@wordpress/element'
|
|
73
|
+
);
|
|
43
74
|
|
|
44
75
|
return {
|
|
45
|
-
|
|
46
|
-
default: jest.fn( ( props ) =>
|
|
76
|
+
default: vi.fn( ( props ) =>
|
|
47
77
|
createElement(
|
|
48
78
|
'button',
|
|
49
79
|
{ onClick: () => props.onSaved( mockSaveResult ) },
|
|
50
80
|
'Save result'
|
|
51
81
|
)
|
|
52
82
|
),
|
|
53
|
-
};
|
|
83
|
+
} as unknown as typeof import('../../media-editor');
|
|
54
84
|
} );
|
|
55
85
|
|
|
56
86
|
describe( 'MediaEditorModal', () => {
|
|
57
87
|
beforeEach( () => {
|
|
58
|
-
|
|
88
|
+
vi.clearAllMocks();
|
|
59
89
|
mockSaveResult = {
|
|
60
90
|
id: 11,
|
|
61
91
|
url: 'edited.jpg',
|
|
@@ -66,7 +96,7 @@ describe( 'MediaEditorModal', () => {
|
|
|
66
96
|
},
|
|
67
97
|
};
|
|
68
98
|
|
|
69
|
-
( useSelect as
|
|
99
|
+
( useSelect as Mock ).mockImplementation( ( mapSelect ) =>
|
|
70
100
|
mapSelect( () => ( {
|
|
71
101
|
isOpen: () => true,
|
|
72
102
|
getId: () => 10,
|
|
@@ -74,7 +104,7 @@ describe( 'MediaEditorModal', () => {
|
|
|
74
104
|
getOnClose: () => mockOnClose,
|
|
75
105
|
} ) )
|
|
76
106
|
);
|
|
77
|
-
( useDispatch as
|
|
107
|
+
( useDispatch as Mock ).mockImplementation( ( store ) =>
|
|
78
108
|
store === noticesStore
|
|
79
109
|
? { createSuccessNotice: mockCreateSuccessNotice }
|
|
80
110
|
: { closeMediaEditorModal: mockCloseMediaEditorModal }
|
|
@@ -194,6 +194,7 @@ export default function RotationRuler( props: RotationRulerProps ) {
|
|
|
194
194
|
<div
|
|
195
195
|
className={ clsx( 'rotation-ruler', className ) }
|
|
196
196
|
role="presentation"
|
|
197
|
+
data-testid="rotation-ruler"
|
|
197
198
|
data-disabled={ disabled || undefined }
|
|
198
199
|
{ ...dragHandlers }
|
|
199
200
|
>
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { fireEvent, render, screen } from '@testing-library/react';
|
|
2
3
|
import userEvent from '@testing-library/user-event';
|
|
3
4
|
import { pxToValueDelta, clampValue, quantize } from '../use-ruler-drag';
|
|
4
5
|
import RotationRuler from '../index';
|
|
5
6
|
|
|
7
|
+
globalThis.wpVitest.mockPointerEvent();
|
|
8
|
+
|
|
6
9
|
describe( 'rotation-ruler math', () => {
|
|
7
10
|
describe( 'pxToValueDelta', () => {
|
|
8
11
|
it( 'converts pointer delta to a value delta using pixelsPerStep × step', () => {
|
|
@@ -53,7 +56,7 @@ describe( 'RotationRuler', () => {
|
|
|
53
56
|
|
|
54
57
|
it( 'fires onChange with value + step on ArrowRight', async () => {
|
|
55
58
|
const user = userEvent.setup();
|
|
56
|
-
const onChange =
|
|
59
|
+
const onChange = vi.fn();
|
|
57
60
|
render(
|
|
58
61
|
<RotationRuler
|
|
59
62
|
value={ 0 }
|
|
@@ -70,7 +73,7 @@ describe( 'RotationRuler', () => {
|
|
|
70
73
|
|
|
71
74
|
it( 'fires onChange with value + step / 2 on Shift+ArrowRight', async () => {
|
|
72
75
|
const user = userEvent.setup();
|
|
73
|
-
const onChange =
|
|
76
|
+
const onChange = vi.fn();
|
|
74
77
|
render(
|
|
75
78
|
<RotationRuler
|
|
76
79
|
value={ 0 }
|
|
@@ -84,9 +87,44 @@ describe( 'RotationRuler', () => {
|
|
|
84
87
|
expect( onChange ).toHaveBeenCalledTimes( 1 );
|
|
85
88
|
} );
|
|
86
89
|
|
|
90
|
+
it( 'stops an in-flight drag when it becomes disabled', () => {
|
|
91
|
+
const onChange = vi.fn();
|
|
92
|
+
const props = {
|
|
93
|
+
value: 0,
|
|
94
|
+
onChange,
|
|
95
|
+
label: 'Fine rotation',
|
|
96
|
+
};
|
|
97
|
+
const { rerender } = render( <RotationRuler { ...props } /> );
|
|
98
|
+
const strip = screen.getByTestId( 'rotation-ruler' );
|
|
99
|
+
|
|
100
|
+
if ( ! strip.hasPointerCapture ) {
|
|
101
|
+
strip.hasPointerCapture = () => false;
|
|
102
|
+
}
|
|
103
|
+
if ( ! strip.setPointerCapture ) {
|
|
104
|
+
strip.setPointerCapture = () => {};
|
|
105
|
+
}
|
|
106
|
+
if ( ! strip.releasePointerCapture ) {
|
|
107
|
+
strip.releasePointerCapture = () => {};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
fireEvent.pointerDown( strip, {
|
|
111
|
+
button: 0,
|
|
112
|
+
clientX: 0,
|
|
113
|
+
pointerId: 1,
|
|
114
|
+
} );
|
|
115
|
+
fireEvent.pointerMove( strip, { clientX: 40, pointerId: 1 } );
|
|
116
|
+
expect( onChange ).toHaveBeenCalled();
|
|
117
|
+
|
|
118
|
+
rerender( <RotationRuler { ...props } disabled /> );
|
|
119
|
+
onChange.mockClear();
|
|
120
|
+
|
|
121
|
+
fireEvent.pointerMove( strip, { clientX: 90, pointerId: 1 } );
|
|
122
|
+
expect( onChange ).not.toHaveBeenCalled();
|
|
123
|
+
} );
|
|
124
|
+
|
|
87
125
|
it( 'does not fire onChange when disabled', async () => {
|
|
88
126
|
const user = userEvent.setup();
|
|
89
|
-
const onChange =
|
|
127
|
+
const onChange = vi.fn();
|
|
90
128
|
render(
|
|
91
129
|
<RotationRuler
|
|
92
130
|
value={ 0 }
|
|
@@ -152,6 +152,14 @@ export function useRulerDrag(
|
|
|
152
152
|
// listeners or stale pointer capture.
|
|
153
153
|
useEffect( () => endDrag, [ endDrag ] );
|
|
154
154
|
|
|
155
|
+
// Becoming disabled mid-drag ends it, so a rotation cannot keep
|
|
156
|
+
// changing after a save has read it.
|
|
157
|
+
useEffect( () => {
|
|
158
|
+
if ( disabled ) {
|
|
159
|
+
endDrag();
|
|
160
|
+
}
|
|
161
|
+
}, [ disabled, endDrag ] );
|
|
162
|
+
|
|
155
163
|
const onPointerDown = useCallback(
|
|
156
164
|
( event: React.PointerEvent< HTMLElement > ) => {
|
|
157
165
|
if ( disabled || event.button !== 0 ) {
|
|
@@ -184,7 +192,7 @@ export function useRulerDrag(
|
|
|
184
192
|
const onPointerMove = useCallback(
|
|
185
193
|
( event: React.PointerEvent< HTMLElement > ) => {
|
|
186
194
|
const state = latestRef.current;
|
|
187
|
-
if ( ! state.dragging ) {
|
|
195
|
+
if ( disabled || ! state.dragging ) {
|
|
188
196
|
return;
|
|
189
197
|
}
|
|
190
198
|
// Shift toggles fine mode mid-drag. The drag math computes
|
|
@@ -215,7 +223,7 @@ export function useRulerDrag(
|
|
|
215
223
|
onChange( next );
|
|
216
224
|
}
|
|
217
225
|
},
|
|
218
|
-
[ onChange, min, max, step, pixelsPerStep ]
|
|
226
|
+
[ disabled, onChange, min, max, step, pixelsPerStep ]
|
|
219
227
|
);
|
|
220
228
|
|
|
221
229
|
return {
|