@wordpress/media-editor 0.21.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 (339) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/build/components/media-editor/index.cjs +533 -686
  3. package/build/components/media-editor/index.cjs.map +1 -7
  4. package/build/components/media-editor/use-crop-options.cjs +43 -57
  5. package/build/components/media-editor/use-crop-options.cjs.map +1 -7
  6. package/build/components/media-editor/use-save-media-editor.cjs +111 -185
  7. package/build/components/media-editor/use-save-media-editor.cjs.map +1 -7
  8. package/build/components/media-editor-canvas/index.cjs +122 -124
  9. package/build/components/media-editor-canvas/index.cjs.map +1 -7
  10. package/build/components/media-editor-crop-panel/index.cjs +65 -72
  11. package/build/components/media-editor-crop-panel/index.cjs.map +1 -7
  12. package/build/components/media-editor-fine-rotation/index.cjs +62 -72
  13. package/build/components/media-editor-fine-rotation/index.cjs.map +1 -7
  14. package/build/components/media-editor-image-controls/index.cjs +183 -257
  15. package/build/components/media-editor-image-controls/index.cjs.map +1 -7
  16. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +119 -157
  17. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +1 -7
  18. package/build/components/media-editor-modal/build-modifiers.cjs +107 -88
  19. package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -7
  20. package/build/components/media-editor-modal/index.cjs +112 -155
  21. package/build/components/media-editor-modal/index.cjs.map +1 -7
  22. package/build/components/media-editor-provider/index.cjs +32 -55
  23. package/build/components/media-editor-provider/index.cjs.map +1 -7
  24. package/build/components/media-form/index.cjs +76 -77
  25. package/build/components/media-form/index.cjs.map +1 -7
  26. package/build/components/media-preview/index.cjs +114 -114
  27. package/build/components/media-preview/index.cjs.map +1 -7
  28. package/build/components/rotation-ruler/index.cjs +181 -188
  29. package/build/components/rotation-ruler/index.cjs.map +1 -7
  30. package/build/components/rotation-ruler/use-ruler-drag.cjs +132 -149
  31. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -7
  32. package/build/hooks/use-crop-gesture-handlers.cjs +82 -76
  33. package/build/hooks/use-crop-gesture-handlers.cjs.map +1 -7
  34. package/build/image-editor/core/camera.cjs +257 -205
  35. package/build/image-editor/core/camera.cjs.map +1 -7
  36. package/build/image-editor/core/constants.cjs +127 -102
  37. package/build/image-editor/core/constants.cjs.map +1 -7
  38. package/build/image-editor/core/containment.cjs +275 -262
  39. package/build/image-editor/core/containment.cjs.map +1 -7
  40. package/build/image-editor/core/crop-rect.cjs +33 -44
  41. package/build/image-editor/core/crop-rect.cjs.map +1 -7
  42. package/build/image-editor/core/export/canvas-renderer.cjs +165 -135
  43. package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -7
  44. package/build/image-editor/core/index.cjs +88 -61
  45. package/build/image-editor/core/interaction-controller.cjs +583 -646
  46. package/build/image-editor/core/interaction-controller.cjs.map +1 -7
  47. package/build/image-editor/core/math/rotation.cjs +26 -35
  48. package/build/image-editor/core/math/rotation.cjs.map +1 -7
  49. package/build/image-editor/core/math/sanitize.cjs +124 -80
  50. package/build/image-editor/core/math/sanitize.cjs.map +1 -7
  51. package/build/image-editor/core/setter-helpers.cjs +57 -60
  52. package/build/image-editor/core/setter-helpers.cjs.map +1 -7
  53. package/build/image-editor/core/source-region.cjs +220 -190
  54. package/build/image-editor/core/source-region.cjs.map +1 -7
  55. package/build/image-editor/core/state.cjs +278 -265
  56. package/build/image-editor/core/state.cjs.map +1 -7
  57. package/build/image-editor/core/stencil-math.cjs +193 -206
  58. package/build/image-editor/core/stencil-math.cjs.map +1 -7
  59. package/build/image-editor/core/transform-style.cjs +39 -48
  60. package/build/image-editor/core/transform-style.cjs.map +1 -7
  61. package/build/image-editor/core/transforms/pipeline.cjs +36 -38
  62. package/build/image-editor/core/transforms/pipeline.cjs.map +1 -7
  63. package/build/image-editor/core/types.cjs +4 -18
  64. package/build/image-editor/core/viewport-state.cjs +31 -45
  65. package/build/image-editor/core/viewport-state.cjs.map +1 -7
  66. package/build/image-editor/index.cjs +65 -64
  67. package/build/image-editor/react/components/cropper-provider.cjs +44 -45
  68. package/build/image-editor/react/components/cropper-provider.cjs.map +1 -7
  69. package/build/image-editor/react/components/cropper.cjs +583 -710
  70. package/build/image-editor/react/components/cropper.cjs.map +1 -7
  71. package/build/image-editor/react/components/index.cjs +22 -33
  72. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +135 -160
  73. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -7
  74. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +28 -59
  75. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +1 -7
  76. package/build/image-editor/react/components/overlays/grid-overlay.cjs +58 -88
  77. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +1 -7
  78. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +361 -420
  79. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +1 -7
  80. package/build/image-editor/react/components/viewport-provider.cjs +21 -46
  81. package/build/image-editor/react/components/viewport-provider.cjs.map +1 -7
  82. package/build/image-editor/react/hooks/build-cropper-setters.cjs +76 -67
  83. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -7
  84. package/build/image-editor/react/hooks/index.cjs +16 -31
  85. package/build/image-editor/react/hooks/use-aria-announcer.cjs +90 -146
  86. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +1 -7
  87. package/build/image-editor/react/hooks/use-cropper-controller.cjs +80 -0
  88. package/build/image-editor/react/hooks/use-cropper-controller.cjs.map +1 -0
  89. package/build/image-editor/react/hooks/use-interaction.cjs +179 -205
  90. package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -7
  91. package/build/image-editor/react/hooks/use-transform-style.cjs +20 -34
  92. package/build/image-editor/react/hooks/use-transform-style.cjs.map +1 -7
  93. package/build/image-editor/react/hooks/use-viewport-state.cjs +36 -47
  94. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -7
  95. package/build/image-editor/react/visually-hidden-style.cjs +21 -38
  96. package/build/image-editor/react/visually-hidden-style.cjs.map +1 -7
  97. package/build/index.cjs +45 -39
  98. package/build/lock-unlock.cjs +9 -35
  99. package/build/lock-unlock.cjs.map +1 -7
  100. package/build/private-apis.cjs +16 -37
  101. package/build/private-apis.cjs.map +1 -7
  102. package/build/state/aspect-ratio-presets.cjs +53 -0
  103. package/build/state/aspect-ratio-presets.cjs.map +1 -0
  104. package/build/state/composite-reducer.cjs +134 -112
  105. package/build/state/composite-reducer.cjs.map +1 -7
  106. package/build/state/index.cjs +63 -47
  107. package/build/state/media-editor-state-provider.cjs +35 -48
  108. package/build/state/media-editor-state-provider.cjs.map +1 -7
  109. package/build/state/types.cjs +12 -31
  110. package/build/state/types.cjs.map +1 -7
  111. package/build/state/use-media-editor-state.cjs +261 -294
  112. package/build/state/use-media-editor-state.cjs.map +1 -7
  113. package/build/store/actions.cjs +17 -44
  114. package/build/store/actions.cjs.map +1 -7
  115. package/build/store/constants.cjs +8 -29
  116. package/build/store/constants.cjs.map +1 -7
  117. package/build/store/index.cjs +33 -40
  118. package/build/store/index.cjs.map +1 -7
  119. package/build/store/reducer.cjs +28 -50
  120. package/build/store/reducer.cjs.map +1 -7
  121. package/build/store/selectors.cjs +16 -41
  122. package/build/store/selectors.cjs.map +1 -7
  123. package/build/utils/get-media-type.cjs +18 -41
  124. package/build/utils/get-media-type.cjs.map +1 -7
  125. package/build/utils/index.cjs +9 -28
  126. package/build-module/components/media-editor/index.mjs +490 -673
  127. package/build-module/components/media-editor/index.mjs.map +1 -7
  128. package/build-module/components/media-editor/use-crop-options.mjs +38 -32
  129. package/build-module/components/media-editor/use-crop-options.mjs.map +1 -7
  130. package/build-module/components/media-editor/use-save-media-editor.mjs +89 -157
  131. package/build-module/components/media-editor/use-save-media-editor.mjs.map +1 -7
  132. package/build-module/components/media-editor-canvas/index.mjs +98 -96
  133. package/build-module/components/media-editor-canvas/index.mjs.map +1 -7
  134. package/build-module/components/media-editor-crop-panel/index.mjs +43 -46
  135. package/build-module/components/media-editor-crop-panel/index.mjs.map +1 -7
  136. package/build-module/components/media-editor-fine-rotation/index.mjs +39 -45
  137. package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -7
  138. package/build-module/components/media-editor-image-controls/index.mjs +172 -237
  139. package/build-module/components/media-editor-image-controls/index.mjs.map +1 -7
  140. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +112 -137
  141. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +1 -7
  142. package/build-module/components/media-editor-modal/build-modifiers.mjs +103 -64
  143. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +1 -7
  144. package/build-module/components/media-editor-modal/index.mjs +88 -124
  145. package/build-module/components/media-editor-modal/index.mjs.map +1 -7
  146. package/build-module/components/media-editor-provider/index.mjs +26 -29
  147. package/build-module/components/media-editor-provider/index.mjs.map +1 -7
  148. package/build-module/components/media-form/index.mjs +68 -56
  149. package/build-module/components/media-form/index.mjs.map +1 -7
  150. package/build-module/components/media-preview/index.mjs +106 -90
  151. package/build-module/components/media-preview/index.mjs.map +1 -7
  152. package/build-module/components/rotation-ruler/index.mjs +161 -164
  153. package/build-module/components/rotation-ruler/index.mjs.map +1 -7
  154. package/build-module/components/rotation-ruler/use-ruler-drag.mjs +125 -122
  155. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -7
  156. package/build-module/hooks/use-crop-gesture-handlers.mjs +76 -50
  157. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +1 -7
  158. package/build-module/image-editor/core/camera.mjs +245 -178
  159. package/build-module/image-editor/core/camera.mjs.map +1 -7
  160. package/build-module/image-editor/core/constants.mjs +110 -64
  161. package/build-module/image-editor/core/constants.mjs.map +1 -7
  162. package/build-module/image-editor/core/containment.mjs +265 -237
  163. package/build-module/image-editor/core/containment.mjs.map +1 -7
  164. package/build-module/image-editor/core/crop-rect.mjs +29 -20
  165. package/build-module/image-editor/core/crop-rect.mjs.map +1 -7
  166. package/build-module/image-editor/core/export/canvas-renderer.mjs +155 -105
  167. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -7
  168. package/build-module/image-editor/core/index.mjs +5 -34
  169. package/build-module/image-editor/core/interaction-controller.mjs +579 -628
  170. package/build-module/image-editor/core/interaction-controller.mjs.map +1 -7
  171. package/build-module/image-editor/core/math/rotation.mjs +21 -10
  172. package/build-module/image-editor/core/math/rotation.mjs.map +1 -7
  173. package/build-module/image-editor/core/math/sanitize.mjs +116 -52
  174. package/build-module/image-editor/core/math/sanitize.mjs.map +1 -7
  175. package/build-module/image-editor/core/setter-helpers.mjs +52 -35
  176. package/build-module/image-editor/core/setter-helpers.mjs.map +1 -7
  177. package/build-module/image-editor/core/source-region.mjs +210 -160
  178. package/build-module/image-editor/core/source-region.mjs.map +1 -7
  179. package/build-module/image-editor/core/state.mjs +271 -243
  180. package/build-module/image-editor/core/state.mjs.map +1 -7
  181. package/build-module/image-editor/core/stencil-math.mjs +185 -178
  182. package/build-module/image-editor/core/stencil-math.mjs.map +1 -7
  183. package/build-module/image-editor/core/transform-style.mjs +34 -23
  184. package/build-module/image-editor/core/transform-style.mjs.map +1 -7
  185. package/build-module/image-editor/core/transforms/pipeline.mjs +29 -11
  186. package/build-module/image-editor/core/transforms/pipeline.mjs.map +1 -7
  187. package/build-module/image-editor/core/types.mjs +0 -1
  188. package/build-module/image-editor/core/viewport-state.mjs +26 -20
  189. package/build-module/image-editor/core/viewport-state.mjs.map +1 -7
  190. package/build-module/image-editor/index.mjs +3 -38
  191. package/build-module/image-editor/react/components/cropper-provider.mjs +38 -21
  192. package/build-module/image-editor/react/components/cropper-provider.mjs.map +1 -7
  193. package/build-module/image-editor/react/components/cropper.mjs +550 -686
  194. package/build-module/image-editor/react/components/cropper.mjs.map +1 -7
  195. package/build-module/image-editor/react/components/index.mjs +1 -7
  196. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +130 -135
  197. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -7
  198. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +24 -35
  199. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +1 -7
  200. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +54 -64
  201. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +1 -7
  202. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +355 -407
  203. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +1 -7
  204. package/build-module/image-editor/react/components/viewport-provider.mjs +15 -22
  205. package/build-module/image-editor/react/components/viewport-provider.mjs.map +1 -7
  206. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +72 -43
  207. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -7
  208. package/build-module/image-editor/react/hooks/index.mjs +2 -7
  209. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +83 -119
  210. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +1 -7
  211. package/build-module/image-editor/react/hooks/use-cropper-controller.mjs +75 -0
  212. package/build-module/image-editor/react/hooks/use-cropper-controller.mjs.map +1 -0
  213. package/build-module/image-editor/react/hooks/use-interaction.mjs +175 -183
  214. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +1 -7
  215. package/build-module/image-editor/react/hooks/use-transform-style.mjs +15 -9
  216. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +1 -7
  217. package/build-module/image-editor/react/hooks/use-viewport-state.mjs +32 -26
  218. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -7
  219. package/build-module/image-editor/react/visually-hidden-style.mjs +17 -14
  220. package/build-module/image-editor/react/visually-hidden-style.mjs.map +1 -7
  221. package/build-module/index.mjs +3 -10
  222. package/build-module/lock-unlock.mjs +5 -10
  223. package/build-module/lock-unlock.mjs.map +1 -7
  224. package/build-module/private-apis.mjs +8 -9
  225. package/build-module/private-apis.mjs.map +1 -7
  226. package/build-module/state/aspect-ratio-presets.mjs +48 -0
  227. package/build-module/state/aspect-ratio-presets.mjs.map +1 -0
  228. package/build-module/state/composite-reducer.mjs +114 -90
  229. package/build-module/state/composite-reducer.mjs.map +1 -7
  230. package/build-module/state/index.mjs +5 -25
  231. package/build-module/state/media-editor-state-provider.mjs +29 -24
  232. package/build-module/state/media-editor-state-provider.mjs.map +1 -7
  233. package/build-module/state/types.mjs +8 -7
  234. package/build-module/state/types.mjs.map +1 -7
  235. package/build-module/state/use-media-editor-state.mjs +256 -286
  236. package/build-module/state/use-media-editor-state.mjs.map +1 -7
  237. package/build-module/store/actions.mjs +12 -19
  238. package/build-module/store/actions.mjs.map +1 -7
  239. package/build-module/store/constants.mjs +4 -5
  240. package/build-module/store/constants.mjs.map +1 -7
  241. package/build-module/store/index.mjs +8 -9
  242. package/build-module/store/index.mjs.map +1 -7
  243. package/build-module/store/reducer.mjs +23 -26
  244. package/build-module/store/reducer.mjs.map +1 -7
  245. package/build-module/store/selectors.mjs +8 -13
  246. package/build-module/store/selectors.mjs.map +1 -7
  247. package/build-module/utils/get-media-type.mjs +14 -17
  248. package/build-module/utils/get-media-type.mjs.map +1 -7
  249. package/build-module/utils/index.mjs +1 -6
  250. package/build-style/style-rtl.css +41 -4
  251. package/build-style/style.css +41 -4
  252. package/build-types/components/media-editor/index.d.ts +4 -0
  253. package/build-types/components/media-editor/index.d.ts.map +1 -1
  254. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
  255. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
  256. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  257. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  258. package/build-types/components/media-editor-modal/build-modifiers.d.ts +6 -5
  259. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -1
  260. package/build-types/components/media-preview/index.d.ts.map +1 -1
  261. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
  262. package/build-types/image-editor/core/constants.d.ts +0 -1
  263. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  264. package/build-types/image-editor/core/index.d.ts +1 -1
  265. package/build-types/image-editor/core/index.d.ts.map +1 -1
  266. package/build-types/image-editor/core/setter-helpers.d.ts +1 -1
  267. package/build-types/image-editor/core/types.d.ts +1 -1
  268. package/build-types/image-editor/index.d.ts +7 -6
  269. package/build-types/image-editor/index.d.ts.map +1 -1
  270. package/build-types/image-editor/react/components/cropper-provider.d.ts +2 -2
  271. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
  272. package/build-types/image-editor/react/components/cropper.d.ts +5 -14
  273. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  274. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +2 -2
  275. package/build-types/image-editor/react/hooks/index.d.ts +2 -2
  276. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
  277. package/build-types/image-editor/react/hooks/{use-cropper-reducer.d.ts → use-cropper-controller.d.ts} +12 -12
  278. package/build-types/image-editor/react/hooks/use-cropper-controller.d.ts.map +1 -0
  279. package/build-types/state/aspect-ratio-presets.d.ts +7 -0
  280. package/build-types/state/aspect-ratio-presets.d.ts.map +1 -0
  281. package/build-types/state/composite-reducer.d.ts +1 -1
  282. package/build-types/state/composite-reducer.d.ts.map +1 -1
  283. package/build-types/state/index.d.ts +1 -0
  284. package/build-types/state/index.d.ts.map +1 -1
  285. package/build-types/state/types.d.ts +2 -1
  286. package/build-types/state/types.d.ts.map +1 -1
  287. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  288. package/package.json +21 -21
  289. package/src/components/media-editor/index.tsx +215 -162
  290. package/src/components/media-editor/style.scss +19 -0
  291. package/src/components/media-editor/use-crop-options.ts +5 -2
  292. package/src/components/media-editor/use-save-media-editor.ts +2 -6
  293. package/src/components/media-editor-canvas/index.tsx +16 -26
  294. package/src/components/media-editor-image-controls/index.tsx +4 -2
  295. package/src/components/media-editor-modal/build-modifiers.ts +13 -7
  296. package/src/components/media-editor-modal/style.scss +1 -1
  297. package/src/components/media-preview/index.tsx +66 -23
  298. package/src/components/media-preview/style.scss +16 -2
  299. package/src/components/rotation-ruler/style.scss +6 -2
  300. package/src/components/rotation-ruler/use-ruler-drag.ts +6 -11
  301. package/src/image-editor/README.md +8 -8
  302. package/src/image-editor/core/constants.ts +0 -13
  303. package/src/image-editor/core/index.ts +1 -5
  304. package/src/image-editor/core/setter-helpers.ts +1 -1
  305. package/src/image-editor/core/types.ts +1 -1
  306. package/src/image-editor/docs/architecture.md +1 -1
  307. package/src/image-editor/docs/recipes.md +3 -3
  308. package/src/image-editor/index.ts +7 -16
  309. package/src/image-editor/react/components/cropper-provider.tsx +5 -5
  310. package/src/image-editor/react/components/cropper.tsx +19 -27
  311. package/src/image-editor/react/hooks/build-cropper-setters.ts +2 -2
  312. package/src/image-editor/react/hooks/index.ts +2 -2
  313. package/src/image-editor/react/hooks/{use-cropper-reducer.ts → use-cropper-controller.ts} +11 -11
  314. package/src/state/aspect-ratio-presets.ts +18 -0
  315. package/src/state/composite-reducer.ts +2 -2
  316. package/src/state/index.ts +1 -0
  317. package/src/state/types.ts +2 -6
  318. package/src/state/use-media-editor-state.ts +7 -5
  319. package/build/image-editor/core/index.cjs.map +0 -7
  320. package/build/image-editor/core/types.cjs.map +0 -7
  321. package/build/image-editor/index.cjs.map +0 -7
  322. package/build/image-editor/react/components/index.cjs.map +0 -7
  323. package/build/image-editor/react/hooks/index.cjs.map +0 -7
  324. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +0 -113
  325. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +0 -7
  326. package/build/index.cjs.map +0 -7
  327. package/build/state/index.cjs.map +0 -7
  328. package/build/utils/index.cjs.map +0 -7
  329. package/build-module/image-editor/core/index.mjs.map +0 -7
  330. package/build-module/image-editor/core/types.mjs.map +0 -7
  331. package/build-module/image-editor/index.mjs.map +0 -7
  332. package/build-module/image-editor/react/components/index.mjs.map +0 -7
  333. package/build-module/image-editor/react/hooks/index.mjs.map +0 -7
  334. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +0 -100
  335. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +0 -7
  336. package/build-module/index.mjs.map +0 -7
  337. package/build-module/state/index.mjs.map +0 -7
  338. package/build-module/utils/index.mjs.map +0 -7
  339. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +0 -1
