@wordpress/media-editor 0.11.0 → 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 (179) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/build/components/media-editor/index.cjs +41 -51
  3. package/build/components/media-editor/index.cjs.map +3 -3
  4. package/build/components/media-editor/use-crop-options.cjs.map +1 -1
  5. package/build/components/media-editor-canvas/index.cjs +63 -15
  6. package/build/components/media-editor-canvas/index.cjs.map +3 -3
  7. package/build/components/media-editor-crop-panel/index.cjs +15 -45
  8. package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
  9. package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
  10. package/build/components/media-editor-image-controls/index.cjs +230 -0
  11. package/build/components/media-editor-image-controls/index.cjs.map +7 -0
  12. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +4 -4
  13. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +2 -2
  14. package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -1
  15. package/build/components/media-form/index.cjs +12 -23
  16. package/build/components/media-form/index.cjs.map +3 -3
  17. package/build/components/rotation-ruler/index.cjs +1 -1
  18. package/build/components/rotation-ruler/index.cjs.map +1 -1
  19. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -1
  20. package/build/image-editor/core/camera.cjs +13 -0
  21. package/build/image-editor/core/camera.cjs.map +2 -2
  22. package/build/image-editor/core/constants.cjs +15 -3
  23. package/build/image-editor/core/constants.cjs.map +2 -2
  24. package/build/image-editor/core/containment.cjs.map +1 -1
  25. package/build/image-editor/core/crop-rect.cjs.map +1 -1
  26. package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -1
  27. package/build/image-editor/core/interaction-controller.cjs +9 -7
  28. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  29. package/build/image-editor/core/math/sanitize.cjs.map +1 -1
  30. package/build/image-editor/core/setter-helpers.cjs.map +1 -1
  31. package/build/image-editor/core/source-region.cjs +111 -2
  32. package/build/image-editor/core/source-region.cjs.map +2 -2
  33. package/build/image-editor/core/state.cjs.map +1 -1
  34. package/build/image-editor/core/stencil-math.cjs +14 -6
  35. package/build/image-editor/core/stencil-math.cjs.map +2 -2
  36. package/build/image-editor/core/transform-style.cjs.map +1 -1
  37. package/build/image-editor/core/types.cjs.map +1 -1
  38. package/build/image-editor/react/components/cropper.cjs +231 -49
  39. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  40. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -1
  41. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +1 -0
  42. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
  43. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +65 -19
  44. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  45. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -1
  46. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +1 -1
  47. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +2 -2
  48. package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -1
  49. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -1
  50. package/build/index.cjs.map +1 -1
  51. package/build/state/types.cjs.map +1 -1
  52. package/build/state/use-media-editor-state.cjs +1 -1
  53. package/build/state/use-media-editor-state.cjs.map +2 -2
  54. package/build-module/components/media-editor/index.mjs +41 -51
  55. package/build-module/components/media-editor/index.mjs.map +2 -2
  56. package/build-module/components/media-editor/use-crop-options.mjs.map +1 -1
  57. package/build-module/components/media-editor-canvas/index.mjs +55 -17
  58. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  59. package/build-module/components/media-editor-crop-panel/index.mjs +8 -51
  60. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  61. package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
  62. package/build-module/components/media-editor-image-controls/index.mjs +221 -0
  63. package/build-module/components/media-editor-image-controls/index.mjs.map +7 -0
  64. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +4 -4
  65. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +2 -2
  66. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +1 -1
  67. package/build-module/components/media-form/index.mjs +12 -13
  68. package/build-module/components/media-form/index.mjs.map +2 -2
  69. package/build-module/components/rotation-ruler/index.mjs +1 -1
  70. package/build-module/components/rotation-ruler/index.mjs.map +1 -1
  71. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -1
  72. package/build-module/image-editor/core/camera.mjs +12 -0
  73. package/build-module/image-editor/core/camera.mjs.map +2 -2
  74. package/build-module/image-editor/core/constants.mjs +10 -2
  75. package/build-module/image-editor/core/constants.mjs.map +2 -2
  76. package/build-module/image-editor/core/containment.mjs.map +1 -1
  77. package/build-module/image-editor/core/crop-rect.mjs.map +1 -1
  78. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -1
  79. package/build-module/image-editor/core/interaction-controller.mjs +9 -7
  80. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  81. package/build-module/image-editor/core/math/sanitize.mjs.map +1 -1
  82. package/build-module/image-editor/core/setter-helpers.mjs.map +1 -1
  83. package/build-module/image-editor/core/source-region.mjs +108 -1
  84. package/build-module/image-editor/core/source-region.mjs.map +2 -2
  85. package/build-module/image-editor/core/state.mjs.map +1 -1
  86. package/build-module/image-editor/core/stencil-math.mjs +12 -5
  87. package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
  88. package/build-module/image-editor/core/transform-style.mjs.map +1 -1
  89. package/build-module/image-editor/react/components/cropper.mjs +242 -52
  90. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  91. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -1
  92. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +1 -0
  93. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
  94. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +65 -19
  95. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  96. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -1
  97. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +1 -1
  98. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +2 -2
  99. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +1 -1
  100. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -1
  101. package/build-module/index.mjs.map +1 -1
  102. package/build-module/state/types.mjs.map +1 -1
  103. package/build-module/state/use-media-editor-state.mjs +1 -1
  104. package/build-module/state/use-media-editor-state.mjs.map +2 -2
  105. package/build-style/style-rtl.css +179 -47
  106. package/build-style/style.css +179 -46
  107. package/build-types/components/media-editor/index.d.ts +6 -7
  108. package/build-types/components/media-editor/index.d.ts.map +1 -1
  109. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  110. package/build-types/components/media-editor-crop-panel/index.d.ts +11 -6
  111. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  112. package/build-types/components/media-editor-fine-rotation/index.d.ts +4 -3
  113. package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
  114. package/build-types/components/media-editor-image-controls/index.d.ts +46 -0
  115. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -0
  116. package/build-types/components/media-form/index.d.ts.map +1 -1
  117. package/build-types/image-editor/core/camera.d.ts +25 -1
  118. package/build-types/image-editor/core/camera.d.ts.map +1 -1
  119. package/build-types/image-editor/core/constants.d.ts +30 -1
  120. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  121. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  122. package/build-types/image-editor/core/source-region.d.ts +23 -1
  123. package/build-types/image-editor/core/source-region.d.ts.map +1 -1
  124. package/build-types/image-editor/core/stencil-math.d.ts +22 -4
  125. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
  126. package/build-types/image-editor/core/types.d.ts +14 -0
  127. package/build-types/image-editor/core/types.d.ts.map +1 -1
  128. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  129. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
  130. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +17 -14
  131. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  132. package/package.json +30 -20
  133. package/src/components/media-editor/index.tsx +69 -77
  134. package/src/components/media-editor/style.scss +136 -4
  135. package/src/components/media-editor-canvas/index.tsx +82 -13
  136. package/src/components/media-editor-canvas/style.scss +41 -0
  137. package/src/components/media-editor-crop-panel/index.tsx +16 -62
  138. package/src/components/media-editor-crop-panel/test/index.tsx +25 -41
  139. package/src/components/media-editor-fine-rotation/index.tsx +4 -3
  140. package/src/components/media-editor-image-controls/index.tsx +262 -0
  141. package/src/components/media-editor-image-controls/style.scss +48 -0
  142. package/src/components/media-editor-image-controls/test/index.tsx +150 -0
  143. package/src/components/media-editor-modal/style.scss +32 -42
  144. package/src/components/media-form/index.tsx +17 -21
  145. package/src/components/media-form/style.scss +6 -0
  146. package/src/components/rotation-ruler/style.scss +10 -3
  147. package/src/image-editor/core/camera.ts +60 -1
  148. package/src/image-editor/core/constants.ts +34 -1
  149. package/src/image-editor/core/interaction-controller.ts +25 -11
  150. package/src/image-editor/core/source-region.ts +202 -1
  151. package/src/image-editor/core/stencil-math.ts +42 -10
  152. package/src/image-editor/core/test/camera.ts +272 -1
  153. package/src/image-editor/core/test/interaction-controller.ts +100 -0
  154. package/src/image-editor/core/test/stencil-math.ts +153 -0
  155. package/src/image-editor/core/types.ts +17 -0
  156. package/src/image-editor/react/components/cropper.scss +28 -17
  157. package/src/image-editor/react/components/cropper.tsx +362 -62
  158. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +1 -0
  159. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +101 -32
  160. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +169 -17
  161. package/src/image-editor/react/components/test/cropper.tsx +689 -6
  162. package/src/style.scss +2 -1
  163. package/tsconfig.tsbuildinfo +1 -1
  164. package/build/components/media-editor-transform-controls/index.cjs +0 -84
  165. package/build/components/media-editor-transform-controls/index.cjs.map +0 -7
  166. package/build/components/media-form/sidebar-datetime-view.cjs +0 -53
  167. package/build/components/media-form/sidebar-datetime-view.cjs.map +0 -7
  168. package/build-module/components/media-editor-transform-controls/index.mjs +0 -68
  169. package/build-module/components/media-editor-transform-controls/index.mjs.map +0 -7
  170. package/build-module/components/media-form/sidebar-datetime-view.mjs +0 -32
  171. package/build-module/components/media-form/sidebar-datetime-view.mjs.map +0 -7
  172. package/build-types/components/media-editor-transform-controls/index.d.ts +0 -8
  173. package/build-types/components/media-editor-transform-controls/index.d.ts.map +0 -1
  174. package/build-types/components/media-form/sidebar-datetime-view.d.ts +0 -10
  175. package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +0 -1
  176. package/src/components/media-editor-transform-controls/index.tsx +0 -70
  177. package/src/components/media-editor-transform-controls/style.scss +0 -8
  178. package/src/components/media-form/sidebar-datetime-view.tsx +0 -47
  179. package/src/components/media-form/test/sidebar-datetime-view.test.tsx +0 -96
