@wordpress/media-editor 0.13.0 → 0.14.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 +9 -0
- package/build/components/media-editor/index.cjs +38 -39
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor-crop-panel/index.cjs +0 -1
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +1 -11
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +8 -8
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +75 -32
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +40 -43
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +0 -1
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +1 -11
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +8 -8
- 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 +76 -38
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-style/style-rtl.css +12 -6
- package/build-style/style.css +12 -6
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
- package/build-types/components/media-editor/use-save-media-editor.d.ts +1 -1
- package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
- package/build-types/components/media-editor-canvas/index.d.ts +1 -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 +1 -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 +1 -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 +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts +1 -1
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/index.d.ts +1 -1
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-provider/index.d.ts +1 -1
- package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts +1 -1
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts +1 -1
- package/build-types/image-editor/react/components/cropper-provider.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/overlays/dimensions-overlay.d.ts +1 -1
- package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +1 -1
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +1 -1
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/components/viewport-provider.d.ts +1 -1
- package/build-types/image-editor/react/components/viewport-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +5 -4
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
- package/build-types/state/media-editor-state-provider.d.ts +1 -1
- package/build-types/state/media-editor-state-provider.d.ts.map +1 -1
- package/build-types/store/actions.d.ts +1 -1
- package/build-types/store/actions.d.ts.map +1 -1
- package/package.json +23 -21
- package/src/components/media-editor/index.tsx +75 -74
- package/src/components/media-editor/style.scss +9 -10
- package/src/components/media-editor-crop-panel/index.tsx +0 -1
- package/src/components/media-editor-image-controls/style.scss +1 -1
- package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +1 -1
- package/src/components/media-preview/style.scss +1 -1
- package/src/components/rotation-ruler/style.scss +2 -2
- package/src/image-editor/react/components/cropper.tsx +3 -12
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +8 -8
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +17 -15
- package/src/image-editor/react/components/test/cropper.tsx +17 -17
- package/src/image-editor/react/hooks/test/use-aria-announcer.ts +413 -5
- package/src/image-editor/react/hooks/use-aria-announcer.ts +120 -47
- package/src/image-editor/stories/rectangle-crop.story.tsx +0 -7
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { act, renderHook } from '@testing-library/react';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* WordPress dependencies
|
|
8
|
+
*/
|
|
9
|
+
import { speak } from '@wordpress/a11y';
|
|
10
|
+
|
|
6
11
|
/**
|
|
7
12
|
* Internal dependencies
|
|
8
13
|
*/
|
|
@@ -10,6 +15,10 @@ import { useAriaAnnouncer } from '../use-aria-announcer';
|
|
|
10
15
|
import { DEFAULT_STATE } from '../../../core/constants';
|
|
11
16
|
import type { CropperState } from '../../../core/types';
|
|
12
17
|
|
|
18
|
+
jest.mock( '@wordpress/a11y', () => ( {
|
|
19
|
+
speak: jest.fn(),
|
|
20
|
+
} ) );
|
|
21
|
+
|
|
13
22
|
function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
14
23
|
return {
|
|
15
24
|
...DEFAULT_STATE,
|
|
@@ -28,6 +37,7 @@ function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
|
28
37
|
describe( 'useAriaAnnouncer', () => {
|
|
29
38
|
beforeEach( () => {
|
|
30
39
|
jest.useFakeTimers();
|
|
40
|
+
( speak as jest.Mock ).mockClear();
|
|
31
41
|
} );
|
|
32
42
|
|
|
33
43
|
afterEach( () => {
|
|
@@ -35,7 +45,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
35
45
|
} );
|
|
36
46
|
|
|
37
47
|
it( 'announces horizontal flip changes', () => {
|
|
38
|
-
const {
|
|
48
|
+
const { rerender } = renderHook(
|
|
39
49
|
( { state } ) => useAriaAnnouncer( state ),
|
|
40
50
|
{ initialProps: { state: makeState() } }
|
|
41
51
|
);
|
|
@@ -49,7 +59,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
49
59
|
} );
|
|
50
60
|
act( () => jest.advanceTimersByTime( 300 ) );
|
|
51
61
|
|
|
52
|
-
expect(
|
|
62
|
+
expect( speak ).toHaveBeenCalledWith( 'Flipped horizontally' );
|
|
53
63
|
|
|
54
64
|
rerender( {
|
|
55
65
|
state: makeState( {
|
|
@@ -58,24 +68,422 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
58
68
|
} );
|
|
59
69
|
act( () => jest.advanceTimersByTime( 300 ) );
|
|
60
70
|
|
|
61
|
-
expect(
|
|
71
|
+
expect( speak ).toHaveBeenCalledWith( 'Flip removed' );
|
|
62
72
|
} );
|
|
63
73
|
|
|
64
74
|
it( 'announces combined flip state when both axes are active', () => {
|
|
65
|
-
const {
|
|
75
|
+
const { rerender } = renderHook(
|
|
76
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
77
|
+
{ initialProps: { state: makeState() } }
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
81
|
+
|
|
82
|
+
rerender( {
|
|
83
|
+
state: makeState( {
|
|
84
|
+
flip: { horizontal: true, vertical: true },
|
|
85
|
+
} ),
|
|
86
|
+
} );
|
|
87
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
88
|
+
|
|
89
|
+
expect( speak ).toHaveBeenCalledWith(
|
|
90
|
+
'Flipped horizontally and vertically'
|
|
91
|
+
);
|
|
92
|
+
} );
|
|
93
|
+
|
|
94
|
+
it( 'announces crop in pixels when image dimensions are available', () => {
|
|
95
|
+
const image = {
|
|
96
|
+
src: 'test.jpg',
|
|
97
|
+
naturalWidth: 1000,
|
|
98
|
+
naturalHeight: 800,
|
|
99
|
+
};
|
|
100
|
+
const { rerender } = renderHook(
|
|
101
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
102
|
+
{ initialProps: { state: makeState( { image } ) } }
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
106
|
+
|
|
107
|
+
rerender( {
|
|
108
|
+
state: makeState( {
|
|
109
|
+
image,
|
|
110
|
+
cropRect: { x: 0, y: 0, width: 0.8, height: 0.5 },
|
|
111
|
+
} ),
|
|
112
|
+
} );
|
|
113
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
114
|
+
|
|
115
|
+
expect( speak ).toHaveBeenCalledWith( 'Crop 800 by 400 pixels' );
|
|
116
|
+
} );
|
|
117
|
+
|
|
118
|
+
it( 'announces a single-pixel crop change', () => {
|
|
119
|
+
const image = {
|
|
120
|
+
src: 'test.jpg',
|
|
121
|
+
naturalWidth: 2000,
|
|
122
|
+
naturalHeight: 1000,
|
|
123
|
+
};
|
|
124
|
+
const { rerender } = renderHook(
|
|
125
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
126
|
+
{
|
|
127
|
+
initialProps: {
|
|
128
|
+
state: makeState( {
|
|
129
|
+
image,
|
|
130
|
+
cropRect: { x: 0, y: 0, width: 0.5, height: 0.5 },
|
|
131
|
+
} ),
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
137
|
+
|
|
138
|
+
// 0.5 → 0.5005 of a 2000px-wide image is a one-pixel change (1000 →
|
|
139
|
+
// 1001), well under the old ~0.5% percentage threshold.
|
|
140
|
+
rerender( {
|
|
141
|
+
state: makeState( {
|
|
142
|
+
image,
|
|
143
|
+
cropRect: { x: 0, y: 0, width: 0.5005, height: 0.5 },
|
|
144
|
+
} ),
|
|
145
|
+
} );
|
|
146
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
147
|
+
|
|
148
|
+
expect( speak ).toHaveBeenCalledWith( 'Crop 1001 by 500 pixels' );
|
|
149
|
+
} );
|
|
150
|
+
|
|
151
|
+
it( 'does not announce the crop when only zoom changes', () => {
|
|
152
|
+
const image = {
|
|
153
|
+
src: 'test.jpg',
|
|
154
|
+
naturalWidth: 2000,
|
|
155
|
+
naturalHeight: 1000,
|
|
156
|
+
};
|
|
157
|
+
const { rerender } = renderHook(
|
|
158
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
159
|
+
{
|
|
160
|
+
initialProps: {
|
|
161
|
+
state: makeState( {
|
|
162
|
+
image,
|
|
163
|
+
cropRect: { x: 0, y: 0, width: 0.5, height: 0.5 },
|
|
164
|
+
} ),
|
|
165
|
+
},
|
|
166
|
+
}
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
170
|
+
|
|
171
|
+
rerender( {
|
|
172
|
+
state: makeState( {
|
|
173
|
+
image,
|
|
174
|
+
cropRect: { x: 0, y: 0, width: 0.5, height: 0.5 },
|
|
175
|
+
zoom: 1.5,
|
|
176
|
+
} ),
|
|
177
|
+
} );
|
|
178
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
179
|
+
|
|
180
|
+
expect( speak ).toHaveBeenCalledWith( 'Zoom 150%' );
|
|
181
|
+
} );
|
|
182
|
+
|
|
183
|
+
it( 'announces a single-pixel crop change on a rotated non-square image', () => {
|
|
184
|
+
// Portrait image rotated 90°: the announced width scales by the
|
|
185
|
+
// snap-rotation bbox (2000), not naturalWidth (800), so a one-pixel
|
|
186
|
+
// change is 0.0005 of the crop rect — below a naturalWidth-based guard.
|
|
187
|
+
const image = {
|
|
188
|
+
src: 'test.jpg',
|
|
189
|
+
naturalWidth: 800,
|
|
190
|
+
naturalHeight: 2000,
|
|
191
|
+
};
|
|
192
|
+
const { rerender } = renderHook(
|
|
193
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
194
|
+
{
|
|
195
|
+
initialProps: {
|
|
196
|
+
state: makeState( {
|
|
197
|
+
image,
|
|
198
|
+
rotation: 90,
|
|
199
|
+
cropRect: { x: 0, y: 0, width: 0.5, height: 0.5 },
|
|
200
|
+
} ),
|
|
201
|
+
},
|
|
202
|
+
}
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
206
|
+
|
|
207
|
+
rerender( {
|
|
208
|
+
state: makeState( {
|
|
209
|
+
image,
|
|
210
|
+
rotation: 90,
|
|
211
|
+
cropRect: { x: 0, y: 0, width: 0.5005, height: 0.5 },
|
|
212
|
+
} ),
|
|
213
|
+
} );
|
|
214
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
215
|
+
|
|
216
|
+
expect( speak ).toHaveBeenCalledWith( 'Crop 1001 by 400 pixels' );
|
|
217
|
+
} );
|
|
218
|
+
|
|
219
|
+
it( 'announces clockwise rotation with direction', () => {
|
|
220
|
+
const { rerender } = renderHook(
|
|
66
221
|
( { state } ) => useAriaAnnouncer( state ),
|
|
67
222
|
{ initialProps: { state: makeState() } }
|
|
68
223
|
);
|
|
69
224
|
|
|
70
225
|
act( () => jest.advanceTimersByTime( 300 ) );
|
|
71
226
|
|
|
227
|
+
rerender( {
|
|
228
|
+
state: makeState( { rotation: 15 } ),
|
|
229
|
+
} );
|
|
230
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
231
|
+
|
|
232
|
+
expect( speak ).toHaveBeenCalledWith( 'Rotated 15 degrees clockwise' );
|
|
233
|
+
} );
|
|
234
|
+
|
|
235
|
+
it( 'announces counterclockwise when flip inverts visual direction', () => {
|
|
236
|
+
const { rerender } = renderHook(
|
|
237
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
238
|
+
{
|
|
239
|
+
initialProps: {
|
|
240
|
+
state: makeState( {
|
|
241
|
+
flip: { horizontal: true, vertical: false },
|
|
242
|
+
} ),
|
|
243
|
+
},
|
|
244
|
+
}
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
248
|
+
|
|
249
|
+
// With a single-axis flip, rotation 10 appears as -10 visually.
|
|
250
|
+
rerender( {
|
|
251
|
+
state: makeState( {
|
|
252
|
+
rotation: 10,
|
|
253
|
+
flip: { horizontal: true, vertical: false },
|
|
254
|
+
} ),
|
|
255
|
+
} );
|
|
256
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
257
|
+
|
|
258
|
+
expect( speak ).toHaveBeenCalledWith(
|
|
259
|
+
'Rotated 10 degrees counterclockwise'
|
|
260
|
+
);
|
|
261
|
+
} );
|
|
262
|
+
|
|
263
|
+
it( 'announces a 90° snap under a single flip in visual terms', () => {
|
|
264
|
+
const { rerender } = renderHook(
|
|
265
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
266
|
+
{
|
|
267
|
+
initialProps: {
|
|
268
|
+
state: makeState( {
|
|
269
|
+
flip: { horizontal: true, vertical: false },
|
|
270
|
+
} ),
|
|
271
|
+
},
|
|
272
|
+
}
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
276
|
+
|
|
277
|
+
// Flip horizontal, then rotate 90° clockwise: the reducer negates the
|
|
278
|
+
// visual direction for the field, so it stores 270 (normalized -90).
|
|
279
|
+
// Visually the user rotated 90° clockwise, so that's what we announce.
|
|
280
|
+
rerender( {
|
|
281
|
+
state: makeState( {
|
|
282
|
+
rotation: 270,
|
|
283
|
+
flip: { horizontal: true, vertical: false },
|
|
284
|
+
} ),
|
|
285
|
+
} );
|
|
286
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
287
|
+
|
|
288
|
+
expect( speak ).toHaveBeenCalledWith( 'Rotated 90 degrees clockwise' );
|
|
289
|
+
} );
|
|
290
|
+
|
|
291
|
+
it( 'announces a 90° snap under a vertical flip in visual terms', () => {
|
|
292
|
+
const { rerender } = renderHook(
|
|
293
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
294
|
+
{
|
|
295
|
+
initialProps: {
|
|
296
|
+
state: makeState( {
|
|
297
|
+
flip: { horizontal: false, vertical: true },
|
|
298
|
+
} ),
|
|
299
|
+
},
|
|
300
|
+
}
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
304
|
+
|
|
72
305
|
rerender( {
|
|
73
306
|
state: makeState( {
|
|
307
|
+
rotation: 270,
|
|
308
|
+
flip: { horizontal: false, vertical: true },
|
|
309
|
+
} ),
|
|
310
|
+
} );
|
|
311
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
312
|
+
|
|
313
|
+
expect( speak ).toHaveBeenCalledWith( 'Rotated 90 degrees clockwise' );
|
|
314
|
+
} );
|
|
315
|
+
|
|
316
|
+
it( 'announces non-inverted rotation when both axes are flipped', () => {
|
|
317
|
+
// Two mirrors restore handedness (equivalent to a 180° rotation), so
|
|
318
|
+
// the visual direction matches the unflipped case, not the single-flip.
|
|
319
|
+
const { rerender } = renderHook(
|
|
320
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
321
|
+
{
|
|
322
|
+
initialProps: {
|
|
323
|
+
state: makeState( {
|
|
324
|
+
flip: { horizontal: true, vertical: true },
|
|
325
|
+
} ),
|
|
326
|
+
},
|
|
327
|
+
}
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
331
|
+
|
|
332
|
+
rerender( {
|
|
333
|
+
state: makeState( {
|
|
334
|
+
rotation: 270,
|
|
74
335
|
flip: { horizontal: true, vertical: true },
|
|
75
336
|
} ),
|
|
76
337
|
} );
|
|
77
338
|
act( () => jest.advanceTimersByTime( 300 ) );
|
|
78
339
|
|
|
79
|
-
expect(
|
|
340
|
+
expect( speak ).toHaveBeenCalledWith(
|
|
341
|
+
'Rotated 90 degrees counterclockwise'
|
|
342
|
+
);
|
|
343
|
+
} );
|
|
344
|
+
|
|
345
|
+
it( 'announces multiple changes with rotation first', () => {
|
|
346
|
+
const { rerender } = renderHook(
|
|
347
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
348
|
+
{ initialProps: { state: makeState() } }
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
352
|
+
|
|
353
|
+
rerender( {
|
|
354
|
+
state: makeState( { rotation: 90, zoom: 1.5 } ),
|
|
355
|
+
} );
|
|
356
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
357
|
+
|
|
358
|
+
expect( speak ).toHaveBeenCalledWith(
|
|
359
|
+
'Rotated 90 degrees clockwise, Zoom 150%'
|
|
360
|
+
);
|
|
361
|
+
} );
|
|
362
|
+
|
|
363
|
+
it( 'announces rotation back to zero', () => {
|
|
364
|
+
const { rerender } = renderHook(
|
|
365
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
366
|
+
{ initialProps: { state: makeState( { rotation: 15 } ) } }
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
370
|
+
|
|
371
|
+
rerender( {
|
|
372
|
+
state: makeState( { rotation: 0 } ),
|
|
373
|
+
} );
|
|
374
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
375
|
+
|
|
376
|
+
expect( speak ).toHaveBeenCalledWith( 'Rotation 0 degrees' );
|
|
377
|
+
} );
|
|
378
|
+
|
|
379
|
+
it( 'announces rotation after 90° snap plus fine adjustment', () => {
|
|
380
|
+
const { rerender } = renderHook(
|
|
381
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
382
|
+
{ initialProps: { state: makeState() } }
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
386
|
+
|
|
387
|
+
// 90° CW snap + 15° fine = 105° stored.
|
|
388
|
+
rerender( {
|
|
389
|
+
state: makeState( { rotation: 105 } ),
|
|
390
|
+
} );
|
|
391
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
392
|
+
|
|
393
|
+
expect( speak ).toHaveBeenCalledWith( 'Rotated 105 degrees clockwise' );
|
|
394
|
+
} );
|
|
395
|
+
|
|
396
|
+
it( 'announces CCW for 90° snap CCW plus fine CCW adjustment', () => {
|
|
397
|
+
const { rerender } = renderHook(
|
|
398
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
399
|
+
{ initialProps: { state: makeState() } }
|
|
400
|
+
);
|
|
401
|
+
|
|
402
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
403
|
+
|
|
404
|
+
// 90° CCW snap (270°) + 15° CCW fine (255° stored).
|
|
405
|
+
// visualRotation = 255 → normalized to -105.
|
|
406
|
+
rerender( {
|
|
407
|
+
state: makeState( { rotation: 255 } ),
|
|
408
|
+
} );
|
|
409
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
410
|
+
|
|
411
|
+
expect( speak ).toHaveBeenCalledWith(
|
|
412
|
+
'Rotated 105 degrees counterclockwise'
|
|
413
|
+
);
|
|
414
|
+
} );
|
|
415
|
+
|
|
416
|
+
it( 'announces CCW fine rotation from 0° (stored as 350°)', () => {
|
|
417
|
+
const { rerender } = renderHook(
|
|
418
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
419
|
+
{ initialProps: { state: makeState() } }
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
423
|
+
|
|
424
|
+
// -10° from 0° is stored as 350° after normalization.
|
|
425
|
+
// visualRotation = -10 → counterclockwise.
|
|
426
|
+
rerender( {
|
|
427
|
+
state: makeState( { rotation: 350 } ),
|
|
428
|
+
} );
|
|
429
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
430
|
+
|
|
431
|
+
expect( speak ).toHaveBeenCalledWith(
|
|
432
|
+
'Rotated 10 degrees counterclockwise'
|
|
433
|
+
);
|
|
434
|
+
} );
|
|
435
|
+
|
|
436
|
+
it( 'suppresses unchanged values in combined announcements', () => {
|
|
437
|
+
const image = {
|
|
438
|
+
src: 'test.jpg',
|
|
439
|
+
naturalWidth: 1000,
|
|
440
|
+
naturalHeight: 800,
|
|
441
|
+
};
|
|
442
|
+
const { rerender } = renderHook(
|
|
443
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
444
|
+
{
|
|
445
|
+
initialProps: {
|
|
446
|
+
state: makeState( { image, rotation: 15 } ),
|
|
447
|
+
},
|
|
448
|
+
}
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
452
|
+
|
|
453
|
+
( speak as jest.Mock ).mockClear();
|
|
454
|
+
|
|
455
|
+
// Only change crop — zoom and rotation should be suppressed.
|
|
456
|
+
rerender( {
|
|
457
|
+
state: makeState( {
|
|
458
|
+
image,
|
|
459
|
+
rotation: 15,
|
|
460
|
+
cropRect: { x: 0, y: 0, width: 0.5, height: 0.5 },
|
|
461
|
+
} ),
|
|
462
|
+
} );
|
|
463
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
464
|
+
|
|
465
|
+
expect( speak ).toHaveBeenCalledWith( 'Crop 500 by 400 pixels' );
|
|
466
|
+
expect( speak ).not.toHaveBeenCalledWith(
|
|
467
|
+
expect.stringContaining( 'Zoom' )
|
|
468
|
+
);
|
|
469
|
+
expect( speak ).not.toHaveBeenCalledWith(
|
|
470
|
+
expect.stringContaining( 'Rotated' )
|
|
471
|
+
);
|
|
472
|
+
} );
|
|
473
|
+
|
|
474
|
+
it( 'announces only zoom when only zoom changes', () => {
|
|
475
|
+
const { rerender } = renderHook(
|
|
476
|
+
( { state } ) => useAriaAnnouncer( state ),
|
|
477
|
+
{ initialProps: { state: makeState() } }
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
481
|
+
|
|
482
|
+
rerender( {
|
|
483
|
+
state: makeState( { zoom: 1.5 } ),
|
|
484
|
+
} );
|
|
485
|
+
act( () => jest.advanceTimersByTime( 300 ) );
|
|
486
|
+
|
|
487
|
+
expect( speak ).toHaveBeenCalledWith( 'Zoom 150%' );
|
|
80
488
|
} );
|
|
81
489
|
} );
|
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
useEffect,
|
|
7
|
-
useLayoutEffect,
|
|
8
|
-
useRef,
|
|
9
|
-
} from '@wordpress/element';
|
|
4
|
+
import { speak } from '@wordpress/a11y';
|
|
5
|
+
import { useEffect, useLayoutEffect, useRef } from '@wordpress/element';
|
|
10
6
|
import { __, sprintf } from '@wordpress/i18n';
|
|
11
7
|
|
|
12
8
|
/**
|
|
13
9
|
* Internal dependencies
|
|
14
10
|
*/
|
|
15
11
|
import type { CropperState } from '../../core/types';
|
|
12
|
+
import { getSourceRegion } from '../../core/source-region';
|
|
16
13
|
|
|
17
14
|
/** Debounce delay for ARIA live announcements (ms). */
|
|
18
15
|
const ARIA_DEBOUNCE_MS = 300;
|
|
19
16
|
|
|
20
|
-
function
|
|
17
|
+
function getFlipAnnouncement(
|
|
18
|
+
state: CropperState,
|
|
19
|
+
previousState: CropperState | null
|
|
20
|
+
): string | undefined {
|
|
21
|
+
if (
|
|
22
|
+
! previousState ||
|
|
23
|
+
( previousState.flip.horizontal === state.flip.horizontal &&
|
|
24
|
+
previousState.flip.vertical === state.flip.vertical )
|
|
25
|
+
) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
21
28
|
const { horizontal, vertical } = state.flip;
|
|
22
29
|
if ( horizontal && vertical ) {
|
|
23
30
|
return __( 'Flipped horizontally and vertically' );
|
|
@@ -31,64 +38,132 @@ function buildFlipAnnouncement( state: CropperState ): string {
|
|
|
31
38
|
return __( 'Flip removed' );
|
|
32
39
|
}
|
|
33
40
|
|
|
34
|
-
|
|
35
|
-
function buildAnnouncement(
|
|
41
|
+
function getRotationAnnouncement(
|
|
36
42
|
state: CropperState,
|
|
37
43
|
previousState: CropperState | null
|
|
38
|
-
): string {
|
|
44
|
+
): string | undefined {
|
|
39
45
|
if (
|
|
40
46
|
previousState &&
|
|
41
|
-
( previousState.
|
|
42
|
-
previousState.flip.vertical !== state.flip.vertical )
|
|
47
|
+
Math.round( previousState.rotation ) === Math.round( state.rotation )
|
|
43
48
|
) {
|
|
44
|
-
return
|
|
49
|
+
return undefined;
|
|
45
50
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
// Announce the visual rotation — what the user perceives on screen. A
|
|
52
|
+
// single-axis flip mirrors the image, reversing the entire on-screen
|
|
53
|
+
// rotation relative to the stored field (the same S·R·S inversion the
|
|
54
|
+
// reducer applies), so negate the whole angle in that case.
|
|
55
|
+
const singleFlip = state.flip.horizontal !== state.flip.vertical;
|
|
56
|
+
const visualDir = singleFlip ? -1 : 1;
|
|
57
|
+
// Normalize to (-180, 180] so the sign indicates direction.
|
|
58
|
+
let visualRotation = ( Math.round( state.rotation ) * visualDir ) % 360;
|
|
59
|
+
if ( visualRotation > 180 ) {
|
|
60
|
+
visualRotation -= 360;
|
|
61
|
+
}
|
|
62
|
+
if ( visualRotation <= -180 ) {
|
|
63
|
+
visualRotation += 360;
|
|
64
|
+
}
|
|
65
|
+
if ( visualRotation === 0 ) {
|
|
66
|
+
return previousState ? __( 'Rotation 0 degrees' ) : undefined;
|
|
67
|
+
}
|
|
68
|
+
if ( visualRotation > 0 ) {
|
|
69
|
+
return sprintf(
|
|
70
|
+
/* translators: %d: rotation angle in degrees. */
|
|
71
|
+
__( 'Rotated %d degrees clockwise' ),
|
|
72
|
+
visualRotation
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return sprintf(
|
|
76
|
+
/* translators: %d: rotation angle in degrees. */
|
|
77
|
+
__( 'Rotated %d degrees counterclockwise' ),
|
|
78
|
+
Math.abs( visualRotation )
|
|
54
79
|
);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function getCropAnnouncement(
|
|
83
|
+
state: CropperState,
|
|
84
|
+
previousState: CropperState | null
|
|
85
|
+
): string | undefined {
|
|
86
|
+
if ( ! state.image ) {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
const imageSize = {
|
|
90
|
+
width: state.image.naturalWidth,
|
|
91
|
+
height: state.image.naturalHeight,
|
|
92
|
+
};
|
|
93
|
+
const region = getSourceRegion( state, imageSize );
|
|
94
|
+
// Announce only when the crop's pixel dimensions change. Measuring both the
|
|
95
|
+
// previous and current crop rects under the current rotation and zoom keeps
|
|
96
|
+
// the comparison in the same pixel units we announce, and leaves a zoom-only
|
|
97
|
+
// change — which doesn't move the crop rect — silent.
|
|
98
|
+
if ( previousState?.image ) {
|
|
99
|
+
const previousRegion = getSourceRegion(
|
|
100
|
+
{ ...state, cropRect: previousState.cropRect },
|
|
101
|
+
imageSize
|
|
62
102
|
);
|
|
103
|
+
if (
|
|
104
|
+
Math.round( previousRegion.width ) === Math.round( region.width ) &&
|
|
105
|
+
Math.round( previousRegion.height ) === Math.round( region.height )
|
|
106
|
+
) {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
63
109
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
__( 'Crop %1$d%% by %2$d%%' ),
|
|
70
|
-
cropW,
|
|
71
|
-
cropH
|
|
72
|
-
)
|
|
110
|
+
return sprintf(
|
|
111
|
+
/* translators: 1: crop width in pixels, 2: crop height in pixels. */
|
|
112
|
+
__( 'Crop %1$d by %2$d pixels' ),
|
|
113
|
+
Math.round( region.width ),
|
|
114
|
+
Math.round( region.height )
|
|
73
115
|
);
|
|
74
|
-
|
|
75
|
-
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function getZoomAnnouncement(
|
|
119
|
+
state: CropperState,
|
|
120
|
+
previousState: CropperState | null
|
|
121
|
+
): string | undefined {
|
|
122
|
+
if (
|
|
123
|
+
previousState &&
|
|
124
|
+
Math.round( previousState.zoom * 100 ) ===
|
|
125
|
+
Math.round( state.zoom * 100 )
|
|
126
|
+
) {
|
|
127
|
+
return undefined;
|
|
76
128
|
}
|
|
129
|
+
return sprintf(
|
|
130
|
+
/* translators: %d: zoom level as a percentage. */
|
|
131
|
+
__( 'Zoom %d%%' ),
|
|
132
|
+
Math.round( state.zoom * 100 )
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function buildAnnouncement(
|
|
137
|
+
state: CropperState,
|
|
138
|
+
previousState: CropperState | null
|
|
139
|
+
): string {
|
|
140
|
+
// Flip changes are announced alone.
|
|
141
|
+
const flip = getFlipAnnouncement( state, previousState );
|
|
142
|
+
if ( flip ) {
|
|
143
|
+
return flip;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const parts = [
|
|
147
|
+
getRotationAnnouncement( state, previousState ),
|
|
148
|
+
getZoomAnnouncement( state, previousState ),
|
|
149
|
+
getCropAnnouncement( state, previousState ),
|
|
150
|
+
].filter( ( part ): part is string => part !== undefined );
|
|
151
|
+
|
|
77
152
|
return parts.join( ', ' );
|
|
78
153
|
}
|
|
79
154
|
|
|
80
155
|
/**
|
|
81
156
|
* Debounce and dedupe ARIA-live announcements for screen readers as the
|
|
82
|
-
* cropper state changes.
|
|
83
|
-
*
|
|
157
|
+
* cropper state changes. Calls `speak()` from `@wordpress/a11y` to use the
|
|
158
|
+
* centralized, persistent live region instead of a local one that may be
|
|
159
|
+
* dynamically mounted.
|
|
84
160
|
*
|
|
85
|
-
* Debouncing avoids flooding
|
|
161
|
+
* Debouncing avoids flooding announcements during drag/pointermove bursts;
|
|
86
162
|
* deduping avoids re-announcing the same state.
|
|
87
163
|
*
|
|
88
164
|
* @param state The current cropper state to announce.
|
|
89
165
|
*/
|
|
90
|
-
export function useAriaAnnouncer( state: CropperState ):
|
|
91
|
-
const [ ariaMessage, setAriaMessage ] = useState( '' );
|
|
166
|
+
export function useAriaAnnouncer( state: CropperState ): void {
|
|
92
167
|
const timerRef = useRef< ReturnType< typeof setTimeout > >();
|
|
93
168
|
const prevMessageRef = useRef( '' );
|
|
94
169
|
const prevStateRef = useRef< CropperState | null >( null );
|
|
@@ -105,7 +180,7 @@ export function useAriaAnnouncer( state: CropperState ): string {
|
|
|
105
180
|
prevStateRef.current = current;
|
|
106
181
|
if ( msg !== prevMessageRef.current ) {
|
|
107
182
|
prevMessageRef.current = msg;
|
|
108
|
-
|
|
183
|
+
speak( msg );
|
|
109
184
|
}
|
|
110
185
|
}, ARIA_DEBOUNCE_MS );
|
|
111
186
|
|
|
@@ -120,6 +195,4 @@ export function useAriaAnnouncer( state: CropperState ): string {
|
|
|
120
195
|
state.flip.horizontal,
|
|
121
196
|
state.flip.vertical,
|
|
122
197
|
] );
|
|
123
|
-
|
|
124
|
-
return ariaMessage;
|
|
125
198
|
}
|