@wordpress/media-editor 0.20.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (386) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +2 -1
  3. package/build/components/media-editor/index.cjs +533 -695
  4. package/build/components/media-editor/index.cjs.map +1 -7
  5. package/build/components/media-editor/use-crop-options.cjs +43 -58
  6. package/build/components/media-editor/use-crop-options.cjs.map +1 -7
  7. package/build/components/media-editor/use-save-media-editor.cjs +111 -185
  8. package/build/components/media-editor/use-save-media-editor.cjs.map +1 -7
  9. package/build/components/media-editor-canvas/index.cjs +122 -124
  10. package/build/components/media-editor-canvas/index.cjs.map +1 -7
  11. package/build/components/media-editor-crop-panel/index.cjs +65 -68
  12. package/build/components/media-editor-crop-panel/index.cjs.map +1 -7
  13. package/build/components/media-editor-fine-rotation/index.cjs +62 -72
  14. package/build/components/media-editor-fine-rotation/index.cjs.map +1 -7
  15. package/build/components/media-editor-image-controls/index.cjs +183 -257
  16. package/build/components/media-editor-image-controls/index.cjs.map +1 -7
  17. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +119 -157
  18. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +1 -7
  19. package/build/components/media-editor-modal/build-modifiers.cjs +107 -88
  20. package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -7
  21. package/build/components/media-editor-modal/index.cjs +112 -153
  22. package/build/components/media-editor-modal/index.cjs.map +1 -7
  23. package/build/components/media-editor-provider/index.cjs +32 -54
  24. package/build/components/media-editor-provider/index.cjs.map +1 -7
  25. package/build/components/media-form/index.cjs +77 -78
  26. package/build/components/media-form/index.cjs.map +1 -7
  27. package/build/components/media-preview/index.cjs +114 -114
  28. package/build/components/media-preview/index.cjs.map +1 -7
  29. package/build/components/rotation-ruler/index.cjs +181 -188
  30. package/build/components/rotation-ruler/index.cjs.map +1 -7
  31. package/build/components/rotation-ruler/use-ruler-drag.cjs +132 -149
  32. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -7
  33. package/build/hooks/use-crop-gesture-handlers.cjs +82 -76
  34. package/build/hooks/use-crop-gesture-handlers.cjs.map +1 -7
  35. package/build/image-editor/core/camera.cjs +257 -205
  36. package/build/image-editor/core/camera.cjs.map +1 -7
  37. package/build/image-editor/core/constants.cjs +127 -102
  38. package/build/image-editor/core/constants.cjs.map +1 -7
  39. package/build/image-editor/core/containment.cjs +275 -262
  40. package/build/image-editor/core/containment.cjs.map +1 -7
  41. package/build/image-editor/core/crop-rect.cjs +33 -44
  42. package/build/image-editor/core/crop-rect.cjs.map +1 -7
  43. package/build/image-editor/core/export/canvas-renderer.cjs +165 -135
  44. package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -7
  45. package/build/image-editor/core/index.cjs +88 -61
  46. package/build/image-editor/core/interaction-controller.cjs +583 -646
  47. package/build/image-editor/core/interaction-controller.cjs.map +1 -7
  48. package/build/image-editor/core/math/rotation.cjs +26 -35
  49. package/build/image-editor/core/math/rotation.cjs.map +1 -7
  50. package/build/image-editor/core/math/sanitize.cjs +124 -80
  51. package/build/image-editor/core/math/sanitize.cjs.map +1 -7
  52. package/build/image-editor/core/setter-helpers.cjs +57 -60
  53. package/build/image-editor/core/setter-helpers.cjs.map +1 -7
  54. package/build/image-editor/core/source-region.cjs +220 -190
  55. package/build/image-editor/core/source-region.cjs.map +1 -7
  56. package/build/image-editor/core/state.cjs +278 -265
  57. package/build/image-editor/core/state.cjs.map +1 -7
  58. package/build/image-editor/core/stencil-math.cjs +193 -206
  59. package/build/image-editor/core/stencil-math.cjs.map +1 -7
  60. package/build/image-editor/core/transform-style.cjs +39 -48
  61. package/build/image-editor/core/transform-style.cjs.map +1 -7
  62. package/build/image-editor/core/transforms/pipeline.cjs +36 -38
  63. package/build/image-editor/core/transforms/pipeline.cjs.map +1 -7
  64. package/build/image-editor/core/types.cjs +4 -18
  65. package/build/image-editor/core/viewport-state.cjs +31 -45
  66. package/build/image-editor/core/viewport-state.cjs.map +1 -7
  67. package/build/image-editor/index.cjs +65 -64
  68. package/build/image-editor/react/components/cropper-provider.cjs +44 -45
  69. package/build/image-editor/react/components/cropper-provider.cjs.map +1 -7
  70. package/build/image-editor/react/components/cropper.cjs +583 -720
  71. package/build/image-editor/react/components/cropper.cjs.map +1 -7
  72. package/build/image-editor/react/components/index.cjs +22 -33
  73. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +135 -160
  74. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -7
  75. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +28 -59
  76. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +1 -7
  77. package/build/image-editor/react/components/overlays/grid-overlay.cjs +58 -88
  78. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +1 -7
  79. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +361 -420
  80. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +1 -7
  81. package/build/image-editor/react/components/viewport-provider.cjs +21 -46
  82. package/build/image-editor/react/components/viewport-provider.cjs.map +1 -7
  83. package/build/image-editor/react/hooks/build-cropper-setters.cjs +76 -67
  84. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -7
  85. package/build/image-editor/react/hooks/index.cjs +16 -31
  86. package/build/image-editor/react/hooks/use-aria-announcer.cjs +90 -146
  87. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +1 -7
  88. package/build/image-editor/react/hooks/use-cropper-controller.cjs +80 -0
  89. package/build/image-editor/react/hooks/use-cropper-controller.cjs.map +1 -0
  90. package/build/image-editor/react/hooks/use-interaction.cjs +179 -205
  91. package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -7
  92. package/build/image-editor/react/hooks/use-transform-style.cjs +20 -34
  93. package/build/image-editor/react/hooks/use-transform-style.cjs.map +1 -7
  94. package/build/image-editor/react/hooks/use-viewport-state.cjs +36 -47
  95. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -7
  96. package/build/image-editor/react/visually-hidden-style.cjs +21 -38
  97. package/build/image-editor/react/visually-hidden-style.cjs.map +1 -7
  98. package/build/index.cjs +45 -39
  99. package/build/lock-unlock.cjs +9 -35
  100. package/build/lock-unlock.cjs.map +1 -7
  101. package/build/private-apis.cjs +16 -37
  102. package/build/private-apis.cjs.map +1 -7
  103. package/build/state/aspect-ratio-presets.cjs +53 -0
  104. package/build/state/aspect-ratio-presets.cjs.map +1 -0
  105. package/build/state/composite-reducer.cjs +136 -89
  106. package/build/state/composite-reducer.cjs.map +1 -7
  107. package/build/state/index.cjs +63 -47
  108. package/build/state/media-editor-state-provider.cjs +35 -48
  109. package/build/state/media-editor-state-provider.cjs.map +1 -7
  110. package/build/state/types.cjs +12 -31
  111. package/build/state/types.cjs.map +1 -7
  112. package/build/state/use-media-editor-state.cjs +261 -281
  113. package/build/state/use-media-editor-state.cjs.map +1 -7
  114. package/build/store/actions.cjs +17 -44
  115. package/build/store/actions.cjs.map +1 -7
  116. package/build/store/constants.cjs +8 -29
  117. package/build/store/constants.cjs.map +1 -7
  118. package/build/store/index.cjs +33 -40
  119. package/build/store/index.cjs.map +1 -7
  120. package/build/store/reducer.cjs +28 -50
  121. package/build/store/reducer.cjs.map +1 -7
  122. package/build/store/selectors.cjs +16 -41
  123. package/build/store/selectors.cjs.map +1 -7
  124. package/build/utils/get-media-type.cjs +18 -41
  125. package/build/utils/get-media-type.cjs.map +1 -7
  126. package/build/utils/index.cjs +9 -28
  127. package/build-module/components/media-editor/index.mjs +490 -682
  128. package/build-module/components/media-editor/index.mjs.map +1 -7
  129. package/build-module/components/media-editor/use-crop-options.mjs +38 -33
  130. package/build-module/components/media-editor/use-crop-options.mjs.map +1 -7
  131. package/build-module/components/media-editor/use-save-media-editor.mjs +89 -157
  132. package/build-module/components/media-editor/use-save-media-editor.mjs.map +1 -7
  133. package/build-module/components/media-editor-canvas/index.mjs +97 -95
  134. package/build-module/components/media-editor-canvas/index.mjs.map +1 -7
  135. package/build-module/components/media-editor-crop-panel/index.mjs +44 -43
  136. package/build-module/components/media-editor-crop-panel/index.mjs.map +1 -7
  137. package/build-module/components/media-editor-fine-rotation/index.mjs +39 -45
  138. package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -7
  139. package/build-module/components/media-editor-image-controls/index.mjs +172 -237
  140. package/build-module/components/media-editor-image-controls/index.mjs.map +1 -7
  141. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +112 -137
  142. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +1 -7
  143. package/build-module/components/media-editor-modal/build-modifiers.mjs +103 -64
  144. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +1 -7
  145. package/build-module/components/media-editor-modal/index.mjs +88 -122
  146. package/build-module/components/media-editor-modal/index.mjs.map +1 -7
  147. package/build-module/components/media-editor-provider/index.mjs +26 -28
  148. package/build-module/components/media-editor-provider/index.mjs.map +1 -7
  149. package/build-module/components/media-form/index.mjs +69 -54
  150. package/build-module/components/media-form/index.mjs.map +1 -7
  151. package/build-module/components/media-preview/index.mjs +105 -89
  152. package/build-module/components/media-preview/index.mjs.map +1 -7
  153. package/build-module/components/rotation-ruler/index.mjs +161 -164
  154. package/build-module/components/rotation-ruler/index.mjs.map +1 -7
  155. package/build-module/components/rotation-ruler/use-ruler-drag.mjs +125 -122
  156. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -7
  157. package/build-module/hooks/use-crop-gesture-handlers.mjs +76 -50
  158. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +1 -7
  159. package/build-module/image-editor/core/camera.mjs +245 -178
  160. package/build-module/image-editor/core/camera.mjs.map +1 -7
  161. package/build-module/image-editor/core/constants.mjs +110 -64
  162. package/build-module/image-editor/core/constants.mjs.map +1 -7
  163. package/build-module/image-editor/core/containment.mjs +265 -237
  164. package/build-module/image-editor/core/containment.mjs.map +1 -7
  165. package/build-module/image-editor/core/crop-rect.mjs +29 -20
  166. package/build-module/image-editor/core/crop-rect.mjs.map +1 -7
  167. package/build-module/image-editor/core/export/canvas-renderer.mjs +155 -105
  168. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -7
  169. package/build-module/image-editor/core/index.mjs +5 -34
  170. package/build-module/image-editor/core/interaction-controller.mjs +579 -628
  171. package/build-module/image-editor/core/interaction-controller.mjs.map +1 -7
  172. package/build-module/image-editor/core/math/rotation.mjs +21 -10
  173. package/build-module/image-editor/core/math/rotation.mjs.map +1 -7
  174. package/build-module/image-editor/core/math/sanitize.mjs +116 -52
  175. package/build-module/image-editor/core/math/sanitize.mjs.map +1 -7
  176. package/build-module/image-editor/core/setter-helpers.mjs +52 -35
  177. package/build-module/image-editor/core/setter-helpers.mjs.map +1 -7
  178. package/build-module/image-editor/core/source-region.mjs +210 -160
  179. package/build-module/image-editor/core/source-region.mjs.map +1 -7
  180. package/build-module/image-editor/core/state.mjs +271 -243
  181. package/build-module/image-editor/core/state.mjs.map +1 -7
  182. package/build-module/image-editor/core/stencil-math.mjs +185 -178
  183. package/build-module/image-editor/core/stencil-math.mjs.map +1 -7
  184. package/build-module/image-editor/core/transform-style.mjs +34 -23
  185. package/build-module/image-editor/core/transform-style.mjs.map +1 -7
  186. package/build-module/image-editor/core/transforms/pipeline.mjs +29 -11
  187. package/build-module/image-editor/core/transforms/pipeline.mjs.map +1 -7
  188. package/build-module/image-editor/core/types.mjs +0 -1
  189. package/build-module/image-editor/core/viewport-state.mjs +26 -20
  190. package/build-module/image-editor/core/viewport-state.mjs.map +1 -7
  191. package/build-module/image-editor/index.mjs +3 -38
  192. package/build-module/image-editor/react/components/cropper-provider.mjs +38 -21
  193. package/build-module/image-editor/react/components/cropper-provider.mjs.map +1 -7
  194. package/build-module/image-editor/react/components/cropper.mjs +550 -696
  195. package/build-module/image-editor/react/components/cropper.mjs.map +1 -7
  196. package/build-module/image-editor/react/components/index.mjs +1 -7
  197. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +130 -135
  198. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -7
  199. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +24 -35
  200. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +1 -7
  201. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +54 -64
  202. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +1 -7
  203. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +355 -407
  204. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +1 -7
  205. package/build-module/image-editor/react/components/viewport-provider.mjs +15 -22
  206. package/build-module/image-editor/react/components/viewport-provider.mjs.map +1 -7
  207. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +72 -43
  208. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -7
  209. package/build-module/image-editor/react/hooks/index.mjs +2 -7
  210. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +83 -119
  211. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +1 -7
  212. package/build-module/image-editor/react/hooks/use-cropper-controller.mjs +75 -0
  213. package/build-module/image-editor/react/hooks/use-cropper-controller.mjs.map +1 -0
  214. package/build-module/image-editor/react/hooks/use-interaction.mjs +175 -183
  215. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +1 -7
  216. package/build-module/image-editor/react/hooks/use-transform-style.mjs +15 -9
  217. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +1 -7
  218. package/build-module/image-editor/react/hooks/use-viewport-state.mjs +32 -26
  219. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -7
  220. package/build-module/image-editor/react/visually-hidden-style.mjs +17 -14
  221. package/build-module/image-editor/react/visually-hidden-style.mjs.map +1 -7
  222. package/build-module/index.mjs +3 -10
  223. package/build-module/lock-unlock.mjs +5 -10
  224. package/build-module/lock-unlock.mjs.map +1 -7
  225. package/build-module/private-apis.mjs +8 -9
  226. package/build-module/private-apis.mjs.map +1 -7
  227. package/build-module/state/aspect-ratio-presets.mjs +48 -0
  228. package/build-module/state/aspect-ratio-presets.mjs.map +1 -0
  229. package/build-module/state/composite-reducer.mjs +114 -75
  230. package/build-module/state/composite-reducer.mjs.map +1 -7
  231. package/build-module/state/index.mjs +5 -25
  232. package/build-module/state/media-editor-state-provider.mjs +29 -24
  233. package/build-module/state/media-editor-state-provider.mjs.map +1 -7
  234. package/build-module/state/types.mjs +8 -7
  235. package/build-module/state/types.mjs.map +1 -7
  236. package/build-module/state/use-media-editor-state.mjs +256 -273
  237. package/build-module/state/use-media-editor-state.mjs.map +1 -7
  238. package/build-module/store/actions.mjs +12 -19
  239. package/build-module/store/actions.mjs.map +1 -7
  240. package/build-module/store/constants.mjs +4 -5
  241. package/build-module/store/constants.mjs.map +1 -7
  242. package/build-module/store/index.mjs +8 -9
  243. package/build-module/store/index.mjs.map +1 -7
  244. package/build-module/store/reducer.mjs +23 -26
  245. package/build-module/store/reducer.mjs.map +1 -7
  246. package/build-module/store/selectors.mjs +8 -13
  247. package/build-module/store/selectors.mjs.map +1 -7
  248. package/build-module/utils/get-media-type.mjs +14 -17
  249. package/build-module/utils/get-media-type.mjs.map +1 -7
  250. package/build-module/utils/index.mjs +1 -6
  251. package/build-style/style-rtl.css +41 -4
  252. package/build-style/style.css +41 -4
  253. package/build-types/components/media-editor/index.d.ts +6 -1
  254. package/build-types/components/media-editor/index.d.ts.map +1 -1
  255. package/build-types/components/media-editor/use-crop-options.d.ts +0 -1
  256. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
  257. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
  258. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  259. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  260. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  261. package/build-types/components/media-editor-modal/build-modifiers.d.ts +6 -5
  262. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -1
  263. package/build-types/components/media-editor-modal/index.d.ts +7 -2
  264. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  265. package/build-types/components/media-editor-provider/index.d.ts +7 -1
  266. package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
  267. package/build-types/components/media-form/index.d.ts +5 -2
  268. package/build-types/components/media-form/index.d.ts.map +1 -1
  269. package/build-types/components/media-preview/index.d.ts.map +1 -1
  270. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
  271. package/build-types/image-editor/core/constants.d.ts +0 -1
  272. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  273. package/build-types/image-editor/core/index.d.ts +1 -1
  274. package/build-types/image-editor/core/index.d.ts.map +1 -1
  275. package/build-types/image-editor/core/setter-helpers.d.ts +1 -1
  276. package/build-types/image-editor/core/types.d.ts +1 -1
  277. package/build-types/image-editor/index.d.ts +7 -6
  278. package/build-types/image-editor/index.d.ts.map +1 -1
  279. package/build-types/image-editor/react/components/cropper-provider.d.ts +2 -2
  280. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
  281. package/build-types/image-editor/react/components/cropper.d.ts +5 -14
  282. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  283. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +2 -2
  284. package/build-types/image-editor/react/hooks/index.d.ts +2 -2
  285. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
  286. package/build-types/image-editor/react/hooks/{use-cropper-reducer.d.ts → use-cropper-controller.d.ts} +12 -12
  287. package/build-types/image-editor/react/hooks/use-cropper-controller.d.ts.map +1 -0
  288. package/build-types/state/aspect-ratio-presets.d.ts +7 -0
  289. package/build-types/state/aspect-ratio-presets.d.ts.map +1 -0
  290. package/build-types/state/composite-reducer.d.ts +2 -2
  291. package/build-types/state/composite-reducer.d.ts.map +1 -1
  292. package/build-types/state/index.d.ts +2 -1
  293. package/build-types/state/index.d.ts.map +1 -1
  294. package/build-types/state/types.d.ts +36 -6
  295. package/build-types/state/types.d.ts.map +1 -1
  296. package/build-types/state/use-media-editor-state.d.ts +16 -5
  297. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  298. package/package.json +35 -24
  299. package/src/components/media-editor/index.tsx +226 -183
  300. package/src/components/media-editor/style.scss +19 -0
  301. package/src/components/media-editor/use-crop-options.ts +5 -4
  302. package/src/components/media-editor/use-save-media-editor.ts +2 -6
  303. package/src/components/media-editor-canvas/index.tsx +18 -28
  304. package/src/components/media-editor-crop-panel/index.tsx +17 -9
  305. package/src/components/media-editor-image-controls/index.tsx +4 -2
  306. package/src/components/media-editor-image-controls/style.scss +1 -1
  307. package/src/components/media-editor-modal/build-modifiers.ts +13 -7
  308. package/src/components/media-editor-modal/index.tsx +8 -1
  309. package/src/components/media-editor-modal/style.scss +1 -1
  310. package/src/components/media-editor-provider/index.tsx +8 -1
  311. package/src/components/media-form/index.tsx +48 -42
  312. package/src/components/media-preview/index.tsx +68 -25
  313. package/src/components/media-preview/style.scss +16 -2
  314. package/src/components/rotation-ruler/style.scss +6 -2
  315. package/src/components/rotation-ruler/use-ruler-drag.ts +6 -11
  316. package/src/image-editor/README.md +8 -8
  317. package/src/image-editor/core/constants.ts +0 -13
  318. package/src/image-editor/core/index.ts +1 -5
  319. package/src/image-editor/core/setter-helpers.ts +1 -1
  320. package/src/image-editor/core/types.ts +1 -1
  321. package/src/image-editor/docs/architecture.md +5 -1
  322. package/src/image-editor/docs/recipes.md +3 -3
  323. package/src/image-editor/index.ts +7 -16
  324. package/src/image-editor/react/components/cropper-provider.tsx +5 -5
  325. package/src/image-editor/react/components/cropper.tsx +20 -42
  326. package/src/image-editor/react/hooks/build-cropper-setters.ts +2 -2
  327. package/src/image-editor/react/hooks/index.ts +2 -2
  328. package/src/image-editor/react/hooks/{use-cropper-reducer.ts → use-cropper-controller.ts} +11 -11
  329. package/src/state/aspect-ratio-presets.ts +18 -0
  330. package/src/state/composite-reducer.ts +26 -13
  331. package/src/state/index.ts +3 -0
  332. package/src/state/types.ts +39 -11
  333. package/src/state/use-media-editor-state.ts +53 -16
  334. package/build/image-editor/core/index.cjs.map +0 -7
  335. package/build/image-editor/core/types.cjs.map +0 -7
  336. package/build/image-editor/index.cjs.map +0 -7
  337. package/build/image-editor/react/components/index.cjs.map +0 -7
  338. package/build/image-editor/react/hooks/index.cjs.map +0 -7
  339. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +0 -113
  340. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +0 -7
  341. package/build/index.cjs.map +0 -7
  342. package/build/state/index.cjs.map +0 -7
  343. package/build/utils/index.cjs.map +0 -7
  344. package/build-module/image-editor/core/index.mjs.map +0 -7
  345. package/build-module/image-editor/core/types.mjs.map +0 -7
  346. package/build-module/image-editor/index.mjs.map +0 -7
  347. package/build-module/image-editor/react/components/index.mjs.map +0 -7
  348. package/build-module/image-editor/react/hooks/index.mjs.map +0 -7
  349. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +0 -100
  350. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +0 -7
  351. package/build-module/index.mjs.map +0 -7
  352. package/build-module/state/index.mjs.map +0 -7
  353. package/build-module/utils/index.mjs.map +0 -7
  354. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +0 -1
  355. package/global.d.ts +0 -5
  356. package/src/components/media-editor/test/restore-original.browser.test.tsx +0 -227
  357. package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -106
  358. package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -72
  359. package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -186
  360. package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -359
  361. package/src/components/media-editor-modal/test/index.jsdom.test.tsx +0 -182
  362. package/src/components/rotation-ruler/stories/index.story.tsx +0 -43
  363. package/src/components/rotation-ruler/test/index.jsdom.test.tsx +0 -140
  364. package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +0 -823
  365. package/src/image-editor/core/math/test/sanitize.ts +0 -242
  366. package/src/image-editor/core/test/camera.ts +0 -1218
  367. package/src/image-editor/core/test/interaction-controller.ts +0 -1299
  368. package/src/image-editor/core/test/preview-export-parity.ts +0 -249
  369. package/src/image-editor/core/test/render-camera-bridge.ts +0 -349
  370. package/src/image-editor/core/test/state.ts +0 -876
  371. package/src/image-editor/core/test/stencil-math.ts +0 -665
  372. package/src/image-editor/core/transforms/test/pipeline.ts +0 -267
  373. package/src/image-editor/react/components/stencils/test/rectangle-stencil.browser.test.tsx +0 -438
  374. package/src/image-editor/react/components/test/cropper.browser.test.tsx +0 -1462
  375. package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +0 -491
  376. package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +0 -1049
  377. package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +0 -213
  378. package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +0 -140
  379. package/src/image-editor/stories/rectangle-crop.story.tsx +0 -1073
  380. package/src/image-editor/stories/style.module.css +0 -65
  381. package/src/state/test/use-media-editor-state.browser.test.tsx +0 -79
  382. package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -386
  383. package/src/store/test/reducer.ts +0 -64
  384. package/tsconfig.build.json +0 -26
  385. package/tsconfig.build.tsbuildinfo +0 -1
  386. package/tsconfig.json +0 -37
