@wordpress/media-editor 0.11.1 → 0.11.2-next.v.202606191442.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 (138) hide show
  1. package/build/components/media-editor/index.cjs.map +1 -1
  2. package/build/components/media-editor/use-crop-options.cjs.map +1 -1
  3. package/build/components/media-editor-canvas/index.cjs +63 -15
  4. package/build/components/media-editor-canvas/index.cjs.map +3 -3
  5. package/build/components/media-editor-fine-rotation/index.cjs.map +1 -1
  6. package/build/components/media-editor-image-controls/index.cjs +2 -2
  7. package/build/components/media-editor-image-controls/index.cjs.map +2 -2
  8. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +4 -4
  9. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +2 -2
  10. package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -1
  11. package/build/components/rotation-ruler/index.cjs +1 -1
  12. package/build/components/rotation-ruler/index.cjs.map +1 -1
  13. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -1
  14. package/build/image-editor/core/camera.cjs +13 -0
  15. package/build/image-editor/core/camera.cjs.map +2 -2
  16. package/build/image-editor/core/constants.cjs +12 -3
  17. package/build/image-editor/core/constants.cjs.map +2 -2
  18. package/build/image-editor/core/containment.cjs.map +1 -1
  19. package/build/image-editor/core/crop-rect.cjs.map +1 -1
  20. package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -1
  21. package/build/image-editor/core/interaction-controller.cjs +9 -7
  22. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  23. package/build/image-editor/core/math/sanitize.cjs.map +1 -1
  24. package/build/image-editor/core/setter-helpers.cjs.map +1 -1
  25. package/build/image-editor/core/source-region.cjs +111 -2
  26. package/build/image-editor/core/source-region.cjs.map +2 -2
  27. package/build/image-editor/core/state.cjs.map +1 -1
  28. package/build/image-editor/core/stencil-math.cjs +3 -4
  29. package/build/image-editor/core/stencil-math.cjs.map +2 -2
  30. package/build/image-editor/core/transform-style.cjs.map +1 -1
  31. package/build/image-editor/core/types.cjs.map +1 -1
  32. package/build/image-editor/react/components/cropper.cjs +232 -55
  33. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  34. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -1
  35. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +1 -0
  36. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
  37. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +65 -19
  38. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  39. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -1
  40. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +1 -1
  41. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +2 -2
  42. package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -1
  43. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -1
  44. package/build/index.cjs.map +1 -1
  45. package/build/state/types.cjs.map +1 -1
  46. package/build/state/use-media-editor-state.cjs +1 -1
  47. package/build/state/use-media-editor-state.cjs.map +2 -2
  48. package/build-module/components/media-editor/index.mjs.map +1 -1
  49. package/build-module/components/media-editor/use-crop-options.mjs.map +1 -1
  50. package/build-module/components/media-editor-canvas/index.mjs +55 -17
  51. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  52. package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -1
  53. package/build-module/components/media-editor-image-controls/index.mjs +2 -2
  54. package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
  55. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +4 -4
  56. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +2 -2
  57. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +1 -1
  58. package/build-module/components/rotation-ruler/index.mjs +1 -1
  59. package/build-module/components/rotation-ruler/index.mjs.map +1 -1
  60. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -1
  61. package/build-module/image-editor/core/camera.mjs +12 -0
  62. package/build-module/image-editor/core/camera.mjs.map +2 -2
  63. package/build-module/image-editor/core/constants.mjs +8 -2
  64. package/build-module/image-editor/core/constants.mjs.map +2 -2
  65. package/build-module/image-editor/core/containment.mjs.map +1 -1
  66. package/build-module/image-editor/core/crop-rect.mjs.map +1 -1
  67. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -1
  68. package/build-module/image-editor/core/interaction-controller.mjs +9 -7
  69. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  70. package/build-module/image-editor/core/math/sanitize.mjs.map +1 -1
  71. package/build-module/image-editor/core/setter-helpers.mjs.map +1 -1
  72. package/build-module/image-editor/core/source-region.mjs +108 -1
  73. package/build-module/image-editor/core/source-region.mjs.map +2 -2
  74. package/build-module/image-editor/core/state.mjs.map +1 -1
  75. package/build-module/image-editor/core/stencil-math.mjs +3 -4
  76. package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
  77. package/build-module/image-editor/core/transform-style.mjs.map +1 -1
  78. package/build-module/image-editor/react/components/cropper.mjs +242 -57
  79. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  80. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -1
  81. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +1 -0
  82. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
  83. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +65 -19
  84. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  85. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -1
  86. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +1 -1
  87. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +2 -2
  88. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +1 -1
  89. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -1
  90. package/build-module/index.mjs.map +1 -1
  91. package/build-module/state/types.mjs.map +1 -1
  92. package/build-module/state/use-media-editor-state.mjs +1 -1
  93. package/build-module/state/use-media-editor-state.mjs.map +2 -2
  94. package/build-style/style-rtl.css +49 -6
  95. package/build-style/style.css +49 -6
  96. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  97. package/build-types/image-editor/core/camera.d.ts +25 -1
  98. package/build-types/image-editor/core/camera.d.ts.map +1 -1
  99. package/build-types/image-editor/core/constants.d.ts +20 -1
  100. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  101. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  102. package/build-types/image-editor/core/source-region.d.ts +23 -1
  103. package/build-types/image-editor/core/source-region.d.ts.map +1 -1
  104. package/build-types/image-editor/core/stencil-math.d.ts +10 -5
  105. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
  106. package/build-types/image-editor/core/types.d.ts +14 -0
  107. package/build-types/image-editor/core/types.d.ts.map +1 -1
  108. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  109. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
  110. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +17 -14
  111. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  112. package/package.json +25 -20
  113. package/src/components/media-editor/style.scss +21 -9
  114. package/src/components/media-editor-canvas/index.tsx +82 -13
  115. package/src/components/media-editor-canvas/style.scss +41 -0
  116. package/src/image-editor/core/camera.ts +60 -1
  117. package/src/image-editor/core/constants.ts +23 -1
  118. package/src/image-editor/core/interaction-controller.ts +25 -11
  119. package/src/image-editor/core/source-region.ts +202 -1
  120. package/src/image-editor/core/stencil-math.ts +23 -14
  121. package/src/image-editor/core/test/camera.ts +272 -1
  122. package/src/image-editor/core/test/interaction-controller.ts +100 -0
  123. package/src/image-editor/core/test/stencil-math.ts +120 -6
  124. package/src/image-editor/core/types.ts +17 -0
  125. package/src/image-editor/react/components/cropper.tsx +362 -72
  126. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +1 -0
  127. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +101 -32
  128. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +169 -17
  129. package/src/image-editor/react/components/test/cropper.tsx +689 -6
  130. package/tsconfig.tsbuildinfo +1 -1
  131. package/build-types/components/media-editor-toolbar/index.d.ts +0 -24
  132. package/build-types/components/media-editor-toolbar/index.d.ts.map +0 -1
  133. package/build-types/components/media-editor-transform-controls/index.d.ts +0 -21
  134. package/build-types/components/media-editor-transform-controls/index.d.ts.map +0 -1
  135. package/build-types/components/media-form/sidebar-datetime-view.d.ts +0 -10
  136. package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +0 -1
  137. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -108
  138. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
