@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,876 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import type { CropperState, Size } from '../types';
3
- import { ABSOLUTE_MIN_ZOOM, DEFAULT_STATE, MAX_ZOOM } from '../constants';
4
- import { cropperReducer, enforceContainment, isStateDirty } from '../state';
5
- import {
6
- createCamera,
7
- screenToWorld,
8
- getVisibleBounds,
9
- getImageFit,
10
- } from '../camera';
11
-
12
- const IMAGE: Size = { width: 1600, height: 900 };
13
- const PORTRAIT_IMAGE: Size = { width: 900, height: 1600 };
14
- const CONTAINER: Size = { width: 800, height: 600 };
15
-
16
- function makeState( overrides: Partial< CropperState > = {} ): CropperState {
17
- const merged = {
18
- ...DEFAULT_STATE,
19
- image: {
20
- src: 'test.jpg',
21
- naturalWidth: IMAGE.width,
22
- naturalHeight: IMAGE.height,
23
- },
24
- ...overrides,
25
- };
26
- // Default base fields to the live values so tests setting `zoom`,
27
- // `crop`, or `rotation` represent a user who explicitly committed
28
- // to that pose. Tests that want to exercise base/live divergence
29
- // can set the base fields explicitly in overrides.
30
- if ( overrides.baseZoom === undefined ) {
31
- merged.baseZoom = merged.zoom;
32
- }
33
- if ( overrides.basePan === undefined ) {
34
- merged.basePan = { x: merged.pan.x, y: merged.pan.y };
35
- }
36
- if ( overrides.baseRotation === undefined ) {
37
- merged.baseRotation = merged.rotation;
38
- }
39
- return merged;
40
- }
41
-
42
- /**
43
- * Get the world-space region visible through the crop rect.
44
- *
45
- * Projects the four crop-rect corners from screen space back to
46
- * normalized world space [0,1], returning the axis-aligned bounding
47
- * box. This tells us exactly what part of the image the user sees
48
- * inside the crop area.
49
- *
50
- * @param state The cropper state to inspect.
51
- * @param imageSize The natural image dimensions.
52
- * @param containerSize The container dimensions.
53
- * @return The world-space AABB of the visible region.
54
- */
55
- function getCropWorldRegion(
56
- state: CropperState,
57
- imageSize: Size,
58
- containerSize: Size
59
- ): { minX: number; minY: number; maxX: number; maxY: number } {
60
- const camera = createCamera( state, containerSize, imageSize );
61
-
62
- // Build base camera (zero pan, zoom=1) for stencil positioning.
63
- const baseCamera = createCamera(
64
- { ...state, pan: { x: 0, y: 0 }, zoom: 1 },
65
- containerSize,
66
- imageSize
67
- );
68
- const vb = getVisibleBounds( baseCamera );
69
- const cr = state.cropRect;
70
-
71
- // Stencil corners in screen space.
72
- const corners: { x: number; y: number }[] = [
73
- { x: vb.left + cr.x * vb.width, y: vb.top + cr.y * vb.height },
74
- {
75
- x: vb.left + ( cr.x + cr.width ) * vb.width,
76
- y: vb.top + cr.y * vb.height,
77
- },
78
- {
79
- x: vb.left + ( cr.x + cr.width ) * vb.width,
80
- y: vb.top + ( cr.y + cr.height ) * vb.height,
81
- },
82
- {
83
- x: vb.left + cr.x * vb.width,
84
- y: vb.top + ( cr.y + cr.height ) * vb.height,
85
- },
86
- ];
87
-
88
- // Map to world space via inverse camera.
89
- const worldPoints = corners.map( ( c ) => screenToWorld( camera, c ) );
90
- return {
91
- minX: Math.min( ...worldPoints.map( ( p ) => p.x ) ),
92
- minY: Math.min( ...worldPoints.map( ( p ) => p.y ) ),
93
- maxX: Math.max( ...worldPoints.map( ( p ) => p.x ) ),
94
- maxY: Math.max( ...worldPoints.map( ( p ) => p.y ) ),
95
- };
96
- }
97
-
98
- describe( 'enforceContainment', () => {
99
- it( 'is nearly a no-op at default state', () => {
100
- const state = makeState();
101
- const result = enforceContainment( state );
102
- // Floating-point epsilon may introduce tiny drift (~1e-9),
103
- // so we check closeness rather than reference equality.
104
- expect( result.pan.x ).toBeCloseTo( 0, 5 );
105
- expect( result.pan.y ).toBeCloseTo( 0, 5 );
106
- expect( result.zoom ).toBeCloseTo( 1, 5 );
107
- expect( result.cropRect ).toEqual( state.cropRect );
108
- } );
109
-
110
- it( 'keeps zoom at the absolute minimum for a degenerate crop rect', () => {
111
- // A degenerate crop has coverage minimum 0; the floor must come
112
- // from ABSOLUTE_MIN_ZOOM. Use a sub-floor (but >EPSILON) zoom so
113
- // sanitization doesn't bump it to 1 before restrictPanZoom runs.
114
- const state = makeState( {
115
- cropRect: { x: 0.5, y: 0.5, width: 0, height: 0 },
116
- zoom: ABSOLUTE_MIN_ZOOM / 2,
117
- } );
118
- const result = enforceContainment( state );
119
-
120
- expect( result.zoom ).toBe( ABSOLUTE_MIN_ZOOM );
121
- } );
122
-
123
- it( 'bumps zoom before shrinking crop for small rotation', () => {
124
- const state = makeState( {
125
- rotation: 15,
126
- zoom: 1,
127
- } );
128
- const result = enforceContainment( state );
129
- // Zoom should be bumped to cover the rotated image.
130
- expect( result.zoom ).toBeGreaterThan( 1 );
131
- // Crop rect should remain full size (zoom was bumped instead of shrinking crop).
132
- expect( result.cropRect.width ).toBeCloseTo( 1, 2 );
133
- expect( result.cropRect.height ).toBeCloseTo( 1, 2 );
134
- } );
135
-
136
- it( 'returns same reference when nothing changes', () => {
137
- const state = makeState( { zoom: 2 } );
138
- const enforced = enforceContainment( state );
139
- const enforced2 = enforceContainment( enforced );
140
- expect( enforced2 ).toBe( enforced );
141
- } );
142
- } );
143
-
144
- describe( 'isStateDirty', () => {
145
- it( 'ignores floating-point noise from containment math', () => {
146
- const initial = makeState();
147
- const current = makeState( {
148
- pan: { x: initial.pan.x, y: initial.pan.y + 2e-8 },
149
- } );
150
-
151
- expect( isStateDirty( current, initial ) ).toBe( false );
152
- } );
153
-
154
- it( 'detects visible cropper state changes', () => {
155
- const initial = makeState();
156
- const current = makeState( {
157
- pan: { x: initial.pan.x, y: initial.pan.y + 0.01 },
158
- } );
159
-
160
- expect( isStateDirty( current, initial ) ).toBe( true );
161
- } );
162
- } );
163
-
164
- describe( 'cropperReducer — SETTLE_CROP', () => {
165
- /**
166
- * Assert that the world-space region visible through the crop is
167
- * the same before and after SETTLE_CROP, within a tolerance.
168
- *
169
- * @param before State before settle.
170
- * @param after State after settle.
171
- * @param tolerance Allowed difference per axis (default 0.02).
172
- */
173
- function expectSameVisibleRegion(
174
- before: CropperState,
175
- after: CropperState,
176
- tolerance = 0.02
177
- ) {
178
- const regionBefore = getCropWorldRegion( before, IMAGE, CONTAINER );
179
- const regionAfter = getCropWorldRegion( after, IMAGE, CONTAINER );
180
-
181
- expect( regionAfter.minX ).toBeCloseTo(
182
- regionBefore.minX,
183
- -Math.log10( tolerance )
184
- );
185
- expect( regionAfter.minY ).toBeCloseTo(
186
- regionBefore.minY,
187
- -Math.log10( tolerance )
188
- );
189
- expect( regionAfter.maxX ).toBeCloseTo(
190
- regionBefore.maxX,
191
- -Math.log10( tolerance )
192
- );
193
- expect( regionAfter.maxY ).toBeCloseTo(
194
- regionBefore.maxY,
195
- -Math.log10( tolerance )
196
- );
197
- }
198
-
199
- it( 'preserves image selection for a centered small crop', () => {
200
- const state = makeState( {
201
- cropRect: { x: 0.25, y: 0.25, width: 0.5, height: 0.5 },
202
- zoom: 1,
203
- pan: { x: 0, y: 0 },
204
- } );
205
- const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
206
-
207
- // Crop rect should expand to fill the visual area.
208
- expect( settled.cropRect.width ).toBeGreaterThan( 0.9 );
209
- // Zoom should increase to compensate for the larger crop.
210
- expect( settled.zoom ).toBeGreaterThan( state.zoom );
211
- // The same image region should be visible.
212
- expectSameVisibleRegion( state, settled );
213
- } );
214
-
215
- it( 'preserves image selection for an off-center crop', () => {
216
- const state = makeState( {
217
- cropRect: { x: 0.1, y: 0.1, width: 0.4, height: 0.3 },
218
- zoom: 1.5,
219
- pan: { x: 0.05, y: -0.02 },
220
- } );
221
- const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
222
-
223
- expect( settled.zoom ).toBeGreaterThan( 0 );
224
- expectSameVisibleRegion( state, settled );
225
- } );
226
-
227
- it( 'preserves image selection for a wide crop (landscape)', () => {
228
- // Wide crop: fills width, short in height.
229
- const state = makeState( {
230
- cropRect: { x: 0.0, y: 0.3, width: 1.0, height: 0.4 },
231
- zoom: 1,
232
- pan: { x: 0, y: 0 },
233
- } );
234
- const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
235
-
236
- // Aspect ratio of crop should be preserved.
237
- const ratioBefore = state.cropRect.width / state.cropRect.height;
238
- const ratioAfter = settled.cropRect.width / settled.cropRect.height;
239
- expect( ratioAfter ).toBeCloseTo( ratioBefore, 2 );
240
-
241
- expectSameVisibleRegion( state, settled );
242
- } );
243
-
244
- it( 'preserves image selection for a tall crop (portrait)', () => {
245
- const state = makeState( {
246
- cropRect: { x: 0.3, y: 0.0, width: 0.4, height: 1.0 },
247
- zoom: 1,
248
- pan: { x: 0, y: 0 },
249
- } );
250
- const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
251
-
252
- const ratioBefore = state.cropRect.width / state.cropRect.height;
253
- const ratioAfter = settled.cropRect.width / settled.cropRect.height;
254
- expect( ratioAfter ).toBeCloseTo( ratioBefore, 2 );
255
-
256
- expectSameVisibleRegion( state, settled );
257
- } );
258
-
259
- it( 'preserves image selection after zoom + off-center crop', () => {
260
- const state = makeState( {
261
- cropRect: { x: 0.05, y: 0.2, width: 0.6, height: 0.5 },
262
- zoom: 2,
263
- pan: { x: 0.1, y: -0.05 },
264
- } );
265
- const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
266
-
267
- expect( settled.zoom ).toBeGreaterThan( 0 );
268
- expectSameVisibleRegion( state, settled );
269
- } );
270
-
271
- it( 'preserves image selection with rotation', () => {
272
- // After rotation, the user resizes the crop. Settle should still
273
- // preserve what they see.
274
- const state = makeState( {
275
- cropRect: { x: 0.2, y: 0.2, width: 0.6, height: 0.6 },
276
- zoom: 1.3,
277
- rotation: 20,
278
- pan: { x: 0, y: 0 },
279
- } );
280
- const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
281
-
282
- expect( settled.zoom ).toBeGreaterThan( 0 );
283
- expectSameVisibleRegion( state, settled );
284
- } );
285
-
286
- it( 'caps zoom at MAX_ZOOM for tight crops and keeps the visible region', () => {
287
- // A 5% crop at zoom=1 would otherwise settle to zoom=20 (1/0.05),
288
- // pushing state past the user-facing zoom cap and breaking wheel
289
- // and slider interactions that clamp to MAX_ZOOM. With the cap,
290
- // the post-settle crop is smaller than the viewport but the
291
- // content the user framed is preserved.
292
- const state = makeState( {
293
- cropRect: { x: 0.4, y: 0.4, width: 0.05, height: 0.05 },
294
- zoom: 1,
295
- pan: { x: 0, y: 0 },
296
- } );
297
- const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
298
-
299
- expect( settled.zoom ).toBeCloseTo( MAX_ZOOM, 5 );
300
- // The scale was capped at MAX_ZOOM/zoom (=10), not at the uncapped
301
- // fit scale of 20 — so the new crop is 5% × 10 = 50% of the viewport,
302
- // not 100%.
303
- expect( settled.cropRect.width ).toBeCloseTo( 0.5, 5 );
304
- expect( settled.cropRect.height ).toBeCloseTo( 0.5, 5 );
305
- expectSameVisibleRegion( state, settled );
306
- } );
307
-
308
- it( 'allows a fine-rotated tall portrait crop to settle below 1x zoom', () => {
309
- const state = makeState( {
310
- image: {
311
- src: 'portrait.jpg',
312
- naturalWidth: PORTRAIT_IMAGE.width,
313
- naturalHeight: PORTRAIT_IMAGE.height,
314
- },
315
- cropRect: { x: 0.46, y: -0.02, width: 0.08, height: 1.04 },
316
- rotation: 19,
317
- zoom: 1,
318
- pan: { x: 0, y: 0 },
319
- } );
320
- const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
321
-
322
- expect( settled.cropRect.height ).toBeCloseTo( 1, 5 );
323
- expect( settled.zoom ).toBeLessThan( 1 );
324
- expect( settled.zoom ).toBeGreaterThan( 0.95 );
325
-
326
- const regionBefore = getCropWorldRegion(
327
- state,
328
- PORTRAIT_IMAGE,
329
- CONTAINER
330
- );
331
- const regionAfter = getCropWorldRegion(
332
- settled,
333
- PORTRAIT_IMAGE,
334
- CONTAINER
335
- );
336
- expect( regionAfter.minX ).toBeCloseTo( regionBefore.minX, 1 );
337
- expect( regionAfter.minY ).toBeCloseTo( regionBefore.minY, 1 );
338
- expect( regionAfter.maxX ).toBeCloseTo( regionBefore.maxX, 1 );
339
- expect( regionAfter.maxY ).toBeCloseTo( regionBefore.maxY, 1 );
340
- } );
341
-
342
- it( 'is a no-op when crop is already full-sized and centered', () => {
343
- const state = makeState( {
344
- cropRect: { x: 0, y: 0, width: 1, height: 1 },
345
- zoom: 1,
346
- pan: { x: 0, y: 0 },
347
- } );
348
- const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
349
-
350
- // Crop should remain the same.
351
- expect( settled.cropRect.x ).toBeCloseTo( 0, 5 );
352
- expect( settled.cropRect.y ).toBeCloseTo( 0, 5 );
353
- expect( settled.cropRect.width ).toBeCloseTo( 1, 5 );
354
- expect( settled.cropRect.height ).toBeCloseTo( 1, 5 );
355
- expect( settled.zoom ).toBeCloseTo( 1, 5 );
356
- } );
357
-
358
- it( 'preserves image selection across 50 random crop/zoom/pan combos', () => {
359
- for ( let i = 0; i < 50; i++ ) {
360
- // Generate deterministic-ish crop rects in the preservation
361
- // regime. Capped zoom behavior is covered by a dedicated test.
362
- const cropW = 0.4 + ( ( i * 7 ) % 6 ) / 10;
363
- const cropH = 0.4 + ( ( i * 11 ) % 6 ) / 10;
364
- const cropX = Math.min( ( ( i * 3 ) % 10 ) / 10, 1 - cropW );
365
- const cropY = Math.min( ( ( i * 5 ) % 10 ) / 10, 1 - cropH );
366
- const zoom = 1 + ( ( i * 13 ) % 12 ) / 10;
367
- const panX = ( ( ( i * 17 ) % 20 ) - 10 ) / 100;
368
- const panY = ( ( ( i * 19 ) % 20 ) - 10 ) / 100;
369
-
370
- const state = enforceContainment(
371
- makeState( {
372
- cropRect: {
373
- x: cropX,
374
- y: cropY,
375
- width: cropW,
376
- height: cropH,
377
- },
378
- zoom,
379
- pan: { x: panX, y: panY },
380
- } )
381
- );
382
-
383
- const settled = cropperReducer( state, {
384
- type: 'SETTLE_CROP',
385
- } );
386
-
387
- const expectedUncappedZoom =
388
- state.zoom /
389
- Math.max( state.cropRect.width, state.cropRect.height );
390
- expect( expectedUncappedZoom ).toBeLessThanOrEqual( MAX_ZOOM );
391
-
392
- const regionBefore = getCropWorldRegion( state, IMAGE, CONTAINER );
393
- const regionAfter = getCropWorldRegion( settled, IMAGE, CONTAINER );
394
-
395
- const dx = Math.abs( regionAfter.minX - regionBefore.minX );
396
- const dy = Math.abs( regionAfter.minY - regionBefore.minY );
397
- const dw = Math.abs(
398
- regionAfter.maxX -
399
- regionAfter.minX -
400
- ( regionBefore.maxX - regionBefore.minX )
401
- );
402
- const dh = Math.abs(
403
- regionAfter.maxY -
404
- regionAfter.minY -
405
- ( regionBefore.maxY - regionBefore.minY )
406
- );
407
-
408
- // Allow 3% tolerance — enforceContainment may nudge
409
- // things slightly when the crop is near image edges.
410
- const tol = 0.03;
411
- expect( dx ).toBeLessThanOrEqual( tol );
412
- expect( dy ).toBeLessThanOrEqual( tol );
413
- expect( dw ).toBeLessThanOrEqual( tol );
414
- expect( dh ).toBeLessThanOrEqual( tol );
415
- }
416
- } );
417
-
418
- it( 'diagonal corner drag: small crop settles accurately', () => {
419
- // Simulate dragging SE corner diagonally inward to make a small crop.
420
- // Start with full crop, drag to 30% x 30%.
421
- const afterDrag = enforceContainment(
422
- makeState( {
423
- cropRect: { x: 0.0, y: 0.0, width: 0.3, height: 0.3 },
424
- zoom: 1,
425
- pan: { x: 0, y: 0 },
426
- } )
427
- );
428
-
429
- const settled = cropperReducer( afterDrag, { type: 'SETTLE_CROP' } );
430
-
431
- // The settled crop should fill the visual area.
432
- expect(
433
- Math.max( settled.cropRect.width, settled.cropRect.height )
434
- ).toBeCloseTo( 1, 1 );
435
-
436
- // The visible image region should be preserved.
437
- const regionBefore = getCropWorldRegion( afterDrag, IMAGE, CONTAINER );
438
- const regionAfter = getCropWorldRegion( settled, IMAGE, CONTAINER );
439
-
440
- // Width/height of the visible region should match.
441
- const wBefore = regionBefore.maxX - regionBefore.minX;
442
- const hBefore = regionBefore.maxY - regionBefore.minY;
443
- const wAfter = regionAfter.maxX - regionAfter.minX;
444
- const hAfter = regionAfter.maxY - regionAfter.minY;
445
- expect( wAfter ).toBeCloseTo( wBefore, 1 );
446
- expect( hAfter ).toBeCloseTo( hBefore, 1 );
447
-
448
- // Position should match.
449
- expect( regionAfter.minX ).toBeCloseTo( regionBefore.minX, 1 );
450
- expect( regionAfter.minY ).toBeCloseTo( regionBefore.minY, 1 );
451
- } );
452
-
453
- it( 'diagonal corner drag from NW: off-center small crop settles accurately', () => {
454
- // Dragging NW corner inward — crop ends up at bottom-right.
455
- const afterDrag = enforceContainment(
456
- makeState( {
457
- cropRect: { x: 0.5, y: 0.5, width: 0.4, height: 0.4 },
458
- zoom: 1,
459
- pan: { x: 0, y: 0 },
460
- } )
461
- );
462
-
463
- const settled = cropperReducer( afterDrag, { type: 'SETTLE_CROP' } );
464
-
465
- const regionBefore = getCropWorldRegion( afterDrag, IMAGE, CONTAINER );
466
- const regionAfter = getCropWorldRegion( settled, IMAGE, CONTAINER );
467
-
468
- const wBefore = regionBefore.maxX - regionBefore.minX;
469
- const hBefore = regionBefore.maxY - regionBefore.minY;
470
- const wAfter = regionAfter.maxX - regionAfter.minX;
471
- const hAfter = regionAfter.maxY - regionAfter.minY;
472
- expect( wAfter ).toBeCloseTo( wBefore, 1 );
473
- expect( hAfter ).toBeCloseTo( hBefore, 1 );
474
- expect( regionAfter.minX ).toBeCloseTo( regionBefore.minX, 1 );
475
- expect( regionAfter.minY ).toBeCloseTo( regionBefore.minY, 1 );
476
- } );
477
-
478
- it( 'diagonal drag to very small crop does not over-zoom', () => {
479
- // Extreme case: 10% crop. Zoom should be ~10x (or capped at MAX_ZOOM).
480
- const afterDrag = enforceContainment(
481
- makeState( {
482
- cropRect: { x: 0.45, y: 0.45, width: 0.1, height: 0.1 },
483
- zoom: 1,
484
- pan: { x: 0, y: 0 },
485
- } )
486
- );
487
-
488
- const settled = cropperReducer( afterDrag, { type: 'SETTLE_CROP' } );
489
-
490
- // Zoom should be proportional to 1/0.1 = 10.
491
- // But MAX_ZOOM may cap it.
492
- expect( settled.zoom ).toBeLessThanOrEqual( 10 );
493
- expect( settled.zoom ).toBeGreaterThan( 5 );
494
-
495
- const regionBefore = getCropWorldRegion( afterDrag, IMAGE, CONTAINER );
496
- const regionAfter = getCropWorldRegion( settled, IMAGE, CONTAINER );
497
- const wBefore = regionBefore.maxX - regionBefore.minX;
498
- const wAfter = regionAfter.maxX - regionAfter.minX;
499
- expect( wAfter ).toBeCloseTo( wBefore, 1 );
500
- } );
501
-
502
- it( 'caps settle zoom for crops smaller than the maximum zoom can preserve', () => {
503
- const afterDrag = enforceContainment(
504
- makeState( {
505
- cropRect: { x: 0.475, y: 0.475, width: 0.05, height: 0.05 },
506
- zoom: 1,
507
- pan: { x: 0, y: 0 },
508
- } )
509
- );
510
-
511
- const settled = cropperReducer( afterDrag, { type: 'SETTLE_CROP' } );
512
-
513
- expect( settled.zoom ).toBe( MAX_ZOOM );
514
-
515
- const regionAfter = getCropWorldRegion( settled, IMAGE, CONTAINER );
516
- expect( regionAfter.minX ).toBeGreaterThanOrEqual( -0.001 );
517
- expect( regionAfter.minY ).toBeGreaterThanOrEqual( -0.001 );
518
- expect( regionAfter.maxX ).toBeLessThanOrEqual( 1.001 );
519
- expect( regionAfter.maxY ).toBeLessThanOrEqual( 1.001 );
520
- } );
521
- } );
522
-
523
- describe( 'cropperReducer — SET_ZOOM_AT_POINT', () => {
524
- it( 'focal-point zoom preserves the focal point position', () => {
525
- const state = makeState( { zoom: 1, pan: { x: 0, y: 0 } } );
526
- const { elementSize, visualSize } = getImageFit( CONTAINER, IMAGE, 0 );
527
-
528
- // Simulate focal point at top-left quarter of the container.
529
- const focalScreenX = CONTAINER.width * 0.25;
530
- const focalScreenY = CONTAINER.height * 0.25;
531
-
532
- // Compute what world point is under the focal point before zoom.
533
- const cameraBefore = createCamera( state, CONTAINER, elementSize );
534
- const worldBefore = screenToWorld( cameraBefore, {
535
- x: focalScreenX,
536
- y: focalScreenY,
537
- } );
538
-
539
- // Apply focal-point zoom using the same formula as the interaction controller.
540
- const newZoom = 2;
541
- const visSize = visualSize;
542
- const fx = focalScreenX - CONTAINER.width / 2;
543
- const fy = focalScreenY - CONTAINER.height / 2;
544
- const zoomRatio = 1 - newZoom / state.zoom;
545
- const focalNormX = fx / visSize.width;
546
- const focalNormY = fy / visSize.height;
547
- const newCropX = state.pan.x + ( focalNormX - state.pan.x ) * zoomRatio;
548
- const newCropY = state.pan.y + ( focalNormY - state.pan.y ) * zoomRatio;
549
-
550
- const zoomed = cropperReducer( state, {
551
- type: 'SET_ZOOM_AT_POINT',
552
- payload: { zoom: newZoom, pan: { x: newCropX, y: newCropY } },
553
- } );
554
-
555
- // Compute what world point is under the focal point after zoom.
556
- const cameraAfter = createCamera( zoomed, CONTAINER, elementSize );
557
- const worldAfter = screenToWorld( cameraAfter, {
558
- x: focalScreenX,
559
- y: focalScreenY,
560
- } );
561
-
562
- // The same world point should be under the cursor.
563
- expect( worldAfter.x ).toBeCloseTo( worldBefore.x, 1 );
564
- expect( worldAfter.y ).toBeCloseTo( worldBefore.y, 1 );
565
- } );
566
- } );
567
-
568
- describe( 'cropperReducer — SNAP_ROTATE_90', () => {
569
- it( 'swaps crop width and height around the same center', () => {
570
- const state = makeState( {
571
- cropRect: { x: 0.1, y: 0.2, width: 0.6, height: 0.4 },
572
- } );
573
- const centerBefore = {
574
- x: state.cropRect.x + state.cropRect.width / 2,
575
- y: state.cropRect.y + state.cropRect.height / 2,
576
- };
577
-
578
- const rotated = cropperReducer( state, {
579
- type: 'SNAP_ROTATE_90',
580
- payload: { direction: 1 },
581
- } );
582
-
583
- const centerAfter = {
584
- x: rotated.cropRect.x + rotated.cropRect.width / 2,
585
- y: rotated.cropRect.y + rotated.cropRect.height / 2,
586
- };
587
-
588
- // Center should be preserved.
589
- expect( centerAfter.x ).toBeCloseTo( centerBefore.x, 2 );
590
- expect( centerAfter.y ).toBeCloseTo( centerBefore.y, 2 );
591
-
592
- // Rotation should advance by 90.
593
- expect( rotated.rotation ).toBe( 90 );
594
- } );
595
-
596
- it( 'rotates pan 90° CW so framed content stays framed', () => {
597
- const state = makeState( {
598
- zoom: 2,
599
- pan: { x: 0.1, y: 0.2 },
600
- } );
601
-
602
- const rotated = cropperReducer( state, {
603
- type: 'SNAP_ROTATE_90',
604
- payload: { direction: 1 },
605
- } );
606
-
607
- // CW rotation: (px, py) → (-py, px)
608
- expect( rotated.pan.x ).toBeCloseTo( -0.2, 5 );
609
- expect( rotated.pan.y ).toBeCloseTo( 0.1, 5 );
610
- } );
611
-
612
- it( 'rotates pan 90° CCW in the other direction', () => {
613
- const state = makeState( {
614
- zoom: 2,
615
- pan: { x: 0.1, y: 0.2 },
616
- } );
617
-
618
- const rotated = cropperReducer( state, {
619
- type: 'SNAP_ROTATE_90',
620
- payload: { direction: -1 },
621
- } );
622
-
623
- // CCW rotation: (px, py) → (py, -px)
624
- expect( rotated.pan.x ).toBeCloseTo( 0.2, 5 );
625
- expect( rotated.pan.y ).toBeCloseTo( -0.1, 5 );
626
- } );
627
-
628
- it( 'four CW 90° rotations return pan to the original position', () => {
629
- const state = makeState( {
630
- zoom: 2,
631
- pan: { x: 0.15, y: -0.08 },
632
- } );
633
- let result = state;
634
- for ( let i = 0; i < 4; i++ ) {
635
- result = cropperReducer( result, {
636
- type: 'SNAP_ROTATE_90',
637
- payload: { direction: 1 },
638
- } );
639
- }
640
- expect( result.pan.x ).toBeCloseTo( state.pan.x, 5 );
641
- expect( result.pan.y ).toBeCloseTo( state.pan.y, 5 );
642
- } );
643
-
644
- it( 'single-axis flip: direction=+1 means visual CW, so rotation field decreases', () => {
645
- // `direction` is the visual rotation the caller wants. With a
646
- // single-axis flip, on-screen rotation is reversed relative to
647
- // the rotation field, so internally the field decreases by 90°
648
- // to achieve a visual CW rotation. Pan still rotates by +90° in
649
- // the visual direction to keep framing.
650
- const state = makeState( {
651
- zoom: 2,
652
- pan: { x: 0.1, y: 0.2 },
653
- rotation: 0,
654
- flip: { horizontal: true, vertical: false },
655
- } );
656
-
657
- const rotated = cropperReducer( state, {
658
- type: 'SNAP_ROTATE_90',
659
- payload: { direction: 1 },
660
- } );
661
-
662
- // Field moved 0 → 270 (i.e. -90 normalized).
663
- expect( rotated.rotation ).toBe( 270 );
664
- // Pan rotates by +90° CW: (px, py) → (-py, px).
665
- expect( rotated.pan.x ).toBeCloseTo( -0.2, 5 );
666
- expect( rotated.pan.y ).toBeCloseTo( 0.1, 5 );
667
- } );
668
-
669
- it( 'both-axis flip: rotation field advances normally (two flips cancel)', () => {
670
- const state = makeState( {
671
- zoom: 2,
672
- pan: { x: 0.1, y: 0.2 },
673
- flip: { horizontal: true, vertical: true },
674
- } );
675
-
676
- const rotated = cropperReducer( state, {
677
- type: 'SNAP_ROTATE_90',
678
- payload: { direction: 1 },
679
- } );
680
-
681
- expect( rotated.rotation ).toBe( 90 );
682
- expect( rotated.pan.x ).toBeCloseTo( -0.2, 5 );
683
- expect( rotated.pan.y ).toBeCloseTo( 0.1, 5 );
684
- } );
685
- } );
686
-
687
- describe( 'cropperReducer — SET_FLIP', () => {
688
- it( 'mirrors cropRect horizontally on horizontal flip', () => {
689
- const state = makeState( {
690
- cropRect: { x: 0.1, y: 0.2, width: 0.3, height: 0.4 },
691
- } );
692
-
693
- const flipped = cropperReducer( state, {
694
- type: 'SET_FLIP',
695
- payload: { horizontal: true, vertical: false },
696
- } );
697
-
698
- // x mirrors: newX = 1 - oldX - width
699
- expect( flipped.cropRect.x ).toBeCloseTo( 0.6, 5 );
700
- expect( flipped.cropRect.y ).toBe( 0.2 );
701
- expect( flipped.cropRect.width ).toBe( 0.3 );
702
- expect( flipped.cropRect.height ).toBe( 0.4 );
703
- } );
704
-
705
- it( 'mirrors cropRect vertically on vertical flip', () => {
706
- const state = makeState( {
707
- cropRect: { x: 0.1, y: 0.2, width: 0.3, height: 0.4 },
708
- } );
709
-
710
- const flipped = cropperReducer( state, {
711
- type: 'SET_FLIP',
712
- payload: { horizontal: false, vertical: true },
713
- } );
714
-
715
- expect( flipped.cropRect.x ).toBe( 0.1 );
716
- expect( flipped.cropRect.y ).toBeCloseTo( 0.4, 5 );
717
- expect( flipped.cropRect.width ).toBe( 0.3 );
718
- expect( flipped.cropRect.height ).toBe( 0.4 );
719
- } );
720
-
721
- it( 'mirrors pan horizontally on horizontal flip', () => {
722
- const state = makeState( {
723
- zoom: 2,
724
- pan: { x: 0.15, y: 0.07 },
725
- } );
726
-
727
- const flipped = cropperReducer( state, {
728
- type: 'SET_FLIP',
729
- payload: { horizontal: true, vertical: false },
730
- } );
731
-
732
- expect( flipped.pan.x ).toBeCloseTo( -0.15, 5 );
733
- expect( flipped.pan.y ).toBeCloseTo( 0.07, 5 );
734
- } );
735
-
736
- it( 'is its own inverse — two flips return to the original', () => {
737
- const state = makeState( {
738
- zoom: 2,
739
- pan: { x: 0.1, y: -0.05 },
740
- cropRect: { x: 0.2, y: 0.3, width: 0.5, height: 0.4 },
741
- } );
742
-
743
- const once = cropperReducer( state, {
744
- type: 'SET_FLIP',
745
- payload: { horizontal: true, vertical: true },
746
- } );
747
- const twice = cropperReducer( once, {
748
- type: 'SET_FLIP',
749
- payload: { horizontal: false, vertical: false },
750
- } );
751
-
752
- expect( twice.pan.x ).toBeCloseTo( state.pan.x, 5 );
753
- expect( twice.pan.y ).toBeCloseTo( state.pan.y, 5 );
754
- expect( twice.cropRect.x ).toBeCloseTo( state.cropRect.x, 5 );
755
- expect( twice.cropRect.y ).toBeCloseTo( state.cropRect.y, 5 );
756
- } );
757
-
758
- it( 'does nothing to cropRect or pan when flip state is unchanged', () => {
759
- const state = makeState( {
760
- zoom: 2,
761
- pan: { x: 0.1, y: 0.2 },
762
- cropRect: { x: 0.1, y: 0.2, width: 0.3, height: 0.4 },
763
- flip: { horizontal: true, vertical: false },
764
- } );
765
-
766
- const result = cropperReducer( state, {
767
- type: 'SET_FLIP',
768
- payload: { horizontal: true, vertical: false },
769
- } );
770
-
771
- expect( result.pan.x ).toBeCloseTo( 0.1, 5 );
772
- expect( result.pan.y ).toBeCloseTo( 0.2, 5 );
773
- expect( result.cropRect.x ).toBe( 0.1 );
774
- } );
775
-
776
- it( 'flip is viewport-relative — negates pan axis independent of rotation', () => {
777
- // With viewport-relative flip, a horizontal flip always negates pan.x
778
- // regardless of current rotation (same viewport axis gets mirrored).
779
- const state = makeState( {
780
- zoom: 2,
781
- pan: { x: 0.15, y: 0.07 },
782
- rotation: 90,
783
- } );
784
-
785
- const flipped = cropperReducer( state, {
786
- type: 'SET_FLIP',
787
- payload: { horizontal: true, vertical: false },
788
- } );
789
-
790
- expect( flipped.pan.x ).toBeCloseTo( -0.15, 5 );
791
- expect( flipped.pan.y ).toBeCloseTo( 0.07, 5 );
792
- } );
793
- } );
794
-
795
- describe( 'cropperReducer — SET_ROTATION', () => {
796
- it( 'rotates pan by the angle delta', () => {
797
- const state = makeState( {
798
- zoom: 2,
799
- pan: { x: 0.1, y: 0 },
800
- rotation: 0,
801
- } );
802
-
803
- // 90° rotation via SET_ROTATION: same math as SNAP_ROTATE_90
804
- // pan: (0.1, 0) → (0, 0.1) after 90° CCW in standard math
805
- // (positive rotation in our sin/cos convention)
806
- const rotated = cropperReducer( state, {
807
- type: 'SET_ROTATION',
808
- payload: 90,
809
- } );
810
-
811
- // delta = 90°; cos(90°)=0, sin(90°)=1
812
- // newX = px*0 - py*1 = 0
813
- // newY = px*1 + py*0 = 0.1
814
- expect( rotated.pan.x ).toBeCloseTo( 0, 5 );
815
- expect( rotated.pan.y ).toBeCloseTo( 0.1, 5 );
816
- } );
817
-
818
- it( 'preserves zoom (no reset)', () => {
819
- const state = makeState( { zoom: 3, rotation: 0 } );
820
- const rotated = cropperReducer( state, {
821
- type: 'SET_ROTATION',
822
- payload: 30,
823
- } );
824
- expect( rotated.zoom ).toBeGreaterThanOrEqual( 3 );
825
- } );
826
-
827
- it( 'small rotation ticks accumulate without drift', () => {
828
- // Simulate a slider going 0° → 45° in 9 ticks of 5°.
829
- // SET_ROTATION derives each tick from the fixed basePan (not
830
- // from the previous tick's potentially-clamped pan), so the
831
- // stepwise result matches a single 45° rotation exactly.
832
- const state = makeState( {
833
- zoom: 2,
834
- pan: { x: 0.1, y: 0 },
835
- } );
836
-
837
- let stepwise = state;
838
- for ( let i = 1; i <= 9; i++ ) {
839
- stepwise = cropperReducer( stepwise, {
840
- type: 'SET_ROTATION',
841
- payload: i * 5,
842
- } );
843
- }
844
-
845
- const direct = cropperReducer( state, {
846
- type: 'SET_ROTATION',
847
- payload: 45,
848
- } );
849
-
850
- expect( stepwise.pan.x ).toBeCloseTo( direct.pan.x, 5 );
851
- expect( stepwise.pan.y ).toBeCloseTo( direct.pan.y, 5 );
852
- } );
853
-
854
- it( 'rotating away and back to 0° returns to the exact base pan', () => {
855
- // Regression test for accumulated-clamp drift: rotating 0° →
856
- // through various angles → 0° should land at the original
857
- // pan, even when panned near an edge where containment would
858
- // have clamped at intermediate angles.
859
- const state = makeState( {
860
- zoom: 2,
861
- pan: { x: 0.1, y: -0.05 },
862
- } );
863
-
864
- let result = state;
865
- for ( const angle of [ 10, 25, 40, 45, 30, 15, 0 ] ) {
866
- result = cropperReducer( result, {
867
- type: 'SET_ROTATION',
868
- payload: angle,
869
- } );
870
- }
871
-
872
- expect( result.pan.x ).toBeCloseTo( state.pan.x, 5 );
873
- expect( result.pan.y ).toBeCloseTo( state.pan.y, 5 );
874
- expect( result.zoom ).toBeCloseTo( state.zoom, 5 );
875
- } );
876
- } );