@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.
- package/build/components/media-editor-canvas/index.cjs +54 -0
- package/build/components/media-editor-canvas/index.cjs.map +7 -0
- package/build/components/media-editor-crop-panel/index.cjs +116 -0
- package/build/components/media-editor-crop-panel/index.cjs.map +7 -0
- package/build/components/media-editor-modal/build-modifiers.cjs +91 -0
- package/build/components/media-editor-modal/build-modifiers.cjs.map +7 -0
- package/build/components/media-editor-modal/index.cjs +497 -0
- package/build/components/media-editor-modal/index.cjs.map +7 -0
- package/build/components/media-editor-toolbar/index.cjs +146 -0
- package/build/components/media-editor-toolbar/index.cjs.map +7 -0
- package/build/components/media-preview/index.cjs +1 -1
- package/build/components/media-preview/index.cjs.map +2 -2
- package/build/image-editor/core/camera.cjs +194 -0
- package/build/image-editor/core/camera.cjs.map +7 -0
- package/build/image-editor/core/constants.cjs +81 -0
- package/build/image-editor/core/constants.cjs.map +7 -0
- package/build/image-editor/core/containment.cjs +245 -0
- package/build/image-editor/core/containment.cjs.map +7 -0
- package/build/image-editor/core/export/canvas-renderer.cjs +151 -0
- package/build/image-editor/core/export/canvas-renderer.cjs.map +7 -0
- package/build/image-editor/core/index.cjs +50 -0
- package/build/image-editor/core/index.cjs.map +7 -0
- package/build/image-editor/core/interaction-controller.cjs +608 -0
- package/build/image-editor/core/interaction-controller.cjs.map +7 -0
- package/build/image-editor/core/math/rotation.cjs +41 -0
- package/build/image-editor/core/math/rotation.cjs.map +7 -0
- package/build/image-editor/core/source-region.cjs +95 -0
- package/build/image-editor/core/source-region.cjs.map +7 -0
- package/build/image-editor/core/state.cjs +269 -0
- package/build/image-editor/core/state.cjs.map +7 -0
- package/build/image-editor/core/stencil-math.cjs +197 -0
- package/build/image-editor/core/stencil-math.cjs.map +7 -0
- package/build/image-editor/core/transform-style.cjs +46 -0
- package/build/image-editor/core/transform-style.cjs.map +7 -0
- package/build/image-editor/core/transforms/pipeline.cjs +43 -0
- package/build/image-editor/core/transforms/pipeline.cjs.map +7 -0
- package/build/image-editor/core/types.cjs +19 -0
- package/build/image-editor/core/types.cjs.map +7 -0
- package/build/image-editor/index.cjs +57 -0
- package/build/image-editor/index.cjs.map +7 -0
- package/build/image-editor/react/components/cropper-provider.cjs +50 -0
- package/build/image-editor/react/components/cropper-provider.cjs.map +7 -0
- package/build/image-editor/react/components/cropper.cjs +365 -0
- package/build/image-editor/react/components/cropper.cjs.map +7 -0
- package/build/image-editor/react/components/index.cjs +36 -0
- package/build/image-editor/react/components/index.cjs.map +7 -0
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +58 -0
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +7 -0
- package/build/image-editor/react/components/overlays/grid-overlay.cjs +91 -0
- package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +7 -0
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +351 -0
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +7 -0
- package/build/image-editor/react/hooks/index.cjs +31 -0
- package/build/image-editor/react/hooks/index.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +68 -0
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-cropper-state.cjs +158 -0
- package/build/image-editor/react/hooks/use-cropper-state.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-interaction.cjs +161 -0
- package/build/image-editor/react/hooks/use-interaction.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-transform-style.cjs +38 -0
- package/build/image-editor/react/hooks/use-transform-style.cjs.map +7 -0
- package/build/index.cjs +5 -2
- package/build/index.cjs.map +2 -2
- package/build/lock-unlock.cjs +37 -0
- package/build/lock-unlock.cjs.map +7 -0
- package/build/private-apis.cjs +38 -0
- package/build/private-apis.cjs.map +7 -0
- package/build/store/actions.cjs +45 -0
- package/build/store/actions.cjs.map +7 -0
- package/build/store/constants.cjs +31 -0
- package/build/store/constants.cjs.map +7 -0
- package/build/store/index.cjs +51 -0
- package/build/store/index.cjs.map +7 -0
- package/build/store/reducer.cjs +51 -0
- package/build/store/reducer.cjs.map +7 -0
- package/build/store/selectors.cjs +43 -0
- package/build/store/selectors.cjs.map +7 -0
- package/build-module/components/media-editor-canvas/index.mjs +33 -0
- package/build-module/components/media-editor-canvas/index.mjs.map +7 -0
- package/build-module/components/media-editor-crop-panel/index.mjs +100 -0
- package/build-module/components/media-editor-crop-panel/index.mjs.map +7 -0
- package/build-module/components/media-editor-modal/build-modifiers.mjs +66 -0
- package/build-module/components/media-editor-modal/build-modifiers.mjs.map +7 -0
- package/build-module/components/media-editor-modal/index.mjs +483 -0
- package/build-module/components/media-editor-modal/index.mjs.map +7 -0
- package/build-module/components/media-editor-toolbar/index.mjs +130 -0
- package/build-module/components/media-editor-toolbar/index.mjs.map +7 -0
- package/build-module/components/media-preview/index.mjs +1 -1
- package/build-module/components/media-preview/index.mjs.map +2 -2
- package/build-module/image-editor/core/camera.mjs +163 -0
- package/build-module/image-editor/core/camera.mjs.map +7 -0
- package/build-module/image-editor/core/constants.mjs +51 -0
- package/build-module/image-editor/core/constants.mjs.map +7 -0
- package/build-module/image-editor/core/containment.mjs +218 -0
- package/build-module/image-editor/core/containment.mjs.map +7 -0
- package/build-module/image-editor/core/export/canvas-renderer.mjs +120 -0
- package/build-module/image-editor/core/export/canvas-renderer.mjs.map +7 -0
- package/build-module/image-editor/core/index.mjs +24 -0
- package/build-module/image-editor/core/index.mjs.map +7 -0
- package/build-module/image-editor/core/interaction-controller.mjs +583 -0
- package/build-module/image-editor/core/interaction-controller.mjs.map +7 -0
- package/build-module/image-editor/core/math/rotation.mjs +15 -0
- package/build-module/image-editor/core/math/rotation.mjs.map +7 -0
- package/build-module/image-editor/core/source-region.mjs +69 -0
- package/build-module/image-editor/core/source-region.mjs.map +7 -0
- package/build-module/image-editor/core/state.mjs +242 -0
- package/build-module/image-editor/core/state.mjs.map +7 -0
- package/build-module/image-editor/core/stencil-math.mjs +169 -0
- package/build-module/image-editor/core/stencil-math.mjs.map +7 -0
- package/build-module/image-editor/core/transform-style.mjs +21 -0
- package/build-module/image-editor/core/transform-style.mjs.map +7 -0
- package/build-module/image-editor/core/transforms/pipeline.mjs +17 -0
- package/build-module/image-editor/core/transforms/pipeline.mjs.map +7 -0
- package/build-module/image-editor/core/types.mjs +1 -0
- package/build-module/image-editor/core/types.mjs.map +7 -0
- package/build-module/image-editor/index.mjs +30 -0
- package/build-module/image-editor/index.mjs.map +7 -0
- package/build-module/image-editor/react/components/cropper-provider.mjs +26 -0
- package/build-module/image-editor/react/components/cropper-provider.mjs.map +7 -0
- package/build-module/image-editor/react/components/cropper.mjs +337 -0
- package/build-module/image-editor/react/components/cropper.mjs.map +7 -0
- package/build-module/image-editor/react/components/index.mjs +9 -0
- package/build-module/image-editor/react/components/index.mjs.map +7 -0
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +33 -0
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +7 -0
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +66 -0
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +7 -0
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +330 -0
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/index.mjs +6 -0
- package/build-module/image-editor/react/hooks/index.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +43 -0
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs +137 -0
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs +138 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-transform-style.mjs +13 -0
- package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +7 -0
- package/build-module/index.mjs +3 -1
- package/build-module/index.mjs.map +2 -2
- package/build-module/lock-unlock.mjs +11 -0
- package/build-module/lock-unlock.mjs.map +7 -0
- package/build-module/private-apis.mjs +13 -0
- package/build-module/private-apis.mjs.map +7 -0
- package/build-module/store/actions.mjs +19 -0
- package/build-module/store/actions.mjs.map +7 -0
- package/build-module/store/constants.mjs +6 -0
- package/build-module/store/constants.mjs.map +7 -0
- package/build-module/store/index.mjs +16 -0
- package/build-module/store/index.mjs.map +7 -0
- package/build-module/store/reducer.mjs +26 -0
- package/build-module/store/reducer.mjs.map +7 -0
- package/build-module/store/selectors.mjs +16 -0
- package/build-module/store/selectors.mjs.map +7 -0
- package/build-style/style-rtl.css +315 -0
- package/build-style/style.css +315 -0
- package/build-types/components/media-editor-canvas/index.d.ts +22 -0
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -0
- package/build-types/components/media-editor-crop-panel/index.d.ts +45 -0
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/build-modifiers.d.ts +83 -0
- package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/index.d.ts +20 -0
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -0
- package/build-types/components/media-editor-toolbar/index.d.ts +21 -0
- package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -0
- package/build-types/components/media-preview/index.d.ts.map +1 -1
- package/build-types/image-editor/core/camera.d.ts +107 -0
- package/build-types/image-editor/core/camera.d.ts.map +1 -0
- package/build-types/image-editor/core/constants.d.ts +46 -0
- package/build-types/image-editor/core/constants.d.ts.map +1 -0
- package/build-types/image-editor/core/containment.d.ts +61 -0
- package/build-types/image-editor/core/containment.d.ts.map +1 -0
- package/build-types/image-editor/core/export/canvas-renderer.d.ts +106 -0
- package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +1 -0
- package/build-types/image-editor/core/index.d.ts +8 -0
- package/build-types/image-editor/core/index.d.ts.map +1 -0
- package/build-types/image-editor/core/interaction-controller.d.ts +178 -0
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -0
- package/build-types/image-editor/core/math/rotation.d.ts +18 -0
- package/build-types/image-editor/core/math/rotation.d.ts.map +1 -0
- package/build-types/image-editor/core/source-region.d.ts +75 -0
- package/build-types/image-editor/core/source-region.d.ts.map +1 -0
- package/build-types/image-editor/core/state.d.ts +42 -0
- package/build-types/image-editor/core/state.d.ts.map +1 -0
- package/build-types/image-editor/core/stencil-math.d.ts +79 -0
- package/build-types/image-editor/core/stencil-math.d.ts.map +1 -0
- package/build-types/image-editor/core/transform-style.d.ts +23 -0
- package/build-types/image-editor/core/transform-style.d.ts.map +1 -0
- package/build-types/image-editor/core/transforms/pipeline.d.ts +27 -0
- package/build-types/image-editor/core/transforms/pipeline.d.ts.map +1 -0
- package/build-types/image-editor/core/types.d.ts +211 -0
- package/build-types/image-editor/core/types.d.ts.map +1 -0
- package/build-types/image-editor/index.d.ts +16 -0
- package/build-types/image-editor/index.d.ts.map +1 -0
- package/build-types/image-editor/react/components/cropper-provider.d.ts +37 -0
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -0
- package/build-types/image-editor/react/components/cropper.d.ts +59 -0
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -0
- package/build-types/image-editor/react/components/index.d.ts +4 -0
- package/build-types/image-editor/react/components/index.d.ts.map +1 -0
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +30 -0
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -0
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +30 -0
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -0
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +32 -0
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/index.d.ts +3 -0
- package/build-types/image-editor/react/hooks/index.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +16 -0
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +68 -0
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts +60 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-transform-style.d.ts +16 -0
- package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +1 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts +16 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -0
- package/build-types/index.d.ts +1 -0
- package/build-types/index.d.ts.map +1 -1
- package/build-types/lock-unlock.d.ts +2 -0
- package/build-types/lock-unlock.d.ts.map +1 -0
- package/build-types/private-apis.d.ts +2 -0
- package/build-types/private-apis.d.ts.map +1 -0
- package/build-types/store/actions.d.ts +27 -0
- package/build-types/store/actions.d.ts.map +1 -0
- package/build-types/store/constants.d.ts +2 -0
- package/build-types/store/constants.d.ts.map +1 -0
- package/build-types/store/index.d.ts +4 -0
- package/build-types/store/index.d.ts.map +1 -0
- package/build-types/store/reducer.d.ts +23 -0
- package/build-types/store/reducer.d.ts.map +1 -0
- package/build-types/store/selectors.d.ts +8 -0
- package/build-types/store/selectors.d.ts.map +1 -0
- package/package.json +21 -8
- package/src/components/media-editor-canvas/index.tsx +56 -0
- package/src/components/media-editor-canvas/style.scss +9 -0
- package/src/components/media-editor-crop-panel/index.tsx +143 -0
- package/src/components/media-editor-modal/build-modifiers.ts +164 -0
- package/src/components/media-editor-modal/index.tsx +639 -0
- package/src/components/media-editor-modal/style.scss +139 -0
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +343 -0
- package/src/components/media-editor-toolbar/index.tsx +150 -0
- package/src/components/media-editor-toolbar/style.scss +21 -0
- package/src/components/media-preview/index.tsx +2 -1
- package/src/image-editor/README.md +162 -0
- package/src/image-editor/core/camera.ts +352 -0
- package/src/image-editor/core/constants.ts +93 -0
- package/src/image-editor/core/containment.ts +435 -0
- package/src/image-editor/core/export/canvas-renderer.ts +253 -0
- package/src/image-editor/core/export/test/canvas-renderer.ts +813 -0
- package/src/image-editor/core/index.ts +35 -0
- package/src/image-editor/core/interaction-controller.ts +889 -0
- package/src/image-editor/core/math/rotation.ts +26 -0
- package/src/image-editor/core/source-region.ts +166 -0
- package/src/image-editor/core/state.ts +428 -0
- package/src/image-editor/core/stencil-math.ts +330 -0
- package/src/image-editor/core/test/camera.ts +593 -0
- package/src/image-editor/core/test/interaction-controller.ts +996 -0
- package/src/image-editor/core/test/preview-export-parity.ts +242 -0
- package/src/image-editor/core/test/render-camera-bridge.ts +348 -0
- package/src/image-editor/core/test/state.ts +762 -0
- package/src/image-editor/core/test/stencil-math.ts +321 -0
- package/src/image-editor/core/transform-style.ts +44 -0
- package/src/image-editor/core/transforms/pipeline.ts +43 -0
- package/src/image-editor/core/transforms/test/pipeline.ts +269 -0
- package/src/image-editor/core/types.ts +177 -0
- package/src/image-editor/docs/architecture.md +96 -0
- package/src/image-editor/docs/recipes.md +720 -0
- package/src/image-editor/docs/roadmap.md +66 -0
- package/src/image-editor/index.ts +43 -0
- package/src/image-editor/react/components/cropper-provider.tsx +73 -0
- package/src/image-editor/react/components/cropper.scss +195 -0
- package/src/image-editor/react/components/cropper.tsx +534 -0
- package/src/image-editor/react/components/index.ts +3 -0
- package/src/image-editor/react/components/overlays/dimming-overlay.tsx +57 -0
- package/src/image-editor/react/components/overlays/grid-overlay.tsx +80 -0
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +513 -0
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +265 -0
- package/src/image-editor/react/components/test/cropper.tsx +140 -0
- package/src/image-editor/react/hooks/index.ts +2 -0
- package/src/image-editor/react/hooks/test/use-cropper-state.ts +913 -0
- package/src/image-editor/react/hooks/test/use-interaction.ts +218 -0
- package/src/image-editor/react/hooks/test/use-transform-style.ts +146 -0
- package/src/image-editor/react/hooks/use-aria-announcer.ts +64 -0
- package/src/image-editor/react/hooks/use-cropper-state.ts +238 -0
- package/src/image-editor/react/hooks/use-interaction.ts +223 -0
- package/src/image-editor/react/hooks/use-transform-style.ts +30 -0
- package/src/image-editor/stories/rectangle-crop.story.tsx +1085 -0
- package/src/image-editor/stories/style.css +94 -0
- package/src/image-editor/style.scss +1 -0
- package/src/index.ts +3 -0
- package/src/lock-unlock.ts +10 -0
- package/src/private-apis.ts +12 -0
- package/src/store/actions.ts +33 -0
- package/src/store/constants.ts +1 -0
- package/src/store/index.ts +20 -0
- package/src/store/reducer.ts +48 -0
- package/src/store/selectors.ts +16 -0
- package/src/store/test/reducer.ts +57 -0
- package/src/style.scss +4 -0
- package/tsconfig.json +10 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,996 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
InteractionController,
|
|
6
|
+
type CropperInteractionActions,
|
|
7
|
+
} from '../interaction-controller';
|
|
8
|
+
import type { CropperState, Size } from '../types';
|
|
9
|
+
import { DEFAULT_STATE, MIN_ZOOM, MAX_ZOOM } from '../constants';
|
|
10
|
+
|
|
11
|
+
// The test environment is Node (not jsdom), so DOM globals like HTMLElement
|
|
12
|
+
// and Element are not available. Provide minimal stubs so that `instanceof`
|
|
13
|
+
// checks in the controller work as expected.
|
|
14
|
+
if ( typeof globalThis.HTMLElement === 'undefined' ) {
|
|
15
|
+
( globalThis as any ).HTMLElement = class HTMLElement {};
|
|
16
|
+
}
|
|
17
|
+
if ( typeof globalThis.Element === 'undefined' ) {
|
|
18
|
+
( globalThis as any ).Element = class Element {};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Create a cropper state with an image set so restrictPanZoom works.
|
|
23
|
+
*
|
|
24
|
+
* @param overrides Partial state overrides.
|
|
25
|
+
* @return A complete CropperState.
|
|
26
|
+
*/
|
|
27
|
+
function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
28
|
+
return {
|
|
29
|
+
...DEFAULT_STATE,
|
|
30
|
+
image: {
|
|
31
|
+
src: 'test.jpg',
|
|
32
|
+
naturalWidth: 1000,
|
|
33
|
+
naturalHeight: 600,
|
|
34
|
+
},
|
|
35
|
+
...overrides,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create a minimal PointerEvent-like object for testing.
|
|
41
|
+
*
|
|
42
|
+
* @param overrides Partial PointerEvent property overrides.
|
|
43
|
+
* @return A mock PointerEvent.
|
|
44
|
+
*/
|
|
45
|
+
function createPointerEvent(
|
|
46
|
+
overrides: Partial< PointerEvent > = {}
|
|
47
|
+
): PointerEvent {
|
|
48
|
+
return {
|
|
49
|
+
button: 0,
|
|
50
|
+
clientX: 0,
|
|
51
|
+
clientY: 0,
|
|
52
|
+
pointerId: 1,
|
|
53
|
+
preventDefault: jest.fn(),
|
|
54
|
+
...overrides,
|
|
55
|
+
} as unknown as PointerEvent;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Create a mock HTMLElement that tracks event listeners and supports _fire.
|
|
60
|
+
*
|
|
61
|
+
* @return A mock HTMLElement with a _fire helper to simulate events.
|
|
62
|
+
*/
|
|
63
|
+
function createMockElement(): HTMLElement & {
|
|
64
|
+
_fire: ( type: string, event: unknown ) => void;
|
|
65
|
+
} {
|
|
66
|
+
const listeners: Record< string, EventListener[] > = {};
|
|
67
|
+
return {
|
|
68
|
+
focus: jest.fn(),
|
|
69
|
+
setPointerCapture: jest.fn(),
|
|
70
|
+
releasePointerCapture: jest.fn(),
|
|
71
|
+
addEventListener: jest.fn( ( type: string, fn: EventListener ) => {
|
|
72
|
+
if ( ! listeners[ type ] ) {
|
|
73
|
+
listeners[ type ] = [];
|
|
74
|
+
}
|
|
75
|
+
listeners[ type ].push( fn );
|
|
76
|
+
} ),
|
|
77
|
+
removeEventListener: jest.fn( ( type: string, fn: EventListener ) => {
|
|
78
|
+
if ( listeners[ type ] ) {
|
|
79
|
+
listeners[ type ] = listeners[ type ].filter(
|
|
80
|
+
( l ) => l !== fn
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
} ),
|
|
84
|
+
ownerDocument: {
|
|
85
|
+
activeElement: null,
|
|
86
|
+
},
|
|
87
|
+
_fire( type: string, event: unknown ) {
|
|
88
|
+
listeners[ type ]?.forEach( ( fn ) =>
|
|
89
|
+
fn( event as unknown as Event )
|
|
90
|
+
);
|
|
91
|
+
},
|
|
92
|
+
} as unknown as HTMLElement & {
|
|
93
|
+
_fire: ( type: string, event: unknown ) => void;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Create a minimal WheelEvent-like object for testing.
|
|
99
|
+
*
|
|
100
|
+
* @param overrides Partial WheelEvent property overrides.
|
|
101
|
+
* @return A mock WheelEvent.
|
|
102
|
+
*/
|
|
103
|
+
function createWheelEvent(
|
|
104
|
+
overrides: Partial< WheelEvent > & { currentTarget?: unknown } = {}
|
|
105
|
+
): WheelEvent {
|
|
106
|
+
return {
|
|
107
|
+
preventDefault: jest.fn(),
|
|
108
|
+
deltaY: 0,
|
|
109
|
+
clientX: 0,
|
|
110
|
+
clientY: 0,
|
|
111
|
+
...overrides,
|
|
112
|
+
} as unknown as WheelEvent;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Create a minimal KeyboardEvent-like object for testing.
|
|
117
|
+
*
|
|
118
|
+
* @param key The key value.
|
|
119
|
+
* @param overrides Partial KeyboardEvent property overrides.
|
|
120
|
+
* @return A mock KeyboardEvent.
|
|
121
|
+
*/
|
|
122
|
+
function createKeyboardEvent(
|
|
123
|
+
key: string,
|
|
124
|
+
overrides: Partial< KeyboardEvent > = {}
|
|
125
|
+
): KeyboardEvent {
|
|
126
|
+
return {
|
|
127
|
+
key,
|
|
128
|
+
preventDefault: jest.fn(),
|
|
129
|
+
...overrides,
|
|
130
|
+
} as unknown as KeyboardEvent;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Create a minimal TouchEvent-like object for testing.
|
|
135
|
+
*
|
|
136
|
+
* @param touches Array of touch point coordinates.
|
|
137
|
+
* @param overrides Partial TouchEvent property overrides.
|
|
138
|
+
* @return A mock TouchEvent.
|
|
139
|
+
*/
|
|
140
|
+
function createTouchEvent(
|
|
141
|
+
touches: Array< { clientX: number; clientY: number } >,
|
|
142
|
+
overrides: Partial< TouchEvent > = {}
|
|
143
|
+
): TouchEvent {
|
|
144
|
+
return {
|
|
145
|
+
preventDefault: jest.fn(),
|
|
146
|
+
touches,
|
|
147
|
+
...overrides,
|
|
148
|
+
} as unknown as TouchEvent;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Create a mock DOMRect for container bounding rect.
|
|
153
|
+
*
|
|
154
|
+
* @param overrides Partial DOMRect property overrides.
|
|
155
|
+
* @return A mock DOMRect.
|
|
156
|
+
*/
|
|
157
|
+
function createContainerRect( overrides: Partial< DOMRect > = {} ): DOMRect {
|
|
158
|
+
return {
|
|
159
|
+
left: 0,
|
|
160
|
+
top: 0,
|
|
161
|
+
width: 500,
|
|
162
|
+
height: 300,
|
|
163
|
+
right: 500,
|
|
164
|
+
bottom: 300,
|
|
165
|
+
x: 0,
|
|
166
|
+
y: 0,
|
|
167
|
+
toJSON: jest.fn(),
|
|
168
|
+
...overrides,
|
|
169
|
+
} as DOMRect;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Create a mock Document object that tracks event listeners and
|
|
174
|
+
* supports _fire for simulating touch events.
|
|
175
|
+
*
|
|
176
|
+
* @return A mock Document with a _fire helper.
|
|
177
|
+
*/
|
|
178
|
+
function createMockDocument(): Document & {
|
|
179
|
+
_fire: ( type: string, event: unknown ) => void;
|
|
180
|
+
} {
|
|
181
|
+
const listeners: Record< string, EventListener[] > = {};
|
|
182
|
+
return {
|
|
183
|
+
addEventListener: jest.fn( ( type: string, fn: EventListener ) => {
|
|
184
|
+
if ( ! listeners[ type ] ) {
|
|
185
|
+
listeners[ type ] = [];
|
|
186
|
+
}
|
|
187
|
+
listeners[ type ].push( fn );
|
|
188
|
+
} ),
|
|
189
|
+
removeEventListener: jest.fn( ( type: string, fn: EventListener ) => {
|
|
190
|
+
if ( listeners[ type ] ) {
|
|
191
|
+
listeners[ type ] = listeners[ type ].filter(
|
|
192
|
+
( l ) => l !== fn
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
} ),
|
|
196
|
+
_fire( type: string, event: unknown ) {
|
|
197
|
+
listeners[ type ]?.forEach( ( fn ) =>
|
|
198
|
+
fn( event as unknown as Event )
|
|
199
|
+
);
|
|
200
|
+
},
|
|
201
|
+
} as unknown as Document & {
|
|
202
|
+
_fire: ( type: string, event: unknown ) => void;
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
describe( 'InteractionController', () => {
|
|
207
|
+
const containerSize: Size = { width: 500, height: 300 };
|
|
208
|
+
const imageSize: Size = { width: 500, height: 300 };
|
|
209
|
+
let actionMocks: jest.Mocked< CropperInteractionActions >;
|
|
210
|
+
|
|
211
|
+
// Store original requestAnimationFrame so we can restore it.
|
|
212
|
+
const originalRAF = globalThis.requestAnimationFrame;
|
|
213
|
+
const originalCAF = globalThis.cancelAnimationFrame;
|
|
214
|
+
|
|
215
|
+
beforeAll( () => {
|
|
216
|
+
// Replace requestAnimationFrame with immediate execution for tests.
|
|
217
|
+
globalThis.requestAnimationFrame = ( cb: FrameRequestCallback ) => {
|
|
218
|
+
cb( 0 );
|
|
219
|
+
return 0;
|
|
220
|
+
};
|
|
221
|
+
globalThis.cancelAnimationFrame = jest.fn();
|
|
222
|
+
} );
|
|
223
|
+
|
|
224
|
+
afterAll( () => {
|
|
225
|
+
globalThis.requestAnimationFrame = originalRAF;
|
|
226
|
+
globalThis.cancelAnimationFrame = originalCAF;
|
|
227
|
+
} );
|
|
228
|
+
|
|
229
|
+
beforeEach( () => {
|
|
230
|
+
actionMocks = {
|
|
231
|
+
setPan: jest.fn(),
|
|
232
|
+
setZoom: jest.fn(),
|
|
233
|
+
setZoomAtPoint: jest.fn(),
|
|
234
|
+
snapRotate90: jest.fn(),
|
|
235
|
+
};
|
|
236
|
+
} );
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Create an InteractionController with sensible test defaults.
|
|
240
|
+
*
|
|
241
|
+
* @param state The cropper state to return from getState.
|
|
242
|
+
* @param options Additional InteractionControllerOptions overrides.
|
|
243
|
+
* @return The controller instance and the options object (for lazy mutation).
|
|
244
|
+
*/
|
|
245
|
+
function createController(
|
|
246
|
+
state: CropperState,
|
|
247
|
+
options: Record< string, unknown > = {}
|
|
248
|
+
) {
|
|
249
|
+
const opts = {
|
|
250
|
+
getState: () => state,
|
|
251
|
+
actions: actionMocks,
|
|
252
|
+
getContainerSize: () => containerSize,
|
|
253
|
+
getImageSize: () => imageSize as Size | undefined,
|
|
254
|
+
...options,
|
|
255
|
+
};
|
|
256
|
+
const controller = new InteractionController( opts );
|
|
257
|
+
return { controller, opts };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
describe( 'pointer drag', () => {
|
|
261
|
+
it( 'calls setPan on pointerdown + pointermove', () => {
|
|
262
|
+
const state = makeState( { zoom: 2 } );
|
|
263
|
+
const { controller } = createController( state );
|
|
264
|
+
const el = createMockElement();
|
|
265
|
+
|
|
266
|
+
controller.handlePointerDown(
|
|
267
|
+
createPointerEvent( { clientX: 100, clientY: 100 } ),
|
|
268
|
+
el
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
// Simulate pointermove via the listener registered on el.
|
|
272
|
+
el._fire(
|
|
273
|
+
'pointermove',
|
|
274
|
+
createPointerEvent( { clientX: 150, clientY: 120 } )
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
expect( actionMocks.setPan ).toHaveBeenCalled();
|
|
278
|
+
|
|
279
|
+
const payload = actionMocks.setPan.mock.calls[ 0 ][ 0 ];
|
|
280
|
+
// Delta: (150-100)/500 = 0.1 in x, (120-100)/300 = 0.0667 in y.
|
|
281
|
+
expect( typeof payload.x ).toBe( 'number' );
|
|
282
|
+
expect( typeof payload.y ).toBe( 'number' );
|
|
283
|
+
|
|
284
|
+
// Clean up.
|
|
285
|
+
el._fire( 'pointerup', createPointerEvent() );
|
|
286
|
+
} );
|
|
287
|
+
|
|
288
|
+
it( 'stops dispatching after pointerup', () => {
|
|
289
|
+
const state = makeState( { zoom: 2 } );
|
|
290
|
+
const { controller } = createController( state );
|
|
291
|
+
const el = createMockElement();
|
|
292
|
+
|
|
293
|
+
controller.handlePointerDown(
|
|
294
|
+
createPointerEvent( { clientX: 100, clientY: 100 } ),
|
|
295
|
+
el
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
// Simulate pointerup.
|
|
299
|
+
el._fire( 'pointerup', createPointerEvent() );
|
|
300
|
+
|
|
301
|
+
jest.clearAllMocks();
|
|
302
|
+
|
|
303
|
+
// Another pointermove should not dispatch because the listener
|
|
304
|
+
// was removed after pointerup.
|
|
305
|
+
el._fire(
|
|
306
|
+
'pointermove',
|
|
307
|
+
createPointerEvent( { clientX: 200, clientY: 200 } )
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
expect( actionMocks.setPan ).not.toHaveBeenCalled();
|
|
311
|
+
expect( actionMocks.setZoom ).not.toHaveBeenCalled();
|
|
312
|
+
expect( actionMocks.setZoomAtPoint ).not.toHaveBeenCalled();
|
|
313
|
+
expect( actionMocks.snapRotate90 ).not.toHaveBeenCalled();
|
|
314
|
+
} );
|
|
315
|
+
|
|
316
|
+
it( 'calls onGestureStart on pointerdown and onGestureEnd on pointerup', () => {
|
|
317
|
+
const state = makeState( { zoom: 2 } );
|
|
318
|
+
const onGestureStart = jest.fn();
|
|
319
|
+
const onGestureEnd = jest.fn();
|
|
320
|
+
const { controller } = createController( state, {
|
|
321
|
+
onGestureStart,
|
|
322
|
+
onGestureEnd,
|
|
323
|
+
} );
|
|
324
|
+
const el = createMockElement();
|
|
325
|
+
|
|
326
|
+
controller.handlePointerDown( createPointerEvent(), el );
|
|
327
|
+
|
|
328
|
+
expect( onGestureStart ).toHaveBeenCalledTimes( 1 );
|
|
329
|
+
expect( onGestureEnd ).not.toHaveBeenCalled();
|
|
330
|
+
|
|
331
|
+
el._fire( 'pointerup', createPointerEvent() );
|
|
332
|
+
|
|
333
|
+
expect( onGestureEnd ).toHaveBeenCalledTimes( 1 );
|
|
334
|
+
} );
|
|
335
|
+
|
|
336
|
+
it( 'focuses the element on pointerdown', () => {
|
|
337
|
+
const state = makeState( { zoom: 2 } );
|
|
338
|
+
const { controller } = createController( state );
|
|
339
|
+
const el = createMockElement();
|
|
340
|
+
|
|
341
|
+
controller.handlePointerDown( createPointerEvent(), el );
|
|
342
|
+
|
|
343
|
+
expect( el.focus ).toHaveBeenCalled();
|
|
344
|
+
} );
|
|
345
|
+
|
|
346
|
+
it( 'reports isDragging via onStatusChange', () => {
|
|
347
|
+
const state = makeState( { zoom: 2 } );
|
|
348
|
+
const onStatusChange = jest.fn();
|
|
349
|
+
const { controller } = createController( state, {
|
|
350
|
+
onStatusChange,
|
|
351
|
+
} );
|
|
352
|
+
const el = createMockElement();
|
|
353
|
+
|
|
354
|
+
controller.handlePointerDown( createPointerEvent(), el );
|
|
355
|
+
|
|
356
|
+
expect( onStatusChange ).toHaveBeenCalledWith(
|
|
357
|
+
expect.objectContaining( { isDragging: true } )
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
onStatusChange.mockClear();
|
|
361
|
+
el._fire( 'pointerup', createPointerEvent() );
|
|
362
|
+
|
|
363
|
+
expect( onStatusChange ).toHaveBeenCalledWith(
|
|
364
|
+
expect.objectContaining( { isDragging: false } )
|
|
365
|
+
);
|
|
366
|
+
} );
|
|
367
|
+
} );
|
|
368
|
+
|
|
369
|
+
describe( 'wheel zoom', () => {
|
|
370
|
+
it( 'calls setZoom on wheel without currentTarget element', () => {
|
|
371
|
+
const state = makeState( { zoom: 2 } );
|
|
372
|
+
const { controller } = createController( state );
|
|
373
|
+
|
|
374
|
+
// No currentTarget — falls through to SET_ZOOM path.
|
|
375
|
+
controller.handleWheel(
|
|
376
|
+
createWheelEvent( { deltaY: -100, currentTarget: null } )
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
expect( actionMocks.setZoom ).toHaveBeenCalled();
|
|
380
|
+
|
|
381
|
+
const setZoomCall = actionMocks.setZoom.mock.calls[ 0 ];
|
|
382
|
+
// deltaY=-100, zoomSpeed=0.01, delta = 1, newZoom = 2+1 = 3.
|
|
383
|
+
expect( setZoomCall![ 0 ] ).toBe( 3 );
|
|
384
|
+
} );
|
|
385
|
+
|
|
386
|
+
it( 'calls setZoomAtPoint on wheel with currentTarget element', () => {
|
|
387
|
+
const state = makeState( { zoom: 2 } );
|
|
388
|
+
const { controller } = createController( state );
|
|
389
|
+
|
|
390
|
+
// Create a target that passes `instanceof Element` with the
|
|
391
|
+
// stub Element class registered above.
|
|
392
|
+
const target = Object.create(
|
|
393
|
+
( globalThis as any ).Element.prototype
|
|
394
|
+
);
|
|
395
|
+
target.getBoundingClientRect = () => ( {
|
|
396
|
+
left: 0,
|
|
397
|
+
top: 0,
|
|
398
|
+
width: 500,
|
|
399
|
+
height: 300,
|
|
400
|
+
} );
|
|
401
|
+
|
|
402
|
+
controller.handleWheel(
|
|
403
|
+
createWheelEvent( {
|
|
404
|
+
deltaY: -100,
|
|
405
|
+
clientX: 250,
|
|
406
|
+
clientY: 150,
|
|
407
|
+
currentTarget: target,
|
|
408
|
+
} )
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
expect( actionMocks.setZoomAtPoint ).toHaveBeenCalled();
|
|
412
|
+
expect( actionMocks.setZoomAtPoint.mock.calls[ 0 ][ 0 ] ).toBe( 3 );
|
|
413
|
+
} );
|
|
414
|
+
|
|
415
|
+
it( 'clamps to maxZoom on large positive wheel', () => {
|
|
416
|
+
const state = makeState( { zoom: 9 } );
|
|
417
|
+
const { controller } = createController( state );
|
|
418
|
+
|
|
419
|
+
controller.handleWheel(
|
|
420
|
+
createWheelEvent( { deltaY: -500, currentTarget: null } )
|
|
421
|
+
);
|
|
422
|
+
|
|
423
|
+
const setZoomCall = actionMocks.setZoom.mock.calls[ 0 ];
|
|
424
|
+
// 9 + 5 = 14, clamped to MAX_ZOOM (10).
|
|
425
|
+
expect( setZoomCall![ 0 ] ).toBe( MAX_ZOOM );
|
|
426
|
+
} );
|
|
427
|
+
|
|
428
|
+
it( 'clamps to minZoom on large negative wheel', () => {
|
|
429
|
+
const state = makeState( { zoom: 2 } );
|
|
430
|
+
const { controller } = createController( state );
|
|
431
|
+
|
|
432
|
+
controller.handleWheel(
|
|
433
|
+
createWheelEvent( { deltaY: 500, currentTarget: null } )
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
const setZoomCall = actionMocks.setZoom.mock.calls[ 0 ];
|
|
437
|
+
// 2 + (-5) = -3, clamped to MIN_ZOOM (1).
|
|
438
|
+
expect( setZoomCall![ 0 ] ).toBe( MIN_ZOOM );
|
|
439
|
+
} );
|
|
440
|
+
|
|
441
|
+
it( 'respects custom zoomSpeed (read lazily from options)', () => {
|
|
442
|
+
const state = makeState( { zoom: 2 } );
|
|
443
|
+
const { controller, opts } = createController( state, {
|
|
444
|
+
zoomSpeed: 0.01,
|
|
445
|
+
} );
|
|
446
|
+
|
|
447
|
+
// Mutate the option after construction to verify laziness.
|
|
448
|
+
( opts as Record< string, unknown > ).zoomSpeed = 0.02;
|
|
449
|
+
|
|
450
|
+
controller.handleWheel(
|
|
451
|
+
createWheelEvent( { deltaY: -100, currentTarget: null } )
|
|
452
|
+
);
|
|
453
|
+
|
|
454
|
+
const setZoomCall = actionMocks.setZoom.mock.calls[ 0 ];
|
|
455
|
+
// deltaY=-100, zoomSpeed=0.02, delta = 2, zoom = 2+2 = 4.
|
|
456
|
+
expect( setZoomCall![ 0 ] ).toBe( 4 );
|
|
457
|
+
} );
|
|
458
|
+
|
|
459
|
+
it( 'calls onGestureStart on first wheel, onGestureEnd after debounce', () => {
|
|
460
|
+
jest.useFakeTimers( {
|
|
461
|
+
doNotFake: [ 'requestAnimationFrame', 'cancelAnimationFrame' ],
|
|
462
|
+
} );
|
|
463
|
+
const state = makeState( { zoom: 2 } );
|
|
464
|
+
const onGestureStart = jest.fn();
|
|
465
|
+
const onGestureEnd = jest.fn();
|
|
466
|
+
const { controller } = createController( state, {
|
|
467
|
+
onGestureStart,
|
|
468
|
+
onGestureEnd,
|
|
469
|
+
} );
|
|
470
|
+
|
|
471
|
+
controller.handleWheel(
|
|
472
|
+
createWheelEvent( { deltaY: -50, currentTarget: null } )
|
|
473
|
+
);
|
|
474
|
+
|
|
475
|
+
expect( onGestureStart ).toHaveBeenCalledTimes( 1 );
|
|
476
|
+
expect( onGestureEnd ).not.toHaveBeenCalled();
|
|
477
|
+
|
|
478
|
+
// Another wheel event should not call onGestureStart again.
|
|
479
|
+
controller.handleWheel(
|
|
480
|
+
createWheelEvent( { deltaY: -50, currentTarget: null } )
|
|
481
|
+
);
|
|
482
|
+
expect( onGestureStart ).toHaveBeenCalledTimes( 1 );
|
|
483
|
+
|
|
484
|
+
// Advance past the 300ms debounce.
|
|
485
|
+
jest.advanceTimersByTime( 350 );
|
|
486
|
+
|
|
487
|
+
expect( onGestureEnd ).toHaveBeenCalledTimes( 1 );
|
|
488
|
+
|
|
489
|
+
jest.useRealTimers();
|
|
490
|
+
} );
|
|
491
|
+
} );
|
|
492
|
+
|
|
493
|
+
describe( 'keyboard', () => {
|
|
494
|
+
it( 'calls setPan on ArrowUp', () => {
|
|
495
|
+
const state = makeState( { zoom: 2 } );
|
|
496
|
+
const { controller } = createController( state );
|
|
497
|
+
|
|
498
|
+
controller.handleKeyDown( createKeyboardEvent( 'ArrowUp' ) );
|
|
499
|
+
|
|
500
|
+
expect( actionMocks.setPan ).toHaveBeenCalled();
|
|
501
|
+
|
|
502
|
+
const call = actionMocks.setPan.mock.calls[ 0 ];
|
|
503
|
+
// ArrowUp scrolls the viewport up — image moves down, so y increases.
|
|
504
|
+
expect( call![ 0 ].y ).toBeGreaterThanOrEqual( 0 );
|
|
505
|
+
} );
|
|
506
|
+
|
|
507
|
+
it( 'calls setPan on ArrowDown', () => {
|
|
508
|
+
const state = makeState( { zoom: 2 } );
|
|
509
|
+
const { controller } = createController( state );
|
|
510
|
+
|
|
511
|
+
controller.handleKeyDown( createKeyboardEvent( 'ArrowDown' ) );
|
|
512
|
+
|
|
513
|
+
expect( actionMocks.setPan ).toHaveBeenCalled();
|
|
514
|
+
} );
|
|
515
|
+
|
|
516
|
+
it( 'calls setPan on ArrowLeft', () => {
|
|
517
|
+
const state = makeState( { zoom: 2 } );
|
|
518
|
+
const { controller } = createController( state );
|
|
519
|
+
|
|
520
|
+
controller.handleKeyDown( createKeyboardEvent( 'ArrowLeft' ) );
|
|
521
|
+
|
|
522
|
+
expect( actionMocks.setPan ).toHaveBeenCalled();
|
|
523
|
+
|
|
524
|
+
const call = actionMocks.setPan.mock.calls[ 0 ];
|
|
525
|
+
// ArrowLeft scrolls the viewport left — image moves right, so x increases.
|
|
526
|
+
expect( call![ 0 ].x ).toBeGreaterThanOrEqual( 0 );
|
|
527
|
+
} );
|
|
528
|
+
|
|
529
|
+
it( 'calls setPan on ArrowRight', () => {
|
|
530
|
+
const state = makeState( { zoom: 2 } );
|
|
531
|
+
const { controller } = createController( state );
|
|
532
|
+
|
|
533
|
+
controller.handleKeyDown( createKeyboardEvent( 'ArrowRight' ) );
|
|
534
|
+
|
|
535
|
+
expect( actionMocks.setPan ).toHaveBeenCalled();
|
|
536
|
+
} );
|
|
537
|
+
|
|
538
|
+
it( 'calls setZoom on + key', () => {
|
|
539
|
+
const state = makeState( { zoom: 2 } );
|
|
540
|
+
const { controller } = createController( state );
|
|
541
|
+
|
|
542
|
+
controller.handleKeyDown( createKeyboardEvent( '+' ) );
|
|
543
|
+
|
|
544
|
+
expect( actionMocks.setZoom ).toHaveBeenCalled();
|
|
545
|
+
|
|
546
|
+
const call = actionMocks.setZoom.mock.calls[ 0 ];
|
|
547
|
+
// 2 + 0.5 = 2.5.
|
|
548
|
+
expect( call![ 0 ] ).toBe( 2.5 );
|
|
549
|
+
} );
|
|
550
|
+
|
|
551
|
+
it( 'calls setZoom on - key', () => {
|
|
552
|
+
const state = makeState( { zoom: 3 } );
|
|
553
|
+
const { controller } = createController( state );
|
|
554
|
+
|
|
555
|
+
controller.handleKeyDown( createKeyboardEvent( '-' ) );
|
|
556
|
+
|
|
557
|
+
const call = actionMocks.setZoom.mock.calls[ 0 ];
|
|
558
|
+
// 3 - 0.5 = 2.5.
|
|
559
|
+
expect( call![ 0 ] ).toBe( 2.5 );
|
|
560
|
+
} );
|
|
561
|
+
|
|
562
|
+
it( 'calls snapRotate90 on r key', () => {
|
|
563
|
+
const state = makeState( { rotation: 0 } );
|
|
564
|
+
const { controller } = createController( state );
|
|
565
|
+
|
|
566
|
+
controller.handleKeyDown( createKeyboardEvent( 'r' ) );
|
|
567
|
+
|
|
568
|
+
expect( actionMocks.snapRotate90 ).toHaveBeenCalledWith( 1 );
|
|
569
|
+
} );
|
|
570
|
+
|
|
571
|
+
it( 'calls snapRotate90 on R key', () => {
|
|
572
|
+
const state = makeState( { rotation: 90 } );
|
|
573
|
+
const { controller } = createController( state );
|
|
574
|
+
|
|
575
|
+
controller.handleKeyDown( createKeyboardEvent( 'R' ) );
|
|
576
|
+
|
|
577
|
+
expect( actionMocks.snapRotate90 ).toHaveBeenCalledWith( 1 );
|
|
578
|
+
} );
|
|
579
|
+
|
|
580
|
+
it.each( [ 'metaKey', 'ctrlKey', 'altKey', 'shiftKey' ] )(
|
|
581
|
+
'does not rotate when %s is held with r',
|
|
582
|
+
( modifier ) => {
|
|
583
|
+
const state = makeState( { rotation: 0 } );
|
|
584
|
+
const { controller } = createController( state );
|
|
585
|
+
|
|
586
|
+
controller.handleKeyDown(
|
|
587
|
+
createKeyboardEvent( 'r', { [ modifier ]: true } )
|
|
588
|
+
);
|
|
589
|
+
|
|
590
|
+
expect( actionMocks.setPan ).not.toHaveBeenCalled();
|
|
591
|
+
expect( actionMocks.setZoom ).not.toHaveBeenCalled();
|
|
592
|
+
expect( actionMocks.setZoomAtPoint ).not.toHaveBeenCalled();
|
|
593
|
+
expect( actionMocks.snapRotate90 ).not.toHaveBeenCalled();
|
|
594
|
+
}
|
|
595
|
+
);
|
|
596
|
+
|
|
597
|
+
it( 'respects custom keyboardStep (read lazily)', () => {
|
|
598
|
+
const state = makeState( { zoom: 2 } );
|
|
599
|
+
const { controller, opts } = createController( state, {
|
|
600
|
+
keyboardStep: 0.05,
|
|
601
|
+
} );
|
|
602
|
+
|
|
603
|
+
// Mutate after construction.
|
|
604
|
+
( opts as Record< string, unknown > ).keyboardStep = 0.1;
|
|
605
|
+
|
|
606
|
+
controller.handleKeyDown( createKeyboardEvent( 'ArrowRight' ) );
|
|
607
|
+
|
|
608
|
+
const call = actionMocks.setPan.mock.calls[ 0 ];
|
|
609
|
+
// ArrowRight scrolls the viewport right — image moves left, so x decreases.
|
|
610
|
+
// 0 - 0.1 = -0.1, within bounds.
|
|
611
|
+
expect( call![ 0 ].x ).toBeCloseTo( -0.1 );
|
|
612
|
+
} );
|
|
613
|
+
|
|
614
|
+
it( 'does not dispatch on unhandled keys', () => {
|
|
615
|
+
const state = makeState();
|
|
616
|
+
const { controller } = createController( state );
|
|
617
|
+
|
|
618
|
+
controller.handleKeyDown( createKeyboardEvent( 'a' ) );
|
|
619
|
+
|
|
620
|
+
expect( actionMocks.setPan ).not.toHaveBeenCalled();
|
|
621
|
+
expect( actionMocks.setZoom ).not.toHaveBeenCalled();
|
|
622
|
+
expect( actionMocks.setZoomAtPoint ).not.toHaveBeenCalled();
|
|
623
|
+
expect( actionMocks.snapRotate90 ).not.toHaveBeenCalled();
|
|
624
|
+
} );
|
|
625
|
+
} );
|
|
626
|
+
|
|
627
|
+
describe( 'touch', () => {
|
|
628
|
+
it( 'single-finger pan calls setPan on first move', () => {
|
|
629
|
+
const state = makeState( { zoom: 2 } );
|
|
630
|
+
const { controller } = createController( state );
|
|
631
|
+
const doc = createMockDocument();
|
|
632
|
+
const rect = createContainerRect();
|
|
633
|
+
|
|
634
|
+
controller.handleTouchStart(
|
|
635
|
+
createTouchEvent( [ { clientX: 100, clientY: 100 } ] ),
|
|
636
|
+
rect,
|
|
637
|
+
doc
|
|
638
|
+
);
|
|
639
|
+
|
|
640
|
+
// Simulate touchmove — pan should start on first move, no delay.
|
|
641
|
+
doc._fire(
|
|
642
|
+
'touchmove',
|
|
643
|
+
createTouchEvent( [ { clientX: 150, clientY: 120 } ] )
|
|
644
|
+
);
|
|
645
|
+
|
|
646
|
+
expect( actionMocks.setPan ).toHaveBeenCalled();
|
|
647
|
+
} );
|
|
648
|
+
|
|
649
|
+
it( 'calls onGestureStart/onGestureEnd for single-finger pan', () => {
|
|
650
|
+
const state = makeState( { zoom: 2 } );
|
|
651
|
+
const onGestureStart = jest.fn();
|
|
652
|
+
const onGestureEnd = jest.fn();
|
|
653
|
+
const { controller } = createController( state, {
|
|
654
|
+
onGestureStart,
|
|
655
|
+
onGestureEnd,
|
|
656
|
+
} );
|
|
657
|
+
const doc = createMockDocument();
|
|
658
|
+
const rect = createContainerRect();
|
|
659
|
+
|
|
660
|
+
controller.handleTouchStart(
|
|
661
|
+
createTouchEvent( [ { clientX: 100, clientY: 100 } ] ),
|
|
662
|
+
rect,
|
|
663
|
+
doc
|
|
664
|
+
);
|
|
665
|
+
|
|
666
|
+
expect( onGestureStart ).toHaveBeenCalledTimes( 1 );
|
|
667
|
+
expect( onGestureEnd ).not.toHaveBeenCalled();
|
|
668
|
+
|
|
669
|
+
doc._fire( 'touchend', createTouchEvent( [] ) );
|
|
670
|
+
|
|
671
|
+
expect( onGestureEnd ).toHaveBeenCalledTimes( 1 );
|
|
672
|
+
} );
|
|
673
|
+
|
|
674
|
+
it( 'reports isDragging on first single-finger move', () => {
|
|
675
|
+
const state = makeState( { zoom: 2 } );
|
|
676
|
+
const onStatusChange = jest.fn();
|
|
677
|
+
const { controller } = createController( state, {
|
|
678
|
+
onStatusChange,
|
|
679
|
+
} );
|
|
680
|
+
const doc = createMockDocument();
|
|
681
|
+
const rect = createContainerRect();
|
|
682
|
+
|
|
683
|
+
controller.handleTouchStart(
|
|
684
|
+
createTouchEvent( [ { clientX: 100, clientY: 100 } ] ),
|
|
685
|
+
rect,
|
|
686
|
+
doc
|
|
687
|
+
);
|
|
688
|
+
|
|
689
|
+
// isDragging is not set at touchstart — only on first move.
|
|
690
|
+
expect( onStatusChange ).not.toHaveBeenCalledWith(
|
|
691
|
+
expect.objectContaining( { isDragging: true } )
|
|
692
|
+
);
|
|
693
|
+
|
|
694
|
+
// First move triggers isDragging.
|
|
695
|
+
doc._fire(
|
|
696
|
+
'touchmove',
|
|
697
|
+
createTouchEvent( [ { clientX: 105, clientY: 105 } ] )
|
|
698
|
+
);
|
|
699
|
+
|
|
700
|
+
expect( onStatusChange ).toHaveBeenCalledWith(
|
|
701
|
+
expect.objectContaining( { isDragging: true } )
|
|
702
|
+
);
|
|
703
|
+
|
|
704
|
+
onStatusChange.mockClear();
|
|
705
|
+
doc._fire( 'touchend', createTouchEvent( [] ) );
|
|
706
|
+
|
|
707
|
+
expect( onStatusChange ).toHaveBeenCalledWith(
|
|
708
|
+
expect.objectContaining( { isDragging: false } )
|
|
709
|
+
);
|
|
710
|
+
} );
|
|
711
|
+
|
|
712
|
+
it( 'pinch zoom calls setZoomAtPoint atomically', () => {
|
|
713
|
+
const state = makeState( { zoom: 1 } );
|
|
714
|
+
const { controller } = createController( state );
|
|
715
|
+
const doc = createMockDocument();
|
|
716
|
+
const rect = createContainerRect();
|
|
717
|
+
|
|
718
|
+
// Two-finger pinch start.
|
|
719
|
+
controller.handleTouchStart(
|
|
720
|
+
createTouchEvent( [
|
|
721
|
+
{ clientX: 200, clientY: 150 },
|
|
722
|
+
{ clientX: 300, clientY: 150 },
|
|
723
|
+
] ),
|
|
724
|
+
rect,
|
|
725
|
+
doc
|
|
726
|
+
);
|
|
727
|
+
|
|
728
|
+
// Spread fingers apart — increase distance.
|
|
729
|
+
doc._fire(
|
|
730
|
+
'touchmove',
|
|
731
|
+
createTouchEvent( [
|
|
732
|
+
{ clientX: 150, clientY: 150 },
|
|
733
|
+
{ clientX: 350, clientY: 150 },
|
|
734
|
+
] )
|
|
735
|
+
);
|
|
736
|
+
|
|
737
|
+
expect( actionMocks.setZoomAtPoint ).toHaveBeenCalled();
|
|
738
|
+
} );
|
|
739
|
+
|
|
740
|
+
it( 'calls onGestureStart for pinch, onGestureEnd on touchend', () => {
|
|
741
|
+
const state = makeState( { zoom: 1 } );
|
|
742
|
+
const onGestureStart = jest.fn();
|
|
743
|
+
const onGestureEnd = jest.fn();
|
|
744
|
+
const { controller } = createController( state, {
|
|
745
|
+
onGestureStart,
|
|
746
|
+
onGestureEnd,
|
|
747
|
+
} );
|
|
748
|
+
const doc = createMockDocument();
|
|
749
|
+
const rect = createContainerRect();
|
|
750
|
+
|
|
751
|
+
controller.handleTouchStart(
|
|
752
|
+
createTouchEvent( [
|
|
753
|
+
{ clientX: 200, clientY: 150 },
|
|
754
|
+
{ clientX: 300, clientY: 150 },
|
|
755
|
+
] ),
|
|
756
|
+
rect,
|
|
757
|
+
doc
|
|
758
|
+
);
|
|
759
|
+
|
|
760
|
+
expect( onGestureStart ).toHaveBeenCalledTimes( 1 );
|
|
761
|
+
|
|
762
|
+
doc._fire( 'touchend', createTouchEvent( [] ) );
|
|
763
|
+
|
|
764
|
+
expect( onGestureEnd ).toHaveBeenCalledTimes( 1 );
|
|
765
|
+
} );
|
|
766
|
+
|
|
767
|
+
it( 'late second finger switches from pan to pinch', () => {
|
|
768
|
+
const state = makeState( { zoom: 2 } );
|
|
769
|
+
const { controller } = createController( state );
|
|
770
|
+
const doc = createMockDocument();
|
|
771
|
+
const rect = createContainerRect();
|
|
772
|
+
|
|
773
|
+
// First finger lands alone.
|
|
774
|
+
controller.handleTouchStart(
|
|
775
|
+
createTouchEvent( [ { clientX: 200, clientY: 150 } ] ),
|
|
776
|
+
rect,
|
|
777
|
+
doc
|
|
778
|
+
);
|
|
779
|
+
|
|
780
|
+
// First move with 1 finger — starts pan.
|
|
781
|
+
doc._fire(
|
|
782
|
+
'touchmove',
|
|
783
|
+
createTouchEvent( [ { clientX: 210, clientY: 155 } ] )
|
|
784
|
+
);
|
|
785
|
+
expect( actionMocks.setPan ).toHaveBeenCalled();
|
|
786
|
+
jest.clearAllMocks();
|
|
787
|
+
|
|
788
|
+
// Second finger arrives via touchstart.
|
|
789
|
+
controller.handleTouchStart(
|
|
790
|
+
createTouchEvent( [
|
|
791
|
+
{ clientX: 200, clientY: 150 },
|
|
792
|
+
{ clientX: 350, clientY: 150 },
|
|
793
|
+
] ),
|
|
794
|
+
rect,
|
|
795
|
+
doc
|
|
796
|
+
);
|
|
797
|
+
|
|
798
|
+
// Move with 2 fingers — should pinch, not pan.
|
|
799
|
+
doc._fire(
|
|
800
|
+
'touchmove',
|
|
801
|
+
createTouchEvent( [
|
|
802
|
+
{ clientX: 180, clientY: 150 },
|
|
803
|
+
{ clientX: 370, clientY: 150 },
|
|
804
|
+
] )
|
|
805
|
+
);
|
|
806
|
+
|
|
807
|
+
expect( actionMocks.setZoomAtPoint ).toHaveBeenCalled();
|
|
808
|
+
// Should NOT have panned after switching.
|
|
809
|
+
expect( actionMocks.setPan ).not.toHaveBeenCalled();
|
|
810
|
+
} );
|
|
811
|
+
|
|
812
|
+
it( 'mid-move second finger triggers pinch without touchstart', () => {
|
|
813
|
+
const state = makeState( { zoom: 2 } );
|
|
814
|
+
const { controller } = createController( state );
|
|
815
|
+
const doc = createMockDocument();
|
|
816
|
+
const rect = createContainerRect();
|
|
817
|
+
|
|
818
|
+
// First finger lands.
|
|
819
|
+
controller.handleTouchStart(
|
|
820
|
+
createTouchEvent( [ { clientX: 200, clientY: 150 } ] ),
|
|
821
|
+
rect,
|
|
822
|
+
doc
|
|
823
|
+
);
|
|
824
|
+
|
|
825
|
+
// First touchmove already has 2 fingers (browser may batch).
|
|
826
|
+
doc._fire(
|
|
827
|
+
'touchmove',
|
|
828
|
+
createTouchEvent( [
|
|
829
|
+
{ clientX: 200, clientY: 150 },
|
|
830
|
+
{ clientX: 350, clientY: 150 },
|
|
831
|
+
] )
|
|
832
|
+
);
|
|
833
|
+
|
|
834
|
+
// First 2-finger move initializes pinch state, no dispatch yet.
|
|
835
|
+
expect( actionMocks.setPan ).not.toHaveBeenCalled();
|
|
836
|
+
expect( actionMocks.setZoom ).not.toHaveBeenCalled();
|
|
837
|
+
expect( actionMocks.setZoomAtPoint ).not.toHaveBeenCalled();
|
|
838
|
+
expect( actionMocks.snapRotate90 ).not.toHaveBeenCalled();
|
|
839
|
+
|
|
840
|
+
// Second 2-finger move dispatches pinch zoom.
|
|
841
|
+
doc._fire(
|
|
842
|
+
'touchmove',
|
|
843
|
+
createTouchEvent( [
|
|
844
|
+
{ clientX: 180, clientY: 150 },
|
|
845
|
+
{ clientX: 370, clientY: 150 },
|
|
846
|
+
] )
|
|
847
|
+
);
|
|
848
|
+
|
|
849
|
+
expect( actionMocks.setZoomAtPoint ).toHaveBeenCalled();
|
|
850
|
+
} );
|
|
851
|
+
|
|
852
|
+
it( 'does not switch to pan after pinch finger is lifted', () => {
|
|
853
|
+
const state = makeState( { zoom: 2 } );
|
|
854
|
+
const { controller } = createController( state );
|
|
855
|
+
const doc = createMockDocument();
|
|
856
|
+
const rect = createContainerRect();
|
|
857
|
+
|
|
858
|
+
// Two fingers land simultaneously.
|
|
859
|
+
controller.handleTouchStart(
|
|
860
|
+
createTouchEvent( [
|
|
861
|
+
{ clientX: 200, clientY: 150 },
|
|
862
|
+
{ clientX: 350, clientY: 150 },
|
|
863
|
+
] ),
|
|
864
|
+
rect,
|
|
865
|
+
doc
|
|
866
|
+
);
|
|
867
|
+
|
|
868
|
+
// Pinch move.
|
|
869
|
+
doc._fire(
|
|
870
|
+
'touchmove',
|
|
871
|
+
createTouchEvent( [
|
|
872
|
+
{ clientX: 180, clientY: 150 },
|
|
873
|
+
{ clientX: 370, clientY: 150 },
|
|
874
|
+
] )
|
|
875
|
+
);
|
|
876
|
+
|
|
877
|
+
jest.clearAllMocks();
|
|
878
|
+
|
|
879
|
+
// One finger lifts — move with 1 touch should NOT pan
|
|
880
|
+
// because didPinch is true.
|
|
881
|
+
doc._fire(
|
|
882
|
+
'touchmove',
|
|
883
|
+
createTouchEvent( [ { clientX: 250, clientY: 160 } ] )
|
|
884
|
+
);
|
|
885
|
+
|
|
886
|
+
expect( actionMocks.setPan ).not.toHaveBeenCalled();
|
|
887
|
+
} );
|
|
888
|
+
} );
|
|
889
|
+
|
|
890
|
+
describe( 'lazy options', () => {
|
|
891
|
+
it( 'changing options.minZoom after construction affects behavior', () => {
|
|
892
|
+
const state = makeState( { zoom: 2 } );
|
|
893
|
+
const { controller, opts } = createController( state, {
|
|
894
|
+
minZoom: MIN_ZOOM,
|
|
895
|
+
} );
|
|
896
|
+
|
|
897
|
+
// Raise minZoom to 1.5 — a large negative wheel should clamp there.
|
|
898
|
+
( opts as Record< string, unknown > ).minZoom = 1.5;
|
|
899
|
+
|
|
900
|
+
controller.handleWheel(
|
|
901
|
+
createWheelEvent( { deltaY: 500, currentTarget: null } )
|
|
902
|
+
);
|
|
903
|
+
|
|
904
|
+
const setZoomCall = actionMocks.setZoom.mock.calls[ 0 ];
|
|
905
|
+
expect( setZoomCall![ 0 ] ).toBe( 1.5 );
|
|
906
|
+
} );
|
|
907
|
+
} );
|
|
908
|
+
|
|
909
|
+
describe( 'destroy', () => {
|
|
910
|
+
it( 'cleans up active touch listeners', () => {
|
|
911
|
+
const state = makeState( { zoom: 2 } );
|
|
912
|
+
const { controller } = createController( state );
|
|
913
|
+
const doc = createMockDocument();
|
|
914
|
+
const rect = createContainerRect();
|
|
915
|
+
|
|
916
|
+
controller.handleTouchStart(
|
|
917
|
+
createTouchEvent( [ { clientX: 100, clientY: 100 } ] ),
|
|
918
|
+
rect,
|
|
919
|
+
doc
|
|
920
|
+
);
|
|
921
|
+
|
|
922
|
+
controller.destroy();
|
|
923
|
+
|
|
924
|
+
// Touch listeners should have been removed via touchCleanup.
|
|
925
|
+
expect( doc.removeEventListener ).toHaveBeenCalledWith(
|
|
926
|
+
'touchmove',
|
|
927
|
+
expect.any( Function )
|
|
928
|
+
);
|
|
929
|
+
expect( doc.removeEventListener ).toHaveBeenCalledWith(
|
|
930
|
+
'touchend',
|
|
931
|
+
expect.any( Function )
|
|
932
|
+
);
|
|
933
|
+
expect( doc.removeEventListener ).toHaveBeenCalledWith(
|
|
934
|
+
'touchcancel',
|
|
935
|
+
expect.any( Function )
|
|
936
|
+
);
|
|
937
|
+
} );
|
|
938
|
+
|
|
939
|
+
it( 'cleans up active pointer drag listeners', () => {
|
|
940
|
+
const state = makeState( { zoom: 2 } );
|
|
941
|
+
const { controller } = createController( state );
|
|
942
|
+
const el = createMockElement();
|
|
943
|
+
|
|
944
|
+
// Start a pointer drag — this registers move/up/lostcapture.
|
|
945
|
+
controller.handlePointerDown(
|
|
946
|
+
createPointerEvent( { clientX: 100, clientY: 100 } ),
|
|
947
|
+
el
|
|
948
|
+
);
|
|
949
|
+
expect( el.addEventListener ).toHaveBeenCalledWith(
|
|
950
|
+
'pointermove',
|
|
951
|
+
expect.any( Function )
|
|
952
|
+
);
|
|
953
|
+
|
|
954
|
+
// Destroy mid-drag without dispatching pointerup.
|
|
955
|
+
controller.destroy();
|
|
956
|
+
|
|
957
|
+
expect( el.removeEventListener ).toHaveBeenCalledWith(
|
|
958
|
+
'pointermove',
|
|
959
|
+
expect.any( Function )
|
|
960
|
+
);
|
|
961
|
+
expect( el.removeEventListener ).toHaveBeenCalledWith(
|
|
962
|
+
'pointerup',
|
|
963
|
+
expect.any( Function )
|
|
964
|
+
);
|
|
965
|
+
expect( el.removeEventListener ).toHaveBeenCalledWith(
|
|
966
|
+
'lostpointercapture',
|
|
967
|
+
expect.any( Function )
|
|
968
|
+
);
|
|
969
|
+
} );
|
|
970
|
+
|
|
971
|
+
it( 'does not dispatch if pointermove fires after destroy', () => {
|
|
972
|
+
const state = makeState( { zoom: 2 } );
|
|
973
|
+
const { controller } = createController( state );
|
|
974
|
+
const el = createMockElement();
|
|
975
|
+
|
|
976
|
+
controller.handlePointerDown(
|
|
977
|
+
createPointerEvent( { clientX: 100, clientY: 100 } ),
|
|
978
|
+
el
|
|
979
|
+
);
|
|
980
|
+
jest.clearAllMocks();
|
|
981
|
+
|
|
982
|
+
controller.destroy();
|
|
983
|
+
|
|
984
|
+
// A late pointermove event (e.g. queued before unmount) must
|
|
985
|
+
// not reach the handler — listeners were removed.
|
|
986
|
+
el._fire(
|
|
987
|
+
'pointermove',
|
|
988
|
+
createPointerEvent( { clientX: 150, clientY: 100 } )
|
|
989
|
+
);
|
|
990
|
+
expect( actionMocks.setPan ).not.toHaveBeenCalled();
|
|
991
|
+
expect( actionMocks.setZoom ).not.toHaveBeenCalled();
|
|
992
|
+
expect( actionMocks.setZoomAtPoint ).not.toHaveBeenCalled();
|
|
993
|
+
expect( actionMocks.snapRotate90 ).not.toHaveBeenCalled();
|
|
994
|
+
} );
|
|
995
|
+
} );
|
|
996
|
+
} );
|