@wordpress/media-editor 0.9.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (322) hide show
  1. package/build/components/media-editor/index.cjs +212 -170
  2. package/build/components/media-editor/index.cjs.map +3 -3
  3. package/build/components/media-editor/use-crop-options.cjs +63 -0
  4. package/build/components/media-editor/use-crop-options.cjs.map +7 -0
  5. package/build/components/media-editor/use-save-media-editor.cjs +185 -0
  6. package/build/components/media-editor/use-save-media-editor.cjs.map +7 -0
  7. package/build/components/media-editor-canvas/index.cjs +33 -12
  8. package/build/components/media-editor-canvas/index.cjs.map +2 -2
  9. package/build/components/media-editor-crop-panel/index.cjs +70 -84
  10. package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
  11. package/build/components/media-editor-fine-rotation/index.cjs +81 -0
  12. package/build/components/media-editor-fine-rotation/index.cjs.map +7 -0
  13. package/build/components/media-editor-modal/index.cjs +63 -16
  14. package/build/components/media-editor-modal/index.cjs.map +3 -3
  15. package/build/components/media-editor-transform-controls/index.cjs +84 -0
  16. package/build/components/media-editor-transform-controls/index.cjs.map +7 -0
  17. package/build/components/media-form/index.cjs +19 -1
  18. package/build/components/media-form/index.cjs.map +3 -3
  19. package/build/components/media-form/sidebar-datetime-view.cjs +53 -0
  20. package/build/components/media-form/sidebar-datetime-view.cjs.map +7 -0
  21. package/build/components/rotation-ruler/index.cjs +8 -2
  22. package/build/components/rotation-ruler/index.cjs.map +2 -2
  23. package/build/hooks/use-crop-gesture-handlers.cjs +41 -4
  24. package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
  25. package/build/image-editor/core/camera.cjs +29 -11
  26. package/build/image-editor/core/camera.cjs.map +2 -2
  27. package/build/image-editor/core/constants.cjs +6 -0
  28. package/build/image-editor/core/constants.cjs.map +2 -2
  29. package/build/image-editor/core/containment.cjs +71 -34
  30. package/build/image-editor/core/containment.cjs.map +2 -2
  31. package/build/image-editor/core/crop-rect.cjs +48 -0
  32. package/build/image-editor/core/crop-rect.cjs.map +7 -0
  33. package/build/image-editor/core/index.cjs +12 -0
  34. package/build/image-editor/core/index.cjs.map +2 -2
  35. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  36. package/build/image-editor/core/math/sanitize.cjs +92 -0
  37. package/build/image-editor/core/math/sanitize.cjs.map +7 -0
  38. package/build/image-editor/core/setter-helpers.cjs +64 -0
  39. package/build/image-editor/core/setter-helpers.cjs.map +7 -0
  40. package/build/image-editor/core/source-region.cjs +16 -14
  41. package/build/image-editor/core/source-region.cjs.map +2 -2
  42. package/build/image-editor/core/state.cjs +20 -10
  43. package/build/image-editor/core/state.cjs.map +2 -2
  44. package/build/image-editor/core/stencil-math.cjs +31 -20
  45. package/build/image-editor/core/stencil-math.cjs.map +2 -2
  46. package/build/image-editor/core/transform-style.cjs +12 -6
  47. package/build/image-editor/core/transform-style.cjs.map +2 -2
  48. package/build/image-editor/core/types.cjs.map +1 -1
  49. package/build/image-editor/index.cjs +12 -2
  50. package/build/image-editor/index.cjs.map +2 -2
  51. package/build/image-editor/react/components/cropper-provider.cjs +2 -2
  52. package/build/image-editor/react/components/cropper-provider.cjs.map +2 -2
  53. package/build/image-editor/react/components/cropper.cjs +120 -47
  54. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  55. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +172 -0
  56. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +7 -0
  57. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +19 -9
  58. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  59. package/build/image-editor/react/hooks/build-cropper-setters.cjs +71 -0
  60. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +7 -0
  61. package/build/image-editor/react/hooks/index.cjs +6 -3
  62. package/build/image-editor/react/hooks/index.cjs.map +2 -2
  63. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +113 -0
  64. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +7 -0
  65. package/build/state/composite-reducer.cjs +103 -0
  66. package/build/state/composite-reducer.cjs.map +7 -0
  67. package/build/state/index.cjs +48 -0
  68. package/build/state/index.cjs.map +7 -0
  69. package/build/state/media-editor-state-provider.cjs +58 -0
  70. package/build/state/media-editor-state-provider.cjs.map +7 -0
  71. package/build/state/types.cjs +33 -0
  72. package/build/state/types.cjs.map +7 -0
  73. package/build/state/use-media-editor-state.cjs +287 -0
  74. package/build/state/use-media-editor-state.cjs.map +7 -0
  75. package/build/store/actions.cjs +4 -2
  76. package/build/store/actions.cjs.map +2 -2
  77. package/build/store/reducer.cjs +5 -3
  78. package/build/store/reducer.cjs.map +2 -2
  79. package/build/store/selectors.cjs +5 -0
  80. package/build/store/selectors.cjs.map +2 -2
  81. package/build-module/components/media-editor/index.mjs +224 -177
  82. package/build-module/components/media-editor/index.mjs.map +2 -2
  83. package/build-module/components/media-editor/use-crop-options.mjs +37 -0
  84. package/build-module/components/media-editor/use-crop-options.mjs.map +7 -0
  85. package/build-module/components/media-editor/use-save-media-editor.mjs +151 -0
  86. package/build-module/components/media-editor/use-save-media-editor.mjs.map +7 -0
  87. package/build-module/components/media-editor-canvas/index.mjs +34 -13
  88. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  89. package/build-module/components/media-editor-crop-panel/index.mjs +73 -89
  90. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  91. package/build-module/components/media-editor-fine-rotation/index.mjs +50 -0
  92. package/build-module/components/media-editor-fine-rotation/index.mjs.map +7 -0
  93. package/build-module/components/media-editor-modal/index.mjs +64 -17
  94. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  95. package/build-module/components/media-editor-transform-controls/index.mjs +68 -0
  96. package/build-module/components/media-editor-transform-controls/index.mjs.map +7 -0
  97. package/build-module/components/media-form/index.mjs +9 -1
  98. package/build-module/components/media-form/index.mjs.map +2 -2
  99. package/build-module/components/media-form/sidebar-datetime-view.mjs +32 -0
  100. package/build-module/components/media-form/sidebar-datetime-view.mjs.map +7 -0
  101. package/build-module/components/rotation-ruler/index.mjs +8 -2
  102. package/build-module/components/rotation-ruler/index.mjs.map +2 -2
  103. package/build-module/hooks/use-crop-gesture-handlers.mjs +41 -4
  104. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
  105. package/build-module/image-editor/core/camera.mjs +34 -11
  106. package/build-module/image-editor/core/camera.mjs.map +2 -2
  107. package/build-module/image-editor/core/constants.mjs +4 -0
  108. package/build-module/image-editor/core/constants.mjs.map +2 -2
  109. package/build-module/image-editor/core/containment.mjs +75 -34
  110. package/build-module/image-editor/core/containment.mjs.map +2 -2
  111. package/build-module/image-editor/core/crop-rect.mjs +23 -0
  112. package/build-module/image-editor/core/crop-rect.mjs.map +7 -0
  113. package/build-module/image-editor/core/index.mjs +12 -0
  114. package/build-module/image-editor/core/index.mjs.map +2 -2
  115. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  116. package/build-module/image-editor/core/math/sanitize.mjs +63 -0
  117. package/build-module/image-editor/core/math/sanitize.mjs.map +7 -0
  118. package/build-module/image-editor/core/setter-helpers.mjs +39 -0
  119. package/build-module/image-editor/core/setter-helpers.mjs.map +7 -0
  120. package/build-module/image-editor/core/source-region.mjs +16 -14
  121. package/build-module/image-editor/core/source-region.mjs.map +2 -2
  122. package/build-module/image-editor/core/state.mjs +25 -11
  123. package/build-module/image-editor/core/state.mjs.map +2 -2
  124. package/build-module/image-editor/core/stencil-math.mjs +30 -19
  125. package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
  126. package/build-module/image-editor/core/transform-style.mjs +12 -6
  127. package/build-module/image-editor/core/transform-style.mjs.map +2 -2
  128. package/build-module/image-editor/index.mjs +11 -2
  129. package/build-module/image-editor/index.mjs.map +2 -2
  130. package/build-module/image-editor/react/components/cropper-provider.mjs +3 -3
  131. package/build-module/image-editor/react/components/cropper-provider.mjs.map +2 -2
  132. package/build-module/image-editor/react/components/cropper.mjs +121 -48
  133. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  134. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +147 -0
  135. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +7 -0
  136. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +19 -9
  137. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  138. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +46 -0
  139. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +7 -0
  140. package/build-module/image-editor/react/hooks/index.mjs +4 -2
  141. package/build-module/image-editor/react/hooks/index.mjs.map +2 -2
  142. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +100 -0
  143. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +7 -0
  144. package/build-module/state/composite-reducer.mjs +82 -0
  145. package/build-module/state/composite-reducer.mjs.map +7 -0
  146. package/build-module/state/index.mjs +26 -0
  147. package/build-module/state/index.mjs.map +7 -0
  148. package/build-module/state/media-editor-state-provider.mjs +34 -0
  149. package/build-module/state/media-editor-state-provider.mjs.map +7 -0
  150. package/build-module/state/types.mjs +8 -0
  151. package/build-module/state/types.mjs.map +7 -0
  152. package/build-module/state/use-media-editor-state.mjs +278 -0
  153. package/build-module/state/use-media-editor-state.mjs.map +7 -0
  154. package/build-module/store/actions.mjs +4 -2
  155. package/build-module/store/actions.mjs.map +2 -2
  156. package/build-module/store/reducer.mjs +5 -3
  157. package/build-module/store/reducer.mjs.map +2 -2
  158. package/build-module/store/selectors.mjs +4 -0
  159. package/build-module/store/selectors.mjs.map +2 -2
  160. package/build-style/style-rtl.css +97 -44
  161. package/build-style/style.css +97 -44
  162. package/build-types/components/media-editor/index.d.ts +14 -5
  163. package/build-types/components/media-editor/index.d.ts.map +1 -1
  164. package/build-types/components/media-editor/use-crop-options.d.ts +35 -0
  165. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -0
  166. package/build-types/components/media-editor/use-save-media-editor.d.ts +29 -0
  167. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -0
  168. package/build-types/components/media-editor-canvas/index.d.ts +5 -10
  169. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  170. package/build-types/components/media-editor-crop-panel/index.d.ts +6 -25
  171. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  172. package/build-types/components/media-editor-fine-rotation/index.d.ts +14 -0
  173. package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -0
  174. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  175. package/build-types/components/media-editor-transform-controls/index.d.ts +8 -0
  176. package/build-types/components/media-editor-transform-controls/index.d.ts.map +1 -0
  177. package/build-types/components/media-form/index.d.ts.map +1 -1
  178. package/build-types/components/media-form/sidebar-datetime-view.d.ts +10 -0
  179. package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +1 -0
  180. package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
  181. package/build-types/hooks/use-crop-gesture-handlers.d.ts +13 -14
  182. package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
  183. package/build-types/image-editor/core/camera.d.ts.map +1 -1
  184. package/build-types/image-editor/core/constants.d.ts +18 -0
  185. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  186. package/build-types/image-editor/core/containment.d.ts +10 -0
  187. package/build-types/image-editor/core/containment.d.ts.map +1 -1
  188. package/build-types/image-editor/core/crop-rect.d.ts +19 -0
  189. package/build-types/image-editor/core/crop-rect.d.ts.map +1 -0
  190. package/build-types/image-editor/core/index.d.ts +3 -1
  191. package/build-types/image-editor/core/index.d.ts.map +1 -1
  192. package/build-types/image-editor/core/interaction-controller.d.ts +5 -1
  193. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  194. package/build-types/image-editor/core/math/sanitize.d.ts +70 -0
  195. package/build-types/image-editor/core/math/sanitize.d.ts.map +1 -0
  196. package/build-types/image-editor/core/setter-helpers.d.ts +24 -0
  197. package/build-types/image-editor/core/setter-helpers.d.ts.map +1 -0
  198. package/build-types/image-editor/core/source-region.d.ts.map +1 -1
  199. package/build-types/image-editor/core/state.d.ts +12 -0
  200. package/build-types/image-editor/core/state.d.ts.map +1 -1
  201. package/build-types/image-editor/core/stencil-math.d.ts +23 -18
  202. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
  203. package/build-types/image-editor/core/transform-style.d.ts.map +1 -1
  204. package/build-types/image-editor/core/types.d.ts +23 -4
  205. package/build-types/image-editor/core/types.d.ts.map +1 -1
  206. package/build-types/image-editor/index.d.ts +4 -4
  207. package/build-types/image-editor/index.d.ts.map +1 -1
  208. package/build-types/image-editor/react/components/cropper-provider.d.ts +11 -7
  209. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
  210. package/build-types/image-editor/react/components/cropper.d.ts +6 -4
  211. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  212. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +36 -0
  213. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +1 -0
  214. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +2 -1
  215. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  216. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +47 -0
  217. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +1 -0
  218. package/build-types/image-editor/react/hooks/index.d.ts +4 -2
  219. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
  220. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts +58 -0
  221. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +1 -0
  222. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
  223. package/build-types/state/composite-reducer.d.ts +39 -0
  224. package/build-types/state/composite-reducer.d.ts.map +1 -0
  225. package/build-types/state/index.d.ts +7 -0
  226. package/build-types/state/index.d.ts.map +1 -0
  227. package/build-types/state/media-editor-state-provider.d.ts +34 -0
  228. package/build-types/state/media-editor-state-provider.d.ts.map +1 -0
  229. package/build-types/state/types.d.ts +96 -0
  230. package/build-types/state/types.d.ts.map +1 -0
  231. package/build-types/state/use-media-editor-state.d.ts +101 -0
  232. package/build-types/state/use-media-editor-state.d.ts.map +1 -0
  233. package/build-types/store/actions.d.ts +3 -1
  234. package/build-types/store/actions.d.ts.map +1 -1
  235. package/build-types/store/reducer.d.ts +3 -0
  236. package/build-types/store/reducer.d.ts.map +1 -1
  237. package/build-types/store/selectors.d.ts +1 -0
  238. package/build-types/store/selectors.d.ts.map +1 -1
  239. package/package.json +19 -16
  240. package/src/components/media-editor/index.tsx +262 -229
  241. package/src/components/media-editor/style.scss +0 -9
  242. package/src/components/media-editor/test/use-crop-options.tsx +110 -0
  243. package/src/components/media-editor/use-crop-options.ts +79 -0
  244. package/src/components/media-editor/use-save-media-editor.ts +214 -0
  245. package/src/components/media-editor-canvas/index.tsx +57 -25
  246. package/src/components/media-editor-crop-panel/index.tsx +53 -87
  247. package/src/components/media-editor-crop-panel/test/index.tsx +46 -53
  248. package/src/components/media-editor-fine-rotation/index.tsx +74 -0
  249. package/src/components/media-editor-modal/index.tsx +70 -16
  250. package/src/components/media-editor-modal/style.scss +80 -0
  251. package/src/components/media-editor-modal/test/build-modifiers.test.ts +14 -0
  252. package/src/components/media-editor-modal/test/index.tsx +146 -0
  253. package/src/components/media-editor-transform-controls/index.tsx +70 -0
  254. package/src/components/media-editor-transform-controls/style.scss +8 -0
  255. package/src/components/media-form/index.tsx +15 -1
  256. package/src/components/media-form/sidebar-datetime-view.tsx +47 -0
  257. package/src/components/media-form/test/sidebar-datetime-view.test.tsx +96 -0
  258. package/src/components/rotation-ruler/README.md +11 -25
  259. package/src/components/rotation-ruler/index.tsx +17 -2
  260. package/src/components/rotation-ruler/style.scss +49 -13
  261. package/src/hooks/use-crop-gesture-handlers.ts +64 -17
  262. package/src/image-editor/README.md +28 -129
  263. package/src/image-editor/core/camera.ts +51 -26
  264. package/src/image-editor/core/constants.ts +19 -0
  265. package/src/image-editor/core/containment.ts +102 -49
  266. package/src/image-editor/core/crop-rect.ts +45 -0
  267. package/src/image-editor/core/index.ts +16 -4
  268. package/src/image-editor/core/interaction-controller.ts +5 -1
  269. package/src/image-editor/core/math/sanitize.ts +158 -0
  270. package/src/image-editor/core/math/test/sanitize.ts +244 -0
  271. package/src/image-editor/core/setter-helpers.ts +66 -0
  272. package/src/image-editor/core/source-region.ts +21 -14
  273. package/src/image-editor/core/state.ts +67 -15
  274. package/src/image-editor/core/stencil-math.ts +55 -35
  275. package/src/image-editor/core/test/camera.ts +367 -1
  276. package/src/image-editor/core/test/state.ts +101 -8
  277. package/src/image-editor/core/test/stencil-math.ts +193 -0
  278. package/src/image-editor/core/transform-style.ts +21 -6
  279. package/src/image-editor/core/types.ts +24 -4
  280. package/src/image-editor/docs/architecture.md +48 -89
  281. package/src/image-editor/docs/recipes.md +123 -659
  282. package/src/image-editor/index.ts +12 -3
  283. package/src/image-editor/react/components/cropper-provider.tsx +16 -12
  284. package/src/image-editor/react/components/cropper.scss +23 -4
  285. package/src/image-editor/react/components/cropper.tsx +194 -89
  286. package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +269 -0
  287. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +19 -8
  288. package/src/image-editor/react/components/test/cropper.tsx +55 -13
  289. package/src/image-editor/react/hooks/build-cropper-setters.ts +105 -0
  290. package/src/image-editor/react/hooks/index.ts +4 -2
  291. package/src/image-editor/react/hooks/test/{use-cropper-state.ts → use-cropper-reducer.ts} +142 -286
  292. package/src/image-editor/react/hooks/use-cropper-reducer.ts +204 -0
  293. package/src/image-editor/stories/rectangle-crop.story.tsx +7 -17
  294. package/src/state/composite-reducer.ts +137 -0
  295. package/src/state/index.ts +20 -0
  296. package/src/state/media-editor-state-provider.tsx +70 -0
  297. package/src/state/test/use-media-editor-state.ts +330 -0
  298. package/src/state/types.ts +101 -0
  299. package/src/state/use-media-editor-state.ts +472 -0
  300. package/src/store/actions.ts +3 -0
  301. package/src/store/reducer.ts +6 -1
  302. package/src/store/selectors.ts +4 -0
  303. package/src/store/test/reducer.ts +9 -0
  304. package/src/style.scss +1 -1
  305. package/tsconfig.json +3 -0
  306. package/tsconfig.tsbuildinfo +1 -1
  307. package/build/components/media-editor-toolbar/index.cjs +0 -214
  308. package/build/components/media-editor-toolbar/index.cjs.map +0 -7
  309. package/build/image-editor/react/hooks/use-cropper-state.cjs +0 -277
  310. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +0 -7
  311. package/build-module/components/media-editor-toolbar/index.mjs +0 -190
  312. package/build-module/components/media-editor-toolbar/index.mjs.map +0 -7
  313. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +0 -262
  314. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +0 -7
  315. package/build-types/components/media-editor-toolbar/index.d.ts +0 -24
  316. package/build-types/components/media-editor-toolbar/index.d.ts.map +0 -1
  317. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -97
  318. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
  319. package/src/components/media-editor-toolbar/index.tsx +0 -209
  320. package/src/components/media-editor-toolbar/style.scss +0 -49
  321. package/src/image-editor/docs/roadmap.md +0 -66
  322. package/src/image-editor/react/hooks/use-cropper-state.ts +0 -457
