@wordpress/media-editor 0.18.0 → 0.19.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.
Files changed (37) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +7 -7
  3. package/build/components/media-editor/index.cjs.map +2 -2
  4. package/build/components/media-editor/use-save-media-editor.cjs.map +2 -2
  5. package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
  6. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  7. package/build/image-editor/core/setter-helpers.cjs.map +2 -2
  8. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +2 -2
  9. package/build-module/components/media-editor/index.mjs.map +2 -2
  10. package/build-module/components/media-editor/use-save-media-editor.mjs.map +2 -2
  11. package/build-module/components/media-editor-crop-panel/index.mjs +2 -2
  12. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  13. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  14. package/build-module/image-editor/core/setter-helpers.mjs.map +2 -2
  15. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +2 -2
  16. package/build-style/style-rtl.css +21 -22
  17. package/build-style/style.css +6 -0
  18. package/global.d.ts +0 -1
  19. package/package.json +23 -24
  20. package/src/components/media-editor/index.tsx +2 -2
  21. package/src/components/media-editor/use-save-media-editor.ts +5 -5
  22. package/src/components/media-editor-crop-panel/index.tsx +2 -2
  23. package/src/components/media-editor-modal/test/index.jsdom.test.tsx +7 -7
  24. package/src/components/rotation-ruler/stories/index.story.tsx +2 -2
  25. package/src/components/rotation-ruler/style.scss +9 -0
  26. package/src/image-editor/core/interaction-controller.ts +2 -2
  27. package/src/image-editor/core/setter-helpers.ts +1 -1
  28. package/src/image-editor/react/components/cropper.scss +6 -0
  29. package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +1 -1
  30. package/src/image-editor/react/components/stencils/test/{rectangle-stencil.jsdom.test.tsx → rectangle-stencil.browser.test.tsx} +80 -98
  31. package/src/image-editor/react/components/test/{cropper.jsdom.test.tsx → cropper.browser.test.tsx} +230 -221
  32. package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +1 -1
  33. package/src/image-editor/stories/rectangle-crop.story.tsx +24 -23
  34. package/src/image-editor/stories/{style.css → style.module.css} +8 -37
  35. package/tsconfig.build.json +1 -1
  36. package/tsconfig.build.tsbuildinfo +1 -1
  37. package/tsconfig.json +1 -2
@@ -1,5 +1,7 @@
1
- import { beforeAll, describe, expect, it, vi } from 'vitest';
2
- import { render, screen, fireEvent, act } from '@testing-library/react';
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { userEvent } from 'vitest/browser';
3
+ import { screen, fireEvent, act } from '@testing-library/react';
4
+ import { render } from 'vitest-browser-react';
3
5
  import { RectangleStencil } from '../rectangle-stencil';
