@wordpress/media-editor 0.7.1-next.v.202604201441.0 → 0.8.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 (300) hide show
  1. package/build/components/media-editor-canvas/index.cjs +54 -0
  2. package/build/components/media-editor-canvas/index.cjs.map +7 -0
  3. package/build/components/media-editor-crop-panel/index.cjs +116 -0
  4. package/build/components/media-editor-crop-panel/index.cjs.map +7 -0
  5. package/build/components/media-editor-modal/build-modifiers.cjs +91 -0
  6. package/build/components/media-editor-modal/build-modifiers.cjs.map +7 -0
  7. package/build/components/media-editor-modal/index.cjs +497 -0
  8. package/build/components/media-editor-modal/index.cjs.map +7 -0
  9. package/build/components/media-editor-toolbar/index.cjs +146 -0
  10. package/build/components/media-editor-toolbar/index.cjs.map +7 -0
  11. package/build/image-editor/core/camera.cjs +194 -0
  12. package/build/image-editor/core/camera.cjs.map +7 -0
  13. package/build/image-editor/core/constants.cjs +81 -0
  14. package/build/image-editor/core/constants.cjs.map +7 -0
  15. package/build/image-editor/core/containment.cjs +245 -0
  16. package/build/image-editor/core/containment.cjs.map +7 -0
  17. package/build/image-editor/core/export/canvas-renderer.cjs +151 -0
  18. package/build/image-editor/core/export/canvas-renderer.cjs.map +7 -0
  19. package/build/image-editor/core/index.cjs +50 -0
  20. package/build/image-editor/core/index.cjs.map +7 -0
  21. package/build/image-editor/core/interaction-controller.cjs +608 -0
  22. package/build/image-editor/core/interaction-controller.cjs.map +7 -0
  23. package/build/image-editor/core/math/rotation.cjs +41 -0
  24. package/build/image-editor/core/math/rotation.cjs.map +7 -0
  25. package/build/image-editor/core/source-region.cjs +95 -0
  26. package/build/image-editor/core/source-region.cjs.map +7 -0
  27. package/build/image-editor/core/state.cjs +269 -0
  28. package/build/image-editor/core/state.cjs.map +7 -0
  29. package/build/image-editor/core/stencil-math.cjs +197 -0
  30. package/build/image-editor/core/stencil-math.cjs.map +7 -0
  31. package/build/image-editor/core/transform-style.cjs +46 -0
  32. package/build/image-editor/core/transform-style.cjs.map +7 -0
  33. package/build/image-editor/core/transforms/pipeline.cjs +43 -0
  34. package/build/image-editor/core/transforms/pipeline.cjs.map +7 -0
  35. package/build/image-editor/core/types.cjs +19 -0
  36. package/build/image-editor/core/types.cjs.map +7 -0
  37. package/build/image-editor/index.cjs +57 -0
  38. package/build/image-editor/index.cjs.map +7 -0
  39. package/build/image-editor/react/components/cropper-provider.cjs +50 -0
  40. package/build/image-editor/react/components/cropper-provider.cjs.map +7 -0
  41. package/build/image-editor/react/components/cropper.cjs +365 -0
  42. package/build/image-editor/react/components/cropper.cjs.map +7 -0
  43. package/build/image-editor/react/components/index.cjs +36 -0
  44. package/build/image-editor/react/components/index.cjs.map +7 -0
  45. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +58 -0
  46. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +7 -0
  47. package/build/image-editor/react/components/overlays/grid-overlay.cjs +91 -0
  48. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +7 -0
  49. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +351 -0
  50. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +7 -0
  51. package/build/image-editor/react/hooks/index.cjs +31 -0
  52. package/build/image-editor/react/hooks/index.cjs.map +7 -0
  53. package/build/image-editor/react/hooks/use-aria-announcer.cjs +68 -0
  54. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +7 -0
  55. package/build/image-editor/react/hooks/use-cropper-state.cjs +158 -0
  56. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +7 -0
  57. package/build/image-editor/react/hooks/use-interaction.cjs +161 -0
  58. package/build/image-editor/react/hooks/use-interaction.cjs.map +7 -0
  59. package/build/image-editor/react/hooks/use-transform-style.cjs +38 -0
  60. package/build/image-editor/react/hooks/use-transform-style.cjs.map +7 -0
  61. package/build/index.cjs +5 -2
  62. package/build/index.cjs.map +2 -2
  63. package/build/lock-unlock.cjs +37 -0
  64. package/build/lock-unlock.cjs.map +7 -0
  65. package/build/private-apis.cjs +38 -0
  66. package/build/private-apis.cjs.map +7 -0
  67. package/build/store/actions.cjs +45 -0
  68. package/build/store/actions.cjs.map +7 -0
  69. package/build/store/constants.cjs +31 -0
  70. package/build/store/constants.cjs.map +7 -0
  71. package/build/store/index.cjs +51 -0
  72. package/build/store/index.cjs.map +7 -0
  73. package/build/store/reducer.cjs +51 -0
  74. package/build/store/reducer.cjs.map +7 -0
  75. package/build/store/selectors.cjs +43 -0
  76. package/build/store/selectors.cjs.map +7 -0
  77. package/build-module/components/media-editor-canvas/index.mjs +33 -0
  78. package/build-module/components/media-editor-canvas/index.mjs.map +7 -0
  79. package/build-module/components/media-editor-crop-panel/index.mjs +100 -0
  80. package/build-module/components/media-editor-crop-panel/index.mjs.map +7 -0
  81. package/build-module/components/media-editor-modal/build-modifiers.mjs +66 -0
  82. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +7 -0
  83. package/build-module/components/media-editor-modal/index.mjs +483 -0
  84. package/build-module/components/media-editor-modal/index.mjs.map +7 -0
  85. package/build-module/components/media-editor-toolbar/index.mjs +130 -0
  86. package/build-module/components/media-editor-toolbar/index.mjs.map +7 -0
  87. package/build-module/image-editor/core/camera.mjs +163 -0
  88. package/build-module/image-editor/core/camera.mjs.map +7 -0
  89. package/build-module/image-editor/core/constants.mjs +51 -0
  90. package/build-module/image-editor/core/constants.mjs.map +7 -0
  91. package/build-module/image-editor/core/containment.mjs +218 -0
  92. package/build-module/image-editor/core/containment.mjs.map +7 -0
  93. package/build-module/image-editor/core/export/canvas-renderer.mjs +120 -0
  94. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +7 -0
  95. package/build-module/image-editor/core/index.mjs +24 -0
  96. package/build-module/image-editor/core/index.mjs.map +7 -0
  97. package/build-module/image-editor/core/interaction-controller.mjs +583 -0
  98. package/build-module/image-editor/core/interaction-controller.mjs.map +7 -0
  99. package/build-module/image-editor/core/math/rotation.mjs +15 -0
  100. package/build-module/image-editor/core/math/rotation.mjs.map +7 -0
  101. package/build-module/image-editor/core/source-region.mjs +69 -0
  102. package/build-module/image-editor/core/source-region.mjs.map +7 -0
  103. package/build-module/image-editor/core/state.mjs +242 -0
  104. package/build-module/image-editor/core/state.mjs.map +7 -0
  105. package/build-module/image-editor/core/stencil-math.mjs +169 -0
  106. package/build-module/image-editor/core/stencil-math.mjs.map +7 -0
  107. package/build-module/image-editor/core/transform-style.mjs +21 -0
  108. package/build-module/image-editor/core/transform-style.mjs.map +7 -0
  109. package/build-module/image-editor/core/transforms/pipeline.mjs +17 -0
  110. package/build-module/image-editor/core/transforms/pipeline.mjs.map +7 -0
  111. package/build-module/image-editor/core/types.mjs +1 -0
  112. package/build-module/image-editor/core/types.mjs.map +7 -0
  113. package/build-module/image-editor/index.mjs +30 -0
  114. package/build-module/image-editor/index.mjs.map +7 -0
  115. package/build-module/image-editor/react/components/cropper-provider.mjs +26 -0
  116. package/build-module/image-editor/react/components/cropper-provider.mjs.map +7 -0
  117. package/build-module/image-editor/react/components/cropper.mjs +337 -0
  118. package/build-module/image-editor/react/components/cropper.mjs.map +7 -0
  119. package/build-module/image-editor/react/components/index.mjs +9 -0
  120. package/build-module/image-editor/react/components/index.mjs.map +7 -0
  121. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +33 -0
  122. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +7 -0
  123. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +66 -0
  124. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +7 -0
  125. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +330 -0
  126. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +7 -0
  127. package/build-module/image-editor/react/hooks/index.mjs +6 -0
  128. package/build-module/image-editor/react/hooks/index.mjs.map +7 -0
  129. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +43 -0
  130. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +7 -0
  131. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +137 -0
  132. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +7 -0
  133. package/build-module/image-editor/react/hooks/use-interaction.mjs +138 -0
  134. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +7 -0
  135. package/build-module/image-editor/react/hooks/use-transform-style.mjs +13 -0
  136. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +7 -0
  137. package/build-module/index.mjs +3 -1
  138. package/build-module/index.mjs.map +2 -2
  139. package/build-module/lock-unlock.mjs +11 -0
  140. package/build-module/lock-unlock.mjs.map +7 -0
  141. package/build-module/private-apis.mjs +13 -0
  142. package/build-module/private-apis.mjs.map +7 -0
  143. package/build-module/store/actions.mjs +19 -0
  144. package/build-module/store/actions.mjs.map +7 -0
  145. package/build-module/store/constants.mjs +6 -0
  146. package/build-module/store/constants.mjs.map +7 -0
  147. package/build-module/store/index.mjs +16 -0
  148. package/build-module/store/index.mjs.map +7 -0
  149. package/build-module/store/reducer.mjs +26 -0
  150. package/build-module/store/reducer.mjs.map +7 -0
  151. package/build-module/store/selectors.mjs +16 -0
  152. package/build-module/store/selectors.mjs.map +7 -0
  153. package/build-style/style-rtl.css +315 -0
  154. package/build-style/style.css +315 -0
  155. package/build-types/components/media-editor-canvas/index.d.ts +22 -0
  156. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -0
  157. package/build-types/components/media-editor-crop-panel/index.d.ts +45 -0
  158. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -0
  159. package/build-types/components/media-editor-modal/build-modifiers.d.ts +83 -0
  160. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -0
  161. package/build-types/components/media-editor-modal/index.d.ts +20 -0
  162. package/build-types/components/media-editor-modal/index.d.ts.map +1 -0
  163. package/build-types/components/media-editor-toolbar/index.d.ts +21 -0
  164. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -0
  165. package/build-types/image-editor/core/camera.d.ts +107 -0
  166. package/build-types/image-editor/core/camera.d.ts.map +1 -0
  167. package/build-types/image-editor/core/constants.d.ts +46 -0
  168. package/build-types/image-editor/core/constants.d.ts.map +1 -0
  169. package/build-types/image-editor/core/containment.d.ts +61 -0
  170. package/build-types/image-editor/core/containment.d.ts.map +1 -0
  171. package/build-types/image-editor/core/export/canvas-renderer.d.ts +106 -0
  172. package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +1 -0
  173. package/build-types/image-editor/core/index.d.ts +8 -0
  174. package/build-types/image-editor/core/index.d.ts.map +1 -0
  175. package/build-types/image-editor/core/interaction-controller.d.ts +178 -0
  176. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -0
  177. package/build-types/image-editor/core/math/rotation.d.ts +18 -0
  178. package/build-types/image-editor/core/math/rotation.d.ts.map +1 -0
  179. package/build-types/image-editor/core/source-region.d.ts +75 -0
  180. package/build-types/image-editor/core/source-region.d.ts.map +1 -0
  181. package/build-types/image-editor/core/state.d.ts +42 -0
  182. package/build-types/image-editor/core/state.d.ts.map +1 -0
  183. package/build-types/image-editor/core/stencil-math.d.ts +79 -0
  184. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -0
  185. package/build-types/image-editor/core/transform-style.d.ts +23 -0
  186. package/build-types/image-editor/core/transform-style.d.ts.map +1 -0
  187. package/build-types/image-editor/core/transforms/pipeline.d.ts +27 -0
  188. package/build-types/image-editor/core/transforms/pipeline.d.ts.map +1 -0
  189. package/build-types/image-editor/core/types.d.ts +211 -0
  190. package/build-types/image-editor/core/types.d.ts.map +1 -0
  191. package/build-types/image-editor/index.d.ts +16 -0
  192. package/build-types/image-editor/index.d.ts.map +1 -0
  193. package/build-types/image-editor/react/components/cropper-provider.d.ts +37 -0
  194. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -0
  195. package/build-types/image-editor/react/components/cropper.d.ts +59 -0
  196. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -0
  197. package/build-types/image-editor/react/components/index.d.ts +4 -0
  198. package/build-types/image-editor/react/components/index.d.ts.map +1 -0
  199. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +30 -0
  200. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -0
  201. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +30 -0
  202. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -0
  203. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +32 -0
  204. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -0
  205. package/build-types/image-editor/react/hooks/index.d.ts +3 -0
  206. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -0
  207. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +16 -0
  208. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -0
  209. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +68 -0
  210. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -0
  211. package/build-types/image-editor/react/hooks/use-interaction.d.ts +60 -0
  212. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -0
  213. package/build-types/image-editor/react/hooks/use-transform-style.d.ts +16 -0
  214. package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +1 -0
  215. package/build-types/image-editor/stories/rectangle-crop.story.d.ts +16 -0
  216. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -0
  217. package/build-types/index.d.ts +1 -0
  218. package/build-types/index.d.ts.map +1 -1
  219. package/build-types/lock-unlock.d.ts +2 -0
  220. package/build-types/lock-unlock.d.ts.map +1 -0
  221. package/build-types/private-apis.d.ts +2 -0
  222. package/build-types/private-apis.d.ts.map +1 -0
  223. package/build-types/store/actions.d.ts +27 -0
  224. package/build-types/store/actions.d.ts.map +1 -0
  225. package/build-types/store/constants.d.ts +2 -0
  226. package/build-types/store/constants.d.ts.map +1 -0
  227. package/build-types/store/index.d.ts +4 -0
  228. package/build-types/store/index.d.ts.map +1 -0
  229. package/build-types/store/reducer.d.ts +23 -0
  230. package/build-types/store/reducer.d.ts.map +1 -0
  231. package/build-types/store/selectors.d.ts +8 -0
  232. package/build-types/store/selectors.d.ts.map +1 -0
  233. package/package.json +21 -7
  234. package/src/components/media-editor-canvas/index.tsx +56 -0
  235. package/src/components/media-editor-canvas/style.scss +9 -0
  236. package/src/components/media-editor-crop-panel/index.tsx +143 -0
  237. package/src/components/media-editor-modal/build-modifiers.ts +164 -0
  238. package/src/components/media-editor-modal/index.tsx +639 -0
  239. package/src/components/media-editor-modal/style.scss +139 -0
  240. package/src/components/media-editor-modal/test/build-modifiers.test.ts +343 -0
  241. package/src/components/media-editor-toolbar/index.tsx +150 -0
  242. package/src/components/media-editor-toolbar/style.scss +21 -0
  243. package/src/image-editor/README.md +162 -0
  244. package/src/image-editor/core/camera.ts +352 -0
  245. package/src/image-editor/core/constants.ts +93 -0
  246. package/src/image-editor/core/containment.ts +435 -0
  247. package/src/image-editor/core/export/canvas-renderer.ts +253 -0
  248. package/src/image-editor/core/export/test/canvas-renderer.ts +813 -0
  249. package/src/image-editor/core/index.ts +35 -0
  250. package/src/image-editor/core/interaction-controller.ts +889 -0
  251. package/src/image-editor/core/math/rotation.ts +26 -0
  252. package/src/image-editor/core/source-region.ts +166 -0
  253. package/src/image-editor/core/state.ts +428 -0
  254. package/src/image-editor/core/stencil-math.ts +330 -0
  255. package/src/image-editor/core/test/camera.ts +593 -0
  256. package/src/image-editor/core/test/interaction-controller.ts +996 -0
  257. package/src/image-editor/core/test/preview-export-parity.ts +242 -0
  258. package/src/image-editor/core/test/render-camera-bridge.ts +348 -0
  259. package/src/image-editor/core/test/state.ts +762 -0
  260. package/src/image-editor/core/test/stencil-math.ts +321 -0
  261. package/src/image-editor/core/transform-style.ts +44 -0
  262. package/src/image-editor/core/transforms/pipeline.ts +43 -0
  263. package/src/image-editor/core/transforms/test/pipeline.ts +269 -0
  264. package/src/image-editor/core/types.ts +177 -0
  265. package/src/image-editor/docs/architecture.md +96 -0
  266. package/src/image-editor/docs/recipes.md +720 -0
  267. package/src/image-editor/docs/roadmap.md +66 -0
  268. package/src/image-editor/index.ts +43 -0
  269. package/src/image-editor/react/components/cropper-provider.tsx +73 -0
  270. package/src/image-editor/react/components/cropper.scss +195 -0
  271. package/src/image-editor/react/components/cropper.tsx +534 -0
  272. package/src/image-editor/react/components/index.ts +3 -0
  273. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +57 -0
  274. package/src/image-editor/react/components/overlays/grid-overlay.tsx +80 -0
  275. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +513 -0
  276. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +265 -0
  277. package/src/image-editor/react/components/test/cropper.tsx +140 -0
  278. package/src/image-editor/react/hooks/index.ts +2 -0
  279. package/src/image-editor/react/hooks/test/use-cropper-state.ts +913 -0
  280. package/src/image-editor/react/hooks/test/use-interaction.ts +218 -0
  281. package/src/image-editor/react/hooks/test/use-transform-style.ts +146 -0
  282. package/src/image-editor/react/hooks/use-aria-announcer.ts +64 -0
  283. package/src/image-editor/react/hooks/use-cropper-state.ts +238 -0
  284. package/src/image-editor/react/hooks/use-interaction.ts +223 -0
  285. package/src/image-editor/react/hooks/use-transform-style.ts +30 -0
  286. package/src/image-editor/stories/rectangle-crop.story.tsx +1085 -0
  287. package/src/image-editor/stories/style.css +94 -0
  288. package/src/image-editor/style.scss +1 -0
  289. package/src/index.ts +3 -0
  290. package/src/lock-unlock.ts +10 -0
  291. package/src/private-apis.ts +12 -0
  292. package/src/store/actions.ts +33 -0
  293. package/src/store/constants.ts +1 -0
  294. package/src/store/index.ts +20 -0
  295. package/src/store/reducer.ts +48 -0
  296. package/src/store/selectors.ts +16 -0
  297. package/src/store/test/reducer.ts +57 -0
  298. package/src/style.scss +4 -0
  299. package/tsconfig.json +10 -2
  300. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,513 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useCallback, useEffect, useMemo, useRef } from '@wordpress/element';
