@wordpress/media-editor 0.11.1 → 0.12.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 (139) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/components/media-editor/index.cjs.map +1 -1
  3. package/build/components/media-editor/use-crop-options.cjs.map +1 -1
  4. package/build/components/media-editor-canvas/index.cjs +63 -15
  5. package/build/components/media-editor-canvas/index.cjs.map +3 -3
  6. package/build/components/media-editor-fine-rotation/index.cjs.map +1 -1
  7. package/build/components/media-editor-image-controls/index.cjs +2 -2
  8. package/build/components/media-editor-image-controls/index.cjs.map +2 -2
  9. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +4 -4
  10. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +2 -2
  11. package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -1
  12. package/build/components/rotation-ruler/index.cjs +1 -1
  13. package/build/components/rotation-ruler/index.cjs.map +1 -1
  14. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -1
  15. package/build/image-editor/core/camera.cjs +13 -0
  16. package/build/image-editor/core/camera.cjs.map +2 -2
  17. package/build/image-editor/core/constants.cjs +12 -3
  18. package/build/image-editor/core/constants.cjs.map +2 -2
  19. package/build/image-editor/core/containment.cjs.map +1 -1
  20. package/build/image-editor/core/crop-rect.cjs.map +1 -1
  21. package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -1
  22. package/build/image-editor/core/interaction-controller.cjs +9 -7
  23. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  24. package/build/image-editor/core/math/sanitize.cjs.map +1 -1
  25. package/build/image-editor/core/setter-helpers.cjs.map +1 -1
  26. package/build/image-editor/core/source-region.cjs +111 -2
  27. package/build/image-editor/core/source-region.cjs.map +2 -2
  28. package/build/image-editor/core/state.cjs.map +1 -1
  29. package/build/image-editor/core/stencil-math.cjs +3 -4
  30. package/build/image-editor/core/stencil-math.cjs.map +2 -2
  31. package/build/image-editor/core/transform-style.cjs.map +1 -1
  32. package/build/image-editor/core/types.cjs.map +1 -1
  33. package/build/image-editor/react/components/cropper.cjs +232 -55
  34. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  35. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -1
  36. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +1 -0
  37. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
  38. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +65 -19
  39. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  40. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -1
  41. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +1 -1
  42. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +2 -2
  43. package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -1
  44. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -1
  45. package/build/index.cjs.map +1 -1
  46. package/build/state/types.cjs.map +1 -1
  47. package/build/state/use-media-editor-state.cjs +1 -1
  48. package/build/state/use-media-editor-state.cjs.map +2 -2
  49. package/build-module/components/media-editor/index.mjs.map +1 -1
  50. package/build-module/components/media-editor/use-crop-options.mjs.map +1 -1
  51. package/build-module/components/media-editor-canvas/index.mjs +55 -17
  52. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  53. package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -1
  54. package/build-module/components/media-editor-image-controls/index.mjs +2 -2
  55. package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
  56. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +4 -4
  57. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +2 -2
  58. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +1 -1
  59. package/build-module/components/rotation-ruler/index.mjs +1 -1
  60. package/build-module/components/rotation-ruler/index.mjs.map +1 -1
  61. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -1
  62. package/build-module/image-editor/core/camera.mjs +12 -0
  63. package/build-module/image-editor/core/camera.mjs.map +2 -2
  64. package/build-module/image-editor/core/constants.mjs +8 -2
  65. package/build-module/image-editor/core/constants.mjs.map +2 -2
  66. package/build-module/image-editor/core/containment.mjs.map +1 -1
  67. package/build-module/image-editor/core/crop-rect.mjs.map +1 -1
  68. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -1
  69. package/build-module/image-editor/core/interaction-controller.mjs +9 -7
  70. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  71. package/build-module/image-editor/core/math/sanitize.mjs.map +1 -1
  72. package/build-module/image-editor/core/setter-helpers.mjs.map +1 -1
  73. package/build-module/image-editor/core/source-region.mjs +108 -1
  74. package/build-module/image-editor/core/source-region.mjs.map +2 -2
  75. package/build-module/image-editor/core/state.mjs.map +1 -1
  76. package/build-module/image-editor/core/stencil-math.mjs +3 -4
  77. package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
  78. package/build-module/image-editor/core/transform-style.mjs.map +1 -1
  79. package/build-module/image-editor/react/components/cropper.mjs +242 -57
  80. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  81. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -1
  82. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +1 -0
  83. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
  84. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +65 -19
  85. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  86. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -1
  87. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +1 -1
  88. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +2 -2
  89. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +1 -1
  90. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -1
  91. package/build-module/index.mjs.map +1 -1
  92. package/build-module/state/types.mjs.map +1 -1
  93. package/build-module/state/use-media-editor-state.mjs +1 -1
  94. package/build-module/state/use-media-editor-state.mjs.map +2 -2
  95. package/build-style/style-rtl.css +49 -6
  96. package/build-style/style.css +49 -6
  97. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  98. package/build-types/image-editor/core/camera.d.ts +25 -1
  99. package/build-types/image-editor/core/camera.d.ts.map +1 -1
  100. package/build-types/image-editor/core/constants.d.ts +20 -1
  101. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  102. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  103. package/build-types/image-editor/core/source-region.d.ts +23 -1
  104. package/build-types/image-editor/core/source-region.d.ts.map +1 -1
  105. package/build-types/image-editor/core/stencil-math.d.ts +10 -5
  106. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
  107. package/build-types/image-editor/core/types.d.ts +14 -0
  108. package/build-types/image-editor/core/types.d.ts.map +1 -1
  109. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  110. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
  111. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +17 -14
  112. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  113. package/package.json +25 -20
  114. package/src/components/media-editor/style.scss +21 -9
  115. package/src/components/media-editor-canvas/index.tsx +82 -13
  116. package/src/components/media-editor-canvas/style.scss +41 -0
  117. package/src/image-editor/core/camera.ts +60 -1
  118. package/src/image-editor/core/constants.ts +23 -1
  119. package/src/image-editor/core/interaction-controller.ts +25 -11
  120. package/src/image-editor/core/source-region.ts +202 -1
  121. package/src/image-editor/core/stencil-math.ts +23 -14
  122. package/src/image-editor/core/test/camera.ts +272 -1
  123. package/src/image-editor/core/test/interaction-controller.ts +100 -0
  124. package/src/image-editor/core/test/stencil-math.ts +120 -6
  125. package/src/image-editor/core/types.ts +17 -0
  126. package/src/image-editor/react/components/cropper.tsx +362 -72
  127. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +1 -0
  128. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +101 -32
  129. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +169 -17
  130. package/src/image-editor/react/components/test/cropper.tsx +689 -6
  131. package/tsconfig.tsbuildinfo +1 -1
  132. package/build-types/components/media-editor-toolbar/index.d.ts +0 -24
  133. package/build-types/components/media-editor-toolbar/index.d.ts.map +0 -1
  134. package/build-types/components/media-editor-transform-controls/index.d.ts +0 -21
  135. package/build-types/components/media-editor-transform-controls/index.d.ts.map +0 -1
  136. package/build-types/components/media-form/sidebar-datetime-view.d.ts +0 -10
  137. package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +0 -1
  138. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -108
  139. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
