@wordpress/media-editor 0.9.0 → 0.10.1

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 (322) hide show
  1. package/build/components/media-editor/index.cjs +212 -170
  2. package/build/components/media-editor/index.cjs.map +3 -3
  3. package/build/components/media-editor/use-crop-options.cjs +63 -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 +33 -12
  8. package/build/components/media-editor-canvas/index.cjs.map +2 -2
  9. package/build/components/media-editor-crop-panel/index.cjs +70 -84
  10. package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
  11. package/build/components/media-editor-fine-rotation/index.cjs +81 -0
  12. package/build/components/media-editor-fine-rotation/index.cjs.map +7 -0
  13. package/build/components/media-editor-modal/index.cjs +63 -16
  14. package/build/components/media-editor-modal/index.cjs.map +3 -3
  15. package/build/components/media-editor-transform-controls/index.cjs +84 -0
  16. package/build/components/media-editor-transform-controls/index.cjs.map +7 -0
  17. package/build/components/media-form/index.cjs +19 -1
  18. package/build/components/media-form/index.cjs.map +3 -3
  19. package/build/components/media-form/sidebar-datetime-view.cjs +53 -0
  20. package/build/components/media-form/sidebar-datetime-view.cjs.map +7 -0
  21. package/build/components/rotation-ruler/index.cjs +8 -2
  22. package/build/components/rotation-ruler/index.cjs.map +2 -2
  23. package/build/hooks/use-crop-gesture-handlers.cjs +41 -4
  24. package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
  25. package/build/image-editor/core/camera.cjs +29 -11
  26. package/build/image-editor/core/camera.cjs.map +2 -2
  27. package/build/image-editor/core/constants.cjs +6 -0
  28. package/build/image-editor/core/constants.cjs.map +2 -2
  29. package/build/image-editor/core/containment.cjs +71 -34
  30. package/build/image-editor/core/containment.cjs.map +2 -2
  31. package/build/image-editor/core/crop-rect.cjs +48 -0
  32. package/build/image-editor/core/crop-rect.cjs.map +7 -0
  33. package/build/image-editor/core/index.cjs +12 -0
  34. package/build/image-editor/core/index.cjs.map +2 -2
  35. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  36. package/build/image-editor/core/math/sanitize.cjs +92 -0
  37. package/build/image-editor/core/math/sanitize.cjs.map +7 -0
  38. package/build/image-editor/core/setter-helpers.cjs +64 -0
  39. package/build/image-editor/core/setter-helpers.cjs.map +7 -0
  40. package/build/image-editor/core/source-region.cjs +16 -14
  41. package/build/image-editor/core/source-region.cjs.map +2 -2
  42. package/build/image-editor/core/state.cjs +20 -10
  43. package/build/image-editor/core/state.cjs.map +2 -2
  44. package/build/image-editor/core/stencil-math.cjs +31 -20
  45. package/build/image-editor/core/stencil-math.cjs.map +2 -2
  46. package/build/image-editor/core/transform-style.cjs +12 -6
  47. package/build/image-editor/core/transform-style.cjs.map +2 -2
  48. package/build/image-editor/core/types.cjs.map +1 -1
  49. package/build/image-editor/index.cjs +12 -2
  50. package/build/image-editor/index.cjs.map +2 -2
  51. package/build/image-editor/react/components/cropper-provider.cjs +2 -2
  52. package/build/image-editor/react/components/cropper-provider.cjs.map +2 -2
  53. package/build/image-editor/react/components/cropper.cjs +120 -47
  54. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  55. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +172 -0
  56. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +7 -0
  57. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +19 -9
  58. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  59. package/build/image-editor/react/hooks/build-cropper-setters.cjs +71 -0
  60. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +7 -0
  61. package/build/image-editor/react/hooks/index.cjs +6 -3
  62. package/build/image-editor/react/hooks/index.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/state/composite-reducer.cjs +103 -0
  66. package/build/state/composite-reducer.cjs.map +7 -0
  67. package/build/state/index.cjs +48 -0
  68. package/build/state/index.cjs.map +7 -0
  69. package/build/state/media-editor-state-provider.cjs +58 -0
  70. package/build/state/media-editor-state-provider.cjs.map +7 -0
  71. package/build/state/types.cjs +33 -0
  72. package/build/state/types.cjs.map +7 -0
  73. package/build/state/use-media-editor-state.cjs +287 -0
  74. package/build/state/use-media-editor-state.cjs.map +7 -0
  75. package/build/store/actions.cjs +4 -2
  76. package/build/store/actions.cjs.map +2 -2
  77. package/build/store/reducer.cjs +5 -3
  78. package/build/store/reducer.cjs.map +2 -2
  79. package/build/store/selectors.cjs +5 -0
  80. package/build/store/selectors.cjs.map +2 -2
  81. package/build-module/components/media-editor/index.mjs +224 -177
  82. package/build-module/components/media-editor/index.mjs.map +2 -2
  83. package/build-module/components/media-editor/use-crop-options.mjs +37 -0
  84. package/build-module/components/media-editor/use-crop-options.mjs.map +7 -0
  85. package/build-module/components/media-editor/use-save-media-editor.mjs +151 -0
  86. package/build-module/components/media-editor/use-save-media-editor.mjs.map +7 -0
  87. package/build-module/components/media-editor-canvas/index.mjs +34 -13
  88. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  89. package/build-module/components/media-editor-crop-panel/index.mjs +73 -89
  90. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  91. package/build-module/components/media-editor-fine-rotation/index.mjs +50 -0
  92. package/build-module/components/media-editor-fine-rotation/index.mjs.map +7 -0
  93. package/build-module/components/media-editor-modal/index.mjs +64 -17
  94. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  95. package/build-module/components/media-editor-transform-controls/index.mjs +68 -0
  96. package/build-module/components/media-editor-transform-controls/index.mjs.map +7 -0
  97. package/build-module/components/media-form/index.mjs +9 -1
  98. package/build-module/components/media-form/index.mjs.map +2 -2
  99. package/build-module/components/media-form/sidebar-datetime-view.mjs +32 -0
  100. package/build-module/components/media-form/sidebar-datetime-view.mjs.map +7 -0
  101. package/build-module/components/rotation-ruler/index.mjs +8 -2
  102. package/build-module/components/rotation-ruler/index.mjs.map +2 -2
  103. package/build-module/hooks/use-crop-gesture-handlers.mjs +41 -4
  104. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
  105. package/build-module/image-editor/core/camera.mjs +34 -11
  106. package/build-module/image-editor/core/camera.mjs.map +2 -2
  107. package/build-module/image-editor/core/constants.mjs +4 -0
  108. package/build-module/image-editor/core/constants.mjs.map +2 -2
  109. package/build-module/image-editor/core/containment.mjs +75 -34
  110. package/build-module/image-editor/core/containment.mjs.map +2 -2
  111. package/build-module/image-editor/core/crop-rect.mjs +23 -0
  112. package/build-module/image-editor/core/crop-rect.mjs.map +7 -0
  113. package/build-module/image-editor/core/index.mjs +12 -0
  114. package/build-module/image-editor/core/index.mjs.map +2 -2
  115. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  116. package/build-module/image-editor/core/math/sanitize.mjs +63 -0
  117. package/build-module/image-editor/core/math/sanitize.mjs.map +7 -0
  118. package/build-module/image-editor/core/setter-helpers.mjs +39 -0
  119. package/build-module/image-editor/core/setter-helpers.mjs.map +7 -0
  120. package/build-module/image-editor/core/source-region.mjs +16 -14
  121. package/build-module/image-editor/core/source-region.mjs.map +2 -2
  122. package/build-module/image-editor/core/state.mjs +25 -11
  123. package/build-module/image-editor/core/state.mjs.map +2 -2
  124. package/build-module/image-editor/core/stencil-math.mjs +30 -19
  125. package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
  126. package/build-module/image-editor/core/transform-style.mjs +12 -6
  127. package/build-module/image-editor/core/transform-style.mjs.map +2 -2
  128. package/build-module/image-editor/index.mjs +11 -2
  129. package/build-module/image-editor/index.mjs.map +2 -2
  130. package/build-module/image-editor/react/components/cropper-provider.mjs +3 -3
  131. package/build-module/image-editor/react/components/cropper-provider.mjs.map +2 -2
  132. package/build-module/image-editor/react/components/cropper.mjs +121 -48
  133. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  134. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +147 -0
  135. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +7 -0
  136. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +19 -9
  137. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  138. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +46 -0
  139. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +7 -0
  140. package/build-module/image-editor/react/hooks/index.mjs +4 -2
  141. package/build-module/image-editor/react/hooks/index.mjs.map +2 -2
  142. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +100 -0
  143. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +7 -0
  144. package/build-module/state/composite-reducer.mjs +82 -0
  145. package/build-module/state/composite-reducer.mjs.map +7 -0
  146. package/build-module/state/index.mjs +26 -0
  147. package/build-module/state/index.mjs.map +7 -0
  148. package/build-module/state/media-editor-state-provider.mjs +34 -0
  149. package/build-module/state/media-editor-state-provider.mjs.map +7 -0
  150. package/build-module/state/types.mjs +8 -0
  151. package/build-module/state/types.mjs.map +7 -0
  152. package/build-module/state/use-media-editor-state.mjs +278 -0
  153. package/build-module/state/use-media-editor-state.mjs.map +7 -0
  154. package/build-module/store/actions.mjs +4 -2
  155. package/build-module/store/actions.mjs.map +2 -2
  156. package/build-module/store/reducer.mjs +5 -3
  157. package/build-module/store/reducer.mjs.map +2 -2
  158. package/build-module/store/selectors.mjs +4 -0
  159. package/build-module/store/selectors.mjs.map +2 -2
  160. package/build-style/style-rtl.css +97 -44
  161. package/build-style/style.css +97 -44
  162. package/build-types/components/media-editor/index.d.ts +14 -5
  163. package/build-types/components/media-editor/index.d.ts.map +1 -1
  164. package/build-types/components/media-editor/use-crop-options.d.ts +35 -0
  165. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -0
  166. package/build-types/components/media-editor/use-save-media-editor.d.ts +29 -0
  167. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -0
  168. package/build-types/components/media-editor-canvas/index.d.ts +5 -10
  169. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  170. package/build-types/components/media-editor-crop-panel/index.d.ts +6 -25
  171. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  172. package/build-types/components/media-editor-fine-rotation/index.d.ts +14 -0
  173. package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -0
  174. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  175. package/build-types/components/media-editor-transform-controls/index.d.ts +8 -0
  176. package/build-types/components/media-editor-transform-controls/index.d.ts.map +1 -0
  177. package/build-types/components/media-form/index.d.ts.map +1 -1
  178. package/build-types/components/media-form/sidebar-datetime-view.d.ts +10 -0
  179. package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +1 -0
  180. package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
  181. package/build-types/hooks/use-crop-gesture-handlers.d.ts +13 -14
  182. package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
  183. package/build-types/image-editor/core/camera.d.ts.map +1 -1
  184. package/build-types/image-editor/core/constants.d.ts +18 -0
  185. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  186. package/build-types/image-editor/core/containment.d.ts +10 -0
  187. package/build-types/image-editor/core/containment.d.ts.map +1 -1
  188. package/build-types/image-editor/core/crop-rect.d.ts +19 -0
  189. package/build-types/image-editor/core/crop-rect.d.ts.map +1 -0
  190. package/build-types/image-editor/core/index.d.ts +3 -1
  191. package/build-types/image-editor/core/index.d.ts.map +1 -1
  192. package/build-types/image-editor/core/interaction-controller.d.ts +5 -1
  193. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  194. package/build-types/image-editor/core/math/sanitize.d.ts +70 -0
  195. package/build-types/image-editor/core/math/sanitize.d.ts.map +1 -0
  196. package/build-types/image-editor/core/setter-helpers.d.ts +24 -0
  197. package/build-types/image-editor/core/setter-helpers.d.ts.map +1 -0
  198. package/build-types/image-editor/core/source-region.d.ts.map +1 -1
  199. package/build-types/image-editor/core/state.d.ts +12 -0
  200. package/build-types/image-editor/core/state.d.ts.map +1 -1
  201. package/build-types/image-editor/core/stencil-math.d.ts +23 -18
  202. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
  203. package/build-types/image-editor/core/transform-style.d.ts.map +1 -1
  204. package/build-types/image-editor/core/types.d.ts +23 -4
  205. package/build-types/image-editor/core/types.d.ts.map +1 -1
  206. package/build-types/image-editor/index.d.ts +4 -4
  207. package/build-types/image-editor/index.d.ts.map +1 -1
  208. package/build-types/image-editor/react/components/cropper-provider.d.ts +11 -7
  209. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
  210. package/build-types/image-editor/react/components/cropper.d.ts +6 -4
  211. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  212. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +36 -0
  213. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +1 -0
  214. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +2 -1
  215. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  216. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +47 -0
  217. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +1 -0
  218. package/build-types/image-editor/react/hooks/index.d.ts +4 -2
  219. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
  220. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts +58 -0
  221. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +1 -0
  222. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
  223. package/build-types/state/composite-reducer.d.ts +39 -0
  224. package/build-types/state/composite-reducer.d.ts.map +1 -0
  225. package/build-types/state/index.d.ts +7 -0
  226. package/build-types/state/index.d.ts.map +1 -0
  227. package/build-types/state/media-editor-state-provider.d.ts +34 -0
  228. package/build-types/state/media-editor-state-provider.d.ts.map +1 -0
  229. package/build-types/state/types.d.ts +96 -0
  230. package/build-types/state/types.d.ts.map +1 -0
  231. package/build-types/state/use-media-editor-state.d.ts +101 -0
  232. package/build-types/state/use-media-editor-state.d.ts.map +1 -0
  233. package/build-types/store/actions.d.ts +3 -1
  234. package/build-types/store/actions.d.ts.map +1 -1
  235. package/build-types/store/reducer.d.ts +3 -0
  236. package/build-types/store/reducer.d.ts.map +1 -1
  237. package/build-types/store/selectors.d.ts +1 -0
  238. package/build-types/store/selectors.d.ts.map +1 -1
  239. package/package.json +19 -16
  240. package/src/components/media-editor/index.tsx +262 -229
  241. package/src/components/media-editor/style.scss +0 -9
  242. package/src/components/media-editor/test/use-crop-options.tsx +110 -0
  243. package/src/components/media-editor/use-crop-options.ts +79 -0
  244. package/src/components/media-editor/use-save-media-editor.ts +214 -0
  245. package/src/components/media-editor-canvas/index.tsx +57 -25
  246. package/src/components/media-editor-crop-panel/index.tsx +53 -87
  247. package/src/components/media-editor-crop-panel/test/index.tsx +46 -53
  248. package/src/components/media-editor-fine-rotation/index.tsx +74 -0
  249. package/src/components/media-editor-modal/index.tsx +70 -16
  250. package/src/components/media-editor-modal/style.scss +80 -0
  251. package/src/components/media-editor-modal/test/build-modifiers.test.ts +14 -0
  252. package/src/components/media-editor-modal/test/index.tsx +146 -0
  253. package/src/components/media-editor-transform-controls/index.tsx +70 -0
  254. package/src/components/media-editor-transform-controls/style.scss +8 -0
  255. package/src/components/media-form/index.tsx +15 -1
  256. package/src/components/media-form/sidebar-datetime-view.tsx +47 -0
  257. package/src/components/media-form/test/sidebar-datetime-view.test.tsx +96 -0
  258. package/src/components/rotation-ruler/README.md +11 -25
  259. package/src/components/rotation-ruler/index.tsx +17 -2
  260. package/src/components/rotation-ruler/style.scss +49 -13
  261. package/src/hooks/use-crop-gesture-handlers.ts +64 -17
  262. package/src/image-editor/README.md +28 -129
  263. package/src/image-editor/core/camera.ts +51 -26
  264. package/src/image-editor/core/constants.ts +19 -0
  265. package/src/image-editor/core/containment.ts +102 -49
  266. package/src/image-editor/core/crop-rect.ts +45 -0
  267. package/src/image-editor/core/index.ts +16 -4
  268. package/src/image-editor/core/interaction-controller.ts +5 -1
  269. package/src/image-editor/core/math/sanitize.ts +158 -0
  270. package/src/image-editor/core/math/test/sanitize.ts +244 -0
  271. package/src/image-editor/core/setter-helpers.ts +66 -0
  272. package/src/image-editor/core/source-region.ts +21 -14
  273. package/src/image-editor/core/state.ts +67 -15
  274. package/src/image-editor/core/stencil-math.ts +55 -35
  275. package/src/image-editor/core/test/camera.ts +367 -1
  276. package/src/image-editor/core/test/state.ts +101 -8
  277. package/src/image-editor/core/test/stencil-math.ts +193 -0
  278. package/src/image-editor/core/transform-style.ts +21 -6
  279. package/src/image-editor/core/types.ts +24 -4
  280. package/src/image-editor/docs/architecture.md +48 -89
  281. package/src/image-editor/docs/recipes.md +123 -659
  282. package/src/image-editor/index.ts +12 -3
  283. package/src/image-editor/react/components/cropper-provider.tsx +16 -12
  284. package/src/image-editor/react/components/cropper.scss +23 -4
  285. package/src/image-editor/react/components/cropper.tsx +194 -89
  286. package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +269 -0
  287. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +19 -8
  288. package/src/image-editor/react/components/test/cropper.tsx +55 -13
  289. package/src/image-editor/react/hooks/build-cropper-setters.ts +105 -0
  290. package/src/image-editor/react/hooks/index.ts +4 -2
  291. package/src/image-editor/react/hooks/test/{use-cropper-state.ts → use-cropper-reducer.ts} +142 -286
  292. package/src/image-editor/react/hooks/use-cropper-reducer.ts +204 -0
  293. package/src/image-editor/stories/rectangle-crop.story.tsx +7 -17
  294. package/src/state/composite-reducer.ts +137 -0
  295. package/src/state/index.ts +20 -0
  296. package/src/state/media-editor-state-provider.tsx +70 -0
  297. package/src/state/test/use-media-editor-state.ts +330 -0
  298. package/src/state/types.ts +101 -0
  299. package/src/state/use-media-editor-state.ts +472 -0
  300. package/src/store/actions.ts +3 -0
  301. package/src/store/reducer.ts +6 -1
  302. package/src/store/selectors.ts +4 -0
  303. package/src/store/test/reducer.ts +9 -0
  304. package/src/style.scss +1 -1
  305. package/tsconfig.json +3 -0
  306. package/tsconfig.tsbuildinfo +1 -1
  307. package/build/components/media-editor-toolbar/index.cjs +0 -214
  308. package/build/components/media-editor-toolbar/index.cjs.map +0 -7
  309. package/build/image-editor/react/hooks/use-cropper-state.cjs +0 -277
  310. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +0 -7
  311. package/build-module/components/media-editor-toolbar/index.mjs +0 -190
  312. package/build-module/components/media-editor-toolbar/index.mjs.map +0 -7
  313. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +0 -262
  314. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +0 -7
  315. package/build-types/components/media-editor-toolbar/index.d.ts +0 -24
  316. package/build-types/components/media-editor-toolbar/index.d.ts.map +0 -1
  317. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -97
  318. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
  319. package/src/components/media-editor-toolbar/index.tsx +0 -209
  320. package/src/components/media-editor-toolbar/style.scss +0 -49
  321. package/src/image-editor/docs/roadmap.md +0 -66
  322. 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;
