@wordpress/media-editor 0.7.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 (306) 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/components/media-preview/index.cjs +1 -1
  12. package/build/components/media-preview/index.cjs.map +2 -2
  13. package/build/image-editor/core/camera.cjs +194 -0
  14. package/build/image-editor/core/camera.cjs.map +7 -0
  15. package/build/image-editor/core/constants.cjs +81 -0
  16. package/build/image-editor/core/constants.cjs.map +7 -0
  17. package/build/image-editor/core/containment.cjs +245 -0
  18. package/build/image-editor/core/containment.cjs.map +7 -0
  19. package/build/image-editor/core/export/canvas-renderer.cjs +151 -0
  20. package/build/image-editor/core/export/canvas-renderer.cjs.map +7 -0
  21. package/build/image-editor/core/index.cjs +50 -0
  22. package/build/image-editor/core/index.cjs.map +7 -0
  23. package/build/image-editor/core/interaction-controller.cjs +608 -0
  24. package/build/image-editor/core/interaction-controller.cjs.map +7 -0
  25. package/build/image-editor/core/math/rotation.cjs +41 -0
  26. package/build/image-editor/core/math/rotation.cjs.map +7 -0
  27. package/build/image-editor/core/source-region.cjs +95 -0
  28. package/build/image-editor/core/source-region.cjs.map +7 -0
  29. package/build/image-editor/core/state.cjs +269 -0
  30. package/build/image-editor/core/state.cjs.map +7 -0
  31. package/build/image-editor/core/stencil-math.cjs +197 -0
  32. package/build/image-editor/core/stencil-math.cjs.map +7 -0
  33. package/build/image-editor/core/transform-style.cjs +46 -0
  34. package/build/image-editor/core/transform-style.cjs.map +7 -0
  35. package/build/image-editor/core/transforms/pipeline.cjs +43 -0
  36. package/build/image-editor/core/transforms/pipeline.cjs.map +7 -0
  37. package/build/image-editor/core/types.cjs +19 -0
  38. package/build/image-editor/core/types.cjs.map +7 -0
  39. package/build/image-editor/index.cjs +57 -0
  40. package/build/image-editor/index.cjs.map +7 -0
  41. package/build/image-editor/react/components/cropper-provider.cjs +50 -0
  42. package/build/image-editor/react/components/cropper-provider.cjs.map +7 -0
  43. package/build/image-editor/react/components/cropper.cjs +365 -0
  44. package/build/image-editor/react/components/cropper.cjs.map +7 -0
  45. package/build/image-editor/react/components/index.cjs +36 -0
  46. package/build/image-editor/react/components/index.cjs.map +7 -0
  47. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +58 -0
  48. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +7 -0
  49. package/build/image-editor/react/components/overlays/grid-overlay.cjs +91 -0
  50. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +7 -0
  51. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +351 -0
  52. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +7 -0
  53. package/build/image-editor/react/hooks/index.cjs +31 -0
  54. package/build/image-editor/react/hooks/index.cjs.map +7 -0
  55. package/build/image-editor/react/hooks/use-aria-announcer.cjs +68 -0
  56. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +7 -0
  57. package/build/image-editor/react/hooks/use-cropper-state.cjs +158 -0
  58. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +7 -0
  59. package/build/image-editor/react/hooks/use-interaction.cjs +161 -0
  60. package/build/image-editor/react/hooks/use-interaction.cjs.map +7 -0
  61. package/build/image-editor/react/hooks/use-transform-style.cjs +38 -0
  62. package/build/image-editor/react/hooks/use-transform-style.cjs.map +7 -0
  63. package/build/index.cjs +5 -2
  64. package/build/index.cjs.map +2 -2
  65. package/build/lock-unlock.cjs +37 -0
  66. package/build/lock-unlock.cjs.map +7 -0
  67. package/build/private-apis.cjs +38 -0
  68. package/build/private-apis.cjs.map +7 -0
  69. package/build/store/actions.cjs +45 -0
  70. package/build/store/actions.cjs.map +7 -0
  71. package/build/store/constants.cjs +31 -0
  72. package/build/store/constants.cjs.map +7 -0
  73. package/build/store/index.cjs +51 -0
  74. package/build/store/index.cjs.map +7 -0
  75. package/build/store/reducer.cjs +51 -0
  76. package/build/store/reducer.cjs.map +7 -0
  77. package/build/store/selectors.cjs +43 -0
  78. package/build/store/selectors.cjs.map +7 -0
  79. package/build-module/components/media-editor-canvas/index.mjs +33 -0
  80. package/build-module/components/media-editor-canvas/index.mjs.map +7 -0
  81. package/build-module/components/media-editor-crop-panel/index.mjs +100 -0
  82. package/build-module/components/media-editor-crop-panel/index.mjs.map +7 -0
  83. package/build-module/components/media-editor-modal/build-modifiers.mjs +66 -0
  84. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +7 -0
  85. package/build-module/components/media-editor-modal/index.mjs +483 -0
  86. package/build-module/components/media-editor-modal/index.mjs.map +7 -0
  87. package/build-module/components/media-editor-toolbar/index.mjs +130 -0
  88. package/build-module/components/media-editor-toolbar/index.mjs.map +7 -0
  89. package/build-module/components/media-preview/index.mjs +1 -1
  90. package/build-module/components/media-preview/index.mjs.map +2 -2
  91. package/build-module/image-editor/core/camera.mjs +163 -0
  92. package/build-module/image-editor/core/camera.mjs.map +7 -0
  93. package/build-module/image-editor/core/constants.mjs +51 -0
  94. package/build-module/image-editor/core/constants.mjs.map +7 -0
  95. package/build-module/image-editor/core/containment.mjs +218 -0
  96. package/build-module/image-editor/core/containment.mjs.map +7 -0
  97. package/build-module/image-editor/core/export/canvas-renderer.mjs +120 -0
  98. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +7 -0
  99. package/build-module/image-editor/core/index.mjs +24 -0
  100. package/build-module/image-editor/core/index.mjs.map +7 -0
  101. package/build-module/image-editor/core/interaction-controller.mjs +583 -0
  102. package/build-module/image-editor/core/interaction-controller.mjs.map +7 -0
  103. package/build-module/image-editor/core/math/rotation.mjs +15 -0
  104. package/build-module/image-editor/core/math/rotation.mjs.map +7 -0
  105. package/build-module/image-editor/core/source-region.mjs +69 -0
  106. package/build-module/image-editor/core/source-region.mjs.map +7 -0
  107. package/build-module/image-editor/core/state.mjs +242 -0
  108. package/build-module/image-editor/core/state.mjs.map +7 -0
  109. package/build-module/image-editor/core/stencil-math.mjs +169 -0
  110. package/build-module/image-editor/core/stencil-math.mjs.map +7 -0
  111. package/build-module/image-editor/core/transform-style.mjs +21 -0
  112. package/build-module/image-editor/core/transform-style.mjs.map +7 -0
  113. package/build-module/image-editor/core/transforms/pipeline.mjs +17 -0
  114. package/build-module/image-editor/core/transforms/pipeline.mjs.map +7 -0
  115. package/build-module/image-editor/core/types.mjs +1 -0
  116. package/build-module/image-editor/core/types.mjs.map +7 -0
  117. package/build-module/image-editor/index.mjs +30 -0
  118. package/build-module/image-editor/index.mjs.map +7 -0
  119. package/build-module/image-editor/react/components/cropper-provider.mjs +26 -0
  120. package/build-module/image-editor/react/components/cropper-provider.mjs.map +7 -0
  121. package/build-module/image-editor/react/components/cropper.mjs +337 -0
  122. package/build-module/image-editor/react/components/cropper.mjs.map +7 -0
  123. package/build-module/image-editor/react/components/index.mjs +9 -0
  124. package/build-module/image-editor/react/components/index.mjs.map +7 -0
  125. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +33 -0
  126. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +7 -0
  127. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +66 -0
  128. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +7 -0
  129. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +330 -0
  130. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +7 -0
  131. package/build-module/image-editor/react/hooks/index.mjs +6 -0
  132. package/build-module/image-editor/react/hooks/index.mjs.map +7 -0
  133. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +43 -0
  134. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +7 -0
  135. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +137 -0
  136. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +7 -0
  137. package/build-module/image-editor/react/hooks/use-interaction.mjs +138 -0
  138. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +7 -0
  139. package/build-module/image-editor/react/hooks/use-transform-style.mjs +13 -0
  140. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +7 -0
  141. package/build-module/index.mjs +3 -1
  142. package/build-module/index.mjs.map +2 -2
  143. package/build-module/lock-unlock.mjs +11 -0
  144. package/build-module/lock-unlock.mjs.map +7 -0
  145. package/build-module/private-apis.mjs +13 -0
  146. package/build-module/private-apis.mjs.map +7 -0
  147. package/build-module/store/actions.mjs +19 -0
  148. package/build-module/store/actions.mjs.map +7 -0
  149. package/build-module/store/constants.mjs +6 -0
  150. package/build-module/store/constants.mjs.map +7 -0
  151. package/build-module/store/index.mjs +16 -0
  152. package/build-module/store/index.mjs.map +7 -0
  153. package/build-module/store/reducer.mjs +26 -0
  154. package/build-module/store/reducer.mjs.map +7 -0
  155. package/build-module/store/selectors.mjs +16 -0
  156. package/build-module/store/selectors.mjs.map +7 -0
  157. package/build-style/style-rtl.css +315 -0
  158. package/build-style/style.css +315 -0
  159. package/build-types/components/media-editor-canvas/index.d.ts +22 -0
  160. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -0
  161. package/build-types/components/media-editor-crop-panel/index.d.ts +45 -0
  162. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -0
  163. package/build-types/components/media-editor-modal/build-modifiers.d.ts +83 -0
  164. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -0
  165. package/build-types/components/media-editor-modal/index.d.ts +20 -0
  166. package/build-types/components/media-editor-modal/index.d.ts.map +1 -0
  167. package/build-types/components/media-editor-toolbar/index.d.ts +21 -0
  168. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -0
  169. package/build-types/components/media-preview/index.d.ts.map +1 -1
  170. package/build-types/image-editor/core/camera.d.ts +107 -0
  171. package/build-types/image-editor/core/camera.d.ts.map +1 -0
  172. package/build-types/image-editor/core/constants.d.ts +46 -0
  173. package/build-types/image-editor/core/constants.d.ts.map +1 -0
  174. package/build-types/image-editor/core/containment.d.ts +61 -0
  175. package/build-types/image-editor/core/containment.d.ts.map +1 -0
  176. package/build-types/image-editor/core/export/canvas-renderer.d.ts +106 -0
  177. package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +1 -0
  178. package/build-types/image-editor/core/index.d.ts +8 -0
  179. package/build-types/image-editor/core/index.d.ts.map +1 -0
  180. package/build-types/image-editor/core/interaction-controller.d.ts +178 -0
  181. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -0
  182. package/build-types/image-editor/core/math/rotation.d.ts +18 -0
  183. package/build-types/image-editor/core/math/rotation.d.ts.map +1 -0
  184. package/build-types/image-editor/core/source-region.d.ts +75 -0
  185. package/build-types/image-editor/core/source-region.d.ts.map +1 -0
  186. package/build-types/image-editor/core/state.d.ts +42 -0
  187. package/build-types/image-editor/core/state.d.ts.map +1 -0
  188. package/build-types/image-editor/core/stencil-math.d.ts +79 -0
  189. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -0
  190. package/build-types/image-editor/core/transform-style.d.ts +23 -0
  191. package/build-types/image-editor/core/transform-style.d.ts.map +1 -0
  192. package/build-types/image-editor/core/transforms/pipeline.d.ts +27 -0
  193. package/build-types/image-editor/core/transforms/pipeline.d.ts.map +1 -0
  194. package/build-types/image-editor/core/types.d.ts +211 -0
  195. package/build-types/image-editor/core/types.d.ts.map +1 -0
  196. package/build-types/image-editor/index.d.ts +16 -0
  197. package/build-types/image-editor/index.d.ts.map +1 -0
  198. package/build-types/image-editor/react/components/cropper-provider.d.ts +37 -0
  199. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -0
  200. package/build-types/image-editor/react/components/cropper.d.ts +59 -0
  201. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -0
  202. package/build-types/image-editor/react/components/index.d.ts +4 -0
  203. package/build-types/image-editor/react/components/index.d.ts.map +1 -0
  204. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +30 -0
  205. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -0
  206. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +30 -0
  207. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -0
  208. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +32 -0
  209. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -0
  210. package/build-types/image-editor/react/hooks/index.d.ts +3 -0
  211. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -0
  212. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +16 -0
  213. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -0
  214. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +68 -0
  215. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -0
  216. package/build-types/image-editor/react/hooks/use-interaction.d.ts +60 -0
  217. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -0
  218. package/build-types/image-editor/react/hooks/use-transform-style.d.ts +16 -0
  219. package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +1 -0
  220. package/build-types/image-editor/stories/rectangle-crop.story.d.ts +16 -0
  221. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -0
  222. package/build-types/index.d.ts +1 -0
  223. package/build-types/index.d.ts.map +1 -1
  224. package/build-types/lock-unlock.d.ts +2 -0
  225. package/build-types/lock-unlock.d.ts.map +1 -0
  226. package/build-types/private-apis.d.ts +2 -0
  227. package/build-types/private-apis.d.ts.map +1 -0
  228. package/build-types/store/actions.d.ts +27 -0
  229. package/build-types/store/actions.d.ts.map +1 -0
  230. package/build-types/store/constants.d.ts +2 -0
  231. package/build-types/store/constants.d.ts.map +1 -0
  232. package/build-types/store/index.d.ts +4 -0
  233. package/build-types/store/index.d.ts.map +1 -0
  234. package/build-types/store/reducer.d.ts +23 -0
  235. package/build-types/store/reducer.d.ts.map +1 -0
  236. package/build-types/store/selectors.d.ts +8 -0
  237. package/build-types/store/selectors.d.ts.map +1 -0
  238. package/package.json +21 -8
  239. package/src/components/media-editor-canvas/index.tsx +56 -0
  240. package/src/components/media-editor-canvas/style.scss +9 -0
  241. package/src/components/media-editor-crop-panel/index.tsx +143 -0
  242. package/src/components/media-editor-modal/build-modifiers.ts +164 -0
  243. package/src/components/media-editor-modal/index.tsx +639 -0
  244. package/src/components/media-editor-modal/style.scss +139 -0
  245. package/src/components/media-editor-modal/test/build-modifiers.test.ts +343 -0
  246. package/src/components/media-editor-toolbar/index.tsx +150 -0
  247. package/src/components/media-editor-toolbar/style.scss +21 -0
  248. package/src/components/media-preview/index.tsx +2 -1
  249. package/src/image-editor/README.md +162 -0
  250. package/src/image-editor/core/camera.ts +352 -0
  251. package/src/image-editor/core/constants.ts +93 -0
  252. package/src/image-editor/core/containment.ts +435 -0
  253. package/src/image-editor/core/export/canvas-renderer.ts +253 -0
  254. package/src/image-editor/core/export/test/canvas-renderer.ts +813 -0
  255. package/src/image-editor/core/index.ts +35 -0
  256. package/src/image-editor/core/interaction-controller.ts +889 -0
  257. package/src/image-editor/core/math/rotation.ts +26 -0
  258. package/src/image-editor/core/source-region.ts +166 -0
  259. package/src/image-editor/core/state.ts +428 -0
  260. package/src/image-editor/core/stencil-math.ts +330 -0
  261. package/src/image-editor/core/test/camera.ts +593 -0
  262. package/src/image-editor/core/test/interaction-controller.ts +996 -0
  263. package/src/image-editor/core/test/preview-export-parity.ts +242 -0
  264. package/src/image-editor/core/test/render-camera-bridge.ts +348 -0
  265. package/src/image-editor/core/test/state.ts +762 -0
  266. package/src/image-editor/core/test/stencil-math.ts +321 -0
  267. package/src/image-editor/core/transform-style.ts +44 -0
  268. package/src/image-editor/core/transforms/pipeline.ts +43 -0
  269. package/src/image-editor/core/transforms/test/pipeline.ts +269 -0
  270. package/src/image-editor/core/types.ts +177 -0
  271. package/src/image-editor/docs/architecture.md +96 -0
  272. package/src/image-editor/docs/recipes.md +720 -0
  273. package/src/image-editor/docs/roadmap.md +66 -0
  274. package/src/image-editor/index.ts +43 -0
  275. package/src/image-editor/react/components/cropper-provider.tsx +73 -0
  276. package/src/image-editor/react/components/cropper.scss +195 -0
  277. package/src/image-editor/react/components/cropper.tsx +534 -0
  278. package/src/image-editor/react/components/index.ts +3 -0
  279. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +57 -0
  280. package/src/image-editor/react/components/overlays/grid-overlay.tsx +80 -0
  281. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +513 -0
  282. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +265 -0
  283. package/src/image-editor/react/components/test/cropper.tsx +140 -0
  284. package/src/image-editor/react/hooks/index.ts +2 -0
  285. package/src/image-editor/react/hooks/test/use-cropper-state.ts +913 -0
  286. package/src/image-editor/react/hooks/test/use-interaction.ts +218 -0
  287. package/src/image-editor/react/hooks/test/use-transform-style.ts +146 -0
  288. package/src/image-editor/react/hooks/use-aria-announcer.ts +64 -0
  289. package/src/image-editor/react/hooks/use-cropper-state.ts +238 -0
  290. package/src/image-editor/react/hooks/use-interaction.ts +223 -0
  291. package/src/image-editor/react/hooks/use-transform-style.ts +30 -0
  292. package/src/image-editor/stories/rectangle-crop.story.tsx +1085 -0
  293. package/src/image-editor/stories/style.css +94 -0
  294. package/src/image-editor/style.scss +1 -0
  295. package/src/index.ts +3 -0
  296. package/src/lock-unlock.ts +10 -0
  297. package/src/private-apis.ts +12 -0
  298. package/src/store/actions.ts +33 -0
  299. package/src/store/constants.ts +1 -0
  300. package/src/store/index.ts +20 -0
  301. package/src/store/reducer.ts +48 -0
  302. package/src/store/selectors.ts +16 -0
  303. package/src/store/test/reducer.ts +57 -0
  304. package/src/style.scss +4 -0
  305. package/tsconfig.json +10 -2
  306. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,1085 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import type { Meta, StoryObj } from '@storybook/react-vite';
