@wordpress/media-editor 0.9.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,204 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import {
5
+ useReducer,
6
+ useCallback,
7
+ useMemo,
8
+ useRef,
9
+ useState,
10
+ } from '@wordpress/element';
11
+
12
+ /**
13
+ * Internal dependencies
14
+ */
15
+ import type {
16
+ CropperState,
17
+ CropperAction,
18
+ NormalizedRect,
19
+ Size,
20
+ } from '../../core/types';
21
+ import { DEFAULT_STATE } from '../../core/constants';
22
+ import { exportCroppedImage } from '../../core/export/canvas-renderer';
23
+ import {
24
+ cropperReducer,
25
+ enforceContainment,
26
+ isStateDirty,
27
+ } from '../../core/state';
28
+ import {
29
+ buildCropperSetters,
30
+ type CropperSetters,
31
+ } from './build-cropper-setters';
32
+
33
+ /**
34
+ * The cropper controller surface — the contract the Cropper component
35
+ * and its interaction hooks consume.
36
+ *
37
+ * History (undo / redo / commitHistory) is intentionally NOT part of
38
+ * this interface. The cropper is a pure state machine; history is a
39
+ * higher-layer concern owned by a composite store
40
+ * (see media-editor/src/state).
41
+ */
42
+ export interface CropperController extends CropperSetters {
43
+ /** The current cropper state (read-only). */
44
+ state: CropperState;
45
+ /** Set the loaded image (natural size and src). */
46
+ setImage: ( image: CropperState[ 'image' ] ) => void;
47
+ /** Reset the state. Optionally merge partial state overrides. */
48
+ reset: ( resetState?: Partial< CropperState > ) => void;
49
+ /** Whether the current state differs from the initial state. */
50
+ isDirty: boolean;
51
+ /**
52
+ * Export the cropped image as a Blob. Throws on failure — see
53
+ * `exportCroppedImage` for error semantics.
54
+ */
55
+ getCroppedImage: ( mimeType?: string, quality?: number ) => Promise< Blob >;
56
+ /**
57
+ * Report the current rendered image size to the controller. The
58
+ * Cropper component calls this on visualSize changes; composite
59
+ * controllers use it to compute aspect-ratio reshapes. The pure
60
+ * cropper hook ignores it (no-op).
61
+ */
62
+ setVisualSize: ( size: Size ) => void;
63
+ /**
64
+ * Adjust the cropRect in response to a viewport change (window
65
+ * resize, canvas resize). Behaves like `setCropRect` but, in
66
+ * composite controllers, does NOT record an undo entry — viewport
67
+ * reshapes aren't editor actions. The pure cropper hook aliases
68
+ * this to `setCropRect` (history is a composite concern).
69
+ */
70
+ adjustCropRectForViewport: ( rect: NormalizedRect ) => void;
71
+ }
72
+
73
+ function areCropperImagesEqual(
74
+ a: CropperState[ 'image' ],
75
+ b: CropperState[ 'image' ]
76
+ ): boolean {
77
+ return (
78
+ a?.src === b?.src &&
79
+ a?.naturalWidth === b?.naturalWidth &&
80
+ a?.naturalHeight === b?.naturalHeight
81
+ );
82
+ }
83
+
84
+ /**
85
+ * Pure reducer hook for the image cropper.
86
+ *
87
+ * Wraps the framework-agnostic `cropperReducer` and exposes named
88
+ * setters (built from the shared `buildCropperSetters` factory).
89
+ * Does NOT manage undo/redo history — that responsibility belongs to
90
+ * the surrounding application (see `useMediaEditorState` for the
91
+ * composite store used by the media editor).
92
+ *
93
+ * @param initialState Optional partial state to merge with DEFAULT_STATE.
94
+ * @return The cropper controller — state, setters, and helpers.
95
+ */
96
+ export function useCropperReducer(
97
+ initialState?: Partial< CropperState >
98
+ ): CropperController {
99
+ const [ state, dispatch ] = useReducer(
100
+ cropperReducer,
101
+ initialState,
102
+ ( init ) => enforceContainment( { ...DEFAULT_STATE, ...init } )
103
+ );
104
+
105
+ // The "clean" snapshot the current state is compared against for
106
+ // `isDirty`. Stored in state (not a ref) so the comparison reads
107
+ // a value, not a ref's `current`. `setImage` and `reset` refresh
108
+ // this when the consumer establishes a new baseline.
109
+ const [ initialBaseline, setInitialBaseline ] = useState< CropperState >(
110
+ () => enforceContainment( { ...DEFAULT_STATE, ...initialState } )
111
+ );
112
+ // Latest-state ref used by setters that need to read fresh state
113
+ // (focal-point zoom, toggleFlip, reset, setImage). Updated
114
+ // synchronously inside `dispatchAndSync` so multiple actions in
115
+ // one event see each other's output before React commits.
116
+ const stateRef = useRef( state );
117
+
118
+ const dispatchAndSync = useCallback( ( action: CropperAction ) => {
119
+ const next = cropperReducer( stateRef.current, action );
120
+ stateRef.current = next;
121
+ dispatch( action );
122
+ return next;
123
+ }, [] );
124
+
125
+ const setImage = useCallback(
126
+ ( image: CropperState[ 'image' ] ) => {
127
+ if ( areCropperImagesEqual( stateRef.current.image, image ) ) {
128
+ return;
129
+ }
130
+ const next = dispatchAndSync( {
131
+ type: 'SET_IMAGE',
132
+ payload: image,
133
+ } );
134
+ // Refresh the "clean" snapshot to match the post-load state
135
+ // produced by the reducer. Otherwise containment can nudge
136
+ // pan/zoom by tiny amounts on load and `isDirty` would
137
+ // report true from the start.
138
+ setInitialBaseline( next );
139
+ },
140
+ [ dispatchAndSync ]
141
+ );
142
+
143
+ const reset = useCallback(
144
+ ( resetState?: Partial< CropperState > ) => {
145
+ const nextInitialState = dispatchAndSync( {
146
+ type: 'RESET',
147
+ payload: resetState,
148
+ } );
149
+ // Mirror the reducer's RESET exactly so isDirty stays in
150
+ // sync. The containment step can tweak pan/zoom/cropRect by
151
+ // float ulp; fold those into the "initial" snapshot or
152
+ // isDirty would report true after a reset.
153
+ setInitialBaseline( nextInitialState );
154
+ },
155
+ [ dispatchAndSync ]
156
+ );
157
+
158
+ const setters = useMemo(
159
+ () => buildCropperSetters( dispatchAndSync, () => stateRef.current ),
160
+ [ dispatchAndSync ]
161
+ );
162
+
163
+ const isDirty = isStateDirty( state, initialBaseline );
164
+
165
+ const getCroppedImage = useCallback(
166
+ ( mimeType?: string, quality?: number ): Promise< Blob > => {
167
+ if ( ! state.image ) {
168
+ return Promise.reject(
169
+ new Error( 'No image loaded — call setImage first.' )
170
+ );
171
+ }
172
+ return exportCroppedImage(
173
+ state.image.src,
174
+ state,
175
+ mimeType,
176
+ quality
177
+ );
178
+ },
179
+ [ state ]
180
+ );
181
+
182
+ // No-op for the pure hook — visualSize matters only when a
183
+ // composite store needs to compute aspect-ratio reshapes
184
+ // (see useMediaEditorState). Typed to accept a Size so it
185
+ // satisfies the controller surface; the value is intentionally
186
+ // ignored.
187
+ const setVisualSize = useCallback( () => {}, [] );
188
+
189
+ // In the pure hook, history isn't a concern — viewport-driven
190
+ // reshape is identical to setCropRect.
191
+ const adjustCropRectForViewport = setters.setCropRect;
192
+
193
+ const controller: CropperController = {
194
+ ...setters,
195
+ state,
196
+ setImage,
197
+ reset,
198
+ isDirty,
199
+ getCroppedImage,
200
+ setVisualSize,
201
+ adjustCropRectForViewport,
202
+ };
203
+ return controller;
204
+ }
@@ -108,8 +108,9 @@ export function useInteraction(
108
108
  const [ isZooming, setIsZooming ] = useState( false );
109
109
  const [ isGestureActive, setIsGestureActive ] = useState( false );
110
110
  const [ isKeyboardPanning, setIsKeyboardPanning ] = useState( false );
111
- const keyboardInteractionTimerRef =
112
- useRef< ReturnType< typeof setTimeout > >();
111
+ const keyboardInteractionTimerRef = useRef<
112
+ ReturnType< typeof setTimeout > | undefined
113
+ >( undefined );
113
114
  // Tracks whether a keyboard gesture (pan or zoom) is currently active so
114
115
  // onGestureStart is only fired once per gesture, not on every key repeat.
115
116
  const isKeyboardGestureActiveRef = useRef( false );
@@ -29,14 +29,14 @@ import {
29
29
  * Internal dependencies
30
30
  */
31
31
  import { Cropper } from '../react/components/cropper';
32
- import { useCropperState } from '../react/hooks/use-cropper-state';
32
+ import { useCropperReducer } from '../react/hooks/use-cropper-reducer';
33
33
  import {
34
- MIN_ZOOM,
35
34
  MAX_ZOOM,
36
35
  MAX_ROTATION_OFFSET,
37
36
  DEFAULT_ASPECT_RATIOS,
38
37
  ORIGINAL_ASPECT_RATIO,
39
38
  } from '../core/constants';
39
+ import { getMinZoom, restrictPanZoom } from '../core/containment';
40
40
  import {
41
41
  loadImage,
42
42
  renderToCanvas,
@@ -50,7 +50,6 @@ import {
50
50
  getImageFit,
51
51
  getVisibleBounds,
52
52
  } from '../core/camera';
53
- import { restrictPanZoom } from '../core/containment';
54
53
  import { getSourceRegion } from '../core/source-region';
55
54
  import './style.css';
56
55
 
@@ -143,7 +142,7 @@ type Story = StoryObj< typeof Cropper >;
143
142
  * Default story. Basic cropper with a sample image, no controls.
144
143
  */
145
144
  const DefaultComponent = () => {
146
- const controller = useCropperState();
145
+ const controller = useCropperReducer();
147
146
 
148
147
  return (
149
148
  <div>
@@ -167,7 +166,7 @@ export const Default: Story = {
167
166
  * Full interactive demo with controls.
168
167
  */
169
168
  const WithControlsComponent = () => {
170
- const controller = useCropperState();
169
+ const controller = useCropperReducer();
171
170
  const {
172
171
  state,
173
172
  setRotation,
@@ -397,7 +396,6 @@ const WithControlsComponent = () => {
397
396
  <FlexItem>
398
397
  <SelectControl
399
398
  __next40pxDefaultSize
400
- __nextHasNoMarginBottom
401
399
  label="Aspect ratio"
402
400
  hideLabelFromVision
403
401
  value={ aspectRatioValue }
@@ -412,7 +410,6 @@ const WithControlsComponent = () => {
412
410
  </FlexItem>
413
411
  <FlexItem>
414
412
  <ToggleControl
415
- __nextHasNoMarginBottom
416
413
  label="Freeform"
417
414
  checked={ freeformCrop }
418
415
  onChange={ setFreeformCrop }
@@ -421,7 +418,6 @@ const WithControlsComponent = () => {
421
418
  <FlexItem>
422
419
  <SelectControl
423
420
  __next40pxDefaultSize
424
- __nextHasNoMarginBottom
425
421
  label="Grid"
426
422
  hideLabelFromVision
427
423
  value={ gridMode }
@@ -455,7 +451,6 @@ const WithControlsComponent = () => {
455
451
  <div className="image-editor-story__sliders">
456
452
  <RangeControl
457
453
  __next40pxDefaultSize
458
- __nextHasNoMarginBottom
459
454
  label="Fine rotation"
460
455
  min={ -MAX_ROTATION_OFFSET }
461
456
  max={ MAX_ROTATION_OFFSET }
@@ -465,9 +460,8 @@ const WithControlsComponent = () => {
465
460
  />
466
461
  <RangeControl
467
462
  __next40pxDefaultSize
468
- __nextHasNoMarginBottom
469
463
  label="Zoom"
470
- min={ MIN_ZOOM }
464
+ min={ getMinZoom( state ) }
471
465
  max={ MAX_ZOOM }
472
466
  step={ 0.1 }
473
467
  value={ state.zoom }
@@ -545,7 +539,7 @@ export const WithControls: Story = {
545
539
  * or verify the camera and render paths agree.
546
540
  */
547
541
  const DebugComponent = () => {
548
- const controller = useCropperState();
542
+ const controller = useCropperReducer();
549
543
  const {
550
544
  state,
551
545
  setRotation,
@@ -817,7 +811,6 @@ const DebugComponent = () => {
817
811
  </FlexItem>
818
812
  <FlexItem>
819
813
  <ToggleControl
820
- __nextHasNoMarginBottom
821
814
  label="Freeform"
822
815
  checked={ freeformCrop }
823
816
  onChange={ setFreeformCrop }
@@ -839,7 +832,6 @@ const DebugComponent = () => {
839
832
  <FlexItem>
840
833
  <SelectControl
841
834
  __next40pxDefaultSize
842
- __nextHasNoMarginBottom
843
835
  label="Format"
844
836
  hideLabelFromVision
845
837
  value={ exportFormat as 'image/jpeg' }
@@ -875,7 +867,6 @@ const DebugComponent = () => {
875
867
  <div className="image-editor-story__sliders">
876
868
  <RangeControl
877
869
  __next40pxDefaultSize
878
- __nextHasNoMarginBottom
879
870
  label="Fine rotation"
880
871
  min={ -MAX_ROTATION_OFFSET }
881
872
  max={ MAX_ROTATION_OFFSET }
@@ -885,9 +876,8 @@ const DebugComponent = () => {
885
876
  />
886
877
  <RangeControl
887
878
  __next40pxDefaultSize
888
- __nextHasNoMarginBottom
889
879
  label="Zoom"
890
- min={ MIN_ZOOM }
880
+ min={ getMinZoom( state ) }
891
881
  max={ MAX_ZOOM }
892
882
  step={ 0.1 }
893
883
  value={ state.zoom }
@@ -0,0 +1,152 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import {
5
+ cropperReducer,
6
+ areCropperStatesEqual,
7
+ computeInscribedRect,
8
+ type CropperState,
9
+ } from '../image-editor';
10
+ import {
11
+ DEFAULT_CROP_OPTIONS,
12
+ type CropOptionsSlice,
13
+ type MediaEditorAction,
14
+ type MediaEditorState,
15
+ } from './types';
16
+
17
+ /**
18
+ * Pure composite reducer for the media editor.
19
+ *
20
+ * Delegates `CROPPER`-tagged actions to the framework-agnostic
21
+ * `cropperReducer` and owns first-class transitions for the sidebar
22
+ * `cropOptions` slice. `RESTORE_SNAPSHOT` replaces the whole composite
23
+ * in one transaction (used by undo/redo).
24
+ *
25
+ * Note: this reducer carries NO history of its own. Snapshots and
26
+ * stacks live in the `useMediaEditorState` hook that wraps it.
27
+ *
28
+ * @param state Current composite state.
29
+ * @param action Action to apply.
30
+ * @return Next composite state.
31
+ */
32
+ export function mediaEditorReducer(
33
+ state: MediaEditorState,
34
+ action: MediaEditorAction
35
+ ): MediaEditorState {
36
+ switch ( action.type ) {
37
+ case 'CROPPER': {
38
+ const nextCropper = cropperReducer( state.cropper, action.action );
39
+ if ( nextCropper === state.cropper ) {
40
+ return state;
41
+ }
42
+ return { ...state, cropper: nextCropper };
43
+ }
44
+ case 'SET_ASPECT_RATIO_VALUE': {
45
+ const { presetKey, resolved, visualSize } = action.payload;
46
+ // Atomically: update the preset key AND reshape the
47
+ // cropRect (when the ratio is non-Free and we have a
48
+ // visualSize). One transaction = one undo entry.
49
+ const shouldReshape =
50
+ resolved !== undefined &&
51
+ resolved > 0 &&
52
+ visualSize.width > 0 &&
53
+ visualSize.height > 0;
54
+ const nextCropOptions: CropOptionsSlice = {
55
+ ...state.cropOptions,
56
+ aspectRatioValue: presetKey,
57
+ };
58
+ if ( ! shouldReshape ) {
59
+ if ( state.cropOptions.aspectRatioValue === presetKey ) {
60
+ return state;
61
+ }
62
+ return { ...state, cropOptions: nextCropOptions };
63
+ }
64
+ const newCropRect = computeInscribedRect( resolved, visualSize );
65
+ const nextCropper = cropperReducer( state.cropper, {
66
+ type: 'SET_CROP_RECT',
67
+ payload: newCropRect,
68
+ } );
69
+ return {
70
+ cropper: nextCropper,
71
+ cropOptions: nextCropOptions,
72
+ };
73
+ }
74
+ case 'SET_FREEFORM_CROP': {
75
+ if ( state.cropOptions.freeformCrop === action.payload ) {
76
+ return state;
77
+ }
78
+ return {
79
+ ...state,
80
+ cropOptions: {
81
+ ...state.cropOptions,
82
+ freeformCrop: action.payload,
83
+ },
84
+ };
85
+ }
86
+ case 'RESET_CROP_OPTIONS': {
87
+ if (
88
+ state.cropOptions.aspectRatioValue ===
89
+ DEFAULT_CROP_OPTIONS.aspectRatioValue &&
90
+ state.cropOptions.freeformCrop ===
91
+ DEFAULT_CROP_OPTIONS.freeformCrop
92
+ ) {
93
+ return state;
94
+ }
95
+ return {
96
+ ...state,
97
+ cropOptions: { ...DEFAULT_CROP_OPTIONS },
98
+ };
99
+ }
100
+ case 'RESTORE_SNAPSHOT':
101
+ return action.payload;
102
+ case 'VIEWPORT_ADJUST_CROP_RECT': {
103
+ const nextCropper = cropperReducer( state.cropper, {
104
+ type: 'SET_CROP_RECT',
105
+ payload: action.payload,
106
+ } );
107
+ if ( nextCropper === state.cropper ) {
108
+ return state;
109
+ }
110
+ return { ...state, cropper: nextCropper };
111
+ }
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Value-equality check for two composite states. Used by the history
117
+ * machinery to dedup snapshots when an action turned out to be a no-op.
118
+ *
119
+ * @param a First composite state.
120
+ * @param b Second composite state.
121
+ * @return Whether `a` and `b` are value-equal across all slices.
122
+ */
123
+ export function areMediaEditorStatesEqual(
124
+ a: MediaEditorState,
125
+ b: MediaEditorState
126
+ ): boolean {
127
+ if ( a === b ) {
128
+ return true;
129
+ }
130
+ return (
131
+ areCropperStatesEqual( a.cropper, b.cropper ) &&
132
+ a.cropOptions.aspectRatioValue === b.cropOptions.aspectRatioValue &&
133
+ a.cropOptions.freeformCrop === b.cropOptions.freeformCrop
134
+ );
135
+ }
136
+
137
+ /**
138
+ * Build the initial composite state from optional partial overrides.
139
+ *
140
+ * @param initialCropper Initial cropper-slice fields to merge.
141
+ * @param initialOptions Initial cropOptions-slice fields to merge.
142
+ * @return Composite initial state.
143
+ */
144
+ export function buildInitialMediaEditorState(
145
+ initialCropper: CropperState,
146
+ initialOptions?: Partial< CropOptionsSlice >
147
+ ): MediaEditorState {
148
+ return {
149
+ cropper: initialCropper,
150
+ cropOptions: { ...DEFAULT_CROP_OPTIONS, ...initialOptions },
151
+ };
152
+ }
@@ -0,0 +1,20 @@
1
+ export {
2
+ useMediaEditorState,
3
+ resolveAspectRatio,
4
+ } from './use-media-editor-state';
5
+ export type { MediaEditorController } from './use-media-editor-state';
6
+ export {
7
+ MediaEditorStateProvider,
8
+ useMediaEditor,
9
+ } from './media-editor-state-provider';
10
+ export {
11
+ mediaEditorReducer,
12
+ areMediaEditorStatesEqual,
13
+ buildInitialMediaEditorState,
14
+ } from './composite-reducer';
15
+ export { DEFAULT_CROP_OPTIONS } from './types';
16
+ export type {
17
+ CropOptionsSlice,
18
+ MediaEditorState,
19
+ MediaEditorAction,
20
+ } from './types';
@@ -0,0 +1,70 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { createContext, useContext } from '@wordpress/element';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import type { CropperState } from '../image-editor';
10
+ import {
11
+ useMediaEditorState,
12
+ type MediaEditorController,
13
+ } from './use-media-editor-state';
14
+ import type { CropOptionsSlice } from './types';
15
+
16
+ const MediaEditorStateContext = createContext< MediaEditorController | null >(
17
+ null
18
+ );
19
+
20
+ interface MediaEditorStateProviderProps {
21
+ /** Optional cropper-slice initial state. */
22
+ initialCropperState?: Partial< CropperState >;
23
+ /** Optional cropOptions-slice initial state. */
24
+ initialCropOptions?: Partial< CropOptionsSlice >;
25
+ /** Child components. */
26
+ children: React.ReactNode;
27
+ }
28
+
29
+ /**
30
+ * Provider that vends the composite media-editor controller via
31
+ * context. Wrap the media editor in this so every consumer
32
+ * (canvas, toolbar, sidebar) reads from the same store.
33
+ *
34
+ * @param props
35
+ * @param props.initialCropperState
36
+ * @param props.initialCropOptions
37
+ * @param props.children
38
+ */
39
+ export function MediaEditorStateProvider( {
40
+ initialCropperState,
41
+ initialCropOptions,
42
+ children,
43
+ }: MediaEditorStateProviderProps ) {
44
+ const controller = useMediaEditorState( {
45
+ cropper: initialCropperState,
46
+ cropOptions: initialCropOptions,
47
+ } );
48
+
49
+ return (
50
+ <MediaEditorStateContext.Provider value={ controller }>
51
+ { children }
52
+ </MediaEditorStateContext.Provider>
53
+ );
54
+ }
55
+
56
+ /**
57
+ * Consume the composite media-editor controller. Throws if used
58
+ * outside a `<MediaEditorStateProvider>`.
59
+ *
60
+ * @return The composite controller.
61
+ */
62
+ export function useMediaEditor(): MediaEditorController {
63
+ const context = useContext( MediaEditorStateContext );
64
+ if ( ! context ) {
65
+ throw new Error(
66
+ 'useMediaEditor must be used within a MediaEditorStateProvider.'
67
+ );
68
+ }
69
+ return context;
70
+ }