5
+ import { __ } from '@wordpress/i18n';
6
+
7
+ /**
8
+ * Internal dependencies
9
+ */
10
+ import type { StencilProps, NormalizedRect } from '../../../core/types';
11
+ import {
12
+ computeFreeResizeRect,
13
+ computeLockedResizeRect,
14
+ computeShiftLockedResizeRect,
15
+ type HandlePosition,
16
+ type CropBounds,
17
+ type ResizeDragState,
18
+ } from '../../../core/stencil-math';
19
+
20
+ /**
21
+ * Corner handle positions only — used when aspect ratio is locked.
22
+ * Ordered clockwise from top-left for logical tab order.
23
+ */
24
+ const CORNER_POSITIONS: HandlePosition[] = [ 'nw', 'ne', 'se', 'sw' ];
25
+
26
+ /**
27
+ * All handle positions rendered by the stencil.
28
+ * Ordered clockwise from top-left for logical tab order.
29
+ */
30
+ const ALL_POSITIONS: HandlePosition[] = [
31
+ 'nw',
32
+ 'n',
33
+ 'ne',
34
+ 'e',
35
+ 'se',
36
+ 's',
37
+ 'sw',
38
+ 'w',
39
+ ];
40
+
41
+ /**
42
+ * Get the translated aria-label for a resize handle position.
43
+ *
44
+ * @param pos The handle position identifier.
45
+ * @return The translated label string.
46
+ */
47
+ function getHandleLabel( pos: HandlePosition ): string {
48
+ switch ( pos ) {
49
+ case 'n':
50
+ return __( 'Resize top edge' );
51
+ case 's':
52
+ return __( 'Resize bottom edge' );
53
+ case 'e':
54
+ return __( 'Resize right edge' );
55
+ case 'w':
56
+ return __( 'Resize left edge' );
57
+ case 'nw':
58
+ return __( 'Resize top-left corner' );
59
+ case 'ne':
60
+ return __( 'Resize top-right corner' );
61
+ case 'sw':
62
+ return __( 'Resize bottom-left corner' );
63
+ case 'se':
64
+ return __( 'Resize bottom-right corner' );
65
+ }
66
+ }
67
+
68
+ /** Fine step for keyboard-driven handle resize, in normalized coordinates. */
69
+ const KEYBOARD_STEP = 0.01;
70
+
71
+ /** Coarse step when Shift is held — 10× the fine step. */
72
+ const KEYBOARD_STEP_SHIFT = 0.1;
73
+
74
+ /** Delay before keyboard resize triggers settle (ms). */
75
+ const KEYBOARD_SETTLE_DELAY = 500;
76
+
77
+ /**
78
+ * Props for the RectangleStencil component.
79
+ */
80
+ type RectangleStencilProps = StencilProps;
81
+
82
+ /**
83
+ * A rectangular crop stencil with resize handles.
84
+ *
85
+ * In freeform mode, handles resize the crop area. Clicks inside the
86
+ * crop pass through to the container for image panning. The crop
87
+ * auto-centers after resize via SETTLE_CROP.
88
+ *
89
+ * @param props Component props implementing StencilProps.
90
+ * @param props.cropRect The crop rectangle in normalized coordinates.
91
+ * @param props.containerSize The container element dimensions in pixels.
92
+ * @param props.imageSize The rendered image dimensions in pixels.
93
+ * @param props.onCropChange Callback fired when the crop rect changes.
94
+ * @param props.onResizeStart Callback fired when a resize drag starts.
95
+ * @param props.onResizeEnd Callback fired when a resize drag ends (mouseup).
96
+ * @param props.aspectRatio Optional fixed aspect ratio (width / height).
97
+ * @param props.freeformCrop Whether resize handles are shown.
98
+ * @param props.stencilTransition CSS transition string for settle animation.
99
+ * @param props.cropBounds Maximum crop rect bounds from camera (zoom/rotation-aware).
100
+ * @param props.onEscape Called when Escape is pressed on a resize handle.
101
+ * @return The rectangle stencil element.
102
+ */
103
+ export function RectangleStencil( {
104
+ cropRect,
105
+ containerSize,
106
+ imageSize,
107
+ onCropChange,
108
+ onResizeStart,
109
+ onResizeEnd,
110
+ aspectRatio,
111
+ freeformCrop = false,
112
+ stencilTransition,
113
+ cropBounds,
114
+ onEscape,
115
+ }: RectangleStencilProps ) {
116
+ // Use cropBounds from the camera if available, otherwise default to [0,1].
117
+ const boundsMinX = cropBounds?.minX ?? 0;
118
+ const boundsMinY = cropBounds?.minY ?? 0;
119
+ const boundsMaxX = cropBounds?.maxX ?? 1;
120
+ const boundsMaxY = cropBounds?.maxY ?? 1;
121
+ const bounds: CropBounds = useMemo(
122
+ () => ( {
123
+ minX: boundsMinX,
124
+ minY: boundsMinY,
125
+ maxX: boundsMaxX,
126
+ maxY: boundsMaxY,
127
+ } ),
128
+ [ boundsMinX, boundsMinY, boundsMaxX, boundsMaxY ]
129
+ );
130
+ const keyboardSettleTimerRef = useRef< ReturnType< typeof setTimeout > >();
131
+ const keyboardResizeActiveRef = useRef( false );
132
+ const hasLockedRatio = !! ( aspectRatio && aspectRatio > 0 );
133
+
134
+ // Clear the pending keyboard settle timer on unmount so it can't
135
+ // fire onResizeEnd / dispatch onto an unmounted parent.
136
+ useEffect( () => {
137
+ return () => {
138
+ clearTimeout( keyboardSettleTimerRef.current );
139
+ keyboardResizeActiveRef.current = false;
140
+ };
141
+ }, [] );
142
+
143
+ // Latest callbacks for the drag listeners. The drag closure in
144
+ // handlePointerDown reads from this ref so it always sees current
145
+ // props without having to re-attach listeners mid-drag. Previously,
146
+ // drag listeners lived in a useEffect that re-ran whenever `bounds`
147
+ // changed (every SET_CROP_RECT dispatch), creating a window where
148
+ // pointerup could be missed and the crop would never settle.
149
+ const latestHandlersRef = useRef< {
150
+ hasLockedRatio: boolean;
151
+ computeLockedRect: (
152
+ drag: ResizeDragState,
153
+ clientX: number,
154
+ clientY: number
155
+ ) => NormalizedRect;
156
+ computeFreeRect: (
157
+ drag: ResizeDragState,
158
+ clientX: number,
159
+ clientY: number
160
+ ) => NormalizedRect;
161
+ computeShiftLockedRect: (
162
+ drag: ResizeDragState,
163
+ clientX: number,
164
+ clientY: number
165
+ ) => NormalizedRect;
166
+ onCropChange: ( rect: NormalizedRect ) => void;
167
+ onResizeEnd?: () => void;
168
+ } | null >( null );
169
+
170
+ // The normalized aspect ratio: the w/h ratio in normalized space that
171
+ // produces the desired pixel aspect ratio.
172
+ // pixelW = w * imageSize.width, pixelH = h * imageSize.height
173
+ // pixelW / pixelH = aspectRatio => w / h = aspectRatio * imageSize.height / imageSize.width
174
+ const normalizedRatio = useMemo( () => {
175
+ if ( ! hasLockedRatio || imageSize.width === 0 ) {
176
+ return 0;
177
+ }
178
+ return ( aspectRatio * imageSize.height ) / imageSize.width;
179
+ }, [ aspectRatio, hasLockedRatio, imageSize.width, imageSize.height ] );
180
+
181
+ // Convert normalized crop rect to pixel bounds, accounting for the
182
+ // image offset within the container.
183
+ const offsetX = ( containerSize.width - imageSize.width ) / 2;
184
+ const offsetY = ( containerSize.height - imageSize.height ) / 2;
185
+ const left = offsetX + cropRect.x * imageSize.width;
186
+ const top = offsetY + cropRect.y * imageSize.height;
187
+ const width = cropRect.width * imageSize.width;
188
+ const height = cropRect.height * imageSize.height;
189
+
190
+ /**
191
+ * Start a resize drag on a handle. Registers pointer listeners
192
+ * synchronously so no events are missed between capture and the
193
+ * next React commit.
194
+ */
195
+ const handlePointerDown = useCallback(
196
+ ( handle: HandlePosition, event: React.PointerEvent ) => {
197
+ if ( event.button !== 0 ) {
198
+ return;
199
+ }
200
+ event.preventDefault();
201
+ event.stopPropagation();
202
+ // Blur any previously focused handle.
203
+ const ownerDoc = event.currentTarget.ownerDocument;
204
+ if ( ownerDoc.activeElement instanceof HTMLElement ) {
205
+ ownerDoc.activeElement.blur();
206
+ }
207
+ // Capture pointer so drag works across iframe boundaries.
208
+ const el = event.currentTarget as HTMLButtonElement;
209
+ el.setPointerCapture( event.pointerId );
210
+
211
+ const drag: ResizeDragState = {
212
+ handle,
213
+ startX: event.clientX,
214
+ startY: event.clientY,
215
+ startRect: { ...cropRect },
216
+ };
217
+
218
+ // RAF-throttle: stash the latest pointer position and
219
+ // process it once per animation frame. Avoids dispatching
220
+ // on every pointermove (which fires at much higher than
221
+ // screen refresh rate) and matches the pan-drag path in
222
+ // InteractionController.
223
+ let rafId = 0;
224
+ let latestX = event.clientX;
225
+ let latestY = event.clientY;
226
+ let latestShift = event.shiftKey;
227
+
228
+ const onMove = ( e: Event ) => {
229
+ const pe = e as PointerEvent;
230
+ latestX = pe.clientX;
231
+ latestY = pe.clientY;
232
+ latestShift = pe.shiftKey;
233
+ if ( rafId ) {
234
+ return;
235
+ }
236
+ rafId = requestAnimationFrame( () => {
237
+ rafId = 0;
238
+ const h = latestHandlersRef.current;
239
+ if ( ! h ) {
240
+ return;
241
+ }
242
+ let newRect: NormalizedRect;
243
+ if ( h.hasLockedRatio ) {
244
+ newRect = h.computeLockedRect( drag, latestX, latestY );
245
+ } else if ( latestShift ) {
246
+ newRect = h.computeShiftLockedRect(
247
+ drag,
248
+ latestX,
249
+ latestY
250
+ );
251
+ } else {
252
+ newRect = h.computeFreeRect( drag, latestX, latestY );
253
+ }
254
+ h.onCropChange( newRect );
255
+ } );
256
+ };
257
+
258
+ // Guard against duplicate firing: pointerup and
259
+ // lostpointercapture both fire on normal release.
260
+ let ended = false;
261
+ const onEnd = () => {
262
+ if ( ended ) {
263
+ return;
264
+ }
265
+ ended = true;
266
+ if ( rafId ) {
267
+ cancelAnimationFrame( rafId );
268
+ rafId = 0;
269
+ }
270
+ el.removeEventListener( 'pointermove', onMove );
271
+ el.removeEventListener( 'pointerup', onEnd );
272
+ el.removeEventListener( 'lostpointercapture', onEnd );
273
+ latestHandlersRef.current?.onResizeEnd?.();
274
+ // Restore focus to the handle so arrow keys work
275
+ // immediately after a mouse drag. Browsers suppress
276
+ // :focus-visible after pointer interactions, so the
277
+ // focus ring stays hidden until the user presses a key.
278
+ el.focus( { preventScroll: true } );
279
+ };
280
+
281
+ el.addEventListener( 'pointermove', onMove );
282
+ el.addEventListener( 'pointerup', onEnd );
283
+ el.addEventListener( 'lostpointercapture', onEnd );
284
+
285
+ onResizeStart?.();
286
+ // Cancel any pending keyboard settle so it can't fire onResizeEnd
287
+ // mid-drag if the user switches from keyboard to pointer within
288
+ // the settle window.
289
+ clearTimeout( keyboardSettleTimerRef.current );
290
+ keyboardResizeActiveRef.current = false;
291
+ },
292
+ [ cropRect, onResizeStart ]
293
+ );
294
+
295
+ /**
296
+ * Compute the new crop rect for a free (no aspect ratio) resize.
297
+ * Delegates to the pure function in core/stencil-math.ts.
298
+ */
299
+ const computeFreeRect = useCallback(
300
+ (
301
+ drag: ResizeDragState,
302
+ clientX: number,
303
+ clientY: number
304
+ ): NormalizedRect =>
305
+ computeFreeResizeRect( drag, clientX, clientY, imageSize, bounds ),
306
+ [ imageSize, bounds ]
307
+ );
308
+
309
+ /**
310
+ * Compute the new crop rect for a locked-aspect-ratio corner resize.
311
+ * Delegates to the pure function in core/stencil-math.ts.
312
+ */
313
+ const computeLockedRect = useCallback(
314
+ (
315
+ drag: ResizeDragState,
316
+ clientX: number,
317
+ clientY: number
318
+ ): NormalizedRect =>
319
+ computeLockedResizeRect(
320
+ drag,
321
+ clientX,
322
+ clientY,
323
+ imageSize,
324
+ bounds,
325
+ normalizedRatio
326
+ ),
327
+ [ imageSize, bounds, normalizedRatio ]
328
+ );
329
+
330
+ /**
331
+ * Compute the new crop rect when Shift is held during a freeform
332
+ * resize — preserves the start rect's aspect ratio.
333
+ */
334
+ const computeShiftLockedRect = useCallback(
335
+ (
336
+ drag: ResizeDragState,
337
+ clientX: number,
338
+ clientY: number
339
+ ): NormalizedRect =>
340
+ computeShiftLockedResizeRect(
341
+ drag,
342
+ clientX,
343
+ clientY,
344
+ imageSize,
345
+ bounds
346
+ ),
347
+ [ imageSize, bounds ]
348
+ );
349
+
350
+ latestHandlersRef.current = {
351
+ hasLockedRatio,
352
+ computeLockedRect,
353
+ computeFreeRect,
354
+ computeShiftLockedRect,
355
+ onCropChange,
356
+ onResizeEnd,
357
+ };
358
+
359
+ /**
360
+ * Handle keyboard events on a resize handle.
361
+ * Arrow keys resize; Escape returns focus to the canvas.
362
+ * Shift multiplies the step size by 10 for coarser movement.
363
+ */
364
+ const handleKeyDown = useCallback(
365
+ ( handle: HandlePosition, event: React.KeyboardEvent ) => {
366
+ const key = event.key;
367
+
368
+ if ( key === 'Escape' ) {
369
+ event.preventDefault();
370
+ event.stopPropagation();
371
+ onEscape?.();
372
+ return;
373
+ }
374
+
375
+ if (
376
+ key !== 'ArrowUp' &&
377
+ key !== 'ArrowDown' &&
378
+ key !== 'ArrowLeft' &&
379
+ key !== 'ArrowRight'
380
+ ) {
381
+ return;
382
+ }
383
+
384
+ event.preventDefault();
385
+ event.stopPropagation();
386
+
387
+ if ( ! keyboardResizeActiveRef.current ) {
388
+ keyboardResizeActiveRef.current = true;
389
+ onResizeStart?.();
390
+ }
391
+
392
+ const scheduleKeyboardResizeEnd = () => {
393
+ clearTimeout( keyboardSettleTimerRef.current );
394
+ keyboardSettleTimerRef.current = setTimeout( () => {
395
+ keyboardResizeActiveRef.current = false;
396
+ onResizeEnd?.();
397
+ }, KEYBOARD_SETTLE_DELAY );
398
+ };
399
+
400
+ const step = event.shiftKey ? KEYBOARD_STEP_SHIFT : KEYBOARD_STEP;
401
+
402
+ // Determine the normalized delta from the arrow key.
403
+ let dx = 0;
404
+ let dy = 0;
405
+ if ( key === 'ArrowLeft' ) {
406
+ dx = -step;
407
+ }
408
+ if ( key === 'ArrowRight' ) {
409
+ dx = step;
410
+ }
411
+ if ( key === 'ArrowUp' ) {
412
+ dy = -step;
413
+ }
414
+ if ( key === 'ArrowDown' ) {
415
+ dy = step;
416
+ }
417
+
418
+ if ( hasLockedRatio ) {
419
+ // For locked aspect ratio, synthesize a drag from the
420
+ // current rect and apply the delta via computeLockedRect.
421
+ const syntheticDrag: ResizeDragState = {
422
+ handle,
423
+ startX: 0,
424
+ startY: 0,
425
+ startRect: { ...cropRect },
426
+ };
427
+ const clientX = dx * imageSize.width;
428
+ const clientY = dy * imageSize.height;
429
+ onCropChange(
430
+ computeLockedRect( syntheticDrag, clientX, clientY )
431
+ );
432
+ scheduleKeyboardResizeEnd();
433
+ } else {
434
+ // For freeform resize, synthesize a drag via computeFreeRect.
435
+ const syntheticDrag: ResizeDragState = {
436
+ handle,
437
+ startX: 0,
438
+ startY: 0,
439
+ startRect: { ...cropRect },
440
+ };
441
+ const clientX = dx * imageSize.width;
442
+ const clientY = dy * imageSize.height;
443
+ onCropChange(
444
+ computeFreeRect( syntheticDrag, clientX, clientY )
445
+ );
446
+ scheduleKeyboardResizeEnd();
447
+ }
448
+ },
449
+ [
450
+ cropRect,
451
+ hasLockedRatio,
452
+ imageSize.width,
453
+ imageSize.height,
454
+ computeLockedRect,
455
+ computeFreeRect,
456
+ onCropChange,
457
+ onResizeStart,
458
+ onResizeEnd,
459
+ onEscape,
460
+ ]
461
+ );
462
+
463
+ if ( containerSize.width === 0 || containerSize.height === 0 ) {
464
+ return null;
465
+ }
466
+
467
+ const handles = hasLockedRatio ? CORNER_POSITIONS : ALL_POSITIONS;
468
+
469
+ return (
470
+ <div
471
+ className="wp-media-editor-image-editor__stencil"
472
+ style={ {
473
+ left,
474
+ top,
475
+ width,
476
+ height,
477
+ transition: stencilTransition,
478
+ } }
479
+ >
480
+ { /* The crop rectangle border. pointer-events: none is set in
481
+ CSS so clicks pass through to the container for panning. */ }
482
+ <div
483
+ className="wp-media-editor-image-editor__stencil-rect"
484
+ style={ {
485
+ width: '100%',
486
+ height: '100%',
487
+ top: 0,
488
+ left: 0,
489
+ } }
490
+ />
491
+ { /* Resize handles — only in freeform mode.
492
+ Semantics: role="button" with aria-label describing
493
+ which edge/corner. Arrow keys resize (onKeyDown).
494
+ role="slider" would be more precise but requires
495
+ per-handle aria-valuemin/max/now which don't map
496
+ cleanly to a 2D crop rect. */ }
497
+ { freeformCrop &&
498
+ handles.map( ( pos ) => (
499
+ <button
500
+ key={ pos }
501
+ type="button"
502
+ className={ `wp-media-editor-image-editor__handle wp-media-editor-image-editor__handle--${ pos }` }
503
+ onPointerDown={ ( event ) =>
504
+ handlePointerDown( pos, event )
505
+ }
506
+ onTouchStart={ ( event ) => event.stopPropagation() }
507
+ onKeyDown={ ( event ) => handleKeyDown( pos, event ) }
508
+ aria-label={ getHandleLabel( pos ) }
509
+ />
510
+ ) ) }
511
+ </div>
512
+ );
513
+ }