@@ -0,0 +1,244 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import {
5
+ isValidSize,
6
+ safeBoundedNumber,
7
+ sanitizeCropperState,
8
+ sanitizeRect,
9
+ } from '../sanitize';
10
+ import { DEFAULT_STATE } from '../../constants';
11
+ import type { CropperState } from '../../types';
12
+
13
+ describe( 'safeBoundedNumber', () => {
14
+ it( 'returns the value when it is finite and in range', () => {
15
+ expect( safeBoundedNumber( 42, 0 ) ).toBe( 42 );
16
+ expect( safeBoundedNumber( -3.14, 0 ) ).toBe( -3.14 );
17
+ expect( safeBoundedNumber( 0, 1 ) ).toBe( 0 );
18
+ } );
19
+
20
+ it( 'returns the fallback for NaN', () => {
21
+ expect( safeBoundedNumber( Number.NaN, 7 ) ).toBe( 7 );
22
+ } );
23
+
24
+ it( 'returns the fallback for ±Infinity', () => {
25
+ expect( safeBoundedNumber( Number.POSITIVE_INFINITY, 7 ) ).toBe( 7 );
26
+ expect( safeBoundedNumber( Number.NEGATIVE_INFINITY, 7 ) ).toBe( 7 );
27
+ } );
28
+
29
+ it( 'returns the fallback for finite values beyond the safe magnitude', () => {
30
+ // MAX_VALUE is technically finite but multiplying it through trig
31
+ // and matrix code overflows to Infinity.
32
+ expect( safeBoundedNumber( Number.MAX_VALUE, 1 ) ).toBe( 1 );
33
+ expect( safeBoundedNumber( -Number.MAX_VALUE, 1 ) ).toBe( 1 );
34
+ expect( safeBoundedNumber( 1e10, 1 ) ).toBe( 1 );
35
+ } );
36
+
37
+ it( 'accepts values right at the safe magnitude boundary', () => {
38
+ expect( safeBoundedNumber( 1e6, 0 ) ).toBe( 1e6 );
39
+ expect( safeBoundedNumber( -1e6, 0 ) ).toBe( -1e6 );
40
+ } );
41
+
42
+ it( 'accepts sub-normal values (sub-normal protection lives elsewhere)', () => {
43
+ // Sub-normals don't overflow on their own; they only cause trouble
44
+ // when used as divisors. That guard lives in sanitizeCropperState's
45
+ // zoom check, not here.
46
+ expect( safeBoundedNumber( Number.MIN_VALUE, 0 ) ).toBe(
47
+ Number.MIN_VALUE
48
+ );
49
+ } );
50
+ } );
51
+
52
+ describe( 'isValidSize', () => {
53
+ it( 'returns true for finite, positive, in-range sizes', () => {
54
+ expect( isValidSize( { width: 100, height: 100 } ) ).toBe( true );
55
+ expect( isValidSize( { width: 1, height: 1 } ) ).toBe( true );
56
+ expect( isValidSize( { width: 1e6, height: 1e6 } ) ).toBe( true );
57
+ } );
58
+
59
+ it( 'returns false for zero or negative dimensions', () => {
60
+ expect( isValidSize( { width: 0, height: 100 } ) ).toBe( false );
61
+ expect( isValidSize( { width: 100, height: 0 } ) ).toBe( false );
62
+ expect( isValidSize( { width: -1, height: 100 } ) ).toBe( false );
63
+ } );
64
+
65
+ it( 'returns false for non-finite dimensions', () => {
66
+ expect( isValidSize( { width: Number.NaN, height: 100 } ) ).toBe(
67
+ false
68
+ );
69
+ expect(
70
+ isValidSize( {
71
+ width: Number.POSITIVE_INFINITY,
72
+ height: 100,
73
+ } )
74
+ ).toBe( false );
75
+ } );
76
+
77
+ it( 'returns false for dimensions beyond the safe magnitude', () => {
78
+ expect( isValidSize( { width: Number.MAX_VALUE, height: 100 } ) ).toBe(
79
+ false
80
+ );
81
+ expect( isValidSize( { width: 1e10, height: 100 } ) ).toBe( false );
82
+ } );
83
+
84
+ it( 'returns false for sub-normal dimensions (overflow when divided into)', () => {
85
+ // Number.MIN_VALUE passes `> 0` but `container / MIN_VALUE`
86
+ // overflows to Infinity downstream in getImageFit / createCamera.
87
+ expect( isValidSize( { width: Number.MIN_VALUE, height: 100 } ) ).toBe(
88
+ false
89
+ );
90
+ expect( isValidSize( { width: 100, height: Number.MIN_VALUE } ) ).toBe(
91
+ false
92
+ );
93
+ } );
94
+ } );
95
+
96
+ describe( 'sanitizeRect', () => {
97
+ it( 'replaces non-finite fields with 0', () => {
98
+ expect(
99
+ sanitizeRect( {
100
+ x: Number.NaN,
101
+ y: 0.1,
102
+ width: Number.POSITIVE_INFINITY,
103
+ height: 0.5,
104
+ } )
105
+ ).toEqual( { x: 0, y: 0.1, width: 0, height: 0.5 } );
106
+ } );
107
+
108
+ it( 'leaves a clean rect unchanged', () => {
109
+ expect(
110
+ sanitizeRect( { x: 0.1, y: 0.2, width: 0.5, height: 0.3 } )
111
+ ).toEqual( { x: 0.1, y: 0.2, width: 0.5, height: 0.3 } );
112
+ } );
113
+ } );
114
+
115
+ describe( 'sanitizeCropperState', () => {
116
+ function makeBaseState(
117
+ overrides: Partial< CropperState > = {}
118
+ ): CropperState {
119
+ return {
120
+ ...DEFAULT_STATE,
121
+ ...overrides,
122
+ };
123
+ }
124
+
125
+ it( 'leaves a clean state unchanged', () => {
126
+ const state = makeBaseState( {
127
+ pan: { x: 0.1, y: -0.05 },
128
+ zoom: 1.5,
129
+ rotation: 30,
130
+ } );
131
+ const out = sanitizeCropperState( state );
132
+ expect( out.pan ).toEqual( { x: 0.1, y: -0.05 } );
133
+ expect( out.zoom ).toBe( 1.5 );
134
+ expect( out.rotation ).toBe( 30 );
135
+ } );
136
+
137
+ it( 'replaces NaN pan with zero', () => {
138
+ const state = makeBaseState( {
139
+ pan: { x: Number.NaN, y: Number.NaN },
140
+ } );
141
+ const out = sanitizeCropperState( state );
142
+ expect( out.pan ).toEqual( { x: 0, y: 0 } );
143
+ } );
144
+
145
+ it( 'replaces non-finite zoom with 1 (identity)', () => {
146
+ expect(
147
+ sanitizeCropperState( makeBaseState( { zoom: Number.NaN } ) ).zoom
148
+ ).toBe( 1 );
149
+ expect(
150
+ sanitizeCropperState(
151
+ makeBaseState( { zoom: Number.POSITIVE_INFINITY } )
152
+ ).zoom
153
+ ).toBe( 1 );
154
+ expect(
155
+ sanitizeCropperState( makeBaseState( { zoom: -2 } ) ).zoom
156
+ ).toBe( 1 );
157
+ } );
158
+
159
+ it( 'replaces sub-normal zoom with 1 (prevents division explosion)', () => {
160
+ expect(
161
+ sanitizeCropperState( makeBaseState( { zoom: Number.MIN_VALUE } ) )
162
+ .zoom
163
+ ).toBe( 1 );
164
+ } );
165
+
166
+ it( 'replaces extreme rotation magnitudes with 0', () => {
167
+ expect(
168
+ sanitizeCropperState(
169
+ makeBaseState( { rotation: Number.MAX_VALUE } )
170
+ ).rotation
171
+ ).toBe( 0 );
172
+ expect(
173
+ sanitizeCropperState( makeBaseState( { rotation: Number.NaN } ) )
174
+ .rotation
175
+ ).toBe( 0 );
176
+ } );
177
+
178
+ it( 'sanitizes cropRect fields independently', () => {
179
+ const state = makeBaseState( {
180
+ cropRect: {
181
+ x: Number.NaN,
182
+ y: 0.1,
183
+ width: Number.POSITIVE_INFINITY,
184
+ height: 0.5,
185
+ },
186
+ } );
187
+ const out = sanitizeCropperState( state );
188
+ expect( out.cropRect ).toEqual( {
189
+ x: 0,
190
+ y: 0.1,
191
+ width: 0,
192
+ height: 0.5,
193
+ } );
194
+ } );
195
+
196
+ it( 'sanitizes base pose fields (basePan, baseZoom, baseRotation)', () => {
197
+ const state = makeBaseState( {
198
+ basePan: { x: Number.NaN, y: 0 },
199
+ baseZoom: Number.NEGATIVE_INFINITY,
200
+ baseRotation: Number.MAX_VALUE,
201
+ } );
202
+ const out = sanitizeCropperState( state );
203
+ expect( out.basePan ).toEqual( { x: 0, y: 0 } );
204
+ expect( out.baseZoom ).toBe( 1 );
205
+ expect( out.baseRotation ).toBe( 0 );
206
+ } );
207
+
208
+ it( 'returns the same reference when no field needs substitution', () => {
209
+ // Called once per pointermove (via restrictPanZoom + createCamera),
210
+ // so the no-change fast path matters. Clean state → same reference,
211
+ // no per-frame GC churn.
212
+ const state = makeBaseState( {
213
+ pan: { x: 0.1, y: -0.05 },
214
+ zoom: 1.5,
215
+ rotation: 30,
216
+ } );
217
+ expect( sanitizeCropperState( state ) ).toBe( state );
218
+ } );
219
+
220
+ it( 'preserves sub-object references when only some fields change', () => {
221
+ // If pan is clean but zoom is corrupted, pan should keep its
222
+ // reference even though the parent state must be a new object.
223
+ const pan = { x: 0.1, y: -0.05 };
224
+ const state = makeBaseState( { pan, zoom: Number.NaN } );
225
+ const out = sanitizeCropperState( state );
226
+ expect( out ).not.toBe( state );
227
+ expect( out.pan ).toBe( pan );
228
+ expect( out.zoom ).toBe( 1 );
229
+ } );
230
+
231
+ it( 'leaves state.image untouched (caller passes imageSize separately)', () => {
232
+ // The math layer never reads state.image — image dimensions arrive as
233
+ // a separate `imageSize` argument. Cloning state.image on every call
234
+ // would break the reducer's reference-equality short-circuit.
235
+ const image = {
236
+ src: 'test.jpg',
237
+ naturalWidth: 1600,
238
+ naturalHeight: 900,
239
+ };
240
+ const state = makeBaseState( { image } );
241
+ const out = sanitizeCropperState( state );
242
+ expect( out.image ).toBe( image );
243
+ } );
244
+ } );
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import type { CropperState, NormalizedPoint, CropperAction } from './types';
5
+ import { MAX_ZOOM } from './constants';
6
+ import { getMinZoom, restrictPanZoom } from './containment';
7
+
8
+ /**
9
+ * Build the reducer action that produces a "cursorless" zoom — zoom
10
+ * anchored at the current crop-rect center, with pan clamped to keep
11
+ * the image covering the crop.
12
+ *
13
+ * The slider, the `+`/`-` keys, and the toolbar buttons have no
14
+ * natural focal point (unlike wheel/pinch zoom), so they default to
15
+ * the crop center. Returns `null` when the clamped zoom matches the
16
+ * current zoom (no-op).
17
+ *
18
+ * Extracted so multiple hooks (the pure `useCropperReducer` and the
19
+ * composite `useMediaEditorState`) can share the math without
20
+ * duplicating it.
21
+ *
22
+ * @param state The current cropper state.
23
+ * @param requestedZoom The zoom value the caller asked for.
24
+ * @return A `SET_ZOOM_AT_POINT` action, or `null` if no change is required.
25
+ */
26
+ export function buildFocalPointZoomAction(
27
+ state: CropperState,
28
+ requestedZoom: number
29
+ ): CropperAction | null {
30
+ const clampedZoom = Math.min(
31
+ MAX_ZOOM,
32
+ Math.max( getMinZoom( state ), requestedZoom )
33
+ );
34
+ if ( clampedZoom === state.zoom ) {
35
+ return null;
36
+ }
37
+ // Crop center expressed in the same coord system as `state.pan`:
38
+ // container-center-relative, normalized by image rendered size.
39
+ // `cropRect` is image-top-left-origin (0–1), so subtract 0.5
40
+ // to recenter.
41
+ const { cropRect } = state;
42
+ const focalNormX = cropRect.x + cropRect.width / 2 - 0.5;
43
+ const focalNormY = cropRect.y + cropRect.height / 2 - 0.5;
44
+ const zoomRatio = 1 - clampedZoom / state.zoom;
45
+ const newPanX = state.pan.x + ( focalNormX - state.pan.x ) * zoomRatio;
46
+ const newPanY = state.pan.y + ( focalNormY - state.pan.y ) * zoomRatio;
47
+ const imageSize: { width: number; height: number } = state.image
48
+ ? {
49
+ width: state.image.naturalWidth,
50
+ height: state.image.naturalHeight,
51
+ }
52
+ : { width: 1, height: 1 };
53
+ const { pan: clampedPan }: { pan: NormalizedPoint } = restrictPanZoom(
54
+ {
55
+ ...state,
56
+ zoom: clampedZoom,
57
+ pan: { x: newPanX, y: newPanY },
58
+ },
59
+ imageSize,
60
+ state.cropRect
61
+ );
62
+ return {
63
+ type: 'SET_ZOOM_AT_POINT',
64
+ payload: { zoom: clampedZoom, pan: clampedPan },
65
+ };
66
+ }
@@ -8,6 +8,7 @@ import { mat2d, vec2 } from 'gl-matrix';
8
8
  */