@@ -30,10 +30,15 @@ import type {
30
30
  NormalizedRect,
31
31
  } from '../../core/types';
32
32
  import type { CropperController } from '../hooks/use-cropper-reducer';
33
- import { getImageFit, getRotatedBBox } from '../../core/camera';
33
+ import { getImageFit, getRotatedBBox, getViewScale } from '../../core/camera';
34
34
  import { getImageCropBounds, getMinZoom } from '../../core/containment';
35
- import { MIN_CROP_PIXELS } from '../../core/constants';
35
+ import {
36
+ MAX_VIEW_SCALE,
37
+ PIXEL_SNAP_DISPLAY_SCALE,
38
+ SETTLE_TARGET_CANVAS_FILL,
39
+ } from '../../core/constants';
36
40
  import { computeInscribedRect } from '../../core/crop-rect';
41
+ import { getMinCropPixels } from '../../core/stencil-math';
37
42
  import { useInteraction } from '../hooks/use-interaction';
38
43
  import { useTransformStyle } from '../hooks/use-transform-style';
39
44
  import { useAriaAnnouncer } from '../hooks/use-aria-announcer';
@@ -41,12 +46,31 @@ import { RectangleStencil } from './stencils/rectangle-stencil';
41
46
  import { DimmingOverlay } from './overlays/dimming-overlay';
