@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
@@ -1,4 +1,11 @@
1
1
  // packages/media-editor/src/image-editor/core/stencil-math.ts
2
+ import { MIN_CROP_PIXELS, MIN_CROP_SCREEN_PX } from "./constants.mjs";
3
+ function getMinCropPixels(displayScale) {
4
+ if (displayScale <= 0) {
5
+ return MIN_CROP_PIXELS;
6
+ }
7
+ return Math.max(MIN_CROP_PIXELS, MIN_CROP_SCREEN_PX / displayScale);
8
+ }
2
9
  var DEFAULT_MIN_CROP_SIZE = { width: 0.05, height: 0.05 };
3
10
  function computeFreeResizeRect(drag, clientX, clientY, imageSize, bounds, minCropSize = DEFAULT_MIN_CROP_SIZE) {
4
11
  const dx = imageSize.width > 0 ? (clientX - drag.startX) / imageSize.width : 0;
@@ -40,7 +47,7 @@ function computeFreeResizeRect(drag, clientX, clientY, imageSize, bounds, minCro
40
47
  height: edgeBottom - edgeTop
41
48
  };
42
49
  }
43
- function computeLockedResizeRect(drag, clientX, clientY, imageSize, bounds, normalizedRatio, minCropSize = DEFAULT_MIN_CROP_SIZE) {
50
+ function computeLockedResizeRect(drag, clientX, clientY, imageSize, bounds, normalizedRatio, minCropSize = DEFAULT_MIN_CROP_SIZE, driverAxis) {
44
51
  if (normalizedRatio <= 0 || imageSize.width <= 0 || imageSize.height <= 0) {
45
52
  return { ...drag.startRect };
46
53
  }
@@ -63,10 +70,9 @@ function computeLockedResizeRect(drag, clientX, clientY, imageSize, bounds, norm
63
70
  const minDistH = minDistW / normalizedRatio;
64
71
  distW = Math.max(distW, minDistW);
65
72
  distH = Math.max(distH, minDistH);
66
- const pixelDistW = distW * imageSize.width;
67
- const pixelDistH = distH * imageSize.height;
68
73
  const pixelRatio = normalizedRatio * imageSize.width / imageSize.height;
69
- if (pixelDistW / pixelDistH > pixelRatio) {
74
+ const isWidthDriver = driverAxis === "width" || !driverAxis && distW * imageSize.width / (distH * imageSize.height) > pixelRatio;
75
+ if (isWidthDriver) {
70
76
  distH = distW / normalizedRatio;
71
77
  } else {
72
78
  distW = distH * normalizedRatio;
@@ -175,6 +181,7 @@ export {
175
181
  DEFAULT_MIN_CROP_SIZE,
176
182
  computeFreeResizeRect,
177
183
  computeLockedResizeRect,
178
- computeShiftLockedResizeRect
184
+ computeShiftLockedResizeRect,
185
+ getMinCropPixels
179
186
  };
180
187
  //# sourceMappingURL=stencil-math.mjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/image-editor/core/stencil-math.ts"],
4
- "sourcesContent": ["/**\n * Internal dependencies\n */\nimport type { HandlePosition, NormalizedRect, Size } from './types';\n\nexport type { HandlePosition };\n\n/**\n * Default minimum crop rect dimension in normalized space, used when no\n * explicit per-axis minimum is supplied (e.g. before the source image\n * has loaded). Real usage derives a pixel-based min from the source\n * image dimensions and `MIN_CROP_PIXELS` in `constants.ts`.\n */\nexport const DEFAULT_MIN_CROP_SIZE: Size = { width: 0.05, height: 0.05 };\n\n/**\n * Bounds within which the crop rect must stay.\n */\nexport interface CropBounds {\n\tminX: number;\n\tminY: number;\n\tmaxX: number;\n\tmaxY: number;\n}\n\n/**\n * Drag state for tracking a resize interaction.\n */\nexport interface ResizeDragState {\n\t/** Which handle is being dragged. */\n\thandle: HandlePosition;\n\t/** The mouse position (pixels) when the drag started. */\n\tstartX: number;\n\tstartY: number;\n\t/** The crop rect (normalized) when the drag started. */\n\tstartRect: NormalizedRect;\n}\n\n/**\n * Compute the new crop rect for a free (no aspect ratio) resize.\n *\n * Each edge moves independently based on the handle being dragged.\n * Edges are clamped to the provided bounds and maintain a minimum size.\n *\n * @param drag The current drag state.\n * @param clientX Current mouse/touch X position in pixels.\n * @param clientY Current mouse/touch Y position in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param bounds The allowed crop area bounds.\n * @param minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @return The new crop rect in normalized coordinates.\n */\nexport function computeFreeResizeRect(\n\tdrag: ResizeDragState,\n\tclientX: number,\n\tclientY: number,\n\timageSize: Size,\n\tbounds: CropBounds,\n\tminCropSize: Size = DEFAULT_MIN_CROP_SIZE\n): NormalizedRect {\n\tconst dx =\n\t\timageSize.width > 0 ? ( clientX - drag.startX ) / imageSize.width : 0;\n\tconst dy =\n\t\timageSize.height > 0 ? ( clientY - drag.startY ) / imageSize.height : 0;\n\n\tconst s = drag.startRect;\n\tconst handle = drag.handle;\n\n\tlet edgeTop = s.y;\n\tlet edgeBottom = s.y + s.height;\n\tlet edgeLeft = s.x;\n\tlet edgeRight = s.x + s.width;\n\n\tif ( handle === 'n' || handle === 'nw' || handle === 'ne' ) {\n\t\tedgeTop = Math.max(\n\t\t\tbounds.minY,\n\t\t\tMath.min( s.y + dy, edgeBottom - minCropSize.height )\n\t\t);\n\t}\n\tif ( handle === 's' || handle === 'sw' || handle === 'se' ) {\n\t\tedgeBottom = Math.max(\n\t\t\tedgeTop + minCropSize.height,\n\t\t\tMath.min( s.y + s.height + dy, bounds.maxY )\n\t\t);\n\t}\n\tif ( handle === 'w' || handle === 'nw' || handle === 'sw' ) {\n\t\tedgeLeft = Math.max(\n\t\t\tbounds.minX,\n\t\t\tMath.min( s.x + dx, edgeRight - minCropSize.width )\n\t\t);\n\t}\n\tif ( handle === 'e' || handle === 'ne' || handle === 'se' ) {\n\t\tedgeRight = Math.max(\n\t\t\tedgeLeft + minCropSize.width,\n\t\t\tMath.min( s.x + s.width + dx, bounds.maxX )\n\t\t);\n\t}\n\n\treturn {\n\t\tx: edgeLeft,\n\t\ty: edgeTop,\n\t\twidth: edgeRight - edgeLeft,\n\t\theight: edgeBottom - edgeTop,\n\t};\n}\n\n/**\n * Compute the new crop rect for a locked-aspect-ratio corner resize.\n *\n * The opposite corner is the anchor. The dragged corner moves freely\n * but the result is clamped to maintain the aspect ratio and stay\n * within bounds.\n *\n * @param drag The current drag state.\n * @param clientX Current mouse/touch X position in pixels.\n * @param clientY Current mouse/touch Y position in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param bounds The allowed crop area bounds.\n * @param normalizedRatio The locked aspect ratio (width / height in normalized space).\n * @param minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @return The new crop rect in normalized coordinates.\n */\nexport function computeLockedResizeRect(\n\tdrag: ResizeDragState,\n\tclientX: number,\n\tclientY: number,\n\timageSize: Size,\n\tbounds: CropBounds,\n\tnormalizedRatio: number,\n\tminCropSize: Size = DEFAULT_MIN_CROP_SIZE\n): NormalizedRect {\n\t// The math below divides by `normalizedRatio` and `imageSize`, so\n\t// bail out with the start rect when any of them is zero. This can\n\t// happen if a resize is triggered (e.g. via keyboard arrows on a\n\t// focused handle) before the image has loaded.\n\tif (\n\t\tnormalizedRatio <= 0 ||\n\t\timageSize.width <= 0 ||\n\t\timageSize.height <= 0\n\t) {\n\t\treturn { ...drag.startRect };\n\t}\n\n\tconst dx = ( clientX - drag.startX ) / imageSize.width;\n\tconst dy = ( clientY - drag.startY ) / imageSize.height;\n\n\tconst s = drag.startRect;\n\tconst handle = drag.handle;\n\n\t// Determine the anchor corner (opposite to the dragged corner).\n\tconst anchorX = handle === 'nw' || handle === 'sw' ? s.x + s.width : s.x;\n\tconst anchorY = handle === 'nw' || handle === 'ne' ? s.y + s.height : s.y;\n\n\t// Direction the crop grows from the anchor (+1 = right/down, -1 = left/up).\n\tconst dirX = handle === 'nw' || handle === 'sw' ? -1 : 1;\n\tconst dirY = handle === 'nw' || handle === 'ne' ? -1 : 1;\n\n\t// Desired new position of the dragged corner.\n\tconst draggedX =\n\t\t( handle === 'nw' || handle === 'sw' ? s.x : s.x + s.width ) + dx;\n\tconst draggedY =\n\t\t( handle === 'nw' || handle === 'ne' ? s.y : s.y + s.height ) + dy;\n\n\t// Raw distances from anchor to dragged corner.\n\tlet distW = ( draggedX - anchorX ) * dirX;\n\tlet distH = ( draggedY - anchorY ) * dirY;\n\n\t// Enforce minimum size on both axes, projecting through the ratio so\n\t// neither axis falls below its per-axis floor.\n\tconst minDistW = Math.max(\n\t\tminCropSize.width,\n\t\tminCropSize.height * normalizedRatio\n\t);\n\tconst minDistH = minDistW / normalizedRatio;\n\tdistW = Math.max( distW, minDistW );\n\tdistH = Math.max( distH, minDistH );\n\n\t// Determine which axis \"drives\" \u2014 whichever the user moved more\n\t// (in pixel space) determines the size, the other follows. The\n\t// `normalizedRatio` is w/h in normalized space; the equivalent\n\t// pixel-space ratio is `normalizedRatio * imageW / imageH`. We\n\t// compare the pixel motion ratio against that pixel-space ratio\n\t// so the units line up (was a unit mismatch on non-square images).\n\tconst pixelDistW = distW * imageSize.width;\n\tconst pixelDistH = distH * imageSize.height;\n\tconst pixelRatio = ( normalizedRatio * imageSize.width ) / imageSize.height;\n\tif ( pixelDistW / pixelDistH > pixelRatio ) {\n\t\t// Width is the driver \u2014 compute height from ratio.\n\t\tdistH = distW / normalizedRatio;\n\t} else {\n\t\t// Height is the driver \u2014 compute width from ratio.\n\t\tdistW = distH * normalizedRatio;\n\t}\n\n\t// Clamp to image coverage bounds. If the rect would exceed,\n\t// shrink it (maintaining ratio) to fit.\n\tconst maxW = dirX > 0 ? bounds.maxX - anchorX : anchorX - bounds.minX;\n\tconst maxH = dirY > 0 ? bounds.maxY - anchorY : anchorY - bounds.minY;\n\n\tif ( distW > maxW ) {\n\t\tdistW = maxW;\n\t\tdistH = distW / normalizedRatio;\n\t}\n\tif ( distH > maxH ) {\n\t\tdistH = maxH;\n\t\tdistW = distH * normalizedRatio;\n\t}\n\n\t// Enforce minimum after clamping.\n\tdistW = Math.max( distW, minDistW );\n\tdistH = Math.max( distH, minDistH );\n\n\t// Compute the final rect position from the anchor.\n\tconst newX = dirX > 0 ? anchorX : anchorX - distW;\n\tconst newY = dirY > 0 ? anchorY : anchorY - distH;\n\n\treturn { x: newX, y: newY, width: distW, height: distH };\n}\n\n/**\n * Compute the new crop rect for a resize that temporarily locks the\n * aspect ratio to `drag.startRect`'s ratio (e.g. while Shift is held).\n *\n * Corner handles delegate to {@link computeLockedResizeRect}. Edge\n * handles size the dragged axis freely, then expand the perpendicular\n * axis symmetrically around the rect's center to preserve the ratio,\n * clamping symmetrically so the rect stays within bounds.\n *\n * @param drag The current drag state.\n * @param clientX Current mouse/touch X position in pixels.\n * @param clientY Current mouse/touch Y position in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param bounds The allowed crop area bounds.\n * @param minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @return The new crop rect in normalized coordinates.\n */\nexport function computeShiftLockedResizeRect(\n\tdrag: ResizeDragState,\n\tclientX: number,\n\tclientY: number,\n\timageSize: Size,\n\tbounds: CropBounds,\n\tminCropSize: Size = DEFAULT_MIN_CROP_SIZE\n): NormalizedRect {\n\tconst s = drag.startRect;\n\tconst pixelW = s.width * imageSize.width;\n\tconst pixelH = s.height * imageSize.height;\n\tif ( pixelH <= 0 || pixelW <= 0 ) {\n\t\treturn computeFreeResizeRect(\n\t\t\tdrag,\n\t\t\tclientX,\n\t\t\tclientY,\n\t\t\timageSize,\n\t\t\tbounds,\n\t\t\tminCropSize\n\t\t);\n\t}\n\tconst normalizedRatio = s.width / s.height;\n\n\tconst handle = drag.handle;\n\tif (\n\t\thandle === 'nw' ||\n\t\thandle === 'ne' ||\n\t\thandle === 'sw' ||\n\t\thandle === 'se'\n\t) {\n\t\treturn computeLockedResizeRect(\n\t\t\tdrag,\n\t\t\tclientX,\n\t\t\tclientY,\n\t\t\timageSize,\n\t\t\tbounds,\n\t\t\tnormalizedRatio,\n\t\t\tminCropSize\n\t\t);\n\t}\n\n\t// Edge handle: size the dragged axis with free-resize logic, then\n\t// expand the perpendicular axis symmetrically around the rect's center.\n\tconst free = computeFreeResizeRect(\n\t\tdrag,\n\t\tclientX,\n\t\tclientY,\n\t\timageSize,\n\t\tbounds,\n\t\tminCropSize\n\t);\n\n\tif ( handle === 'n' || handle === 's' ) {\n\t\t// Height is the driver; derive width from ratio.\n\t\tlet newHeight = free.height;\n\t\tlet newWidth = newHeight * normalizedRatio;\n\t\tconst centerX = s.x + s.width / 2;\n\t\t// Symmetric clamp: limit by the smaller of the two side gaps so\n\t\t// the rect stays centered around centerX.\n\t\tconst maxWidth =\n\t\t\tMath.min( centerX - bounds.minX, bounds.maxX - centerX ) * 2;\n\t\tif ( newWidth > maxWidth ) {\n\t\t\tnewWidth = maxWidth;\n\t\t\tnewHeight = newWidth / normalizedRatio;\n\t\t}\n\t\t// Enforce the minimum on the driving axis, then derive the\n\t\t// other axis from the ratio so it stays consistent. The\n\t\t// effective minimum height has to satisfy the per-axis floor\n\t\t// on both axes simultaneously.\n\t\tconst minHeight = Math.max(\n\t\t\tminCropSize.height,\n\t\t\tminCropSize.width / normalizedRatio\n\t\t);\n\t\tif ( newHeight < minHeight ) {\n\t\t\tnewHeight = minHeight;\n\t\t\tnewWidth = newHeight * normalizedRatio;\n\t\t}\n\t\t// Re-anchor the dragged axis to the opposite edge so the\n\t\t// height adjustment after clamping keeps that edge fixed.\n\t\tconst newY = handle === 'n' ? s.y + s.height - newHeight : s.y;\n\t\treturn {\n\t\t\tx: centerX - newWidth / 2,\n\t\t\ty: newY,\n\t\t\twidth: newWidth,\n\t\t\theight: newHeight,\n\t\t};\n\t}\n\n\t// 'e' or 'w': width is the driver; derive height from ratio.\n\tlet newWidth = free.width;\n\tlet newHeight = newWidth / normalizedRatio;\n\tconst centerY = s.y + s.height / 2;\n\tconst maxHeight =\n\t\tMath.min( centerY - bounds.minY, bounds.maxY - centerY ) * 2;\n\tif ( newHeight > maxHeight ) {\n\t\tnewHeight = maxHeight;\n\t\tnewWidth = newHeight * normalizedRatio;\n\t}\n\tconst minWidth = Math.max(\n\t\tminCropSize.width,\n\t\tminCropSize.height * normalizedRatio\n\t);\n\tif ( newWidth < minWidth ) {\n\t\tnewWidth = minWidth;\n\t\tnewHeight = newWidth / normalizedRatio;\n\t}\n\tconst newX = handle === 'w' ? s.x + s.width - newWidth : s.x;\n\treturn {\n\t\tx: newX,\n\t\ty: centerY - newHeight / 2,\n\t\twidth: newWidth,\n\t\theight: newHeight,\n\t};\n}\n"],
5
- "mappings": ";AAaO,IAAM,wBAA8B,EAAE,OAAO,MAAM,QAAQ,KAAK;AAuChE,SAAS,sBACf,MACA,SACA,SACA,WACA,QACA,cAAoB,uBACH;AACjB,QAAM,KACL,UAAU,QAAQ,KAAM,UAAU,KAAK,UAAW,UAAU,QAAQ;AACrE,QAAM,KACL,UAAU,SAAS,KAAM,UAAU,KAAK,UAAW,UAAU,SAAS;AAEvE,QAAM,IAAI,KAAK;AACf,QAAM,SAAS,KAAK;AAEpB,MAAI,UAAU,EAAE;AAChB,MAAI,aAAa,EAAE,IAAI,EAAE;AACzB,MAAI,WAAW,EAAE;AACjB,MAAI,YAAY,EAAE,IAAI,EAAE;AAExB,MAAK,WAAW,OAAO,WAAW,QAAQ,WAAW,MAAO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B,MAAI,YAAY,WAAW;AAC3B,QAAM,UAAU,EAAE,IAAI,EAAE,SAAS;AACjC,QAAM,YACL,KAAK,IAAK,UAAU,OAAO,MAAM,OAAO,OAAO,OAAQ,IAAI;AAC5D,MAAK,YAAY,WAAY;AAC5B,gBAAY;AACZ,eAAW,YAAY;AAAA,EACxB;AACA,QAAM,WAAW,KAAK;AAAA,IACrB,YAAY;AAAA,IACZ,YAAY,SAAS;AAAA,EACtB;AACA,MAAK,WAAW,UAAW;AAC1B,eAAW;AACX,gBAAY,WAAW;AAAA,EACxB;AACA,QAAM,OAAO,WAAW,MAAM,EAAE,IAAI,EAAE,QAAQ,WAAW,EAAE;AAC3D,SAAO;AAAA,IACN,GAAG;AAAA,IACH,GAAG,UAAU,YAAY;AAAA,IACzB,OAAO;AAAA,IACP,QAAQ;AAAA,EACT;AACD;",
4
+ "sourcesContent": ["/**\n * Internal dependencies\n */\nimport { MIN_CROP_PIXELS, MIN_CROP_SCREEN_PX } from './constants';\nimport type { HandlePosition, NormalizedRect, Size } from './types';\n\nexport type { HandlePosition };\n\n/** Axis whose resize distance controls the locked-ratio size projection. */\nexport type ResizeDriverAxis = 'width' | 'height';\n\n/**\n * Resolve the minimum crop dimension, in source-image pixels, for the current\n * display scale.\n *\n * Two floors compete: a fixed source-pixel floor (`MIN_CROP_PIXELS`) that\n * avoids sub-pixel crops, and an on-screen floor (`MIN_CROP_SCREEN_PX`) that\n * keeps the crop large enough to grab. The on-screen floor converted to source\n * pixels is `MIN_CROP_SCREEN_PX / displayScale`; the binding constraint is the\n * larger of the two. When the image is shown small (small `displayScale`) the\n * on-screen floor dominates; once it is shown large enough the source-pixel\n * floor takes over.\n *\n * @param displayScale CSS pixels rendered per source pixel\n * (fit scale × zoom × view scale).\n * @return Minimum crop dimension in source-image pixels.\n */\nexport function getMinCropPixels( displayScale: number ): number {\n\tif ( displayScale <= 0 ) {\n\t\treturn MIN_CROP_PIXELS;\n\t}\n\treturn Math.max( MIN_CROP_PIXELS, MIN_CROP_SCREEN_PX / displayScale );\n}\n\n/**\n * Default minimum crop rect dimension in normalized space, used when no\n * explicit per-axis minimum is supplied (e.g. before the source image\n * has loaded). Real usage derives a pixel-based min from the source\n * image dimensions and `MIN_CROP_PIXELS` in `constants.ts`.\n */\nexport const DEFAULT_MIN_CROP_SIZE: Size = { width: 0.05, height: 0.05 };\n\n/**\n * Bounds within which the crop rect must stay.\n */\nexport interface CropBounds {\n\tminX: number;\n\tminY: number;\n\tmaxX: number;\n\tmaxY: number;\n}\n\n/**\n * Drag state for tracking a resize interaction.\n */\nexport interface ResizeDragState {\n\t/** Which handle is being dragged. */\n\thandle: HandlePosition;\n\t/** The mouse position (pixels) when the drag started. */\n\tstartX: number;\n\tstartY: number;\n\t/** The crop rect (normalized) when the drag started. */\n\tstartRect: NormalizedRect;\n}\n\n/**\n * Compute the new crop rect for a free (no aspect ratio) resize.\n *\n * Each edge moves independently based on the handle being dragged.\n * Edges are clamped to the provided bounds and maintain a minimum size.\n *\n * @param drag The current drag state.\n * @param clientX Current mouse/touch X position in pixels.\n * @param clientY Current mouse/touch Y position in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param bounds The allowed crop area bounds.\n * @param minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @return The new crop rect in normalized coordinates.\n */\nexport function computeFreeResizeRect(\n\tdrag: ResizeDragState,\n\tclientX: number,\n\tclientY: number,\n\timageSize: Size,\n\tbounds: CropBounds,\n\tminCropSize: Size = DEFAULT_MIN_CROP_SIZE\n): NormalizedRect {\n\tconst dx =\n\t\timageSize.width > 0 ? ( clientX - drag.startX ) / imageSize.width : 0;\n\tconst dy =\n\t\timageSize.height > 0 ? ( clientY - drag.startY ) / imageSize.height : 0;\n\n\tconst s = drag.startRect;\n\tconst handle = drag.handle;\n\n\tlet edgeTop = s.y;\n\tlet edgeBottom = s.y + s.height;\n\tlet edgeLeft = s.x;\n\tlet edgeRight = s.x + s.width;\n\n\tif ( handle === 'n' || handle === 'nw' || handle === 'ne' ) {\n\t\tedgeTop = Math.max(\n\t\t\tbounds.minY,\n\t\t\tMath.min( s.y + dy, edgeBottom - minCropSize.height )\n\t\t);\n\t}\n\tif ( handle === 's' || handle === 'sw' || handle === 'se' ) {\n\t\tedgeBottom = Math.max(\n\t\t\tedgeTop + minCropSize.height,\n\t\t\tMath.min( s.y + s.height + dy, bounds.maxY )\n\t\t);\n\t}\n\tif ( handle === 'w' || handle === 'nw' || handle === 'sw' ) {\n\t\tedgeLeft = Math.max(\n\t\t\tbounds.minX,\n\t\t\tMath.min( s.x + dx, edgeRight - minCropSize.width )\n\t\t);\n\t}\n\tif ( handle === 'e' || handle === 'ne' || handle === 'se' ) {\n\t\tedgeRight = Math.max(\n\t\t\tedgeLeft + minCropSize.width,\n\t\t\tMath.min( s.x + s.width + dx, bounds.maxX )\n\t\t);\n\t}\n\n\treturn {\n\t\tx: edgeLeft,\n\t\ty: edgeTop,\n\t\twidth: edgeRight - edgeLeft,\n\t\theight: edgeBottom - edgeTop,\n\t};\n}\n\n/**\n * Compute the new crop rect for a locked-aspect-ratio corner resize.\n *\n * The opposite corner is the anchor. The dragged corner moves freely\n * but the result is clamped to maintain the aspect ratio and stay\n * within bounds.\n *\n * @param drag The current drag state.\n * @param clientX Current mouse/touch X position in pixels.\n * @param clientY Current mouse/touch Y position in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param bounds The allowed crop area bounds.\n * @param normalizedRatio The locked aspect ratio (width / height in normalized space).\n * @param minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @param driverAxis Optional explicit driver axis. Used for keyboard\n * resize, where one arrow axis should determine the step.\n * @return The new crop rect in normalized coordinates.\n */\nexport function computeLockedResizeRect(\n\tdrag: ResizeDragState,\n\tclientX: number,\n\tclientY: number,\n\timageSize: Size,\n\tbounds: CropBounds,\n\tnormalizedRatio: number,\n\tminCropSize: Size = DEFAULT_MIN_CROP_SIZE,\n\tdriverAxis?: ResizeDriverAxis\n): NormalizedRect {\n\t// The math below divides by `normalizedRatio` and `imageSize`, so\n\t// bail out with the start rect when any of them is zero. This can\n\t// happen if a resize is triggered (e.g. via keyboard arrows on a\n\t// focused handle) before the image has loaded.\n\tif (\n\t\tnormalizedRatio <= 0 ||\n\t\timageSize.width <= 0 ||\n\t\timageSize.height <= 0\n\t) {\n\t\treturn { ...drag.startRect };\n\t}\n\n\tconst dx = ( clientX - drag.startX ) / imageSize.width;\n\tconst dy = ( clientY - drag.startY ) / imageSize.height;\n\n\tconst s = drag.startRect;\n\tconst handle = drag.handle;\n\n\t// Determine the anchor corner (opposite to the dragged corner).\n\tconst anchorX = handle === 'nw' || handle === 'sw' ? s.x + s.width : s.x;\n\tconst anchorY = handle === 'nw' || handle === 'ne' ? s.y + s.height : s.y;\n\n\t// Direction the crop grows from the anchor (+1 = right/down, -1 = left/up).\n\tconst dirX = handle === 'nw' || handle === 'sw' ? -1 : 1;\n\tconst dirY = handle === 'nw' || handle === 'ne' ? -1 : 1;\n\n\t// Desired new position of the dragged corner.\n\tconst draggedX =\n\t\t( handle === 'nw' || handle === 'sw' ? s.x : s.x + s.width ) + dx;\n\tconst draggedY =\n\t\t( handle === 'nw' || handle === 'ne' ? s.y : s.y + s.height ) + dy;\n\n\t// Raw distances from anchor to dragged corner.\n\tlet distW = ( draggedX - anchorX ) * dirX;\n\tlet distH = ( draggedY - anchorY ) * dirY;\n\n\t// Enforce minimum size on both axes, projecting through the ratio so\n\t// neither axis falls below its per-axis floor.\n\tconst minDistW = Math.max(\n\t\tminCropSize.width,\n\t\tminCropSize.height * normalizedRatio\n\t);\n\tconst minDistH = minDistW / normalizedRatio;\n\tdistW = Math.max( distW, minDistW );\n\tdistH = Math.max( distH, minDistH );\n\n\t// Determine which axis \"drives\". Pointer drags use the dragged-corner\n\t// geometry so the projected crop changes continuously as the pointer moves.\n\t// Keyboard resize passes an explicit axis because a single-axis arrow step\n\t// should drive that axis instead of being cancelled by the unchanged axis.\n\tconst pixelRatio = ( normalizedRatio * imageSize.width ) / imageSize.height;\n\tconst isWidthDriver =\n\t\tdriverAxis === 'width' ||\n\t\t( ! driverAxis &&\n\t\t\t( distW * imageSize.width ) / ( distH * imageSize.height ) >\n\t\t\t\tpixelRatio );\n\n\tif ( isWidthDriver ) {\n\t\t// Width is the driver — compute height from ratio.\n\t\tdistH = distW / normalizedRatio;\n\t} else {\n\t\t// Height is the driver — compute width from ratio.\n\t\tdistW = distH * normalizedRatio;\n\t}\n\n\t// Clamp to image coverage bounds. If the rect would exceed,\n\t// shrink it (maintaining ratio) to fit.\n\tconst maxW = dirX > 0 ? bounds.maxX - anchorX : anchorX - bounds.minX;\n\tconst maxH = dirY > 0 ? bounds.maxY - anchorY : anchorY - bounds.minY;\n\n\tif ( distW > maxW ) {\n\t\tdistW = maxW;\n\t\tdistH = distW / normalizedRatio;\n\t}\n\tif ( distH > maxH ) {\n\t\tdistH = maxH;\n\t\tdistW = distH * normalizedRatio;\n\t}\n\n\t// Enforce minimum after clamping.\n\tdistW = Math.max( distW, minDistW );\n\tdistH = Math.max( distH, minDistH );\n\n\t// Compute the final rect position from the anchor.\n\tconst newX = dirX > 0 ? anchorX : anchorX - distW;\n\tconst newY = dirY > 0 ? anchorY : anchorY - distH;\n\n\treturn { x: newX, y: newY, width: distW, height: distH };\n}\n\n/**\n * Compute the new crop rect for a resize that temporarily locks the\n * aspect ratio to `drag.startRect`'s ratio (e.g. while Shift is held).\n *\n * Corner handles delegate to {@link computeLockedResizeRect}. Edge\n * handles size the dragged axis freely, then expand the perpendicular\n * axis symmetrically around the rect's center to preserve the ratio,\n * clamping symmetrically so the rect stays within bounds.\n *\n * @param drag The current drag state.\n * @param clientX Current mouse/touch X position in pixels.\n * @param clientY Current mouse/touch Y position in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param bounds The allowed crop area bounds.\n * @param minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @return The new crop rect in normalized coordinates.\n */\nexport function computeShiftLockedResizeRect(\n\tdrag: ResizeDragState,\n\tclientX: number,\n\tclientY: number,\n\timageSize: Size,\n\tbounds: CropBounds,\n\tminCropSize: Size = DEFAULT_MIN_CROP_SIZE\n): NormalizedRect {\n\tconst s = drag.startRect;\n\tconst pixelW = s.width * imageSize.width;\n\tconst pixelH = s.height * imageSize.height;\n\tif ( pixelH <= 0 || pixelW <= 0 ) {\n\t\treturn computeFreeResizeRect(\n\t\t\tdrag,\n\t\t\tclientX,\n\t\t\tclientY,\n\t\t\timageSize,\n\t\t\tbounds,\n\t\t\tminCropSize\n\t\t);\n\t}\n\tconst normalizedRatio = s.width / s.height;\n\n\tconst handle = drag.handle;\n\tif (\n\t\thandle === 'nw' ||\n\t\thandle === 'ne' ||\n\t\thandle === 'sw' ||\n\t\thandle === 'se'\n\t) {\n\t\treturn computeLockedResizeRect(\n\t\t\tdrag,\n\t\t\tclientX,\n\t\t\tclientY,\n\t\t\timageSize,\n\t\t\tbounds,\n\t\t\tnormalizedRatio,\n\t\t\tminCropSize\n\t\t);\n\t}\n\n\t// Edge handle: size the dragged axis with free-resize logic, then\n\t// expand the perpendicular axis symmetrically around the rect's center.\n\tconst free = computeFreeResizeRect(\n\t\tdrag,\n\t\tclientX,\n\t\tclientY,\n\t\timageSize,\n\t\tbounds,\n\t\tminCropSize\n\t);\n\n\tif ( handle === 'n' || handle === 's' ) {\n\t\t// Height is the driver; derive width from ratio.\n\t\tlet newHeight = free.height;\n\t\tlet newWidth = newHeight * normalizedRatio;\n\t\tconst centerX = s.x + s.width / 2;\n\t\t// Symmetric clamp: limit by the smaller of the two side gaps so\n\t\t// the rect stays centered around centerX.\n\t\tconst maxWidth =\n\t\t\tMath.min( centerX - bounds.minX, bounds.maxX - centerX ) * 2;\n\t\tif ( newWidth > maxWidth ) {\n\t\t\tnewWidth = maxWidth;\n\t\t\tnewHeight = newWidth / normalizedRatio;\n\t\t}\n\t\t// Enforce the minimum on the driving axis, then derive the\n\t\t// other axis from the ratio so it stays consistent. The\n\t\t// effective minimum height has to satisfy the per-axis floor\n\t\t// on both axes simultaneously.\n\t\tconst minHeight = Math.max(\n\t\t\tminCropSize.height,\n\t\t\tminCropSize.width / normalizedRatio\n\t\t);\n\t\tif ( newHeight < minHeight ) {\n\t\t\tnewHeight = minHeight;\n\t\t\tnewWidth = newHeight * normalizedRatio;\n\t\t}\n\t\t// Re-anchor the dragged axis to the opposite edge so the\n\t\t// height adjustment after clamping keeps that edge fixed.\n\t\tconst newY = handle === 'n' ? s.y + s.height - newHeight : s.y;\n\t\treturn {\n\t\t\tx: centerX - newWidth / 2,\n\t\t\ty: newY,\n\t\t\twidth: newWidth,\n\t\t\theight: newHeight,\n\t\t};\n\t}\n\n\t// 'e' or 'w': width is the driver; derive height from ratio.\n\tlet newWidth = free.width;\n\tlet newHeight = newWidth / normalizedRatio;\n\tconst centerY = s.y + s.height / 2;\n\tconst maxHeight =\n\t\tMath.min( centerY - bounds.minY, bounds.maxY - centerY ) * 2;\n\tif ( newHeight > maxHeight ) {\n\t\tnewHeight = maxHeight;\n\t\tnewWidth = newHeight * normalizedRatio;\n\t}\n\tconst minWidth = Math.max(\n\t\tminCropSize.width,\n\t\tminCropSize.height * normalizedRatio\n\t);\n\tif ( newWidth < minWidth ) {\n\t\tnewWidth = minWidth;\n\t\tnewHeight = newWidth / normalizedRatio;\n\t}\n\tconst newX = handle === 'w' ? s.x + s.width - newWidth : s.x;\n\treturn {\n\t\tx: newX,\n\t\ty: centerY - newHeight / 2,\n\t\twidth: newWidth,\n\t\theight: newHeight,\n\t};\n}\n"],
5
+ "mappings": ";AAGA,SAAS,iBAAiB,0BAA0B;AAwB7C,SAAS,iBAAkB,cAA+B;AAChE,MAAK,gBAAgB,GAAI;AACxB,WAAO;AAAA,EACR;AACA,SAAO,KAAK,IAAK,iBAAiB,qBAAqB,YAAa;AACrE;AAQO,IAAM,wBAA8B,EAAE,OAAO,MAAM,QAAQ,KAAK;AAuChE,SAAS,sBACf,MACA,SACA,SACA,WACA,QACA,cAAoB,uBACH;AACjB,QAAM,KACL,UAAU,QA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AY;AAAA,MACZ,YAAY,QAAQ;AAAA,IACrB;AACA,QAAKD,aAAY,WAAY;AAC5B,MAAAA,aAAY;AACZ,MAAAC,YAAWD,aAAY;AAAA,IACxB;AAGA,UAAM,OAAO,WAAW,MAAM,EAAE,IAAI,EAAE,SAASA,aAAY,EAAE;AAC7D,WAAO;AAAA,MACN,GAAG,UAAUC,YAAW;AAAA,MACxB,GAAG;AAAA,MACH,OAAOA;AAAA,MACP,QAAQD;AAAA,IACT;AAAA,EACD;AAGA,MAAI,WAAW,KAAK;AACpB,MAAI,YAAY,WAAW;AAC3B,QAAM,UAAU,EAAE,IAAI,EAAE,SAAS;AACjC,QAAM,YACL,KAAK,IAAK,UAAU,OAAO,MAAM,OAAO,OAAO,OAAQ,IAAI;AAC5D,MAAK,YAAY,WAAY;AAC5B,gBAAY;AACZ,eAAW,YAAY;AAAA,EACxB;AACA,QAAM,WAAW,KAAK;AAAA,IACrB,YAAY;AAAA,IACZ,YAAY,SAAS;AAAA,EACtB;AACA,MAAK,WAAW,UAAW;AAC1B,eAAW;AACX,gBAAY,WAAW;AAAA,EACxB;AACA,QAAM,OAAO,WAAW,MAAM,EAAE,IAAI,EAAE,QAAQ,WAAW,EAAE;AAC3D,SAAO;AAAA,IACN,GAAG;AAAA,IACH,GAAG,UAAU,YAAY;AAAA,IACzB,OAAO;AAAA,IACP,QAAQ;AAAA,EACT;AACD;",
6
6
  "names": ["newHeight", "newWidth"]
7
7
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/image-editor/core/transform-style.ts"],
4
- "sourcesContent": ["/**\n * Internal dependencies\n */\nimport type { CropperState, Size } from './types';\nimport { degreesToRadians } from './math/rotation';\nimport { isValidSize, sanitizeCropperState } from './math/sanitize';\n\n/** Identity CSS transform \u2014 applied when inputs aren't safe to compose. */\nconst IDENTITY_MATRIX_STYLE = 'matrix(1, 0, 0, 1, 0, 0)';\n\n/**\n * Computes a CSS matrix() transform string from the cropper state.\n *\n * The combined transform is: translate(tx, ty) * scale(sx, sy) * rotate(r) * scale(z)\n * expressed as a 2D CSS matrix(a, b, c, d, tx, ty).\n *\n * Flip is composed outside rotation so it is viewport-relative \u2014 horizontal\n * flip mirrors across the viewport's vertical axis regardless of rotation.\n * Must match the matrix order in `createCamera` / `getImageCropBounds`.\n *\n * This is a pure function with no framework dependencies. The React hook\n * `useTransformStyle` wraps this in `useMemo` for memoization.\n *\n * @param state The current cropper state.\n * @param imageSize The rendered image dimensions in pixels.\n * @return A CSS transform string.\n */\nexport function computeTransformStyle(\n\tstate: CropperState,\n\timageSize: Size\n): string {\n\t// Match the identity short-circuit used by `createCamera` and\n\t// `getSourceRegion` so the preview path stays consistent with the math\n\t// path when `imageSize` itself is hostile (state sanitization below\n\t// only guards the state fields, not these size arguments).\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn IDENTITY_MATRIX_STYLE;\n\t}\n\t// Sanitize so hostile state can't produce `matrix(NaN, NaN, ...)` here\n\t// while the parallel `createCamera` path returns a finite matrix. Both\n\t// paths must agree under defense-in-depth conditions.\n\tconst safeState = sanitizeCropperState( state );\n\tconst translateX = safeState.pan.x * imageSize.width;\n\tconst translateY = safeState.pan.y * imageSize.height;\n\tconst rad = degreesToRadians( safeState.rotation );\n\tconst cos = Math.cos( rad );\n\tconst sin = Math.sin( rad );\n\tconst sx = safeState.flip.horizontal ? -1 : 1;\n\tconst sy = safeState.flip.vertical ? -1 : 1;\n\tconst z = safeState.zoom;\n\n\t// Combined: translate(tx,ty) * scale(sx,sy) * rotate(r) * scale(z)\n\tconst a = sx * cos * z;\n\tconst b = sy * sin * z;\n\tconst c = -sx * sin * z;\n\tconst d = sy * cos * z;\n\n\treturn `matrix(${ a }, ${ b }, ${ c }, ${ d }, ${ translateX }, ${ translateY })`;\n}\n"],
4
+ "sourcesContent": ["/**\n * Internal dependencies\n */\nimport type { CropperState, Size } from './types';\nimport { degreesToRadians } from './math/rotation';\nimport { isValidSize, sanitizeCropperState } from './math/sanitize';\n\n/** Identity CSS transform — applied when inputs aren't safe to compose. */\nconst IDENTITY_MATRIX_STYLE = 'matrix(1, 0, 0, 1, 0, 0)';\n\n/**\n * Computes a CSS matrix() transform string from the cropper state.\n *\n * The combined transform is: translate(tx, ty) * scale(sx, sy) * rotate(r) * scale(z)\n * expressed as a 2D CSS matrix(a, b, c, d, tx, ty).\n *\n * Flip is composed outside rotation so it is viewport-relative — horizontal\n * flip mirrors across the viewport's vertical axis regardless of rotation.\n * Must match the matrix order in `createCamera` / `getImageCropBounds`.\n *\n * This is a pure function with no framework dependencies. The React hook\n * `useTransformStyle` wraps this in `useMemo` for memoization.\n *\n * @param state The current cropper state.\n * @param imageSize The rendered image dimensions in pixels.\n * @return A CSS transform string.\n */\nexport function computeTransformStyle(\n\tstate: CropperState,\n\timageSize: Size\n): string {\n\t// Match the identity short-circuit used by `createCamera` and\n\t// `getSourceRegion` so the preview path stays consistent with the math\n\t// path when `imageSize` itself is hostile (state sanitization below\n\t// only guards the state fields, not these size arguments).\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn IDENTITY_MATRIX_STYLE;\n\t}\n\t// Sanitize so hostile state can't produce `matrix(NaN, NaN, ...)` here\n\t// while the parallel `createCamera` path returns a finite matrix. Both\n\t// paths must agree under defense-in-depth conditions.\n\tconst safeState = sanitizeCropperState( state );\n\tconst translateX = safeState.pan.x * imageSize.width;\n\tconst translateY = safeState.pan.y * imageSize.height;\n\tconst rad = degreesToRadians( safeState.rotation );\n\tconst cos = Math.cos( rad );\n\tconst sin = Math.sin( rad );\n\tconst sx = safeState.flip.horizontal ? -1 : 1;\n\tconst sy = safeState.flip.vertical ? -1 : 1;\n\tconst z = safeState.zoom;\n\n\t// Combined: translate(tx,ty) * scale(sx,sy) * rotate(r) * scale(z)\n\tconst a = sx * cos * z;\n\tconst b = sy * sin * z;\n\tconst c = -sx * sin * z;\n\tconst d = sy * cos * z;\n\n\treturn `matrix(${ a }, ${ b }, ${ c }, ${ d }, ${ translateX }, ${ translateY })`;\n}\n"],
5
5
  "mappings": ";AAIA,SAAS,wBAAwB;AACjC,SAAS,aAAa,4BAA4B;AAGlD,IAAM,wBAAwB;AAmBvB,SAAS,sBACf,OACA,WACS;AAKT,MAAK,CAAE,YAAa,SAAU,GAAI;AACjC,WAAO;AAAA,EACR;AAIA,QAAM,YAAY,qBAAsB,KAAM;AAC9C,QAAM,aAAa,UAAU,IAAI,IAAI,UAAU;AAC/C,QAAM,aAAa,UAAU,IAAI,IAAI,UAAU;AAC/C,QAAM,MAAM,iBAAkB,UAAU,QAAS;AACjD,QAAM,MAAM,KAAK,IAAK,GAAI;AAC1B,QAAM,MAAM,KAAK,IAAK,GAAI;AAC1B,QAAM,KAAK,UAAU,KAAK,aAAa,KAAK;AAC5C,QAAM,KAAK,UAAU,KAAK,WAAW,KAAK;AAC1C,QAAM,IAAI,UAAU;AAGpB,QAAM,IAAI,KAAK,MAAM;AACrB,QAAM,IAAI,KAAK,MAAM;AACrB,QAAM,IAAI,CAAC,KAAK,MAAM;AACtB,QAAM,IAAI,KAAK,MAAM;AAErB,SAAO,UAAW,CAAE,KAAM,CAAE,KAAM,CAAE,KAAM,CAAE,KAAM,UAAW,KAAM,UAAW;AAC/E;",
6
6
  "names": []
7
7
  }
@@ -11,10 +11,15 @@ import {
11
11
  forwardRef
12
12
  } from "@wordpress/element";
13
13
  import { __ } from "@wordpress/i18n";
14
- import { getImageFit, getRotatedBBox } from "../../core/camera.mjs";
14
+ import { getImageFit, getRotatedBBox, getViewScale } from "../../core/camera.mjs";
15
15
  import { getImageCropBounds, getMinZoom } from "../../core/containment.mjs";
16
- import { MIN_CROP_PIXELS } from "../../core/constants.mjs";
16
+ import {
17
+ MAX_VIEW_SCALE,
18
+ PIXEL_SNAP_DISPLAY_SCALE,
19
+ SETTLE_TARGET_CANVAS_FILL
20
+ } from "../../core/constants.mjs";
17
21
  import { computeInscribedRect } from "../../core/crop-rect.mjs";
22
+ import { getMinCropPixels } from "../../core/stencil-math.mjs";
18
23
  import { useInteraction } from "../hooks/use-interaction.mjs";
19
24
  import { useTransformStyle } from "../hooks/use-transform-style.mjs";
20
25
  import { useAriaAnnouncer } from "../hooks/use-aria-announcer.mjs";
@@ -22,11 +27,25 @@ import { RectangleStencil } from "./stencils/rectangle-stencil.mjs";
22
27
  import { DimmingOverlay } from "./overlays/dimming-overlay.mjs";
23
28
  import { GridOverlay } from "./overlays/grid-overlay.mjs";
24
29
  import { DimensionsOverlay } from "./overlays/dimensions-overlay.mjs";
25
- import { getSourceRegion } from "../../core/source-region.mjs";
30
+ import {
31
+ getSourceRegion,
32
+ snapCropRectToSourcePixelGrid,
33
+ snapCropRectToSourcePixels
34
+ } from "../../core/source-region.mjs";
26
35
  import { ViewportProvider, useViewport } from "./viewport-provider.mjs";
27
36
  import { VISUALLY_HIDDEN_STYLE } from "../visually-hidden-style.mjs";
28
37
  import { jsx, jsxs } from "react/jsx-runtime";
29
38
  var CROP_RECT_EPSILON = 1e-6;
39
+ var CARDINAL_ROTATION_EPSILON = 1e-6;
40
+ var SETTLE_TRANSITION_DURATION_MS = 200;
41
+ var SETTLE_TRANSITION_FALLBACK_MS = SETTLE_TRANSITION_DURATION_MS + 100;
42
+ var SETTLE_TRANSFORM_TRANSITION = `transform ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`;
43
+ var SETTLE_STENCIL_TRANSITION = [
44
+ `left ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`,
45
+ `top ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`,
46
+ `width ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`,
47
+ `height ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`
48
+ ].join(", ");
30
49
  function CropperInner({
31
50
  src,
32
51
  controller,
@@ -132,24 +151,6 @@ function CropperInner({
132
151
  ),
133
152
  [canvasSize, naturalWidth, naturalHeight, state.rotation]
134
153
  );
135
- const minCropSize = useMemo(() => {
136
- if (naturalWidth <= 0 || naturalHeight <= 0) {
137
- return void 0;
138
- }
139
- const snapRotation = Math.round(state.rotation / 90) * 90;
140
- const bbox = getRotatedBBox(
141
- naturalWidth,
142
- naturalHeight,
143
- snapRotation
144
- );
145
- return {
146
- width: Math.min(1, MIN_CROP_PIXELS * state.zoom / bbox.width),
147
- height: Math.min(
148
- 1,
149
- MIN_CROP_PIXELS * state.zoom / bbox.height
150
- )
151
- };
152
- }, [naturalWidth, naturalHeight, state.rotation, state.zoom]);
153
154
  useEffect(() => {
154
155
  setVisualSize(visualSize);
155
156
  }, [visualSize, setVisualSize]);
@@ -202,16 +203,133 @@ function CropperInner({
202
203
  const [isResizing, setIsResizing] = useState(false);
203
204
  const isResizingRef = useRef(false);
204
205
  const isSettlingRef = useRef(false);
206
+ const [isTouchPinching, setIsTouchPinchingState] = useState(false);
207
+ const isTouchPinchingRef = useRef(false);
208
+ const setTouchPinching = useCallback((isPinching) => {
209
+ isTouchPinchingRef.current = isPinching;
210
+ setIsTouchPinchingState(isPinching);
211
+ }, []);
205
212
  const [activeHandle, setActiveHandle] = useState(
206
213
  null
207
214
  );
215
+ const viewScaleRest = useMemo(
216
+ () => getViewScale(
217
+ state.cropRect,
218
+ canvasSize,
219
+ visualSize,
220
+ SETTLE_TARGET_CANVAS_FILL,
221
+ MAX_VIEW_SCALE
222
+ ),
223
+ [state.cropRect, canvasSize, visualSize]
224
+ );
225
+ const [frozenViewScale, setFrozenViewScale] = useState(1);
226
+ const viewScale = isResizing ? frozenViewScale : viewScaleRest;
227
+ const scaledVisualSize = useMemo(
228
+ () => ({
229
+ width: visualSize.width * viewScale,
230
+ height: visualSize.height * viewScale
231
+ }),
232
+ [visualSize.width, visualSize.height, viewScale]
233
+ );
234
+ const displayScale = naturalWidth > 0 ? elementSize.width / naturalWidth * state.zoom * viewScale : 0;
235
+ const keyboardResizeStep = useMemo(() => {
236
+ if (displayScale < PIXEL_SNAP_DISPLAY_SCALE || aspectRatio && aspectRatio > 0 || naturalWidth <= 0 || naturalHeight <= 0) {
237
+ return void 0;
238
+ }
239
+ const snapRotation = Math.round(state.rotation / 90) * 90;
240
+ if (Math.abs(state.rotation - snapRotation) >= CARDINAL_ROTATION_EPSILON) {
241
+ return void 0;
242
+ }
243
+ const bbox = getRotatedBBox(
244
+ naturalWidth,
245
+ naturalHeight,
246
+ snapRotation
247
+ );
248
+ if (bbox.width <= 0 || bbox.height <= 0) {
249
+ return void 0;
250
+ }
251
+ return {
252
+ width: state.zoom / bbox.width,
253
+ height: state.zoom / bbox.height
254
+ };
255
+ }, [
256
+ displayScale,
257
+ aspectRatio,
258
+ naturalWidth,
259
+ naturalHeight,
260
+ state.rotation,
261
+ state.zoom
262
+ ]);
263
+ const minCropSize = useMemo(() => {
264
+ if (naturalWidth <= 0 || naturalHeight <= 0) {
265
+ return void 0;
266
+ }
267
+ const snapRotation = Math.round(state.rotation / 90) * 90;
268
+ const bbox = getRotatedBBox(
269
+ naturalWidth,
270
+ naturalHeight,
271
+ snapRotation
272
+ );
273
+ const minPixels = getMinCropPixels(displayScale);
274
+ return {
275
+ width: Math.min(1, minPixels * state.zoom / bbox.width),
276
+ height: Math.min(1, minPixels * state.zoom / bbox.height)
277
+ };
278
+ }, [
279
+ naturalWidth,
280
+ naturalHeight,
281
+ state.rotation,
282
+ state.zoom,
283
+ displayScale
284
+ ]);
285
+ const snapCropRect = useCallback(
286
+ (rect, handle) => {
287
+ if (displayScale < PIXEL_SNAP_DISPLAY_SCALE || naturalWidth <= 0 || naturalHeight <= 0) {
288
+ return rect;
289
+ }
290
+ return snapCropRectToSourcePixels(
291
+ state,
292
+ { width: naturalWidth, height: naturalHeight },
293
+ rect,
294
+ handle
295
+ );
296
+ },
297
+ [displayScale, naturalWidth, naturalHeight, state]
298
+ );
299
+ const wasPixelSnapEnabledRef = useRef(false);
300
+ useEffect(() => {
301
+ const isPixelSnapEnabled = freeformCrop && (!aspectRatio || aspectRatio <= 0) && displayScale >= PIXEL_SNAP_DISPLAY_SCALE && naturalWidth > 0 && naturalHeight > 0;
302
+ const wasPixelSnapEnabled = wasPixelSnapEnabledRef.current;
303
+ wasPixelSnapEnabledRef.current = isPixelSnapEnabled;
304
+ if (!isPixelSnapEnabled || wasPixelSnapEnabled) {
305
+ return;
306
+ }
307
+ const snappedCropRect = snapCropRectToSourcePixelGrid(
308
+ state,
309
+ { width: naturalWidth, height: naturalHeight },
310
+ state.cropRect
311
+ );
312
+ const currentCropRect = state.cropRect;
313
+ if (Math.abs(currentCropRect.x - snappedCropRect.x) < CROP_RECT_EPSILON && Math.abs(currentCropRect.y - snappedCropRect.y) < CROP_RECT_EPSILON && Math.abs(currentCropRect.width - snappedCropRect.width) < CROP_RECT_EPSILON && Math.abs(currentCropRect.height - snappedCropRect.height) < CROP_RECT_EPSILON) {
314
+ return;
315
+ }
316
+ setCropRect(snappedCropRect);
317
+ }, [
318
+ aspectRatio,
319
+ displayScale,
320
+ freeformCrop,
321
+ naturalWidth,
322
+ naturalHeight,
323
+ setCropRect,
324
+ state
325
+ ]);
208
326
  const {
209
327
  handlers,
210
328
  onWheelNative,
211
329
  isDragging,
212
330
  isZooming,
213
331
  isPlacementActive: isInteractionPlacementActive
214
- } = useInteraction(state, controller, canvasSize, visualSize, {
332
+ } = useInteraction(state, controller, canvasSize, scaledVisualSize, {
215
333
  minZoom: effectiveMinZoom,
216
334
  maxZoom,
217
335
  onGestureStart,
@@ -222,12 +340,36 @@ function CropperInner({
222
340
  onPointerDownCapture: () => {
223
341
  setIsFocusVisible(false);
224
342
  },
343
+ onTouchStartCapture: (event) => {
344
+ if (event.touches.length > 1) {
345
+ setTouchPinching(true);
346
+ }
347
+ },
348
+ onTouchMoveCapture: (event) => {
349
+ if (event.touches.length > 1) {
350
+ setTouchPinching(true);
351
+ }
352
+ },
353
+ onTouchEndCapture: (event) => {
354
+ if (event.touches.length === 0) {
355
+ setTouchPinching(false);
356
+ }
357
+ },
358
+ onTouchCancelCapture: (event) => {
359
+ if (event.touches.length === 0) {
360
+ setTouchPinching(false);
361
+ }
362
+ },
225
363
  onKeyDownCapture: (event) => {
226
364
  if (!["Shift", "Control", "Alt", "Meta"].includes(event.key)) {
227
365
  setIsFocusVisible(true);
228
366
  }
229
367
  },
230
368
  onPointerDown: (event) => {
369
+ if (isResizingRef.current || isTouchPinchingRef.current || event.pointerType === "touch" && event.isPrimary === false) {
370
+ event.preventDefault();
371
+ return;
372
+ }
231
373
  handlers.onPointerDown?.(event);
232
374
  setIsFocusVisible(false);
233
375
  }
@@ -270,25 +412,28 @@ function CropperInner({
270
412
  );
271
413
  const handleCropChange = useCallback(
272
414
  (rect) => {
415
+ if (isTouchPinchingRef.current) {
416
+ return;
417
+ }
273
418
  setCropRect(rect);
274
- if (isResizingRef.current && visualSize.width > 0 && visualSize.height > 0) {
275
- const offsetX = (canvasSize.width - visualSize.width) / 2;
276
- const offsetY = (canvasSize.height - visualSize.height) / 2;
419
+ if (isResizingRef.current && scaledVisualSize.width > 0 && scaledVisualSize.height > 0) {
420
+ const offsetX = (canvasSize.width - scaledVisualSize.width) / 2;
421
+ const offsetY = (canvasSize.height - scaledVisualSize.height) / 2;
277
422
  const rightOverflow = Math.max(
278
423
  0,
279
- offsetX + (rect.x + rect.width) * visualSize.width - canvasSize.width
424
+ offsetX + (rect.x + rect.width) * scaledVisualSize.width - canvasSize.width
280
425
  );
281
426
  const leftOverflow = Math.max(
282
427
  0,
283
- -(offsetX + rect.x * visualSize.width)
428
+ -(offsetX + rect.x * scaledVisualSize.width)
284
429
  );
285
430
  const bottomOverflow = Math.max(
286
431
  0,
287
- offsetY + (rect.y + rect.height) * visualSize.height - canvasSize.height
432
+ offsetY + (rect.y + rect.height) * scaledVisualSize.height - canvasSize.height
288
433
  );
289
434
  const topOverflow = Math.max(
290
435
  0,
291
- -(offsetY + rect.y * visualSize.height)
436
+ -(offsetY + rect.y * scaledVisualSize.height)
292
437
  );
293
438
  setViewportPan({
294
439
  x: -rightOverflow + leftOverflow,
@@ -296,10 +441,15 @@ function CropperInner({
296
441
  });
297
442
  }
298
443
  },
299
- [setCropRect, setViewportPan, canvasSize, visualSize]
444
+ [setCropRect, setViewportPan, canvasSize, scaledVisualSize]
300
445
  );
301
446
  const [settling, setSettling] = useState(false);
302
447
  const settleTimerRef = useRef();
448
+ const finishSettling = useCallback(() => {
449
+ clearTimeout(settleTimerRef.current);
450
+ isSettlingRef.current = false;
451
+ setSettling(false);
452
+ }, []);
303
453
  useEffect(() => {
304
454
  return () => {
305
455
  clearTimeout(settleTimerRef.current);
@@ -323,39 +473,56 @@ function CropperInner({
323
473
  }, []);
324
474
  const handleResizeStart = useCallback(
325
475
  (handle) => {
476
+ if (isTouchPinchingRef.current) {
477
+ return;
478
+ }
479
+ setFrozenViewScale(viewScaleRest);
326
480
  isResizingRef.current = true;
327
481
  setIsResizing(true);
328
482
  setActiveHandle(handle ?? null);
329
- clearTimeout(settleTimerRef.current);
330
- isSettlingRef.current = false;
331
- setSettling(false);
483
+ finishSettling();
332
484
  resetViewport();
333
485
  onGestureStart?.();
334
486
  },
335
- [onGestureStart, resetViewport]
487
+ [finishSettling, onGestureStart, resetViewport, viewScaleRest]
336
488
  );
337
489
  const handleResizeEnd = useCallback(() => {
490
+ const isCancellingForPinch = isTouchPinchingRef.current;
338
491
  isResizingRef.current = false;
339
492
  setIsResizing(false);
340
493
  setActiveHandle(null);
494
+ clearTimeout(settleTimerRef.current);
495
+ if (isCancellingForPinch) {
496
+ finishSettling();
497
+ resetViewport();
498
+ return;
499
+ }
341
500
  isSettlingRef.current = true;
342
501
  setSettling(true);
343
502
  resetViewport();
344
503
  settleCrop();
345
504
  onGestureEnd?.();
346
- clearTimeout(settleTimerRef.current);
347
- settleTimerRef.current = setTimeout(() => {
348
- isSettlingRef.current = false;
349
- setSettling(false);
350
- }, 200);
351
- }, [settleCrop, onGestureEnd, resetViewport]);
505
+ settleTimerRef.current = setTimeout(
506
+ finishSettling,
507
+ SETTLE_TRANSITION_FALLBACK_MS
508
+ );
509
+ }, [finishSettling, settleCrop, onGestureEnd, resetViewport]);
510
+ const handleSettleTransitionEnd = useCallback(
511
+ (event) => {
512
+ if (!isSettlingRef.current || event.propertyName !== "transform") {
513
+ return;
514
+ }
515
+ finishSettling();
516
+ },
517
+ [finishSettling]
518
+ );
352
519
  let imageTransition;
353
520
  if (settling) {
354
- imageTransition = "transform 200ms ease-out";
521
+ imageTransition = SETTLE_TRANSFORM_TRANSITION;
355
522
  } else if (isZooming) {
356
523
  imageTransition = "transform 150ms linear";
357
524
  }
358
- const settleStencilTransition = settling ? "left 200ms ease-out, top 200ms ease-out, width 200ms ease-out, height 200ms ease-out" : void 0;
525
+ const settleStencilTransition = settling ? SETTLE_STENCIL_TRANSITION : void 0;
359
526
  const imageStyle = useMemo(() => {
360
527
  if (elementSize.width === 0 || elementSize.height === 0) {
361
528
  return {};
@@ -369,11 +536,25 @@ function CropperInner({
369
536
  maxHeight: elementSize.height,
370
537
  left: centerX,
371
538
  top: centerY,
372
- transform: transformString,
373
- transition: imageTransition
539
+ // Always lead with `scale()` (identity at rest) so the transform's
540
+ // function list stays structurally constant across rest, resize, and
541
+ // settle. A conditional that dropped `scale()` at viewScale === 1
542
+ // would change the list shape exactly when the settle transition
543
+ // crosses 1:1, forcing the browser to fall back to matrix-decomposition
544
+ // interpolation instead of interpolating each function in turn.
545
+ transform: `scale(${viewScale}) ${transformString}`,
546
+ transition: imageTransition,
547
+ imageRendering: displayScale > 1 ? "pixelated" : void 0
374
548
  };
375
- }, [canvasSize, elementSize, transformString, imageTransition]);
376
- const settleTransition = settling ? "transform 200ms ease-out" : void 0;
549
+ }, [
550
+ canvasSize,
551
+ elementSize,
552
+ transformString,
553
+ imageTransition,
554
+ displayScale,
555
+ viewScale
556
+ ]);
557
+ const settleTransition = settling ? SETTLE_TRANSFORM_TRANSITION : void 0;
377
558
  const willChange = isResizing || settling ? "transform" : void 0;
378
559
  let stageStyle;
379
560
  if (viewportState.pan.x !== 0 || viewportState.pan.y !== 0) {
@@ -383,7 +564,11 @@ function CropperInner({
383
564
  willChange
384
565
  };
385
566
  } else if (settling) {
386
- stageStyle = { transition: settleTransition, willChange };
567
+ stageStyle = {
568
+ transform: "translate(0px, 0px)",
569
+ transition: settleTransition,
570
+ willChange
571
+ };
387
572
  }
388
573
  const setContainerRef = useCallback(
389
574
  (element) => {
@@ -444,11 +629,13 @@ function CropperInner({
444
629
  className: "wp-media-editor-image-editor__stage",
445
630
  "data-testid": "cropper-stage",
446
631
  style: stageStyle,
632
+ onTransitionEnd: handleSettleTransitionEnd,
447
633
  children: [
448
634
  /* @__PURE__ */ jsx(
449
635
  "img",
450
636
  {
451
637
  className: "wp-media-editor-image-editor__image",
638
+ "data-testid": "cropper-image",
452
639
  src,
453
640
  alt: "",
454
641
  onLoad: handleImageLoad,
@@ -461,7 +648,7 @@ function CropperInner({
461
648
  {
462
649
  cropRect: state.cropRect,
463
650
  containerSize: canvasSize,
464
- imageSize: visualSize,
651
+ imageSize: scaledVisualSize,
465
652
  transition: settleStencilTransition
466
653
  }
467
654
  ),
@@ -470,16 +657,19 @@ function CropperInner({
470
657
  {
471
658
  cropRect: state.cropRect,
472
659
  containerSize: canvasSize,
473
- imageSize: visualSize,
660
+ imageSize: scaledVisualSize,
474
661
  onCropChange: handleCropChange,
475
662
  onResizeStart: handleResizeStart,
476
663
  onResizeEnd: handleResizeEnd,
477
664
  onEscape: handleEscape,
478
665
  aspectRatio,
479
666
  freeformCrop,
667
+ isResizeDisabled: isTouchPinching,
480
668
  stencilTransition: settleStencilTransition,
481
669
  cropBounds,
482
- minCropSize
670
+ minCropSize,
671
+ snapCropRect,
672
+ keyboardResizeStep
483
673
  }
484
674
  ),
485
675
  (showGrid === true || isInteractiveGrid) && /* @__PURE__ */ jsx(
@@ -487,7 +677,7 @@ function CropperInner({
487
677
  {
488
678
  cropRect: state.cropRect,
489
679
  containerSize: canvasSize,
490
- imageSize: visualSize
680
+ imageSize: scaledVisualSize
491
681
  }
492
682
  ),
493
683
  activeHandle && outputSize && /* @__PURE__ */ jsx(
@@ -495,7 +685,7 @@ function CropperInner({
495
685
  {
496
686
  cropRect: state.cropRect,
497
687
  containerSize: canvasSize,
498
- imageSize: visualSize,
688
+ imageSize: scaledVisualSize,
499
689
  activeHandle,
500
690
  outputWidth: outputSize.width,
501
691
  outputHeight: outputSize.height