9
9
  import type { CropperState, Size } from './types';
10
10
  import { createCamera, getRotatedBBox, getVisibleBounds } from './camera';
11
+ import { isValidSize, sanitizeCropperState } from './math/sanitize';
11
12
 
12
13
  /**
13
14
  * The selected image region in source-pixel coordinates.
@@ -51,22 +52,28 @@ export function getSourceRegion(
51
52
  state: CropperState,
52
53
  imageSize: Size
53
54
  ): SourceRegion {
54
- if ( imageSize.width === 0 || imageSize.height === 0 ) {
55
+ // Sanitize before the zero-size short-circuit so the early return
56
+ // doesn't leak raw non-finite state.rotation or state.zoom out
57
+ // through the metadata fields. (state.flip is boolean and isn't
58
+ // at risk of NaN propagation.)
59
+ const safeState = sanitizeCropperState( state );
60
+
61
+ if ( ! isValidSize( imageSize ) ) {
55
62
  return {
56
63
  x: 0,
57
64
  y: 0,
58
65
  width: 0,
59
66
  height: 0,
60
- rotation: state.rotation,
61
- flip: { ...state.flip },
62
- zoom: state.zoom,
67
+ rotation: safeState.rotation,
68
+ flip: { ...safeState.flip },
69
+ zoom: safeState.zoom,
63
70
  };
64
71
  }
65
72
 
66
73
  // Use a synthetic 1:1 container so the camera maps normalized coords
67
74
  // to a known pixel space. The container size cancels out.
68
75
  const syntheticContainer: Size = { width: 1000, height: 1000 };
69
- const camera = createCamera( state, syntheticContainer, imageSize );
76
+ const camera = createCamera( safeState, syntheticContainer, imageSize );
70
77
 
71
78
  // Inverse camera maps screen pixels back to normalized [0,1] world coords.
72
79
  const inv = mat2d.create();
@@ -76,13 +83,13 @@ export function getSourceRegion(
76
83
  // (zoom=1, no pan) to locate the visual bounds, then place the
77
84
  // crop rect within them.
78
85
  const baseCamera = createCamera(
79
- { ...state, pan: { x: 0, y: 0 }, zoom: 1 },
86
+ { ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },
80
87
  syntheticContainer,
81
88
  imageSize
82
89
  );
83
90
  const visibleBounds = getVisibleBounds( baseCamera );
84
91
 
85
- const cropRect = state.cropRect;
92
+ const cropRect = safeState.cropRect;
86
93
  const cropCenterScreenX =
87
94
  visibleBounds.left +
88
95
  ( cropRect.x + cropRect.width / 2 ) * visibleBounds.width;
@@ -100,23 +107,23 @@ export function getSourceRegion(
100
107
 
101
108
  // Crop rect size in the snap-rotation visual space, divided by zoom
102
109
  // for source-pixel dimensions. Matches the stencil's reference frame.
103
- const snapRotation = Math.round( state.rotation / 90 ) * 90;
110
+ const snapRotation = Math.round( safeState.rotation / 90 ) * 90;
104
111
  const { width: rotW, height: rotH } = getRotatedBBox(
105
112
  imageSize.width,
106
113
  imageSize.height,
107
114
  snapRotation
108
115
  );
109
- const sourceW = ( cropRect.width * rotW ) / state.zoom;
110
- const sourceH = ( cropRect.height * rotH ) / state.zoom;
116
+ const sourceW = ( cropRect.width * rotW ) / safeState.zoom;
117
+ const sourceH = ( cropRect.height * rotH ) / safeState.zoom;
111
118
 
112
119
  return {
113
120
  x: srcCenter[ 0 ] * imageSize.width - sourceW / 2,
114
121
  y: srcCenter[ 1 ] * imageSize.height - sourceH / 2,
115
122
  width: sourceW,
116
123
  height: sourceH,
117
- rotation: state.rotation,
118
- flip: { ...state.flip },
119
- zoom: state.zoom,
124
+ rotation: safeState.rotation,
125
+ flip: { ...safeState.flip },
126
+ zoom: safeState.zoom,
120
127
  };
121
128
  }
122
129
 
@@ -153,7 +160,7 @@ export function getSourceRegionPercent(
153
160
  state: CropperState,
154
161
  imageSize: Size
155
162
  ): SourceRegionPercent {
156
- if ( imageSize.width === 0 || imageSize.height === 0 ) {
163
+ if ( ! isValidSize( imageSize ) ) {
157
164
  return { x: 0, y: 0, width: 0, height: 0 };
158
165
  }
159
166
  const region = getSourceRegion( state, imageSize );
@@ -2,7 +2,12 @@
2
2
  * Internal dependencies
3
3
  */