42
47
  import { GridOverlay } from './overlays/grid-overlay';
43
48
  import { DimensionsOverlay } from './overlays/dimensions-overlay';
44
- import { getSourceRegion } from '../../core/source-region';
49
+ import {
50
+ getSourceRegion,
51
+ snapCropRectToSourcePixelGrid,
52
+ snapCropRectToSourcePixels,
53
+ } from '../../core/source-region';
45
54
  import { ViewportProvider, useViewport } from './viewport-provider';
46
55
  import { VISUALLY_HIDDEN_STYLE } from '../visually-hidden-style';
47
56
 
48
57
  /** Threshold for comparing normalized crop rect values. */
49
58
  const CROP_RECT_EPSILON = 1e-6;
59
+ /** Threshold for deciding whether rotation is at a 90-degree stop. */
60
+ const CARDINAL_ROTATION_EPSILON = 1e-6;
61
+ /** Duration of the settle transform transition after resize end. */
62
+ const SETTLE_TRANSITION_DURATION_MS = 200;
63
+ // Fallback timer for when the computed transform is unchanged and no
64
+ // `transitionend` fires. Sits past the CSS duration so it only wins the race
65
+ // when the transition genuinely never runs.
66
+ const SETTLE_TRANSITION_FALLBACK_MS = SETTLE_TRANSITION_DURATION_MS + 100;
67
+ const SETTLE_TRANSFORM_TRANSITION = `transform ${ SETTLE_TRANSITION_DURATION_MS }ms ease-out`;
68
+ const SETTLE_STENCIL_TRANSITION = [
69
+ `left ${ SETTLE_TRANSITION_DURATION_MS }ms ease-out`,
70
+ `top ${ SETTLE_TRANSITION_DURATION_MS }ms ease-out`,
71
+ `width ${ SETTLE_TRANSITION_DURATION_MS }ms ease-out`,
72
+ `height ${ SETTLE_TRANSITION_DURATION_MS }ms ease-out`,
73
+ ].join( ', ' );
50
74
 
