@wordpress/media-editor 0.7.1-next.v.202604201441.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/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/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/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 -7
- 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/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,762 @@
|
|
|
1
|
+
import type { CropperState, Size } from '../types';
|
|
2
|
+
import { DEFAULT_STATE } from '../constants';
|
|
3
|
+
import { cropperReducer, enforceContainment } from '../state';
|
|
4
|
+
import {
|
|
5
|
+
createCamera,
|
|
6
|
+
screenToWorld,
|
|
7
|
+
getVisibleBounds,
|
|
8
|
+
getImageFit,
|
|
9
|
+
} from '../camera';
|
|
10
|
+
|
|
11
|
+
const IMAGE: Size = { width: 1600, height: 900 };
|
|
12
|
+
const CONTAINER: Size = { width: 800, height: 600 };
|
|
13
|
+
|
|
14
|
+
function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
15
|
+
const merged = {
|
|
16
|
+
...DEFAULT_STATE,
|
|
17
|
+
image: {
|
|
18
|
+
src: 'test.jpg',
|
|
19
|
+
naturalWidth: IMAGE.width,
|
|
20
|
+
naturalHeight: IMAGE.height,
|
|
21
|
+
},
|
|
22
|
+
...overrides,
|
|
23
|
+
};
|
|
24
|
+
// Default base fields to the live values so tests setting `zoom`,
|
|
25
|
+
// `crop`, or `rotation` represent a user who explicitly committed
|
|
26
|
+
// to that pose. Tests that want to exercise base/live divergence
|
|
27
|
+
// can set the base fields explicitly in overrides.
|
|
28
|
+
if ( overrides.baseZoom === undefined ) {
|
|
29
|
+
merged.baseZoom = merged.zoom;
|
|
30
|
+
}
|
|
31
|
+
if ( overrides.basePan === undefined ) {
|
|
32
|
+
merged.basePan = { x: merged.pan.x, y: merged.pan.y };
|
|
33
|
+
}
|
|
34
|
+
if ( overrides.baseRotation === undefined ) {
|
|
35
|
+
merged.baseRotation = merged.rotation;
|
|
36
|
+
}
|
|
37
|
+
return merged;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Get the world-space region visible through the crop rect.
|
|
42
|
+
*
|
|
43
|
+
* Projects the four crop-rect corners from screen space back to
|
|
44
|
+
* normalized world space [0,1], returning the axis-aligned bounding
|
|
45
|
+
* box. This tells us exactly what part of the image the user sees
|
|
46
|
+
* inside the crop area.
|
|
47
|
+
*
|
|
48
|
+
* @param state The cropper state to inspect.
|
|
49
|
+
* @param imageSize The natural image dimensions.
|
|
50
|
+
* @param containerSize The container dimensions.
|
|
51
|
+
* @return The world-space AABB of the visible region.
|
|
52
|
+
*/
|
|
53
|
+
function getCropWorldRegion(
|
|
54
|
+
state: CropperState,
|
|
55
|
+
imageSize: Size,
|
|
56
|
+
containerSize: Size
|
|
57
|
+
): { minX: number; minY: number; maxX: number; maxY: number } {
|
|
58
|
+
const camera = createCamera( state, containerSize, imageSize );
|
|
59
|
+
|
|
60
|
+
// Build base camera (zero pan, zoom=1) for stencil positioning.
|
|
61
|
+
const baseCamera = createCamera(
|
|
62
|
+
{ ...state, pan: { x: 0, y: 0 }, zoom: 1 },
|
|
63
|
+
containerSize,
|
|
64
|
+
imageSize
|
|
65
|
+
);
|
|
66
|
+
const vb = getVisibleBounds( baseCamera );
|
|
67
|
+
const cr = state.cropRect;
|
|
68
|
+
|
|
69
|
+
// Stencil corners in screen space.
|
|
70
|
+
const corners: { x: number; y: number }[] = [
|
|
71
|
+
{ x: vb.left + cr.x * vb.width, y: vb.top + cr.y * vb.height },
|
|
72
|
+
{
|
|
73
|
+
x: vb.left + ( cr.x + cr.width ) * vb.width,
|
|
74
|
+
y: vb.top + cr.y * vb.height,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
x: vb.left + ( cr.x + cr.width ) * vb.width,
|
|
78
|
+
y: vb.top + ( cr.y + cr.height ) * vb.height,
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
x: vb.left + cr.x * vb.width,
|
|
82
|
+
y: vb.top + ( cr.y + cr.height ) * vb.height,
|
|
83
|
+
},
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
// Map to world space via inverse camera.
|
|
87
|
+
const worldPoints = corners.map( ( c ) => screenToWorld( camera, c ) );
|
|
88
|
+
return {
|
|
89
|
+
minX: Math.min( ...worldPoints.map( ( p ) => p.x ) ),
|
|
90
|
+
minY: Math.min( ...worldPoints.map( ( p ) => p.y ) ),
|
|
91
|
+
maxX: Math.max( ...worldPoints.map( ( p ) => p.x ) ),
|
|
92
|
+
maxY: Math.max( ...worldPoints.map( ( p ) => p.y ) ),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
describe( 'enforceContainment', () => {
|
|
97
|
+
it( 'is nearly a no-op at default state', () => {
|
|
98
|
+
const state = makeState();
|
|
99
|
+
const result = enforceContainment( state );
|
|
100
|
+
// Floating-point epsilon may introduce tiny drift (~1e-9),
|
|
101
|
+
// so we check closeness rather than reference equality.
|
|
102
|
+
expect( result.pan.x ).toBeCloseTo( 0, 5 );
|
|
103
|
+
expect( result.pan.y ).toBeCloseTo( 0, 5 );
|
|
104
|
+
expect( result.zoom ).toBeCloseTo( 1, 5 );
|
|
105
|
+
expect( result.cropRect ).toEqual( state.cropRect );
|
|
106
|
+
} );
|
|
107
|
+
|
|
108
|
+
it( 'bumps zoom before shrinking crop for small rotation', () => {
|
|
109
|
+
const state = makeState( {
|
|
110
|
+
rotation: 15,
|
|
111
|
+
zoom: 1,
|
|
112
|
+
} );
|
|
113
|
+
const result = enforceContainment( state );
|
|
114
|
+
// Zoom should be bumped to cover the rotated image.
|
|
115
|
+
expect( result.zoom ).toBeGreaterThan( 1 );
|
|
116
|
+
// Crop rect should remain full size (zoom was bumped instead of shrinking crop).
|
|
117
|
+
expect( result.cropRect.width ).toBeCloseTo( 1, 2 );
|
|
118
|
+
expect( result.cropRect.height ).toBeCloseTo( 1, 2 );
|
|
119
|
+
} );
|
|
120
|
+
|
|
121
|
+
it( 'returns same reference when nothing changes', () => {
|
|
122
|
+
const state = makeState( { zoom: 2 } );
|
|
123
|
+
const enforced = enforceContainment( state );
|
|
124
|
+
const enforced2 = enforceContainment( enforced );
|
|
125
|
+
expect( enforced2 ).toBe( enforced );
|
|
126
|
+
} );
|
|
127
|
+
} );
|
|
128
|
+
|
|
129
|
+
describe( 'cropperReducer — SETTLE_CROP', () => {
|
|
130
|
+
/**
|
|
131
|
+
* Assert that the world-space region visible through the crop is
|
|
132
|
+
* the same before and after SETTLE_CROP, within a tolerance.
|
|
133
|
+
*
|
|
134
|
+
* @param before State before settle.
|
|
135
|
+
* @param after State after settle.
|
|
136
|
+
* @param tolerance Allowed difference per axis (default 0.02).
|
|
137
|
+
*/
|
|
138
|
+
function expectSameVisibleRegion(
|
|
139
|
+
before: CropperState,
|
|
140
|
+
after: CropperState,
|
|
141
|
+
tolerance = 0.02
|
|
142
|
+
) {
|
|
143
|
+
const regionBefore = getCropWorldRegion( before, IMAGE, CONTAINER );
|
|
144
|
+
const regionAfter = getCropWorldRegion( after, IMAGE, CONTAINER );
|
|
145
|
+
|
|
146
|
+
expect( regionAfter.minX ).toBeCloseTo(
|
|
147
|
+
regionBefore.minX,
|
|
148
|
+
-Math.log10( tolerance )
|
|
149
|
+
);
|
|
150
|
+
expect( regionAfter.minY ).toBeCloseTo(
|
|
151
|
+
regionBefore.minY,
|
|
152
|
+
-Math.log10( tolerance )
|
|
153
|
+
);
|
|
154
|
+
expect( regionAfter.maxX ).toBeCloseTo(
|
|
155
|
+
regionBefore.maxX,
|
|
156
|
+
-Math.log10( tolerance )
|
|
157
|
+
);
|
|
158
|
+
expect( regionAfter.maxY ).toBeCloseTo(
|
|
159
|
+
regionBefore.maxY,
|
|
160
|
+
-Math.log10( tolerance )
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
it( 'preserves image selection for a centered small crop', () => {
|
|
165
|
+
const state = makeState( {
|
|
166
|
+
cropRect: { x: 0.25, y: 0.25, width: 0.5, height: 0.5 },
|
|
167
|
+
zoom: 1,
|
|
168
|
+
pan: { x: 0, y: 0 },
|
|
169
|
+
} );
|
|
170
|
+
const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
|
|
171
|
+
|
|
172
|
+
// Crop rect should expand to fill the visual area.
|
|
173
|
+
expect( settled.cropRect.width ).toBeGreaterThan( 0.9 );
|
|
174
|
+
// Zoom should increase to compensate for the larger crop.
|
|
175
|
+
expect( settled.zoom ).toBeGreaterThan( state.zoom );
|
|
176
|
+
// The same image region should be visible.
|
|
177
|
+
expectSameVisibleRegion( state, settled );
|
|
178
|
+
} );
|
|
179
|
+
|
|
180
|
+
it( 'preserves image selection for an off-center crop', () => {
|
|
181
|
+
const state = makeState( {
|
|
182
|
+
cropRect: { x: 0.1, y: 0.1, width: 0.4, height: 0.3 },
|
|
183
|
+
zoom: 1.5,
|
|
184
|
+
pan: { x: 0.05, y: -0.02 },
|
|
185
|
+
} );
|
|
186
|
+
const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
|
|
187
|
+
|
|
188
|
+
expect( settled.zoom ).toBeGreaterThan( 0 );
|
|
189
|
+
expectSameVisibleRegion( state, settled );
|
|
190
|
+
} );
|
|
191
|
+
|
|
192
|
+
it( 'preserves image selection for a wide crop (landscape)', () => {
|
|
193
|
+
// Wide crop: fills width, short in height.
|
|
194
|
+
const state = makeState( {
|
|
195
|
+
cropRect: { x: 0.0, y: 0.3, width: 1.0, height: 0.4 },
|
|
196
|
+
zoom: 1,
|
|
197
|
+
pan: { x: 0, y: 0 },
|
|
198
|
+
} );
|
|
199
|
+
const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
|
|
200
|
+
|
|
201
|
+
// Aspect ratio of crop should be preserved.
|
|
202
|
+
const ratioBefore = state.cropRect.width / state.cropRect.height;
|
|
203
|
+
const ratioAfter = settled.cropRect.width / settled.cropRect.height;
|
|
204
|
+
expect( ratioAfter ).toBeCloseTo( ratioBefore, 2 );
|
|
205
|
+
|
|
206
|
+
expectSameVisibleRegion( state, settled );
|
|
207
|
+
} );
|
|
208
|
+
|
|
209
|
+
it( 'preserves image selection for a tall crop (portrait)', () => {
|
|
210
|
+
const state = makeState( {
|
|
211
|
+
cropRect: { x: 0.3, y: 0.0, width: 0.4, height: 1.0 },
|
|
212
|
+
zoom: 1,
|
|
213
|
+
pan: { x: 0, y: 0 },
|
|
214
|
+
} );
|
|
215
|
+
const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
|
|
216
|
+
|
|
217
|
+
const ratioBefore = state.cropRect.width / state.cropRect.height;
|
|
218
|
+
const ratioAfter = settled.cropRect.width / settled.cropRect.height;
|
|
219
|
+
expect( ratioAfter ).toBeCloseTo( ratioBefore, 2 );
|
|
220
|
+
|
|
221
|
+
expectSameVisibleRegion( state, settled );
|
|
222
|
+
} );
|
|
223
|
+
|
|
224
|
+
it( 'preserves image selection after zoom + off-center crop', () => {
|
|
225
|
+
const state = makeState( {
|
|
226
|
+
cropRect: { x: 0.05, y: 0.2, width: 0.6, height: 0.5 },
|
|
227
|
+
zoom: 2,
|
|
228
|
+
pan: { x: 0.1, y: -0.05 },
|
|
229
|
+
} );
|
|
230
|
+
const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
|
|
231
|
+
|
|
232
|
+
expect( settled.zoom ).toBeGreaterThan( 0 );
|
|
233
|
+
expectSameVisibleRegion( state, settled );
|
|
234
|
+
} );
|
|
235
|
+
|
|
236
|
+
it( 'preserves image selection with rotation', () => {
|
|
237
|
+
// After rotation, the user resizes the crop. Settle should still
|
|
238
|
+
// preserve what they see.
|
|
239
|
+
const state = makeState( {
|
|
240
|
+
cropRect: { x: 0.2, y: 0.2, width: 0.6, height: 0.6 },
|
|
241
|
+
zoom: 1.3,
|
|
242
|
+
rotation: 20,
|
|
243
|
+
pan: { x: 0, y: 0 },
|
|
244
|
+
} );
|
|
245
|
+
const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
|
|
246
|
+
|
|
247
|
+
expect( settled.zoom ).toBeGreaterThan( 0 );
|
|
248
|
+
expectSameVisibleRegion( state, settled );
|
|
249
|
+
} );
|
|
250
|
+
|
|
251
|
+
it( 'is a no-op when crop is already full-sized and centered', () => {
|
|
252
|
+
const state = makeState( {
|
|
253
|
+
cropRect: { x: 0, y: 0, width: 1, height: 1 },
|
|
254
|
+
zoom: 1,
|
|
255
|
+
pan: { x: 0, y: 0 },
|
|
256
|
+
} );
|
|
257
|
+
const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
|
|
258
|
+
|
|
259
|
+
// Crop should remain the same.
|
|
260
|
+
expect( settled.cropRect.x ).toBeCloseTo( 0, 5 );
|
|
261
|
+
expect( settled.cropRect.y ).toBeCloseTo( 0, 5 );
|
|
262
|
+
expect( settled.cropRect.width ).toBeCloseTo( 1, 5 );
|
|
263
|
+
expect( settled.cropRect.height ).toBeCloseTo( 1, 5 );
|
|
264
|
+
expect( settled.zoom ).toBeCloseTo( 1, 5 );
|
|
265
|
+
} );
|
|
266
|
+
|
|
267
|
+
it( 'preserves image selection across 50 random crop/zoom/pan combos', () => {
|
|
268
|
+
let passCount = 0;
|
|
269
|
+
for ( let i = 0; i < 50; i++ ) {
|
|
270
|
+
// Generate deterministic-ish crop rects.
|
|
271
|
+
const cropW = 0.15 + ( ( i * 7 ) % 8 ) / 10;
|
|
272
|
+
const cropH = 0.15 + ( ( i * 11 ) % 8 ) / 10;
|
|
273
|
+
const cropX = Math.min( ( ( i * 3 ) % 10 ) / 10, 1 - cropW );
|
|
274
|
+
const cropY = Math.min( ( ( i * 5 ) % 10 ) / 10, 1 - cropH );
|
|
275
|
+
const zoom = 1 + ( ( i * 13 ) % 30 ) / 10;
|
|
276
|
+
const panX = ( ( ( i * 17 ) % 20 ) - 10 ) / 100;
|
|
277
|
+
const panY = ( ( ( i * 19 ) % 20 ) - 10 ) / 100;
|
|
278
|
+
|
|
279
|
+
const state = enforceContainment(
|
|
280
|
+
makeState( {
|
|
281
|
+
cropRect: {
|
|
282
|
+
x: cropX,
|
|
283
|
+
y: cropY,
|
|
284
|
+
width: cropW,
|
|
285
|
+
height: cropH,
|
|
286
|
+
},
|
|
287
|
+
zoom,
|
|
288
|
+
pan: { x: panX, y: panY },
|
|
289
|
+
} )
|
|
290
|
+
);
|
|
291
|
+
|
|
292
|
+
const settled = cropperReducer( state, {
|
|
293
|
+
type: 'SETTLE_CROP',
|
|
294
|
+
} );
|
|
295
|
+
|
|
296
|
+
const regionBefore = getCropWorldRegion( state, IMAGE, CONTAINER );
|
|
297
|
+
const regionAfter = getCropWorldRegion( settled, IMAGE, CONTAINER );
|
|
298
|
+
|
|
299
|
+
const dx = Math.abs( regionAfter.minX - regionBefore.minX );
|
|
300
|
+
const dy = Math.abs( regionAfter.minY - regionBefore.minY );
|
|
301
|
+
const dw = Math.abs(
|
|
302
|
+
regionAfter.maxX -
|
|
303
|
+
regionAfter.minX -
|
|
304
|
+
( regionBefore.maxX - regionBefore.minX )
|
|
305
|
+
);
|
|
306
|
+
const dh = Math.abs(
|
|
307
|
+
regionAfter.maxY -
|
|
308
|
+
regionAfter.minY -
|
|
309
|
+
( regionBefore.maxY - regionBefore.minY )
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
// Allow 3% tolerance — enforceContainment may nudge
|
|
313
|
+
// things slightly when the crop is near image edges.
|
|
314
|
+
const tol = 0.03;
|
|
315
|
+
expect( dx ).toBeLessThanOrEqual( tol );
|
|
316
|
+
expect( dy ).toBeLessThanOrEqual( tol );
|
|
317
|
+
expect( dw ).toBeLessThanOrEqual( tol );
|
|
318
|
+
expect( dh ).toBeLessThanOrEqual( tol );
|
|
319
|
+
passCount++;
|
|
320
|
+
}
|
|
321
|
+
expect( passCount ).toBe( 50 );
|
|
322
|
+
} );
|
|
323
|
+
|
|
324
|
+
it( 'diagonal corner drag: small crop settles accurately', () => {
|
|
325
|
+
// Simulate dragging SE corner diagonally inward to make a small crop.
|
|
326
|
+
// Start with full crop, drag to 30% x 30%.
|
|
327
|
+
const afterDrag = enforceContainment(
|
|
328
|
+
makeState( {
|
|
329
|
+
cropRect: { x: 0.0, y: 0.0, width: 0.3, height: 0.3 },
|
|
330
|
+
zoom: 1,
|
|
331
|
+
pan: { x: 0, y: 0 },
|
|
332
|
+
} )
|
|
333
|
+
);
|
|
334
|
+
|
|
335
|
+
const settled = cropperReducer( afterDrag, { type: 'SETTLE_CROP' } );
|
|
336
|
+
|
|
337
|
+
// The settled crop should fill the visual area.
|
|
338
|
+
expect(
|
|
339
|
+
Math.max( settled.cropRect.width, settled.cropRect.height )
|
|
340
|
+
).toBeCloseTo( 1, 1 );
|
|
341
|
+
|
|
342
|
+
// The visible image region should be preserved.
|
|
343
|
+
const regionBefore = getCropWorldRegion( afterDrag, IMAGE, CONTAINER );
|
|
344
|
+
const regionAfter = getCropWorldRegion( settled, IMAGE, CONTAINER );
|
|
345
|
+
|
|
346
|
+
// Width/height of the visible region should match.
|
|
347
|
+
const wBefore = regionBefore.maxX - regionBefore.minX;
|
|
348
|
+
const hBefore = regionBefore.maxY - regionBefore.minY;
|
|
349
|
+
const wAfter = regionAfter.maxX - regionAfter.minX;
|
|
350
|
+
const hAfter = regionAfter.maxY - regionAfter.minY;
|
|
351
|
+
expect( wAfter ).toBeCloseTo( wBefore, 1 );
|
|
352
|
+
expect( hAfter ).toBeCloseTo( hBefore, 1 );
|
|
353
|
+
|
|
354
|
+
// Position should match.
|
|
355
|
+
expect( regionAfter.minX ).toBeCloseTo( regionBefore.minX, 1 );
|
|
356
|
+
expect( regionAfter.minY ).toBeCloseTo( regionBefore.minY, 1 );
|
|
357
|
+
} );
|
|
358
|
+
|
|
359
|
+
it( 'diagonal corner drag from NW: off-center small crop settles accurately', () => {
|
|
360
|
+
// Dragging NW corner inward — crop ends up at bottom-right.
|
|
361
|
+
const afterDrag = enforceContainment(
|
|
362
|
+
makeState( {
|
|
363
|
+
cropRect: { x: 0.5, y: 0.5, width: 0.4, height: 0.4 },
|
|
364
|
+
zoom: 1,
|
|
365
|
+
pan: { x: 0, y: 0 },
|
|
366
|
+
} )
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
const settled = cropperReducer( afterDrag, { type: 'SETTLE_CROP' } );
|
|
370
|
+
|
|
371
|
+
const regionBefore = getCropWorldRegion( afterDrag, IMAGE, CONTAINER );
|
|
372
|
+
const regionAfter = getCropWorldRegion( settled, IMAGE, CONTAINER );
|
|
373
|
+
|
|
374
|
+
const wBefore = regionBefore.maxX - regionBefore.minX;
|
|
375
|
+
const hBefore = regionBefore.maxY - regionBefore.minY;
|
|
376
|
+
const wAfter = regionAfter.maxX - regionAfter.minX;
|
|
377
|
+
const hAfter = regionAfter.maxY - regionAfter.minY;
|
|
378
|
+
expect( wAfter ).toBeCloseTo( wBefore, 1 );
|
|
379
|
+
expect( hAfter ).toBeCloseTo( hBefore, 1 );
|
|
380
|
+
expect( regionAfter.minX ).toBeCloseTo( regionBefore.minX, 1 );
|
|
381
|
+
expect( regionAfter.minY ).toBeCloseTo( regionBefore.minY, 1 );
|
|
382
|
+
} );
|
|
383
|
+
|
|
384
|
+
it( 'diagonal drag to very small crop does not over-zoom', () => {
|
|
385
|
+
// Extreme case: 10% crop. Zoom should be ~10x (or capped at MAX_ZOOM).
|
|
386
|
+
const afterDrag = enforceContainment(
|
|
387
|
+
makeState( {
|
|
388
|
+
cropRect: { x: 0.45, y: 0.45, width: 0.1, height: 0.1 },
|
|
389
|
+
zoom: 1,
|
|
390
|
+
pan: { x: 0, y: 0 },
|
|
391
|
+
} )
|
|
392
|
+
);
|
|
393
|
+
|
|
394
|
+
const settled = cropperReducer( afterDrag, { type: 'SETTLE_CROP' } );
|
|
395
|
+
|
|
396
|
+
// Zoom should be proportional to 1/0.1 = 10.
|
|
397
|
+
// But MAX_ZOOM may cap it.
|
|
398
|
+
expect( settled.zoom ).toBeLessThanOrEqual( 10 );
|
|
399
|
+
expect( settled.zoom ).toBeGreaterThan( 5 );
|
|
400
|
+
|
|
401
|
+
const regionBefore = getCropWorldRegion( afterDrag, IMAGE, CONTAINER );
|
|
402
|
+
const regionAfter = getCropWorldRegion( settled, IMAGE, CONTAINER );
|
|
403
|
+
const wBefore = regionBefore.maxX - regionBefore.minX;
|
|
404
|
+
const wAfter = regionAfter.maxX - regionAfter.minX;
|
|
405
|
+
expect( wAfter ).toBeCloseTo( wBefore, 1 );
|
|
406
|
+
} );
|
|
407
|
+
} );
|
|
408
|
+
|
|
409
|
+
describe( 'cropperReducer — SET_ZOOM_AT_POINT', () => {
|
|
410
|
+
it( 'focal-point zoom preserves the focal point position', () => {
|
|
411
|
+
const state = makeState( { zoom: 1, pan: { x: 0, y: 0 } } );
|
|
412
|
+
const { elementSize, visualSize } = getImageFit( CONTAINER, IMAGE, 0 );
|
|
413
|
+
|
|
414
|
+
// Simulate focal point at top-left quarter of the container.
|
|
415
|
+
const focalScreenX = CONTAINER.width * 0.25;
|
|
416
|
+
const focalScreenY = CONTAINER.height * 0.25;
|
|
417
|
+
|
|
418
|
+
// Compute what world point is under the focal point before zoom.
|
|
419
|
+
const cameraBefore = createCamera( state, CONTAINER, elementSize );
|
|
420
|
+
const worldBefore = screenToWorld( cameraBefore, {
|
|
421
|
+
x: focalScreenX,
|
|
422
|
+
y: focalScreenY,
|
|
423
|
+
} );
|
|
424
|
+
|
|
425
|
+
// Apply focal-point zoom using the same formula as the interaction controller.
|
|
426
|
+
const newZoom = 2;
|
|
427
|
+
const visSize = visualSize;
|
|
428
|
+
const fx = focalScreenX - CONTAINER.width / 2;
|
|
429
|
+
const fy = focalScreenY - CONTAINER.height / 2;
|
|
430
|
+
const zoomRatio = 1 - newZoom / state.zoom;
|
|
431
|
+
const focalNormX = fx / visSize.width;
|
|
432
|
+
const focalNormY = fy / visSize.height;
|
|
433
|
+
const newCropX = state.pan.x + ( focalNormX - state.pan.x ) * zoomRatio;
|
|
434
|
+
const newCropY = state.pan.y + ( focalNormY - state.pan.y ) * zoomRatio;
|
|
435
|
+
|
|
436
|
+
const zoomed = cropperReducer( state, {
|
|
437
|
+
type: 'SET_ZOOM_AT_POINT',
|
|
438
|
+
payload: { zoom: newZoom, pan: { x: newCropX, y: newCropY } },
|
|
439
|
+
} );
|
|
440
|
+
|
|
441
|
+
// Compute what world point is under the focal point after zoom.
|
|
442
|
+
const cameraAfter = createCamera( zoomed, CONTAINER, elementSize );
|
|
443
|
+
const worldAfter = screenToWorld( cameraAfter, {
|
|
444
|
+
x: focalScreenX,
|
|
445
|
+
y: focalScreenY,
|
|
446
|
+
} );
|
|
447
|
+
|
|
448
|
+
// The same world point should be under the cursor.
|
|
449
|
+
expect( worldAfter.x ).toBeCloseTo( worldBefore.x, 1 );
|
|
450
|
+
expect( worldAfter.y ).toBeCloseTo( worldBefore.y, 1 );
|
|
451
|
+
} );
|
|
452
|
+
} );
|
|
453
|
+
|
|
454
|
+
describe( 'cropperReducer — SNAP_ROTATE_90', () => {
|
|
455
|
+
it( 'swaps crop width and height around the same center', () => {
|
|
456
|
+
const state = makeState( {
|
|
457
|
+
cropRect: { x: 0.1, y: 0.2, width: 0.6, height: 0.4 },
|
|
458
|
+
} );
|
|
459
|
+
const centerBefore = {
|
|
460
|
+
x: state.cropRect.x + state.cropRect.width / 2,
|
|
461
|
+
y: state.cropRect.y + state.cropRect.height / 2,
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
const rotated = cropperReducer( state, {
|
|
465
|
+
type: 'SNAP_ROTATE_90',
|
|
466
|
+
payload: { direction: 1 },
|
|
467
|
+
} );
|
|
468
|
+
|
|
469
|
+
const centerAfter = {
|
|
470
|
+
x: rotated.cropRect.x + rotated.cropRect.width / 2,
|
|
471
|
+
y: rotated.cropRect.y + rotated.cropRect.height / 2,
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
// Center should be preserved.
|
|
475
|
+
expect( centerAfter.x ).toBeCloseTo( centerBefore.x, 2 );
|
|
476
|
+
expect( centerAfter.y ).toBeCloseTo( centerBefore.y, 2 );
|
|
477
|
+
|
|
478
|
+
// Rotation should advance by 90.
|
|
479
|
+
expect( rotated.rotation ).toBe( 90 );
|
|
480
|
+
} );
|
|
481
|
+
|
|
482
|
+
it( 'rotates pan 90° CW so framed content stays framed', () => {
|
|
483
|
+
const state = makeState( {
|
|
484
|
+
zoom: 2,
|
|
485
|
+
pan: { x: 0.1, y: 0.2 },
|
|
486
|
+
} );
|
|
487
|
+
|
|
488
|
+
const rotated = cropperReducer( state, {
|
|
489
|
+
type: 'SNAP_ROTATE_90',
|
|
490
|
+
payload: { direction: 1 },
|
|
491
|
+
} );
|
|
492
|
+
|
|
493
|
+
// CW rotation: (px, py) → (-py, px)
|
|
494
|
+
expect( rotated.pan.x ).toBeCloseTo( -0.2, 5 );
|
|
495
|
+
expect( rotated.pan.y ).toBeCloseTo( 0.1, 5 );
|
|
496
|
+
} );
|
|
497
|
+
|
|
498
|
+
it( 'rotates pan 90° CCW in the other direction', () => {
|
|
499
|
+
const state = makeState( {
|
|
500
|
+
zoom: 2,
|
|
501
|
+
pan: { x: 0.1, y: 0.2 },
|
|
502
|
+
} );
|
|
503
|
+
|
|
504
|
+
const rotated = cropperReducer( state, {
|
|
505
|
+
type: 'SNAP_ROTATE_90',
|
|
506
|
+
payload: { direction: -1 },
|
|
507
|
+
} );
|
|
508
|
+
|
|
509
|
+
// CCW rotation: (px, py) → (py, -px)
|
|
510
|
+
expect( rotated.pan.x ).toBeCloseTo( 0.2, 5 );
|
|
511
|
+
expect( rotated.pan.y ).toBeCloseTo( -0.1, 5 );
|
|
512
|
+
} );
|
|
513
|
+
|
|
514
|
+
it( 'four CW 90° rotations return pan to the original position', () => {
|
|
515
|
+
const state = makeState( {
|
|
516
|
+
zoom: 2,
|
|
517
|
+
pan: { x: 0.15, y: -0.08 },
|
|
518
|
+
} );
|
|
519
|
+
let result = state;
|
|
520
|
+
for ( let i = 0; i < 4; i++ ) {
|
|
521
|
+
result = cropperReducer( result, {
|
|
522
|
+
type: 'SNAP_ROTATE_90',
|
|
523
|
+
payload: { direction: 1 },
|
|
524
|
+
} );
|
|
525
|
+
}
|
|
526
|
+
expect( result.pan.x ).toBeCloseTo( state.pan.x, 5 );
|
|
527
|
+
expect( result.pan.y ).toBeCloseTo( state.pan.y, 5 );
|
|
528
|
+
} );
|
|
529
|
+
|
|
530
|
+
it( 'single-axis flip: direction=+1 means visual CW, so rotation field decreases', () => {
|
|
531
|
+
// `direction` is the visual rotation the caller wants. With a
|
|
532
|
+
// single-axis flip, on-screen rotation is reversed relative to
|
|
533
|
+
// the rotation field, so internally the field decreases by 90°
|
|
534
|
+
// to achieve a visual CW rotation. Pan still rotates by +90° in
|
|
535
|
+
// the visual direction to keep framing.
|
|
536
|
+
const state = makeState( {
|
|
537
|
+
zoom: 2,
|
|
538
|
+
pan: { x: 0.1, y: 0.2 },
|
|
539
|
+
rotation: 0,
|
|
540
|
+
flip: { horizontal: true, vertical: false },
|
|
541
|
+
} );
|
|
542
|
+
|
|
543
|
+
const rotated = cropperReducer( state, {
|
|
544
|
+
type: 'SNAP_ROTATE_90',
|
|
545
|
+
payload: { direction: 1 },
|
|
546
|
+
} );
|
|
547
|
+
|
|
548
|
+
// Field moved 0 → 270 (i.e. -90 normalized).
|
|
549
|
+
expect( rotated.rotation ).toBe( 270 );
|
|
550
|
+
// Pan rotates by +90° CW: (px, py) → (-py, px).
|
|
551
|
+
expect( rotated.pan.x ).toBeCloseTo( -0.2, 5 );
|
|
552
|
+
expect( rotated.pan.y ).toBeCloseTo( 0.1, 5 );
|
|
553
|
+
} );
|
|
554
|
+
|
|
555
|
+
it( 'both-axis flip: rotation field advances normally (two flips cancel)', () => {
|
|
556
|
+
const state = makeState( {
|
|
557
|
+
zoom: 2,
|
|
558
|
+
pan: { x: 0.1, y: 0.2 },
|
|
559
|
+
flip: { horizontal: true, vertical: true },
|
|
560
|
+
} );
|
|
561
|
+
|
|
562
|
+
const rotated = cropperReducer( state, {
|
|
563
|
+
type: 'SNAP_ROTATE_90',
|
|
564
|
+
payload: { direction: 1 },
|
|
565
|
+
} );
|
|
566
|
+
|
|
567
|
+
expect( rotated.rotation ).toBe( 90 );
|
|
568
|
+
expect( rotated.pan.x ).toBeCloseTo( -0.2, 5 );
|
|
569
|
+
expect( rotated.pan.y ).toBeCloseTo( 0.1, 5 );
|
|
570
|
+
} );
|
|
571
|
+
} );
|
|
572
|
+
|
|
573
|
+
describe( 'cropperReducer — SET_FLIP', () => {
|
|
574
|
+
it( 'mirrors cropRect horizontally on horizontal flip', () => {
|
|
575
|
+
const state = makeState( {
|
|
576
|
+
cropRect: { x: 0.1, y: 0.2, width: 0.3, height: 0.4 },
|
|
577
|
+
} );
|
|
578
|
+
|
|
579
|
+
const flipped = cropperReducer( state, {
|
|
580
|
+
type: 'SET_FLIP',
|
|
581
|
+
payload: { horizontal: true, vertical: false },
|
|
582
|
+
} );
|
|
583
|
+
|
|
584
|
+
// x mirrors: newX = 1 - oldX - width
|
|
585
|
+
expect( flipped.cropRect.x ).toBeCloseTo( 0.6, 5 );
|
|
586
|
+
expect( flipped.cropRect.y ).toBe( 0.2 );
|
|
587
|
+
expect( flipped.cropRect.width ).toBe( 0.3 );
|
|
588
|
+
expect( flipped.cropRect.height ).toBe( 0.4 );
|
|
589
|
+
} );
|
|
590
|
+
|
|
591
|
+
it( 'mirrors cropRect vertically on vertical flip', () => {
|
|
592
|
+
const state = makeState( {
|
|
593
|
+
cropRect: { x: 0.1, y: 0.2, width: 0.3, height: 0.4 },
|
|
594
|
+
} );
|
|
595
|
+
|
|
596
|
+
const flipped = cropperReducer( state, {
|
|
597
|
+
type: 'SET_FLIP',
|
|
598
|
+
payload: { horizontal: false, vertical: true },
|
|
599
|
+
} );
|
|
600
|
+
|
|
601
|
+
expect( flipped.cropRect.x ).toBe( 0.1 );
|
|
602
|
+
expect( flipped.cropRect.y ).toBeCloseTo( 0.4, 5 );
|
|
603
|
+
expect( flipped.cropRect.width ).toBe( 0.3 );
|
|
604
|
+
expect( flipped.cropRect.height ).toBe( 0.4 );
|
|
605
|
+
} );
|
|
606
|
+
|
|
607
|
+
it( 'mirrors pan horizontally on horizontal flip', () => {
|
|
608
|
+
const state = makeState( {
|
|
609
|
+
zoom: 2,
|
|
610
|
+
pan: { x: 0.15, y: 0.07 },
|
|
611
|
+
} );
|
|
612
|
+
|
|
613
|
+
const flipped = cropperReducer( state, {
|
|
614
|
+
type: 'SET_FLIP',
|
|
615
|
+
payload: { horizontal: true, vertical: false },
|
|
616
|
+
} );
|
|
617
|
+
|
|
618
|
+
expect( flipped.pan.x ).toBeCloseTo( -0.15, 5 );
|
|
619
|
+
expect( flipped.pan.y ).toBeCloseTo( 0.07, 5 );
|
|
620
|
+
} );
|
|
621
|
+
|
|
622
|
+
it( 'is its own inverse — two flips return to the original', () => {
|
|
623
|
+
const state = makeState( {
|
|
624
|
+
zoom: 2,
|
|
625
|
+
pan: { x: 0.1, y: -0.05 },
|
|
626
|
+
cropRect: { x: 0.2, y: 0.3, width: 0.5, height: 0.4 },
|
|
627
|
+
} );
|
|
628
|
+
|
|
629
|
+
const once = cropperReducer( state, {
|
|
630
|
+
type: 'SET_FLIP',
|
|
631
|
+
payload: { horizontal: true, vertical: true },
|
|
632
|
+
} );
|
|
633
|
+
const twice = cropperReducer( once, {
|
|
634
|
+
type: 'SET_FLIP',
|
|
635
|
+
payload: { horizontal: false, vertical: false },
|
|
636
|
+
} );
|
|
637
|
+
|
|
638
|
+
expect( twice.pan.x ).toBeCloseTo( state.pan.x, 5 );
|
|
639
|
+
expect( twice.pan.y ).toBeCloseTo( state.pan.y, 5 );
|
|
640
|
+
expect( twice.cropRect.x ).toBeCloseTo( state.cropRect.x, 5 );
|
|
641
|
+
expect( twice.cropRect.y ).toBeCloseTo( state.cropRect.y, 5 );
|
|
642
|
+
} );
|
|
643
|
+
|
|
644
|
+
it( 'does nothing to cropRect or pan when flip state is unchanged', () => {
|
|
645
|
+
const state = makeState( {
|
|
646
|
+
zoom: 2,
|
|
647
|
+
pan: { x: 0.1, y: 0.2 },
|
|
648
|
+
cropRect: { x: 0.1, y: 0.2, width: 0.3, height: 0.4 },
|
|
649
|
+
flip: { horizontal: true, vertical: false },
|
|
650
|
+
} );
|
|
651
|
+
|
|
652
|
+
const result = cropperReducer( state, {
|
|
653
|
+
type: 'SET_FLIP',
|
|
654
|
+
payload: { horizontal: true, vertical: false },
|
|
655
|
+
} );
|
|
656
|
+
|
|
657
|
+
expect( result.pan.x ).toBeCloseTo( 0.1, 5 );
|
|
658
|
+
expect( result.pan.y ).toBeCloseTo( 0.2, 5 );
|
|
659
|
+
expect( result.cropRect.x ).toBe( 0.1 );
|
|
660
|
+
} );
|
|
661
|
+
|
|
662
|
+
it( 'flip is viewport-relative — negates pan axis independent of rotation', () => {
|
|
663
|
+
// With viewport-relative flip, a horizontal flip always negates pan.x
|
|
664
|
+
// regardless of current rotation (same viewport axis gets mirrored).
|
|
665
|
+
const state = makeState( {
|
|
666
|
+
zoom: 2,
|
|
667
|
+
pan: { x: 0.15, y: 0.07 },
|
|
668
|
+
rotation: 90,
|
|
669
|
+
} );
|
|
670
|
+
|
|
671
|
+
const flipped = cropperReducer( state, {
|
|
672
|
+
type: 'SET_FLIP',
|
|
673
|
+
payload: { horizontal: true, vertical: false },
|
|
674
|
+
} );
|
|
675
|
+
|
|
676
|
+
expect( flipped.pan.x ).toBeCloseTo( -0.15, 5 );
|
|
677
|
+
expect( flipped.pan.y ).toBeCloseTo( 0.07, 5 );
|
|
678
|
+
} );
|
|
679
|
+
} );
|
|
680
|
+
|
|
681
|
+
describe( 'cropperReducer — SET_ROTATION', () => {
|
|
682
|
+
it( 'rotates pan by the angle delta', () => {
|
|
683
|
+
const state = makeState( {
|
|
684
|
+
zoom: 2,
|
|
685
|
+
pan: { x: 0.1, y: 0 },
|
|
686
|
+
rotation: 0,
|
|
687
|
+
} );
|
|
688
|
+
|
|
689
|
+
// 90° rotation via SET_ROTATION: same math as SNAP_ROTATE_90
|
|
690
|
+
// pan: (0.1, 0) → (0, 0.1) after 90° CCW in standard math
|
|
691
|
+
// (positive rotation in our sin/cos convention)
|
|
692
|
+
const rotated = cropperReducer( state, {
|
|
693
|
+
type: 'SET_ROTATION',
|
|
694
|
+
payload: 90,
|
|
695
|
+
} );
|
|
696
|
+
|
|
697
|
+
// delta = 90°; cos(90°)=0, sin(90°)=1
|
|
698
|
+
// newX = px*0 - py*1 = 0
|
|
699
|
+
// newY = px*1 + py*0 = 0.1
|
|
700
|
+
expect( rotated.pan.x ).toBeCloseTo( 0, 5 );
|
|
701
|
+
expect( rotated.pan.y ).toBeCloseTo( 0.1, 5 );
|
|
702
|
+
} );
|
|
703
|
+
|
|
704
|
+
it( 'preserves zoom (no reset)', () => {
|
|
705
|
+
const state = makeState( { zoom: 3, rotation: 0 } );
|
|
706
|
+
const rotated = cropperReducer( state, {
|
|
707
|
+
type: 'SET_ROTATION',
|
|
708
|
+
payload: 30,
|
|
709
|
+
} );
|
|
710
|
+
expect( rotated.zoom ).toBeGreaterThanOrEqual( 3 );
|
|
711
|
+
} );
|
|
712
|
+
|
|
713
|
+
it( 'small rotation ticks accumulate without drift', () => {
|
|
714
|
+
// Simulate a slider going 0° → 45° in 9 ticks of 5°.
|
|
715
|
+
// SET_ROTATION derives each tick from the fixed basePan (not
|
|
716
|
+
// from the previous tick's potentially-clamped pan), so the
|
|
717
|
+
// stepwise result matches a single 45° rotation exactly.
|
|
718
|
+
const state = makeState( {
|
|
719
|
+
zoom: 2,
|
|
720
|
+
pan: { x: 0.1, y: 0 },
|
|
721
|
+
} );
|
|
722
|
+
|
|
723
|
+
let stepwise = state;
|
|
724
|
+
for ( let i = 1; i <= 9; i++ ) {
|
|
725
|
+
stepwise = cropperReducer( stepwise, {
|
|
726
|
+
type: 'SET_ROTATION',
|
|
727
|
+
payload: i * 5,
|
|
728
|
+
} );
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
const direct = cropperReducer( state, {
|
|
732
|
+
type: 'SET_ROTATION',
|
|
733
|
+
payload: 45,
|
|
734
|
+
} );
|
|
735
|
+
|
|
736
|
+
expect( stepwise.pan.x ).toBeCloseTo( direct.pan.x, 5 );
|
|
737
|
+
expect( stepwise.pan.y ).toBeCloseTo( direct.pan.y, 5 );
|
|
738
|
+
} );
|
|
739
|
+
|
|
740
|
+
it( 'rotating away and back to 0° returns to the exact base pan', () => {
|
|
741
|
+
// Regression test for accumulated-clamp drift: rotating 0° →
|
|
742
|
+
// through various angles → 0° should land at the original
|
|
743
|
+
// pan, even when panned near an edge where containment would
|
|
744
|
+
// have clamped at intermediate angles.
|
|
745
|
+
const state = makeState( {
|
|
746
|
+
zoom: 2,
|
|
747
|
+
pan: { x: 0.1, y: -0.05 },
|
|
748
|
+
} );
|
|
749
|
+
|
|
750
|
+
let result = state;
|
|
751
|
+
for ( const angle of [ 10, 25, 40, 45, 30, 15, 0 ] ) {
|
|
752
|
+
result = cropperReducer( result, {
|
|
753
|
+
type: 'SET_ROTATION',
|
|
754
|
+
payload: angle,
|
|
755
|
+
} );
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
expect( result.pan.x ).toBeCloseTo( state.pan.x, 5 );
|
|
759
|
+
expect( result.pan.y ).toBeCloseTo( state.pan.y, 5 );
|
|
760
|
+
expect( result.zoom ).toBeCloseTo( state.zoom, 5 );
|
|
761
|
+
} );
|
|
762
|
+
} );
|