@wordpress/media-editor 0.11.1 → 0.11.2-next.v.202606191442.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/build/components/media-editor/index.cjs.map +1 -1
  2. package/build/components/media-editor/use-crop-options.cjs.map +1 -1
  3. package/build/components/media-editor-canvas/index.cjs +63 -15
  4. package/build/components/media-editor-canvas/index.cjs.map +3 -3
  5. package/build/components/media-editor-fine-rotation/index.cjs.map +1 -1
  6. package/build/components/media-editor-image-controls/index.cjs +2 -2
  7. package/build/components/media-editor-image-controls/index.cjs.map +2 -2
  8. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +4 -4
  9. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +2 -2
  10. package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -1
  11. package/build/components/rotation-ruler/index.cjs +1 -1
  12. package/build/components/rotation-ruler/index.cjs.map +1 -1
  13. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -1
  14. package/build/image-editor/core/camera.cjs +13 -0
  15. package/build/image-editor/core/camera.cjs.map +2 -2
  16. package/build/image-editor/core/constants.cjs +12 -3
  17. package/build/image-editor/core/constants.cjs.map +2 -2
  18. package/build/image-editor/core/containment.cjs.map +1 -1
  19. package/build/image-editor/core/crop-rect.cjs.map +1 -1
  20. package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -1
  21. package/build/image-editor/core/interaction-controller.cjs +9 -7
  22. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  23. package/build/image-editor/core/math/sanitize.cjs.map +1 -1
  24. package/build/image-editor/core/setter-helpers.cjs.map +1 -1
  25. package/build/image-editor/core/source-region.cjs +111 -2
  26. package/build/image-editor/core/source-region.cjs.map +2 -2
  27. package/build/image-editor/core/state.cjs.map +1 -1
  28. package/build/image-editor/core/stencil-math.cjs +3 -4
  29. package/build/image-editor/core/stencil-math.cjs.map +2 -2
  30. package/build/image-editor/core/transform-style.cjs.map +1 -1
  31. package/build/image-editor/core/types.cjs.map +1 -1
  32. package/build/image-editor/react/components/cropper.cjs +232 -55
  33. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  34. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -1
  35. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +1 -0
  36. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
  37. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +65 -19
  38. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  39. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -1
  40. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +1 -1
  41. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +2 -2
  42. package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -1
  43. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -1
  44. package/build/index.cjs.map +1 -1
  45. package/build/state/types.cjs.map +1 -1
  46. package/build/state/use-media-editor-state.cjs +1 -1
  47. package/build/state/use-media-editor-state.cjs.map +2 -2
  48. package/build-module/components/media-editor/index.mjs.map +1 -1
  49. package/build-module/components/media-editor/use-crop-options.mjs.map +1 -1
  50. package/build-module/components/media-editor-canvas/index.mjs +55 -17
  51. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  52. package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -1
  53. package/build-module/components/media-editor-image-controls/index.mjs +2 -2
  54. package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
  55. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +4 -4
  56. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +2 -2
  57. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +1 -1
  58. package/build-module/components/rotation-ruler/index.mjs +1 -1
  59. package/build-module/components/rotation-ruler/index.mjs.map +1 -1
  60. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -1
  61. package/build-module/image-editor/core/camera.mjs +12 -0
  62. package/build-module/image-editor/core/camera.mjs.map +2 -2
  63. package/build-module/image-editor/core/constants.mjs +8 -2
  64. package/build-module/image-editor/core/constants.mjs.map +2 -2
  65. package/build-module/image-editor/core/containment.mjs.map +1 -1
  66. package/build-module/image-editor/core/crop-rect.mjs.map +1 -1
  67. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -1
  68. package/build-module/image-editor/core/interaction-controller.mjs +9 -7
  69. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  70. package/build-module/image-editor/core/math/sanitize.mjs.map +1 -1
  71. package/build-module/image-editor/core/setter-helpers.mjs.map +1 -1
  72. package/build-module/image-editor/core/source-region.mjs +108 -1
  73. package/build-module/image-editor/core/source-region.mjs.map +2 -2
  74. package/build-module/image-editor/core/state.mjs.map +1 -1
  75. package/build-module/image-editor/core/stencil-math.mjs +3 -4
  76. package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
  77. package/build-module/image-editor/core/transform-style.mjs.map +1 -1
  78. package/build-module/image-editor/react/components/cropper.mjs +242 -57
  79. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  80. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -1
  81. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +1 -0
  82. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
  83. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +65 -19
  84. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  85. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -1
  86. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +1 -1
  87. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +2 -2
  88. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +1 -1
  89. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -1
  90. package/build-module/index.mjs.map +1 -1
  91. package/build-module/state/types.mjs.map +1 -1
  92. package/build-module/state/use-media-editor-state.mjs +1 -1
  93. package/build-module/state/use-media-editor-state.mjs.map +2 -2
  94. package/build-style/style-rtl.css +49 -6
  95. package/build-style/style.css +49 -6
  96. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  97. package/build-types/image-editor/core/camera.d.ts +25 -1
  98. package/build-types/image-editor/core/camera.d.ts.map +1 -1
  99. package/build-types/image-editor/core/constants.d.ts +20 -1
  100. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  101. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  102. package/build-types/image-editor/core/source-region.d.ts +23 -1
  103. package/build-types/image-editor/core/source-region.d.ts.map +1 -1
  104. package/build-types/image-editor/core/stencil-math.d.ts +10 -5
  105. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
  106. package/build-types/image-editor/core/types.d.ts +14 -0
  107. package/build-types/image-editor/core/types.d.ts.map +1 -1
  108. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  109. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
  110. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +17 -14
  111. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  112. package/package.json +25 -20
  113. package/src/components/media-editor/style.scss +21 -9
  114. package/src/components/media-editor-canvas/index.tsx +82 -13
  115. package/src/components/media-editor-canvas/style.scss +41 -0
  116. package/src/image-editor/core/camera.ts +60 -1
  117. package/src/image-editor/core/constants.ts +23 -1
  118. package/src/image-editor/core/interaction-controller.ts +25 -11
  119. package/src/image-editor/core/source-region.ts +202 -1
  120. package/src/image-editor/core/stencil-math.ts +23 -14
  121. package/src/image-editor/core/test/camera.ts +272 -1
  122. package/src/image-editor/core/test/interaction-controller.ts +100 -0
  123. package/src/image-editor/core/test/stencil-math.ts +120 -6
  124. package/src/image-editor/core/types.ts +17 -0
  125. package/src/image-editor/react/components/cropper.tsx +362 -72
  126. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +1 -0
  127. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +101 -32
  128. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +169 -17
  129. package/src/image-editor/react/components/test/cropper.tsx +689 -6
  130. package/tsconfig.tsbuildinfo +1 -1
  131. package/build-types/components/media-editor-toolbar/index.d.ts +0 -24
  132. package/build-types/components/media-editor-toolbar/index.d.ts.map +0 -1
  133. package/build-types/components/media-editor-transform-controls/index.d.ts +0 -21
  134. package/build-types/components/media-editor-transform-controls/index.d.ts.map +0 -1
  135. package/build-types/components/media-form/sidebar-datetime-view.d.ts +0 -10
  136. package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +0 -1
  137. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -108
  138. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
