@wordpress/media-editor 0.22.0 → 0.24.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 (391) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/build/components/media-editor/index.cjs +229 -126
  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 +41 -11
  7. package/build/components/media-editor/use-save-media-editor.cjs.map +1 -1
  8. package/build/components/media-editor-canvas/index.cjs +15 -18
  9. package/build/components/media-editor-canvas/index.cjs.map +1 -1
  10. package/build/components/media-editor-crop-panel/index.cjs +26 -8
  11. package/build/components/media-editor-crop-panel/index.cjs.map +1 -1
  12. package/build/components/media-editor-fine-rotation/index.cjs +13 -12
  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 +33 -33
  17. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +1 -1
  18. package/build/components/media-editor-modal/index.cjs +10 -7
  19. package/build/components/media-editor-modal/index.cjs.map +1 -1
  20. package/build/components/media-editor-panel/error-boundary.cjs +68 -0
  21. package/build/components/media-editor-panel/error-boundary.cjs.map +1 -0
  22. package/build/components/media-editor-panel/index.cjs +158 -0
  23. package/build/components/media-editor-panel/index.cjs.map +1 -0
  24. package/build/components/media-form/index.cjs +7 -1
  25. package/build/components/media-form/index.cjs.map +1 -1
  26. package/build/components/media-preview/index.cjs +143 -37
  27. package/build/components/media-preview/index.cjs.map +1 -1
  28. package/build/components/rotation-ruler/index.cjs +9 -7
  29. package/build/components/rotation-ruler/index.cjs.map +1 -1
  30. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -1
  31. package/build/hooks/use-crop-gesture-handlers.cjs.map +1 -1
  32. package/build/index.cjs +10 -8
  33. package/build/private-apis.cjs.map +1 -1
  34. package/build/state/aspect-ratio-presets.cjs +12 -6
  35. package/build/state/aspect-ratio-presets.cjs.map +1 -1
  36. package/build/state/composite-reducer.cjs +41 -74
  37. package/build/state/composite-reducer.cjs.map +1 -1
  38. package/build/state/index.cjs +0 -39
  39. package/build/state/types.cjs +1 -2
  40. package/build/state/types.cjs.map +1 -1
  41. package/build/state/use-media-editor-state.cjs +70 -106
  42. package/build/state/use-media-editor-state.cjs.map +1 -1
  43. package/build/store/index.cjs +11 -9
  44. package/build/store/index.cjs.map +1 -1
  45. package/build/utils/get-media-type.cjs +8 -0
  46. package/build/utils/get-media-type.cjs.map +1 -1
  47. package/build/utils/index.cjs +6 -0
  48. package/build-module/components/media-editor/index.mjs +217 -117
  49. package/build-module/components/media-editor/index.mjs.map +1 -1
  50. package/build-module/components/media-editor/use-crop-options.mjs +4 -5
  51. package/build-module/components/media-editor/use-crop-options.mjs.map +1 -1
  52. package/build-module/components/media-editor/use-save-media-editor.mjs +32 -4
  53. package/build-module/components/media-editor/use-save-media-editor.mjs.map +1 -1
  54. package/build-module/components/media-editor-canvas/index.mjs +8 -13
  55. package/build-module/components/media-editor-canvas/index.mjs.map +1 -1
  56. package/build-module/components/media-editor-crop-panel/index.mjs +17 -1
  57. package/build-module/components/media-editor-crop-panel/index.mjs.map +1 -1
  58. package/build-module/components/media-editor-fine-rotation/index.mjs +4 -5
  59. package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -1
  60. package/build-module/components/media-editor-image-controls/index.mjs +5 -7
  61. package/build-module/components/media-editor-image-controls/index.mjs.map +1 -1
  62. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +33 -33
  63. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +1 -1
  64. package/build-module/components/media-editor-modal/index.mjs +1 -0
  65. package/build-module/components/media-editor-modal/index.mjs.map +1 -1
  66. package/build-module/components/media-editor-panel/error-boundary.mjs +63 -0
  67. package/build-module/components/media-editor-panel/error-boundary.mjs.map +1 -0
  68. package/build-module/components/media-editor-panel/index.mjs +123 -0
  69. package/build-module/components/media-editor-panel/index.mjs.map +1 -0
  70. package/build-module/components/media-form/index.mjs +7 -1
  71. package/build-module/components/media-form/index.mjs.map +1 -1
  72. package/build-module/components/media-preview/index.mjs +123 -41
  73. package/build-module/components/media-preview/index.mjs.map +1 -1
  74. package/build-module/state/aspect-ratio-presets.mjs +12 -7
  75. package/build-module/state/aspect-ratio-presets.mjs.map +1 -1
  76. package/build-module/state/composite-reducer.mjs +32 -67
  77. package/build-module/state/composite-reducer.mjs.map +1 -1
  78. package/build-module/state/index.mjs +2 -5
  79. package/build-module/state/types.mjs +1 -2
  80. package/build-module/state/types.mjs.map +1 -1
  81. package/build-module/state/use-media-editor-state.mjs +70 -105
  82. package/build-module/state/use-media-editor-state.mjs.map +1 -1
  83. package/build-module/utils/get-media-type.mjs +8 -1
  84. package/build-module/utils/get-media-type.mjs.map +1 -1
  85. package/build-module/utils/index.mjs +1 -1
  86. package/build-style/style-rtl.css +122 -87
  87. package/build-style/style.css +121 -87
  88. package/build-types/components/media-editor/index.d.ts +25 -3
  89. package/build-types/components/media-editor/index.d.ts.map +1 -1
  90. package/build-types/components/media-editor/use-crop-options.d.ts +2 -3
  91. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
  92. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
  93. package/build-types/components/media-editor-canvas/index.d.ts +3 -3
  94. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  95. package/build-types/components/media-editor-crop-panel/index.d.ts +1 -1
  96. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  97. package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
  98. package/build-types/components/media-editor-image-controls/index.d.ts +1 -1
  99. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  100. package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -1
  101. package/build-types/components/media-editor-modal/index.d.ts +1 -1
  102. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  103. package/build-types/components/media-editor-panel/error-boundary.d.ts +23 -0
  104. package/build-types/components/media-editor-panel/error-boundary.d.ts.map +1 -0
  105. package/build-types/components/media-editor-panel/index.d.ts +46 -0
  106. package/build-types/components/media-editor-panel/index.d.ts.map +1 -0
  107. package/build-types/components/media-form/index.d.ts.map +1 -1
  108. package/build-types/components/media-preview/index.d.ts.map +1 -1
  109. package/build-types/state/aspect-ratio-presets.d.ts +20 -3
  110. package/build-types/state/aspect-ratio-presets.d.ts.map +1 -1
  111. package/build-types/state/composite-reducer.d.ts +17 -19
  112. package/build-types/state/composite-reducer.d.ts.map +1 -1
  113. package/build-types/state/index.d.ts +2 -5
  114. package/build-types/state/index.d.ts.map +1 -1
  115. package/build-types/state/types.d.ts +20 -64
  116. package/build-types/state/types.d.ts.map +1 -1
  117. package/build-types/state/use-media-editor-state.d.ts +40 -54
  118. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  119. package/build-types/utils/get-media-type.d.ts +3 -1
  120. package/build-types/utils/get-media-type.d.ts.map +1 -1
  121. package/build-types/utils/index.d.ts +1 -1
  122. package/build-types/utils/index.d.ts.map +1 -1
  123. package/package.json +27 -24
  124. package/src/components/media-editor/index.tsx +372 -142
  125. package/src/components/media-editor/style.scss +21 -1
  126. package/src/components/media-editor/use-crop-options.ts +7 -11
  127. package/src/components/media-editor/use-save-media-editor.ts +48 -6
  128. package/src/components/media-editor-canvas/index.tsx +15 -26
  129. package/src/components/media-editor-canvas/style.scss +6 -6
  130. package/src/components/media-editor-crop-panel/index.tsx +38 -2
  131. package/src/components/media-editor-crop-panel/style.scss +21 -0
  132. package/src/components/media-editor-fine-rotation/index.tsx +6 -6
  133. package/src/components/media-editor-image-controls/index.tsx +11 -9
  134. package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +56 -63
  135. package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +1 -1
  136. package/src/components/media-editor-modal/index.tsx +4 -1
  137. package/src/components/media-editor-panel/error-boundary.tsx +88 -0
  138. package/src/components/media-editor-panel/index.tsx +211 -0
  139. package/src/components/media-form/index.tsx +7 -1
  140. package/src/components/media-preview/index.tsx +195 -37
  141. package/src/components/media-preview/style.scss +11 -32
  142. package/src/components/rotation-ruler/style.scss +3 -3
  143. package/src/state/README.md +34 -0
  144. package/src/state/aspect-ratio-presets.ts +22 -3
  145. package/src/state/composite-reducer.ts +42 -80
  146. package/src/state/index.ts +2 -18
  147. package/src/state/types.ts +18 -68
  148. package/src/state/use-media-editor-state.ts +162 -242
  149. package/src/style.scss +2 -1
  150. package/src/utils/get-media-type.ts +9 -1
  151. package/src/utils/index.ts +1 -1
  152. package/build/components/media-editor-modal/build-modifiers.cjs +0 -110
  153. package/build/components/media-editor-modal/build-modifiers.cjs.map +0 -1
  154. package/build/image-editor/core/camera.cjs +0 -277
  155. package/build/image-editor/core/camera.cjs.map +0 -1
  156. package/build/image-editor/core/constants.cjs +0 -133
  157. package/build/image-editor/core/constants.cjs.map +0 -1
  158. package/build/image-editor/core/containment.cjs +0 -289
  159. package/build/image-editor/core/containment.cjs.map +0 -1
  160. package/build/image-editor/core/crop-rect.cjs +0 -37
  161. package/build/image-editor/core/crop-rect.cjs.map +0 -1
  162. package/build/image-editor/core/export/canvas-renderer.cjs +0 -181
  163. package/build/image-editor/core/export/canvas-renderer.cjs.map +0 -1
  164. package/build/image-editor/core/index.cjs +0 -89
  165. package/build/image-editor/core/interaction-controller.cjs +0 -591
  166. package/build/image-editor/core/interaction-controller.cjs.map +0 -1
  167. package/build/image-editor/core/math/rotation.cjs +0 -32
  168. package/build/image-editor/core/math/rotation.cjs.map +0 -1
  169. package/build/image-editor/core/math/sanitize.cjs +0 -136
  170. package/build/image-editor/core/math/sanitize.cjs.map +0 -1
  171. package/build/image-editor/core/setter-helpers.cjs +0 -61
  172. package/build/image-editor/core/setter-helpers.cjs.map +0 -1
  173. package/build/image-editor/core/source-region.cjs +0 -236
  174. package/build/image-editor/core/source-region.cjs.map +0 -1
  175. package/build/image-editor/core/state.cjs +0 -296
  176. package/build/image-editor/core/state.cjs.map +0 -1
  177. package/build/image-editor/core/stencil-math.cjs +0 -203
  178. package/build/image-editor/core/stencil-math.cjs.map +0 -1
  179. package/build/image-editor/core/transform-style.cjs +0 -43
  180. package/build/image-editor/core/transform-style.cjs.map +0 -1
  181. package/build/image-editor/core/transforms/pipeline.cjs +0 -41
  182. package/build/image-editor/core/transforms/pipeline.cjs.map +0 -1
  183. package/build/image-editor/core/types.cjs +0 -5
  184. package/build/image-editor/core/viewport-state.cjs +0 -36
  185. package/build/image-editor/core/viewport-state.cjs.map +0 -1
  186. package/build/image-editor/index.cjs +0 -68
  187. package/build/image-editor/react/components/cropper-provider.cjs +0 -49
  188. package/build/image-editor/react/components/cropper-provider.cjs.map +0 -1
  189. package/build/image-editor/react/components/cropper.cjs +0 -595
  190. package/build/image-editor/react/components/cropper.cjs.map +0 -1
  191. package/build/image-editor/react/components/index.cjs +0 -25
  192. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +0 -147
  193. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +0 -1
  194. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +0 -31
  195. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +0 -1
  196. package/build/image-editor/react/components/overlays/grid-overlay.cjs +0 -61
  197. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +0 -1
  198. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +0 -367
  199. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +0 -1
  200. package/build/image-editor/react/components/viewport-provider.cjs +0 -26
  201. package/build/image-editor/react/components/viewport-provider.cjs.map +0 -1
  202. package/build/image-editor/react/hooks/build-cropper-setters.cjs +0 -80
  203. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +0 -1
  204. package/build/image-editor/react/hooks/index.cjs +0 -19
  205. package/build/image-editor/react/hooks/use-aria-announcer.cjs +0 -104
  206. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +0 -1
  207. package/build/image-editor/react/hooks/use-cropper-controller.cjs +0 -80
  208. package/build/image-editor/react/hooks/use-cropper-controller.cjs.map +0 -1
  209. package/build/image-editor/react/hooks/use-interaction.cjs +0 -187
  210. package/build/image-editor/react/hooks/use-interaction.cjs.map +0 -1
  211. package/build/image-editor/react/hooks/use-transform-style.cjs +0 -24
  212. package/build/image-editor/react/hooks/use-transform-style.cjs.map +0 -1
  213. package/build/image-editor/react/hooks/use-viewport-state.cjs +0 -40
  214. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +0 -1
  215. package/build/image-editor/react/visually-hidden-style.cjs +0 -24
  216. package/build/image-editor/react/visually-hidden-style.cjs.map +0 -1
  217. package/build-module/components/media-editor-modal/build-modifiers.mjs +0 -105
  218. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +0 -1
  219. package/build-module/image-editor/core/camera.mjs +0 -265
  220. package/build-module/image-editor/core/camera.mjs.map +0 -1
  221. package/build-module/image-editor/core/constants.mjs +0 -115
  222. package/build-module/image-editor/core/constants.mjs.map +0 -1
  223. package/build-module/image-editor/core/containment.mjs +0 -281
  224. package/build-module/image-editor/core/containment.mjs.map +0 -1
  225. package/build-module/image-editor/core/crop-rect.mjs +0 -32
  226. package/build-module/image-editor/core/crop-rect.mjs.map +0 -1
  227. package/build-module/image-editor/core/export/canvas-renderer.mjs +0 -170
  228. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +0 -1
  229. package/build-module/image-editor/core/index.mjs +0 -7
  230. package/build-module/image-editor/core/interaction-controller.mjs +0 -586
  231. package/build-module/image-editor/core/interaction-controller.mjs.map +0 -1
  232. package/build-module/image-editor/core/math/rotation.mjs +0 -26
  233. package/build-module/image-editor/core/math/rotation.mjs.map +0 -1
  234. package/build-module/image-editor/core/math/sanitize.mjs +0 -127
  235. package/build-module/image-editor/core/math/sanitize.mjs.map +0 -1
  236. package/build-module/image-editor/core/setter-helpers.mjs +0 -56
  237. package/build-module/image-editor/core/setter-helpers.mjs.map +0 -1
  238. package/build-module/image-editor/core/source-region.mjs +0 -228
  239. package/build-module/image-editor/core/source-region.mjs.map +0 -1
  240. package/build-module/image-editor/core/state.mjs +0 -288
  241. package/build-module/image-editor/core/state.mjs.map +0 -1
  242. package/build-module/image-editor/core/stencil-math.mjs +0 -194
  243. package/build-module/image-editor/core/stencil-math.mjs.map +0 -1
  244. package/build-module/image-editor/core/transform-style.mjs +0 -38
  245. package/build-module/image-editor/core/transform-style.mjs.map +0 -1
  246. package/build-module/image-editor/core/transforms/pipeline.mjs +0 -35
  247. package/build-module/image-editor/core/transforms/pipeline.mjs.map +0 -1
  248. package/build-module/image-editor/core/types.mjs +0 -0
  249. package/build-module/image-editor/core/viewport-state.mjs +0 -30
  250. package/build-module/image-editor/core/viewport-state.mjs.map +0 -1
  251. package/build-module/image-editor/index.mjs +0 -4
  252. package/build-module/image-editor/react/components/cropper-provider.mjs +0 -43
  253. package/build-module/image-editor/react/components/cropper-provider.mjs.map +0 -1
  254. package/build-module/image-editor/react/components/cropper.mjs +0 -568
  255. package/build-module/image-editor/react/components/cropper.mjs.map +0 -1
  256. package/build-module/image-editor/react/components/index.mjs +0 -3
  257. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +0 -142
  258. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +0 -1
  259. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +0 -26
  260. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +0 -1
  261. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +0 -56
  262. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +0 -1
  263. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +0 -362
  264. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +0 -1
  265. package/build-module/image-editor/react/components/viewport-provider.mjs +0 -20
  266. package/build-module/image-editor/react/components/viewport-provider.mjs.map +0 -1
  267. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +0 -75
  268. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +0 -1
  269. package/build-module/image-editor/react/hooks/index.mjs +0 -3
  270. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +0 -99
  271. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +0 -1
  272. package/build-module/image-editor/react/hooks/use-cropper-controller.mjs +0 -75
  273. package/build-module/image-editor/react/hooks/use-cropper-controller.mjs.map +0 -1
  274. package/build-module/image-editor/react/hooks/use-interaction.mjs +0 -182
  275. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +0 -1
  276. package/build-module/image-editor/react/hooks/use-transform-style.mjs +0 -19
  277. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +0 -1
  278. package/build-module/image-editor/react/hooks/use-viewport-state.mjs +0 -35
  279. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +0 -1
  280. package/build-module/image-editor/react/visually-hidden-style.mjs +0 -19
  281. package/build-module/image-editor/react/visually-hidden-style.mjs.map +0 -1
  282. package/build-types/components/media-editor-modal/build-modifiers.d.ts +0 -81
  283. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +0 -1
  284. package/build-types/image-editor/core/camera.d.ts +0 -128
  285. package/build-types/image-editor/core/camera.d.ts.map +0 -1
  286. package/build-types/image-editor/core/constants.d.ts +0 -98
  287. package/build-types/image-editor/core/constants.d.ts.map +0 -1
  288. package/build-types/image-editor/core/containment.d.ts +0 -72
  289. package/build-types/image-editor/core/containment.d.ts.map +0 -1
  290. package/build-types/image-editor/core/crop-rect.d.ts +0 -16
  291. package/build-types/image-editor/core/crop-rect.d.ts.map +0 -1
  292. package/build-types/image-editor/core/export/canvas-renderer.d.ts +0 -103
  293. package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +0 -1
  294. package/build-types/image-editor/core/index.d.ts +0 -10
  295. package/build-types/image-editor/core/index.d.ts.map +0 -1
  296. package/build-types/image-editor/core/interaction-controller.d.ts +0 -191
  297. package/build-types/image-editor/core/interaction-controller.d.ts.map +0 -1
  298. package/build-types/image-editor/core/math/rotation.d.ts +0 -18
  299. package/build-types/image-editor/core/math/rotation.d.ts.map +0 -1
  300. package/build-types/image-editor/core/math/sanitize.d.ts +0 -67
  301. package/build-types/image-editor/core/math/sanitize.d.ts.map +0 -1
  302. package/build-types/image-editor/core/setter-helpers.d.ts +0 -21
  303. package/build-types/image-editor/core/setter-helpers.d.ts.map +0 -1
  304. package/build-types/image-editor/core/source-region.d.ts +0 -94
  305. package/build-types/image-editor/core/source-region.d.ts.map +0 -1
  306. package/build-types/image-editor/core/state.d.ts +0 -51
  307. package/build-types/image-editor/core/state.d.ts.map +0 -1
  308. package/build-types/image-editor/core/stencil-math.d.ts +0 -102
  309. package/build-types/image-editor/core/stencil-math.d.ts.map +0 -1
  310. package/build-types/image-editor/core/transform-style.d.ts +0 -20
  311. package/build-types/image-editor/core/transform-style.d.ts.map +0 -1
  312. package/build-types/image-editor/core/transforms/pipeline.d.ts +0 -24
  313. package/build-types/image-editor/core/transforms/pipeline.d.ts.map +0 -1
  314. package/build-types/image-editor/core/types.d.ts +0 -269
  315. package/build-types/image-editor/core/types.d.ts.map +0 -1
  316. package/build-types/image-editor/core/viewport-state.d.ts +0 -4
  317. package/build-types/image-editor/core/viewport-state.d.ts.map +0 -1
  318. package/build-types/image-editor/index.d.ts +0 -17
  319. package/build-types/image-editor/index.d.ts.map +0 -1
  320. package/build-types/image-editor/react/components/cropper-provider.d.ts +0 -38
  321. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +0 -1
  322. package/build-types/image-editor/react/components/cropper.d.ts +0 -54
  323. package/build-types/image-editor/react/components/cropper.d.ts.map +0 -1
  324. package/build-types/image-editor/react/components/index.d.ts +0 -4
  325. package/build-types/image-editor/react/components/index.d.ts.map +0 -1
  326. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +0 -33
  327. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +0 -1
  328. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +0 -17
  329. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +0 -1
  330. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +0 -27
  331. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +0 -1
  332. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +0 -33
  333. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +0 -1
  334. package/build-types/image-editor/react/components/viewport-provider.d.ts +0 -6
  335. package/build-types/image-editor/react/components/viewport-provider.d.ts.map +0 -1
  336. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +0 -44
  337. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +0 -1
  338. package/build-types/image-editor/react/hooks/index.d.ts +0 -5
  339. package/build-types/image-editor/react/hooks/index.d.ts.map +0 -1
  340. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +0 -14
  341. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +0 -1
  342. package/build-types/image-editor/react/hooks/use-cropper-controller.d.ts +0 -55
  343. package/build-types/image-editor/react/hooks/use-cropper-controller.d.ts.map +0 -1
  344. package/build-types/image-editor/react/hooks/use-interaction.d.ts +0 -65
  345. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +0 -1
  346. package/build-types/image-editor/react/hooks/use-transform-style.d.ts +0 -13
  347. package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +0 -1
  348. package/build-types/image-editor/react/hooks/use-viewport-state.d.ts +0 -16
  349. package/build-types/image-editor/react/hooks/use-viewport-state.d.ts.map +0 -1
  350. package/build-types/image-editor/react/visually-hidden-style.d.ts +0 -6
  351. package/build-types/image-editor/react/visually-hidden-style.d.ts.map +0 -1
  352. package/src/components/media-editor-modal/build-modifiers.ts +0 -167
  353. package/src/image-editor/README.md +0 -61
  354. package/src/image-editor/core/camera.ts +0 -429
  355. package/src/image-editor/core/constants.ts +0 -137
  356. package/src/image-editor/core/containment.ts +0 -469
  357. package/src/image-editor/core/crop-rect.ts +0 -42
  358. package/src/image-editor/core/export/canvas-renderer.ts +0 -250
  359. package/src/image-editor/core/index.ts +0 -43
  360. package/src/image-editor/core/interaction-controller.ts +0 -976
  361. package/src/image-editor/core/math/rotation.ts +0 -26
  362. package/src/image-editor/core/math/sanitize.ts +0 -155
  363. package/src/image-editor/core/setter-helpers.ts +0 -63
  364. package/src/image-editor/core/source-region.ts +0 -367
  365. package/src/image-editor/core/state.ts +0 -484
  366. package/src/image-editor/core/stencil-math.ts +0 -379
  367. package/src/image-editor/core/transform-style.ts +0 -56
  368. package/src/image-editor/core/transforms/pipeline.ts +0 -40
  369. package/src/image-editor/core/types.ts +0 -230
  370. package/src/image-editor/core/viewport-state.ts +0 -30
  371. package/src/image-editor/docs/architecture.md +0 -63
  372. package/src/image-editor/docs/recipes.md +0 -184
  373. package/src/image-editor/index.ts +0 -43
  374. package/src/image-editor/react/components/cropper-provider.tsx +0 -70
  375. package/src/image-editor/react/components/cropper.scss +0 -259
  376. package/src/image-editor/react/components/cropper.tsx +0 -1108
  377. package/src/image-editor/react/components/index.ts +0 -3
  378. package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +0 -262
  379. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +0 -55
  380. package/src/image-editor/react/components/overlays/grid-overlay.tsx +0 -77
  381. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +0 -623
  382. package/src/image-editor/react/components/viewport-provider.tsx +0 -32
  383. package/src/image-editor/react/hooks/build-cropper-setters.ts +0 -102
  384. package/src/image-editor/react/hooks/index.ts +0 -4
  385. package/src/image-editor/react/hooks/use-aria-announcer.ts +0 -193
  386. package/src/image-editor/react/hooks/use-cropper-controller.ts +0 -197
  387. package/src/image-editor/react/hooks/use-interaction.ts +0 -294
  388. package/src/image-editor/react/hooks/use-transform-style.ts +0 -23
  389. package/src/image-editor/react/hooks/use-viewport-state.ts +0 -41
  390. package/src/image-editor/react/visually-hidden-style.ts +0 -15
  391. package/src/image-editor/style.scss +0 -1
