@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
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { act, renderHook } from '@testing-library/react';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import { useMediaEditorState } from '../use-media-editor-state';
|
|
10
|
+
|
|
11
|
+
const IMAGE = {
|
|
12
|
+
src: 'test.jpg',
|
|
13
|
+
naturalWidth: 1200,
|
|
14
|
+
naturalHeight: 600,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function setupHook( visualSize = { width: 600, height: 400 } ) {
|
|
18
|
+
const view = renderHook( () =>
|
|
19
|
+
useMediaEditorState( { cropper: { image: IMAGE } } )
|
|
20
|
+
);
|
|
21
|
+
act( () => {
|
|
22
|
+
view.result.current.setVisualSize( visualSize );
|
|
23
|
+
} );
|
|
24
|
+
return view;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe( 'useMediaEditorState', () => {
|
|
28
|
+
describe( 'initial state', () => {
|
|
29
|
+
it( 'starts with default cropOptions', () => {
|
|
30
|
+
const { result } = renderHook( () => useMediaEditorState() );
|
|
31
|
+
|
|
32
|
+
expect( result.current.cropOptions.aspectRatioValue ).toBe( '0' );
|
|
33
|
+
} );
|
|
34
|
+
|
|
35
|
+
it( 'merges initial cropper and cropOptions overrides', () => {
|
|
36
|
+
const { result } = renderHook( () =>
|
|
37
|
+
useMediaEditorState( {
|
|
38
|
+
cropper: { image: IMAGE },
|
|
39
|
+
cropOptions: { aspectRatioValue: '1' },
|
|
40
|
+
} )
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
expect( result.current.state.image ).toEqual( IMAGE );
|
|
44
|
+
expect( result.current.cropOptions ).toEqual( {
|
|
45
|
+
aspectRatioValue: '1',
|
|
46
|
+
} );
|
|
47
|
+
} );
|
|
48
|
+
|
|
49
|
+
it( 'starts with isDirty=false and no undo / redo', () => {
|
|
50
|
+
const { result } = setupHook();
|
|
51
|
+
|
|
52
|
+
expect( result.current.isDirty ).toBe( false );
|
|
53
|
+
expect( result.current.isCropperDirty ).toBe( false );
|
|
54
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
55
|
+
expect( result.current.hasRedo ).toBe( false );
|
|
56
|
+
} );
|
|
57
|
+
} );
|
|
58
|
+
|
|
59
|
+
describe( 'history — undo / redo across slices', () => {
|
|
60
|
+
it( 'records the pre-state of each action and undoes it', () => {
|
|
61
|
+
const { result } = setupHook();
|
|
62
|
+
|
|
63
|
+
act( () => result.current.setRotation( 45 ) );
|
|
64
|
+
|
|
65
|
+
expect( result.current.state.rotation ).toBe( 45 );
|
|
66
|
+
expect( result.current.hasUndo ).toBe( true );
|
|
67
|
+
|
|
68
|
+
act( () => result.current.undo() );
|
|
69
|
+
|
|
70
|
+
expect( result.current.state.rotation ).toBe( 0 );
|
|
71
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
72
|
+
expect( result.current.hasRedo ).toBe( true );
|
|
73
|
+
} );
|
|
74
|
+
|
|
75
|
+
it( 'redo re-applies an undone action', () => {
|
|
76
|
+
const { result } = setupHook();
|
|
77
|
+
|
|
78
|
+
act( () => result.current.setRotation( 45 ) );
|
|
79
|
+
act( () => result.current.undo() );
|
|
80
|
+
act( () => result.current.redo() );
|
|
81
|
+
|
|
82
|
+
expect( result.current.state.rotation ).toBe( 45 );
|
|
83
|
+
} );
|
|
84
|
+
|
|
85
|
+
it( 'redo stack clears on a new action after undo', () => {
|
|
86
|
+
const { result } = setupHook();
|
|
87
|
+
|
|
88
|
+
act( () => result.current.setRotation( 45 ) );
|
|
89
|
+
act( () => result.current.undo() );
|
|
90
|
+
act( () => result.current.setRotation( 90 ) );
|
|
91
|
+
|
|
92
|
+
expect( result.current.hasRedo ).toBe( false );
|
|
93
|
+
} );
|
|
94
|
+
|
|
95
|
+
it( 'no-op actions do not record history entries', () => {
|
|
96
|
+
const { result } = setupHook();
|
|
97
|
+
|
|
98
|
+
act( () => result.current.setAspectRatioValue( '0' ) );
|
|
99
|
+
|
|
100
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
101
|
+
} );
|
|
102
|
+
|
|
103
|
+
it( 'a single undo restores both cropOptions and cropRect after aspect-ratio change', () => {
|
|
104
|
+
const { result } = setupHook();
|
|
105
|
+
const initialCropRect = result.current.state.cropRect;
|
|
106
|
+
const initialPreset = result.current.cropOptions.aspectRatioValue;
|
|
107
|
+
|
|
108
|
+
act( () => result.current.setAspectRatioValue( '1' ) ); // Square
|
|
109
|
+
|
|
110
|
+
// Both slices changed atomically: preset key and cropRect.
|
|
111
|
+
expect( result.current.cropOptions.aspectRatioValue ).toBe( '1' );
|
|
112
|
+
expect( result.current.state.cropRect ).not.toEqual(
|
|
113
|
+
initialCropRect
|
|
114
|
+
);
|
|
115
|
+
expect( result.current.isCropperDirty ).toBe( true );
|
|
116
|
+
// Only ONE undo entry — atomic transaction.
|
|
117
|
+
expect( result.current.hasUndo ).toBe( true );
|
|
118
|
+
|
|
119
|
+
act( () => result.current.undo() );
|
|
120
|
+
|
|
121
|
+
expect( result.current.cropOptions.aspectRatioValue ).toBe(
|
|
122
|
+
initialPreset
|
|
123
|
+
);
|
|
124
|
+
expect( result.current.state.cropRect ).toEqual( initialCropRect );
|
|
125
|
+
} );
|
|
126
|
+
} );
|
|
127
|
+
|
|
128
|
+
describe( 'gestures — coalesce many changes into one undo entry', () => {
|
|
129
|
+
it( 'coalesces all changes between beginGesture and endGesture', () => {
|
|
130
|
+
const { result } = setupHook();
|
|
131
|
+
|
|
132
|
+
act( () => result.current.beginGesture() );
|
|
133
|
+
act( () => result.current.setRotation( 10 ) );
|
|
134
|
+
act( () => result.current.setRotation( 20 ) );
|
|
135
|
+
act( () => result.current.setRotation( 30 ) );
|
|
136
|
+
act( () => result.current.endGesture() );
|
|
137
|
+
|
|
138
|
+
expect( result.current.state.rotation ).toBe( 30 );
|
|
139
|
+
expect( result.current.hasUndo ).toBe( true );
|
|
140
|
+
|
|
141
|
+
// One undo restores all the way to the pre-gesture state.
|
|
142
|
+
act( () => result.current.undo() );
|
|
143
|
+
expect( result.current.state.rotation ).toBe( 0 );
|
|
144
|
+
} );
|
|
145
|
+
|
|
146
|
+
it( 'an empty gesture (no changes) records no history', () => {
|
|
147
|
+
const { result } = setupHook();
|
|
148
|
+
|
|
149
|
+
act( () => result.current.beginGesture() );
|
|
150
|
+
act( () => result.current.endGesture() );
|
|
151
|
+
|
|
152
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
153
|
+
} );
|
|
154
|
+
|
|
155
|
+
it( 'undo during an open gesture flushes the gesture first', () => {
|
|
156
|
+
const { result } = setupHook();
|
|
157
|
+
|
|
158
|
+
act( () => result.current.beginGesture() );
|
|
159
|
+
act( () => result.current.setRotation( 45 ) );
|
|
160
|
+
// Undo without endGesture should still flush the in-flight change.
|
|
161
|
+
act( () => result.current.undo() );
|
|
162
|
+
|
|
163
|
+
expect( result.current.state.rotation ).toBe( 0 );
|
|
164
|
+
expect( result.current.hasRedo ).toBe( true );
|
|
165
|
+
} );
|
|
166
|
+
|
|
167
|
+
it( 'keeps the original gesture snapshot if beginGesture is called repeatedly', () => {
|
|
168
|
+
const { result } = setupHook();
|
|
169
|
+
|
|
170
|
+
act( () => {
|
|
171
|
+
result.current.beginGesture();
|
|
172
|
+
result.current.setRotation( 10 );
|
|
173
|
+
result.current.beginGesture();
|
|
174
|
+
result.current.setRotation( 20 );
|
|
175
|
+
result.current.endGesture();
|
|
176
|
+
} );
|
|
177
|
+
|
|
178
|
+
expect( result.current.state.rotation ).toBe( 20 );
|
|
179
|
+
|
|
180
|
+
act( () => result.current.undo() );
|
|
181
|
+
|
|
182
|
+
expect( result.current.state.rotation ).toBe( 0 );
|
|
183
|
+
} );
|
|
184
|
+
|
|
185
|
+
it( 'does not record a gesture that returns to its starting state', () => {
|
|
186
|
+
const { result } = setupHook();
|
|
187
|
+
|
|
188
|
+
act( () => {
|
|
189
|
+
result.current.beginGesture();
|
|
190
|
+
result.current.setRotation( 10 );
|
|
191
|
+
result.current.setRotation( 0 );
|
|
192
|
+
result.current.endGesture();
|
|
193
|
+
} );
|
|
194
|
+
|
|
195
|
+
expect( result.current.state.rotation ).toBe( 0 );
|
|
196
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
197
|
+
} );
|
|
198
|
+
} );
|
|
199
|
+
|
|
200
|
+
describe( 'SET_ASPECT_RATIO_VALUE atomicity', () => {
|
|
201
|
+
it( 'reshapes cropRect when a fixed ratio is selected with visualSize > 0', () => {
|
|
202
|
+
const { result } = setupHook( { width: 600, height: 400 } );
|
|
203
|
+
|
|
204
|
+
act( () => result.current.setAspectRatioValue( '1' ) ); // Square
|
|
205
|
+
|
|
206
|
+
// At visualSize 600x400, the inscribed square has w/h such that
|
|
207
|
+
// pixel ratio = 1. normalizedRatio = 1 * 400 / 600 = 0.666...
|
|
208
|
+
// So normalized w = 0.666..., h = 1.
|
|
209
|
+
expect( result.current.state.cropRect.width ).toBeCloseTo(
|
|
210
|
+
2 / 3,
|
|
211
|
+
5
|
|
212
|
+
);
|
|
213
|
+
expect( result.current.state.cropRect.height ).toBeCloseTo( 1, 5 );
|
|
214
|
+
} );
|
|
215
|
+
|
|
216
|
+
it( 'Free leaves the cropRect alone', () => {
|
|
217
|
+
const { result } = setupHook();
|
|
218
|
+
const original = result.current.state.cropRect;
|
|
219
|
+
|
|
220
|
+
// User-picked custom crop first.
|
|
221
|
+
act( () =>
|
|
222
|
+
result.current.setCropRect( {
|
|
223
|
+
x: 0.1,
|
|
224
|
+
y: 0.1,
|
|
225
|
+
width: 0.5,
|
|
226
|
+
height: 0.5,
|
|
227
|
+
} )
|
|
228
|
+
);
|
|
229
|
+
const beforeFree = result.current.state.cropRect;
|
|
230
|
+
|
|
231
|
+
// Switch to Free — cropRect should NOT snap back to anything.
|
|
232
|
+
act( () => result.current.setAspectRatioValue( '0' ) );
|
|
233
|
+
|
|
234
|
+
expect( result.current.state.cropRect ).toEqual( beforeFree );
|
|
235
|
+
expect( original ).not.toEqual( beforeFree );
|
|
236
|
+
} );
|
|
237
|
+
|
|
238
|
+
it( 'resolves Original from the loaded image', () => {
|
|
239
|
+
const { result } = setupHook();
|
|
240
|
+
|
|
241
|
+
act( () => result.current.setAspectRatioValue( '-1' ) ); // Original
|
|
242
|
+
|
|
243
|
+
// Image is 1200x600 = ratio 2. inscribedRect at visualSize 600x400:
|
|
244
|
+
// normalizedRatio = 2 * 400 / 600 = 1.333... > 1, so h = 1/1.333 = 0.75.
|
|
245
|
+
expect( result.current.state.cropRect.width ).toBeCloseTo( 1, 5 );
|
|
246
|
+
expect( result.current.state.cropRect.height ).toBeCloseTo(
|
|
247
|
+
0.75,
|
|
248
|
+
5
|
|
249
|
+
);
|
|
250
|
+
} );
|
|
251
|
+
|
|
252
|
+
it( 'groups cropper reset and cropOptions reset in one undo step', () => {
|
|
253
|
+
const { result } = setupHook();
|
|
254
|
+
|
|
255
|
+
act( () => result.current.setAspectRatioValue( '1' ) );
|
|
256
|
+
act( () => result.current.setRotation( 45 ) );
|
|
257
|
+
|
|
258
|
+
act( () => {
|
|
259
|
+
result.current.beginGesture();
|
|
260
|
+
result.current.reset();
|
|
261
|
+
result.current.resetCropOptions();
|
|
262
|
+
result.current.endGesture();
|
|
263
|
+
} );
|
|
264
|
+
|
|
265
|
+
expect( result.current.state.rotation ).toBe( 0 );
|
|
266
|
+
expect( result.current.cropOptions ).toEqual( {
|
|
267
|
+
aspectRatioValue: '0',
|
|
268
|
+
} );
|
|
269
|
+
|
|
270
|
+
act( () => result.current.undo() );
|
|
271
|
+
|
|
272
|
+
expect( result.current.state.rotation ).toBe( 45 );
|
|
273
|
+
expect( result.current.cropOptions ).toEqual( {
|
|
274
|
+
aspectRatioValue: '1',
|
|
275
|
+
} );
|
|
276
|
+
} );
|
|
277
|
+
} );
|
|
278
|
+
|
|
279
|
+
describe( 'viewport reshape is silent', () => {
|
|
280
|
+
it( 'adjustCropRectForViewport does NOT record an undo entry', () => {
|
|
281
|
+
const { result } = setupHook();
|
|
282
|
+
|
|
283
|
+
act( () =>
|
|
284
|
+
result.current.adjustCropRectForViewport( {
|
|
285
|
+
x: 0,
|
|
286
|
+
y: 0,
|
|
287
|
+
width: 0.5,
|
|
288
|
+
height: 0.5,
|
|
289
|
+
} )
|
|
290
|
+
);
|
|
291
|
+
|
|
292
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
293
|
+
} );
|
|
294
|
+
} );
|
|
295
|
+
|
|
296
|
+
describe( 'setImage', () => {
|
|
297
|
+
it( 'clears history and resets isDirty', () => {
|
|
298
|
+
const { result } = setupHook();
|
|
299
|
+
|
|
300
|
+
act( () => result.current.setRotation( 45 ) );
|
|
301
|
+
expect( result.current.hasUndo ).toBe( true );
|
|
302
|
+
expect( result.current.isDirty ).toBe( true );
|
|
303
|
+
|
|
304
|
+
act( () =>
|
|
305
|
+
result.current.setImage( {
|
|
306
|
+
src: 'other.jpg',
|
|
307
|
+
naturalWidth: 800,
|
|
308
|
+
naturalHeight: 800,
|
|
309
|
+
} )
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
313
|
+
expect( result.current.hasRedo ).toBe( false );
|
|
314
|
+
expect( result.current.isDirty ).toBe( false );
|
|
315
|
+
} );
|
|
316
|
+
|
|
317
|
+
it( 'does not clear history when the same image is reported again', () => {
|
|
318
|
+
const { result } = setupHook();
|
|
319
|
+
|
|
320
|
+
act( () => result.current.setRotation( 45 ) );
|
|
321
|
+
expect( result.current.hasUndo ).toBe( true );
|
|
322
|
+
expect( result.current.isCropperDirty ).toBe( true );
|
|
323
|
+
|
|
324
|
+
act( () => result.current.setImage( IMAGE ) );
|
|
325
|
+
|
|
326
|
+
expect( result.current.hasUndo ).toBe( true );
|
|
327
|
+
expect( result.current.isCropperDirty ).toBe( true );
|
|
328
|
+
} );
|
|
329
|
+
} );
|
|
330
|
+
} );
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type {
|
|
5
|
+
CropperAction,
|
|
6
|
+
CropperState,
|
|
7
|
+
NormalizedRect,
|
|
8
|
+
Size,
|
|
9
|
+
} from '../image-editor';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Sidebar control state for the cropper. Lives in the composite store
|
|
13
|
+
* so it participates in the same undo history as cropper geometry.
|
|
14
|
+
*/
|
|
15
|
+
export interface CropOptionsSlice {
|
|
16
|
+
/**
|
|
17
|
+
* Selected aspect-ratio preset as a string (round-trippable through
|
|
18
|
+
* `<SelectControl>`). `'0'` = Free, `'-1'` = Original, any positive
|
|
19
|
+
* decimal = fixed ratio.
|
|
20
|
+
*/
|
|
21
|
+
aspectRatioValue: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Default values for the crop-options slice. Mirrors the initial state
|
|
26
|
+
* used by the sidebar before the user has touched anything.
|
|
27
|
+
*/
|
|
28
|
+
export const DEFAULT_CROP_OPTIONS: CropOptionsSlice = {
|
|
29
|
+
aspectRatioValue: '0',
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The full composite state for the media editor — cropper geometry
|
|
34
|
+
* plus the sidebar control slice. Future feature slices (text
|
|
35
|
+
* overlays, filters, …) extend this object.
|
|
36
|
+
*
|
|
37
|
+
* Every undo/redo snapshot is a full `MediaEditorState`. Slices stay
|
|
38
|
+
* referentially equal across actions that don't touch them.
|
|
39
|
+
*/
|
|
40
|
+
export interface MediaEditorState {
|
|
41
|
+
cropper: CropperState;
|
|
42
|
+
cropOptions: CropOptionsSlice;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Composite reducer action union.
|
|
47
|
+
*
|
|
48
|
+
* Cropper geometry actions are wrapped in a `CROPPER` envelope and
|
|
49
|
+
* delegated to the pure `cropperReducer`. Sidebar and history
|
|
50
|
+
* concerns are first-class actions on this reducer.
|
|
51
|
+
*/
|
|
52
|
+
export type MediaEditorAction =
|
|
53
|
+
/**
|
|
54
|
+
* Delegate to the underlying cropper reducer. Used for every
|
|
55
|
+
* geometry transition (pan / zoom / rotation / flip / cropRect /
|
|
56
|
+
* settle / etc.).
|
|
57
|
+
*/
|
|
58
|
+
| {
|
|
59
|
+
type: 'CROPPER';
|
|
60
|
+
action: CropperAction;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Set the aspect-ratio preset. Atomically updates the preset key
|
|
64
|
+
* and the cropRect (computed from the resolved ratio and the
|
|
65
|
+
* current visualSize). One transaction = one undo entry.
|
|
66
|
+
*/
|
|
67
|
+
| {
|
|
68
|
+
type: 'SET_ASPECT_RATIO_VALUE';
|
|
69
|
+
payload: {
|
|
70
|
+
/** Preset key as it appears in the dropdown. */
|
|
71
|
+
presetKey: string;
|
|
72
|
+
/**
|
|
73
|
+
* Resolved width/height ratio. `undefined` = Free
|
|
74
|
+
* (no lock; cropRect stays where it is).
|
|
75
|
+
*/
|
|
76
|
+
resolved: number | undefined;
|
|
77
|
+
/**
|
|
78
|
+
* Rendered image size used to compute the inscribed
|
|
79
|
+
* rect. Caller supplies it because the reducer is
|
|
80
|
+
* framework-agnostic and has no DOM access.
|
|
81
|
+
*/
|
|
82
|
+
visualSize: Size;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** Reset cropOptions to defaults. */
|
|
86
|
+
| { type: 'RESET_CROP_OPTIONS' }
|
|
87
|
+
/**
|
|
88
|
+
* Replace the entire composite state with `payload`. Used by
|
|
89
|
+
* undo/redo to restore a full snapshot atomically.
|
|
90
|
+
*/
|
|
91
|
+
| { type: 'RESTORE_SNAPSHOT'; payload: MediaEditorState }
|
|
92
|
+
/**
|
|
93
|
+
* Adjust the cropper's cropRect in response to a viewport change
|
|
94
|
+
* (e.g., window resize). Behaviorally identical to a `CROPPER
|
|
95
|
+
* SET_CROP_RECT` dispatch, but tagged so the hook layer can skip
|
|
96
|
+
* history recording — viewport reshapes aren't editor actions.
|
|
97
|
+
*/
|
|
98
|
+
| {
|
|
99
|
+
type: 'VIEWPORT_ADJUST_CROP_RECT';
|
|
100
|
+
payload: NormalizedRect;
|
|
101
|
+
};
|