@wordpress/media-editor 0.8.0 → 0.8.1-next.v.202605131032.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 +560 -0
- package/build/components/media-editor/index.cjs.map +7 -0
- package/build/components/media-editor-canvas/index.cjs +14 -2
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +34 -27
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +162 -0
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +7 -0
- package/build/components/media-editor-modal/index.cjs +42 -430
- package/build/components/media-editor-modal/index.cjs.map +3 -3
- package/build/components/media-editor-toolbar/index.cjs +143 -75
- package/build/components/media-editor-toolbar/index.cjs.map +3 -3
- package/build/components/media-form/index.cjs +3 -0
- package/build/components/media-form/index.cjs.map +2 -2
- package/build/components/rotation-ruler/index.cjs +197 -0
- package/build/components/rotation-ruler/index.cjs.map +7 -0
- package/build/components/rotation-ruler/use-ruler-drag.cjs +154 -0
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +7 -0
- package/build/hooks/use-crop-gesture-handlers.cjs +43 -0
- package/build/hooks/use-crop-gesture-handlers.cjs.map +7 -0
- package/build/image-editor/core/constants.cjs +9 -0
- package/build/image-editor/core/constants.cjs.map +2 -2
- package/build/image-editor/core/containment.cjs +7 -13
- package/build/image-editor/core/containment.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs +43 -7
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/core/state.cjs +5 -1
- package/build/image-editor/core/state.cjs.map +2 -2
- package/build/image-editor/core/transform-style.cjs.map +1 -1
- package/build/image-editor/core/types.cjs.map +1 -1
- package/build/image-editor/core/viewport-state.cjs +50 -0
- package/build/image-editor/core/viewport-state.cjs.map +7 -0
- package/build/image-editor/react/components/cropper.cjs +192 -72
- package/build/image-editor/react/components/cropper.cjs.map +3 -3
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +5 -2
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +16 -4
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/components/viewport-provider.cjs +51 -0
- package/build/image-editor/react/components/viewport-provider.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +53 -6
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-cropper-state.cjs +127 -8
- package/build/image-editor/react/hooks/use-cropper-state.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs +26 -5
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-viewport-state.cjs +51 -0
- package/build/image-editor/react/hooks/use-viewport-state.cjs.map +7 -0
- package/build/image-editor/react/visually-hidden-style.cjs +41 -0
- package/build/image-editor/react/visually-hidden-style.cjs.map +7 -0
- package/build/private-apis.cjs +2 -0
- package/build/private-apis.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +545 -0
- package/build-module/components/media-editor/index.mjs.map +7 -0
- package/build-module/components/media-editor-canvas/index.mjs +14 -2
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +35 -28
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +144 -0
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +7 -0
- package/build-module/components/media-editor-modal/index.mjs +46 -455
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-toolbar/index.mjs +137 -77
- package/build-module/components/media-editor-toolbar/index.mjs.map +2 -2
- package/build-module/components/media-form/index.mjs +3 -0
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/index.mjs +166 -0
- package/build-module/components/rotation-ruler/index.mjs.map +7 -0
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs +126 -0
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +7 -0
- package/build-module/hooks/use-crop-gesture-handlers.mjs +17 -0
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +7 -0
- package/build-module/image-editor/core/constants.mjs +6 -0
- package/build-module/image-editor/core/constants.mjs.map +2 -2
- package/build-module/image-editor/core/containment.mjs +6 -12
- package/build-module/image-editor/core/containment.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs +50 -8
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/core/state.mjs +5 -1
- package/build-module/image-editor/core/state.mjs.map +2 -2
- package/build-module/image-editor/core/transform-style.mjs.map +1 -1
- package/build-module/image-editor/core/viewport-state.mjs +24 -0
- package/build-module/image-editor/core/viewport-state.mjs.map +7 -0
- package/build-module/image-editor/react/components/cropper.mjs +195 -73
- package/build-module/image-editor/react/components/cropper.mjs.map +3 -3
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +5 -2
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +26 -5
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/components/viewport-provider.mjs +27 -0
- package/build-module/image-editor/react/components/viewport-provider.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +59 -7
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs +134 -9
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs +26 -5
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs +29 -0
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +7 -0
- package/build-module/image-editor/react/visually-hidden-style.mjs +16 -0
- package/build-module/image-editor/react/visually-hidden-style.mjs.map +7 -0
- package/build-module/private-apis.mjs +2 -0
- package/build-module/private-apis.mjs.map +2 -2
- package/build-style/style-rtl.css +254 -29
- package/build-style/style.css +254 -29
- package/build-types/components/media-editor/index.d.ts +34 -0
- package/build-types/components/media-editor/index.d.ts.map +1 -0
- package/build-types/components/media-editor-canvas/index.d.ts +10 -1
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts +6 -0
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/index.d.ts +2 -2
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-provider/index.d.ts +1 -1
- package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
- package/build-types/components/media-editor-toolbar/index.d.ts +4 -1
- package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts +1 -1
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/index.d.ts +46 -0
- package/build-types/components/rotation-ruler/index.d.ts.map +1 -0
- package/build-types/components/rotation-ruler/stories/index.story.d.ts +14 -0
- package/build-types/components/rotation-ruler/stories/index.story.d.ts.map +1 -0
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts +61 -0
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -0
- package/build-types/hooks/use-crop-gesture-handlers.d.ts +42 -0
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -0
- package/build-types/image-editor/core/constants.d.ts +9 -0
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/containment.d.ts +11 -7
- package/build-types/image-editor/core/containment.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts +14 -2
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/core/state.d.ts.map +1 -1
- package/build-types/image-editor/core/transform-style.d.ts +1 -1
- package/build-types/image-editor/core/types.d.ts +25 -0
- package/build-types/image-editor/core/types.d.ts.map +1 -1
- package/build-types/image-editor/core/viewport-state.d.ts +7 -0
- package/build-types/image-editor/core/viewport-state.d.ts.map +1 -0
- package/build-types/image-editor/react/components/cropper-provider.d.ts +1 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +2 -1
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +3 -13
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +1 -1
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/components/viewport-provider.d.ts +9 -0
- package/build-types/image-editor/react/components/viewport-provider.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +29 -0
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts +5 -2
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-viewport-state.d.ts +19 -0
- package/build-types/image-editor/react/hooks/use-viewport-state.d.ts.map +1 -0
- package/build-types/image-editor/react/visually-hidden-style.d.ts +6 -0
- package/build-types/image-editor/react/visually-hidden-style.d.ts.map +1 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
- package/build-types/private-apis.d.ts.map +1 -1
- package/build-types/store/actions.d.ts +3 -3
- package/build-types/store/actions.d.ts.map +1 -1
- package/package.json +16 -15
- package/src/components/media-editor/index.tsx +709 -0
- package/src/components/media-editor/style.scss +89 -0
- package/src/components/media-editor-canvas/index.tsx +24 -0
- package/src/components/media-editor-crop-panel/index.tsx +39 -27
- package/src/components/media-editor-crop-panel/test/index.tsx +109 -0
- package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +177 -0
- package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +53 -0
- package/src/components/media-editor-modal/index.tsx +49 -589
- package/src/components/media-editor-modal/style.scss +13 -112
- package/src/components/media-editor-toolbar/index.tsx +122 -63
- package/src/components/media-editor-toolbar/style.scss +39 -11
- package/src/components/media-form/index.tsx +5 -0
- package/src/components/rotation-ruler/README.md +37 -0
- package/src/components/rotation-ruler/index.tsx +266 -0
- package/src/components/rotation-ruler/stories/index.story.tsx +54 -0
- package/src/components/rotation-ruler/style.scss +147 -0
- package/src/components/rotation-ruler/test/index.tsx +109 -0
- package/src/components/rotation-ruler/use-ruler-drag.ts +230 -0
- package/src/hooks/use-crop-gesture-handlers.ts +54 -0
- package/src/image-editor/core/constants.ts +12 -0
- package/src/image-editor/core/containment.ts +16 -28
- package/src/image-editor/core/interaction-controller.ts +66 -10
- package/src/image-editor/core/state.ts +15 -8
- package/src/image-editor/core/test/camera.ts +15 -26
- package/src/image-editor/core/test/interaction-controller.ts +158 -7
- package/src/image-editor/core/test/state.ts +21 -1
- package/src/image-editor/core/transform-style.ts +1 -1
- package/src/image-editor/core/types.ts +16 -0
- package/src/image-editor/core/viewport-state.ts +33 -0
- package/src/image-editor/docs/architecture.md +1 -1
- package/src/image-editor/docs/recipes.md +2 -2
- package/src/image-editor/react/components/cropper.scss +28 -0
- package/src/image-editor/react/components/cropper.tsx +268 -72
- package/src/image-editor/react/components/overlays/dimming-overlay.tsx +12 -12
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +27 -8
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +29 -3
- package/src/image-editor/react/components/test/cropper.tsx +294 -5
- package/src/image-editor/react/components/viewport-provider.tsx +39 -0
- package/src/image-editor/react/hooks/test/use-aria-announcer.ts +81 -0
- package/src/image-editor/react/hooks/test/use-cropper-state.ts +286 -0
- package/src/image-editor/react/hooks/test/use-interaction.ts +1 -0
- package/src/image-editor/react/hooks/use-aria-announcer.ts +68 -7
- package/src/image-editor/react/hooks/use-cropper-state.ts +230 -11
- package/src/image-editor/react/hooks/use-interaction.ts +36 -6
- package/src/image-editor/react/hooks/use-viewport-state.ts +48 -0
- package/src/image-editor/react/visually-hidden-style.ts +15 -0
- package/src/private-apis.ts +2 -0
- package/src/style.scss +3 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -123,6 +123,41 @@ describe( 'useCropperState', () => {
|
|
|
123
123
|
} );
|
|
124
124
|
} );
|
|
125
125
|
|
|
126
|
+
it( 'toggleFlip should toggle horizontal flip', () => {
|
|
127
|
+
const { result } = renderHook( () => useCropperState() );
|
|
128
|
+
|
|
129
|
+
act( () => {
|
|
130
|
+
result.current.toggleFlip( 'horizontal' );
|
|
131
|
+
} );
|
|
132
|
+
|
|
133
|
+
expect( result.current.state.flip ).toEqual( {
|
|
134
|
+
horizontal: true,
|
|
135
|
+
vertical: false,
|
|
136
|
+
} );
|
|
137
|
+
|
|
138
|
+
act( () => {
|
|
139
|
+
result.current.toggleFlip( 'horizontal' );
|
|
140
|
+
} );
|
|
141
|
+
|
|
142
|
+
expect( result.current.state.flip ).toEqual( {
|
|
143
|
+
horizontal: false,
|
|
144
|
+
vertical: false,
|
|
145
|
+
} );
|
|
146
|
+
} );
|
|
147
|
+
|
|
148
|
+
it( 'toggleFlip should toggle vertical flip independently', () => {
|
|
149
|
+
const { result } = renderHook( () => useCropperState() );
|
|
150
|
+
|
|
151
|
+
act( () => {
|
|
152
|
+
result.current.toggleFlip( 'vertical' );
|
|
153
|
+
} );
|
|
154
|
+
|
|
155
|
+
expect( result.current.state.flip ).toEqual( {
|
|
156
|
+
horizontal: false,
|
|
157
|
+
vertical: true,
|
|
158
|
+
} );
|
|
159
|
+
} );
|
|
160
|
+
|
|
126
161
|
it( 'should dispatch SET_CROP_RECT via setCropRect', () => {
|
|
127
162
|
const { result } = renderHook( () => useCropperState() );
|
|
128
163
|
|
|
@@ -827,6 +862,257 @@ describe( 'useCropperState', () => {
|
|
|
827
862
|
} );
|
|
828
863
|
} );
|
|
829
864
|
|
|
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
|
+
|
|
830
1116
|
describe( 'public controller contract', () => {
|
|
831
1117
|
it( 'does not expose the raw reducer dispatch', () => {
|
|
832
1118
|
const { result } = renderHook( () => useCropperState() );
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
useState,
|
|
6
|
+
useEffect,
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useRef,
|
|
9
|
+
} from '@wordpress/element';
|
|
10
|
+
import { __, sprintf } from '@wordpress/i18n';
|
|
5
11
|
|
|
6
12
|
/**
|
|
7
13
|
* Internal dependencies
|
|
@@ -11,16 +17,63 @@ import type { CropperState } from '../../core/types';
|
|
|
11
17
|
/** Debounce delay for ARIA live announcements (ms). */
|
|
12
18
|
const ARIA_DEBOUNCE_MS = 300;
|
|
13
19
|
|
|
20
|
+
function buildFlipAnnouncement( state: CropperState ): string {
|
|
21
|
+
const { horizontal, vertical } = state.flip;
|
|
22
|
+
if ( horizontal && vertical ) {
|
|
23
|
+
return __( 'Flipped horizontally and vertically' );
|
|
24
|
+
}
|
|
25
|
+
if ( horizontal ) {
|
|
26
|
+
return __( 'Flipped horizontally' );
|
|
27
|
+
}
|
|
28
|
+
if ( vertical ) {
|
|
29
|
+
return __( 'Flipped vertically' );
|
|
30
|
+
}
|
|
31
|
+
return __( 'Flip removed' );
|
|
32
|
+
}
|
|
33
|
+
|
|
14
34
|
// Build a human-readable announcement string from cropper state.
|
|
15
|
-
function buildAnnouncement(
|
|
35
|
+
function buildAnnouncement(
|
|
36
|
+
state: CropperState,
|
|
37
|
+
previousState: CropperState | null
|
|
38
|
+
): string {
|
|
39
|
+
if (
|
|
40
|
+
previousState &&
|
|
41
|
+
( previousState.flip.horizontal !== state.flip.horizontal ||
|
|
42
|
+
previousState.flip.vertical !== state.flip.vertical )
|
|
43
|
+
) {
|
|
44
|
+
return buildFlipAnnouncement( state );
|
|
45
|
+
}
|
|
46
|
+
|
|
16
47
|
const parts: string[] = [];
|
|
17
|
-
parts.push(
|
|
48
|
+
parts.push(
|
|
49
|
+
sprintf(
|
|
50
|
+
/* translators: %d: zoom level as a percentage. */
|
|
51
|
+
__( 'Zoom %d%%' ),
|
|
52
|
+
Math.round( state.zoom * 100 )
|
|
53
|
+
)
|
|
54
|
+
);
|
|
18
55
|
if ( state.rotation !== 0 ) {
|
|
19
|
-
parts.push(
|
|
56
|
+
parts.push(
|
|
57
|
+
sprintf(
|
|
58
|
+
/* translators: %d: rotation angle in degrees. */
|
|
59
|
+
__( 'Rotation %d degrees' ),
|
|
60
|
+
Math.round( state.rotation )
|
|
61
|
+
)
|
|
62
|
+
);
|
|
20
63
|
}
|
|
21
64
|
const cropW = Math.round( state.cropRect.width * 100 );
|
|
22
65
|
const cropH = Math.round( state.cropRect.height * 100 );
|
|
23
|
-
parts.push(
|
|
66
|
+
parts.push(
|
|
67
|
+
sprintf(
|
|
68
|
+
/* translators: 1: crop width as a percentage, 2: crop height as a percentage. */
|
|
69
|
+
__( 'Crop %1$d%% by %2$d%%' ),
|
|
70
|
+
cropW,
|
|
71
|
+
cropH
|
|
72
|
+
)
|
|
73
|
+
);
|
|
74
|
+
if ( state.flip.horizontal || state.flip.vertical ) {
|
|
75
|
+
parts.push( buildFlipAnnouncement( state ) );
|
|
76
|
+
}
|
|
24
77
|
return parts.join( ', ' );
|
|
25
78
|
}
|
|
26
79
|
|
|
@@ -38,11 +91,18 @@ export function useAriaAnnouncer( state: CropperState ): string {
|
|
|
38
91
|
const [ ariaMessage, setAriaMessage ] = useState( '' );
|
|
39
92
|
const timerRef = useRef< ReturnType< typeof setTimeout > >();
|
|
40
93
|
const prevMessageRef = useRef( '' );
|
|
94
|
+
const prevStateRef = useRef< CropperState | null >( null );
|
|
95
|
+
const latestStateRef = useRef( state );
|
|
96
|
+
useLayoutEffect( () => {
|
|
97
|
+
latestStateRef.current = state;
|
|
98
|
+
}, [ state ] );
|
|
41
99
|
|
|
42
100
|
useEffect( () => {
|
|
43
101
|
clearTimeout( timerRef.current );
|
|
44
102
|
timerRef.current = setTimeout( () => {
|
|
45
|
-
const
|
|
103
|
+
const current = latestStateRef.current;
|
|
104
|
+
const msg = buildAnnouncement( current, prevStateRef.current );
|
|
105
|
+
prevStateRef.current = current;
|
|
46
106
|
if ( msg !== prevMessageRef.current ) {
|
|
47
107
|
prevMessageRef.current = msg;
|
|
48
108
|
setAriaMessage( msg );
|
|
@@ -57,7 +117,8 @@ export function useAriaAnnouncer( state: CropperState ): string {
|
|
|
57
117
|
state.rotation,
|
|
58
118
|
state.cropRect.width,
|
|
59
119
|
state.cropRect.height,
|
|
60
|
-
state,
|
|
120
|
+
state.flip.horizontal,
|
|
121
|
+
state.flip.vertical,
|
|
61
122
|
] );
|
|
62
123
|
|
|
63
124
|
return ariaMessage;
|