@wordpress/media-editor 0.20.0 → 0.22.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 (386) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +2 -1
  3. package/build/components/media-editor/index.cjs +533 -695
  4. package/build/components/media-editor/index.cjs.map +1 -7
  5. package/build/components/media-editor/use-crop-options.cjs +43 -58
  6. package/build/components/media-editor/use-crop-options.cjs.map +1 -7
  7. package/build/components/media-editor/use-save-media-editor.cjs +111 -185
  8. package/build/components/media-editor/use-save-media-editor.cjs.map +1 -7
  9. package/build/components/media-editor-canvas/index.cjs +122 -124
  10. package/build/components/media-editor-canvas/index.cjs.map +1 -7
  11. package/build/components/media-editor-crop-panel/index.cjs +65 -68
  12. package/build/components/media-editor-crop-panel/index.cjs.map +1 -7
  13. package/build/components/media-editor-fine-rotation/index.cjs +62 -72
  14. package/build/components/media-editor-fine-rotation/index.cjs.map +1 -7
  15. package/build/components/media-editor-image-controls/index.cjs +183 -257
  16. package/build/components/media-editor-image-controls/index.cjs.map +1 -7
  17. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +119 -157
  18. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +1 -7
  19. package/build/components/media-editor-modal/build-modifiers.cjs +107 -88
  20. package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -7
  21. package/build/components/media-editor-modal/index.cjs +112 -153
  22. package/build/components/media-editor-modal/index.cjs.map +1 -7
  23. package/build/components/media-editor-provider/index.cjs +32 -54
  24. package/build/components/media-editor-provider/index.cjs.map +1 -7
  25. package/build/components/media-form/index.cjs +77 -78
  26. package/build/components/media-form/index.cjs.map +1 -7
  27. package/build/components/media-preview/index.cjs +114 -114
  28. package/build/components/media-preview/index.cjs.map +1 -7
  29. package/build/components/rotation-ruler/index.cjs +181 -188
  30. package/build/components/rotation-ruler/index.cjs.map +1 -7
  31. package/build/components/rotation-ruler/use-ruler-drag.cjs +132 -149
  32. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -7
  33. package/build/hooks/use-crop-gesture-handlers.cjs +82 -76
  34. package/build/hooks/use-crop-gesture-handlers.cjs.map +1 -7
  35. package/build/image-editor/core/camera.cjs +257 -205
  36. package/build/image-editor/core/camera.cjs.map +1 -7
  37. package/build/image-editor/core/constants.cjs +127 -102
  38. package/build/image-editor/core/constants.cjs.map +1 -7
  39. package/build/image-editor/core/containment.cjs +275 -262
  40. package/build/image-editor/core/containment.cjs.map +1 -7
  41. package/build/image-editor/core/crop-rect.cjs +33 -44
  42. package/build/image-editor/core/crop-rect.cjs.map +1 -7
  43. package/build/image-editor/core/export/canvas-renderer.cjs +165 -135
  44. package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -7
  45. package/build/image-editor/core/index.cjs +88 -61
  46. package/build/image-editor/core/interaction-controller.cjs +583 -646
  47. package/build/image-editor/core/interaction-controller.cjs.map +1 -7
  48. package/build/image-editor/core/math/rotation.cjs +26 -35
  49. package/build/image-editor/core/math/rotation.cjs.map +1 -7
  50. package/build/image-editor/core/math/sanitize.cjs +124 -80
  51. package/build/image-editor/core/math/sanitize.cjs.map +1 -7
  52. package/build/image-editor/core/setter-helpers.cjs +57 -60
  53. package/build/image-editor/core/setter-helpers.cjs.map +1 -7
  54. package/build/image-editor/core/source-region.cjs +220 -190
  55. package/build/image-editor/core/source-region.cjs.map +1 -7
  56. package/build/image-editor/core/state.cjs +278 -265
  57. package/build/image-editor/core/state.cjs.map +1 -7
  58. package/build/image-editor/core/stencil-math.cjs +193 -206
  59. package/build/image-editor/core/stencil-math.cjs.map +1 -7
  60. package/build/image-editor/core/transform-style.cjs +39 -48
  61. package/build/image-editor/core/transform-style.cjs.map +1 -7
  62. package/build/image-editor/core/transforms/pipeline.cjs +36 -38
  63. package/build/image-editor/core/transforms/pipeline.cjs.map +1 -7
  64. package/build/image-editor/core/types.cjs +4 -18
  65. package/build/image-editor/core/viewport-state.cjs +31 -45
  66. package/build/image-editor/core/viewport-state.cjs.map +1 -7
  67. package/build/image-editor/index.cjs +65 -64
  68. package/build/image-editor/react/components/cropper-provider.cjs +44 -45
  69. package/build/image-editor/react/components/cropper-provider.cjs.map +1 -7
  70. package/build/image-editor/react/components/cropper.cjs +583 -720
  71. package/build/image-editor/react/components/cropper.cjs.map +1 -7
  72. package/build/image-editor/react/components/index.cjs +22 -33
  73. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +135 -160
  74. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -7
  75. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +28 -59
  76. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +1 -7
  77. package/build/image-editor/react/components/overlays/grid-overlay.cjs +58 -88
  78. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +1 -7
  79. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +361 -420
  80. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +1 -7
  81. package/build/image-editor/react/components/viewport-provider.cjs +21 -46
  82. package/build/image-editor/react/components/viewport-provider.cjs.map +1 -7
  83. package/build/image-editor/react/hooks/build-cropper-setters.cjs +76 -67
  84. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -7
  85. package/build/image-editor/react/hooks/index.cjs +16 -31
  86. package/build/image-editor/react/hooks/use-aria-announcer.cjs +90 -146
  87. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +1 -7
  88. package/build/image-editor/react/hooks/use-cropper-controller.cjs +80 -0
  89. package/build/image-editor/react/hooks/use-cropper-controller.cjs.map +1 -0
  90. package/build/image-editor/react/hooks/use-interaction.cjs +179 -205
  91. package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -7
  92. package/build/image-editor/react/hooks/use-transform-style.cjs +20 -34
  93. package/build/image-editor/react/hooks/use-transform-style.cjs.map +1 -7
  94. package/build/image-editor/react/hooks/use-viewport-state.cjs +36 -47
  95. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -7
  96. package/build/image-editor/react/visually-hidden-style.cjs +21 -38
  97. package/build/image-editor/react/visually-hidden-style.cjs.map +1 -7
  98. package/build/index.cjs +45 -39
  99. package/build/lock-unlock.cjs +9 -35
  100. package/build/lock-unlock.cjs.map +1 -7
  101. package/build/private-apis.cjs +16 -37
  102. package/build/private-apis.cjs.map +1 -7
  103. package/build/state/aspect-ratio-presets.cjs +53 -0
  104. package/build/state/aspect-ratio-presets.cjs.map +1 -0
  105. package/build/state/composite-reducer.cjs +136 -89
  106. package/build/state/composite-reducer.cjs.map +1 -7
  107. package/build/state/index.cjs +63 -47
  108. package/build/state/media-editor-state-provider.cjs +35 -48
  109. package/build/state/media-editor-state-provider.cjs.map +1 -7
  110. package/build/state/types.cjs +12 -31
  111. package/build/state/types.cjs.map +1 -7
  112. package/build/state/use-media-editor-state.cjs +261 -281
  113. package/build/state/use-media-editor-state.cjs.map +1 -7
  114. package/build/store/actions.cjs +17 -44
  115. package/build/store/actions.cjs.map +1 -7
  116. package/build/store/constants.cjs +8 -29
  117. package/build/store/constants.cjs.map +1 -7
  118. package/build/store/index.cjs +33 -40
  119. package/build/store/index.cjs.map +1 -7
  120. package/build/store/reducer.cjs +28 -50
  121. package/build/store/reducer.cjs.map +1 -7
  122. package/build/store/selectors.cjs +16 -41
  123. package/build/store/selectors.cjs.map +1 -7
  124. package/build/utils/get-media-type.cjs +18 -41
  125. package/build/utils/get-media-type.cjs.map +1 -7
  126. package/build/utils/index.cjs +9 -28
  127. package/build-module/components/media-editor/index.mjs +490 -682
  128. package/build-module/components/media-editor/index.mjs.map +1 -7
  129. package/build-module/components/media-editor/use-crop-options.mjs +38 -33
  130. package/build-module/components/media-editor/use-crop-options.mjs.map +1 -7
  131. package/build-module/components/media-editor/use-save-media-editor.mjs +89 -157
  132. package/build-module/components/media-editor/use-save-media-editor.mjs.map +1 -7
  133. package/build-module/components/media-editor-canvas/index.mjs +97 -95
  134. package/build-module/components/media-editor-canvas/index.mjs.map +1 -7
  135. package/build-module/components/media-editor-crop-panel/index.mjs +44 -43
  136. package/build-module/components/media-editor-crop-panel/index.mjs.map +1 -7
  137. package/build-module/components/media-editor-fine-rotation/index.mjs +39 -45
  138. package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -7
  139. package/build-module/components/media-editor-image-controls/index.mjs +172 -237
  140. package/build-module/components/media-editor-image-controls/index.mjs.map +1 -7
  141. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +112 -137
  142. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +1 -7
  143. package/build-module/components/media-editor-modal/build-modifiers.mjs +103 -64
  144. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +1 -7
  145. package/build-module/components/media-editor-modal/index.mjs +88 -122
  146. package/build-module/components/media-editor-modal/index.mjs.map +1 -7
  147. package/build-module/components/media-editor-provider/index.mjs +26 -28
  148. package/build-module/components/media-editor-provider/index.mjs.map +1 -7
  149. package/build-module/components/media-form/index.mjs +69 -54
  150. package/build-module/components/media-form/index.mjs.map +1 -7
  151. package/build-module/components/media-preview/index.mjs +105 -89
  152. package/build-module/components/media-preview/index.mjs.map +1 -7
  153. package/build-module/components/rotation-ruler/index.mjs +161 -164
  154. package/build-module/components/rotation-ruler/index.mjs.map +1 -7
  155. package/build-module/components/rotation-ruler/use-ruler-drag.mjs +125 -122
  156. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -7
  157. package/build-module/hooks/use-crop-gesture-handlers.mjs +76 -50
  158. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +1 -7
  159. package/build-module/image-editor/core/camera.mjs +245 -178
  160. package/build-module/image-editor/core/camera.mjs.map +1 -7
  161. package/build-module/image-editor/core/constants.mjs +110 -64
  162. package/build-module/image-editor/core/constants.mjs.map +1 -7
  163. package/build-module/image-editor/core/containment.mjs +265 -237
  164. package/build-module/image-editor/core/containment.mjs.map +1 -7
  165. package/build-module/image-editor/core/crop-rect.mjs +29 -20
  166. package/build-module/image-editor/core/crop-rect.mjs.map +1 -7
  167. package/build-module/image-editor/core/export/canvas-renderer.mjs +155 -105
  168. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -7
  169. package/build-module/image-editor/core/index.mjs +5 -34
  170. package/build-module/image-editor/core/interaction-controller.mjs +579 -628
  171. package/build-module/image-editor/core/interaction-controller.mjs.map +1 -7
  172. package/build-module/image-editor/core/math/rotation.mjs +21 -10
  173. package/build-module/image-editor/core/math/rotation.mjs.map +1 -7
  174. package/build-module/image-editor/core/math/sanitize.mjs +116 -52
  175. package/build-module/image-editor/core/math/sanitize.mjs.map +1 -7
  176. package/build-module/image-editor/core/setter-helpers.mjs +52 -35
  177. package/build-module/image-editor/core/setter-helpers.mjs.map +1 -7
  178. package/build-module/image-editor/core/source-region.mjs +210 -160
  179. package/build-module/image-editor/core/source-region.mjs.map +1 -7
  180. package/build-module/image-editor/core/state.mjs +271 -243
  181. package/build-module/image-editor/core/state.mjs.map +1 -7
  182. package/build-module/image-editor/core/stencil-math.mjs +185 -178
  183. package/build-module/image-editor/core/stencil-math.mjs.map +1 -7
  184. package/build-module/image-editor/core/transform-style.mjs +34 -23
  185. package/build-module/image-editor/core/transform-style.mjs.map +1 -7
  186. package/build-module/image-editor/core/transforms/pipeline.mjs +29 -11
  187. package/build-module/image-editor/core/transforms/pipeline.mjs.map +1 -7
  188. package/build-module/image-editor/core/types.mjs +0 -1
  189. package/build-module/image-editor/core/viewport-state.mjs +26 -20
  190. package/build-module/image-editor/core/viewport-state.mjs.map +1 -7
  191. package/build-module/image-editor/index.mjs +3 -38
  192. package/build-module/image-editor/react/components/cropper-provider.mjs +38 -21
  193. package/build-module/image-editor/react/components/cropper-provider.mjs.map +1 -7
  194. package/build-module/image-editor/react/components/cropper.mjs +550 -696
  195. package/build-module/image-editor/react/components/cropper.mjs.map +1 -7
  196. package/build-module/image-editor/react/components/index.mjs +1 -7
  197. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +130 -135
  198. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -7
  199. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +24 -35
  200. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +1 -7
  201. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +54 -64
  202. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +1 -7
  203. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +355 -407
  204. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +1 -7
  205. package/build-module/image-editor/react/components/viewport-provider.mjs +15 -22
  206. package/build-module/image-editor/react/components/viewport-provider.mjs.map +1 -7
  207. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +72 -43
  208. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -7
  209. package/build-module/image-editor/react/hooks/index.mjs +2 -7
  210. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +83 -119
  211. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +1 -7
  212. package/build-module/image-editor/react/hooks/use-cropper-controller.mjs +75 -0
  213. package/build-module/image-editor/react/hooks/use-cropper-controller.mjs.map +1 -0
  214. package/build-module/image-editor/react/hooks/use-interaction.mjs +175 -183
  215. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +1 -7
  216. package/build-module/image-editor/react/hooks/use-transform-style.mjs +15 -9
  217. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +1 -7
  218. package/build-module/image-editor/react/hooks/use-viewport-state.mjs +32 -26
  219. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -7
  220. package/build-module/image-editor/react/visually-hidden-style.mjs +17 -14
  221. package/build-module/image-editor/react/visually-hidden-style.mjs.map +1 -7
  222. package/build-module/index.mjs +3 -10
  223. package/build-module/lock-unlock.mjs +5 -10
  224. package/build-module/lock-unlock.mjs.map +1 -7
  225. package/build-module/private-apis.mjs +8 -9
  226. package/build-module/private-apis.mjs.map +1 -7
  227. package/build-module/state/aspect-ratio-presets.mjs +48 -0
  228. package/build-module/state/aspect-ratio-presets.mjs.map +1 -0
  229. package/build-module/state/composite-reducer.mjs +114 -75
  230. package/build-module/state/composite-reducer.mjs.map +1 -7
  231. package/build-module/state/index.mjs +5 -25
  232. package/build-module/state/media-editor-state-provider.mjs +29 -24
  233. package/build-module/state/media-editor-state-provider.mjs.map +1 -7
  234. package/build-module/state/types.mjs +8 -7
  235. package/build-module/state/types.mjs.map +1 -7
  236. package/build-module/state/use-media-editor-state.mjs +256 -273
  237. package/build-module/state/use-media-editor-state.mjs.map +1 -7
  238. package/build-module/store/actions.mjs +12 -19
  239. package/build-module/store/actions.mjs.map +1 -7
  240. package/build-module/store/constants.mjs +4 -5
  241. package/build-module/store/constants.mjs.map +1 -7
  242. package/build-module/store/index.mjs +8 -9
  243. package/build-module/store/index.mjs.map +1 -7
  244. package/build-module/store/reducer.mjs +23 -26
  245. package/build-module/store/reducer.mjs.map +1 -7
  246. package/build-module/store/selectors.mjs +8 -13
  247. package/build-module/store/selectors.mjs.map +1 -7
  248. package/build-module/utils/get-media-type.mjs +14 -17
  249. package/build-module/utils/get-media-type.mjs.map +1 -7
  250. package/build-module/utils/index.mjs +1 -6
  251. package/build-style/style-rtl.css +41 -4
  252. package/build-style/style.css +41 -4
  253. package/build-types/components/media-editor/index.d.ts +6 -1
  254. package/build-types/components/media-editor/index.d.ts.map +1 -1
  255. package/build-types/components/media-editor/use-crop-options.d.ts +0 -1
  256. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
  257. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
  258. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  259. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  260. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  261. package/build-types/components/media-editor-modal/build-modifiers.d.ts +6 -5
  262. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -1
  263. package/build-types/components/media-editor-modal/index.d.ts +7 -2
  264. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  265. package/build-types/components/media-editor-provider/index.d.ts +7 -1
  266. package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
  267. package/build-types/components/media-form/index.d.ts +5 -2
  268. package/build-types/components/media-form/index.d.ts.map +1 -1
  269. package/build-types/components/media-preview/index.d.ts.map +1 -1
  270. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
  271. package/build-types/image-editor/core/constants.d.ts +0 -1
  272. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  273. package/build-types/image-editor/core/index.d.ts +1 -1
  274. package/build-types/image-editor/core/index.d.ts.map +1 -1
  275. package/build-types/image-editor/core/setter-helpers.d.ts +1 -1
  276. package/build-types/image-editor/core/types.d.ts +1 -1
  277. package/build-types/image-editor/index.d.ts +7 -6
  278. package/build-types/image-editor/index.d.ts.map +1 -1
  279. package/build-types/image-editor/react/components/cropper-provider.d.ts +2 -2
  280. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
  281. package/build-types/image-editor/react/components/cropper.d.ts +5 -14
  282. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  283. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +2 -2
  284. package/build-types/image-editor/react/hooks/index.d.ts +2 -2
  285. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
  286. package/build-types/image-editor/react/hooks/{use-cropper-reducer.d.ts → use-cropper-controller.d.ts} +12 -12
  287. package/build-types/image-editor/react/hooks/use-cropper-controller.d.ts.map +1 -0
  288. package/build-types/state/aspect-ratio-presets.d.ts +7 -0
  289. package/build-types/state/aspect-ratio-presets.d.ts.map +1 -0
  290. package/build-types/state/composite-reducer.d.ts +2 -2
  291. package/build-types/state/composite-reducer.d.ts.map +1 -1
  292. package/build-types/state/index.d.ts +2 -1
  293. package/build-types/state/index.d.ts.map +1 -1
  294. package/build-types/state/types.d.ts +36 -6
  295. package/build-types/state/types.d.ts.map +1 -1
  296. package/build-types/state/use-media-editor-state.d.ts +16 -5
  297. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  298. package/package.json +35 -24
  299. package/src/components/media-editor/index.tsx +226 -183
  300. package/src/components/media-editor/style.scss +19 -0
  301. package/src/components/media-editor/use-crop-options.ts +5 -4
  302. package/src/components/media-editor/use-save-media-editor.ts +2 -6
  303. package/src/components/media-editor-canvas/index.tsx +18 -28
  304. package/src/components/media-editor-crop-panel/index.tsx +17 -9
  305. package/src/components/media-editor-image-controls/index.tsx +4 -2
  306. package/src/components/media-editor-image-controls/style.scss +1 -1
  307. package/src/components/media-editor-modal/build-modifiers.ts +13 -7
  308. package/src/components/media-editor-modal/index.tsx +8 -1
  309. package/src/components/media-editor-modal/style.scss +1 -1
  310. package/src/components/media-editor-provider/index.tsx +8 -1
  311. package/src/components/media-form/index.tsx +48 -42
  312. package/src/components/media-preview/index.tsx +68 -25
  313. package/src/components/media-preview/style.scss +16 -2
  314. package/src/components/rotation-ruler/style.scss +6 -2
  315. package/src/components/rotation-ruler/use-ruler-drag.ts +6 -11
  316. package/src/image-editor/README.md +8 -8
  317. package/src/image-editor/core/constants.ts +0 -13
  318. package/src/image-editor/core/index.ts +1 -5
  319. package/src/image-editor/core/setter-helpers.ts +1 -1
  320. package/src/image-editor/core/types.ts +1 -1
  321. package/src/image-editor/docs/architecture.md +5 -1
  322. package/src/image-editor/docs/recipes.md +3 -3
  323. package/src/image-editor/index.ts +7 -16
  324. package/src/image-editor/react/components/cropper-provider.tsx +5 -5
  325. package/src/image-editor/react/components/cropper.tsx +20 -42
  326. package/src/image-editor/react/hooks/build-cropper-setters.ts +2 -2
  327. package/src/image-editor/react/hooks/index.ts +2 -2
  328. package/src/image-editor/react/hooks/{use-cropper-reducer.ts → use-cropper-controller.ts} +11 -11
  329. package/src/state/aspect-ratio-presets.ts +18 -0
  330. package/src/state/composite-reducer.ts +26 -13
  331. package/src/state/index.ts +3 -0
  332. package/src/state/types.ts +39 -11
  333. package/src/state/use-media-editor-state.ts +53 -16
  334. package/build/image-editor/core/index.cjs.map +0 -7
  335. package/build/image-editor/core/types.cjs.map +0 -7
  336. package/build/image-editor/index.cjs.map +0 -7
  337. package/build/image-editor/react/components/index.cjs.map +0 -7
  338. package/build/image-editor/react/hooks/index.cjs.map +0 -7
  339. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +0 -113
  340. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +0 -7
  341. package/build/index.cjs.map +0 -7
  342. package/build/state/index.cjs.map +0 -7
  343. package/build/utils/index.cjs.map +0 -7
  344. package/build-module/image-editor/core/index.mjs.map +0 -7
  345. package/build-module/image-editor/core/types.mjs.map +0 -7
  346. package/build-module/image-editor/index.mjs.map +0 -7
  347. package/build-module/image-editor/react/components/index.mjs.map +0 -7
  348. package/build-module/image-editor/react/hooks/index.mjs.map +0 -7
  349. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +0 -100
  350. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +0 -7
  351. package/build-module/index.mjs.map +0 -7
  352. package/build-module/state/index.mjs.map +0 -7
  353. package/build-module/utils/index.mjs.map +0 -7
  354. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +0 -1
  355. package/global.d.ts +0 -5
  356. package/src/components/media-editor/test/restore-original.browser.test.tsx +0 -227
  357. package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -106
  358. package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -72
  359. package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -186
  360. package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -359
  361. package/src/components/media-editor-modal/test/index.jsdom.test.tsx +0 -182
  362. package/src/components/rotation-ruler/stories/index.story.tsx +0 -43
  363. package/src/components/rotation-ruler/test/index.jsdom.test.tsx +0 -140
  364. package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +0 -823
  365. package/src/image-editor/core/math/test/sanitize.ts +0 -242
  366. package/src/image-editor/core/test/camera.ts +0 -1218
  367. package/src/image-editor/core/test/interaction-controller.ts +0 -1299
  368. package/src/image-editor/core/test/preview-export-parity.ts +0 -249
  369. package/src/image-editor/core/test/render-camera-bridge.ts +0 -349
  370. package/src/image-editor/core/test/state.ts +0 -876
  371. package/src/image-editor/core/test/stencil-math.ts +0 -665
  372. package/src/image-editor/core/transforms/test/pipeline.ts +0 -267
  373. package/src/image-editor/react/components/stencils/test/rectangle-stencil.browser.test.tsx +0 -438
  374. package/src/image-editor/react/components/test/cropper.browser.test.tsx +0 -1462
  375. package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +0 -491
  376. package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +0 -1049
  377. package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +0 -213
  378. package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +0 -140
  379. package/src/image-editor/stories/rectangle-crop.story.tsx +0 -1073
  380. package/src/image-editor/stories/style.module.css +0 -65
  381. package/src/state/test/use-media-editor-state.browser.test.tsx +0 -79
  382. package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -386
  383. package/src/store/test/reducer.ts +0 -64
  384. package/tsconfig.build.json +0 -26
  385. package/tsconfig.build.tsbuildinfo +0 -1
  386. package/tsconfig.json +0 -37