@@ -1,732 +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
- const setContainerRef = (0, import_element.useCallback)(
593
- (element) => {
594
- if (typeof ref === "function") {
595
- ref(element);
596
- } else if (ref) {
597
- ref.current = element;
598
- }
599
- },
600
- [ref]
601
- );
602
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
603
- "div",
604
- {
605
- ref: setContainerRef,
606
- className: (0, import_clsx.default)(
607
- "wp-media-editor-image-editor",
608
- isDragging && "wp-media-editor-image-editor--dragging",
609
- className
610
- ),
611
- children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
612
- "div",
613
- {
614
- ref: canvasRef,
615
- className: (0, import_clsx.default)(
616
- "wp-media-editor-image-editor__canvas",
617
- isInteractiveGrid && "wp-media-editor-image-editor__canvas--grid-interactive",
618
- showInteractiveGrid && "wp-media-editor-image-editor__canvas--show-grid",
619
- settling && "wp-media-editor-image-editor__canvas--settling",
620
- // Marks the cropper as in keyboard-interaction mode.
621
- // CSS uses :focus on the canvas to show the stencil
622
- // outline and :focus on a handle to show its ring,
623
- // so the class applies whenever any cropper element
624
- // has keyboard focus.
625
- isFocusVisible && "wp-media-editor-image-editor__canvas--focus-visible"
626
- ),
627
- tabIndex: 0,
628
- role: "group",
629
- "aria-label": (0, import_i18n.__)("Crop area"),
630
- "aria-describedby": isCropAreaFocused ? cropAreaDescriptionId : void 0,
631
- onFocus: handleCropAreaFocus,
632
- onBlur: handleCropAreaBlur,
633
- ...canvasHandlers,
634
- children: [
635
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
636
- "div",
637
- {
638
- id: cropAreaDescriptionId,
639
- style: import_visually_hidden_style.VISUALLY_HIDDEN_STYLE,
640
- children: (0, import_i18n.__)(
641
- "When this area is focused, use arrow keys to move the image and plus or minus to zoom. Tab to resize handles and controls."
642
- )
643
- }
644
- ),
645
- /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
646
- "div",
647
- {
648
- className: "wp-media-editor-image-editor__stage",
649
- "data-testid": "cropper-stage",
650
- style: stageStyle,
651
- onTransitionEnd: handleSettleTransitionEnd,
652
- children: [
653
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
654
- "img",
655
- {
656
- className: "wp-media-editor-image-editor__image",
657
- "data-testid": "cropper-image",
658
- src,
659
- alt: "",
660
- onLoad: handleImageLoad,
661
- style: imageStyle,
662
- draggable: false
663
- }
664
- ),
665
- showDimming && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
666
- import_dimming_overlay.DimmingOverlay,
667
- {
668
- cropRect: state.cropRect,
669
- containerSize: canvasSize,
670
- imageSize: scaledVisualSize,
671
- transition: settleStencilTransition
672
- }
673
- ),
674
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
675
- StencilComponent,
676
- {
677
- cropRect: state.cropRect,
678
- containerSize: canvasSize,
679
- imageSize: scaledVisualSize,
680
- onCropChange: handleCropChange,
681
- onResizeStart: handleResizeStart,
682
- onResizeEnd: handleResizeEnd,
683
- onEscape: handleEscape,
684
- aspectRatio,
685
- freeformCrop,
686
- isResizeDisabled: isTouchPinching || disabled,
687
- stencilTransition: settleStencilTransition,
688
- cropBounds,
689
- minCropSize,
690
- snapCropRect,
691
- keyboardResizeStep
692
- }
693
- ),
694
- (showGrid === true || isInteractiveGrid) && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
695
- import_grid_overlay.GridOverlay,
696
- {
697
- cropRect: state.cropRect,
698
- containerSize: canvasSize,
699
- imageSize: scaledVisualSize
700
- }
701
- ),
702
- activeHandle && outputSize && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
703
- import_dimensions_overlay.DimensionsOverlay,
704
- {
705
- cropRect: state.cropRect,
706
- containerSize: canvasSize,
707
- imageSize: scaledVisualSize,
708
- activeHandle,
709
- outputWidth: outputSize.width,
710
- outputHeight: outputSize.height
711
- }
712
- )
713
- ]
714
- }
715
- )
716
- ]
717
- }
718
- )
719
- }
720
- );
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
+ });
721
587
  }
722
- var CropperInnerWithRef = (0, import_element.forwardRef)(
723
- CropperInner
724
- );
725
- var Cropper = (0, import_element.forwardRef)(
726
- (props, ref) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_viewport_provider.ViewportProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CropperInnerWithRef, { ...props, ref }) })
727
- );
728
- // Annotate the CommonJS export names for ESM import in node:
729
- 0 && (module.exports = {
730
- Cropper
731
- });
732
- //# 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