@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
@@ -0,0 +1,269 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useLayoutEffect, useRef, useState } from '@wordpress/element';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import type { HandlePosition, NormalizedRect, Size } from '../../../core/types';
10
+
11
+ const HANDLE_GAP_PX = 12;
12
+
13
+ /**
14
+ * Which side of the handle a tooltip axis sits on. `before` places the
15
+ * tooltip on the negative side of the handle (above for Y, left for X),
16
+ * `after` on the positive side, `center` keeps it aligned with the handle.
17
+ */
18
+ type AxisSide = 'before' | 'center' | 'after';
19
+
20
+ interface Anchor {
21
+ x: AxisSide;
22
+ y: AxisSide;
23
+ }
24
+
25
+ const TRANSLATE_PERCENT: Record< AxisSide, string > = {
26
+ before: '-100%',
27
+ center: '-50%',
28
+ after: '0',
29
+ };
30
+
31
+ const GAP_DIRECTION: Record< AxisSide, number > = {
32
+ before: -1,
33
+ center: 0,
34
+ after: 1,
35
+ };
36
+
37
+ const OPPOSITE_SIDE: Record< AxisSide, AxisSide > = {
38
+ before: 'after',
39
+ center: 'center',
40
+ after: 'before',
41
+ };
42
+
43
+ /**
44
+ * Preferred anchor for a handle, before edge-clipping is considered:
45
+ * tooltip sits outward from the crop rectangle's center.
46
+ *
47
+ * @param handle
48
+ */
49
+ function preferredAnchor( handle: HandlePosition ): Anchor {
50
+ let x: AxisSide = 'center';
51
+ if ( handle.includes( 'w' ) ) {
52
+ x = 'before';
53
+ } else if ( handle.includes( 'e' ) ) {
54
+ x = 'after';
55
+ }
56
+
57
+ let y: AxisSide = 'center';
58
+ if ( handle.includes( 'n' ) ) {
59
+ y = 'before';
60
+ } else if ( handle.includes( 's' ) ) {
61
+ y = 'after';
62
+ }
63
+
64
+ return { x, y };
65
+ }
66
+
67
+ /**
68
+ * Start coordinate of a tooltip placed on the given side of `handlePos`,
69
+ * accounting for the per-side handle gap and the tooltip's own length.
70
+ *
71
+ * @param side
72
+ * @param handlePos
73
+ * @param length
74
+ */
75
+ function sideStart(
76
+ side: AxisSide,
77
+ handlePos: number,
78
+ length: number
79
+ ): number {
80
+ if ( side === 'before' ) {
81
+ return handlePos - HANDLE_GAP_PX - length;
82
+ }
83
+ if ( side === 'after' ) {
84
+ return handlePos + HANDLE_GAP_PX;
85
+ }
86
+ return handlePos - length / 2;
87
+ }
88
+
89
+ /**
90
+ * Picks a side that keeps the tooltip inside the container along one
91
+ * axis, preferring the supplied side and falling back to its opposite,
92
+ * then center.
93
+ *
94
+ * @param preferred
95
+ * @param handlePos
96
+ * @param length
97
+ * @param containerLength
98
+ */
99
+ function fittingSide(
100
+ preferred: AxisSide,
101
+ handlePos: number,
102
+ length: number,
103
+ containerLength: number
104
+ ): AxisSide {
105
+ const fits = ( side: AxisSide ) => {
106
+ const start = sideStart( side, handlePos, length );
107
+ return start >= 0 && start + length <= containerLength;
108
+ };
109
+
110
+ if ( fits( preferred ) ) {
111
+ return preferred;
112
+ }
113
+ const opposite = OPPOSITE_SIDE[ preferred ];
114
+ if ( opposite !== preferred && fits( opposite ) ) {
115
+ return opposite;
116
+ }
117
+ if ( preferred !== 'center' && fits( 'center' ) ) {
118
+ return 'center';
119
+ }
120
+ // None of the candidates fully fit — pick the one that pushes the
121
+ // least past the container edge. Without this, the chain falls back
122
+ // to `preferred`, which is the off-canvas side that triggered the
123
+ // flip in the first place, so the tooltip ends up clipped.
124
+ const overflow = ( side: AxisSide ) => {
125
+ const start = sideStart( side, handlePos, length );
126
+ return (
127
+ Math.max( 0, -start ) +
128
+ Math.max( 0, start + length - containerLength )
129
+ );
130
+ };
131
+ const candidates: AxisSide[] = [ preferred, opposite, 'center' ];
132
+ return candidates.reduce( ( best, side ) =>
133
+ overflow( side ) < overflow( best ) ? side : best
134
+ );
135
+ }
136
+
137
+ interface DimensionsOverlayProps {
138
+ /** Crop rectangle in normalized coordinates. */
139
+ cropRect: NormalizedRect;
140
+ /** Container element dimensions in pixels. */
141
+ containerSize: Size;
142
+ /** Rendered image dimensions in pixels within the container. */
143
+ imageSize: Size;
144
+ /** Currently-dragged handle, or null when no pointer drag is active. */
145
+ activeHandle: HandlePosition | null;
146
+ /** Output crop width in source pixels. */
147
+ outputWidth: number;
148
+ /** Output crop height in source pixels. */
149
+ outputHeight: number;
150
+ }
151
+
152
+ /**
153
+ * Tooltip that follows the active resize handle during a pointer drag,
154
+ * showing the current output dimensions of the crop in source pixels.
155
+ *
156
+ * Renders nothing outside of a pointer-driven resize, so keyboard
157
+ * arrow-key adjustments and pan/zoom gestures stay quiet.
158
+ *
159
+ * @param props
160
+ * @param props.cropRect
161
+ * @param props.containerSize
162
+ * @param props.imageSize
163
+ * @param props.activeHandle
164
+ * @param props.outputWidth
165
+ * @param props.outputHeight
166
+ */
167
+ export function DimensionsOverlay( {
168
+ cropRect,
169
+ containerSize,
170
+ imageSize,
171
+ activeHandle,
172
+ outputWidth,
173
+ outputHeight,
174
+ }: DimensionsOverlayProps ) {
175
+ const tooltipRef = useRef< HTMLDivElement >( null );
176
+ const [ tooltipSize, setTooltipSize ] = useState< Size | null >( null );
177
+
178
+ // Measure the rendered tooltip so we can flip its anchor when it
179
+ // would otherwise be clipped by the cropper's `overflow: hidden`
180
+ // root. Runs synchronously before paint, so the user never sees
181
+ // the un-flipped position. Tracks `outputWidth`/`outputHeight`
182
+ // because they drive the digit count, the only thing that changes
183
+ // the tooltip's intrinsic size during a drag.
184
+ useLayoutEffect( () => {
185
+ if ( ! tooltipRef.current ) {
186
+ return;
187
+ }
188
+ const rect = tooltipRef.current.getBoundingClientRect();
189
+ setTooltipSize( ( prev ) => {
190
+ if (
191
+ prev &&
192
+ prev.width === rect.width &&
193
+ prev.height === rect.height
194
+ ) {
195
+ return prev;
196
+ }
197
+ return { width: rect.width, height: rect.height };
198
+ } );
199
+ }, [ outputWidth, outputHeight ] );
200
+
201
+ if (
202
+ ! activeHandle ||
203
+ containerSize.width === 0 ||
204
+ containerSize.height === 0
205
+ ) {
206
+ return null;
207
+ }
208
+
209
+ const offsetX = ( containerSize.width - imageSize.width ) / 2;
210
+ const offsetY = ( containerSize.height - imageSize.height ) / 2;
211
+ const left = offsetX + cropRect.x * imageSize.width;
212
+ const top = offsetY + cropRect.y * imageSize.height;
213
+ const width = cropRect.width * imageSize.width;
214
+ const height = cropRect.height * imageSize.height;
215
+
216
+ // Handle pixel position within the container. Same math as the
217
+ // stencil's handle layout, just keyed by direction here so the
218
+ // tooltip can anchor to any of the eight.
219
+ let handleX = left + width / 2;
220
+ if ( activeHandle.includes( 'w' ) ) {
221
+ handleX = left;
222
+ } else if ( activeHandle.includes( 'e' ) ) {
223
+ handleX = left + width;
224
+ }
225
+ let handleY = top + height / 2;
226
+ if ( activeHandle.includes( 'n' ) ) {
227
+ handleY = top;
228
+ } else if ( activeHandle.includes( 's' ) ) {
229
+ handleY = top + height;
230
+ }
231
+
232
+ const preferred = preferredAnchor( activeHandle );
233
+ const anchor: Anchor = tooltipSize
234
+ ? {
235
+ x: fittingSide(
236
+ preferred.x,
237
+ handleX,
238
+ tooltipSize.width,
239
+ containerSize.width
240
+ ),
241
+ y: fittingSide(
242
+ preferred.y,
243
+ handleY,
244
+ tooltipSize.height,
245
+ containerSize.height
246
+ ),
247
+ }
248
+ : preferred;
249
+
250
+ return (
251
+ <div
252
+ ref={ tooltipRef }
253
+ className="wp-media-editor-image-editor__dimensions-tooltip"
254
+ data-testid="cropper-dimensions-tooltip"
255
+ style={ {
256
+ left: handleX + GAP_DIRECTION[ anchor.x ] * HANDLE_GAP_PX,
257
+ top: handleY + GAP_DIRECTION[ anchor.y ] * HANDLE_GAP_PX,
258
+ transform: `translate(${ TRANSLATE_PERCENT[ anchor.x ] }, ${
259
+ TRANSLATE_PERCENT[ anchor.y ]
260
+ })`,
261
+ } }
262
+ aria-hidden="true"
263
+ >
264
+ { `W: ${ Math.round( outputWidth ) }px H: ${ Math.round(
265
+ outputHeight
266
+ ) }px` }
267
+ </div>
268
+ );
269
+ }
@@ -103,6 +103,7 @@ type RectangleStencilProps = StencilProps;
103
103
  * @param props.stencilTransition CSS transition string for settle animation.