@@ -1,178 +1,228 @@
1
- // packages/media-editor/src/image-editor/core/source-region.ts
2
1
  import { mat2d, vec2 } from "gl-matrix";
3
2
  import { createCamera, getRotatedBBox, getVisibleBounds } from "./camera.mjs";
4
3
  import { isValidSize, sanitizeCropperState } from "./math/sanitize.mjs";
5
4
  import { normalizeRotation } from "./math/rotation.mjs";
5
+ // packages/media-editor/src/image-editor/core/source-region.ts
6
+ /**
7
+ * Get the selected image region in source-pixel coordinates.
8
+ *
9
+ * Converts the current crop state (normalized visual space) to the actual
10
+ * region of the original image that's selected. This is the bridge between
11
+ * the cropper's internal coordinate system and external tools (image
12
+ * processing libraries, AI APIs, server-side processing) that work in
13
+ * source-pixel coordinates.
14
+ *
15
+ * The returned rectangle accounts for pan, zoom, and the crop rect position,
16
+ * but expresses the crop in the unrotated image's coordinate space. Rotation
17
+ * and flip are included as separate fields since they represent transforms,
18
+ * not a region.
19
+ *
20
+ * @param state The current cropper state.
21
+ * @param imageSize The natural dimensions of the source image.
22
+ * @return The selected region in source pixels plus rotation/flip metadata.
23
+ */
6
24
  function getSourceRegion(state, imageSize) {
7
- const safeState = sanitizeCropperState(state);
8
- if (!isValidSize(imageSize)) {
9
- return {
10
- x: 0,
11
- y: 0,
12
- width: 0,
13
- height: 0,
14
- rotation: safeState.rotation,
15
- flip: { ...safeState.flip },
16
- zoom: safeState.zoom
17
- };
18
- }
19
- const syntheticContainer = { width: 1e3, height: 1e3 };
20
- const camera = createCamera(safeState, syntheticContainer, imageSize);
21
- const inv = mat2d.create();
22
- mat2d.invert(inv, camera);
23
- const baseCamera = createCamera(
24
- { ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },
25
- syntheticContainer,
26
- imageSize
27
- );
28
- const visibleBounds = getVisibleBounds(baseCamera);
29
- const cropRect = safeState.cropRect;
30
- const cropCenterScreenX = visibleBounds.left + (cropRect.x + cropRect.width / 2) * visibleBounds.width;
31
- const cropCenterScreenY = visibleBounds.top + (cropRect.y + cropRect.height / 2) * visibleBounds.height;
32
- const srcCenter = vec2.create();
33
- vec2.transformMat2d(
34
- srcCenter,
35
- [cropCenterScreenX, cropCenterScreenY],
36
- inv
37
- );
38
- const snapRotation = Math.round(safeState.rotation / 90) * 90;
39
- const { width: rotW, height: rotH } = getRotatedBBox(
40
- imageSize.width,
41
- imageSize.height,
42
- snapRotation
43
- );
44
- const sourceW = cropRect.width * rotW / safeState.zoom;
45
- const sourceH = cropRect.height * rotH / safeState.zoom;
46
- return {
47
- x: srcCenter[0] * imageSize.width - sourceW / 2,
48
- y: srcCenter[1] * imageSize.height - sourceH / 2,
49
- width: sourceW,
50
- height: sourceH,
51
- rotation: safeState.rotation,
52
- flip: { ...safeState.flip },
53
- zoom: safeState.zoom
54
- };
25
+ const safeState = sanitizeCropperState(state);
26
+ if (!isValidSize(imageSize)) return {
27
+ x: 0,
28
+ y: 0,
29
+ width: 0,
30
+ height: 0,
31
+ rotation: safeState.rotation,
32
+ flip: { ...safeState.flip },
33
+ zoom: safeState.zoom
34
+ };
35
+ const syntheticContainer = {
36
+ width: 1e3,
37
+ height: 1e3
38
+ };
39
+ const camera = createCamera(safeState, syntheticContainer, imageSize);
40
+ const inv = mat2d.create();
41
+ mat2d.invert(inv, camera);
42
+ const baseCamera = createCamera({
43
+ ...safeState,
44
+ pan: {
45
+ x: 0,
46
+ y: 0
47
+ },
48
+ zoom: 1
49
+ }, syntheticContainer, imageSize);
50
+ const visibleBounds = getVisibleBounds(baseCamera);
51
+ const cropRect = safeState.cropRect;
52
+ const cropCenterScreenX = visibleBounds.left + (cropRect.x + cropRect.width / 2) * visibleBounds.width;
53
+ const cropCenterScreenY = visibleBounds.top + (cropRect.y + cropRect.height / 2) * visibleBounds.height;
54
+ const srcCenter = vec2.create();
55
+ vec2.transformMat2d(srcCenter, [cropCenterScreenX, cropCenterScreenY], inv);
56
+ const snapRotation = Math.round(safeState.rotation / 90) * 90;
57
+ const { width: rotW, height: rotH } = getRotatedBBox(imageSize.width, imageSize.height, snapRotation);
58
+ const sourceW = cropRect.width * rotW / safeState.zoom;
59
+ const sourceH = cropRect.height * rotH / safeState.zoom;
60
+ return {
61
+ x: srcCenter[0] * imageSize.width - sourceW / 2,
62
+ y: srcCenter[1] * imageSize.height - sourceH / 2,
63
+ width: sourceW,
64
+ height: sourceH,
65
+ rotation: safeState.rotation,
66
+ flip: { ...safeState.flip },
67
+ zoom: safeState.zoom
68
+ };
55
69
  }
