@wordpress/media-editor 0.8.1-next.v.202605131032.0 → 0.10.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 (292) hide show
  1. package/build/components/media-editor/index.cjs +34 -135
  2. package/build/components/media-editor/index.cjs.map +3 -3
  3. package/build/components/media-editor/use-crop-options.cjs +87 -0
  4. package/build/components/media-editor/use-crop-options.cjs.map +7 -0
  5. package/build/components/media-editor/use-save-media-editor.cjs +185 -0
  6. package/build/components/media-editor/use-save-media-editor.cjs.map +7 -0
  7. package/build/components/media-editor-canvas/index.cjs +32 -11
  8. package/build/components/media-editor-canvas/index.cjs.map +2 -2
  9. package/build/components/media-editor-crop-panel/index.cjs +66 -83
  10. package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
  11. package/build/components/media-editor-modal/index.cjs +40 -11
  12. package/build/components/media-editor-modal/index.cjs.map +3 -3
  13. package/build/components/media-editor-toolbar/index.cjs +7 -3
  14. package/build/components/media-editor-toolbar/index.cjs.map +2 -2
  15. package/build/components/media-form/index.cjs +19 -1
  16. package/build/components/media-form/index.cjs.map +3 -3
  17. package/build/components/media-form/sidebar-datetime-view.cjs +53 -0
  18. package/build/components/media-form/sidebar-datetime-view.cjs.map +7 -0
  19. package/build/components/rotation-ruler/index.cjs +8 -2
  20. package/build/components/rotation-ruler/index.cjs.map +2 -2
  21. package/build/hooks/use-crop-gesture-handlers.cjs +41 -4
  22. package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
  23. package/build/image-editor/core/camera.cjs +29 -11
  24. package/build/image-editor/core/camera.cjs.map +2 -2
  25. package/build/image-editor/core/constants.cjs +6 -0
  26. package/build/image-editor/core/constants.cjs.map +2 -2
  27. package/build/image-editor/core/containment.cjs +71 -34
  28. package/build/image-editor/core/containment.cjs.map +2 -2
  29. package/build/image-editor/core/crop-rect.cjs +48 -0
  30. package/build/image-editor/core/crop-rect.cjs.map +7 -0
  31. package/build/image-editor/core/index.cjs +12 -0
  32. package/build/image-editor/core/index.cjs.map +2 -2
  33. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  34. package/build/image-editor/core/math/sanitize.cjs +92 -0
  35. package/build/image-editor/core/math/sanitize.cjs.map +7 -0
  36. package/build/image-editor/core/setter-helpers.cjs +64 -0
  37. package/build/image-editor/core/setter-helpers.cjs.map +7 -0
  38. package/build/image-editor/core/source-region.cjs +16 -14
  39. package/build/image-editor/core/source-region.cjs.map +2 -2
  40. package/build/image-editor/core/state.cjs +20 -10
  41. package/build/image-editor/core/state.cjs.map +2 -2
  42. package/build/image-editor/core/stencil-math.cjs +31 -20
  43. package/build/image-editor/core/stencil-math.cjs.map +2 -2
  44. package/build/image-editor/core/transform-style.cjs +12 -6
  45. package/build/image-editor/core/transform-style.cjs.map +2 -2
  46. package/build/image-editor/core/types.cjs.map +1 -1
  47. package/build/image-editor/index.cjs +12 -2
  48. package/build/image-editor/index.cjs.map +2 -2
  49. package/build/image-editor/react/components/cropper-provider.cjs +2 -2
  50. package/build/image-editor/react/components/cropper-provider.cjs.map +2 -2
  51. package/build/image-editor/react/components/cropper.cjs +121 -48
  52. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  53. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +172 -0
  54. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +7 -0
  55. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +20 -10
  56. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  57. package/build/image-editor/react/hooks/build-cropper-setters.cjs +71 -0
  58. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +7 -0
  59. package/build/image-editor/react/hooks/index.cjs +6 -3
  60. package/build/image-editor/react/hooks/index.cjs.map +2 -2
  61. package/build/image-editor/react/hooks/use-aria-announcer.cjs +3 -1
  62. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
  63. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +113 -0
  64. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +7 -0
  65. package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
  66. package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
  67. package/build/state/composite-reducer.cjs +115 -0
  68. package/build/state/composite-reducer.cjs.map +7 -0
  69. package/build/state/index.cjs +48 -0
  70. package/build/state/index.cjs.map +7 -0
  71. package/build/state/media-editor-state-provider.cjs +58 -0
  72. package/build/state/media-editor-state-provider.cjs.map +7 -0
  73. package/build/state/types.cjs +34 -0
  74. package/build/state/types.cjs.map +7 -0
  75. package/build/state/use-media-editor-state.cjs +298 -0
  76. package/build/state/use-media-editor-state.cjs.map +7 -0
  77. package/build-module/components/media-editor/index.mjs +40 -140
  78. package/build-module/components/media-editor/index.mjs.map +2 -2
  79. package/build-module/components/media-editor/use-crop-options.mjs +61 -0
  80. package/build-module/components/media-editor/use-crop-options.mjs.map +7 -0
  81. package/build-module/components/media-editor/use-save-media-editor.mjs +151 -0
  82. package/build-module/components/media-editor/use-save-media-editor.mjs.map +7 -0
  83. package/build-module/components/media-editor-canvas/index.mjs +33 -12
  84. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  85. package/build-module/components/media-editor-crop-panel/index.mjs +68 -83
  86. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  87. package/build-module/components/media-editor-modal/index.mjs +40 -11
  88. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  89. package/build-module/components/media-editor-toolbar/index.mjs +7 -3
  90. package/build-module/components/media-editor-toolbar/index.mjs.map +2 -2
  91. package/build-module/components/media-form/index.mjs +9 -1
  92. package/build-module/components/media-form/index.mjs.map +2 -2
  93. package/build-module/components/media-form/sidebar-datetime-view.mjs +32 -0
  94. package/build-module/components/media-form/sidebar-datetime-view.mjs.map +7 -0
  95. package/build-module/components/rotation-ruler/index.mjs +8 -2
  96. package/build-module/components/rotation-ruler/index.mjs.map +2 -2
  97. package/build-module/hooks/use-crop-gesture-handlers.mjs +41 -4
  98. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
  99. package/build-module/image-editor/core/camera.mjs +34 -11
  100. package/build-module/image-editor/core/camera.mjs.map +2 -2
  101. package/build-module/image-editor/core/constants.mjs +4 -0
  102. package/build-module/image-editor/core/constants.mjs.map +2 -2
  103. package/build-module/image-editor/core/containment.mjs +75 -34
  104. package/build-module/image-editor/core/containment.mjs.map +2 -2
  105. package/build-module/image-editor/core/crop-rect.mjs +23 -0
  106. package/build-module/image-editor/core/crop-rect.mjs.map +7 -0
  107. package/build-module/image-editor/core/index.mjs +12 -0
  108. package/build-module/image-editor/core/index.mjs.map +2 -2
  109. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  110. package/build-module/image-editor/core/math/sanitize.mjs +63 -0
  111. package/build-module/image-editor/core/math/sanitize.mjs.map +7 -0
  112. package/build-module/image-editor/core/setter-helpers.mjs +39 -0
  113. package/build-module/image-editor/core/setter-helpers.mjs.map +7 -0
  114. package/build-module/image-editor/core/source-region.mjs +16 -14
  115. package/build-module/image-editor/core/source-region.mjs.map +2 -2
  116. package/build-module/image-editor/core/state.mjs +25 -11
  117. package/build-module/image-editor/core/state.mjs.map +2 -2
  118. package/build-module/image-editor/core/stencil-math.mjs +30 -19
  119. package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
  120. package/build-module/image-editor/core/transform-style.mjs +12 -6
  121. package/build-module/image-editor/core/transform-style.mjs.map +2 -2
  122. package/build-module/image-editor/index.mjs +11 -2
  123. package/build-module/image-editor/index.mjs.map +2 -2
  124. package/build-module/image-editor/react/components/cropper-provider.mjs +3 -3
  125. package/build-module/image-editor/react/components/cropper-provider.mjs.map +2 -2
  126. package/build-module/image-editor/react/components/cropper.mjs +122 -49
  127. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  128. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +147 -0
  129. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +7 -0
  130. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +20 -10
  131. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  132. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +46 -0
  133. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +7 -0
  134. package/build-module/image-editor/react/hooks/index.mjs +4 -2
  135. package/build-module/image-editor/react/hooks/index.mjs.map +2 -2
  136. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +3 -1
  137. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
  138. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +100 -0
  139. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +7 -0
  140. package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
  141. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
  142. package/build-module/state/composite-reducer.mjs +94 -0
  143. package/build-module/state/composite-reducer.mjs.map +7 -0
  144. package/build-module/state/index.mjs +26 -0
  145. package/build-module/state/index.mjs.map +7 -0
  146. package/build-module/state/media-editor-state-provider.mjs +34 -0
  147. package/build-module/state/media-editor-state-provider.mjs.map +7 -0
  148. package/build-module/state/types.mjs +9 -0
  149. package/build-module/state/types.mjs.map +7 -0
  150. package/build-module/state/use-media-editor-state.mjs +289 -0
  151. package/build-module/state/use-media-editor-state.mjs.map +7 -0
  152. package/build-style/style-rtl.css +46 -9
  153. package/build-style/style.css +46 -9
  154. package/build-types/components/media-editor/index.d.ts +4 -7
  155. package/build-types/components/media-editor/index.d.ts.map +1 -1
  156. package/build-types/components/media-editor/use-crop-options.d.ts +37 -0
  157. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -0
  158. package/build-types/components/media-editor/use-save-media-editor.d.ts +29 -0
  159. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -0
  160. package/build-types/components/media-editor-canvas/index.d.ts +5 -10
  161. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  162. package/build-types/components/media-editor-crop-panel/index.d.ts +4 -16
  163. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  164. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  165. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -1
  166. package/build-types/components/media-form/index.d.ts.map +1 -1
  167. package/build-types/components/media-form/sidebar-datetime-view.d.ts +10 -0
  168. package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +1 -0
  169. package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
  170. package/build-types/hooks/use-crop-gesture-handlers.d.ts +13 -14
  171. package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
  172. package/build-types/image-editor/core/camera.d.ts.map +1 -1
  173. package/build-types/image-editor/core/constants.d.ts +18 -0
  174. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  175. package/build-types/image-editor/core/containment.d.ts +10 -0
  176. package/build-types/image-editor/core/containment.d.ts.map +1 -1
  177. package/build-types/image-editor/core/crop-rect.d.ts +19 -0
  178. package/build-types/image-editor/core/crop-rect.d.ts.map +1 -0
  179. package/build-types/image-editor/core/index.d.ts +3 -1
  180. package/build-types/image-editor/core/index.d.ts.map +1 -1
  181. package/build-types/image-editor/core/interaction-controller.d.ts +5 -1
  182. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  183. package/build-types/image-editor/core/math/sanitize.d.ts +70 -0
  184. package/build-types/image-editor/core/math/sanitize.d.ts.map +1 -0
  185. package/build-types/image-editor/core/setter-helpers.d.ts +24 -0
  186. package/build-types/image-editor/core/setter-helpers.d.ts.map +1 -0
  187. package/build-types/image-editor/core/source-region.d.ts.map +1 -1
  188. package/build-types/image-editor/core/state.d.ts +12 -0
  189. package/build-types/image-editor/core/state.d.ts.map +1 -1
  190. package/build-types/image-editor/core/stencil-math.d.ts +23 -18
  191. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
  192. package/build-types/image-editor/core/transform-style.d.ts.map +1 -1
  193. package/build-types/image-editor/core/types.d.ts +23 -4
  194. package/build-types/image-editor/core/types.d.ts.map +1 -1
  195. package/build-types/image-editor/index.d.ts +4 -4
  196. package/build-types/image-editor/index.d.ts.map +1 -1
  197. package/build-types/image-editor/react/components/cropper-provider.d.ts +11 -7
  198. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
  199. package/build-types/image-editor/react/components/cropper.d.ts +6 -4
  200. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  201. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +36 -0
  202. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +1 -0
  203. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +2 -1
  204. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  205. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +47 -0
  206. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +1 -0
  207. package/build-types/image-editor/react/hooks/index.d.ts +4 -2
  208. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
  209. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
  210. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts +58 -0
  211. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +1 -0
  212. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
  213. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
  214. package/build-types/state/composite-reducer.d.ts +39 -0
  215. package/build-types/state/composite-reducer.d.ts.map +1 -0
  216. package/build-types/state/index.d.ts +7 -0
  217. package/build-types/state/index.d.ts.map +1 -0
  218. package/build-types/state/media-editor-state-provider.d.ts +34 -0
  219. package/build-types/state/media-editor-state-provider.d.ts.map +1 -0
  220. package/build-types/state/types.d.ts +104 -0
  221. package/build-types/state/types.d.ts.map +1 -0
  222. package/build-types/state/use-media-editor-state.d.ts +104 -0
  223. package/build-types/state/use-media-editor-state.d.ts.map +1 -0
  224. package/package.json +19 -17
  225. package/src/components/media-editor/index.tsx +52 -188
  226. package/src/components/media-editor/test/use-crop-options.tsx +164 -0
  227. package/src/components/media-editor/use-crop-options.ts +115 -0
  228. package/src/components/media-editor/use-save-media-editor.ts +214 -0
  229. package/src/components/media-editor-canvas/index.tsx +56 -24
  230. package/src/components/media-editor-crop-panel/index.tsx +25 -61
  231. package/src/components/media-editor-crop-panel/test/index.tsx +17 -43
  232. package/src/components/media-editor-modal/index.tsx +50 -12
  233. package/src/components/media-editor-modal/test/build-modifiers.test.ts +14 -0
  234. package/src/components/media-editor-modal/test/index.tsx +143 -0
  235. package/src/components/media-editor-toolbar/index.tsx +9 -5
  236. package/src/components/media-form/index.tsx +15 -1
  237. package/src/components/media-form/sidebar-datetime-view.tsx +47 -0
  238. package/src/components/media-form/test/sidebar-datetime-view.test.tsx +96 -0
  239. package/src/components/rotation-ruler/README.md +11 -25
  240. package/src/components/rotation-ruler/index.tsx +17 -2
  241. package/src/components/rotation-ruler/style.scss +49 -13
  242. package/src/hooks/use-crop-gesture-handlers.ts +66 -17
  243. package/src/image-editor/README.md +28 -129
  244. package/src/image-editor/core/camera.ts +51 -26
  245. package/src/image-editor/core/constants.ts +19 -0
  246. package/src/image-editor/core/containment.ts +102 -49
  247. package/src/image-editor/core/crop-rect.ts +45 -0
  248. package/src/image-editor/core/index.ts +16 -4
  249. package/src/image-editor/core/interaction-controller.ts +5 -1
  250. package/src/image-editor/core/math/sanitize.ts +158 -0
  251. package/src/image-editor/core/math/test/sanitize.ts +244 -0
  252. package/src/image-editor/core/setter-helpers.ts +66 -0
  253. package/src/image-editor/core/source-region.ts +21 -14
  254. package/src/image-editor/core/state.ts +67 -15
  255. package/src/image-editor/core/stencil-math.ts +55 -35
  256. package/src/image-editor/core/test/camera.ts +367 -1
  257. package/src/image-editor/core/test/state.ts +101 -8
  258. package/src/image-editor/core/test/stencil-math.ts +193 -0
  259. package/src/image-editor/core/transform-style.ts +21 -6
  260. package/src/image-editor/core/types.ts +24 -4
  261. package/src/image-editor/docs/architecture.md +48 -89
  262. package/src/image-editor/docs/recipes.md +123 -659
  263. package/src/image-editor/index.ts +12 -3
  264. package/src/image-editor/react/components/cropper-provider.tsx +16 -12
  265. package/src/image-editor/react/components/cropper.scss +23 -4
  266. package/src/image-editor/react/components/cropper.tsx +197 -90
  267. package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +269 -0
  268. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +22 -9
  269. package/src/image-editor/react/components/test/cropper.tsx +55 -13
  270. package/src/image-editor/react/hooks/build-cropper-setters.ts +105 -0
  271. package/src/image-editor/react/hooks/index.ts +4 -2
  272. package/src/image-editor/react/hooks/test/{use-cropper-state.ts → use-cropper-reducer.ts} +142 -286
  273. package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
  274. package/src/image-editor/react/hooks/use-cropper-reducer.ts +204 -0
  275. package/src/image-editor/react/hooks/use-interaction.ts +3 -2
  276. package/src/image-editor/stories/rectangle-crop.story.tsx +7 -17
  277. package/src/state/composite-reducer.ts +152 -0
  278. package/src/state/index.ts +20 -0
  279. package/src/state/media-editor-state-provider.tsx +70 -0
  280. package/src/state/test/use-media-editor-state.ts +402 -0
  281. package/src/state/types.ts +107 -0
  282. package/src/state/use-media-editor-state.ts +488 -0
  283. package/tsconfig.json +2 -0
  284. package/tsconfig.tsbuildinfo +1 -1
  285. package/build/image-editor/react/hooks/use-cropper-state.cjs +0 -277
  286. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +0 -7
  287. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +0 -262
  288. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +0 -7
  289. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -97
  290. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
  291. package/src/image-editor/docs/roadmap.md +0 -66
  292. package/src/image-editor/react/hooks/use-cropper-state.ts +0 -457