4
6
  import type {
5
7
  HandlePosition,
@@ -18,15 +20,13 @@ const CONTAINER_SIZE: Size = { width: 600, height: 400 };
18
20
  const IMAGE_SIZE: Size = { width: 500, height: 300 };
19
21
  const CROP_BOUNDS = { minX: 0, minY: 0, maxX: 1, maxY: 1 };
20
22
 
21
- globalThis.wpVitest.mockPointerEvent();
22
-
23
23
  /**
24
24
  * Render a RectangleStencil in freeform mode with sensible defaults.
25
25
  * Returns the mock callbacks; use `screen` for DOM queries.
26
26
  *
27
27
  * @param overrides Props to override on the rendered stencil.
28
28
  */
29
- function renderStencil(
29
+ async function renderStencil(
30
30
  overrides: Partial< React.ComponentProps< typeof RectangleStencil > > = {}
31
31
  ) {
32
32
  const onCropChange = vi.fn();
@@ -46,7 +46,7 @@ function renderStencil(
46
46
  ...overrides,
47
47
  };
48
48
 
49
- const utils = render( <RectangleStencil { ...props } /> );
49
+ const utils = await render( <RectangleStencil { ...props } /> );
50
50
 
51
51
  return {
52
52
  ...utils,
@@ -59,19 +59,9 @@ function renderStencil(
59
59
  }
60
60
 
61
61
  describe( 'RectangleStencil', () => {
62
- // jsdom does not implement pointer capture, so stub it for handle drag tests.
63
- beforeAll( () => {
64
- if ( ! HTMLElement.prototype.setPointerCapture ) {
65
- HTMLElement.prototype.setPointerCapture = vi.fn();
66
- }
67
- if ( ! HTMLElement.prototype.releasePointerCapture ) {
68
- HTMLElement.prototype.releasePointerCapture = vi.fn();
69
- }
70
- } );
71
-
72
62
  describe( 'tab order', () => {
73
- it( 'renders handles clockwise from top-left in freeform mode', () => {
74
- renderStencil();
63
+ it( 'renders handles clockwise from top-left in freeform mode', async () => {
64
+ await renderStencil();
75
65
  const labels = screen
76
66
  .getAllByRole( 'button' )
77
67
  .map( ( b ) => b.getAttribute( 'aria-label' ) );
@@ -88,8 +78,8 @@ describe( 'RectangleStencil', () => {
88
78
  ] );
89
79
  } );
90
80
 
91
- it( 'describes keyboard resizing on resize handles', () => {
92
- renderStencil();
81
+ it( 'describes keyboard resizing on resize handles', async () => {
82
+ await renderStencil();
93
83
  expect(
94
84
  screen.getByRole( 'button', {
95
85
  name: 'Resize from top-left corner',
@@ -99,8 +89,8 @@ describe( 'RectangleStencil', () => {
99
89
  );
100
90
  } );
101
91
 
102
- it( 'renders corner handles clockwise from top-left when aspect ratio is locked', () => {
103
- renderStencil( { aspectRatio: 16 / 9 } );
92
+ it( 'renders corner handles clockwise from top-left when aspect ratio is locked', async () => {
93
+ await renderStencil( { aspectRatio: 16 / 9 } );
104
94
  const labels = screen
105
95
  .getAllByRole( 'button' )
106
96
  .map( ( b ) => b.getAttribute( 'aria-label' ) );
@@ -115,10 +105,10 @@ describe( 'RectangleStencil', () => {
115
105
  } );
116
106
 
117
107
  describe( 'keyboard — Escape', () => {
118
- it( 'handles Escape on a handle without bubbling', () => {
108
+ it( 'handles Escape on a handle without bubbling', async () => {
119
109
  const onKeyDown = vi.fn();
120
110
  const onEscape = vi.fn();
121
- render(
111
+ await render(
122
112
  // eslint-disable-next-line jsx-a11y/no-static-element-interactions
123
113
  <div onKeyDown={ onKeyDown }>
124
114
  <RectangleStencil
@@ -134,17 +124,19 @@ describe( 'RectangleStencil', () => {
134
124
  );
135
125
  const [ firstHandle ] = screen.getAllByRole( 'button' );
136
126
 
137
- fireEvent.keyDown( firstHandle, { key: 'Escape' } );
127
+ firstHandle.focus();
128
+ await userEvent.keyboard( '{Escape}' );
138
129
 
139
130
  expect( onEscape ).toHaveBeenCalledTimes( 1 );
140
131
  expect( onKeyDown ).not.toHaveBeenCalled();
141
132
  } );
142
133
 
143
- it( 'does not call onCropChange when Escape is pressed', () => {
144
- const { onCropChange, onEscape } = renderStencil();
134
+ it( 'does not call onCropChange when Escape is pressed', async () => {
135
+ const { onCropChange, onEscape } = await renderStencil();
145
136
  const [ firstHandle ] = screen.getAllByRole( 'button' );
146
137
 
147
- fireEvent.keyDown( firstHandle, { key: 'Escape' } );
138
+ firstHandle.focus();
139
+ await userEvent.keyboard( '{Escape}' );
148
140
 
149
141
  expect( onEscape ).toHaveBeenCalledTimes( 1 );
150
142
  expect( onCropChange ).not.toHaveBeenCalled();
@@ -152,9 +144,9 @@ describe( 'RectangleStencil', () => {
152
144
  } );
153
145
 
154
146
  describe( 'keyboard — arrow keys (fine step)', () => {
155
- it( 'calls onResizeStart once and onResizeEnd after keyboard resize settles', () => {
147
+ it( 'calls onResizeStart once and onResizeEnd after keyboard resize settles', async () => {
156
148
  vi.useFakeTimers();
157
- const { onResizeStart, onResizeEnd } = renderStencil();
149
+ const { onResizeStart, onResizeEnd } = await renderStencil();
158
150
  const eHandle = screen.getAllByRole( 'button' )[ 3 ];
159
151
 
160
152
  fireEvent.keyDown( eHandle, { key: 'ArrowRight' } );
@@ -171,17 +163,19 @@ describe( 'RectangleStencil', () => {
171
163
  vi.useRealTimers();
172
164
  } );
173
165
 
174
- it( 'closes a pending keyboard resize when resizing becomes disabled', () => {
166
+ it( 'closes a pending keyboard resize when resizing becomes disabled', async () => {
175
167
  vi.useFakeTimers();
176
168
  const { props, rerender, onResizeStart, onResizeEnd } =
177
- renderStencil();
169
+ await renderStencil();
178
170
  const eHandle = screen.getAllByRole( 'button' )[ 3 ];
179
171
 
180
172
  fireEvent.keyDown( eHandle, { key: 'ArrowRight' } );
181
173
  expect( onResizeStart ).toHaveBeenCalledTimes( 1 );
182
174
  expect( onResizeEnd ).not.toHaveBeenCalled();
183
175
 
184
- rerender( <RectangleStencil { ...props } isResizeDisabled /> );
176
+ await rerender(
177
+ <RectangleStencil { ...props } isResizeDisabled />
178
+ );
185
179
 
186
180
  // Closed straight away, not left to the settle timer.
187
181
  expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
@@ -194,15 +188,13 @@ describe( 'RectangleStencil', () => {
194
188
  vi.useRealTimers();
195
189
  } );
196
190
 
197
- it( 'moves the right edge right by KEYBOARD_STEP on ArrowRight (no Shift)', () => {
198
- const { onCropChange } = renderStencil();
191
+ it( 'moves the right edge right by KEYBOARD_STEP on ArrowRight (no Shift)', async () => {
192
+ const { onCropChange } = await renderStencil();
199
193
  // 'e' handle is the 4th button in clockwise order (nw, n, ne, e).
200
194
  const eHandle = screen.getAllByRole( 'button' )[ 3 ];
201
195
 
202
- fireEvent.keyDown( eHandle, {
203
- key: 'ArrowRight',
204
- shiftKey: false,
205
- } );
196
+ eHandle.focus();
197
+ await userEvent.keyboard( '{ArrowRight}' );
206
198
 
207
199
  expect( onCropChange ).toHaveBeenCalledTimes( 1 );
208
200
  const newRect: NormalizedRect = onCropChange.mock.calls[ 0 ][ 0 ];
@@ -211,15 +203,13 @@ describe( 'RectangleStencil', () => {
211
203
  expect( newRect.x ).toBeCloseTo( 0.1, 5 );
212
204
  } );
213
205
 
214
- it( 'moves the bottom edge down by KEYBOARD_STEP on ArrowDown (no Shift)', () => {
215
- const { onCropChange } = renderStencil();
206
+ it( 'moves the bottom edge down by KEYBOARD_STEP on ArrowDown (no Shift)', async () => {
207
+ const { onCropChange } = await renderStencil();
216
208
  // 's' handle is the 6th button (nw, n, ne, e, se, s).
217
209
  const sHandle = screen.getAllByRole( 'button' )[ 5 ];
218
210
 
219
- fireEvent.keyDown( sHandle, {
220
- key: 'ArrowDown',
221
- shiftKey: false,
222
- } );
211
+ sHandle.focus();
212
+ await userEvent.keyboard( '{ArrowDown}' );
223
213
 
224
214
  expect( onCropChange ).toHaveBeenCalledTimes( 1 );
225
215
  const newRect: NormalizedRect = onCropChange.mock.calls[ 0 ][ 0 ];
@@ -228,7 +218,7 @@ describe( 'RectangleStencil', () => {
228
218
  expect( newRect.y ).toBeCloseTo( 0.1, 5 );
229
219
  } );
230
220
 
231
- it( 'applies snapCropRect to freeform resize output', () => {
221
+ it( 'applies snapCropRect to freeform resize output', async () => {
232
222
  const snapCropRect = vi.fn<
233
223
  (
234
224
  rect: NormalizedRect,
@@ -238,13 +228,11 @@ describe( 'RectangleStencil', () => {
238
228
  ...rect,
239
229
  width: 0.82,
240
230
  } ) );
241
- const { onCropChange } = renderStencil( { snapCropRect } );
231
+ const { onCropChange } = await renderStencil( { snapCropRect } );
242
232
  const eHandle = screen.getAllByRole( 'button' )[ 3 ];
243
233
 
244
- fireEvent.keyDown( eHandle, {
245
- key: 'ArrowRight',
246
- shiftKey: false,
247
- } );
234
+ eHandle.focus();
235
+ await userEvent.keyboard( '{ArrowRight}' );
248
236
 
249
237
  expect( snapCropRect ).toHaveBeenCalledTimes( 1 );
250
238
  expect( snapCropRect.mock.calls[ 0 ][ 0 ].width ).toBeCloseTo(
@@ -258,8 +246,8 @@ describe( 'RectangleStencil', () => {
258
246
  );
259
247
  } );
260
248
 
261
- it( 'shrinks a locked-ratio crop from a corner handle', () => {
262
- const { onCropChange } = renderStencil( {
249
+ it( 'shrinks a locked-ratio crop from a corner handle', async () => {
250
+ const { onCropChange } = await renderStencil( {
263
251
  aspectRatio: 1,
264
252
  containerSize: { width: 500, height: 500 },
265
253
  imageSize: { width: 500, height: 500 },
@@ -268,10 +256,8 @@ describe( 'RectangleStencil', () => {
268
256
  name: 'Resize from top-left corner',
269
257
  } );
270
258
 
271
- fireEvent.keyDown( nwHandle, {
272
- key: 'ArrowRight',
273
- shiftKey: false,
274
- } );
259
+ nwHandle.focus();
260
+ await userEvent.keyboard( '{ArrowRight}' );
275
261
 
276
262
  expect( onCropChange ).toHaveBeenCalledTimes( 1 );
277
263
  const newRect: NormalizedRect = onCropChange.mock.calls[ 0 ][ 0 ];
@@ -295,11 +281,12 @@ describe( 'RectangleStencil', () => {
295
281
  } );
296
282
 
297
283
  describe( 'keyboard — arrow keys (coarse step with Shift)', () => {
298
- it( 'moves the right edge right by KEYBOARD_STEP_SHIFT on Shift+ArrowRight', () => {
299
- const { onCropChange } = renderStencil();
284
+ it( 'moves the right edge right by KEYBOARD_STEP_SHIFT on Shift+ArrowRight', async () => {
285
+ const { onCropChange } = await renderStencil();
300
286
  const eHandle = screen.getAllByRole( 'button' )[ 3 ];
301
287
 
302
- fireEvent.keyDown( eHandle, { key: 'ArrowRight', shiftKey: true } );
288
+ eHandle.focus();
289
+ await userEvent.keyboard( '{Shift>}{ArrowRight}{/Shift}' );
303
290
 
304
291
  expect( onCropChange ).toHaveBeenCalledTimes( 1 );
305
292
  const newRect: NormalizedRect = onCropChange.mock.calls[ 0 ][ 0 ];
@@ -307,20 +294,18 @@ describe( 'RectangleStencil', () => {
307
294
  expect( newRect.width ).toBeCloseTo( 0.9, 5 );
308
295
  } );
309
296
 
310
- it( 'step is 10x larger with Shift than without', () => {
311
- const { onCropChange } = renderStencil();
297
+ it( 'step is 10x larger with Shift than without', async () => {
298
+ const { onCropChange } = await renderStencil();
312
299
  const eHandle = screen.getAllByRole( 'button' )[ 3 ];
313
300
 
314
- fireEvent.keyDown( eHandle, {
315
- key: 'ArrowRight',
316
- shiftKey: false,
317
- } );
301
+ eHandle.focus();
302
+ await userEvent.keyboard( '{ArrowRight}' );
318
303
  const fineRect: NormalizedRect = onCropChange.mock.calls[ 0 ][ 0 ];
319
304
  const fineDelta = fineRect.width - DEFAULT_CROP_RECT.width;
320
305
 
321
306
  onCropChange.mockClear();
322
307
 
323
- fireEvent.keyDown( eHandle, { key: 'ArrowRight', shiftKey: true } );
308
+ await userEvent.keyboard( '{Shift>}{ArrowRight}{/Shift}' );
324
309
  const coarseRect: NormalizedRect =
325
310
  onCropChange.mock.calls[ 0 ][ 0 ];
326
311
  const coarseDelta = coarseRect.width - DEFAULT_CROP_RECT.width;
@@ -328,25 +313,23 @@ describe( 'RectangleStencil', () => {
328
313
  expect( coarseDelta / fineDelta ).toBeCloseTo( 10, 0 );
329
314
  } );
330
315
 
331
- it( 'does not apply snapCropRect while resizing a locked aspect ratio', () => {
316
+ it( 'does not apply snapCropRect while resizing a locked aspect ratio', async () => {
332
317
  const snapCropRect = vi.fn( ( rect: NormalizedRect ) => rect );
333
- renderStencil( { aspectRatio: 1, snapCropRect } );
318
+ await renderStencil( { aspectRatio: 1, snapCropRect } );
334
319
  const nwHandle = screen.getByRole( 'button', {
335
320
  name: 'Resize from top-left corner',
336
321
  } );
337
322
 
338
- fireEvent.keyDown( nwHandle, {
339
- key: 'ArrowRight',
340
- shiftKey: false,
341
- } );
323
+ nwHandle.focus();
324
+ await userEvent.keyboard( '{ArrowRight}' );
342
325
 
343
326
  expect( snapCropRect ).not.toHaveBeenCalled();
344
327
  } );
345
328
  } );
346
329
 
347
330
  describe( 'pointer drag — focus after release', () => {
348
- it( 'calls onResizeStart on pointerdown and onResizeEnd on pointerup', () => {
349
- const { onResizeStart, onResizeEnd } = renderStencil();
331
+ it( 'calls onResizeStart on pointerdown and onResizeEnd on pointerup', async () => {
332
+ const { onResizeStart, onResizeEnd } = await renderStencil();
350
333
  const [ firstHandle ] = screen.getAllByRole( 'button' );
351
334
 
352
335
  fireEvent.pointerDown( firstHandle, {
@@ -364,27 +347,20 @@ describe( 'RectangleStencil', () => {
364
347
  expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
365
348
  } );
366
349
 
367
- it( 'focuses the handle button after a pointer drag ends', () => {
368
- renderStencil();
350
+ it( 'focuses the handle button after a pointer drag ends', async () => {
351
+ await renderStencil();
369
352
  const [ firstHandle ] = screen.getAllByRole( 'button' );
370
353
 
371
354
  vi.spyOn( firstHandle, 'focus' );
372
355
 
373
- fireEvent.pointerDown( firstHandle, {
374
- button: 0,
375
- clientX: 100,
376
- clientY: 100,
377
- pointerId: 1,
378
- } );
379
- fireEvent.pointerUp( firstHandle, {
380
- pointerId: 1,
381
- } );
356
+ await userEvent.dragAndDrop( firstHandle, firstHandle );
382
357
 
383
358
  expect( firstHandle.focus ).toHaveBeenCalled();
359
+ expect( firstHandle ).toHaveFocus();
384
360
  } );
385
361
 
386
- it( 'does not start a pointer resize while resizing is disabled', () => {
387
- const { onResizeStart } = renderStencil( {
362
+ it( 'does not start a pointer resize while resizing is disabled', async () => {
363
+ const { onResizeStart } = await renderStencil( {
388
364
  isResizeDisabled: true,
389
365
  } );
390
366
  const [ firstHandle ] = screen.getAllByRole( 'button' );
@@ -399,9 +375,9 @@ describe( 'RectangleStencil', () => {
399
375
  expect( onResizeStart ).not.toHaveBeenCalled();
400
376
  } );
401
377
 
402
- it( 'cancels an active pointer resize when resizing becomes disabled', () => {
378
+ it( 'cancels an active pointer resize when resizing becomes disabled', async () => {
403
379
  const { props, rerender, onResizeStart, onResizeEnd } =
404
- renderStencil();
380
+ await renderStencil();
405
381
  const [ firstHandle ] = screen.getAllByRole( 'button' );
406
382
 
407
383
  fireEvent.pointerDown( firstHandle, {
@@ -414,7 +390,9 @@ describe( 'RectangleStencil', () => {
414
390
  expect( onResizeStart ).toHaveBeenCalledTimes( 1 );
415
391
  expect( onResizeEnd ).not.toHaveBeenCalled();
416
392
 
417
- rerender( <RectangleStencil { ...props } isResizeDisabled /> );
393
+ await rerender(
394
+ <RectangleStencil { ...props } isResizeDisabled />
395
+ );
418
396
 
419
397
  expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
420
398
 
@@ -423,9 +401,9 @@ describe( 'RectangleStencil', () => {
423
401
  expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
424
402
  } );
425
403
 
426
- it( 'only stops touchstart propagation for single-touch handle gestures', () => {
404
+ it( 'only stops touchstart propagation for single-touch handle gestures', async () => {
427
405
  const onTouchStart = vi.fn();
428
- render(
406
+ await render(
429
407
  <div onTouchStart={ onTouchStart }>
430
408
  <RectangleStencil
431
409
  cropRect={ DEFAULT_CROP_RECT }
@@ -438,17 +416,21 @@ describe( 'RectangleStencil', () => {
438
416
  </div>
439
417
  );
440
418
  const [ firstHandle ] = screen.getAllByRole( 'button' );
419
+ const createTouch = ( identifier: number, clientX: number ) =>
420
+ new Touch( {
421
+ identifier,
422
+ target: firstHandle,
423
+ clientX,
424
+ clientY: 100,
425
+ } );
441
426
 
442
427
  fireEvent.touchStart( firstHandle, {
443
- touches: [ { clientX: 100, clientY: 100 } ],
428
+ touches: [ createTouch( 1, 100 ) ],
444
429
  } );
445
430
  expect( onTouchStart ).not.toHaveBeenCalled();
446
431
 
447
432
  fireEvent.touchStart( firstHandle, {
448
- touches: [
449
- { clientX: 100, clientY: 100 },
450
- { clientX: 160, clientY: 100 },
451
- ],
433
+ touches: [ createTouch( 1, 100 ), createTouch( 2, 160 ) ],
452
434
  } );
453
435
  expect( onTouchStart ).toHaveBeenCalledTimes( 1 );
454
436
  } );