70
+ /**
71
+ * Convert a source-pixel region back to the cropper's normalized crop rect.
72
+ *
73
+ * The math intentionally mirrors `getSourceRegion`: map the requested source
74
+ * center through the current camera, then express that screen point inside the
75
+ * same base visible bounds used to position crop overlays.
76
+ *
77
+ * @param state The current cropper state.
78
+ * @param imageSize The natural dimensions of the source image.
79
+ * @param region The source-pixel region to represent as a crop rect.
80
+ * @return The normalized crop rect, or null when inputs cannot be mapped.
81
+ */
56
82
  function getCropRectFromSourceRegion(state, imageSize, region) {
57
- if (!isValidSize(imageSize) || region.width <= 0 || region.height <= 0) {
58
- return null;
59
- }
60
- const safeState = sanitizeCropperState(state);
61
- const syntheticContainer = { width: 1e3, height: 1e3 };
62
- const baseCamera = createCamera(
63
- { ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },
64
- syntheticContainer,
65
- imageSize
66
- );
67
- const visibleBounds = getVisibleBounds(baseCamera);
68
- if (visibleBounds.width <= 0 || visibleBounds.height <= 0) {
69
- return null;
70
- }
71
- const sourceCenter = vec2.fromValues(
72
- (region.x + region.width / 2) / imageSize.width,
73
- (region.y + region.height / 2) / imageSize.height
74
- );
75
- const screenCenter = vec2.create();
76
- const camera = createCamera(safeState, syntheticContainer, imageSize);
77
- vec2.transformMat2d(screenCenter, sourceCenter, camera);
78
- const snapRotation = Math.round(safeState.rotation / 90) * 90;
79
- const { width: rotW, height: rotH } = getRotatedBBox(
80
- imageSize.width,
81
- imageSize.height,
82
- snapRotation
83
- );
84
- if (rotW <= 0 || rotH <= 0) {
85
- return null;
86
- }
87
- const width = region.width * safeState.zoom / rotW;
88
- const height = region.height * safeState.zoom / rotH;
89
- const centerX = (screenCenter[0] - visibleBounds.left) / visibleBounds.width;
90
- const centerY = (screenCenter[1] - visibleBounds.top) / visibleBounds.height;
91
- return {
92
- x: centerX - width / 2,
93
- y: centerY - height / 2,
94
- width,
95
- height
96
- };
83
+ if (!isValidSize(imageSize) || region.width <= 0 || region.height <= 0) return null;
84
+ const safeState = sanitizeCropperState(state);
85
+ const syntheticContainer = {
86
+ width: 1e3,
87
+ height: 1e3
88
+ };
89
+ const baseCamera = createCamera({
90
+ ...safeState,
91
+ pan: {
92
+ x: 0,
93
+ y: 0
94
+ },
95
+ zoom: 1
96
+ }, syntheticContainer, imageSize);
97
+ const visibleBounds = getVisibleBounds(baseCamera);
98
+ if (visibleBounds.width <= 0 || visibleBounds.height <= 0) return null;
99
+ const sourceCenter = vec2.fromValues((region.x + region.width / 2) / imageSize.width, (region.y + region.height / 2) / imageSize.height);
100
+ const screenCenter = vec2.create();
101
+ const camera = createCamera(safeState, syntheticContainer, imageSize);
102
+ vec2.transformMat2d(screenCenter, sourceCenter, camera);
103
+ const snapRotation = Math.round(safeState.rotation / 90) * 90;
104
+ const { width: rotW, height: rotH } = getRotatedBBox(imageSize.width, imageSize.height, snapRotation);
105
+ if (rotW <= 0 || rotH <= 0) return null;
106
+ const width = region.width * safeState.zoom / rotW;
107
+ const height = region.height * safeState.zoom / rotH;
108
+ const centerX = (screenCenter[0] - visibleBounds.left) / visibleBounds.width;
109
+ const centerY = (screenCenter[1] - visibleBounds.top) / visibleBounds.height;
110
+ return {
111
+ x: centerX - width / 2,
112
+ y: centerY - height / 2,
113
+ width,
114
+ height
115
+ };
97
116
  }
