@wordpress/media-editor 0.17.2-next.v.202609031004.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-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 +22 -22
- package/src/components/media-editor/index.tsx +42 -5
- package/src/components/media-editor/test/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.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.jsdom.test.tsx +37 -0
- package/src/components/media-editor-modal/test/index.jsdom.test.tsx +65 -35
- package/src/components/rotation-ruler/index.tsx +1 -0
- package/src/components/rotation-ruler/test/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.jsdom.test.ts +76 -63
- package/src/image-editor/core/interaction-controller.ts +8 -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.jsdom.test.tsx +54 -36
- package/src/image-editor/react/components/test/cropper.jsdom.test.tsx +304 -0
- package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +57 -44
- package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +1 -0
- package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +17 -16
- package/src/image-editor/react/hooks/test/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.jsdom.test.ts +1 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.json +6 -1
|
@@ -1,12 +1,25 @@
|
|
|
1
|
+
import {
|
|
2
|
+
afterEach,
|
|
3
|
+
beforeEach,
|
|
4
|
+
describe,
|
|
5
|
+
expect,
|
|
6
|
+
it,
|
|
7
|
+
vi,
|
|
8
|
+
type Mock,
|
|
9
|
+
} from 'vitest';
|
|
1
10
|
import { act, renderHook } from '@testing-library/react';
|
|
2
11
|
import { speak } from '@wordpress/a11y';
|
|
3
12
|
import { useAriaAnnouncer } from '../use-aria-announcer';
|
|
4
13
|
import { DEFAULT_STATE } from '../../../core/constants';
|
|
5
14
|
import type { CropperState } from '../../../core/types';
|
|
6
15
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
16
|
+
vi.mock(
|
|
17
|
+
import( '@wordpress/a11y' ),
|
|
18
|
+
() =>
|
|
19
|
+
( {
|
|
20
|
+
speak: vi.fn(),
|
|
21
|
+
} ) as unknown as typeof import('@wordpress/a11y')
|
|
22
|
+
);
|
|
10
23
|
|
|
11
24
|
function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
12
25
|
return {
|
|
@@ -25,12 +38,12 @@ function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
|
25
38
|
|
|
26
39
|
describe( 'useAriaAnnouncer', () => {
|
|
27
40
|
beforeEach( () => {
|
|
28
|
-
|
|
29
|
-
( speak as
|
|
41
|
+
vi.useFakeTimers();
|
|
42
|
+
( speak as Mock ).mockClear();
|
|
30
43
|
} );
|
|
31
44
|
|
|
32
45
|
afterEach( () => {
|
|
33
|
-
|
|
46
|
+
vi.useRealTimers();
|
|
34
47
|
} );
|
|
35
48
|
|
|
36
49
|
it( 'announces horizontal flip changes', () => {
|
|
@@ -39,14 +52,14 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
39
52
|
{ initialProps: { state: makeState() } }
|
|
40
53
|
);
|
|
41
54
|
|
|
42
|
-
act( () =>
|
|
55
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
43
56
|
|
|
44
57
|
rerender( {
|
|
45
58
|
state: makeState( {
|
|
46
59
|
flip: { horizontal: true, vertical: false },
|
|
47
60
|
} ),
|
|
48
61
|
} );
|
|
49
|
-
act( () =>
|
|
62
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
50
63
|
|
|
51
64
|
expect( speak ).toHaveBeenCalledWith( 'Flipped horizontally' );
|
|
52
65
|
|
|
@@ -55,7 +68,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
55
68
|
flip: { horizontal: false, vertical: false },
|
|
56
69
|
} ),
|
|
57
70
|
} );
|
|
58
|
-
act( () =>
|
|
71
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
59
72
|
|
|
60
73
|
expect( speak ).toHaveBeenCalledWith( 'Flip removed' );
|
|
61
74
|
} );
|
|
@@ -66,14 +79,14 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
66
79
|
{ initialProps: { state: makeState() } }
|
|
67
80
|
);
|
|
68
81
|
|
|
69
|
-
act( () =>
|
|
82
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
70
83
|
|
|
71
84
|
rerender( {
|
|
72
85
|
state: makeState( {
|
|
73
86
|
flip: { horizontal: true, vertical: true },
|
|
74
87
|
} ),
|
|
75
88
|
} );
|
|
76
|
-
act( () =>
|
|
89
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
77
90
|
|
|
78
91
|
expect( speak ).toHaveBeenCalledWith(
|
|
79
92
|
'Flipped horizontally and vertically'
|
|
@@ -91,7 +104,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
91
104
|
{ initialProps: { state: makeState( { image } ) } }
|
|
92
105
|
);
|
|
93
106
|
|
|
94
|
-
act( () =>
|
|
107
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
95
108
|
|
|
96
109
|
rerender( {
|
|
97
110
|
state: makeState( {
|
|
@@ -99,7 +112,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
99
112
|
cropRect: { x: 0, y: 0, width: 0.8, height: 0.5 },
|
|
100
113
|
} ),
|
|
101
114
|
} );
|
|
102
|
-
act( () =>
|
|
115
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
103
116
|
|
|
104
117
|
expect( speak ).toHaveBeenCalledWith( 'Crop 800 by 400 pixels' );
|
|
105
118
|
} );
|
|
@@ -122,7 +135,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
122
135
|
}
|
|
123
136
|
);
|
|
124
137
|
|
|
125
|
-
act( () =>
|
|
138
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
126
139
|
|
|
127
140
|
// 0.5 → 0.5005 of a 2000px-wide image is a one-pixel change (1000 →
|
|
128
141
|
// 1001), well under the old ~0.5% percentage threshold.
|
|
@@ -132,7 +145,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
132
145
|
cropRect: { x: 0, y: 0, width: 0.5005, height: 0.5 },
|
|
133
146
|
} ),
|
|
134
147
|
} );
|
|
135
|
-
act( () =>
|
|
148
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
136
149
|
|
|
137
150
|
expect( speak ).toHaveBeenCalledWith( 'Crop 1001 by 500 pixels' );
|
|
138
151
|
} );
|
|
@@ -155,7 +168,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
155
168
|
}
|
|
156
169
|
);
|
|
157
170
|
|
|
158
|
-
act( () =>
|
|
171
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
159
172
|
|
|
160
173
|
rerender( {
|
|
161
174
|
state: makeState( {
|
|
@@ -164,7 +177,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
164
177
|
zoom: 1.5,
|
|
165
178
|
} ),
|
|
166
179
|
} );
|
|
167
|
-
act( () =>
|
|
180
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
168
181
|
|
|
169
182
|
expect( speak ).toHaveBeenCalledWith( 'Zoom 150%' );
|
|
170
183
|
} );
|
|
@@ -191,7 +204,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
191
204
|
}
|
|
192
205
|
);
|
|
193
206
|
|
|
194
|
-
act( () =>
|
|
207
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
195
208
|
|
|
196
209
|
rerender( {
|
|
197
210
|
state: makeState( {
|
|
@@ -200,7 +213,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
200
213
|
cropRect: { x: 0, y: 0, width: 0.5005, height: 0.5 },
|
|
201
214
|
} ),
|
|
202
215
|
} );
|
|
203
|
-
act( () =>
|
|
216
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
204
217
|
|
|
205
218
|
expect( speak ).toHaveBeenCalledWith( 'Crop 1001 by 400 pixels' );
|
|
206
219
|
} );
|
|
@@ -211,12 +224,12 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
211
224
|
{ initialProps: { state: makeState() } }
|
|
212
225
|
);
|
|
213
226
|
|
|
214
|
-
act( () =>
|
|
227
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
215
228
|
|
|
216
229
|
rerender( {
|
|
217
230
|
state: makeState( { rotation: 15 } ),
|
|
218
231
|
} );
|
|
219
|
-
act( () =>
|
|
232
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
220
233
|
|
|
221
234
|
expect( speak ).toHaveBeenCalledWith( 'Rotated 15 degrees clockwise' );
|
|
222
235
|
} );
|
|
@@ -233,7 +246,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
233
246
|
}
|
|
234
247
|
);
|
|
235
248
|
|
|
236
|
-
act( () =>
|
|
249
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
237
250
|
|
|
238
251
|
// With a single-axis flip, rotation 10 appears as -10 visually.
|
|
239
252
|
rerender( {
|
|
@@ -242,7 +255,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
242
255
|
flip: { horizontal: true, vertical: false },
|
|
243
256
|
} ),
|
|
244
257
|
} );
|
|
245
|
-
act( () =>
|
|
258
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
246
259
|
|
|
247
260
|
expect( speak ).toHaveBeenCalledWith(
|
|
248
261
|
'Rotated 10 degrees counterclockwise'
|
|
@@ -261,7 +274,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
261
274
|
}
|
|
262
275
|
);
|
|
263
276
|
|
|
264
|
-
act( () =>
|
|
277
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
265
278
|
|
|
266
279
|
// Flip horizontal, then rotate 90° clockwise: the reducer negates the
|
|
267
280
|
// visual direction for the field, so it stores 270 (normalized -90).
|
|
@@ -272,7 +285,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
272
285
|
flip: { horizontal: true, vertical: false },
|
|
273
286
|
} ),
|
|
274
287
|
} );
|
|
275
|
-
act( () =>
|
|
288
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
276
289
|
|
|
277
290
|
expect( speak ).toHaveBeenCalledWith( 'Rotated 90 degrees clockwise' );
|
|
278
291
|
} );
|
|
@@ -289,7 +302,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
289
302
|
}
|
|
290
303
|
);
|
|
291
304
|
|
|
292
|
-
act( () =>
|
|
305
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
293
306
|
|
|
294
307
|
rerender( {
|
|
295
308
|
state: makeState( {
|
|
@@ -297,7 +310,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
297
310
|
flip: { horizontal: false, vertical: true },
|
|
298
311
|
} ),
|
|
299
312
|
} );
|
|
300
|
-
act( () =>
|
|
313
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
301
314
|
|
|
302
315
|
expect( speak ).toHaveBeenCalledWith( 'Rotated 90 degrees clockwise' );
|
|
303
316
|
} );
|
|
@@ -316,7 +329,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
316
329
|
}
|
|
317
330
|
);
|
|
318
331
|
|
|
319
|
-
act( () =>
|
|
332
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
320
333
|
|
|
321
334
|
rerender( {
|
|
322
335
|
state: makeState( {
|
|
@@ -324,7 +337,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
324
337
|
flip: { horizontal: true, vertical: true },
|
|
325
338
|
} ),
|
|
326
339
|
} );
|
|
327
|
-
act( () =>
|
|
340
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
328
341
|
|
|
329
342
|
expect( speak ).toHaveBeenCalledWith(
|
|
330
343
|
'Rotated 90 degrees counterclockwise'
|
|
@@ -337,12 +350,12 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
337
350
|
{ initialProps: { state: makeState() } }
|
|
338
351
|
);
|
|
339
352
|
|
|
340
|
-
act( () =>
|
|
353
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
341
354
|
|
|
342
355
|
rerender( {
|
|
343
356
|
state: makeState( { rotation: 90, zoom: 1.5 } ),
|
|
344
357
|
} );
|
|
345
|
-
act( () =>
|
|
358
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
346
359
|
|
|
347
360
|
expect( speak ).toHaveBeenCalledWith(
|
|
348
361
|
'Rotated 90 degrees clockwise, Zoom 150%'
|
|
@@ -355,12 +368,12 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
355
368
|
{ initialProps: { state: makeState( { rotation: 15 } ) } }
|
|
356
369
|
);
|
|
357
370
|
|
|
358
|
-
act( () =>
|
|
371
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
359
372
|
|
|
360
373
|
rerender( {
|
|
361
374
|
state: makeState( { rotation: 0 } ),
|
|
362
375
|
} );
|
|
363
|
-
act( () =>
|
|
376
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
364
377
|
|
|
365
378
|
expect( speak ).toHaveBeenCalledWith( 'Rotation 0 degrees' );
|
|
366
379
|
} );
|
|
@@ -371,13 +384,13 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
371
384
|
{ initialProps: { state: makeState() } }
|
|
372
385
|
);
|
|
373
386
|
|
|
374
|
-
act( () =>
|
|
387
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
375
388
|
|
|
376
389
|
// 90° CW snap + 15° fine = 105° stored.
|
|
377
390
|
rerender( {
|
|
378
391
|
state: makeState( { rotation: 105 } ),
|
|
379
392
|
} );
|
|
380
|
-
act( () =>
|
|
393
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
381
394
|
|
|
382
395
|
expect( speak ).toHaveBeenCalledWith( 'Rotated 105 degrees clockwise' );
|
|
383
396
|
} );
|
|
@@ -388,14 +401,14 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
388
401
|
{ initialProps: { state: makeState() } }
|
|
389
402
|
);
|
|
390
403
|
|
|
391
|
-
act( () =>
|
|
404
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
392
405
|
|
|
393
406
|
// 90° CCW snap (270°) + 15° CCW fine (255° stored).
|
|
394
407
|
// visualRotation = 255 → normalized to -105.
|
|
395
408
|
rerender( {
|
|
396
409
|
state: makeState( { rotation: 255 } ),
|
|
397
410
|
} );
|
|
398
|
-
act( () =>
|
|
411
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
399
412
|
|
|
400
413
|
expect( speak ).toHaveBeenCalledWith(
|
|
401
414
|
'Rotated 105 degrees counterclockwise'
|
|
@@ -408,14 +421,14 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
408
421
|
{ initialProps: { state: makeState() } }
|
|
409
422
|
);
|
|
410
423
|
|
|
411
|
-
act( () =>
|
|
424
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
412
425
|
|
|
413
426
|
// -10° from 0° is stored as 350° after normalization.
|
|
414
427
|
// visualRotation = -10 → counterclockwise.
|
|
415
428
|
rerender( {
|
|
416
429
|
state: makeState( { rotation: 350 } ),
|
|
417
430
|
} );
|
|
418
|
-
act( () =>
|
|
431
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
419
432
|
|
|
420
433
|
expect( speak ).toHaveBeenCalledWith(
|
|
421
434
|
'Rotated 10 degrees counterclockwise'
|
|
@@ -437,9 +450,9 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
437
450
|
}
|
|
438
451
|
);
|
|
439
452
|
|
|
440
|
-
act( () =>
|
|
453
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
441
454
|
|
|
442
|
-
( speak as
|
|
455
|
+
( speak as Mock ).mockClear();
|
|
443
456
|
|
|
444
457
|
// Only change crop — zoom and rotation should be suppressed.
|
|
445
458
|
rerender( {
|
|
@@ -449,7 +462,7 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
449
462
|
cropRect: { x: 0, y: 0, width: 0.5, height: 0.5 },
|
|
450
463
|
} ),
|
|
451
464
|
} );
|
|
452
|
-
act( () =>
|
|
465
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
453
466
|
|
|
454
467
|
expect( speak ).toHaveBeenCalledWith( 'Crop 500 by 400 pixels' );
|
|
455
468
|
expect( speak ).not.toHaveBeenCalledWith(
|
|
@@ -466,12 +479,12 @@ describe( 'useAriaAnnouncer', () => {
|
|
|
466
479
|
{ initialProps: { state: makeState() } }
|
|
467
480
|
);
|
|
468
481
|
|
|
469
|
-
act( () =>
|
|
482
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
470
483
|
|
|
471
484
|
rerender( {
|
|
472
485
|
state: makeState( { zoom: 1.5 } ),
|
|
473
486
|
} );
|
|
474
|
-
act( () =>
|
|
487
|
+
act( () => vi.advanceTimersByTime( 300 ) );
|
|
475
488
|
|
|
476
489
|
expect( speak ).toHaveBeenCalledWith( 'Zoom 150%' );
|
|
477
490
|
} );
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi, type Mocked } from 'vitest';
|
|
1
2
|
import { act, renderHook } from '@testing-library/react';
|
|
2
3
|
import { useInteraction } from '../use-interaction';
|
|
3
4
|
import type { CropperInteractionActions } from '../../../core/interaction-controller';
|
|
@@ -20,13 +21,13 @@ function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
|
20
21
|
};
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
function createActions():
|
|
24
|
+
function createActions(): Mocked< CropperInteractionActions > {
|
|
24
25
|
return {
|
|
25
|
-
setPan:
|
|
26
|
-
setZoom:
|
|
27
|
-
setZoomAtPoint:
|
|
28
|
-
snapRotate90:
|
|
29
|
-
toggleFlip:
|
|
26
|
+
setPan: vi.fn(),
|
|
27
|
+
setZoom: vi.fn(),
|
|
28
|
+
setZoomAtPoint: vi.fn(),
|
|
29
|
+
snapRotate90: vi.fn(),
|
|
30
|
+
toggleFlip: vi.fn(),
|
|
30
31
|
};
|
|
31
32
|
}
|
|
32
33
|
|
|
@@ -34,7 +35,7 @@ function createWheelEvent(
|
|
|
34
35
|
overrides: Partial< WheelEvent > & { currentTarget?: unknown } = {}
|
|
35
36
|
): WheelEvent {
|
|
36
37
|
return {
|
|
37
|
-
preventDefault:
|
|
38
|
+
preventDefault: vi.fn(),
|
|
38
39
|
deltaY: -100,
|
|
39
40
|
clientX: 0,
|
|
40
41
|
clientY: 0,
|
|
@@ -48,7 +49,7 @@ function createKeyboardEvent( key: string ): React.KeyboardEvent {
|
|
|
48
49
|
key,
|
|
49
50
|
nativeEvent: {
|
|
50
51
|
key,
|
|
51
|
-
preventDefault:
|
|
52
|
+
preventDefault: vi.fn(),
|
|
52
53
|
},
|
|
53
54
|
} as unknown as React.KeyboardEvent;
|
|
54
55
|
}
|
|
@@ -57,7 +58,7 @@ function createTouchEvent(
|
|
|
57
58
|
touches: Array< { clientX: number; clientY: number } >
|
|
58
59
|
): TouchEvent {
|
|
59
60
|
return {
|
|
60
|
-
preventDefault:
|
|
61
|
+
preventDefault: vi.fn(),
|
|
61
62
|
touches,
|
|
62
63
|
} as unknown as TouchEvent;
|
|
63
64
|
}
|
|
@@ -67,13 +68,13 @@ function createTouchDocument(): Document & {
|
|
|
67
68
|
} {
|
|
68
69
|
const listeners: Record< string, EventListener[] > = {};
|
|
69
70
|
return {
|
|
70
|
-
addEventListener:
|
|
71
|
+
addEventListener: vi.fn( ( type: string, fn: EventListener ) => {
|
|
71
72
|
if ( ! listeners[ type ] ) {
|
|
72
73
|
listeners[ type ] = [];
|
|
73
74
|
}
|
|
74
75
|
listeners[ type ].push( fn );
|
|
75
76
|
} ),
|
|
76
|
-
removeEventListener:
|
|
77
|
+
removeEventListener: vi.fn( ( type: string, fn: EventListener ) => {
|
|
77
78
|
if ( listeners[ type ] ) {
|
|
78
79
|
listeners[ type ] = listeners[ type ].filter(
|
|
79
80
|
( listener ) => listener !== fn
|
|
@@ -104,11 +105,11 @@ function createTouchTarget( ownerDocument: Document ) {
|
|
|
104
105
|
|
|
105
106
|
describe( 'useInteraction grid placement signal', () => {
|
|
106
107
|
afterEach( () => {
|
|
107
|
-
|
|
108
|
+
vi.useRealTimers();
|
|
108
109
|
} );
|
|
109
110
|
|
|
110
111
|
it( 'sets isPlacementActive during keyboard pan, then clears it after idle', () => {
|
|
111
|
-
|
|
112
|
+
vi.useFakeTimers();
|
|
112
113
|
const { result } = renderHook( () =>
|
|
113
114
|
useInteraction(
|
|
114
115
|
makeState(),
|
|
@@ -127,7 +128,7 @@ describe( 'useInteraction grid placement signal', () => {
|
|
|
127
128
|
expect( result.current.isPlacementActive ).toBe( true );
|
|
128
129
|
|
|
129
130
|
act( () => {
|
|
130
|
-
|
|
131
|
+
vi.advanceTimersByTime( 300 );
|
|
131
132
|
} );
|
|
132
133
|
|
|
133
134
|
expect( result.current.isPlacementActive ).toBe( false );
|
|
@@ -154,7 +155,7 @@ describe( 'useInteraction grid placement signal', () => {
|
|
|
154
155
|
);
|
|
155
156
|
|
|
156
157
|
it( 'sets isPlacementActive during wheel zoom, then clears it after the wheel debounce', () => {
|
|
157
|
-
|
|
158
|
+
vi.useFakeTimers();
|
|
158
159
|
const { result } = renderHook( () =>
|
|
159
160
|
useInteraction(
|
|
160
161
|
makeState(),
|
|
@@ -173,7 +174,7 @@ describe( 'useInteraction grid placement signal', () => {
|
|
|
173
174
|
expect( result.current.isPlacementActive ).toBe( true );
|
|
174
175
|
|
|
175
176
|
act( () => {
|
|
176
|
-
|
|
177
|
+
vi.advanceTimersByTime( 300 );
|
|
177
178
|
} );
|
|
178
179
|
|
|
179
180
|
expect( result.current.isPlacementActive ).toBe( false );
|
|
@@ -46,6 +46,11 @@ export interface UseInteractionOptions {
|
|
|
46
46
|
onGestureStart?: () => void;
|
|
47
47
|
/** Fires when a continuous gesture ends (pointer release). */
|
|
48
48
|
onGestureEnd?: () => void;
|
|
49
|
+
/**
|
|
50
|
+
* Ignore all interaction. Any gesture already in flight is torn down,
|
|
51
|
+
* so a drag started before this flipped cannot keep moving the image.
|
|
52
|
+
*/
|
|
53
|
+
disabled?: boolean;
|
|
49
54
|
}
|
|
50
55
|
|
|
51
56
|
/** How long keyboard placement stays active after the latest handled key. */
|
|
@@ -200,12 +205,48 @@ export function useInteraction(
|
|
|
200
205
|
};
|
|
201
206
|
}, [ startPlacementGesture, stopPlacementGesture ] );
|
|
202
207
|
|
|
208
|
+
// Cancel any gesture already in flight when interaction is switched off,
|
|
209
|
+
// so a drag that started before the flip cannot keep moving the image.
|
|
210
|
+
// `destroy()` releases the pointer/touch listeners and the pending rAF;
|
|
211
|
+
// the controller keeps working for later gestures once re-enabled.
|
|
212
|
+
//
|
|
213
|
+
// It does not signal the end of the gesture it just cut short, so that
|
|
214
|
+
// is done here: a caller told a gesture began is otherwise left waiting
|
|
215
|
+
// for an end that never comes, which keeps undo/redo disabled and leaves
|
|
216
|
+
// the cropper's history batch open after a failed save.
|
|
217
|
+
const isDisabled = options?.disabled ?? false;
|
|
218
|
+
useEffect( () => {
|
|
219
|
+
if ( ! isDisabled ) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
controllerRef.current?.destroy();
|
|
223
|
+
setIsDragging( false );
|
|
224
|
+
setIsZooming( false );
|
|
225
|
+
setIsKeyboardPanning( false );
|
|
226
|
+
const wasGestureOpen =
|
|
227
|
+
isGestureActive || isKeyboardGestureActiveRef.current;
|
|
228
|
+
if ( isKeyboardGestureActiveRef.current ) {
|
|
229
|
+
isKeyboardGestureActiveRef.current = false;
|
|
230
|
+
clearTimeout( keyboardInteractionTimerRef.current );
|
|
231
|
+
}
|
|
232
|
+
stopPlacementGesture();
|
|
233
|
+
if ( wasGestureOpen ) {
|
|
234
|
+
optionsRef.current?.onGestureEnd?.();
|
|
235
|
+
}
|
|
236
|
+
}, [ isDisabled, isGestureActive, stopPlacementGesture ] );
|
|
237
|
+
|
|
203
238
|
const onPointerDown = useCallback( ( e: React.PointerEvent ) => {
|
|
239
|
+
if ( optionsRef.current?.disabled ) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
204
242
|
const el = e.currentTarget as HTMLElement;
|
|
205
243
|
controllerRef.current?.handlePointerDown( e.nativeEvent, el );
|
|
206
244
|
}, [] );
|
|
207
245
|
|
|
208
246
|
const onTouchStart = useCallback( ( e: React.TouchEvent ) => {
|
|
247
|
+
if ( optionsRef.current?.disabled ) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
209
250
|
const el = e.currentTarget as HTMLElement;
|
|
210
251
|
const rect = el.getBoundingClientRect();
|
|
211
252
|
controllerRef.current?.handleTouchStart(
|
|
@@ -217,6 +258,9 @@ export function useInteraction(
|
|
|
217
258
|
|
|
218
259
|
const onKeyDown = useCallback(
|
|
219
260
|
( e: React.KeyboardEvent ) => {
|
|
261
|
+
if ( optionsRef.current?.disabled ) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
220
264
|
if ( isHandledKeyboardPan( e.nativeEvent ) ) {
|
|
221
265
|
setIsKeyboardPanning( true );
|
|
222
266
|
signalKeyboardGesture();
|
|
@@ -229,6 +273,9 @@ export function useInteraction(
|
|
|
229
273
|
);
|
|
230
274
|
|
|
231
275
|
const onWheelNative = useCallback( ( e: WheelEvent ) => {
|
|
276
|
+
if ( optionsRef.current?.disabled ) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
232
279
|
controllerRef.current?.handleWheel( e );
|
|
233
280
|
}, [] );
|
|
234
281
|
|