@wordpress/media-editor 0.20.0 → 0.21.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 (116) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +2 -1
  3. package/build/components/media-editor/index.cjs +7 -16
  4. package/build/components/media-editor/index.cjs.map +3 -3
  5. package/build/components/media-editor/use-crop-options.cjs +1 -2
  6. package/build/components/media-editor/use-crop-options.cjs.map +2 -2
  7. package/build/components/media-editor-canvas/index.cjs.map +3 -3
  8. package/build/components/media-editor-crop-panel/index.cjs +12 -8
  9. package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
  10. package/build/components/media-editor-modal/index.cjs +2 -0
  11. package/build/components/media-editor-modal/index.cjs.map +2 -2
  12. package/build/components/media-editor-provider/index.cjs +2 -1
  13. package/build/components/media-editor-provider/index.cjs.map +2 -2
  14. package/build/components/media-form/index.cjs +28 -28
  15. package/build/components/media-form/index.cjs.map +3 -3
  16. package/build/components/media-preview/index.cjs.map +3 -3
  17. package/build/image-editor/react/components/cropper.cjs +1 -11
  18. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  19. package/build/state/composite-reducer.cjs +33 -8
  20. package/build/state/composite-reducer.cjs.map +3 -3
  21. package/build/state/index.cjs.map +1 -1
  22. package/build/state/types.cjs.map +1 -1
  23. package/build/state/use-media-editor-state.cjs +20 -7
  24. package/build/state/use-media-editor-state.cjs.map +2 -2
  25. package/build-module/components/media-editor/index.mjs +9 -18
  26. package/build-module/components/media-editor/index.mjs.map +2 -2
  27. package/build-module/components/media-editor/use-crop-options.mjs +1 -2
  28. package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
  29. package/build-module/components/media-editor-canvas/index.mjs +2 -2
  30. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  31. package/build-module/components/media-editor-crop-panel/index.mjs +13 -9
  32. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  33. package/build-module/components/media-editor-modal/index.mjs +2 -0
  34. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  35. package/build-module/components/media-editor-provider/index.mjs +2 -1
  36. package/build-module/components/media-editor-provider/index.mjs.map +2 -2
  37. package/build-module/components/media-form/index.mjs +33 -30
  38. package/build-module/components/media-form/index.mjs.map +2 -2
  39. package/build-module/components/media-preview/index.mjs +2 -2
  40. package/build-module/components/media-preview/index.mjs.map +2 -2
  41. package/build-module/image-editor/react/components/cropper.mjs +1 -11
  42. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  43. package/build-module/state/composite-reducer.mjs +23 -8
  44. package/build-module/state/composite-reducer.mjs.map +2 -2
  45. package/build-module/state/index.mjs.map +1 -1
  46. package/build-module/state/types.mjs.map +1 -1
  47. package/build-module/state/use-media-editor-state.mjs +20 -7
  48. package/build-module/state/use-media-editor-state.mjs.map +2 -2
  49. package/build-types/components/media-editor/index.d.ts +2 -1
  50. package/build-types/components/media-editor/index.d.ts.map +1 -1
  51. package/build-types/components/media-editor/use-crop-options.d.ts +0 -1
  52. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
  53. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  54. package/build-types/components/media-editor-modal/index.d.ts +7 -2
  55. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  56. package/build-types/components/media-editor-provider/index.d.ts +7 -1
  57. package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
  58. package/build-types/components/media-form/index.d.ts +5 -2
  59. package/build-types/components/media-form/index.d.ts.map +1 -1
  60. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  61. package/build-types/state/composite-reducer.d.ts +1 -1
  62. package/build-types/state/composite-reducer.d.ts.map +1 -1
  63. package/build-types/state/index.d.ts +1 -1
  64. package/build-types/state/index.d.ts.map +1 -1
  65. package/build-types/state/types.d.ts +34 -5
  66. package/build-types/state/types.d.ts.map +1 -1
  67. package/build-types/state/use-media-editor-state.d.ts +16 -5
  68. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  69. package/package.json +33 -22
  70. package/src/components/media-editor/index.tsx +12 -22
  71. package/src/components/media-editor/use-crop-options.ts +0 -2
  72. package/src/components/media-editor-canvas/index.tsx +2 -2
  73. package/src/components/media-editor-crop-panel/index.tsx +17 -9
  74. package/src/components/media-editor-image-controls/style.scss +1 -1
  75. package/src/components/media-editor-modal/index.tsx +8 -1
  76. package/src/components/media-editor-provider/index.tsx +8 -1
  77. package/src/components/media-form/index.tsx +48 -42
  78. package/src/components/media-preview/index.tsx +2 -2
  79. package/src/image-editor/docs/architecture.md +4 -0
  80. package/src/image-editor/react/components/cropper.tsx +1 -15
  81. package/src/state/composite-reducer.ts +24 -11
  82. package/src/state/index.ts +2 -0
  83. package/src/state/types.ts +37 -5
  84. package/src/state/use-media-editor-state.ts +46 -11
  85. package/global.d.ts +0 -5
  86. package/src/components/media-editor/test/restore-original.browser.test.tsx +0 -227
  87. package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -106
  88. package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -72
  89. package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -186
  90. package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -359
  91. package/src/components/media-editor-modal/test/index.jsdom.test.tsx +0 -182
  92. package/src/components/rotation-ruler/stories/index.story.tsx +0 -43
  93. package/src/components/rotation-ruler/test/index.jsdom.test.tsx +0 -140
  94. package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +0 -823
  95. package/src/image-editor/core/math/test/sanitize.ts +0 -242
  96. package/src/image-editor/core/test/camera.ts +0 -1218
  97. package/src/image-editor/core/test/interaction-controller.ts +0 -1299
  98. package/src/image-editor/core/test/preview-export-parity.ts +0 -249
  99. package/src/image-editor/core/test/render-camera-bridge.ts +0 -349
  100. package/src/image-editor/core/test/state.ts +0 -876
  101. package/src/image-editor/core/test/stencil-math.ts +0 -665
  102. package/src/image-editor/core/transforms/test/pipeline.ts +0 -267
  103. package/src/image-editor/react/components/stencils/test/rectangle-stencil.browser.test.tsx +0 -438
  104. package/src/image-editor/react/components/test/cropper.browser.test.tsx +0 -1462
  105. package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +0 -491
  106. package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +0 -1049
  107. package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +0 -213
  108. package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +0 -140
  109. package/src/image-editor/stories/rectangle-crop.story.tsx +0 -1073
  110. package/src/image-editor/stories/style.module.css +0 -65
  111. package/src/state/test/use-media-editor-state.browser.test.tsx +0 -79
  112. package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -386
  113. package/src/store/test/reducer.ts +0 -64
  114. package/tsconfig.build.json +0 -26
  115. package/tsconfig.build.tsbuildinfo +0 -1
  116. package/tsconfig.json +0 -37
