@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,722 +1,595 @@
1
1
  "use strict";
2
+ Object.defineProperties(exports, {
3
+ __esModule: { value: true },
4
+ [Symbol.toStringTag]: { value: "Module" }
5
+ });
6
+ // \0rolldown/runtime.js
2
7
  var __create = Object.create;
3
8
  var __defProp = Object.defineProperty;
4
9
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
10
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
11
  var __getProtoOf = Object.getPrototypeOf;
7
12
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
13
  var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
14
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
15
+ key = keys[i];
16
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
17
+ get: ((k) => from[k]).bind(null, key),
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ return to;
19
22
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
24
+ value: mod,
25
+ enumerable: true
26
+ }) : target, mod));
27
+ let clsx = require("clsx");
28
+ clsx = __toESM(clsx);
29
+ let _wordpress_element = require("@wordpress/element");
30
+ let _wordpress_i18n = require("@wordpress/i18n");
31
+ let ______core_camera_ts = require("../../core/camera.cjs");
32
+ let ______core_containment_ts = require("../../core/containment.cjs");
33
+ let ______core_constants_ts = require("../../core/constants.cjs");
34
+ let ______core_crop_rect_ts = require("../../core/crop-rect.cjs");
35
+ let ______core_stencil_math_ts = require("../../core/stencil-math.cjs");
36
+ let ___hooks_use_interaction_ts = require("../hooks/use-interaction.cjs");
37
+ let ___hooks_use_transform_style_ts = require("../hooks/use-transform-style.cjs");
38
+ let ___hooks_use_aria_announcer_ts = require("../hooks/use-aria-announcer.cjs");
39
+ let __stencils_rectangle_stencil_tsx = require("./stencils/rectangle-stencil.cjs");
40
+ let __overlays_dimming_overlay_tsx = require("./overlays/dimming-overlay.cjs");
41
+ let __overlays_grid_overlay_tsx = require("./overlays/grid-overlay.cjs");
42
+ let __overlays_dimensions_overlay_tsx = require("./overlays/dimensions-overlay.cjs");
43
+ let ______core_source_region_ts = require("../../core/source-region.cjs");
44
+ let __viewport_provider_tsx = require("./viewport-provider.cjs");
45
+ let ___visually_hidden_style_ts = require("../visually-hidden-style.cjs");
46
+ let react_jsx_runtime = require("react/jsx-runtime");
30
47
  // packages/media-editor/src/image-editor/react/components/cropper.tsx