@@ -1,1049 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { renderHook, act } from '@testing-library/react';
3
- import { useCropperReducer } from '../use-cropper-reducer';
4
- import { DEFAULT_STATE } from '../../../core/constants';
5
-
6
- describe( 'useCropperReducer', () => {
7
- it( 'should initialize with DEFAULT_STATE', () => {
8
- const { result } = renderHook( () => useCropperReducer() );
9
-
10
- expect( result.current.state ).toEqual( DEFAULT_STATE );
11
- } );
12
-
13
- it( 'should initialize with merged partial state', () => {
14
- const { result } = renderHook( () =>
15
- useCropperReducer( { zoom: 2, rotation: 45 } )
16
- );
17
-
18
- expect( result.current.state.zoom ).toBe( 2 );
19
- expect( result.current.state.rotation ).toBe( 45 );
20
- // Non-overridden fields should match defaults.
21
- expect( result.current.state.pan ).toEqual( DEFAULT_STATE.pan );
22
- } );
23
-
24
- it( 'should dispatch SET_CROP via setCrop', () => {
25
- const { result } = renderHook( () => useCropperReducer() );
26
-
27
- // At zoom=1, crop is clamped to (0,0) — no panning possible.
28
- // Zoom in first to allow panning.
29
- act( () => {
30
- result.current.setZoom( 3 );
31
- } );
32
-
33
- act( () => {
34
- result.current.setPan( { x: 0.3, y: 0.2 } );
35
- } );
36
-
37
- expect( result.current.state.pan.x ).toBeCloseTo( 0.3 );
38
- expect( result.current.state.pan.y ).toBeCloseTo( 0.2 );
39
- } );
40
-
41
- it( 'should dispatch SET_ZOOM via setZoom', () => {
42
- const { result } = renderHook( () => useCropperReducer() );
43
-
44
- act( () => {
45
- result.current.setZoom( 3 );
46
- } );
47
-
48
- expect( result.current.state.zoom ).toBe( 3 );
49
- } );
50
-
51
- it( 'leaves pan at zero on setZoom when the cropRect is centered', () => {
52
- // Sanity: the default cropRect spans the full image, so its
53
- // center coincides with the image center and the crop-center
54
- // focal-point correction degenerates to "no pan change."
55
- // Guards against regressions where setZoom accidentally pans.
56
- const { result } = renderHook( () => useCropperReducer() );
57
-
58
- act( () => {
59
- result.current.setZoom( 4 );
60
- } );
61
-
62
- expect( result.current.state.zoom ).toBe( 4 );
63
- expect( result.current.state.pan ).toEqual( { x: 0, y: 0 } );
64
-
65
- act( () => {
66
- result.current.setZoom( 2 );
67
- } );
68
-
69
- expect( result.current.state.zoom ).toBe( 2 );
70
- expect( result.current.state.pan ).toEqual( { x: 0, y: 0 } );
71
- } );
72
-
73
- it( 'anchors setZoom at the crop-rect center', () => {
74
- // With an off-center crop, setZoom must shift pan toward the
75
- // crop center as zoom decreases — otherwise `enforceContainment`
76
- // would translate the image toward the nearest viewport corner
77
- // (the original bug this method fixes).
78
- const { result } = renderHook( () => useCropperReducer() );
79
-
80
- act( () => {
81
- result.current.setZoom( 4 );
82
- } );
83
- // Place the crop in the bottom-right quadrant — center
84
- // at ( 0.7, 0.7 ), i.e. ( +0.2, +0.2 ) in pan coords.
85
- act( () => {
86
- result.current.setCropRect( {
87
- x: 0.6,
88
- y: 0.6,
89
- width: 0.2,
90
- height: 0.2,
91
- } );
92
- } );
93
- act( () => {
94
- result.current.setPan( { x: 0, y: 0 } );
95
- } );
96
-
97
- expect( result.current.state.pan ).toEqual( { x: 0, y: 0 } );
98
-
99
- // Zoom out — pan should move toward the focal point in +x, +y,
100
- // not stay at the origin (which would corner-snap on containment).
101
- act( () => {
102
- result.current.setZoom( 2 );
103
- } );
104
-
105
- expect( result.current.state.zoom ).toBe( 2 );
106
- expect( result.current.state.pan.x ).toBeGreaterThan( 0 );
107
- expect( result.current.state.pan.y ).toBeGreaterThan( 0 );
108
- } );
109
-
110
- it( 'should dispatch SET_ZOOM_AT_POINT via setZoomAtPoint', () => {
111
- const { result } = renderHook( () => useCropperReducer() );
112
-
113
- act( () => {
114
- result.current.setZoomAtPoint( 3, { x: 0.2, y: 0.1 } );
115
- } );
116
-
117
- expect( result.current.state.zoom ).toBe( 3 );
118
- expect( result.current.state.pan ).toEqual( { x: 0.2, y: 0.1 } );
119
- } );
120
-
121
- it( 'should clamp zoom to valid range via SET_ZOOM', () => {
122
- const { result } = renderHook( () => useCropperReducer() );
123
-
124
- act( () => {
125
- result.current.setZoom( 15 );
126
- } );
127
-
128
- expect( result.current.state.zoom ).toBe( 10 );
129
-
130
- act( () => {
131
- result.current.setZoom( 0.5 );
132
- } );
133
-
134
- expect( result.current.state.zoom ).toBe( 1 );
135
- } );
136
-
137
- it( 'allows setZoom below 1 when the coverage minimum permits', () => {
138
- // Thin, fine-rotated crop on a portrait image: the coverage
139
- // minimum drops below 1, so `setZoom` (slider / keyboard path)
140
- // must honour values below 1 rather than clamping at MIN_ZOOM.
141
- const { result } = renderHook( () =>
142
- useCropperReducer( {
143
- image: {
144
- src: 'portrait.jpg',
145
- naturalWidth: 900,
146
- naturalHeight: 1600,
147
- },
148
- rotation: 19,
149
- cropRect: { x: 0.48, y: 0, width: 0.04, height: 1 },
150
- } )
151
- );
152
-
153
- act( () => {
154
- result.current.setZoom( 0.9 );
155
- } );
156
-
157
- expect( result.current.state.zoom ).toBeLessThan( 1 );
158
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 0.9 );
159
- } );
160
-
161
- it( 'should dispatch SET_ROTATION via setRotation', () => {
162
- const { result } = renderHook( () => useCropperReducer() );
163
-
164
- act( () => {
165
- result.current.setRotation( 90 );
166
- } );
167
-
168
- expect( result.current.state.rotation ).toBe( 90 );
169
- } );
170
-
171
- it( 'should normalize rotation via SET_ROTATION', () => {
172
- const { result } = renderHook( () => useCropperReducer() );
173
-
174
- act( () => {
175
- result.current.setRotation( -90 );
176
- } );
177
-
178
- expect( result.current.state.rotation ).toBe( 270 );
179
-
180
- act( () => {
181
- result.current.setRotation( 450 );
182
- } );
183
-
184
- expect( result.current.state.rotation ).toBe( 90 );
185
- } );
186
-
187
- it( 'should dispatch SET_FLIP via setFlip', () => {
188
- const { result } = renderHook( () => useCropperReducer() );
189
-
190
- act( () => {
191
- result.current.setFlip( {
192
- horizontal: true,
193
- vertical: false,
194
- } );
195
- } );
196
-
197
- expect( result.current.state.flip ).toEqual( {
198
- horizontal: true,
199
- vertical: false,
200
- } );
201
- } );
202
-
203
- it( 'toggleFlip should toggle horizontal flip', () => {
204
- const { result } = renderHook( () => useCropperReducer() );
205
-
206
- act( () => {
207
- result.current.toggleFlip( 'horizontal' );
208
- } );
209
-
210
- expect( result.current.state.flip ).toEqual( {
211
- horizontal: true,
212
- vertical: false,
213
- } );
214
-
215
- act( () => {
216
- result.current.toggleFlip( 'horizontal' );
217
- } );
218
-
219
- expect( result.current.state.flip ).toEqual( {
220
- horizontal: false,
221
- vertical: false,
222
- } );
223
- } );
224
-
225
- it( 'toggleFlip should toggle vertical flip independently', () => {
226
- const { result } = renderHook( () => useCropperReducer() );
227
-
228
- act( () => {
229
- result.current.toggleFlip( 'vertical' );
230
- } );
231
-
232
- expect( result.current.state.flip ).toEqual( {
233
- horizontal: false,
234
- vertical: true,
235
- } );
236
- } );
237
-
238
- it( 'should dispatch SET_CROP_RECT via setCropRect', () => {
239
- const { result } = renderHook( () => useCropperReducer() );
240
-
241
- const rect = { x: 0.1, y: 0.2, width: 0.5, height: 0.6 };
242
- act( () => {
243
- result.current.setCropRect( rect );
244
- } );
245
-
246
- expect( result.current.state.cropRect ).toEqual( rect );
247
- } );
248
-
249
- it( 'should dispatch APPLY_OPERATION via applyOperation', () => {
250
- const { result } = renderHook( () => useCropperReducer() );
251
-
252
- act( () => {
253
- result.current.applyOperation( {
254
- type: 'rotate',
255
- degrees: 90,
256
- } );
257
- } );
258
-
259
- expect( result.current.state.rotation ).toBe( 90 );
260
- } );
261
-
262
- it( 'should handle APPLY_OPERATION for flip', () => {
263
- const { result } = renderHook( () => useCropperReducer() );
264
-
265
- act( () => {
266
- result.current.applyOperation( {
267
- type: 'flip',
268
- direction: 'horizontal',
269
- } );
270
- } );
271
-
272
- expect( result.current.state.flip.horizontal ).toBe( true );
273
- expect( result.current.state.flip.vertical ).toBe( false );
274
- } );
275
-
276
- it( 'should accumulate rotation via APPLY_OPERATION', () => {
277
- const { result } = renderHook( () => useCropperReducer() );
278
-
279
- act( () => {
280
- result.current.applyOperation( {
281
- type: 'rotate',
282
- degrees: 90,
283
- } );
284
- } );
285
-
286
- act( () => {
287
- result.current.applyOperation( {
288
- type: 'rotate',
289
- degrees: 45,
290
- } );
291
- } );
292
-
293
- expect( result.current.state.rotation ).toBe( 135 );
294
- } );
295
-
296
- it( 'should handle APPLY_OPERATION for crop', () => {
297
- const { result } = renderHook( () => useCropperReducer() );
298
-
299
- const rect = { x: 0.1, y: 0.2, width: 0.5, height: 0.6 };
300
- act( () => {
301
- result.current.applyOperation( {
302
- type: 'crop',
303
- rect,
304
- } );
305
- } );
306
-
307
- expect( result.current.state.cropRect ).toEqual( rect );
308
- } );
309
-
310
- it( 'should handle APPLY_OPERATION for zoom', () => {
311
- const { result } = renderHook( () => useCropperReducer() );
312
-
313
- act( () => {
314
- result.current.applyOperation( {
315
- type: 'zoom',
316
- factor: 3,
317
- } );
318
- } );
319
-
320
- expect( result.current.state.zoom ).toBe( 3 );
321
- } );
322
-
323
- it( 'should reset to DEFAULT_STATE', () => {
324
- const { result } = renderHook( () => useCropperReducer() );
325
-
326
- act( () => {
327
- result.current.setZoom( 5 );
328
- result.current.setRotation( 180 );
329
- } );
330
-
331
- expect( result.current.state.zoom ).toBe( 5 );
332
-
333
- act( () => {
334
- result.current.reset();
335
- } );
336
-
337
- expect( result.current.state ).toEqual( DEFAULT_STATE );
338
- } );
339
-
340
- it( 'should reset to a custom state', () => {
341
- const { result } = renderHook( () => useCropperReducer() );
342
-
343
- act( () => {
344
- result.current.setZoom( 5 );
345
- } );
346
-
347
- act( () => {
348
- result.current.reset( { zoom: 2 } );
349
- } );
350
-
351
- expect( result.current.state.zoom ).toBe( 2 );
352
- expect( result.current.state.pan ).toEqual( DEFAULT_STATE.pan );
353
- } );
354
-
355
- describe( 'isDirty', () => {
356
- it( 'should be false initially', () => {
357
- const { result } = renderHook( () => useCropperReducer() );
358
-
359
- expect( result.current.isDirty ).toBe( false );
360
- } );
361
-
362
- it( 'should be true after changing crop', () => {
363
- const { result } = renderHook( () => useCropperReducer() );
364
-
365
- // Zoom in first so crop change isn't clamped to (0,0).
366
- act( () => {
367
- result.current.setZoom( 3 );
368
- } );
369
-
370
- act( () => {
371
- result.current.setPan( { x: 0.1, y: 0.2 } );
372
- } );
373
-
374
- expect( result.current.isDirty ).toBe( true );
375
- } );
376
-
377
- it( 'should be true after changing zoom', () => {
378
- const { result } = renderHook( () => useCropperReducer() );
379
-
380
- act( () => {
381
- result.current.setZoom( 2 );
382
- } );
383
-
384
- expect( result.current.isDirty ).toBe( true );
385
- } );
386
-
387
- it( 'should be true after changing rotation', () => {
388
- const { result } = renderHook( () => useCropperReducer() );
389
-
390
- act( () => {
391
- result.current.setRotation( 45 );
392
- } );
393
-
394
- expect( result.current.isDirty ).toBe( true );
395
- } );
396
-
397
- it( 'should be true after changing flip', () => {
398
- const { result } = renderHook( () => useCropperReducer() );
399
-
400
- act( () => {
401
- result.current.setFlip( {
402
- horizontal: true,
403
- vertical: false,
404
- } );
405
- } );
406
-
407
- expect( result.current.isDirty ).toBe( true );
408
- } );
409
-
410
- it( 'should be false after reset', () => {
411
- const { result } = renderHook( () => useCropperReducer() );
412
-
413
- act( () => {
414
- result.current.setZoom( 5 );
415
- } );
416
-
417
- expect( result.current.isDirty ).toBe( true );
418
-
419
- act( () => {
420
- result.current.reset();
421
- } );
422
-
423
- expect( result.current.isDirty ).toBe( false );
424
- } );
425
-
426
- it( 'should be false after manually resetting fields to initial values', () => {
427
- const { result } = renderHook( () => useCropperReducer() );
428
-
429
- // Zoom in and pan.
430
- act( () => {
431
- result.current.setZoom( 3 );
432
- } );
433
-
434
- act( () => {
435
- result.current.setPan( { x: 0.2, y: 0.2 } );
436
- } );
437
-
438
- expect( result.current.isDirty ).toBe( true );
439
-
440
- // Reset each field back to its initial value manually.
441
- act( () => {
442
- result.current.setZoom( 1 );
443
- } );
444
-
445
- act( () => {
446
- result.current.setPan( { x: 0, y: 0 } );
447
- } );
448
-
449
- expect( result.current.isDirty ).toBe( false );
450
- } );
451
-
452
- // Regression: loading an image runs the new state through
453
- // enforceContainment, which can nudge pan/zoom by float ulp on
454
- // non-square images. The initial snapshot must be refreshed so
455
- // isDirty still reports false at a "clean" post-load state.
456
- it( 'should be false after setImage on a non-square image', () => {
457
- const { result } = renderHook( () => useCropperReducer() );
458
-
459
- act( () => {
460
- result.current.setImage( {
461
- src: 'test.jpg',
462
- naturalWidth: 1600,
463
- naturalHeight: 900,
464
- } );
465
- } );
466
-
467
- expect( result.current.isDirty ).toBe( false );
468
- } );
469
-
470
- it( 'does not reset the clean baseline when the same image is reported again', () => {
471
- const image = {
472
- src: 'test.jpg',
473
- naturalWidth: 1600,
474
- naturalHeight: 900,
475
- };
476
- const { result } = renderHook( () => useCropperReducer() );
477
-
478
- act( () => {
479
- result.current.setImage( image );
480
- } );
481
- act( () => {
482
- result.current.setRotation( 45 );
483
- } );
484
- expect( result.current.isDirty ).toBe( true );
485
-
486
- act( () => {
487
- result.current.setImage( image );
488
- } );
489
-
490
- expect( result.current.isDirty ).toBe( true );
491
- expect( result.current.state.rotation ).toBe( 45 );
492
- } );
493
-
494
- // Regression: reset() on a loaded image must produce a "clean"
495
- // snapshot that matches what the reducer actually stored
496
- // (preserving the image, re-enforcing containment). Otherwise
497
- // isDirty reports true right after reset.
498
- it( 'should be false after reset on a loaded image', () => {
499
- const { result } = renderHook( () => useCropperReducer() );
500
-
501
- act( () => {
502
- result.current.setImage( {
503
- src: 'test.jpg',
504
- naturalWidth: 1600,
505
- naturalHeight: 900,
506
- } );
507
- } );
508
-
509
- act( () => {
510
- result.current.setZoom( 2 );
511
- result.current.setRotation( 45 );
512
- } );
513
-
514
- expect( result.current.isDirty ).toBe( true );
515
-
516
- act( () => {
517
- result.current.reset();
518
- } );
519
-
520
- expect( result.current.isDirty ).toBe( false );
521
- } );
522
- } );
523
-
524
- describe( 'containment enforcement', () => {
525
- // For these tests, we need a loaded image so the reducer can
526
- // compute minZoomForCover and restrict position.
527
- function setupWithImage() {
528
- const view = renderHook( () => useCropperReducer() );
529
- act( () => {
530
- view.result.current.setImage( {
531
- src: 'test.jpg',
532
- naturalWidth: 1000,
533
- naturalHeight: 500,
534
- } );
535
- } );
536
- return view;
537
- }
538
-
539
- it( 'should restrict position when SET_CROP is dispatched', () => {
540
- const { result } = setupWithImage();
541
-
542
- // At zoom=1, no panning is possible — the image exactly covers
543
- // the crop area. Any pan value should be clamped to (0, 0).
544
- act( () => {
545
- result.current.setPan( { x: 0.5, y: 0.5 } );
546
- } );
547
-
548
- expect( result.current.state.pan.x ).toBeCloseTo( 0, 5 );
549
- expect( result.current.state.pan.y ).toBeCloseTo( 0, 5 );
550
- } );
551
-
552
- it( 'should allow limited panning at zoom > 1', () => {
553
- const { result } = setupWithImage();
554
-
555
- act( () => {
556
- result.current.setZoom( 2 );
557
- } );
558
-
559
- act( () => {
560
- result.current.setPan( { x: 0.3, y: 0.3 } );
561
- } );
562
-
563
- // Should be within bounds (not clamped to 0)
564
- expect( result.current.state.pan.x ).toBeGreaterThan( 0 );
565
- expect( result.current.state.pan.y ).toBeGreaterThan( 0 );
566
-
567
- // But if we try an extreme pan, it should be clamped
568
- act( () => {
569
- result.current.setPan( { x: 100, y: 100 } );
570
- } );
571
-
572
- expect( result.current.state.pan.x ).toBeLessThan( 1 );
573
- expect( result.current.state.pan.y ).toBeLessThan( 1 );
574
- } );
575
-
576
- it( 'should bump zoom to cover crop during rotation', () => {
577
- const { result } = setupWithImage();
578
-
579
- expect( result.current.state.zoom ).toBe( 1 );
580
-
581
- // Rotate 45 degrees via simple SET_ROTATION (no container).
582
- // The crop rect is unchanged and zoom is bumped to cover.
583
- act( () => {
584
- result.current.setRotation( 45 );
585
- } );
586
-
587
- expect( result.current.state.rotation ).toBe( 45 );
588
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 1 );
589
- } );
590
-
591
- it( 'should preserve zoom across rotation changes', () => {
592
- const { result } = setupWithImage();
593
-
594
- act( () => {
595
- result.current.setZoom( 2 );
596
- } );
597
-
598
- act( () => {
599
- result.current.setRotation( 45 );
600
- } );
601
-
602
- // Zoom should be at least 2 (may be higher for 45° coverage).
603
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 2 );
604
-
605
- act( () => {
606
- result.current.setRotation( 0 );
607
- } );
608
-
609
- // Zoom should not drop below the user's explicit level.
610
- // It only ratchets up; the user can zoom out explicitly if
611
- // they want.
612
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 2 );
613
- } );
614
-
615
- it( 'should reset pan when rotation changes', () => {
616
- const { result } = setupWithImage();
617
-
618
- // Zoom in and pan
619
- act( () => {
620
- result.current.setZoom( 3 );
621
- } );
622
- act( () => {
623
- result.current.setPan( { x: 0.2, y: 0.2 } );
624
- } );
625
-
626
- expect( result.current.state.pan.x ).toBeCloseTo( 0.2 );
627
-
628
- // Rotate — position should be re-restricted
629
- act( () => {
630
- result.current.setRotation( 45 );
631
- } );
632
-
633
- // The position should be within the new valid range
634
- const { pan, zoom } = result.current.state;
635
- // Verify the position is valid for the new state
636
- expect( Math.abs( pan.x ) ).toBeLessThanOrEqual( zoom );
637
- expect( Math.abs( pan.y ) ).toBeLessThanOrEqual( zoom );
638
- } );
639
-
640
- it( 'should not allow zoom below minZoomForCover', () => {
641
- const { result } = setupWithImage();
642
-
643
- // At rotation=0, minZoom is 1 for a full crop rect.
644
- act( () => {
645
- result.current.setZoom( 0.5 );
646
- } );
647
-
648
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 1 );
649
-
650
- // Zoom above maximum should be clamped to 10.
651
- act( () => {
652
- result.current.setZoom( 15 );
653
- } );
654
-
655
- expect( result.current.state.zoom ).toBe( 10 );
656
- } );
657
-
658
- it( 'should re-restrict position after zoom change', () => {
659
- const { result } = setupWithImage();
660
-
661
- // Zoom in and pan to the edge
662
- act( () => {
663
- result.current.setZoom( 4 );
664
- } );
665
- act( () => {
666
- result.current.setPan( { x: 1, y: 1 } );
667
- } );
668
-
669
- const panAtZoom4 = { ...result.current.state.pan };
670
-
671
- // Zoom out — pan should be reduced to stay within bounds
672
- act( () => {
673
- result.current.setZoom( 2 );
674
- } );
675
-
676
- expect(
677
- Math.abs( result.current.state.pan.x )
678
- ).toBeLessThanOrEqual( Math.abs( panAtZoom4.x ) );
679
- } );
680
-
681
- it( 'should rescale crop and bump zoom at 90-degree rotation for non-square image', () => {
682
- const { result } = setupWithImage();
683
- // Image is 1000x500 (2:1). At 90° the visual box flips from
684
- // landscape to portrait. The crop rect is rescaled to preserve
685
- // pixel size, and zoom is bumped to cover it.
686
- act( () => {
687
- result.current.setRotation( 90 );
688
- } );
689
-
690
- const { cropRect, zoom } = result.current.state;
691
- // Crop pixel dimensions preserved — the normalized values
692
- // change because the visual box dimensions swapped.
693
- expect( cropRect.width ).toBeGreaterThan( 0 );
694
- expect( cropRect.height ).toBeGreaterThan( 0 );
695
- // Zoom bumps to cover the rescaled crop.
696
- expect( zoom ).toBeGreaterThanOrEqual( 1 );
697
- } );
698
-
699
- it( 'should not reduce zoom when SET_CROP_RECT shrinks the crop', () => {
700
- const { result } = setupWithImage();
701
-
702
- // Zoom in explicitly.
703
- act( () => {
704
- result.current.setZoom( 2 );
705
- } );
706
- expect( result.current.state.zoom ).toBe( 2 );
707
-
708
- // Rotate so that containment matters.
709
- act( () => {
710
- result.current.setRotation( 45 );
711
- } );
712
- const zoomAfterRotation = result.current.state.zoom;
713
-
714
- // Now shrink the crop rect — zoom should not decrease.
715
- act( () => {
716
- result.current.setCropRect( {
717
- x: 0.1,
718
- y: 0.1,
719
- width: 0.3,
720
- height: 0.3,
721
- } );
722
- } );
723
-
724
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual(
725
- zoomAfterRotation
726
- );
727
- } );
728
-
729
- it( 'should bump zoom when SET_CROP_RECT requires it at rotation', () => {
730
- const { result } = setupWithImage();
731
-
732
- act( () => {
733
- result.current.setRotation( 45 );
734
- } );
735
-
736
- // Zoom was already bumped by rotation to cover full crop.
737
- const zoomAfterRotation = result.current.state.zoom;
738
- expect( zoomAfterRotation ).toBeGreaterThan( 1 );
739
-
740
- // Setting a smaller crop rect then a full one should keep zoom
741
- // at the level needed for the full rect.
742
- act( () => {
743
- result.current.setCropRect( {
744
- x: 0.2,
745
- y: 0.2,
746
- width: 0.6,
747
- height: 0.6,
748
- } );
749
- } );
750
-
751
- act( () => {
752
- result.current.setCropRect( {
753
- x: 0,
754
- y: 0,
755
- width: 1,
756
- height: 1,
757
- } );
758
- } );
759
-
760
- // Zoom should be at least what rotation required.
761
- expect( result.current.state.zoom ).toBeGreaterThanOrEqual(
762
- zoomAfterRotation
763
- );
764
- expect( result.current.state.cropRect.width ).toBeCloseTo( 1, 1 );
765
- expect( result.current.state.cropRect.height ).toBeCloseTo( 1, 1 );
766
- } );
767
- } );
768
-
769
- describe( 'SETTLE_CROP', () => {
770
- function setupWithImage() {
771
- const view = renderHook( () => useCropperReducer() );
772
- act( () => {
773
- view.result.current.setImage( {
774
- src: 'test.jpg',
775
- naturalWidth: 1000,
776
- naturalHeight: 500,
777
- } );
778
- } );
779
- return view;
780
- }
781
-
782
- it( 'expands a small crop to fill height and centers it', () => {
783
- const { result } = setupWithImage();
784
-
785
- // Shrink the crop rect to a small centered area.
786
- act( () => {
787
- result.current.setCropRect( {
788
- x: 0.25,
789
- y: 0.25,
790
- width: 0.5,
791
- height: 0.5,
792
- } );
793
- } );
794
-
795
- act( () => {
796
- result.current.settleCrop();
797
- } );
798
-
799
- const { cropRect } = result.current.state;
800
- // Should expand to fill height (height=1) or width (width=1).
801
- expect(
802
- Math.abs( cropRect.height - 1 ) < 0.01 ||
803
- Math.abs( cropRect.width - 1 ) < 0.01
804
- ).toBe( true );
805
- // Should be centered.
806
- const cx = cropRect.x + cropRect.width / 2;
807
- const cy = cropRect.y + cropRect.height / 2;
808
- expect( cx ).toBeCloseTo( 0.5, 1 );
809
- expect( cy ).toBeCloseTo( 0.5, 1 );
810
- } );
811
-
812
- it( 'preserves the image selection (visible content center does not jump)', () => {
813
- const { result } = setupWithImage();
814
-
815
- // Zoom in so there is room to pan.
816
- act( () => {
817
- result.current.setZoom( 3 );
818
- } );
819
-
820
- // Resize crop to a smaller centered area (center at 0.5, 0.5).
821
- act( () => {
822
- result.current.setCropRect( {
823
- x: 0.25,
824
- y: 0.25,
825
- width: 0.5,
826
- height: 0.5,
827
- } );
828
- } );
829
-
830
- // Record pre-settle state.
831
- const preCropRect = result.current.state.cropRect;
832
- const preZoom = result.current.state.zoom;
833
-
834
- act( () => {
835
- result.current.settleCrop();
836
- } );
837
-
838
- const postCropRect = result.current.state.cropRect;
839
- const postZoom = result.current.state.zoom;
840
-
841
- // The zoom should scale by the expansion factor.
842
- const s = postCropRect.height / preCropRect.height;
843
- expect( postZoom ).toBeCloseTo( preZoom * s, 1 );
844
-
845
- // The crop center should remain at (0.5, 0.5).
846
- const postCx = postCropRect.x + postCropRect.width / 2;
847
- const postCy = postCropRect.y + postCropRect.height / 2;
848
- expect( postCx ).toBeCloseTo( 0.5, 1 );
849
- expect( postCy ).toBeCloseTo( 0.5, 1 );
850
- } );
851
-
852
- it( 'is a no-op when crop is already full and centered', () => {
853
- const { result } = setupWithImage();
854
-
855
- // State is already default: full crop, centered.
856
- const stateBefore = result.current.state;
857
-
858
- act( () => {
859
- result.current.settleCrop();
860
- } );
861
-
862
- const stateAfter = result.current.state;
863
- expect( stateAfter.cropRect ).toEqual( stateBefore.cropRect );
864
- expect( stateAfter.zoom ).toEqual( stateBefore.zoom );
865
- expect( stateAfter.pan.x ).toBeCloseTo( stateBefore.pan.x, 5 );
866
- expect( stateAfter.pan.y ).toBeCloseTo( stateBefore.pan.y, 5 );
867
- } );
868
- } );
869
-
870
- describe( 'SNAP_ROTATE_90', () => {
871
- function setupWithImage() {
872
- const view = renderHook( () => useCropperReducer() );
873
- act( () => {
874
- view.result.current.setImage( {
875
- src: 'test.jpg',
876
- naturalWidth: 1000,
877
- naturalHeight: 500,
878
- } );
879
- } );
880
- return view;
881
- }
882
-
883
- it( 'after snap rotate CW, crop width and height are swapped', () => {
884
- const { result } = setupWithImage();
885
-
886
- // Set a non-square crop so the swap is visible.
887
- act( () => {
888
- result.current.setCropRect( {
889
- x: 0.1,
890
- y: 0.2,
891
- width: 0.6,
892
- height: 0.4,
893
- } );
894
- } );
895
-
896
- const widthBefore = result.current.state.cropRect.width;
897
- const heightBefore = result.current.state.cropRect.height;
898
-
899
- act( () => {
900
- result.current.snapRotate90( 1 );
901
- } );
902
-
903
- // After snap rotate, width and height should be swapped
904
- // (though enforceContainment may adjust slightly).
905
- const { cropRect } = result.current.state;
906
- expect( cropRect.width ).toBeCloseTo( heightBefore, 1 );
907
- expect( cropRect.height ).toBeCloseTo( widthBefore, 1 );
908
- expect( result.current.state.rotation ).toBe( 90 );
909
- } );
910
-
911
- it( 'after snap rotate CCW, crop width and height are swapped', () => {
912
- const { result } = setupWithImage();
913
-
914
- act( () => {
915
- result.current.setCropRect( {
916
- x: 0.1,
917
- y: 0.2,
918
- width: 0.6,
919
- height: 0.4,
920
- } );
921
- } );
922
-
923
- const widthBefore = result.current.state.cropRect.width;
924
- const heightBefore = result.current.state.cropRect.height;
925
-
926
- act( () => {
927
- result.current.snapRotate90( -1 );
928
- } );
929
-
930
- const { cropRect } = result.current.state;
931
- expect( cropRect.width ).toBeCloseTo( heightBefore, 1 );
932
- expect( cropRect.height ).toBeCloseTo( widthBefore, 1 );
933
- expect( result.current.state.rotation ).toBe( 270 );
934
- } );
935
-
936
- it( 'after two snap rotates CW (180 degrees), dimensions return to original', () => {
937
- const { result } = setupWithImage();
938
-
939
- act( () => {
940
- result.current.setCropRect( {
941
- x: 0.1,
942
- y: 0.2,
943
- width: 0.6,
944
- height: 0.4,
945
- } );
946
- } );
947
-
948
- const widthBefore = result.current.state.cropRect.width;
949
- const heightBefore = result.current.state.cropRect.height;
950
-
951
- act( () => {
952
- result.current.snapRotate90( 1 );
953
- } );
954
-
955
- act( () => {
956
- result.current.snapRotate90( 1 );
957
- } );
958
-
959
- const { cropRect } = result.current.state;
960
- expect( cropRect.width ).toBeCloseTo( widthBefore, 1 );
961
- expect( cropRect.height ).toBeCloseTo( heightBefore, 1 );
962
- expect( result.current.state.rotation ).toBe( 180 );
963
- } );
964
- } );
965
-
966
- describe( 'public controller contract', () => {
967
- it( 'does not expose the raw reducer dispatch', () => {
968
- const { result } = renderHook( () => useCropperReducer() );
969
-
970
- expect( '__dispatch' in result.current ).toBe( false );
971
- } );
972
- } );
973
-
974
- describe( 'isStateDirty coverage guard', () => {
975
- // This test ensures that isStateDirty checks ALL fields in CropperState.
976
- // If you add a field to CropperState/DEFAULT_STATE and this test fails,
977
- // you MUST also update isStateDirty() in use-cropper-reducer.ts.
978
- it( 'should track all CropperState fields', () => {
979
- // Flatten DEFAULT_STATE keys into dot-notation paths.
980
- // e.g., { pan: { x: 0, y: 0 } } → ['crop.x', 'crop.y']
981
- function flattenKeys(
982
- obj: Record< string, unknown >,
983
- prefix = ''
984
- ): string[] {
985
- const keys: string[] = [];
986
- for ( const key of Object.keys( obj ) ) {
987
- const fullKey = prefix ? `${ prefix }.${ key }` : key;
988
- const value = obj[ key ];
989
- if (
990
- value !== null &&
991
- typeof value === 'object' &&
992
- ! Array.isArray( value )
993
- ) {
994
- keys.push(
995
- ...flattenKeys(
996
- value as Record< string, unknown >,
997
- fullKey
998
- )
999
- );
1000
- } else {
1001
- keys.push( fullKey );
1002
- }
1003
- }
1004
- return keys.sort();
1005
- }
1006
-
1007
- // The fields that isStateDirty currently checks.
1008
- // Keep this in sync — if you add a field to CropperState,
1009
- // add it here AND in isStateDirty().
1010
- const dirtyCheckedFields = [
1011
- 'pan.x',
1012
- 'pan.y',
1013
- 'cropRect.height',
1014
- 'cropRect.width',
1015
- 'cropRect.x',
1016
- 'cropRect.y',
1017
- 'flip.horizontal',
1018
- 'flip.vertical',
1019
- 'rotation',
1020
- 'zoom',
1021
- ].sort();
1022
-
1023
- // 'image' is intentionally excluded — it's set once on load
1024
- // and doesn't represent a user edit.
1025
- // 'image' is set once on load.
1026
- // 'basePan', 'baseZoom', 'baseRotation' mirror the user's
1027
- // committed pose and are redundant for dirty tracking — if
1028
- // any base field differs from initial, the corresponding
1029
- // live field (crop/zoom/rotation) will also differ.
1030
- const excludedFields = [
1031
- 'image',
1032
- 'basePan',
1033
- 'baseZoom',
1034
- 'baseRotation',
1035
- ];
1036
-
1037
- const stateKeys = flattenKeys(
1038
- DEFAULT_STATE as unknown as Record< string, unknown >
1039
- ).filter(
1040
- ( key ) =>
1041
- ! excludedFields.some(
1042
- ( ex ) => key === ex || key.startsWith( ex + '.' )
1043
- )
1044
- );
1045
-
1046
- expect( stateKeys ).toEqual( dirtyCheckedFields );
1047
- } );
1048
- } );
1049
- } );