51
75
  /**
52
76
  * Props for the Cropper component.
@@ -268,32 +292,6 @@ function CropperInner(
268
292
  [ canvasSize, naturalWidth, naturalHeight, state.rotation ]
269
293
  );
270
294
 
271
- // Per-axis minimum crop size in normalized space, expressing a
272
- // pixel floor on the captured source region. cropRect is normalized
273
- // in the viewport's snap-rotation bbox; the captured source-pixel
274
- // width is `cropRect.width * bbox.width / zoom`, so the normalized
275
- // floor scales with `zoom` to keep the source-pixel floor constant.
276
- // Without this, SETTLE_CROP zooms in proportional to the shrink and
277
- // successive drags can crop arbitrarily small.
278
- const minCropSize: Size | undefined = useMemo( () => {
279
- if ( naturalWidth <= 0 || naturalHeight <= 0 ) {
280
- return undefined;
281
- }
282
- const snapRotation = Math.round( state.rotation / 90 ) * 90;
283
- const bbox = getRotatedBBox(
284
- naturalWidth,
285
- naturalHeight,
286
- snapRotation
287
- );
288
- return {
289
- width: Math.min( 1, ( MIN_CROP_PIXELS * state.zoom ) / bbox.width ),
290
- height: Math.min(
291
- 1,
292
- ( MIN_CROP_PIXELS * state.zoom ) / bbox.height
293
- ),
294
- };
295
- }, [ naturalWidth, naturalHeight, state.rotation, state.zoom ] );
296
-
297
295
  // Report the rendered image size to the controller. Composite
298
296
  // controllers need it to compute aspect-ratio reshapes from the
299
297
  // reducer (the dropdown dispatches without DOM access); pure
@@ -394,6 +392,12 @@ function CropperInner(
394
392
  const [ isResizing, setIsResizing ] = useState( false );
395
393
  const isResizingRef = useRef( false );
396
394
  const isSettlingRef = useRef( false );
395
+ const [ isTouchPinching, setIsTouchPinchingState ] = useState( false );
396
+ const isTouchPinchingRef = useRef( false );
397
+ const setTouchPinching = useCallback( ( isPinching: boolean ) => {
398
+ isTouchPinchingRef.current = isPinching;
399
+ setIsTouchPinchingState( isPinching );
400
+ }, [] );
397
401
  // Direction of the handle the user is currently resizing — pointer or
398
402
  // keyboard. `null` outside of an active resize. Drives the live
399
403
  // dimensions tooltip overlay.
@@ -401,6 +405,185 @@ function CropperInner(
401
405
  null
402
406
  );
403
407
 
408
+ // Presentational magnification so the at-rest crop fills the canvas. The
409
+ // crop overlay is laid out as `cropRect * visualSize` (the contain-fit
410
+ // footprint, no zoom), so a crop whose aspect differs from the image's can
411
+ // render far smaller than the canvas allows. Scaling `elementSize` and
412
+ // `visualSize` together magnifies the whole scene uniformly — image,
413
+ // overlays, and the pixel->normalized interaction math — and the root's
414
+ // `overflow: hidden` clips the image bleed. Magnifying around the footprint
415
+ // centre keeps the crop centred only because the at-rest crop is always
416
+ // centred (SETTLE_CROP, computeInscribedRect, and the initial full-frame
417
+ // crop all centre it).
418
+ const viewScaleRest = useMemo(
419
+ () =>
420
+ getViewScale(
421
+ state.cropRect,
422
+ canvasSize,
423
+ visualSize,
424
+ SETTLE_TARGET_CANVAS_FILL,
425
+ MAX_VIEW_SCALE
426
+ ),
427
+ [ state.cropRect, canvasSize, visualSize ]
428
+ );
429
+ // During a resize the magnification is frozen at its pre-drag value rather
430
+ // than recomputed per pointer move: recomputing live would zoom the scene
431
+ // under the cursor, and snapping to 1 would reset the zoom the moment a
432
+ // handle is grabbed. `handleResizeStart` snapshots the current rest value
433
+ // into `frozenViewScale`; on release the settle recomputes from the new crop.
434
+ const [ frozenViewScale, setFrozenViewScale ] = useState( 1 );
435
+ const viewScale = isResizing ? frozenViewScale : viewScaleRest;
436
+ const scaledVisualSize = useMemo(
437
+ () => ( {
438
+ width: visualSize.width * viewScale,
439
+ height: visualSize.height * viewScale,
440
+ } ),
441
+ [ visualSize.width, visualSize.height, viewScale ]
442
+ );
443
+ // CSS pixels rendered per source pixel: the contain fit
444
+ // (elementSize / natural), times zoom and the view-scale magnification.
445
+ const displayScale =
446
+ naturalWidth > 0
447
+ ? ( elementSize.width / naturalWidth ) * state.zoom * viewScale
448
+ : 0;
449
+ const keyboardResizeStep = useMemo( () => {
450
+ if (
451
+ displayScale < PIXEL_SNAP_DISPLAY_SCALE ||
452
+ ( aspectRatio && aspectRatio > 0 ) ||
453
+ naturalWidth <= 0 ||
454
+ naturalHeight <= 0
455
+ ) {
456
+ return undefined;
457
+ }
458
+ const snapRotation = Math.round( state.rotation / 90 ) * 90;
459
+ if (
460
+ Math.abs( state.rotation - snapRotation ) >=
461
+ CARDINAL_ROTATION_EPSILON
462
+ ) {
463
+ return undefined;
464
+ }
465
+ const bbox = getRotatedBBox(
466
+ naturalWidth,
467
+ naturalHeight,
468
+ snapRotation
469
+ );
470
+ if ( bbox.width <= 0 || bbox.height <= 0 ) {
471
+ return undefined;
472
+ }
473
+ // Keyboard steps are normalized crop-space deltas, but snapping wants
474
+ // one source pixel per key press. The normalized delta for one source
475
+ // pixel is different on each axis when the snap-rotation bbox is not
476
+ // square, so this must be an object with separate width/height steps
477
+ // instead of a single scalar.
478
+ return {
479
+ width: state.zoom / bbox.width,
480
+ height: state.zoom / bbox.height,
481
+ };
482
+ }, [
483
+ displayScale,
484
+ aspectRatio,
485
+ naturalWidth,
486
+ naturalHeight,
487
+ state.rotation,
488
+ state.zoom,
489
+ ] );
490
+
491
+ // Per-axis minimum crop size in normalized space, expressing a pixel floor
492
+ // on the captured source region. cropRect is normalized in the viewport's
493
+ // snap-rotation bbox; the captured source-pixel width is
494
+ // `cropRect.width * bbox.width / zoom`, so the normalized floor scales with
495
+ // `zoom` to keep the source-pixel floor constant. The floor itself adapts to
496
+ // the on-screen display scale (fit × zoom × view scale) via
497
+ // `getMinCropPixels`, so on a large image shown small the crop can't shrink
498
+ // until the handles collapse — it yields to the 24px source floor only once
499
+ // the image is shown large enough.
500
+ const minCropSize: Size | undefined = useMemo( () => {
501
+ if ( naturalWidth <= 0 || naturalHeight <= 0 ) {
502
+ return undefined;
503
+ }
504
+ const snapRotation = Math.round( state.rotation / 90 ) * 90;
505
+ const bbox = getRotatedBBox(
506
+ naturalWidth,
507
+ naturalHeight,
508
+ snapRotation
509
+ );
510
+ const minPixels = getMinCropPixels( displayScale );
511
+ return {
512
+ width: Math.min( 1, ( minPixels * state.zoom ) / bbox.width ),
513
+ height: Math.min( 1, ( minPixels * state.zoom ) / bbox.height ),
514
+ };
515
+ }, [
516
+ naturalWidth,
517
+ naturalHeight,
518
+ state.rotation,
519
+ state.zoom,
520
+ displayScale,
521
+ ] );
522
+
523
+ const snapCropRect = useCallback(
524
+ ( rect: NormalizedRect, handle: HandlePosition ): NormalizedRect => {
525
+ if (
526
+ displayScale < PIXEL_SNAP_DISPLAY_SCALE ||
527
+ naturalWidth <= 0 ||
528
+ naturalHeight <= 0
529
+ ) {
530
+ return rect;
531
+ }
532
+ return snapCropRectToSourcePixels(
533
+ state,
534
+ { width: naturalWidth, height: naturalHeight },
535
+ rect,
536
+ handle
537
+ );
538
+ },
539
+ [ displayScale, naturalWidth, naturalHeight, state ]
540
+ );
541
+
542
+ const wasPixelSnapEnabledRef = useRef( false );
543
+ useEffect( () => {
544
+ const isPixelSnapEnabled =
545
+ freeformCrop &&
546
+ ( ! aspectRatio || aspectRatio <= 0 ) &&
547
+ displayScale >= PIXEL_SNAP_DISPLAY_SCALE &&
548
+ naturalWidth > 0 &&
549
+ naturalHeight > 0;
550
+ const wasPixelSnapEnabled = wasPixelSnapEnabledRef.current;
551
+ wasPixelSnapEnabledRef.current = isPixelSnapEnabled;
552
+
553
+ if ( ! isPixelSnapEnabled || wasPixelSnapEnabled ) {
554
+ return;
555
+ }
556
+
557
+ const snappedCropRect = snapCropRectToSourcePixelGrid(
558
+ state,
559
+ { width: naturalWidth, height: naturalHeight },
560
+ state.cropRect
561
+ );
562
+ const currentCropRect = state.cropRect;
563
+ if (
564
+ Math.abs( currentCropRect.x - snappedCropRect.x ) <
565
+ CROP_RECT_EPSILON &&
566
+ Math.abs( currentCropRect.y - snappedCropRect.y ) <
567
+ CROP_RECT_EPSILON &&
568
+ Math.abs( currentCropRect.width - snappedCropRect.width ) <
569
+ CROP_RECT_EPSILON &&
570
+ Math.abs( currentCropRect.height - snappedCropRect.height ) <
571
+ CROP_RECT_EPSILON
572
+ ) {
573
+ return;
574
+ }
575
+
576
+ setCropRect( snappedCropRect );
577
+ }, [
578
+ aspectRatio,
579
+ displayScale,
580
+ freeformCrop,
581
+ naturalWidth,
582
+ naturalHeight,
583
+ setCropRect,
584
+ state,
585
+ ] );
586
+
404
587
  // Use the interaction hook for mouse, touch, and keyboard events.
405
588
  const {
406
589
  handlers,
@@ -408,7 +591,7 @@ function CropperInner(
408
591
  isDragging,
409
592
  isZooming,
410
593
  isPlacementActive: isInteractionPlacementActive,
411
- } = useInteraction( state, controller, canvasSize, visualSize, {
594
+ } = useInteraction( state, controller, canvasSize, scaledVisualSize, {
412
595
  minZoom: effectiveMinZoom,
413
596
  maxZoom,
414
597
  onGestureStart,
@@ -429,6 +612,26 @@ function CropperInner(
429
612
  onPointerDownCapture: () => {
430
613
  setIsFocusVisible( false );
431
614
  },
615
+ onTouchStartCapture: ( event: React.TouchEvent< HTMLDivElement > ) => {
616
+ if ( event.touches.length > 1 ) {
617
+ setTouchPinching( true );
618
+ }
619
+ },
620
+ onTouchMoveCapture: ( event: React.TouchEvent< HTMLDivElement > ) => {
621
+ if ( event.touches.length > 1 ) {
622
+ setTouchPinching( true );
623
+ }
624
+ },
625
+ onTouchEndCapture: ( event: React.TouchEvent< HTMLDivElement > ) => {
626
+ if ( event.touches.length === 0 ) {
627
+ setTouchPinching( false );
628
+ }
629
+ },
630
+ onTouchCancelCapture: ( event: React.TouchEvent< HTMLDivElement > ) => {
631
+ if ( event.touches.length === 0 ) {
632
+ setTouchPinching( false );
633
+ }
634
+ },
432
635
  onKeyDownCapture: ( event: React.KeyboardEvent< HTMLDivElement > ) => {
433
636
  // Modifier-only keypresses precede another key rather than
434
637
  // indicating deliberate keyboard interaction on their own.
@@ -439,6 +642,14 @@ function CropperInner(
439
642
  }
440
643
  },
441
644
  onPointerDown: ( event: React.PointerEvent< HTMLDivElement > ) => {
645
+ if (
646
+ isResizingRef.current ||
647
+ isTouchPinchingRef.current ||
648
+ ( event.pointerType === 'touch' && event.isPrimary === false )
649
+ ) {
650
+ event.preventDefault();
651
+ return;
652
+ }
442
653
  handlers.onPointerDown?.( event );
443
654
  // Re-assert false after handlers run — el.focus() inside the
444
655
  // handler fires onFocus, which may otherwise set it back to true.
@@ -473,7 +684,10 @@ function CropperInner(
473
684
  };
474
685
  }, [ onWheelNative ] );
475
686
 
476
- // Use the transform style hook for the image CSS transform.
687
+ // Use the unscaled contain-fit footprint here. `imageStyle` prepends
688
+ // `scale(viewScale)` to this matrix, so pan translations are magnified with
689
+ // the rest of the scene; using `scaledVisualSize` here would apply viewScale
690
+ // to pan twice.
477
691
  const transformString = useTransformStyle( state, visualSize );
478
692
 
479
693
  /**
@@ -500,35 +714,40 @@ function CropperInner(
500
714
 
501
715
  const handleCropChange = useCallback(
502
716
  ( rect: NormalizedRect ) => {
717
+ if ( isTouchPinchingRef.current ) {
718
+ return;
719
+ }
503
720
  setCropRect( rect );
504
721
  // During a resize drag, pan the viewport so the handle stays
505
722
  // visible even when the crop extends beyond the canvas edge.
506
723
  if (
507
724
  isResizingRef.current &&
508
- visualSize.width > 0 &&
509
- visualSize.height > 0
725
+ scaledVisualSize.width > 0 &&
726
+ scaledVisualSize.height > 0
510
727
  ) {
511
- const offsetX = ( canvasSize.width - visualSize.width ) / 2;
512
- const offsetY = ( canvasSize.height - visualSize.height ) / 2;
728
+ const offsetX =
729
+ ( canvasSize.width - scaledVisualSize.width ) / 2;
730
+ const offsetY =
731
+ ( canvasSize.height - scaledVisualSize.height ) / 2;
513
732
  const rightOverflow = Math.max(
514
733
  0,
515
734
  offsetX +
516
- ( rect.x + rect.width ) * visualSize.width -
735
+ ( rect.x + rect.width ) * scaledVisualSize.width -
517
736
  canvasSize.width
518
737
  );
519
738
  const leftOverflow = Math.max(
520
739
  0,
521
- -( offsetX + rect.x * visualSize.width )
740
+ -( offsetX + rect.x * scaledVisualSize.width )
522
741
  );
523
742
  const bottomOverflow = Math.max(
524
743
  0,
525
744
  offsetY +
526
- ( rect.y + rect.height ) * visualSize.height -
745
+ ( rect.y + rect.height ) * scaledVisualSize.height -
527
746
  canvasSize.height
528
747
  );
529
748
  const topOverflow = Math.max(
530
749
  0,
531
- -( offsetY + rect.y * visualSize.height )
750
+ -( offsetY + rect.y * scaledVisualSize.height )
532
751
  );
533
752
  setViewportPan( {
534
753
  x: -rightOverflow + leftOverflow,
@@ -536,12 +755,17 @@ function CropperInner(
536
755
  } );
537
756
  }
538
757
  },
539
- [ setCropRect, setViewportPan, canvasSize, visualSize ]
758
+ [ setCropRect, setViewportPan, canvasSize, scaledVisualSize ]
540
759
  );
541
760
 
542
- // Settling animation: brief linear transition after resize end.
761
+ // Settling animation: brief transition after resize end.
543
762
  const [ settling, setSettling ] = useState( false );
544
763
  const settleTimerRef = useRef< ReturnType< typeof setTimeout > >();
764
+ const finishSettling = useCallback( () => {
765
+ clearTimeout( settleTimerRef.current );
766
+ isSettlingRef.current = false;
767
+ setSettling( false );
768
+ }, [] );
545
769
 
546
770
  // Clear the pending settle timer on unmount so it can't fire a
547
771
  // state update on an unmounted component.
@@ -583,19 +807,23 @@ function CropperInner(
583
807
 
584
808
  const handleResizeStart = useCallback(
585
809
  ( handle?: HandlePosition ) => {
810
+ if ( isTouchPinchingRef.current ) {
811
+ return;
812
+ }
813
+ // Freeze the magnification at its current value so grabbing a
814
+ // handle doesn't reset the zoom; it holds for the whole drag.
815
+ setFrozenViewScale( viewScaleRest );
586
816
  isResizingRef.current = true;
587
817
  setIsResizing( true );
588
818
  setActiveHandle( handle ?? null );
589
819
  // Clear any in-flight settle so transitions don't apply during the
590
820
  // new drag (rapid successive resizes would otherwise inherit the
591
821
  // previous settle animation).
592
- clearTimeout( settleTimerRef.current );
593
- isSettlingRef.current = false;
594
- setSettling( false );
822
+ finishSettling();
595
823
  resetViewport();
596
824
  onGestureStart?.();
597
825
  },
598
- [ onGestureStart, resetViewport ]
826
+ [ finishSettling, onGestureStart, resetViewport, viewScaleRest ]
599
827
  );
600
828
 
601
829
  /**
@@ -603,9 +831,24 @@ function CropperInner(
603
831
  * and reset the viewport pan back to neutral.
604
832
  */
