@wordpress/media-editor 0.20.0 → 0.22.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 (386) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +2 -1
  3. package/build/components/media-editor/index.cjs +533 -695
  4. package/build/components/media-editor/index.cjs.map +1 -7
  5. package/build/components/media-editor/use-crop-options.cjs +43 -58
  6. package/build/components/media-editor/use-crop-options.cjs.map +1 -7
  7. package/build/components/media-editor/use-save-media-editor.cjs +111 -185
  8. package/build/components/media-editor/use-save-media-editor.cjs.map +1 -7
  9. package/build/components/media-editor-canvas/index.cjs +122 -124
  10. package/build/components/media-editor-canvas/index.cjs.map +1 -7
  11. package/build/components/media-editor-crop-panel/index.cjs +65 -68
  12. package/build/components/media-editor-crop-panel/index.cjs.map +1 -7
  13. package/build/components/media-editor-fine-rotation/index.cjs +62 -72
  14. package/build/components/media-editor-fine-rotation/index.cjs.map +1 -7
  15. package/build/components/media-editor-image-controls/index.cjs +183 -257
  16. package/build/components/media-editor-image-controls/index.cjs.map +1 -7
  17. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +119 -157
  18. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +1 -7
  19. package/build/components/media-editor-modal/build-modifiers.cjs +107 -88
  20. package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -7
  21. package/build/components/media-editor-modal/index.cjs +112 -153
  22. package/build/components/media-editor-modal/index.cjs.map +1 -7
  23. package/build/components/media-editor-provider/index.cjs +32 -54
  24. package/build/components/media-editor-provider/index.cjs.map +1 -7
  25. package/build/components/media-form/index.cjs +77 -78
  26. package/build/components/media-form/index.cjs.map +1 -7
  27. package/build/components/media-preview/index.cjs +114 -114
  28. package/build/components/media-preview/index.cjs.map +1 -7
  29. package/build/components/rotation-ruler/index.cjs +181 -188
  30. package/build/components/rotation-ruler/index.cjs.map +1 -7
  31. package/build/components/rotation-ruler/use-ruler-drag.cjs +132 -149
  32. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -7
  33. package/build/hooks/use-crop-gesture-handlers.cjs +82 -76
  34. package/build/hooks/use-crop-gesture-handlers.cjs.map +1 -7
  35. package/build/image-editor/core/camera.cjs +257 -205
  36. package/build/image-editor/core/camera.cjs.map +1 -7
  37. package/build/image-editor/core/constants.cjs +127 -102
  38. package/build/image-editor/core/constants.cjs.map +1 -7
  39. package/build/image-editor/core/containment.cjs +275 -262
  40. package/build/image-editor/core/containment.cjs.map +1 -7
  41. package/build/image-editor/core/crop-rect.cjs +33 -44
  42. package/build/image-editor/core/crop-rect.cjs.map +1 -7
  43. package/build/image-editor/core/export/canvas-renderer.cjs +165 -135
  44. package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -7
  45. package/build/image-editor/core/index.cjs +88 -61
  46. package/build/image-editor/core/interaction-controller.cjs +583 -646
  47. package/build/image-editor/core/interaction-controller.cjs.map +1 -7
  48. package/build/image-editor/core/math/rotation.cjs +26 -35
  49. package/build/image-editor/core/math/rotation.cjs.map +1 -7
  50. package/build/image-editor/core/math/sanitize.cjs +124 -80
  51. package/build/image-editor/core/math/sanitize.cjs.map +1 -7
  52. package/build/image-editor/core/setter-helpers.cjs +57 -60
  53. package/build/image-editor/core/setter-helpers.cjs.map +1 -7
  54. package/build/image-editor/core/source-region.cjs +220 -190
  55. package/build/image-editor/core/source-region.cjs.map +1 -7
  56. package/build/image-editor/core/state.cjs +278 -265
  57. package/build/image-editor/core/state.cjs.map +1 -7
  58. package/build/image-editor/core/stencil-math.cjs +193 -206
  59. package/build/image-editor/core/stencil-math.cjs.map +1 -7
  60. package/build/image-editor/core/transform-style.cjs +39 -48
  61. package/build/image-editor/core/transform-style.cjs.map +1 -7
  62. package/build/image-editor/core/transforms/pipeline.cjs +36 -38
  63. package/build/image-editor/core/transforms/pipeline.cjs.map +1 -7
  64. package/build/image-editor/core/types.cjs +4 -18
  65. package/build/image-editor/core/viewport-state.cjs +31 -45
  66. package/build/image-editor/core/viewport-state.cjs.map +1 -7
  67. package/build/image-editor/index.cjs +65 -64
  68. package/build/image-editor/react/components/cropper-provider.cjs +44 -45
  69. package/build/image-editor/react/components/cropper-provider.cjs.map +1 -7
  70. package/build/image-editor/react/components/cropper.cjs +583 -720
  71. package/build/image-editor/react/components/cropper.cjs.map +1 -7
  72. package/build/image-editor/react/components/index.cjs +22 -33
  73. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +135 -160
  74. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -7
  75. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +28 -59
  76. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +1 -7
  77. package/build/image-editor/react/components/overlays/grid-overlay.cjs +58 -88
  78. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +1 -7
  79. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +361 -420
  80. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +1 -7
  81. package/build/image-editor/react/components/viewport-provider.cjs +21 -46
  82. package/build/image-editor/react/components/viewport-provider.cjs.map +1 -7
  83. package/build/image-editor/react/hooks/build-cropper-setters.cjs +76 -67
  84. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -7
  85. package/build/image-editor/react/hooks/index.cjs +16 -31
  86. package/build/image-editor/react/hooks/use-aria-announcer.cjs +90 -146
  87. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +1 -7
  88. package/build/image-editor/react/hooks/use-cropper-controller.cjs +80 -0
  89. package/build/image-editor/react/hooks/use-cropper-controller.cjs.map +1 -0
  90. package/build/image-editor/react/hooks/use-interaction.cjs +179 -205
  91. package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -7
  92. package/build/image-editor/react/hooks/use-transform-style.cjs +20 -34
  93. package/build/image-editor/react/hooks/use-transform-style.cjs.map +1 -7
  94. package/build/image-editor/react/hooks/use-viewport-state.cjs +36 -47
  95. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -7
  96. package/build/image-editor/react/visually-hidden-style.cjs +21 -38
  97. package/build/image-editor/react/visually-hidden-style.cjs.map +1 -7
  98. package/build/index.cjs +45 -39
  99. package/build/lock-unlock.cjs +9 -35
  100. package/build/lock-unlock.cjs.map +1 -7
  101. package/build/private-apis.cjs +16 -37
  102. package/build/private-apis.cjs.map +1 -7
  103. package/build/state/aspect-ratio-presets.cjs +53 -0
  104. package/build/state/aspect-ratio-presets.cjs.map +1 -0
  105. package/build/state/composite-reducer.cjs +136 -89
  106. package/build/state/composite-reducer.cjs.map +1 -7
  107. package/build/state/index.cjs +63 -47
  108. package/build/state/media-editor-state-provider.cjs +35 -48
  109. package/build/state/media-editor-state-provider.cjs.map +1 -7
  110. package/build/state/types.cjs +12 -31
  111. package/build/state/types.cjs.map +1 -7
  112. package/build/state/use-media-editor-state.cjs +261 -281
  113. package/build/state/use-media-editor-state.cjs.map +1 -7
  114. package/build/store/actions.cjs +17 -44
  115. package/build/store/actions.cjs.map +1 -7
  116. package/build/store/constants.cjs +8 -29
  117. package/build/store/constants.cjs.map +1 -7
  118. package/build/store/index.cjs +33 -40
  119. package/build/store/index.cjs.map +1 -7
  120. package/build/store/reducer.cjs +28 -50
  121. package/build/store/reducer.cjs.map +1 -7
  122. package/build/store/selectors.cjs +16 -41
  123. package/build/store/selectors.cjs.map +1 -7
  124. package/build/utils/get-media-type.cjs +18 -41
  125. package/build/utils/get-media-type.cjs.map +1 -7
  126. package/build/utils/index.cjs +9 -28
  127. package/build-module/components/media-editor/index.mjs +490 -682
  128. package/build-module/components/media-editor/index.mjs.map +1 -7
  129. package/build-module/components/media-editor/use-crop-options.mjs +38 -33
  130. package/build-module/components/media-editor/use-crop-options.mjs.map +1 -7
  131. package/build-module/components/media-editor/use-save-media-editor.mjs +89 -157
  132. package/build-module/components/media-editor/use-save-media-editor.mjs.map +1 -7
  133. package/build-module/components/media-editor-canvas/index.mjs +97 -95
  134. package/build-module/components/media-editor-canvas/index.mjs.map +1 -7
  135. package/build-module/components/media-editor-crop-panel/index.mjs +44 -43
  136. package/build-module/components/media-editor-crop-panel/index.mjs.map +1 -7
  137. package/build-module/components/media-editor-fine-rotation/index.mjs +39 -45
  138. package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -7
  139. package/build-module/components/media-editor-image-controls/index.mjs +172 -237
  140. package/build-module/components/media-editor-image-controls/index.mjs.map +1 -7
  141. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +112 -137
  142. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +1 -7
  143. package/build-module/components/media-editor-modal/build-modifiers.mjs +103 -64
  144. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +1 -7
  145. package/build-module/components/media-editor-modal/index.mjs +88 -122
  146. package/build-module/components/media-editor-modal/index.mjs.map +1 -7
  147. package/build-module/components/media-editor-provider/index.mjs +26 -28
  148. package/build-module/components/media-editor-provider/index.mjs.map +1 -7
  149. package/build-module/components/media-form/index.mjs +69 -54
  150. package/build-module/components/media-form/index.mjs.map +1 -7
  151. package/build-module/components/media-preview/index.mjs +105 -89
  152. package/build-module/components/media-preview/index.mjs.map +1 -7
  153. package/build-module/components/rotation-ruler/index.mjs +161 -164
  154. package/build-module/components/rotation-ruler/index.mjs.map +1 -7
  155. package/build-module/components/rotation-ruler/use-ruler-drag.mjs +125 -122
  156. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -7
  157. package/build-module/hooks/use-crop-gesture-handlers.mjs +76 -50
  158. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +1 -7
  159. package/build-module/image-editor/core/camera.mjs +245 -178
  160. package/build-module/image-editor/core/camera.mjs.map +1 -7
  161. package/build-module/image-editor/core/constants.mjs +110 -64
  162. package/build-module/image-editor/core/constants.mjs.map +1 -7
  163. package/build-module/image-editor/core/containment.mjs +265 -237
  164. package/build-module/image-editor/core/containment.mjs.map +1 -7
  165. package/build-module/image-editor/core/crop-rect.mjs +29 -20
  166. package/build-module/image-editor/core/crop-rect.mjs.map +1 -7
  167. package/build-module/image-editor/core/export/canvas-renderer.mjs +155 -105
  168. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -7
  169. package/build-module/image-editor/core/index.mjs +5 -34
  170. package/build-module/image-editor/core/interaction-controller.mjs +579 -628
  171. package/build-module/image-editor/core/interaction-controller.mjs.map +1 -7
  172. package/build-module/image-editor/core/math/rotation.mjs +21 -10
  173. package/build-module/image-editor/core/math/rotation.mjs.map +1 -7
  174. package/build-module/image-editor/core/math/sanitize.mjs +116 -52
  175. package/build-module/image-editor/core/math/sanitize.mjs.map +1 -7
  176. package/build-module/image-editor/core/setter-helpers.mjs +52 -35
  177. package/build-module/image-editor/core/setter-helpers.mjs.map +1 -7
  178. package/build-module/image-editor/core/source-region.mjs +210 -160
  179. package/build-module/image-editor/core/source-region.mjs.map +1 -7
  180. package/build-module/image-editor/core/state.mjs +271 -243
  181. package/build-module/image-editor/core/state.mjs.map +1 -7
  182. package/build-module/image-editor/core/stencil-math.mjs +185 -178
  183. package/build-module/image-editor/core/stencil-math.mjs.map +1 -7
  184. package/build-module/image-editor/core/transform-style.mjs +34 -23
  185. package/build-module/image-editor/core/transform-style.mjs.map +1 -7
  186. package/build-module/image-editor/core/transforms/pipeline.mjs +29 -11
  187. package/build-module/image-editor/core/transforms/pipeline.mjs.map +1 -7
  188. package/build-module/image-editor/core/types.mjs +0 -1
  189. package/build-module/image-editor/core/viewport-state.mjs +26 -20
  190. package/build-module/image-editor/core/viewport-state.mjs.map +1 -7
  191. package/build-module/image-editor/index.mjs +3 -38
  192. package/build-module/image-editor/react/components/cropper-provider.mjs +38 -21
  193. package/build-module/image-editor/react/components/cropper-provider.mjs.map +1 -7
  194. package/build-module/image-editor/react/components/cropper.mjs +550 -696
  195. package/build-module/image-editor/react/components/cropper.mjs.map +1 -7
  196. package/build-module/image-editor/react/components/index.mjs +1 -7
  197. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +130 -135
  198. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -7
  199. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +24 -35
  200. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +1 -7
  201. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +54 -64
  202. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +1 -7
  203. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +355 -407
  204. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +1 -7
  205. package/build-module/image-editor/react/components/viewport-provider.mjs +15 -22
  206. package/build-module/image-editor/react/components/viewport-provider.mjs.map +1 -7
  207. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +72 -43
  208. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -7
  209. package/build-module/image-editor/react/hooks/index.mjs +2 -7
  210. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +83 -119
  211. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +1 -7
  212. package/build-module/image-editor/react/hooks/use-cropper-controller.mjs +75 -0
  213. package/build-module/image-editor/react/hooks/use-cropper-controller.mjs.map +1 -0
  214. package/build-module/image-editor/react/hooks/use-interaction.mjs +175 -183
  215. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +1 -7
  216. package/build-module/image-editor/react/hooks/use-transform-style.mjs +15 -9
  217. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +1 -7
  218. package/build-module/image-editor/react/hooks/use-viewport-state.mjs +32 -26
  219. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -7
  220. package/build-module/image-editor/react/visually-hidden-style.mjs +17 -14
  221. package/build-module/image-editor/react/visually-hidden-style.mjs.map +1 -7
  222. package/build-module/index.mjs +3 -10
  223. package/build-module/lock-unlock.mjs +5 -10
  224. package/build-module/lock-unlock.mjs.map +1 -7
  225. package/build-module/private-apis.mjs +8 -9
  226. package/build-module/private-apis.mjs.map +1 -7
  227. package/build-module/state/aspect-ratio-presets.mjs +48 -0
  228. package/build-module/state/aspect-ratio-presets.mjs.map +1 -0
  229. package/build-module/state/composite-reducer.mjs +114 -75
  230. package/build-module/state/composite-reducer.mjs.map +1 -7
  231. package/build-module/state/index.mjs +5 -25
  232. package/build-module/state/media-editor-state-provider.mjs +29 -24
  233. package/build-module/state/media-editor-state-provider.mjs.map +1 -7
  234. package/build-module/state/types.mjs +8 -7
  235. package/build-module/state/types.mjs.map +1 -7
  236. package/build-module/state/use-media-editor-state.mjs +256 -273
  237. package/build-module/state/use-media-editor-state.mjs.map +1 -7
  238. package/build-module/store/actions.mjs +12 -19
  239. package/build-module/store/actions.mjs.map +1 -7
  240. package/build-module/store/constants.mjs +4 -5
  241. package/build-module/store/constants.mjs.map +1 -7
  242. package/build-module/store/index.mjs +8 -9
  243. package/build-module/store/index.mjs.map +1 -7
  244. package/build-module/store/reducer.mjs +23 -26
  245. package/build-module/store/reducer.mjs.map +1 -7
  246. package/build-module/store/selectors.mjs +8 -13
  247. package/build-module/store/selectors.mjs.map +1 -7
  248. package/build-module/utils/get-media-type.mjs +14 -17
  249. package/build-module/utils/get-media-type.mjs.map +1 -7
  250. package/build-module/utils/index.mjs +1 -6
  251. package/build-style/style-rtl.css +41 -4
  252. package/build-style/style.css +41 -4
  253. package/build-types/components/media-editor/index.d.ts +6 -1
  254. package/build-types/components/media-editor/index.d.ts.map +1 -1
  255. package/build-types/components/media-editor/use-crop-options.d.ts +0 -1
  256. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
  257. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
  258. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  259. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  260. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  261. package/build-types/components/media-editor-modal/build-modifiers.d.ts +6 -5
  262. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -1
  263. package/build-types/components/media-editor-modal/index.d.ts +7 -2
  264. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  265. package/build-types/components/media-editor-provider/index.d.ts +7 -1
  266. package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
  267. package/build-types/components/media-form/index.d.ts +5 -2
  268. package/build-types/components/media-form/index.d.ts.map +1 -1
  269. package/build-types/components/media-preview/index.d.ts.map +1 -1
  270. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
  271. package/build-types/image-editor/core/constants.d.ts +0 -1
  272. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  273. package/build-types/image-editor/core/index.d.ts +1 -1
  274. package/build-types/image-editor/core/index.d.ts.map +1 -1
  275. package/build-types/image-editor/core/setter-helpers.d.ts +1 -1
  276. package/build-types/image-editor/core/types.d.ts +1 -1
  277. package/build-types/image-editor/index.d.ts +7 -6
  278. package/build-types/image-editor/index.d.ts.map +1 -1
  279. package/build-types/image-editor/react/components/cropper-provider.d.ts +2 -2
  280. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
  281. package/build-types/image-editor/react/components/cropper.d.ts +5 -14
  282. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  283. package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +2 -2
  284. package/build-types/image-editor/react/hooks/index.d.ts +2 -2
  285. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
  286. package/build-types/image-editor/react/hooks/{use-cropper-reducer.d.ts → use-cropper-controller.d.ts} +12 -12
  287. package/build-types/image-editor/react/hooks/use-cropper-controller.d.ts.map +1 -0
  288. package/build-types/state/aspect-ratio-presets.d.ts +7 -0
  289. package/build-types/state/aspect-ratio-presets.d.ts.map +1 -0
  290. package/build-types/state/composite-reducer.d.ts +2 -2
  291. package/build-types/state/composite-reducer.d.ts.map +1 -1
  292. package/build-types/state/index.d.ts +2 -1
  293. package/build-types/state/index.d.ts.map +1 -1
  294. package/build-types/state/types.d.ts +36 -6
  295. package/build-types/state/types.d.ts.map +1 -1
  296. package/build-types/state/use-media-editor-state.d.ts +16 -5
  297. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  298. package/package.json +35 -24
  299. package/src/components/media-editor/index.tsx +226 -183
  300. package/src/components/media-editor/style.scss +19 -0
  301. package/src/components/media-editor/use-crop-options.ts +5 -4
  302. package/src/components/media-editor/use-save-media-editor.ts +2 -6
  303. package/src/components/media-editor-canvas/index.tsx +18 -28
  304. package/src/components/media-editor-crop-panel/index.tsx +17 -9
  305. package/src/components/media-editor-image-controls/index.tsx +4 -2
  306. package/src/components/media-editor-image-controls/style.scss +1 -1
  307. package/src/components/media-editor-modal/build-modifiers.ts +13 -7
  308. package/src/components/media-editor-modal/index.tsx +8 -1
  309. package/src/components/media-editor-modal/style.scss +1 -1
  310. package/src/components/media-editor-provider/index.tsx +8 -1
  311. package/src/components/media-form/index.tsx +48 -42
  312. package/src/components/media-preview/index.tsx +68 -25
  313. package/src/components/media-preview/style.scss +16 -2
  314. package/src/components/rotation-ruler/style.scss +6 -2
  315. package/src/components/rotation-ruler/use-ruler-drag.ts +6 -11
  316. package/src/image-editor/README.md +8 -8
  317. package/src/image-editor/core/constants.ts +0 -13
  318. package/src/image-editor/core/index.ts +1 -5
  319. package/src/image-editor/core/setter-helpers.ts +1 -1
  320. package/src/image-editor/core/types.ts +1 -1
  321. package/src/image-editor/docs/architecture.md +5 -1
  322. package/src/image-editor/docs/recipes.md +3 -3
  323. package/src/image-editor/index.ts +7 -16
  324. package/src/image-editor/react/components/cropper-provider.tsx +5 -5
  325. package/src/image-editor/react/components/cropper.tsx +20 -42
  326. package/src/image-editor/react/hooks/build-cropper-setters.ts +2 -2
  327. package/src/image-editor/react/hooks/index.ts +2 -2
  328. package/src/image-editor/react/hooks/{use-cropper-reducer.ts → use-cropper-controller.ts} +11 -11
  329. package/src/state/aspect-ratio-presets.ts +18 -0
  330. package/src/state/composite-reducer.ts +26 -13
  331. package/src/state/index.ts +3 -0
  332. package/src/state/types.ts +39 -11
  333. package/src/state/use-media-editor-state.ts +53 -16
  334. package/build/image-editor/core/index.cjs.map +0 -7
  335. package/build/image-editor/core/types.cjs.map +0 -7
  336. package/build/image-editor/index.cjs.map +0 -7
  337. package/build/image-editor/react/components/index.cjs.map +0 -7
  338. package/build/image-editor/react/hooks/index.cjs.map +0 -7
  339. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +0 -113
  340. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +0 -7
  341. package/build/index.cjs.map +0 -7
  342. package/build/state/index.cjs.map +0 -7
  343. package/build/utils/index.cjs.map +0 -7
  344. package/build-module/image-editor/core/index.mjs.map +0 -7
  345. package/build-module/image-editor/core/types.mjs.map +0 -7
  346. package/build-module/image-editor/index.mjs.map +0 -7
  347. package/build-module/image-editor/react/components/index.mjs.map +0 -7
  348. package/build-module/image-editor/react/hooks/index.mjs.map +0 -7
  349. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +0 -100
  350. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +0 -7
  351. package/build-module/index.mjs.map +0 -7
  352. package/build-module/state/index.mjs.map +0 -7
  353. package/build-module/utils/index.mjs.map +0 -7
  354. package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +0 -1
  355. package/global.d.ts +0 -5
  356. package/src/components/media-editor/test/restore-original.browser.test.tsx +0 -227
  357. package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -106
  358. package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -72
  359. package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -186
  360. package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -359
  361. package/src/components/media-editor-modal/test/index.jsdom.test.tsx +0 -182
  362. package/src/components/rotation-ruler/stories/index.story.tsx +0 -43
  363. package/src/components/rotation-ruler/test/index.jsdom.test.tsx +0 -140
  364. package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +0 -823
  365. package/src/image-editor/core/math/test/sanitize.ts +0 -242
  366. package/src/image-editor/core/test/camera.ts +0 -1218
  367. package/src/image-editor/core/test/interaction-controller.ts +0 -1299
  368. package/src/image-editor/core/test/preview-export-parity.ts +0 -249
  369. package/src/image-editor/core/test/render-camera-bridge.ts +0 -349
  370. package/src/image-editor/core/test/state.ts +0 -876
  371. package/src/image-editor/core/test/stencil-math.ts +0 -665
  372. package/src/image-editor/core/transforms/test/pipeline.ts +0 -267
  373. package/src/image-editor/react/components/stencils/test/rectangle-stencil.browser.test.tsx +0 -438
  374. package/src/image-editor/react/components/test/cropper.browser.test.tsx +0 -1462
  375. package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +0 -491
  376. package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +0 -1049
  377. package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +0 -213
  378. package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +0 -140
  379. package/src/image-editor/stories/rectangle-crop.story.tsx +0 -1073
  380. package/src/image-editor/stories/style.module.css +0 -65
  381. package/src/state/test/use-media-editor-state.browser.test.tsx +0 -79
  382. package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -386
  383. package/src/store/test/reducer.ts +0 -64
  384. package/tsconfig.build.json +0 -26
  385. package/tsconfig.build.tsbuildinfo +0 -1
  386. package/tsconfig.json +0 -37