31
- var cropper_exports = {};
32
- __export(cropper_exports, {
33
- Cropper: () => Cropper
34
- });
35
- module.exports = __toCommonJS(cropper_exports);
36
- var import_clsx = __toESM(require("clsx"));
37
- var import_element = require("@wordpress/element");
38
- var import_i18n = require("@wordpress/i18n");
39
- var import_camera = require("../../core/camera.cjs");
40
- var import_containment = require("../../core/containment.cjs");
41
- var import_constants = require("../../core/constants.cjs");
42
- var import_crop_rect = require("../../core/crop-rect.cjs");
43
- var import_stencil_math = require("../../core/stencil-math.cjs");
44
- var import_use_interaction = require("../hooks/use-interaction.cjs");
45
- var import_use_transform_style = require("../hooks/use-transform-style.cjs");
46
- var import_use_aria_announcer = require("../hooks/use-aria-announcer.cjs");
47
- var import_rectangle_stencil = require("./stencils/rectangle-stencil.cjs");
48
- var import_dimming_overlay = require("./overlays/dimming-overlay.cjs");
49
- var import_grid_overlay = require("./overlays/grid-overlay.cjs");
50
- var import_dimensions_overlay = require("./overlays/dimensions-overlay.cjs");
51
- var import_source_region = require("../../core/source-region.cjs");
52
- var import_viewport_provider = require("./viewport-provider.cjs");
53
- var import_visually_hidden_style = require("../visually-hidden-style.cjs");
54
- var import_jsx_runtime = require("react/jsx-runtime");
55
- var CROP_RECT_EPSILON = 1e-6;
56
- var CARDINAL_ROTATION_EPSILON = 1e-6;
57
- var SETTLE_TRANSITION_DURATION_MS = 200;
58
- var SETTLE_TRANSITION_FALLBACK_MS = SETTLE_TRANSITION_DURATION_MS + 100;
59
- var SETTLE_TRANSFORM_TRANSITION = `transform ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`;
60
- var SETTLE_STENCIL_TRANSITION = [
61
- `left ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`,
62
- `top ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`,
63
- `width ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`,
64
- `height ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`
48
+ /** Threshold for comparing normalized crop rect values. */
49
+ const CROP_RECT_EPSILON = 1e-6;
50
+ /** Threshold for deciding whether rotation is at a 90-degree stop. */
51
+ const CARDINAL_ROTATION_EPSILON = 1e-6;
52
+ /** Duration of the settle transform transition after resize end. */
53
+ const SETTLE_TRANSITION_DURATION_MS = 200;
54
+ const SETTLE_TRANSITION_FALLBACK_MS = 300;
55
+ const SETTLE_TRANSFORM_TRANSITION = `transform ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`;
56
+ const SETTLE_STENCIL_TRANSITION = [
57
+ `left ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`,
58
+ `top ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`,
59
+ `width ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`,
60
+ `height ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`
65
61
  ].join(", ");
66
- function CropperInner({
67
- src,
68
- controller,
69
- stencil: StencilComponent = import_rectangle_stencil.RectangleStencil,
70
- showGrid = false,
71
- isPlacementActive = false,
72
- showDimming = true,
73
- showDimensions = true,
74
- minZoom,
75
- maxZoom,
76
- aspectRatio,
77
- freeformCrop = false,
78
- focusOnMount = false,
79
- disabled = false,
80
- onImageLoaded,
81
- onStateChange,
82
- onGestureStart,
83
- onGestureEnd,
84
- className
85
- }, ref) {
86
- const {
87
- state,
88
- setImage,
89
- setCropRect,
90
- settleCrop,
91
- setVisualSize,
92
- adjustCropRectForViewport
93
- } = controller;
94
- const {
95
- viewport: viewportState,
96
- setViewportPan,
97
- resetViewport
98
- } = (0, import_viewport_provider.useViewport)();
99
- const canvasRef = (0, import_element.useRef)(null);
100
- const cropAreaDescriptionId = (0, import_element.useId)();
101
- const [isCropAreaFocused, setIsCropAreaFocused] = (0, import_element.useState)(focusOnMount);
102
- const [isFocusVisible, setIsFocusVisible] = (0, import_element.useState)(false);
103
- const [canvasSize, setCanvasSize] = (0, import_element.useState)({
104
- width: 0,
105
- height: 0
106
- });
107
- (0, import_element.useLayoutEffect)(() => {
108
- if (focusOnMount) {
109
- canvasRef.current?.focus({ preventScroll: true });
110
- }
111
- }, [focusOnMount]);
112
- const handleCropAreaFocus = (0, import_element.useCallback)(
113
- (event) => {
114
- const target = event.target;
115
- if (target === event.currentTarget) {
116
- setIsCropAreaFocused(true);
117
- }
118
- if (event.relatedTarget !== null && target.matches(":focus-visible")) {
119
- setIsFocusVisible(true);
120
- }
121
- },
122
- []
123
- );
124
- const handleCropAreaBlur = (0, import_element.useCallback)(
125
- (event) => {
126
- if (event.target === event.currentTarget) {
127
- setIsCropAreaFocused(false);
128
- }
129
- if (!event.currentTarget.contains(
130
- event.relatedTarget
131
- )) {
132
- setIsFocusVisible(false);
133
- }
134
- },
135
- []
136
- );
137
- (0, import_element.useEffect)(() => {
138
- const element = canvasRef.current;
139
- if (!element) {
140
- return;
141
- }
142
- const observer = new ResizeObserver((entries) => {
143
- for (const entry of entries) {
144
- const { width, height } = entry.contentRect;
145
- setCanvasSize((prev) => {
146
- if (prev.width === width && prev.height === height) {
147
- return prev;
148
- }
149
- return { width, height };
150
- });
151
- }
152
- });
153
- observer.observe(element);
154
- return () => {
155
- observer.disconnect();
156
- };
157
- }, []);
158
- (0, import_element.useEffect)(() => {
159
- onStateChange?.(state);
160
- }, [state, onStateChange]);
161
- (0, import_use_aria_announcer.useAriaAnnouncer)(state);
162
- const naturalWidth = state.image?.naturalWidth ?? 0;
163
- const naturalHeight = state.image?.naturalHeight ?? 0;
164
- const { elementSize, visualSize } = (0, import_element.useMemo)(
165
- () => (0, import_camera.getImageFit)(
166
- canvasSize,
167
- { width: naturalWidth, height: naturalHeight },
168
- state.rotation
169
- ),
170
- [canvasSize, naturalWidth, naturalHeight, state.rotation]
171
- );
172
- (0, import_element.useEffect)(() => {
173
- setVisualSize(visualSize);
174
- }, [visualSize, setVisualSize]);
175
- (0, import_element.useEffect)(() => {
176
- if (freeformCrop || visualSize.width === 0 || visualSize.height === 0 || !aspectRatio || aspectRatio <= 0) {
177
- return;
178
- }
179
- const rect = (0, import_crop_rect.computeInscribedRect)(aspectRatio, visualSize);
180
- const current = state.cropRect;
181
- if (Math.abs(current.x - rect.x) < CROP_RECT_EPSILON && Math.abs(current.y - rect.y) < CROP_RECT_EPSILON && Math.abs(current.width - rect.width) < CROP_RECT_EPSILON && Math.abs(current.height - rect.height) < CROP_RECT_EPSILON) {
182
- return;
183
- }
184
- adjustCropRectForViewport(rect);
185
- }, [
186
- freeformCrop,
187
- aspectRatio,
188
- visualSize,
189
- adjustCropRectForViewport,
190
- state.cropRect
191
- ]);
192
- const prevAspectRatioRef = (0, import_element.useRef)(aspectRatio);
193
- (0, import_element.useEffect)(() => {
194
- if (prevAspectRatioRef.current === aspectRatio) {
195
- return;
196
- }
197
- prevAspectRatioRef.current = aspectRatio;
198
- if (!freeformCrop || visualSize.width === 0 || visualSize.height === 0 || !aspectRatio || aspectRatio <= 0) {
199
- return;
200
- }
201
- const rect = (0, import_crop_rect.computeInscribedRect)(aspectRatio, visualSize);
202
- const current = state.cropRect;
203
- if (Math.abs(current.x - rect.x) < CROP_RECT_EPSILON && Math.abs(current.y - rect.y) < CROP_RECT_EPSILON && Math.abs(current.width - rect.width) < CROP_RECT_EPSILON && Math.abs(current.height - rect.height) < CROP_RECT_EPSILON) {
204
- return;
205
- }
206
- adjustCropRectForViewport(rect);
207
- }, [
208
- aspectRatio,
209
- freeformCrop,
210
- visualSize,
211
- adjustCropRectForViewport,
212
- state.cropRect
213
- ]);
214
- const cropBounds = (0, import_element.useMemo)(() => {
215
- if (!state.image || elementSize.width === 0) {
216
- return void 0;
217
- }
218
- return (0, import_containment.getImageCropBounds)(state, elementSize, visualSize);
219
- }, [state, elementSize, visualSize]);
220
- const effectiveMinZoom = minZoom !== void 0 ? minZoom : (0, import_containment.getMinZoom)(state);
221
- const [isResizing, setIsResizing] = (0, import_element.useState)(false);
222
- const isResizingRef = (0, import_element.useRef)(false);
223
- const isSettlingRef = (0, import_element.useRef)(false);
224
- const [isTouchPinching, setIsTouchPinchingState] = (0, import_element.useState)(false);
225
- const isTouchPinchingRef = (0, import_element.useRef)(false);
226
- const setTouchPinching = (0, import_element.useCallback)((isPinching) => {
227
- isTouchPinchingRef.current = isPinching;
228
- setIsTouchPinchingState(isPinching);
229
- }, []);
230
- const [activeHandle, setActiveHandle] = (0, import_element.useState)(
231
- null
232
- );
233
- const viewScaleRest = (0, import_element.useMemo)(
234
- () => (0, import_camera.getViewScale)(
235
- state.cropRect,
236
- canvasSize,
237
- visualSize,
238
- import_constants.SETTLE_TARGET_CANVAS_FILL,
239
- import_constants.MAX_VIEW_SCALE
240
- ),
241
- [state.cropRect, canvasSize, visualSize]
242
- );
243
- const [frozenViewScale, setFrozenViewScale] = (0, import_element.useState)(1);
244
- const viewScale = isResizing ? frozenViewScale : viewScaleRest;
245
- const scaledVisualSize = (0, import_element.useMemo)(
246
- () => ({
247
- width: visualSize.width * viewScale,
248
- height: visualSize.height * viewScale
249
- }),
250
- [visualSize.width, visualSize.height, viewScale]
251
- );
252
- const displayScale = naturalWidth > 0 ? elementSize.width / naturalWidth * state.zoom * viewScale : 0;
253
- const keyboardResizeStep = (0, import_element.useMemo)(() => {
254
- if (displayScale < import_constants.PIXEL_SNAP_DISPLAY_SCALE || aspectRatio && aspectRatio > 0 || naturalWidth <= 0 || naturalHeight <= 0) {
255
- return void 0;
256
- }
257
- const snapRotation = Math.round(state.rotation / 90) * 90;
258
- if (Math.abs(state.rotation - snapRotation) >= CARDINAL_ROTATION_EPSILON) {
259
- return void 0;
260
- }
261
- const bbox = (0, import_camera.getRotatedBBox)(
262
- naturalWidth,
263
- naturalHeight,
264
- snapRotation
265
- );
266
- if (bbox.width <= 0 || bbox.height <= 0) {
267
- return void 0;
268
- }
269
- return {
270
- width: state.zoom / bbox.width,
271
- height: state.zoom / bbox.height
272
- };
273
- }, [
274
- displayScale,
275
- aspectRatio,
276
- naturalWidth,
277
- naturalHeight,
278
- state.rotation,
279
- state.zoom
280
- ]);
281
- const minCropSize = (0, import_element.useMemo)(() => {
282
- if (naturalWidth <= 0 || naturalHeight <= 0) {
283
- return void 0;
284
- }
285
- const snapRotation = Math.round(state.rotation / 90) * 90;
286
- const bbox = (0, import_camera.getRotatedBBox)(
287
- naturalWidth,
288
- naturalHeight,
289
- snapRotation
290
- );
291
- const minPixels = (0, import_stencil_math.getMinCropPixels)(displayScale);
292
- return {
293
- width: Math.min(1, minPixels * state.zoom / bbox.width),
294
- height: Math.min(1, minPixels * state.zoom / bbox.height)
295
- };
296
- }, [
297
- naturalWidth,
298
- naturalHeight,
299
- state.rotation,
300
- state.zoom,
301
- displayScale
302
- ]);
303
- const snapCropRect = (0, import_element.useCallback)(
304
- (rect, handle) => {
305
- if (displayScale < import_constants.PIXEL_SNAP_DISPLAY_SCALE || naturalWidth <= 0 || naturalHeight <= 0) {
306
- return rect;
307
- }
308
- return (0, import_source_region.snapCropRectToSourcePixels)(
309
- state,
310
- { width: naturalWidth, height: naturalHeight },
311
- rect,
312
- handle
313
- );
314
- },
315
- [displayScale, naturalWidth, naturalHeight, state]
316
- );
317
- const wasPixelSnapEnabledRef = (0, import_element.useRef)(false);
318
- (0, import_element.useEffect)(() => {
319
- const isPixelSnapEnabled = freeformCrop && (!aspectRatio || aspectRatio <= 0) && displayScale >= import_constants.PIXEL_SNAP_DISPLAY_SCALE && naturalWidth > 0 && naturalHeight > 0;
320
- const wasPixelSnapEnabled = wasPixelSnapEnabledRef.current;
321
- wasPixelSnapEnabledRef.current = isPixelSnapEnabled;
322
- if (!isPixelSnapEnabled || wasPixelSnapEnabled) {
323
- return;
324
- }
325
- const snappedCropRect = (0, import_source_region.snapCropRectToSourcePixelGrid)(
326
- state,
327
- { width: naturalWidth, height: naturalHeight },
328
- state.cropRect
329
- );
330
- const currentCropRect = state.cropRect;
331
- if (Math.abs(currentCropRect.x - snappedCropRect.x) < CROP_RECT_EPSILON && Math.abs(currentCropRect.y - snappedCropRect.y) < CROP_RECT_EPSILON && Math.abs(currentCropRect.width - snappedCropRect.width) < CROP_RECT_EPSILON && Math.abs(currentCropRect.height - snappedCropRect.height) < CROP_RECT_EPSILON) {
332
- return;
333
- }
334
- adjustCropRectForViewport(snappedCropRect);
335
- }, [
336
- adjustCropRectForViewport,
337
- aspectRatio,
338
- displayScale,
339
- freeformCrop,
340
- naturalWidth,
341
- naturalHeight,
342
- state
343
- ]);
344
- const {
345
- handlers,
346
- onWheelNative,
347
- isDragging,
348
- isZooming,
349
- isPlacementActive: isInteractionPlacementActive
350
- } = (0, import_use_interaction.useInteraction)(state, controller, canvasSize, scaledVisualSize, {
351
- minZoom: effectiveMinZoom,
352
- maxZoom,
353
- onGestureStart,
354
- onGestureEnd,
355
- disabled
356
- });
357
- const canvasHandlers = {
358
- ...handlers,
359
- onPointerDownCapture: () => {
360
- setIsFocusVisible(false);
361
- },
362
- onTouchStartCapture: (event) => {
363
- if (event.touches.length > 1) {
364
- setTouchPinching(true);
365
- }
366
- },
367
- onTouchMoveCapture: (event) => {
368
- if (event.touches.length > 1) {
369
- setTouchPinching(true);
370
- }
371
- },
372
- onTouchEndCapture: (event) => {
373
- if (event.touches.length === 0) {
374
- setTouchPinching(false);
375
- }
376
- },
377
- onTouchCancelCapture: (event) => {
378
- if (event.touches.length === 0) {
379
- setTouchPinching(false);
380
- }
381
- },
382
- onKeyDownCapture: (event) => {
383
- if (!["Shift", "Control", "Alt", "Meta"].includes(event.key)) {
384
- setIsFocusVisible(true);
385
- }
386
- },
387
- onPointerDown: (event) => {
388
- if (disabled || isResizingRef.current || isTouchPinchingRef.current || event.pointerType === "touch" && event.isPrimary === false) {
389
- event.preventDefault();
390
- return;
391
- }
392
- handlers.onPointerDown?.(event);
393
- setIsFocusVisible(false);
394
- }
395
- };
396
- (0, import_element.useEffect)(() => {
397
- const el = canvasRef.current;
398
- if (!el) {
399
- return;
400
- }
401
- const handleWheel = (event) => {
402
- if (isResizingRef.current || isSettlingRef.current) {
403
- event.preventDefault();
404
- return;
405
- }
406
- onWheelNative(event);
407
- };
408
- el.addEventListener("wheel", handleWheel, {
409
- passive: false
410
- });
411
- return () => {
412
- el.removeEventListener("wheel", handleWheel);
413
- };
414
- }, [onWheelNative]);
415
- const transformString = (0, import_use_transform_style.useTransformStyle)(state, visualSize);
416
- const handleImageLoad = (0, import_element.useCallback)(
417
- (event) => {
418
- const img = event.currentTarget;
419
- const size = {
420
- width: img.naturalWidth,
421
- height: img.naturalHeight
422
- };
423
- setImage({
424
- src,
425
- naturalWidth: size.width,
426
- naturalHeight: size.height
427
- });
428
- onImageLoaded?.(size);
429
- },
430
- [src, setImage, onImageLoaded]
431
- );
432
- const handleCropChange = (0, import_element.useCallback)(
433
- (rect) => {
434
- if (isTouchPinchingRef.current) {
435
- return;
436
- }
437
- setCropRect(rect);
438
- if (isResizingRef.current && scaledVisualSize.width > 0 && scaledVisualSize.height > 0) {
439
- const offsetX = (canvasSize.width - scaledVisualSize.width) / 2;
440
- const offsetY = (canvasSize.height - scaledVisualSize.height) / 2;
441
- const rightOverflow = Math.max(
442
- 0,
443
- offsetX + (rect.x + rect.width) * scaledVisualSize.width - canvasSize.width
444
- );
445
- const leftOverflow = Math.max(
446
- 0,
447
- -(offsetX + rect.x * scaledVisualSize.width)
448
- );
449
- const bottomOverflow = Math.max(
450
- 0,
451
- offsetY + (rect.y + rect.height) * scaledVisualSize.height - canvasSize.height
452
- );
453
- const topOverflow = Math.max(
454
- 0,
455
- -(offsetY + rect.y * scaledVisualSize.height)
456
- );
457
- setViewportPan({
458
- x: -rightOverflow + leftOverflow,
459
- y: -bottomOverflow + topOverflow
460
- });
461
- }
462
- },
463
- [setCropRect, setViewportPan, canvasSize, scaledVisualSize]
464
- );
465
- const [settling, setSettling] = (0, import_element.useState)(false);
466
- const settleTimerRef = (0, import_element.useRef)(void 0);
467
- const finishSettling = (0, import_element.useCallback)(() => {
468
- clearTimeout(settleTimerRef.current);
469
- isSettlingRef.current = false;
470
- setSettling(false);
471
- }, []);
472
- (0, import_element.useEffect)(() => {
473
- return () => {
474
- clearTimeout(settleTimerRef.current);
475
- };
476
- }, []);
477
- const isInteractiveGrid = showGrid === "interactive";
478
- const showInteractiveGrid = isInteractiveGrid && (isInteractionPlacementActive || isResizing || isPlacementActive);
479
- const outputSize = (0, import_element.useMemo)(() => {
480
- if (!showDimensions || !activeHandle || !state.image) {
481
- return null;
482
- }
483
- const region = (0, import_source_region.getSourceRegion)(state, {
484
- width: state.image.naturalWidth,
485
- height: state.image.naturalHeight
486
- });
487
- return { width: region.width, height: region.height };
488
- }, [showDimensions, activeHandle, state]);
489
- const handleEscape = (0, import_element.useCallback)(() => {
490
- setIsFocusVisible(true);
491
- canvasRef.current?.focus({ preventScroll: true });
492
- }, []);
493
- const handleResizeStart = (0, import_element.useCallback)(
494
- (handle) => {
495
- if (isTouchPinchingRef.current) {
496
- return;
497
- }
498
- setFrozenViewScale(viewScaleRest);
499
- isResizingRef.current = true;
500
- setIsResizing(true);
501
- setActiveHandle(handle ?? null);
502
- finishSettling();
503
- resetViewport();
504
- onGestureStart?.();
505
- },
506
- [finishSettling, onGestureStart, resetViewport, viewScaleRest]
507
- );
508
- const handleResizeEnd = (0, import_element.useCallback)(() => {
509
- const isCancellingForPinch = isTouchPinchingRef.current;
510
- isResizingRef.current = false;
511
- setIsResizing(false);
512
- setActiveHandle(null);
513
- clearTimeout(settleTimerRef.current);
514
- if (isCancellingForPinch) {
515
- finishSettling();
516
- resetViewport();
517
- return;
518
- }
519
- isSettlingRef.current = true;
520
- setSettling(true);
521
- resetViewport();
522
- settleCrop();
523
- onGestureEnd?.();
524
- settleTimerRef.current = setTimeout(
525
- finishSettling,
526
- SETTLE_TRANSITION_FALLBACK_MS
527
- );
528
- }, [finishSettling, settleCrop, onGestureEnd, resetViewport]);
529
- const handleSettleTransitionEnd = (0, import_element.useCallback)(
530
- (event) => {
531
- if (!isSettlingRef.current || event.propertyName !== "transform") {
532
- return;
533
- }
534
- finishSettling();
535
- },
536
- [finishSettling]
537
- );
538
- let imageTransition;
539
- if (settling) {
540
- imageTransition = SETTLE_TRANSFORM_TRANSITION;
541
- } else if (isZooming) {
542
- imageTransition = "transform 150ms linear";
543
- }
544
- const settleStencilTransition = settling ? SETTLE_STENCIL_TRANSITION : void 0;
545
- const imageStyle = (0, import_element.useMemo)(() => {
546
- if (elementSize.width === 0 || elementSize.height === 0) {
547
- return {};
548
- }
549
- const centerX = (canvasSize.width - elementSize.width) / 2;
550
- const centerY = (canvasSize.height - elementSize.height) / 2;
551
- return {
552
- width: elementSize.width,
553
- height: elementSize.height,
554
- maxWidth: elementSize.width,
555
- maxHeight: elementSize.height,
556
- left: centerX,
557
- top: centerY,
558
- // Always lead with `scale()` (identity at rest) so the transform's
559
- // function list stays structurally constant across rest, resize, and
560
- // settle. A conditional that dropped `scale()` at viewScale === 1
561
- // would change the list shape exactly when the settle transition
562
- // crosses 1:1, forcing the browser to fall back to matrix-decomposition
563
- // interpolation instead of interpolating each function in turn.
564
- transform: `scale(${viewScale}) ${transformString}`,
565
- transition: imageTransition,
566
- imageRendering: displayScale > 1 ? "pixelated" : void 0
567
- };
568
- }, [
569
- canvasSize,
570
- elementSize,
571
- transformString,
572
- imageTransition,
573
- displayScale,
574
- viewScale
575
- ]);
576
- const settleTransition = settling ? SETTLE_TRANSFORM_TRANSITION : void 0;
577
- const willChange = isResizing || settling ? "transform" : void 0;
578
- let stageStyle;
579
- if (viewportState.pan.x !== 0 || viewportState.pan.y !== 0) {
580
- stageStyle = {
581
- transform: `translate(${viewportState.pan.x}px, ${viewportState.pan.y}px)`,
582
- transition: settleTransition,
583
- willChange
584
- };
585
- } else if (settling) {
586
- stageStyle = {
587
- transform: "translate(0px, 0px)",
588
- transition: settleTransition,
589
- willChange
590
- };
591
- }
592
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
593
- "div",
594
- {
595
- ref,
596
- className: (0, import_clsx.default)(
597
- "wp-media-editor-image-editor",
598
- isDragging && "wp-media-editor-image-editor--dragging",
599
- className
600
- ),
601
- children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
602
- "div",
603
- {
604
- ref: canvasRef,
605
- className: (0, import_clsx.default)(
606
- "wp-media-editor-image-editor__canvas",
607
- isInteractiveGrid && "wp-media-editor-image-editor__canvas--grid-interactive",
608
- showInteractiveGrid && "wp-media-editor-image-editor__canvas--show-grid",
609
- settling && "wp-media-editor-image-editor__canvas--settling",
610
- // Marks the cropper as in keyboard-interaction mode.
611
- // CSS uses :focus on the canvas to show the stencil
612
- // outline and :focus on a handle to show its ring,
613
- // so the class applies whenever any cropper element
614
- // has keyboard focus.
615
- isFocusVisible && "wp-media-editor-image-editor__canvas--focus-visible"
616
- ),
617
- tabIndex: 0,
618
- role: "group",
619
- "aria-label": (0, import_i18n.__)("Crop area"),
620
- "aria-describedby": isCropAreaFocused ? cropAreaDescriptionId : void 0,
621
- onFocus: handleCropAreaFocus,
622
- onBlur: handleCropAreaBlur,
623
- ...canvasHandlers,
624
- children: [
625
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
626
- "div",
627
- {
628
- id: cropAreaDescriptionId,
629
- style: import_visually_hidden_style.VISUALLY_HIDDEN_STYLE,
630
- children: (0, import_i18n.__)(
631
- "When this area is focused, use arrow keys to move the image and plus or minus to zoom. Tab to resize handles and controls."
632
- )
633
- }
634
- ),
635
- /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
636
- "div",
637
- {
638
- className: "wp-media-editor-image-editor__stage",
639
- "data-testid": "cropper-stage",
640
- style: stageStyle,
641
- onTransitionEnd: handleSettleTransitionEnd,
642
- children: [
643
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
644
- "img",
645
- {
646
- className: "wp-media-editor-image-editor__image",
647
- "data-testid": "cropper-image",
648
- src,
649
- alt: "",
650
- onLoad: handleImageLoad,
651
- style: imageStyle,
652
- draggable: false
653
- }
654
- ),
655
- showDimming && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
656
- import_dimming_overlay.DimmingOverlay,
657
- {
658
- cropRect: state.cropRect,
659
- containerSize: canvasSize,
660
- imageSize: scaledVisualSize,
661
- transition: settleStencilTransition
662
- }
663
- ),
664
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
665
- StencilComponent,
666
- {
667
- cropRect: state.cropRect,
668
- containerSize: canvasSize,
669
- imageSize: scaledVisualSize,
670
- onCropChange: handleCropChange,
671
- onResizeStart: handleResizeStart,
672
- onResizeEnd: handleResizeEnd,
673
- onEscape: handleEscape,
674
- aspectRatio,
675
- freeformCrop,
676
- isResizeDisabled: isTouchPinching || disabled,
677
- stencilTransition: settleStencilTransition,
678
- cropBounds,
679
- minCropSize,
680
- snapCropRect,
681
- keyboardResizeStep
682
- }
683
- ),
684
- (showGrid === true || isInteractiveGrid) && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
685
- import_grid_overlay.GridOverlay,
686
- {
687
- cropRect: state.cropRect,
688
- containerSize: canvasSize,
689
- imageSize: scaledVisualSize
690
- }
691
- ),
692
- activeHandle && outputSize && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
693
- import_dimensions_overlay.DimensionsOverlay,
694
- {
695
- cropRect: state.cropRect,
696
- containerSize: canvasSize,
697
- imageSize: scaledVisualSize,
698
- activeHandle,
699
- outputWidth: outputSize.width,
700
- outputHeight: outputSize.height
701
- }
702
- )
703
- ]
704
- }
705
- )
706
- ]
707
- }
708
- )
709
- }
710
- );
62
+ /**
63
+ * The main image cropper component.
64
+ *
65
+ * Renders an image within a container with interactive crop overlays.
66
+ * Creates the camera once per render via getImageFit, then passes
67
+ * derived values to stencil, overlays, and interaction hooks.
68
+ *
69
+ * The component fills its parent container (100% width and height).
70
+ * Wrap it in a sized container to control its dimensions.
71
+ *
72
+ * @param root0 Component props implementing CropperProps.
73
+ * @param root0.src Image source URL.
74
+ * @param root0.controller The cropper controller from `useCropperController` or a composite store.
75
+ * @param root0.stencil Custom stencil component.
76
+ * @param root0.showGrid Grid overlay mode: false | true | 'interactive'.
77
+ * @param root0.isPlacementActive Keep grid visible during external placement activity.
78
+ * @param root0.showDimming Show dimming overlay outside crop.
79
+ * @param root0.showDimensions Show live dimensions tooltip during resize.
80
+ * @param root0.minZoom Minimum zoom level override.
81
+ * @param root0.maxZoom Maximum zoom level.
82
+ * @param root0.aspectRatio Fixed aspect ratio (width/height).
83
+ * @param root0.freeformCrop Enable resize handles.
84
+ * @param root0.focusOnMount Focus the crop area on mount.
85
+ * @param root0.disabled Ignore all crop interaction.
86
+ * @param root0.onImageLoaded Image load callback.
87
+ * @param root0.onImageError Image error callback.
88
+ * @param root0.onGestureStart Gesture boundary start.
89
+ * @param root0.onGestureEnd Gesture boundary end.
90
+ * @param root0.className Additional CSS class.
91
+ * @param ref Forwarded ref for the container div.
92
+ */
93
+ function CropperInner({ src, controller, stencil: StencilComponent = __stencils_rectangle_stencil_tsx.RectangleStencil, showGrid = false, isPlacementActive = false, showDimming = true, showDimensions = true, minZoom, maxZoom, aspectRatio, freeformCrop = false, focusOnMount = false, disabled = false, onImageLoaded, onImageError, onGestureStart, onGestureEnd, className }, ref) {
94
+ const { state, setImage, setCropRect, settleCrop, setVisualSize, adjustCropRectForViewport } = controller;
95
+ const { viewport: viewportState, setViewportPan, resetViewport } = (0, __viewport_provider_tsx.useViewport)();
96
+ const canvasRef = (0, _wordpress_element.useRef)(null);
97
+ const cropAreaDescriptionId = (0, _wordpress_element.useId)();
98
+ const [isCropAreaFocused, setIsCropAreaFocused] = (0, _wordpress_element.useState)(focusOnMount);
99
+ const [isFocusVisible, setIsFocusVisible] = (0, _wordpress_element.useState)(false);
100
+ const [canvasSize, setCanvasSize] = (0, _wordpress_element.useState)({
101
+ width: 0,
102
+ height: 0
103
+ });
104
+ (0, _wordpress_element.useLayoutEffect)(() => {
105
+ if (focusOnMount) canvasRef.current?.focus({ preventScroll: true });
106
+ }, [focusOnMount]);
107
+ const handleCropAreaFocus = (0, _wordpress_element.useCallback)((event) => {
108
+ const target = event.target;
109
+ if (target === event.currentTarget) setIsCropAreaFocused(true);
110
+ if (event.relatedTarget !== null && target.matches(":focus-visible")) setIsFocusVisible(true);
111
+ }, []);
112
+ const handleCropAreaBlur = (0, _wordpress_element.useCallback)((event) => {
113
+ if (event.target === event.currentTarget) setIsCropAreaFocused(false);
114
+ if (!event.currentTarget.contains(event.relatedTarget)) setIsFocusVisible(false);
115
+ }, []);
116
+ (0, _wordpress_element.useEffect)(() => {
117
+ const element = canvasRef.current;
118
+ if (!element) return;
119
+ const observer = new ResizeObserver((entries) => {
120
+ for (const entry of entries) {
121
+ const { width, height } = entry.contentRect;
122
+ (0, _wordpress_element.flushSync)(() => {
123
+ setCanvasSize((prev) => {
124
+ if (prev.width === width && prev.height === height) return prev;
125
+ return {
126
+ width,
127
+ height
128
+ };
129
+ });
130
+ });
131
+ }
132
+ });
133
+ observer.observe(element);
134
+ return () => {
135
+ observer.disconnect();
136
+ };
137
+ }, []);
138
+ (0, ___hooks_use_aria_announcer_ts.useAriaAnnouncer)(state);
139
+ const naturalWidth = state.image?.naturalWidth ?? 0;
140
+ const naturalHeight = state.image?.naturalHeight ?? 0;
141
+ const { elementSize, visualSize } = (0, _wordpress_element.useMemo)(() => (0, ______core_camera_ts.getImageFit)(canvasSize, {
142
+ width: naturalWidth,
143
+ height: naturalHeight
144
+ }, state.rotation), [
145
+ canvasSize,
146
+ naturalWidth,
147
+ naturalHeight,
148
+ state.rotation
149
+ ]);
150
+ (0, _wordpress_element.useEffect)(() => {
151
+ setVisualSize(visualSize);
152
+ }, [visualSize, setVisualSize]);
153
+ (0, _wordpress_element.useEffect)(() => {
154
+ if (freeformCrop || visualSize.width === 0 || visualSize.height === 0 || !aspectRatio || aspectRatio <= 0) return;
155
+ const rect = (0, ______core_crop_rect_ts.computeInscribedRect)(aspectRatio, visualSize);
156
+ const current = state.cropRect;
157
+ if (Math.abs(current.x - rect.x) < CROP_RECT_EPSILON && Math.abs(current.y - rect.y) < CROP_RECT_EPSILON && Math.abs(current.width - rect.width) < CROP_RECT_EPSILON && Math.abs(current.height - rect.height) < CROP_RECT_EPSILON) return;
158
+ adjustCropRectForViewport(rect);
159
+ }, [
160
+ freeformCrop,
161
+ aspectRatio,
162
+ visualSize,
163
+ adjustCropRectForViewport,
164
+ state.cropRect
165
+ ]);
166
+ const prevAspectRatioRef = (0, _wordpress_element.useRef)(aspectRatio);
167
+ (0, _wordpress_element.useEffect)(() => {
168
+ if (prevAspectRatioRef.current === aspectRatio) return;
169
+ prevAspectRatioRef.current = aspectRatio;
170
+ if (!freeformCrop || visualSize.width === 0 || visualSize.height === 0 || !aspectRatio || aspectRatio <= 0) return;
171
+ const rect = (0, ______core_crop_rect_ts.computeInscribedRect)(aspectRatio, visualSize);
172
+ const current = state.cropRect;
173
+ if (Math.abs(current.x - rect.x) < CROP_RECT_EPSILON && Math.abs(current.y - rect.y) < CROP_RECT_EPSILON && Math.abs(current.width - rect.width) < CROP_RECT_EPSILON && Math.abs(current.height - rect.height) < CROP_RECT_EPSILON) return;
174
+ adjustCropRectForViewport(rect);
175
+ }, [
176
+ aspectRatio,
177
+ freeformCrop,
178
+ visualSize,
179
+ adjustCropRectForViewport,
180
+ state.cropRect
181
+ ]);
182
+ const cropBounds = (0, _wordpress_element.useMemo)(() => {
183
+ if (!state.image || elementSize.width === 0) return;
184
+ return (0, ______core_containment_ts.getImageCropBounds)(state, elementSize, visualSize);
185
+ }, [
186
+ state,
187
+ elementSize,
188
+ visualSize
189
+ ]);
190
+ const effectiveMinZoom = minZoom !== void 0 ? minZoom : (0, ______core_containment_ts.getMinZoom)(state);
191
+ const [isResizing, setIsResizing] = (0, _wordpress_element.useState)(false);
192
+ const isResizingRef = (0, _wordpress_element.useRef)(false);
193
+ const isSettlingRef = (0, _wordpress_element.useRef)(false);
194
+ const [isTouchPinching, setIsTouchPinchingState] = (0, _wordpress_element.useState)(false);
195
+ const isTouchPinchingRef = (0, _wordpress_element.useRef)(false);
196
+ const setTouchPinching = (0, _wordpress_element.useCallback)((isPinching) => {
197
+ isTouchPinchingRef.current = isPinching;
198
+ setIsTouchPinchingState(isPinching);
199
+ }, []);
200
+ const [activeHandle, setActiveHandle] = (0, _wordpress_element.useState)(null);
201
+ const viewScaleRest = (0, _wordpress_element.useMemo)(() => (0, ______core_camera_ts.getViewScale)(state.cropRect, canvasSize, visualSize, ______core_constants_ts.SETTLE_TARGET_CANVAS_FILL, ______core_constants_ts.MAX_VIEW_SCALE), [
202
+ state.cropRect,
203
+ canvasSize,
204
+ visualSize
205
+ ]);
206
+ const [frozenViewScale, setFrozenViewScale] = (0, _wordpress_element.useState)(1);
207
+ const viewScale = isResizing ? frozenViewScale : viewScaleRest;
208
+ const scaledVisualSize = (0, _wordpress_element.useMemo)(() => ({
209
+ width: visualSize.width * viewScale,
210
+ height: visualSize.height * viewScale
211
+ }), [
212
+ visualSize.width,
213
+ visualSize.height,
214
+ viewScale
215
+ ]);
216
+ const displayScale = naturalWidth > 0 ? elementSize.width / naturalWidth * state.zoom * viewScale : 0;
217
+ const keyboardResizeStep = (0, _wordpress_element.useMemo)(() => {
218
+ if (displayScale < ______core_constants_ts.PIXEL_SNAP_DISPLAY_SCALE || aspectRatio && aspectRatio > 0 || naturalWidth <= 0 || naturalHeight <= 0) return;
219
+ const snapRotation = Math.round(state.rotation / 90) * 90;
220
+ if (Math.abs(state.rotation - snapRotation) >= CARDINAL_ROTATION_EPSILON) return;
221
+ const bbox = (0, ______core_camera_ts.getRotatedBBox)(naturalWidth, naturalHeight, snapRotation);
222
+ if (bbox.width <= 0 || bbox.height <= 0) return;
223
+ return {
224
+ width: state.zoom / bbox.width,
225
+ height: state.zoom / bbox.height
226
+ };
227
+ }, [
228
+ displayScale,
229
+ aspectRatio,
230
+ naturalWidth,
231
+ naturalHeight,
232
+ state.rotation,
233
+ state.zoom
234
+ ]);
235
+ const minCropSize = (0, _wordpress_element.useMemo)(() => {
236
+ if (naturalWidth <= 0 || naturalHeight <= 0) return;
237
+ const snapRotation = Math.round(state.rotation / 90) * 90;
238
+ const bbox = (0, ______core_camera_ts.getRotatedBBox)(naturalWidth, naturalHeight, snapRotation);
239
+ const minPixels = (0, ______core_stencil_math_ts.getMinCropPixels)(displayScale);
240
+ return {
241
+ width: Math.min(1, minPixels * state.zoom / bbox.width),
242
+ height: Math.min(1, minPixels * state.zoom / bbox.height)
243
+ };
244
+ }, [
245
+ naturalWidth,
246
+ naturalHeight,
247
+ state.rotation,
248
+ state.zoom,
249
+ displayScale
250
+ ]);
251
+ const snapCropRect = (0, _wordpress_element.useCallback)((rect, handle) => {
252
+ if (displayScale < ______core_constants_ts.PIXEL_SNAP_DISPLAY_SCALE || naturalWidth <= 0 || naturalHeight <= 0) return rect;
253
+ return (0, ______core_source_region_ts.snapCropRectToSourcePixels)(state, {
254
+ width: naturalWidth,
255
+ height: naturalHeight
256
+ }, rect, handle);
257
+ }, [
258
+ displayScale,
259
+ naturalWidth,
260
+ naturalHeight,
261
+ state
262
+ ]);
263
+ const wasPixelSnapEnabledRef = (0, _wordpress_element.useRef)(false);
264
+ (0, _wordpress_element.useEffect)(() => {
265
+ const isPixelSnapEnabled = freeformCrop && (!aspectRatio || aspectRatio <= 0) && displayScale >= ______core_constants_ts.PIXEL_SNAP_DISPLAY_SCALE && naturalWidth > 0 && naturalHeight > 0;
266
+ const wasPixelSnapEnabled = wasPixelSnapEnabledRef.current;
267
+ wasPixelSnapEnabledRef.current = isPixelSnapEnabled;
268
+ if (!isPixelSnapEnabled || wasPixelSnapEnabled) return;
269
+ const snappedCropRect = (0, ______core_source_region_ts.snapCropRectToSourcePixelGrid)(state, {
270
+ width: naturalWidth,
271
+ height: naturalHeight
272
+ }, state.cropRect);
273
+ const currentCropRect = state.cropRect;
274
+ if (Math.abs(currentCropRect.x - snappedCropRect.x) < CROP_RECT_EPSILON && Math.abs(currentCropRect.y - snappedCropRect.y) < CROP_RECT_EPSILON && Math.abs(currentCropRect.width - snappedCropRect.width) < CROP_RECT_EPSILON && Math.abs(currentCropRect.height - snappedCropRect.height) < CROP_RECT_EPSILON) return;
275
+ adjustCropRectForViewport(snappedCropRect);
276
+ }, [
277
+ adjustCropRectForViewport,
278
+ aspectRatio,
279
+ displayScale,
280
+ freeformCrop,
281
+ naturalWidth,
282
+ naturalHeight,
283
+ state
284
+ ]);
285
+ const { handlers, onWheelNative, isDragging, isZooming, isPlacementActive: isInteractionPlacementActive } = (0, ___hooks_use_interaction_ts.useInteraction)(state, controller, canvasSize, scaledVisualSize, {
286
+ minZoom: effectiveMinZoom,
287
+ maxZoom,
288
+ onGestureStart,
289
+ onGestureEnd,
290
+ disabled
291
+ });
292
+ const canvasHandlers = {
293
+ ...handlers,
294
+ onPointerDownCapture: () => {
295
+ setIsFocusVisible(false);
296
+ },
297
+ onTouchStartCapture: (event) => {
298
+ if (event.touches.length > 1) setTouchPinching(true);
299
+ },
300
+ onTouchMoveCapture: (event) => {
301
+ if (event.touches.length > 1) setTouchPinching(true);
302
+ },
303
+ onTouchEndCapture: (event) => {
304
+ if (event.touches.length === 0) setTouchPinching(false);
305
+ },
306
+ onTouchCancelCapture: (event) => {
307
+ if (event.touches.length === 0) setTouchPinching(false);
308
+ },
309
+ onKeyDownCapture: (event) => {
310
+ if (![
311
+ "Shift",
312
+ "Control",
313
+ "Alt",
314
+ "Meta"
315
+ ].includes(event.key)) setIsFocusVisible(true);
316
+ },
317
+ onPointerDown: (event) => {
318
+ if (disabled || isResizingRef.current || isTouchPinchingRef.current || event.pointerType === "touch" && event.isPrimary === false) {
319
+ event.preventDefault();
320
+ return;
321
+ }
322
+ handlers.onPointerDown?.(event);
323
+ setIsFocusVisible(false);
324
+ }
325
+ };
326
+ (0, _wordpress_element.useEffect)(() => {
327
+ const el = canvasRef.current;
328
+ if (!el) return;
329
+ const handleWheel = (event) => {
330
+ if (isResizingRef.current || isSettlingRef.current) {
331
+ event.preventDefault();
332
+ return;
333
+ }
334
+ onWheelNative(event);
335
+ };
336
+ el.addEventListener("wheel", handleWheel, { passive: false });
337
+ return () => {
338
+ el.removeEventListener("wheel", handleWheel);
339
+ };
340
+ }, [onWheelNative]);
341
+ const transformString = (0, ___hooks_use_transform_style_ts.useTransformStyle)(state, visualSize);
342
+ /**
343
+ * Handle the image load event.
344
+ */
345
+ const handleImageLoad = (0, _wordpress_element.useCallback)((event) => {
346
+ const img = event.currentTarget;
347
+ const size = {
348
+ width: img.naturalWidth,
349
+ height: img.naturalHeight
350
+ };
351
+ setImage({
352
+ src,
353
+ naturalWidth: size.width,
354
+ naturalHeight: size.height
355
+ });
356
+ onImageLoaded?.(size);
357
+ }, [
358
+ src,
359
+ setImage,
360
+ onImageLoaded
361
+ ]);
362
+ const handleCropChange = (0, _wordpress_element.useCallback)((rect) => {
363
+ if (isTouchPinchingRef.current) return;
364
+ setCropRect(rect);
365
+ if (isResizingRef.current && scaledVisualSize.width > 0 && scaledVisualSize.height > 0) {
366
+ const offsetX = (canvasSize.width - scaledVisualSize.width) / 2;
367
+ const offsetY = (canvasSize.height - scaledVisualSize.height) / 2;
368
+ const rightOverflow = Math.max(0, offsetX + (rect.x + rect.width) * scaledVisualSize.width - canvasSize.width);
369
+ const leftOverflow = Math.max(0, -(offsetX + rect.x * scaledVisualSize.width));
370
+ const bottomOverflow = Math.max(0, offsetY + (rect.y + rect.height) * scaledVisualSize.height - canvasSize.height);
371
+ const topOverflow = Math.max(0, -(offsetY + rect.y * scaledVisualSize.height));
372
+ setViewportPan({
373
+ x: -rightOverflow + leftOverflow,
374
+ y: -bottomOverflow + topOverflow
375
+ });
376
+ }
377
+ }, [
378
+ setCropRect,
379
+ setViewportPan,
380
+ canvasSize,
381
+ scaledVisualSize
382
+ ]);
383
+ const [settling, setSettling] = (0, _wordpress_element.useState)(false);
384
+ const settleTimerRef = (0, _wordpress_element.useRef)(void 0);
385
+ const finishSettling = (0, _wordpress_element.useCallback)(() => {
386
+ clearTimeout(settleTimerRef.current);
387
+ isSettlingRef.current = false;
388
+ setSettling(false);
389
+ }, []);
390
+ (0, _wordpress_element.useEffect)(() => {
391
+ return () => {
392
+ clearTimeout(settleTimerRef.current);
393
+ };
394
+ }, []);
395
+ const isInteractiveGrid = showGrid === "interactive";
396
+ const showInteractiveGrid = isInteractiveGrid && (isInteractionPlacementActive || isResizing || isPlacementActive);
397
+ const outputSize = (0, _wordpress_element.useMemo)(() => {
398
+ if (!showDimensions || !activeHandle || !state.image) return null;
399
+ const region = (0, ______core_source_region_ts.getSourceRegion)(state, {
400
+ width: state.image.naturalWidth,
401
+ height: state.image.naturalHeight
402
+ });
403
+ return {
404
+ width: region.width,
405
+ height: region.height
406
+ };
407
+ }, [
408
+ showDimensions,
409
+ activeHandle,
410
+ state
411
+ ]);
412
+ /**
413
+ * Handle Escape on a resize handle — return focus to the canvas so
414
+ * arrow keys pan the image rather than resize.
415
+ */
416
+ const handleEscape = (0, _wordpress_element.useCallback)(() => {
417
+ setIsFocusVisible(true);
418
+ canvasRef.current?.focus({ preventScroll: true });
419
+ }, []);
420
+ const handleResizeStart = (0, _wordpress_element.useCallback)((handle) => {
421
+ if (isTouchPinchingRef.current) return;
422
+ setFrozenViewScale(viewScaleRest);
423
+ isResizingRef.current = true;
424
+ setIsResizing(true);
425
+ setActiveHandle(handle ?? null);
426
+ finishSettling();
427
+ resetViewport();
428
+ onGestureStart?.();
429
+ }, [
430
+ finishSettling,
431
+ onGestureStart,
432
+ resetViewport,
433
+ viewScaleRest
434
+ ]);
435
+ /**
436
+ * Handle resize end — settle the crop rect (re-center, fill height)
437
+ * and reset the viewport pan back to neutral.
438
+ */
439
+ const handleResizeEnd = (0, _wordpress_element.useCallback)(() => {
440
+ const isCancellingForPinch = isTouchPinchingRef.current;
441
+ isResizingRef.current = false;
442
+ setIsResizing(false);
443
+ setActiveHandle(null);
444
+ clearTimeout(settleTimerRef.current);
445
+ if (isCancellingForPinch) {
446
+ finishSettling();
447
+ resetViewport();
448
+ return;
449
+ }
450
+ isSettlingRef.current = true;
451
+ setSettling(true);
452
+ resetViewport();
453
+ settleCrop();
454
+ onGestureEnd?.();
455
+ settleTimerRef.current = setTimeout(finishSettling, SETTLE_TRANSITION_FALLBACK_MS);
456
+ }, [
457
+ finishSettling,
458
+ settleCrop,
459
+ onGestureEnd,
460
+ resetViewport
461
+ ]);
462
+ /**
463
+ * Finish settling when the settle transform transition actually completes.
464
+ * The handler lives on the stage, but in the common (un-panned) case the
465
+ * transform transition runs on the image layer and its `transitionend`
466
+ * bubbles up to here. Other transitionend events (stencil left/top/width/
467
+ * height) also bubble up, so only the transform transition should clear the
468
+ * settle state.
469
+ */
470
+ const handleSettleTransitionEnd = (0, _wordpress_element.useCallback)((event) => {
471
+ if (!isSettlingRef.current || event.propertyName !== "transform") return;
472
+ finishSettling();
473
+ }, [finishSettling]);
474
+ let imageTransition;
475
+ if (settling) imageTransition = SETTLE_TRANSFORM_TRANSITION;
476
+ else if (isZooming) imageTransition = "transform 150ms linear";
477
+ const settleStencilTransition = settling ? SETTLE_STENCIL_TRANSITION : void 0;
478
+ const imageStyle = (0, _wordpress_element.useMemo)(() => {
479
+ if (elementSize.width === 0 || elementSize.height === 0) return {};
480
+ const centerX = (canvasSize.width - elementSize.width) / 2;
481
+ const centerY = (canvasSize.height - elementSize.height) / 2;
482
+ return {
483
+ width: elementSize.width,
484
+ height: elementSize.height,
485
+ maxWidth: elementSize.width,
486
+ maxHeight: elementSize.height,
487
+ left: centerX,
488
+ top: centerY,
489
+ transform: `scale(${viewScale}) ${transformString}`,
490
+ transition: imageTransition,
491
+ imageRendering: displayScale > 1 ? "pixelated" : void 0
492
+ };
493
+ }, [
494
+ canvasSize,
495
+ elementSize,
496
+ transformString,
497
+ imageTransition,
498
+ displayScale,
499
+ viewScale
500
+ ]);
501
+ const settleTransition = settling ? SETTLE_TRANSFORM_TRANSITION : void 0;
502
+ const willChange = isResizing || settling ? "transform" : void 0;
503
+ let stageStyle;
504
+ if (viewportState.pan.x !== 0 || viewportState.pan.y !== 0) stageStyle = {
505
+ transform: `translate(${viewportState.pan.x}px, ${viewportState.pan.y}px)`,
506
+ transition: settleTransition,
507
+ willChange
508
+ };
509
+ else if (settling) stageStyle = {
510
+ transform: "translate(0px, 0px)",
511
+ transition: settleTransition,
512
+ willChange
513
+ };
514
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
515
+ ref,
516
+ className: (0, clsx.default)("wp-media-editor-image-editor", isDragging && "wp-media-editor-image-editor--dragging", className),
517
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
518
+ ref: canvasRef,
519
+ className: (0, clsx.default)("wp-media-editor-image-editor__canvas", isInteractiveGrid && "wp-media-editor-image-editor__canvas--grid-interactive", showInteractiveGrid && "wp-media-editor-image-editor__canvas--show-grid", settling && "wp-media-editor-image-editor__canvas--settling", isFocusVisible && "wp-media-editor-image-editor__canvas--focus-visible"),
520
+ tabIndex: 0,
521
+ role: "group",
522
+ "aria-label": (0, _wordpress_i18n.__)("Crop area"),
523
+ "aria-describedby": isCropAreaFocused ? cropAreaDescriptionId : void 0,
524
+ onFocus: handleCropAreaFocus,
525
+ onBlur: handleCropAreaBlur,
526
+ ...canvasHandlers,
527
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
528
+ id: cropAreaDescriptionId,
529
+ style: ___visually_hidden_style_ts.VISUALLY_HIDDEN_STYLE,
530
+ children: (0, _wordpress_i18n.__)("When this area is focused, use arrow keys to move the image and plus or minus to zoom. Tab to resize handles and controls.")
531
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
532
+ className: "wp-media-editor-image-editor__stage",
533
+ "data-testid": "cropper-stage",
534
+ style: stageStyle,
535
+ onTransitionEnd: handleSettleTransitionEnd,
536
+ children: [
537
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", {
538
+ className: "wp-media-editor-image-editor__image",
539
+ "data-testid": "cropper-image",
540
+ src,
541
+ alt: "",
542
+ onLoad: handleImageLoad,
543
+ onError: onImageError,
544
+ style: imageStyle,
545
+ draggable: false
546
+ }),
547
+ showDimming && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__overlays_dimming_overlay_tsx.DimmingOverlay, {
548
+ cropRect: state.cropRect,
549
+ containerSize: canvasSize,
550
+ imageSize: scaledVisualSize,
551
+ transition: settleStencilTransition
552
+ }),
553
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(StencilComponent, {
554
+ cropRect: state.cropRect,
555
+ containerSize: canvasSize,
556
+ imageSize: scaledVisualSize,
557
+ onCropChange: handleCropChange,
558
+ onResizeStart: handleResizeStart,
559
+ onResizeEnd: handleResizeEnd,
560
+ onEscape: handleEscape,
561
+ aspectRatio,
562
+ freeformCrop,
563
+ isResizeDisabled: isTouchPinching || disabled,
564
+ stencilTransition: settleStencilTransition,
565
+ cropBounds,
566
+ minCropSize,
567
+ snapCropRect,
568
+ keyboardResizeStep
569
+ }),
570
+ (showGrid === true || isInteractiveGrid) && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__overlays_grid_overlay_tsx.GridOverlay, {
571
+ cropRect: state.cropRect,
572
+ containerSize: canvasSize,
573
+ imageSize: scaledVisualSize
574
+ }),
575
+ activeHandle && outputSize && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__overlays_dimensions_overlay_tsx.DimensionsOverlay, {
576
+ cropRect: state.cropRect,
577
+ containerSize: canvasSize,
578
+ imageSize: scaledVisualSize,
579
+ activeHandle,
580
+ outputWidth: outputSize.width,
581
+ outputHeight: outputSize.height
582
+ })
583
+ ]
584
+ })]
585
+ })
586
+ });
711
587
  }
712
- var CropperInnerWithRef = (0, import_element.forwardRef)(
713
- CropperInner
714
- );
715
- var Cropper = (0, import_element.forwardRef)(
716
- (props, ref) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_viewport_provider.ViewportProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CropperInnerWithRef, { ...props, ref }) })
717
- );
718
- // Annotate the CommonJS export names for ESM import in node:
719
- 0 && (module.exports = {
720
- Cropper
721
- });
722
- //# sourceMappingURL=cropper.cjs.map
588
+ const CropperInnerWithRef = (0, _wordpress_element.forwardRef)(CropperInner);
589
+ const Cropper = (0, _wordpress_element.forwardRef)((props, ref) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__viewport_provider_tsx.ViewportProvider, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CropperInnerWithRef, {
590
+ ...props,
591
+ ref
592
+ }) }));
593
+ exports.Cropper = Cropper;
594
+
595
+ //# sourceMappingURL=cropper.cjs.map