@@ -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 { getMinCropPixels } from '../../core/stencil-math';
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,42 +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
- //
279
- // The source-pixel floor is resolved per display scale so the crop
280
- // stays grabbable: a large image fit into a small canvas would render
281
- // the 24px floor only a few CSS pixels wide. `elementSize.width /
282
- // naturalWidth` is the fit scale; × zoom gives CSS px per source px.
283
- const minCropSize: Size | undefined = useMemo( () => {
284
- if ( naturalWidth <= 0 || naturalHeight <= 0 ) {
285
- return undefined;
286
- }
287
- const snapRotation = Math.round( state.rotation / 90 ) * 90;
288
- const bbox = getRotatedBBox(
289
- naturalWidth,
290
- naturalHeight,
291
- snapRotation
292
- );
293
- const displayScale = ( elementSize.width / naturalWidth ) * state.zoom;
294
- const minPixels = getMinCropPixels( displayScale );
295
- return {
296
- width: Math.min( 1, ( minPixels * state.zoom ) / bbox.width ),
297
- height: Math.min( 1, ( minPixels * state.zoom ) / bbox.height ),
298
- };
299
- }, [
300
- naturalWidth,
301
- naturalHeight,
302
- state.rotation,
303
- state.zoom,
304
- elementSize.width,
305
- ] );
306
-
307
295
  // Report the rendered image size to the controller. Composite