@@ -1,635 +1,586 @@
1
- // packages/media-editor/src/image-editor/core/interaction-controller.ts
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.mjs";
1
+ import { DEFAULT_KEYBOARD_STEP, DEFAULT_WHEEL_ZOOM_SPEED, KEYBOARD_SHIFT_STEP_MULTIPLIER, MAX_ZOOM, MIN_ZOOM } from "./constants.mjs";
9
2
  import { restrictPanZoom } from "./containment.mjs";
10
- var DOUBLE_TAP_TIME = 300;
11
- var DOUBLE_TAP_DISTANCE = 30;
12
- var ZOOM_ANIMATION_DURATION = 200;
3
+ // packages/media-editor/src/image-editor/core/interaction-controller.ts
4
+ /** Time window for detecting a double-tap gesture (ms). */
5
+ const DOUBLE_TAP_TIME = 300;
6
+ /** Max distance between taps to count as a double-tap (px). */
7
+ const DOUBLE_TAP_DISTANCE = 30;
8
+ /** Duration of the zoom animation state (ms). */
9
+ const ZOOM_ANIMATION_DURATION = 200;
10
+ /**
11
+ * Get the natural image dimensions from cropper state, falling back to 1x1.
12
+ *
13
+ * @param state The current cropper state.
14
+ * @return The image dimensions.
15
+ */
13
16
  function getImageSizeFromState(state) {
14
- return state.image ? { width: state.image.naturalWidth, height: state.image.naturalHeight } : { width: 1, height: 1 };
17
+ return state.image ? {
18
+ width: state.image.naturalWidth,
19
+ height: state.image.naturalHeight
20
+ } : {
21
+ width: 1,
22
+ height: 1
23
+ };
15
24
  }
