@wordpress/media-editor 0.7.1-next.v.202604201441.0 → 0.8.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 (300) hide show
  1. package/build/components/media-editor-canvas/index.cjs +54 -0
  2. package/build/components/media-editor-canvas/index.cjs.map +7 -0
  3. package/build/components/media-editor-crop-panel/index.cjs +116 -0
  4. package/build/components/media-editor-crop-panel/index.cjs.map +7 -0
  5. package/build/components/media-editor-modal/build-modifiers.cjs +91 -0
  6. package/build/components/media-editor-modal/build-modifiers.cjs.map +7 -0
  7. package/build/components/media-editor-modal/index.cjs +497 -0
  8. package/build/components/media-editor-modal/index.cjs.map +7 -0
  9. package/build/components/media-editor-toolbar/index.cjs +146 -0
  10. package/build/components/media-editor-toolbar/index.cjs.map +7 -0
  11. package/build/image-editor/core/camera.cjs +194 -0
  12. package/build/image-editor/core/camera.cjs.map +7 -0
  13. package/build/image-editor/core/constants.cjs +81 -0
  14. package/build/image-editor/core/constants.cjs.map +7 -0
  15. package/build/image-editor/core/containment.cjs +245 -0
  16. package/build/image-editor/core/containment.cjs.map +7 -0
  17. package/build/image-editor/core/export/canvas-renderer.cjs +151 -0
  18. package/build/image-editor/core/export/canvas-renderer.cjs.map +7 -0
  19. package/build/image-editor/core/index.cjs +50 -0
  20. package/build/image-editor/core/index.cjs.map +7 -0
  21. package/build/image-editor/core/interaction-controller.cjs +608 -0
  22. package/build/image-editor/core/interaction-controller.cjs.map +7 -0
  23. package/build/image-editor/core/math/rotation.cjs +41 -0
  24. package/build/image-editor/core/math/rotation.cjs.map +7 -0
  25. package/build/image-editor/core/source-region.cjs +95 -0
  26. package/build/image-editor/core/source-region.cjs.map +7 -0
  27. package/build/image-editor/core/state.cjs +269 -0
  28. package/build/image-editor/core/state.cjs.map +7 -0
  29. package/build/image-editor/core/stencil-math.cjs +197 -0
  30. package/build/image-editor/core/stencil-math.cjs.map +7 -0
  31. package/build/image-editor/core/transform-style.cjs +46 -0
  32. package/build/image-editor/core/transform-style.cjs.map +7 -0
  33. package/build/image-editor/core/transforms/pipeline.cjs +43 -0
  34. package/build/image-editor/core/transforms/pipeline.cjs.map +7 -0
  35. package/build/image-editor/core/types.cjs +19 -0
  36. package/build/image-editor/core/types.cjs.map +7 -0
  37. package/build/image-editor/index.cjs +57 -0
  38. package/build/image-editor/index.cjs.map +7 -0
  39. package/build/image-editor/react/components/cropper-provider.cjs +50 -0
  40. package/build/image-editor/react/components/cropper-provider.cjs.map +7 -0
  41. package/build/image-editor/react/components/cropper.cjs +365 -0
  42. package/build/image-editor/react/components/cropper.cjs.map +7 -0
  43. package/build/image-editor/react/components/index.cjs +36 -0
  44. package/build/image-editor/react/components/index.cjs.map +7 -0
  45. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +58 -0
  46. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +7 -0
  47. package/build/image-editor/react/components/overlays/grid-overlay.cjs +91 -0
  48. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +7 -0
  49. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +351 -0
  50. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +7 -0
  51. package/build/image-editor/react/hooks/index.cjs +31 -0
  52. package/build/image-editor/react/hooks/index.cjs.map +7 -0
  53. package/build/image-editor/react/hooks/use-aria-announcer.cjs +68 -0
  54. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +7 -0
  55. package/build/image-editor/react/hooks/use-cropper-state.cjs +158 -0
  56. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +7 -0
  57. package/build/image-editor/react/hooks/use-interaction.cjs +161 -0
  58. package/build/image-editor/react/hooks/use-interaction.cjs.map +7 -0
  59. package/build/image-editor/react/hooks/use-transform-style.cjs +38 -0
  60. package/build/image-editor/react/hooks/use-transform-style.cjs.map +7 -0
  61. package/build/index.cjs +5 -2
  62. package/build/index.cjs.map +2 -2
  63. package/build/lock-unlock.cjs +37 -0
  64. package/build/lock-unlock.cjs.map +7 -0
  65. package/build/private-apis.cjs +38 -0
  66. package/build/private-apis.cjs.map +7 -0
  67. package/build/store/actions.cjs +45 -0
  68. package/build/store/actions.cjs.map +7 -0
  69. package/build/store/constants.cjs +31 -0
  70. package/build/store/constants.cjs.map +7 -0
  71. package/build/store/index.cjs +51 -0
  72. package/build/store/index.cjs.map +7 -0
  73. package/build/store/reducer.cjs +51 -0
  74. package/build/store/reducer.cjs.map +7 -0
  75. package/build/store/selectors.cjs +43 -0
  76. package/build/store/selectors.cjs.map +7 -0
  77. package/build-module/components/media-editor-canvas/index.mjs +33 -0
  78. package/build-module/components/media-editor-canvas/index.mjs.map +7 -0
  79. package/build-module/components/media-editor-crop-panel/index.mjs +100 -0
  80. package/build-module/components/media-editor-crop-panel/index.mjs.map +7 -0
  81. package/build-module/components/media-editor-modal/build-modifiers.mjs +66 -0
  82. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +7 -0
  83. package/build-module/components/media-editor-modal/index.mjs +483 -0
  84. package/build-module/components/media-editor-modal/index.mjs.map +7 -0
  85. package/build-module/components/media-editor-toolbar/index.mjs +130 -0
  86. package/build-module/components/media-editor-toolbar/index.mjs.map +7 -0
  87. package/build-module/image-editor/core/camera.mjs +163 -0
  88. package/build-module/image-editor/core/camera.mjs.map +7 -0
  89. package/build-module/image-editor/core/constants.mjs +51 -0
  90. package/build-module/image-editor/core/constants.mjs.map +7 -0
  91. package/build-module/image-editor/core/containment.mjs +218 -0
  92. package/build-module/image-editor/core/containment.mjs.map +7 -0
  93. package/build-module/image-editor/core/export/canvas-renderer.mjs +120 -0
  94. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +7 -0
  95. package/build-module/image-editor/core/index.mjs +24 -0
  96. package/build-module/image-editor/core/index.mjs.map +7 -0
  97. package/build-module/image-editor/core/interaction-controller.mjs +583 -0
  98. package/build-module/image-editor/core/interaction-controller.mjs.map +7 -0
  99. package/build-module/image-editor/core/math/rotation.mjs +15 -0
  100. package/build-module/image-editor/core/math/rotation.mjs.map +7 -0
  101. package/build-module/image-editor/core/source-region.mjs +69 -0
  102. package/build-module/image-editor/core/source-region.mjs.map +7 -0
  103. package/build-module/image-editor/core/state.mjs +242 -0
  104. package/build-module/image-editor/core/state.mjs.map +7 -0
  105. package/build-module/image-editor/core/stencil-math.mjs +169 -0
  106. package/build-module/image-editor/core/stencil-math.mjs.map +7 -0
  107. package/build-module/image-editor/core/transform-style.mjs +21 -0
  108. package/build-module/image-editor/core/transform-style.mjs.map +7 -0
  109. package/build-module/image-editor/core/transforms/pipeline.mjs +17 -0
  110. package/build-module/image-editor/core/transforms/pipeline.mjs.map +7 -0
  111. package/build-module/image-editor/core/types.mjs +1 -0
  112. package/build-module/image-editor/core/types.mjs.map +7 -0
  113. package/build-module/image-editor/index.mjs +30 -0
  114. package/build-module/image-editor/index.mjs.map +7 -0
  115. package/build-module/image-editor/react/components/cropper-provider.mjs +26 -0
  116. package/build-module/image-editor/react/components/cropper-provider.mjs.map +7 -0
  117. package/build-module/image-editor/react/components/cropper.mjs +337 -0
  118. package/build-module/image-editor/react/components/cropper.mjs.map +7 -0
  119. package/build-module/image-editor/react/components/index.mjs +9 -0
  120. package/build-module/image-editor/react/components/index.mjs.map +7 -0
  121. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +33 -0
  122. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +7 -0
  123. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +66 -0
  124. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +7 -0
  125. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +330 -0
  126. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +7 -0
  127. package/build-module/image-editor/react/hooks/index.mjs +6 -0
  128. package/build-module/image-editor/react/hooks/index.mjs.map +7 -0
  129. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +43 -0
  130. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +7 -0
  131. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +137 -0
  132. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +7 -0
  133. package/build-module/image-editor/react/hooks/use-interaction.mjs +138 -0
  134. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +7 -0
  135. package/build-module/image-editor/react/hooks/use-transform-style.mjs +13 -0
  136. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +7 -0
  137. package/build-module/index.mjs +3 -1
  138. package/build-module/index.mjs.map +2 -2
  139. package/build-module/lock-unlock.mjs +11 -0
  140. package/build-module/lock-unlock.mjs.map +7 -0
  141. package/build-module/private-apis.mjs +13 -0
  142. package/build-module/private-apis.mjs.map +7 -0
  143. package/build-module/store/actions.mjs +19 -0
  144. package/build-module/store/actions.mjs.map +7 -0
  145. package/build-module/store/constants.mjs +6 -0
  146. package/build-module/store/constants.mjs.map +7 -0
  147. package/build-module/store/index.mjs +16 -0
  148. package/build-module/store/index.mjs.map +7 -0
  149. package/build-module/store/reducer.mjs +26 -0
  150. package/build-module/store/reducer.mjs.map +7 -0
  151. package/build-module/store/selectors.mjs +16 -0
  152. package/build-module/store/selectors.mjs.map +7 -0
  153. package/build-style/style-rtl.css +315 -0
  154. package/build-style/style.css +315 -0
  155. package/build-types/components/media-editor-canvas/index.d.ts +22 -0
  156. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -0
  157. package/build-types/components/media-editor-crop-panel/index.d.ts +45 -0
  158. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -0
  159. package/build-types/components/media-editor-modal/build-modifiers.d.ts +83 -0
  160. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -0
  161. package/build-types/components/media-editor-modal/index.d.ts +20 -0
  162. package/build-types/components/media-editor-modal/index.d.ts.map +1 -0
  163. package/build-types/components/media-editor-toolbar/index.d.ts +21 -0
  164. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -0
  165. package/build-types/image-editor/core/camera.d.ts +107 -0
  166. package/build-types/image-editor/core/camera.d.ts.map +1 -0
  167. package/build-types/image-editor/core/constants.d.ts +46 -0
  168. package/build-types/image-editor/core/constants.d.ts.map +1 -0
  169. package/build-types/image-editor/core/containment.d.ts +61 -0
  170. package/build-types/image-editor/core/containment.d.ts.map +1 -0
  171. package/build-types/image-editor/core/export/canvas-renderer.d.ts +106 -0
  172. package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +1 -0
  173. package/build-types/image-editor/core/index.d.ts +8 -0
  174. package/build-types/image-editor/core/index.d.ts.map +1 -0
  175. package/build-types/image-editor/core/interaction-controller.d.ts +178 -0
  176. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -0
  177. package/build-types/image-editor/core/math/rotation.d.ts +18 -0
  178. package/build-types/image-editor/core/math/rotation.d.ts.map +1 -0
  179. package/build-types/image-editor/core/source-region.d.ts +75 -0
  180. package/build-types/image-editor/core/source-region.d.ts.map +1 -0
  181. package/build-types/image-editor/core/state.d.ts +42 -0
  182. package/build-types/image-editor/core/state.d.ts.map +1 -0
  183. package/build-types/image-editor/core/stencil-math.d.ts +79 -0
  184. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -0
  185. package/build-types/image-editor/core/transform-style.d.ts +23 -0
  186. package/build-types/image-editor/core/transform-style.d.ts.map +1 -0
  187. package/build-types/image-editor/core/transforms/pipeline.d.ts +27 -0
  188. package/build-types/image-editor/core/transforms/pipeline.d.ts.map +1 -0
  189. package/build-types/image-editor/core/types.d.ts +211 -0
  190. package/build-types/image-editor/core/types.d.ts.map +1 -0
  191. package/build-types/image-editor/index.d.ts +16 -0
  192. package/build-types/image-editor/index.d.ts.map +1 -0
  193. package/build-types/image-editor/react/components/cropper-provider.d.ts +37 -0
  194. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -0
  195. package/build-types/image-editor/react/components/cropper.d.ts +59 -0
  196. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -0
  197. package/build-types/image-editor/react/components/index.d.ts +4 -0
  198. package/build-types/image-editor/react/components/index.d.ts.map +1 -0
  199. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +30 -0
  200. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -0
  201. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +30 -0
  202. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -0
  203. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +32 -0
  204. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -0
  205. package/build-types/image-editor/react/hooks/index.d.ts +3 -0
  206. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -0
  207. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +16 -0
  208. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -0
  209. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +68 -0
  210. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -0
  211. package/build-types/image-editor/react/hooks/use-interaction.d.ts +60 -0
  212. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -0
  213. package/build-types/image-editor/react/hooks/use-transform-style.d.ts +16 -0
  214. package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +1 -0
  215. package/build-types/image-editor/stories/rectangle-crop.story.d.ts +16 -0
  216. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -0
  217. package/build-types/index.d.ts +1 -0
  218. package/build-types/index.d.ts.map +1 -1
  219. package/build-types/lock-unlock.d.ts +2 -0
  220. package/build-types/lock-unlock.d.ts.map +1 -0
  221. package/build-types/private-apis.d.ts +2 -0
  222. package/build-types/private-apis.d.ts.map +1 -0
  223. package/build-types/store/actions.d.ts +27 -0
  224. package/build-types/store/actions.d.ts.map +1 -0
  225. package/build-types/store/constants.d.ts +2 -0
  226. package/build-types/store/constants.d.ts.map +1 -0
  227. package/build-types/store/index.d.ts +4 -0
  228. package/build-types/store/index.d.ts.map +1 -0
  229. package/build-types/store/reducer.d.ts +23 -0
  230. package/build-types/store/reducer.d.ts.map +1 -0
  231. package/build-types/store/selectors.d.ts +8 -0
  232. package/build-types/store/selectors.d.ts.map +1 -0
  233. package/package.json +21 -7
  234. package/src/components/media-editor-canvas/index.tsx +56 -0
  235. package/src/components/media-editor-canvas/style.scss +9 -0
  236. package/src/components/media-editor-crop-panel/index.tsx +143 -0
  237. package/src/components/media-editor-modal/build-modifiers.ts +164 -0
  238. package/src/components/media-editor-modal/index.tsx +639 -0
  239. package/src/components/media-editor-modal/style.scss +139 -0
  240. package/src/components/media-editor-modal/test/build-modifiers.test.ts +343 -0
  241. package/src/components/media-editor-toolbar/index.tsx +150 -0
  242. package/src/components/media-editor-toolbar/style.scss +21 -0
  243. package/src/image-editor/README.md +162 -0
  244. package/src/image-editor/core/camera.ts +352 -0
  245. package/src/image-editor/core/constants.ts +93 -0
  246. package/src/image-editor/core/containment.ts +435 -0
  247. package/src/image-editor/core/export/canvas-renderer.ts +253 -0
  248. package/src/image-editor/core/export/test/canvas-renderer.ts +813 -0
  249. package/src/image-editor/core/index.ts +35 -0
  250. package/src/image-editor/core/interaction-controller.ts +889 -0
  251. package/src/image-editor/core/math/rotation.ts +26 -0
  252. package/src/image-editor/core/source-region.ts +166 -0
  253. package/src/image-editor/core/state.ts +428 -0
  254. package/src/image-editor/core/stencil-math.ts +330 -0
  255. package/src/image-editor/core/test/camera.ts +593 -0
  256. package/src/image-editor/core/test/interaction-controller.ts +996 -0
  257. package/src/image-editor/core/test/preview-export-parity.ts +242 -0
  258. package/src/image-editor/core/test/render-camera-bridge.ts +348 -0
  259. package/src/image-editor/core/test/state.ts +762 -0
  260. package/src/image-editor/core/test/stencil-math.ts +321 -0
  261. package/src/image-editor/core/transform-style.ts +44 -0
  262. package/src/image-editor/core/transforms/pipeline.ts +43 -0
  263. package/src/image-editor/core/transforms/test/pipeline.ts +269 -0
  264. package/src/image-editor/core/types.ts +177 -0
  265. package/src/image-editor/docs/architecture.md +96 -0
  266. package/src/image-editor/docs/recipes.md +720 -0
  267. package/src/image-editor/docs/roadmap.md +66 -0
  268. package/src/image-editor/index.ts +43 -0
  269. package/src/image-editor/react/components/cropper-provider.tsx +73 -0
  270. package/src/image-editor/react/components/cropper.scss +195 -0
  271. package/src/image-editor/react/components/cropper.tsx +534 -0
  272. package/src/image-editor/react/components/index.ts +3 -0
  273. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +57 -0
  274. package/src/image-editor/react/components/overlays/grid-overlay.tsx +80 -0
  275. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +513 -0
  276. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +265 -0
  277. package/src/image-editor/react/components/test/cropper.tsx +140 -0
  278. package/src/image-editor/react/hooks/index.ts +2 -0
  279. package/src/image-editor/react/hooks/test/use-cropper-state.ts +913 -0
  280. package/src/image-editor/react/hooks/test/use-interaction.ts +218 -0
  281. package/src/image-editor/react/hooks/test/use-transform-style.ts +146 -0
  282. package/src/image-editor/react/hooks/use-aria-announcer.ts +64 -0
  283. package/src/image-editor/react/hooks/use-cropper-state.ts +238 -0
  284. package/src/image-editor/react/hooks/use-interaction.ts +223 -0
  285. package/src/image-editor/react/hooks/use-transform-style.ts +30 -0
  286. package/src/image-editor/stories/rectangle-crop.story.tsx +1085 -0
  287. package/src/image-editor/stories/style.css +94 -0
  288. package/src/image-editor/style.scss +1 -0
  289. package/src/index.ts +3 -0
  290. package/src/lock-unlock.ts +10 -0
  291. package/src/private-apis.ts +12 -0
  292. package/src/store/actions.ts +33 -0
  293. package/src/store/constants.ts +1 -0
  294. package/src/store/index.ts +20 -0
  295. package/src/store/reducer.ts +48 -0
  296. package/src/store/selectors.ts +16 -0
  297. package/src/store/test/reducer.ts +57 -0
  298. package/src/style.scss +4 -0
  299. package/tsconfig.json +10 -2
  300. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,330 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import type { NormalizedRect, Size } from './types';
