@wordpress/media-editor 0.7.0 → 0.8.0

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