98
- var CARDINAL_ROTATION_EPSILON = 1e-6;
117
+ const CARDINAL_ROTATION_EPSILON = 1e-6;
118
+ /**
119
+ * Check whether a rotation is effectively at a 90-degree stop.
120
+ *
121
+ * @param rotation The rotation angle in degrees.
122
+ * @return Whether the rotation is close enough to a cardinal angle.
123
+ */
99
124
  function isCardinalRotation(rotation) {
100
- const normalizedRotation = normalizeRotation(rotation);
101
- const nearestCardinal = Math.round(normalizedRotation / 90) * 90;
102
- return Math.abs(normalizedRotation - nearestCardinal) < CARDINAL_ROTATION_EPSILON;
125
+ const normalizedRotation = normalizeRotation(rotation);
126
+ const nearestCardinal = Math.round(normalizedRotation / 90) * 90;
127
+ return Math.abs(normalizedRotation - nearestCardinal) < CARDINAL_ROTATION_EPSILON;
103
128
  }
129
+ /**
130
+ * Snap a crop rect so the selected source-region edges land on whole pixels.
131
+ *
132
+ * This is a display-time affordance for visible pixel grids; it does not alter
133
+ * pan/zoom/rotation. Callers decide when snapping is appropriate.
134
+ *
135
+ * @param state The current cropper state.
136
+ * @param imageSize The natural dimensions of the source image.
137
+ * @param cropRect The candidate crop rect from resize math.
138
+ * @param handle The active resize handle. Only edges moved by the handle snap.
139
+ * @return A crop rect whose selected source-region edges are whole pixels.
140
+ */
104
141
  function snapCropRectToSourcePixels(state, imageSize, cropRect, handle) {
105
- const safeState = sanitizeCropperState(state);
106
- if (!isCardinalRotation(safeState.rotation)) {
107
- return cropRect;
108
- }
109
- return snapCropRectEdgesToSourcePixels(safeState, imageSize, cropRect, {
110
- left: handle.includes("w"),
111
- top: handle.includes("n"),
112
- right: handle.includes("e"),
113
- bottom: handle.includes("s")
114
- });
142
+ const safeState = sanitizeCropperState(state);
143
+ if (!isCardinalRotation(safeState.rotation)) return cropRect;
144
+ return snapCropRectEdgesToSourcePixels(safeState, imageSize, cropRect, {
145
+ left: handle.includes("w"),
146
+ top: handle.includes("n"),
147
+ right: handle.includes("e"),
148
+ bottom: handle.includes("s")
149
+ });
115
150
  }