@@ -1,167 +0,0 @@
1
- import type { CropperController } from '../../image-editor';
2
- import { getRotatedBBox } from '../../image-editor/core/camera';
3
-
4
- /**
5
- * A single modifier in the Core REST `/edit` payload. Order is
6
- * significant — the server applies modifiers sequentially (see
7
- * `WP_REST_Attachments_Controller::edit_media_item`).
8
- *
9
- * Shape/units:
10
- * - `flip`: booleans per axis. Applied first in this module's output.
11
- * - `rotate`: `angle` in degrees, clockwise-positive (matches the
12
- * cropper's own convention; Core negates it internally for
13
- * `WP_Image_Editor::rotate`, which is counterclockwise-positive).
14
- * - `crop`: percentages (0–100) of the image's dimensions **at the
15
- * moment the crop is applied**. Because modifiers are applied in
16
- * order, after a preceding `rotate` the server's
17
- * `WP_Image_Editor::get_size()` reports the full post-rotate AABB,
18
- * so the percentages are of that AABB. Origin is top-left.
19
- */
20
- export type Modifier =
21
- | {
22
- type: 'flip';
23
- args: { flip: { horizontal: boolean; vertical: boolean } };
24
- }
25
- | { type: 'rotate'; args: { angle: number } }
26
- | {
27
- type: 'crop';
28
- args: { left: number; top: number; width: number; height: number };
29
- };
30
-
31
- /**
32
- * Tolerance (percent) used to decide whether a crop rect is effectively
33
- * full-frame. Sub-pixel / float-ulp deltas below this value are treated
34
- * as no crop. Matches the historical threshold used by the legacy
35
- * `use-save-image.js` (which compared against `99.9`).
36
- */
37
- const CROP_TOLERANCE = 0.1;
38
-
39
- /**
40
- * Build a Core-compatible modifiers array from the cropper state.
41
- *
42
- * Emits `[flip, rotate, crop]` in that order. Identity operations are
43
- * pruned — an absent flip, a rotation that normalizes to 0°, and a crop
44
- * rect that covers (within tolerance) the full post-rotate AABB each
45
- * produce no modifier. An identity state (fresh image, no edits) returns
46
- * an empty array; callers can use that to skip the `/edit` call.
47
- *
48
- * Frames and math:
49
- *
50
- * The cropper's `cropRect` is stored as fractions of the **snap-rotation**
51
- * bounding box (the stencil-reference frame — see `createCamera` /
52
- * `createExportCamera`). Pan is in that frame; zoom scales the image
53
- * beneath a fixed stencil. Two conversions are needed to reach the frame
54
- * Core's crop modifier applies against:
55
- *
56
- * 1. Express the stencil rect in snap-AABB **pixels**:
57
- * Image screen-normalized top-left with zoom/pan is
58
- * `(0.5 + pan - zoom/2)`; the stencil's pixel footprint is the
59
- * inverse of that transform applied to `cropRect`.
60
- *
61
- * 2. Translate from snap-AABB coords to full-AABB coords. Both AABBs
62
- * share the source-image center, so the conversion is a pure
63
- * translation by `(fullCenter - snapCenter)`. Finally, express the
64
- * translated rect as percentages of the full AABB.
65
- *
66
- * Rotation sign: the cropper composes `flip · R(θ)` on the source (see
67
- * `createExportCamera` — rotate then flip). Core's `[flip, rotate]`
68
- * applies the opposite order: `R(θ') · flip`. For both to land on the
69
- * same pixels we invert the rotation sign when exactly one flip axis
70
- * is set (`flip_h · R(θ) = R(-θ) · flip_h`). Both-or-neither flips
71
- * leave the sign alone because `flip_h · flip_v = -I` commutes with
72
- * rotation.
73
- *
74
- * @param cropper The cropper controller. The current edit and the loaded
75
- * image's natural size are both read from its state.
76
- * @return Ordered modifier array; empty when nothing is server-relevant or
77
- * no image has loaded.
78
- */
79
- export function buildModifiers(
80
- cropper: Pick< CropperController, 'state' >
81
- ): Modifier[] {
82
- const modifiers: Modifier[] = [];
83
- const { state } = cropper;
84
- const { image } = state;
85
-
86
- if ( ! image || image.naturalWidth === 0 || image.naturalHeight === 0 ) {
87
- return modifiers;
88
- }
89
-
90
- const imageSize = {
91
- width: image.naturalWidth,
92
- height: image.naturalHeight,
93
- };
94
- const { cropRect, pan, zoom, flip } = state;
95
- const hasFlipH = flip.horizontal;
96
- const hasFlipV = flip.vertical;
97
-
98
- if ( hasFlipH || hasFlipV ) {
99
- modifiers.push( {
100
- type: 'flip',
101
- args: { flip: { horizontal: hasFlipH, vertical: hasFlipV } },
102
- } );
103
- }
104
-
105
- const rawAngle = ( ( state.rotation % 360 ) + 360 ) % 360;
106
- // Invert the sign for single-axis flips so Core's `flip → rotate`
107
- // pipeline matches the cropper's `rotate → flip` composition.
108
- const singleAxisFlip = hasFlipH !== hasFlipV;
109
- const signedAngle = singleAxisFlip ? ( 360 - rawAngle ) % 360 : rawAngle;
110
- if ( signedAngle !== 0 ) {
111
- modifiers.push( { type: 'rotate', args: { angle: signedAngle } } );
112
- }
113
-
114
- const snapRotation = Math.round( state.rotation / 90 ) * 90;
115
- const { width: snapW, height: snapH } = getRotatedBBox(
116
- imageSize.width,
117
- imageSize.height,
118
- snapRotation
119
- );
120
- const { width: fullW, height: fullH } = getRotatedBBox(
121
- imageSize.width,
122
- imageSize.height,
123
- state.rotation
124
- );
125
-
126
- // Stencil rect in snap-AABB pixels. Inverts `createExportCamera`'s
127
- // pan/zoom composition to recover the pixel rectangle the stencil
128
- // framed in that frame.
129
- const imgLeft = 0.5 + pan.x - zoom / 2;
130
- const imgTop = 0.5 + pan.y - zoom / 2;
131
- const snapX = ( ( cropRect.x - imgLeft ) / zoom ) * snapW;
132
- const snapY = ( ( cropRect.y - imgTop ) / zoom ) * snapH;
133
- const widthPx = ( cropRect.width / zoom ) * snapW;
134
- const heightPx = ( cropRect.height / zoom ) * snapH;
135
-
136
- // Translate to the full AABB (both centered on source).
137
- const offsetX = ( fullW - snapW ) / 2;
138
- const offsetY = ( fullH - snapH ) / 2;
139
- const fullX = snapX + offsetX;
140
- const fullY = snapY + offsetY;
141
-
142
- // Percentages of the full AABB — Core's crop frame after rotate.
143
- const leftPct = ( fullX / fullW ) * 100;
144
- const topPct = ( fullY / fullH ) * 100;
145
- const widthPct = ( widthPx / fullW ) * 100;
146
- const heightPct = ( heightPx / fullH ) * 100;
147
-
148
- const coversFullFrame =
149
- leftPct <= CROP_TOLERANCE &&
150
- topPct <= CROP_TOLERANCE &&
151
- widthPct >= 100 - CROP_TOLERANCE &&
152
- heightPct >= 100 - CROP_TOLERANCE;
153
-
154
- if ( ! coversFullFrame ) {
155
- modifiers.push( {
156
- type: 'crop',
157
- args: {
158
- left: leftPct,
159
- top: topPct,
160
- width: widthPct,
161
- height: heightPct,
162
- },
163
- } );
164
- }
165
-
166
- return modifiers;
167
- }
@@ -1,61 +0,0 @@
1
- # Image Editor
2
-
3
- > **Status: internal.** This module is not part of `@wordpress/media-editor`'s public API. Import it only from inside the media-editor package, through the local `../image-editor` barrel.
4
-
5
- The image editor is the cropper engine used by the media editor:
6
-
7
- - **Core:** state, camera math, containment, source-region, transforms, and canvas export.
8
- - **React:** `<Cropper>`, `useCropperController`, and the optional `CropperProvider` / `useCropper` context wrapper.
9
-
10
- `useCropperController` does not provide undo/redo. The media editor's composite state layer adds history and sidebar crop options on top of the cropper reducer, importing the reducer-level helpers from `image-editor/core` directly.
11
-
12
- ## Quick Start
13
-
14
- ```tsx
15
- import { Cropper, useCropperController } from '../image-editor';
16
-
17
- function ImageCropper() {
18
- const controller = useCropperController();
19
-
20
- return (
21
- <div style={ { width: 600, height: 400 } }>
22
- <Cropper
23
- src="https://example.com/photo.jpg"
24
- controller={ controller }
25
- freeformCrop
26
- showDimming
27
- showGrid="interactive"
28
- />
29
- </div>
30
- );
31
- }
32
- ```
33
-
34
- `useCropperController` returns a controller object with the current `state`, named setters, `reset`, `isDirty`, and `getCroppedImage`.
35
-
36
- ## Internal APIs
37
-
38
- ### `Cropper`
39
-
40
- Fills its parent container. Required props are `src` and `controller`. Common options include `freeformCrop`, `aspectRatio`, `showGrid`, `showDimming`, `showDimensions`, `onGestureStart`, and `onGestureEnd`.
41
-
42
- ### `useCropperController( initialState? )`
43
-
44
- Pure React state hook for the cropper reducer. Returns a `CropperController`: current state, named setters, `reset`, `isDirty`, and `getCroppedImage`.
45
-
46
- ### `CropperProvider` / `useCropper`
47
-
48
- Convenience context wrapper around `useCropperController` for component trees that need shared cropper state.
49
-
50
- ## Core Helpers
51
-
52
- `index.ts` also exports helpers for source-region calculation, pipeline replay, and canvas export. Use the named setters or `TransformOperation` values for state changes. The reducer-level helpers that `useMediaEditorState()` composes the cropper with are imported from `core` directly and are not part of this index.
53
-
54
- ## Styles
55
-
56
- Styles are bundled through the media-editor stylesheet. Classes use the `wp-media-editor-image-editor` prefix.
57
-
58
- ## More
59
-
60
- - [docs/architecture.md](docs/architecture.md)
61
- - [docs/recipes.md](docs/recipes.md)
@@ -1,429 +0,0 @@
1
- import { mat2d, vec2 } from 'gl-matrix';
2
- import type {
3
- CropperState,
4
- NormalizedPoint,
5
- NormalizedRect,
6
- Size,
7
- Camera,
8
- } from './types';
9
- import { degreesToRadians } from './math/rotation';
10
- import {
11
- isSafeNumber,
12
- isValidSize,
13
- safeBoundedNumber,
14
- sanitizeCropperState,
15
- } from './math/sanitize';
16
-
17
- // Pre-allocated scratch buffers for `screenToWorld`, which is called per
18
- // pointermove. Module-level singletons — safe because usage is synchronous.
19
- const _scratchMat = mat2d.create();
20
- const _scratchVec = vec2.create();
21
-
22
- /**
23
- * Compute the axis-aligned bounding box of a rectangle after rotation.
24
- *
25
- * @param width The width of the rectangle.
26
- * @param height The height of the rectangle.
27
- * @param rotation The rotation angle in degrees.
28
- * @return The bounding box size after rotation.
29
- */
30
- export function getRotatedBBox(
31
- width: number,
32
- height: number,
33
- rotation: number
34
- ): Size {
35
- // Short-circuit on any unsafe input. `isSafeNumber` is stricter than
36
- // `Number.isFinite`: very large but technically-finite rotations (e.g.
37
- // `Number.MAX_VALUE`) overflow inside `degreesToRadians` and then
38
- // `Math.cos`/`Math.sin` return `NaN`, so the magnitude bound matters.
39
- if (
40
- ! isSafeNumber( width ) ||
41
- ! isSafeNumber( height ) ||
42
- ! isSafeNumber( rotation ) ||
43
- width <= 0 ||
44
- height <= 0
45
- ) {
46
- return { width: 0, height: 0 };
47
- }
48
- const rad = degreesToRadians( rotation );
49
- const cosR = Math.abs( Math.cos( rad ) );
50
- const sinR = Math.abs( Math.sin( rad ) );
51
- return {
52
- width: cosR * width + sinR * height,
53
- height: sinR * width + cosR * height,
54
- };
55
- }
56
-
57
- /**
58
- * Compute the fitted (unrotated) image element dimensions and the visual
59
- * (rotated) bounding box dimensions for a given container, image, and rotation.
60
- *
61
- * This is the same "contain" fit logic used by createCamera, extracted so
62
- * cropper.tsx can size the <img> element and position overlays without
63
- * duplicating the math.
64
- *
65
- * @param containerSize The container dimensions in pixels.
66
- * @param imageSize The natural image dimensions in pixels.
67
- * @param rotation The rotation angle in degrees.
68
- * @return The fitted element size and visual bounding box size.
69
- */
70
- export function getImageFit(
71
- containerSize: Size,
72
- imageSize: Size,
73
- rotation: number
74
- ): { elementSize: Size; visualSize: Size } {
75
- if ( ! isValidSize( containerSize ) || ! isValidSize( imageSize ) ) {
76
- return {
77
- elementSize: { width: 0, height: 0 },
78
- visualSize: { width: 0, height: 0 },
79
- };
80
- }
81
- const safeRotation = safeBoundedNumber( rotation, 0 );
82
- // Snap rotation to the nearest 90° multiple for layout sizing.
83
- // This keeps the stencil a stable size through fine ±45° rotation
84
- // (no inflation at 15°/30° etc.) while still swapping aspect at
85
- // 90°/180°/270° so the snap rotate preserves the framed content.
86
- const snapRotation = Math.round( safeRotation / 90 ) * 90;
87
- const naturalBBox = getRotatedBBox(
88
- imageSize.width,
89
- imageSize.height,
90
- snapRotation
91
- );
92
- // Defensive: getRotatedBBox returns zero dims for hostile inputs, which
93
- // would make fitScale infinite. Mirror the zero-container short-circuit.
94
- if ( naturalBBox.width === 0 || naturalBBox.height === 0 ) {
95
- return {
96
- elementSize: { width: 0, height: 0 },
97
- visualSize: { width: 0, height: 0 },
98
- };
99
- }
100
- const fitScale = Math.min(
101
- containerSize.width / naturalBBox.width,
102
- containerSize.height / naturalBBox.height
103
- );
104
- const renderedW = imageSize.width * fitScale;
105
- const renderedH = imageSize.height * fitScale;
106
- const visualSize = getRotatedBBox( renderedW, renderedH, snapRotation );
107
- return {
108
- elementSize: { width: renderedW, height: renderedH },
109
- visualSize,
110
- };
111
- }
112
-
113
- /**
114
- * Resolve the presentational view magnification that makes the crop fill the
115
- * canvas.
116
- *
117
- * The crop overlay is laid out as `cropRect * visualSize` (the contain-fit
118
- * footprint, with no zoom), so its on-screen size is capped by that footprint.
119
- * When a crop's aspect differs from the image's, it can be much smaller than
120
- * the canvas could show (e.g. a square crop in a tall image is bound by the
121
- * footprint's narrow axis). Scaling `elementSize` and `visualSize` together by
122
- * the returned factor uniformly magnifies the whole scene — image, crop
123
- * overlay, and overlays — so the crop reaches `targetFill` of its constraining
124
- * canvas axis, with the image bleeding past the canvas (clipped by the host).
125
- *
126
- * Purely presentational: it does not touch `zoom`/`pan`/`cropRect`/`rotation`,
127
- * so the framed source region — and therefore the export — is unchanged.
128
- *
129
- * @param cropRect The crop rectangle, normalized to the footprint.
130
- * @param canvasSize The canvas size in pixels.
131
- * @param visualSize The contain-fit (rotated) image footprint in pixels.
132
- * @param targetFill Fraction of the constraining canvas axis to fill (0–1].
133
- * @param maxScale Upper bound on the magnification.
134
- * @return A magnification factor in [1, maxScale]; 1 when none is needed.
135
- */
136
- export function getViewScale(
137
- cropRect: NormalizedRect,
138
- canvasSize: Size,
139
- visualSize: Size,
140
- targetFill: number,
141
- maxScale: number
142
- ): number {
143
- const cropScreenW = cropRect.width * visualSize.width;
144
- const cropScreenH = cropRect.height * visualSize.height;
145
- // `> 0` comparisons (rather than `<= 0`) reject NaN as well as zero and
146
- // negatives, so a non-finite rect/dimension/scale can't propagate to a
147
- // `scale(NaN)`. `isValidSize` already guards finiteness of the sizes.
148
- if (
149
- ! isValidSize( canvasSize ) ||
150
- ! isValidSize( visualSize ) ||
151
- ! ( cropScreenW > 0 ) ||
152
- ! ( cropScreenH > 0 ) ||
153
- ! Number.isFinite( targetFill ) ||
154
- ! Number.isFinite( maxScale ) ||
155
- ! ( targetFill > 0 ) ||
156
- ! ( maxScale >= 1 )
157
- ) {
158
- return 1;
159
- }
160
- const kW = ( targetFill * canvasSize.width ) / cropScreenW;
161
- const kH = ( targetFill * canvasSize.height ) / cropScreenH;
162
- const k = Math.min( kW, kH );
163
- return Math.min( maxScale, Math.max( 1, k ) );
164
- }
165
-
166
- /**
167
- * Compose a camera matrix from cropper state, container, and image dimensions.
168
- *
169
- * The matrix maps normalized world coordinates [0,1] x [0,1] to screen pixels.
170
- * Input (0,0) = image top-left, (1,1) = image bottom-right.
171
- *
172
- * Composition order (left-to-right = outermost first, applied last to point):
173
- * M = T_containerCenter * T_pan * S_flip * R_rotation * S_zoom * T_center * S_toRenderedPixels
174
- *
175
- * Flip is composed outside rotation, so `flip.horizontal` / `flip.vertical`
176
- * are viewport-relative: the image mirrors across the viewport's vertical /
177
- * horizontal axis regardless of current rotation.
178
- *
179
- * @param state The current cropper state (zoom, rotation, flip, crop).
180
- * @param containerSize The size of the container in pixels.
181
- * @param imageSize The natural size of the image in pixels.
182
- * @return The composed camera matrix.
183
- */
184
- export function createCamera(
185
- state: CropperState,
186
- containerSize: Size,
187
- imageSize: Size
188
- ): Camera {
189
- const m = mat2d.create();
190
-
191
- if ( ! isValidSize( containerSize ) || ! isValidSize( imageSize ) ) {
192
- return m;
193
- }
194
-
195
- const safeState = sanitizeCropperState( state );
196
-
197
- // Use the nearest 90° multiple for layout sizing so the stencil
198
- // and visual bounds are stable through fine rotation. The actual
199
- // `state.rotation` is still used for the rotation component of
200
- // the matrix below.
201
- const snapRotation = Math.round( safeState.rotation / 90 ) * 90;
202
-
203
- // Rotated bounding box of the natural image (at snap angle).
204
- const naturalBBox = getRotatedBBox(
205
- imageSize.width,
206
- imageSize.height,
207
- snapRotation
208
- );
209
-
210
- // Defensive: getRotatedBBox returns zero dims for unsafe inputs. Mirror
211
- // the zero-size short-circuit so `fitScale` doesn't become Infinity.
212
- if ( naturalBBox.width === 0 || naturalBBox.height === 0 ) {
213
- return m;
214
- }
215
-
216
- // "Contain" fit: scale rotated bounding box to fit within container.
217
- const fitScale = Math.min(
218
- containerSize.width / naturalBBox.width,
219
- containerSize.height / naturalBBox.height
220
- );
221
-
222
- // The rendered (unrotated) image dimensions at this fit scale.
223
- const renderedW = imageSize.width * fitScale;
224
- const renderedH = imageSize.height * fitScale;
225
-
226
- // Visual (rotated) image footprint in pixels (at snap angle).
227
- const { width: visualW, height: visualH } = getRotatedBBox(
228
- renderedW,
229
- renderedH,
230
- snapRotation
231
- );
232
-
233
- // Build matrix left-to-right (outermost first).
234
- // Innermost operations (last in code) are applied first to input point.
235
-
236
- // Outermost: translate to container center.
237
- mat2d.translate( m, m, [
238
- containerSize.width / 2,
239
- containerSize.height / 2,
240
- ] );
241
-
242
- // Pan offset in visual-space pixels.
243
- mat2d.translate( m, m, [
244
- safeState.pan.x * visualW,
245
- safeState.pan.y * visualH,
246
- ] );
247
-
248
- // Flip (viewport-relative — composed outside rotation so horizontal
249
- // flip always mirrors across the viewport's vertical axis).
250
- mat2d.scale( m, m, [
251
- safeState.flip.horizontal ? -1 : 1,
252
- safeState.flip.vertical ? -1 : 1,
253
- ] );
254
-
255
- // Rotate.
256
- mat2d.rotate( m, m, degreesToRadians( safeState.rotation ) );
257
-
258
- // Zoom.
259
- mat2d.scale( m, m, [ safeState.zoom, safeState.zoom ] );
260
-
261
- // Center origin (shift so 0.5,0.5 in rendered-pixel space = origin).
262
- mat2d.translate( m, m, [ -renderedW / 2, -renderedH / 2 ] );
263
-
264
- // Innermost: scale from normalized [0,1] to rendered pixels.
265
- mat2d.scale( m, m, [ renderedW, renderedH ] );
266
-
267
- return m;
268
- }
269
-
270
- /**
271
- * Transform a normalized world point [0,1] to screen pixels.
272
- *
273
- * @param camera The camera matrix from createCamera.
274
- * @param point The normalized world coordinate to transform.
275
- * @return The screen pixel coordinate.
276
- */
277
- export function worldToScreen(
278
- camera: Camera,
279
- point: NormalizedPoint
280
- ): { x: number; y: number } {
281
- const out = vec2.create();
282
- vec2.transformMat2d( out, [ point.x, point.y ], camera );
283
- return { x: out[ 0 ], y: out[ 1 ] };
284
- }
285
-
286
- /**
287
- * Transform a screen pixel point to normalized world coordinates [0,1].
288
- *
289
- * @param camera The camera matrix from createCamera.
290
- * @param point The screen pixel coordinate to transform.
291
- * @param point.x The x component of the screen pixel coordinate.
292
- * @param point.y The y component of the screen pixel coordinate.
293
- * @return The normalized world coordinate.
294
- */
295
- export function screenToWorld(
296
- camera: Camera,
297
- point: { x: number; y: number }
298
- ): NormalizedPoint {
299
- mat2d.invert( _scratchMat, camera );
300
- const out = _scratchVec;
301
- vec2.transformMat2d( out, [ point.x, point.y ], _scratchMat );
302
- return { x: out[ 0 ], y: out[ 1 ] };
303
- }
304
-
305
- /**
306
- * The bounding box of a transformed region in screen (pixel) space.
307
- */
308
- export interface VisualBounds {
309
- left: number;
310
- top: number;
311
- width: number;
312
- height: number;
313
- }
314
-
315
- /**
316
- * Compute the axis-aligned bounding box of a set of corners after
317
- * transforming them through a camera matrix.
318
- *
319
- * @param camera The camera matrix from createCamera.
320
- * @param corners The corners to transform (each as [x, y]).
321
- * @return The screen-space bounding box.
322
- */
323
- function aabb( camera: Camera, corners: [ number, number ][] ): VisualBounds {
324
- const screenCorners = corners.map( ( c ) => {
325
- const out = vec2.create();
326
- vec2.transformMat2d( out, c, camera );
327
- return out;
328
- } );
329
- let minX = screenCorners[ 0 ][ 0 ];
330
- let maxX = screenCorners[ 0 ][ 0 ];
331
- let minY = screenCorners[ 0 ][ 1 ];
332
- let maxY = screenCorners[ 0 ][ 1 ];
333
- for ( let i = 1; i < screenCorners.length; i++ ) {
334
- const s = screenCorners[ i ];
335
- if ( s[ 0 ] < minX ) {
336
- minX = s[ 0 ];
337
- }
338
- if ( s[ 0 ] > maxX ) {
339
- maxX = s[ 0 ];
340
- }
341
- if ( s[ 1 ] < minY ) {
342
- minY = s[ 1 ];
343
- }
344
- if ( s[ 1 ] > maxY ) {
345
- maxY = s[ 1 ];
346
- }
347
- }
348
- return { left: minX, top: minY, width: maxX - minX, height: maxY - minY };
349
- }
350
-
351
- /**
352
- * Returns the axis-aligned bounding box of the full image (normalized [0,1]x[0,1])
353
- * after applying the camera transform.
354
- *
355
- * @param camera The camera matrix from createCamera.
356
- * @return The screen-space bounding box of the full image.
357
- */
358
- export function getVisibleBounds( camera: Camera ): VisualBounds {
359
- return aabb( camera, [
360
- [ 0, 0 ],
361
- [ 1, 0 ],
362
- [ 1, 1 ],
363
- [ 0, 1 ],
364
- ] );
365
- }
366
-
367
- /**
368
- * Compose a camera matrix for exporting to a canvas.
369
- *
370
- * The resulting matrix maps image-pixel coordinates directly to output-canvas
371
- * coordinates. Apply it with `ctx.setTransform( ...camera )` then
372
- * `ctx.drawImage( image, 0, 0 )`.
373
- *
374
- * The transform chain mirrors the `renderToCanvas` function in canvas-renderer.ts:
375
- * translate(visualCenter - cropOffset + outCenter) → flip → rotate → zoom → translate(-imgCenter)
376
- *
377
- * @param state The current cropper state.
378
- * @param imageSize The natural size of the source image in pixels.
379
- * @param outputSize The desired output canvas size in pixels.
380
- * @return The composed export camera matrix.
381
- */
382
- export function createExportCamera(
383
- state: CropperState,
384
- imageSize: Size,
385
- outputSize: Size
386
- ): Camera {
387
- const m = mat2d.create();
388
- if ( ! isValidSize( imageSize ) || ! isValidSize( outputSize ) ) {
389
- return m;
390
- }
391
- const { rotation, flip, cropRect, zoom, pan } =
392
- sanitizeCropperState( state );
393
- // Reference frame for cropRect/pan is the snap-rotation bbox — that's
394
- // what the stencil and CSS matrix use in the preview (see createCamera
395
- // and getImageFit). Using the true rotation here would position the
396
- // crop window at a different offset than the stencil framed, and show
397
- // a shifted region after any fine rotation.
398
- const snapRotation = Math.round( rotation / 90 ) * 90;
399
- const { width: rotW, height: rotH } = getRotatedBBox(
400
- imageSize.width,
401
- imageSize.height,
402
- snapRotation
403
- );
404
-
405
- // Scale factor to map the natural crop region to the output canvas size.
406
- const naturalCropW = cropRect.width * rotW;
407
- const naturalCropH = cropRect.height * rotH;
408
- const outputScaleX = naturalCropW > 0 ? outputSize.width / naturalCropW : 1;
409
- const outputScaleY =
410
- naturalCropH > 0 ? outputSize.height / naturalCropH : 1;
411
-
412
- const cropOffsetX = cropRect.x * rotW + outputSize.width / 2 / outputScaleX;
413
- const cropOffsetY =
414
- cropRect.y * rotH + outputSize.height / 2 / outputScaleY;
415
- const visualCenterX = rotW / 2 + pan.x * rotW;
416
- const visualCenterY = rotH / 2 + pan.y * rotH;
417
- mat2d.scale( m, m, [ outputScaleX, outputScaleY ] );
418
- mat2d.translate( m, m, [
419
- visualCenterX - cropOffsetX + outputSize.width / 2 / outputScaleX,
420
- visualCenterY - cropOffsetY + outputSize.height / 2 / outputScaleY,
421
- ] );
422
- // Flip is composed outside rotation so it acts in viewport/output space —
423
- // must match createCamera's order for preview and export to agree.
424
- mat2d.scale( m, m, [ flip.horizontal ? -1 : 1, flip.vertical ? -1 : 1 ] );
425
- mat2d.rotate( m, m, degreesToRadians( rotation ) );
426
- mat2d.scale( m, m, [ zoom, zoom ] );
427
- mat2d.translate( m, m, [ -imageSize.width / 2, -imageSize.height / 2 ] );
428
- return m;
429
- }