@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,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalizes a rotation value to the range [0, 360).
|
|
3
|
+
*
|
|
4
|
+
* Negative rotations are converted to their positive clockwise equivalent.
|
|
5
|
+
* For example, -90 becomes 270, -45 becomes 315, and 450 becomes 90.
|
|
6
|
+
*
|
|
7
|
+
* @param degrees The rotation value in degrees.
|
|
8
|
+
* @return The normalized rotation in the range [0, 360).
|
|
9
|
+
*/
|
|
10
|
+
export function normalizeRotation( degrees: number ): number {
|
|
11
|
+
if ( degrees >= 0 ) {
|
|
12
|
+
return degrees % 360;
|
|
13
|
+
}
|
|
14
|
+
// For negative rotations, convert to positive clockwise equivalent.
|
|
15
|
+
return ( 360 + ( degrees % 360 ) ) % 360;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Converts an angle from degrees to radians.
|
|
20
|
+
*
|
|
21
|
+
* @param degrees The angle in degrees.
|
|
22
|
+
* @return The angle in radians.
|
|
23
|
+
*/
|
|
24
|
+
export function degreesToRadians( degrees: number ): number {
|
|
25
|
+
return ( degrees * Math.PI ) / 180;
|
|
26
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { mat2d, vec2 } from 'gl-matrix';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import type { CropperState, Size } from './types';
|
|
10
|
+
import { createCamera, getRotatedBBox, getVisibleBounds } from './camera';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The selected image region in source-pixel coordinates.
|
|
14
|
+
*/
|
|
15
|
+
export interface SourceRegion {
|
|
16
|
+
/** X offset in source pixels. */
|
|
17
|
+
x: number;
|
|
18
|
+
/** Y offset in source pixels. */
|
|
19
|
+
y: number;
|
|
20
|
+
/** Width in source pixels. */
|
|
21
|
+
width: number;
|
|
22
|
+
/** Height in source pixels. */
|
|
23
|
+
height: number;
|
|
24
|
+
/** Rotation in degrees (0-360). */
|
|
25
|
+
rotation: number;
|
|
26
|
+
/** Flip state. */
|
|
27
|
+
flip: { horizontal: boolean; vertical: boolean };
|
|
28
|
+
/** Zoom factor applied. */
|
|
29
|
+
zoom: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Get the selected image region in source-pixel coordinates.
|
|
34
|
+
*
|
|
35
|
+
* Converts the current crop state (normalized visual space) to the actual
|
|
36
|
+
* region of the original image that's selected. This is the bridge between
|
|
37
|
+
* the cropper's internal coordinate system and external tools (image
|
|
38
|
+
* processing libraries, AI APIs, server-side processing) that work in
|
|
39
|
+
* source-pixel coordinates.
|
|
40
|
+
*
|
|
41
|
+
* The returned rectangle accounts for pan, zoom, and the crop rect position,
|
|
42
|
+
* but expresses the crop in the unrotated image's coordinate space. Rotation
|
|
43
|
+
* and flip are included as separate fields since they represent transforms,
|
|
44
|
+
* not a region.
|
|
45
|
+
*
|
|
46
|
+
* @param state The current cropper state.
|
|
47
|
+
* @param imageSize The natural dimensions of the source image.
|
|
48
|
+
* @return The selected region in source pixels plus rotation/flip metadata.
|
|
49
|
+
*/
|
|
50
|
+
export function getSourceRegion(
|
|
51
|
+
state: CropperState,
|
|
52
|
+
imageSize: Size
|
|
53
|
+
): SourceRegion {
|
|
54
|
+
if ( imageSize.width === 0 || imageSize.height === 0 ) {
|
|
55
|
+
return {
|
|
56
|
+
x: 0,
|
|
57
|
+
y: 0,
|
|
58
|
+
width: 0,
|
|
59
|
+
height: 0,
|
|
60
|
+
rotation: state.rotation,
|
|
61
|
+
flip: { ...state.flip },
|
|
62
|
+
zoom: state.zoom,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Use a synthetic 1:1 container so the camera maps normalized coords
|
|
67
|
+
// to a known pixel space. The container size cancels out.
|
|
68
|
+
const syntheticContainer: Size = { width: 1000, height: 1000 };
|
|
69
|
+
const camera = createCamera( state, syntheticContainer, imageSize );
|
|
70
|
+
|
|
71
|
+
// Inverse camera maps screen pixels back to normalized [0,1] world coords.
|
|
72
|
+
const inv = mat2d.create();
|
|
73
|
+
mat2d.invert( inv, camera );
|
|
74
|
+
|
|
75
|
+
// The crop rect center in screen space. We need the base camera
|
|
76
|
+
// (zoom=1, no pan) to locate the visual bounds, then place the
|
|
77
|
+
// crop rect within them.
|
|
78
|
+
const baseCamera = createCamera(
|
|
79
|
+
{ ...state, pan: { x: 0, y: 0 }, zoom: 1 },
|
|
80
|
+
syntheticContainer,
|
|
81
|
+
imageSize
|
|
82
|
+
);
|
|
83
|
+
const visibleBounds = getVisibleBounds( baseCamera );
|
|
84
|
+
|
|
85
|
+
const cropRect = state.cropRect;
|
|
86
|
+
const cropCenterScreenX =
|
|
87
|
+
visibleBounds.left +
|
|
88
|
+
( cropRect.x + cropRect.width / 2 ) * visibleBounds.width;
|
|
89
|
+
const cropCenterScreenY =
|
|
90
|
+
visibleBounds.top +
|
|
91
|
+
( cropRect.y + cropRect.height / 2 ) * visibleBounds.height;
|
|
92
|
+
|
|
93
|
+
// Transform crop center through inverse camera to get source position.
|
|
94
|
+
const srcCenter = vec2.create();
|
|
95
|
+
vec2.transformMat2d(
|
|
96
|
+
srcCenter,
|
|
97
|
+
[ cropCenterScreenX, cropCenterScreenY ],
|
|
98
|
+
inv
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
// Crop rect size in the snap-rotation visual space, divided by zoom
|
|
102
|
+
// for source-pixel dimensions. Matches the stencil's reference frame.
|
|
103
|
+
const snapRotation = Math.round( state.rotation / 90 ) * 90;
|
|
104
|
+
const { width: rotW, height: rotH } = getRotatedBBox(
|
|
105
|
+
imageSize.width,
|
|
106
|
+
imageSize.height,
|
|
107
|
+
snapRotation
|
|
108
|
+
);
|
|
109
|
+
const sourceW = ( cropRect.width * rotW ) / state.zoom;
|
|
110
|
+
const sourceH = ( cropRect.height * rotH ) / state.zoom;
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
x: srcCenter[ 0 ] * imageSize.width - sourceW / 2,
|
|
114
|
+
y: srcCenter[ 1 ] * imageSize.height - sourceH / 2,
|
|
115
|
+
width: sourceW,
|
|
116
|
+
height: sourceH,
|
|
117
|
+
rotation: state.rotation,
|
|
118
|
+
flip: { ...state.flip },
|
|
119
|
+
zoom: state.zoom,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The selected crop region expressed as percentages of the source image.
|
|
125
|
+
*/
|
|
126
|
+
export interface SourceRegionPercent {
|
|
127
|
+
/** X offset as a percentage (0–100) of the source image width. */
|
|
128
|
+
x: number;
|
|
129
|
+
/** Y offset as a percentage (0–100) of the source image height. */
|
|
130
|
+
y: number;
|
|
131
|
+
/** Width as a percentage (0–100) of the source image width. */
|
|
132
|
+
width: number;
|
|
133
|
+
/** Height as a percentage (0–100) of the source image height. */
|
|
134
|
+
height: number;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Get the selected image region as percentages of the source image dimensions.
|
|
139
|
+
*
|
|
140
|
+
* Returns `{ x, y, width, height }` where each value is a percentage (0–100)
|
|
141
|
+
* of the source image's natural width or height. This format is compatible
|
|
142
|
+
* with the WordPress REST API attachments `/edit` endpoint and CSS-based
|
|
143
|
+
* crop workflows.
|
|
144
|
+
*
|
|
145
|
+
* Internally delegates to `getSourceRegion` and divides by the image
|
|
146
|
+
* dimensions, so accuracy is identical.
|
|
147
|
+
*
|
|
148
|
+
* @param state The current cropper state.
|
|
149
|
+
* @param imageSize The natural dimensions of the source image.
|
|
150
|
+
* @return The crop region as percentages (0–100).
|
|
151
|
+
*/
|
|
152
|
+
export function getSourceRegionPercent(
|
|
153
|
+
state: CropperState,
|
|
154
|
+
imageSize: Size
|
|
155
|
+
): SourceRegionPercent {
|
|
156
|
+
if ( imageSize.width === 0 || imageSize.height === 0 ) {
|
|
157
|
+
return { x: 0, y: 0, width: 0, height: 0 };
|
|
158
|
+
}
|
|
159
|
+
const region = getSourceRegion( state, imageSize );
|
|
160
|
+
return {
|
|
161
|
+
x: ( region.x / imageSize.width ) * 100,
|
|
162
|
+
y: ( region.y / imageSize.height ) * 100,
|
|
163
|
+
width: ( region.width / imageSize.width ) * 100,
|
|
164
|
+
height: ( region.height / imageSize.height ) * 100,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { CropperAction, CropperState, TransformOperation } from './types';
|
|
5
|
+
import { DEFAULT_STATE, MAX_ZOOM } from './constants';
|
|
6
|
+
import { normalizeRotation, degreesToRadians } from './math/rotation';
|
|
7
|
+
import { restrictPanZoom, restrictCropRect } from './containment';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Translate a pipeline transform operation into the equivalent
|
|
11
|
+
* reducer action. Pipeline ops aren't 1:1 with reducer actions —
|
|
12
|
+
* `rotate` is a delta here, whereas SET_ROTATION takes an absolute
|
|
13
|
+
* angle — so deltas are resolved against the current state.
|
|
14
|
+
*
|
|
15
|
+
* Used by APPLY_OPERATION (and by the pipeline module's
|
|
16
|
+
* `applyOperationToState`, which is now a thin wrapper around the
|
|
17
|
+
* reducer) so that pipeline replay produces identical bounded
|
|
18
|
+
* state to UI interaction or direct setters.
|
|
19
|
+
*
|
|
20
|
+
* @param state The current cropper state.
|
|
21
|
+
* @param op The pipeline operation.
|
|
22
|
+
* @return The equivalent reducer action.
|
|
23
|
+
*/
|
|
24
|
+
function operationToAction(
|
|
25
|
+
state: CropperState,
|
|
26
|
+
op: TransformOperation
|
|
27
|
+
): CropperAction {
|
|
28
|
+
switch ( op.type ) {
|
|
29
|
+
case 'crop':
|
|
30
|
+
return { type: 'SET_CROP_RECT', payload: { ...op.rect } };
|
|
31
|
+
case 'rotate': {
|
|
32
|
+
// `op.degrees` is the visual rotation the caller wants (positive
|
|
33
|
+
// = CW on screen). A single-axis flip inverts on-screen rotation
|
|
34
|
+
// relative to the rotation field, so negate the field delta in
|
|
35
|
+
// that case — this keeps `{ type: 'rotate', degrees: 90 }` a
|
|
36
|
+
// reliable "90° clockwise visually" instruction across flip
|
|
37
|
+
// states, matching the SNAP_ROTATE_90 convention.
|
|
38
|
+
const singleFlip = state.flip.horizontal !== state.flip.vertical;
|
|
39
|
+
const fieldDelta = singleFlip ? -op.degrees : op.degrees;
|
|
40
|
+
return {
|
|
41
|
+
type: 'SET_ROTATION',
|
|
42
|
+
payload: normalizeRotation( state.rotation + fieldDelta ),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
case 'flip':
|
|
46
|
+
return {
|
|
47
|
+
type: 'SET_FLIP',
|
|
48
|
+
payload: {
|
|
49
|
+
...state.flip,
|
|
50
|
+
[ op.direction ]: ! state.flip[ op.direction ],
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
case 'zoom':
|
|
54
|
+
return { type: 'SET_ZOOM', payload: op.factor };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Enforces containment: restricts the crop rect to fit within the
|
|
60
|
+
* rotated image, computes minimum zoom, clamps zoom, and restricts
|
|
61
|
+
* the pan position so the image always covers the crop area.
|
|
62
|
+
* Called after every relevant state transition.
|
|
63
|
+
*
|
|
64
|
+
* @param state The state to enforce containment on.
|
|
65
|
+
* @return The state with cropRect, zoom, and position restricted.
|
|
66
|
+
*/
|
|
67
|
+
export function enforceContainment( state: CropperState ): CropperState {
|
|
68
|
+
if ( ! state.image ) {
|
|
69
|
+
return state;
|
|
70
|
+
}
|
|
71
|
+
const imageSize = {
|
|
72
|
+
width: state.image.naturalWidth,
|
|
73
|
+
height: state.image.naturalHeight,
|
|
74
|
+
};
|
|
75
|
+
const imageAspectRatio = imageSize.width / imageSize.height;
|
|
76
|
+
|
|
77
|
+
// 1. First bump zoom so the image can cover the crop rect as-is.
|
|
78
|
+
// This ensures that explicit crop rect changes (e.g., fixed-crop
|
|
79
|
+
// mode during rotation) get zoom accommodation, not crop shrinkage.
|
|
80
|
+
const { pan: panAfterZoom, zoom } = restrictPanZoom(
|
|
81
|
+
state,
|
|
82
|
+
imageSize,
|
|
83
|
+
state.cropRect
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// 2. Now restrict the crop rect at the (possibly bumped) zoom.
|
|
87
|
+
// This handles cases where the crop rect is still too large
|
|
88
|
+
// (e.g., if zoom hit MAX_ZOOM).
|
|
89
|
+
const cropRect = restrictCropRect(
|
|
90
|
+
state.cropRect,
|
|
91
|
+
zoom,
|
|
92
|
+
state.rotation,
|
|
93
|
+
imageAspectRatio
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
// 3. If the crop rect was shrunk, re-restrict pan for the new rect.
|
|
97
|
+
let pan = panAfterZoom;
|
|
98
|
+
if ( cropRect !== state.cropRect ) {
|
|
99
|
+
( { pan } = restrictPanZoom(
|
|
100
|
+
{ ...state, zoom, cropRect },
|
|
101
|
+
imageSize,
|
|
102
|
+
cropRect
|
|
103
|
+
) );
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (
|
|
107
|
+
pan.x === state.pan.x &&
|
|
108
|
+
pan.y === state.pan.y &&
|
|
109
|
+
zoom === state.zoom &&
|
|
110
|
+
cropRect === state.cropRect
|
|
111
|
+
) {
|
|
112
|
+
return state;
|
|
113
|
+
}
|
|
114
|
+
return { ...state, pan, zoom, cropRect };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Snapshot the current `crop`, `zoom`, and `rotation` into the
|
|
119
|
+
* corresponding `basePan`, `baseZoom`, `baseRotation` fields. Called
|
|
120
|
+
* by every committing action so that the next SET_ROTATION derives
|
|
121
|
+
* its transient pan from a fresh base (not from a drifted one).
|
|
122
|
+
*
|
|
123
|
+
* SET_ROTATION is the only action that does NOT commit base — it
|
|
124
|
+
* reads the base, computes a transient pan, and leaves the base
|
|
125
|
+
* alone. This is what eliminates rotation drift near edges.
|
|
126
|
+
*
|
|
127
|
+
* @param next The post-containment state to commit.
|
|
128
|
+
* @return The state with base fields synced to current.
|
|
129
|
+
*/
|
|
130
|
+
function commitBase( next: CropperState ): CropperState {
|
|
131
|
+
if (
|
|
132
|
+
next.basePan.x === next.pan.x &&
|
|
133
|
+
next.basePan.y === next.pan.y &&
|
|
134
|
+
next.baseZoom === next.zoom &&
|
|
135
|
+
next.baseRotation === next.rotation
|
|
136
|
+
) {
|
|
137
|
+
return next;
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
...next,
|
|
141
|
+
basePan: { x: next.pan.x, y: next.pan.y },
|
|
142
|
+
baseZoom: next.zoom,
|
|
143
|
+
baseRotation: next.rotation,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Reducer function for cropper state management.
|
|
149
|
+
*
|
|
150
|
+
* Every state transition that could invalidate the containment invariant
|
|
151
|
+
* (crop, zoom, rotation, cropRect, flip) is followed by enforceContainment
|
|
152
|
+
* to ensure the image always covers the crop area.
|
|
153
|
+
*
|
|
154
|
+
* Committing actions (everything except SET_ROTATION) also call
|
|
155
|
+
* `commitBase` to snapshot the post-containment state as the new
|
|
156
|
+
* base pose. SET_ROTATION derives from that base without mutating
|
|
157
|
+
* it, which prevents accumulated-clamp drift during continuous fine
|
|
158
|
+
* rotation.
|
|
159
|
+
*
|
|
160
|
+
* @param state The current cropper state.
|
|
161
|
+
* @param action The action to process.
|
|
162
|
+
* @return The new cropper state.
|
|
163
|
+
*/
|
|
164
|
+
export function cropperReducer(
|
|
165
|
+
state: CropperState,
|
|
166
|
+
action: CropperAction
|
|
167
|
+
): CropperState {
|
|
168
|
+
// Every action runs through enforceContainment to maintain the invariant:
|
|
169
|
+
// the image always fully covers the crop area.
|
|
170
|
+
switch ( action.type ) {
|
|
171
|
+
case 'SET_IMAGE':
|
|
172
|
+
return commitBase(
|
|
173
|
+
enforceContainment( {
|
|
174
|
+
...state,
|
|
175
|
+
image: action.payload,
|
|
176
|
+
} )
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
case 'SET_PAN':
|
|
180
|
+
return commitBase(
|
|
181
|
+
enforceContainment( {
|
|
182
|
+
...state,
|
|
183
|
+
pan: action.payload,
|
|
184
|
+
} )
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
case 'SET_ZOOM': {
|
|
188
|
+
const z = Math.min( MAX_ZOOM, Math.max( 1, action.payload ) );
|
|
189
|
+
return commitBase(
|
|
190
|
+
enforceContainment( {
|
|
191
|
+
...state,
|
|
192
|
+
zoom: z,
|
|
193
|
+
} )
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
case 'SET_ZOOM_AT_POINT': {
|
|
198
|
+
const z = Math.min( MAX_ZOOM, Math.max( 1, action.payload.zoom ) );
|
|
199
|
+
return commitBase(
|
|
200
|
+
enforceContainment( {
|
|
201
|
+
...state,
|
|
202
|
+
zoom: z,
|
|
203
|
+
pan: action.payload.pan,
|
|
204
|
+
} )
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
case 'SET_ROTATION': {
|
|
209
|
+
// Rotate the base pan around the crop-rect center by the
|
|
210
|
+
// delta from the base rotation. Deriving each tick from
|
|
211
|
+
// the fixed base — rather than compounding on the previous
|
|
212
|
+
// tick — prevents accumulated-clamp drift. Rotating
|
|
213
|
+
// 0°→30°→0° now returns to the exact base pan.
|
|
214
|
+
//
|
|
215
|
+
// Zoom starts from baseZoom; enforceContainment may bump it
|
|
216
|
+
// up to cover the rotated crop, but never raises the base.
|
|
217
|
+
//
|
|
218
|
+
// SET_ROTATION is the only action that does NOT call
|
|
219
|
+
// commitBase — the base pose stays pinned at the user's
|
|
220
|
+
// last committed pan/zoom/rotation.
|
|
221
|
+
const newRotation = normalizeRotation( action.payload );
|
|
222
|
+
// With viewport-relative flip, pan rotation is conjugated by
|
|
223
|
+
// the flip (S * R * S) — which reverses direction when exactly
|
|
224
|
+
// one axis is flipped. Negate sin in that case so the crop
|
|
225
|
+
// stays framed on the same image content through rotation.
|
|
226
|
+
const singleFlip = state.flip.horizontal !== state.flip.vertical;
|
|
227
|
+
const deltaRad = degreesToRadians(
|
|
228
|
+
newRotation - state.baseRotation
|
|
229
|
+
);
|
|
230
|
+
const cos = Math.cos( deltaRad );
|
|
231
|
+
const sin = Math.sin( deltaRad ) * ( singleFlip ? -1 : 1 );
|
|
232
|
+
const cropCx = state.cropRect.x + state.cropRect.width / 2;
|
|
233
|
+
const cropCy = state.cropRect.y + state.cropRect.height / 2;
|
|
234
|
+
const ox = cropCx - 0.5;
|
|
235
|
+
const oy = cropCy - 0.5;
|
|
236
|
+
const dx = state.basePan.x - ox;
|
|
237
|
+
const dy = state.basePan.y - oy;
|
|
238
|
+
return enforceContainment( {
|
|
239
|
+
...state,
|
|
240
|
+
rotation: newRotation,
|
|
241
|
+
zoom: state.baseZoom,
|
|
242
|
+
pan: {
|
|
243
|
+
x: ox + cos * dx - sin * dy,
|
|
244
|
+
y: oy + sin * dx + cos * dy,
|
|
245
|
+
},
|
|
246
|
+
} );
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
case 'SNAP_ROTATE_90': {
|
|
250
|
+
// 90° snap: the crop rect rotates 90° around its own center
|
|
251
|
+
// (width/height swap), and the pan rotates 90° around the
|
|
252
|
+
// pan-space origin. The combination keeps the same image
|
|
253
|
+
// slice framed — just viewed through a rotated window.
|
|
254
|
+
//
|
|
255
|
+
// `direction` is the visual rotation the caller wants: +1
|
|
256
|
+
// means "rotate the image CW on screen", −1 means "CCW".
|
|
257
|
+
// With a single-axis flip active, rotation on screen appears
|
|
258
|
+
// reversed relative to the rotation field (S·R·S conjugation),
|
|
259
|
+
// so internally we negate the direction applied to the
|
|
260
|
+
// `rotation` field. Pan rotates in the caller's direction —
|
|
261
|
+
// that way the same image content stays framed after the
|
|
262
|
+
// snap regardless of flip state.
|
|
263
|
+
const rawDir = action.payload.direction;
|
|
264
|
+
const singleFlip = state.flip.horizontal !== state.flip.vertical;
|
|
265
|
+
const fieldDir = singleFlip ? -rawDir : rawDir;
|
|
266
|
+
const rot90 = normalizeRotation( state.rotation + fieldDir * 90 );
|
|
267
|
+
const rect = state.cropRect;
|
|
268
|
+
const cx = rect.x + rect.width / 2;
|
|
269
|
+
const cy = rect.y + rect.height / 2;
|
|
270
|
+
|
|
271
|
+
// Rotate pan vector 90° around origin.
|
|
272
|
+
// CW (rawDir=+1): (px, py) → (-py, px)
|
|
273
|
+
// CCW (rawDir=-1): (px, py) → (py, -px)
|
|
274
|
+
const newPanX = rawDir === 1 ? -state.pan.y : state.pan.y;
|
|
275
|
+
const newPanY = rawDir === 1 ? state.pan.x : -state.pan.x;
|
|
276
|
+
|
|
277
|
+
return commitBase(
|
|
278
|
+
enforceContainment( {
|
|
279
|
+
...state,
|
|
280
|
+
rotation: rot90,
|
|
281
|
+
zoom: state.baseZoom,
|
|
282
|
+
pan: { x: newPanX, y: newPanY },
|
|
283
|
+
cropRect: {
|
|
284
|
+
x: cx - rect.height / 2,
|
|
285
|
+
y: cy - rect.width / 2,
|
|
286
|
+
width: rect.height,
|
|
287
|
+
height: rect.width,
|
|
288
|
+
},
|
|
289
|
+
} )
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
case 'SET_FLIP': {
|
|
294
|
+
// Mirror the crop rect and pan so the same image content stays
|
|
295
|
+
// framed after a flip. Flip in the camera matrix is composed
|
|
296
|
+
// outside rotation (viewport-relative), so both `pan` and
|
|
297
|
+
// `cropRect` live in viewport-aligned space: negating the flipped
|
|
298
|
+
// axis on each is enough — no rotation math needed.
|
|
299
|
+
const oldFlip = state.flip;
|
|
300
|
+
const newFlip = action.payload;
|
|
301
|
+
const flippedH = oldFlip.horizontal !== newFlip.horizontal;
|
|
302
|
+
const flippedV = oldFlip.vertical !== newFlip.vertical;
|
|
303
|
+
const rect = state.cropRect;
|
|
304
|
+
|
|
305
|
+
return commitBase(
|
|
306
|
+
enforceContainment( {
|
|
307
|
+
...state,
|
|
308
|
+
flip: newFlip,
|
|
309
|
+
pan: {
|
|
310
|
+
x: flippedH ? -state.pan.x : state.pan.x,
|
|
311
|
+
y: flippedV ? -state.pan.y : state.pan.y,
|
|
312
|
+
},
|
|
313
|
+
cropRect: {
|
|
314
|
+
x: flippedH ? 1 - rect.x - rect.width : rect.x,
|
|
315
|
+
y: flippedV ? 1 - rect.y - rect.height : rect.y,
|
|
316
|
+
width: rect.width,
|
|
317
|
+
height: rect.height,
|
|
318
|
+
},
|
|
319
|
+
} )
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
case 'SET_CROP_RECT':
|
|
324
|
+
return commitBase(
|
|
325
|
+
enforceContainment( {
|
|
326
|
+
...state,
|
|
327
|
+
cropRect: action.payload,
|
|
328
|
+
} )
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
case 'SETTLE_CROP': {
|
|
332
|
+
// After a resize drag ends: expand the crop to fill the
|
|
333
|
+
// available height (maintaining its aspect ratio), center it,
|
|
334
|
+
// and adjust zoom/pan so the exact same image content that
|
|
335
|
+
// was visible inside the old crop is visible in the new one.
|
|
336
|
+
const rect = state.cropRect;
|
|
337
|
+
if ( rect.width === 0 || rect.height === 0 || ! state.image ) {
|
|
338
|
+
return state;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// New crop: fill height (or width), maintain aspect ratio, center.
|
|
342
|
+
const normalizedRatio = rect.width / rect.height;
|
|
343
|
+
let newH = 1;
|
|
344
|
+
let newW = normalizedRatio;
|
|
345
|
+
if ( newW > 1 ) {
|
|
346
|
+
newW = 1;
|
|
347
|
+
newH = 1 / normalizedRatio;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Scale factor: how much the crop grew.
|
|
351
|
+
const s = newH / rect.height;
|
|
352
|
+
|
|
353
|
+
// The old crop center in normalized visual space.
|
|
354
|
+
const oldCx = rect.x + rect.width / 2;
|
|
355
|
+
const oldCy = rect.y + rect.height / 2;
|
|
356
|
+
|
|
357
|
+
// Zoom scales by s so the same image region fills the
|
|
358
|
+
// larger crop at the same relative size.
|
|
359
|
+
// Pan: the visible content center was at
|
|
360
|
+
// (cropCx - crop.x, cropCy - crop.y)
|
|
361
|
+
// in visual-normalized space. After centering the crop to
|
|
362
|
+
// (0.5, 0.5), the pan must place that same content at
|
|
363
|
+
// the new center. Both pan and zoom scale by s because
|
|
364
|
+
// the CSS translate is independent of zoom.
|
|
365
|
+
return commitBase(
|
|
366
|
+
enforceContainment( {
|
|
367
|
+
...state,
|
|
368
|
+
zoom: state.zoom * s,
|
|
369
|
+
pan: {
|
|
370
|
+
x: ( state.pan.x - oldCx + 0.5 ) * s,
|
|
371
|
+
y: ( state.pan.y - oldCy + 0.5 ) * s,
|
|
372
|
+
},
|
|
373
|
+
cropRect: {
|
|
374
|
+
x: ( 1 - newW ) / 2,
|
|
375
|
+
y: ( 1 - newH ) / 2,
|
|
376
|
+
width: newW,
|
|
377
|
+
height: newH,
|
|
378
|
+
},
|
|
379
|
+
} )
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
case 'APPLY_OPERATION':
|
|
384
|
+
// Route through the action equivalent so a pipeline replay
|
|
385
|
+
// yields the same bounded state as the matching UI/setter
|
|
386
|
+
// path (containment, baseZoom relaxation, pan rotation
|
|
387
|
+
// around crop center, etc.).
|
|
388
|
+
return cropperReducer(
|
|
389
|
+
state,
|
|
390
|
+
operationToAction( state, action.payload )
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
case 'RESET':
|
|
394
|
+
return commitBase(
|
|
395
|
+
enforceContainment( {
|
|
396
|
+
...DEFAULT_STATE,
|
|
397
|
+
image: state.image,
|
|
398
|
+
...action.payload,
|
|
399
|
+
} )
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Shallow comparison of key cropper state fields to determine if
|
|
406
|
+
* the state has been modified from an initial snapshot.
|
|
407
|
+
*
|
|
408
|
+
* @param current The current cropper state.
|
|
409
|
+
* @param initial The initial cropper state snapshot.
|
|
410
|
+
* @return True if any tracked field differs.
|
|
411
|
+
*/
|
|
412
|
+
export function isStateDirty(
|
|
413
|
+
current: CropperState,
|
|
414
|
+
initial: CropperState
|
|
415
|
+
): boolean {
|
|
416
|
+
return (
|
|
417
|
+
current.pan.x !== initial.pan.x ||
|
|
418
|
+
current.pan.y !== initial.pan.y ||
|
|
419
|
+
current.zoom !== initial.zoom ||
|
|
420
|
+
current.rotation !== initial.rotation ||
|
|
421
|
+
current.flip.horizontal !== initial.flip.horizontal ||
|
|
422
|
+
current.flip.vertical !== initial.flip.vertical ||
|
|
423
|
+
current.cropRect.x !== initial.cropRect.x ||
|
|
424
|
+
current.cropRect.y !== initial.cropRect.y ||
|
|
425
|
+
current.cropRect.width !== initial.cropRect.width ||
|
|
426
|
+
current.cropRect.height !== initial.cropRect.height
|
|
427
|
+
);
|
|
428
|
+
}
|