151
+ /**
152
+ * Snap all source-region edges for a crop rect to the nearest whole pixels.
153
+ *
154
+ * @param state The current cropper state.
155
+ * @param imageSize The natural dimensions of the source image.
156
+ * @param cropRect The crop rect to align to the source-pixel grid.
157
+ * @return A crop rect whose source-region edges are whole pixels.
158
+ */
116
159
  function snapCropRectToSourcePixelGrid(state, imageSize, cropRect) {
117
- return snapCropRectEdgesToSourcePixels(state, imageSize, cropRect, {
118
- left: true,
119
- top: true,
120
- right: true,
121
- bottom: true
122
- });
160
+ return snapCropRectEdgesToSourcePixels(state, imageSize, cropRect, {
161
+ left: true,
162
+ top: true,
163
+ right: true,
164
+ bottom: true
165
+ });
123
166
  }
124
167
  function snapCropRectEdgesToSourcePixels(state, imageSize, cropRect, edges) {
125
- if (!isValidSize(imageSize)) {
126
- return cropRect;
127
- }
128
- const stateWithCrop = sanitizeCropperState({ ...state, cropRect });
129
- const region = getSourceRegion(stateWithCrop, imageSize);
130
- const shouldSnapX = edges.left || edges.right;
131
- const shouldSnapY = edges.top || edges.bottom;
132
- const left = edges.left ? Math.round(region.x) : region.x;
133
- const top = edges.top ? Math.round(region.y) : region.y;
134
- const right = edges.right ? Math.round(region.x + region.width) : region.x + region.width;
135
- const bottom = edges.bottom ? Math.round(region.y + region.height) : region.y + region.height;
136
- if (right <= left || bottom <= top) {
137
- return cropRect;
138
- }
139
- const snappedCropRect = getCropRectFromSourceRegion(
140
- stateWithCrop,
141
- imageSize,
142
- {
143
- ...region,
144
- x: left,
145
- y: top,
146
- width: right - left,
147
- height: bottom - top
148
- }
149
- );
150
- if (!snappedCropRect) {
151
- return cropRect;
152
- }
153
- return {
154
- x: shouldSnapX ? snappedCropRect.x : cropRect.x,
155
- y: shouldSnapY ? snappedCropRect.y : cropRect.y,
156
- width: shouldSnapX ? snappedCropRect.width : cropRect.width,
157
- height: shouldSnapY ? snappedCropRect.height : cropRect.height
158
- };
168
+ if (!isValidSize(imageSize)) return cropRect;
169
+ const stateWithCrop = sanitizeCropperState({
170
+ ...state,
171
+ cropRect
172
+ });
173
+ const region = getSourceRegion(stateWithCrop, imageSize);
174
+ const shouldSnapX = edges.left || edges.right;
175
+ const shouldSnapY = edges.top || edges.bottom;
176
+ const left = edges.left ? Math.round(region.x) : region.x;
177
+ const top = edges.top ? Math.round(region.y) : region.y;
178
+ const right = edges.right ? Math.round(region.x + region.width) : region.x + region.width;
179
+ const bottom = edges.bottom ? Math.round(region.y + region.height) : region.y + region.height;
180
+ if (right <= left || bottom <= top) return cropRect;
181
+ const snappedCropRect = getCropRectFromSourceRegion(stateWithCrop, imageSize, {
182
+ ...region,
183
+ x: left,
184
+ y: top,
185
+ width: right - left,
186
+ height: bottom - top
187
+ });
188
+ if (!snappedCropRect) return cropRect;
189
+ return {
190
+ x: shouldSnapX ? snappedCropRect.x : cropRect.x,
191
+ y: shouldSnapY ? snappedCropRect.y : cropRect.y,
192
+ width: shouldSnapX ? snappedCropRect.width : cropRect.width,
193
+ height: shouldSnapY ? snappedCropRect.height : cropRect.height
194
+ };
159
195
  }
196
+ /**
197
+ * Get the selected image region as percentages of the source image dimensions.
198
+ *
199
+ * Returns `{ x, y, width, height }` where each value is a percentage (0–100)
200
+ * of the source image's natural width or height. This format is compatible
201
+ * with the WordPress REST API attachments `/edit` endpoint and CSS-based
202
+ * crop workflows.
203
+ *
204
+ * Internally delegates to `getSourceRegion` and divides by the image
205
+ * dimensions, so accuracy is identical.
206
+ *
207
+ * @param state The current cropper state.
208
+ * @param imageSize The natural dimensions of the source image.
209
+ * @return The crop region as percentages (0–100).
210
+ */
160
211
  function getSourceRegionPercent(state, imageSize) {
161
- if (!isValidSize(imageSize)) {
162
- return { x: 0, y: 0, width: 0, height: 0 };
163
- }
164
- const region = getSourceRegion(state, imageSize);
165
- return {
166
- x: region.x / imageSize.width * 100,
167
- y: region.y / imageSize.height * 100,
168
- width: region.width / imageSize.width * 100,
169
- height: region.height / imageSize.height * 100
170
- };
212
+ if (!isValidSize(imageSize)) return {
213
+ x: 0,
214
+ y: 0,
215
+ width: 0,
216
+ height: 0
217
+ };
218
+ const region = getSourceRegion(state, imageSize);
219
+ return {
220
+ x: region.x / imageSize.width * 100,
221
+ y: region.y / imageSize.height * 100,
222
+ width: region.width / imageSize.width * 100,
223
+ height: region.height / imageSize.height * 100
224
+ };
171
225
  }