605
833
  const handleResizeEnd = useCallback( () => {
834
+ const isCancellingForPinch = isTouchPinchingRef.current;
606
835
  isResizingRef.current = false;
607
836
  setIsResizing( false );
608
837
  setActiveHandle( null );
838
+ clearTimeout( settleTimerRef.current );
839
+
840
+ if ( isCancellingForPinch ) {
841
+ // A pinch took over mid-resize. Tear down resize UI without
842
+ // settling or firing onGestureEnd: the pinch owns the gesture
843
+ // boundary and fires the end on its own touchend. The resize and
844
+ // the pinch therefore collapse into a single undo step, since
845
+ // `beginGesture` is idempotent and the snapshot captured at resize
846
+ // start is flushed by the pinch's `endGesture`.
847
+ finishSettling();
848
+ resetViewport();
849
+ return;
850
+ }
851
+
609
852
  isSettlingRef.current = true;
610
853
  setSettling( true );
611
854
  // Reset viewport pan first so it transitions back to zero in sync
@@ -613,30 +856,61 @@ function CropperInner(
613
856
  resetViewport();
614
857
  settleCrop();
615
858
  onGestureEnd?.();
616
- clearTimeout( settleTimerRef.current );
617
- settleTimerRef.current = setTimeout( () => {
618
- isSettlingRef.current = false;
619
- setSettling( false );
620
- }, 200 );
621
- }, [ settleCrop, onGestureEnd, resetViewport ] );
859
+ // Normal completion is driven by transitionend so settling cannot clear
860
+ // before the browser has actually finished animating. Keep a fallback for
861
+ // cases where the computed transform is unchanged and no event fires.
862
+ settleTimerRef.current = setTimeout(
863
+ finishSettling,
864
+ SETTLE_TRANSITION_FALLBACK_MS
865
+ );
866
+ }, [ finishSettling, settleCrop, onGestureEnd, resetViewport ] );
867
+
868
+ /**
869
+ * Finish settling when the settle transform transition actually completes.
870
+ * The handler lives on the stage, but in the common (un-panned) case the
871
+ * transform transition runs on the image layer and its `transitionend`
872
+ * bubbles up to here. Other transitionend events (stencil left/top/width/
873
+ * height) also bubble up, so only the transform transition should clear the
874
+ * settle state.
875
+ */
876
+ const handleSettleTransitionEnd = useCallback(
877
+ ( event: React.TransitionEvent< HTMLDivElement > ) => {
878
+ if (
879
+ ! isSettlingRef.current ||
880
+ event.propertyName !== 'transform'
881
+ ) {
882
+ return;
883
+ }
884
+ finishSettling();
885
+ },
886
+ [ finishSettling ]
887
+ );
622
888
 
