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