@@ -25,6 +25,7 @@ import {
25
25
  type HandlePosition,
26
26
  type CropBounds,
27
27
  type ResizeDragState,
28
+ type ResizeDriverAxis,
28
29
  } from '../../../core/stencil-math';
29
30
  import { VISUALLY_HIDDEN_STYLE } from '../../visually-hidden-style';
30
31
 
@@ -91,19 +92,22 @@ type RectangleStencilProps = StencilProps;
91
92
  * crop pass through to the container for image panning. The crop
92
93
  * auto-centers after resize via SETTLE_CROP.
93
94
  *
94
- * @param props Component props implementing StencilProps.
95
- * @param props.cropRect The crop rectangle in normalized coordinates.
96
- * @param props.containerSize The container element dimensions in pixels.
97
- * @param props.imageSize The rendered image dimensions in pixels.
98
- * @param props.onCropChange Callback fired when the crop rect changes.
99
- * @param props.onResizeStart Callback fired when a resize drag starts.
100
- * @param props.onResizeEnd Callback fired when a resize drag ends (mouseup).
101
- * @param props.aspectRatio Optional fixed aspect ratio (width / height).
102
- * @param props.freeformCrop Whether resize handles are shown.
103
- * @param props.stencilTransition CSS transition string for settle animation.
104
- * @param props.cropBounds Maximum crop rect bounds from camera (zoom/rotation-aware).
105
- * @param props.onEscape Called when Escape is pressed on a resize handle.
106
- * @param props.minCropSize Minimum crop rect dimension in normalized space, per axis.
95
+ * @param props Component props implementing StencilProps.
96
+ * @param props.cropRect The crop rectangle in normalized coordinates.
97
+ * @param props.containerSize The container element dimensions in pixels.
98
+ * @param props.imageSize The rendered image dimensions in pixels.
99
+ * @param props.onCropChange Callback fired when the crop rect changes.
100
+ * @param props.onResizeStart Callback fired when a resize drag starts.
101
+ * @param props.onResizeEnd Callback fired when a resize drag ends (mouseup).
102
+ * @param props.aspectRatio Optional fixed aspect ratio (width / height).
103
+ * @param props.freeformCrop Whether resize handles are shown.
104
+ * @param props.isResizeDisabled Whether resize handles should ignore pointer and keyboard input.
105
+ * @param props.stencilTransition CSS transition string for settle animation.
106
+ * @param props.cropBounds Maximum crop rect bounds from camera (zoom/rotation-aware).
107
+ * @param props.onEscape Called when Escape is pressed on a resize handle.
108
+ * @param props.minCropSize Minimum crop rect dimension in normalized space, per axis.
109
+ * @param props.snapCropRect Optional post-processor for freeform resize output.
110
+ * @param props.keyboardResizeStep Optional keyboard resize step in normalized space, per axis.
107
111
  * @return The rectangle stencil element.