@@ -288,7 +290,7 @@ export function RectangleStencil( {
288
290
  el.addEventListener( 'pointerup', onEnd );
289
291
  el.addEventListener( 'lostpointercapture', onEnd );
290
292
 
291
- onResizeStart?.();
293
+ onResizeStart?.( handle );
292
294
  // Cancel any pending keyboard settle so it can't fire onResizeEnd
293
295
  // mid-drag if the user switches from keyboard to pointer within
294
296
  // the settle window.
@@ -308,8 +310,15 @@ export function RectangleStencil( {
308
310
  clientX: number,
309
311
  clientY: number
310
312
  ): NormalizedRect =>
311
- computeFreeResizeRect( drag, clientX, clientY, imageSize, bounds ),
312
- [ imageSize, bounds ]
313
+ computeFreeResizeRect(
314
+ drag,
315
+ clientX,
316
+ clientY,
317
+ imageSize,
318
+ bounds,
319
+ minCropSize
320
+ ),
321
+ [ imageSize, bounds, minCropSize ]
313
322
  );
314
323
 
315
324
  /**
@@ -328,9 +337,10 @@ export function RectangleStencil( {
328
337
  clientY,
329
338
  imageSize,
330
339
  bounds,
331
- normalizedRatio
340
+ normalizedRatio,
341
+ minCropSize
332
342
  ),
333
- [ imageSize, bounds, normalizedRatio ]
343
+ [ imageSize, bounds, normalizedRatio, minCropSize ]
334
344
  );
335
345
 
336
346
  /**
@@ -348,9 +358,10 @@ export function RectangleStencil( {
348
358
  clientX,
349
359
  clientY,
350
360
  imageSize,
351
- bounds
361
+ bounds,
362
+ minCropSize
352
363
  ),
353
- [ imageSize, bounds ]
364
+ [ imageSize, bounds, minCropSize ]
354
365
  );
355
366
 
356
367
  latestHandlersRef.current = {
@@ -392,7 +403,7 @@ export function RectangleStencil( {
392
403
 
393
404
  if ( ! keyboardResizeActiveRef.current ) {
394
405
  keyboardResizeActiveRef.current = true;
395
- onResizeStart?.();
406
+ onResizeStart?.( handle );
396
407
  }
397
408
 
398
409
  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';