104
104
  * @param props.cropBounds Maximum crop rect bounds from camera (zoom/rotation-aware).
105
105
  * @param props.onEscape Called when Escape is pressed on a resize handle.
106
+ * @param props.minCropSize Minimum crop rect dimension in normalized space, per axis.
106
107
  * @return The rectangle stencil element.
107
108
  */
108
109
  export function RectangleStencil( {
@@ -117,6 +118,7 @@ export function RectangleStencil( {
117
118
  stencilTransition,
118
119
  cropBounds,
119
120
  onEscape,
121
+ minCropSize,
120
122
  }: RectangleStencilProps ) {
121
123
  // Use cropBounds from the camera if available, otherwise default to [0,1].
122
124
  const boundsMinX = cropBounds?.minX ?? 0;
@@ -132,7 +134,9 @@ export function RectangleStencil( {
132
134
  } ),
133
135
  [ boundsMinX, boundsMinY, boundsMaxX, boundsMaxY ]
134
136
  );
135
- const keyboardSettleTimerRef = useRef< ReturnType< typeof setTimeout > >();
137
+ const keyboardSettleTimerRef = useRef<
138
+ ReturnType< typeof setTimeout > | undefined
139
+ >( undefined );
136
140
  const keyboardResizeActiveRef = useRef( false );
137
141
  const resizeHandleDescriptionId = useId();
138
142
  const hasLockedRatio = !! ( aspectRatio && aspectRatio > 0 );
@@ -288,7 +292,7 @@ export function RectangleStencil( {
288
292
  el.addEventListener( 'pointerup', onEnd );
289
293
  el.addEventListener( 'lostpointercapture', onEnd );
290
294
 
291
- onResizeStart?.();
295
+ onResizeStart?.( handle );
292
296
  // Cancel any pending keyboard settle so it can't fire onResizeEnd
293
297
  // mid-drag if the user switches from keyboard to pointer within
294
298
  // the settle window.
@@ -308,8 +312,15 @@ export function RectangleStencil( {
308
312
  clientX: number,
309
313
  clientY: number
310
314
  ): NormalizedRect =>
311
- computeFreeResizeRect( drag, clientX, clientY, imageSize, bounds ),
312
- [ imageSize, bounds ]
315
+ computeFreeResizeRect(
316
+ drag,
317
+ clientX,
318
+ clientY,
319
+ imageSize,
320
+ bounds,
321
+ minCropSize
322
+ ),
323
+ [ imageSize, bounds, minCropSize ]
313
324
  );
314
325
 
315
326
  /**
@@ -328,9 +339,10 @@ export function RectangleStencil( {
328
339
  clientY,
329
340
  imageSize,
330
341
  bounds,
331
- normalizedRatio
342
+ normalizedRatio,
343
+ minCropSize
332
344
  ),
333
- [ imageSize, bounds, normalizedRatio ]
345
+ [ imageSize, bounds, normalizedRatio, minCropSize ]
334
346
  );
335
347
 
336
348
  /**
@@ -348,9 +360,10 @@ export function RectangleStencil( {
348
360
  clientX,
349
361
  clientY,
350
362
  imageSize,
351
- bounds
363
+ bounds,
364
+ minCropSize
352
365
  ),
353
- [ imageSize, bounds ]
366
+ [ imageSize, bounds, minCropSize ]
354
367
  );
355
368
 
356
369
  latestHandlersRef.current = {
@@ -392,7 +405,7 @@ export function RectangleStencil( {
392
405
 
393
406
  if ( ! keyboardResizeActiveRef.current ) {
394
407
  keyboardResizeActiveRef.current = true;
395
- onResizeStart?.();
408
+ onResizeStart?.( handle );
396
409
  }
397
410
 
398
411
  const scheduleKeyboardResizeEnd = () => {
@@ -13,7 +13,7 @@ import {
13
13
  * Internal dependencies
14
14
  */
15
15
  import { Cropper } from '../cropper';
16
- import type { UseCropperStateReturn } from '../../hooks/use-cropper-state';
16
+ import type { CropperController } from '../../hooks/use-cropper-reducer';
17
17
  import { DEFAULT_STATE } from '../../../core/constants';
18
18
 
19
19
  const GRID_TEST_ID = 'cropper-grid';
@@ -21,7 +21,7 @@ const GRID_INTERACTIVE_CLASS =
21
21
  'wp-media-editor-image-editor__canvas--grid-interactive';
22
22
  const SHOW_GRID_CLASS = 'wp-media-editor-image-editor__canvas--show-grid';
23
23
 
24
- function createController(): UseCropperStateReturn {
24
+ function createController(): CropperController {
25
25
  return {
26
26
  state: {
27
27
  ...DEFAULT_STATE,
@@ -37,18 +37,16 @@ function createController(): UseCropperStateReturn {
37
37
  setZoomAtPoint: jest.fn(),
38
38
  setRotation: jest.fn(),
39
39
  setFlip: jest.fn(),
40
+ toggleFlip: jest.fn(),
40
41
  snapRotate90: jest.fn(),
41
42
  setCropRect: jest.fn(),
42
43
  settleCrop: jest.fn(),
43
44
  applyOperation: jest.fn(),
44
45
  reset: jest.fn(),
45
46
  isDirty: false,
46
- hasUndo: false,
47
- hasRedo: false,
48
- undo: jest.fn(),
49
- redo: jest.fn(),
50
- commitHistory: jest.fn(),
51
47
  getCroppedImage: jest.fn(),
48
+ setVisualSize: jest.fn(),
49
+ adjustCropRectForViewport: jest.fn(),
52
50
  };
53
51
  }
54
52
 
@@ -176,6 +174,46 @@ describe( 'Cropper', () => {
176
174
  );
177
175
  } );
178
176
 
177
+ it( 'clears the keyboard-active class when a pointer drag starts on a handle', async () => {
178
+ render(
179
+ <Cropper
180
+ src="test.jpg"
181
+ controller={ createController() }
182
+ showDimming={ false }
183
+ freeformCrop
184
+ focusOnMount
185
+ />
186
+ );
187
+
188
+ const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
189
+ const handle = await screen.findByRole( 'button', {
190
+ name: 'Resize top-left corner',
191
+ } );
192
+ const focusVisibleClass =
193
+ 'wp-media-editor-image-editor__canvas--focus-visible';
194
+
195
+ // Simulate keyboard resize on a handle — the canvas should pick up
196
+ // the keyboard-active class via the capture-phase keydown listener.
197
+ act( () => {
198
+ handle.focus();
199
+ } );
200
+ fireEvent.keyDown( handle, { key: 'ArrowRight' } );
201
+ expect( canvas ).toHaveClass( focusVisibleClass );
202
+
203
+ // Switching to the mouse on the same handle must drop the
204
+ // keyboard-active state, even though the handle's pointerdown
205
+ // handler calls stopPropagation.
206
+ fireEvent.pointerDown( handle, {
207
+ button: 0,
208
+ clientX: 100,
209
+ clientY: 100,
210
+ pointerId: 1,
211
+ } );
212
+ expect( canvas ).not.toHaveClass( focusVisibleClass );
213
+
214
+ fireEvent.pointerUp( handle, { pointerId: 1 } );
215
+ } );
216
+
179
217
  it( 'returns focus to the crop area on Escape from a resize handle', async () => {
180
218
  render(
181
219
  <Cropper
@@ -254,6 +292,7 @@ describe( 'Cropper', () => {
254
292
  name: 'Resize top-left corner',
255
293
  } );
256
294
  ( controller.setCropRect as jest.Mock ).mockClear();
295
+ ( controller.adjustCropRectForViewport as jest.Mock ).mockClear();
257
296
 
258
297
  rerender(
259
298
  <Cropper
@@ -270,6 +309,7 @@ describe( 'Cropper', () => {
270
309
  } )
271
310
  ).not.toBeInTheDocument();
272
311
  expect( controller.setCropRect ).not.toHaveBeenCalled();
312
+ expect( controller.adjustCropRectForViewport ).not.toHaveBeenCalled();
273
313
  } );
274
314
 
275
315
  it( 'centers a fixed-ratio crop when freeform handles are off', async () => {
@@ -289,12 +329,14 @@ describe( 'Cropper', () => {
289
329
  );
290
330
 
291
331
  await waitFor( () =>
292
- expect( controller.setCropRect ).toHaveBeenCalledWith( {
293
- x: expect.closeTo( 1 / 6, 5 ),
294
- y: 0,
295
- width: expect.closeTo( 2 / 3, 5 ),
296
- height: 1,
297
- } )
332
+ expect( controller.adjustCropRectForViewport ).toHaveBeenCalledWith(
333
+ {
334
+ x: expect.closeTo( 1 / 6, 5 ),
335
+ y: 0,
336
+ width: expect.closeTo( 2 / 3, 5 ),
337
+ height: 1,
338
+ }
339
+ )
298
340
  );
299
341
  } );
300
342
 
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import type {
5
+ CropperAction,
6
+ CropperState,
7
+ Flip,
8
+ NormalizedPoint,
9
+ NormalizedRect,
10
+ TransformOperation,
11
+ } from '../../core/types';
12
+ import { buildFocalPointZoomAction } from '../../core/setter-helpers';
13
+
14
+ /**
15
+ * The named cropper setters shared by every hook that drives the
16
+ * cropper reducer — the pure `useCropperReducer` and any composite
17
+ * store that delegates cropper actions (e.g. `useMediaEditorState`).
18
+ *
19
+ * `setImage` and `reset` are intentionally NOT included: each hook
20
+ * has additional bookkeeping for them (dirty-baseline, history clear)
21
+ * that doesn't fit a shared factory.
22
+ */
23
+ export interface CropperSetters {
24
+ setPan: ( pan: NormalizedPoint ) => void;
25
+ setZoom: ( zoom: number ) => void;
26
+ setZoomAtPoint: ( zoom: number, pan: NormalizedPoint ) => void;
27
+ setRotation: ( rotation: number ) => void;
28
+ setFlip: ( flip: Flip ) => void;
29
+ toggleFlip: ( direction: 'horizontal' | 'vertical' ) => void;
30
+ snapRotate90: ( direction: 1 | -1 ) => void;
31
+ setCropRect: ( rect: NormalizedRect ) => void;
32
+ settleCrop: () => void;
33
+ applyOperation: ( op: TransformOperation ) => void;
34
+ }
35
+
36
+ /**
37
+ * Build the named cropper setters from a dispatch function and a
38
+ * state getter. The two consuming hooks differ in how they dispatch:
39
+ *
40
+ * - `useCropperReducer` dispatches directly into its `useReducer`.
41
+ * - `useMediaEditorState` wraps cropper actions in a composite
42
+ * `CROPPER` envelope so the history machinery can record them.
43
+ *
44
+ * Both pass their own dispatch function and a getter that returns
45
+ * the current `CropperState`. The factory shape keeps the two hooks
46
+ * in lockstep — add a setter here once, both hooks get it.
47
+ *
48
+ * @param dispatchCropperAction Apply a `CropperAction` to the
49
+ * underlying reducer.
50
+ * @param getCropperState Return the current `CropperState`.
51
+ * Used by setters that need to read
52
+ * the latest state to compute their
53
+ * payload (focal-point zoom, flip toggle).
54
+ * @return The named cropper setters.
55
+ */
56
+ export function buildCropperSetters(
57
+ dispatchCropperAction: ( action: CropperAction ) => void,
58
+ getCropperState: () => CropperState
59
+ ): CropperSetters {
60
+ return {
61
+ setPan: ( pan ) =>
62
+ dispatchCropperAction( { type: 'SET_PAN', payload: pan } ),
63
+ setZoom: ( zoom ) => {
64
+ const action = buildFocalPointZoomAction( getCropperState(), zoom );
65
+ if ( action ) {
66
+ dispatchCropperAction( action );
67
+ }
68
+ },
69
+ setZoomAtPoint: ( zoom, pan ) =>
70
+ dispatchCropperAction( {
71
+ type: 'SET_ZOOM_AT_POINT',
72
+ payload: { zoom, pan },
73
+ } ),
74
+ setRotation: ( rotation ) =>
75
+ dispatchCropperAction( {
76
+ type: 'SET_ROTATION',
77
+ payload: rotation,
78
+ } ),
79
+ setFlip: ( flip ) =>
80
+ dispatchCropperAction( { type: 'SET_FLIP', payload: flip } ),
81
+ toggleFlip: ( direction ) => {
82
+ const { flip } = getCropperState();
83
+ dispatchCropperAction( {
84
+ type: 'SET_FLIP',
85
+ payload: { ...flip, [ direction ]: ! flip[ direction ] },
86
+ } );
87
+ },
88
+ snapRotate90: ( direction ) =>
89
+ dispatchCropperAction( {
90
+ type: 'SNAP_ROTATE_90',
91
+ payload: { direction },
92
+ } ),
93
+ setCropRect: ( rect ) =>
94
+ dispatchCropperAction( {
95
+ type: 'SET_CROP_RECT',
96
+ payload: rect,
97
+ } ),
98
+ settleCrop: () => dispatchCropperAction( { type: 'SETTLE_CROP' } ),
99
+ applyOperation: ( op ) =>
100
+ dispatchCropperAction( {
101
+ type: 'APPLY_OPERATION',
102
+ payload: op,
103
+ } ),
104
+ };
105
+ }
@@ -1,2 +1,4 @@
1
- export { useCropperState } from './use-cropper-state';
2
- export type { UseCropperStateReturn } from './use-cropper-state';
1
+ export { useCropperReducer } from './use-cropper-reducer';
2
+ export type { CropperController } from './use-cropper-reducer';
3
+ export { buildCropperSetters } from './build-cropper-setters';
4
+ export type { CropperSetters } from './build-cropper-setters';