@@ -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 { MIN_CROP_PIXELS, MIN_CROP_SCREEN_PX } from './constants';\nimport type { HandlePosition, NormalizedRect, Size } from './types';\n\nexport type { HandlePosition };\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 zoomed out (small `displayScale`) the on-screen floor\n * dominates; once the image is shown large enough the source-pixel floor takes\n * over.\n *\n * @param displayScale CSS pixels rendered per source pixel (fit scale \u00D7 zoom).\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 * @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": ";AAGA,SAAS,iBAAiB,0BAA0B;AAoB7C,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,QAAQ,KAAM,UAA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;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;",
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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
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 { getMinCropPixels } from "../../core/stencil-math.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,29 +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
- const displayScale = elementSize.width / naturalWidth * state.zoom;
146
- const minPixels = getMinCropPixels(displayScale);
147
- return {
148
- width: Math.min(1, minPixels * state.zoom / bbox.width),
149
- height: Math.min(1, minPixels * state.zoom / bbox.height)
150
- };
151
- }, [
152
- naturalWidth,
153
- naturalHeight,
154
- state.rotation,
155
- state.zoom,
156
- elementSize.width
157
- ]);
158
154
  useEffect(() => {
159
155
  setVisualSize(visualSize);
160
156
  }, [visualSize, setVisualSize]);