@@ -2,7 +2,12 @@
2
2
  * Internal dependencies
3
3
  */
4
4
  import type { CropperAction, CropperState, TransformOperation } from './types';
5
- import { DEFAULT_STATE, MAX_ZOOM } from './constants';
5
+ import {
6
+ ABSOLUTE_MIN_ZOOM,
7
+ DEFAULT_STATE,
8
+ MAX_ZOOM,
9
+ MIN_ZOOM,
10
+ } from './constants';
6
11
  import { normalizeRotation, degreesToRadians } from './math/rotation';
7
12
  import { restrictPanZoom, restrictCropRect } from './containment';
8
13
 
@@ -13,6 +18,50 @@ function nearlyEqual( a: number, b: number ): boolean {
13
18
  return Math.abs( a - b ) < STATE_EPSILON;
14
19
  }
15
20
 
21
+ /**
22
+ * Value-equality check for two cropper states.
23
+ *
24
+ * Used by history machinery to dedup snapshots and detect no-op
25
+ * transitions. Compares every field that participates in the rendered
26
+ * output; ignores reference identity.
27
+ *
28
+ * @param a First cropper state.
29
+ * @param b Second cropper state.
30
+ * @return Whether the two states are value-equal across all tracked fields.
31
+ */
32
+ export function areCropperStatesEqual(
33
+ a: CropperState,
34
+ b: CropperState
35
+ ): boolean {
36
+ const aImage = a.image;
37
+ const bImage = b.image;
38
+ return (
39
+ aImage?.src === bImage?.src &&
40
+ aImage?.naturalWidth === bImage?.naturalWidth &&
41
+ aImage?.naturalHeight === bImage?.naturalHeight &&
42
+ nearlyEqual( a.pan.x, b.pan.x ) &&
43
+ nearlyEqual( a.pan.y, b.pan.y ) &&
44
+ nearlyEqual( a.zoom, b.zoom ) &&
45
+ nearlyEqual( a.rotation, b.rotation ) &&
46
+ a.flip.horizontal === b.flip.horizontal &&
47
+ a.flip.vertical === b.flip.vertical &&
48
+ nearlyEqual( a.cropRect.x, b.cropRect.x ) &&
49
+ nearlyEqual( a.cropRect.y, b.cropRect.y ) &&
50
+ nearlyEqual( a.cropRect.width, b.cropRect.width ) &&
51
+ nearlyEqual( a.cropRect.height, b.cropRect.height )
52
+ );
53
+ }
54
+
55
+ function clampRequestedZoom( state: CropperState, zoom: number ): number {
56
+ // With an image, enforceContainment raises zoom to the coverage-aware
57
+ // floor below. Without an image, no enforcement runs, so MIN_ZOOM stays
58
+ // the conservative resting default.
59
+ if ( state.image ) {
60
+ return Math.min( MAX_ZOOM, Math.max( ABSOLUTE_MIN_ZOOM, zoom ) );
61
+ }
62
+ return Math.min( MAX_ZOOM, Math.max( MIN_ZOOM, zoom ) );
63
+ }
64
+
16
65
  /**
17
66
  * Translate a pipeline transform operation into the equivalent
18
67
  * reducer action. Pipeline ops aren't 1:1 with reducer actions —
@@ -192,7 +241,7 @@ export function cropperReducer(
192
241
  );
193
242
 
194
243
  case 'SET_ZOOM': {
195
- const z = Math.min( MAX_ZOOM, Math.max( 1, action.payload ) );
244
+ const z = clampRequestedZoom( state, action.payload );
196
245
  return commitBase(
197
246
  enforceContainment( {
198
247
  ...state,
@@ -202,7 +251,7 @@ export function cropperReducer(
202
251
  }
203
252
 
204
253
  case 'SET_ZOOM_AT_POINT': {
205
- const z = Math.min( MAX_ZOOM, Math.max( 1, action.payload.zoom ) );
254
+ const z = clampRequestedZoom( state, action.payload.zoom );
206
255
  return commitBase(
207
256
  enforceContainment( {
208
257
  ...state,
@@ -345,24 +394,27 @@ export function cropperReducer(
345
394
  return state;
346
395
  }
347
396
 
348
- // New crop: fill height (or width), maintain aspect ratio, center.
349
- const normalizedRatio = rect.width / rect.height;
350
- let newH = 1;
351
- let newW = normalizedRatio;
352
- if ( newW > 1 ) {
353
- newW = 1;
354
- newH = 1 / normalizedRatio;
355
- }
397
+ // Scale factor: how much the crop grows. fitScale fills the
398
+ // larger axis to 1 (viewport-filling settle). Cap so the
399
+ // resulting zoom stays at or below MAX_ZOOM — tight crops
400
+ // would otherwise push state.zoom past the user-facing cap
401
+ // and break wheel/slider interactions, which clamp to
402
+ // MAX_ZOOM and snap state.zoom down on the first input.
403
+ const fitScale = 1 / Math.max( rect.width, rect.height );
404
+ const zoomCap = state.zoom > 0 ? MAX_ZOOM / state.zoom : fitScale;
405
+ const s = Math.min( fitScale, zoomCap );
356
406
 
357
- // Scale factor: how much the crop grew.
358
- const s = newH / rect.height;
407
+ const newW = rect.width * s;
408
+ const newH = rect.height * s;
359
409
 
360
410
  // The old crop center in normalized visual space.
361
411
  const oldCx = rect.x + rect.width / 2;
362
412
  const oldCy = rect.y + rect.height / 2;
363
413
 
364
- // Zoom scales by s so the same image region fills the
365
- // larger crop at the same relative size.
414
+ // Zoom scales by s so the same image region fills the new
415
+ // crop at the same relative size. When s is capped, the new
416
+ // crop is smaller than the viewport but the visible content
417
+ // inside it remains the source region the user framed.
366
418
  // Pan: the visible content center was at
367
419
  // (cropCx - crop.x, cropCy - crop.y)
368
420
  // in visual-normalized space. After centering the crop to
@@ -1,15 +1,17 @@
1
1
  /**
2
2
  * Internal dependencies
3
3
  */
