@wordpress/media-editor 0.21.0 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (432) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/build/components/media-editor/index.cjs +586 -686
  3. package/build/components/media-editor/index.cjs.map +1 -7
  4. package/build/components/media-editor/use-crop-options.cjs +42 -57
  5. package/build/components/media-editor/use-crop-options.cjs.map +1 -7
  6. package/build/components/media-editor/use-save-media-editor.cjs +131 -185
  7. package/build/components/media-editor/use-save-media-editor.cjs.map +1 -7
  8. package/build/components/media-editor-canvas/index.cjs +117 -124
  9. package/build/components/media-editor-canvas/index.cjs.map +1 -7
  10. package/build/components/media-editor-crop-panel/index.cjs +81 -72
  11. package/build/components/media-editor-crop-panel/index.cjs.map +1 -7
  12. package/build/components/media-editor-fine-rotation/index.cjs +61 -72
  13. package/build/components/media-editor-fine-rotation/index.cjs.map +1 -7
  14. package/build/components/media-editor-image-controls/index.cjs +181 -257
  15. package/build/components/media-editor-image-controls/index.cjs.map +1 -7
  16. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +120 -157
  17. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +1 -7
  18. package/build/components/media-editor-modal/index.cjs +112 -155
  19. package/build/components/media-editor-modal/index.cjs.map +1 -7
  20. package/build/components/media-editor-provider/index.cjs +32 -55
  21. package/build/components/media-editor-provider/index.cjs.map +1 -7
  22. package/build/components/media-form/index.cjs +82 -77
  23. package/build/components/media-form/index.cjs.map +1 -7
  24. package/build/components/media-preview/index.cjs +113 -114
  25. package/build/components/media-preview/index.cjs.map +1 -7
  26. package/build/components/rotation-ruler/index.cjs +181 -188
  27. package/build/components/rotation-ruler/index.cjs.map +1 -7
  28. package/build/components/rotation-ruler/use-ruler-drag.cjs +132 -149
  29. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -7
  30. package/build/hooks/use-crop-gesture-handlers.cjs +82 -76
  31. package/build/hooks/use-crop-gesture-handlers.cjs.map +1 -7
  32. package/build/index.cjs +45 -39
  33. package/build/lock-unlock.cjs +9 -35
  34. package/build/lock-unlock.cjs.map +1 -7
  35. package/build/private-apis.cjs +16 -37
  36. package/build/private-apis.cjs.map +1 -7
  37. package/build/state/aspect-ratio-presets.cjs +59 -0
  38. package/build/state/aspect-ratio-presets.cjs.map +1 -0
  39. package/build/state/composite-reducer.cjs +100 -113
  40. package/build/state/composite-reducer.cjs.map +1 -7
  41. package/build/state/index.cjs +22 -45
  42. package/build/state/media-editor-state-provider.cjs +35 -48
  43. package/build/state/media-editor-state-provider.cjs.map +1 -7
  44. package/build/state/types.cjs +11 -31
  45. package/build/state/types.cjs.map +1 -7
  46. package/build/state/use-media-editor-state.cjs +227 -296
  47. package/build/state/use-media-editor-state.cjs.map +1 -7
  48. package/build/store/actions.cjs +17 -44
  49. package/build/store/actions.cjs.map +1 -7
  50. package/build/store/constants.cjs +8 -29
  51. package/build/store/constants.cjs.map +1 -7
  52. package/build/store/index.cjs +33 -40
  53. package/build/store/index.cjs.map +1 -7
  54. package/build/store/reducer.cjs +28 -50
  55. package/build/store/reducer.cjs.map +1 -7
  56. package/build/store/selectors.cjs +16 -41
  57. package/build/store/selectors.cjs.map +1 -7
  58. package/build/utils/get-media-type.cjs +18 -41
  59. package/build/utils/get-media-type.cjs.map +1 -7
  60. package/build/utils/index.cjs +9 -28
  61. package/build-module/components/media-editor/index.mjs +545 -675
  62. package/build-module/components/media-editor/index.mjs.map +1 -7
  63. package/build-module/components/media-editor/use-crop-options.mjs +37 -32
  64. package/build-module/components/media-editor/use-crop-options.mjs.map +1 -7
  65. package/build-module/components/media-editor/use-save-media-editor.mjs +109 -157
  66. package/build-module/components/media-editor/use-save-media-editor.mjs.map +1 -7
  67. package/build-module/components/media-editor-canvas/index.mjs +95 -98
  68. package/build-module/components/media-editor-canvas/index.mjs.map +1 -7
  69. package/build-module/components/media-editor-crop-panel/index.mjs +59 -46
  70. package/build-module/components/media-editor-crop-panel/index.mjs.map +1 -7
  71. package/build-module/components/media-editor-fine-rotation/index.mjs +39 -46
  72. package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -7
  73. package/build-module/components/media-editor-image-controls/index.mjs +171 -238
  74. package/build-module/components/media-editor-image-controls/index.mjs.map +1 -7
  75. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +113 -137
  76. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +1 -7
  77. package/build-module/components/media-editor-modal/index.mjs +88 -124
  78. package/build-module/components/media-editor-modal/index.mjs.map +1 -7
  79. package/build-module/components/media-editor-provider/index.mjs +26 -29
  80. package/build-module/components/media-editor-provider/index.mjs.map +1 -7
  81. package/build-module/components/media-form/index.mjs +74 -56
  82. package/build-module/components/media-form/index.mjs.map +1 -7
  83. package/build-module/components/media-preview/index.mjs +105 -90
  84. package/build-module/components/media-preview/index.mjs.map +1 -7
  85. package/build-module/components/rotation-ruler/index.mjs +161 -164
  86. package/build-module/components/rotation-ruler/index.mjs.map +1 -7
  87. package/build-module/components/rotation-ruler/use-ruler-drag.mjs +125 -122
  88. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -7
  89. package/build-module/hooks/use-crop-gesture-handlers.mjs +76 -50
  90. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +1 -7
  91. package/build-module/index.mjs +3 -10
  92. package/build-module/lock-unlock.mjs +5 -10
  93. package/build-module/lock-unlock.mjs.map +1 -7
  94. package/build-module/private-apis.mjs +8 -9
  95. package/build-module/private-apis.mjs.map +1 -7
  96. package/build-module/state/aspect-ratio-presets.mjs +53 -0
  97. package/build-module/state/aspect-ratio-presets.mjs.map +1 -0
  98. package/build-module/state/composite-reducer.mjs +80 -91
  99. package/build-module/state/composite-reducer.mjs.map +1 -7
  100. package/build-module/state/index.mjs +3 -26
  101. package/build-module/state/media-editor-state-provider.mjs +29 -24
  102. package/build-module/state/media-editor-state-provider.mjs.map +1 -7
  103. package/build-module/state/types.mjs +7 -7
  104. package/build-module/state/types.mjs.map +1 -7
  105. package/build-module/state/use-media-editor-state.mjs +223 -288
  106. package/build-module/state/use-media-editor-state.mjs.map +1 -7
  107. package/build-module/store/actions.mjs +12 -19
  108. package/build-module/store/actions.mjs.map +1 -7
  109. package/build-module/store/constants.mjs +4 -5
  110. package/build-module/store/constants.mjs.map +1 -7
  111. package/build-module/store/index.mjs +8 -9
  112. package/build-module/store/index.mjs.map +1 -7
  113. package/build-module/store/reducer.mjs +23 -26
  114. package/build-module/store/reducer.mjs.map +1 -7
  115. package/build-module/store/selectors.mjs +8 -13
  116. package/build-module/store/selectors.mjs.map +1 -7
  117. package/build-module/utils/get-media-type.mjs +14 -17
  118. package/build-module/utils/get-media-type.mjs.map +1 -7
  119. package/build-module/utils/index.mjs +1 -6
  120. package/build-style/style-rtl.css +145 -61
  121. package/build-style/style.css +144 -61
  122. package/build-types/components/media-editor/index.d.ts +20 -2
  123. package/build-types/components/media-editor/index.d.ts.map +1 -1
  124. package/build-types/components/media-editor/use-crop-options.d.ts +2 -3
  125. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
  126. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
  127. package/build-types/components/media-editor-canvas/index.d.ts +3 -3
  128. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  129. package/build-types/components/media-editor-crop-panel/index.d.ts +1 -1
  130. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  131. package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
  132. package/build-types/components/media-editor-image-controls/index.d.ts +1 -1
  133. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  134. package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -1
  135. package/build-types/components/media-editor-modal/index.d.ts +1 -1
  136. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  137. package/build-types/components/media-form/index.d.ts.map +1 -1
  138. package/build-types/components/media-preview/index.d.ts.map +1 -1
  139. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
  140. package/build-types/state/aspect-ratio-presets.d.ts +24 -0
  141. package/build-types/state/aspect-ratio-presets.d.ts.map +1 -0
  142. package/build-types/state/composite-reducer.d.ts +17 -19
  143. package/build-types/state/composite-reducer.d.ts.map +1 -1
  144. package/build-types/state/index.d.ts +2 -4
  145. package/build-types/state/index.d.ts.map +1 -1
  146. package/build-types/state/types.d.ts +20 -63
  147. package/build-types/state/types.d.ts.map +1 -1
  148. package/build-types/state/use-media-editor-state.d.ts +33 -52
  149. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  150. package/package.json +22 -23
  151. package/src/components/media-editor/index.tsx +355 -167
  152. package/src/components/media-editor/style.scss +19 -0
  153. package/src/components/media-editor/use-crop-options.ts +9 -10
  154. package/src/components/media-editor/use-save-media-editor.ts +48 -10
  155. package/src/components/media-editor-canvas/index.tsx +30 -51
  156. package/src/components/media-editor-canvas/style.scss +6 -6
  157. package/src/components/media-editor-crop-panel/index.tsx +38 -2
  158. package/src/components/media-editor-crop-panel/style.scss +21 -0
  159. package/src/components/media-editor-fine-rotation/index.tsx +6 -6
  160. package/src/components/media-editor-image-controls/index.tsx +15 -11
  161. package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +1 -0
  162. package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +1 -1
  163. package/src/components/media-editor-modal/index.tsx +1 -1
  164. package/src/components/media-editor-modal/style.scss +1 -1
  165. package/src/components/media-form/index.tsx +7 -1
  166. package/src/components/media-preview/index.tsx +65 -23
  167. package/src/components/media-preview/style.scss +16 -2
  168. package/src/components/rotation-ruler/style.scss +9 -5
  169. package/src/components/rotation-ruler/use-ruler-drag.ts +6 -11
  170. package/src/state/README.md +32 -0
  171. package/src/state/aspect-ratio-presets.ts +37 -0
  172. package/src/state/composite-reducer.ts +42 -80
  173. package/src/state/index.ts +2 -17
  174. package/src/state/types.ts +18 -72
  175. package/src/state/use-media-editor-state.ts +155 -238
  176. package/src/style.scss +2 -1
  177. package/build/components/media-editor-modal/build-modifiers.cjs +0 -91
  178. package/build/components/media-editor-modal/build-modifiers.cjs.map +0 -7
  179. package/build/image-editor/core/camera.cjs +0 -225
  180. package/build/image-editor/core/camera.cjs.map +0 -7
  181. package/build/image-editor/core/constants.cjs +0 -108
  182. package/build/image-editor/core/constants.cjs.map +0 -7
  183. package/build/image-editor/core/containment.cjs +0 -276
  184. package/build/image-editor/core/containment.cjs.map +0 -7
  185. package/build/image-editor/core/crop-rect.cjs +0 -48
  186. package/build/image-editor/core/crop-rect.cjs.map +0 -7
  187. package/build/image-editor/core/export/canvas-renderer.cjs +0 -151
  188. package/build/image-editor/core/export/canvas-renderer.cjs.map +0 -7
  189. package/build/image-editor/core/index.cjs +0 -62
  190. package/build/image-editor/core/index.cjs.map +0 -7
  191. package/build/image-editor/core/interaction-controller.cjs +0 -654
  192. package/build/image-editor/core/interaction-controller.cjs.map +0 -7
  193. package/build/image-editor/core/math/rotation.cjs +0 -41
  194. package/build/image-editor/core/math/rotation.cjs.map +0 -7
  195. package/build/image-editor/core/math/sanitize.cjs +0 -92
  196. package/build/image-editor/core/math/sanitize.cjs.map +0 -7
  197. package/build/image-editor/core/setter-helpers.cjs +0 -64
  198. package/build/image-editor/core/setter-helpers.cjs.map +0 -7
  199. package/build/image-editor/core/source-region.cjs +0 -206
  200. package/build/image-editor/core/source-region.cjs.map +0 -7
  201. package/build/image-editor/core/state.cjs +0 -283
  202. package/build/image-editor/core/state.cjs.map +0 -7
  203. package/build/image-editor/core/stencil-math.cjs +0 -216
  204. package/build/image-editor/core/stencil-math.cjs.map +0 -7
  205. package/build/image-editor/core/transform-style.cjs +0 -52
  206. package/build/image-editor/core/transform-style.cjs.map +0 -7
  207. package/build/image-editor/core/transforms/pipeline.cjs +0 -43
  208. package/build/image-editor/core/transforms/pipeline.cjs.map +0 -7
  209. package/build/image-editor/core/types.cjs +0 -19
  210. package/build/image-editor/core/types.cjs.map +0 -7
  211. package/build/image-editor/core/viewport-state.cjs +0 -50
  212. package/build/image-editor/core/viewport-state.cjs.map +0 -7
  213. package/build/image-editor/index.cjs +0 -67
  214. package/build/image-editor/index.cjs.map +0 -7
  215. package/build/image-editor/react/components/cropper-provider.cjs +0 -50
  216. package/build/image-editor/react/components/cropper-provider.cjs.map +0 -7
  217. package/build/image-editor/react/components/cropper.cjs +0 -722
  218. package/build/image-editor/react/components/cropper.cjs.map +0 -7
  219. package/build/image-editor/react/components/index.cjs +0 -36
  220. package/build/image-editor/react/components/index.cjs.map +0 -7
  221. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +0 -172
  222. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +0 -7
  223. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +0 -62
  224. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +0 -7
  225. package/build/image-editor/react/components/overlays/grid-overlay.cjs +0 -91
  226. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +0 -7
  227. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +0 -426
  228. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +0 -7
  229. package/build/image-editor/react/components/viewport-provider.cjs +0 -51
  230. package/build/image-editor/react/components/viewport-provider.cjs.map +0 -7
  231. package/build/image-editor/react/hooks/build-cropper-setters.cjs +0 -71
  232. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +0 -7
  233. package/build/image-editor/react/hooks/index.cjs +0 -34
  234. package/build/image-editor/react/hooks/index.cjs.map +0 -7
  235. package/build/image-editor/react/hooks/use-aria-announcer.cjs +0 -160
  236. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +0 -7
  237. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +0 -113
  238. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +0 -7
  239. package/build/image-editor/react/hooks/use-interaction.cjs +0 -213
  240. package/build/image-editor/react/hooks/use-interaction.cjs.map +0 -7
  241. package/build/image-editor/react/hooks/use-transform-style.cjs +0 -38
  242. package/build/image-editor/react/hooks/use-transform-style.cjs.map +0 -7
  243. package/build/image-editor/react/hooks/use-viewport-state.cjs +0 -51
  244. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +0 -7
  245. package/build/image-editor/react/visually-hidden-style.cjs +0 -41
  246. package/build/image-editor/react/visually-hidden-style.cjs.map +0 -7
  247. package/build/index.cjs.map +0 -7
  248. package/build/state/index.cjs.map +0 -7
  249. package/build/utils/index.cjs.map +0 -7
  250. package/build-module/components/media-editor-modal/build-modifiers.mjs +0 -66
  251. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +0 -7
  252. package/build-module/image-editor/core/camera.mjs +0 -198
  253. package/build-module/image-editor/core/camera.mjs.map +0 -7
  254. package/build-module/image-editor/core/constants.mjs +0 -69
  255. package/build-module/image-editor/core/constants.mjs.map +0 -7
  256. package/build-module/image-editor/core/containment.mjs +0 -253
  257. package/build-module/image-editor/core/containment.mjs.map +0 -7
  258. package/build-module/image-editor/core/crop-rect.mjs +0 -23
  259. package/build-module/image-editor/core/crop-rect.mjs.map +0 -7
  260. package/build-module/image-editor/core/export/canvas-renderer.mjs +0 -120
  261. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +0 -7
  262. package/build-module/image-editor/core/index.mjs +0 -36
  263. package/build-module/image-editor/core/index.mjs.map +0 -7
  264. package/build-module/image-editor/core/interaction-controller.mjs +0 -635
  265. package/build-module/image-editor/core/interaction-controller.mjs.map +0 -7
  266. package/build-module/image-editor/core/math/rotation.mjs +0 -15
  267. package/build-module/image-editor/core/math/rotation.mjs.map +0 -7
  268. package/build-module/image-editor/core/math/sanitize.mjs +0 -63
  269. package/build-module/image-editor/core/math/sanitize.mjs.map +0 -7
  270. package/build-module/image-editor/core/setter-helpers.mjs +0 -39
  271. package/build-module/image-editor/core/setter-helpers.mjs.map +0 -7
  272. package/build-module/image-editor/core/source-region.mjs +0 -178
  273. package/build-module/image-editor/core/source-region.mjs.map +0 -7
  274. package/build-module/image-editor/core/state.mjs +0 -260
  275. package/build-module/image-editor/core/state.mjs.map +0 -7
  276. package/build-module/image-editor/core/stencil-math.mjs +0 -187
  277. package/build-module/image-editor/core/stencil-math.mjs.map +0 -7
  278. package/build-module/image-editor/core/transform-style.mjs +0 -27
  279. package/build-module/image-editor/core/transform-style.mjs.map +0 -7
  280. package/build-module/image-editor/core/transforms/pipeline.mjs +0 -17
  281. package/build-module/image-editor/core/transforms/pipeline.mjs.map +0 -7
  282. package/build-module/image-editor/core/types.mjs +0 -1
  283. package/build-module/image-editor/core/types.mjs.map +0 -7
  284. package/build-module/image-editor/core/viewport-state.mjs +0 -24
  285. package/build-module/image-editor/core/viewport-state.mjs.map +0 -7
  286. package/build-module/image-editor/index.mjs +0 -39
  287. package/build-module/image-editor/index.mjs.map +0 -7
  288. package/build-module/image-editor/react/components/cropper-provider.mjs +0 -26
  289. package/build-module/image-editor/react/components/cropper-provider.mjs.map +0 -7
  290. package/build-module/image-editor/react/components/cropper.mjs +0 -704
  291. package/build-module/image-editor/react/components/cropper.mjs.map +0 -7
  292. package/build-module/image-editor/react/components/index.mjs +0 -9
  293. package/build-module/image-editor/react/components/index.mjs.map +0 -7
  294. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +0 -147
  295. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +0 -7
  296. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +0 -37
  297. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +0 -7
  298. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +0 -66
  299. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +0 -7
  300. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +0 -414
  301. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +0 -7
  302. package/build-module/image-editor/react/components/viewport-provider.mjs +0 -27
  303. package/build-module/image-editor/react/components/viewport-provider.mjs.map +0 -7
  304. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +0 -46
  305. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +0 -7
  306. package/build-module/image-editor/react/hooks/index.mjs +0 -8
  307. package/build-module/image-editor/react/hooks/index.mjs.map +0 -7
  308. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +0 -135
  309. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +0 -7
  310. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +0 -100
  311. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +0 -7
  312. package/build-module/image-editor/react/hooks/use-interaction.mjs +0 -190
  313. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +0 -7
  314. package/build-module/image-editor/react/hooks/use-transform-style.mjs +0 -13
  315. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +0 -7
  316. package/build-module/image-editor/react/hooks/use-viewport-state.mjs +0 -29
  317. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +0 -7
  318. package/build-module/image-editor/react/visually-hidden-style.mjs +0 -16
  319. package/build-module/image-editor/react/visually-hidden-style.mjs.map +0 -7
  320. package/build-module/index.mjs.map +0 -7
  321. package/build-module/state/index.mjs.map +0 -7
  322. package/build-module/utils/index.mjs.map +0 -7
  323. package/build-types/components/media-editor-modal/build-modifiers.d.ts +0 -80
  324. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +0 -1
  325. package/build-types/image-editor/core/camera.d.ts +0 -128
  326. package/build-types/image-editor/core/camera.d.ts.map +0 -1
  327. package/build-types/image-editor/core/constants.d.ts +0 -99
  328. package/build-types/image-editor/core/constants.d.ts.map +0 -1
  329. package/build-types/image-editor/core/containment.d.ts +0 -72
  330. package/build-types/image-editor/core/containment.d.ts.map +0 -1
  331. package/build-types/image-editor/core/crop-rect.d.ts +0 -16
  332. package/build-types/image-editor/core/crop-rect.d.ts.map +0 -1
  333. package/build-types/image-editor/core/export/canvas-renderer.d.ts +0 -103
  334. package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +0 -1
  335. package/build-types/image-editor/core/index.d.ts +0 -10
  336. package/build-types/image-editor/core/index.d.ts.map +0 -1
  337. package/build-types/image-editor/core/interaction-controller.d.ts +0 -191
  338. package/build-types/image-editor/core/interaction-controller.d.ts.map +0 -1
  339. package/build-types/image-editor/core/math/rotation.d.ts +0 -18
  340. package/build-types/image-editor/core/math/rotation.d.ts.map +0 -1
  341. package/build-types/image-editor/core/math/sanitize.d.ts +0 -67
  342. package/build-types/image-editor/core/math/sanitize.d.ts.map +0 -1
  343. package/build-types/image-editor/core/setter-helpers.d.ts +0 -21
  344. package/build-types/image-editor/core/setter-helpers.d.ts.map +0 -1
  345. package/build-types/image-editor/core/source-region.d.ts +0 -94
  346. package/build-types/image-editor/core/source-region.d.ts.map +0 -1
  347. package/build-types/image-editor/core/state.d.ts +0 -51
  348. package/build-types/image-editor/core/state.d.ts.map +0 -1
  349. package/build-types/image-editor/core/stencil-math.d.ts +0 -102
  350. package/build-types/image-editor/core/stencil-math.d.ts.map +0 -1
  351. package/build-types/image-editor/core/transform-style.d.ts +0 -20
  352. package/build-types/image-editor/core/transform-style.d.ts.map +0 -1
  353. package/build-types/image-editor/core/transforms/pipeline.d.ts +0 -24
  354. package/build-types/image-editor/core/transforms/pipeline.d.ts.map +0 -1
  355. package/build-types/image-editor/core/types.d.ts +0 -269
  356. package/build-types/image-editor/core/types.d.ts.map +0 -1
  357. package/build-types/image-editor/core/viewport-state.d.ts +0 -4
  358. package/build-types/image-editor/core/viewport-state.d.ts.map +0 -1
  359. package/build-types/image-editor/index.d.ts +0 -16
  360. package/build-types/image-editor/index.d.ts.map +0 -1
  361. package/build-types/image-editor/react/components/cropper-provider.d.ts +0 -38
  362. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +0 -1
  363. package/build-types/image-editor/react/components/cropper.d.ts +0 -63
  364. package/build-types/image-editor/react/components/cropper.d.ts.map +0 -1
  365. package/build-types/image-editor/react/components/index.d.ts +0 -4
  366. package/build-types/image-editor/react/components/index.d.ts.map +0 -1
  367. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +0 -33
  368. package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +0 -1
  369. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +0 -17
  370. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +0 -1
  371. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +0 -27
  372. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +0 -1
  373. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +0 -33
  374. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +0 -1
  375. package/build-types/image-editor/react/components/viewport-provider.d.ts +0 -6
  376. package/build-types/image-editor/react/components/viewport-provider.d.ts.map +0 -1
  377. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +0 -44
  378. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +0 -1
  379. package/build-types/image-editor/react/hooks/index.d.ts +0 -5
  380. package/build-types/image-editor/react/hooks/index.d.ts.map +0 -1
  381. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +0 -14
  382. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +0 -1
  383. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts +0 -55
  384. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +0 -1
  385. package/build-types/image-editor/react/hooks/use-interaction.d.ts +0 -65
  386. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +0 -1
  387. package/build-types/image-editor/react/hooks/use-transform-style.d.ts +0 -13
  388. package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +0 -1
  389. package/build-types/image-editor/react/hooks/use-viewport-state.d.ts +0 -16
  390. package/build-types/image-editor/react/hooks/use-viewport-state.d.ts.map +0 -1
  391. package/build-types/image-editor/react/visually-hidden-style.d.ts +0 -6
  392. package/build-types/image-editor/react/visually-hidden-style.d.ts.map +0 -1
  393. package/src/components/media-editor-modal/build-modifiers.ts +0 -161
  394. package/src/image-editor/README.md +0 -61
  395. package/src/image-editor/core/camera.ts +0 -429
  396. package/src/image-editor/core/constants.ts +0 -150
  397. package/src/image-editor/core/containment.ts +0 -469
  398. package/src/image-editor/core/crop-rect.ts +0 -42
  399. package/src/image-editor/core/export/canvas-renderer.ts +0 -250
  400. package/src/image-editor/core/index.ts +0 -47
  401. package/src/image-editor/core/interaction-controller.ts +0 -976
  402. package/src/image-editor/core/math/rotation.ts +0 -26
  403. package/src/image-editor/core/math/sanitize.ts +0 -155
  404. package/src/image-editor/core/setter-helpers.ts +0 -63
  405. package/src/image-editor/core/source-region.ts +0 -367
  406. package/src/image-editor/core/state.ts +0 -484
  407. package/src/image-editor/core/stencil-math.ts +0 -379
  408. package/src/image-editor/core/transform-style.ts +0 -56
  409. package/src/image-editor/core/transforms/pipeline.ts +0 -40
  410. package/src/image-editor/core/types.ts +0 -230
  411. package/src/image-editor/core/viewport-state.ts +0 -30
  412. package/src/image-editor/docs/architecture.md +0 -63
  413. package/src/image-editor/docs/recipes.md +0 -184
  414. package/src/image-editor/index.ts +0 -52
  415. package/src/image-editor/react/components/cropper-provider.tsx +0 -70
  416. package/src/image-editor/react/components/cropper.scss +0 -259
  417. package/src/image-editor/react/components/cropper.tsx +0 -1116
  418. package/src/image-editor/react/components/index.ts +0 -3
  419. package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +0 -262
  420. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +0 -55
  421. package/src/image-editor/react/components/overlays/grid-overlay.tsx +0 -77
  422. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +0 -623
  423. package/src/image-editor/react/components/viewport-provider.tsx +0 -32
  424. package/src/image-editor/react/hooks/build-cropper-setters.ts +0 -102
  425. package/src/image-editor/react/hooks/index.ts +0 -4
  426. package/src/image-editor/react/hooks/use-aria-announcer.ts +0 -193
  427. package/src/image-editor/react/hooks/use-cropper-reducer.ts +0 -197
  428. package/src/image-editor/react/hooks/use-interaction.ts +0 -294
  429. package/src/image-editor/react/hooks/use-transform-style.ts +0 -23
  430. package/src/image-editor/react/hooks/use-viewport-state.ts +0 -41
  431. package/src/image-editor/react/visually-hidden-style.ts +0 -15
  432. package/src/image-editor/style.scss +0 -1
