@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,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
computeLockedResizeRect,
|
|
6
|
+
computeShiftLockedResizeRect,
|
|
7
|
+
type CropBounds,
|
|
8
|
+
type ResizeDragState,
|
|
9
|
+
} from '../stencil-math';
|
|
10
|
+
import type { Size } from '../types';
|
|
11
|
+
|
|
12
|
+
const FULL_BOUNDS: CropBounds = { minX: 0, minY: 0, maxX: 1, maxY: 1 };
|
|
13
|
+
|
|
14
|
+
describe( 'computeLockedResizeRect — driver-axis selection', () => {
|
|
15
|
+
// On non-square images, the locked-ratio resize must pick the
|
|
16
|
+
// driving axis based on which the user moved more in *pixel* space,
|
|
17
|
+
// compared against the *pixel* aspect ratio. Comparing pixel motion
|
|
18
|
+
// against the normalized w/h ratio is a unit mismatch and produces
|
|
19
|
+
// the wrong axis on non-square images.
|
|
20
|
+
//
|
|
21
|
+
// Setup: a wide 1600×900 image, locked to a square (1:1) crop.
|
|
22
|
+
// Drag from the NW corner of a (degenerate) start rect, anchored at
|
|
23
|
+
// (0, 0), so distW/distH after the drag equals the drag offset.
|
|
24
|
+
const imageSize: Size = { width: 1600, height: 900 };
|
|
25
|
+
const aspectRatio = 1;
|
|
26
|
+
// The stencil normalizes the pixel ratio for the math:
|
|
27
|
+
// normalizedRatio = aspectRatio * imageH / imageW.
|
|
28
|
+
const normalizedRatio =
|
|
29
|
+
( aspectRatio * imageSize.height ) / imageSize.width;
|
|
30
|
+
const startRect = { x: 0, y: 0, width: 0, height: 0 };
|
|
31
|
+
|
|
32
|
+
it( 'lets height drive when the user moves more pixels vertically than horizontally (between normalized and pixel thresholds)', () => {
|
|
33
|
+
// Drag SE corner to (144px, 160px) from anchor (0, 0).
|
|
34
|
+
// pixelDistW=144, pixelDistH=160, ratio=0.9 — comfortably above
|
|
35
|
+
// MIN_CROP_SIZE (0.05 = 80px on a 1600px image), so neither axis
|
|
36
|
+
// is at the min-clamp boundary. The ratio (0.9) sits between the
|
|
37
|
+
// old buggy threshold (normalizedRatio=0.5625) and the correct
|
|
38
|
+
// pixel-space threshold (aspectRatio=1) — that's the gap where
|
|
39
|
+
// the unit mismatch produced the wrong driver axis.
|
|
40
|
+
// Correct: 0.9 < aspectRatio(1) → height drives → 160×160 pixels.
|
|
41
|
+
const drag: ResizeDragState = {
|
|
42
|
+
handle: 'se',
|
|
43
|
+
startX: 0,
|
|
44
|
+
startY: 0,
|
|
45
|
+
startRect,
|
|
46
|
+
};
|
|
47
|
+
const rect = computeLockedResizeRect(
|
|
48
|
+
drag,
|
|
49
|
+
144,
|
|
50
|
+
160,
|
|
51
|
+
imageSize,
|
|
52
|
+
FULL_BOUNDS,
|
|
53
|
+
normalizedRatio
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const pixelW = rect.width * imageSize.width;
|
|
57
|
+
const pixelH = rect.height * imageSize.height;
|
|
58
|
+
|
|
59
|
+
expect( pixelW / pixelH ).toBeCloseTo( 1, 5 );
|
|
60
|
+
expect( pixelH ).toBeCloseTo( 160, 5 );
|
|
61
|
+
expect( pixelW ).toBeCloseTo( 160, 5 );
|
|
62
|
+
} );
|
|
63
|
+
|
|
64
|
+
it( 'returns the start rect unchanged when normalizedRatio is 0 (image not loaded)', () => {
|
|
65
|
+
// Reachable in practice: keyboard arrows on a focused resize
|
|
66
|
+
// handle before the image's natural size is known make the
|
|
67
|
+
// stencil pass `normalizedRatio === 0`.
|
|
68
|
+
const preLoadStart = { x: 0.1, y: 0.2, width: 0.3, height: 0.4 };
|
|
69
|
+
const drag: ResizeDragState = {
|
|
70
|
+
handle: 'se',
|
|
71
|
+
startX: 0,
|
|
72
|
+
startY: 0,
|
|
73
|
+
startRect: preLoadStart,
|
|
74
|
+
};
|
|
75
|
+
const rect = computeLockedResizeRect(
|
|
76
|
+
drag,
|
|
77
|
+
100,
|
|
78
|
+
100,
|
|
79
|
+
{ width: 0, height: 0 },
|
|
80
|
+
FULL_BOUNDS,
|
|
81
|
+
0
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
expect( rect ).toEqual( preLoadStart );
|
|
85
|
+
} );
|
|
86
|
+
|
|
87
|
+
it( 'lets width drive when the user moves more pixels horizontally than vertically', () => {
|
|
88
|
+
// Drag SE corner to (200px, 50px) from anchor (0, 0).
|
|
89
|
+
// pixelDistW=200, pixelDistH=50, ratio=4 > aspectRatio(1).
|
|
90
|
+
// Width drives. Result: 200×200 pixels.
|
|
91
|
+
const drag: ResizeDragState = {
|
|
92
|
+
handle: 'se',
|
|
93
|
+
startX: 0,
|
|
94
|
+
startY: 0,
|
|
95
|
+
startRect,
|
|
96
|
+
};
|
|
97
|
+
const rect = computeLockedResizeRect(
|
|
98
|
+
drag,
|
|
99
|
+
200,
|
|
100
|
+
50,
|
|
101
|
+
imageSize,
|
|
102
|
+
FULL_BOUNDS,
|
|
103
|
+
normalizedRatio
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const pixelW = rect.width * imageSize.width;
|
|
107
|
+
const pixelH = rect.height * imageSize.height;
|
|
108
|
+
|
|
109
|
+
expect( pixelW / pixelH ).toBeCloseTo( 1, 5 );
|
|
110
|
+
expect( pixelW ).toBeCloseTo( 200, 5 );
|
|
111
|
+
expect( pixelH ).toBeCloseTo( 200, 5 );
|
|
112
|
+
} );
|
|
113
|
+
} );
|
|
114
|
+
|
|
115
|
+
const IMAGE: Size = { width: 1000, height: 500 };
|
|
116
|
+
|
|
117
|
+
const START_RECT = { x: 0.2, y: 0.2, width: 0.4, height: 0.6 };
|
|
118
|
+
|
|
119
|
+
function makeDrag(
|
|
120
|
+
handle: ResizeDragState[ 'handle' ],
|
|
121
|
+
startX = 500,
|
|
122
|
+
startY = 250
|
|
123
|
+
): ResizeDragState {
|
|
124
|
+
return {
|
|
125
|
+
handle,
|
|
126
|
+
startX,
|
|
127
|
+
startY,
|
|
128
|
+
startRect: { ...START_RECT },
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
describe( 'computeShiftLockedResizeRect', () => {
|
|
133
|
+
describe( 'corner handles', () => {
|
|
134
|
+
it( 'preserves the start rect ratio when dragging the SE corner outward', () => {
|
|
135
|
+
const drag = makeDrag( 'se' );
|
|
136
|
+
// Drag right by 200px and down by 50px — width is the bigger
|
|
137
|
+
// pixel motion, so it drives.
|
|
138
|
+
const rect = computeShiftLockedResizeRect(
|
|
139
|
+
drag,
|
|
140
|
+
700,
|
|
141
|
+
300,
|
|
142
|
+
IMAGE,
|
|
143
|
+
FULL_BOUNDS
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
const startPixelRatio =
|
|
147
|
+
( START_RECT.width * IMAGE.width ) /
|
|
148
|
+
( START_RECT.height * IMAGE.height );
|
|
149
|
+
const newPixelRatio =
|
|
150
|
+
( rect.width * IMAGE.width ) / ( rect.height * IMAGE.height );
|
|
151
|
+
|
|
152
|
+
expect( newPixelRatio ).toBeCloseTo( startPixelRatio, 5 );
|
|
153
|
+
// SE drag anchors at the NW corner of the start rect.
|
|
154
|
+
expect( rect.x ).toBeCloseTo( START_RECT.x, 5 );
|
|
155
|
+
expect( rect.y ).toBeCloseTo( START_RECT.y, 5 );
|
|
156
|
+
} );
|
|
157
|
+
|
|
158
|
+
it( 'preserves the start rect ratio when dragging the NW corner inward', () => {
|
|
159
|
+
const drag = makeDrag( 'nw' );
|
|
160
|
+
const rect = computeShiftLockedResizeRect(
|
|
161
|
+
drag,
|
|
162
|
+
600,
|
|
163
|
+
300,
|
|
164
|
+
IMAGE,
|
|
165
|
+
FULL_BOUNDS
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const startPixelRatio =
|
|
169
|
+
( START_RECT.width * IMAGE.width ) /
|
|
170
|
+
( START_RECT.height * IMAGE.height );
|
|
171
|
+
const newPixelRatio =
|
|
172
|
+
( rect.width * IMAGE.width ) / ( rect.height * IMAGE.height );
|
|
173
|
+
expect( newPixelRatio ).toBeCloseTo( startPixelRatio, 5 );
|
|
174
|
+
// NW drag anchors at the SE corner of the start rect.
|
|
175
|
+
expect( rect.x + rect.width ).toBeCloseTo(
|
|
176
|
+
START_RECT.x + START_RECT.width,
|
|
177
|
+
5
|
|
178
|
+
);
|
|
179
|
+
expect( rect.y + rect.height ).toBeCloseTo(
|
|
180
|
+
START_RECT.y + START_RECT.height,
|
|
181
|
+
5
|
|
182
|
+
);
|
|
183
|
+
} );
|
|
184
|
+
} );
|
|
185
|
+
|
|
186
|
+
describe( 'edge handles — symmetric expansion', () => {
|
|
187
|
+
it( 'east edge: expands height symmetrically around the start rect center', () => {
|
|
188
|
+
const drag = makeDrag( 'e' );
|
|
189
|
+
// Expand width by 100px to the right.
|
|
190
|
+
const rect = computeShiftLockedResizeRect(
|
|
191
|
+
drag,
|
|
192
|
+
600,
|
|
193
|
+
250,
|
|
194
|
+
IMAGE,
|
|
195
|
+
FULL_BOUNDS
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
const startCenterY = START_RECT.y + START_RECT.height / 2;
|
|
199
|
+
expect( rect.y + rect.height / 2 ).toBeCloseTo( startCenterY, 5 );
|
|
200
|
+
// x-anchored to start.x.
|
|
201
|
+
expect( rect.x ).toBeCloseTo( START_RECT.x, 5 );
|
|
202
|
+
// Ratio preserved.
|
|
203
|
+
const startPixelRatio =
|
|
204
|
+
( START_RECT.width * IMAGE.width ) /
|
|
205
|
+
( START_RECT.height * IMAGE.height );
|
|
206
|
+
const newPixelRatio =
|
|
207
|
+
( rect.width * IMAGE.width ) / ( rect.height * IMAGE.height );
|
|
208
|
+
expect( newPixelRatio ).toBeCloseTo( startPixelRatio, 5 );
|
|
209
|
+
} );
|
|
210
|
+
|
|
211
|
+
it( 'north edge: expands width symmetrically around the start rect center', () => {
|
|
212
|
+
const drag = makeDrag( 'n' );
|
|
213
|
+
// Drag the top edge up by 50px (taller crop).
|
|
214
|
+
const rect = computeShiftLockedResizeRect(
|
|
215
|
+
drag,
|
|
216
|
+
500,
|
|
217
|
+
200,
|
|
218
|
+
IMAGE,
|
|
219
|
+
FULL_BOUNDS
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
const startCenterX = START_RECT.x + START_RECT.width / 2;
|
|
223
|
+
expect( rect.x + rect.width / 2 ).toBeCloseTo( startCenterX, 5 );
|
|
224
|
+
// Bottom edge stays anchored.
|
|
225
|
+
expect( rect.y + rect.height ).toBeCloseTo(
|
|
226
|
+
START_RECT.y + START_RECT.height,
|
|
227
|
+
5
|
|
228
|
+
);
|
|
229
|
+
const startPixelRatio =
|
|
230
|
+
( START_RECT.width * IMAGE.width ) /
|
|
231
|
+
( START_RECT.height * IMAGE.height );
|
|
232
|
+
const newPixelRatio =
|
|
233
|
+
( rect.width * IMAGE.width ) / ( rect.height * IMAGE.height );
|
|
234
|
+
expect( newPixelRatio ).toBeCloseTo( startPixelRatio, 5 );
|
|
235
|
+
} );
|
|
236
|
+
} );
|
|
237
|
+
|
|
238
|
+
describe( 'minimum size clamping preserves ratio', () => {
|
|
239
|
+
it( 'east edge: clamps a tall crop without breaking ratio when width hits MIN_CROP_SIZE', () => {
|
|
240
|
+
// Tall start rect — small ratio (0.0625 in normalized space).
|
|
241
|
+
const tallStart = { x: 0.2, y: 0.1, width: 0.05, height: 0.8 };
|
|
242
|
+
const drag: ResizeDragState = {
|
|
243
|
+
handle: 'e',
|
|
244
|
+
startX: 250,
|
|
245
|
+
startY: 250,
|
|
246
|
+
startRect: tallStart,
|
|
247
|
+
};
|
|
248
|
+
// Drag the east edge inward — width tries to shrink below MIN.
|
|
249
|
+
const rect = computeShiftLockedResizeRect(
|
|
250
|
+
drag,
|
|
251
|
+
200,
|
|
252
|
+
250,
|
|
253
|
+
IMAGE,
|
|
254
|
+
FULL_BOUNDS
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
const startRatio = tallStart.width / tallStart.height;
|
|
258
|
+
const newRatio = rect.width / rect.height;
|
|
259
|
+
expect( newRatio ).toBeCloseTo( startRatio, 5 );
|
|
260
|
+
expect( rect.width ).toBeGreaterThanOrEqual( 0.05 - 1e-9 );
|
|
261
|
+
expect( rect.height ).toBeGreaterThanOrEqual( 0.05 - 1e-9 );
|
|
262
|
+
} );
|
|
263
|
+
|
|
264
|
+
it( 'north edge: clamps a wide crop without breaking ratio when height hits MIN_CROP_SIZE', () => {
|
|
265
|
+
// Wide start rect — large ratio (16 in normalized space).
|
|
266
|
+
const wideStart = { x: 0.1, y: 0.4, width: 0.8, height: 0.05 };
|
|
267
|
+
const drag: ResizeDragState = {
|
|
268
|
+
handle: 'n',
|
|
269
|
+
startX: 500,
|
|
270
|
+
startY: 200,
|
|
271
|
+
startRect: wideStart,
|
|
272
|
+
};
|
|
273
|
+
// Drag the north edge down — height tries to shrink below MIN.
|
|
274
|
+
const rect = computeShiftLockedResizeRect(
|
|
275
|
+
drag,
|
|
276
|
+
500,
|
|
277
|
+
220,
|
|
278
|
+
IMAGE,
|
|
279
|
+
FULL_BOUNDS
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
const startRatio = wideStart.width / wideStart.height;
|
|
283
|
+
const newRatio = rect.width / rect.height;
|
|
284
|
+
expect( newRatio ).toBeCloseTo( startRatio, 5 );
|
|
285
|
+
expect( rect.width ).toBeGreaterThanOrEqual( 0.05 - 1e-9 );
|
|
286
|
+
expect( rect.height ).toBeGreaterThanOrEqual( 0.05 - 1e-9 );
|
|
287
|
+
} );
|
|
288
|
+
} );
|
|
289
|
+
|
|
290
|
+
describe( 'bounds clamping', () => {
|
|
291
|
+
it( 'east edge: clamps height to the symmetric bounds limit and shrinks width to keep ratio', () => {
|
|
292
|
+
// Center-y of START_RECT is 0.5; tightest bounds are bounds.minY=0.4
|
|
293
|
+
// and bounds.maxY=0.6 → max half-height = 0.1, so max height = 0.2.
|
|
294
|
+
const tightBounds: CropBounds = {
|
|
295
|
+
minX: 0,
|
|
296
|
+
minY: 0.4,
|
|
297
|
+
maxX: 1,
|
|
298
|
+
maxY: 0.6,
|
|
299
|
+
};
|
|
300
|
+
const drag = makeDrag( 'e' );
|
|
301
|
+
// Drag far right — height would otherwise exceed the bound.
|
|
302
|
+
const rect = computeShiftLockedResizeRect(
|
|
303
|
+
drag,
|
|
304
|
+
1000,
|
|
305
|
+
250,
|
|
306
|
+
IMAGE,
|
|
307
|
+
tightBounds
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
expect( rect.height ).toBeLessThanOrEqual( 0.2 + 1e-9 );
|
|
311
|
+
// Centered around 0.5.
|
|
312
|
+
expect( rect.y + rect.height / 2 ).toBeCloseTo( 0.5, 5 );
|
|
313
|
+
const startPixelRatio =
|
|
314
|
+
( START_RECT.width * IMAGE.width ) /
|
|
315
|
+
( START_RECT.height * IMAGE.height );
|
|
316
|
+
const newPixelRatio =
|
|
317
|
+
( rect.width * IMAGE.width ) / ( rect.height * IMAGE.height );
|
|
318
|
+
expect( newPixelRatio ).toBeCloseTo( startPixelRatio, 5 );
|
|
319
|
+
} );
|
|
320
|
+
} );
|
|
321
|
+
} );
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { CropperState, Size } from './types';
|
|
5
|
+
import { degreesToRadians } from './math/rotation';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Computes a CSS matrix() transform string from the cropper state.
|
|
9
|
+
*
|
|
10
|
+
* The combined transform is: translate(tx, ty) * scale(sx, sy) * rotate(r) * scale(z)
|
|
11
|
+
* expressed as a 2D CSS matrix(a, b, c, d, tx, ty).
|
|
12
|
+
*
|
|
13
|
+
* Flip is composed outside rotation so it is viewport-relative — horizontal
|
|
14
|
+
* flip mirrors across the viewport's vertical axis regardless of rotation.
|
|
15
|
+
* Must match the matrix order in `createCamera` / `getCropBounds`.
|
|
16
|
+
*
|
|
17
|
+
* This is a pure function with no framework dependencies. The React hook
|
|
18
|
+
* `useTransformStyle` wraps this in `useMemo` for memoization.
|
|
19
|
+
*
|
|
20
|
+
* @param state The current cropper state.
|
|
21
|
+
* @param imageSize The rendered image dimensions in pixels.
|
|
22
|
+
* @return A CSS transform string.
|
|
23
|
+
*/
|
|
24
|
+
export function computeTransformStyle(
|
|
25
|
+
state: CropperState,
|
|
26
|
+
imageSize: Size
|
|
27
|
+
): string {
|
|
28
|
+
const translateX = state.pan.x * imageSize.width;
|
|
29
|
+
const translateY = state.pan.y * imageSize.height;
|
|
30
|
+
const rad = degreesToRadians( state.rotation );
|
|
31
|
+
const cos = Math.cos( rad );
|
|
32
|
+
const sin = Math.sin( rad );
|
|
33
|
+
const sx = state.flip.horizontal ? -1 : 1;
|
|
34
|
+
const sy = state.flip.vertical ? -1 : 1;
|
|
35
|
+
const z = state.zoom;
|
|
36
|
+
|
|
37
|
+
// Combined: translate(tx,ty) * scale(sx,sy) * rotate(r) * scale(z)
|
|
38
|
+
const a = sx * cos * z;
|
|
39
|
+
const b = sy * sin * z;
|
|
40
|
+
const c = -sx * sin * z;
|
|
41
|
+
const d = sy * cos * z;
|
|
42
|
+
|
|
43
|
+
return `matrix(${ a }, ${ b }, ${ c }, ${ d }, ${ translateX }, ${ translateY })`;
|
|
44
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { TransformOperation, CropperState } from '../types';
|
|
5
|
+
import { DEFAULT_STATE } from '../constants';
|
|
6
|
+
import { cropperReducer } from '../state';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Apply a single transform operation to a cropper state, returning
|
|
10
|
+
* a new state with the containment invariant enforced.
|
|
11
|
+
*
|
|
12
|
+
* Delegates to the reducer via APPLY_OPERATION so pipeline replay
|
|
13
|
+
* produces identical bounded state to UI interaction or setter
|
|
14
|
+
* calls. The same logical operation yields the same result
|
|
15
|
+
* regardless of entrypoint.
|
|
16
|
+
*
|
|
17
|
+
* @param state The current cropper state.
|
|
18
|
+
* @param op The operation to apply.
|
|
19
|
+
* @return A new cropper state with the operation applied.
|
|
20
|
+
*/
|
|
21
|
+
export function applyOperationToState(
|
|
22
|
+
state: CropperState,
|
|
23
|
+
op: TransformOperation
|
|
24
|
+
): CropperState {
|
|
25
|
+
return cropperReducer( state, { type: 'APPLY_OPERATION', payload: op } );
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Replay all operations from an initial state to produce the final state.
|
|
30
|
+
*
|
|
31
|
+
* @param pipeline The array of transform operations to replay.
|
|
32
|
+
* @param initialState The starting state. Defaults to DEFAULT_STATE.
|
|
33
|
+
* @return The resulting cropper state after all operations are applied.
|
|
34
|
+
*/
|
|
35
|
+
export function stateFromPipeline(
|
|
36
|
+
pipeline: TransformOperation[],
|
|
37
|
+
initialState: CropperState = { ...DEFAULT_STATE }
|
|
38
|
+
): CropperState {
|
|
39
|
+
return pipeline.reduce(
|
|
40
|
+
( state, op ) => applyOperationToState( state, op ),
|
|
41
|
+
initialState
|
|
42
|
+
);
|
|
43
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { CropperState, TransformOperation } from '../../types';
|
|
5
|
+
import { DEFAULT_STATE } from '../../constants';
|
|
6
|
+
import { applyOperationToState, stateFromPipeline } from '../pipeline';
|
|
7
|
+
import { cropperReducer } from '../../state';
|
|
8
|
+
|
|
9
|
+
describe( 'applyOperationToState', () => {
|
|
10
|
+
let baseState: CropperState;
|
|
11
|
+
|
|
12
|
+
beforeEach( () => {
|
|
13
|
+
baseState = {
|
|
14
|
+
...DEFAULT_STATE,
|
|
15
|
+
flip: { ...DEFAULT_STATE.flip },
|
|
16
|
+
pan: { ...DEFAULT_STATE.pan },
|
|
17
|
+
cropRect: { ...DEFAULT_STATE.cropRect },
|
|
18
|
+
};
|
|
19
|
+
} );
|
|
20
|
+
|
|
21
|
+
it( 'should handle crop: set cropRect', () => {
|
|
22
|
+
const result = applyOperationToState( baseState, {
|
|
23
|
+
type: 'crop',
|
|
24
|
+
rect: { x: 0.1, y: 0.2, width: 0.5, height: 0.6 },
|
|
25
|
+
} );
|
|
26
|
+
|
|
27
|
+
expect( result.cropRect ).toEqual( {
|
|
28
|
+
x: 0.1,
|
|
29
|
+
y: 0.2,
|
|
30
|
+
width: 0.5,
|
|
31
|
+
height: 0.6,
|
|
32
|
+
} );
|
|
33
|
+
} );
|
|
34
|
+
|
|
35
|
+
it( 'should handle rotate: add degrees and normalize', () => {
|
|
36
|
+
const result = applyOperationToState( baseState, {
|
|
37
|
+
type: 'rotate',
|
|
38
|
+
degrees: 90,
|
|
39
|
+
} );
|
|
40
|
+
|
|
41
|
+
expect( result.rotation ).toBe( 90 );
|
|
42
|
+
} );
|
|
43
|
+
|
|
44
|
+
it( 'should handle rotate: accumulate and normalize past 360', () => {
|
|
45
|
+
let state = applyOperationToState( baseState, {
|
|
46
|
+
type: 'rotate',
|
|
47
|
+
degrees: 270,
|
|
48
|
+
} );
|
|
49
|
+
state = applyOperationToState( state, {
|
|
50
|
+
type: 'rotate',
|
|
51
|
+
degrees: 180,
|
|
52
|
+
} );
|
|
53
|
+
|
|
54
|
+
// 270 + 180 = 450 -> 90 (normalized)
|
|
55
|
+
expect( state.rotation ).toBe( 90 );
|
|
56
|
+
} );
|
|
57
|
+
|
|
58
|
+
it( 'should handle rotate: negative degrees normalize correctly', () => {
|
|
59
|
+
const result = applyOperationToState( baseState, {
|
|
60
|
+
type: 'rotate',
|
|
61
|
+
degrees: -90,
|
|
62
|
+
} );
|
|
63
|
+
|
|
64
|
+
expect( result.rotation ).toBe( 270 );
|
|
65
|
+
} );
|
|
66
|
+
|
|
67
|
+
it( 'should handle flip horizontal: toggle on', () => {
|
|
68
|
+
const result = applyOperationToState( baseState, {
|
|
69
|
+
type: 'flip',
|
|
70
|
+
direction: 'horizontal',
|
|
71
|
+
} );
|
|
72
|
+
|
|
73
|
+
expect( result.flip.horizontal ).toBe( true );
|
|
74
|
+
expect( result.flip.vertical ).toBe( false );
|
|
75
|
+
} );
|
|
76
|
+
|
|
77
|
+
it( 'should handle flip horizontal: toggle off', () => {
|
|
78
|
+
const flippedState: CropperState = {
|
|
79
|
+
...baseState,
|
|
80
|
+
flip: { horizontal: true, vertical: false },
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const result = applyOperationToState( flippedState, {
|
|
84
|
+
type: 'flip',
|
|
85
|
+
direction: 'horizontal',
|
|
86
|
+
} );
|
|
87
|
+
|
|
88
|
+
expect( result.flip.horizontal ).toBe( false );
|
|
89
|
+
expect( result.flip.vertical ).toBe( false );
|
|
90
|
+
} );
|
|
91
|
+
|
|
92
|
+
it( 'should handle flip vertical: toggle on', () => {
|
|
93
|
+
const result = applyOperationToState( baseState, {
|
|
94
|
+
type: 'flip',
|
|
95
|
+
direction: 'vertical',
|
|
96
|
+
} );
|
|
97
|
+
|
|
98
|
+
expect( result.flip.horizontal ).toBe( false );
|
|
99
|
+
expect( result.flip.vertical ).toBe( true );
|
|
100
|
+
} );
|
|
101
|
+
|
|
102
|
+
it( 'should handle zoom: set zoom factor', () => {
|
|
103
|
+
const result = applyOperationToState( baseState, {
|
|
104
|
+
type: 'zoom',
|
|
105
|
+
factor: 3.5,
|
|
106
|
+
} );
|
|
107
|
+
|
|
108
|
+
expect( result.zoom ).toBe( 3.5 );
|
|
109
|
+
} );
|
|
110
|
+
|
|
111
|
+
it( 'should not mutate the original state', () => {
|
|
112
|
+
const original = { ...baseState };
|
|
113
|
+
applyOperationToState( baseState, {
|
|
114
|
+
type: 'rotate',
|
|
115
|
+
degrees: 90,
|
|
116
|
+
} );
|
|
117
|
+
|
|
118
|
+
expect( baseState.rotation ).toBe( original.rotation );
|
|
119
|
+
} );
|
|
120
|
+
} );
|
|
121
|
+
|
|
122
|
+
describe( 'stateFromPipeline', () => {
|
|
123
|
+
it( 'should return default state for an empty pipeline', () => {
|
|
124
|
+
const result = stateFromPipeline( [] );
|
|
125
|
+
expect( result.rotation ).toBe( 0 );
|
|
126
|
+
expect( result.zoom ).toBe( 1 );
|
|
127
|
+
expect( result.flip ).toEqual( { horizontal: false, vertical: false } );
|
|
128
|
+
} );
|
|
129
|
+
|
|
130
|
+
it( 'should apply a sequence of operations correctly', () => {
|
|
131
|
+
const pipeline: TransformOperation[] = [
|
|
132
|
+
{ type: 'rotate', degrees: 45 },
|
|
133
|
+
{ type: 'flip', direction: 'horizontal' },
|
|
134
|
+
{ type: 'zoom', factor: 2 },
|
|
135
|
+
{
|
|
136
|
+
type: 'crop',
|
|
137
|
+
rect: { x: 0.25, y: 0.25, width: 0.5, height: 0.5 },
|
|
138
|
+
},
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
const result = stateFromPipeline( pipeline );
|
|
142
|
+
|
|
143
|
+
expect( result.rotation ).toBe( 45 );
|
|
144
|
+
expect( result.flip.horizontal ).toBe( true );
|
|
145
|
+
expect( result.flip.vertical ).toBe( false );
|
|
146
|
+
expect( result.zoom ).toBe( 2 );
|
|
147
|
+
expect( result.cropRect ).toEqual( {
|
|
148
|
+
x: 0.25,
|
|
149
|
+
y: 0.25,
|
|
150
|
+
width: 0.5,
|
|
151
|
+
height: 0.5,
|
|
152
|
+
} );
|
|
153
|
+
} );
|
|
154
|
+
|
|
155
|
+
it( 'should use custom initial state when provided', () => {
|
|
156
|
+
// The custom initial represents a user who committed to this
|
|
157
|
+
// pose via UI actions. Sync base fields so the pipeline replay
|
|
158
|
+
// (which routes through the reducer) doesn't relax them.
|
|
159
|
+
const customInitial: CropperState = {
|
|
160
|
+
...DEFAULT_STATE,
|
|
161
|
+
rotation: 90,
|
|
162
|
+
baseRotation: 90,
|
|
163
|
+
zoom: 2,
|
|
164
|
+
baseZoom: 2,
|
|
165
|
+
flip: { horizontal: true, vertical: false },
|
|
166
|
+
pan: { ...DEFAULT_STATE.pan },
|
|
167
|
+
basePan: { ...DEFAULT_STATE.pan },
|
|
168
|
+
cropRect: { ...DEFAULT_STATE.cropRect },
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const pipeline: TransformOperation[] = [
|
|
172
|
+
{ type: 'rotate', degrees: 90 },
|
|
173
|
+
];
|
|
174
|
+
|
|
175
|
+
const result = stateFromPipeline( pipeline, customInitial );
|
|
176
|
+
|
|
177
|
+
// Single-axis flip is active, so `degrees: 90` (visual CW) is
|
|
178
|
+
// applied as -90° to the rotation field: 90 − 90 = 0.
|
|
179
|
+
expect( result.rotation ).toBe( 0 );
|
|
180
|
+
// Other state should carry forward.
|
|
181
|
+
expect( result.zoom ).toBe( 2 );
|
|
182
|
+
expect( result.flip.horizontal ).toBe( true );
|
|
183
|
+
} );
|
|
184
|
+
} );
|
|
185
|
+
|
|
186
|
+
describe( 'pipeline / reducer parity', () => {
|
|
187
|
+
// These tests pin the API parity contract: applying a pipeline op
|
|
188
|
+
// via applyOperationToState and dispatching the equivalent reducer
|
|
189
|
+
// action should produce the same bounded state.
|
|
190
|
+
const stateWithImage = {
|
|
191
|
+
...DEFAULT_STATE,
|
|
192
|
+
image: {
|
|
193
|
+
src: 'test.jpg',
|
|
194
|
+
naturalWidth: 1600,
|
|
195
|
+
naturalHeight: 900,
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
it( 'rotate op matches SET_ROTATION (absolute angle)', () => {
|
|
200
|
+
const pipelineResult = applyOperationToState( stateWithImage, {
|
|
201
|
+
type: 'rotate',
|
|
202
|
+
degrees: 30,
|
|
203
|
+
} );
|
|
204
|
+
// Pipeline op is additive, reducer action is absolute, so use
|
|
205
|
+
// the resolved absolute angle in the reducer call.
|
|
206
|
+
const reducerResult = cropperReducer( stateWithImage, {
|
|
207
|
+
type: 'SET_ROTATION',
|
|
208
|
+
payload: 30,
|
|
209
|
+
} );
|
|
210
|
+
expect( pipelineResult.rotation ).toBe( reducerResult.rotation );
|
|
211
|
+
expect( pipelineResult.zoom ).toBe( reducerResult.zoom );
|
|
212
|
+
expect( pipelineResult.pan.x ).toBeCloseTo( reducerResult.pan.x, 6 );
|
|
213
|
+
expect( pipelineResult.pan.y ).toBeCloseTo( reducerResult.pan.y, 6 );
|
|
214
|
+
} );
|
|
215
|
+
|
|
216
|
+
it( 'zoom op matches SET_ZOOM', () => {
|
|
217
|
+
const pipelineResult = applyOperationToState( stateWithImage, {
|
|
218
|
+
type: 'zoom',
|
|
219
|
+
factor: 3,
|
|
220
|
+
} );
|
|
221
|
+
const reducerResult = cropperReducer( stateWithImage, {
|
|
222
|
+
type: 'SET_ZOOM',
|
|
223
|
+
payload: 3,
|
|
224
|
+
} );
|
|
225
|
+
expect( pipelineResult.zoom ).toBe( reducerResult.zoom );
|
|
226
|
+
expect( pipelineResult.baseZoom ).toBe( reducerResult.baseZoom );
|
|
227
|
+
} );
|
|
228
|
+
|
|
229
|
+
it( 'flip op matches SET_FLIP', () => {
|
|
230
|
+
const pipelineResult = applyOperationToState( stateWithImage, {
|
|
231
|
+
type: 'flip',
|
|
232
|
+
direction: 'horizontal',
|
|
233
|
+
} );
|
|
234
|
+
const reducerResult = cropperReducer( stateWithImage, {
|
|
235
|
+
type: 'SET_FLIP',
|
|
236
|
+
payload: { horizontal: true, vertical: false },
|
|
237
|
+
} );
|
|
238
|
+
expect( pipelineResult.flip ).toEqual( reducerResult.flip );
|
|
239
|
+
} );
|
|
240
|
+
|
|
241
|
+
it( 'crop op matches SET_CROP_RECT', () => {
|
|
242
|
+
const rect = { x: 0.1, y: 0.1, width: 0.4, height: 0.4 };
|
|
243
|
+
const pipelineResult = applyOperationToState( stateWithImage, {
|
|
244
|
+
type: 'crop',
|
|
245
|
+
rect,
|
|
246
|
+
} );
|
|
247
|
+
const reducerResult = cropperReducer( stateWithImage, {
|
|
248
|
+
type: 'SET_CROP_RECT',
|
|
249
|
+
payload: rect,
|
|
250
|
+
} );
|
|
251
|
+
expect( pipelineResult.cropRect ).toEqual( reducerResult.cropRect );
|
|
252
|
+
expect( pipelineResult.zoom ).toBe( reducerResult.zoom );
|
|
253
|
+
} );
|
|
254
|
+
|
|
255
|
+
it( 'rotate op: degrees means visual CW, negated on single-axis flip', () => {
|
|
256
|
+
// `{ type: 'rotate', degrees: 90 }` means "90° clockwise visually".
|
|
257
|
+
// On a single-flipped image the rotation field must go the other
|
|
258
|
+
// way to achieve that visual outcome — matching snapRotate90.
|
|
259
|
+
const flippedState = {
|
|
260
|
+
...stateWithImage,
|
|
261
|
+
flip: { horizontal: true, vertical: false },
|
|
262
|
+
};
|
|
263
|
+
const result = applyOperationToState( flippedState, {
|
|
264
|
+
type: 'rotate',
|
|
265
|
+
degrees: 90,
|
|
266
|
+
} );
|
|
267
|
+
expect( result.rotation ).toBe( 270 );
|
|
268
|
+
} );
|
|
269
|
+
} );
|