@wordpress/media-editor 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/components/media-editor/index.cjs +34 -135
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +87 -0
- package/build/components/media-editor/use-crop-options.cjs.map +7 -0
- package/build/components/media-editor/use-save-media-editor.cjs +185 -0
- package/build/components/media-editor/use-save-media-editor.cjs.map +7 -0
- package/build/components/media-editor-canvas/index.cjs +32 -11
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +66 -83
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-modal/index.cjs +40 -11
- package/build/components/media-editor-modal/index.cjs.map +3 -3
- package/build/components/media-editor-toolbar/index.cjs +7 -3
- package/build/components/media-editor-toolbar/index.cjs.map +2 -2
- package/build/components/media-form/index.cjs +19 -1
- package/build/components/media-form/index.cjs.map +3 -3
- package/build/components/media-form/sidebar-datetime-view.cjs +53 -0
- package/build/components/media-form/sidebar-datetime-view.cjs.map +7 -0
- package/build/components/rotation-ruler/index.cjs +8 -2
- package/build/components/rotation-ruler/index.cjs.map +2 -2
- package/build/hooks/use-crop-gesture-handlers.cjs +41 -4
- package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
- package/build/image-editor/core/camera.cjs +29 -11
- package/build/image-editor/core/camera.cjs.map +2 -2
- package/build/image-editor/core/constants.cjs +6 -0
- package/build/image-editor/core/constants.cjs.map +2 -2
- package/build/image-editor/core/containment.cjs +71 -34
- package/build/image-editor/core/containment.cjs.map +2 -2
- package/build/image-editor/core/crop-rect.cjs +48 -0
- package/build/image-editor/core/crop-rect.cjs.map +7 -0
- package/build/image-editor/core/index.cjs +12 -0
- package/build/image-editor/core/index.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/core/math/sanitize.cjs +92 -0
- package/build/image-editor/core/math/sanitize.cjs.map +7 -0
- package/build/image-editor/core/setter-helpers.cjs +64 -0
- package/build/image-editor/core/setter-helpers.cjs.map +7 -0
- package/build/image-editor/core/source-region.cjs +16 -14
- package/build/image-editor/core/source-region.cjs.map +2 -2
- package/build/image-editor/core/state.cjs +20 -10
- package/build/image-editor/core/state.cjs.map +2 -2
- package/build/image-editor/core/stencil-math.cjs +31 -20
- package/build/image-editor/core/stencil-math.cjs.map +2 -2
- package/build/image-editor/core/transform-style.cjs +12 -6
- package/build/image-editor/core/transform-style.cjs.map +2 -2
- package/build/image-editor/core/types.cjs.map +1 -1
- package/build/image-editor/index.cjs +12 -2
- package/build/image-editor/index.cjs.map +2 -2
- package/build/image-editor/react/components/cropper-provider.cjs +2 -2
- package/build/image-editor/react/components/cropper-provider.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +121 -48
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +172 -0
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +7 -0
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +20 -10
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/build-cropper-setters.cjs +71 -0
- package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +7 -0
- package/build/image-editor/react/hooks/index.cjs +6 -3
- package/build/image-editor/react/hooks/index.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +3 -1
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs +113 -0
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/state/composite-reducer.cjs +115 -0
- package/build/state/composite-reducer.cjs.map +7 -0
- package/build/state/index.cjs +48 -0
- package/build/state/index.cjs.map +7 -0
- package/build/state/media-editor-state-provider.cjs +58 -0
- package/build/state/media-editor-state-provider.cjs.map +7 -0
- package/build/state/types.cjs +34 -0
- package/build/state/types.cjs.map +7 -0
- package/build/state/use-media-editor-state.cjs +298 -0
- package/build/state/use-media-editor-state.cjs.map +7 -0
- package/build-module/components/media-editor/index.mjs +40 -140
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +61 -0
- package/build-module/components/media-editor/use-crop-options.mjs.map +7 -0
- package/build-module/components/media-editor/use-save-media-editor.mjs +151 -0
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +7 -0
- package/build-module/components/media-editor-canvas/index.mjs +33 -12
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +68 -83
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-modal/index.mjs +40 -11
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-toolbar/index.mjs +7 -3
- package/build-module/components/media-editor-toolbar/index.mjs.map +2 -2
- package/build-module/components/media-form/index.mjs +9 -1
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/media-form/sidebar-datetime-view.mjs +32 -0
- package/build-module/components/media-form/sidebar-datetime-view.mjs.map +7 -0
- package/build-module/components/rotation-ruler/index.mjs +8 -2
- package/build-module/components/rotation-ruler/index.mjs.map +2 -2
- package/build-module/hooks/use-crop-gesture-handlers.mjs +41 -4
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
- package/build-module/image-editor/core/camera.mjs +34 -11
- package/build-module/image-editor/core/camera.mjs.map +2 -2
- package/build-module/image-editor/core/constants.mjs +4 -0
- package/build-module/image-editor/core/constants.mjs.map +2 -2
- package/build-module/image-editor/core/containment.mjs +75 -34
- package/build-module/image-editor/core/containment.mjs.map +2 -2
- package/build-module/image-editor/core/crop-rect.mjs +23 -0
- package/build-module/image-editor/core/crop-rect.mjs.map +7 -0
- package/build-module/image-editor/core/index.mjs +12 -0
- package/build-module/image-editor/core/index.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/core/math/sanitize.mjs +63 -0
- package/build-module/image-editor/core/math/sanitize.mjs.map +7 -0
- package/build-module/image-editor/core/setter-helpers.mjs +39 -0
- package/build-module/image-editor/core/setter-helpers.mjs.map +7 -0
- package/build-module/image-editor/core/source-region.mjs +16 -14
- package/build-module/image-editor/core/source-region.mjs.map +2 -2
- package/build-module/image-editor/core/state.mjs +25 -11
- package/build-module/image-editor/core/state.mjs.map +2 -2
- package/build-module/image-editor/core/stencil-math.mjs +30 -19
- package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
- package/build-module/image-editor/core/transform-style.mjs +12 -6
- package/build-module/image-editor/core/transform-style.mjs.map +2 -2
- package/build-module/image-editor/index.mjs +11 -2
- package/build-module/image-editor/index.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper-provider.mjs +3 -3
- package/build-module/image-editor/react/components/cropper-provider.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +122 -49
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +147 -0
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +7 -0
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +20 -10
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +46 -0
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/index.mjs +4 -2
- package/build-module/image-editor/react/hooks/index.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +3 -1
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +100 -0
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/state/composite-reducer.mjs +94 -0
- package/build-module/state/composite-reducer.mjs.map +7 -0
- package/build-module/state/index.mjs +26 -0
- package/build-module/state/index.mjs.map +7 -0
- package/build-module/state/media-editor-state-provider.mjs +34 -0
- package/build-module/state/media-editor-state-provider.mjs.map +7 -0
- package/build-module/state/types.mjs +9 -0
- package/build-module/state/types.mjs.map +7 -0
- package/build-module/state/use-media-editor-state.mjs +289 -0
- package/build-module/state/use-media-editor-state.mjs.map +7 -0
- package/build-style/style-rtl.css +46 -9
- package/build-style/style.css +46 -9
- package/build-types/components/media-editor/index.d.ts +4 -7
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +37 -0
- package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -0
- package/build-types/components/media-editor/use-save-media-editor.d.ts +29 -0
- package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -0
- package/build-types/components/media-editor-canvas/index.d.ts +5 -10
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +4 -16
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/components/media-form/sidebar-datetime-view.d.ts +10 -0
- package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +1 -0
- package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
- package/build-types/hooks/use-crop-gesture-handlers.d.ts +13 -14
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
- package/build-types/image-editor/core/camera.d.ts.map +1 -1
- package/build-types/image-editor/core/constants.d.ts +18 -0
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/containment.d.ts +10 -0
- package/build-types/image-editor/core/containment.d.ts.map +1 -1
- package/build-types/image-editor/core/crop-rect.d.ts +19 -0
- package/build-types/image-editor/core/crop-rect.d.ts.map +1 -0
- package/build-types/image-editor/core/index.d.ts +3 -1
- package/build-types/image-editor/core/index.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts +5 -1
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/core/math/sanitize.d.ts +70 -0
- package/build-types/image-editor/core/math/sanitize.d.ts.map +1 -0
- package/build-types/image-editor/core/setter-helpers.d.ts +24 -0
- package/build-types/image-editor/core/setter-helpers.d.ts.map +1 -0
- package/build-types/image-editor/core/source-region.d.ts.map +1 -1
- package/build-types/image-editor/core/state.d.ts +12 -0
- package/build-types/image-editor/core/state.d.ts.map +1 -1
- package/build-types/image-editor/core/stencil-math.d.ts +23 -18
- package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
- package/build-types/image-editor/core/transform-style.d.ts.map +1 -1
- package/build-types/image-editor/core/types.d.ts +23 -4
- package/build-types/image-editor/core/types.d.ts.map +1 -1
- package/build-types/image-editor/index.d.ts +4 -4
- package/build-types/image-editor/index.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts +11 -7
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +6 -4
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +36 -0
- package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +1 -0
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +2 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +47 -0
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/index.d.ts +4 -2
- package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts +58 -0
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts +39 -0
- package/build-types/state/composite-reducer.d.ts.map +1 -0
- package/build-types/state/index.d.ts +7 -0
- package/build-types/state/index.d.ts.map +1 -0
- package/build-types/state/media-editor-state-provider.d.ts +34 -0
- package/build-types/state/media-editor-state-provider.d.ts.map +1 -0
- package/build-types/state/types.d.ts +104 -0
- package/build-types/state/types.d.ts.map +1 -0
- package/build-types/state/use-media-editor-state.d.ts +104 -0
- package/build-types/state/use-media-editor-state.d.ts.map +1 -0
- package/package.json +19 -17
- package/src/components/media-editor/index.tsx +52 -188
- package/src/components/media-editor/test/use-crop-options.tsx +164 -0
- package/src/components/media-editor/use-crop-options.ts +115 -0
- package/src/components/media-editor/use-save-media-editor.ts +214 -0
- package/src/components/media-editor-canvas/index.tsx +56 -24
- package/src/components/media-editor-crop-panel/index.tsx +25 -61
- package/src/components/media-editor-crop-panel/test/index.tsx +17 -43
- package/src/components/media-editor-modal/index.tsx +50 -12
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +14 -0
- package/src/components/media-editor-modal/test/index.tsx +143 -0
- package/src/components/media-editor-toolbar/index.tsx +9 -5
- package/src/components/media-form/index.tsx +15 -1
- package/src/components/media-form/sidebar-datetime-view.tsx +47 -0
- package/src/components/media-form/test/sidebar-datetime-view.test.tsx +96 -0
- package/src/components/rotation-ruler/README.md +11 -25
- package/src/components/rotation-ruler/index.tsx +17 -2
- package/src/components/rotation-ruler/style.scss +49 -13
- package/src/hooks/use-crop-gesture-handlers.ts +66 -17
- package/src/image-editor/README.md +28 -129
- package/src/image-editor/core/camera.ts +51 -26
- package/src/image-editor/core/constants.ts +19 -0
- package/src/image-editor/core/containment.ts +102 -49
- package/src/image-editor/core/crop-rect.ts +45 -0
- package/src/image-editor/core/index.ts +16 -4
- package/src/image-editor/core/interaction-controller.ts +5 -1
- package/src/image-editor/core/math/sanitize.ts +158 -0
- package/src/image-editor/core/math/test/sanitize.ts +244 -0
- package/src/image-editor/core/setter-helpers.ts +66 -0
- package/src/image-editor/core/source-region.ts +21 -14
- package/src/image-editor/core/state.ts +67 -15
- package/src/image-editor/core/stencil-math.ts +55 -35
- package/src/image-editor/core/test/camera.ts +367 -1
- package/src/image-editor/core/test/state.ts +101 -8
- package/src/image-editor/core/test/stencil-math.ts +193 -0
- package/src/image-editor/core/transform-style.ts +21 -6
- package/src/image-editor/core/types.ts +24 -4
- package/src/image-editor/docs/architecture.md +48 -89
- package/src/image-editor/docs/recipes.md +123 -659
- package/src/image-editor/index.ts +12 -3
- package/src/image-editor/react/components/cropper-provider.tsx +16 -12
- package/src/image-editor/react/components/cropper.scss +23 -4
- package/src/image-editor/react/components/cropper.tsx +197 -90
- package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +269 -0
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +22 -9
- package/src/image-editor/react/components/test/cropper.tsx +55 -13
- package/src/image-editor/react/hooks/build-cropper-setters.ts +105 -0
- package/src/image-editor/react/hooks/index.ts +4 -2
- package/src/image-editor/react/hooks/test/{use-cropper-state.ts → use-cropper-reducer.ts} +142 -286
- package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
- package/src/image-editor/react/hooks/use-cropper-reducer.ts +204 -0
- package/src/image-editor/react/hooks/use-interaction.ts +3 -2
- package/src/image-editor/stories/rectangle-crop.story.tsx +7 -17
- package/src/state/composite-reducer.ts +152 -0
- package/src/state/index.ts +20 -0
- package/src/state/media-editor-state-provider.tsx +70 -0
- package/src/state/test/use-media-editor-state.ts +402 -0
- package/src/state/types.ts +107 -0
- package/src/state/use-media-editor-state.ts +488 -0
- package/tsconfig.json +2 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/build/image-editor/react/hooks/use-cropper-state.cjs +0 -277
- package/build/image-editor/react/hooks/use-cropper-state.cjs.map +0 -7
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs +0 -262
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +0 -7
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -97
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
- package/src/image-editor/docs/roadmap.md +0 -66
- package/src/image-editor/react/hooks/use-cropper-state.ts +0 -457
|
@@ -0,0 +1,402 @@
|
|
|
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 (Free + freeform on)', () => {
|
|
30
|
+
const { result } = renderHook( () => useMediaEditorState() );
|
|
31
|
+
|
|
32
|
+
expect( result.current.cropOptions.aspectRatioValue ).toBe( '0' );
|
|
33
|
+
expect( result.current.cropOptions.freeformCrop ).toBe( true );
|
|
34
|
+
} );
|
|
35
|
+
|
|
36
|
+
it( 'merges initial cropper and cropOptions overrides', () => {
|
|
37
|
+
const { result } = renderHook( () =>
|
|
38
|
+
useMediaEditorState( {
|
|
39
|
+
cropper: { image: IMAGE },
|
|
40
|
+
cropOptions: { aspectRatioValue: '1', freeformCrop: false },
|
|
41
|
+
} )
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
expect( result.current.state.image ).toEqual( IMAGE );
|
|
45
|
+
expect( result.current.cropOptions ).toEqual( {
|
|
46
|
+
aspectRatioValue: '1',
|
|
47
|
+
freeformCrop: false,
|
|
48
|
+
} );
|
|
49
|
+
} );
|
|
50
|
+
|
|
51
|
+
it( 'starts with isDirty=false and no undo / redo', () => {
|
|
52
|
+
const { result } = setupHook();
|
|
53
|
+
|
|
54
|
+
expect( result.current.isDirty ).toBe( false );
|
|
55
|
+
expect( result.current.isCropperDirty ).toBe( false );
|
|
56
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
57
|
+
expect( result.current.hasRedo ).toBe( false );
|
|
58
|
+
} );
|
|
59
|
+
} );
|
|
60
|
+
|
|
61
|
+
describe( 'history — undo / redo across slices', () => {
|
|
62
|
+
it( 'records the pre-state of each action and undoes it', () => {
|
|
63
|
+
const { result } = setupHook();
|
|
64
|
+
|
|
65
|
+
act( () => result.current.setRotation( 45 ) );
|
|
66
|
+
|
|
67
|
+
expect( result.current.state.rotation ).toBe( 45 );
|
|
68
|
+
expect( result.current.hasUndo ).toBe( true );
|
|
69
|
+
|
|
70
|
+
act( () => result.current.undo() );
|
|
71
|
+
|
|
72
|
+
expect( result.current.state.rotation ).toBe( 0 );
|
|
73
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
74
|
+
expect( result.current.hasRedo ).toBe( true );
|
|
75
|
+
} );
|
|
76
|
+
|
|
77
|
+
it( 'redo re-applies an undone action', () => {
|
|
78
|
+
const { result } = setupHook();
|
|
79
|
+
|
|
80
|
+
act( () => result.current.setRotation( 45 ) );
|
|
81
|
+
act( () => result.current.undo() );
|
|
82
|
+
act( () => result.current.redo() );
|
|
83
|
+
|
|
84
|
+
expect( result.current.state.rotation ).toBe( 45 );
|
|
85
|
+
} );
|
|
86
|
+
|
|
87
|
+
it( 'redo stack clears on a new action after undo', () => {
|
|
88
|
+
const { result } = setupHook();
|
|
89
|
+
|
|
90
|
+
act( () => result.current.setRotation( 45 ) );
|
|
91
|
+
act( () => result.current.undo() );
|
|
92
|
+
act( () => result.current.setRotation( 90 ) );
|
|
93
|
+
|
|
94
|
+
expect( result.current.hasRedo ).toBe( false );
|
|
95
|
+
} );
|
|
96
|
+
|
|
97
|
+
it( 'undo restores cropOptions changes (freeform toggle)', () => {
|
|
98
|
+
const { result } = setupHook();
|
|
99
|
+
|
|
100
|
+
act( () => result.current.setFreeformCrop( false ) );
|
|
101
|
+
expect( result.current.cropOptions.freeformCrop ).toBe( false );
|
|
102
|
+
expect( result.current.isDirty ).toBe( true );
|
|
103
|
+
expect( result.current.isCropperDirty ).toBe( false );
|
|
104
|
+
|
|
105
|
+
act( () => result.current.undo() );
|
|
106
|
+
expect( result.current.cropOptions.freeformCrop ).toBe( true );
|
|
107
|
+
} );
|
|
108
|
+
|
|
109
|
+
it( 'no-op actions do not record history entries', () => {
|
|
110
|
+
const { result } = setupHook();
|
|
111
|
+
|
|
112
|
+
// Setting freeformCrop to its current value should be a no-op.
|
|
113
|
+
act( () => result.current.setFreeformCrop( true ) );
|
|
114
|
+
|
|
115
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
116
|
+
} );
|
|
117
|
+
|
|
118
|
+
it( 'a single undo restores both cropOptions and cropRect after aspect-ratio change', () => {
|
|
119
|
+
const { result } = setupHook();
|
|
120
|
+
const initialCropRect = result.current.state.cropRect;
|
|
121
|
+
const initialPreset = result.current.cropOptions.aspectRatioValue;
|
|
122
|
+
|
|
123
|
+
act( () => result.current.setAspectRatioValue( '1' ) ); // Square
|
|
124
|
+
|
|
125
|
+
// Both slices changed atomically: preset key and cropRect.
|
|
126
|
+
expect( result.current.cropOptions.aspectRatioValue ).toBe( '1' );
|
|
127
|
+
expect( result.current.state.cropRect ).not.toEqual(
|
|
128
|
+
initialCropRect
|
|
129
|
+
);
|
|
130
|
+
expect( result.current.isCropperDirty ).toBe( true );
|
|
131
|
+
// Only ONE undo entry — atomic transaction.
|
|
132
|
+
expect( result.current.hasUndo ).toBe( true );
|
|
133
|
+
|
|
134
|
+
act( () => result.current.undo() );
|
|
135
|
+
|
|
136
|
+
expect( result.current.cropOptions.aspectRatioValue ).toBe(
|
|
137
|
+
initialPreset
|
|
138
|
+
);
|
|
139
|
+
expect( result.current.state.cropRect ).toEqual( initialCropRect );
|
|
140
|
+
} );
|
|
141
|
+
} );
|
|
142
|
+
|
|
143
|
+
describe( 'gestures — coalesce many changes into one undo entry', () => {
|
|
144
|
+
it( 'coalesces all changes between beginGesture and endGesture', () => {
|
|
145
|
+
const { result } = setupHook();
|
|
146
|
+
|
|
147
|
+
act( () => result.current.beginGesture() );
|
|
148
|
+
act( () => result.current.setRotation( 10 ) );
|
|
149
|
+
act( () => result.current.setRotation( 20 ) );
|
|
150
|
+
act( () => result.current.setRotation( 30 ) );
|
|
151
|
+
act( () => result.current.endGesture() );
|
|
152
|
+
|
|
153
|
+
expect( result.current.state.rotation ).toBe( 30 );
|
|
154
|
+
expect( result.current.hasUndo ).toBe( true );
|
|
155
|
+
|
|
156
|
+
// One undo restores all the way to the pre-gesture state.
|
|
157
|
+
act( () => result.current.undo() );
|
|
158
|
+
expect( result.current.state.rotation ).toBe( 0 );
|
|
159
|
+
} );
|
|
160
|
+
|
|
161
|
+
it( 'an empty gesture (no changes) records no history', () => {
|
|
162
|
+
const { result } = setupHook();
|
|
163
|
+
|
|
164
|
+
act( () => result.current.beginGesture() );
|
|
165
|
+
act( () => result.current.endGesture() );
|
|
166
|
+
|
|
167
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
168
|
+
} );
|
|
169
|
+
|
|
170
|
+
it( 'undo during an open gesture flushes the gesture first', () => {
|
|
171
|
+
const { result } = setupHook();
|
|
172
|
+
|
|
173
|
+
act( () => result.current.beginGesture() );
|
|
174
|
+
act( () => result.current.setRotation( 45 ) );
|
|
175
|
+
// Undo without endGesture should still flush the in-flight change.
|
|
176
|
+
act( () => result.current.undo() );
|
|
177
|
+
|
|
178
|
+
expect( result.current.state.rotation ).toBe( 0 );
|
|
179
|
+
expect( result.current.hasRedo ).toBe( true );
|
|
180
|
+
} );
|
|
181
|
+
|
|
182
|
+
it( 'keeps the original gesture snapshot if beginGesture is called repeatedly', () => {
|
|
183
|
+
const { result } = setupHook();
|
|
184
|
+
|
|
185
|
+
act( () => {
|
|
186
|
+
result.current.beginGesture();
|
|
187
|
+
result.current.setRotation( 10 );
|
|
188
|
+
result.current.beginGesture();
|
|
189
|
+
result.current.setRotation( 20 );
|
|
190
|
+
result.current.endGesture();
|
|
191
|
+
} );
|
|
192
|
+
|
|
193
|
+
expect( result.current.state.rotation ).toBe( 20 );
|
|
194
|
+
|
|
195
|
+
act( () => result.current.undo() );
|
|
196
|
+
|
|
197
|
+
expect( result.current.state.rotation ).toBe( 0 );
|
|
198
|
+
} );
|
|
199
|
+
|
|
200
|
+
it( 'does not record a gesture that returns to its starting state', () => {
|
|
201
|
+
const { result } = setupHook();
|
|
202
|
+
|
|
203
|
+
act( () => {
|
|
204
|
+
result.current.beginGesture();
|
|
205
|
+
result.current.setRotation( 10 );
|
|
206
|
+
result.current.setRotation( 0 );
|
|
207
|
+
result.current.endGesture();
|
|
208
|
+
} );
|
|
209
|
+
|
|
210
|
+
expect( result.current.state.rotation ).toBe( 0 );
|
|
211
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
212
|
+
} );
|
|
213
|
+
|
|
214
|
+
it( 'groups a Free preset change and freeform re-enable in one undo step', () => {
|
|
215
|
+
const { result } = renderHook( () =>
|
|
216
|
+
useMediaEditorState( {
|
|
217
|
+
cropper: { image: IMAGE },
|
|
218
|
+
cropOptions: {
|
|
219
|
+
aspectRatioValue: '1',
|
|
220
|
+
freeformCrop: false,
|
|
221
|
+
},
|
|
222
|
+
} )
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
act( () => {
|
|
226
|
+
result.current.beginGesture();
|
|
227
|
+
result.current.setAspectRatioValue( '0' );
|
|
228
|
+
result.current.setFreeformCrop( true );
|
|
229
|
+
result.current.endGesture();
|
|
230
|
+
} );
|
|
231
|
+
|
|
232
|
+
expect( result.current.cropOptions ).toEqual( {
|
|
233
|
+
aspectRatioValue: '0',
|
|
234
|
+
freeformCrop: true,
|
|
235
|
+
} );
|
|
236
|
+
|
|
237
|
+
act( () => result.current.undo() );
|
|
238
|
+
|
|
239
|
+
expect( result.current.cropOptions ).toEqual( {
|
|
240
|
+
aspectRatioValue: '1',
|
|
241
|
+
freeformCrop: false,
|
|
242
|
+
} );
|
|
243
|
+
} );
|
|
244
|
+
} );
|
|
245
|
+
|
|
246
|
+
describe( 'SET_ASPECT_RATIO_VALUE atomicity', () => {
|
|
247
|
+
it( 'reshapes cropRect when a fixed ratio is selected with visualSize > 0', () => {
|
|
248
|
+
const { result } = setupHook( { width: 600, height: 400 } );
|
|
249
|
+
|
|
250
|
+
act( () => result.current.setAspectRatioValue( '1' ) ); // Square
|
|
251
|
+
|
|
252
|
+
// At visualSize 600x400, the inscribed square has w/h such that
|
|
253
|
+
// pixel ratio = 1. normalizedRatio = 1 * 400 / 600 = 0.666...
|
|
254
|
+
// So normalized w = 0.666..., h = 1.
|
|
255
|
+
expect( result.current.state.cropRect.width ).toBeCloseTo(
|
|
256
|
+
2 / 3,
|
|
257
|
+
5
|
|
258
|
+
);
|
|
259
|
+
expect( result.current.state.cropRect.height ).toBeCloseTo( 1, 5 );
|
|
260
|
+
} );
|
|
261
|
+
|
|
262
|
+
it( 'Free leaves the cropRect alone', () => {
|
|
263
|
+
const { result } = setupHook();
|
|
264
|
+
const original = result.current.state.cropRect;
|
|
265
|
+
|
|
266
|
+
// User-picked custom crop first.
|
|
267
|
+
act( () =>
|
|
268
|
+
result.current.setCropRect( {
|
|
269
|
+
x: 0.1,
|
|
270
|
+
y: 0.1,
|
|
271
|
+
width: 0.5,
|
|
272
|
+
height: 0.5,
|
|
273
|
+
} )
|
|
274
|
+
);
|
|
275
|
+
const beforeFree = result.current.state.cropRect;
|
|
276
|
+
|
|
277
|
+
// Switch to Free — cropRect should NOT snap back to anything.
|
|
278
|
+
act( () => result.current.setAspectRatioValue( '0' ) );
|
|
279
|
+
|
|
280
|
+
expect( result.current.state.cropRect ).toEqual( beforeFree );
|
|
281
|
+
expect( original ).not.toEqual( beforeFree );
|
|
282
|
+
} );
|
|
283
|
+
|
|
284
|
+
it( 'resolves Original from the loaded image', () => {
|
|
285
|
+
const { result } = setupHook();
|
|
286
|
+
|
|
287
|
+
act( () => result.current.setAspectRatioValue( '-1' ) ); // Original
|
|
288
|
+
|
|
289
|
+
// Image is 1200x600 = ratio 2. inscribedRect at visualSize 600x400:
|
|
290
|
+
// normalizedRatio = 2 * 400 / 600 = 1.333... > 1, so h = 1/1.333 = 0.75.
|
|
291
|
+
expect( result.current.state.cropRect.width ).toBeCloseTo( 1, 5 );
|
|
292
|
+
expect( result.current.state.cropRect.height ).toBeCloseTo(
|
|
293
|
+
0.75,
|
|
294
|
+
5
|
|
295
|
+
);
|
|
296
|
+
} );
|
|
297
|
+
|
|
298
|
+
it( 'records exactly ONE history entry for an aspect-ratio change followed by a freeform toggle', () => {
|
|
299
|
+
const { result } = setupHook();
|
|
300
|
+
const original = result.current.state.cropRect;
|
|
301
|
+
const originalPreset = result.current.cropOptions.aspectRatioValue;
|
|
302
|
+
const originalFreeform = result.current.cropOptions.freeformCrop;
|
|
303
|
+
|
|
304
|
+
act( () => result.current.setAspectRatioValue( '1' ) ); // Square
|
|
305
|
+
act( () => result.current.setFreeformCrop( false ) );
|
|
306
|
+
|
|
307
|
+
// Two discrete actions → two undo entries.
|
|
308
|
+
act( () => result.current.undo() );
|
|
309
|
+
expect( result.current.cropOptions.freeformCrop ).toBe(
|
|
310
|
+
originalFreeform
|
|
311
|
+
);
|
|
312
|
+
expect( result.current.cropOptions.aspectRatioValue ).toBe( '1' );
|
|
313
|
+
|
|
314
|
+
act( () => result.current.undo() );
|
|
315
|
+
expect( result.current.cropOptions.aspectRatioValue ).toBe(
|
|
316
|
+
originalPreset
|
|
317
|
+
);
|
|
318
|
+
expect( result.current.state.cropRect ).toEqual( original );
|
|
319
|
+
} );
|
|
320
|
+
|
|
321
|
+
it( 'groups cropper reset and cropOptions reset in one undo step', () => {
|
|
322
|
+
const { result } = setupHook();
|
|
323
|
+
|
|
324
|
+
act( () => result.current.setAspectRatioValue( '1' ) );
|
|
325
|
+
act( () => result.current.setFreeformCrop( false ) );
|
|
326
|
+
act( () => result.current.setRotation( 45 ) );
|
|
327
|
+
|
|
328
|
+
act( () => {
|
|
329
|
+
result.current.beginGesture();
|
|
330
|
+
result.current.reset();
|
|
331
|
+
result.current.resetCropOptions();
|
|
332
|
+
result.current.endGesture();
|
|
333
|
+
} );
|
|
334
|
+
|
|
335
|
+
expect( result.current.state.rotation ).toBe( 0 );
|
|
336
|
+
expect( result.current.cropOptions ).toEqual( {
|
|
337
|
+
aspectRatioValue: '0',
|
|
338
|
+
freeformCrop: true,
|
|
339
|
+
} );
|
|
340
|
+
|
|
341
|
+
act( () => result.current.undo() );
|
|
342
|
+
|
|
343
|
+
expect( result.current.state.rotation ).toBe( 45 );
|
|
344
|
+
expect( result.current.cropOptions ).toEqual( {
|
|
345
|
+
aspectRatioValue: '1',
|
|
346
|
+
freeformCrop: false,
|
|
347
|
+
} );
|
|
348
|
+
} );
|
|
349
|
+
} );
|
|
350
|
+
|
|
351
|
+
describe( 'viewport reshape is silent', () => {
|
|
352
|
+
it( 'adjustCropRectForViewport does NOT record an undo entry', () => {
|
|
353
|
+
const { result } = setupHook();
|
|
354
|
+
|
|
355
|
+
act( () =>
|
|
356
|
+
result.current.adjustCropRectForViewport( {
|
|
357
|
+
x: 0,
|
|
358
|
+
y: 0,
|
|
359
|
+
width: 0.5,
|
|
360
|
+
height: 0.5,
|
|
361
|
+
} )
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
365
|
+
} );
|
|
366
|
+
} );
|
|
367
|
+
|
|
368
|
+
describe( 'setImage', () => {
|
|
369
|
+
it( 'clears history and resets isDirty', () => {
|
|
370
|
+
const { result } = setupHook();
|
|
371
|
+
|
|
372
|
+
act( () => result.current.setRotation( 45 ) );
|
|
373
|
+
expect( result.current.hasUndo ).toBe( true );
|
|
374
|
+
expect( result.current.isDirty ).toBe( true );
|
|
375
|
+
|
|
376
|
+
act( () =>
|
|
377
|
+
result.current.setImage( {
|
|
378
|
+
src: 'other.jpg',
|
|
379
|
+
naturalWidth: 800,
|
|
380
|
+
naturalHeight: 800,
|
|
381
|
+
} )
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
385
|
+
expect( result.current.hasRedo ).toBe( false );
|
|
386
|
+
expect( result.current.isDirty ).toBe( false );
|
|
387
|
+
} );
|
|
388
|
+
|
|
389
|
+
it( 'does not clear history when the same image is reported again', () => {
|
|
390
|
+
const { result } = setupHook();
|
|
391
|
+
|
|
392
|
+
act( () => result.current.setRotation( 45 ) );
|
|
393
|
+
expect( result.current.hasUndo ).toBe( true );
|
|
394
|
+
expect( result.current.isCropperDirty ).toBe( true );
|
|
395
|
+
|
|
396
|
+
act( () => result.current.setImage( IMAGE ) );
|
|
397
|
+
|
|
398
|
+
expect( result.current.hasUndo ).toBe( true );
|
|
399
|
+
expect( result.current.isCropperDirty ).toBe( true );
|
|
400
|
+
} );
|
|
401
|
+
} );
|
|
402
|
+
} );
|
|
@@ -0,0 +1,107 @@
|
|
|
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 — preset selection and
|
|
13
|
+
* resize-handle visibility. Lives in the composite store so it
|
|
14
|
+
* participates in the same undo history as cropper geometry.
|
|
15
|
+
*/
|
|
16
|
+
export interface CropOptionsSlice {
|
|
17
|
+
/**
|
|
18
|
+
* Selected aspect-ratio preset as a string (round-trippable through
|
|
19
|
+
* `<SelectControl>`). `'0'` = Free, `'-1'` = Original, any positive
|
|
20
|
+
* decimal = fixed ratio.
|
|
21
|
+
*/
|
|
22
|
+
aspectRatioValue: string;
|
|
23
|
+
/** Whether the cropper exposes resize handles. */
|
|
24
|
+
freeformCrop: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Default values for the crop-options slice. Mirrors the initial state
|
|
29
|
+
* used by the sidebar before the user has touched anything.
|
|
30
|
+
*/
|
|
31
|
+
export const DEFAULT_CROP_OPTIONS: CropOptionsSlice = {
|
|
32
|
+
aspectRatioValue: '0',
|
|
33
|
+
freeformCrop: true,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The full composite state for the media editor — cropper geometry
|
|
38
|
+
* plus the sidebar control slice. Future feature slices (text
|
|
39
|
+
* overlays, filters, …) extend this object.
|
|
40
|
+
*
|
|
41
|
+
* Every undo/redo snapshot is a full `MediaEditorState`. Slices stay
|
|
42
|
+
* referentially equal across actions that don't touch them.
|
|
43
|
+
*/
|
|
44
|
+
export interface MediaEditorState {
|
|
45
|
+
cropper: CropperState;
|
|
46
|
+
cropOptions: CropOptionsSlice;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Composite reducer action union.
|
|
51
|
+
*
|
|
52
|
+
* Cropper geometry actions are wrapped in a `CROPPER` envelope and
|
|
53
|
+
* delegated to the pure `cropperReducer`. Sidebar and history
|
|
54
|
+
* concerns are first-class actions on this reducer.
|
|
55
|
+
*/
|
|
56
|
+
export type MediaEditorAction =
|
|
57
|
+
/**
|
|
58
|
+
* Delegate to the underlying cropper reducer. Used for every
|
|
59
|
+
* geometry transition (pan / zoom / rotation / flip / cropRect /
|
|
60
|
+
* settle / etc.).
|
|
61
|
+
*/
|
|
62
|
+
| {
|
|
63
|
+
type: 'CROPPER';
|
|
64
|
+
action: CropperAction;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Set the aspect-ratio preset. Atomically updates the preset key
|
|
68
|
+
* and the cropRect (computed from the resolved ratio and the
|
|
69
|
+
* current visualSize). One transaction = one undo entry.
|
|
70
|
+
*/
|
|
71
|
+
| {
|
|
72
|
+
type: 'SET_ASPECT_RATIO_VALUE';
|
|
73
|
+
payload: {
|
|
74
|
+
/** Preset key as it appears in the dropdown. */
|
|
75
|
+
presetKey: string;
|
|
76
|
+
/**
|
|
77
|
+
* Resolved width/height ratio. `undefined` = Free
|
|
78
|
+
* (no lock; cropRect stays where it is).
|
|
79
|
+
*/
|
|
80
|
+
resolved: number | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Rendered image size used to compute the inscribed
|
|
83
|
+
* rect. Caller supplies it because the reducer is
|
|
84
|
+
* framework-agnostic and has no DOM access.
|
|
85
|
+
*/
|
|
86
|
+
visualSize: Size;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/** Toggle the resize-handle (freeform) mode. */
|
|
90
|
+
| { type: 'SET_FREEFORM_CROP'; payload: boolean }
|
|
91
|
+
/** Reset both cropOptions fields to defaults. */
|
|
92
|
+
| { type: 'RESET_CROP_OPTIONS' }
|
|
93
|
+
/**
|
|
94
|
+
* Replace the entire composite state with `payload`. Used by
|
|
95
|
+
* undo/redo to restore a full snapshot atomically.
|
|
96
|
+
*/
|
|
97
|
+
| { type: 'RESTORE_SNAPSHOT'; payload: MediaEditorState }
|
|
98
|
+
/**
|
|
99
|
+
* Adjust the cropper's cropRect in response to a viewport change
|
|
100
|
+
* (e.g., window resize). Behaviorally identical to a `CROPPER
|
|
101
|
+
* SET_CROP_RECT` dispatch, but tagged so the hook layer can skip
|
|
102
|
+
* history recording — viewport reshapes aren't editor actions.
|
|
103
|
+
*/
|
|
104
|
+
| {
|
|
105
|
+
type: 'VIEWPORT_ADJUST_CROP_RECT';
|
|
106
|
+
payload: NormalizedRect;
|
|
107
|
+
};
|