623
889
  let imageTransition: string | undefined;
624
890
  if ( settling ) {
625
- imageTransition = 'transform 200ms ease-out';
891
+ imageTransition = SETTLE_TRANSFORM_TRANSITION;
626
892
  } else if ( isZooming ) {
627
893
  imageTransition = 'transform 150ms linear';
628
894
  }
629
895
  const settleStencilTransition = settling
630
- ? 'left 200ms ease-out, top 200ms ease-out, width 200ms ease-out, height 200ms ease-out'
896
+ ? SETTLE_STENCIL_TRANSITION
631
897
  : undefined;
632
898
 
633
- // Compute the image's CSS style.
899
+ // Compute the image's CSS style. The element keeps its contain-fit box; the
900
+ // view-scale magnification is folded into the transform as an outer
901
+ // `scale()` so the whole settle motion (zoom, pan, and magnification) is one
902
+ // coherent transform that interpolates as a unit — the image slides/scales
903
+ // to its position rather than animating its box separately. Magnifying
904
+ // around the centred box keeps the (centred) crop centred.
634
905
  const imageStyle = useMemo( (): React.CSSProperties => {
635
906
  if ( elementSize.width === 0 || elementSize.height === 0 ) {
636
907
  return {};
637
908
  }
638
909
  const centerX = ( canvasSize.width - elementSize.width ) / 2;
639
910
  const centerY = ( canvasSize.height - elementSize.height ) / 2;
911
+ // Above 1:1 the image is upscaled, so render nearest-neighbour to keep
912
+ // pixel boundaries crisp (e.g. small images the cropper magnifies);
913
+ // below 1:1 leave smoothing on for downscaled large images.
640
914
  return {
641
915
  width: elementSize.width,
642
916
  height: elementSize.height,
@@ -644,17 +918,31 @@ function CropperInner(
644
918
  maxHeight: elementSize.height,
645
919
  left: centerX,
646
920
  top: centerY,
647
- transform: transformString,
921
+ // Always lead with `scale()` (identity at rest) so the transform's
922
+ // function list stays structurally constant across rest, resize, and
923
+ // settle. A conditional that dropped `scale()` at viewScale === 1
924
+ // would change the list shape exactly when the settle transition
925
+ // crosses 1:1, forcing the browser to fall back to matrix-decomposition
926
+ // interpolation instead of interpolating each function in turn.
927
+ transform: `scale(${ viewScale }) ${ transformString }`,
648
928
  transition: imageTransition,
929
+ imageRendering: displayScale > 1 ? 'pixelated' : undefined,
649
930
  };
650
- }, [ canvasSize, elementSize, transformString, imageTransition ] );
931
+ }, [
932
+ canvasSize,
933
+ elementSize,
934
+ transformString,
935
+ imageTransition,
936
+ displayScale,
937
+ viewScale,
938
+ ] );
651
939
 