5
+
6
+ /** Minimum crop rect dimension in normalized space (5% of visual area). */
7
+ export const MIN_CROP_SIZE = 0.05;
8
+
9
+ /**
10
+ * Handle position identifiers for the 8 resize handles.
11
+ */
12
+ export type HandlePosition = 'n' | 's' | 'e' | 'w' | 'nw' | 'ne' | 'sw' | 'se';
13
+
14
+ /**
15
+ * Bounds within which the crop rect must stay.
16
+ */
17
+ export interface CropBounds {
18
+ minX: number;
19
+ minY: number;
20
+ maxX: number;
21
+ maxY: number;
22
+ }
23
+
24
+ /**
25
+ * Drag state for tracking a resize interaction.
26
+ */
27
+ export interface ResizeDragState {
28
+ /** Which handle is being dragged. */
29
+ handle: HandlePosition;
30
+ /** The mouse position (pixels) when the drag started. */
31
+ startX: number;
32
+ startY: number;
33
+ /** The crop rect (normalized) when the drag started. */
34
+ startRect: NormalizedRect;
35
+ }
36
+
37
+ /**
38
+ * Compute the new crop rect for a free (no aspect ratio) resize.
39
+ *
40
+ * Each edge moves independently based on the handle being dragged.
41
+ * Edges are clamped to the provided bounds and maintain a minimum size.
42
+ *
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.
48
+ * @return The new crop rect in normalized coordinates.
49
+ */
50
+ export function computeFreeResizeRect(
51
+ drag: ResizeDragState,
52
+ clientX: number,
53
+ clientY: number,
54
+ imageSize: Size,
55
+ bounds: CropBounds
56
+ ): NormalizedRect {
57
+ const dx =
58
+ imageSize.width > 0 ? ( clientX - drag.startX ) / imageSize.width : 0;
59
+ const dy =
60
+ imageSize.height > 0 ? ( clientY - drag.startY ) / imageSize.height : 0;
61
+
62
+ const s = drag.startRect;
63
+ const handle = drag.handle;
64
+
65
+ let edgeTop = s.y;
66
+ let edgeBottom = s.y + s.height;
67
+ let edgeLeft = s.x;
68
+ let edgeRight = s.x + s.width;
69
+
70
+ if ( handle === 'n' || handle === 'nw' || handle === 'ne' ) {
71
+ edgeTop = Math.max(
72
+ bounds.minY,
73
+ Math.min( s.y + dy, edgeBottom - MIN_CROP_SIZE )
74
+ );
75
+ }
76
+ if ( handle === 's' || handle === 'sw' || handle === 'se' ) {
77
+ edgeBottom = Math.max(
78
+ edgeTop + MIN_CROP_SIZE,
79
+ Math.min( s.y + s.height + dy, bounds.maxY )
80
+ );
81
+ }
82
+ if ( handle === 'w' || handle === 'nw' || handle === 'sw' ) {
83
+ edgeLeft = Math.max(
84
+ bounds.minX,
85
+ Math.min( s.x + dx, edgeRight - MIN_CROP_SIZE )
86
+ );
87
+ }
88
+ if ( handle === 'e' || handle === 'ne' || handle === 'se' ) {
89
+ edgeRight = Math.max(
90
+ edgeLeft + MIN_CROP_SIZE,
91
+ Math.min( s.x + s.width + dx, bounds.maxX )
92
+ );
93
+ }
94
+
95
+ return {
96
+ x: edgeLeft,
97
+ y: edgeTop,
98
+ width: edgeRight - edgeLeft,
99
+ height: edgeBottom - edgeTop,
100
+ };
101
+ }
102
+
103
+ /**
104
+ * Compute the new crop rect for a locked-aspect-ratio corner resize.
105
+ *
106
+ * The opposite corner is the anchor. The dragged corner moves freely
107
+ * but the result is clamped to maintain the aspect ratio and stay
108
+ * within bounds.
109
+ *
110
+ * @param drag The current drag state.
111
+ * @param clientX Current mouse/touch X position in pixels.
112
+ * @param clientY Current mouse/touch Y position in pixels.
113
+ * @param imageSize The rendered image dimensions in pixels.
114
+ * @param bounds The allowed crop area bounds.
115
+ * @param normalizedRatio The locked aspect ratio (width / height in normalized space).
116
+ * @return The new crop rect in normalized coordinates.
117
+ */
118
+ export function computeLockedResizeRect(
119
+ drag: ResizeDragState,
120
+ clientX: number,
121
+ clientY: number,
122
+ imageSize: Size,
123
+ bounds: CropBounds,
124
+ normalizedRatio: number
125
+ ): NormalizedRect {
126
+ // The math below divides by `normalizedRatio` and `imageSize`, so
127
+ // bail out with the start rect when any of them is zero. This can
128
+ // happen if a resize is triggered (e.g. via keyboard arrows on a
129
+ // focused handle) before the image has loaded.
130
+ if (
131
+ normalizedRatio <= 0 ||
132
+ imageSize.width <= 0 ||
133
+ imageSize.height <= 0
134
+ ) {
135
+ return { ...drag.startRect };
136
+ }
137
+
138
+ const dx = ( clientX - drag.startX ) / imageSize.width;
139
+ const dy = ( clientY - drag.startY ) / imageSize.height;
140
+
141
+ const s = drag.startRect;
142
+ const handle = drag.handle;
143
+
144
+ // Determine the anchor corner (opposite to the dragged corner).
145
+ const anchorX = handle === 'nw' || handle === 'sw' ? s.x + s.width : s.x;
146
+ const anchorY = handle === 'nw' || handle === 'ne' ? s.y + s.height : s.y;
147
+
148
+ // Direction the crop grows from the anchor (+1 = right/down, -1 = left/up).
149
+ const dirX = handle === 'nw' || handle === 'sw' ? -1 : 1;
150
+ const dirY = handle === 'nw' || handle === 'ne' ? -1 : 1;
151
+
152
+ // Desired new position of the dragged corner.
153
+ const draggedX =
154
+ ( handle === 'nw' || handle === 'sw' ? s.x : s.x + s.width ) + dx;
155
+ const draggedY =
156
+ ( handle === 'nw' || handle === 'ne' ? s.y : s.y + s.height ) + dy;
157
+
158
+ // Raw distances from anchor to dragged corner.
159
+ let distW = ( draggedX - anchorX ) * dirX;
160
+ let distH = ( draggedY - anchorY ) * dirY;
161
+
162
+ // Enforce minimum size.
163
+ distW = Math.max( distW, MIN_CROP_SIZE );
164
+ distH = Math.max( distH, MIN_CROP_SIZE );
165
+
166
+ // Determine which axis "drives" — whichever the user moved more
167
+ // (in pixel space) determines the size, the other follows. The
168
+ // `normalizedRatio` is w/h in normalized space; the equivalent
169
+ // pixel-space ratio is `normalizedRatio * imageW / imageH`. We
170
+ // compare the pixel motion ratio against that pixel-space ratio
171
+ // so the units line up (was a unit mismatch on non-square images).
172
+ const pixelDistW = distW * imageSize.width;
173
+ const pixelDistH = distH * imageSize.height;
174
+ const pixelRatio = ( normalizedRatio * imageSize.width ) / imageSize.height;
175
+ if ( pixelDistW / pixelDistH > pixelRatio ) {
176
+ // Width is the driver — compute height from ratio.
177
+ distH = distW / normalizedRatio;
178
+ } else {
179
+ // Height is the driver — compute width from ratio.
180
+ distW = distH * normalizedRatio;
181
+ }
182
+
183
+ // Clamp to image coverage bounds. If the rect would exceed,
184
+ // shrink it (maintaining ratio) to fit.
185
+ const maxW = dirX > 0 ? bounds.maxX - anchorX : anchorX - bounds.minX;
186
+ const maxH = dirY > 0 ? bounds.maxY - anchorY : anchorY - bounds.minY;
187
+
188
+ if ( distW > maxW ) {
189
+ distW = maxW;
190
+ distH = distW / normalizedRatio;
191
+ }
192
+ if ( distH > maxH ) {
193
+ distH = maxH;
194
+ distW = distH * normalizedRatio;
195
+ }
196
+
197
+ // Enforce minimum after clamping.
198
+ distW = Math.max( distW, MIN_CROP_SIZE );
199
+ distH = Math.max( distH, MIN_CROP_SIZE );
200
+
201
+ // Compute the final rect position from the anchor.
202
+ const newX = dirX > 0 ? anchorX : anchorX - distW;
203
+ const newY = dirY > 0 ? anchorY : anchorY - distH;
204
+
205
+ return { x: newX, y: newY, width: distW, height: distH };
206
+ }
207
+
208
+ /**
209
+ * Compute the new crop rect for a resize that temporarily locks the
210
+ * aspect ratio to `drag.startRect`'s ratio (e.g. while Shift is held).
211
+ *
212
+ * Corner handles delegate to {@link computeLockedResizeRect}. Edge
213
+ * handles size the dragged axis freely, then expand the perpendicular
214
+ * axis symmetrically around the rect's center to preserve the ratio,
215
+ * clamping symmetrically so the rect stays within bounds.
216
+ *
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.
222
+ * @return The new crop rect in normalized coordinates.
223
+ */
224
+ export function computeShiftLockedResizeRect(
225
+ drag: ResizeDragState,
226
+ clientX: number,
227
+ clientY: number,
228
+ imageSize: Size,
229
+ bounds: CropBounds
230
+ ): NormalizedRect {
231
+ const s = drag.startRect;
232
+ const pixelW = s.width * imageSize.width;
233
+ const pixelH = s.height * imageSize.height;
234
+ if ( pixelH <= 0 || pixelW <= 0 ) {
235
+ return computeFreeResizeRect(
236
+ drag,
237
+ clientX,
238
+ clientY,
239
+ imageSize,
240
+ bounds
241
+ );
242
+ }
243
+ const normalizedRatio = s.width / s.height;
244
+
245
+ const handle = drag.handle;
246
+ if (
247
+ handle === 'nw' ||
248
+ handle === 'ne' ||
249
+ handle === 'sw' ||
250
+ handle === 'se'
251
+ ) {
252
+ return computeLockedResizeRect(
253
+ drag,
254
+ clientX,
255
+ clientY,
256
+ imageSize,
257
+ bounds,
258
+ normalizedRatio
259
+ );
260
+ }
261
+
262
+ // Edge handle: size the dragged axis with free-resize logic, then
263
+ // expand the perpendicular axis symmetrically around the rect's center.
264
+ const free = computeFreeResizeRect(
265
+ drag,
266
+ clientX,
267
+ clientY,
268
+ imageSize,
269
+ bounds
270
+ );
271
+
272
+ if ( handle === 'n' || handle === 's' ) {
273
+ // Height is the driver; derive width from ratio.
274
+ let newHeight = free.height;
275
+ let newWidth = newHeight * normalizedRatio;
276
+ const centerX = s.x + s.width / 2;
277
+ // Symmetric clamp: limit by the smaller of the two side gaps so
278
+ // the rect stays centered around centerX.
279
+ const maxWidth =
280
+ Math.min( centerX - bounds.minX, bounds.maxX - centerX ) * 2;
281
+ if ( newWidth > maxWidth ) {
282
+ newWidth = maxWidth;
283
+ newHeight = newWidth / normalizedRatio;
284
+ }
285
+ // Enforce the minimum on the driving axis, then derive the
286
+ // 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.
289
+ const minHeight = Math.max(
290
+ MIN_CROP_SIZE,
291
+ MIN_CROP_SIZE / normalizedRatio
292
+ );
293
+ if ( newHeight < minHeight ) {
294
+ newHeight = minHeight;
295
+ newWidth = newHeight * normalizedRatio;
296
+ }
297
+ // Re-anchor the dragged axis to the opposite edge so the
298
+ // height adjustment after clamping keeps that edge fixed.
299
+ const newY = handle === 'n' ? s.y + s.height - newHeight : s.y;
300
+ return {
301
+ x: centerX - newWidth / 2,
302
+ y: newY,
303
+ width: newWidth,
304
+ height: newHeight,
305
+ };
306
+ }
307
+
308
+ // 'e' or 'w': width is the driver; derive height from ratio.
309
+ let newWidth = free.width;
310
+ let newHeight = newWidth / normalizedRatio;
311
+ const centerY = s.y + s.height / 2;
312
+ const maxHeight =
313
+ Math.min( centerY - bounds.minY, bounds.maxY - centerY ) * 2;
314
+ if ( newHeight > maxHeight ) {
315
+ newHeight = maxHeight;
316
+ newWidth = newHeight * normalizedRatio;
317
+ }
318
+ const minWidth = Math.max( MIN_CROP_SIZE, MIN_CROP_SIZE * normalizedRatio );
319
+ if ( newWidth < minWidth ) {
320
+ newWidth = minWidth;
321
+ newHeight = newWidth / normalizedRatio;
322
+ }
323
+ const newX = handle === 'w' ? s.x + s.width - newWidth : s.x;
324
+ return {
325
+ x: newX,
326
+ y: centerY - newHeight / 2,
327
+ width: newWidth,
328
+ height: newHeight,
329
+ };
330
+ }