@@ -1,976 +0,0 @@
1
- import type { CropperState, NormalizedPoint, Size } from './types';
2
- import {
3
- DEFAULT_KEYBOARD_STEP,
4
- DEFAULT_WHEEL_ZOOM_SPEED,
5
- KEYBOARD_SHIFT_STEP_MULTIPLIER,
6
- MIN_ZOOM,
7
- MAX_ZOOM,
8
- } from './constants';
9
- import { restrictPanZoom } from './containment';
10
-
11
- /** Time window for detecting a double-tap gesture (ms). */
12
- const DOUBLE_TAP_TIME = 300;
13
- /** Max distance between taps to count as a double-tap (px). */
14
- const DOUBLE_TAP_DISTANCE = 30;
15
- /** Duration of the zoom animation state (ms). */
16
- const ZOOM_ANIMATION_DURATION = 200;
17
-
18
- /**
19
- * Get the natural image dimensions from cropper state, falling back to 1x1.
20
- *
21
- * @param state The current cropper state.
22
- * @return The image dimensions.
23
- */
24
- function getImageSizeFromState( state: CropperState ): {
25
- width: number;
26
- height: number;
27
- } {
28
- return state.image
29
- ? { width: state.image.naturalWidth, height: state.image.naturalHeight }
30
- : { width: 1, height: 1 };
31
- }
32
-
33
- /**
34
- * Get the distance between two touch points.
35
- *
36
- * @param t1 The first touch.
37
- * @param t2 The second touch.
38
- * @return The pixel distance between the two touches.
39
- */
40
- function getTouchDistance( t1: Touch, t2: Touch ): number {
41
- const dx = t1.clientX - t2.clientX;
42
- const dy = t1.clientY - t2.clientY;
43
- return Math.sqrt( dx * dx + dy * dy );
44
- }
45
-
46
- /**
47
- * Status reported by the interaction controller when drag/zoom state changes.
48
- */
49
- export interface InteractionStatus {
50
- /** Whether a drag (pan) interaction is in progress. */
51
- isDragging: boolean;
52
- /** Whether a double-tap zoom animation is in progress. */
53
- isZooming: boolean;
54
- }
55
-
56
- /**
57
- * State updates the interaction controller can request from its host UI.
58
- */
59
- export interface CropperInteractionActions {
60
- /** Set the image pan offset in normalized coordinates. */
61
- setPan: ( pan: NormalizedPoint ) => void;
62
- /**
63
- * Set the zoom level. Cursorless surfaces (slider, keyboard `+`/`-`)
64
- * rely on this anchoring at the crop center; pointer-driven zoom
65
- * paths use `setZoomAtPoint` with an explicit focal point instead.
66
- */
67
- setZoom: ( zoom: number ) => void;
68
- /** Set zoom and pan together for focal-point zoom. */
69
- setZoomAtPoint: ( zoom: number, pan: NormalizedPoint ) => void;
70
- /** Snap rotate by 90 degrees. */
71
- snapRotate90: ( direction: 1 | -1 ) => void;
72
- /** Toggle flip on the given axis. */
73
- toggleFlip?: ( direction: 'horizontal' | 'vertical' ) => void;
74
- }
75
-
76
- /**
77
- * Options for creating an InteractionController.
78
- *
79
- * Scalar options (minZoom, maxZoom, etc.) are read lazily on each
80
- * interaction, so changes to the options object take effect immediately
81
- * without recreating the controller.
82
- */
83
- export interface InteractionControllerOptions {
84
- /** Returns the current cropper state. Called on every interaction. */
85
- getState: () => CropperState;
86
- /** State updates the interaction controller can request from its host UI. */
87
- actions: CropperInteractionActions;
88
- /** Returns the container dimensions in pixels. */
89
- getContainerSize: () => Size;
90
- /** Returns the rendered image dimensions in pixels, if available. */
91
- getImageSize: () => Size | undefined;
92
- /** Minimum zoom level. Defaults to MIN_ZOOM. Read lazily. */
93
- minZoom?: number;
94
- /** Maximum zoom level. Defaults to MAX_ZOOM. Read lazily. */
95
- maxZoom?: number;
96
- /** Zoom speed multiplier for wheel events. Defaults to 0.0025. Read lazily. */
97
- zoomSpeed?: number;
98
- /**
99
- * Pan step size in normalized coords for keyboard events.
100
- * Defaults to 0.01. Read lazily. Shift multiplies it by 10.
101
- */
102
- keyboardStep?: number;
103
- /** Zoom level for double-tap zoom. Defaults to 2. Read lazily. */
104
- doubleTapZoom?: number;
105
- /** Fires when a continuous gesture begins (pan drag, pinch zoom). */
106
- onGestureStart?: () => void;
107
- /** Fires when a continuous gesture ends (pointer release). */
108
- onGestureEnd?: () => void;
109
- /** Called when isDragging or isZooming changes. */
110
- onStatusChange?: ( status: InteractionStatus ) => void;
111
- }
112
-
113
- /**
114
- * Framework-agnostic imperative controller for image cropper interactions.
115
- *
116
- * Handles mouse, touch, and keyboard event processing for pan, zoom,
117
- * and crop manipulation. Uses requestAnimationFrame for drag/pinch
118
- * updates to avoid layout thrashing.
119
- *
120
- * The controller does not register DOM event listeners itself — that is the
121
- * responsibility of the UI layer (React hook, Vue directive, or vanilla JS).
122
- * Instead it exposes `handlePointerDown`, `handleWheel`, `handleTouchStart`,
123
- * and `handleKeyDown` methods that the UI layer calls with native DOM events.
124
- *
125
- * Scalar options (minZoom, maxZoom, zoomSpeed, keyboardStep, doubleTapZoom)
126
- * are read lazily from `this.options` on each interaction, so the UI layer
127
- * can update them without recreating the controller.
128
- *
129
- * Call `destroy()` to clean up timers and pending animation frames.
130
- */
131
- export class InteractionController {
132
- private readonly options: InteractionControllerOptions;
133
-
134
- /** Current drag/zoom status. */
135
- private isDragging = false;
136
- private isZooming = false;
137
-
138
- /** Active drag state during pointer interactions. */
139
- private drag: {
140
- startX: number;
141
- startY: number;
142
- startPanX: number;
143
- startPanY: number;
144
- } | null = null;
145
-
146
- /** Active touch state during touch interactions. */
147
- private touch: {
148
- /** Finger distance at pinch start (0 until pinch detected). */
149
- startDistance: number;
150
- /** Zoom level when pinch was detected. */
151
- startZoom: number;
152
- /** First touch X/Y (for single-finger pan delta). */
153
- lastTouchX: number;
154
- lastTouchY: number;
155
- /** Pan when gesture started. */
156
- startPanX: number;
157
- startPanY: number;
158
- /** Container rect snapshot for focal-point math. */
159
- containerRect?: DOMRect;
160
- /** Midpoint of two fingers when pinch was detected. */
161
- startMidX: number;
162
- startMidY: number;
163
- /** Whether pinch was ever detected during this gesture. */
164
- didPinch: boolean;
165
- /** Whether any dispatch has occurred (prevents dispatching before first move). */
166
- moved: boolean;
167
- } | null = null;
168
-
169
- /** Cleanup function for active touch listeners on document. */
170
- private touchCleanup: ( () => void ) | null = null;
171
-
172
- /**
173
- * Cleanup function for active pointer-drag listeners on the
174
- * captured element. Set while a pointer drag is in progress so
175
- * `destroy()` can tear the drag down cleanly if the consumer
176
- * unmounts mid-drag. Cleared on pointerup/lostpointercapture.
177
- */
178
- private pointerCleanup: ( () => void ) | null = null;
179
-
180
- /** Last tap info for double-tap detection. */
181
- private lastTap: {
182
- time: number;
183
- x: number;
184
- y: number;
185
- } | null = null;
186
-
187
- /** Timer for the zoom animation state (double-tap). */
188
- private zoomTimer: ReturnType< typeof setTimeout > | undefined;
189
-
190
- /** Timer for wheel gesture debounce. */
191
- private wheelGestureTimer: ReturnType< typeof setTimeout > | undefined;
192
-
193
- /** Whether a wheel gesture is currently active. */
194
- private wheelGestureActive = false;
195
-
196
- /** Current requestAnimationFrame ID. */
197
- private rafId = 0;
198
-
199
- constructor( options: InteractionControllerOptions ) {
200
- this.options = options;
201
- }
202
-
203
- /** Read minZoom lazily so option changes take effect immediately. */
204
- private get minZoom(): number {
205
- return this.options.minZoom ?? MIN_ZOOM;
206
- }
207
-
208
- /** Read maxZoom lazily so option changes take effect immediately. */
209
- private get maxZoom(): number {
210
- return this.options.maxZoom ?? MAX_ZOOM;
211
- }
212
-
213
- /** Read zoomSpeed lazily so option changes take effect immediately. */
214
- private get zoomSpeed(): number {
215
- return this.options.zoomSpeed ?? DEFAULT_WHEEL_ZOOM_SPEED;
216
- }
217
-
218
- /** Read keyboardStep lazily so option changes take effect immediately. */
219
- private get keyboardStep(): number {
220
- return this.options.keyboardStep ?? DEFAULT_KEYBOARD_STEP;
221
- }
222
-
223
- /**
224
- * Get the keyboard pan step, including modifier-based coarse movement.
225
- *
226
- * @param event The native KeyboardEvent.
227
- * @return The normalized pan step.
228
- */
229
- private getKeyboardStep( event: KeyboardEvent ): number {
230
- return event.shiftKey
231
- ? this.keyboardStep * KEYBOARD_SHIFT_STEP_MULTIPLIER
232
- : this.keyboardStep;
233
- }
234
-
235
- /** Read doubleTapZoom lazily so option changes take effect immediately. */
236
- private get doubleTapZoom(): number {
237
- return this.options.doubleTapZoom ?? 2;
238
- }
239
-
240
- /**
241
- * Update the drag/zoom status and notify via callback if changed.
242
- *
243
- * @param update Partial status with isDragging and/or isZooming.
244
- */
245
- private setStatus(
246
- update: Partial< { isDragging: boolean; isZooming: boolean } >
247
- ): void {
248
- let changed = false;
249
- if (
250
- update.isDragging !== undefined &&
251
- update.isDragging !== this.isDragging
252
- ) {
253
- this.isDragging = update.isDragging;
254
- changed = true;
255
- }
256
- if (
257
- update.isZooming !== undefined &&
258
- update.isZooming !== this.isZooming
259
- ) {
260
- this.isZooming = update.isZooming;
261
- changed = true;
262
- }
263
- if ( changed ) {
264
- this.options.onStatusChange?.( {
265
- isDragging: this.isDragging,
266
- isZooming: this.isZooming,
267
- } );
268
- }
269
- }
270
-
271
- /**
272
- * Handle a pointer-down event on the container element.
273
- *
274
- * Initiates a drag (pan) interaction. Captures the pointer on the
275
- * provided element and registers pointermove/pointerup/lostpointercapture
276
- * listeners for the duration of the drag.
277
- *
278
- * @param e The native PointerEvent.
279
- * @param el The DOM element to capture the pointer on.
280
- */
281
- handlePointerDown( e: PointerEvent, el: HTMLElement ): void {
282
- // Only handle primary button (left click / first touch).
283
- if ( e.button !== 0 ) {
284
- return;
285
- }
286
- // Touch gestures are handled by touchstart/touchmove. Starting the
287
- // pointer drag path too can briefly toggle drag UI before pinch begins.
288
- if ( e.pointerType === 'touch' ) {
289
- return;
290
- }
291
- e.preventDefault();
292
-
293
- // Blur any focused handle so its focus ring doesn't linger,
294
- // but leave the canvas itself alone so keyboard control works after drag.
295
- const ownerDoc = el.ownerDocument;
296
- if (
297
- ownerDoc?.activeElement instanceof HTMLElement &&
298
- ownerDoc.activeElement !== el
299
- ) {
300
- ownerDoc.activeElement.blur();
301
- }
302
-
303
- el.focus();
304
-
305
- // Capture pointer so drag works across iframe boundaries.
306
- el.setPointerCapture( e.pointerId );
307
-
308
- // End a pending wheel gesture now, so its debounce timer cannot
309
- // fire mid-drag and end the drag's gesture along with it.
310
- if ( this.wheelGestureActive ) {
311
- clearTimeout( this.wheelGestureTimer );
312
- this.wheelGestureActive = false;
313
- this.options.onGestureEnd?.();
314
- }
315
-
316
- this.setStatus( { isDragging: true } );
317
- this.options.onGestureStart?.();
318
-
319
- const currentState = this.options.getState();
320
- this.drag = {
321
- startX: e.clientX,
322
- startY: e.clientY,
323
- startPanX: currentState.pan.x,
324
- startPanY: currentState.pan.y,
325
- };
326
-
327
- const onPointerMove = ( moveEvent: Event ) => {
328
- const drag = this.drag;
329
- if ( ! drag ) {
330
- return;
331
- }
332
- const pe = moveEvent as PointerEvent;
333
-
334
- cancelAnimationFrame( this.rafId );
335
- this.rafId = requestAnimationFrame( () => {
336
- const s = this.options.getState();
337
- const imgSize = this.options.getImageSize();
338
- const containerSize = this.options.getContainerSize();
339
- const panSize = imgSize ?? containerSize;
340
- const deltaX =
341
- panSize.width > 0
342
- ? ( pe.clientX - drag.startX ) / panSize.width
343
- : 0;
344
- const deltaY =
345
- panSize.height > 0
346
- ? ( pe.clientY - drag.startY ) / panSize.height
347
- : 0;
348
-
349
- const { pan: newCrop } = restrictPanZoom(
350
- {
351
- ...s,
352
- pan: {
353
- x: drag.startPanX + deltaX,
354
- y: drag.startPanY + deltaY,
355
- },
356
- },
357
- getImageSizeFromState( s ),
358
- s.cropRect
359
- );
360
-
361
- this.options.actions.setPan( newCrop );
362
- } );
363
- };
364
-
365
- const removeListeners = () => {
366
- el.removeEventListener( 'pointermove', onPointerMove );
367
- el.removeEventListener( 'pointerup', onPointerUp );
368
- el.removeEventListener( 'lostpointercapture', onPointerUp );
369
- };
370
-
371
- const onPointerUp = () => {
372
- this.setStatus( { isDragging: false } );
373
- this.options.onGestureEnd?.();
374
- this.drag = null;
375
- cancelAnimationFrame( this.rafId );
376
- removeListeners();
377
- this.pointerCleanup = null;
378
- };
379
-
380
- el.addEventListener( 'pointermove', onPointerMove );
381
- el.addEventListener( 'pointerup', onPointerUp );
382
- el.addEventListener( 'lostpointercapture', onPointerUp );
383
- // Expose cleanup to destroy() in case the consumer unmounts
384
- // mid-drag. The pending rAF is cancelled in destroy().
385
- this.pointerCleanup = removeListeners;
386
- }
387
-
388
- /**
389
- * Handle a wheel event on the container element.
390
- *
391
- * Implements focal-point zoom: the point under the cursor stays
392
- * stationary on screen while the zoom level changes.
393
- *
394
- * Must be registered with `{ passive: false }` to allow preventDefault.
395
- *
396
- * @param e The native WheelEvent.
397
- */
398
- handleWheel( e: WheelEvent ): void {
399
- e.preventDefault();
400
-
401
- if ( this.drag ) {
402
- return;
403
- }
404
-
405
- // Debounced gesture boundaries for wheel zoom.
406
- // Start on first wheel event, end after 300ms of no events.
407
- if ( ! this.wheelGestureActive ) {
408
- this.wheelGestureActive = true;
409
- this.options.onGestureStart?.();
410
- }
411
- clearTimeout( this.wheelGestureTimer );
412
- this.wheelGestureTimer = setTimeout( () => {
413
- this.wheelGestureActive = false;
414
- this.options.onGestureEnd?.();
415
- }, DOUBLE_TAP_TIME );
416
-
417
- const s = this.options.getState();
418
- const delta = -e.deltaY * this.zoomSpeed;
419
- const newZoom = Math.min(
420
- this.maxZoom,
421
- Math.max( this.minZoom, s.zoom + delta )
422
- );
423
-
424
- if ( newZoom === s.zoom ) {
425
- return;
426
- }
427
-
428
- // Focal-point zoom: keep the point under the cursor stationary.
429
- const containerSize = this.options.getContainerSize();
430
- const imgSize = this.options.getImageSize();
431
- const visSize = imgSize ?? containerSize;
432
- const target = e.currentTarget;
433
- const rect =
434
- target instanceof Element
435
- ? target.getBoundingClientRect()
436
- : undefined;
437
- if ( visSize.width > 0 && visSize.height > 0 && rect ) {
438
- const fx = e.clientX - rect.left - containerSize.width / 2;
439
- const fy = e.clientY - rect.top - containerSize.height / 2;
440
-
441
- const zoomRatio = 1 - newZoom / s.zoom;
442
- const focalNormX = fx / visSize.width;
443
- const focalNormY = fy / visSize.height;
444
- const newCropX = s.pan.x + ( focalNormX - s.pan.x ) * zoomRatio;
445
- const newCropY = s.pan.y + ( focalNormY - s.pan.y ) * zoomRatio;
446
-
447
- const { pan: clampedCrop } = restrictPanZoom(
448
- { ...s, zoom: newZoom, pan: { x: newCropX, y: newCropY } },
449
- getImageSizeFromState( s ),
450
- s.cropRect
451
- );
452
- this.options.actions.setZoomAtPoint( newZoom, clampedCrop );
453
- } else {
454
- this.options.actions.setZoom( newZoom );
455
- }
456
- }
457
-
458
- /**
459
- * Handle a touch-start event on the container element.
460
- *
461
- * Supports single-finger pan, two-finger pinch zoom, and
462
- * double-tap to toggle between fit and 2x zoom. Registers
463
- * touchmove/touchend/touchcancel on the provided document for the
464
- * duration of the gesture.
465
- *
466
- * @param e The native TouchEvent.
467
- * @param containerRect The bounding rect of the container element.
468
- * @param doc The document to register move/end listeners on.
469
- * Defaults to globalThis.document. Pass the iframe's
470
- * contentDocument when running inside an iframe.
471
- */
472
- handleTouchStart(
473
- e: TouchEvent,
474
- containerRect: DOMRect,
475
- doc: Document = document
476
- ): void {
477
- // Second finger arriving during an existing gesture — snapshot
478
- // pinch start values. handleTouchMove will detect 2 touches and
479
- // handle pinch automatically.
480
- if ( this.touch && e.touches.length === 2 ) {
481
- const s = this.options.getState();
482
- const distance = getTouchDistance( e.touches[ 0 ], e.touches[ 1 ] );
483
- const midX =
484
- ( e.touches[ 0 ].clientX + e.touches[ 1 ].clientX ) / 2;
485
- const midY =
486
- ( e.touches[ 0 ].clientY + e.touches[ 1 ].clientY ) / 2;
487
- this.touch.didPinch = true;
488
- this.touch.startDistance = distance;
489
- this.touch.startZoom = s.zoom;
490
- this.touch.startPanX = s.pan.x;
491
- this.touch.startPanY = s.pan.y;
492
- this.touch.startMidX = midX;
493
- this.touch.startMidY = midY;
494
- this.touch.containerRect = containerRect;
495
- this.setStatus( { isDragging: false } );
496
- return;
497
- }
498
-
499
- if ( e.touches.length === 1 ) {
500
- const tapX = e.touches[ 0 ].clientX;
501
- const tapY = e.touches[ 0 ].clientY;
502
- if ( this.tryDoubleTap( e, tapX, tapY, containerRect ) ) {
503
- return;
504
- }
505
-
506
- const currentState = this.options.getState();
507
-
508
- // Record touch state. Don't decide pan vs pinch yet —
509
- // that happens in handleTouchMove based on touches.length.
510
- this.touch = {
511
- startDistance: 0,
512
- startZoom: currentState.zoom,
513
- lastTouchX: e.touches[ 0 ].clientX,
514
- lastTouchY: e.touches[ 0 ].clientY,
515
- startPanX: currentState.pan.x,
516
- startPanY: currentState.pan.y,
517
- containerRect,
518
- startMidX: 0,
519
- startMidY: 0,
520
- didPinch: false,
521
- moved: false,
522
- };
523
- this.options.onGestureStart?.();
524
- } else if ( e.touches.length === 2 ) {
525
- // Both fingers landed simultaneously (rare but possible).
526
- const currentState = this.options.getState();
527
- const distance = getTouchDistance( e.touches[ 0 ], e.touches[ 1 ] );
528
- const midX =
529
- ( e.touches[ 0 ].clientX + e.touches[ 1 ].clientX ) / 2;
530
- const midY =
531
- ( e.touches[ 0 ].clientY + e.touches[ 1 ].clientY ) / 2;
532
- this.touch = {
533
- startDistance: distance,
534
- startZoom: currentState.zoom,
535
- lastTouchX: 0,
536
- lastTouchY: 0,
537
- startPanX: currentState.pan.x,
538
- startPanY: currentState.pan.y,
539
- containerRect,
540
- startMidX: midX,
541
- startMidY: midY,
542
- didPinch: true,
543
- moved: false,
544
- };
545
- this.options.onGestureStart?.();
546
- }
547
-
548
- const onTouchEnd = () => {
549
- this.touch = null;
550
- this.touchCleanup = null;
551
- cancelAnimationFrame( this.rafId );
552
- doc.removeEventListener( 'touchmove', this.handleTouchMove );
553
- doc.removeEventListener( 'touchend', onTouchEnd );
554
- doc.removeEventListener( 'touchcancel', onTouchEnd );
555
- this.setStatus( { isDragging: false } );
556
- this.options.onGestureEnd?.();
557
- };
558
-
559
- // Clean up any previous touch listeners before registering new ones.
560
- this.touchCleanup?.();
561
-
562
- doc.addEventListener( 'touchmove', this.handleTouchMove, {
563
- passive: false,
564
- } );
565
- doc.addEventListener( 'touchend', onTouchEnd );
566
- doc.addEventListener( 'touchcancel', onTouchEnd );
567
-
568
- this.touchCleanup = onTouchEnd;
569
- }
570
-
571
- // Handle a touchmove event during an active touch gesture. Decides pinch
572
- // vs. pan based on current `moveEvent.touches.length`, so the gesture
573
- // can transition between modes without timing sensitivity (e.g. fingers
574
- // landing 0ms vs 200ms apart). Arrow-property binding so `this` stays
575
- // attached when used as an event listener.
576
- private handleTouchMove = ( moveEvent: TouchEvent ): void => {
577
- const touch = this.touch;
578
- if ( ! touch ) {
579
- return;
580
- }
581
- moveEvent.preventDefault();
582
-
583
- cancelAnimationFrame( this.rafId );
584
- this.rafId = requestAnimationFrame( () => {
585
- const s = this.options.getState();
586
-
587
- if ( moveEvent.touches.length === 2 ) {
588
- // Two fingers → always pinch, regardless of how gesture
589
- // started. Initialize pinch state on first 2-finger move.
590
- if ( ! touch.didPinch ) {
591
- touch.didPinch = true;
592
- touch.startDistance = getTouchDistance(
593
- moveEvent.touches[ 0 ],
594
- moveEvent.touches[ 1 ]
595
- );
596
- touch.startZoom = s.zoom;
597
- touch.startPanX = s.pan.x;
598
- touch.startPanY = s.pan.y;
599
- touch.startMidX =
600
- ( moveEvent.touches[ 0 ].clientX +
601
- moveEvent.touches[ 1 ].clientX ) /
602
- 2;
603
- touch.startMidY =
604
- ( moveEvent.touches[ 0 ].clientY +
605
- moveEvent.touches[ 1 ].clientY ) /
606
- 2;
607
- this.setStatus( { isDragging: false } );
608
- return;
609
- }
610
- const latestContainerSize = this.options.getContainerSize();
611
- const latestImageSize = this.options.getImageSize();
612
-
613
- // Pinch zoom with focal point at finger midpoint,
614
- // plus simultaneous pan from midpoint drift.
615
- const t0 = moveEvent.touches[ 0 ];
616
- const t1 = moveEvent.touches[ 1 ];
617
- const currentDistance = getTouchDistance( t0, t1 );
618
- const ratio = currentDistance / touch.startDistance;
619
- const newZoom = Math.min(
620
- this.maxZoom,
621
- Math.max( this.minZoom, touch.startZoom * ratio )
622
- );
623
-
624
- const visSize = latestImageSize ?? latestContainerSize;
625
- const rect = touch.containerRect;
626
- if ( visSize.width > 0 && visSize.height > 0 && rect ) {
627
- // Current midpoint.
628
- const mx =
629
- ( t0.clientX + t1.clientX ) / 2 -
630
- rect.left -
631
- latestContainerSize.width / 2;
632
- const my =
633
- ( t0.clientY + t1.clientY ) / 2 -
634
- rect.top -
635
- latestContainerSize.height / 2;
636
-
637
- // Pan from midpoint drift (fingers moving together).
638
- const startMx =
639
- touch.startMidX -
640
- rect.left -
641
- latestContainerSize.width / 2;
642
- const startMy =
643
- touch.startMidY -
644
- rect.top -
645
- latestContainerSize.height / 2;
646
- const panDx =
647
- visSize.width > 0
648
- ? ( mx - startMx ) / visSize.width
649
- : 0;
650
- const panDy =
651
- visSize.height > 0
652
- ? ( my - startMy ) / visSize.height
653
- : 0;
654
-
655
- // Focal-point zoom correction. Keep this based on the
656
- // pinch-start state; using the current reducer state here
657
- // compounds prior touchmove frames and makes a fixed pinch
658
- // center drift after the image is already zoomed or panned.
659
- const zoomRatio =
660
- touch.startZoom !== 0
661
- ? 1 - newZoom / touch.startZoom
662
- : 0;
663
- const startFocalNormX = startMx / visSize.width;
664
- const startFocalNormY = startMy / visSize.height;
665
-
666
- // Combined: midpoint drift + zoom around the pinch-start
667
- // midpoint, so moving both fingers together pans while
668
- // symmetric pinches keep the start center stationary.
669
- const newCropX =
670
- touch.startPanX +
671
- panDx +
672
- ( startFocalNormX - touch.startPanX ) * zoomRatio;
673
- const newCropY =
674
- touch.startPanY +
675
- panDy +
676
- ( startFocalNormY - touch.startPanY ) * zoomRatio;
677
-
678
- const { pan: clampedCrop } = restrictPanZoom(
679
- {
680
- ...s,
681
- zoom: newZoom,
682
- pan: { x: newCropX, y: newCropY },
683
- },
684
- getImageSizeFromState( s ),
685
- s.cropRect
686
- );
687
- this.options.actions.setZoomAtPoint( newZoom, clampedCrop );
688
- } else if ( newZoom !== s.zoom ) {
689
- this.options.actions.setZoom( newZoom );
690
- }
691
- } else if ( moveEvent.touches.length === 1 && ! touch.didPinch ) {
692
- // One finger and no pinch ever detected → pan.
693
- // If fingers went 2→1 (didPinch is true), we do NOT
694
- // switch to pan — avoids accidental pan after releasing
695
- // one finger from a pinch.
696
- if ( ! touch.moved ) {
697
- touch.moved = true;
698
- this.setStatus( { isDragging: true } );
699
- }
700
- const panImageSize = this.options.getImageSize();
701
- const panContainerSize = this.options.getContainerSize();
702
- const panSize = panImageSize ?? panContainerSize;
703
- const deltaX =
704
- panSize.width > 0
705
- ? ( moveEvent.touches[ 0 ].clientX -
706
- touch.lastTouchX ) /
707
- panSize.width
708
- : 0;
709
- const deltaY =
710
- panSize.height > 0
711
- ? ( moveEvent.touches[ 0 ].clientY -
712
- touch.lastTouchY ) /
713
- panSize.height
714
- : 0;
715
-
716
- const { pan: newCrop } = restrictPanZoom(
717
- {
718
- ...s,
719
- pan: {
720
- x: touch.startPanX + deltaX,
721
- y: touch.startPanY + deltaY,
722
- },
723
- },
724
- getImageSizeFromState( s ),
725
- s.cropRect
726
- );
727
-
728
- this.options.actions.setPan( newCrop );
729
- }
730
- } );
731
- };
732
-
733
- // Handle double-tap-to-toggle-zoom. On a second tap within the configured
734
- // time/distance threshold, toggles zoom between `minZoom` and
735
- // `doubleTapZoom` around the tap point and dispatches. Returns `true` if
736
- // the tap was handled as a double-tap (caller should early-return),
737
- // `false` otherwise (caller proceeds with normal single-tap handling).
738
- // Either way, records the tap for next time.
739
- private tryDoubleTap(
740
- e: TouchEvent,
741
- tapX: number,
742
- tapY: number,
743
- containerRect: DOMRect
744
- ): boolean {
745
- const now = Date.now();
746
- const lastTap = this.lastTap;
747
- if ( ! lastTap ) {
748
- this.lastTap = { time: now, x: tapX, y: tapY };
749
- return false;
750
- }
751
- const timeDelta = now - lastTap.time;
752
- const distDelta = Math.sqrt(
753
- ( tapX - lastTap.x ) ** 2 + ( tapY - lastTap.y ) ** 2
754
- );
755
- const isDoubleTap =
756
- timeDelta < DOUBLE_TAP_TIME && distDelta < DOUBLE_TAP_DISTANCE;
757
- if ( ! isDoubleTap ) {
758
- this.lastTap = { time: now, x: tapX, y: tapY };
759
- return false;
760
- }
761
-
762
- // It's a double-tap — suppress browser zoom.
763
- e.preventDefault();
764
- this.lastTap = null;
765
-
766
- const currentState = this.options.getState();
767
- const containerSize = this.options.getContainerSize();
768
- const imgSize = this.options.getImageSize();
769
-
770
- // Toggle: if past halfway to doubleTapZoom, go back to 1x.
771
- const targetZoom =
772
- currentState.zoom > ( this.minZoom + this.doubleTapZoom ) / 2
773
- ? this.minZoom
774
- : this.doubleTapZoom;
775
- const visSize = imgSize ?? containerSize;
776
-
777
- // Enable zoom animation before dispatching.
778
- this.setStatus( { isZooming: true } );
779
- clearTimeout( this.zoomTimer );
780
- this.zoomTimer = setTimeout( () => {
781
- this.setStatus( { isZooming: false } );
782
- }, ZOOM_ANIMATION_DURATION );
783
-
784
- // Double-tap bypasses the normal handleTouchStart gesture path so
785
- // onGestureStart/End are never called — fire them here so undo
786
- // receives a proper boundary around this discrete zoom action.
787
- this.options.onGestureStart?.();
788
-
789
- if ( visSize.width > 0 && visSize.height > 0 ) {
790
- const fx = tapX - containerRect.left - containerSize.width / 2;
791
- const fy = tapY - containerRect.top - containerSize.height / 2;
792
-
793
- const zoomRatio = 1 - targetZoom / currentState.zoom;
794
- const focalNormX = fx / visSize.width;
795
- const focalNormY = fy / visSize.height;
796
- const newCropX =
797
- currentState.pan.x +
798
- ( focalNormX - currentState.pan.x ) * zoomRatio;
799
- const newCropY =
800
- currentState.pan.y +
801
- ( focalNormY - currentState.pan.y ) * zoomRatio;
802
-
803
- const { pan: clampedCrop } = restrictPanZoom(
804
- {
805
- ...currentState,
806
- zoom: targetZoom,
807
- pan: { x: newCropX, y: newCropY },
808
- },
809
- getImageSizeFromState( currentState ),
810
- currentState.cropRect
811
- );
812
- this.options.actions.setZoomAtPoint( targetZoom, clampedCrop );
813
- } else {
814
- this.options.actions.setZoom( targetZoom );
815
- }
816
-
817
- this.options.onGestureEnd?.();
818
- return true;
819
- }
820
-
821
- /**
822
- * Handle a keydown event on the container element.
823
- *
824
- * Supports arrow keys for panning, +/- for zoom, and r/R for rotation.
825
- *
826
- * @param e The native KeyboardEvent.
827
- */
828
- handleKeyDown( e: KeyboardEvent ): void {
829
- const currentState = this.options.getState();
830
-
831
- switch ( e.key ) {
832
- case 'ArrowUp': {
833
- e.preventDefault();
834
- const keyboardStep = this.getKeyboardStep( e );
835
- const { pan: newCrop } = restrictPanZoom(
836
- {
837
- ...currentState,
838
- pan: {
839
- x: currentState.pan.x,
840
- y: currentState.pan.y + keyboardStep,
841
- },
842
- },
843
- getImageSizeFromState( currentState ),
844
- currentState.cropRect
845
- );
846
- this.options.actions.setPan( newCrop );
847
- break;
848
- }
849
- case 'ArrowDown': {
850
- e.preventDefault();
851
- const keyboardStep = this.getKeyboardStep( e );
852
- const { pan: newCrop } = restrictPanZoom(
853
- {
854
- ...currentState,
855
- pan: {
856
- x: currentState.pan.x,
857
- y: currentState.pan.y - keyboardStep,
858
- },
859
- },
860
- getImageSizeFromState( currentState ),
861
- currentState.cropRect
862
- );
863
- this.options.actions.setPan( newCrop );
864
- break;
865
- }
866
- case 'ArrowLeft': {
867
- e.preventDefault();
868
- const keyboardStep = this.getKeyboardStep( e );
869
- const { pan: newCrop } = restrictPanZoom(
870
- {
871
- ...currentState,
872
- pan: {
873
- x: currentState.pan.x + keyboardStep,
874
- y: currentState.pan.y,
875
- },
876
- },
877
- getImageSizeFromState( currentState ),
878
- currentState.cropRect
879
- );
880
- this.options.actions.setPan( newCrop );
881
- break;
882
- }
883
- case 'ArrowRight': {
884
- e.preventDefault();
885
- const keyboardStep = this.getKeyboardStep( e );
886
- const { pan: newCrop } = restrictPanZoom(
887
- {
888
- ...currentState,
889
- pan: {
890
- x: currentState.pan.x - keyboardStep,
891
- y: currentState.pan.y,
892
- },
893
- },
894
- getImageSizeFromState( currentState ),
895
- currentState.cropRect
896
- );
897
- this.options.actions.setPan( newCrop );
898
- break;
899
- }
900
- case '+':
901
- case '=': {
902
- e.preventDefault();
903
- const newZoom = Math.min(
904
- this.maxZoom,
905
- Math.max( this.minZoom, currentState.zoom + 0.5 )
906
- );
907
- this.options.actions.setZoom( newZoom );
908
- break;
909
- }
910
- case '-':
911
- case '_': {
912
- e.preventDefault();
913
- const newZoom = Math.min(
914
- this.maxZoom,
915
- Math.max( this.minZoom, currentState.zoom - 0.5 )
916
- );
917
- this.options.actions.setZoom( newZoom );
918
- break;
919
- }
920
- case 'r':
921
- case 'R': {
922
- if ( e.metaKey || e.ctrlKey || e.altKey ) {
923
- break;
924
- }
925
- e.preventDefault();
926
- this.options.actions.snapRotate90( e.shiftKey ? -1 : 1 );
927
- break;
928
- }
929
- case 'h':
930
- case 'H': {
931
- if ( e.metaKey || e.ctrlKey || e.altKey || e.shiftKey ) {
932
- break;
933
- }
934
- e.preventDefault();
935
- this.options.actions.toggleFlip?.( 'horizontal' );
936
- break;
937
- }
938
- case 'v':
939
- case 'V': {
940
- if ( e.metaKey || e.ctrlKey || e.altKey || e.shiftKey ) {
941
- break;
942
- }
943
- e.preventDefault();
944
- this.options.actions.toggleFlip?.( 'vertical' );
945
- break;
946
- }
947
- }
948
- }
949
-
950
- /**
951
- * Clean up all timers, animation frames, and active touch listeners.
952
- *
953
- * Must be called when the controller is no longer needed (e.g., on
954
- * component unmount or element removal).
955
- */
956
- destroy(): void {
957
- cancelAnimationFrame( this.rafId );
958
- clearTimeout( this.zoomTimer );
959
- clearTimeout( this.wheelGestureTimer );
960
- this.touchCleanup?.();
961
- this.touchCleanup = null;
962
- this.pointerCleanup?.();
963
- this.pointerCleanup = null;
964
- this.drag = null;
965
- this.touch = null;
966
- this.lastTap = null;
967
- // Reset the gesture bookkeeping too. The timers above are cancelled
968
- // rather than run, so these flags would otherwise stay set: a stale
969
- // `wheelGestureActive` suppresses the next wheel gesture's start,
970
- // and `setStatus` dedupes against `isDragging` / `isZooming`, so a
971
- // stale value swallows the next real change.
972
- this.wheelGestureActive = false;
973
- this.isDragging = false;
974
- this.isZooming = false;
975
- }
976
- }