652
940
  // Viewport pan CSS transform for the stage div. Applied during resize
653
941
  // drags to keep handles visible when the crop extends past the canvas edge.
654
942
  // Transitions back to zero during the settle animation.
655
943
  // will-change promotes the stage to its own compositor layer while the
656
944
  // transform is active, keeping per-frame pan updates off the main thread.
657
- const settleTransition = settling ? 'transform 200ms ease-out' : undefined;
945
+ const settleTransition = settling ? SETTLE_TRANSFORM_TRANSITION : undefined;
658
946
  const willChange = isResizing || settling ? 'transform' : undefined;
659
947
  let stageStyle: React.CSSProperties | undefined;
660
948
  if ( viewportState.pan.x !== 0 || viewportState.pan.y !== 0 ) {
@@ -664,7 +952,14 @@ function CropperInner(
664
952
  willChange,
665
953
  };
666
954
  } else if ( settling ) {
667
- stageStyle = { transition: settleTransition, willChange };
955
+ // Animate the viewport pan back to an explicit identity translate rather
956
+ // than dropping the transform property, so the reset eases instead of
957
+ // snapping (transitions to a removed transform are unreliable).
958
+ stageStyle = {
959
+ transform: 'translate(0px, 0px)',
960
+ transition: settleTransition,
961
+ willChange,
962
+ };
668
963
  }
