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