4
- import type { NormalizedRect, Size } from './types';
4
+ import type { HandlePosition, NormalizedRect, Size } from './types';
5
5
 
6
- /** Minimum crop rect dimension in normalized space (5% of visual area). */
7
- export const MIN_CROP_SIZE = 0.05;
6
+ export type { HandlePosition };
8
7
 
9
8
  /**
10
- * Handle position identifiers for the 8 resize handles.
9
+ * Default minimum crop rect dimension in normalized space, used when no
10
+ * explicit per-axis minimum is supplied (e.g. before the source image
11
+ * has loaded). Real usage derives a pixel-based min from the source
12
+ * image dimensions and `MIN_CROP_PIXELS` in `constants.ts`.
11
13
  */
12
- export type HandlePosition = 'n' | 's' | 'e' | 'w' | 'nw' | 'ne' | 'sw' | 'se';
14
+ export const DEFAULT_MIN_CROP_SIZE: Size = { width: 0.05, height: 0.05 };
13
15
 
14
16
  /**
15
17
  * Bounds within which the crop rect must stay.
@@ -40,11 +42,12 @@ export interface ResizeDragState {
40
42
  * Each edge moves independently based on the handle being dragged.
41
43
  * Edges are clamped to the provided bounds and maintain a minimum size.
42
44
  *
43
- * @param drag The current drag state.
44
- * @param clientX Current mouse/touch X position in pixels.
45
- * @param clientY Current mouse/touch Y position in pixels.
46
- * @param imageSize The rendered image dimensions in pixels.
47
- * @param bounds The allowed crop area bounds.
45
+ * @param drag The current drag state.
46
+ * @param clientX Current mouse/touch X position in pixels.
47
+ * @param clientY Current mouse/touch Y position in pixels.
48
+ * @param imageSize The rendered image dimensions in pixels.
49
+ * @param bounds The allowed crop area bounds.
50
+ * @param minCropSize Minimum crop rect dimension in normalized space, per axis.
48
51
  * @return The new crop rect in normalized coordinates.
49
52
  */