669
964
 
670
965
  // Forward the root element to the consumer's ref.
@@ -749,10 +1044,12 @@ function CropperInner(
749
1044
  className="wp-media-editor-image-editor__stage"
750
1045
  data-testid="cropper-stage"
751
1046
  style={ stageStyle }
1047
+ onTransitionEnd={ handleSettleTransitionEnd }
752
1048
  >
753
1049
  { /* The image layer */ }
754
1050
  <img
755
1051
  className="wp-media-editor-image-editor__image"
1052
+ data-testid="cropper-image"
756
1053
  src={ src }
757
1054
  alt=""
758
1055
  onLoad={ handleImageLoad }
@@ -765,7 +1062,7 @@ function CropperInner(
765
1062
  <DimmingOverlay
766
1063
  cropRect={ state.cropRect }
767
1064
  containerSize={ canvasSize }
768
- imageSize={ visualSize }
1065
+ imageSize={ scaledVisualSize }
769
1066
  transition={ settleStencilTransition }
770
1067
  />
771
1068
  ) }
@@ -774,16 +1071,19 @@ function CropperInner(
774
1071
  <StencilComponent
775
1072
  cropRect={ state.cropRect }
776
1073
  containerSize={ canvasSize }
777
- imageSize={ visualSize }
1074
+ imageSize={ scaledVisualSize }
778
1075
  onCropChange={ handleCropChange }
779
1076
  onResizeStart={ handleResizeStart }
780
1077
  onResizeEnd={ handleResizeEnd }
781
1078
  onEscape={ handleEscape }
782
1079
  aspectRatio={ aspectRatio }
783
1080
  freeformCrop={ freeformCrop }
1081
+ isResizeDisabled={ isTouchPinching }
784
1082
  stencilTransition={ settleStencilTransition }
785
1083
  cropBounds={ cropBounds }
786
1084
  minCropSize={ minCropSize }
1085
+ snapCropRect={ snapCropRect }
1086
+ keyboardResizeStep={ keyboardResizeStep }
787
1087
  />
788
1088
 
789
1089
  { /* Rule-of-thirds grid */ }
@@ -791,7 +1091,7 @@ function CropperInner(
791
1091
  <GridOverlay
792
1092
  cropRect={ state.cropRect }
793
1093
  containerSize={ canvasSize }
794
- imageSize={ visualSize }
1094
+ imageSize={ scaledVisualSize }
795
1095
  />
796
1096
  ) }
797
1097
 
@@ -800,7 +1100,7 @@ function CropperInner(
800
1100
  <DimensionsOverlay
801
1101
  cropRect={ state.cropRect }
802
1102
  containerSize={ canvasSize }
803
- imageSize={ visualSize }
1103
+ imageSize={ scaledVisualSize }
804
1104
  activeHandle={ activeHandle }
805
1105
  outputWidth={ outputSize.width }
806
1106
  outputHeight={ outputSize.height }
@@ -45,6 +45,7 @@ export function DimmingOverlay( {
45
45
  return (
46
46
  <div
47
47
  className="wp-media-editor-image-editor__dimming"
48
+ data-testid="cropper-dimming"
48
49
  style={ {
49
50
  left,
50
51
  top,