@wordpress/media-editor 0.9.0 → 0.10.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/index.cjs +34 -135
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +87 -0
- package/build/components/media-editor/use-crop-options.cjs.map +7 -0
- package/build/components/media-editor/use-save-media-editor.cjs +185 -0
- package/build/components/media-editor/use-save-media-editor.cjs.map +7 -0
- package/build/components/media-editor-canvas/index.cjs +32 -11
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +66 -83
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-modal/index.cjs +40 -11
- package/build/components/media-editor-modal/index.cjs.map +3 -3
- package/build/components/media-editor-toolbar/index.cjs +7 -3
- package/build/components/media-editor-toolbar/index.cjs.map +2 -2
- package/build/components/media-form/index.cjs +19 -1
- package/build/components/media-form/index.cjs.map +3 -3
- package/build/components/media-form/sidebar-datetime-view.cjs +53 -0
- package/build/components/media-form/sidebar-datetime-view.cjs.map +7 -0
- package/build/components/rotation-ruler/index.cjs +8 -2
- package/build/components/rotation-ruler/index.cjs.map +2 -2
- package/build/hooks/use-crop-gesture-handlers.cjs +41 -4
- package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
- package/build/image-editor/core/camera.cjs +29 -11
- package/build/image-editor/core/camera.cjs.map +2 -2
- package/build/image-editor/core/constants.cjs +6 -0
- package/build/image-editor/core/constants.cjs.map +2 -2
- package/build/image-editor/core/containment.cjs +71 -34
- package/build/image-editor/core/containment.cjs.map +2 -2
- package/build/image-editor/core/crop-rect.cjs +48 -0
- package/build/image-editor/core/crop-rect.cjs.map +7 -0
- package/build/image-editor/core/index.cjs +12 -0
- package/build/image-editor/core/index.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/core/math/sanitize.cjs +92 -0
- package/build/image-editor/core/math/sanitize.cjs.map +7 -0
- package/build/image-editor/core/setter-helpers.cjs +64 -0
- package/build/image-editor/core/setter-helpers.cjs.map +7 -0
- package/build/image-editor/core/source-region.cjs +16 -14
- package/build/image-editor/core/source-region.cjs.map +2 -2
- package/build/image-editor/core/state.cjs +20 -10
- package/build/image-editor/core/state.cjs.map +2 -2
- package/build/image-editor/core/stencil-math.cjs +31 -20
- package/build/image-editor/core/stencil-math.cjs.map +2 -2
- package/build/image-editor/core/transform-style.cjs +12 -6
- package/build/image-editor/core/transform-style.cjs.map +2 -2
- package/build/image-editor/core/types.cjs.map +1 -1
- package/build/image-editor/index.cjs +12 -2
- package/build/image-editor/index.cjs.map +2 -2
- package/build/image-editor/react/components/cropper-provider.cjs +2 -2
- package/build/image-editor/react/components/cropper-provider.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +121 -48
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +172 -0
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +7 -0
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +20 -10
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/build-cropper-setters.cjs +71 -0
- package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +7 -0
- package/build/image-editor/react/hooks/index.cjs +6 -3
- package/build/image-editor/react/hooks/index.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +3 -1
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs +113 -0
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/state/composite-reducer.cjs +115 -0
- package/build/state/composite-reducer.cjs.map +7 -0
- package/build/state/index.cjs +48 -0
- package/build/state/index.cjs.map +7 -0
- package/build/state/media-editor-state-provider.cjs +58 -0
- package/build/state/media-editor-state-provider.cjs.map +7 -0
- package/build/state/types.cjs +34 -0
- package/build/state/types.cjs.map +7 -0
- package/build/state/use-media-editor-state.cjs +298 -0
- package/build/state/use-media-editor-state.cjs.map +7 -0
- package/build-module/components/media-editor/index.mjs +40 -140
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +61 -0
- package/build-module/components/media-editor/use-crop-options.mjs.map +7 -0
- package/build-module/components/media-editor/use-save-media-editor.mjs +151 -0
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +7 -0
- package/build-module/components/media-editor-canvas/index.mjs +33 -12
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +68 -83
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-modal/index.mjs +40 -11
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-toolbar/index.mjs +7 -3
- package/build-module/components/media-editor-toolbar/index.mjs.map +2 -2
- package/build-module/components/media-form/index.mjs +9 -1
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/media-form/sidebar-datetime-view.mjs +32 -0
- package/build-module/components/media-form/sidebar-datetime-view.mjs.map +7 -0
- package/build-module/components/rotation-ruler/index.mjs +8 -2
- package/build-module/components/rotation-ruler/index.mjs.map +2 -2
- package/build-module/hooks/use-crop-gesture-handlers.mjs +41 -4
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
- package/build-module/image-editor/core/camera.mjs +34 -11
- package/build-module/image-editor/core/camera.mjs.map +2 -2
- package/build-module/image-editor/core/constants.mjs +4 -0
- package/build-module/image-editor/core/constants.mjs.map +2 -2
- package/build-module/image-editor/core/containment.mjs +75 -34
- package/build-module/image-editor/core/containment.mjs.map +2 -2
- package/build-module/image-editor/core/crop-rect.mjs +23 -0
- package/build-module/image-editor/core/crop-rect.mjs.map +7 -0
- package/build-module/image-editor/core/index.mjs +12 -0
- package/build-module/image-editor/core/index.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/core/math/sanitize.mjs +63 -0
- package/build-module/image-editor/core/math/sanitize.mjs.map +7 -0
- package/build-module/image-editor/core/setter-helpers.mjs +39 -0
- package/build-module/image-editor/core/setter-helpers.mjs.map +7 -0
- package/build-module/image-editor/core/source-region.mjs +16 -14
- package/build-module/image-editor/core/source-region.mjs.map +2 -2
- package/build-module/image-editor/core/state.mjs +25 -11
- package/build-module/image-editor/core/state.mjs.map +2 -2
- package/build-module/image-editor/core/stencil-math.mjs +30 -19
- package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
- package/build-module/image-editor/core/transform-style.mjs +12 -6
- package/build-module/image-editor/core/transform-style.mjs.map +2 -2
- package/build-module/image-editor/index.mjs +11 -2
- package/build-module/image-editor/index.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper-provider.mjs +3 -3
- package/build-module/image-editor/react/components/cropper-provider.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +122 -49
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +147 -0
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +7 -0
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +20 -10
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +46 -0
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/index.mjs +4 -2
- package/build-module/image-editor/react/hooks/index.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +3 -1
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +100 -0
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/state/composite-reducer.mjs +94 -0
- package/build-module/state/composite-reducer.mjs.map +7 -0
- package/build-module/state/index.mjs +26 -0
- package/build-module/state/index.mjs.map +7 -0
- package/build-module/state/media-editor-state-provider.mjs +34 -0
- package/build-module/state/media-editor-state-provider.mjs.map +7 -0
- package/build-module/state/types.mjs +9 -0
- package/build-module/state/types.mjs.map +7 -0
- package/build-module/state/use-media-editor-state.mjs +289 -0
- package/build-module/state/use-media-editor-state.mjs.map +7 -0
- package/build-style/style-rtl.css +46 -9
- package/build-style/style.css +46 -9
- package/build-types/components/media-editor/index.d.ts +4 -7
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +37 -0
- package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -0
- package/build-types/components/media-editor/use-save-media-editor.d.ts +29 -0
- package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -0
- package/build-types/components/media-editor-canvas/index.d.ts +5 -10
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +4 -16
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/components/media-form/sidebar-datetime-view.d.ts +10 -0
- package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +1 -0
- package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
- package/build-types/hooks/use-crop-gesture-handlers.d.ts +13 -14
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
- package/build-types/image-editor/core/camera.d.ts.map +1 -1
- package/build-types/image-editor/core/constants.d.ts +18 -0
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/containment.d.ts +10 -0
- package/build-types/image-editor/core/containment.d.ts.map +1 -1
- package/build-types/image-editor/core/crop-rect.d.ts +19 -0
- package/build-types/image-editor/core/crop-rect.d.ts.map +1 -0
- package/build-types/image-editor/core/index.d.ts +3 -1
- package/build-types/image-editor/core/index.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts +5 -1
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/core/math/sanitize.d.ts +70 -0
- package/build-types/image-editor/core/math/sanitize.d.ts.map +1 -0
- package/build-types/image-editor/core/setter-helpers.d.ts +24 -0
- package/build-types/image-editor/core/setter-helpers.d.ts.map +1 -0
- package/build-types/image-editor/core/source-region.d.ts.map +1 -1
- package/build-types/image-editor/core/state.d.ts +12 -0
- package/build-types/image-editor/core/state.d.ts.map +1 -1
- package/build-types/image-editor/core/stencil-math.d.ts +23 -18
- package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
- package/build-types/image-editor/core/transform-style.d.ts.map +1 -1
- package/build-types/image-editor/core/types.d.ts +23 -4
- package/build-types/image-editor/core/types.d.ts.map +1 -1
- package/build-types/image-editor/index.d.ts +4 -4
- package/build-types/image-editor/index.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts +11 -7
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +6 -4
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +36 -0
- package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +1 -0
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +2 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +47 -0
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/index.d.ts +4 -2
- package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts +58 -0
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts +39 -0
- package/build-types/state/composite-reducer.d.ts.map +1 -0
- package/build-types/state/index.d.ts +7 -0
- package/build-types/state/index.d.ts.map +1 -0
- package/build-types/state/media-editor-state-provider.d.ts +34 -0
- package/build-types/state/media-editor-state-provider.d.ts.map +1 -0
- package/build-types/state/types.d.ts +104 -0
- package/build-types/state/types.d.ts.map +1 -0
- package/build-types/state/use-media-editor-state.d.ts +104 -0
- package/build-types/state/use-media-editor-state.d.ts.map +1 -0
- package/package.json +19 -17
- package/src/components/media-editor/index.tsx +52 -188
- package/src/components/media-editor/test/use-crop-options.tsx +164 -0
- package/src/components/media-editor/use-crop-options.ts +115 -0
- package/src/components/media-editor/use-save-media-editor.ts +214 -0
- package/src/components/media-editor-canvas/index.tsx +56 -24
- package/src/components/media-editor-crop-panel/index.tsx +25 -61
- package/src/components/media-editor-crop-panel/test/index.tsx +17 -43
- package/src/components/media-editor-modal/index.tsx +50 -12
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +14 -0
- package/src/components/media-editor-modal/test/index.tsx +143 -0
- package/src/components/media-editor-toolbar/index.tsx +9 -5
- package/src/components/media-form/index.tsx +15 -1
- package/src/components/media-form/sidebar-datetime-view.tsx +47 -0
- package/src/components/media-form/test/sidebar-datetime-view.test.tsx +96 -0
- package/src/components/rotation-ruler/README.md +11 -25
- package/src/components/rotation-ruler/index.tsx +17 -2
- package/src/components/rotation-ruler/style.scss +49 -13
- package/src/hooks/use-crop-gesture-handlers.ts +66 -17
- package/src/image-editor/README.md +28 -129
- package/src/image-editor/core/camera.ts +51 -26
- package/src/image-editor/core/constants.ts +19 -0
- package/src/image-editor/core/containment.ts +102 -49
- package/src/image-editor/core/crop-rect.ts +45 -0
- package/src/image-editor/core/index.ts +16 -4
- package/src/image-editor/core/interaction-controller.ts +5 -1
- package/src/image-editor/core/math/sanitize.ts +158 -0
- package/src/image-editor/core/math/test/sanitize.ts +244 -0
- package/src/image-editor/core/setter-helpers.ts +66 -0
- package/src/image-editor/core/source-region.ts +21 -14
- package/src/image-editor/core/state.ts +67 -15
- package/src/image-editor/core/stencil-math.ts +55 -35
- package/src/image-editor/core/test/camera.ts +367 -1
- package/src/image-editor/core/test/state.ts +101 -8
- package/src/image-editor/core/test/stencil-math.ts +193 -0
- package/src/image-editor/core/transform-style.ts +21 -6
- package/src/image-editor/core/types.ts +24 -4
- package/src/image-editor/docs/architecture.md +48 -89
- package/src/image-editor/docs/recipes.md +123 -659
- package/src/image-editor/index.ts +12 -3
- package/src/image-editor/react/components/cropper-provider.tsx +16 -12
- package/src/image-editor/react/components/cropper.scss +23 -4
- package/src/image-editor/react/components/cropper.tsx +197 -90
- package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +269 -0
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +22 -9
- package/src/image-editor/react/components/test/cropper.tsx +55 -13
- package/src/image-editor/react/hooks/build-cropper-setters.ts +105 -0
- package/src/image-editor/react/hooks/index.ts +4 -2
- package/src/image-editor/react/hooks/test/{use-cropper-state.ts → use-cropper-reducer.ts} +142 -286
- package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
- package/src/image-editor/react/hooks/use-cropper-reducer.ts +204 -0
- package/src/image-editor/react/hooks/use-interaction.ts +3 -2
- package/src/image-editor/stories/rectangle-crop.story.tsx +7 -17
- package/src/state/composite-reducer.ts +152 -0
- package/src/state/index.ts +20 -0
- package/src/state/media-editor-state-provider.tsx +70 -0
- package/src/state/test/use-media-editor-state.ts +402 -0
- package/src/state/types.ts +107 -0
- package/src/state/use-media-editor-state.ts +488 -0
- package/tsconfig.json +2 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/build/image-editor/react/hooks/use-cropper-state.cjs +0 -277
- package/build/image-editor/react/hooks/use-cropper-state.cjs.map +0 -7
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs +0 -262
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +0 -7
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -97
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
- package/src/image-editor/docs/roadmap.md +0 -66
- package/src/image-editor/react/hooks/use-cropper-state.ts +0 -457
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { NormalizedRect, Size } from './types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Largest rect of the given pixel aspect ratio that fits inside the
|
|
8
|
+
* visual bounds, centered in [0,1] × [0,1] normalized space. Returns
|
|
9
|
+
* a full-frame rect (1×1) if `aspectRatio` is unset or non-positive.
|
|
10
|
+
*
|
|
11
|
+
* The returned rect is the same in either freeform or fixed-crop
|
|
12
|
+
* mode — it's the canonical "centered crop for this ratio at this
|
|
13
|
+
* canvas size".
|
|
14
|
+
*
|
|
15
|
+
* @param aspectRatio Desired pixel width/height ratio. `undefined`
|
|
16
|
+
* (or 0/negative) means no lock — returns full frame.
|
|
17
|
+
* @param visualSize Rendered image dimensions in pixels.
|
|
18
|
+
*/
|
|
19
|
+
export function computeInscribedRect(
|
|
20
|
+
aspectRatio: number | undefined,
|
|
21
|
+
visualSize: Size
|
|
22
|
+
): NormalizedRect {
|
|
23
|
+
let w = 1;
|
|
24
|
+
let h = 1;
|
|
25
|
+
if ( aspectRatio && aspectRatio > 0 && visualSize.width > 0 ) {
|
|
26
|
+
// normalizedRatio = w/h in normalized space that produces the
|
|
27
|
+
// desired pixel aspect ratio.
|
|
28
|
+
// pixelW = w * visualW, pixelH = h * visualH
|
|
29
|
+
// pixelW / pixelH = aspectRatio
|
|
30
|
+
// => w / h = aspectRatio * visualH / visualW
|
|
31
|
+
const normalizedRatio =
|
|
32
|
+
( aspectRatio * visualSize.height ) / visualSize.width;
|
|
33
|
+
if ( normalizedRatio <= 1 ) {
|
|
34
|
+
w = normalizedRatio;
|
|
35
|
+
} else {
|
|
36
|
+
h = 1 / normalizedRatio;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
x: ( 1 - w ) / 2,
|
|
41
|
+
y: ( 1 - h ) / 2,
|
|
42
|
+
width: w,
|
|
43
|
+
height: h,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
// Types
|
|
2
2
|
export type {
|
|
3
3
|
CropperState,
|
|
4
|
+
CropperAction,
|
|
4
5
|
TransformOperation,
|
|
5
6
|
NormalizedPoint,
|
|
6
7
|
NormalizedRect,
|
|
7
8
|
Size,
|
|
8
9
|
Flip,
|
|
9
10
|
StencilProps,
|
|
11
|
+
HandlePosition,
|
|
10
12
|
} from './types';
|
|
11
|
-
// Note: `CropperAction` is intentionally not exported. The reducer's
|
|
12
|
-
// action shape is an internal detail; consumers drive state through
|
|
13
|
-
// the named setters on `UseCropperStateReturn` (setPan, setZoom, etc.).
|
|
14
|
-
// This keeps the public API stable as the reducer evolves.
|
|
15
13
|
|
|
16
14
|
// Constants
|
|
17
15
|
export {
|
|
@@ -21,10 +19,24 @@ export {
|
|
|
21
19
|
} from './constants';
|
|
22
20
|
export type { AspectRatioPreset } from './constants';
|
|
23
21
|
|
|
22
|
+
// Reducer + state helpers. `CropperAction` and `cropperReducer` are
|
|
23
|
+
// exported so composite stores can delegate cropper actions through the
|
|
24
|
+
// pure reducer (see `useMediaEditorState`). Most consumers should
|
|
25
|
+
// instead drive state through the named setters on `CropperController`.
|
|
26
|
+
export {
|
|
27
|
+
cropperReducer,
|
|
28
|
+
enforceContainment,
|
|
29
|
+
areCropperStatesEqual,
|
|
30
|
+
isStateDirty,
|
|
31
|
+
} from './state';
|
|
32
|
+
|
|
24
33
|
// Source region (pixel and percentage)
|
|
25
34
|
export { getSourceRegion, getSourceRegionPercent } from './source-region';
|
|
26
35
|
export type { SourceRegion, SourceRegionPercent } from './source-region';
|
|
27
36
|
|
|
37
|
+
// Crop-rect helpers
|
|
38
|
+
export { computeInscribedRect } from './crop-rect';
|
|
39
|
+
|
|
28
40
|
// Pipeline
|
|
29
41
|
export {
|
|
30
42
|
applyOperationToState,
|
|
@@ -62,7 +62,11 @@ export interface InteractionStatus {
|
|
|
62
62
|
export interface CropperInteractionActions {
|
|
63
63
|
/** Set the image pan offset in normalized coordinates. */
|
|
64
64
|
setPan: ( pan: NormalizedPoint ) => void;
|
|
65
|
-
/**
|
|
65
|
+
/**
|
|
66
|
+
* Set the zoom level. Cursorless surfaces (slider, keyboard `+`/`-`)
|
|
67
|
+
* rely on this anchoring at the crop center; pointer-driven zoom
|
|
68
|
+
* paths use `setZoomAtPoint` with an explicit focal point instead.
|
|
69
|
+
*/
|
|
66
70
|
setZoom: ( zoom: number ) => void;
|
|
67
71
|
/** Set zoom and pan together for focal-point zoom. */
|
|
68
72
|
setZoomAtPoint: ( zoom: number, pan: NormalizedPoint ) => void;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { CropperState, NormalizedRect, Size } from '../types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Magnitude beyond which any cropper-math input is treated as corrupt.
|
|
8
|
+
* Picks a value well above any realistic image/rotation/zoom/pan but far
|
|
9
|
+
* below `Number.MAX_VALUE`, so multiplications inside trig and matrix code
|
|
10
|
+
* can't overflow to `Infinity`.
|
|
11
|
+
*/
|
|
12
|
+
const MAX_SAFE_MAGNITUDE = 1e6;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Returns `value` if it's finite and within `[-MAX_SAFE_MAGNITUDE,
|
|
16
|
+
* MAX_SAFE_MAGNITUDE]`, otherwise `fallback`. Stricter than `Number.isFinite`
|
|
17
|
+
* because extreme finite magnitudes (e.g. `Number.MAX_VALUE`) overflow
|
|
18
|
+
* downstream trig/matrix math even though the spec reports them as finite.
|
|
19
|
+
*
|
|
20
|
+
* Note: sub-normal denormals (e.g. `Number.MIN_VALUE`) are accepted by this
|
|
21
|
+
* function — they don't overflow on their own. Protection against divisions
|
|
22
|
+
* by sub-normal values lives in `sanitizeCropperState`'s zoom guard.
|
|
23
|
+
*
|
|
24
|
+
* @param value The candidate value.
|
|
25
|
+
* @param fallback The replacement to return when `value` is unsafe.
|
|
26
|
+
* @return Either `value` or `fallback`.
|
|
27
|
+
*/
|
|
28
|
+
export function safeBoundedNumber( value: number, fallback: number ): number {
|
|
29
|
+
if ( ! Number.isFinite( value ) ) {
|
|
30
|
+
return fallback;
|
|
31
|
+
}
|
|
32
|
+
if ( Math.abs( value ) > MAX_SAFE_MAGNITUDE ) {
|
|
33
|
+
return fallback;
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Returns `true` when `value` is finite and within `[-MAX_SAFE_MAGNITUDE,
|
|
40
|
+
* MAX_SAFE_MAGNITUDE]`. The predicate companion to {@link safeBoundedNumber}
|
|
41
|
+
* for callers that need to short-circuit rather than substitute a fallback.
|
|
42
|
+
*
|
|
43
|
+
* @param value The candidate value.
|
|
44
|
+
* @return Whether `value` is safe to use in math.
|
|
45
|
+
*/
|
|
46
|
+
export function isSafeNumber( value: number ): boolean {
|
|
47
|
+
return Number.isFinite( value ) && Math.abs( value ) <= MAX_SAFE_MAGNITUDE;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Returns `true` when both dimensions are finite and within the safe
|
|
52
|
+
* range `[Number.EPSILON, MAX_SAFE_MAGNITUDE]`. The lower bound matters
|
|
53
|
+
* because sub-normal dimensions (e.g. `Number.MIN_VALUE`) pass `> 0` but
|
|
54
|
+
* make `container / dim` overflow to `Infinity` downstream in
|
|
55
|
+
* `getImageFit` / `createCamera`. Use this to short-circuit math that
|
|
56
|
+
* would otherwise produce non-finite output.
|
|
57
|
+
*
|
|
58
|
+
* @param size The size to check.
|
|
59
|
+
* @return Whether `size` is safe to use in math.
|
|
60
|
+
*/
|
|
61
|
+
export function isValidSize( size: Size ): boolean {
|
|
62
|
+
return (
|
|
63
|
+
Number.isFinite( size.width ) &&
|
|
64
|
+
Number.isFinite( size.height ) &&
|
|
65
|
+
size.width >= Number.EPSILON &&
|
|
66
|
+
size.height >= Number.EPSILON &&
|
|
67
|
+
size.width <= MAX_SAFE_MAGNITUDE &&
|
|
68
|
+
size.height <= MAX_SAFE_MAGNITUDE
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Returns a copy of `rect` with every numeric field replaced by a safe
|
|
74
|
+
* value via {@link safeBoundedNumber}. Returns the same reference when no
|
|
75
|
+
* field needed substitution so callers can keep their reference-equality
|
|
76
|
+
* short-circuits intact for clean inputs.
|
|
77
|
+
*
|
|
78
|
+
* @param rect The rect to sanitize.
|
|
79
|
+
* @return A rect with all numeric fields finite.
|
|
80
|
+
*/
|
|
81
|
+
export function sanitizeRect( rect: NormalizedRect ): NormalizedRect {
|
|
82
|
+
const x = safeBoundedNumber( rect.x, 0 );
|
|
83
|
+
const y = safeBoundedNumber( rect.y, 0 );
|
|
84
|
+
const width = safeBoundedNumber( rect.width, 0 );
|
|
85
|
+
const height = safeBoundedNumber( rect.height, 0 );
|
|
86
|
+
if (
|
|
87
|
+
x === rect.x &&
|
|
88
|
+
y === rect.y &&
|
|
89
|
+
width === rect.width &&
|
|
90
|
+
height === rect.height
|
|
91
|
+
) {
|
|
92
|
+
return rect;
|
|
93
|
+
}
|
|
94
|
+
return { x, y, width, height };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Returns a copy of `state` with the cropper-math numeric fields replaced
|
|
99
|
+
* by safe defaults. Covers `pan`, `zoom`, `rotation`, the matching base
|
|
100
|
+
* pose, and `cropRect`. The `state.image` numeric fields (naturalWidth /
|
|
101
|
+
* naturalHeight) are intentionally NOT sanitized — image dimensions reach
|
|
102
|
+
* the math layer as a separate `imageSize` argument, and cloning the
|
|
103
|
+
* image object here would break the reducer's reference-equality
|
|
104
|
+
* short-circuit.
|
|
105
|
+
*
|
|
106
|
+
* Returns the same `state` reference when no field needed substitution
|
|
107
|
+
* so callers in hot paths (every pointermove via `restrictPanZoom`) can
|
|
108
|
+
* keep their reference-equality optimizations. Sub-object references
|
|
109
|
+
* (`pan`, `basePan`, `cropRect`) are also preserved when their fields
|
|
110
|
+
* are individually clean.
|
|
111
|
+
*
|
|
112
|
+
* @param state The cropper state to sanitize.
|
|
113
|
+
* @return A cropper state with the math-relevant numeric fields finite.
|
|
114
|
+
*/
|
|
115
|
+
export function sanitizeCropperState( state: CropperState ): CropperState {
|
|
116
|
+
const panX = safeBoundedNumber( state.pan.x, 0 );
|
|
117
|
+
const panY = safeBoundedNumber( state.pan.y, 0 );
|
|
118
|
+
const basePanX = safeBoundedNumber( state.basePan.x, 0 );
|
|
119
|
+
const basePanY = safeBoundedNumber( state.basePan.y, 0 );
|
|
120
|
+
const rotation = safeBoundedNumber( state.rotation, 0 );
|
|
121
|
+
const baseRotation = safeBoundedNumber( state.baseRotation, 0 );
|
|
122
|
+
const rawZoom = safeBoundedNumber( state.zoom, 1 );
|
|
123
|
+
// Zoom must be strictly positive AND large enough that 1/zoom doesn't
|
|
124
|
+
// overflow. Sub-normals (e.g. Number.MIN_VALUE) pass `> 0` but make
|
|
125
|
+
// division explode, so guard against them with Number.EPSILON.
|
|
126
|
+
const zoom = rawZoom >= Number.EPSILON ? rawZoom : 1;
|
|
127
|
+
const rawBaseZoom = safeBoundedNumber( state.baseZoom, 1 );
|
|
128
|
+
const baseZoom = rawBaseZoom >= Number.EPSILON ? rawBaseZoom : 1;
|
|
129
|
+
const cropRect = sanitizeRect( state.cropRect );
|
|
130
|
+
|
|
131
|
+
const panUnchanged = panX === state.pan.x && panY === state.pan.y;
|
|
132
|
+
const basePanUnchanged =
|
|
133
|
+
basePanX === state.basePan.x && basePanY === state.basePan.y;
|
|
134
|
+
|
|
135
|
+
if (
|
|
136
|
+
panUnchanged &&
|
|
137
|
+
basePanUnchanged &&
|
|
138
|
+
zoom === state.zoom &&
|
|
139
|
+
baseZoom === state.baseZoom &&
|
|
140
|
+
rotation === state.rotation &&
|
|
141
|
+
baseRotation === state.baseRotation &&
|
|
142
|
+
cropRect === state.cropRect
|
|
143
|
+
) {
|
|
144
|
+
return state;
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
...state,
|
|
148
|
+
pan: panUnchanged ? state.pan : { x: panX, y: panY },
|
|
149
|
+
zoom,
|
|
150
|
+
rotation,
|
|
151
|
+
basePan: basePanUnchanged
|
|
152
|
+
? state.basePan
|
|
153
|
+
: { x: basePanX, y: basePanY },
|
|
154
|
+
baseZoom,
|
|
155
|
+
baseRotation,
|
|
156
|
+
cropRect,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
isValidSize,
|
|
6
|
+
safeBoundedNumber,
|
|
7
|
+
sanitizeCropperState,
|
|
8
|
+
sanitizeRect,
|
|
9
|
+
} from '../sanitize';
|
|
10
|
+
import { DEFAULT_STATE } from '../../constants';
|
|
11
|
+
import type { CropperState } from '../../types';
|
|
12
|
+
|
|
13
|
+
describe( 'safeBoundedNumber', () => {
|
|
14
|
+
it( 'returns the value when it is finite and in range', () => {
|
|
15
|
+
expect( safeBoundedNumber( 42, 0 ) ).toBe( 42 );
|
|
16
|
+
expect( safeBoundedNumber( -3.14, 0 ) ).toBe( -3.14 );
|
|
17
|
+
expect( safeBoundedNumber( 0, 1 ) ).toBe( 0 );
|
|
18
|
+
} );
|
|
19
|
+
|
|
20
|
+
it( 'returns the fallback for NaN', () => {
|
|
21
|
+
expect( safeBoundedNumber( Number.NaN, 7 ) ).toBe( 7 );
|
|
22
|
+
} );
|
|
23
|
+
|
|
24
|
+
it( 'returns the fallback for ±Infinity', () => {
|
|
25
|
+
expect( safeBoundedNumber( Number.POSITIVE_INFINITY, 7 ) ).toBe( 7 );
|
|
26
|
+
expect( safeBoundedNumber( Number.NEGATIVE_INFINITY, 7 ) ).toBe( 7 );
|
|
27
|
+
} );
|
|
28
|
+
|
|
29
|
+
it( 'returns the fallback for finite values beyond the safe magnitude', () => {
|
|
30
|
+
// MAX_VALUE is technically finite but multiplying it through trig
|
|
31
|
+
// and matrix code overflows to Infinity.
|
|
32
|
+
expect( safeBoundedNumber( Number.MAX_VALUE, 1 ) ).toBe( 1 );
|
|
33
|
+
expect( safeBoundedNumber( -Number.MAX_VALUE, 1 ) ).toBe( 1 );
|
|
34
|
+
expect( safeBoundedNumber( 1e10, 1 ) ).toBe( 1 );
|
|
35
|
+
} );
|
|
36
|
+
|
|
37
|
+
it( 'accepts values right at the safe magnitude boundary', () => {
|
|
38
|
+
expect( safeBoundedNumber( 1e6, 0 ) ).toBe( 1e6 );
|
|
39
|
+
expect( safeBoundedNumber( -1e6, 0 ) ).toBe( -1e6 );
|
|
40
|
+
} );
|
|
41
|
+
|
|
42
|
+
it( 'accepts sub-normal values (sub-normal protection lives elsewhere)', () => {
|
|
43
|
+
// Sub-normals don't overflow on their own; they only cause trouble
|
|
44
|
+
// when used as divisors. That guard lives in sanitizeCropperState's
|
|
45
|
+
// zoom check, not here.
|
|
46
|
+
expect( safeBoundedNumber( Number.MIN_VALUE, 0 ) ).toBe(
|
|
47
|
+
Number.MIN_VALUE
|
|
48
|
+
);
|
|
49
|
+
} );
|
|
50
|
+
} );
|
|
51
|
+
|
|
52
|
+
describe( 'isValidSize', () => {
|
|
53
|
+
it( 'returns true for finite, positive, in-range sizes', () => {
|
|
54
|
+
expect( isValidSize( { width: 100, height: 100 } ) ).toBe( true );
|
|
55
|
+
expect( isValidSize( { width: 1, height: 1 } ) ).toBe( true );
|
|
56
|
+
expect( isValidSize( { width: 1e6, height: 1e6 } ) ).toBe( true );
|
|
57
|
+
} );
|
|
58
|
+
|
|
59
|
+
it( 'returns false for zero or negative dimensions', () => {
|
|
60
|
+
expect( isValidSize( { width: 0, height: 100 } ) ).toBe( false );
|
|
61
|
+
expect( isValidSize( { width: 100, height: 0 } ) ).toBe( false );
|
|
62
|
+
expect( isValidSize( { width: -1, height: 100 } ) ).toBe( false );
|
|
63
|
+
} );
|
|
64
|
+
|
|
65
|
+
it( 'returns false for non-finite dimensions', () => {
|
|
66
|
+
expect( isValidSize( { width: Number.NaN, height: 100 } ) ).toBe(
|
|
67
|
+
false
|
|
68
|
+
);
|
|
69
|
+
expect(
|
|
70
|
+
isValidSize( {
|
|
71
|
+
width: Number.POSITIVE_INFINITY,
|
|
72
|
+
height: 100,
|
|
73
|
+
} )
|
|
74
|
+
).toBe( false );
|
|
75
|
+
} );
|
|
76
|
+
|
|
77
|
+
it( 'returns false for dimensions beyond the safe magnitude', () => {
|
|
78
|
+
expect( isValidSize( { width: Number.MAX_VALUE, height: 100 } ) ).toBe(
|
|
79
|
+
false
|
|
80
|
+
);
|
|
81
|
+
expect( isValidSize( { width: 1e10, height: 100 } ) ).toBe( false );
|
|
82
|
+
} );
|
|
83
|
+
|
|
84
|
+
it( 'returns false for sub-normal dimensions (overflow when divided into)', () => {
|
|
85
|
+
// Number.MIN_VALUE passes `> 0` but `container / MIN_VALUE`
|
|
86
|
+
// overflows to Infinity downstream in getImageFit / createCamera.
|
|
87
|
+
expect( isValidSize( { width: Number.MIN_VALUE, height: 100 } ) ).toBe(
|
|
88
|
+
false
|
|
89
|
+
);
|
|
90
|
+
expect( isValidSize( { width: 100, height: Number.MIN_VALUE } ) ).toBe(
|
|
91
|
+
false
|
|
92
|
+
);
|
|
93
|
+
} );
|
|
94
|
+
} );
|
|
95
|
+
|
|
96
|
+
describe( 'sanitizeRect', () => {
|
|
97
|
+
it( 'replaces non-finite fields with 0', () => {
|
|
98
|
+
expect(
|
|
99
|
+
sanitizeRect( {
|
|
100
|
+
x: Number.NaN,
|
|
101
|
+
y: 0.1,
|
|
102
|
+
width: Number.POSITIVE_INFINITY,
|
|
103
|
+
height: 0.5,
|
|
104
|
+
} )
|
|
105
|
+
).toEqual( { x: 0, y: 0.1, width: 0, height: 0.5 } );
|
|
106
|
+
} );
|
|
107
|
+
|
|
108
|
+
it( 'leaves a clean rect unchanged', () => {
|
|
109
|
+
expect(
|
|
110
|
+
sanitizeRect( { x: 0.1, y: 0.2, width: 0.5, height: 0.3 } )
|
|
111
|
+
).toEqual( { x: 0.1, y: 0.2, width: 0.5, height: 0.3 } );
|
|
112
|
+
} );
|
|
113
|
+
} );
|
|
114
|
+
|
|
115
|
+
describe( 'sanitizeCropperState', () => {
|
|
116
|
+
function makeBaseState(
|
|
117
|
+
overrides: Partial< CropperState > = {}
|
|
118
|
+
): CropperState {
|
|
119
|
+
return {
|
|
120
|
+
...DEFAULT_STATE,
|
|
121
|
+
...overrides,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
it( 'leaves a clean state unchanged', () => {
|
|
126
|
+
const state = makeBaseState( {
|
|
127
|
+
pan: { x: 0.1, y: -0.05 },
|
|
128
|
+
zoom: 1.5,
|
|
129
|
+
rotation: 30,
|
|
130
|
+
} );
|
|
131
|
+
const out = sanitizeCropperState( state );
|
|
132
|
+
expect( out.pan ).toEqual( { x: 0.1, y: -0.05 } );
|
|
133
|
+
expect( out.zoom ).toBe( 1.5 );
|
|
134
|
+
expect( out.rotation ).toBe( 30 );
|
|
135
|
+
} );
|
|
136
|
+
|
|
137
|
+
it( 'replaces NaN pan with zero', () => {
|
|
138
|
+
const state = makeBaseState( {
|
|
139
|
+
pan: { x: Number.NaN, y: Number.NaN },
|
|
140
|
+
} );
|
|
141
|
+
const out = sanitizeCropperState( state );
|
|
142
|
+
expect( out.pan ).toEqual( { x: 0, y: 0 } );
|
|
143
|
+
} );
|
|
144
|
+
|
|
145
|
+
it( 'replaces non-finite zoom with 1 (identity)', () => {
|
|
146
|
+
expect(
|
|
147
|
+
sanitizeCropperState( makeBaseState( { zoom: Number.NaN } ) ).zoom
|
|
148
|
+
).toBe( 1 );
|
|
149
|
+
expect(
|
|
150
|
+
sanitizeCropperState(
|
|
151
|
+
makeBaseState( { zoom: Number.POSITIVE_INFINITY } )
|
|
152
|
+
).zoom
|
|
153
|
+
).toBe( 1 );
|
|
154
|
+
expect(
|
|
155
|
+
sanitizeCropperState( makeBaseState( { zoom: -2 } ) ).zoom
|
|
156
|
+
).toBe( 1 );
|
|
157
|
+
} );
|
|
158
|
+
|
|
159
|
+
it( 'replaces sub-normal zoom with 1 (prevents division explosion)', () => {
|
|
160
|
+
expect(
|
|
161
|
+
sanitizeCropperState( makeBaseState( { zoom: Number.MIN_VALUE } ) )
|
|
162
|
+
.zoom
|
|
163
|
+
).toBe( 1 );
|
|
164
|
+
} );
|
|
165
|
+
|
|
166
|
+
it( 'replaces extreme rotation magnitudes with 0', () => {
|
|
167
|
+
expect(
|
|
168
|
+
sanitizeCropperState(
|
|
169
|
+
makeBaseState( { rotation: Number.MAX_VALUE } )
|
|
170
|
+
).rotation
|
|
171
|
+
).toBe( 0 );
|
|
172
|
+
expect(
|
|
173
|
+
sanitizeCropperState( makeBaseState( { rotation: Number.NaN } ) )
|
|
174
|
+
.rotation
|
|
175
|
+
).toBe( 0 );
|
|
176
|
+
} );
|
|
177
|
+
|
|
178
|
+
it( 'sanitizes cropRect fields independently', () => {
|
|
179
|
+
const state = makeBaseState( {
|
|
180
|
+
cropRect: {
|
|
181
|
+
x: Number.NaN,
|
|
182
|
+
y: 0.1,
|
|
183
|
+
width: Number.POSITIVE_INFINITY,
|
|
184
|
+
height: 0.5,
|
|
185
|
+
},
|
|
186
|
+
} );
|
|
187
|
+
const out = sanitizeCropperState( state );
|
|
188
|
+
expect( out.cropRect ).toEqual( {
|
|
189
|
+
x: 0,
|
|
190
|
+
y: 0.1,
|
|
191
|
+
width: 0,
|
|
192
|
+
height: 0.5,
|
|
193
|
+
} );
|
|
194
|
+
} );
|
|
195
|
+
|
|
196
|
+
it( 'sanitizes base pose fields (basePan, baseZoom, baseRotation)', () => {
|
|
197
|
+
const state = makeBaseState( {
|
|
198
|
+
basePan: { x: Number.NaN, y: 0 },
|
|
199
|
+
baseZoom: Number.NEGATIVE_INFINITY,
|
|
200
|
+
baseRotation: Number.MAX_VALUE,
|
|
201
|
+
} );
|
|
202
|
+
const out = sanitizeCropperState( state );
|
|
203
|
+
expect( out.basePan ).toEqual( { x: 0, y: 0 } );
|
|
204
|
+
expect( out.baseZoom ).toBe( 1 );
|
|
205
|
+
expect( out.baseRotation ).toBe( 0 );
|
|
206
|
+
} );
|
|
207
|
+
|
|
208
|
+
it( 'returns the same reference when no field needs substitution', () => {
|
|
209
|
+
// Called once per pointermove (via restrictPanZoom + createCamera),
|
|
210
|
+
// so the no-change fast path matters. Clean state → same reference,
|
|
211
|
+
// no per-frame GC churn.
|
|
212
|
+
const state = makeBaseState( {
|
|
213
|
+
pan: { x: 0.1, y: -0.05 },
|
|
214
|
+
zoom: 1.5,
|
|
215
|
+
rotation: 30,
|
|
216
|
+
} );
|
|
217
|
+
expect( sanitizeCropperState( state ) ).toBe( state );
|
|
218
|
+
} );
|
|
219
|
+
|
|
220
|
+
it( 'preserves sub-object references when only some fields change', () => {
|
|
221
|
+
// If pan is clean but zoom is corrupted, pan should keep its
|
|
222
|
+
// reference even though the parent state must be a new object.
|
|
223
|
+
const pan = { x: 0.1, y: -0.05 };
|
|
224
|
+
const state = makeBaseState( { pan, zoom: Number.NaN } );
|
|
225
|
+
const out = sanitizeCropperState( state );
|
|
226
|
+
expect( out ).not.toBe( state );
|
|
227
|
+
expect( out.pan ).toBe( pan );
|
|
228
|
+
expect( out.zoom ).toBe( 1 );
|
|
229
|
+
} );
|
|
230
|
+
|
|
231
|
+
it( 'leaves state.image untouched (caller passes imageSize separately)', () => {
|
|
232
|
+
// The math layer never reads state.image — image dimensions arrive as
|
|
233
|
+
// a separate `imageSize` argument. Cloning state.image on every call
|
|
234
|
+
// would break the reducer's reference-equality short-circuit.
|
|
235
|
+
const image = {
|
|
236
|
+
src: 'test.jpg',
|
|
237
|
+
naturalWidth: 1600,
|
|
238
|
+
naturalHeight: 900,
|
|
239
|
+
};
|
|
240
|
+
const state = makeBaseState( { image } );
|
|
241
|
+
const out = sanitizeCropperState( state );
|
|
242
|
+
expect( out.image ).toBe( image );
|
|
243
|
+
} );
|
|
244
|
+
} );
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { CropperState, NormalizedPoint, CropperAction } from './types';
|
|
5
|
+
import { MAX_ZOOM } from './constants';
|
|
6
|
+
import { getMinZoom, restrictPanZoom } from './containment';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Build the reducer action that produces a "cursorless" zoom — zoom
|
|
10
|
+
* anchored at the current crop-rect center, with pan clamped to keep
|
|
11
|
+
* the image covering the crop.
|
|
12
|
+
*
|
|
13
|
+
* The slider, the `+`/`-` keys, and the toolbar buttons have no
|
|
14
|
+
* natural focal point (unlike wheel/pinch zoom), so they default to
|
|
15
|
+
* the crop center. Returns `null` when the clamped zoom matches the
|
|
16
|
+
* current zoom (no-op).
|
|
17
|
+
*
|
|
18
|
+
* Extracted so multiple hooks (the pure `useCropperReducer` and the
|
|
19
|
+
* composite `useMediaEditorState`) can share the math without
|
|
20
|
+
* duplicating it.
|
|
21
|
+
*
|
|
22
|
+
* @param state The current cropper state.
|
|
23
|
+
* @param requestedZoom The zoom value the caller asked for.
|
|
24
|
+
* @return A `SET_ZOOM_AT_POINT` action, or `null` if no change is required.
|
|
25
|
+
*/
|
|
26
|
+
export function buildFocalPointZoomAction(
|
|
27
|
+
state: CropperState,
|
|
28
|
+
requestedZoom: number
|
|
29
|
+
): CropperAction | null {
|
|
30
|
+
const clampedZoom = Math.min(
|
|
31
|
+
MAX_ZOOM,
|
|
32
|
+
Math.max( getMinZoom( state ), requestedZoom )
|
|
33
|
+
);
|
|
34
|
+
if ( clampedZoom === state.zoom ) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
// Crop center expressed in the same coord system as `state.pan`:
|
|
38
|
+
// container-center-relative, normalized by image rendered size.
|
|
39
|
+
// `cropRect` is image-top-left-origin (0–1), so subtract 0.5
|
|
40
|
+
// to recenter.
|
|
41
|
+
const { cropRect } = state;
|
|
42
|
+
const focalNormX = cropRect.x + cropRect.width / 2 - 0.5;
|
|
43
|
+
const focalNormY = cropRect.y + cropRect.height / 2 - 0.5;
|
|
44
|
+
const zoomRatio = 1 - clampedZoom / state.zoom;
|
|
45
|
+
const newPanX = state.pan.x + ( focalNormX - state.pan.x ) * zoomRatio;
|
|
46
|
+
const newPanY = state.pan.y + ( focalNormY - state.pan.y ) * zoomRatio;
|
|
47
|
+
const imageSize: { width: number; height: number } = state.image
|
|
48
|
+
? {
|
|
49
|
+
width: state.image.naturalWidth,
|
|
50
|
+
height: state.image.naturalHeight,
|
|
51
|
+
}
|
|
52
|
+
: { width: 1, height: 1 };
|
|
53
|
+
const { pan: clampedPan }: { pan: NormalizedPoint } = restrictPanZoom(
|
|
54
|
+
{
|
|
55
|
+
...state,
|
|
56
|
+
zoom: clampedZoom,
|
|
57
|
+
pan: { x: newPanX, y: newPanY },
|
|
58
|
+
},
|
|
59
|
+
imageSize,
|
|
60
|
+
state.cropRect
|
|
61
|
+
);
|
|
62
|
+
return {
|
|
63
|
+
type: 'SET_ZOOM_AT_POINT',
|
|
64
|
+
payload: { zoom: clampedZoom, pan: clampedPan },
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -8,6 +8,7 @@ import { mat2d, vec2 } from 'gl-matrix';
|
|
|
8
8
|
*/
|
|
9
9
|
import type { CropperState, Size } from './types';
|
|
10
10
|
import { createCamera, getRotatedBBox, getVisibleBounds } from './camera';
|
|
11
|
+
import { isValidSize, sanitizeCropperState } from './math/sanitize';
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* The selected image region in source-pixel coordinates.
|
|
@@ -51,22 +52,28 @@ export function getSourceRegion(
|
|
|
51
52
|
state: CropperState,
|
|
52
53
|
imageSize: Size
|
|
53
54
|
): SourceRegion {
|
|
54
|
-
|
|
55
|
+
// Sanitize before the zero-size short-circuit so the early return
|
|
56
|
+
// doesn't leak raw non-finite state.rotation or state.zoom out
|
|
57
|
+
// through the metadata fields. (state.flip is boolean and isn't
|
|
58
|
+
// at risk of NaN propagation.)
|
|
59
|
+
const safeState = sanitizeCropperState( state );
|
|
60
|
+
|
|
61
|
+
if ( ! isValidSize( imageSize ) ) {
|
|
55
62
|
return {
|
|
56
63
|
x: 0,
|
|
57
64
|
y: 0,
|
|
58
65
|
width: 0,
|
|
59
66
|
height: 0,
|
|
60
|
-
rotation:
|
|
61
|
-
flip: { ...
|
|
62
|
-
zoom:
|
|
67
|
+
rotation: safeState.rotation,
|
|
68
|
+
flip: { ...safeState.flip },
|
|
69
|
+
zoom: safeState.zoom,
|
|
63
70
|
};
|
|
64
71
|
}
|
|
65
72
|
|
|
66
73
|
// Use a synthetic 1:1 container so the camera maps normalized coords
|
|
67
74
|
// to a known pixel space. The container size cancels out.
|
|
68
75
|
const syntheticContainer: Size = { width: 1000, height: 1000 };
|
|
69
|
-
const camera = createCamera(
|
|
76
|
+
const camera = createCamera( safeState, syntheticContainer, imageSize );
|
|
70
77
|
|
|
71
78
|
// Inverse camera maps screen pixels back to normalized [0,1] world coords.
|
|
72
79
|
const inv = mat2d.create();
|
|
@@ -76,13 +83,13 @@ export function getSourceRegion(
|
|
|
76
83
|
// (zoom=1, no pan) to locate the visual bounds, then place the
|
|
77
84
|
// crop rect within them.
|
|
78
85
|
const baseCamera = createCamera(
|
|
79
|
-
{ ...
|
|
86
|
+
{ ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },
|
|
80
87
|
syntheticContainer,
|
|
81
88
|
imageSize
|
|
82
89
|
);
|
|
83
90
|
const visibleBounds = getVisibleBounds( baseCamera );
|
|
84
91
|
|
|
85
|
-
const cropRect =
|
|
92
|
+
const cropRect = safeState.cropRect;
|
|
86
93
|
const cropCenterScreenX =
|
|
87
94
|
visibleBounds.left +
|
|
88
95
|
( cropRect.x + cropRect.width / 2 ) * visibleBounds.width;
|
|
@@ -100,23 +107,23 @@ export function getSourceRegion(
|
|
|
100
107
|
|
|
101
108
|
// Crop rect size in the snap-rotation visual space, divided by zoom
|
|
102
109
|
// for source-pixel dimensions. Matches the stencil's reference frame.
|
|
103
|
-
const snapRotation = Math.round(
|
|
110
|
+
const snapRotation = Math.round( safeState.rotation / 90 ) * 90;
|
|
104
111
|
const { width: rotW, height: rotH } = getRotatedBBox(
|
|
105
112
|
imageSize.width,
|
|
106
113
|
imageSize.height,
|
|
107
114
|
snapRotation
|
|
108
115
|
);
|
|
109
|
-
const sourceW = ( cropRect.width * rotW ) /
|
|
110
|
-
const sourceH = ( cropRect.height * rotH ) /
|
|
116
|
+
const sourceW = ( cropRect.width * rotW ) / safeState.zoom;
|
|
117
|
+
const sourceH = ( cropRect.height * rotH ) / safeState.zoom;
|
|
111
118
|
|
|
112
119
|
return {
|
|
113
120
|
x: srcCenter[ 0 ] * imageSize.width - sourceW / 2,
|
|
114
121
|
y: srcCenter[ 1 ] * imageSize.height - sourceH / 2,
|
|
115
122
|
width: sourceW,
|
|
116
123
|
height: sourceH,
|
|
117
|
-
rotation:
|
|
118
|
-
flip: { ...
|
|
119
|
-
zoom:
|
|
124
|
+
rotation: safeState.rotation,
|
|
125
|
+
flip: { ...safeState.flip },
|
|
126
|
+
zoom: safeState.zoom,
|
|
120
127
|
};
|
|
121
128
|
}
|
|
122
129
|
|
|
@@ -153,7 +160,7 @@ export function getSourceRegionPercent(
|
|
|
153
160
|
state: CropperState,
|
|
154
161
|
imageSize: Size
|
|
155
162
|
): SourceRegionPercent {
|
|
156
|
-
if (
|
|
163
|
+
if ( ! isValidSize( imageSize ) ) {
|
|
157
164
|
return { x: 0, y: 0, width: 0, height: 0 };
|
|
158
165
|
}
|
|
159
166
|
const region = getSourceRegion( state, imageSize );
|