4
4
  import type { CropperAction, CropperState, TransformOperation } from './types';
5
- import { DEFAULT_STATE, MAX_ZOOM } from './constants';
5
+ import {
6
+ ABSOLUTE_MIN_ZOOM,
7
+ DEFAULT_STATE,
8
+ MAX_ZOOM,
9
+ MIN_ZOOM,
10
+ } from './constants';
6
11
  import { normalizeRotation, degreesToRadians } from './math/rotation';
7
12
  import { restrictPanZoom, restrictCropRect } from './containment';
8
13
 
@@ -13,6 +18,50 @@ function nearlyEqual( a: number, b: number ): boolean {
13
18
  return Math.abs( a - b ) < STATE_EPSILON;
14
19
  }
15
20
 
21
+ /**
22
+ * Value-equality check for two cropper states.
23
+ *
24
+ * Used by history machinery to dedup snapshots and detect no-op
25
+ * transitions. Compares every field that participates in the rendered
26
+ * output; ignores reference identity.
27
+ *
28
+ * @param a First cropper state.
29
+ * @param b Second cropper state.
30
+ * @return Whether the two states are value-equal across all tracked fields.
31
+ */
32
+ export function areCropperStatesEqual(
33
+ a: CropperState,
34
+ b: CropperState
35
+ ): boolean {
36
+ const aImage = a.image;
37
+ const bImage = b.image;
38
+ return (
39
+ aImage?.src === bImage?.src &&
40
+ aImage?.naturalWidth === bImage?.naturalWidth &&
41
+ aImage?.naturalHeight === bImage?.naturalHeight &&
42
+ nearlyEqual( a.pan.x, b.pan.x ) &&
43
+ nearlyEqual( a.pan.y, b.pan.y ) &&
44
+ nearlyEqual( a.zoom, b.zoom ) &&
45
+ nearlyEqual( a.rotation, b.rotation ) &&
46
+ a.flip.horizontal === b.flip.horizontal &&
47
+ a.flip.vertical === b.flip.vertical &&
48
+ nearlyEqual( a.cropRect.x, b.cropRect.x ) &&
49
+ nearlyEqual( a.cropRect.y, b.cropRect.y ) &&
50
+ nearlyEqual( a.cropRect.width, b.cropRect.width ) &&
51
+ nearlyEqual( a.cropRect.height, b.cropRect.height )
52
+ );
53
+ }
54
+
55
+ function clampRequestedZoom( state: CropperState, zoom: number ): number {
56
+ // With an image, enforceContainment raises zoom to the coverage-aware
57
+ // floor below. Without an image, no enforcement runs, so MIN_ZOOM stays
58
+ // the conservative resting default.
59
+ if ( state.image ) {
60
+ return Math.min( MAX_ZOOM, Math.max( ABSOLUTE_MIN_ZOOM, zoom ) );
61
+ }
62
+ return Math.min( MAX_ZOOM, Math.max( MIN_ZOOM, zoom ) );
63
+ }
64
+
16
65
  /**
17
66
  * Translate a pipeline transform operation into the equivalent
18
67
  * reducer action. Pipeline ops aren't 1:1 with reducer actions —
@@ -192,7 +241,7 @@ export function cropperReducer(
192
241
  );
193
242
 
194
243
  case 'SET_ZOOM': {
195
- const z = Math.min( MAX_ZOOM, Math.max( 1, action.payload ) );
244
+ const z = clampRequestedZoom( state, action.payload );
196
245
  return commitBase(
197
246
  enforceContainment( {
198
247
  ...state,
@@ -202,7 +251,7 @@ export function cropperReducer(
202
251
  }
203
252
 
204
253
  case 'SET_ZOOM_AT_POINT': {
205
- const z = Math.min( MAX_ZOOM, Math.max( 1, action.payload.zoom ) );
254
+ const z = clampRequestedZoom( state, action.payload.zoom );
206
255
  return commitBase(
207
256
  enforceContainment( {
208
257
  ...state,
@@ -345,24 +394,27 @@ export function cropperReducer(
345
394
  return state;
346
395
  }
347
396
 
348
- // New crop: fill height (or width), maintain aspect ratio, center.
349
- const normalizedRatio = rect.width / rect.height;
350
- let newH = 1;
351
- let newW = normalizedRatio;
352
- if ( newW > 1 ) {
353
- newW = 1;
354
- newH = 1 / normalizedRatio;
355
- }
397
+ // Scale factor: how much the crop grows. fitScale fills the
398
+ // larger axis to 1 (viewport-filling settle). Cap so the
399
+ // resulting zoom stays at or below MAX_ZOOM — tight crops
400
+ // would otherwise push state.zoom past the user-facing cap
401
+ // and break wheel/slider interactions, which clamp to
402
+ // MAX_ZOOM and snap state.zoom down on the first input.
403
+ const fitScale = 1 / Math.max( rect.width, rect.height );
404
+ const zoomCap = state.zoom > 0 ? MAX_ZOOM / state.zoom : fitScale;
405
+ const s = Math.min( fitScale, zoomCap );
356
406
 
357
- // Scale factor: how much the crop grew.
358
- const s = newH / rect.height;
407
+ const newW = rect.width * s;
408
+ const newH = rect.height * s;
359
409
 
360
410
  // The old crop center in normalized visual space.
361
411
  const oldCx = rect.x + rect.width / 2;
362
412
  const oldCy = rect.y + rect.height / 2;
363
413
 
364
- // Zoom scales by s so the same image region fills the
365
- // larger crop at the same relative size.
414
+ // Zoom scales by s so the same image region fills the new
415
+ // crop at the same relative size. When s is capped, the new
416
+ // crop is smaller than the viewport but the visible content
417
+ // inside it remains the source region the user framed.
366
418
  // Pan: the visible content center was at
367
419
  // (cropCx - crop.x, cropCy - crop.y)
368
420
  // in visual-normalized space. After centering the crop to