308
296
  // controllers need it to compute aspect-ratio reshapes from the
309
297
  // reducer (the dropdown dispatches without DOM access); pure
@@ -404,6 +392,12 @@ function CropperInner(
404
392
  const [ isResizing, setIsResizing ] = useState( false );
405
393
  const isResizingRef = useRef( false );
406
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
+ }, [] );
407
401
  // Direction of the handle the user is currently resizing — pointer or
408
402
  // keyboard. `null` outside of an active resize. Drives the live
409
403
  // dimensions tooltip overlay.
@@ -411,6 +405,185 @@ function CropperInner(
411
405
  null
412
406
  );
413
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
+
414
587
  // Use the interaction hook for mouse, touch, and keyboard events.
415
588
  const {
416
589
  handlers,
@@ -418,7 +591,7 @@ function CropperInner(
418
591
  isDragging,
419
592
  isZooming,
420
593
  isPlacementActive: isInteractionPlacementActive,
421
- } = useInteraction( state, controller, canvasSize, visualSize, {
594
+ } = useInteraction( state, controller, canvasSize, scaledVisualSize, {
422
595
  minZoom: effectiveMinZoom,
423
596
  maxZoom,
424
597
  onGestureStart,
@@ -439,6 +612,26 @@ function CropperInner(
439
612
  onPointerDownCapture: () => {
440
613
  setIsFocusVisible( false );
441
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
+ },
442
635
  onKeyDownCapture: ( event: React.KeyboardEvent< HTMLDivElement > ) => {
443
636
  // Modifier-only keypresses precede another key rather than
444
637
  // indicating deliberate keyboard interaction on their own.
@@ -449,6 +642,14 @@ function CropperInner(
449
642
  }
450
643
  },
451
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
+ }
452
653
  handlers.onPointerDown?.( event );
453
654
  // Re-assert false after handlers run — el.focus() inside the
454
655
  // handler fires onFocus, which may otherwise set it back to true.
@@ -483,7 +684,10 @@ function CropperInner(
483
684
  };
484
685
  }, [ onWheelNative ] );
485
686
 
486
- // 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.
487
691
  const transformString = useTransformStyle( state, visualSize );
488
692
 
489
693
  /**
@@ -510,35 +714,40 @@ function CropperInner(
510
714
 
511
715
  const handleCropChange = useCallback(
512
716
  ( rect: NormalizedRect ) => {
717
+ if ( isTouchPinchingRef.current ) {
718
+ return;
719
+ }
513
720
  setCropRect( rect );
514
721
  // During a resize drag, pan the viewport so the handle stays
515
722
  // visible even when the crop extends beyond the canvas edge.
516
723
  if (
517
724
  isResizingRef.current &&
518
- visualSize.width > 0 &&
519
- visualSize.height > 0
725
+ scaledVisualSize.width > 0 &&
726
+ scaledVisualSize.height > 0
520
727
  ) {
521
- const offsetX = ( canvasSize.width - visualSize.width ) / 2;
522
- 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;
523
732
  const rightOverflow = Math.max(
524
733
  0,
525
734
  offsetX +
526
- ( rect.x + rect.width ) * visualSize.width -
735
+ ( rect.x + rect.width ) * scaledVisualSize.width -
527
736
  canvasSize.width
528
737
  );
529
738
  const leftOverflow = Math.max(
530
739
  0,
531
- -( offsetX + rect.x * visualSize.width )
740
+ -( offsetX + rect.x * scaledVisualSize.width )
532
741
  );
533
742
  const bottomOverflow = Math.max(
534
743
  0,
535
744
  offsetY +
536
- ( rect.y + rect.height ) * visualSize.height -
745
+ ( rect.y + rect.height ) * scaledVisualSize.height -
537
746
  canvasSize.height
538
747
  );
539
748
  const topOverflow = Math.max(
540
749
  0,
541
- -( offsetY + rect.y * visualSize.height )
750
+ -( offsetY + rect.y * scaledVisualSize.height )
542
751
  );
543
752
  setViewportPan( {
544
753
  x: -rightOverflow + leftOverflow,
@@ -546,12 +755,17 @@ function CropperInner(
546
755
  } );
547
756
  }
548
757
  },
549
- [ setCropRect, setViewportPan, canvasSize, visualSize ]
758
+ [ setCropRect, setViewportPan, canvasSize, scaledVisualSize ]
550
759
  );
551
760
 
552
- // Settling animation: brief linear transition after resize end.
761
+ // Settling animation: brief transition after resize end.
553
762
  const [ settling, setSettling ] = useState( false );
554
763
  const settleTimerRef = useRef< ReturnType< typeof setTimeout > >();
764
+ const finishSettling = useCallback( () => {
765
+ clearTimeout( settleTimerRef.current );
766
+ isSettlingRef.current = false;
767
+ setSettling( false );
768
+ }, [] );
555
769
 
556
770
  // Clear the pending settle timer on unmount so it can't fire a
557
771
  // state update on an unmounted component.
@@ -593,19 +807,23 @@ function CropperInner(
593
807
 
594
808
  const handleResizeStart = useCallback(
595
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 );
596
816
  isResizingRef.current = true;
597
817
  setIsResizing( true );
598
818
  setActiveHandle( handle ?? null );
599
819
  // Clear any in-flight settle so transitions don't apply during the
600
820
  // new drag (rapid successive resizes would otherwise inherit the
601
821
  // previous settle animation).
602
- clearTimeout( settleTimerRef.current );
603
- isSettlingRef.current = false;
604
- setSettling( false );
822
+ finishSettling();
605
823
  resetViewport();
606
824
  onGestureStart?.();
607
825
  },
608
- [ onGestureStart, resetViewport ]
826
+ [ finishSettling, onGestureStart, resetViewport, viewScaleRest ]
609
827
  );
610
828
 
611
829
  /**
@@ -613,9 +831,24 @@ function CropperInner(
613
831
  * and reset the viewport pan back to neutral.
614
832
  */
615
833
  const handleResizeEnd = useCallback( () => {
834
+ const isCancellingForPinch = isTouchPinchingRef.current;
616
835
  isResizingRef.current = false;
617
836
  setIsResizing( false );
618
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
+
619
852
  isSettlingRef.current = true;
620
853
  setSettling( true );
621
854
  // Reset viewport pan first so it transitions back to zero in sync
@@ -623,30 +856,61 @@ function CropperInner(
623
856
  resetViewport();
624
857
  settleCrop();
625
858
  onGestureEnd?.();
626
- clearTimeout( settleTimerRef.current );
627
- settleTimerRef.current = setTimeout( () => {
628
- isSettlingRef.current = false;
629
- setSettling( false );
630
- }, 200 );
631
- }, [ 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
+ );
632
888
 
633
889
  let imageTransition: string | undefined;
634
890
  if ( settling ) {
635
- imageTransition = 'transform 200ms ease-out';
891
+ imageTransition = SETTLE_TRANSFORM_TRANSITION;
636
892
  } else if ( isZooming ) {
637
893
  imageTransition = 'transform 150ms linear';
638
894
  }
639
895
  const settleStencilTransition = settling
640
- ? 'left 200ms ease-out, top 200ms ease-out, width 200ms ease-out, height 200ms ease-out'
896
+ ? SETTLE_STENCIL_TRANSITION
641
897
  : undefined;
642
898
 
643
- // 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.
644
905
  const imageStyle = useMemo( (): React.CSSProperties => {
645
906
  if ( elementSize.width === 0 || elementSize.height === 0 ) {
646
907
  return {};
647
908
  }
648
909
  const centerX = ( canvasSize.width - elementSize.width ) / 2;
649
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.
650
914
  return {
651
915
  width: elementSize.width,
652
916
  height: elementSize.height,
@@ -654,17 +918,31 @@ function CropperInner(
654
918
  maxHeight: elementSize.height,
655
919
  left: centerX,
656
920
  top: centerY,
657
- 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 }`,
658
928
  transition: imageTransition,
929
+ imageRendering: displayScale > 1 ? 'pixelated' : undefined,
659
930
  };
660
- }, [ canvasSize, elementSize, transformString, imageTransition ] );
931
+ }, [
932
+ canvasSize,
933
+ elementSize,
934
+ transformString,
935
+ imageTransition,
936
+ displayScale,
937
+ viewScale,
938
+ ] );
661
939
 
662
940
  // Viewport pan CSS transform for the stage div. Applied during resize
663
941
  // drags to keep handles visible when the crop extends past the canvas edge.
664
942
  // Transitions back to zero during the settle animation.
665
943
  // will-change promotes the stage to its own compositor layer while the
666
944
  // transform is active, keeping per-frame pan updates off the main thread.
667
- const settleTransition = settling ? 'transform 200ms ease-out' : undefined;
945
+ const settleTransition = settling ? SETTLE_TRANSFORM_TRANSITION : undefined;
668
946
  const willChange = isResizing || settling ? 'transform' : undefined;
669
947
  let stageStyle: React.CSSProperties | undefined;
670
948
  if ( viewportState.pan.x !== 0 || viewportState.pan.y !== 0 ) {
@@ -674,7 +952,14 @@ function CropperInner(
674
952
  willChange,
675
953
  };
676
954
  } else if ( settling ) {
677
- 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
+ };
678
963
  }
679
964
 
680
965
  // Forward the root element to the consumer's ref.
@@ -759,10 +1044,12 @@ function CropperInner(
759
1044
  className="wp-media-editor-image-editor__stage"
760
1045
  data-testid="cropper-stage"
761
1046
  style={ stageStyle }
1047
+ onTransitionEnd={ handleSettleTransitionEnd }
762
1048
  >
763
1049
  { /* The image layer */ }
764
1050
  <img
765
1051
  className="wp-media-editor-image-editor__image"
1052
+ data-testid="cropper-image"
766
1053
  src={ src }
767
1054
  alt=""
768
1055
  onLoad={ handleImageLoad }
@@ -775,7 +1062,7 @@ function CropperInner(
775
1062
  <DimmingOverlay
776
1063
  cropRect={ state.cropRect }
777
1064
  containerSize={ canvasSize }
778
- imageSize={ visualSize }
1065
+ imageSize={ scaledVisualSize }
779
1066
  transition={ settleStencilTransition }
780
1067
  />
781
1068
  ) }
@@ -784,16 +1071,19 @@ function CropperInner(
784
1071
  <StencilComponent
785
1072
  cropRect={ state.cropRect }
786
1073
  containerSize={ canvasSize }
787
- imageSize={ visualSize }
1074
+ imageSize={ scaledVisualSize }
788
1075
  onCropChange={ handleCropChange }
789
1076
  onResizeStart={ handleResizeStart }
790
1077
  onResizeEnd={ handleResizeEnd }
791
1078
  onEscape={ handleEscape }
792
1079
  aspectRatio={ aspectRatio }
793
1080
  freeformCrop={ freeformCrop }
1081
+ isResizeDisabled={ isTouchPinching }
794
1082
  stencilTransition={ settleStencilTransition }
795
1083
  cropBounds={ cropBounds }
796
1084
  minCropSize={ minCropSize }
1085
+ snapCropRect={ snapCropRect }
1086
+ keyboardResizeStep={ keyboardResizeStep }
797
1087
  />
798
1088
 
799
1089
  { /* Rule-of-thirds grid */ }
@@ -801,7 +1091,7 @@ function CropperInner(
801
1091
  <GridOverlay
802
1092
  cropRect={ state.cropRect }
803
1093
  containerSize={ canvasSize }
804
- imageSize={ visualSize }
1094
+ imageSize={ scaledVisualSize }
805
1095
  />
806
1096
  ) }
807
1097
 
@@ -810,7 +1100,7 @@ function CropperInner(
810
1100
  <DimensionsOverlay
811
1101
  cropRect={ state.cropRect }
812
1102
  containerSize={ canvasSize }
813
- imageSize={ visualSize }
1103
+ imageSize={ scaledVisualSize }
814
1104
  activeHandle={ activeHandle }
815
1105
  outputWidth={ outputSize.width }
816
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,