108
112
  */
109
113
  export function RectangleStencil( {
@@ -115,10 +119,13 @@ export function RectangleStencil( {
115
119
  onResizeEnd,
116
120
  aspectRatio,
117
121
  freeformCrop = false,
122
+ isResizeDisabled = false,
118
123
  stencilTransition,
119
124
  cropBounds,
120
125
  onEscape,
121
126
  minCropSize,
127
+ snapCropRect,
128
+ keyboardResizeStep,
122
129
  }: RectangleStencilProps ) {
123
130
  // Use cropBounds from the camera if available, otherwise default to [0,1].
124
131
  const boundsMinX = cropBounds?.minX ?? 0;
@@ -138,6 +145,9 @@ export function RectangleStencil( {
138
145
  const keyboardResizeActiveRef = useRef( false );
139
146
  const resizeHandleDescriptionId = useId();
140
147
  const hasLockedRatio = !! ( aspectRatio && aspectRatio > 0 );
148
+ const activePointerResizeRef = useRef< {
149
+ cancel: ( notifyResizeEnd?: boolean ) => void;
150
+ } | null >( null );
141
151
 
142
152
  // Clear the pending keyboard settle timer on unmount so it can't
143
153
  // fire onResizeEnd / dispatch onto an unmounted parent.
@@ -145,9 +155,16 @@ export function RectangleStencil( {
145
155
  return () => {
146
156
  clearTimeout( keyboardSettleTimerRef.current );
147
157
  keyboardResizeActiveRef.current = false;
158
+ activePointerResizeRef.current?.cancel( false );
148
159
  };
149
160
  }, [] );
150
161
 
162
+ useEffect( () => {
163
+ if ( isResizeDisabled ) {
164
+ activePointerResizeRef.current?.cancel();
165
+ }
166
+ }, [ isResizeDisabled ] );
167
+
151
168
  // Latest callbacks for the drag listeners. The drag closure in
152
169
  // handlePointerDown reads from this ref so it always sees current
153
170
  // props without having to re-attach listeners mid-drag. Previously,
@@ -159,7 +176,8 @@ export function RectangleStencil( {
159
176
  computeLockedRect: (
160
177
  drag: ResizeDragState,
161
178
  clientX: number,
162
- clientY: number
179
+ clientY: number,
180
+ driverAxis?: ResizeDriverAxis
163
181
  ) => NormalizedRect;
164
182
  computeFreeRect: (
165
183
  drag: ResizeDragState,
@@ -173,6 +191,10 @@ export function RectangleStencil( {
173
191
  ) => NormalizedRect;
174
192
  onCropChange: ( rect: NormalizedRect ) => void;
175
193
  onResizeEnd?: () => void;
194
+ snapCropRect?: (
195
+ rect: NormalizedRect,
196
+ handle: HandlePosition
197
+ ) => NormalizedRect;
176
198
  } | null >( null );
177
199
 
178
200
  // The normalized aspect ratio: the w/h ratio in normalized space that
@@ -202,6 +224,14 @@ export function RectangleStencil( {
202
224
  */
203
225
  const handlePointerDown = useCallback(
204
226
  ( handle: HandlePosition, event: React.PointerEvent ) => {
227
+ if (
228
+ isResizeDisabled ||
229
+ ( event.pointerType === 'touch' && event.isPrimary === false )
230
+ ) {
231
+ event.preventDefault();
232
+ event.stopPropagation();
233
+ return;
234
+ }
205
235
  if ( event.button !== 0 ) {
206
236
  return;
207
237
  }
@@ -258,6 +288,8 @@ export function RectangleStencil( {
258
288
  );
259
289
  } else {
260
290
  newRect = h.computeFreeRect( drag, latestX, latestY );
291
+ newRect =
292
+ h.snapCropRect?.( newRect, drag.handle ) ?? newRect;
261
293
  }
262
294
  h.onCropChange( newRect );
263
295
  } );
@@ -266,7 +298,10 @@ export function RectangleStencil( {
266
298
  // Guard against duplicate firing: pointerup and
267
299
  // lostpointercapture both fire on normal release.
268
300
  let ended = false;
269
- const onEnd = () => {
301
+ const endResize = (
302
+ notifyResizeEnd = true,
303
+ restoreFocus = true
304
+ ) => {
270
305
  if ( ended ) {
271
306
  return;
272
307
  }
@@ -278,17 +313,26 @@ export function RectangleStencil( {
278
313
  el.removeEventListener( 'pointermove', onMove );
279
314
  el.removeEventListener( 'pointerup', onEnd );
280
315
  el.removeEventListener( 'lostpointercapture', onEnd );
281
- latestHandlersRef.current?.onResizeEnd?.();
316
+ activePointerResizeRef.current = null;
317
+ if ( notifyResizeEnd ) {
318
+ latestHandlersRef.current?.onResizeEnd?.();
319
+ }
282
320
  // Restore focus to the handle so arrow keys work
283
321
  // immediately after a mouse drag. Browsers suppress
284
322
  // :focus-visible after pointer interactions, so the
285
323
  // focus ring stays hidden until the user presses a key.
286
- el.focus( { preventScroll: true } );
324
+ if ( restoreFocus ) {
325
+ el.focus( { preventScroll: true } );
326
+ }
287
327
  };
328
+ const cancelResize = ( notifyResizeEnd = true ) =>
329
+ endResize( notifyResizeEnd, false );
330
+ const onEnd = () => endResize();
288
331
 
289
332
  el.addEventListener( 'pointermove', onMove );
290
333
  el.addEventListener( 'pointerup', onEnd );
291
334
  el.addEventListener( 'lostpointercapture', onEnd );
335
+ activePointerResizeRef.current = { cancel: cancelResize };
292
336
 
293
337
  onResizeStart?.( handle );
294
338
  // Cancel any pending keyboard settle so it can't fire onResizeEnd
@@ -297,7 +341,7 @@ export function RectangleStencil( {
297
341
  clearTimeout( keyboardSettleTimerRef.current );
298
342
  keyboardResizeActiveRef.current = false;
299
343
  },
300
- [ cropRect, onResizeStart ]
344
+ [ cropRect, isResizeDisabled, onResizeStart ]
301
345
  );
302
346
 
303
347
  /**
@@ -329,7 +373,8 @@ export function RectangleStencil( {
329
373
  (
330
374
  drag: ResizeDragState,
331
375
  clientX: number,
332
- clientY: number
376
+ clientY: number,
377
+ driverAxis?: ResizeDriverAxis
333
378
  ): NormalizedRect =>
334
379
  computeLockedResizeRect(
335
380
  drag,
@@ -338,7 +383,8 @@ export function RectangleStencil( {
338
383
  imageSize,
339
384
  bounds,
340
385
  normalizedRatio,
341
- minCropSize
386
+ minCropSize,
387
+ driverAxis
342
388
  ),
343
389
  [ imageSize, bounds, normalizedRatio, minCropSize ]
344
390
  );
@@ -371,6 +417,7 @@ export function RectangleStencil( {
371
417
  computeShiftLockedRect,
372
418
  onCropChange,
373
419
  onResizeEnd,
420
+ snapCropRect,
374
421
  };
375
422
 
376
423
  /**
@@ -382,6 +429,10 @@ export function RectangleStencil( {
382
429
  ( handle: HandlePosition, event: React.KeyboardEvent ) => {
383
430
  const key = event.key;
384
431
 
432
+ if ( isResizeDisabled ) {
433
+ return;
434
+ }
435
+
385
436
  if ( key === 'Escape' ) {
386
437
  event.preventDefault();
387
438
  event.stopPropagation();
@@ -414,25 +465,31 @@ export function RectangleStencil( {
414
465
  }, KEYBOARD_SETTLE_DELAY );
415
466
  };
416
467
 
417
- const step = event.shiftKey
418
- ? DEFAULT_KEYBOARD_STEP * KEYBOARD_SHIFT_STEP_MULTIPLIER
419
- : DEFAULT_KEYBOARD_STEP;
468
+ const stepMultiplier = event.shiftKey
469
+ ? KEYBOARD_SHIFT_STEP_MULTIPLIER
470
+ : 1;
471
+ const stepX = keyboardResizeStep?.width ?? DEFAULT_KEYBOARD_STEP;
472
+ const stepY = keyboardResizeStep?.height ?? DEFAULT_KEYBOARD_STEP;
473
+ const adjustedStepX = stepX * stepMultiplier;
474
+ const adjustedStepY = stepY * stepMultiplier;
420
475
 
421
476
  // Determine the normalized delta from the arrow key.
422
477
  let dx = 0;
423
478
  let dy = 0;
424
479
  if ( key === 'ArrowLeft' ) {
425
- dx = -step;
480
+ dx = -adjustedStepX;
426
481
  }
427
482
  if ( key === 'ArrowRight' ) {
428
- dx = step;
483
+ dx = adjustedStepX;
429
484
  }
430
485
  if ( key === 'ArrowUp' ) {
431
- dy = -step;
486
+ dy = -adjustedStepY;
432
487
  }
433
488
  if ( key === 'ArrowDown' ) {
434
- dy = step;
489
+ dy = adjustedStepY;
435
490
  }
491
+ const keyboardDriverAxis: ResizeDriverAxis =
492
+ dx !== 0 ? 'width' : 'height';
436
493
 
437
494
  if ( hasLockedRatio ) {
438
495
  // For locked aspect ratio, synthesize a drag from the
@@ -446,7 +503,12 @@ export function RectangleStencil( {
446
503
  const clientX = dx * imageSize.width;
447
504
  const clientY = dy * imageSize.height;
448
505
  onCropChange(
449
- computeLockedRect( syntheticDrag, clientX, clientY )
506
+ computeLockedRect(
507
+ syntheticDrag,
508
+ clientX,
509
+ clientY,
510
+ keyboardDriverAxis
511
+ )
450
512
  );
451
513
  scheduleKeyboardResizeEnd();
452
514
  } else {
@@ -459,9 +521,8 @@ export function RectangleStencil( {
459
521
  };
460
522
  const clientX = dx * imageSize.width;
461
523
  const clientY = dy * imageSize.height;
462
- onCropChange(
463
- computeFreeRect( syntheticDrag, clientX, clientY )
464
- );
524
+ const rect = computeFreeRect( syntheticDrag, clientX, clientY );
525
+ onCropChange( snapCropRect?.( rect, handle ) ?? rect );
465
526
  scheduleKeyboardResizeEnd();
466
527
  }
467
528
  },
@@ -476,6 +537,9 @@ export function RectangleStencil( {
476
537
  onResizeStart,
477
538
  onResizeEnd,
478
539
  onEscape,
540
+ keyboardResizeStep,
541
+ snapCropRect,
542
+ isResizeDisabled,
479
543
  ]
480
544
  );
481
545
 
@@ -488,6 +552,7 @@ export function RectangleStencil( {
488
552
  return (
489
553
  <div
490
554
  className="wp-media-editor-image-editor__stencil"
555
+ data-testid="cropper-stencil"
491
556
  style={ {
492
557
  left,
493
558
  top,
@@ -532,7 +597,11 @@ export function RectangleStencil( {
532
597
  onPointerDown={ ( event ) =>
533
598
  handlePointerDown( pos, event )
534
599
  }
535
- onTouchStart={ ( event ) => event.stopPropagation() }
600
+ onTouchStart={ ( event ) => {
601
+ if ( event.touches.length < 2 ) {
602
+ event.stopPropagation();
603
+ }
604
+ } }
536
605
  onKeyDown={ ( event ) => handleKeyDown( pos, event ) }
537
606
  aria-label={ getHandleLabel( pos ) }
538
607
  aria-describedby={ resizeHandleDescriptionId }
@@ -8,6 +8,7 @@ import { render, screen, fireEvent, act } from '@testing-library/react';
8
8
  */
9
9
  import { RectangleStencil } from '../rectangle-stencil';
10
10
  import type { NormalizedRect, Size } from '../../../../core/types';
11
+ import { DEFAULT_KEYBOARD_STEP } from '../../../../core/constants';
11
12
 
12
13
  const DEFAULT_CROP_RECT: NormalizedRect = {
13
14
  x: 0.1,
@@ -32,23 +33,29 @@ function renderStencil(
32
33
  const onResizeStart = jest.fn();
33
34
  const onResizeEnd = jest.fn();
34
35
  const onEscape = jest.fn();
35
-
36
- render(
37
- <RectangleStencil
38
- cropRect={ DEFAULT_CROP_RECT }
39
- containerSize={ CONTAINER_SIZE }
40
- imageSize={ IMAGE_SIZE }
41
- onCropChange={ onCropChange }
42
- onResizeStart={ onResizeStart }
43
- onResizeEnd={ onResizeEnd }
44
- onEscape={ onEscape }
45
- freeformCrop
46
- cropBounds={ CROP_BOUNDS }
47
- { ...overrides }
48
- />
49
- );
50
-
51
- return { onCropChange, onResizeStart, onResizeEnd, onEscape };
36
+ const props = {
37
+ cropRect: DEFAULT_CROP_RECT,
38
+ containerSize: CONTAINER_SIZE,
39
+ imageSize: IMAGE_SIZE,
40
+ onCropChange,
41
+ onResizeStart,
42
+ onResizeEnd,
43
+ onEscape,
44
+ freeformCrop: true,
45
+ cropBounds: CROP_BOUNDS,
46
+ ...overrides,
47
+ };
48
+
49
+ const utils = render( <RectangleStencil { ...props } /> );
50
+
51
+ return {
52
+ ...utils,
53
+ props,
54
+ onCropChange,
55
+ onResizeStart,
56
+ onResizeEnd,
57
+ onEscape,
58
+ };
52
59
  }
53
60
 
54
61
  describe( 'RectangleStencil', () => {
@@ -212,6 +219,66 @@ describe( 'RectangleStencil', () => {
212
219
  expect( newRect.height ).toBeCloseTo( 0.81, 5 );
213
220
  expect( newRect.y ).toBeCloseTo( 0.1, 5 );
214
221
  } );
222
+
223
+ it( 'applies snapCropRect to freeform resize output', () => {
224
+ const snapCropRect = jest.fn( ( rect: NormalizedRect ) => ( {
225
+ ...rect,
226
+ width: 0.82,
227
+ } ) );
228
+ const { onCropChange } = renderStencil( { snapCropRect } );
229
+ const eHandle = screen.getAllByRole( 'button' )[ 3 ];
230
+
231
+ fireEvent.keyDown( eHandle, {
232
+ key: 'ArrowRight',
233
+ shiftKey: false,
234
+ } );
235
+
236
+ expect( snapCropRect ).toHaveBeenCalledTimes( 1 );
237
+ expect( snapCropRect.mock.calls[ 0 ][ 0 ].width ).toBeCloseTo(
238
+ 0.81,
239
+ 5
240
+ );
241
+ expect( snapCropRect.mock.calls[ 0 ][ 1 ] ).toBe( 'e' );
242
+ expect( onCropChange.mock.calls[ 0 ][ 0 ].width ).toBeCloseTo(
243
+ 0.82,
244
+ 5
245
+ );
246
+ } );
247
+
248
+ it( 'shrinks a locked-ratio crop from a corner handle', () => {
249
+ const { onCropChange } = renderStencil( {
250
+ aspectRatio: 1,
251
+ containerSize: { width: 500, height: 500 },
252
+ imageSize: { width: 500, height: 500 },
253
+ } );
254
+ const nwHandle = screen.getByRole( 'button', {
255
+ name: 'Resize top-left corner',
256
+ } );
257
+
258
+ fireEvent.keyDown( nwHandle, {
259
+ key: 'ArrowRight',
260
+ shiftKey: false,
261
+ } );
262
+
263
+ expect( onCropChange ).toHaveBeenCalledTimes( 1 );
264
+ const newRect: NormalizedRect = onCropChange.mock.calls[ 0 ][ 0 ];
265
+ expect( newRect.x ).toBeCloseTo(
266
+ DEFAULT_CROP_RECT.x + DEFAULT_KEYBOARD_STEP,
267
+ 5
268
+ );
269
+ expect( newRect.y ).toBeCloseTo(
270
+ DEFAULT_CROP_RECT.y + DEFAULT_KEYBOARD_STEP,
271
+ 5
272
+ );
273
+ expect( newRect.width ).toBeCloseTo(
274
+ DEFAULT_CROP_RECT.width - DEFAULT_KEYBOARD_STEP,
275
+ 5
276
+ );
277
+ expect( newRect.height ).toBeCloseTo(
278
+ DEFAULT_CROP_RECT.height - DEFAULT_KEYBOARD_STEP,
279
+ 5
280
+ );
281
+ } );
215
282
  } );
216
283
 
217
284
  describe( 'keyboard — arrow keys (coarse step with Shift)', () => {
@@ -247,6 +314,21 @@ describe( 'RectangleStencil', () => {
247
314
 
248
315
  expect( coarseDelta / fineDelta ).toBeCloseTo( 10, 0 );
249
316
  } );
317
+
318
+ it( 'does not apply snapCropRect while resizing a locked aspect ratio', () => {
319
+ const snapCropRect = jest.fn( ( rect: NormalizedRect ) => rect );
320
+ renderStencil( { aspectRatio: 1, snapCropRect } );
321
+ const nwHandle = screen.getByRole( 'button', {
322
+ name: 'Resize top-left corner',
323
+ } );
324
+
325
+ fireEvent.keyDown( nwHandle, {
326
+ key: 'ArrowRight',
327
+ shiftKey: false,
328
+ } );
329
+
330
+ expect( snapCropRect ).not.toHaveBeenCalled();
331
+ } );
250
332
  } );
251
333
 
252
334
  describe( 'pointer drag — focus after release', () => {
@@ -287,5 +369,75 @@ describe( 'RectangleStencil', () => {
287
369
 
288
370
  expect( firstHandle.focus ).toHaveBeenCalled();
289
371
  } );
372
+
373
+ it( 'does not start a pointer resize while resizing is disabled', () => {
374
+ const { onResizeStart } = renderStencil( {
375
+ isResizeDisabled: true,
376
+ } );
377
+ const [ firstHandle ] = screen.getAllByRole( 'button' );
378
+
379
+ fireEvent.pointerDown( firstHandle, {
380
+ button: 0,
381
+ clientX: 100,
382
+ clientY: 100,
383
+ pointerId: 1,
384
+ } );
385
+
386
+ expect( onResizeStart ).not.toHaveBeenCalled();
387
+ } );
388
+
389
+ it( 'cancels an active pointer resize when resizing becomes disabled', () => {
390
+ const { props, rerender, onResizeStart, onResizeEnd } =
391
+ renderStencil();
392
+ const [ firstHandle ] = screen.getAllByRole( 'button' );
393
+
394
+ fireEvent.pointerDown( firstHandle, {
395
+ button: 0,
396
+ clientX: 100,
397
+ clientY: 100,
398
+ pointerId: 1,
399
+ } );
400
+
401
+ expect( onResizeStart ).toHaveBeenCalledTimes( 1 );
402
+ expect( onResizeEnd ).not.toHaveBeenCalled();
403
+
404
+ rerender( <RectangleStencil { ...props } isResizeDisabled /> );
405
+
406
+ expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
407
+
408
+ fireEvent.pointerUp( firstHandle, { pointerId: 1 } );
409
+
410
+ expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
411
+ } );
412
+
413
+ it( 'only stops touchstart propagation for single-touch handle gestures', () => {
414
+ const onTouchStart = jest.fn();
415
+ render(
416
+ <div onTouchStart={ onTouchStart }>
417
+ <RectangleStencil
418
+ cropRect={ DEFAULT_CROP_RECT }
419
+ containerSize={ CONTAINER_SIZE }
420
+ imageSize={ IMAGE_SIZE }
421
+ onCropChange={ jest.fn() }
422
+ freeformCrop
423
+ cropBounds={ CROP_BOUNDS }
424
+ />
425
+ </div>
426
+ );
427
+ const [ firstHandle ] = screen.getAllByRole( 'button' );
428
+
429
+ fireEvent.touchStart( firstHandle, {
430
+ touches: [ { clientX: 100, clientY: 100 } ],
431
+ } );
432
+ expect( onTouchStart ).not.toHaveBeenCalled();
433
+
434
+ fireEvent.touchStart( firstHandle, {
435
+ touches: [
436
+ { clientX: 100, clientY: 100 },
437
+ { clientX: 160, clientY: 100 },
438
+ ],
439
+ } );
440
+ expect( onTouchStart ).toHaveBeenCalledTimes( 1 );
441
+ } );
290
442
  } );
291
443
  } );