@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
|
@@ -6,19 +6,19 @@ import { renderHook, act } from '@testing-library/react';
|
|
|
6
6
|
/**
|
|
7
7
|
* Internal dependencies
|
|
8
8
|
*/
|
|
9
|
-
import {
|
|
9
|
+
import { useCropperReducer } from '../use-cropper-reducer';
|
|
10
10
|
import { DEFAULT_STATE } from '../../../core/constants';
|
|
11
11
|
|
|
12
|
-
describe( '
|
|
12
|
+
describe( 'useCropperReducer', () => {
|
|
13
13
|
it( 'should initialize with DEFAULT_STATE', () => {
|
|
14
|
-
const { result } = renderHook( () =>
|
|
14
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
15
15
|
|
|
16
16
|
expect( result.current.state ).toEqual( DEFAULT_STATE );
|
|
17
17
|
} );
|
|
18
18
|
|
|
19
19
|
it( 'should initialize with merged partial state', () => {
|
|
20
20
|
const { result } = renderHook( () =>
|
|
21
|
-
|
|
21
|
+
useCropperReducer( { zoom: 2, rotation: 45 } )
|
|
22
22
|
);
|
|
23
23
|
|
|
24
24
|
expect( result.current.state.zoom ).toBe( 2 );
|
|
@@ -28,7 +28,7 @@ describe( 'useCropperState', () => {
|
|
|
28
28
|
} );
|
|
29
29
|
|
|
30
30
|
it( 'should dispatch SET_CROP via setCrop', () => {
|
|
31
|
-
const { result } = renderHook( () =>
|
|
31
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
32
32
|
|
|
33
33
|
// At zoom=1, crop is clamped to (0,0) — no panning possible.
|
|
34
34
|
// Zoom in first to allow panning.
|
|
@@ -45,7 +45,7 @@ describe( 'useCropperState', () => {
|
|
|
45
45
|
} );
|
|
46
46
|
|
|
47
47
|
it( 'should dispatch SET_ZOOM via setZoom', () => {
|
|
48
|
-
const { result } = renderHook( () =>
|
|
48
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
49
49
|
|
|
50
50
|
act( () => {
|
|
51
51
|
result.current.setZoom( 3 );
|
|
@@ -54,8 +54,67 @@ describe( 'useCropperState', () => {
|
|
|
54
54
|
expect( result.current.state.zoom ).toBe( 3 );
|
|
55
55
|
} );
|
|
56
56
|
|
|
57
|
+
it( 'leaves pan at zero on setZoom when the cropRect is centered', () => {
|
|
58
|
+
// Sanity: the default cropRect spans the full image, so its
|
|
59
|
+
// center coincides with the image center and the crop-center
|
|
60
|
+
// focal-point correction degenerates to "no pan change."
|
|
61
|
+
// Guards against regressions where setZoom accidentally pans.
|
|
62
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
63
|
+
|
|
64
|
+
act( () => {
|
|
65
|
+
result.current.setZoom( 4 );
|
|
66
|
+
} );
|
|
67
|
+
|
|
68
|
+
expect( result.current.state.zoom ).toBe( 4 );
|
|
69
|
+
expect( result.current.state.pan ).toEqual( { x: 0, y: 0 } );
|
|
70
|
+
|
|
71
|
+
act( () => {
|
|
72
|
+
result.current.setZoom( 2 );
|
|
73
|
+
} );
|
|
74
|
+
|
|
75
|
+
expect( result.current.state.zoom ).toBe( 2 );
|
|
76
|
+
expect( result.current.state.pan ).toEqual( { x: 0, y: 0 } );
|
|
77
|
+
} );
|
|
78
|
+
|
|
79
|
+
it( 'anchors setZoom at the crop-rect center', () => {
|
|
80
|
+
// With an off-center crop, setZoom must shift pan toward the
|
|
81
|
+
// crop center as zoom decreases — otherwise `enforceContainment`
|
|
82
|
+
// would translate the image toward the nearest viewport corner
|
|
83
|
+
// (the original bug this method fixes).
|
|
84
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
85
|
+
|
|
86
|
+
act( () => {
|
|
87
|
+
result.current.setZoom( 4 );
|
|
88
|
+
} );
|
|
89
|
+
// Place the crop in the bottom-right quadrant — center
|
|
90
|
+
// at ( 0.7, 0.7 ), i.e. ( +0.2, +0.2 ) in pan coords.
|
|
91
|
+
act( () => {
|
|
92
|
+
result.current.setCropRect( {
|
|
93
|
+
x: 0.6,
|
|
94
|
+
y: 0.6,
|
|
95
|
+
width: 0.2,
|
|
96
|
+
height: 0.2,
|
|
97
|
+
} );
|
|
98
|
+
} );
|
|
99
|
+
act( () => {
|
|
100
|
+
result.current.setPan( { x: 0, y: 0 } );
|
|
101
|
+
} );
|
|
102
|
+
|
|
103
|
+
expect( result.current.state.pan ).toEqual( { x: 0, y: 0 } );
|
|
104
|
+
|
|
105
|
+
// Zoom out — pan should move toward the focal point in +x, +y,
|
|
106
|
+
// not stay at the origin (which would corner-snap on containment).
|
|
107
|
+
act( () => {
|
|
108
|
+
result.current.setZoom( 2 );
|
|
109
|
+
} );
|
|
110
|
+
|
|
111
|
+
expect( result.current.state.zoom ).toBe( 2 );
|
|
112
|
+
expect( result.current.state.pan.x ).toBeGreaterThan( 0 );
|
|
113
|
+
expect( result.current.state.pan.y ).toBeGreaterThan( 0 );
|
|
114
|
+
} );
|
|
115
|
+
|
|
57
116
|
it( 'should dispatch SET_ZOOM_AT_POINT via setZoomAtPoint', () => {
|
|
58
|
-
const { result } = renderHook( () =>
|
|
117
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
59
118
|
|
|
60
119
|
act( () => {
|
|
61
120
|
result.current.setZoomAtPoint( 3, { x: 0.2, y: 0.1 } );
|
|
@@ -66,7 +125,7 @@ describe( 'useCropperState', () => {
|
|
|
66
125
|
} );
|
|
67
126
|
|
|
68
127
|
it( 'should clamp zoom to valid range via SET_ZOOM', () => {
|
|
69
|
-
const { result } = renderHook( () =>
|
|
128
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
70
129
|
|
|
71
130
|
act( () => {
|
|
72
131
|
result.current.setZoom( 15 );
|
|
@@ -81,8 +140,32 @@ describe( 'useCropperState', () => {
|
|
|
81
140
|
expect( result.current.state.zoom ).toBe( 1 );
|
|
82
141
|
} );
|
|
83
142
|
|
|
143
|
+
it( 'allows setZoom below 1 when the coverage minimum permits', () => {
|
|
144
|
+
// Thin, fine-rotated crop on a portrait image: the coverage
|
|
145
|
+
// minimum drops below 1, so `setZoom` (slider / keyboard path)
|
|
146
|
+
// must honour values below 1 rather than clamping at MIN_ZOOM.
|
|
147
|
+
const { result } = renderHook( () =>
|
|
148
|
+
useCropperReducer( {
|
|
149
|
+
image: {
|
|
150
|
+
src: 'portrait.jpg',
|
|
151
|
+
naturalWidth: 900,
|
|
152
|
+
naturalHeight: 1600,
|
|
153
|
+
},
|
|
154
|
+
rotation: 19,
|
|
155
|
+
cropRect: { x: 0.48, y: 0, width: 0.04, height: 1 },
|
|
156
|
+
} )
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
act( () => {
|
|
160
|
+
result.current.setZoom( 0.9 );
|
|
161
|
+
} );
|
|
162
|
+
|
|
163
|
+
expect( result.current.state.zoom ).toBeLessThan( 1 );
|
|
164
|
+
expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 0.9 );
|
|
165
|
+
} );
|
|
166
|
+
|
|
84
167
|
it( 'should dispatch SET_ROTATION via setRotation', () => {
|
|
85
|
-
const { result } = renderHook( () =>
|
|
168
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
86
169
|
|
|
87
170
|
act( () => {
|
|
88
171
|
result.current.setRotation( 90 );
|
|
@@ -92,7 +175,7 @@ describe( 'useCropperState', () => {
|
|
|
92
175
|
} );
|
|
93
176
|
|
|
94
177
|
it( 'should normalize rotation via SET_ROTATION', () => {
|
|
95
|
-
const { result } = renderHook( () =>
|
|
178
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
96
179
|
|
|
97
180
|
act( () => {
|
|
98
181
|
result.current.setRotation( -90 );
|
|
@@ -108,7 +191,7 @@ describe( 'useCropperState', () => {
|
|
|
108
191
|
} );
|
|
109
192
|
|
|
110
193
|
it( 'should dispatch SET_FLIP via setFlip', () => {
|
|
111
|
-
const { result } = renderHook( () =>
|
|
194
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
112
195
|
|
|
113
196
|
act( () => {
|
|
114
197
|
result.current.setFlip( {
|
|
@@ -124,7 +207,7 @@ describe( 'useCropperState', () => {
|
|
|
124
207
|
} );
|
|
125
208
|
|
|
126
209
|
it( 'toggleFlip should toggle horizontal flip', () => {
|
|
127
|
-
const { result } = renderHook( () =>
|
|
210
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
128
211
|
|
|
129
212
|
act( () => {
|
|
130
213
|
result.current.toggleFlip( 'horizontal' );
|
|
@@ -146,7 +229,7 @@ describe( 'useCropperState', () => {
|
|
|
146
229
|
} );
|
|
147
230
|
|
|
148
231
|
it( 'toggleFlip should toggle vertical flip independently', () => {
|
|
149
|
-
const { result } = renderHook( () =>
|
|
232
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
150
233
|
|
|
151
234
|
act( () => {
|
|
152
235
|
result.current.toggleFlip( 'vertical' );
|
|
@@ -159,7 +242,7 @@ describe( 'useCropperState', () => {
|
|
|
159
242
|
} );
|
|
160
243
|
|
|
161
244
|
it( 'should dispatch SET_CROP_RECT via setCropRect', () => {
|
|
162
|
-
const { result } = renderHook( () =>
|
|
245
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
163
246
|
|
|
164
247
|
const rect = { x: 0.1, y: 0.2, width: 0.5, height: 0.6 };
|
|
165
248
|
act( () => {
|
|
@@ -170,7 +253,7 @@ describe( 'useCropperState', () => {
|
|
|
170
253
|
} );
|
|
171
254
|
|
|
172
255
|
it( 'should dispatch APPLY_OPERATION via applyOperation', () => {
|
|
173
|
-
const { result } = renderHook( () =>
|
|
256
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
174
257
|
|
|
175
258
|
act( () => {
|
|
176
259
|
result.current.applyOperation( {
|
|
@@ -183,7 +266,7 @@ describe( 'useCropperState', () => {
|
|
|
183
266
|
} );
|
|
184
267
|
|
|
185
268
|
it( 'should handle APPLY_OPERATION for flip', () => {
|
|
186
|
-
const { result } = renderHook( () =>
|
|
269
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
187
270
|
|
|
188
271
|
act( () => {
|
|
189
272
|
result.current.applyOperation( {
|
|
@@ -197,7 +280,7 @@ describe( 'useCropperState', () => {
|
|
|
197
280
|
} );
|
|
198
281
|
|
|
199
282
|
it( 'should accumulate rotation via APPLY_OPERATION', () => {
|
|
200
|
-
const { result } = renderHook( () =>
|
|
283
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
201
284
|
|
|
202
285
|
act( () => {
|
|
203
286
|
result.current.applyOperation( {
|
|
@@ -217,7 +300,7 @@ describe( 'useCropperState', () => {
|
|
|
217
300
|
} );
|
|
218
301
|
|
|
219
302
|
it( 'should handle APPLY_OPERATION for crop', () => {
|
|
220
|
-
const { result } = renderHook( () =>
|
|
303
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
221
304
|
|
|
222
305
|
const rect = { x: 0.1, y: 0.2, width: 0.5, height: 0.6 };
|
|
223
306
|
act( () => {
|
|
@@ -231,7 +314,7 @@ describe( 'useCropperState', () => {
|
|
|
231
314
|
} );
|
|
232
315
|
|
|
233
316
|
it( 'should handle APPLY_OPERATION for zoom', () => {
|
|
234
|
-
const { result } = renderHook( () =>
|
|
317
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
235
318
|
|
|
236
319
|
act( () => {
|
|
237
320
|
result.current.applyOperation( {
|
|
@@ -244,7 +327,7 @@ describe( 'useCropperState', () => {
|
|
|
244
327
|
} );
|
|
245
328
|
|
|
246
329
|
it( 'should reset to DEFAULT_STATE', () => {
|
|
247
|
-
const { result } = renderHook( () =>
|
|
330
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
248
331
|
|
|
249
332
|
act( () => {
|
|
250
333
|
result.current.setZoom( 5 );
|
|
@@ -261,7 +344,7 @@ describe( 'useCropperState', () => {
|
|
|
261
344
|
} );
|
|
262
345
|
|
|
263
346
|
it( 'should reset to a custom state', () => {
|
|
264
|
-
const { result } = renderHook( () =>
|
|
347
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
265
348
|
|
|
266
349
|
act( () => {
|
|
267
350
|
result.current.setZoom( 5 );
|
|
@@ -277,13 +360,13 @@ describe( 'useCropperState', () => {
|
|
|
277
360
|
|
|
278
361
|
describe( 'isDirty', () => {
|
|
279
362
|
it( 'should be false initially', () => {
|
|
280
|
-
const { result } = renderHook( () =>
|
|
363
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
281
364
|
|
|
282
365
|
expect( result.current.isDirty ).toBe( false );
|
|
283
366
|
} );
|
|
284
367
|
|
|
285
368
|
it( 'should be true after changing crop', () => {
|
|
286
|
-
const { result } = renderHook( () =>
|
|
369
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
287
370
|
|
|
288
371
|
// Zoom in first so crop change isn't clamped to (0,0).
|
|
289
372
|
act( () => {
|
|
@@ -298,7 +381,7 @@ describe( 'useCropperState', () => {
|
|
|
298
381
|
} );
|
|
299
382
|
|
|
300
383
|
it( 'should be true after changing zoom', () => {
|
|
301
|
-
const { result } = renderHook( () =>
|
|
384
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
302
385
|
|
|
303
386
|
act( () => {
|
|
304
387
|
result.current.setZoom( 2 );
|
|
@@ -308,7 +391,7 @@ describe( 'useCropperState', () => {
|
|
|
308
391
|
} );
|
|
309
392
|
|
|
310
393
|
it( 'should be true after changing rotation', () => {
|
|
311
|
-
const { result } = renderHook( () =>
|
|
394
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
312
395
|
|
|
313
396
|
act( () => {
|
|
314
397
|
result.current.setRotation( 45 );
|
|
@@ -318,7 +401,7 @@ describe( 'useCropperState', () => {
|
|
|
318
401
|
} );
|
|
319
402
|
|
|
320
403
|
it( 'should be true after changing flip', () => {
|
|
321
|
-
const { result } = renderHook( () =>
|
|
404
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
322
405
|
|
|
323
406
|
act( () => {
|
|
324
407
|
result.current.setFlip( {
|
|
@@ -331,7 +414,7 @@ describe( 'useCropperState', () => {
|
|
|
331
414
|
} );
|
|
332
415
|
|
|
333
416
|
it( 'should be false after reset', () => {
|
|
334
|
-
const { result } = renderHook( () =>
|
|
417
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
335
418
|
|
|
336
419
|
act( () => {
|
|
337
420
|
result.current.setZoom( 5 );
|
|
@@ -347,7 +430,7 @@ describe( 'useCropperState', () => {
|
|
|
347
430
|
} );
|
|
348
431
|
|
|
349
432
|
it( 'should be false after manually resetting fields to initial values', () => {
|
|
350
|
-
const { result } = renderHook( () =>
|
|
433
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
351
434
|
|
|
352
435
|
// Zoom in and pan.
|
|
353
436
|
act( () => {
|
|
@@ -377,7 +460,7 @@ describe( 'useCropperState', () => {
|
|
|
377
460
|
// non-square images. The initial snapshot must be refreshed so
|
|
378
461
|
// isDirty still reports false at a "clean" post-load state.
|
|
379
462
|
it( 'should be false after setImage on a non-square image', () => {
|
|
380
|
-
const { result } = renderHook( () =>
|
|
463
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
381
464
|
|
|
382
465
|
act( () => {
|
|
383
466
|
result.current.setImage( {
|
|
@@ -390,12 +473,36 @@ describe( 'useCropperState', () => {
|
|
|
390
473
|
expect( result.current.isDirty ).toBe( false );
|
|
391
474
|
} );
|
|
392
475
|
|
|
476
|
+
it( 'does not reset the clean baseline when the same image is reported again', () => {
|
|
477
|
+
const image = {
|
|
478
|
+
src: 'test.jpg',
|
|
479
|
+
naturalWidth: 1600,
|
|
480
|
+
naturalHeight: 900,
|
|
481
|
+
};
|
|
482
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
483
|
+
|
|
484
|
+
act( () => {
|
|
485
|
+
result.current.setImage( image );
|
|
486
|
+
} );
|
|
487
|
+
act( () => {
|
|
488
|
+
result.current.setRotation( 45 );
|
|
489
|
+
} );
|
|
490
|
+
expect( result.current.isDirty ).toBe( true );
|
|
491
|
+
|
|
492
|
+
act( () => {
|
|
493
|
+
result.current.setImage( image );
|
|
494
|
+
} );
|
|
495
|
+
|
|
496
|
+
expect( result.current.isDirty ).toBe( true );
|
|
497
|
+
expect( result.current.state.rotation ).toBe( 45 );
|
|
498
|
+
} );
|
|
499
|
+
|
|
393
500
|
// Regression: reset() on a loaded image must produce a "clean"
|
|
394
501
|
// snapshot that matches what the reducer actually stored
|
|
395
502
|
// (preserving the image, re-enforcing containment). Otherwise
|
|
396
503
|
// isDirty reports true right after reset.
|
|
397
504
|
it( 'should be false after reset on a loaded image', () => {
|
|
398
|
-
const { result } = renderHook( () =>
|
|
505
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
399
506
|
|
|
400
507
|
act( () => {
|
|
401
508
|
result.current.setImage( {
|
|
@@ -424,7 +531,7 @@ describe( 'useCropperState', () => {
|
|
|
424
531
|
// For these tests, we need a loaded image so the reducer can
|
|
425
532
|
// compute minZoomForCover and restrict position.
|
|
426
533
|
function setupWithImage() {
|
|
427
|
-
const view = renderHook( () =>
|
|
534
|
+
const view = renderHook( () => useCropperReducer() );
|
|
428
535
|
act( () => {
|
|
429
536
|
view.result.current.setImage( {
|
|
430
537
|
src: 'test.jpg',
|
|
@@ -667,7 +774,7 @@ describe( 'useCropperState', () => {
|
|
|
667
774
|
|
|
668
775
|
describe( 'SETTLE_CROP', () => {
|
|
669
776
|
function setupWithImage() {
|
|
670
|
-
const view = renderHook( () =>
|
|
777
|
+
const view = renderHook( () => useCropperReducer() );
|
|
671
778
|
act( () => {
|
|
672
779
|
view.result.current.setImage( {
|
|
673
780
|
src: 'test.jpg',
|
|
@@ -768,7 +875,7 @@ describe( 'useCropperState', () => {
|
|
|
768
875
|
|
|
769
876
|
describe( 'SNAP_ROTATE_90', () => {
|
|
770
877
|
function setupWithImage() {
|
|
771
|
-
const view = renderHook( () =>
|
|
878
|
+
const view = renderHook( () => useCropperReducer() );
|
|
772
879
|
act( () => {
|
|
773
880
|
view.result.current.setImage( {
|
|
774
881
|
src: 'test.jpg',
|
|
@@ -862,260 +969,9 @@ describe( 'useCropperState', () => {
|
|
|
862
969
|
} );
|
|
863
970
|
} );
|
|
864
971
|
|
|
865
|
-
describe( 'undo/redo history', () => {
|
|
866
|
-
const DEBOUNCE_MS = 300;
|
|
867
|
-
|
|
868
|
-
beforeEach( () => jest.useFakeTimers() );
|
|
869
|
-
afterEach( () => jest.useRealTimers() );
|
|
870
|
-
|
|
871
|
-
// --- debounce batching ---
|
|
872
|
-
|
|
873
|
-
it( 'does not create a history entry before the debounce fires', () => {
|
|
874
|
-
const { result } = renderHook( () => useCropperState() );
|
|
875
|
-
act( () => {
|
|
876
|
-
result.current.setZoom( 3 );
|
|
877
|
-
} );
|
|
878
|
-
expect( result.current.hasUndo ).toBe( false );
|
|
879
|
-
} );
|
|
880
|
-
|
|
881
|
-
it( 'creates one history entry after the debounce settles', () => {
|
|
882
|
-
const { result } = renderHook( () => useCropperState() );
|
|
883
|
-
act( () => {
|
|
884
|
-
result.current.setZoom( 2 );
|
|
885
|
-
result.current.setZoom( 3 );
|
|
886
|
-
result.current.setZoom( 4 );
|
|
887
|
-
} );
|
|
888
|
-
act( () => jest.advanceTimersByTime( DEBOUNCE_MS ) );
|
|
889
|
-
expect( result.current.hasUndo ).toBe( true );
|
|
890
|
-
act( () => result.current.undo() );
|
|
891
|
-
expect( result.current.state.zoom ).toBe( 1 );
|
|
892
|
-
expect( result.current.hasUndo ).toBe( false );
|
|
893
|
-
} );
|
|
894
|
-
|
|
895
|
-
it( 'does not create a history entry for a no-op round trip', () => {
|
|
896
|
-
const { result } = renderHook( () => useCropperState() );
|
|
897
|
-
act( () => result.current.setZoom( 2 ) );
|
|
898
|
-
act( () => result.current.setZoom( 1 ) );
|
|
899
|
-
act( () => jest.advanceTimersByTime( DEBOUNCE_MS ) );
|
|
900
|
-
expect( result.current.hasUndo ).toBe( false );
|
|
901
|
-
} );
|
|
902
|
-
|
|
903
|
-
// --- commitHistory flush ---
|
|
904
|
-
|
|
905
|
-
it( 'commitHistory flushes the pending entry immediately', () => {
|
|
906
|
-
const { result } = renderHook( () => useCropperState() );
|
|
907
|
-
// Separate act() calls are required: setZoom dispatches a reducer
|
|
908
|
-
// action that React processes when act() flushes. If commitHistory
|
|
909
|
-
// runs in the same act(), stateRef.current hasn't updated yet and
|
|
910
|
-
// the flush sees no change.
|
|
911
|
-
act( () => result.current.setZoom( 3 ) );
|
|
912
|
-
act( () => result.current.commitHistory() );
|
|
913
|
-
expect( result.current.hasUndo ).toBe( true );
|
|
914
|
-
} );
|
|
915
|
-
|
|
916
|
-
it( 'commitHistory is a no-op when state has not changed', () => {
|
|
917
|
-
const { result } = renderHook( () => useCropperState() );
|
|
918
|
-
act( () => {
|
|
919
|
-
result.current.commitHistory();
|
|
920
|
-
} );
|
|
921
|
-
expect( result.current.hasUndo ).toBe( false );
|
|
922
|
-
} );
|
|
923
|
-
|
|
924
|
-
// --- discrete actions ---
|
|
925
|
-
|
|
926
|
-
it( 'toggleFlip creates a history entry immediately', () => {
|
|
927
|
-
const { result } = renderHook( () => useCropperState() );
|
|
928
|
-
act( () => {
|
|
929
|
-
result.current.toggleFlip( 'horizontal' );
|
|
930
|
-
} );
|
|
931
|
-
expect( result.current.hasUndo ).toBe( true );
|
|
932
|
-
} );
|
|
933
|
-
|
|
934
|
-
it( 'setFlip creates a history entry immediately', () => {
|
|
935
|
-
const { result } = renderHook( () => useCropperState() );
|
|
936
|
-
act( () => {
|
|
937
|
-
result.current.setFlip( { horizontal: true, vertical: false } );
|
|
938
|
-
} );
|
|
939
|
-
expect( result.current.hasUndo ).toBe( true );
|
|
940
|
-
} );
|
|
941
|
-
|
|
942
|
-
it( 'snapRotate90 creates a history entry immediately', () => {
|
|
943
|
-
const { result } = renderHook( () => useCropperState() );
|
|
944
|
-
act( () => {
|
|
945
|
-
result.current.snapRotate90( 1 );
|
|
946
|
-
} );
|
|
947
|
-
expect( result.current.hasUndo ).toBe( true );
|
|
948
|
-
} );
|
|
949
|
-
|
|
950
|
-
it( 'applyOperation creates a history entry immediately', () => {
|
|
951
|
-
const { result } = renderHook( () => useCropperState() );
|
|
952
|
-
act( () => {
|
|
953
|
-
result.current.applyOperation( { type: 'zoom', factor: 3 } );
|
|
954
|
-
} );
|
|
955
|
-
expect( result.current.hasUndo ).toBe( true );
|
|
956
|
-
} );
|
|
957
|
-
|
|
958
|
-
// --- undo / redo round-trip ---
|
|
959
|
-
|
|
960
|
-
it( 'undo restores the previous state and enables redo', () => {
|
|
961
|
-
const { result } = renderHook( () => useCropperState() );
|
|
962
|
-
act( () => result.current.setZoom( 3 ) );
|
|
963
|
-
act( () => result.current.commitHistory() );
|
|
964
|
-
act( () => result.current.undo() );
|
|
965
|
-
expect( result.current.state.zoom ).toBe( 1 );
|
|
966
|
-
expect( result.current.hasUndo ).toBe( false );
|
|
967
|
-
expect( result.current.hasRedo ).toBe( true );
|
|
968
|
-
} );
|
|
969
|
-
|
|
970
|
-
it( 'redo re-applies the undone state', () => {
|
|
971
|
-
const { result } = renderHook( () => useCropperState() );
|
|
972
|
-
act( () => result.current.setZoom( 3 ) );
|
|
973
|
-
act( () => result.current.commitHistory() );
|
|
974
|
-
act( () => result.current.undo() );
|
|
975
|
-
act( () => result.current.redo() );
|
|
976
|
-
expect( result.current.state.zoom ).toBe( 3 );
|
|
977
|
-
expect( result.current.hasRedo ).toBe( false );
|
|
978
|
-
} );
|
|
979
|
-
|
|
980
|
-
it( 'undo after redo returns to a clean initial state', () => {
|
|
981
|
-
const { result } = renderHook( () => useCropperState() );
|
|
982
|
-
act( () => {
|
|
983
|
-
result.current.setImage( {
|
|
984
|
-
src: 'test.jpg',
|
|
985
|
-
naturalWidth: 1000,
|
|
986
|
-
naturalHeight: 500,
|
|
987
|
-
} );
|
|
988
|
-
} );
|
|
989
|
-
act( () => result.current.snapRotate90( 1 ) );
|
|
990
|
-
expect( result.current.isDirty ).toBe( true );
|
|
991
|
-
|
|
992
|
-
act( () => result.current.undo() );
|
|
993
|
-
expect( result.current.isDirty ).toBe( false );
|
|
994
|
-
|
|
995
|
-
act( () => result.current.redo() );
|
|
996
|
-
expect( result.current.isDirty ).toBe( true );
|
|
997
|
-
|
|
998
|
-
act( () => result.current.undo() );
|
|
999
|
-
expect( result.current.state.rotation ).toBe( 0 );
|
|
1000
|
-
expect( result.current.isDirty ).toBe( false );
|
|
1001
|
-
} );
|
|
1002
|
-
|
|
1003
|
-
// --- undo/redo with a pending gesture ---
|
|
1004
|
-
|
|
1005
|
-
it( 'undo flushes a pending gesture before undoing', () => {
|
|
1006
|
-
const { result } = renderHook( () => useCropperState() );
|
|
1007
|
-
// Commit one step, then start a second change without committing.
|
|
1008
|
-
act( () => result.current.setZoom( 3 ) );
|
|
1009
|
-
act( () => result.current.commitHistory() );
|
|
1010
|
-
act( () => result.current.setZoom( 5 ) );
|
|
1011
|
-
// Undo should flush zoom=5 first, then undo back to zoom=3.
|
|
1012
|
-
act( () => result.current.undo() );
|
|
1013
|
-
expect( result.current.state.zoom ).toBe( 3 );
|
|
1014
|
-
expect( result.current.hasRedo ).toBe( true );
|
|
1015
|
-
} );
|
|
1016
|
-
|
|
1017
|
-
it( 'undo with no prior history flushes and undoes the pending gesture', () => {
|
|
1018
|
-
const { result } = renderHook( () => useCropperState() );
|
|
1019
|
-
act( () => {
|
|
1020
|
-
result.current.setZoom( 3 );
|
|
1021
|
-
} );
|
|
1022
|
-
// Undo before the debounce fires — flush saves the change, then
|
|
1023
|
-
// immediately undoes it.
|
|
1024
|
-
act( () => result.current.undo() );
|
|
1025
|
-
expect( result.current.state.zoom ).toBe( 1 );
|
|
1026
|
-
} );
|
|
1027
|
-
|
|
1028
|
-
// --- setImage and reset do not pollute history ---
|
|
1029
|
-
|
|
1030
|
-
it( 'setImage does not create a history entry', () => {
|
|
1031
|
-
const { result } = renderHook( () => useCropperState() );
|
|
1032
|
-
act( () => {
|
|
1033
|
-
result.current.setImage( {
|
|
1034
|
-
src: 'test.jpg',
|
|
1035
|
-
naturalWidth: 100,
|
|
1036
|
-
naturalHeight: 100,
|
|
1037
|
-
} );
|
|
1038
|
-
} );
|
|
1039
|
-
act( () => jest.advanceTimersByTime( DEBOUNCE_MS ) );
|
|
1040
|
-
expect( result.current.hasUndo ).toBe( false );
|
|
1041
|
-
} );
|
|
1042
|
-
|
|
1043
|
-
it( 'reset is undoable and restores a clean current state', () => {
|
|
1044
|
-
const { result } = renderHook( () => useCropperState() );
|
|
1045
|
-
act( () => result.current.setZoom( 3 ) );
|
|
1046
|
-
act( () => result.current.commitHistory() );
|
|
1047
|
-
expect( result.current.hasUndo ).toBe( true );
|
|
1048
|
-
act( () => result.current.reset() );
|
|
1049
|
-
expect( result.current.state.zoom ).toBe( 1 );
|
|
1050
|
-
expect( result.current.isDirty ).toBe( false );
|
|
1051
|
-
expect( result.current.hasUndo ).toBe( true );
|
|
1052
|
-
act( () => result.current.undo() );
|
|
1053
|
-
expect( result.current.state.zoom ).toBe( 3 );
|
|
1054
|
-
} );
|
|
1055
|
-
|
|
1056
|
-
it( 'reset does not create a history entry when already clean', () => {
|
|
1057
|
-
const { result } = renderHook( () => useCropperState() );
|
|
1058
|
-
act( () => result.current.reset() );
|
|
1059
|
-
expect( result.current.hasUndo ).toBe( false );
|
|
1060
|
-
} );
|
|
1061
|
-
|
|
1062
|
-
// --- discrete action flushes a concurrent continuous gesture ---
|
|
1063
|
-
|
|
1064
|
-
it( 'discrete action saves the pending gesture as a separate undo step', () => {
|
|
1065
|
-
const { result } = renderHook( () => useCropperState() );
|
|
1066
|
-
act( () => {
|
|
1067
|
-
result.current.setZoom( 3 );
|
|
1068
|
-
} );
|
|
1069
|
-
act( () => {
|
|
1070
|
-
result.current.setFlip( { horizontal: true, vertical: false } );
|
|
1071
|
-
} );
|
|
1072
|
-
// First undo removes the flip.
|
|
1073
|
-
act( () => result.current.undo() );
|
|
1074
|
-
expect( result.current.state.flip.horizontal ).toBe( false );
|
|
1075
|
-
expect( result.current.state.zoom ).toBe( 3 );
|
|
1076
|
-
// Second undo removes the zoom gesture.
|
|
1077
|
-
act( () => result.current.undo() );
|
|
1078
|
-
expect( result.current.state.zoom ).toBe( 1 );
|
|
1079
|
-
} );
|
|
1080
|
-
|
|
1081
|
-
it( 'settleCrop is grouped into the resize undo step', () => {
|
|
1082
|
-
const { result } = renderHook( () => useCropperState() );
|
|
1083
|
-
act( () => {
|
|
1084
|
-
result.current.setImage( {
|
|
1085
|
-
src: 'test.jpg',
|
|
1086
|
-
naturalWidth: 1000,
|
|
1087
|
-
naturalHeight: 500,
|
|
1088
|
-
} );
|
|
1089
|
-
} );
|
|
1090
|
-
const initialCropRect = result.current.state.cropRect;
|
|
1091
|
-
|
|
1092
|
-
act( () => {
|
|
1093
|
-
result.current.setCropRect( {
|
|
1094
|
-
x: 0.25,
|
|
1095
|
-
y: 0.25,
|
|
1096
|
-
width: 0.5,
|
|
1097
|
-
height: 0.5,
|
|
1098
|
-
} );
|
|
1099
|
-
} );
|
|
1100
|
-
act( () => result.current.settleCrop() );
|
|
1101
|
-
const settledState = result.current.state;
|
|
1102
|
-
|
|
1103
|
-
expect( result.current.hasUndo ).toBe( true );
|
|
1104
|
-
act( () => result.current.undo() );
|
|
1105
|
-
expect( result.current.state.cropRect ).toEqual( initialCropRect );
|
|
1106
|
-
expect( result.current.hasUndo ).toBe( false );
|
|
1107
|
-
|
|
1108
|
-
act( () => result.current.redo() );
|
|
1109
|
-
expect( result.current.state.cropRect ).toEqual(
|
|
1110
|
-
settledState.cropRect
|
|
1111
|
-
);
|
|
1112
|
-
expect( result.current.state.zoom ).toBe( settledState.zoom );
|
|
1113
|
-
} );
|
|
1114
|
-
} );
|
|
1115
|
-
|
|
1116
972
|
describe( 'public controller contract', () => {
|
|
1117
973
|
it( 'does not expose the raw reducer dispatch', () => {
|
|
1118
|
-
const { result } = renderHook( () =>
|
|
974
|
+
const { result } = renderHook( () => useCropperReducer() );
|
|
1119
975
|
|
|
1120
976
|
expect( '__dispatch' in result.current ).toBe( false );
|
|
1121
977
|
} );
|
|
@@ -1124,7 +980,7 @@ describe( 'useCropperState', () => {
|
|
|
1124
980
|
describe( 'isStateDirty coverage guard', () => {
|
|
1125
981
|
// This test ensures that isStateDirty checks ALL fields in CropperState.
|
|
1126
982
|
// If you add a field to CropperState/DEFAULT_STATE and this test fails,
|
|
1127
|
-
// you MUST also update isStateDirty() in use-cropper-
|
|
983
|
+
// you MUST also update isStateDirty() in use-cropper-reducer.ts.
|
|
1128
984
|
it( 'should track all CropperState fields', () => {
|
|
1129
985
|
// Flatten DEFAULT_STATE keys into dot-notation paths.
|
|
1130
986
|
// e.g., { pan: { x: 0, y: 0 } } → ['crop.x', 'crop.y']
|