@@ -207,16 +203,133 @@ function CropperInner({
207
203
  const [isResizing, setIsResizing] = useState(false);
208
204
  const isResizingRef = useRef(false);
209
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
+ }, []);
210
212
  const [activeHandle, setActiveHandle] = useState(
211
213
  null
212
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
+ ]);
213
326
  const {
214
327
  handlers,
215
328
  onWheelNative,
216
329
  isDragging,
217
330
  isZooming,
218
331
  isPlacementActive: isInteractionPlacementActive
219
- } = useInteraction(state, controller, canvasSize, visualSize, {
332
+ } = useInteraction(state, controller, canvasSize, scaledVisualSize, {
220
333
  minZoom: effectiveMinZoom,
221
334
  maxZoom,
222
335
  onGestureStart,
@@ -227,12 +340,36 @@ function CropperInner({
227
340
  onPointerDownCapture: () => {
228
341
  setIsFocusVisible(false);
229
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
+ },
230
363
  onKeyDownCapture: (event) => {
231
364
  if (!["Shift", "Control", "Alt", "Meta"].includes(event.key)) {
232
365
  setIsFocusVisible(true);
233
366
  }
234
367
  },
235
368
  onPointerDown: (event) => {
369
+ if (isResizingRef.current || isTouchPinchingRef.current || event.pointerType === "touch" && event.isPrimary === false) {
370
+ event.preventDefault();
371
+ return;
372
+ }
236
373
  handlers.onPointerDown?.(event);
237
374
  setIsFocusVisible(false);
238
375
  }
@@ -275,25 +412,28 @@ function CropperInner({
275
412
  );
276
413
  const handleCropChange = useCallback(
277
414
  (rect) => {
415
+ if (isTouchPinchingRef.current) {
416
+ return;
417
+ }
278
418
  setCropRect(rect);
279
- if (isResizingRef.current && visualSize.width > 0 && visualSize.height > 0) {
280
- const offsetX = (canvasSize.width - visualSize.width) / 2;
281
- 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;
282
422
  const rightOverflow = Math.max(
283
423
  0,
284
- offsetX + (rect.x + rect.width) * visualSize.width - canvasSize.width
424
+ offsetX + (rect.x + rect.width) * scaledVisualSize.width - canvasSize.width
285
425
  );
286
426
  const leftOverflow = Math.max(
287
427
  0,
288
- -(offsetX + rect.x * visualSize.width)
428
+ -(offsetX + rect.x * scaledVisualSize.width)
289
429
  );
290
430
  const bottomOverflow = Math.max(
291
431
  0,
292
- offsetY + (rect.y + rect.height) * visualSize.height - canvasSize.height
432
+ offsetY + (rect.y + rect.height) * scaledVisualSize.height - canvasSize.height
293
433
  );
294
434
  const topOverflow = Math.max(
295
435
  0,
296
- -(offsetY + rect.y * visualSize.height)
436
+ -(offsetY + rect.y * scaledVisualSize.height)
297
437
  );
298
438
  setViewportPan({
299
439
  x: -rightOverflow + leftOverflow,
@@ -301,10 +441,15 @@ function CropperInner({
301
441
  });
302
442
  }
303
443
  },
304
- [setCropRect, setViewportPan, canvasSize, visualSize]
444
+ [setCropRect, setViewportPan, canvasSize, scaledVisualSize]
305
445
  );
306
446
  const [settling, setSettling] = useState(false);
307
447
  const settleTimerRef = useRef();
448
+ const finishSettling = useCallback(() => {
449
+ clearTimeout(settleTimerRef.current);
450
+ isSettlingRef.current = false;
451
+ setSettling(false);
452
+ }, []);
308
453
  useEffect(() => {
309
454
  return () => {
310
455
  clearTimeout(settleTimerRef.current);
@@ -328,39 +473,56 @@ function CropperInner({
328
473
  }, []);
329
474
  const handleResizeStart = useCallback(
330
475
  (handle) => {
476
+ if (isTouchPinchingRef.current) {
477
+ return;
478
+ }
479
+ setFrozenViewScale(viewScaleRest);
331
480
  isResizingRef.current = true;
332
481
  setIsResizing(true);
333
482
  setActiveHandle(handle ?? null);
334
- clearTimeout(settleTimerRef.current);
335
- isSettlingRef.current = false;
336
- setSettling(false);
483
+ finishSettling();
337
484
  resetViewport();
338
485
  onGestureStart?.();
339
486
  },
340
- [onGestureStart, resetViewport]
487
+ [finishSettling, onGestureStart, resetViewport, viewScaleRest]
341
488
  );
342
489
  const handleResizeEnd = useCallback(() => {
490
+ const isCancellingForPinch = isTouchPinchingRef.current;
343
491
  isResizingRef.current = false;
344
492
  setIsResizing(false);
345
493
  setActiveHandle(null);
494
+ clearTimeout(settleTimerRef.current);
495
+ if (isCancellingForPinch) {
496
+ finishSettling();
497
+ resetViewport();
498
+ return;
499
+ }
346
500
  isSettlingRef.current = true;
347
501
  setSettling(true);
348
502
  resetViewport();
349
503
  settleCrop();
350
504
  onGestureEnd?.();
351
- clearTimeout(settleTimerRef.current);
352
- settleTimerRef.current = setTimeout(() => {
353
- isSettlingRef.current = false;
354
- setSettling(false);
355
- }, 200);
356
- }, [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
+ );
357
519
  let imageTransition;
358
520
  if (settling) {
359
- imageTransition = "transform 200ms ease-out";
521
+ imageTransition = SETTLE_TRANSFORM_TRANSITION;
360
522
  } else if (isZooming) {
361
523
  imageTransition = "transform 150ms linear";
362
524
  }
363
- 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;
364
526
  const imageStyle = useMemo(() => {
365
527
  if (elementSize.width === 0 || elementSize.height === 0) {
366
528
  return {};
@@ -374,11 +536,25 @@ function CropperInner({
374
536
  maxHeight: elementSize.height,
375
537
  left: centerX,
376
538
  top: centerY,
377
- transform: transformString,
378
- 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
379
548
  };
380
- }, [canvasSize, elementSize, transformString, imageTransition]);
381
- 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;
382
558
  const willChange = isResizing || settling ? "transform" : void 0;
383
559
  let stageStyle;
384
560
  if (viewportState.pan.x !== 0 || viewportState.pan.y !== 0) {
@@ -388,7 +564,11 @@ function CropperInner({
388
564
  willChange
389
565
  };
390
566
  } else if (settling) {
391
- stageStyle = { transition: settleTransition, willChange };
567
+ stageStyle = {
568
+ transform: "translate(0px, 0px)",
569
+ transition: settleTransition,
570
+ willChange
571
+ };
392
572
  }
393
573
  const setContainerRef = useCallback(
394
574
  (element) => {
@@ -449,11 +629,13 @@ function CropperInner({
449
629
  className: "wp-media-editor-image-editor__stage",
450
630
  "data-testid": "cropper-stage",
451
631
  style: stageStyle,
632
+ onTransitionEnd: handleSettleTransitionEnd,
452
633
  children: [
453
634
  /* @__PURE__ */ jsx(
454
635
  "img",
455
636
  {
456
637
  className: "wp-media-editor-image-editor__image",
638
+ "data-testid": "cropper-image",
457
639
  src,
458
640
  alt: "",
459
641
  onLoad: handleImageLoad,
@@ -466,7 +648,7 @@ function CropperInner({
466
648
  {
467
649
  cropRect: state.cropRect,
468
650
  containerSize: canvasSize,
469
- imageSize: visualSize,
651
+ imageSize: scaledVisualSize,
470
652
  transition: settleStencilTransition
471
653
  }
472
654
  ),
@@ -475,16 +657,19 @@ function CropperInner({
475
657
  {
476
658
  cropRect: state.cropRect,
477
659
  containerSize: canvasSize,
478
- imageSize: visualSize,
660
+ imageSize: scaledVisualSize,
479
661
  onCropChange: handleCropChange,
480
662
  onResizeStart: handleResizeStart,
481
663
  onResizeEnd: handleResizeEnd,
482
664
  onEscape: handleEscape,
483
665
  aspectRatio,
484
666
  freeformCrop,
667
+ isResizeDisabled: isTouchPinching,
485
668
  stencilTransition: settleStencilTransition,
486
669
  cropBounds,
487
- minCropSize
670
+ minCropSize,
671
+ snapCropRect,
672
+ keyboardResizeStep
488
673
  }
489
674
  ),
490
675
  (showGrid === true || isInteractiveGrid) && /* @__PURE__ */ jsx(
@@ -492,7 +677,7 @@ function CropperInner({
492
677
  {
493
678
  cropRect: state.cropRect,
494
679
  containerSize: canvasSize,
495
- imageSize: visualSize
680
+ imageSize: scaledVisualSize
496
681
  }
497
682
  ),
498
683
  activeHandle && outputSize && /* @__PURE__ */ jsx(
@@ -500,7 +685,7 @@ function CropperInner({
500
685
  {
501
686
  cropRect: state.cropRect,
502
687
  containerSize: canvasSize,
503
- imageSize: visualSize,
688
+ imageSize: scaledVisualSize,
504
689
  activeHandle,
505
690
  outputWidth: outputSize.width,
506
691
  outputHeight: outputSize.height