@wordpress/media-editor 0.22.0 → 0.23.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 (353) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/build/components/media-editor/index.cjs +74 -21
  3. package/build/components/media-editor/index.cjs.map +1 -1
  4. package/build/components/media-editor/use-crop-options.cjs +4 -5
  5. package/build/components/media-editor/use-crop-options.cjs.map +1 -1
  6. package/build/components/media-editor/use-save-media-editor.cjs +23 -3
  7. package/build/components/media-editor/use-save-media-editor.cjs.map +1 -1
  8. package/build/components/media-editor-canvas/index.cjs +6 -11
  9. package/build/components/media-editor-canvas/index.cjs.map +1 -1
  10. package/build/components/media-editor-crop-panel/index.cjs +17 -1
  11. package/build/components/media-editor-crop-panel/index.cjs.map +1 -1
  12. package/build/components/media-editor-fine-rotation/index.cjs +4 -5
  13. package/build/components/media-editor-fine-rotation/index.cjs.map +1 -1
  14. package/build/components/media-editor-image-controls/index.cjs +4 -6
  15. package/build/components/media-editor-image-controls/index.cjs.map +1 -1
  16. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +1 -0
  17. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +1 -1
  18. package/build/components/media-editor-modal/index.cjs.map +1 -1
  19. package/build/components/media-form/index.cjs +7 -1
  20. package/build/components/media-form/index.cjs.map +1 -1
  21. package/build/components/media-preview/index.cjs +0 -1
  22. package/build/components/media-preview/index.cjs.map +1 -1
  23. package/build/state/aspect-ratio-presets.cjs +10 -4
  24. package/build/state/aspect-ratio-presets.cjs.map +1 -1
  25. package/build/state/composite-reducer.cjs +32 -67
  26. package/build/state/composite-reducer.cjs.map +1 -1
  27. package/build/state/index.cjs +0 -39
  28. package/build/state/types.cjs +1 -2
  29. package/build/state/types.cjs.map +1 -1
  30. package/build/state/use-media-editor-state.cjs +70 -106
  31. package/build/state/use-media-editor-state.cjs.map +1 -1
  32. package/build-module/components/media-editor/index.mjs +78 -25
  33. package/build-module/components/media-editor/index.mjs.map +1 -1
  34. package/build-module/components/media-editor/use-crop-options.mjs +4 -5
  35. package/build-module/components/media-editor/use-crop-options.mjs.map +1 -1
  36. package/build-module/components/media-editor/use-save-media-editor.mjs +23 -3
  37. package/build-module/components/media-editor/use-save-media-editor.mjs.map +1 -1
  38. package/build-module/components/media-editor-canvas/index.mjs +8 -13
  39. package/build-module/components/media-editor-canvas/index.mjs.map +1 -1
  40. package/build-module/components/media-editor-crop-panel/index.mjs +17 -1
  41. package/build-module/components/media-editor-crop-panel/index.mjs.map +1 -1
  42. package/build-module/components/media-editor-fine-rotation/index.mjs +4 -5
  43. package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -1
  44. package/build-module/components/media-editor-image-controls/index.mjs +5 -7
  45. package/build-module/components/media-editor-image-controls/index.mjs.map +1 -1
  46. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +1 -0
  47. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +1 -1
  48. package/build-module/components/media-editor-modal/index.mjs.map +1 -1
  49. package/build-module/components/media-form/index.mjs +7 -1
  50. package/build-module/components/media-form/index.mjs.map +1 -1
  51. package/build-module/components/media-preview/index.mjs +0 -1
  52. package/build-module/components/media-preview/index.mjs.map +1 -1
  53. package/build-module/state/aspect-ratio-presets.mjs +10 -5
  54. package/build-module/state/aspect-ratio-presets.mjs.map +1 -1
  55. package/build-module/state/composite-reducer.mjs +32 -67
  56. package/build-module/state/composite-reducer.mjs.map +1 -1
  57. package/build-module/state/index.mjs +2 -5
  58. package/build-module/state/types.mjs +1 -2
  59. package/build-module/state/types.mjs.map +1 -1
  60. package/build-module/state/use-media-editor-state.mjs +70 -105
  61. package/build-module/state/use-media-editor-state.mjs.map +1 -1
  62. package/build-style/style-rtl.css +104 -57
  63. package/build-style/style.css +103 -57
  64. package/build-types/components/media-editor/index.d.ts +17 -3
  65. package/build-types/components/media-editor/index.d.ts.map +1 -1
  66. package/build-types/components/media-editor/use-crop-options.d.ts +2 -3
  67. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
  68. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
  69. package/build-types/components/media-editor-canvas/index.d.ts +3 -3
  70. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  71. package/build-types/components/media-editor-crop-panel/index.d.ts +1 -1
  72. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  73. package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
  74. package/build-types/components/media-editor-image-controls/index.d.ts +1 -1
  75. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  76. package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -1
  77. package/build-types/components/media-editor-modal/index.d.ts +1 -1
  78. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  79. package/build-types/components/media-form/index.d.ts.map +1 -1
  80. package/build-types/components/media-preview/index.d.ts.map +1 -1
  81. package/build-types/state/aspect-ratio-presets.d.ts +20 -3
  82. package/build-types/state/aspect-ratio-presets.d.ts.map +1 -1
  83. package/build-types/state/composite-reducer.d.ts +17 -19
  84. package/build-types/state/composite-reducer.d.ts.map +1 -1
  85. package/build-types/state/index.d.ts +2 -5
  86. package/build-types/state/index.d.ts.map +1 -1
  87. package/build-types/state/types.d.ts +20 -64
  88. package/build-types/state/types.d.ts.map +1 -1
  89. package/build-types/state/use-media-editor-state.d.ts +33 -52
  90. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  91. package/package.json +22 -23
  92. package/src/components/media-editor/index.tsx +163 -28
  93. package/src/components/media-editor/use-crop-options.ts +7 -11
  94. package/src/components/media-editor/use-save-media-editor.ts +48 -6
  95. package/src/components/media-editor-canvas/index.tsx +15 -26
  96. package/src/components/media-editor-canvas/style.scss +6 -6
  97. package/src/components/media-editor-crop-panel/index.tsx +38 -2
  98. package/src/components/media-editor-crop-panel/style.scss +21 -0
  99. package/src/components/media-editor-fine-rotation/index.tsx +6 -6
  100. package/src/components/media-editor-image-controls/index.tsx +11 -9
  101. package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +1 -0
  102. package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +1 -1
  103. package/src/components/media-editor-modal/index.tsx +1 -1
  104. package/src/components/media-form/index.tsx +7 -1
  105. package/src/components/media-preview/index.tsx +0 -1
  106. package/src/components/rotation-ruler/style.scss +3 -3
  107. package/src/state/README.md +32 -0
  108. package/src/state/aspect-ratio-presets.ts +22 -3
  109. package/src/state/composite-reducer.ts +42 -80
  110. package/src/state/index.ts +2 -18
  111. package/src/state/types.ts +18 -68
  112. package/src/state/use-media-editor-state.ts +155 -240
  113. package/src/style.scss +2 -1
  114. package/build/components/media-editor-modal/build-modifiers.cjs +0 -110
  115. package/build/components/media-editor-modal/build-modifiers.cjs.map +0 -1
  116. package/build/image-editor/core/camera.cjs +0 -277
  117. package/build/image-editor/core/camera.cjs.map +0 -1
  118. package/build/image-editor/core/constants.cjs +0 -133
  119. package/build/image-editor/core/constants.cjs.map +0 -1
  120. package/build/image-editor/core/containment.cjs +0 -289
  121. package/build/image-editor/core/containment.cjs.map +0 -1
  122. package/build/image-editor/core/crop-rect.cjs +0 -37
  123. package/build/image-editor/core/crop-rect.cjs.map +0 -1
  124. package/build/image-editor/core/export/canvas-renderer.cjs +0 -181
  125. package/build/image-editor/core/export/canvas-renderer.cjs.map +0 -1
  126. package/build/image-editor/core/index.cjs +0 -89
  127. package/build/image-editor/core/interaction-controller.cjs +0 -591
  128. package/build/image-editor/core/interaction-controller.cjs.map +0 -1
  129. package/build/image-editor/core/math/rotation.cjs +0 -32
  130. package/build/image-editor/core/math/rotation.cjs.map +0 -1
  131. package/build/image-editor/core/math/sanitize.cjs +0 -136
  132. package/build/image-editor/core/math/sanitize.cjs.map +0 -1
  133. package/build/image-editor/core/setter-helpers.cjs +0 -61
  134. package/build/image-editor/core/setter-helpers.cjs.map +0 -1
  135. package/build/image-editor/core/source-region.cjs +0 -236
  136. package/build/image-editor/core/source-region.cjs.map +0 -1
  137. package/build/image-editor/core/state.cjs +0 -296
  138. package/build/image-editor/core/state.cjs.map +0 -1
  139. package/build/image-editor/core/stencil-math.cjs +0 -203
  140. package/build/image-editor/core/stencil-math.cjs.map +0 -1
  141. package/build/image-editor/core/transform-style.cjs +0 -43
  142. package/build/image-editor/core/transform-style.cjs.map +0 -1
  143. package/build/image-editor/core/transforms/pipeline.cjs +0 -41
  144. package/build/image-editor/core/transforms/pipeline.cjs.map +0 -1
  145. package/build/image-editor/core/types.cjs +0 -5
  146. package/build/image-editor/core/viewport-state.cjs +0 -36
  147. package/build/image-editor/core/viewport-state.cjs.map +0 -1
  148. package/build/image-editor/index.cjs +0 -68
  149. package/build/image-editor/react/components/cropper-provider.cjs +0 -49
  150. package/build/image-editor/react/components/cropper-provider.cjs.map +0 -1
  151. package/build/image-editor/react/components/cropper.cjs +0 -595
  152. package/build/image-editor/react/components/cropper.cjs.map +0 -1
  153. package/build/image-editor/react/components/index.cjs +0 -25
  154. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +0 -147
  155. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +0 -1
  156. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +0 -31
  157. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +0 -1
  158. package/build/image-editor/react/components/overlays/grid-overlay.cjs +0 -61
  159. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +0 -1
  160. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +0 -367
  161. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +0 -1
  162. package/build/image-editor/react/components/viewport-provider.cjs +0 -26
  163. package/build/image-editor/react/components/viewport-provider.cjs.map +0 -1
  164. package/build/image-editor/react/hooks/build-cropper-setters.cjs +0 -80
  165. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +0 -1
  166. package/build/image-editor/react/hooks/index.cjs +0 -19
  167. package/build/image-editor/react/hooks/use-aria-announcer.cjs +0 -104
  168. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +0 -1
  169. package/build/image-editor/react/hooks/use-cropper-controller.cjs +0 -80
  170. package/build/image-editor/react/hooks/use-cropper-controller.cjs.map +0 -1
  171. package/build/image-editor/react/hooks/use-interaction.cjs +0 -187
  172. package/build/image-editor/react/hooks/use-interaction.cjs.map +0 -1
  173. package/build/image-editor/react/hooks/use-transform-style.cjs +0 -24
  174. package/build/image-editor/react/hooks/use-transform-style.cjs.map +0 -1
  175. package/build/image-editor/react/hooks/use-viewport-state.cjs +0 -40
  176. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +0 -1
  177. package/build/image-editor/react/visually-hidden-style.cjs +0 -24
  178. package/build/image-editor/react/visually-hidden-style.cjs.map +0 -1
  179. package/build-module/components/media-editor-modal/build-modifiers.mjs +0 -105
  180. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +0 -1
  181. package/build-module/image-editor/core/camera.mjs +0 -265
  182. package/build-module/image-editor/core/camera.mjs.map +0 -1
  183. package/build-module/image-editor/core/constants.mjs +0 -115
  184. package/build-module/image-editor/core/constants.mjs.map +0 -1
  185. package/build-module/image-editor/core/containment.mjs +0 -281
  186. package/build-module/image-editor/core/containment.mjs.map +0 -1
  187. package/build-module/image-editor/core/crop-rect.mjs +0 -32
  188. package/build-module/image-editor/core/crop-rect.mjs.map +0 -1
  189. package/build-module/image-editor/core/export/canvas-renderer.mjs +0 -170
  190. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +0 -1
  191. package/build-module/image-editor/core/index.mjs +0 -7
  192. package/build-module/image-editor/core/interaction-controller.mjs +0 -586
  193. package/build-module/image-editor/core/interaction-controller.mjs.map +0 -1
  194. package/build-module/image-editor/core/math/rotation.mjs +0 -26
  195. package/build-module/image-editor/core/math/rotation.mjs.map +0 -1
  196. package/build-module/image-editor/core/math/sanitize.mjs +0 -127
  197. package/build-module/image-editor/core/math/sanitize.mjs.map +0 -1
  198. package/build-module/image-editor/core/setter-helpers.mjs +0 -56
  199. package/build-module/image-editor/core/setter-helpers.mjs.map +0 -1
  200. package/build-module/image-editor/core/source-region.mjs +0 -228
  201. package/build-module/image-editor/core/source-region.mjs.map +0 -1
  202. package/build-module/image-editor/core/state.mjs +0 -288
  203. package/build-module/image-editor/core/state.mjs.map +0 -1
  204. package/build-module/image-editor/core/stencil-math.mjs +0 -194
  205. package/build-module/image-editor/core/stencil-math.mjs.map +0 -1
  206. package/build-module/image-editor/core/transform-style.mjs +0 -38
  207. package/build-module/image-editor/core/transform-style.mjs.map +0 -1
  208. package/build-module/image-editor/core/transforms/pipeline.mjs +0 -35
  209. package/build-module/image-editor/core/transforms/pipeline.mjs.map +0 -1
  210. package/build-module/image-editor/core/types.mjs +0 -0
  211. package/build-module/image-editor/core/viewport-state.mjs +0 -30
  212. package/build-module/image-editor/core/viewport-state.mjs.map +0 -1
  213. package/build-module/image-editor/index.mjs +0 -4
  214. package/build-module/image-editor/react/components/cropper-provider.mjs +0 -43
  215. package/build-module/image-editor/react/components/cropper-provider.mjs.map +0 -1
  216. package/build-module/image-editor/react/components/cropper.mjs +0 -568
  217. package/build-module/image-editor/react/components/cropper.mjs.map +0 -1
  218. package/build-module/image-editor/react/components/index.mjs +0 -3
  219. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +0 -142
  220. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +0 -1
  221. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +0 -26
  222. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +0 -1
  223. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +0 -56
  224. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +0 -1
  225. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +0 -362
  226. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +0 -1
  227. package/build-module/image-editor/react/components/viewport-provider.mjs +0 -20
  228. package/build-module/image-editor/react/components/viewport-provider.mjs.map +0 -1
  229. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +0 -75
  230. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +0 -1
  231. package/build-module/image-editor/react/hooks/index.mjs +0 -3
  232. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +0 -99
  233. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +0 -1
  234. package/build-module/image-editor/react/hooks/use-cropper-controller.mjs +0 -75
  235. package/build-module/image-editor/react/hooks/use-cropper-controller.mjs.map +0 -1
  236. package/build-module/image-editor/react/hooks/use-interaction.mjs +0 -182
  237. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +0 -1
  238. package/build-module/image-editor/react/hooks/use-transform-style.mjs +0 -19
  239. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +0 -1
  240. package/build-module/image-editor/react/hooks/use-viewport-state.mjs +0 -35
  241. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +0 -1
  242. package/build-module/image-editor/react/visually-hidden-style.mjs +0 -19
  243. package/build-module/image-editor/react/visually-hidden-style.mjs.map +0 -1
  244. package/build-types/components/media-editor-modal/build-modifiers.d.ts +0 -81
  245. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +0 -1
  246. package/build-types/image-editor/core/camera.d.ts +0 -128
  247. package/build-types/image-editor/core/camera.d.ts.map +0 -1
  248. package/build-types/image-editor/core/constants.d.ts +0 -98
  249. package/build-types/image-editor/core/constants.d.ts.map +0 -1
  250. package/build-types/image-editor/core/containment.d.ts +0 -72
  251. package/build-types/image-editor/core/containment.d.ts.map +0 -1
  252. package/build-types/image-editor/core/crop-rect.d.ts +0 -16
  253. package/build-types/image-editor/core/crop-rect.d.ts.map +0 -1
  254. package/build-types/image-editor/core/export/canvas-renderer.d.ts +0 -103
  255. package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +0 -1
  256. package/build-types/image-editor/core/index.d.ts +0 -10
  257. package/build-types/image-editor/core/index.d.ts.map +0 -1
  258. package/build-types/image-editor/core/interaction-controller.d.ts +0 -191
  259. package/build-types/image-editor/core/interaction-controller.d.ts.map +0 -1
  260. package/build-types/image-editor/core/math/rotation.d.ts +0 -18
  261. package/build-types/image-editor/core/math/rotation.d.ts.map +0 -1
  262. package/build-types/image-editor/core/math/sanitize.d.ts +0 -67
  263. package/build-types/image-editor/core/math/sanitize.d.ts.map +0 -1
  264. package/build-types/image-editor/core/setter-helpers.d.ts +0 -21
  265. package/build-types/image-editor/core/setter-helpers.d.ts.map +0 -1
  266. package/build-types/image-editor/core/source-region.d.ts +0 -94
  267. package/build-types/image-editor/core/source-region.d.ts.map +0 -1
  268. package/build-types/image-editor/core/state.d.ts +0 -51
  269. package/build-types/image-editor/core/state.d.ts.map +0 -1
  270. package/build-types/image-editor/core/stencil-math.d.ts +0 -102
  271. package/build-types/image-editor/core/stencil-math.d.ts.map +0 -1
  272. package/build-types/image-editor/core/transform-style.d.ts +0 -20
  273. package/build-types/image-editor/core/transform-style.d.ts.map +0 -1
  274. package/build-types/image-editor/core/transforms/pipeline.d.ts +0 -24
  275. package/build-types/image-editor/core/transforms/pipeline.d.ts.map +0 -1
  276. package/build-types/image-editor/core/types.d.ts +0 -269
  277. package/build-types/image-editor/core/types.d.ts.map +0 -1
  278. package/build-types/image-editor/core/viewport-state.d.ts +0 -4
  279. package/build-types/image-editor/core/viewport-state.d.ts.map +0 -1
  280. package/build-types/image-editor/index.d.ts +0 -17
  281. package/build-types/image-editor/index.d.ts.map +0 -1
  282. package/build-types/image-editor/react/components/cropper-provider.d.ts +0 -38
  283. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +0 -1
  284. package/build-types/image-editor/react/components/cropper.d.ts +0 -54
  285. package/build-types/image-editor/react/components/cropper.d.ts.map +0 -1
  286. package/build-types/image-editor/react/components/index.d.ts +0 -4
  287. package/build-types/image-editor/react/components/index.d.ts.map +0 -1
  288. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +0 -33
  289. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +0 -1
  290. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +0 -17
  291. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +0 -1
  292. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +0 -27
  293. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +0 -1
  294. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +0 -33
  295. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +0 -1
  296. package/build-types/image-editor/react/components/viewport-provider.d.ts +0 -6
  297. package/build-types/image-editor/react/components/viewport-provider.d.ts.map +0 -1
  298. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +0 -44
  299. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +0 -1
  300. package/build-types/image-editor/react/hooks/index.d.ts +0 -5
  301. package/build-types/image-editor/react/hooks/index.d.ts.map +0 -1
  302. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +0 -14
  303. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +0 -1
  304. package/build-types/image-editor/react/hooks/use-cropper-controller.d.ts +0 -55
  305. package/build-types/image-editor/react/hooks/use-cropper-controller.d.ts.map +0 -1
  306. package/build-types/image-editor/react/hooks/use-interaction.d.ts +0 -65
  307. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +0 -1
  308. package/build-types/image-editor/react/hooks/use-transform-style.d.ts +0 -13
  309. package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +0 -1
  310. package/build-types/image-editor/react/hooks/use-viewport-state.d.ts +0 -16
  311. package/build-types/image-editor/react/hooks/use-viewport-state.d.ts.map +0 -1
  312. package/build-types/image-editor/react/visually-hidden-style.d.ts +0 -6
  313. package/build-types/image-editor/react/visually-hidden-style.d.ts.map +0 -1
  314. package/src/components/media-editor-modal/build-modifiers.ts +0 -167
  315. package/src/image-editor/README.md +0 -61
  316. package/src/image-editor/core/camera.ts +0 -429
  317. package/src/image-editor/core/constants.ts +0 -137
  318. package/src/image-editor/core/containment.ts +0 -469
  319. package/src/image-editor/core/crop-rect.ts +0 -42
  320. package/src/image-editor/core/export/canvas-renderer.ts +0 -250
  321. package/src/image-editor/core/index.ts +0 -43
  322. package/src/image-editor/core/interaction-controller.ts +0 -976
  323. package/src/image-editor/core/math/rotation.ts +0 -26
  324. package/src/image-editor/core/math/sanitize.ts +0 -155
  325. package/src/image-editor/core/setter-helpers.ts +0 -63
  326. package/src/image-editor/core/source-region.ts +0 -367
  327. package/src/image-editor/core/state.ts +0 -484
  328. package/src/image-editor/core/stencil-math.ts +0 -379
  329. package/src/image-editor/core/transform-style.ts +0 -56
  330. package/src/image-editor/core/transforms/pipeline.ts +0 -40
  331. package/src/image-editor/core/types.ts +0 -230
  332. package/src/image-editor/core/viewport-state.ts +0 -30
  333. package/src/image-editor/docs/architecture.md +0 -63
  334. package/src/image-editor/docs/recipes.md +0 -184
  335. package/src/image-editor/index.ts +0 -43
  336. package/src/image-editor/react/components/cropper-provider.tsx +0 -70
  337. package/src/image-editor/react/components/cropper.scss +0 -259
  338. package/src/image-editor/react/components/cropper.tsx +0 -1108
  339. package/src/image-editor/react/components/index.ts +0 -3
  340. package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +0 -262
  341. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +0 -55
  342. package/src/image-editor/react/components/overlays/grid-overlay.tsx +0 -77
  343. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +0 -623
  344. package/src/image-editor/react/components/viewport-provider.tsx +0 -32
  345. package/src/image-editor/react/hooks/build-cropper-setters.ts +0 -102
  346. package/src/image-editor/react/hooks/index.ts +0 -4
  347. package/src/image-editor/react/hooks/use-aria-announcer.ts +0 -193
  348. package/src/image-editor/react/hooks/use-cropper-controller.ts +0 -197
  349. package/src/image-editor/react/hooks/use-interaction.ts +0 -294
  350. package/src/image-editor/react/hooks/use-transform-style.ts +0 -23
  351. package/src/image-editor/react/hooks/use-viewport-state.ts +0 -41
  352. package/src/image-editor/react/visually-hidden-style.ts +0 -15
  353. package/src/image-editor/style.scss +0 -1
