@wordpress/media-editor 0.9.0 → 0.10.1
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 +212 -170
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +63 -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 +33 -12
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +70 -84
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-fine-rotation/index.cjs +81 -0
- package/build/components/media-editor-fine-rotation/index.cjs.map +7 -0
- package/build/components/media-editor-modal/index.cjs +63 -16
- package/build/components/media-editor-modal/index.cjs.map +3 -3
- package/build/components/media-editor-transform-controls/index.cjs +84 -0
- package/build/components/media-editor-transform-controls/index.cjs.map +7 -0
- 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 +120 -47
- 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 +19 -9
- 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-cropper-reducer.cjs +113 -0
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +7 -0
- package/build/state/composite-reducer.cjs +103 -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 +33 -0
- package/build/state/types.cjs.map +7 -0
- package/build/state/use-media-editor-state.cjs +287 -0
- package/build/state/use-media-editor-state.cjs.map +7 -0
- package/build/store/actions.cjs +4 -2
- package/build/store/actions.cjs.map +2 -2
- package/build/store/reducer.cjs +5 -3
- package/build/store/reducer.cjs.map +2 -2
- package/build/store/selectors.cjs +5 -0
- package/build/store/selectors.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +224 -177
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +37 -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 +34 -13
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +73 -89
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs +50 -0
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +7 -0
- package/build-module/components/media-editor-modal/index.mjs +64 -17
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-transform-controls/index.mjs +68 -0
- package/build-module/components/media-editor-transform-controls/index.mjs.map +7 -0
- 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 +121 -48
- 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 +19 -9
- 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-cropper-reducer.mjs +100 -0
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +7 -0
- package/build-module/state/composite-reducer.mjs +82 -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 +8 -0
- package/build-module/state/types.mjs.map +7 -0
- package/build-module/state/use-media-editor-state.mjs +278 -0
- package/build-module/state/use-media-editor-state.mjs.map +7 -0
- package/build-module/store/actions.mjs +4 -2
- package/build-module/store/actions.mjs.map +2 -2
- package/build-module/store/reducer.mjs +5 -3
- package/build-module/store/reducer.mjs.map +2 -2
- package/build-module/store/selectors.mjs +4 -0
- package/build-module/store/selectors.mjs.map +2 -2
- package/build-style/style-rtl.css +97 -44
- package/build-style/style.css +97 -44
- package/build-types/components/media-editor/index.d.ts +14 -5
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +35 -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 +6 -25
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-fine-rotation/index.d.ts +14 -0
- package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-transform-controls/index.d.ts +8 -0
- package/build-types/components/media-editor-transform-controls/index.d.ts.map +1 -0
- 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-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/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 +96 -0
- package/build-types/state/types.d.ts.map +1 -0
- package/build-types/state/use-media-editor-state.d.ts +101 -0
- package/build-types/state/use-media-editor-state.d.ts.map +1 -0
- package/build-types/store/actions.d.ts +3 -1
- package/build-types/store/actions.d.ts.map +1 -1
- package/build-types/store/reducer.d.ts +3 -0
- package/build-types/store/reducer.d.ts.map +1 -1
- package/build-types/store/selectors.d.ts +1 -0
- package/build-types/store/selectors.d.ts.map +1 -1
- package/package.json +19 -16
- package/src/components/media-editor/index.tsx +262 -229
- package/src/components/media-editor/style.scss +0 -9
- package/src/components/media-editor/test/use-crop-options.tsx +110 -0
- package/src/components/media-editor/use-crop-options.ts +79 -0
- package/src/components/media-editor/use-save-media-editor.ts +214 -0
- package/src/components/media-editor-canvas/index.tsx +57 -25
- package/src/components/media-editor-crop-panel/index.tsx +53 -87
- package/src/components/media-editor-crop-panel/test/index.tsx +46 -53
- package/src/components/media-editor-fine-rotation/index.tsx +74 -0
- package/src/components/media-editor-modal/index.tsx +70 -16
- package/src/components/media-editor-modal/style.scss +80 -0
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +14 -0
- package/src/components/media-editor-modal/test/index.tsx +146 -0
- package/src/components/media-editor-transform-controls/index.tsx +70 -0
- package/src/components/media-editor-transform-controls/style.scss +8 -0
- 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 +64 -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 +194 -89
- package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +269 -0
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +19 -8
- 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-cropper-reducer.ts +204 -0
- package/src/image-editor/stories/rectangle-crop.story.tsx +7 -17
- package/src/state/composite-reducer.ts +137 -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 +330 -0
- package/src/state/types.ts +101 -0
- package/src/state/use-media-editor-state.ts +472 -0
- package/src/store/actions.ts +3 -0
- package/src/store/reducer.ts +6 -1
- package/src/store/selectors.ts +4 -0
- package/src/store/test/reducer.ts +9 -0
- package/src/style.scss +1 -1
- package/tsconfig.json +3 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/build/components/media-editor-toolbar/index.cjs +0 -214
- package/build/components/media-editor-toolbar/index.cjs.map +0 -7
- 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/components/media-editor-toolbar/index.mjs +0 -190
- package/build-module/components/media-editor-toolbar/index.mjs.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/components/media-editor-toolbar/index.d.ts +0 -24
- package/build-types/components/media-editor-toolbar/index.d.ts.map +0 -1
- 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/components/media-editor-toolbar/index.tsx +0 -209
- package/src/components/media-editor-toolbar/style.scss +0 -49
- package/src/image-editor/docs/roadmap.md +0 -66
- package/src/image-editor/react/hooks/use-cropper-state.ts +0 -457
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import type { NormalizedRect, Size } from './types';
|
|
4
|
+
import type { HandlePosition, NormalizedRect, Size } from './types';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
export const MIN_CROP_SIZE = 0.05;
|
|
6
|
+
export type { HandlePosition };
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
|
-
*
|
|
9
|
+
* Default minimum crop rect dimension in normalized space, used when no
|
|
10
|
+
* explicit per-axis minimum is supplied (e.g. before the source image
|
|
11
|
+
* has loaded). Real usage derives a pixel-based min from the source
|
|
12
|
+
* image dimensions and `MIN_CROP_PIXELS` in `constants.ts`.
|
|
11
13
|
*/
|
|
12
|
-
export
|
|
14
|
+
export const DEFAULT_MIN_CROP_SIZE: Size = { width: 0.05, height: 0.05 };
|
|
13
15
|
|
|
14
16
|
/**
|
|
15
17
|
* Bounds within which the crop rect must stay.
|
|
@@ -40,11 +42,12 @@ export interface ResizeDragState {
|
|
|
40
42
|
* Each edge moves independently based on the handle being dragged.
|
|
41
43
|
* Edges are clamped to the provided bounds and maintain a minimum size.
|
|
42
44
|
*
|
|
43
|
-
* @param drag
|
|
44
|
-
* @param clientX
|
|
45
|
-
* @param clientY
|
|
46
|
-
* @param imageSize
|
|
47
|
-
* @param bounds
|
|
45
|
+
* @param drag The current drag state.
|
|
46
|
+
* @param clientX Current mouse/touch X position in pixels.
|
|
47
|
+
* @param clientY Current mouse/touch Y position in pixels.
|
|
48
|
+
* @param imageSize The rendered image dimensions in pixels.
|
|
49
|
+
* @param bounds The allowed crop area bounds.
|
|
50
|
+
* @param minCropSize Minimum crop rect dimension in normalized space, per axis.
|
|
48
51
|
* @return The new crop rect in normalized coordinates.
|
|
49
52
|
*/
|
|
50
53
|
export function computeFreeResizeRect(
|
|
@@ -52,7 +55,8 @@ export function computeFreeResizeRect(
|
|
|
52
55
|
clientX: number,
|
|
53
56
|
clientY: number,
|
|
54
57
|
imageSize: Size,
|
|
55
|
-
bounds: CropBounds
|
|
58
|
+
bounds: CropBounds,
|
|
59
|
+
minCropSize: Size = DEFAULT_MIN_CROP_SIZE
|
|
56
60
|
): NormalizedRect {
|
|
57
61
|
const dx =
|
|
58
62
|
imageSize.width > 0 ? ( clientX - drag.startX ) / imageSize.width : 0;
|
|
@@ -70,24 +74,24 @@ export function computeFreeResizeRect(
|
|
|
70
74
|
if ( handle === 'n' || handle === 'nw' || handle === 'ne' ) {
|
|
71
75
|
edgeTop = Math.max(
|
|
72
76
|
bounds.minY,
|
|
73
|
-
Math.min( s.y + dy, edgeBottom -
|
|
77
|
+
Math.min( s.y + dy, edgeBottom - minCropSize.height )
|
|
74
78
|
);
|
|
75
79
|
}
|
|
76
80
|
if ( handle === 's' || handle === 'sw' || handle === 'se' ) {
|
|
77
81
|
edgeBottom = Math.max(
|
|
78
|
-
edgeTop +
|
|
82
|
+
edgeTop + minCropSize.height,
|
|
79
83
|
Math.min( s.y + s.height + dy, bounds.maxY )
|
|
80
84
|
);
|
|
81
85
|
}
|
|
82
86
|
if ( handle === 'w' || handle === 'nw' || handle === 'sw' ) {
|
|
83
87
|
edgeLeft = Math.max(
|
|
84
88
|
bounds.minX,
|
|
85
|
-
Math.min( s.x + dx, edgeRight -
|
|
89
|
+
Math.min( s.x + dx, edgeRight - minCropSize.width )
|
|
86
90
|
);
|
|
87
91
|
}
|
|
88
92
|
if ( handle === 'e' || handle === 'ne' || handle === 'se' ) {
|
|
89
93
|
edgeRight = Math.max(
|
|
90
|
-
edgeLeft +
|
|
94
|
+
edgeLeft + minCropSize.width,
|
|
91
95
|
Math.min( s.x + s.width + dx, bounds.maxX )
|
|
92
96
|
);
|
|
93
97
|
}
|
|
@@ -113,6 +117,7 @@ export function computeFreeResizeRect(
|
|
|
113
117
|
* @param imageSize The rendered image dimensions in pixels.
|
|
114
118
|
* @param bounds The allowed crop area bounds.
|
|
115
119
|
* @param normalizedRatio The locked aspect ratio (width / height in normalized space).
|
|
120
|
+
* @param minCropSize Minimum crop rect dimension in normalized space, per axis.
|
|
116
121
|
* @return The new crop rect in normalized coordinates.
|
|
117
122
|
*/
|
|
118
123
|
export function computeLockedResizeRect(
|
|
@@ -121,7 +126,8 @@ export function computeLockedResizeRect(
|
|
|
121
126
|
clientY: number,
|
|
122
127
|
imageSize: Size,
|
|
123
128
|
bounds: CropBounds,
|
|
124
|
-
normalizedRatio: number
|
|
129
|
+
normalizedRatio: number,
|
|
130
|
+
minCropSize: Size = DEFAULT_MIN_CROP_SIZE
|
|
125
131
|
): NormalizedRect {
|
|
126
132
|
// The math below divides by `normalizedRatio` and `imageSize`, so
|
|
127
133
|
// bail out with the start rect when any of them is zero. This can
|
|
@@ -159,9 +165,15 @@ export function computeLockedResizeRect(
|
|
|
159
165
|
let distW = ( draggedX - anchorX ) * dirX;
|
|
160
166
|
let distH = ( draggedY - anchorY ) * dirY;
|
|
161
167
|
|
|
162
|
-
// Enforce minimum size
|
|
163
|
-
|
|
164
|
-
|
|
168
|
+
// Enforce minimum size on both axes, projecting through the ratio so
|
|
169
|
+
// neither axis falls below its per-axis floor.
|
|
170
|
+
const minDistW = Math.max(
|
|
171
|
+
minCropSize.width,
|
|
172
|
+
minCropSize.height * normalizedRatio
|
|
173
|
+
);
|
|
174
|
+
const minDistH = minDistW / normalizedRatio;
|
|
175
|
+
distW = Math.max( distW, minDistW );
|
|
176
|
+
distH = Math.max( distH, minDistH );
|
|
165
177
|
|
|
166
178
|
// Determine which axis "drives" — whichever the user moved more
|
|
167
179
|
// (in pixel space) determines the size, the other follows. The
|
|
@@ -195,8 +207,8 @@ export function computeLockedResizeRect(
|
|
|
195
207
|
}
|
|
196
208
|
|
|
197
209
|
// Enforce minimum after clamping.
|
|
198
|
-
distW = Math.max( distW,
|
|
199
|
-
distH = Math.max( distH,
|
|
210
|
+
distW = Math.max( distW, minDistW );
|
|
211
|
+
distH = Math.max( distH, minDistH );
|
|
200
212
|
|
|
201
213
|
// Compute the final rect position from the anchor.
|
|
202
214
|
const newX = dirX > 0 ? anchorX : anchorX - distW;
|
|
@@ -214,11 +226,12 @@ export function computeLockedResizeRect(
|
|
|
214
226
|
* axis symmetrically around the rect's center to preserve the ratio,
|
|
215
227
|
* clamping symmetrically so the rect stays within bounds.
|
|
216
228
|
*
|
|
217
|
-
* @param drag
|
|
218
|
-
* @param clientX
|
|
219
|
-
* @param clientY
|
|
220
|
-
* @param imageSize
|
|
221
|
-
* @param bounds
|
|
229
|
+
* @param drag The current drag state.
|
|
230
|
+
* @param clientX Current mouse/touch X position in pixels.
|
|
231
|
+
* @param clientY Current mouse/touch Y position in pixels.
|
|
232
|
+
* @param imageSize The rendered image dimensions in pixels.
|
|
233
|
+
* @param bounds The allowed crop area bounds.
|
|
234
|
+
* @param minCropSize Minimum crop rect dimension in normalized space, per axis.
|
|
222
235
|
* @return The new crop rect in normalized coordinates.
|
|
223
236
|
*/
|
|
224
237
|
export function computeShiftLockedResizeRect(
|
|
@@ -226,7 +239,8 @@ export function computeShiftLockedResizeRect(
|
|
|
226
239
|
clientX: number,
|
|
227
240
|
clientY: number,
|
|
228
241
|
imageSize: Size,
|
|
229
|
-
bounds: CropBounds
|
|
242
|
+
bounds: CropBounds,
|
|
243
|
+
minCropSize: Size = DEFAULT_MIN_CROP_SIZE
|
|
230
244
|
): NormalizedRect {
|
|
231
245
|
const s = drag.startRect;
|
|
232
246
|
const pixelW = s.width * imageSize.width;
|
|
@@ -237,7 +251,8 @@ export function computeShiftLockedResizeRect(
|
|
|
237
251
|
clientX,
|
|
238
252
|
clientY,
|
|
239
253
|
imageSize,
|
|
240
|
-
bounds
|
|
254
|
+
bounds,
|
|
255
|
+
minCropSize
|
|
241
256
|
);
|
|
242
257
|
}
|
|
243
258
|
const normalizedRatio = s.width / s.height;
|
|
@@ -255,7 +270,8 @@ export function computeShiftLockedResizeRect(
|
|
|
255
270
|
clientY,
|
|
256
271
|
imageSize,
|
|
257
272
|
bounds,
|
|
258
|
-
normalizedRatio
|
|
273
|
+
normalizedRatio,
|
|
274
|
+
minCropSize
|
|
259
275
|
);
|
|
260
276
|
}
|
|
261
277
|
|
|
@@ -266,7 +282,8 @@ export function computeShiftLockedResizeRect(
|
|
|
266
282
|
clientX,
|
|
267
283
|
clientY,
|
|
268
284
|
imageSize,
|
|
269
|
-
bounds
|
|
285
|
+
bounds,
|
|
286
|
+
minCropSize
|
|
270
287
|
);
|
|
271
288
|
|
|
272
289
|
if ( handle === 'n' || handle === 's' ) {
|
|
@@ -284,11 +301,11 @@ export function computeShiftLockedResizeRect(
|
|
|
284
301
|
}
|
|
285
302
|
// Enforce the minimum on the driving axis, then derive the
|
|
286
303
|
// other axis from the ratio so it stays consistent. The
|
|
287
|
-
// effective minimum height has to satisfy
|
|
288
|
-
// both axes simultaneously.
|
|
304
|
+
// effective minimum height has to satisfy the per-axis floor
|
|
305
|
+
// on both axes simultaneously.
|
|
289
306
|
const minHeight = Math.max(
|
|
290
|
-
|
|
291
|
-
|
|
307
|
+
minCropSize.height,
|
|
308
|
+
minCropSize.width / normalizedRatio
|
|
292
309
|
);
|
|
293
310
|
if ( newHeight < minHeight ) {
|
|
294
311
|
newHeight = minHeight;
|
|
@@ -315,7 +332,10 @@ export function computeShiftLockedResizeRect(
|
|
|
315
332
|
newHeight = maxHeight;
|
|
316
333
|
newWidth = newHeight * normalizedRatio;
|
|
317
334
|
}
|
|
318
|
-
const minWidth = Math.max(
|
|
335
|
+
const minWidth = Math.max(
|
|
336
|
+
minCropSize.width,
|
|
337
|
+
minCropSize.height * normalizedRatio
|
|
338
|
+
);
|
|
319
339
|
if ( newWidth < minWidth ) {
|
|
320
340
|
newWidth = minWidth;
|
|
321
341
|
newHeight = newWidth / normalizedRatio;
|
|
@@ -5,18 +5,22 @@ import {
|
|
|
5
5
|
getVisibleBounds,
|
|
6
6
|
createExportCamera,
|
|
7
7
|
getImageFit,
|
|
8
|
+
getRotatedBBox,
|
|
8
9
|
} from '../camera';
|
|
9
10
|
import {
|
|
10
11
|
restrictPanZoom,
|
|
11
12
|
restrictCropRect,
|
|
12
13
|
getImageCropBounds,
|
|
14
|
+
getMinZoom,
|
|
13
15
|
} from '../containment';
|
|
14
16
|
import { getSourceRegion, getSourceRegionPercent } from '../source-region';
|
|
15
|
-
import {
|
|
17
|
+
import { computeTransformStyle } from '../transform-style';
|
|
18
|
+
import { DEFAULT_STATE, MAX_ZOOM, MIN_ZOOM } from '../constants';
|
|
16
19
|
import type { CropperState, Size } from '../types';
|
|
17
20
|
|
|
18
21
|
const CONTAINER: Size = { width: 800, height: 600 };
|
|
19
22
|
const IMAGE: Size = { width: 1600, height: 900 };
|
|
23
|
+
const PORTRAIT_IMAGE: Size = { width: 900, height: 1600 };
|
|
20
24
|
|
|
21
25
|
function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
22
26
|
return {
|
|
@@ -30,6 +34,47 @@ function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
|
30
34
|
};
|
|
31
35
|
}
|
|
32
36
|
|
|
37
|
+
function expectImageCoversCrop( state: CropperState, imageSize: Size ): void {
|
|
38
|
+
const container: Size = { width: 1000, height: 1000 };
|
|
39
|
+
const camera = createCamera( state, container, imageSize );
|
|
40
|
+
const snapRotation = Math.round( state.rotation / 90 ) * 90;
|
|
41
|
+
const baseCamera = createCamera(
|
|
42
|
+
{
|
|
43
|
+
...state,
|
|
44
|
+
pan: { x: 0, y: 0 },
|
|
45
|
+
zoom: 1,
|
|
46
|
+
rotation: snapRotation,
|
|
47
|
+
},
|
|
48
|
+
container,
|
|
49
|
+
imageSize
|
|
50
|
+
);
|
|
51
|
+
const vb = getVisibleBounds( baseCamera );
|
|
52
|
+
const cr = state.cropRect;
|
|
53
|
+
const stencilCorners: [ number, number ][] = [
|
|
54
|
+
[ vb.left + cr.x * vb.width, vb.top + cr.y * vb.height ],
|
|
55
|
+
[ vb.left + ( cr.x + cr.width ) * vb.width, vb.top + cr.y * vb.height ],
|
|
56
|
+
[
|
|
57
|
+
vb.left + ( cr.x + cr.width ) * vb.width,
|
|
58
|
+
vb.top + ( cr.y + cr.height ) * vb.height,
|
|
59
|
+
],
|
|
60
|
+
[
|
|
61
|
+
vb.left + cr.x * vb.width,
|
|
62
|
+
vb.top + ( cr.y + cr.height ) * vb.height,
|
|
63
|
+
],
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
for ( const corner of stencilCorners ) {
|
|
67
|
+
const w = screenToWorld( camera, {
|
|
68
|
+
x: corner[ 0 ],
|
|
69
|
+
y: corner[ 1 ],
|
|
70
|
+
} );
|
|
71
|
+
expect( w.x ).toBeGreaterThanOrEqual( -0.001 );
|
|
72
|
+
expect( w.x ).toBeLessThanOrEqual( 1.001 );
|
|
73
|
+
expect( w.y ).toBeGreaterThanOrEqual( -0.001 );
|
|
74
|
+
expect( w.y ).toBeLessThanOrEqual( 1.001 );
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
33
78
|
describe( 'createCamera', () => {
|
|
34
79
|
it( 'returns a mat2d', () => {
|
|
35
80
|
const camera = createCamera( makeState(), CONTAINER, IMAGE );
|
|
@@ -120,6 +165,35 @@ describe( 'restrictPanZoom', () => {
|
|
|
120
165
|
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
121
166
|
expect( result.zoom ).toBeGreaterThanOrEqual( 1 );
|
|
122
167
|
} );
|
|
168
|
+
it( 'caps zoom at MAX_ZOOM', () => {
|
|
169
|
+
const state = makeState( { zoom: MAX_ZOOM * 2 } );
|
|
170
|
+
const result = restrictPanZoom( state, IMAGE, state.cropRect );
|
|
171
|
+
expect( result.zoom ).toBe( MAX_ZOOM );
|
|
172
|
+
} );
|
|
173
|
+
it( 'allows zoom below 1 when a fine-rotated portrait crop remains covered', () => {
|
|
174
|
+
const cropRect = { x: 0.46, y: 0, width: 0.08, height: 1 };
|
|
175
|
+
const state = makeState( {
|
|
176
|
+
image: {
|
|
177
|
+
src: 'portrait.jpg',
|
|
178
|
+
naturalWidth: PORTRAIT_IMAGE.width,
|
|
179
|
+
naturalHeight: PORTRAIT_IMAGE.height,
|
|
180
|
+
},
|
|
181
|
+
rotation: 19,
|
|
182
|
+
zoom: 0.9,
|
|
183
|
+
cropRect,
|
|
184
|
+
} );
|
|
185
|
+
const result = restrictPanZoom( state, PORTRAIT_IMAGE, cropRect );
|
|
186
|
+
|
|
187
|
+
expect( result.zoom ).toBeGreaterThan( 0.9 );
|
|
188
|
+
expect( result.zoom ).toBeLessThan( 1 );
|
|
189
|
+
|
|
190
|
+
const restrictedState = makeState( {
|
|
191
|
+
...state,
|
|
192
|
+
pan: result.pan,
|
|
193
|
+
zoom: result.zoom,
|
|
194
|
+
} );
|
|
195
|
+
expectImageCoversCrop( restrictedState, PORTRAIT_IMAGE );
|
|
196
|
+
} );
|
|
123
197
|
it( 'at 90° with zoom=1, allows zero pan on landscape image', () => {
|
|
124
198
|
// At zoom=1, 90° rotation, the image exactly covers the visual area.
|
|
125
199
|
// No pan should be possible in either direction.
|
|
@@ -173,6 +247,36 @@ describe( 'restrictPanZoom', () => {
|
|
|
173
247
|
} );
|
|
174
248
|
} );
|
|
175
249
|
|
|
250
|
+
describe( 'getMinZoom', () => {
|
|
251
|
+
it( 'falls back to MIN_ZOOM for invalid image dimensions', () => {
|
|
252
|
+
const state = makeState( {
|
|
253
|
+
image: {
|
|
254
|
+
src: 'invalid.jpg',
|
|
255
|
+
naturalWidth: 1000,
|
|
256
|
+
naturalHeight: 0,
|
|
257
|
+
},
|
|
258
|
+
} );
|
|
259
|
+
|
|
260
|
+
expect( getMinZoom( state ) ).toBe( MIN_ZOOM );
|
|
261
|
+
} );
|
|
262
|
+
|
|
263
|
+
it( 'returns a finite value for non-finite rotation and crop fields', () => {
|
|
264
|
+
const state = makeState( {
|
|
265
|
+
rotation: Number.NaN,
|
|
266
|
+
cropRect: {
|
|
267
|
+
x: 0,
|
|
268
|
+
y: 0,
|
|
269
|
+
width: Number.NaN,
|
|
270
|
+
height: 1,
|
|
271
|
+
},
|
|
272
|
+
} );
|
|
273
|
+
|
|
274
|
+
const minZoom = getMinZoom( state );
|
|
275
|
+
expect( Number.isFinite( minZoom ) ).toBe( true );
|
|
276
|
+
expect( minZoom ).toBeGreaterThan( 0 );
|
|
277
|
+
} );
|
|
278
|
+
} );
|
|
279
|
+
|
|
176
280
|
describe( 'restrictCropRect', () => {
|
|
177
281
|
it( 'returns same rect when it fits', () => {
|
|
178
282
|
const rect = { x: 0, y: 0, width: 1, height: 1 };
|
|
@@ -580,3 +684,265 @@ describe( 'getImageCropBounds', () => {
|
|
|
580
684
|
expect( bounds.maxY ).toBeGreaterThan( 1.3 );
|
|
581
685
|
} );
|
|
582
686
|
} );
|
|
687
|
+
|
|
688
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
689
|
+
// Non-finite input regressions.
|
|
690
|
+
//
|
|
691
|
+
// The reducer normalizes rotation/zoom/pan on every action, so these paths
|
|
692
|
+
// aren't user-reachable today. But programmatic callers and deserialized
|
|
693
|
+
// state could still feed corrupted values, which used to silently propagate
|
|
694
|
+
// NaN/Infinity into the crop rect, camera matrix, or REST /edit payload.
|
|
695
|
+
// These tests pin the defense-in-depth guards in place.
|
|
696
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
697
|
+
|
|
698
|
+
const HOSTILE_STATE: CropperState = makeState( {
|
|
699
|
+
pan: { x: Number.NaN, y: Number.NEGATIVE_INFINITY },
|
|
700
|
+
zoom: Number.POSITIVE_INFINITY,
|
|
701
|
+
rotation: Number.NaN,
|
|
702
|
+
} );
|
|
703
|
+
|
|
704
|
+
describe( 'getRotatedBBox — non-finite input regression', () => {
|
|
705
|
+
it.each( [
|
|
706
|
+
[ 'NaN width', Number.NaN, 100, 30 ],
|
|
707
|
+
[ '±Infinity height', 100, Number.POSITIVE_INFINITY, 30 ],
|
|
708
|
+
[ 'NaN rotation', 100, 100, Number.NaN ],
|
|
709
|
+
[ '-Infinity rotation', 100, 100, Number.NEGATIVE_INFINITY ],
|
|
710
|
+
// MAX_VALUE is finite but degreesToRadians( MAX_VALUE ) overflows
|
|
711
|
+
// to Infinity, then Math.cos/sin return NaN. The magnitude bound
|
|
712
|
+
// in `isSafeNumber` catches this before the trig.
|
|
713
|
+
[ 'MAX_VALUE rotation', 100, 100, Number.MAX_VALUE ],
|
|
714
|
+
] )( 'returns {0, 0} for %s', ( _label, w, h, rot ) => {
|
|
715
|
+
expect( getRotatedBBox( w, h, rot ) ).toEqual( {
|
|
716
|
+
width: 0,
|
|
717
|
+
height: 0,
|
|
718
|
+
} );
|
|
719
|
+
} );
|
|
720
|
+
|
|
721
|
+
it( 'still returns correct dimensions for normal inputs', () => {
|
|
722
|
+
// Sanity check that the guard doesn't break the happy path.
|
|
723
|
+
const out = getRotatedBBox( 200, 100, 90 );
|
|
724
|
+
expect( out.width ).toBeCloseTo( 100, 5 );
|
|
725
|
+
expect( out.height ).toBeCloseTo( 200, 5 );
|
|
726
|
+
} );
|
|
727
|
+
} );
|
|
728
|
+
|
|
729
|
+
describe( 'getImageFit — non-finite input regression', () => {
|
|
730
|
+
it( 'returns finite sizes when rotation is NaN (treated as 0)', () => {
|
|
731
|
+
const out = getImageFit( CONTAINER, IMAGE, Number.NaN );
|
|
732
|
+
expect( Number.isFinite( out.elementSize.width ) ).toBe( true );
|
|
733
|
+
expect( Number.isFinite( out.elementSize.height ) ).toBe( true );
|
|
734
|
+
expect( Number.isFinite( out.visualSize.width ) ).toBe( true );
|
|
735
|
+
expect( Number.isFinite( out.visualSize.height ) ).toBe( true );
|
|
736
|
+
} );
|
|
737
|
+
|
|
738
|
+
it( 'returns zero sizes when containerSize has a non-finite dimension', () => {
|
|
739
|
+
const out = getImageFit( { width: Number.NaN, height: 600 }, IMAGE, 0 );
|
|
740
|
+
expect( out.elementSize ).toEqual( { width: 0, height: 0 } );
|
|
741
|
+
expect( out.visualSize ).toEqual( { width: 0, height: 0 } );
|
|
742
|
+
} );
|
|
743
|
+
|
|
744
|
+
it( 'returns zero sizes when imageSize has a non-finite dimension', () => {
|
|
745
|
+
const out = getImageFit(
|
|
746
|
+
CONTAINER,
|
|
747
|
+
{ width: Number.POSITIVE_INFINITY, height: 900 },
|
|
748
|
+
0
|
|
749
|
+
);
|
|
750
|
+
expect( out.elementSize ).toEqual( { width: 0, height: 0 } );
|
|
751
|
+
expect( out.visualSize ).toEqual( { width: 0, height: 0 } );
|
|
752
|
+
} );
|
|
753
|
+
} );
|
|
754
|
+
|
|
755
|
+
describe( 'createCamera — non-finite input regression', () => {
|
|
756
|
+
it( 'returns a matrix with all finite entries even for hostile state', () => {
|
|
757
|
+
const m = createCamera( HOSTILE_STATE, CONTAINER, IMAGE );
|
|
758
|
+
expect( m ).toHaveLength( 6 );
|
|
759
|
+
for ( let i = 0; i < 6; i++ ) {
|
|
760
|
+
expect( Number.isFinite( m[ i ] ) ).toBe( true );
|
|
761
|
+
}
|
|
762
|
+
} );
|
|
763
|
+
|
|
764
|
+
it( 'returns identity matrix when imageSize has non-finite dimensions', () => {
|
|
765
|
+
const m = createCamera( makeState(), CONTAINER, {
|
|
766
|
+
width: Number.NaN,
|
|
767
|
+
height: 900,
|
|
768
|
+
} );
|
|
769
|
+
// mat2d.create() returns [1, 0, 0, 1, 0, 0] (identity).
|
|
770
|
+
expect( Array.from( m ) ).toEqual( [ 1, 0, 0, 1, 0, 0 ] );
|
|
771
|
+
} );
|
|
772
|
+
} );
|
|
773
|
+
|
|
774
|
+
describe( 'createExportCamera — non-finite input regression', () => {
|
|
775
|
+
it( 'returns a matrix with all finite entries even for hostile state', () => {
|
|
776
|
+
// This is the matrix passed to ctx.setTransform → ctx.drawImage at
|
|
777
|
+
// export time, so NaN here would corrupt the saved file.
|
|
778
|
+
const m = createExportCamera( HOSTILE_STATE, IMAGE, {
|
|
779
|
+
width: 600,
|
|
780
|
+
height: 400,
|
|
781
|
+
} );
|
|
782
|
+
for ( let i = 0; i < 6; i++ ) {
|
|
783
|
+
expect( Number.isFinite( m[ i ] ) ).toBe( true );
|
|
784
|
+
}
|
|
785
|
+
} );
|
|
786
|
+
|
|
787
|
+
it( 'returns identity matrix when outputSize has non-finite dimensions', () => {
|
|
788
|
+
const m = createExportCamera( makeState(), IMAGE, {
|
|
789
|
+
width: Number.POSITIVE_INFINITY,
|
|
790
|
+
height: 400,
|
|
791
|
+
} );
|
|
792
|
+
expect( Array.from( m ) ).toEqual( [ 1, 0, 0, 1, 0, 0 ] );
|
|
793
|
+
} );
|
|
794
|
+
} );
|
|
795
|
+
|
|
796
|
+
describe( 'restrictCropRect — non-finite input regression', () => {
|
|
797
|
+
const cropRect = { x: 0.1, y: 0.1, width: 0.5, height: 0.5 };
|
|
798
|
+
|
|
799
|
+
it.each( [
|
|
800
|
+
[ 'NaN zoom', cropRect, Number.NaN, 0, 16 / 9 ],
|
|
801
|
+
[ '-Infinity rotation', cropRect, 1, Number.NEGATIVE_INFINITY, 16 / 9 ],
|
|
802
|
+
[ 'NaN aspectRatio', cropRect, 1, 0, Number.NaN ],
|
|
803
|
+
[
|
|
804
|
+
'MAX_VALUE zoom (overflow guard)',
|
|
805
|
+
cropRect,
|
|
806
|
+
Number.MAX_VALUE,
|
|
807
|
+
0,
|
|
808
|
+
16 / 9,
|
|
809
|
+
],
|
|
810
|
+
] )( 'returns a finite rect for %s', ( _label, rect, z, r, a ) => {
|
|
811
|
+
const out = restrictCropRect( rect, z, r, a );
|
|
812
|
+
expect( Number.isFinite( out.x ) ).toBe( true );
|
|
813
|
+
expect( Number.isFinite( out.y ) ).toBe( true );
|
|
814
|
+
expect( Number.isFinite( out.width ) ).toBe( true );
|
|
815
|
+
expect( Number.isFinite( out.height ) ).toBe( true );
|
|
816
|
+
} );
|
|
817
|
+
|
|
818
|
+
it( 'sanitizes cropRect fields on the no-resize (fit-through) path', () => {
|
|
819
|
+
// When the crop already fits (t >= 1 - EPSILON) the function used to
|
|
820
|
+
// return the raw cropRect, letting non-finite fields slip through.
|
|
821
|
+
const hostileRect = {
|
|
822
|
+
x: Number.NaN,
|
|
823
|
+
y: 0.1,
|
|
824
|
+
width: 0.3,
|
|
825
|
+
height: 0.3,
|
|
826
|
+
};
|
|
827
|
+
const out = restrictCropRect( hostileRect, 10, 0, 16 / 9 );
|
|
828
|
+
expect( Number.isFinite( out.x ) ).toBe( true );
|
|
829
|
+
expect( out.x ).toBe( 0 );
|
|
830
|
+
} );
|
|
831
|
+
} );
|
|
832
|
+
|
|
833
|
+
describe( 'restrictPanZoom — non-finite input regression', () => {
|
|
834
|
+
const cropRect = { x: 0.1, y: 0.1, width: 0.5, height: 0.5 };
|
|
835
|
+
|
|
836
|
+
it( 'returns finite pan and zoom for hostile state', () => {
|
|
837
|
+
const out = restrictPanZoom( HOSTILE_STATE, IMAGE, cropRect );
|
|
838
|
+
expect( Number.isFinite( out.zoom ) ).toBe( true );
|
|
839
|
+
expect( Number.isFinite( out.pan.x ) ).toBe( true );
|
|
840
|
+
expect( Number.isFinite( out.pan.y ) ).toBe( true );
|
|
841
|
+
} );
|
|
842
|
+
|
|
843
|
+
it( 'returns finite pan and zoom when only state.zoom is corrupted', () => {
|
|
844
|
+
const state = makeState( { zoom: Number.NaN } );
|
|
845
|
+
const out = restrictPanZoom( state, IMAGE, cropRect );
|
|
846
|
+
expect( Number.isFinite( out.zoom ) ).toBe( true );
|
|
847
|
+
expect( out.zoom ).toBeGreaterThanOrEqual( 1 );
|
|
848
|
+
} );
|
|
849
|
+
|
|
850
|
+
it( 'sanitizes the cropRect argument before feeding it into the math', () => {
|
|
851
|
+
// A NaN cropRect.width would drive getMinZoomForCover to NaN via
|
|
852
|
+
// Math.max(1, NaN, NaN), and that NaN would be returned as zoom.
|
|
853
|
+
const hostileRect = {
|
|
854
|
+
x: 0.1,
|
|
855
|
+
y: 0.1,
|
|
856
|
+
width: Number.NaN,
|
|
857
|
+
height: 0.5,
|
|
858
|
+
};
|
|
859
|
+
const out = restrictPanZoom( makeState(), IMAGE, hostileRect );
|
|
860
|
+
expect( Number.isFinite( out.zoom ) ).toBe( true );
|
|
861
|
+
expect( Number.isFinite( out.pan.x ) ).toBe( true );
|
|
862
|
+
expect( Number.isFinite( out.pan.y ) ).toBe( true );
|
|
863
|
+
} );
|
|
864
|
+
|
|
865
|
+
it( 'returns finite zoom when imageSize is Infinity (aspectRatio NaN guard)', () => {
|
|
866
|
+
// Infinity / Infinity = NaN, which used to leak as `zoom: NaN`
|
|
867
|
+
// through the no-correction early return.
|
|
868
|
+
const rect = { x: 0.1, y: 0.1, width: 0.5, height: 0.5 };
|
|
869
|
+
const out = restrictPanZoom(
|
|
870
|
+
makeState(),
|
|
871
|
+
{ width: Infinity, height: Infinity },
|
|
872
|
+
rect
|
|
873
|
+
);
|
|
874
|
+
expect( Number.isFinite( out.zoom ) ).toBe( true );
|
|
875
|
+
expect( Number.isFinite( out.pan.x ) ).toBe( true );
|
|
876
|
+
expect( Number.isFinite( out.pan.y ) ).toBe( true );
|
|
877
|
+
} );
|
|
878
|
+
} );
|
|
879
|
+
|
|
880
|
+
describe( 'computeTransformStyle — non-finite input regression', () => {
|
|
881
|
+
it( 'produces a finite matrix string for hostile state', () => {
|
|
882
|
+
// CSS preview path must agree with createCamera under hostile state
|
|
883
|
+
// (both should produce safe output, not divergent NaN vs finite).
|
|
884
|
+
const out = computeTransformStyle( HOSTILE_STATE, IMAGE );
|
|
885
|
+
expect( out ).toMatch( /^matrix\(/ );
|
|
886
|
+
expect( out ).not.toMatch( /NaN/ );
|
|
887
|
+
expect( out ).not.toMatch( /Infinity/ );
|
|
888
|
+
} );
|
|
889
|
+
|
|
890
|
+
it( 'returns the identity matrix when imageSize is hostile', () => {
|
|
891
|
+
// State sanitization only covers state fields; imageSize NaN/Infinity
|
|
892
|
+
// would still emit `matrix(..., NaN, NaN)` in the translate components
|
|
893
|
+
// without this explicit guard.
|
|
894
|
+
expect(
|
|
895
|
+
computeTransformStyle( makeState(), {
|
|
896
|
+
width: Number.NaN,
|
|
897
|
+
height: 900,
|
|
898
|
+
} )
|
|
899
|
+
).toBe( 'matrix(1, 0, 0, 1, 0, 0)' );
|
|
900
|
+
expect(
|
|
901
|
+
computeTransformStyle( makeState(), {
|
|
902
|
+
width: Infinity,
|
|
903
|
+
height: Infinity,
|
|
904
|
+
} )
|
|
905
|
+
).toBe( 'matrix(1, 0, 0, 1, 0, 0)' );
|
|
906
|
+
} );
|
|
907
|
+
} );
|
|
908
|
+
|
|
909
|
+
describe( 'getImageCropBounds — non-finite input regression', () => {
|
|
910
|
+
it( 'returns finite bounds for hostile state', () => {
|
|
911
|
+
const out = getImageCropBounds(
|
|
912
|
+
HOSTILE_STATE,
|
|
913
|
+
{ width: 800, height: 450 },
|
|
914
|
+
{ width: 800, height: 450 }
|
|
915
|
+
);
|
|
916
|
+
expect( Number.isFinite( out.minX ) ).toBe( true );
|
|
917
|
+
expect( Number.isFinite( out.minY ) ).toBe( true );
|
|
918
|
+
expect( Number.isFinite( out.maxX ) ).toBe( true );
|
|
919
|
+
expect( Number.isFinite( out.maxY ) ).toBe( true );
|
|
920
|
+
expect( out.minX ).toBeLessThanOrEqual( out.maxX );
|
|
921
|
+
expect( out.minY ).toBeLessThanOrEqual( out.maxY );
|
|
922
|
+
} );
|
|
923
|
+
} );
|
|
924
|
+
|
|
925
|
+
describe( 'getSourceRegion — non-finite input regression', () => {
|
|
926
|
+
it( 'returns a finite source region for hostile state', () => {
|
|
927
|
+
// getSourceRegion feeds the REST /edit endpoint, so NaN here would
|
|
928
|
+
// reach the server.
|
|
929
|
+
const out = getSourceRegion( HOSTILE_STATE, IMAGE );
|
|
930
|
+
expect( Number.isFinite( out.x ) ).toBe( true );
|
|
931
|
+
expect( Number.isFinite( out.y ) ).toBe( true );
|
|
932
|
+
expect( Number.isFinite( out.width ) ).toBe( true );
|
|
933
|
+
expect( Number.isFinite( out.height ) ).toBe( true );
|
|
934
|
+
} );
|
|
935
|
+
|
|
936
|
+
it( 'returns finite metadata on the zero-size early-return path', () => {
|
|
937
|
+
// When imageSize is zero/invalid, the function returns a zero region
|
|
938
|
+
// plus rotation/flip/zoom metadata. Hostile state must not leak NaN
|
|
939
|
+
// out through those metadata fields.
|
|
940
|
+
const out = getSourceRegion( HOSTILE_STATE, {
|
|
941
|
+
width: 0,
|
|
942
|
+
height: 0,
|
|
943
|
+
} );
|
|
944
|
+
expect( Number.isFinite( out.rotation ) ).toBe( true );
|
|
945
|
+
expect( Number.isFinite( out.zoom ) ).toBe( true );
|
|
946
|
+
expect( out.zoom ).toBeGreaterThanOrEqual( 1 );
|
|
947
|
+
} );
|
|
948
|
+
} );
|