@@ -1,1049 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { renderHook, act } from '@testing-library/react';
3
- import { useCropperReducer } from '../use-cropper-reducer';
4
- import { DEFAULT_STATE } from '../../../core/constants';
5
-
6
- describe( 'useCropperReducer', () => {
7
- it( 'should initialize with DEFAULT_STATE', () => {
8
- const { result } = renderHook( () => useCropperReducer() );
9
-
10
- expect( result.current.state ).toEqual( DEFAULT_STATE );
11
- } );
12
-
13
- it( 'should initialize with merged partial state', () => {
14
- const { result } = renderHook( () =>
15
- useCropperReducer( { zoom: 2, rotation: 45 } )
16
- );
17
-
18
- expect( result.current.state.zoom ).toBe( 2 );
19
- expect( result.current.state.rotation ).toBe( 45 );
20
- // Non-overridden fields should match defaults.
21
- expect( result.current.state.pan ).toEqual( DEFAULT_STATE.pan );
22
- } );
23
-
24
- it( 'should dispatch SET_CROP via setCrop', () => {
25
- const { result } = renderHook( () => useCropperReducer() );
26
-
27
- // At zoom=1, crop is clamped to (0,0) — no panning possible.
28
- // Zoom in first to allow panning.
29
- act( () => {
30
- result.current.setZoom( 3 );
31
- } );
32
-
33
- act( () => {
34
- result.current.setPan( { x: 0.3, y: 0.2 } );
35
- } );
36
-
37
- expect( result.current.state.pan.x ).toBeCloseTo( 0.3 );
38
- expect( result.current.state.pan.y ).toBeCloseTo( 0.2 );
39
- } );
40
-
41
- it( 'should dispatch SET_ZOOM via setZoom', () => {
42
- const { result } = renderHook( () => useCropperReducer() );
43
-
44
- act( () => {
45
- result.current.setZoom( 3 );
46
- } );
47
-
48
- expect( result.current.state.zoom ).toBe( 3 );
49
- } );
50
-
51
- it( 'leaves pan at zero on setZoom when the cropRect is centered', () => {
52
- // Sanity: the default cropRect spans the full image, so its
53
- // center coincides with the image center and the crop-center
54
- // focal-point correction degenerates to "no pan change."
55
- // Guards against regressions where setZoom accidentally pans.
56
- const { result } = renderHook( () => useCropperReducer() );
57
-
58
- act( () => {
59
- result.current.setZoom( 4 );
60
- } );
61
-
62
- expect( result.current.state.zoom ).toBe( 4 );
63
- expect( result.current.state.pan ).toEqual( { x: 0, y: 0 } );
64
-
65
- act( () => {
66
- result.current.setZoom( 2 );
67
- } );
68
-
69
- expect( result.current.state.zoom ).toBe( 2 );
70
- expect( result.current.state.pan ).toEqual( { x: 0, y: 0 } );
71
- } );
72
-
73
- it( 'anchors setZoom at the crop-rect center', () => {
74
- // With an off-center crop, setZoom must shift pan toward the
75
- // crop center as zoom decreases — otherwise `enforceContainment`
76
- // would translate the image toward the nearest viewport corner
77
- // (the original bug this method fixes).
78
- const { result } = renderHook( () => useCropperReducer() );
79
-
80
- act( () => {
81
- result.current.setZoom( 4 );
82
- } );
83
- // Place the crop in the bottom-right quadrant — center
84
- // at ( 0.7, 0.7 ), i.e. ( +0.2, +0.2 ) in pan coords.
85
- act( () => {
86
- result.current.setCropRect( {
87
- x: 0.6,
88
- y: 0.6,
89
- width: 0.2,
90
- height: 0.2,
91
- } );
92
- } );
93
- act( () => {
94
- result.current.setPan( { x: 0, y: 0 } );
95
- } );
96
-
97
- expect( result.current.state.pan ).toEqual( { x: 0, y: 0 } );
98
-
99
- // Zoom out — pan should move toward the focal point in +x, +y,
100
- // not stay at the origin (which would corner-snap on containment).
101
- act( () => {
102
- result.current.setZoom( 2 );
103
- } );
104
-
105
- expect( result.current.state.zoom ).toBe( 2 );
106
- expect( result.current.state.pan.x ).toBeGreaterThan( 0 );
107
- expect( result.current.state.pan.y ).toBeGreaterThan( 0 );
108
- } );
109
-
110
- it( 'should dispatch SET_ZOOM_AT_POINT via setZoomAtPoint', () => {
111
- const { result } = renderHook( () => useCropperReducer() );
112
-
113
- act( () => {
114
- result.current.setZoomAtPoint( 3, { x: 0.2, y: 0.1 } );
115
- } );
116
-
117
- expect( result.current.state.zoom ).toBe( 3 );
118
- expect( result.current.state.pan ).toEqual( { x: 0.2, y: 0.1 } );
119
- } );
120
-
121
- it( 'should clamp zoom to valid range via SET_ZOOM', () => {
122
- const { result } = renderHook( () => useCropperReducer() );
123
-
124
- act( () => {
125
- result.current.setZoom( 15 );
126
- } );
127
-
128
- expect( result.current.state.zoom ).toBe( 10 );
129
-
130
- act( () => {
131
- result.current.setZoom( 0.5 );
132
- } );
133
-
134
- expect( result.current.state.zoom ).toBe( 1 );
135
- } );
136
-
137
- it( 'allows setZoom below 1 when the coverage minimum permits', () => {
138
- // Thin, fine-rotated crop on a portrait image: the coverage
139
- // minimum drops below 1, so `setZoom` (slider / keyboard path)
140
- // must honour values below 1 rather than clamping at MIN_ZOOM.
141
- const { result } = renderHook( () =>
142
- useCropperReducer( {
143
- image: {
144
- src: 'portrait.jpg',
145
- naturalWidth: 900,
146
- naturalHeight: 1600,
147
- },
148
- rotation: 19,
149
- cropRect: { x: 0.48, y: 0, width: 0.04, height: 1 },
150
- } )
151
- );
152
-
153
- act( () => {
154
- result.current.setZoom( 0.9 );
155
- } );
156
-
157
- expect( result.current.state.zoom ).toBeLessThan( 1 );
158
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 0.9 );
159
- } );
160
-
161
- it( 'should dispatch SET_ROTATION via setRotation', () => {
162
- const { result } = renderHook( () => useCropperReducer() );
163
-
164
- act( () => {
165
- result.current.setRotation( 90 );
166
- } );
167
-
168
- expect( result.current.state.rotation ).toBe( 90 );
169
- } );
170
-
171
- it( 'should normalize rotation via SET_ROTATION', () => {
172
- const { result } = renderHook( () => useCropperReducer() );
173
-
174
- act( () => {
175
- result.current.setRotation( -90 );
176
- } );
177
-
178
- expect( result.current.state.rotation ).toBe( 270 );
179
-
180
- act( () => {
181
- result.current.setRotation( 450 );
182
- } );
183
-
184
- expect( result.current.state.rotation ).toBe( 90 );
185
- } );
186
-
187
- it( 'should dispatch SET_FLIP via setFlip', () => {
188
- const { result } = renderHook( () => useCropperReducer() );
189
-
190
- act( () => {
191
- result.current.setFlip( {
192
- horizontal: true,
193
- vertical: false,
194
- } );
195
- } );
196
-
197
- expect( result.current.state.flip ).toEqual( {
198
- horizontal: true,
199
- vertical: false,
200
- } );
201
- } );
202
-
203
- it( 'toggleFlip should toggle horizontal flip', () => {
204
- const { result } = renderHook( () => useCropperReducer() );
205
-
206
- act( () => {
207
- result.current.toggleFlip( 'horizontal' );
208
- } );
209
-
210
- expect( result.current.state.flip ).toEqual( {
211
- horizontal: true,
212
- vertical: false,
213
- } );
214
-
215
- act( () => {
216
- result.current.toggleFlip( 'horizontal' );
217
- } );
218
-
219
- expect( result.current.state.flip ).toEqual( {
220
- horizontal: false,
221
- vertical: false,
222
- } );
223
- } );
224
-
225
- it( 'toggleFlip should toggle vertical flip independently', () => {
226
- const { result } = renderHook( () => useCropperReducer() );
227
-
228
- act( () => {
229
- result.current.toggleFlip( 'vertical' );
230
- } );
231
-
232
- expect( result.current.state.flip ).toEqual( {
233
- horizontal: false,
234
- vertical: true,
235
- } );
236
- } );
237
-
238
- it( 'should dispatch SET_CROP_RECT via setCropRect', () => {
239
- const { result } = renderHook( () => useCropperReducer() );
240
-
241
- const rect = { x: 0.1, y: 0.2, width: 0.5, height: 0.6 };
242
- act( () => {
243
- result.current.setCropRect( rect );
244
- } );
245
-
246
- expect( result.current.state.cropRect ).toEqual( rect );
247
- } );
248
-
249
- it( 'should dispatch APPLY_OPERATION via applyOperation', () => {
250
- const { result } = renderHook( () => useCropperReducer() );
251
-
252
- act( () => {
253
- result.current.applyOperation( {
254
- type: 'rotate',
255
- degrees: 90,
256
- } );
257
- } );
258
-
259
- expect( result.current.state.rotation ).toBe( 90 );
260
- } );
261
-
262
- it( 'should handle APPLY_OPERATION for flip', () => {
263
- const { result } = renderHook( () => useCropperReducer() );
264
-
265
- act( () => {
266
- result.current.applyOperation( {
267
- type: 'flip',
268
- direction: 'horizontal',
269
- } );
270
- } );
271
-
272
- expect( result.current.state.flip.horizontal ).toBe( true );
273
- expect( result.current.state.flip.vertical ).toBe( false );
274
- } );
275
-
276
- it( 'should accumulate rotation via APPLY_OPERATION', () => {
277
- const { result } = renderHook( () => useCropperReducer() );
278
-
279
- act( () => {
280
- result.current.applyOperation( {
281
- type: 'rotate',
282
- degrees: 90,
283
- } );
284
- } );
285
-
286
- act( () => {
287
- result.current.applyOperation( {
288
- type: 'rotate',
289
- degrees: 45,
290
- } );
291
- } );
292
-
293
- expect( result.current.state.rotation ).toBe( 135 );
294
- } );
295
-
296
- it( 'should handle APPLY_OPERATION for crop', () => {
297
- const { result } = renderHook( () => useCropperReducer() );
298
-
299
- const rect = { x: 0.1, y: 0.2, width: 0.5, height: 0.6 };
300
- act( () => {
301
- result.current.applyOperation( {
302
- type: 'crop',
303
- rect,
304
- } );
305
- } );
306
-
307
- expect( result.current.state.cropRect ).toEqual( rect );
308
- } );
309
-
310
- it( 'should handle APPLY_OPERATION for zoom', () => {
311
- const { result } = renderHook( () => useCropperReducer() );
312
-
313
- act( () => {
314
- result.current.applyOperation( {
315
- type: 'zoom',
316
- factor: 3,
317
- } );
318
- } );
319
-
320
- expect( result.current.state.zoom ).toBe( 3 );
321
- } );
322
-
323
- it( 'should reset to DEFAULT_STATE', () => {
324
- const { result } = renderHook( () => useCropperReducer() );
325
-
326
- act( () => {
327
- result.current.setZoom( 5 );
328
- result.current.setRotation( 180 );
329
- } );
330
-
331
- expect( result.current.state.zoom ).toBe( 5 );
332
-
333
- act( () => {
334
- result.current.reset();
335
- } );
336
-
337
- expect( result.current.state ).toEqual( DEFAULT_STATE );
338
- } );
339
-
340
- it( 'should reset to a custom state', () => {
341
- const { result } = renderHook( () => useCropperReducer() );
342
-
343
- act( () => {
344
- result.current.setZoom( 5 );
345
- } );
346
-
347
- act( () => {
348
- result.current.reset( { zoom: 2 } );
349
- } );
350
-
351
- expect( result.current.state.zoom ).toBe( 2 );
352
- expect( result.current.state.pan ).toEqual( DEFAULT_STATE.pan );
353
- } );
354
-
355
- describe( 'isDirty', () => {
356
- it( 'should be false initially', () => {
357
- const { result } = renderHook( () => useCropperReducer() );
358
-
359
- expect( result.current.isDirty ).toBe( false );
360
- } );
361
-
362
- it( 'should be true after changing crop', () => {
363
- const { result } = renderHook( () => useCropperReducer() );
364
-
365
- // Zoom in first so crop change isn't clamped to (0,0).
366
- act( () => {
367
- result.current.setZoom( 3 );
368
- } );
369
-
370
- act( () => {
371
- result.current.setPan( { x: 0.1, y: 0.2 } );
372
- } );
373
-
374
- expect( result.current.isDirty ).toBe( true );
375
- } );
376
-
377
- it( 'should be true after changing zoom', () => {
378
- const { result } = renderHook( () => useCropperReducer() );
379
-
380
- act( () => {
381
- result.current.setZoom( 2 );
382
- } );
383
-
384
- expect( result.current.isDirty ).toBe( true );
385
- } );
386
-
387
- it( 'should be true after changing rotation', () => {
388
- const { result } = renderHook( () => useCropperReducer() );
389
-
390
- act( () => {
391
- result.current.setRotation( 45 );
392
- } );
393
-
394
- expect( result.current.isDirty ).toBe( true );
395
- } );
396
-
397
- it( 'should be true after changing flip', () => {
398
- const { result } = renderHook( () => useCropperReducer() );
399
-
400
- act( () => {
401
- result.current.setFlip( {
402
- horizontal: true,
403
- vertical: false,
404
- } );
405
- } );
406
-
407
- expect( result.current.isDirty ).toBe( true );
408
- } );
409
-
410
- it( 'should be false after reset', () => {
411
- const { result } = renderHook( () => useCropperReducer() );
412
-
413
- act( () => {
414
- result.current.setZoom( 5 );
415
- } );
416
-
417
- expect( result.current.isDirty ).toBe( true );
418
-
419
- act( () => {
420
- result.current.reset();
421
- } );
422
-
423
- expect( result.current.isDirty ).toBe( false );
424
- } );
425
-
426
- it( 'should be false after manually resetting fields to initial values', () => {
427
- const { result } = renderHook( () => useCropperReducer() );
428
-
429
- // Zoom in and pan.
430
- act( () => {
431
- result.current.setZoom( 3 );
432
- } );
433
-
434
- act( () => {
435
- result.current.setPan( { x: 0.2, y: 0.2 } );
436
- } );
437
-
438
- expect( result.current.isDirty ).toBe( true );
439
-
440
- // Reset each field back to its initial value manually.
441
- act( () => {
442
- result.current.setZoom( 1 );
443
- } );
444
-
445
- act( () => {
446
- result.current.setPan( { x: 0, y: 0 } );
447
- } );
448
-
449
- expect( result.current.isDirty ).toBe( false );
450
- } );
451
-
452
- // Regression: loading an image runs the new state through
453
- // enforceContainment, which can nudge pan/zoom by float ulp on
454
- // non-square images. The initial snapshot must be refreshed so
455
- // isDirty still reports false at a "clean" post-load state.
456
- it( 'should be false after setImage on a non-square image', () => {
457
- const { result } = renderHook( () => useCropperReducer() );
458
-
459
- act( () => {
460
- result.current.setImage( {
461
- src: 'test.jpg',
462
- naturalWidth: 1600,
463
- naturalHeight: 900,
464
- } );
465
- } );
466
-
467
- expect( result.current.isDirty ).toBe( false );
468
- } );
469
-
470
- it( 'does not reset the clean baseline when the same image is reported again', () => {
471
- const image = {
472
- src: 'test.jpg',
473
- naturalWidth: 1600,
474
- naturalHeight: 900,
475
- };
476
- const { result } = renderHook( () => useCropperReducer() );
477
-
478
- act( () => {
479
- result.current.setImage( image );
480
- } );
481
- act( () => {
482
- result.current.setRotation( 45 );
483
- } );
484
- expect( result.current.isDirty ).toBe( true );
485
-
486
- act( () => {
487
- result.current.setImage( image );
488
- } );
489
-
490
- expect( result.current.isDirty ).toBe( true );
491
- expect( result.current.state.rotation ).toBe( 45 );
492
- } );
493
-
494
- // Regression: reset() on a loaded image must produce a "clean"
495
- // snapshot that matches what the reducer actually stored
496
- // (preserving the image, re-enforcing containment). Otherwise
497
- // isDirty reports true right after reset.
498
- it( 'should be false after reset on a loaded image', () => {
499
- const { result } = renderHook( () => useCropperReducer() );
500
-
501
- act( () => {
502
- result.current.setImage( {
503
- src: 'test.jpg',
504
- naturalWidth: 1600,
505
- naturalHeight: 900,
506
- } );
507
- } );
508
-
509
- act( () => {
510
- result.current.setZoom( 2 );
511
- result.current.setRotation( 45 );
512
- } );
513
-
514
- expect( result.current.isDirty ).toBe( true );
515
-
516
- act( () => {
517
- result.current.reset();
518
- } );
519
-
520
- expect( result.current.isDirty ).toBe( false );
521
- } );
522
- } );
523
-
524
- describe( 'containment enforcement', () => {
525
- // For these tests, we need a loaded image so the reducer can
526
- // compute minZoomForCover and restrict position.
527
- function setupWithImage() {
528
- const view = renderHook( () => useCropperReducer() );
529
- act( () => {
530
- view.result.current.setImage( {
531
- src: 'test.jpg',
532
- naturalWidth: 1000,
533
- naturalHeight: 500,
534
- } );
535
- } );
536
- return view;
537
- }
538
-
539
- it( 'should restrict position when SET_CROP is dispatched', () => {
540
- const { result } = setupWithImage();
541
-
542
- // At zoom=1, no panning is possible — the image exactly covers
543
- // the crop area. Any pan value should be clamped to (0, 0).
544
- act( () => {
545
- result.current.setPan( { x: 0.5, y: 0.5 } );
546
- } );
547
-
548
- expect( result.current.state.pan.x ).toBeCloseTo( 0, 5 );
549
- expect( result.current.state.pan.y ).toBeCloseTo( 0, 5 );
550
- } );
551
-
552
- it( 'should allow limited panning at zoom > 1', () => {
553
- const { result } = setupWithImage();
554
-
555
- act( () => {
556
- result.current.setZoom( 2 );
557
- } );
558
-
559
- act( () => {
560
- result.current.setPan( { x: 0.3, y: 0.3 } );
561
- } );
562
-
563
- // Should be within bounds (not clamped to 0)
564
- expect( result.current.state.pan.x ).toBeGreaterThan( 0 );
565
- expect( result.current.state.pan.y ).toBeGreaterThan( 0 );
566
-
567
- // But if we try an extreme pan, it should be clamped
568
- act( () => {
569
- result.current.setPan( { x: 100, y: 100 } );
570
- } );
571
-
572
- expect( result.current.state.pan.x ).toBeLessThan( 1 );
573
- expect( result.current.state.pan.y ).toBeLessThan( 1 );
574
- } );
575
-
576
- it( 'should bump zoom to cover crop during rotation', () => {
577
- const { result } = setupWithImage();
578
-
579
- expect( result.current.state.zoom ).toBe( 1 );
580
-
581
- // Rotate 45 degrees via simple SET_ROTATION (no container).
582
- // The crop rect is unchanged and zoom is bumped to cover.
583
- act( () => {
584
- result.current.setRotation( 45 );
585
- } );
586
-
587
- expect( result.current.state.rotation ).toBe( 45 );
588
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 1 );
589
- } );
590
-
591
- it( 'should preserve zoom across rotation changes', () => {
592
- const { result } = setupWithImage();
593
-
594
- act( () => {
595
- result.current.setZoom( 2 );
596
- } );
597
-
598
- act( () => {
599
- result.current.setRotation( 45 );
600
- } );
601
-
602
- // Zoom should be at least 2 (may be higher for 45° coverage).
603
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 2 );
604
-
605
- act( () => {
606
- result.current.setRotation( 0 );
607
- } );
608
-
609
- // Zoom should not drop below the user's explicit level.
610
- // It only ratchets up; the user can zoom out explicitly if
611
- // they want.
612
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 2 );
613
- } );
614
-
615
- it( 'should reset pan when rotation changes', () => {
616
- const { result } = setupWithImage();
617
-
618
- // Zoom in and pan
619
- act( () => {
620
- result.current.setZoom( 3 );
621
- } );
622
- act( () => {
623
- result.current.setPan( { x: 0.2, y: 0.2 } );
624
- } );
625
-
626
- expect( result.current.state.pan.x ).toBeCloseTo( 0.2 );
627
-
628
- // Rotate — position should be re-restricted
629
- act( () => {
630
- result.current.setRotation( 45 );
631
- } );
632
-
633
- // The position should be within the new valid range
634
- const { pan, zoom } = result.current.state;
635
- // Verify the position is valid for the new state
636
- expect( Math.abs( pan.x ) ).toBeLessThanOrEqual( zoom );
637
- expect( Math.abs( pan.y ) ).toBeLessThanOrEqual( zoom );
638
- } );
639
-
640
- it( 'should not allow zoom below minZoomForCover', () => {
641
- const { result } = setupWithImage();
642
-
643
- // At rotation=0, minZoom is 1 for a full crop rect.
644
- act( () => {
645
- result.current.setZoom( 0.5 );
646
- } );
647
-
648
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 1 );
649
-
650
- // Zoom above maximum should be clamped to 10.
651
- act( () => {
652
- result.current.setZoom( 15 );
653
- } );
654
-
655
- expect( result.current.state.zoom ).toBe( 10 );
656
- } );
657
-
658
- it( 'should re-restrict position after zoom change', () => {
659
- const { result } = setupWithImage();
660
-
661
- // Zoom in and pan to the edge
662
- act( () => {
663
- result.current.setZoom( 4 );
664
- } );
665
- act( () => {
666
- result.current.setPan( { x: 1, y: 1 } );
667
- } );
668
-
669
- const panAtZoom4 = { ...result.current.state.pan };
670
-
671
- // Zoom out — pan should be reduced to stay within bounds
672
- act( () => {
673
- result.current.setZoom( 2 );
674
- } );
675
-
676
- expect(
677
- Math.abs( result.current.state.pan.x )
678
- ).toBeLessThanOrEqual( Math.abs( panAtZoom4.x ) );
679
- } );
680
-
681
- it( 'should rescale crop and bump zoom at 90-degree rotation for non-square image', () => {
682
- const { result } = setupWithImage();
683
- // Image is 1000x500 (2:1). At 90° the visual box flips from
684
- // landscape to portrait. The crop rect is rescaled to preserve
685
- // pixel size, and zoom is bumped to cover it.
686
- act( () => {
687
- result.current.setRotation( 90 );
688
- } );
689
-
690
- const { cropRect, zoom } = result.current.state;
691
- // Crop pixel dimensions preserved — the normalized values
692
- // change because the visual box dimensions swapped.
693
- expect( cropRect.width ).toBeGreaterThan( 0 );
694
- expect( cropRect.height ).toBeGreaterThan( 0 );
695
- // Zoom bumps to cover the rescaled crop.
696
- expect( zoom ).toBeGreaterThanOrEqual( 1 );
697
- } );
698
-
699
- it( 'should not reduce zoom when SET_CROP_RECT shrinks the crop', () => {
700
- const { result } = setupWithImage();
701
-
702
- // Zoom in explicitly.
703
- act( () => {
704
- result.current.setZoom( 2 );
705
- } );
706
- expect( result.current.state.zoom ).toBe( 2 );
707
-
708
- // Rotate so that containment matters.
709
- act( () => {
710
- result.current.setRotation( 45 );
711
- } );
712
- const zoomAfterRotation = result.current.state.zoom;
713
-
714
- // Now shrink the crop rect — zoom should not decrease.
715
- act( () => {
716
- result.current.setCropRect( {
717
- x: 0.1,
718
- y: 0.1,
719
- width: 0.3,
720
- height: 0.3,
721
- } );
722
- } );
723
-
724
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual(
725
- zoomAfterRotation
726
- );
727
- } );
728
-
729
- it( 'should bump zoom when SET_CROP_RECT requires it at rotation', () => {
730
- const { result } = setupWithImage();
731
-
732
- act( () => {
733
- result.current.setRotation( 45 );
734
- } );
735
-
736
- // Zoom was already bumped by rotation to cover full crop.
737
- const zoomAfterRotation = result.current.state.zoom;
738
- expect( zoomAfterRotation ).toBeGreaterThan( 1 );
739
-
740
- // Setting a smaller crop rect then a full one should keep zoom
741
- // at the level needed for the full rect.
742
- act( () => {
743
- result.current.setCropRect( {
744
- x: 0.2,
745
- y: 0.2,
746
- width: 0.6,
747
- height: 0.6,
748
- } );
749
- } );
750
-
751
- act( () => {
752
- result.current.setCropRect( {
753
- x: 0,
754
- y: 0,
755
- width: 1,
756
- height: 1,
757
- } );
758
- } );
759
-
760
- // Zoom should be at least what rotation required.
761
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual(
762
- zoomAfterRotation
763
- );
764
- expect( result.current.state.cropRect.width ).toBeCloseTo( 1, 1 );
765
- expect( result.current.state.cropRect.height ).toBeCloseTo( 1, 1 );
766
- } );
767
- } );
768
-
769
- describe( 'SETTLE_CROP', () => {
770
- function setupWithImage() {
771
- const view = renderHook( () => useCropperReducer() );
772
- act( () => {
773
- view.result.current.setImage( {
774
- src: 'test.jpg',
775
- naturalWidth: 1000,
776
- naturalHeight: 500,
777
- } );
778
- } );
779
- return view;
780
- }
781
-
782
- it( 'expands a small crop to fill height and centers it', () => {
783
- const { result } = setupWithImage();
784
-
785
- // Shrink the crop rect to a small centered area.
786
- act( () => {
787
- result.current.setCropRect( {
788
- x: 0.25,
789
- y: 0.25,
790
- width: 0.5,
791
- height: 0.5,
792
- } );
793
- } );
794
-
795
- act( () => {
796
- result.current.settleCrop();
797
- } );
798
-
799
- const { cropRect } = result.current.state;
800
- // Should expand to fill height (height=1) or width (width=1).
801
- expect(
802
- Math.abs( cropRect.height - 1 ) < 0.01 ||
803
- Math.abs( cropRect.width - 1 ) < 0.01
804
- ).toBe( true );
805
- // Should be centered.
806
- const cx = cropRect.x + cropRect.width / 2;
807
- const cy = cropRect.y + cropRect.height / 2;
808
- expect( cx ).toBeCloseTo( 0.5, 1 );
809
- expect( cy ).toBeCloseTo( 0.5, 1 );
810
- } );
811
-
812
- it( 'preserves the image selection (visible content center does not jump)', () => {
813
- const { result } = setupWithImage();
814
-
815
- // Zoom in so there is room to pan.
816
- act( () => {
817
- result.current.setZoom( 3 );
818
- } );
819
-
820
- // Resize crop to a smaller centered area (center at 0.5, 0.5).
821
- act( () => {
822
- result.current.setCropRect( {
823
- x: 0.25,
824
- y: 0.25,
825
- width: 0.5,
826
- height: 0.5,
827
- } );
828
- } );
829
-
830
- // Record pre-settle state.
831
- const preCropRect = result.current.state.cropRect;
832
- const preZoom = result.current.state.zoom;
833
-
834
- act( () => {
835
- result.current.settleCrop();
836
- } );
837
-
838
- const postCropRect = result.current.state.cropRect;
839
- const postZoom = result.current.state.zoom;
840
-
841
- // The zoom should scale by the expansion factor.
842
- const s = postCropRect.height / preCropRect.height;
843
- expect( postZoom ).toBeCloseTo( preZoom * s, 1 );
844
-
845
- // The crop center should remain at (0.5, 0.5).
846
- const postCx = postCropRect.x + postCropRect.width / 2;
847
- const postCy = postCropRect.y + postCropRect.height / 2;
848
- expect( postCx ).toBeCloseTo( 0.5, 1 );
849
- expect( postCy ).toBeCloseTo( 0.5, 1 );
850
- } );
851
-
852
- it( 'is a no-op when crop is already full and centered', () => {
853
- const { result } = setupWithImage();
854
-
855
- // State is already default: full crop, centered.
856
- const stateBefore = result.current.state;
857
-
858
- act( () => {
859
- result.current.settleCrop();
860
- } );
861
-
862
- const stateAfter = result.current.state;
863
- expect( stateAfter.cropRect ).toEqual( stateBefore.cropRect );
864
- expect( stateAfter.zoom ).toEqual( stateBefore.zoom );
865
- expect( stateAfter.pan.x ).toBeCloseTo( stateBefore.pan.x, 5 );
866
- expect( stateAfter.pan.y ).toBeCloseTo( stateBefore.pan.y, 5 );
867
- } );
868
- } );
869
-
870
- describe( 'SNAP_ROTATE_90', () => {
871
- function setupWithImage() {
872
- const view = renderHook( () => useCropperReducer() );
873
- act( () => {
874
- view.result.current.setImage( {
875
- src: 'test.jpg',
876
- naturalWidth: 1000,
877
- naturalHeight: 500,
878
- } );
879
- } );
880
- return view;
881
- }
882
-
883
- it( 'after snap rotate CW, crop width and height are swapped', () => {
884
- const { result } = setupWithImage();
885
-
886
- // Set a non-square crop so the swap is visible.
887
- act( () => {
888
- result.current.setCropRect( {
889
- x: 0.1,
890
- y: 0.2,
891
- width: 0.6,
892
- height: 0.4,
893
- } );
894
- } );
895
-
896
- const widthBefore = result.current.state.cropRect.width;
897
- const heightBefore = result.current.state.cropRect.height;
898
-
899
- act( () => {
900
- result.current.snapRotate90( 1 );
901
- } );
902
-
903
- // After snap rotate, width and height should be swapped
904
- // (though enforceContainment may adjust slightly).
905
- const { cropRect } = result.current.state;
906
- expect( cropRect.width ).toBeCloseTo( heightBefore, 1 );
907
- expect( cropRect.height ).toBeCloseTo( widthBefore, 1 );
908
- expect( result.current.state.rotation ).toBe( 90 );
909
- } );
910
-
911
- it( 'after snap rotate CCW, crop width and height are swapped', () => {
912
- const { result } = setupWithImage();
913
-
914
- act( () => {
915
- result.current.setCropRect( {
916
- x: 0.1,
917
- y: 0.2,
918
- width: 0.6,
919
- height: 0.4,
920
- } );
921
- } );
922
-
923
- const widthBefore = result.current.state.cropRect.width;
924
- const heightBefore = result.current.state.cropRect.height;
925
-
926
- act( () => {
927
- result.current.snapRotate90( -1 );
928
- } );
929
-
930
- const { cropRect } = result.current.state;
931
- expect( cropRect.width ).toBeCloseTo( heightBefore, 1 );
932
- expect( cropRect.height ).toBeCloseTo( widthBefore, 1 );
933
- expect( result.current.state.rotation ).toBe( 270 );
934
- } );
935
-
936
- it( 'after two snap rotates CW (180 degrees), dimensions return to original', () => {
937
- const { result } = setupWithImage();
938
-
939
- act( () => {
940
- result.current.setCropRect( {
941
- x: 0.1,
942
- y: 0.2,
943
- width: 0.6,
944
- height: 0.4,
945
- } );
946
- } );
947
-
948
- const widthBefore = result.current.state.cropRect.width;
949
- const heightBefore = result.current.state.cropRect.height;
950
-
951
- act( () => {
952
- result.current.snapRotate90( 1 );
953
- } );
954
-
955
- act( () => {
956
- result.current.snapRotate90( 1 );
957
- } );
958
-
959
- const { cropRect } = result.current.state;
960
- expect( cropRect.width ).toBeCloseTo( widthBefore, 1 );
961
- expect( cropRect.height ).toBeCloseTo( heightBefore, 1 );
962
- expect( result.current.state.rotation ).toBe( 180 );
963
- } );
964
- } );
965
-
966
- describe( 'public controller contract', () => {
967
- it( 'does not expose the raw reducer dispatch', () => {
968
- const { result } = renderHook( () => useCropperReducer() );
969
-
970
- expect( '__dispatch' in result.current ).toBe( false );
971
- } );
972
- } );
973
-
974
- describe( 'isStateDirty coverage guard', () => {
975
- // This test ensures that isStateDirty checks ALL fields in CropperState.
976
- // If you add a field to CropperState/DEFAULT_STATE and this test fails,
977
- // you MUST also update isStateDirty() in use-cropper-reducer.ts.
978
- it( 'should track all CropperState fields', () => {
979
- // Flatten DEFAULT_STATE keys into dot-notation paths.
980
- // e.g., { pan: { x: 0, y: 0 } } → ['crop.x', 'crop.y']
981
- function flattenKeys(
982
- obj: Record< string, unknown >,
983
- prefix = ''
984
- ): string[] {
985
- const keys: string[] = [];
986
- for ( const key of Object.keys( obj ) ) {
987
- const fullKey = prefix ? `${ prefix }.${ key }` : key;
988
- const value = obj[ key ];
989
- if (
990
- value !== null &&
991
- typeof value === 'object' &&
992
- ! Array.isArray( value )
993
- ) {
994
- keys.push(
995
- ...flattenKeys(
996
- value as Record< string, unknown >,
997
- fullKey
998
- )
999
- );
1000
- } else {
1001
- keys.push( fullKey );
1002
- }
1003
- }
1004
- return keys.sort();
1005
- }
1006
-
1007
- // The fields that isStateDirty currently checks.
1008
- // Keep this in sync — if you add a field to CropperState,
1009
- // add it here AND in isStateDirty().
1010
- const dirtyCheckedFields = [
1011
- 'pan.x',
1012
- 'pan.y',
1013
- 'cropRect.height',
1014
- 'cropRect.width',
1015
- 'cropRect.x',
1016
- 'cropRect.y',
1017
- 'flip.horizontal',
1018
- 'flip.vertical',
1019
- 'rotation',
1020
- 'zoom',
1021
- ].sort();
1022
-
1023
- // 'image' is intentionally excluded — it's set once on load
1024
- // and doesn't represent a user edit.
1025
- // 'image' is set once on load.
1026
- // 'basePan', 'baseZoom', 'baseRotation' mirror the user's
1027
- // committed pose and are redundant for dirty tracking — if
1028
- // any base field differs from initial, the corresponding
1029
- // live field (crop/zoom/rotation) will also differ.
1030
- const excludedFields = [
1031
- 'image',
1032
- 'basePan',
1033
- 'baseZoom',
1034
- 'baseRotation',
1035
- ];
1036
-
1037
- const stateKeys = flattenKeys(
1038
- DEFAULT_STATE as unknown as Record< string, unknown >
1039
- ).filter(
1040
- ( key ) =>
1041
- ! excludedFields.some(
1042
- ( ex ) => key === ex || key.startsWith( ex + '.' )
1043
- )
1044
- );
1045
-
1046
- expect( stateKeys ).toEqual( dirtyCheckedFields );
1047
- } );
1048
- } );
1049
- } );