@@ -1,1108 +0,0 @@
1
- import clsx from 'clsx';
2
- import {
3
- useState,
4
- useCallback,
5
- useMemo,
6
- useRef,
7
- useEffect,
8
- useLayoutEffect,
9
- useId,
10
- forwardRef,
11
- flushSync,
12
- } from '@wordpress/element';
13
- import { __ } from '@wordpress/i18n';
14
- import type {
15
- HandlePosition,
16
- StencilProps,
17
- Size,
18
- NormalizedRect,
19
- } from '../../core/types';
20
- import type { CropperController } from '../hooks/use-cropper-controller';
21
- import { getImageFit, getRotatedBBox, getViewScale } from '../../core/camera';
22
- import { getImageCropBounds, getMinZoom } from '../../core/containment';
23
- import {
24
- MAX_VIEW_SCALE,
25
- PIXEL_SNAP_DISPLAY_SCALE,
26
- SETTLE_TARGET_CANVAS_FILL,
27
- } from '../../core/constants';
28
- import { computeInscribedRect } from '../../core/crop-rect';
29
- import { getMinCropPixels } from '../../core/stencil-math';
30
- import { useInteraction } from '../hooks/use-interaction';
31
- import { useTransformStyle } from '../hooks/use-transform-style';
32
- import { useAriaAnnouncer } from '../hooks/use-aria-announcer';
33
- import { RectangleStencil } from './stencils/rectangle-stencil';
34
- import { DimmingOverlay } from './overlays/dimming-overlay';
35
- import { GridOverlay } from './overlays/grid-overlay';
36
- import { DimensionsOverlay } from './overlays/dimensions-overlay';
37
- import {
38
- getSourceRegion,
39
- snapCropRectToSourcePixelGrid,
40
- snapCropRectToSourcePixels,
41
- } from '../../core/source-region';
42
- import { ViewportProvider, useViewport } from './viewport-provider';
43
- import { VISUALLY_HIDDEN_STYLE } from '../visually-hidden-style';
44
-
45
- /** Threshold for comparing normalized crop rect values. */
46
- const CROP_RECT_EPSILON = 1e-6;
47
- /** Threshold for deciding whether rotation is at a 90-degree stop. */
48
- const CARDINAL_ROTATION_EPSILON = 1e-6;
49
- /** Duration of the settle transform transition after resize end. */
50
- const SETTLE_TRANSITION_DURATION_MS = 200;
51
- // Fallback timer for when the computed transform is unchanged and no
52
- // `transitionend` fires. Sits past the CSS duration so it only wins the race
53
- // when the transition genuinely never runs.
54
- const SETTLE_TRANSITION_FALLBACK_MS = SETTLE_TRANSITION_DURATION_MS + 100;
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`,
61
- ].join( ', ' );
62
-
63
- /**
64
- * Props for the Cropper component.
65
- */
66
- export interface CropperProps {
67
- /** Image source URL. */
68
- src: string;
69
- /** The cropper controller from `useCropperController` or a composite store. */
70
- controller: CropperController;
71
- /** Stencil component for the crop area. Defaults to RectangleStencil. */
72
- stencil?: React.ComponentType< StencilProps >;
73
- /** Show the rule-of-thirds grid overlay, or only during interactions. */
74
- showGrid?: boolean | 'interactive';
75
- /** Whether external placement activity should keep the grid visible. */
76
- isPlacementActive?: boolean;
77
- /** Show the dimming overlay outside the crop area. */
78
- showDimming?: boolean;
79
- /** Show the live output dimensions tooltip during a resize. */
80
- showDimensions?: boolean;
81
- /** Minimum zoom level override. Defaults to the coverage-aware minimum. */
82
- minZoom?: number;
83
- /** Maximum zoom level. */
84
- maxZoom?: number;
85
- /** Fixed aspect ratio (width / height) in pixel space for the crop area. */
86
- aspectRatio?: number;
87
- /**
88
- * Enable freeform crop mode with resizable handles.
89
- * When false (default), the crop area is fixed and centered.
90
- * When true, the crop area has resize handles and can be freely repositioned.
91
- */
92
- freeformCrop?: boolean;
93
- /** Focus the crop area when the cropper mounts. */
94
- focusOnMount?: boolean;
95
- /**
96
- * Ignore all crop interaction: pointer drags, wheel zoom, keyboard
97
- * pan/zoom and the stencil's resize handles. Any gesture already in
98
- * flight is cancelled. Used while the edit is being saved, so the
99
- * modifiers sent to the server cannot change after the request starts.
100
- */
101
- disabled?: boolean;
102
- /** Callback fired when the image is loaded. */
103
- onImageLoaded?: ( size: Size ) => void;
104
- /** Callback fired when the image fails to load. */
105
- onImageError?: () => void;
106
- /** Fires when a continuous gesture begins (pan drag, handle resize, pinch zoom). */
107
- onGestureStart?: () => void;
108
- /** Fires when a continuous gesture ends (pointerup, resize settle). */
109
- onGestureEnd?: () => void;
110
- /** Additional className for the container. */
111
- className?: string;
112
- }
113
-
114
- /**
115
- * The main image cropper component.
116
- *
117
- * Renders an image within a container with interactive crop overlays.
118
- * Creates the camera once per render via getImageFit, then passes
119
- * derived values to stencil, overlays, and interaction hooks.
120
- *
121
- * The component fills its parent container (100% width and height).
122
- * Wrap it in a sized container to control its dimensions.
123
- *
124
- * @param root0 Component props implementing CropperProps.
125
- * @param root0.src Image source URL.
126
- * @param root0.controller The cropper controller from `useCropperController` or a composite store.
127
- * @param root0.stencil Custom stencil component.
128
- * @param root0.showGrid Grid overlay mode: false | true | 'interactive'.
129
- * @param root0.isPlacementActive Keep grid visible during external placement activity.
130
- * @param root0.showDimming Show dimming overlay outside crop.
131
- * @param root0.showDimensions Show live dimensions tooltip during resize.
132
- * @param root0.minZoom Minimum zoom level override.
133
- * @param root0.maxZoom Maximum zoom level.
134
- * @param root0.aspectRatio Fixed aspect ratio (width/height).
135
- * @param root0.freeformCrop Enable resize handles.
136
- * @param root0.focusOnMount Focus the crop area on mount.
137
- * @param root0.disabled Ignore all crop interaction.
138
- * @param root0.onImageLoaded Image load callback.
139
- * @param root0.onImageError Image error callback.
140
- * @param root0.onGestureStart Gesture boundary start.
141
- * @param root0.onGestureEnd Gesture boundary end.
142
- * @param root0.className Additional CSS class.
143
- * @param ref Forwarded ref for the container div.
144
- */
145
- function CropperInner(
146
- {
147
- src,
148
- controller,
149
- stencil: StencilComponent = RectangleStencil,
150
- showGrid = false,
151
- isPlacementActive = false,
152
- showDimming = true,
153
- showDimensions = true,
154
- minZoom,
155
- maxZoom,
156
- aspectRatio,
157
- freeformCrop = false,
158
- focusOnMount = false,
159
- disabled = false,
160
- onImageLoaded,
161
- onImageError,
162
- onGestureStart,
163
- onGestureEnd,
164
- className,
165
- }: CropperProps,
166
- ref: React.ForwardedRef< HTMLDivElement >
167
- ) {
168
- const {
169
- state,
170
- setImage,
171
- setCropRect,
172
- settleCrop,
173
- setVisualSize,
174
- adjustCropRectForViewport,
175
- } = controller;
176
- const {
177
- viewport: viewportState,
178
- setViewportPan,
179
- resetViewport,
180
- } = useViewport();
181
- // Canvas measurement via ResizeObserver. The canvas is the inner
182
- // positioning context for image/stencil/handles — inset from the root
183
- // by the handle gutter, so crop math operates on the reduced box.
184
- const canvasRef = useRef< HTMLDivElement >( null );
185
- const cropAreaDescriptionId = useId();
186
- const [ isCropAreaFocused, setIsCropAreaFocused ] =
187
- useState( focusOnMount );
188
- const [ isFocusVisible, setIsFocusVisible ] = useState( false );
189
- const [ canvasSize, setCanvasSize ] = useState< Size >( {
190
- width: 0,
191
- height: 0,
192
- } );
193
-
194
- useLayoutEffect( () => {
195
- if ( focusOnMount ) {
196
- canvasRef.current?.focus( { preventScroll: true } );
197
- }
198
- }, [ focusOnMount ] );
199
-
200
- const handleCropAreaFocus = useCallback(
201
- ( event: React.FocusEvent< HTMLDivElement > ) => {
202
- const target = event.target as HTMLElement;
203
- if ( target === event.currentTarget ) {
204
- setIsCropAreaFocused( true );
205
- }
206
- // Show the outline only when focus arrived via keyboard
207
- // navigation. relatedTarget is null for pointer-initiated and
208
- // cross-window focus, so it reliably excludes those cases
209
- // without needing a separate ref or flag. Applies to focus
210
- // arriving on the canvas itself or on a descendant handle.
211
- if (
212
- event.relatedTarget !== null &&
213
- target.matches( ':focus-visible' )
214
- ) {
215
- setIsFocusVisible( true );
216
- }
217
- },
218
- []
219
- );
220
-
221
- const handleCropAreaBlur = useCallback(
222
- ( event: React.FocusEvent< HTMLDivElement > ) => {
223
- if ( event.target === event.currentTarget ) {
224
- setIsCropAreaFocused( false );
225
- }
226
- // Reset keyboard-active styling only when focus leaves the
227
- // cropper entirely. Moves between the canvas and a handle (or
228
- // between handles) keep the keyboard-active state intact.
229
- if (
230
- ! event.currentTarget.contains(
231
- event.relatedTarget as Node | null
232
- )
233
- ) {
234
- setIsFocusVisible( false );
235
- }
236
- },
237
- []
238
- );
239
-
240
- useEffect( () => {
241
- const element = canvasRef.current;
242
- if ( ! element ) {
243
- return;
244
- }
245
- const observer = new ResizeObserver( ( entries ) => {
246
- for ( const entry of entries ) {
247
- const { width, height } = entry.contentRect;
248
- // Render before the next paint, so the image keeps up with
249
- // a container that is resizing every frame (such as while
250
- // the sidebar animates) instead of trailing it by a frame.
251
- flushSync( () => {
252
- setCanvasSize( ( prev ) => {
253
- if ( prev.width === width && prev.height === height ) {
254
- return prev;
255
- }
256
- return { width, height };
257
- } );
258
- } );
259
- }
260
- } );
261
- observer.observe( element );
262
- return () => {
263
- observer.disconnect();
264
- };
265
- }, [] );
266
-
267
- // ARIA announcements: announce significant state changes for screen readers
268
- // via the centralized @wordpress/a11y speak() API called inside the hook.
269
- useAriaAnnouncer( state );
270
-
271
- // Compute fitted image dimensions and visual bounds from camera math.
272
- const naturalWidth = state.image?.naturalWidth ?? 0;
273
- const naturalHeight = state.image?.naturalHeight ?? 0;
274
- const { elementSize, visualSize } = useMemo(
275
- () =>
276
- getImageFit(
277
- canvasSize,
278
- { width: naturalWidth, height: naturalHeight },
279
- state.rotation
280
- ),
281
- [ canvasSize, naturalWidth, naturalHeight, state.rotation ]
282
- );
283
-
284
- // Report the rendered image size to the controller. Composite
285
- // controllers need it to compute aspect-ratio reshapes from the
286
- // reducer (the dropdown dispatches without DOM access); pure
287
- // controllers ignore it.
288
- useEffect( () => {
289
- setVisualSize( visualSize );
290
- }, [ visualSize, setVisualSize ] );
291
-
292
- // In fixed-crop mode, auto-size the crop rect only when a fixed
293
- // aspect ratio is selected. With "Free" selected, turning freeform
294
- // handles off should preserve the user's current unconstrained
295
- // crop. Uses `adjustCropRectForViewport` so window-resize-driven
296
- // reshapes don't create undo entries (composite controller); the
297
- // dedup at lines below avoids redundant dispatches when the
298
- // reducer has already updated the cropRect for an aspect change.
299
- useEffect( () => {
300
- if (
301
- freeformCrop ||
302
- visualSize.width === 0 ||
303
- visualSize.height === 0 ||
304
- ! aspectRatio ||
305
- aspectRatio <= 0
306
- ) {
307
- return;
308
- }
309
- const rect = computeInscribedRect( aspectRatio, visualSize );
310
- const current = state.cropRect;
311
- if (
312
- Math.abs( current.x - rect.x ) < CROP_RECT_EPSILON &&
313
- Math.abs( current.y - rect.y ) < CROP_RECT_EPSILON &&
314
- Math.abs( current.width - rect.width ) < CROP_RECT_EPSILON &&
315
- Math.abs( current.height - rect.height ) < CROP_RECT_EPSILON
316
- ) {
317
- return;
318
- }
319
- adjustCropRectForViewport( rect );
320
- }, [
321
- freeformCrop,
322
- aspectRatio,
323
- visualSize,
324
- adjustCropRectForViewport,
325
- state.cropRect,
326
- ] );
327
-
328
- // In freeform mode, when aspectRatio changes, reshape the crop to
329
- // the largest inscribed rect of the new ratio. Bails out when the
330
- // cropRect already matches the inscribed rect — composite stores
331
- // reshape atomically inside the reducer, so this effect is a
332
- // no-op there.
333
- const prevAspectRatioRef = useRef( aspectRatio );
334
- useEffect( () => {
335
- if ( prevAspectRatioRef.current === aspectRatio ) {
336
- return;
337
- }
338
- prevAspectRatioRef.current = aspectRatio;
339
-
340
- if (
341
- ! freeformCrop ||
342
- visualSize.width === 0 ||
343
- visualSize.height === 0 ||
344
- ! aspectRatio ||
345
- aspectRatio <= 0
346
- ) {
347
- return;
348
- }
349
- const rect = computeInscribedRect( aspectRatio, visualSize );
350
- const current = state.cropRect;
351
- if (
352
- Math.abs( current.x - rect.x ) < CROP_RECT_EPSILON &&
353
- Math.abs( current.y - rect.y ) < CROP_RECT_EPSILON &&
354
- Math.abs( current.width - rect.width ) < CROP_RECT_EPSILON &&
355
- Math.abs( current.height - rect.height ) < CROP_RECT_EPSILON
356
- ) {
357
- return;
358
- }
359
- adjustCropRectForViewport( rect );
360
- }, [
361
- aspectRatio,
362
- freeformCrop,
363
- visualSize,
364
- adjustCropRectForViewport,
365
- state.cropRect,
366
- ] );
367
-
368
- // Compute the crop handle bounds from the actual image footprint.
369
- // Depends on the full state object because getImageCropBounds reads
370
- // crop, zoom, rotation, flip, and image. React Compiler requires
371
- // the complete dependency; the computation is lightweight (a few
372
- // trig ops + 4 corner transforms).
373
- const cropBounds = useMemo( () => {
374
- if ( ! state.image || elementSize.width === 0 ) {
375
- return undefined;
376
- }
377
- return getImageCropBounds( state, elementSize, visualSize );
378
- }, [ state, elementSize, visualSize ] );
379
- const effectiveMinZoom =
380
- minZoom !== undefined ? minZoom : getMinZoom( state );
381
- const [ isResizing, setIsResizing ] = useState( false );
382
- const isResizingRef = useRef( false );
383
- const isSettlingRef = useRef( false );
384
- const [ isTouchPinching, setIsTouchPinchingState ] = useState( false );
385
- const isTouchPinchingRef = useRef( false );
386
- const setTouchPinching = useCallback( ( isPinching: boolean ) => {
387
- isTouchPinchingRef.current = isPinching;
388
- setIsTouchPinchingState( isPinching );
389
- }, [] );
390
- // Direction of the handle the user is currently resizing — pointer or
391
- // keyboard. `null` outside of an active resize. Drives the live
392
- // dimensions tooltip overlay.
393
- const [ activeHandle, setActiveHandle ] = useState< HandlePosition | null >(
394
- null
395
- );
396
-
397
- // Presentational magnification so the at-rest crop fills the canvas. The
398
- // crop overlay is laid out as `cropRect * visualSize` (the contain-fit
399
- // footprint, no zoom), so a crop whose aspect differs from the image's can
400
- // render far smaller than the canvas allows. Scaling `elementSize` and
401
- // `visualSize` together magnifies the whole scene uniformly — image,
402
- // overlays, and the pixel->normalized interaction math — and the root's
403
- // `overflow: hidden` clips the image bleed. Magnifying around the footprint
404
- // centre keeps the crop centred only because the at-rest crop is always
405
- // centred (SETTLE_CROP, computeInscribedRect, and the initial full-frame
406
- // crop all centre it).
407
- const viewScaleRest = useMemo(
408
- () =>
409
- getViewScale(
410
- state.cropRect,
411
- canvasSize,
412
- visualSize,
413
- SETTLE_TARGET_CANVAS_FILL,
414
- MAX_VIEW_SCALE
415
- ),
416
- [ state.cropRect, canvasSize, visualSize ]
417
- );
418
- // During a resize the magnification is frozen at its pre-drag value rather
419
- // than recomputed per pointer move: recomputing live would zoom the scene
420
- // under the cursor, and snapping to 1 would reset the zoom the moment a
421
- // handle is grabbed. `handleResizeStart` snapshots the current rest value
422
- // into `frozenViewScale`; on release the settle recomputes from the new crop.
423
- const [ frozenViewScale, setFrozenViewScale ] = useState( 1 );
424
- const viewScale = isResizing ? frozenViewScale : viewScaleRest;
425
- const scaledVisualSize = useMemo(
426
- () => ( {
427
- width: visualSize.width * viewScale,
428
- height: visualSize.height * viewScale,
429
- } ),
430
- [ visualSize.width, visualSize.height, viewScale ]
431
- );
432
- // CSS pixels rendered per source pixel: the contain fit
433
- // (elementSize / natural), times zoom and the view-scale magnification.
434
- const displayScale =
435
- naturalWidth > 0
436
- ? ( elementSize.width / naturalWidth ) * state.zoom * viewScale
437
- : 0;
438
- const keyboardResizeStep = useMemo( () => {
439
- if (
440
- displayScale < PIXEL_SNAP_DISPLAY_SCALE ||
441
- ( aspectRatio && aspectRatio > 0 ) ||
442
- naturalWidth <= 0 ||
443
- naturalHeight <= 0
444
- ) {
445
- return undefined;
446
- }
447
- const snapRotation = Math.round( state.rotation / 90 ) * 90;
448
- if (
449
- Math.abs( state.rotation - snapRotation ) >=
450
- CARDINAL_ROTATION_EPSILON
451
- ) {
452
- return undefined;
453
- }
454
- const bbox = getRotatedBBox(
455
- naturalWidth,
456
- naturalHeight,
457
- snapRotation
458
- );
459
- if ( bbox.width <= 0 || bbox.height <= 0 ) {
460
- return undefined;
461
- }
462
- // Keyboard steps are normalized crop-space deltas, but snapping wants
463
- // one source pixel per key press. The normalized delta for one source
464
- // pixel is different on each axis when the snap-rotation bbox is not
465
- // square, so this must be an object with separate width/height steps
466
- // instead of a single scalar.
467
- return {
468
- width: state.zoom / bbox.width,
469
- height: state.zoom / bbox.height,
470
- };
471
- }, [
472
- displayScale,
473
- aspectRatio,
474
- naturalWidth,
475
- naturalHeight,
476
- state.rotation,
477
- state.zoom,
478
- ] );
479
-
480
- // Per-axis minimum crop size in normalized space, expressing a pixel floor
481
- // on the captured source region. cropRect is normalized in the viewport's
482
- // snap-rotation bbox; the captured source-pixel width is
483
- // `cropRect.width * bbox.width / zoom`, so the normalized floor scales with
484
- // `zoom` to keep the source-pixel floor constant. The floor itself adapts to
485
- // the on-screen display scale (fit × zoom × view scale) via
486
- // `getMinCropPixels`, so on a large image shown small the crop can't shrink
487
- // until the handles collapse — it yields to the 24px source floor only once
488
- // the image is shown large enough.
489
- const minCropSize: Size | undefined = useMemo( () => {
490
- if ( naturalWidth <= 0 || naturalHeight <= 0 ) {
491
- return undefined;
492
- }
493
- const snapRotation = Math.round( state.rotation / 90 ) * 90;
494
- const bbox = getRotatedBBox(
495
- naturalWidth,
496
- naturalHeight,
497
- snapRotation
498
- );
499
- const minPixels = getMinCropPixels( displayScale );
500
- return {
501
- width: Math.min( 1, ( minPixels * state.zoom ) / bbox.width ),
502
- height: Math.min( 1, ( minPixels * state.zoom ) / bbox.height ),
503
- };
504
- }, [
505
- naturalWidth,
506
- naturalHeight,
507
- state.rotation,
508
- state.zoom,
509
- displayScale,
510
- ] );
511
-
512
- const snapCropRect = useCallback(
513
- ( rect: NormalizedRect, handle: HandlePosition ): NormalizedRect => {
514
- if (
515
- displayScale < PIXEL_SNAP_DISPLAY_SCALE ||
516
- naturalWidth <= 0 ||
517
- naturalHeight <= 0
518
- ) {
519
- return rect;
520
- }
521
- return snapCropRectToSourcePixels(
522
- state,
523
- { width: naturalWidth, height: naturalHeight },
524
- rect,
525
- handle
526
- );
527
- },
528
- [ displayScale, naturalWidth, naturalHeight, state ]
529
- );
530
-
531
- const wasPixelSnapEnabledRef = useRef( false );
532
- useEffect( () => {
533
- const isPixelSnapEnabled =
534
- freeformCrop &&
535
- ( ! aspectRatio || aspectRatio <= 0 ) &&
536
- displayScale >= PIXEL_SNAP_DISPLAY_SCALE &&
537
- naturalWidth > 0 &&
538
- naturalHeight > 0;
539
- const wasPixelSnapEnabled = wasPixelSnapEnabledRef.current;
540
- wasPixelSnapEnabledRef.current = isPixelSnapEnabled;
541
-
542
- if ( ! isPixelSnapEnabled || wasPixelSnapEnabled ) {
543
- return;
544
- }
545
-
546
- const snappedCropRect = snapCropRectToSourcePixelGrid(
547
- state,
548
- { width: naturalWidth, height: naturalHeight },
549
- state.cropRect
550
- );
551
- const currentCropRect = state.cropRect;
552
- if (
553
- Math.abs( currentCropRect.x - snappedCropRect.x ) <
554
- CROP_RECT_EPSILON &&
555
- Math.abs( currentCropRect.y - snappedCropRect.y ) <
556
- CROP_RECT_EPSILON &&
557
- Math.abs( currentCropRect.width - snappedCropRect.width ) <
558
- CROP_RECT_EPSILON &&
559
- Math.abs( currentCropRect.height - snappedCropRect.height ) <
560
- CROP_RECT_EPSILON
561
- ) {
562
- return;
563
- }
564
-
565
- // The display scale crossing the threshold is not an edit, so the
566
- // snap must not record an undo entry. Recording it here would also
567
- // clear the redo stack whenever an undo or redo crosses it.
568
- adjustCropRectForViewport( snappedCropRect );
569
- }, [
570
- adjustCropRectForViewport,
571
- aspectRatio,
572
- displayScale,
573
- freeformCrop,
574
- naturalWidth,
575
- naturalHeight,
576
- state,
577
- ] );
578
-
579
- // Use the interaction hook for mouse, touch, and keyboard events.
580
- const {
581
- handlers,
582
- onWheelNative,
583
- isDragging,
584
- isZooming,
585
- isPlacementActive: isInteractionPlacementActive,
586
- } = useInteraction( state, controller, canvasSize, scaledVisualSize, {
587
- minZoom: effectiveMinZoom,
588
- maxZoom,
589
- onGestureStart,
590
- onGestureEnd,
591
- disabled,
592
- } );
593
-
594
- // Compose focus-visibility tracking into the canvas event handlers.
595
- // Kept as a spread rather than explicit props to avoid triggering
596
- // jsx-a11y/no-noninteractive-element-interactions on role="group".
597
- //
598
- // The pointer/key tracking lives in the capture phase so it runs
599
- // before any child handler — handles call stopPropagation in their
600
- // own onPointerDown / onKeyDown, which would otherwise prevent the
601
- // keyboard-active state from updating when the user interacts with
602
- // a handle directly.
603
- const canvasHandlers = {
604
- ...handlers,
605
- onPointerDownCapture: () => {
606
- setIsFocusVisible( false );
607
- },
608
- onTouchStartCapture: ( event: React.TouchEvent< HTMLDivElement > ) => {
609
- if ( event.touches.length > 1 ) {
610
- setTouchPinching( true );
611
- }
612
- },
613
- onTouchMoveCapture: ( event: React.TouchEvent< HTMLDivElement > ) => {
614
- if ( event.touches.length > 1 ) {
615
- setTouchPinching( true );
616
- }
617
- },
618
- onTouchEndCapture: ( event: React.TouchEvent< HTMLDivElement > ) => {
619
- if ( event.touches.length === 0 ) {
620
- setTouchPinching( false );
621
- }
622
- },
623
- onTouchCancelCapture: ( event: React.TouchEvent< HTMLDivElement > ) => {
624
- if ( event.touches.length === 0 ) {
625
- setTouchPinching( false );
626
- }
627
- },
628
- onKeyDownCapture: ( event: React.KeyboardEvent< HTMLDivElement > ) => {
629
- // Modifier-only keypresses precede another key rather than
630
- // indicating deliberate keyboard interaction on their own.
631
- if (
632
- ! [ 'Shift', 'Control', 'Alt', 'Meta' ].includes( event.key )
633
- ) {
634
- setIsFocusVisible( true );
635
- }
636
- },
637
- onPointerDown: ( event: React.PointerEvent< HTMLDivElement > ) => {
638
- if (
639
- disabled ||
640
- isResizingRef.current ||
641
- isTouchPinchingRef.current ||
642
- ( event.pointerType === 'touch' && event.isPrimary === false )
643
- ) {
644
- event.preventDefault();
645
- return;
646
- }
647
- handlers.onPointerDown?.( event );
648
- // Re-assert false after handlers run — el.focus() inside the
649
- // handler fires onFocus, which may otherwise set it back to true.
650
- setIsFocusVisible( false );
651
- },
652
- };
653
-
654
- // Register wheel handler natively with { passive: false } so
655
- // preventDefault works. React's onWheel registers as passive. Bound
656
- // to the canvas (not the root) so pointer geometry inside the handler
657
- // resolves against the canvas box.
658
- useEffect( () => {
659
- const el = canvasRef.current;
660
- if ( ! el ) {
661
- return;
662
- }
663
- const handleWheel = ( event: WheelEvent ) => {
664
- // Block wheel zoom while resizing or settling — the stage CSS
665
- // transform changes getBoundingClientRect() during this window,
666
- // so focal-point math would resolve against the wrong center.
667
- if ( isResizingRef.current || isSettlingRef.current ) {
668
- event.preventDefault();
669
- return;
670
- }
671
- onWheelNative( event );
672
- };
673
- el.addEventListener( 'wheel', handleWheel, {
674
- passive: false,
675
- } );
676
- return () => {
677
- el.removeEventListener( 'wheel', handleWheel );
678
- };
679
- }, [ onWheelNative ] );
680
-
681
- // Use the unscaled contain-fit footprint here. `imageStyle` prepends
682
- // `scale(viewScale)` to this matrix, so pan translations are magnified with
683
- // the rest of the scene; using `scaledVisualSize` here would apply viewScale
684
- // to pan twice.
685
- const transformString = useTransformStyle( state, visualSize );
686
-
687
- /**
688
- * Handle the image load event.
689
- */
690
- const handleImageLoad = useCallback(
691
- ( event: React.SyntheticEvent< HTMLImageElement > ) => {
692
- const img = event.currentTarget;
693
- const size: Size = {
694
- width: img.naturalWidth,
695
- height: img.naturalHeight,
696
- };
697
-
698
- setImage( {
699
- src,
700
- naturalWidth: size.width,
701
- naturalHeight: size.height,
702
- } );
703
-
704
- onImageLoaded?.( size );
705
- },
706
- [ src, setImage, onImageLoaded ]
707
- );
708
-
709
- const handleCropChange = useCallback(
710
- ( rect: NormalizedRect ) => {
711
- if ( isTouchPinchingRef.current ) {
712
- return;
713
- }
714
- setCropRect( rect );
715
- // During a resize drag, pan the viewport so the handle stays
716
- // visible even when the crop extends beyond the canvas edge.
717
- if (
718
- isResizingRef.current &&
719
- scaledVisualSize.width > 0 &&
720
- scaledVisualSize.height > 0
721
- ) {
722
- const offsetX =
723
- ( canvasSize.width - scaledVisualSize.width ) / 2;
724
- const offsetY =
725
- ( canvasSize.height - scaledVisualSize.height ) / 2;
726
- const rightOverflow = Math.max(
727
- 0,
728
- offsetX +
729
- ( rect.x + rect.width ) * scaledVisualSize.width -
730
- canvasSize.width
731
- );
732
- const leftOverflow = Math.max(
733
- 0,
734
- -( offsetX + rect.x * scaledVisualSize.width )
735
- );
736
- const bottomOverflow = Math.max(
737
- 0,
738
- offsetY +
739
- ( rect.y + rect.height ) * scaledVisualSize.height -
740
- canvasSize.height
741
- );
742
- const topOverflow = Math.max(
743
- 0,
744
- -( offsetY + rect.y * scaledVisualSize.height )
745
- );
746
- setViewportPan( {
747
- x: -rightOverflow + leftOverflow,
748
- y: -bottomOverflow + topOverflow,
749
- } );
750
- }
751
- },
752
- [ setCropRect, setViewportPan, canvasSize, scaledVisualSize ]
753
- );
754
-
755
- // Settling animation: brief transition after resize end.
756
- const [ settling, setSettling ] = useState( false );
757
- const settleTimerRef = useRef<
758
- ReturnType< typeof setTimeout > | undefined
759
- >( undefined );
760
- const finishSettling = useCallback( () => {
761
- clearTimeout( settleTimerRef.current );
762
- isSettlingRef.current = false;
763
- setSettling( false );
764
- }, [] );
765
-
766
- // Clear the pending settle timer on unmount so it can't fire a
767
- // state update on an unmounted component.
768
- useEffect( () => {
769
- return () => {
770
- clearTimeout( settleTimerRef.current );
771
- };
772
- }, [] );
773
-
774
- const isInteractiveGrid = showGrid === 'interactive';
775
- const showInteractiveGrid =
776
- isInteractiveGrid &&
777
- ( isInteractionPlacementActive || isResizing || isPlacementActive );
778
-
779
- // Output crop size in source pixels — drives the live tooltip
780
- // overlay. Only computed during pointer drags, so the per-frame
781
- // state churn during pan/zoom doesn't pay for it.
782
- const outputSize = useMemo( () => {
783
- if ( ! showDimensions || ! activeHandle || ! state.image ) {
784
- return null;
785
- }
786
- const region = getSourceRegion( state, {
787
- width: state.image.naturalWidth,
788
- height: state.image.naturalHeight,
789
- } );
790
- return { width: region.width, height: region.height };
791
- }, [ showDimensions, activeHandle, state ] );
792
-
793
- /**
794
- * Handle Escape on a resize handle — return focus to the canvas so
795
- * arrow keys pan the image rather than resize.
796
- */
797
- const handleEscape = useCallback( () => {
798
- // Escape is always a keyboard action, so show the outline immediately
799
- // rather than relying on the focus handler's :focus-visible check.
800
- setIsFocusVisible( true );
801
- canvasRef.current?.focus( { preventScroll: true } );
802
- }, [] );
803
-
804
- const handleResizeStart = useCallback(
805
- ( handle?: HandlePosition ) => {
806
- if ( isTouchPinchingRef.current ) {
807
- return;
808
- }
809
- // Freeze the magnification at its current value so grabbing a
810
- // handle doesn't reset the zoom; it holds for the whole drag.
811
- setFrozenViewScale( viewScaleRest );
812
- isResizingRef.current = true;
813
- setIsResizing( true );
814
- setActiveHandle( handle ?? null );
815
- // Clear any in-flight settle so transitions don't apply during the
816
- // new drag (rapid successive resizes would otherwise inherit the
817
- // previous settle animation).
818
- finishSettling();
819
- resetViewport();
820
- onGestureStart?.();
821
- },
822
- [ finishSettling, onGestureStart, resetViewport, viewScaleRest ]
823
- );
824
-
825
- /**
826
- * Handle resize end — settle the crop rect (re-center, fill height)
827
- * and reset the viewport pan back to neutral.
828
- */
829
- const handleResizeEnd = useCallback( () => {
830
- const isCancellingForPinch = isTouchPinchingRef.current;
831
- isResizingRef.current = false;
832
- setIsResizing( false );
833
- setActiveHandle( null );
834
- clearTimeout( settleTimerRef.current );
835
-
836
- if ( isCancellingForPinch ) {
837
- // A pinch took over mid-resize. Tear down resize UI without
838
- // settling or firing onGestureEnd: the pinch owns the gesture
839
- // boundary and fires the end on its own touchend. The resize and
840
- // the pinch therefore collapse into a single undo step, since
841
- // `beginGesture` is idempotent and the snapshot captured at resize
842
- // start is flushed by the pinch's `endGesture`.
843
- finishSettling();
844
- resetViewport();
845
- return;
846
- }
847
-
848
- isSettlingRef.current = true;
849
- setSettling( true );
850
- // Reset viewport pan first so it transitions back to zero in sync
851
- // with the settle animation on the image.
852
- resetViewport();
853
- settleCrop();
854
- onGestureEnd?.();
855
- // Normal completion is driven by transitionend so settling cannot clear
856
- // before the browser has actually finished animating. Keep a fallback for
857
- // cases where the computed transform is unchanged and no event fires.
858
- settleTimerRef.current = setTimeout(
859
- finishSettling,
860
- SETTLE_TRANSITION_FALLBACK_MS
861
- );
862
- }, [ finishSettling, settleCrop, onGestureEnd, resetViewport ] );
863
-
864
- /**
865
- * Finish settling when the settle transform transition actually completes.
866
- * The handler lives on the stage, but in the common (un-panned) case the
867
- * transform transition runs on the image layer and its `transitionend`
868
- * bubbles up to here. Other transitionend events (stencil left/top/width/
869
- * height) also bubble up, so only the transform transition should clear the
870
- * settle state.
871
- */
872
- const handleSettleTransitionEnd = useCallback(
873
- ( event: React.TransitionEvent< HTMLDivElement > ) => {
874
- if (
875
- ! isSettlingRef.current ||
876
- event.propertyName !== 'transform'
877
- ) {
878
- return;
879
- }
880
- finishSettling();
881
- },
882
- [ finishSettling ]
883
- );
884
-
885
- let imageTransition: string | undefined;
886
- if ( settling ) {
887
- imageTransition = SETTLE_TRANSFORM_TRANSITION;
888
- } else if ( isZooming ) {
889
- imageTransition = 'transform 150ms linear';
890
- }
891
- const settleStencilTransition = settling
892
- ? SETTLE_STENCIL_TRANSITION
893
- : undefined;
894
-
895
- // Compute the image's CSS style. The element keeps its contain-fit box; the
896
- // view-scale magnification is folded into the transform as an outer
897
- // `scale()` so the whole settle motion (zoom, pan, and magnification) is one
898
- // coherent transform that interpolates as a unit — the image slides/scales
899
- // to its position rather than animating its box separately. Magnifying
900
- // around the centred box keeps the (centred) crop centred.
901
- const imageStyle = useMemo( (): React.CSSProperties => {
902
- if ( elementSize.width === 0 || elementSize.height === 0 ) {
903
- return {};
904
- }
905
- const centerX = ( canvasSize.width - elementSize.width ) / 2;
906
- const centerY = ( canvasSize.height - elementSize.height ) / 2;
907
- // Above 1:1 the image is upscaled, so render nearest-neighbour to keep
908
- // pixel boundaries crisp (e.g. small images the cropper magnifies);
909
- // below 1:1 leave smoothing on for downscaled large images.
910
- return {
911
- width: elementSize.width,
912
- height: elementSize.height,
913
- maxWidth: elementSize.width,
914
- maxHeight: elementSize.height,
915
- left: centerX,
916
- top: centerY,
917
- // Always lead with `scale()` (identity at rest) so the transform's
918
- // function list stays structurally constant across rest, resize, and
919
- // settle. A conditional that dropped `scale()` at viewScale === 1
920
- // would change the list shape exactly when the settle transition
921
- // crosses 1:1, forcing the browser to fall back to matrix-decomposition
922
- // interpolation instead of interpolating each function in turn.
923
- transform: `scale(${ viewScale }) ${ transformString }`,
924
- transition: imageTransition,
925
- imageRendering: displayScale > 1 ? 'pixelated' : undefined,
926
- };
927
- }, [
928
- canvasSize,
929
- elementSize,
930
- transformString,
931
- imageTransition,
932
- displayScale,
933
- viewScale,
934
- ] );
935
-
936
- // Viewport pan CSS transform for the stage div. Applied during resize
937
- // drags to keep handles visible when the crop extends past the canvas edge.
938
- // Transitions back to zero during the settle animation.
939
- // will-change promotes the stage to its own compositor layer while the
940
- // transform is active, keeping per-frame pan updates off the main thread.
941
- const settleTransition = settling ? SETTLE_TRANSFORM_TRANSITION : undefined;
942
- const willChange = isResizing || settling ? 'transform' : undefined;
943
- let stageStyle: React.CSSProperties | undefined;
944
- if ( viewportState.pan.x !== 0 || viewportState.pan.y !== 0 ) {
945
- stageStyle = {
946
- transform: `translate(${ viewportState.pan.x }px, ${ viewportState.pan.y }px)`,
947
- transition: settleTransition,
948
- willChange,
949
- };
950
- } else if ( settling ) {
951
- // Animate the viewport pan back to an explicit identity translate rather
952
- // than dropping the transform property, so the reset eases instead of
953
- // snapping (transitions to a removed transform are unreliable).
954
- stageStyle = {
955
- transform: 'translate(0px, 0px)',
956
- transition: settleTransition,
957
- willChange,
958
- };
959
- }
960
-
961
- return (
962
- <div
963
- ref={ ref }
964
- className={ clsx(
965
- 'wp-media-editor-image-editor',
966
- isDragging && 'wp-media-editor-image-editor--dragging',
967
- className
968
- ) }
969
- >
970
- { /*
971
- * The canvas is the interactive, inset surface. Handles and
972
- * the ARIA role/tabIndex live here so pointer geometry
973
- * (getBoundingClientRect on e.currentTarget) resolves against
974
- * the same box that crop math uses. The root stays as the
975
- * clipping shell for the dimming overlay's box-shadow.
976
- *
977
- * Not role="application" — that disables the screen reader's
978
- * normal keyboard interception, too heavy-handed for a single
979
- * widget. Screen reader users get the ARIA live region below
980
- * as the announcement channel.
981
- */ }
982
- <div
983
- ref={ canvasRef }
984
- className={ clsx(
985
- 'wp-media-editor-image-editor__canvas',
986
- isInteractiveGrid &&
987
- 'wp-media-editor-image-editor__canvas--grid-interactive',
988
- showInteractiveGrid &&
989
- 'wp-media-editor-image-editor__canvas--show-grid',
990
- settling &&
991
- 'wp-media-editor-image-editor__canvas--settling',
992
- // Marks the cropper as in keyboard-interaction mode.
993
- // CSS uses :focus on the canvas to show the stencil
994
- // outline and :focus on a handle to show its ring,
995
- // so the class applies whenever any cropper element
996
- // has keyboard focus.
997
- isFocusVisible &&
998
- 'wp-media-editor-image-editor__canvas--focus-visible'
999
- ) }
1000
- tabIndex={ 0 }
1001
- role="group"
1002
- aria-label={ __( 'Crop area' ) }
1003
- aria-describedby={
1004
- isCropAreaFocused ? cropAreaDescriptionId : undefined
1005
- }
1006
- onFocus={ handleCropAreaFocus }
1007
- onBlur={ handleCropAreaBlur }
1008
- { ...canvasHandlers }
1009
- >
1010
- <div
1011
- id={ cropAreaDescriptionId }
1012
- style={ VISUALLY_HIDDEN_STYLE }
1013
- >
1014
- { __(
1015
- 'When this area is focused, use arrow keys to move the image and plus or minus to zoom. Tab to resize handles and controls.'
1016
- ) }
1017
- </div>
1018
- { /*
1019
- * The stage is an inner full-size div that receives the
1020
- * viewport pan CSS transform. Keeping the transform here
1021
- * (rather than on the canvas) means the canvas always stays
1022
- * at its natural position, so the root div's background is
1023
- * never exposed when the stage is panned during a resize drag.
1024
- */ }
1025
- <div
1026
- className="wp-media-editor-image-editor__stage"
1027
- data-testid="cropper-stage"
1028
- style={ stageStyle }
1029
- onTransitionEnd={ handleSettleTransitionEnd }
1030
- >
1031
- { /* The image layer */ }
1032
- <img
1033
- className="wp-media-editor-image-editor__image"
1034
- data-testid="cropper-image"
1035
- src={ src }
1036
- alt=""
1037
- onLoad={ handleImageLoad }
1038
- onError={ onImageError }
1039
- style={ imageStyle }
1040
- draggable={ false }
1041
- />
1042
-
1043
- { /* Dimming overlay outside the crop area */ }
1044
- { showDimming && (
1045
- <DimmingOverlay
1046
- cropRect={ state.cropRect }
1047
- containerSize={ canvasSize }
1048
- imageSize={ scaledVisualSize }
1049
- transition={ settleStencilTransition }
1050
- />
1051
- ) }
1052
-
1053
- { /* The stencil (crop area with handles) */ }
1054
- <StencilComponent
1055
- cropRect={ state.cropRect }
1056
- containerSize={ canvasSize }
1057
- imageSize={ scaledVisualSize }
1058
- onCropChange={ handleCropChange }
1059
- onResizeStart={ handleResizeStart }
1060
- onResizeEnd={ handleResizeEnd }
1061
- onEscape={ handleEscape }
1062
- aspectRatio={ aspectRatio }
1063
- freeformCrop={ freeformCrop }
1064
- isResizeDisabled={ isTouchPinching || disabled }
1065
- stencilTransition={ settleStencilTransition }
1066
- cropBounds={ cropBounds }
1067
- minCropSize={ minCropSize }
1068
- snapCropRect={ snapCropRect }
1069
- keyboardResizeStep={ keyboardResizeStep }
1070
- />
1071
-
1072
- { /* Rule-of-thirds grid */ }
1073
- { ( showGrid === true || isInteractiveGrid ) && (
1074
- <GridOverlay
1075
- cropRect={ state.cropRect }
1076
- containerSize={ canvasSize }
1077
- imageSize={ scaledVisualSize }
1078
- />
1079
- ) }
1080
-
1081
- { /* Live dimensions tooltip pinned to the dragged handle. */ }
1082
- { activeHandle && outputSize && (
1083
- <DimensionsOverlay
1084
- cropRect={ state.cropRect }
1085
- containerSize={ canvasSize }
1086
- imageSize={ scaledVisualSize }
1087
- activeHandle={ activeHandle }
1088
- outputWidth={ outputSize.width }
1089
- outputHeight={ outputSize.height }
1090
- />
1091
- ) }
1092
- </div>
1093
- </div>
1094
- </div>
1095
- );
1096
- }
1097
-
1098
- const CropperInnerWithRef = forwardRef< HTMLDivElement, CropperProps >(
1099
- CropperInner
1100
- );
1101
-
1102
- export const Cropper = forwardRef< HTMLDivElement, CropperProps >(
1103
- ( props, ref ) => (
1104
- <ViewportProvider>
1105
- <CropperInnerWithRef { ...props } ref={ ref } />
1106
- </ViewportProvider>
1107
- )
1108
- );