172
- export {
173
- getSourceRegion,
174
- getSourceRegionPercent,
175
- snapCropRectToSourcePixelGrid,
176
- snapCropRectToSourcePixels
177
- };
178
- //# sourceMappingURL=source-region.mjs.map
226
+ export { getSourceRegion, getSourceRegionPercent, snapCropRectToSourcePixelGrid, snapCropRectToSourcePixels };
227
+
228
+ //# sourceMappingURL=source-region.mjs.map
@@ -1,7 +1 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/image-editor/core/source-region.ts"],
4
- "sourcesContent": ["import { mat2d, vec2 } from 'gl-matrix';\nimport type {\n\tCropperState,\n\tHandlePosition,\n\tNormalizedRect,\n\tSize,\n} from './types';\nimport { createCamera, getRotatedBBox, getVisibleBounds } from './camera';\nimport { isValidSize, sanitizeCropperState } from './math/sanitize';\nimport { normalizeRotation } from './math/rotation';\n\n/**\n * The selected image region in source-pixel coordinates.\n */\nexport interface SourceRegion {\n\t/** X offset in source pixels. */\n\tx: number;\n\t/** Y offset in source pixels. */\n\ty: number;\n\t/** Width in source pixels. */\n\twidth: number;\n\t/** Height in source pixels. */\n\theight: number;\n\t/** Rotation in degrees (0-360). */\n\trotation: number;\n\t/** Flip state. */\n\tflip: { horizontal: boolean; vertical: boolean };\n\t/** Zoom factor applied. */\n\tzoom: number;\n}\n\n/**\n * Get the selected image region in source-pixel coordinates.\n *\n * Converts the current crop state (normalized visual space) to the actual\n * region of the original image that's selected. This is the bridge between\n * the cropper's internal coordinate system and external tools (image\n * processing libraries, AI APIs, server-side processing) that work in\n * source-pixel coordinates.\n *\n * The returned rectangle accounts for pan, zoom, and the crop rect position,\n * but expresses the crop in the unrotated image's coordinate space. Rotation\n * and flip are included as separate fields since they represent transforms,\n * not a region.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @return The selected region in source pixels plus rotation/flip metadata.\n */\nexport function getSourceRegion(\n\tstate: CropperState,\n\timageSize: Size\n): SourceRegion {\n\t// Sanitize before the zero-size short-circuit so the early return\n\t// doesn't leak raw non-finite state.rotation or state.zoom out\n\t// through the metadata fields. (state.flip is boolean and isn't\n\t// at risk of NaN propagation.)\n\tconst safeState = sanitizeCropperState( state );\n\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn {\n\t\t\tx: 0,\n\t\t\ty: 0,\n\t\t\twidth: 0,\n\t\t\theight: 0,\n\t\t\trotation: safeState.rotation,\n\t\t\tflip: { ...safeState.flip },\n\t\t\tzoom: safeState.zoom,\n\t\t};\n\t}\n\n\t// Use a synthetic 1:1 container so the camera maps normalized coords\n\t// to a known pixel space. The container size cancels out.\n\tconst syntheticContainer: Size = { width: 1000, height: 1000 };\n\tconst camera = createCamera( safeState, syntheticContainer, imageSize );\n\n\t// Inverse camera maps screen pixels back to normalized [0,1] world coords.\n\tconst inv = mat2d.create();\n\tmat2d.invert( inv, camera );\n\n\t// The crop rect center in screen space. We need the base camera\n\t// (zoom=1, no pan) to locate the visual bounds, then place the\n\t// crop rect within them.\n\tconst baseCamera = createCamera(\n\t\t{ ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },\n\t\tsyntheticContainer,\n\t\timageSize\n\t);\n\tconst visibleBounds = getVisibleBounds( baseCamera );\n\n\tconst cropRect = safeState.cropRect;\n\tconst cropCenterScreenX =\n\t\tvisibleBounds.left +\n\t\t( cropRect.x + cropRect.width / 2 ) * visibleBounds.width;\n\tconst cropCenterScreenY =\n\t\tvisibleBounds.top +\n\t\t( cropRect.y + cropRect.height / 2 ) * visibleBounds.height;\n\n\t// Transform crop center through inverse camera to get source position.\n\tconst srcCenter = vec2.create();\n\tvec2.transformMat2d(\n\t\tsrcCenter,\n\t\t[ cropCenterScreenX, cropCenterScreenY ],\n\t\tinv\n\t);\n\n\t// Crop rect size in the snap-rotation visual space, divided by zoom\n\t// for source-pixel dimensions. Matches the stencil's reference frame.\n\tconst snapRotation = Math.round( safeState.rotation / 90 ) * 90;\n\tconst { width: rotW, height: rotH } = getRotatedBBox(\n\t\timageSize.width,\n\t\timageSize.height,\n\t\tsnapRotation\n\t);\n\tconst sourceW = ( cropRect.width * rotW ) / safeState.zoom;\n\tconst sourceH = ( cropRect.height * rotH ) / safeState.zoom;\n\n\treturn {\n\t\tx: srcCenter[ 0 ] * imageSize.width - sourceW / 2,\n\t\ty: srcCenter[ 1 ] * imageSize.height - sourceH / 2,\n\t\twidth: sourceW,\n\t\theight: sourceH,\n\t\trotation: safeState.rotation,\n\t\tflip: { ...safeState.flip },\n\t\tzoom: safeState.zoom,\n\t};\n}\n\n/**\n * Convert a source-pixel region back to the cropper's normalized crop rect.\n *\n * The math intentionally mirrors `getSourceRegion`: map the requested source\n * center through the current camera, then express that screen point inside the\n * same base visible bounds used to position crop overlays.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @param region The source-pixel region to represent as a crop rect.\n * @return The normalized crop rect, or null when inputs cannot be mapped.\n */\nfunction getCropRectFromSourceRegion(\n\tstate: CropperState,\n\timageSize: Size,\n\tregion: SourceRegion\n): NormalizedRect | null {\n\tif (\n\t\t! isValidSize( imageSize ) ||\n\t\tregion.width <= 0 ||\n\t\tregion.height <= 0\n\t) {\n\t\treturn null;\n\t}\n\n\tconst safeState = sanitizeCropperState( state );\n\tconst syntheticContainer: Size = { width: 1000, height: 1000 };\n\tconst baseCamera = createCamera(\n\t\t{ ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },\n\t\tsyntheticContainer,\n\t\timageSize\n\t);\n\tconst visibleBounds = getVisibleBounds( baseCamera );\n\tif ( visibleBounds.width <= 0 || visibleBounds.height <= 0 ) {\n\t\treturn null;\n\t}\n\n\tconst sourceCenter = vec2.fromValues(\n\t\t( region.x + region.width / 2 ) / imageSize.width,\n\t\t( region.y + region.height / 2 ) / imageSize.height\n\t);\n\tconst screenCenter = vec2.create();\n\tconst camera = createCamera( safeState, syntheticContainer, imageSize );\n\tvec2.transformMat2d( screenCenter, sourceCenter, camera );\n\n\tconst snapRotation = Math.round( safeState.rotation / 90 ) * 90;\n\tconst { width: rotW, height: rotH } = getRotatedBBox(\n\t\timageSize.width,\n\t\timageSize.height,\n\t\tsnapRotation\n\t);\n\tif ( rotW <= 0 || rotH <= 0 ) {\n\t\treturn null;\n\t}\n\n\tconst width = ( region.width * safeState.zoom ) / rotW;\n\tconst height = ( region.height * safeState.zoom ) / rotH;\n\tconst centerX =\n\t\t( screenCenter[ 0 ] - visibleBounds.left ) / visibleBounds.width;\n\tconst centerY =\n\t\t( screenCenter[ 1 ] - visibleBounds.top ) / visibleBounds.height;\n\n\treturn {\n\t\tx: centerX - width / 2,\n\t\ty: centerY - height / 2,\n\t\twidth,\n\t\theight,\n\t};\n}\n\ninterface SourcePixelSnapEdges {\n\tleft: boolean;\n\ttop: boolean;\n\tright: boolean;\n\tbottom: boolean;\n}\n\nconst CARDINAL_ROTATION_EPSILON = 1e-6;\n\n/**\n * Check whether a rotation is effectively at a 90-degree stop.\n *\n * @param rotation The rotation angle in degrees.\n * @return Whether the rotation is close enough to a cardinal angle.\n */\nfunction isCardinalRotation( rotation: number ): boolean {\n\tconst normalizedRotation = normalizeRotation( rotation );\n\tconst nearestCardinal = Math.round( normalizedRotation / 90 ) * 90;\n\treturn (\n\t\tMath.abs( normalizedRotation - nearestCardinal ) <\n\t\tCARDINAL_ROTATION_EPSILON\n\t);\n}\n\n/**\n * Snap a crop rect so the selected source-region edges land on whole pixels.\n *\n * This is a display-time affordance for visible pixel grids; it does not alter\n * pan/zoom/rotation. Callers decide when snapping is appropriate.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @param cropRect The candidate crop rect from resize math.\n * @param handle The active resize handle. Only edges moved by the handle snap.\n * @return A crop rect whose selected source-region edges are whole pixels.\n */\nexport function snapCropRectToSourcePixels(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect,\n\thandle: HandlePosition\n): NormalizedRect {\n\tconst safeState = sanitizeCropperState( state );\n\tif ( ! isCardinalRotation( safeState.rotation ) ) {\n\t\treturn cropRect;\n\t}\n\n\treturn snapCropRectEdgesToSourcePixels( safeState, imageSize, cropRect, {\n\t\tleft: handle.includes( 'w' ),\n\t\ttop: handle.includes( 'n' ),\n\t\tright: handle.includes( 'e' ),\n\t\tbottom: handle.includes( 's' ),\n\t} );\n}\n\n/**\n * Snap all source-region edges for a crop rect to the nearest whole pixels.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @param cropRect The crop rect to align to the source-pixel grid.\n * @return A crop rect whose source-region edges are whole pixels.\n */\nexport function snapCropRectToSourcePixelGrid(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect\n): NormalizedRect {\n\treturn snapCropRectEdgesToSourcePixels( state, imageSize, cropRect, {\n\t\tleft: true,\n\t\ttop: true,\n\t\tright: true,\n\t\tbottom: true,\n\t} );\n}\n\nfunction snapCropRectEdgesToSourcePixels(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect,\n\tedges: SourcePixelSnapEdges\n): NormalizedRect {\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn cropRect;\n\t}\n\n\tconst stateWithCrop = sanitizeCropperState( { ...state, cropRect } );\n\tconst region = getSourceRegion( stateWithCrop, imageSize );\n\tconst shouldSnapX = edges.left || edges.right;\n\tconst shouldSnapY = edges.top || edges.bottom;\n\tconst left = edges.left ? Math.round( region.x ) : region.x;\n\tconst top = edges.top ? Math.round( region.y ) : region.y;\n\tconst right = edges.right\n\t\t? Math.round( region.x + region.width )\n\t\t: region.x + region.width;\n\tconst bottom = edges.bottom\n\t\t? Math.round( region.y + region.height )\n\t\t: region.y + region.height;\n\tif ( right <= left || bottom <= top ) {\n\t\treturn cropRect;\n\t}\n\n\tconst snappedCropRect = getCropRectFromSourceRegion(\n\t\tstateWithCrop,\n\t\timageSize,\n\t\t{\n\t\t\t...region,\n\t\t\tx: left,\n\t\t\ty: top,\n\t\t\twidth: right - left,\n\t\t\theight: bottom - top,\n\t\t}\n\t);\n\tif ( ! snappedCropRect ) {\n\t\treturn cropRect;\n\t}\n\n\treturn {\n\t\tx: shouldSnapX ? snappedCropRect.x : cropRect.x,\n\t\ty: shouldSnapY ? snappedCropRect.y : cropRect.y,\n\t\twidth: shouldSnapX ? snappedCropRect.width : cropRect.width,\n\t\theight: shouldSnapY ? snappedCropRect.height : cropRect.height,\n\t};\n}\n\n/**\n * The selected crop region expressed as percentages of the source image.\n */\nexport interface SourceRegionPercent {\n\t/** X offset as a percentage (0–100) of the source image width. */\n\tx: number;\n\t/** Y offset as a percentage (0–100) of the source image height. */\n\ty: number;\n\t/** Width as a percentage (0–100) of the source image width. */\n\twidth: number;\n\t/** Height as a percentage (0–100) of the source image height. */\n\theight: number;\n}\n\n/**\n * Get the selected image region as percentages of the source image dimensions.\n *\n * Returns `{ x, y, width, height }` where each value is a percentage (0–100)\n * of the source image's natural width or height. This format is compatible\n * with the WordPress REST API attachments `/edit` endpoint and CSS-based\n * crop workflows.\n *\n * Internally delegates to `getSourceRegion` and divides by the image\n * dimensions, so accuracy is identical.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @return The crop region as percentages (0–100).\n */\nexport function getSourceRegionPercent(\n\tstate: CropperState,\n\timageSize: Size\n): SourceRegionPercent {\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn { x: 0, y: 0, width: 0, height: 0 };\n\t}\n\tconst region = getSourceRegion( state, imageSize );\n\treturn {\n\t\tx: ( region.x / imageSize.width ) * 100,\n\t\ty: ( region.y / imageSize.height ) * 100,\n\t\twidth: ( region.width / imageSize.width ) * 100,\n\t\theight: ( region.height / imageSize.height ) * 100,\n\t};\n}\n"],
5
- "mappings": ";AAAA,SAA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":3,"file":"source-region.mjs","names":[],"sources":["../../../src/image-editor/core/source-region.ts"],"sourcesContent":["import { mat2d, vec2 } from 'gl-matrix';\nimport type {\n\tCropperState,\n\tHandlePosition,\n\tNormalizedRect,\n\tSize,\n} from './types';\nimport { createCamera, getRotatedBBox, getVisibleBounds } from './camera';\nimport { isValidSize, sanitizeCropperState } from './math/sanitize';\nimport { normalizeRotation } from './math/rotation';\n\n/**\n * The selected image region in source-pixel coordinates.\n */\nexport interface SourceRegion {\n\t/** X offset in source pixels. */\n\tx: number;\n\t/** Y offset in source pixels. */\n\ty: number;\n\t/** Width in source pixels. */\n\twidth: number;\n\t/** Height in source pixels. */\n\theight: number;\n\t/** Rotation in degrees (0-360). */\n\trotation: number;\n\t/** Flip state. */\n\tflip: { horizontal: boolean; vertical: boolean };\n\t/** Zoom factor applied. */\n\tzoom: number;\n}\n\n/**\n * Get the selected image region in source-pixel coordinates.\n *\n * Converts the current crop state (normalized visual space) to the actual\n * region of the original image that's selected. This is the bridge between\n * the cropper's internal coordinate system and external tools (image\n * processing libraries, AI APIs, server-side processing) that work in\n * source-pixel coordinates.\n *\n * The returned rectangle accounts for pan, zoom, and the crop rect position,\n * but expresses the crop in the unrotated image's coordinate space. Rotation\n * and flip are included as separate fields since they represent transforms,\n * not a region.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @return The selected region in source pixels plus rotation/flip metadata.\n */\nexport function getSourceRegion(\n\tstate: CropperState,\n\timageSize: Size\n): SourceRegion {\n\t// Sanitize before the zero-size short-circuit so the early return\n\t// doesn't leak raw non-finite state.rotation or state.zoom out\n\t// through the metadata fields. (state.flip is boolean and isn't\n\t// at risk of NaN propagation.)\n\tconst safeState = sanitizeCropperState( state );\n\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn {\n\t\t\tx: 0,\n\t\t\ty: 0,\n\t\t\twidth: 0,\n\t\t\theight: 0,\n\t\t\trotation: safeState.rotation,\n\t\t\tflip: { ...safeState.flip },\n\t\t\tzoom: safeState.zoom,\n\t\t};\n\t}\n\n\t// Use a synthetic 1:1 container so the camera maps normalized coords\n\t// to a known pixel space. The container size cancels out.\n\tconst syntheticContainer: Size = { width: 1000, height: 1000 };\n\tconst camera = createCamera( safeState, syntheticContainer, imageSize );\n\n\t// Inverse camera maps screen pixels back to normalized [0,1] world coords.\n\tconst inv = mat2d.create();\n\tmat2d.invert( inv, camera );\n\n\t// The crop rect center in screen space. We need the base camera\n\t// (zoom=1, no pan) to locate the visual bounds, then place the\n\t// crop rect within them.\n\tconst baseCamera = createCamera(\n\t\t{ ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },\n\t\tsyntheticContainer,\n\t\timageSize\n\t);\n\tconst visibleBounds = getVisibleBounds( baseCamera );\n\n\tconst cropRect = safeState.cropRect;\n\tconst cropCenterScreenX =\n\t\tvisibleBounds.left +\n\t\t( cropRect.x + cropRect.width / 2 ) * visibleBounds.width;\n\tconst cropCenterScreenY =\n\t\tvisibleBounds.top +\n\t\t( cropRect.y + cropRect.height / 2 ) * visibleBounds.height;\n\n\t// Transform crop center through inverse camera to get source position.\n\tconst srcCenter = vec2.create();\n\tvec2.transformMat2d(\n\t\tsrcCenter,\n\t\t[ cropCenterScreenX, cropCenterScreenY ],\n\t\tinv\n\t);\n\n\t// Crop rect size in the snap-rotation visual space, divided by zoom\n\t// for source-pixel dimensions. Matches the stencil's reference frame.\n\tconst snapRotation = Math.round( safeState.rotation / 90 ) * 90;\n\tconst { width: rotW, height: rotH } = getRotatedBBox(\n\t\timageSize.width,\n\t\timageSize.height,\n\t\tsnapRotation\n\t);\n\tconst sourceW = ( cropRect.width * rotW ) / safeState.zoom;\n\tconst sourceH = ( cropRect.height * rotH ) / safeState.zoom;\n\n\treturn {\n\t\tx: srcCenter[ 0 ] * imageSize.width - sourceW / 2,\n\t\ty: srcCenter[ 1 ] * imageSize.height - sourceH / 2,\n\t\twidth: sourceW,\n\t\theight: sourceH,\n\t\trotation: safeState.rotation,\n\t\tflip: { ...safeState.flip },\n\t\tzoom: safeState.zoom,\n\t};\n}\n\n/**\n * Convert a source-pixel region back to the cropper's normalized crop rect.\n *\n * The math intentionally mirrors `getSourceRegion`: map the requested source\n * center through the current camera, then express that screen point inside the\n * same base visible bounds used to position crop overlays.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @param region The source-pixel region to represent as a crop rect.\n * @return The normalized crop rect, or null when inputs cannot be mapped.\n */\nfunction getCropRectFromSourceRegion(\n\tstate: CropperState,\n\timageSize: Size,\n\tregion: SourceRegion\n): NormalizedRect | null {\n\tif (\n\t\t! isValidSize( imageSize ) ||\n\t\tregion.width <= 0 ||\n\t\tregion.height <= 0\n\t) {\n\t\treturn null;\n\t}\n\n\tconst safeState = sanitizeCropperState( state );\n\tconst syntheticContainer: Size = { width: 1000, height: 1000 };\n\tconst baseCamera = createCamera(\n\t\t{ ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },\n\t\tsyntheticContainer,\n\t\timageSize\n\t);\n\tconst visibleBounds = getVisibleBounds( baseCamera );\n\tif ( visibleBounds.width <= 0 || visibleBounds.height <= 0 ) {\n\t\treturn null;\n\t}\n\n\tconst sourceCenter = vec2.fromValues(\n\t\t( region.x + region.width / 2 ) / imageSize.width,\n\t\t( region.y + region.height / 2 ) / imageSize.height\n\t);\n\tconst screenCenter = vec2.create();\n\tconst camera = createCamera( safeState, syntheticContainer, imageSize );\n\tvec2.transformMat2d( screenCenter, sourceCenter, camera );\n\n\tconst snapRotation = Math.round( safeState.rotation / 90 ) * 90;\n\tconst { width: rotW, height: rotH } = getRotatedBBox(\n\t\timageSize.width,\n\t\timageSize.height,\n\t\tsnapRotation\n\t);\n\tif ( rotW <= 0 || rotH <= 0 ) {\n\t\treturn null;\n\t}\n\n\tconst width = ( region.width * safeState.zoom ) / rotW;\n\tconst height = ( region.height * safeState.zoom ) / rotH;\n\tconst centerX =\n\t\t( screenCenter[ 0 ] - visibleBounds.left ) / visibleBounds.width;\n\tconst centerY =\n\t\t( screenCenter[ 1 ] - visibleBounds.top ) / visibleBounds.height;\n\n\treturn {\n\t\tx: centerX - width / 2,\n\t\ty: centerY - height / 2,\n\t\twidth,\n\t\theight,\n\t};\n}\n\ninterface SourcePixelSnapEdges {\n\tleft: boolean;\n\ttop: boolean;\n\tright: boolean;\n\tbottom: boolean;\n}\n\nconst CARDINAL_ROTATION_EPSILON = 1e-6;\n\n/**\n * Check whether a rotation is effectively at a 90-degree stop.\n *\n * @param rotation The rotation angle in degrees.\n * @return Whether the rotation is close enough to a cardinal angle.\n */\nfunction isCardinalRotation( rotation: number ): boolean {\n\tconst normalizedRotation = normalizeRotation( rotation );\n\tconst nearestCardinal = Math.round( normalizedRotation / 90 ) * 90;\n\treturn (\n\t\tMath.abs( normalizedRotation - nearestCardinal ) <\n\t\tCARDINAL_ROTATION_EPSILON\n\t);\n}\n\n/**\n * Snap a crop rect so the selected source-region edges land on whole pixels.\n *\n * This is a display-time affordance for visible pixel grids; it does not alter\n * pan/zoom/rotation. Callers decide when snapping is appropriate.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @param cropRect The candidate crop rect from resize math.\n * @param handle The active resize handle. Only edges moved by the handle snap.\n * @return A crop rect whose selected source-region edges are whole pixels.\n */\nexport function snapCropRectToSourcePixels(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect,\n\thandle: HandlePosition\n): NormalizedRect {\n\tconst safeState = sanitizeCropperState( state );\n\tif ( ! isCardinalRotation( safeState.rotation ) ) {\n\t\treturn cropRect;\n\t}\n\n\treturn snapCropRectEdgesToSourcePixels( safeState, imageSize, cropRect, {\n\t\tleft: handle.includes( 'w' ),\n\t\ttop: handle.includes( 'n' ),\n\t\tright: handle.includes( 'e' ),\n\t\tbottom: handle.includes( 's' ),\n\t} );\n}\n\n/**\n * Snap all source-region edges for a crop rect to the nearest whole pixels.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @param cropRect The crop rect to align to the source-pixel grid.\n * @return A crop rect whose source-region edges are whole pixels.\n */\nexport function snapCropRectToSourcePixelGrid(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect\n): NormalizedRect {\n\treturn snapCropRectEdgesToSourcePixels( state, imageSize, cropRect, {\n\t\tleft: true,\n\t\ttop: true,\n\t\tright: true,\n\t\tbottom: true,\n\t} );\n}\n\nfunction snapCropRectEdgesToSourcePixels(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect,\n\tedges: SourcePixelSnapEdges\n): NormalizedRect {\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn cropRect;\n\t}\n\n\tconst stateWithCrop = sanitizeCropperState( { ...state, cropRect } );\n\tconst region = getSourceRegion( stateWithCrop, imageSize );\n\tconst shouldSnapX = edges.left || edges.right;\n\tconst shouldSnapY = edges.top || edges.bottom;\n\tconst left = edges.left ? Math.round( region.x ) : region.x;\n\tconst top = edges.top ? Math.round( region.y ) : region.y;\n\tconst right = edges.right\n\t\t? Math.round( region.x + region.width )\n\t\t: region.x + region.width;\n\tconst bottom = edges.bottom\n\t\t? Math.round( region.y + region.height )\n\t\t: region.y + region.height;\n\tif ( right <= left || bottom <= top ) {\n\t\treturn cropRect;\n\t}\n\n\tconst snappedCropRect = getCropRectFromSourceRegion(\n\t\tstateWithCrop,\n\t\timageSize,\n\t\t{\n\t\t\t...region,\n\t\t\tx: left,\n\t\t\ty: top,\n\t\t\twidth: right - left,\n\t\t\theight: bottom - top,\n\t\t}\n\t);\n\tif ( ! snappedCropRect ) {\n\t\treturn cropRect;\n\t}\n\n\treturn {\n\t\tx: shouldSnapX ? snappedCropRect.x : cropRect.x,\n\t\ty: shouldSnapY ? snappedCropRect.y : cropRect.y,\n\t\twidth: shouldSnapX ? snappedCropRect.width : cropRect.width,\n\t\theight: shouldSnapY ? snappedCropRect.height : cropRect.height,\n\t};\n}\n\n/**\n * The selected crop region expressed as percentages of the source image.\n */\nexport interface SourceRegionPercent {\n\t/** X offset as a percentage (0–100) of the source image width. */\n\tx: number;\n\t/** Y offset as a percentage (0–100) of the source image height. */\n\ty: number;\n\t/** Width as a percentage (0–100) of the source image width. */\n\twidth: number;\n\t/** Height as a percentage (0–100) of the source image height. */\n\theight: number;\n}\n\n/**\n * Get the selected image region as percentages of the source image dimensions.\n *\n * Returns `{ x, y, width, height }` where each value is a percentage (0–100)\n * of the source image's natural width or height. This format is compatible\n * with the WordPress REST API attachments `/edit` endpoint and CSS-based\n * crop workflows.\n *\n * Internally delegates to `getSourceRegion` and divides by the image\n * dimensions, so accuracy is identical.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @return The crop region as percentages (0–100).\n */\nexport function getSourceRegionPercent(\n\tstate: CropperState,\n\timageSize: Size\n): SourceRegionPercent {\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn { x: 0, y: 0, width: 0, height: 0 };\n\t}\n\tconst region = getSourceRegion( state, imageSize );\n\treturn {\n\t\tx: ( region.x / imageSize.width ) * 100,\n\t\ty: ( region.y / imageSize.height ) * 100,\n\t\twidth: ( region.width / imageSize.width ) * 100,\n\t\theight: ( region.height / imageSize.height ) * 100,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAiDA,SAAgB,gBACf,OACA,WACe;CAKf,MAAM,YAAY,qBAAsB,KAAM;CAE9C,IAAK,CAAE,YAAa,SAAU,GAC7B,OAAO;EACN,GAAG;EACH,GAAG;EACH,OAAO;EACP,QAAQ;EACR,UAAU,UAAU;EACpB,MAAM,EAAE,GAAG,UAAU,KAAK;EAC1B,MAAM,UAAU;CACjB;CAKD,MAAM,qBAA2B;EAAE,OAAO;EAAM,QAAQ;CAAK;CAC7D,MAAM,SAAS,aAAc,WAAW,oBAAoB,SAAU;CAGtE,MAAM,MAAM,MAAM,OAAO;CACzB,MAAM,OAAQ,KAAK,MAAO;CAK1B,MAAM,aAAa,aAClB;EAAE,GAAG;EAAW,KAAK;GAAE,GAAG;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