@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,665 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import {
3
- computeFreeResizeRect,
4
- computeLockedResizeRect,
5
- computeShiftLockedResizeRect,
6
- getMinCropPixels,
7
- type CropBounds,
8
- type ResizeDragState,
9
- } from '../stencil-math';
10
- import {
11
- DEFAULT_KEYBOARD_STEP,
12
- MIN_CROP_PIXELS,
13
- MIN_CROP_SCREEN_PX,
14
- } from '../constants';
15
- import type { Size } from '../types';
16
-
17
- describe( 'getMinCropPixels — operable on-screen floor', () => {
18
- it( 'returns the source-pixel hard floor when the image is shown large enough', () => {
19
- const displayScale = ( MIN_CROP_SCREEN_PX / MIN_CROP_PIXELS ) * 1.1;
20
-
21
- expect( getMinCropPixels( displayScale ) ).toBe( MIN_CROP_PIXELS );
22
- } );
23
-
24
- it( 'raises the floor so the crop stays operable when the image is shown small', () => {
25
- // A large image fit into a small window: ~0.2 CSS px per source px.
26
- // The source-pixel floor would render too small on screen. The
27
- // usability term (44 / 0.2 = 220 source px) must win.
28
- expect( getMinCropPixels( 0.2 ) ).toBeCloseTo(
29
- MIN_CROP_SCREEN_PX / 0.2,
30
- 5
31
- );
32
- expect( getMinCropPixels( 0.2 ) ).toBeGreaterThan( MIN_CROP_PIXELS );
33
- } );
34
-
35
- it( 'crosses over from usability floor to hard floor at displayScale = MIN_CROP_SCREEN_PX / MIN_CROP_PIXELS', () => {
36
- const crossover = MIN_CROP_SCREEN_PX / MIN_CROP_PIXELS;
37
- expect( getMinCropPixels( crossover ) ).toBeCloseTo(
38
- MIN_CROP_PIXELS,
39
- 5
40
- );
41
- // Just below the crossover, the usability term exceeds the hard floor.
42
- expect( getMinCropPixels( crossover * 0.99 ) ).toBeGreaterThan(
43
- MIN_CROP_PIXELS
44
- );
45
- } );
46
-
47
- it( 'falls back to the hard floor for a non-positive display scale', () => {
48
- expect( getMinCropPixels( 0 ) ).toBe( MIN_CROP_PIXELS );
49
- expect( getMinCropPixels( -1 ) ).toBe( MIN_CROP_PIXELS );
50
- } );
51
- } );
52
-
53
- const FULL_BOUNDS: CropBounds = { minX: 0, minY: 0, maxX: 1, maxY: 1 };
54
-
55
- describe( 'computeLockedResizeRect — driver-axis selection', () => {
56
- // On non-square images, the locked-ratio resize must pick the
57
- // driving axis based on which the user moved more in *pixel* space,
58
- // compared against the *pixel* aspect ratio. Comparing pixel motion
59
- // against the normalized w/h ratio is a unit mismatch and produces
60
- // the wrong axis on non-square images.
61
- //
62
- // Setup: a wide 1600×900 image, locked to a square (1:1) crop.
63
- // Drag from the NW corner of a (degenerate) start rect, anchored at
64
- // (0, 0), so distW/distH after the drag equals the drag offset.
65
- const imageSize: Size = { width: 1600, height: 900 };
66
- const aspectRatio = 1;
67
- // The stencil normalizes the pixel ratio for the math:
68
- // normalizedRatio = aspectRatio * imageH / imageW.
69
- const normalizedRatio =
70
- ( aspectRatio * imageSize.height ) / imageSize.width;
71
- const startRect = { x: 0, y: 0, width: 0, height: 0 };
72
-
73
- it( 'lets height drive when the user moves more pixels vertically than horizontally (between normalized and pixel thresholds)', () => {
74
- // Drag SE corner to (144px, 160px) from anchor (0, 0).
75
- // pixelDistW=144, pixelDistH=160, ratio=0.9 — comfortably above
76
- // MIN_CROP_SIZE (0.05 = 80px on a 1600px image), so neither axis
77
- // is at the min-clamp boundary. The ratio (0.9) sits between the
78
- // old buggy threshold (normalizedRatio=0.5625) and the correct
79
- // pixel-space threshold (aspectRatio=1) — that's the gap where
80
- // the unit mismatch produced the wrong driver axis.
81
- // Correct: 0.9 < aspectRatio(1) → height drives → 160×160 pixels.
82
- const drag: ResizeDragState = {
83
- handle: 'se',
84
- startX: 0,
85
- startY: 0,
86
- startRect,
87
- };
88
- const rect = computeLockedResizeRect(
89
- drag,
90
- 144,
91
- 160,
92
- imageSize,
93
- FULL_BOUNDS,
94
- normalizedRatio
95
- );
96
-
97
- const pixelW = rect.width * imageSize.width;
98
- const pixelH = rect.height * imageSize.height;
99
-
100
- expect( pixelW / pixelH ).toBeCloseTo( 1, 5 );
101
- expect( pixelH ).toBeCloseTo( 160, 5 );
102
- expect( pixelW ).toBeCloseTo( 160, 5 );
103
- } );
104
-
105
- it( 'returns the start rect unchanged when normalizedRatio is 0 (image not loaded)', () => {
106
- // Reachable in practice: keyboard arrows on a focused resize
107
- // handle before the image's natural size is known make the
108
- // stencil pass `normalizedRatio === 0`.
109
- const preLoadStart = { x: 0.1, y: 0.2, width: 0.3, height: 0.4 };
110
- const drag: ResizeDragState = {
111
- handle: 'se',
112
- startX: 0,
113
- startY: 0,
114
- startRect: preLoadStart,
115
- };
116
- const rect = computeLockedResizeRect(
117
- drag,
118
- 100,
119
- 100,
120
- { width: 0, height: 0 },
121
- FULL_BOUNDS,
122
- 0
123
- );
124
-
125
- expect( rect ).toEqual( preLoadStart );
126
- } );
127
-
128
- it( 'lets width drive when the user moves more pixels horizontally than vertically', () => {
129
- // Drag SE corner to (200px, 50px) from anchor (0, 0).
130
- // pixelDistW=200, pixelDistH=50, ratio=4 > aspectRatio(1).
131
- // Width drives. Result: 200×200 pixels.
132
- const drag: ResizeDragState = {
133
- handle: 'se',
134
- startX: 0,
135
- startY: 0,
136
- startRect,
137
- };
138
- const rect = computeLockedResizeRect(
139
- drag,
140
- 200,
141
- 50,
142
- imageSize,
143
- FULL_BOUNDS,
144
- normalizedRatio
145
- );
146
-
147
- const pixelW = rect.width * imageSize.width;
148
- const pixelH = rect.height * imageSize.height;
149
-
150
- expect( pixelW / pixelH ).toBeCloseTo( 1, 5 );
151
- expect( pixelW ).toBeCloseTo( 200, 5 );
152
- expect( pixelH ).toBeCloseTo( 200, 5 );
153
- } );
154
-
155
- it( 'keeps pointer resize continuous near the pixel-motion threshold', () => {
156
- const originalImageSize: Size = { width: 600, height: 400 };
157
- const fullImageCrop = { x: 0, y: 0, width: 1, height: 1 };
158
- const drag: ResizeDragState = {
159
- handle: 'nw',
160
- startX: 0,
161
- startY: 0,
162
- startRect: fullImageCrop,
163
- };
164
-
165
- const beforeThreshold = computeLockedResizeRect(
166
- drag,
167
- 60,
168
- -39.6,
169
- originalImageSize,
170
- FULL_BOUNDS,
171
- 1
172
- );
173
- const afterThreshold = computeLockedResizeRect(
174
- drag,
175
- 60,
176
- -40.4,
177
- originalImageSize,
178
- FULL_BOUNDS,
179
- 1
180
- );
181
-
182
- expect(
183
- Math.abs( beforeThreshold.width - afterThreshold.width )
184
- ).toBeLessThan( 0.01 );
185
- expect(
186
- Math.abs( beforeThreshold.height - afterThreshold.height )
187
- ).toBeLessThan( 0.01 );
188
- } );
189
-
190
- it( 'shrinks a locked-ratio crop from horizontal keyboard movement', () => {
191
- const squareImageSize: Size = { width: 500, height: 500 };
192
- const lockedStartRect = { x: 0.1, y: 0.1, width: 0.8, height: 0.8 };
193
- const drag: ResizeDragState = {
194
- handle: 'nw',
195
- startX: 0,
196
- startY: 0,
197
- startRect: lockedStartRect,
198
- };
199
- const rect = computeLockedResizeRect(
200
- drag,
201
- DEFAULT_KEYBOARD_STEP * squareImageSize.width,
202
- 0,
203
- squareImageSize,
204
- FULL_BOUNDS,
205
- 1,
206
- undefined,
207
- 'width'
208
- );
209
-
210
- expect( rect.x ).toBeCloseTo(
211
- lockedStartRect.x + DEFAULT_KEYBOARD_STEP,
212
- 5
213
- );
214
- expect( rect.y ).toBeCloseTo(
215
- lockedStartRect.y + DEFAULT_KEYBOARD_STEP,
216
- 5
217
- );
218
- expect( rect.width ).toBeCloseTo(
219
- lockedStartRect.width - DEFAULT_KEYBOARD_STEP,
220
- 5
221
- );
222
- expect( rect.height ).toBeCloseTo(
223
- lockedStartRect.height - DEFAULT_KEYBOARD_STEP,
224
- 5
225
- );
226
- } );
227
-
228
- it( 'shrinks a locked-ratio crop from vertical keyboard movement', () => {
229
- const squareImageSize: Size = { width: 500, height: 500 };
230
- const lockedStartRect = { x: 0.1, y: 0.1, width: 0.8, height: 0.8 };
231
- const drag: ResizeDragState = {
232
- handle: 'nw',
233
- startX: 0,
234
- startY: 0,
235
- startRect: lockedStartRect,
236
- };
237
- const rect = computeLockedResizeRect(
238
- drag,
239
- 0,
240
- DEFAULT_KEYBOARD_STEP * squareImageSize.height,
241
- squareImageSize,
242
- FULL_BOUNDS,
243
- 1,
244
- undefined,
245
- 'height'
246
- );
247
-
248
- expect( rect.x ).toBeCloseTo(
249
- lockedStartRect.x + DEFAULT_KEYBOARD_STEP,
250
- 5
251
- );
252
- expect( rect.y ).toBeCloseTo(
253
- lockedStartRect.y + DEFAULT_KEYBOARD_STEP,
254
- 5
255
- );
256
- expect( rect.width ).toBeCloseTo(
257
- lockedStartRect.width - DEFAULT_KEYBOARD_STEP,
258
- 5
259
- );
260
- expect( rect.height ).toBeCloseTo(
261
- lockedStartRect.height - DEFAULT_KEYBOARD_STEP,
262
- 5
263
- );
264
- } );
265
- } );
266
-
267
- const IMAGE: Size = { width: 1000, height: 500 };
268
-
269
- const START_RECT = { x: 0.2, y: 0.2, width: 0.4, height: 0.6 };
270
-
271
- function makeDrag(
272
- handle: ResizeDragState[ 'handle' ],
273
- startX = 500,
274
- startY = 250
275
- ): ResizeDragState {
276
- return {
277
- handle,
278
- startX,
279
- startY,
280
- startRect: { ...START_RECT },
281
- };
282
- }
283
-
284
- describe( 'computeShiftLockedResizeRect', () => {
285
- describe( 'corner handles', () => {
286
- it( 'preserves the start rect ratio when dragging the SE corner outward', () => {
287
- const drag = makeDrag( 'se' );
288
- // Drag right by 200px and down by 50px — width is the bigger
289
- // pixel motion, so it drives.
290
- const rect = computeShiftLockedResizeRect(
291
- drag,
292
- 700,
293
- 300,
294
- IMAGE,
295
- FULL_BOUNDS
296
- );
297
-
298
- const startPixelRatio =
299
- ( START_RECT.width * IMAGE.width ) /
300
- ( START_RECT.height * IMAGE.height );
301
- const newPixelRatio =
302
- ( rect.width * IMAGE.width ) / ( rect.height * IMAGE.height );
303
-
304
- expect( newPixelRatio ).toBeCloseTo( startPixelRatio, 5 );
305
- // SE drag anchors at the NW corner of the start rect.
306
- expect( rect.x ).toBeCloseTo( START_RECT.x, 5 );
307
- expect( rect.y ).toBeCloseTo( START_RECT.y, 5 );
308
- } );
309
-
310
- it( 'preserves the start rect ratio when dragging the NW corner inward', () => {
311
- const drag = makeDrag( 'nw' );
312
- const rect = computeShiftLockedResizeRect(
313
- drag,
314
- 600,
315
- 300,
316
- IMAGE,
317
- FULL_BOUNDS
318
- );
319
-
320
- const startPixelRatio =
321
- ( START_RECT.width * IMAGE.width ) /
322
- ( START_RECT.height * IMAGE.height );
323
- const newPixelRatio =
324
- ( rect.width * IMAGE.width ) / ( rect.height * IMAGE.height );
325
- expect( newPixelRatio ).toBeCloseTo( startPixelRatio, 5 );
326
- // NW drag anchors at the SE corner of the start rect.
327
- expect( rect.x + rect.width ).toBeCloseTo(
328
- START_RECT.x + START_RECT.width,
329
- 5
330
- );
331
- expect( rect.y + rect.height ).toBeCloseTo(
332
- START_RECT.y + START_RECT.height,
333
- 5
334
- );
335
- } );
336
- } );
337
-
338
- describe( 'edge handles — symmetric expansion', () => {
339
- it( 'east edge: expands height symmetrically around the start rect center', () => {
340
- const drag = makeDrag( 'e' );
341
- // Expand width by 100px to the right.
342
- const rect = computeShiftLockedResizeRect(
343
- drag,
344
- 600,
345
- 250,
346
- IMAGE,
347
- FULL_BOUNDS
348
- );
349
-
350
- const startCenterY = START_RECT.y + START_RECT.height / 2;
351
- expect( rect.y + rect.height / 2 ).toBeCloseTo( startCenterY, 5 );
352
- // x-anchored to start.x.
353
- expect( rect.x ).toBeCloseTo( START_RECT.x, 5 );
354
- // Ratio preserved.
355
- const startPixelRatio =
356
- ( START_RECT.width * IMAGE.width ) /
357
- ( START_RECT.height * IMAGE.height );
358
- const newPixelRatio =
359
- ( rect.width * IMAGE.width ) / ( rect.height * IMAGE.height );
360
- expect( newPixelRatio ).toBeCloseTo( startPixelRatio, 5 );
361
- } );
362
-
363
- it( 'north edge: expands width symmetrically around the start rect center', () => {
364
- const drag = makeDrag( 'n' );
365
- // Drag the top edge up by 50px (taller crop).
366
- const rect = computeShiftLockedResizeRect(
367
- drag,
368
- 500,
369
- 200,
370
- IMAGE,
371
- FULL_BOUNDS
372
- );
373
-
374
- const startCenterX = START_RECT.x + START_RECT.width / 2;
375
- expect( rect.x + rect.width / 2 ).toBeCloseTo( startCenterX, 5 );
376
- // Bottom edge stays anchored.
377
- expect( rect.y + rect.height ).toBeCloseTo(
378
- START_RECT.y + START_RECT.height,
379
- 5
380
- );
381
- const startPixelRatio =
382
- ( START_RECT.width * IMAGE.width ) /
383
- ( START_RECT.height * IMAGE.height );
384
- const newPixelRatio =
385
- ( rect.width * IMAGE.width ) / ( rect.height * IMAGE.height );
386
- expect( newPixelRatio ).toBeCloseTo( startPixelRatio, 5 );
387
- } );
388
- } );
389
-
390
- describe( 'minimum size clamping preserves ratio', () => {
391
- it( 'east edge: clamps a tall crop without breaking ratio when width hits MIN_CROP_SIZE', () => {
392
- // Tall start rect — small ratio (0.0625 in normalized space).
393
- const tallStart = { x: 0.2, y: 0.1, width: 0.05, height: 0.8 };
394
- const drag: ResizeDragState = {
395
- handle: 'e',
396
- startX: 250,
397
- startY: 250,
398
- startRect: tallStart,
399
- };
400
- // Drag the east edge inward — width tries to shrink below MIN.
401
- const rect = computeShiftLockedResizeRect(
402
- drag,
403
- 200,
404
- 250,
405
- IMAGE,
406
- FULL_BOUNDS
407
- );
408
-
409
- const startRatio = tallStart.width / tallStart.height;
410
- const newRatio = rect.width / rect.height;
411
- expect( newRatio ).toBeCloseTo( startRatio, 5 );
412
- expect( rect.width ).toBeGreaterThanOrEqual( 0.05 - 1e-9 );
413
- expect( rect.height ).toBeGreaterThanOrEqual( 0.05 - 1e-9 );
414
- } );
415
-
416
- it( 'north edge: clamps a wide crop without breaking ratio when height hits MIN_CROP_SIZE', () => {
417
- // Wide start rect — large ratio (16 in normalized space).
418
- const wideStart = { x: 0.1, y: 0.4, width: 0.8, height: 0.05 };
419
- const drag: ResizeDragState = {
420
- handle: 'n',
421
- startX: 500,
422
- startY: 200,
423
- startRect: wideStart,
424
- };
425
- // Drag the north edge down — height tries to shrink below MIN.
426
- const rect = computeShiftLockedResizeRect(
427
- drag,
428
- 500,
429
- 220,
430
- IMAGE,
431
- FULL_BOUNDS
432
- );
433
-
434
- const startRatio = wideStart.width / wideStart.height;
435
- const newRatio = rect.width / rect.height;
436
- expect( newRatio ).toBeCloseTo( startRatio, 5 );
437
- expect( rect.width ).toBeGreaterThanOrEqual( 0.05 - 1e-9 );
438
- expect( rect.height ).toBeGreaterThanOrEqual( 0.05 - 1e-9 );
439
- } );
440
- } );
441
-
442
- describe( 'bounds clamping', () => {
443
- it( 'east edge: clamps height to the symmetric bounds limit and shrinks width to keep ratio', () => {
444
- // Center-y of START_RECT is 0.5; tightest bounds are bounds.minY=0.4
445
- // and bounds.maxY=0.6 → max half-height = 0.1, so max height = 0.2.
446
- const tightBounds: CropBounds = {
447
- minX: 0,
448
- minY: 0.4,
449
- maxX: 1,
450
- maxY: 0.6,
451
- };
452
- const drag = makeDrag( 'e' );
453
- // Drag far right — height would otherwise exceed the bound.
454
- const rect = computeShiftLockedResizeRect(
455
- drag,
456
- 1000,
457
- 250,
458
- IMAGE,
459
- tightBounds
460
- );
461
-
462
- expect( rect.height ).toBeLessThanOrEqual( 0.2 + 1e-9 );
463
- // Centered around 0.5.
464
- expect( rect.y + rect.height / 2 ).toBeCloseTo( 0.5, 5 );
465
- const startPixelRatio =
466
- ( START_RECT.width * IMAGE.width ) /
467
- ( START_RECT.height * IMAGE.height );
468
- const newPixelRatio =
469
- ( rect.width * IMAGE.width ) / ( rect.height * IMAGE.height );
470
- expect( newPixelRatio ).toBeCloseTo( startPixelRatio, 5 );
471
- } );
472
- } );
473
- } );
474
-
475
- describe( 'per-axis minCropSize', () => {
476
- // Cover the explicit per-axis floor introduced for the source-pixel
477
- // minimum. The defaults (0.05, 0.05) are already covered by the
478
- // suites above; these tests pass `minCropSize` explicitly to lock in
479
- // the math the cropper relies on.
480
- const IMAGE_NON_SQUARE: Size = { width: 1000, height: 500 };
481
-
482
- describe( 'computeFreeResizeRect', () => {
483
- it( 'clamps the dragged edge to the per-axis floor (width)', () => {
484
- // SE handle from a wide start rect; drag left far enough that
485
- // width would collapse without the floor.
486
- const startRect = { x: 0.1, y: 0.1, width: 0.6, height: 0.6 };
487
- const drag: ResizeDragState = {
488
- handle: 'e',
489
- startX: 700,
490
- startY: 350,
491
- startRect,
492
- };
493
- const minCropSize: Size = { width: 0.2, height: 0.05 };
494
- const rect = computeFreeResizeRect(
495
- drag,
496
- 0,
497
- 350,
498
- IMAGE_NON_SQUARE,
499
- FULL_BOUNDS,
500
- minCropSize
501
- );
502
- expect( rect.width ).toBeCloseTo( minCropSize.width, 5 );
503
- // Left edge anchored at start; right edge pulled in to the floor.
504
- expect( rect.x ).toBeCloseTo( startRect.x, 5 );
505
- } );
506
-
507
- it( 'clamps the dragged edge to the per-axis floor (height)', () => {
508
- const startRect = { x: 0.1, y: 0.1, width: 0.6, height: 0.6 };
509
- const drag: ResizeDragState = {
510
- handle: 's',
511
- startX: 500,
512
- startY: 350,
513
- startRect,
514
- };
515
- const minCropSize: Size = { width: 0.05, height: 0.3 };
516
- const rect = computeFreeResizeRect(
517
- drag,
518
- 500,
519
- 0,
520
- IMAGE_NON_SQUARE,
521
- FULL_BOUNDS,
522
- minCropSize
523
- );
524
- expect( rect.height ).toBeCloseTo( minCropSize.height, 5 );
525
- expect( rect.y ).toBeCloseTo( startRect.y, 5 );
526
- } );
527
-
528
- it( 'uses width vs height independently — different floors per axis', () => {
529
- // Drag NW corner inward far past both floors. The per-axis
530
- // floors clamp x-collapse and y-collapse independently.
531
- const startRect = { x: 0.1, y: 0.1, width: 0.8, height: 0.8 };
532
- const drag: ResizeDragState = {
533
- handle: 'nw',
534
- startX: 100,
535
- startY: 50,
536
- startRect,
537
- };
538
- const minCropSize: Size = { width: 0.4, height: 0.1 };
539
- const rect = computeFreeResizeRect(
540
- drag,
541
- 1000,
542
- 500,
543
- IMAGE_NON_SQUARE,
544
- FULL_BOUNDS,
545
- minCropSize
546
- );
547
- // Both floors hit at the same time on different axes.
548
- expect( rect.width ).toBeCloseTo( minCropSize.width, 5 );
549
- expect( rect.height ).toBeCloseTo( minCropSize.height, 5 );
550
- } );
551
- } );
552
-
553
- describe( 'computeLockedResizeRect — per-axis floor projects through the ratio', () => {
554
- // Wide image, square aspect ratio. The per-axis floor projection
555
- // in `minDistW = max(minCropSize.width, minCropSize.height * normalizedRatio)`
556
- // is the load-bearing piece for non-equal per-axis floors.
557
- const imageSize: Size = { width: 2000, height: 1000 };
558
- const aspectRatio = 1; // pixel-square
559
- const normalizedRatio =
560
- ( aspectRatio * imageSize.height ) / imageSize.width; // 0.5
561
- const startRect = { x: 0, y: 0, width: 0, height: 0 };
562
-
563
- it( 'lifts width to satisfy the height floor when the height floor is the binding axis', () => {
564
- // Drag SE inward to zero — both axes try to collapse. With
565
- // minCropSize = { 0.01, 0.05 } and normalizedRatio 0.5,
566
- // minDistW = max(0.01, 0.05 * 0.5) = 0.025, minDistH = 0.05.
567
- // Driver: pixel motion equal → height drives → distW = distH/2.
568
- const drag: ResizeDragState = {
569
- handle: 'se',
570
- startX: 0,
571
- startY: 0,
572
- startRect,
573
- };
574
- const minCropSize: Size = { width: 0.01, height: 0.05 };
575
- const rect = computeLockedResizeRect(
576
- drag,
577
- 1,
578
- 1,
579
- imageSize,
580
- FULL_BOUNDS,
581
- normalizedRatio,
582
- minCropSize
583
- );
584
- // Height floor binds: width is `height * normalizedRatio`.
585
- expect( rect.height ).toBeCloseTo( minCropSize.height, 5 );
586
- expect( rect.width ).toBeCloseTo(
587
- minCropSize.height * normalizedRatio,
588
- 5
589
- );
590
- } );
591
-
592
- it( 'lifts height to satisfy the width floor when the width floor binds', () => {
593
- // minDistW = max(0.2, 0.05 * 0.5) = 0.2, minDistH = 0.4.
594
- const drag: ResizeDragState = {
595
- handle: 'se',
596
- startX: 0,
597
- startY: 0,
598
- startRect,
599
- };
600
- const minCropSize: Size = { width: 0.2, height: 0.05 };
601
- const rect = computeLockedResizeRect(
602
- drag,
603
- 1,
604
- 1,
605
- imageSize,
606
- FULL_BOUNDS,
607
- normalizedRatio,
608
- minCropSize
609
- );
610
- expect( rect.width ).toBeCloseTo( 0.2, 5 );
611
- expect( rect.height ).toBeCloseTo( 0.4, 5 );
612
- } );
613
- } );
614
-
615
- describe( 'computeShiftLockedResizeRect — edge handles', () => {
616
- // Shift-locked edge resize must enforce the floor on the driver
617
- // axis and on the perpendicular axis after projecting through
618
- // the start rect's normalized ratio.
619
- const imageSize: Size = { width: 1000, height: 500 };
620
- // Wide start rect — normalizedRatio = 0.4/0.4 = 1.
621
- const startRect = { x: 0.3, y: 0.3, width: 0.4, height: 0.4 };
622
-
623
- it( 'east edge: width-driver respects width floor and projects to height', () => {
624
- const drag: ResizeDragState = {
625
- handle: 'e',
626
- startX: 700,
627
- startY: 400,
628
- startRect,
629
- };
630
- // Drag well past the floor toward the start anchor.
631
- const minCropSize: Size = { width: 0.2, height: 0.05 };
632
- const rect = computeShiftLockedResizeRect(
633
- drag,
634
- 300,
635
- 400,
636
- imageSize,
637
- FULL_BOUNDS,
638
- minCropSize
639
- );
640
- // Width clamped at the per-axis floor; height follows ratio (1:1).
641
- expect( rect.width ).toBeCloseTo( minCropSize.width, 5 );
642
- expect( rect.height ).toBeCloseTo( minCropSize.width, 5 );
643
- } );
644
-
645
- it( 'north edge: height-driver respects height floor and projects to width', () => {
646
- const drag: ResizeDragState = {
647
- handle: 'n',
648
- startX: 500,
649
- startY: 300,
650
- startRect,
651
- };
652
- const minCropSize: Size = { width: 0.05, height: 0.2 };
653
- const rect = computeShiftLockedResizeRect(
654
- drag,
655
- 500,
656
- 700,
657
- imageSize,
658
- FULL_BOUNDS,
659
- minCropSize
660
- );
661
- expect( rect.height ).toBeCloseTo( minCropSize.height, 5 );
662
- expect( rect.width ).toBeCloseTo( minCropSize.height, 5 );
663
- } );
664
- } );
665
- } );