@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
@@ -1,457 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import {
5
- useReducer,
6
- useCallback,
7
- useRef,
8
- useState,
9
- useEffect,
10
- } from '@wordpress/element';
11
-
12
- /**
13
- * Internal dependencies
14
- */
15
- import type {
16
- CropperState,
17
- TransformOperation,
18
- NormalizedPoint,
19
- NormalizedRect,
20
- Flip,
21
- } from '../../core/types';
22
- import { DEFAULT_STATE } from '../../core/constants';
23
- import { exportCroppedImage } from '../../core/export/canvas-renderer';
24
- import {
25
- cropperReducer,
26
- enforceContainment,
27
- isStateDirty,
28
- } from '../../core/state';
29
-
30
- /**
31
- * The return type of the useCropperState hook.
32
- *
33
- * The hook exposes its state through named setter methods rather
34
- * than a raw dispatch function. Each setter has a specific
35
- * behavioral contract (see individual JSDoc) that maps 1:1 to a
36
- * reducer action internally. Keeping the reducer actions private
37
- * lets the implementation evolve without breaking consumers.
38
- */
39
- export interface UseCropperStateReturn {
40
- /** The current cropper state (read-only). */
41
- state: CropperState;
42
- /** Set the loaded image (natural size and src). */
43
- setImage: ( image: CropperState[ 'image' ] ) => void;
44
- /**
45
- * Set the image pan offset in normalized coordinates. Use
46
- * `setCropRect` for the crop rectangle.
47
- */
48
- setPan: ( pan: NormalizedPoint ) => void;
49
- /** Set the zoom level. Clamped to [1, 10]. */
50
- setZoom: ( zoom: number ) => void;
51
- /** Set zoom and pan together so focal-point zoom remains atomic. */
52
- setZoomAtPoint: ( zoom: number, pan: NormalizedPoint ) => void;
53
- /** Set the rotation in degrees. Normalized to [0, 360). */
54
- setRotation: ( rotation: number ) => void;
55
- /** Set the flip state. */
56
- setFlip: ( flip: Flip ) => void;
57
- /** Toggle flip on the given axis. */
58
- toggleFlip: ( direction: 'horizontal' | 'vertical' ) => void;
59
- /** Snap rotate 90° preserving the image selection (Google Photos style). */
60
- snapRotate90: ( direction: 1 | -1 ) => void;
61
- /** Set the crop rectangle in normalized coordinates. */
62
- setCropRect: ( rect: NormalizedRect ) => void;
63
- /**
64
- * Settle the crop rect after a resize drag: expand to fill the
65
- * available visual area while preserving the framed image region.
66
- * Typically called from a stencil's `onResizeEnd` callback.
67
- *
68
- * Flushes the pending resize history before dispatching, then suppresses
69
- * the settle dispatch itself so resizing and settling undo as one step.
70
- */
71
- settleCrop: () => void;
72
- /** Apply a transform operation through the pipeline. */
73
- applyOperation: ( op: TransformOperation ) => void;
74
- /** Reset the state. Optionally merge partial state overrides. */
75
- reset: ( resetState?: Partial< CropperState > ) => void;
76
- /** Whether the current state differs from the initial state. */
77
- isDirty: boolean;
78
- /** Whether there is a cropper state to undo. */
79
- hasUndo: boolean;
80
- /** Whether there is a cropper state to redo. */
81
- hasRedo: boolean;
82
- /** Undo the last committed cropper operation. */
83
- undo: () => void;
84
- /** Redo the last undone cropper operation. */
85
- redo: () => void;
86
- /**
87
- * Flush the pending history entry immediately, bypassing the debounce
88
- * timer. Useful in two situations:
89
- *
90
- * - **Pointer-up / key-up**: commit as soon as the user releases so the
91
- * undo button lights up without waiting for the debounce window.
92
- * - **Gesture start**: pass to `onGestureStart` on the `Cropper` so any
93
- * pending sidebar interaction is committed before a new canvas gesture
94
- * begins, keeping the two interactions as separate undo steps.
95
- */
96
- commitHistory: () => void;
97
- /**
98
- * Export the cropped image as a Blob. Throws on failure — see
99
- * `exportCroppedImage` in core for the error semantics (image
100
- * load errors, CORS taint, missing canvas context). Wrap in
101
- * try/catch if you need to recover.
102
- */
103
- getCroppedImage: ( mimeType?: string, quality?: number ) => Promise< Blob >;
104
- }
105
-
106
- /** Milliseconds of inactivity after which a continuous interaction is committed to history. */
107
- const HISTORY_DEBOUNCE_MS = 300;
108
-
109
- /** Small tolerance for cropper floating-point comparisons. */
110
- const HISTORY_EPSILON = 1e-6;
111
-
112
- function nearlyEqual( a: number, b: number ): boolean {
113
- return Math.abs( a - b ) < HISTORY_EPSILON;
114
- }
115
-
116
- function areHistoryStatesEqual( a: CropperState, b: CropperState ): boolean {
117
- const aImage = a.image;
118
- const bImage = b.image;
119
- return (
120
- aImage?.src === bImage?.src &&
121
- aImage?.naturalWidth === bImage?.naturalWidth &&
122
- aImage?.naturalHeight === bImage?.naturalHeight &&
123
- nearlyEqual( a.pan.x, b.pan.x ) &&
124
- nearlyEqual( a.pan.y, b.pan.y ) &&
125
- nearlyEqual( a.zoom, b.zoom ) &&
126
- nearlyEqual( a.rotation, b.rotation ) &&
127
- a.flip.horizontal === b.flip.horizontal &&
128
- a.flip.vertical === b.flip.vertical &&
129
- nearlyEqual( a.cropRect.x, b.cropRect.x ) &&
130
- nearlyEqual( a.cropRect.y, b.cropRect.y ) &&
131
- nearlyEqual( a.cropRect.width, b.cropRect.width ) &&
132
- nearlyEqual( a.cropRect.height, b.cropRect.height )
133
- );
134
- }
135
-
136
- /**
137
- * Reducer-based state management hook for the image editor.
138
- *
139
- * Provides the full cropper state and named setter methods for
140
- * every supported transition. The reducer and containment logic
141
- * live in core/state.ts (framework-agnostic); this hook is a thin
142
- * React wrapper around that pure reducer, with memoized callbacks.
143
- *
144
- * History is recorded via a state-change debounce: any state change
145
- * starts a 300 ms timer; when it fires the pre-change state is pushed
146
- * onto the undo stack. `commitHistory` flushes the timer immediately
147
- * (useful on pointer-up / key-up for a crisp commit feel).
148
- *
149
- * @param initialState Optional partial state to merge with DEFAULT_STATE.
150
- * @return The cropper state, setters, and utilities.
151
- */
152
- export function useCropperState(
153
- initialState?: Partial< CropperState >
154
- ): UseCropperStateReturn {
155
- const [ state, dispatch ] = useReducer(
156
- cropperReducer,
157
- initialState,
158
- ( init ) => enforceContainment( { ...DEFAULT_STATE, ...init } )
159
- );
160
-
161
- const initialRef = useRef< CropperState >(
162
- enforceContainment( { ...DEFAULT_STATE, ...initialState } )
163
- );
164
- // Keep a ref to the latest state so callbacks with stable identity
165
- // (reset, setImage) can read fresh state without re-creating themselves.
166
- const stateRef = useRef( state );
167
- stateRef.current = state;
168
-
169
- // History stack for undo/redo. Using refs for the arrays avoids
170
- // unnecessary re-renders on every push; a pair of boolean state values
171
- // drives the enabled/disabled state of the undo/redo buttons.
172
- const historyRef = useRef< CropperState[] >( [] );
173
- const redoStackRef = useRef< CropperState[] >( [] );
174
- const [ hasUndo, setHasUndo ] = useState( false );
175
- const [ hasRedo, setHasRedo ] = useState( false );
176
-
177
- // Debounce-based history: tracks the last committed state and a pending
178
- // timer. Any state change resets the timer; when it expires the
179
- // pre-change state is pushed to history.
180
- const lastCommittedStateRef = useRef< CropperState | null >( state );
181
- const debounceTimerRef = useRef< ReturnType< typeof setTimeout > >();
182
- // Set to true before dispatching actions that must not produce a debounce
183
- // history entry (undo, redo, reset, setImage, discrete actions).
184
- const suppressDebounceRef = useRef( false );
185
-
186
- // Pushes `entry` (defaults to current state) onto the undo stack and
187
- // clears the redo stack. Skips if `entry` is already the last item,
188
- // so rapid discrete actions on unchanged state don't create duplicates.
189
- const pushToHistory = useCallback( ( entry?: CropperState ) => {
190
- const target = entry ?? stateRef.current;
191
- const previousEntry =
192
- historyRef.current[ historyRef.current.length - 1 ];
193
- if ( previousEntry && areHistoryStatesEqual( previousEntry, target ) ) {
194
- return;
195
- }
196
- historyRef.current = [ ...historyRef.current, target ];
197
- redoStackRef.current = [];
198
- setHasUndo( true );
199
- setHasRedo( false );
200
- }, [] );
201
-
202
- // Watch state and debounce history commits. Any dispatch resets the
203
- // timer; once the state has settled for HISTORY_DEBOUNCE_MS the
204
- // pre-change snapshot is pushed to the undo stack.
205
- useEffect( () => {
206
- // Suppress flag: undo/redo/reset/setImage/discrete actions set this
207
- // to opt out of the debounce for their own state changes.
208
- if ( suppressDebounceRef.current ) {
209
- suppressDebounceRef.current = false;
210
- lastCommittedStateRef.current = stateRef.current;
211
- return;
212
- }
213
- if (
214
- lastCommittedStateRef.current !== null &&
215
- areHistoryStatesEqual(
216
- lastCommittedStateRef.current,
217
- stateRef.current
218
- )
219
- ) {
220
- lastCommittedStateRef.current = stateRef.current;
221
- return;
222
- }
223
- clearTimeout( debounceTimerRef.current );
224
- debounceTimerRef.current = setTimeout( () => {
225
- const snapshot = lastCommittedStateRef.current;
226
- if (
227
- snapshot !== null &&
228
- ! areHistoryStatesEqual( snapshot, stateRef.current )
229
- ) {
230
- pushToHistory( snapshot );
231
- }
232
- lastCommittedStateRef.current = stateRef.current;
233
- }, HISTORY_DEBOUNCE_MS );
234
- return () => clearTimeout( debounceTimerRef.current );
235
- }, [ state, pushToHistory ] );
236
-
237
- // Flush the pending debounce immediately: cancel the timer and push the
238
- // pre-change snapshot if the state has actually changed. Used by
239
- // pointer-up / key-up handlers for an instant commit feel, and by
240
- // undo/redo/discrete actions before recording their own history entry.
241
- const commitHistory = useCallback( () => {
242
- clearTimeout( debounceTimerRef.current );
243
- const snapshot = lastCommittedStateRef.current;
244
- if (
245
- snapshot !== null &&
246
- ! areHistoryStatesEqual( snapshot, stateRef.current )
247
- ) {
248
- pushToHistory( snapshot );
249
- }
250
- lastCommittedStateRef.current = stateRef.current;
251
- }, [ pushToHistory ] );
252
-
253
- const undo = useCallback( () => {
254
- // Flush any pending gesture so it becomes a distinct undo step
255
- // before we pop history. This ensures mid-gesture undo undoes the
256
- // pending change rather than silently discarding it.
257
- commitHistory();
258
- const prev = historyRef.current[ historyRef.current.length - 1 ];
259
- if ( ! prev ) {
260
- return;
261
- }
262
- redoStackRef.current = [ stateRef.current, ...redoStackRef.current ];
263
- historyRef.current = historyRef.current.slice( 0, -1 );
264
- suppressDebounceRef.current = true;
265
- setHasUndo( historyRef.current.length > 0 );
266
- setHasRedo( true );
267
- dispatch( { type: 'RESET', payload: prev } );
268
- }, [ dispatch, commitHistory ] );
269
-
270
- const redo = useCallback( () => {
271
- // Flush any pending gesture before redoing so the in-flight change
272
- // is saved and the redo target lands on top of it.
273
- commitHistory();
274
- const next = redoStackRef.current[ 0 ];
275
- if ( ! next ) {
276
- return;
277
- }
278
- historyRef.current = [ ...historyRef.current, stateRef.current ];
279
- redoStackRef.current = redoStackRef.current.slice( 1 );
280
- suppressDebounceRef.current = true;
281
- setHasUndo( true );
282
- setHasRedo( redoStackRef.current.length > 0 );
283
- dispatch( { type: 'RESET', payload: next } );
284
- }, [ dispatch, commitHistory ] );
285
-
286
- const setImage = useCallback(
287
- ( image: CropperState[ 'image' ] ) => {
288
- clearTimeout( debounceTimerRef.current );
289
- suppressDebounceRef.current = true;
290
- dispatch( { type: 'SET_IMAGE', payload: image } );
291
- // Refresh the "clean" snapshot to match the post-load state
292
- // produced by the reducer. Otherwise containment can nudge
293
- // pan/zoom by tiny amounts on load and `isDirty` would
294
- // report true from the start.
295
- initialRef.current = enforceContainment( {
296
- ...initialRef.current,
297
- image,
298
- } );
299
- },
300
- [ dispatch ]
301
- );
302
-
303
- const setPan = useCallback(
304
- ( pan: NormalizedPoint ) => {
305
- dispatch( { type: 'SET_PAN', payload: pan } );
306
- },
307
- [ dispatch ]
308
- );
309
-
310
- const setZoom = useCallback(
311
- ( zoom: number ) => {
312
- dispatch( { type: 'SET_ZOOM', payload: zoom } );
313
- },
314
- [ dispatch ]
315
- );
316
-
317
- const setZoomAtPoint = useCallback(
318
- ( zoom: number, pan: NormalizedPoint ) => {
319
- dispatch( {
320
- type: 'SET_ZOOM_AT_POINT',
321
- payload: { zoom, pan },
322
- } );
323
- },
324
- [ dispatch ]
325
- );
326
-
327
- const setRotation = useCallback(
328
- ( rotation: number ) => {
329
- dispatch( { type: 'SET_ROTATION', payload: rotation } );
330
- },
331
- [ dispatch ]
332
- );
333
-
334
- const setFlip = useCallback(
335
- ( flip: Flip ) => {
336
- commitHistory(); // flush any pending continuous gesture first
337
- pushToHistory(); // record current state as the undo point
338
- suppressDebounceRef.current = true;
339
- dispatch( { type: 'SET_FLIP', payload: flip } );
340
- },
341
- [ dispatch, pushToHistory, commitHistory ]
342
- );
343
-
344
- const toggleFlip = useCallback(
345
- ( direction: 'horizontal' | 'vertical' ) => {
346
- setFlip( {
347
- ...stateRef.current.flip,
348
- [ direction ]: ! stateRef.current.flip[ direction ],
349
- } );
350
- },
351
- [ setFlip ]
352
- );
353
-
354
- const snapRotate90 = useCallback(
355
- ( direction: 1 | -1 ) => {
356
- commitHistory();
357
- pushToHistory();
358
- suppressDebounceRef.current = true;
359
- dispatch( {
360
- type: 'SNAP_ROTATE_90',
361
- payload: { direction },
362
- } );
363
- },
364
- [ dispatch, pushToHistory, commitHistory ]
365
- );
366
-
367
- const setCropRect = useCallback(
368
- ( rect: NormalizedRect ) => {
369
- dispatch( { type: 'SET_CROP_RECT', payload: rect } );
370
- },
371
- [ dispatch ]
372
- );
373
-
374
- const settleCrop = useCallback( () => {
375
- commitHistory();
376
- suppressDebounceRef.current = true;
377
- dispatch( { type: 'SETTLE_CROP' } );
378
- }, [ dispatch, commitHistory ] );
379
-
380
- const applyOperation = useCallback(
381
- ( op: TransformOperation ) => {
382
- commitHistory();
383
- pushToHistory();
384
- suppressDebounceRef.current = true;
385
- dispatch( { type: 'APPLY_OPERATION', payload: op } );
386
- },
387
- [ dispatch, pushToHistory, commitHistory ]
388
- );
389
-
390
- const reset = useCallback(
391
- ( resetState?: Partial< CropperState > ) => {
392
- commitHistory();
393
- const nextInitialState = enforceContainment( {
394
- ...DEFAULT_STATE,
395
- image: stateRef.current.image,
396
- ...resetState,
397
- } );
398
- if (
399
- ! areHistoryStatesEqual( stateRef.current, nextInitialState )
400
- ) {
401
- pushToHistory();
402
- }
403
- suppressDebounceRef.current = true;
404
- dispatch( { type: 'RESET', payload: resetState } );
405
- // Mirror the reducer's RESET exactly so isDirty stays in
406
- // sync. RESET preserves the currently-loaded image; the
407
- // containment step can tweak pan/zoom/cropRect by float ulp,
408
- // which we must fold into the "initial" snapshot or isDirty
409
- // would report true after a reset.
410
- initialRef.current = nextInitialState;
411
- },
412
- [ dispatch, pushToHistory, commitHistory ]
413
- );
414
-
415
- const isDirty = isStateDirty( state, initialRef.current );
416
-
417
- const getCroppedImage = useCallback(
418
- ( mimeType?: string, quality?: number ): Promise< Blob > => {
419
- if ( ! state.image ) {
420
- return Promise.reject(
421
- new Error( 'No image loaded — call setImage first.' )
422
- );
423
- }
424
- return exportCroppedImage(
425
- state.image.src,
426
- state,
427
- mimeType,
428
- quality
429
- );
430
- },
431
- [ state ]
432
- );
433
-
434
- const controller: UseCropperStateReturn = {
435
- state,
436
- setImage,
437
- setPan,
438
- setZoom,
439
- setZoomAtPoint,
440
- setRotation,
441
- setFlip,
442
- toggleFlip,
443
- snapRotate90,
444
- setCropRect,
445
- settleCrop,
446
- applyOperation,
447
- reset,
448
- isDirty,
449
- getCroppedImage,
450
- hasUndo,
451
- hasRedo,
452
- undo,
453
- redo,
454
- commitHistory,
455
- };
456
- return controller;
457
- }