5
+
6
+ /**
7
+ * WordPress dependencies
8
+ */
9
+ import { useState, useCallback, useEffect, useRef } from '@wordpress/element';
10
+ import {
11
+ Button,
12
+ SelectControl,
13
+ RangeControl,
14
+ ToggleControl,
15
+ Flex,
16
+ FlexItem,
17
+ } from '@wordpress/components';
18
+ import {
19
+ rotateLeft,
20
+ rotateRight,
21
+ flipHorizontal,
22
+ flipVertical,
23
+ reset as resetIcon,
24
+ cloudUpload,
25
+ download as downloadIcon,
26
+ } from '@wordpress/icons';
27
+
28
+ /**
29
+ * Internal dependencies
30
+ */
31
+ import { Cropper } from '../react/components/cropper';
32
+ import { useCropperState } from '../react/hooks/use-cropper-state';
33
+ import {
34
+ MIN_ZOOM,
35
+ MAX_ZOOM,
36
+ MAX_ROTATION_OFFSET,
37
+ DEFAULT_ASPECT_RATIOS,
38
+ ORIGINAL_ASPECT_RATIO,
39
+ } from '../core/constants';
40
+ import {
41
+ loadImage,
42
+ renderToCanvas,
43
+ canvasToDataURL,
44
+ downloadCroppedImage,
45
+ } from '../core/export/canvas-renderer';
46
+ import {
47
+ getRotatedBBox,
48
+ createCamera,
49
+ screenToWorld,
50
+ getImageFit,
51
+ getVisibleBounds,
52
+ } from '../core/camera';
53
+ import { restrictPanZoom } from '../core/containment';
54
+ import { getSourceRegion } from '../core/source-region';
55
+ import './style.css';
56
+
57
+ const SAMPLE_IMAGE = 'image-editor-demo.jpeg';
58
+
59
+ const GRID_MODES = {
60
+ off: false,
61
+ on: true,
62
+ interactive: 'interactive',
63
+ } as const;
64
+
65
+ type GridMode = keyof typeof GRID_MODES;
66
+
67
+ const IMAGE_CREDIT = (
68
+ <p style={ { fontSize: 10, color: '#aaa', margin: '4px 0 0' } }>
69
+ { '"A fashionable melange of English words (1887)" ' }
70
+ <a
71
+ href="http://publicdomainreview.org/2013/01/23/a-fashionable-melange-of-english-words-1887"
72
+ style={ { color: '#bbb' } }
73
+ >
74
+ publicdomainreview.org
75
+ </a>
76
+ { ' by See-ming Lee (SML) is licensed under CC BY 2.0.' }
77
+ </p>
78
+ );
79
+
80
+ // Hook: manage a swappable image source. Defaults to the sample image.
81
+ // When a file is picked, reads it as a data URL so the <img> src updates
82
+ // without leaving the page. The consumer also gets a reset callback to
83
+ // return to the sample.
84
+ function useUploadableImage() {
85
+ const [ src, setSrc ] = useState< string >( SAMPLE_IMAGE );
86
+ const [ isCustom, setIsCustom ] = useState( false );
87
+ const handleFileChange = useCallback(
88
+ ( event: React.ChangeEvent< HTMLInputElement > ) => {
89
+ const file = event.target.files?.[ 0 ];
90
+ if ( ! file ) {
91
+ return;
92
+ }
93
+ const reader = new FileReader();
94
+ reader.onload = () => {
95
+ if ( typeof reader.result === 'string' ) {
96
+ setSrc( reader.result );
97
+ setIsCustom( true );
98
+ }
99
+ };
100
+ reader.readAsDataURL( file );
101
+ },
102
+ []
103
+ );
104
+ const resetToSample = useCallback( () => {
105
+ setSrc( SAMPLE_IMAGE );
106
+ setIsCustom( false );
107
+ }, [] );
108
+ return { src, isCustom, handleFileChange, resetToSample };
109
+ }
110
+
111
+ /**
112
+ * Resolve an aspect ratio value from the select dropdown.
113
+ * 0 = free (no lock), ORIGINAL_ASPECT_RATIO (-1) = image's natural ratio.
114
+ */
115
+ function resolveAspectRatio(
116
+ value: string,
117
+ imageState: { naturalWidth: number; naturalHeight: number } | null
118
+ ): number | undefined {
119
+ const num = parseFloat( value );
120
+ if ( num === 0 ) {
121
+ return undefined; // Free — no lock.
122
+ }
123
+ if ( num === ORIGINAL_ASPECT_RATIO && imageState ) {
124
+ return imageState.naturalWidth / imageState.naturalHeight;
125
+ }
126
+ if ( num > 0 ) {
127
+ return num;
128
+ }
129
+ return undefined;
130
+ }
131
+
132
+ const meta: Meta< typeof Cropper > = {
133
+ title: 'MediaEditor/ImageEditor',
134
+ component: Cropper,
135
+ tags: [ 'status-experimental' ],
136
+ };
137
+
138
+ export default meta;
139
+
140
+ type Story = StoryObj< typeof Cropper >;
141
+
142
+ /**
143
+ * Default story. Basic cropper with a sample image, no controls.
144
+ */
145
+ const DefaultComponent = () => {
146
+ const controller = useCropperState();
147
+
148
+ return (
149
+ <div>
150
+ <div className="image-editor-story__container">
151
+ <Cropper
152
+ src={ SAMPLE_IMAGE }
153
+ controller={ controller }
154
+ showDimming
155
+ />
156
+ </div>
157
+ { IMAGE_CREDIT }
158
+ </div>
159
+ );
160
+ };
161
+
162
+ export const Default: Story = {
163
+ render: DefaultComponent,
164
+ };
165
+
166
+ /**
167
+ * Full interactive demo with controls.
168
+ */
169
+ const WithControlsComponent = () => {
170
+ const controller = useCropperState();
171
+ const {
172
+ state,
173
+ setRotation,
174
+ setFlip,
175
+ setZoom,
176
+ setCropRect,
177
+ snapRotate90,
178
+ reset,
179
+ isDirty,
180
+ } = controller;
181
+
182
+ const [ aspectRatioValue, setAspectRatioValue ] = useState( '0' );
183
+ const [ freeformCrop, setFreeformCrop ] = useState( false );
184
+ const [ gridMode, setGridMode ] = useState< GridMode >( 'interactive' );
185
+ const { src, isCustom, handleFileChange, resetToSample } =
186
+ useUploadableImage();
187
+ const fileInputRef = useRef< HTMLInputElement >( null );
188
+ const openFilePicker = useCallback( () => {
189
+ fileInputRef.current?.click();
190
+ }, [] );
191
+
192
+ // Reset crop/pan/zoom when the image changes so the old framing
193
+ // doesn't linger on a newly-shaped image.
194
+ const prevSrcRef = useRef( src );
195
+ useEffect( () => {
196
+ if ( prevSrcRef.current !== src ) {
197
+ prevSrcRef.current = src;
198
+ reset();
199
+ setAspectRatioValue( '0' );
200
+ }
201
+ }, [ src, reset ] );
202
+
203
+ // Compute the crop dimensions in source pixels.
204
+ const cropDimensions = state.image
205
+ ? getSourceRegion( state, {
206
+ width: state.image.naturalWidth,
207
+ height: state.image.naturalHeight,
208
+ } )
209
+ : null;
210
+
211
+ // The base cardinal angle (nearest 90° step) and the fine offset.
212
+ // `setRotation` is a raw state setter (absolute-angle assignment), so
213
+ // sliders need to flip the offset sign when a single flip is active to
214
+ // stay visually consistent. `snapRotate90` and the pipeline `rotate`
215
+ // op already handle this internally.
216
+ const baseAngle = Math.round( state.rotation / 90 ) * 90;
217
+ const singleFlip = state.flip.horizontal !== state.flip.vertical;
218
+ const visualDir = singleFlip ? -1 : 1;
219
+ const fineOffset = ( state.rotation - baseAngle ) * visualDir;
220
+
221
+ const handleRotateLeft = useCallback( () => {
222
+ snapRotate90( -1 );
223
+ }, [ snapRotate90 ] );
224
+
225
+ const handleRotateRight = useCallback( () => {
226
+ snapRotate90( 1 );
227
+ }, [ snapRotate90 ] );
228
+
229
+ const handleRotationSlider = useCallback(
230
+ ( value: number | undefined ) => {
231
+ if ( value === undefined ) {
232
+ return;
233
+ }
234
+ // Clamp strictly inside [-MAX, MAX). Reaching exactly ±MAX
235
+ // lands state.rotation on a 90° midpoint, which flips the
236
+ // computed baseAngle on the next render; subsequent slider
237
+ // events then spiral because they're added to the NEW base.
238
+ const EPS = 0.01;
239
+ const clamped = Math.max(
240
+ -MAX_ROTATION_OFFSET + EPS,
241
+ Math.min( MAX_ROTATION_OFFSET - EPS, value )
242
+ );
243
+ setRotation( baseAngle + clamped * visualDir );
244
+ },
245
+ [ baseAngle, setRotation, visualDir ]
246
+ );
247
+
248
+ const handleFlipHorizontal = useCallback( () => {
249
+ setFlip( {
250
+ horizontal: ! state.flip.horizontal,
251
+ vertical: state.flip.vertical,
252
+ } );
253
+ }, [ state.flip, setFlip ] );
254
+
255
+ const handleFlipVertical = useCallback( () => {
256
+ setFlip( {
257
+ horizontal: state.flip.horizontal,
258
+ vertical: ! state.flip.vertical,
259
+ } );
260
+ }, [ state.flip, setFlip ] );
261
+
262
+ const handleZoomChange = useCallback(
263
+ ( value: number | undefined ) => {
264
+ if ( value === undefined ) {
265
+ return;
266
+ }
267
+ setZoom( value );
268
+ },
269
+ [ setZoom ]
270
+ );
271
+
272
+ const handleAspectRatioChange = useCallback(
273
+ ( value: string ) => {
274
+ setAspectRatioValue( value );
275
+
276
+ // In fixed mode (freeformCrop=false), the cropper's useEffect
277
+ // auto-computes the crop rect from the aspect ratio. In freeform
278
+ // mode, we adjust the crop rect here to fit the new ratio.
279
+ const resolved = resolveAspectRatio( value, state.image );
280
+ if ( freeformCrop && resolved && resolved > 0 && state.image ) {
281
+ const ratio = resolved;
282
+ const natW = state.image.naturalWidth;
283
+ const natH = state.image.naturalHeight;
284
+ const visualBBox = getRotatedBBox( natW, natH, state.rotation );
285
+ // normalizedRatio = w/h in normalized space that produces
286
+ // the desired pixel aspect ratio.
287
+ const normalizedRatio =
288
+ ( ratio * visualBBox.height ) / visualBBox.width;
289
+
290
+ let w = state.cropRect.width;
291
+ let h = w / normalizedRatio;
292
+
293
+ if ( h > 1 ) {
294
+ h = state.cropRect.height;
295
+ w = h * normalizedRatio;
296
+ }
297
+
298
+ w = Math.min( w, 1 );
299
+ h = Math.min( h, 1 );
300
+
301
+ setCropRect( {
302
+ x: ( 1 - w ) / 2,
303
+ y: ( 1 - h ) / 2,
304
+ width: w,
305
+ height: h,
306
+ } );
307
+ }
308
+ },
309
+ [
310
+ freeformCrop,
311
+ state.image,
312
+ state.cropRect,
313
+ state.rotation,
314
+ setCropRect,
315
+ ]
316
+ );
317
+
318
+ const handleReset = useCallback( () => {
319
+ reset();
320
+ setAspectRatioValue( '0' );
321
+ }, [ reset ] );
322
+
323
+ return (
324
+ <div>
325
+ <input
326
+ ref={ fileInputRef }
327
+ type="file"
328
+ accept="image/*"
329
+ onChange={ handleFileChange }
330
+ className="image-editor-story__hidden-file"
331
+ />
332
+ <div className="image-editor-story__toolbar">
333
+ <Flex align="center" gap={ 2 } wrap>
334
+ <FlexItem>
335
+ <Button
336
+ __next40pxDefaultSize
337
+ variant="primary"
338
+ icon={ cloudUpload }
339
+ onClick={ openFilePicker }
340
+ >
341
+ Upload
342
+ </Button>
343
+ </FlexItem>
344
+ { isCustom && (
345
+ <FlexItem>
346
+ <Button
347
+ __next40pxDefaultSize
348
+ variant="tertiary"
349
+ onClick={ resetToSample }
350
+ >
351
+ Use sample
352
+ </Button>
353
+ </FlexItem>
354
+ ) }
355
+ <FlexItem>
356
+ <Button
357
+ __next40pxDefaultSize
358
+ variant="secondary"
359
+ icon={ rotateLeft }
360
+ label="Rotate 90° counter-clockwise"
361
+ showTooltip
362
+ onClick={ handleRotateLeft }
363
+ />
364
+ </FlexItem>
365
+ <FlexItem>
366
+ <Button
367
+ __next40pxDefaultSize
368
+ variant="secondary"
369
+ icon={ rotateRight }
370
+ label="Rotate 90° clockwise"
371
+ showTooltip
372
+ onClick={ handleRotateRight }
373
+ />
374
+ </FlexItem>
375
+ <FlexItem>
376
+ <Button
377
+ __next40pxDefaultSize
378
+ variant="secondary"
379
+ icon={ flipHorizontal }
380
+ label="Flip horizontal"
381
+ showTooltip
382
+ isPressed={ state.flip.horizontal }
383
+ onClick={ handleFlipHorizontal }
384
+ />
385
+ </FlexItem>
386
+ <FlexItem>
387
+ <Button
388
+ __next40pxDefaultSize
389
+ variant="secondary"
390
+ icon={ flipVertical }
391
+ label="Flip vertical"
392
+ showTooltip
393
+ isPressed={ state.flip.vertical }
394
+ onClick={ handleFlipVertical }
395
+ />
396
+ </FlexItem>
397
+ <FlexItem>
398
+ <SelectControl
399
+ __next40pxDefaultSize
400
+ __nextHasNoMarginBottom
401
+ label="Aspect ratio"
402
+ hideLabelFromVision
403
+ value={ aspectRatioValue }
404
+ onChange={ handleAspectRatioChange }
405
+ options={ DEFAULT_ASPECT_RATIOS.map(
406
+ ( preset ) => ( {
407
+ label: preset.label,
408
+ value: preset.value.toString(),
409
+ } )
410
+ ) }
411
+ />
412
+ </FlexItem>
413
+ <FlexItem>
414
+ <ToggleControl
415
+ __nextHasNoMarginBottom
416
+ label="Freeform"
417
+ checked={ freeformCrop }
418
+ onChange={ setFreeformCrop }
419
+ />
420
+ </FlexItem>
421
+ <FlexItem>
422
+ <SelectControl
423
+ __next40pxDefaultSize
424
+ __nextHasNoMarginBottom
425
+ label="Grid"
426
+ hideLabelFromVision
427
+ value={ gridMode }
428
+ onChange={ ( value ) =>
429
+ setGridMode( value as GridMode )
430
+ }
431
+ options={ [
432
+ { label: 'Grid: off', value: 'off' },
433
+ { label: 'Grid: always on', value: 'on' },
434
+ {
435
+ label: 'Grid: interactive',
436
+ value: 'interactive',
437
+ },
438
+ ] }
439
+ />
440
+ </FlexItem>
441
+ <FlexItem isBlock />
442
+ <FlexItem>
443
+ <Button
444
+ __next40pxDefaultSize
445
+ variant="primary"
446
+ icon={ resetIcon }
447
+ disabled={ ! isDirty }
448
+ accessibleWhenDisabled
449
+ onClick={ handleReset }
450
+ >
451
+ Reset
452
+ </Button>
453
+ </FlexItem>
454
+ </Flex>
455
+ <div className="image-editor-story__sliders">
456
+ <RangeControl
457
+ __next40pxDefaultSize
458
+ __nextHasNoMarginBottom
459
+ label="Fine rotation"
460
+ min={ -MAX_ROTATION_OFFSET }
461
+ max={ MAX_ROTATION_OFFSET }
462
+ step={ 0.5 }
463
+ value={ fineOffset }
464
+ onChange={ handleRotationSlider }
465
+ />
466
+ <RangeControl
467
+ __next40pxDefaultSize
468
+ __nextHasNoMarginBottom
469
+ label="Zoom"
470
+ min={ MIN_ZOOM }
471
+ max={ MAX_ZOOM }
472
+ step={ 0.1 }
473
+ value={ state.zoom }
474
+ onChange={ handleZoomChange }
475
+ />
476
+ </div>
477
+ </div>
478
+
479
+ <div className="image-editor-story__resizable">
480
+ <Cropper
481
+ src={ src }
482
+ controller={ controller }
483
+ showGrid={ GRID_MODES[ gridMode ] }
484
+ showDimming
485
+ freeformCrop={ freeformCrop }
486
+ aspectRatio={ resolveAspectRatio(
487
+ aspectRatioValue,
488
+ state.image
489
+ ) }
490
+ />
491
+ </div>
492
+ <Flex justify="space-between" align="center">
493
+ <FlexItem>{ ! isCustom && IMAGE_CREDIT }</FlexItem>
494
+ { cropDimensions && (
495
+ <FlexItem
496
+ style={ {
497
+ fontSize: 11,
498
+ color: '#757575',
499
+ fontFamily: 'monospace',
500
+ } }
501
+ >
502
+ { Math.round( cropDimensions.width ) } ×{ ' ' }
503
+ { Math.round( cropDimensions.height ) } px
504
+ </FlexItem>
505
+ ) }
506
+ </Flex>
507
+
508
+ <div style={ { marginTop: 16 } }>
509
+ <strong>Current State:</strong>
510
+ <pre className="image-editor-story__state">
511
+ { JSON.stringify(
512
+ {
513
+ rotation: state.rotation,
514
+ zoom: state.zoom,
515
+ cropRect: state.cropRect,
516
+ pan: state.pan,
517
+ image: state.image
518
+ ? {
519
+ naturalWidth: state.image.naturalWidth,
520
+ naturalHeight:
521
+ state.image.naturalHeight,
522
+ }
523
+ : null,
524
+ },
525
+ null,
526
+ 2
527
+ ) }
528
+ </pre>
529
+ </div>
530
+ </div>
531
+ );
532
+ };
533
+
534
+ export const WithControls: Story = {
535
+ render: WithControlsComponent,
536
+ };
537
+
538
+ /**
539
+ * Debug visualization showing camera internals, export preview, and
540
+ * image info alongside the cropper.
541
+ *
542
+ * Displays the camera matrix, crop corner world-space coordinates,
543
+ * restriction values, source region, and a live export preview --
544
+ * updated live as you interact. Use this to debug containment issues
545
+ * or verify the camera and render paths agree.
546
+ */
547
+ const DebugComponent = () => {
548
+ const controller = useCropperState();
549
+ const {
550
+ state,
551
+ setRotation,
552
+ setZoom,
553
+ setFlip,
554
+ snapRotate90,
555
+ reset,
556
+ isDirty,
557
+ } = controller;
558
+
559
+ const [ freeformCrop, setFreeformCrop ] = useState( false );
560
+ const [ exportFormat, setExportFormat ] =
561
+ useState< string >( 'image/jpeg' );
562
+ const { src, isCustom, handleFileChange, resetToSample } =
563
+ useUploadableImage();
564
+ const fileInputRef = useRef< HTMLInputElement >( null );
565
+ const [ previewSrc, setPreviewSrc ] = useState< string | null >( null );
566
+ const imageRef = useRef< HTMLImageElement | null >( null );
567
+
568
+ // Reset crop/pan/zoom when the image changes.
569
+ const prevSrcRef = useRef( src );
570
+ useEffect( () => {
571
+ if ( prevSrcRef.current !== src ) {
572
+ prevSrcRef.current = src;
573
+ reset();
574
+ }
575
+ }, [ src, reset ] );
576
+
577
+ const [ containerSize, setContainerSize ] = useState( {
578
+ width: 0,
579
+ height: 0,
580
+ } );
581
+ const containerRef = useRef< HTMLDivElement >( null );
582
+
583
+ // Load the source image whenever the src changes (sample or uploaded).
584
+ useEffect( () => {
585
+ let cancelled = false;
586
+ loadImage( src ).then( ( img ) => {
587
+ if ( ! cancelled ) {
588
+ imageRef.current = img;
589
+ }
590
+ } );
591
+ return () => {
592
+ cancelled = true;
593
+ };
594
+ }, [ src ] );
595
+
596
+ // Re-render the preview whenever state changes.
597
+ useEffect( () => {
598
+ if ( ! imageRef.current || ! state.image ) {
599
+ return;
600
+ }
601
+ const canvas = renderToCanvas( imageRef.current, state );
602
+ setPreviewSrc( canvasToDataURL( canvas, 'image/jpeg', 0.85 ) );
603
+ }, [ state ] );
604
+
605
+ // Track container size for camera computations.
606
+ useEffect( () => {
607
+ const el = containerRef.current;
608
+ if ( ! el ) {
609
+ return;
610
+ }
611
+ const ro = new ResizeObserver( ( entries ) => {
612
+ const { width, height } = entries[ 0 ].contentRect;
613
+ setContainerSize( { width, height } );
614
+ } );
615
+ ro.observe( el );
616
+ return () => ro.disconnect();
617
+ }, [] );
618
+
619
+ const imageSize = state.image
620
+ ? { width: state.image.naturalWidth, height: state.image.naturalHeight }
621
+ : { width: 0, height: 0 };
622
+
623
+ const hasImage = imageSize.width > 0 && containerSize.width > 0;
624
+ const { elementSize, visualSize } = hasImage
625
+ ? getImageFit( containerSize, imageSize, state.rotation )
626
+ : {
627
+ elementSize: { width: 0, height: 0 },
628
+ visualSize: { width: 0, height: 0 },
629
+ };
630
+
631
+ // Camera and restriction.
632
+ const camera = hasImage
633
+ ? createCamera( state, containerSize, imageSize )
634
+ : null;
635
+ const baseCamera = hasImage
636
+ ? createCamera(
637
+ { ...state, pan: { x: 0, y: 0 }, zoom: 1 },
638
+ containerSize,
639
+ imageSize
640
+ )
641
+ : null;
642
+ const vb = baseCamera ? getVisibleBounds( baseCamera ) : null;
643
+
644
+ // Crop corners in world space (should be inside [0,1] when contained).
645
+ const cropWorldCorners =
646
+ camera && vb
647
+ ? [
648
+ screenToWorld( camera, {
649
+ x: vb.left + state.cropRect.x * vb.width,
650
+ y: vb.top + state.cropRect.y * vb.height,
651
+ } ),
652
+ screenToWorld( camera, {
653
+ x:
654
+ vb.left +
655
+ ( state.cropRect.x + state.cropRect.width ) *
656
+ vb.width,
657
+ y:
658
+ vb.top +
659
+ ( state.cropRect.y + state.cropRect.height ) *
660
+ vb.height,
661
+ } ),
662
+ ]
663
+ : null;
664
+
665
+ // Restriction result.
666
+ const restrictionResult = hasImage
667
+ ? restrictPanZoom( state, imageSize, state.cropRect )
668
+ : null;
669
+
670
+ // Source region.
671
+ const sourceRegion = hasImage ? getSourceRegion( state, imageSize ) : null;
672
+
673
+ // Is contained? All crop corners in [0,1].
674
+ const isContained =
675
+ cropWorldCorners &&
676
+ cropWorldCorners[ 0 ].x >= -0.001 &&
677
+ cropWorldCorners[ 0 ].y >= -0.001 &&
678
+ cropWorldCorners[ 1 ].x <= 1.001 &&
679
+ cropWorldCorners[ 1 ].y <= 1.001;
680
+
681
+ // `setRotation` is a raw state setter (absolute-angle assignment), so
682
+ // sliders need to flip the offset sign when a single flip is active
683
+ // to stay visually consistent. `snapRotate90` already handles this.
684
+ const baseAngle = Math.round( state.rotation / 90 ) * 90;
685
+ const singleFlip = state.flip.horizontal !== state.flip.vertical;
686
+ const visualDir = singleFlip ? -1 : 1;
687
+ const fineOffset = ( state.rotation - baseAngle ) * visualDir;
688
+
689
+ const handleRotateLeft = useCallback( () => {
690
+ snapRotate90( -1 );
691
+ }, [ snapRotate90 ] );
692
+
693
+ const handleRotateRight = useCallback( () => {
694
+ snapRotate90( 1 );
695
+ }, [ snapRotate90 ] );
696
+
697
+ const handleRotationSlider = useCallback(
698
+ ( value: number | undefined ) => {
699
+ if ( value === undefined ) {
700
+ return;
701
+ }
702
+ // Clamp strictly inside [-MAX, MAX). Reaching exactly ±MAX
703
+ // lands state.rotation on a 90° midpoint, which flips the
704
+ // computed baseAngle on the next render; subsequent slider
705
+ // events then spiral because they're added to the NEW base.
706
+ const EPS = 0.01;
707
+ const clamped = Math.max(
708
+ -MAX_ROTATION_OFFSET + EPS,
709
+ Math.min( MAX_ROTATION_OFFSET - EPS, value )
710
+ );
711
+ setRotation( baseAngle + clamped * visualDir );
712
+ },
713
+ [ baseAngle, setRotation, visualDir ]
714
+ );
715
+
716
+ const handleZoomChange = useCallback(
717
+ ( value: number | undefined ) => {
718
+ if ( value === undefined ) {
719
+ return;
720
+ }
721
+ setZoom( value );
722
+ },
723
+ [ setZoom ]
724
+ );
725
+
726
+ const handleFlipHorizontal = useCallback( () => {
727
+ setFlip( {
728
+ horizontal: ! state.flip.horizontal,
729
+ vertical: state.flip.vertical,
730
+ } );
731
+ }, [ state.flip, setFlip ] );
732
+
733
+ const handleFlipVertical = useCallback( () => {
734
+ setFlip( {
735
+ horizontal: state.flip.horizontal,
736
+ vertical: ! state.flip.vertical,
737
+ } );
738
+ }, [ state.flip, setFlip ] );
739
+
740
+ const openFilePicker = useCallback( () => {
741
+ fileInputRef.current?.click();
742
+ }, [] );
743
+
744
+ return (
745
+ <div>
746
+ <input
747
+ ref={ fileInputRef }
748
+ type="file"
749
+ accept="image/*"
750
+ onChange={ handleFileChange }
751
+ className="image-editor-story__hidden-file"
752
+ />
753
+ <div className="image-editor-story__toolbar">
754
+ <Flex align="center" gap={ 2 } wrap>
755
+ <FlexItem>
756
+ <Button
757
+ __next40pxDefaultSize
758
+ variant="primary"
759
+ icon={ cloudUpload }
760
+ onClick={ openFilePicker }
761
+ >
762
+ Upload
763
+ </Button>
764
+ </FlexItem>
765
+ { isCustom && (
766
+ <FlexItem>
767
+ <Button
768
+ __next40pxDefaultSize
769
+ variant="tertiary"
770
+ onClick={ resetToSample }
771
+ >
772
+ Use sample
773
+ </Button>
774
+ </FlexItem>
775
+ ) }
776
+ <FlexItem>
777
+ <Button
778
+ __next40pxDefaultSize
779
+ variant="secondary"
780
+ icon={ rotateLeft }
781
+ label="Rotate 90° counter-clockwise"
782
+ showTooltip
783
+ onClick={ handleRotateLeft }
784
+ />
785
+ </FlexItem>
786
+ <FlexItem>
787
+ <Button
788
+ __next40pxDefaultSize
789
+ variant="secondary"
790
+ icon={ rotateRight }
791
+ label="Rotate 90° clockwise"
792
+ showTooltip
793
+ onClick={ handleRotateRight }
794
+ />
795
+ </FlexItem>
796
+ <FlexItem>
797
+ <Button
798
+ __next40pxDefaultSize
799
+ variant="secondary"
800
+ icon={ flipHorizontal }
801
+ label="Flip horizontal"
802
+ showTooltip
803
+ isPressed={ state.flip.horizontal }
804
+ onClick={ handleFlipHorizontal }
805
+ />
806
+ </FlexItem>
807
+ <FlexItem>
808
+ <Button
809
+ __next40pxDefaultSize
810
+ variant="secondary"
811
+ icon={ flipVertical }
812
+ label="Flip vertical"
813
+ showTooltip
814
+ isPressed={ state.flip.vertical }
815
+ onClick={ handleFlipVertical }
816
+ />
817
+ </FlexItem>
818
+ <FlexItem>
819
+ <ToggleControl
820
+ __nextHasNoMarginBottom
821
+ label="Freeform"
822
+ checked={ freeformCrop }
823
+ onChange={ setFreeformCrop }
824
+ />
825
+ </FlexItem>
826
+ <FlexItem isBlock />
827
+ <FlexItem>
828
+ <Button
829
+ __next40pxDefaultSize
830
+ variant="primary"
831
+ icon={ resetIcon }
832
+ disabled={ ! isDirty }
833
+ accessibleWhenDisabled
834
+ onClick={ () => reset() }
835
+ >
836
+ Reset
837
+ </Button>
838
+ </FlexItem>
839
+ <FlexItem>
840
+ <SelectControl
841
+ __next40pxDefaultSize
842
+ __nextHasNoMarginBottom
843
+ label="Format"
844
+ hideLabelFromVision
845
+ value={ exportFormat as 'image/jpeg' }
846
+ onChange={ ( value ) =>
847
+ setExportFormat( value as string )
848
+ }
849
+ options={ [
850
+ { label: 'JPEG', value: 'image/jpeg' },
851
+ { label: 'PNG', value: 'image/png' },
852
+ { label: 'WebP', value: 'image/webp' },
853
+ ] }
854
+ />
855
+ </FlexItem>
856
+ <FlexItem>
857
+ <Button
858
+ __next40pxDefaultSize
859
+ variant="primary"
860
+ icon={ downloadIcon }
861
+ onClick={ () =>
862
+ downloadCroppedImage(
863
+ src,
864
+ state,
865
+ 'cropped',
866
+ exportFormat,
867
+ 0.9
868
+ )
869
+ }
870
+ >
871
+ Download
872
+ </Button>
873
+ </FlexItem>
874
+ </Flex>
875
+ <div className="image-editor-story__sliders">
876
+ <RangeControl
877
+ __next40pxDefaultSize
878
+ __nextHasNoMarginBottom
879
+ label="Fine rotation"
880
+ min={ -MAX_ROTATION_OFFSET }
881
+ max={ MAX_ROTATION_OFFSET }
882
+ step={ 0.5 }
883
+ value={ fineOffset }
884
+ onChange={ handleRotationSlider }
885
+ />
886
+ <RangeControl
887
+ __next40pxDefaultSize
888
+ __nextHasNoMarginBottom
889
+ label="Zoom"
890
+ min={ MIN_ZOOM }
891
+ max={ MAX_ZOOM }
892
+ step={ 0.1 }
893
+ value={ state.zoom }
894
+ onChange={ handleZoomChange }
895
+ />
896
+ </div>
897
+ </div>
898
+
899
+ <div
900
+ style={ { display: 'flex', gap: 16, alignItems: 'flex-start' } }
901
+ >
902
+ <div
903
+ ref={ containerRef }
904
+ style={ { flex: '1 1 60%', minWidth: 0 } }
905
+ >
906
+ <div className="image-editor-story__container">
907
+ <Cropper
908
+ src={ src }
909
+ controller={ controller }
910
+ showGrid
911
+ showDimming
912
+ freeformCrop={ freeformCrop }
913
+ />
914
+ </div>
915
+ { ! isCustom && IMAGE_CREDIT }
916
+ </div>
917
+
918
+ <div
919
+ style={ {
920
+ flex: '0 0 320px',
921
+ fontSize: 11,
922
+ fontFamily: 'monospace',
923
+ lineHeight: 1.5,
924
+ overflow: 'auto',
925
+ maxHeight: 500,
926
+ } }
927
+ >
928
+ <div className="image-editor-story__export-preview">
929
+ { previewSrc ? (
930
+ <img
931
+ className="image-editor-story__export-image"
932
+ src={ previewSrc }
933
+ alt="Crop preview"
934
+ />
935
+ ) : (
936
+ <p>Loading...</p>
937
+ ) }
938
+ </div>
939
+ <strong>Containment</strong>
940
+ <div
941
+ style={ {
942
+ padding: '2px 6px',
943
+ background: isContained ? '#d4edda' : '#f8d7da',
944
+ borderRadius: 3,
945
+ marginBottom: 8,
946
+ display: 'inline-block',
947
+ } }
948
+ >
949
+ { isContained ? 'COVERED' : 'NOT COVERED' }
950
+ </div>
951
+
952
+ { state.image && (
953
+ <div>
954
+ <strong>Image info</strong>
955
+ <pre style={ { margin: '4px 0' } }>
956
+ { `original: ${ state.image.naturalWidth }×${
957
+ state.image.naturalHeight
958
+ }
959
+ aspect ratio: ${ (
960
+ state.image.naturalWidth /
961
+ state.image.naturalHeight
962
+ ).toFixed( 2 ) }` }
963
+ </pre>
964
+ </div>
965
+ ) }
966
+
967
+ <div>
968
+ <strong>State</strong>
969
+ <pre style={ { margin: '4px 0' } }>
970
+ { `zoom: ${ state.zoom.toFixed( 3 ) }
971
+ rotation: ${ state.rotation.toFixed( 1 ) }°
972
+ pan: (${ state.pan.x.toFixed( 4 ) }, ${ state.pan.y.toFixed( 4 ) })
973
+ cropRect: (${ state.cropRect.x.toFixed( 3 ) }, ${ state.cropRect.y.toFixed(
974
+ 3
975
+ ) }) ${ state.cropRect.width.toFixed(
976
+ 3
977
+ ) }×${ state.cropRect.height.toFixed( 3 ) }
978
+ flip: h=${ state.flip.horizontal } v=${ state.flip.vertical }` }
979
+ </pre>
980
+ </div>
981
+
982
+ <div>
983
+ <strong>Sizes</strong>
984
+ <pre style={ { margin: '4px 0' } }>
985
+ { `container: ${ containerSize.width }×${
986
+ containerSize.height
987
+ }
988
+ element: ${ elementSize.width.toFixed( 0 ) }×${ elementSize.height.toFixed(
989
+ 0
990
+ ) }
991
+ visual: ${ visualSize.width.toFixed( 0 ) }×${ visualSize.height.toFixed(
992
+ 0
993
+ ) }` }
994
+ </pre>
995
+ </div>
996
+
997
+ { camera && (
998
+ <div>
999
+ <strong>Camera matrix</strong>
1000
+ <pre style={ { margin: '4px 0' } }>
1001
+ { `[${ Array.from( camera )
1002
+ .map( ( v ) => v.toFixed( 2 ) )
1003
+ .join( ', ' ) }]` }
1004
+ </pre>
1005
+ </div>
1006
+ ) }
1007
+
1008
+ { cropWorldCorners && (
1009
+ <div>
1010
+ <strong>Crop corners (world space)</strong>
1011
+ <pre style={ { margin: '4px 0' } }>
1012
+ { `TL: (${ cropWorldCorners[ 0 ].x.toFixed(
1013
+ 4
1014
+ ) }, ${ cropWorldCorners[ 0 ].y.toFixed( 4 ) })
1015
+ BR: (${ cropWorldCorners[ 1 ].x.toFixed(
1016
+ 4
1017
+ ) }, ${ cropWorldCorners[ 1 ].y.toFixed(
1018
+ 4
1019
+ ) })` }
1020
+ </pre>
1021
+ <span style={ { fontSize: 10, color: '#666' } }>
1022
+ Should be within [0,1] when contained
1023
+ </span>
1024
+ </div>
1025
+ ) }
1026
+
1027
+ { restrictionResult && (
1028
+ <div>
1029
+ <strong>Restriction result</strong>
1030
+ <pre style={ { margin: '4px 0' } }>
1031
+ { `zoom: ${ restrictionResult.zoom.toFixed(
1032
+ 3
1033
+ ) }
1034
+ pan: (${ restrictionResult.pan.x.toFixed(
1035
+ 4
1036
+ ) }, ${ restrictionResult.pan.y.toFixed(
1037
+ 4
1038
+ ) })` }
1039
+ </pre>
1040
+ </div>
1041
+ ) }
1042
+
1043
+ { sourceRegion && (
1044
+ <div>
1045
+ <strong>Source region (px)</strong>
1046
+ <pre style={ { margin: '4px 0' } }>
1047
+ { `x: ${ sourceRegion.x.toFixed(
1048
+ 0
1049
+ ) } y: ${ sourceRegion.y.toFixed( 0 ) }
1050
+ w: ${ sourceRegion.width.toFixed( 0 ) } h: ${ sourceRegion.height.toFixed(
1051
+ 0
1052
+ ) }` }
1053
+ </pre>
1054
+ </div>
1055
+ ) }
1056
+
1057
+ { sourceRegion &&
1058
+ sourceRegion.width > 0 &&
1059
+ sourceRegion.height > 0 && (
1060
+ <div>
1061
+ <strong>Crop output</strong>
1062
+ <pre style={ { margin: '4px 0' } }>
1063
+ { `dimensions: ${ Math.round(
1064
+ sourceRegion.width
1065
+ ) }×${ Math.round( sourceRegion.height ) }
1066
+ aspect ratio: ${ ( sourceRegion.width / sourceRegion.height ).toFixed( 2 ) }
1067
+ % of original: ${ (
1068
+ ( ( sourceRegion.width *
1069
+ sourceRegion.height ) /
1070
+ ( imageSize.width *
1071
+ imageSize.height ) ) *
1072
+ 100
1073
+ ).toFixed( 1 ) }%` }
1074
+ </pre>
1075
+ </div>
1076
+ ) }
1077
+ </div>
1078
+ </div>
1079
+ </div>
1080
+ );
1081
+ };
1082
+
1083
+ export const Debug: Story = {
1084
+ render: DebugComponent,
1085
+ };