25
+ /**
26
+ * Get the distance between two touch points.
27
+ *
28
+ * @param t1 The first touch.
29
+ * @param t2 The second touch.
30
+ * @return The pixel distance between the two touches.
31
+ */
16
32
  function getTouchDistance(t1, t2) {
17
- const dx = t1.clientX - t2.clientX;
18
- const dy = t1.clientY - t2.clientY;
19
- return Math.sqrt(dx * dx + dy * dy);
33
+ const dx = t1.clientX - t2.clientX;
34
+ const dy = t1.clientY - t2.clientY;
35
+ return Math.sqrt(dx * dx + dy * dy);
20
36
  }
37
+ /**
38
+ * Framework-agnostic imperative controller for image cropper interactions.
39
+ *
40
+ * Handles mouse, touch, and keyboard event processing for pan, zoom,
41
+ * and crop manipulation. Uses requestAnimationFrame for drag/pinch
42
+ * updates to avoid layout thrashing.
43
+ *
44
+ * The controller does not register DOM event listeners itself — that is the
45
+ * responsibility of the UI layer (React hook, Vue directive, or vanilla JS).
46
+ * Instead it exposes `handlePointerDown`, `handleWheel`, `handleTouchStart`,
47
+ * and `handleKeyDown` methods that the UI layer calls with native DOM events.
48
+ *
49
+ * Scalar options (minZoom, maxZoom, zoomSpeed, keyboardStep, doubleTapZoom)
50
+ * are read lazily from `this.options` on each interaction, so the UI layer
51
+ * can update them without recreating the controller.
52
+ *
53
+ * Call `destroy()` to clean up timers and pending animation frames.
54
+ */
21
55
  var InteractionController = class {
22
- options;
23
- /** Current drag/zoom status. */
24
- isDragging = false;
25
- isZooming = false;
26
- /** Active drag state during pointer interactions. */
27
- drag = null;
28
- /** Active touch state during touch interactions. */
29
- touch = null;
30
- /** Cleanup function for active touch listeners on document. */
31
- touchCleanup = null;
32
- /**
33
- * Cleanup function for active pointer-drag listeners on the
34
- * captured element. Set while a pointer drag is in progress so
35
- * `destroy()` can tear the drag down cleanly if the consumer
36
- * unmounts mid-drag. Cleared on pointerup/lostpointercapture.
37
- */
38
- pointerCleanup = null;
39
- /** Last tap info for double-tap detection. */
40
- lastTap = null;
41
- /** Timer for the zoom animation state (double-tap). */
42
- zoomTimer;
43
- /** Timer for wheel gesture debounce. */
44
- wheelGestureTimer;
45
- /** Whether a wheel gesture is currently active. */
46
- wheelGestureActive = false;
47
- /** Current requestAnimationFrame ID. */
48
- rafId = 0;
49
- constructor(options) {
50
- this.options = options;
51
- }
52
- /** Read minZoom lazily so option changes take effect immediately. */
53
- get minZoom() {
54
- return this.options.minZoom ?? MIN_ZOOM;
55
- }
56
- /** Read maxZoom lazily so option changes take effect immediately. */
57
- get maxZoom() {
58
- return this.options.maxZoom ?? MAX_ZOOM;
59
- }
60
- /** Read zoomSpeed lazily so option changes take effect immediately. */
61
- get zoomSpeed() {
62
- return this.options.zoomSpeed ?? DEFAULT_WHEEL_ZOOM_SPEED;
63
- }
64
- /** Read keyboardStep lazily so option changes take effect immediately. */
65
- get keyboardStep() {
66
- return this.options.keyboardStep ?? DEFAULT_KEYBOARD_STEP;
67
- }
68
- /**
69
- * Get the keyboard pan step, including modifier-based coarse movement.
70
- *
71
- * @param event The native KeyboardEvent.
72
- * @return The normalized pan step.
73
- */
74
- getKeyboardStep(event) {
75
- return event.shiftKey ? this.keyboardStep * KEYBOARD_SHIFT_STEP_MULTIPLIER : this.keyboardStep;
76
- }
77
- /** Read doubleTapZoom lazily so option changes take effect immediately. */
78
- get doubleTapZoom() {
79
- return this.options.doubleTapZoom ?? 2;
80
- }
81
- /**
82
- * Update the drag/zoom status and notify via callback if changed.
83
- *
84
- * @param update Partial status with isDragging and/or isZooming.
85
- */
86
- setStatus(update) {
87
- let changed = false;
88
- if (update.isDragging !== void 0 && update.isDragging !== this.isDragging) {
89
- this.isDragging = update.isDragging;
90
- changed = true;
91
- }
92
- if (update.isZooming !== void 0 && update.isZooming !== this.isZooming) {
93
- this.isZooming = update.isZooming;
94
- changed = true;
95
- }
96
- if (changed) {
97
- this.options.onStatusChange?.({
98
- isDragging: this.isDragging,
99
- isZooming: this.isZooming
100
- });
101
- }
102
- }
103
- /**
104
- * Handle a pointer-down event on the container element.
105
- *
106
- * Initiates a drag (pan) interaction. Captures the pointer on the
107
- * provided element and registers pointermove/pointerup/lostpointercapture
108
- * listeners for the duration of the drag.
109
- *
110
- * @param e The native PointerEvent.
111
- * @param el The DOM element to capture the pointer on.
112
- */
113
- handlePointerDown(e, el) {
114
- if (e.button !== 0) {
115
- return;
116
- }
117
- if (e.pointerType === "touch") {
118
- return;
119
- }
120
- e.preventDefault();
121
- const ownerDoc = el.ownerDocument;
122
- if (ownerDoc?.activeElement instanceof HTMLElement && ownerDoc.activeElement !== el) {
123
- ownerDoc.activeElement.blur();
124
- }
125
- el.focus();
126
- el.setPointerCapture(e.pointerId);
127
- if (this.wheelGestureActive) {
128
- clearTimeout(this.wheelGestureTimer);
129
- this.wheelGestureActive = false;
130
- this.options.onGestureEnd?.();
131
- }
132
- this.setStatus({ isDragging: true });
133
- this.options.onGestureStart?.();
134
- const currentState = this.options.getState();
135
- this.drag = {
136
- startX: e.clientX,
137
- startY: e.clientY,
138
- startPanX: currentState.pan.x,
139
- startPanY: currentState.pan.y
140
- };
141
- const onPointerMove = (moveEvent) => {
142
- const drag = this.drag;
143
- if (!drag) {
144
- return;
145
- }
146
- const pe = moveEvent;
147
- cancelAnimationFrame(this.rafId);
148
- this.rafId = requestAnimationFrame(() => {
149
- const s = this.options.getState();
150
- const imgSize = this.options.getImageSize();
151
- const containerSize = this.options.getContainerSize();
152
- const panSize = imgSize ?? containerSize;
153
- const deltaX = panSize.width > 0 ? (pe.clientX - drag.startX) / panSize.width : 0;
154
- const deltaY = panSize.height > 0 ? (pe.clientY - drag.startY) / panSize.height : 0;
155
- const { pan: newCrop } = restrictPanZoom(
156
- {
157
- ...s,
158
- pan: {
159
- x: drag.startPanX + deltaX,
160
- y: drag.startPanY + deltaY
161
- }
162
- },
163
- getImageSizeFromState(s),
164
- s.cropRect
165
- );
166
- this.options.actions.setPan(newCrop);
167
- });
168
- };
169
- const removeListeners = () => {
170
- el.removeEventListener("pointermove", onPointerMove);
171
- el.removeEventListener("pointerup", onPointerUp);
172
- el.removeEventListener("lostpointercapture", onPointerUp);
173
- };
174
- const onPointerUp = () => {
175
- this.setStatus({ isDragging: false });
176
- this.options.onGestureEnd?.();
177
- this.drag = null;
178
- cancelAnimationFrame(this.rafId);
179
- removeListeners();
180
- this.pointerCleanup = null;
181
- };
182
- el.addEventListener("pointermove", onPointerMove);
183
- el.addEventListener("pointerup", onPointerUp);
184
- el.addEventListener("lostpointercapture", onPointerUp);
185
- this.pointerCleanup = removeListeners;
186
- }
187
- /**
188
- * Handle a wheel event on the container element.
189
- *
190
- * Implements focal-point zoom: the point under the cursor stays
191
- * stationary on screen while the zoom level changes.
192
- *
193
- * Must be registered with `{ passive: false }` to allow preventDefault.
194
- *
195
- * @param e The native WheelEvent.
196
- */
197
- handleWheel(e) {
198
- e.preventDefault();
199
- if (this.drag) {
200
- return;
201
- }
202
- if (!this.wheelGestureActive) {
203
- this.wheelGestureActive = true;
204
- this.options.onGestureStart?.();
205
- }
206
- clearTimeout(this.wheelGestureTimer);
207
- this.wheelGestureTimer = setTimeout(() => {
208
- this.wheelGestureActive = false;
209
- this.options.onGestureEnd?.();
210
- }, DOUBLE_TAP_TIME);
211
- const s = this.options.getState();
212
- const delta = -e.deltaY * this.zoomSpeed;
213
- const newZoom = Math.min(
214
- this.maxZoom,
215
- Math.max(this.minZoom, s.zoom + delta)
216
- );
217
- if (newZoom === s.zoom) {
218
- return;
219
- }
220
- const containerSize = this.options.getContainerSize();
221
- const imgSize = this.options.getImageSize();
222
- const visSize = imgSize ?? containerSize;
223
- const target = e.currentTarget;
224
- const rect = target instanceof Element ? target.getBoundingClientRect() : void 0;
225
- if (visSize.width > 0 && visSize.height > 0 && rect) {
226
- const fx = e.clientX - rect.left - containerSize.width / 2;
227
- const fy = e.clientY - rect.top - containerSize.height / 2;
228
- const zoomRatio = 1 - newZoom / s.zoom;
229
- const focalNormX = fx / visSize.width;
230
- const focalNormY = fy / visSize.height;
231
- const newCropX = s.pan.x + (focalNormX - s.pan.x) * zoomRatio;
232
- const newCropY = s.pan.y + (focalNormY - s.pan.y) * zoomRatio;
233
- const { pan: clampedCrop } = restrictPanZoom(
234
- { ...s, zoom: newZoom, pan: { x: newCropX, y: newCropY } },
235
- getImageSizeFromState(s),
236
- s.cropRect
237
- );
238
- this.options.actions.setZoomAtPoint(newZoom, clampedCrop);
239
- } else {
240
- this.options.actions.setZoom(newZoom);
241
- }
242
- }
243
- /**
244
- * Handle a touch-start event on the container element.
245
- *
246
- * Supports single-finger pan, two-finger pinch zoom, and
247
- * double-tap to toggle between fit and 2x zoom. Registers
248
- * touchmove/touchend/touchcancel on the provided document for the
249
- * duration of the gesture.
250
- *
251
- * @param e The native TouchEvent.
252
- * @param containerRect The bounding rect of the container element.
253
- * @param doc The document to register move/end listeners on.
254
- * Defaults to globalThis.document. Pass the iframe's
255
- * contentDocument when running inside an iframe.
256
- */
257
- handleTouchStart(e, containerRect, doc = document) {
258
- if (this.touch && e.touches.length === 2) {
259
- const s = this.options.getState();
260
- const distance = getTouchDistance(e.touches[0], e.touches[1]);
261
- const midX = (e.touches[0].clientX + e.touches[1].clientX) / 2;
262
- const midY = (e.touches[0].clientY + e.touches[1].clientY) / 2;
263
- this.touch.didPinch = true;
264
- this.touch.startDistance = distance;
265
- this.touch.startZoom = s.zoom;
266
- this.touch.startPanX = s.pan.x;
267
- this.touch.startPanY = s.pan.y;
268
- this.touch.startMidX = midX;
269
- this.touch.startMidY = midY;
270
- this.touch.containerRect = containerRect;
271
- this.setStatus({ isDragging: false });
272
- return;
273
- }
274
- if (e.touches.length === 1) {
275
- const tapX = e.touches[0].clientX;
276
- const tapY = e.touches[0].clientY;
277
- if (this.tryDoubleTap(e, tapX, tapY, containerRect)) {
278
- return;
279
- }
280
- const currentState = this.options.getState();
281
- this.touch = {
282
- startDistance: 0,
283
- startZoom: currentState.zoom,
284
- lastTouchX: e.touches[0].clientX,
285
- lastTouchY: e.touches[0].clientY,
286
- startPanX: currentState.pan.x,
287
- startPanY: currentState.pan.y,
288
- containerRect,
289
- startMidX: 0,
290
- startMidY: 0,
291
- didPinch: false,
292
- moved: false
293
- };
294
- this.options.onGestureStart?.();
295
- } else if (e.touches.length === 2) {
296
- const currentState = this.options.getState();
297
- const distance = getTouchDistance(e.touches[0], e.touches[1]);
298
- const midX = (e.touches[0].clientX + e.touches[1].clientX) / 2;
299
- const midY = (e.touches[0].clientY + e.touches[1].clientY) / 2;
300
- this.touch = {
301
- startDistance: distance,
302
- startZoom: currentState.zoom,
303
- lastTouchX: 0,
304
- lastTouchY: 0,
305
- startPanX: currentState.pan.x,
306
- startPanY: currentState.pan.y,
307
- containerRect,
308
- startMidX: midX,
309
- startMidY: midY,
310
- didPinch: true,
311
- moved: false
312
- };
313
- this.options.onGestureStart?.();
314
- }
315
- const onTouchEnd = () => {
316
- this.touch = null;
317
- this.touchCleanup = null;
318
- cancelAnimationFrame(this.rafId);
319
- doc.removeEventListener("touchmove", this.handleTouchMove);
320
- doc.removeEventListener("touchend", onTouchEnd);
321
- doc.removeEventListener("touchcancel", onTouchEnd);
322
- this.setStatus({ isDragging: false });
323
- this.options.onGestureEnd?.();
324
- };
325
- this.touchCleanup?.();
326
- doc.addEventListener("touchmove", this.handleTouchMove, {
327
- passive: false
328
- });
329
- doc.addEventListener("touchend", onTouchEnd);
330
- doc.addEventListener("touchcancel", onTouchEnd);
331
- this.touchCleanup = onTouchEnd;
332
- }
333
- // Handle a touchmove event during an active touch gesture. Decides pinch
334
- // vs. pan based on current `moveEvent.touches.length`, so the gesture
335
- // can transition between modes without timing sensitivity (e.g. fingers
336
- // landing 0ms vs 200ms apart). Arrow-property binding so `this` stays
337
- // attached when used as an event listener.
338
- handleTouchMove = (moveEvent) => {
339
- const touch = this.touch;
340
- if (!touch) {
341
- return;
342
- }
343
- moveEvent.preventDefault();
344
- cancelAnimationFrame(this.rafId);
345
- this.rafId = requestAnimationFrame(() => {
346
- const s = this.options.getState();
347
- if (moveEvent.touches.length === 2) {
348
- if (!touch.didPinch) {
349
- touch.didPinch = true;
350
- touch.startDistance = getTouchDistance(
351
- moveEvent.touches[0],
352
- moveEvent.touches[1]
353
- );
354
- touch.startZoom = s.zoom;
355
- touch.startPanX = s.pan.x;
356
- touch.startPanY = s.pan.y;
357
- touch.startMidX = (moveEvent.touches[0].clientX + moveEvent.touches[1].clientX) / 2;
358
- touch.startMidY = (moveEvent.touches[0].clientY + moveEvent.touches[1].clientY) / 2;
359
- this.setStatus({ isDragging: false });
360
- return;
361
- }
362
- const latestContainerSize = this.options.getContainerSize();
363
- const latestImageSize = this.options.getImageSize();
364
- const t0 = moveEvent.touches[0];
365
- const t1 = moveEvent.touches[1];
366
- const currentDistance = getTouchDistance(t0, t1);
367
- const ratio = currentDistance / touch.startDistance;
368
- const newZoom = Math.min(
369
- this.maxZoom,
370
- Math.max(this.minZoom, touch.startZoom * ratio)
371
- );
372
- const visSize = latestImageSize ?? latestContainerSize;
373
- const rect = touch.containerRect;
374
- if (visSize.width > 0 && visSize.height > 0 && rect) {
375
- const mx = (t0.clientX + t1.clientX) / 2 - rect.left - latestContainerSize.width / 2;
376
- const my = (t0.clientY + t1.clientY) / 2 - rect.top - latestContainerSize.height / 2;
377
- const startMx = touch.startMidX - rect.left - latestContainerSize.width / 2;
378
- const startMy = touch.startMidY - rect.top - latestContainerSize.height / 2;
379
- const panDx = visSize.width > 0 ? (mx - startMx) / visSize.width : 0;
380
- const panDy = visSize.height > 0 ? (my - startMy) / visSize.height : 0;
381
- const zoomRatio = touch.startZoom !== 0 ? 1 - newZoom / touch.startZoom : 0;
382
- const startFocalNormX = startMx / visSize.width;
383
- const startFocalNormY = startMy / visSize.height;
384
- const newCropX = touch.startPanX + panDx + (startFocalNormX - touch.startPanX) * zoomRatio;
385
- const newCropY = touch.startPanY + panDy + (startFocalNormY - touch.startPanY) * zoomRatio;
386
- const { pan: clampedCrop } = restrictPanZoom(
387
- {
388
- ...s,
389
- zoom: newZoom,
390
- pan: { x: newCropX, y: newCropY }
391
- },
392
- getImageSizeFromState(s),
393
- s.cropRect
394
- );
395
- this.options.actions.setZoomAtPoint(newZoom, clampedCrop);
396
- } else if (newZoom !== s.zoom) {
397
- this.options.actions.setZoom(newZoom);
398
- }
399
- } else if (moveEvent.touches.length === 1 && !touch.didPinch) {
400
- if (!touch.moved) {
401
- touch.moved = true;
402
- this.setStatus({ isDragging: true });
403
- }
404
- const panImageSize = this.options.getImageSize();
405
- const panContainerSize = this.options.getContainerSize();
406
- const panSize = panImageSize ?? panContainerSize;
407
- const deltaX = panSize.width > 0 ? (moveEvent.touches[0].clientX - touch.lastTouchX) / panSize.width : 0;
408
- const deltaY = panSize.height > 0 ? (moveEvent.touches[0].clientY - touch.lastTouchY) / panSize.height : 0;
409
- const { pan: newCrop } = restrictPanZoom(
410
- {
411
- ...s,
412
- pan: {
413
- x: touch.startPanX + deltaX,
414
- y: touch.startPanY + deltaY
415
- }
416
- },
417
- getImageSizeFromState(s),
418
- s.cropRect
419
- );
420
- this.options.actions.setPan(newCrop);
421
- }
422
- });
423
- };
424
- // Handle double-tap-to-toggle-zoom. On a second tap within the configured
425
- // time/distance threshold, toggles zoom between `minZoom` and
426
- // `doubleTapZoom` around the tap point and dispatches. Returns `true` if
427
- // the tap was handled as a double-tap (caller should early-return),
428
- // `false` otherwise (caller proceeds with normal single-tap handling).
429
- // Either way, records the tap for next time.
430
- tryDoubleTap(e, tapX, tapY, containerRect) {
431
- const now = Date.now();
432
- const lastTap = this.lastTap;
433
- if (!lastTap) {
434
- this.lastTap = { time: now, x: tapX, y: tapY };
435
- return false;
436
- }
437
- const timeDelta = now - lastTap.time;
438
- const distDelta = Math.sqrt(
439
- (tapX - lastTap.x) ** 2 + (tapY - lastTap.y) ** 2
440
- );
441
- const isDoubleTap = timeDelta < DOUBLE_TAP_TIME && distDelta < DOUBLE_TAP_DISTANCE;
442
- if (!isDoubleTap) {
443
- this.lastTap = { time: now, x: tapX, y: tapY };
444
- return false;
445
- }
446
- e.preventDefault();
447
- this.lastTap = null;
448
- const currentState = this.options.getState();
449
- const containerSize = this.options.getContainerSize();
450
- const imgSize = this.options.getImageSize();
451
- const targetZoom = currentState.zoom > (this.minZoom + this.doubleTapZoom) / 2 ? this.minZoom : this.doubleTapZoom;
452
- const visSize = imgSize ?? containerSize;
453
- this.setStatus({ isZooming: true });
454
- clearTimeout(this.zoomTimer);
455
- this.zoomTimer = setTimeout(() => {
456
- this.setStatus({ isZooming: false });
457
- }, ZOOM_ANIMATION_DURATION);
458
- this.options.onGestureStart?.();
459
- if (visSize.width > 0 && visSize.height > 0) {
460
- const fx = tapX - containerRect.left - containerSize.width / 2;
461
- const fy = tapY - containerRect.top - containerSize.height / 2;
462
- const zoomRatio = 1 - targetZoom / currentState.zoom;
463
- const focalNormX = fx / visSize.width;
464
- const focalNormY = fy / visSize.height;
465
- const newCropX = currentState.pan.x + (focalNormX - currentState.pan.x) * zoomRatio;
466
- const newCropY = currentState.pan.y + (focalNormY - currentState.pan.y) * zoomRatio;
467
- const { pan: clampedCrop } = restrictPanZoom(
468
- {
469
- ...currentState,
470
- zoom: targetZoom,
471
- pan: { x: newCropX, y: newCropY }
472
- },
473
- getImageSizeFromState(currentState),
474
- currentState.cropRect
475
- );
476
- this.options.actions.setZoomAtPoint(targetZoom, clampedCrop);
477
- } else {
478
- this.options.actions.setZoom(targetZoom);
479
- }
480
- this.options.onGestureEnd?.();
481
- return true;
482
- }
483
- /**
484
- * Handle a keydown event on the container element.
485
- *
486
- * Supports arrow keys for panning, +/- for zoom, and r/R for rotation.
487
- *
488
- * @param e The native KeyboardEvent.
489
- */
490
- handleKeyDown(e) {
491
- const currentState = this.options.getState();
492
- switch (e.key) {
493
- case "ArrowUp": {
494
- e.preventDefault();
495
- const keyboardStep = this.getKeyboardStep(e);
496
- const { pan: newCrop } = restrictPanZoom(
497
- {
498
- ...currentState,
499
- pan: {
500
- x: currentState.pan.x,
501
- y: currentState.pan.y + keyboardStep
502
- }
503
- },
504
- getImageSizeFromState(currentState),
505
- currentState.cropRect
506
- );
507
- this.options.actions.setPan(newCrop);
508
- break;
509
- }
510
- case "ArrowDown": {
511
- e.preventDefault();
512
- const keyboardStep = this.getKeyboardStep(e);
513
- const { pan: newCrop } = restrictPanZoom(
514
- {
515
- ...currentState,
516
- pan: {
517
- x: currentState.pan.x,
518
- y: currentState.pan.y - keyboardStep
519
- }
520
- },
521
- getImageSizeFromState(currentState),
522
- currentState.cropRect
523
- );
524
- this.options.actions.setPan(newCrop);
525
- break;
526
- }
527
- case "ArrowLeft": {
528
- e.preventDefault();
529
- const keyboardStep = this.getKeyboardStep(e);
530
- const { pan: newCrop } = restrictPanZoom(
531
- {
532
- ...currentState,
533
- pan: {
534
- x: currentState.pan.x + keyboardStep,
535
- y: currentState.pan.y
536
- }
537
- },
538
- getImageSizeFromState(currentState),
539
- currentState.cropRect
540
- );
541
- this.options.actions.setPan(newCrop);
542
- break;
543
- }
544
- case "ArrowRight": {
545
- e.preventDefault();
546
- const keyboardStep = this.getKeyboardStep(e);
547
- const { pan: newCrop } = restrictPanZoom(
548
- {
549
- ...currentState,
550
- pan: {
551
- x: currentState.pan.x - keyboardStep,
552
- y: currentState.pan.y
553
- }
554
- },
555
- getImageSizeFromState(currentState),
556
- currentState.cropRect
557
- );
558
- this.options.actions.setPan(newCrop);
559
- break;
560
- }
561
- case "+":
562
- case "=": {
563
- e.preventDefault();
564
- const newZoom = Math.min(
565
- this.maxZoom,
566
- Math.max(this.minZoom, currentState.zoom + 0.5)
567
- );
568
- this.options.actions.setZoom(newZoom);
569
- break;
570
- }
571
- case "-":
572
- case "_": {
573
- e.preventDefault();
574
- const newZoom = Math.min(
575
- this.maxZoom,
576
- Math.max(this.minZoom, currentState.zoom - 0.5)
577
- );
578
- this.options.actions.setZoom(newZoom);
579
- break;
580
- }
581
- case "r":
582
- case "R": {
583
- if (e.metaKey || e.ctrlKey || e.altKey) {
584
- break;
585
- }
586
- e.preventDefault();
587
- this.options.actions.snapRotate90(e.shiftKey ? -1 : 1);
588
- break;
589
- }
590
- case "h":
591
- case "H": {
592
- if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey) {
593
- break;
594
- }
595
- e.preventDefault();
596
- this.options.actions.toggleFlip?.("horizontal");
597
- break;
598
- }
599
- case "v":
600
- case "V": {
601
- if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey) {
602
- break;
603
- }
604
- e.preventDefault();
605
- this.options.actions.toggleFlip?.("vertical");
606
- break;
607
- }
608
- }
609
- }
610
- /**
611
- * Clean up all timers, animation frames, and active touch listeners.
612
- *
613
- * Must be called when the controller is no longer needed (e.g., on
614
- * component unmount or element removal).
615
- */
616
- destroy() {
617
- cancelAnimationFrame(this.rafId);
618
- clearTimeout(this.zoomTimer);
619
- clearTimeout(this.wheelGestureTimer);
620
- this.touchCleanup?.();
621
- this.touchCleanup = null;
622
- this.pointerCleanup?.();
623
- this.pointerCleanup = null;
624
- this.drag = null;
625
- this.touch = null;
626
- this.lastTap = null;
627
- this.wheelGestureActive = false;
628
- this.isDragging = false;
629
- this.isZooming = false;
630
- }
631
- };
632
- export {
633
- InteractionController
56
+ options;
57
+ /** Current drag/zoom status. */
58
+ isDragging = false;
59
+ isZooming = false;
60
+ /** Active drag state during pointer interactions. */
61
+ drag = null;
62
+ /** Active touch state during touch interactions. */
63
+ touch = null;
64
+ /** Cleanup function for active touch listeners on document. */
65
+ touchCleanup = null;
66
+ /**
67
+ * Cleanup function for active pointer-drag listeners on the
68
+ * captured element. Set while a pointer drag is in progress so
69
+ * `destroy()` can tear the drag down cleanly if the consumer
70
+ * unmounts mid-drag. Cleared on pointerup/lostpointercapture.
71
+ */
72
+ pointerCleanup = null;
73
+ /** Last tap info for double-tap detection. */
74
+ lastTap = null;
75
+ /** Timer for the zoom animation state (double-tap). */
76
+ zoomTimer;
77
+ /** Timer for wheel gesture debounce. */
78
+ wheelGestureTimer;
79
+ /** Whether a wheel gesture is currently active. */
80
+ wheelGestureActive = false;
81
+ /** Current requestAnimationFrame ID. */
82
+ rafId = 0;
83
+ constructor(options) {
84
+ this.options = options;
85
+ }
86
+ /** Read minZoom lazily so option changes take effect immediately. */
87
+ get minZoom() {
88
+ return this.options.minZoom ?? MIN_ZOOM;
89
+ }
90
+ /** Read maxZoom lazily so option changes take effect immediately. */
91
+ get maxZoom() {
92
+ return this.options.maxZoom ?? MAX_ZOOM;
93
+ }
94
+ /** Read zoomSpeed lazily so option changes take effect immediately. */
95
+ get zoomSpeed() {
96
+ return this.options.zoomSpeed ?? DEFAULT_WHEEL_ZOOM_SPEED;
97
+ }
98
+ /** Read keyboardStep lazily so option changes take effect immediately. */
99
+ get keyboardStep() {
100
+ return this.options.keyboardStep ?? DEFAULT_KEYBOARD_STEP;
101
+ }
102
+ /**
103
+ * Get the keyboard pan step, including modifier-based coarse movement.
104
+ *
105
+ * @param event The native KeyboardEvent.
106
+ * @return The normalized pan step.
107
+ */
108
+ getKeyboardStep(event) {
109
+ return event.shiftKey ? this.keyboardStep * KEYBOARD_SHIFT_STEP_MULTIPLIER : this.keyboardStep;
110
+ }
111
+ /** Read doubleTapZoom lazily so option changes take effect immediately. */
112
+ get doubleTapZoom() {
113
+ return this.options.doubleTapZoom ?? 2;
114
+ }
115
+ /**
116
+ * Update the drag/zoom status and notify via callback if changed.
117
+ *
118
+ * @param update Partial status with isDragging and/or isZooming.
119
+ */
120
+ setStatus(update) {
121
+ let changed = false;
122
+ if (update.isDragging !== void 0 && update.isDragging !== this.isDragging) {
123
+ this.isDragging = update.isDragging;
124
+ changed = true;
125
+ }
126
+ if (update.isZooming !== void 0 && update.isZooming !== this.isZooming) {
127
+ this.isZooming = update.isZooming;
128
+ changed = true;
129
+ }
130
+ if (changed) this.options.onStatusChange?.({
131
+ isDragging: this.isDragging,
132
+ isZooming: this.isZooming
133
+ });
134
+ }
135
+ /**
136
+ * Handle a pointer-down event on the container element.
137
+ *
138
+ * Initiates a drag (pan) interaction. Captures the pointer on the
139
+ * provided element and registers pointermove/pointerup/lostpointercapture
140
+ * listeners for the duration of the drag.
141
+ *
142
+ * @param e The native PointerEvent.
143
+ * @param el The DOM element to capture the pointer on.
144
+ */
145
+ handlePointerDown(e, el) {
146
+ if (e.button !== 0) return;
147
+ if (e.pointerType === "touch") return;
148
+ e.preventDefault();
149
+ const ownerDoc = el.ownerDocument;
150
+ if (ownerDoc?.activeElement instanceof HTMLElement && ownerDoc.activeElement !== el) ownerDoc.activeElement.blur();
151
+ el.focus();
152
+ el.setPointerCapture(e.pointerId);
153
+ if (this.wheelGestureActive) {
154
+ clearTimeout(this.wheelGestureTimer);
155
+ this.wheelGestureActive = false;
156
+ this.options.onGestureEnd?.();
157
+ }
158
+ this.setStatus({ isDragging: true });
159
+ this.options.onGestureStart?.();
160
+ const currentState = this.options.getState();
161
+ this.drag = {
162
+ startX: e.clientX,
163
+ startY: e.clientY,
164
+ startPanX: currentState.pan.x,
165
+ startPanY: currentState.pan.y
166
+ };
167
+ const onPointerMove = (moveEvent) => {
168
+ const drag = this.drag;
169
+ if (!drag) return;
170
+ const pe = moveEvent;
171
+ cancelAnimationFrame(this.rafId);
172
+ this.rafId = requestAnimationFrame(() => {
173
+ const s = this.options.getState();
174
+ const imgSize = this.options.getImageSize();
175
+ const containerSize = this.options.getContainerSize();
176
+ const panSize = imgSize ?? containerSize;
177
+ const deltaX = panSize.width > 0 ? (pe.clientX - drag.startX) / panSize.width : 0;
178
+ const deltaY = panSize.height > 0 ? (pe.clientY - drag.startY) / panSize.height : 0;
179
+ const { pan: newCrop } = restrictPanZoom({
180
+ ...s,
181
+ pan: {
182
+ x: drag.startPanX + deltaX,
183
+ y: drag.startPanY + deltaY
184
+ }
185
+ }, getImageSizeFromState(s), s.cropRect);
186
+ this.options.actions.setPan(newCrop);
187
+ });
188
+ };
189
+ const removeListeners = () => {
190
+ el.removeEventListener("pointermove", onPointerMove);
191
+ el.removeEventListener("pointerup", onPointerUp);
192
+ el.removeEventListener("lostpointercapture", onPointerUp);
193
+ };
194
+ const onPointerUp = () => {
195
+ this.setStatus({ isDragging: false });
196
+ this.options.onGestureEnd?.();
197
+ this.drag = null;
198
+ cancelAnimationFrame(this.rafId);
199
+ removeListeners();
200
+ this.pointerCleanup = null;
201
+ };
202
+ el.addEventListener("pointermove", onPointerMove);
203
+ el.addEventListener("pointerup", onPointerUp);
204
+ el.addEventListener("lostpointercapture", onPointerUp);
205
+ this.pointerCleanup = removeListeners;
206
+ }
207
+ /**
208
+ * Handle a wheel event on the container element.
209
+ *
210
+ * Implements focal-point zoom: the point under the cursor stays
211
+ * stationary on screen while the zoom level changes.
212
+ *
213
+ * Must be registered with `{ passive: false }` to allow preventDefault.
214
+ *
215
+ * @param e The native WheelEvent.
216
+ */
217
+ handleWheel(e) {
218
+ e.preventDefault();
219
+ if (this.drag) return;
220
+ if (!this.wheelGestureActive) {
221
+ this.wheelGestureActive = true;
222
+ this.options.onGestureStart?.();
223
+ }
224
+ clearTimeout(this.wheelGestureTimer);
225
+ this.wheelGestureTimer = setTimeout(() => {
226
+ this.wheelGestureActive = false;
227
+ this.options.onGestureEnd?.();
228
+ }, DOUBLE_TAP_TIME);
229
+ const s = this.options.getState();
230
+ const delta = -e.deltaY * this.zoomSpeed;
231
+ const newZoom = Math.min(this.maxZoom, Math.max(this.minZoom, s.zoom + delta));
232
+ if (newZoom === s.zoom) return;
233
+ const containerSize = this.options.getContainerSize();
234
+ const visSize = this.options.getImageSize() ?? containerSize;
235
+ const target = e.currentTarget;
236
+ const rect = target instanceof Element ? target.getBoundingClientRect() : void 0;
237
+ if (visSize.width > 0 && visSize.height > 0 && rect) {
238
+ const fx = e.clientX - rect.left - containerSize.width / 2;
239
+ const fy = e.clientY - rect.top - containerSize.height / 2;
240
+ const zoomRatio = 1 - newZoom / s.zoom;
241
+ const focalNormX = fx / visSize.width;
242
+ const focalNormY = fy / visSize.height;
243
+ const newCropX = s.pan.x + (focalNormX - s.pan.x) * zoomRatio;
244
+ const newCropY = s.pan.y + (focalNormY - s.pan.y) * zoomRatio;
245
+ const { pan: clampedCrop } = restrictPanZoom({
246
+ ...s,
247
+ zoom: newZoom,
248
+ pan: {
249
+ x: newCropX,
250
+ y: newCropY
251
+ }
252
+ }, getImageSizeFromState(s), s.cropRect);
253
+ this.options.actions.setZoomAtPoint(newZoom, clampedCrop);
254
+ } else this.options.actions.setZoom(newZoom);
255
+ }
256
+ /**
257
+ * Handle a touch-start event on the container element.
258
+ *
259
+ * Supports single-finger pan, two-finger pinch zoom, and
260
+ * double-tap to toggle between fit and 2x zoom. Registers
261
+ * touchmove/touchend/touchcancel on the provided document for the
262
+ * duration of the gesture.
263
+ *
264
+ * @param e The native TouchEvent.
265
+ * @param containerRect The bounding rect of the container element.
266
+ * @param doc The document to register move/end listeners on.
267
+ * Defaults to globalThis.document. Pass the iframe's
268
+ * contentDocument when running inside an iframe.
269
+ */
270
+ handleTouchStart(e, containerRect, doc = document) {
271
+ if (this.touch && e.touches.length === 2) {
272
+ const s = this.options.getState();
273
+ const distance = getTouchDistance(e.touches[0], e.touches[1]);
274
+ const midX = (e.touches[0].clientX + e.touches[1].clientX) / 2;
275
+ const midY = (e.touches[0].clientY + e.touches[1].clientY) / 2;
276
+ this.touch.didPinch = true;
277
+ this.touch.startDistance = distance;
278
+ this.touch.startZoom = s.zoom;
279
+ this.touch.startPanX = s.pan.x;
280
+ this.touch.startPanY = s.pan.y;
281
+ this.touch.startMidX = midX;
282
+ this.touch.startMidY = midY;
283
+ this.touch.containerRect = containerRect;
284
+ this.setStatus({ isDragging: false });
285
+ return;
286
+ }
287
+ if (e.touches.length === 1) {
288
+ const tapX = e.touches[0].clientX;
289
+ const tapY = e.touches[0].clientY;
290
+ if (this.tryDoubleTap(e, tapX, tapY, containerRect)) return;
291
+ const currentState = this.options.getState();
292
+ this.touch = {
293
+ startDistance: 0,
294
+ startZoom: currentState.zoom,
295
+ lastTouchX: e.touches[0].clientX,
296
+ lastTouchY: e.touches[0].clientY,
297
+ startPanX: currentState.pan.x,
298
+ startPanY: currentState.pan.y,
299
+ containerRect,
300
+ startMidX: 0,
301
+ startMidY: 0,
302
+ didPinch: false,
303
+ moved: false
304
+ };
305
+ this.options.onGestureStart?.();
306
+ } else if (e.touches.length === 2) {
307
+ const currentState = this.options.getState();
308
+ const distance = getTouchDistance(e.touches[0], e.touches[1]);
309
+ const midX = (e.touches[0].clientX + e.touches[1].clientX) / 2;
310
+ const midY = (e.touches[0].clientY + e.touches[1].clientY) / 2;
311
+ this.touch = {
312
+ startDistance: distance,
313
+ startZoom: currentState.zoom,
314
+ lastTouchX: 0,
315
+ lastTouchY: 0,
316
+ startPanX: currentState.pan.x,
317
+ startPanY: currentState.pan.y,
318
+ containerRect,
319
+ startMidX: midX,
320
+ startMidY: midY,
321
+ didPinch: true,
322
+ moved: false
323
+ };
324
+ this.options.onGestureStart?.();
325
+ }
326
+ const onTouchEnd = () => {
327
+ this.touch = null;
328
+ this.touchCleanup = null;
329
+ cancelAnimationFrame(this.rafId);
330
+ doc.removeEventListener("touchmove", this.handleTouchMove);
331
+ doc.removeEventListener("touchend", onTouchEnd);
332
+ doc.removeEventListener("touchcancel", onTouchEnd);
333
+ this.setStatus({ isDragging: false });
334
+ this.options.onGestureEnd?.();
335
+ };
336
+ this.touchCleanup?.();
337
+ doc.addEventListener("touchmove", this.handleTouchMove, { passive: false });
338
+ doc.addEventListener("touchend", onTouchEnd);
339
+ doc.addEventListener("touchcancel", onTouchEnd);
340
+ this.touchCleanup = onTouchEnd;
341
+ }
342
+ handleTouchMove = (moveEvent) => {
343
+ const touch = this.touch;
344
+ if (!touch) return;
345
+ moveEvent.preventDefault();
346
+ cancelAnimationFrame(this.rafId);
347
+ this.rafId = requestAnimationFrame(() => {
348
+ const s = this.options.getState();
349
+ if (moveEvent.touches.length === 2) {
350
+ if (!touch.didPinch) {
351
+ touch.didPinch = true;
352
+ touch.startDistance = getTouchDistance(moveEvent.touches[0], moveEvent.touches[1]);
353
+ touch.startZoom = s.zoom;
354
+ touch.startPanX = s.pan.x;
355
+ touch.startPanY = s.pan.y;
356
+ touch.startMidX = (moveEvent.touches[0].clientX + moveEvent.touches[1].clientX) / 2;
357
+ touch.startMidY = (moveEvent.touches[0].clientY + moveEvent.touches[1].clientY) / 2;
358
+ this.setStatus({ isDragging: false });
359
+ return;
360
+ }
361
+ const latestContainerSize = this.options.getContainerSize();
362
+ const latestImageSize = this.options.getImageSize();
363
+ const t0 = moveEvent.touches[0];
364
+ const t1 = moveEvent.touches[1];
365
+ const ratio = getTouchDistance(t0, t1) / touch.startDistance;
366
+ const newZoom = Math.min(this.maxZoom, Math.max(this.minZoom, touch.startZoom * ratio));
367
+ const visSize = latestImageSize ?? latestContainerSize;
368
+ const rect = touch.containerRect;
369
+ if (visSize.width > 0 && visSize.height > 0 && rect) {
370
+ const mx = (t0.clientX + t1.clientX) / 2 - rect.left - latestContainerSize.width / 2;
371
+ const my = (t0.clientY + t1.clientY) / 2 - rect.top - latestContainerSize.height / 2;
372
+ const startMx = touch.startMidX - rect.left - latestContainerSize.width / 2;
373
+ const startMy = touch.startMidY - rect.top - latestContainerSize.height / 2;
374
+ const panDx = visSize.width > 0 ? (mx - startMx) / visSize.width : 0;
375
+ const panDy = visSize.height > 0 ? (my - startMy) / visSize.height : 0;
376
+ const zoomRatio = touch.startZoom !== 0 ? 1 - newZoom / touch.startZoom : 0;
377
+ const startFocalNormX = startMx / visSize.width;
378
+ const startFocalNormY = startMy / visSize.height;
379
+ const newCropX = touch.startPanX + panDx + (startFocalNormX - touch.startPanX) * zoomRatio;
380
+ const newCropY = touch.startPanY + panDy + (startFocalNormY - touch.startPanY) * zoomRatio;
381
+ const { pan: clampedCrop } = restrictPanZoom({
382
+ ...s,
383
+ zoom: newZoom,
384
+ pan: {
385
+ x: newCropX,
386
+ y: newCropY
387
+ }
388
+ }, getImageSizeFromState(s), s.cropRect);
389
+ this.options.actions.setZoomAtPoint(newZoom, clampedCrop);
390
+ } else if (newZoom !== s.zoom) this.options.actions.setZoom(newZoom);
391
+ } else if (moveEvent.touches.length === 1 && !touch.didPinch) {
392
+ if (!touch.moved) {
393
+ touch.moved = true;
394
+ this.setStatus({ isDragging: true });
395
+ }
396
+ const panImageSize = this.options.getImageSize();
397
+ const panContainerSize = this.options.getContainerSize();
398
+ const panSize = panImageSize ?? panContainerSize;
399
+ const deltaX = panSize.width > 0 ? (moveEvent.touches[0].clientX - touch.lastTouchX) / panSize.width : 0;
400
+ const deltaY = panSize.height > 0 ? (moveEvent.touches[0].clientY - touch.lastTouchY) / panSize.height : 0;
401
+ const { pan: newCrop } = restrictPanZoom({
402
+ ...s,
403
+ pan: {
404
+ x: touch.startPanX + deltaX,
405
+ y: touch.startPanY + deltaY
406
+ }
407
+ }, getImageSizeFromState(s), s.cropRect);
408
+ this.options.actions.setPan(newCrop);
409
+ }
410
+ });
411
+ };
412
+ tryDoubleTap(e, tapX, tapY, containerRect) {
413
+ const now = Date.now();
414
+ const lastTap = this.lastTap;
415
+ if (!lastTap) {
416
+ this.lastTap = {
417
+ time: now,
418
+ x: tapX,
419
+ y: tapY
420
+ };
421
+ return false;
422
+ }
423
+ const timeDelta = now - lastTap.time;
424
+ const distDelta = Math.sqrt((tapX - lastTap.x) ** 2 + (tapY - lastTap.y) ** 2);
425
+ if (!(timeDelta < DOUBLE_TAP_TIME && distDelta < DOUBLE_TAP_DISTANCE)) {
426
+ this.lastTap = {
427
+ time: now,
428
+ x: tapX,
429
+ y: tapY
430
+ };
431
+ return false;
432
+ }
433
+ e.preventDefault();
434
+ this.lastTap = null;
435
+ const currentState = this.options.getState();
436
+ const containerSize = this.options.getContainerSize();
437
+ const imgSize = this.options.getImageSize();
438
+ const targetZoom = currentState.zoom > (this.minZoom + this.doubleTapZoom) / 2 ? this.minZoom : this.doubleTapZoom;
439
+ const visSize = imgSize ?? containerSize;
440
+ this.setStatus({ isZooming: true });
441
+ clearTimeout(this.zoomTimer);
442
+ this.zoomTimer = setTimeout(() => {
443
+ this.setStatus({ isZooming: false });
444
+ }, ZOOM_ANIMATION_DURATION);
445
+ this.options.onGestureStart?.();
446
+ if (visSize.width > 0 && visSize.height > 0) {
447
+ const fx = tapX - containerRect.left - containerSize.width / 2;
448
+ const fy = tapY - containerRect.top - containerSize.height / 2;
449
+ const zoomRatio = 1 - targetZoom / currentState.zoom;
450
+ const focalNormX = fx / visSize.width;
451
+ const focalNormY = fy / visSize.height;
452
+ const newCropX = currentState.pan.x + (focalNormX - currentState.pan.x) * zoomRatio;
453
+ const newCropY = currentState.pan.y + (focalNormY - currentState.pan.y) * zoomRatio;
454
+ const { pan: clampedCrop } = restrictPanZoom({
455
+ ...currentState,
456
+ zoom: targetZoom,
457
+ pan: {
458
+ x: newCropX,
459
+ y: newCropY
460
+ }
461
+ }, getImageSizeFromState(currentState), currentState.cropRect);
462
+ this.options.actions.setZoomAtPoint(targetZoom, clampedCrop);
463
+ } else this.options.actions.setZoom(targetZoom);
464
+ this.options.onGestureEnd?.();
465
+ return true;
466
+ }
467
+ /**
468
+ * Handle a keydown event on the container element.
469
+ *
470
+ * Supports arrow keys for panning, +/- for zoom, and r/R for rotation.
471
+ *
472
+ * @param e The native KeyboardEvent.
473
+ */
474
+ handleKeyDown(e) {
475
+ const currentState = this.options.getState();
476
+ switch (e.key) {
477
+ case "ArrowUp": {
478
+ e.preventDefault();
479
+ const keyboardStep = this.getKeyboardStep(e);
480
+ const { pan: newCrop } = restrictPanZoom({
481
+ ...currentState,
482
+ pan: {
483
+ x: currentState.pan.x,
484
+ y: currentState.pan.y + keyboardStep
485
+ }
486
+ }, getImageSizeFromState(currentState), currentState.cropRect);
487
+ this.options.actions.setPan(newCrop);
488
+ break;
489
+ }
490
+ case "ArrowDown": {
491
+ e.preventDefault();
492
+ const keyboardStep = this.getKeyboardStep(e);
493
+ const { pan: newCrop } = restrictPanZoom({
494
+ ...currentState,
495
+ pan: {
496
+ x: currentState.pan.x,
497
+ y: currentState.pan.y - keyboardStep
498
+ }
499
+ }, getImageSizeFromState(currentState), currentState.cropRect);
500
+ this.options.actions.setPan(newCrop);
501
+ break;
502
+ }
503
+ case "ArrowLeft": {
504
+ e.preventDefault();
505
+ const keyboardStep = this.getKeyboardStep(e);
506
+ const { pan: newCrop } = restrictPanZoom({
507
+ ...currentState,
508
+ pan: {
509
+ x: currentState.pan.x + keyboardStep,
510
+ y: currentState.pan.y
511
+ }
512
+ }, getImageSizeFromState(currentState), currentState.cropRect);
513
+ this.options.actions.setPan(newCrop);
514
+ break;
515
+ }
516
+ case "ArrowRight": {
517
+ e.preventDefault();
518
+ const keyboardStep = this.getKeyboardStep(e);
519
+ const { pan: newCrop } = restrictPanZoom({
520
+ ...currentState,
521
+ pan: {
522
+ x: currentState.pan.x - keyboardStep,
523
+ y: currentState.pan.y
524
+ }
525
+ }, getImageSizeFromState(currentState), currentState.cropRect);
526
+ this.options.actions.setPan(newCrop);
527
+ break;
528
+ }
529
+ case "+":
530
+ case "=": {
531
+ e.preventDefault();
532
+ const newZoom = Math.min(this.maxZoom, Math.max(this.minZoom, currentState.zoom + .5));
533
+ this.options.actions.setZoom(newZoom);
534
+ break;
535
+ }
536
+ case "-":
537
+ case "_": {
538
+ e.preventDefault();
539
+ const newZoom = Math.min(this.maxZoom, Math.max(this.minZoom, currentState.zoom - .5));
540
+ this.options.actions.setZoom(newZoom);
541
+ break;
542
+ }
543
+ case "r":
544
+ case "R":
545
+ if (e.metaKey || e.ctrlKey || e.altKey) break;
546
+ e.preventDefault();
547
+ this.options.actions.snapRotate90(e.shiftKey ? -1 : 1);
548
+ break;
549
+ case "h":
550
+ case "H":
551
+ if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey) break;
552
+ e.preventDefault();
553
+ this.options.actions.toggleFlip?.("horizontal");
554
+ break;
555
+ case "v":
556
+ case "V":
557
+ if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey) break;
558
+ e.preventDefault();
559
+ this.options.actions.toggleFlip?.("vertical");
560
+ }
561
+ }
562
+ /**
563
+ * Clean up all timers, animation frames, and active touch listeners.
564
+ *
565
+ * Must be called when the controller is no longer needed (e.g., on
566
+ * component unmount or element removal).
567
+ */
568
+ destroy() {
569
+ cancelAnimationFrame(this.rafId);
570
+ clearTimeout(this.zoomTimer);
571
+ clearTimeout(this.wheelGestureTimer);
572
+ this.touchCleanup?.();
573
+ this.touchCleanup = null;
574
+ this.pointerCleanup?.();
575
+ this.pointerCleanup = null;
576
+ this.drag = null;
577
+ this.touch = null;
578
+ this.lastTap = null;
579
+ this.wheelGestureActive = false;
580
+ this.isDragging = false;
581
+ this.isZooming = false;
582
+ }
634
583
  };
635
- //# sourceMappingURL=interaction-controller.mjs.map
584
+ export { InteractionController };
585
+
586
+ //# sourceMappingURL=interaction-controller.mjs.map