50
53
  export function computeFreeResizeRect(
@@ -52,7 +55,8 @@ export function computeFreeResizeRect(
52
55
  clientX: number,
53
56
  clientY: number,
54
57
  imageSize: Size,
55
- bounds: CropBounds
58
+ bounds: CropBounds,
59
+ minCropSize: Size = DEFAULT_MIN_CROP_SIZE
56
60
  ): NormalizedRect {
57
61
  const dx =
58
62
  imageSize.width > 0 ? ( clientX - drag.startX ) / imageSize.width : 0;
@@ -70,24 +74,24 @@ export function computeFreeResizeRect(
70
74
  if ( handle === 'n' || handle === 'nw' || handle === 'ne' ) {
71
75
  edgeTop = Math.max(
72
76
  bounds.minY,
73
- Math.min( s.y + dy, edgeBottom - MIN_CROP_SIZE )
77
+ Math.min( s.y + dy, edgeBottom - minCropSize.height )
74
78
  );
75
79
  }
76
80
  if ( handle === 's' || handle === 'sw' || handle === 'se' ) {
77
81
  edgeBottom = Math.max(
78
- edgeTop + MIN_CROP_SIZE,
82
+ edgeTop + minCropSize.height,
79
83
  Math.min( s.y + s.height + dy, bounds.maxY )
80
84
  );
81
85
  }
82
86
  if ( handle === 'w' || handle === 'nw' || handle === 'sw' ) {
83
87
  edgeLeft = Math.max(
84
88
  bounds.minX,
85
- Math.min( s.x + dx, edgeRight - MIN_CROP_SIZE )
89
+ Math.min( s.x + dx, edgeRight - minCropSize.width )
86
90
  );
87
91
  }
88
92
  if ( handle === 'e' || handle === 'ne' || handle === 'se' ) {
89
93
  edgeRight = Math.max(
90
- edgeLeft + MIN_CROP_SIZE,
94
+ edgeLeft + minCropSize.width,
91
95
  Math.min( s.x + s.width + dx, bounds.maxX )
92
96
  );
93
97
  }
@@ -113,6 +117,7 @@ export function computeFreeResizeRect(
113
117
  * @param imageSize The rendered image dimensions in pixels.
114
118
  * @param bounds The allowed crop area bounds.
115
119
  * @param normalizedRatio The locked aspect ratio (width / height in normalized space).
120
+ * @param minCropSize Minimum crop rect dimension in normalized space, per axis.
116
121
  * @return The new crop rect in normalized coordinates.
117
122
  */
118
123
  export function computeLockedResizeRect(
@@ -121,7 +126,8 @@ export function computeLockedResizeRect(
121
126
  clientY: number,
122
127
  imageSize: Size,
123
128
  bounds: CropBounds,
124
- normalizedRatio: number
129
+ normalizedRatio: number,
130
+ minCropSize: Size = DEFAULT_MIN_CROP_SIZE
125
131
  ): NormalizedRect {
126
132
  // The math below divides by `normalizedRatio` and `imageSize`, so
127
133
  // bail out with the start rect when any of them is zero. This can
@@ -159,9 +165,15 @@ export function computeLockedResizeRect(
159
165
  let distW = ( draggedX - anchorX ) * dirX;
160
166
  let distH = ( draggedY - anchorY ) * dirY;
161
167
 
162
- // Enforce minimum size.
163
- distW = Math.max( distW, MIN_CROP_SIZE );
164
- distH = Math.max( distH, MIN_CROP_SIZE );
168
+ // Enforce minimum size on both axes, projecting through the ratio so
169
+ // neither axis falls below its per-axis floor.
170
+ const minDistW = Math.max(
171
+ minCropSize.width,
172
+ minCropSize.height * normalizedRatio
173
+ );
174
+ const minDistH = minDistW / normalizedRatio;
175
+ distW = Math.max( distW, minDistW );
176
+ distH = Math.max( distH, minDistH );
165
177
 
166
178
  // Determine which axis "drives" — whichever the user moved more
167
179
  // (in pixel space) determines the size, the other follows. The
@@ -195,8 +207,8 @@ export function computeLockedResizeRect(
195
207
  }
196
208
 
197
209
  // Enforce minimum after clamping.
198
- distW = Math.max( distW, MIN_CROP_SIZE );
199
- distH = Math.max( distH, MIN_CROP_SIZE );
210
+ distW = Math.max( distW, minDistW );
211
+ distH = Math.max( distH, minDistH );
200
212
 
201
213
  // Compute the final rect position from the anchor.
202
214
  const newX = dirX > 0 ? anchorX : anchorX - distW;
@@ -214,11 +226,12 @@ export function computeLockedResizeRect(
214
226
  * axis symmetrically around the rect's center to preserve the ratio,
215
227
  * clamping symmetrically so the rect stays within bounds.
216
228
  *
217
- * @param drag The current drag state.
218
- * @param clientX Current mouse/touch X position in pixels.
219
- * @param clientY Current mouse/touch Y position in pixels.
220
- * @param imageSize The rendered image dimensions in pixels.
221
- * @param bounds The allowed crop area bounds.
229
+ * @param drag The current drag state.
230
+ * @param clientX Current mouse/touch X position in pixels.
231
+ * @param clientY Current mouse/touch Y position in pixels.
232
+ * @param imageSize The rendered image dimensions in pixels.
233
+ * @param bounds The allowed crop area bounds.
234
+ * @param minCropSize Minimum crop rect dimension in normalized space, per axis.
222
235
  * @return The new crop rect in normalized coordinates.
223
236
  */
224
237
  export function computeShiftLockedResizeRect(
@@ -226,7 +239,8 @@ export function computeShiftLockedResizeRect(
226
239
  clientX: number,
227
240
  clientY: number,
228
241
  imageSize: Size,
229
- bounds: CropBounds
242
+ bounds: CropBounds,
243
+ minCropSize: Size = DEFAULT_MIN_CROP_SIZE
230
244
  ): NormalizedRect {
231
245
  const s = drag.startRect;
232
246
  const pixelW = s.width * imageSize.width;
@@ -237,7 +251,8 @@ export function computeShiftLockedResizeRect(
237
251
  clientX,
238
252
  clientY,
239
253
  imageSize,
240
- bounds
254
+ bounds,
255
+ minCropSize
241
256
  );
242
257
  }
243
258
  const normalizedRatio = s.width / s.height;
@@ -255,7 +270,8 @@ export function computeShiftLockedResizeRect(
255
270
  clientY,
256
271
  imageSize,
257
272
  bounds,
258
- normalizedRatio
273
+ normalizedRatio,
274
+ minCropSize
259
275
  );
260
276
  }
261
277
 
@@ -266,7 +282,8 @@ export function computeShiftLockedResizeRect(
266
282
  clientX,
267
283
  clientY,
268
284
  imageSize,
269
- bounds
285
+ bounds,
286
+ minCropSize
270
287
  );
271
288
 
272
289
  if ( handle === 'n' || handle === 's' ) {
@@ -284,11 +301,11 @@ export function computeShiftLockedResizeRect(
284
301
  }
285
302
  // Enforce the minimum on the driving axis, then derive the
286
303
  // other axis from the ratio so it stays consistent. The
287
- // effective minimum height has to satisfy MIN_CROP_SIZE on
288
- // both axes simultaneously.
304
+ // effective minimum height has to satisfy the per-axis floor
305
+ // on both axes simultaneously.
289
306
  const minHeight = Math.max(
290
- MIN_CROP_SIZE,
291
- MIN_CROP_SIZE / normalizedRatio
307
+ minCropSize.height,
308
+ minCropSize.width / normalizedRatio
292
309
  );
293
310
  if ( newHeight < minHeight ) {
294
311
  newHeight = minHeight;
@@ -315,7 +332,10 @@ export function computeShiftLockedResizeRect(
315
332
  newHeight = maxHeight;
316
333
  newWidth = newHeight * normalizedRatio;
317
334
  }
318
- const minWidth = Math.max( MIN_CROP_SIZE, MIN_CROP_SIZE * normalizedRatio );
335
+ const minWidth = Math.max(
336
+ minCropSize.width,
337
+ minCropSize.height * normalizedRatio
338
+ );
319
339
  if ( newWidth < minWidth ) {
320
340
  newWidth = minWidth;
321
341
  newHeight = newWidth / normalizedRatio;