@wordpress/media-editor 0.7.1-next.v.202604201441.0 → 0.8.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-canvas/index.cjs +54 -0
- package/build/components/media-editor-canvas/index.cjs.map +7 -0
- package/build/components/media-editor-crop-panel/index.cjs +116 -0
- package/build/components/media-editor-crop-panel/index.cjs.map +7 -0
- package/build/components/media-editor-modal/build-modifiers.cjs +91 -0
- package/build/components/media-editor-modal/build-modifiers.cjs.map +7 -0
- package/build/components/media-editor-modal/index.cjs +497 -0
- package/build/components/media-editor-modal/index.cjs.map +7 -0
- package/build/components/media-editor-toolbar/index.cjs +146 -0
- package/build/components/media-editor-toolbar/index.cjs.map +7 -0
- package/build/image-editor/core/camera.cjs +194 -0
- package/build/image-editor/core/camera.cjs.map +7 -0
- package/build/image-editor/core/constants.cjs +81 -0
- package/build/image-editor/core/constants.cjs.map +7 -0
- package/build/image-editor/core/containment.cjs +245 -0
- package/build/image-editor/core/containment.cjs.map +7 -0
- package/build/image-editor/core/export/canvas-renderer.cjs +151 -0
- package/build/image-editor/core/export/canvas-renderer.cjs.map +7 -0
- package/build/image-editor/core/index.cjs +50 -0
- package/build/image-editor/core/index.cjs.map +7 -0
- package/build/image-editor/core/interaction-controller.cjs +608 -0
- package/build/image-editor/core/interaction-controller.cjs.map +7 -0
- package/build/image-editor/core/math/rotation.cjs +41 -0
- package/build/image-editor/core/math/rotation.cjs.map +7 -0
- package/build/image-editor/core/source-region.cjs +95 -0
- package/build/image-editor/core/source-region.cjs.map +7 -0
- package/build/image-editor/core/state.cjs +269 -0
- package/build/image-editor/core/state.cjs.map +7 -0
- package/build/image-editor/core/stencil-math.cjs +197 -0
- package/build/image-editor/core/stencil-math.cjs.map +7 -0
- package/build/image-editor/core/transform-style.cjs +46 -0
- package/build/image-editor/core/transform-style.cjs.map +7 -0
- package/build/image-editor/core/transforms/pipeline.cjs +43 -0
- package/build/image-editor/core/transforms/pipeline.cjs.map +7 -0
- package/build/image-editor/core/types.cjs +19 -0
- package/build/image-editor/core/types.cjs.map +7 -0
- package/build/image-editor/index.cjs +57 -0
- package/build/image-editor/index.cjs.map +7 -0
- package/build/image-editor/react/components/cropper-provider.cjs +50 -0
- package/build/image-editor/react/components/cropper-provider.cjs.map +7 -0
- package/build/image-editor/react/components/cropper.cjs +365 -0
- package/build/image-editor/react/components/cropper.cjs.map +7 -0
- package/build/image-editor/react/components/index.cjs +36 -0
- package/build/image-editor/react/components/index.cjs.map +7 -0
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +58 -0
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +7 -0
- package/build/image-editor/react/components/overlays/grid-overlay.cjs +91 -0
- package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +7 -0
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +351 -0
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +7 -0
- package/build/image-editor/react/hooks/index.cjs +31 -0
- package/build/image-editor/react/hooks/index.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +68 -0
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-cropper-state.cjs +158 -0
- package/build/image-editor/react/hooks/use-cropper-state.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-interaction.cjs +161 -0
- package/build/image-editor/react/hooks/use-interaction.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-transform-style.cjs +38 -0
- package/build/image-editor/react/hooks/use-transform-style.cjs.map +7 -0
- package/build/index.cjs +5 -2
- package/build/index.cjs.map +2 -2
- package/build/lock-unlock.cjs +37 -0
- package/build/lock-unlock.cjs.map +7 -0
- package/build/private-apis.cjs +38 -0
- package/build/private-apis.cjs.map +7 -0
- package/build/store/actions.cjs +45 -0
- package/build/store/actions.cjs.map +7 -0
- package/build/store/constants.cjs +31 -0
- package/build/store/constants.cjs.map +7 -0
- package/build/store/index.cjs +51 -0
- package/build/store/index.cjs.map +7 -0
- package/build/store/reducer.cjs +51 -0
- package/build/store/reducer.cjs.map +7 -0
- package/build/store/selectors.cjs +43 -0
- package/build/store/selectors.cjs.map +7 -0
- package/build-module/components/media-editor-canvas/index.mjs +33 -0
- package/build-module/components/media-editor-canvas/index.mjs.map +7 -0
- package/build-module/components/media-editor-crop-panel/index.mjs +100 -0
- package/build-module/components/media-editor-crop-panel/index.mjs.map +7 -0
- package/build-module/components/media-editor-modal/build-modifiers.mjs +66 -0
- package/build-module/components/media-editor-modal/build-modifiers.mjs.map +7 -0
- package/build-module/components/media-editor-modal/index.mjs +483 -0
- package/build-module/components/media-editor-modal/index.mjs.map +7 -0
- package/build-module/components/media-editor-toolbar/index.mjs +130 -0
- package/build-module/components/media-editor-toolbar/index.mjs.map +7 -0
- package/build-module/image-editor/core/camera.mjs +163 -0
- package/build-module/image-editor/core/camera.mjs.map +7 -0
- package/build-module/image-editor/core/constants.mjs +51 -0
- package/build-module/image-editor/core/constants.mjs.map +7 -0
- package/build-module/image-editor/core/containment.mjs +218 -0
- package/build-module/image-editor/core/containment.mjs.map +7 -0
- package/build-module/image-editor/core/export/canvas-renderer.mjs +120 -0
- package/build-module/image-editor/core/export/canvas-renderer.mjs.map +7 -0
- package/build-module/image-editor/core/index.mjs +24 -0
- package/build-module/image-editor/core/index.mjs.map +7 -0
- package/build-module/image-editor/core/interaction-controller.mjs +583 -0
- package/build-module/image-editor/core/interaction-controller.mjs.map +7 -0
- package/build-module/image-editor/core/math/rotation.mjs +15 -0
- package/build-module/image-editor/core/math/rotation.mjs.map +7 -0
- package/build-module/image-editor/core/source-region.mjs +69 -0
- package/build-module/image-editor/core/source-region.mjs.map +7 -0
- package/build-module/image-editor/core/state.mjs +242 -0
- package/build-module/image-editor/core/state.mjs.map +7 -0
- package/build-module/image-editor/core/stencil-math.mjs +169 -0
- package/build-module/image-editor/core/stencil-math.mjs.map +7 -0
- package/build-module/image-editor/core/transform-style.mjs +21 -0
- package/build-module/image-editor/core/transform-style.mjs.map +7 -0
- package/build-module/image-editor/core/transforms/pipeline.mjs +17 -0
- package/build-module/image-editor/core/transforms/pipeline.mjs.map +7 -0
- package/build-module/image-editor/core/types.mjs +1 -0
- package/build-module/image-editor/core/types.mjs.map +7 -0
- package/build-module/image-editor/index.mjs +30 -0
- package/build-module/image-editor/index.mjs.map +7 -0
- package/build-module/image-editor/react/components/cropper-provider.mjs +26 -0
- package/build-module/image-editor/react/components/cropper-provider.mjs.map +7 -0
- package/build-module/image-editor/react/components/cropper.mjs +337 -0
- package/build-module/image-editor/react/components/cropper.mjs.map +7 -0
- package/build-module/image-editor/react/components/index.mjs +9 -0
- package/build-module/image-editor/react/components/index.mjs.map +7 -0
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +33 -0
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +7 -0
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +66 -0
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +7 -0
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +330 -0
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/index.mjs +6 -0
- package/build-module/image-editor/react/hooks/index.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +43 -0
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs +137 -0
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs +138 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-transform-style.mjs +13 -0
- package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +7 -0
- package/build-module/index.mjs +3 -1
- package/build-module/index.mjs.map +2 -2
- package/build-module/lock-unlock.mjs +11 -0
- package/build-module/lock-unlock.mjs.map +7 -0
- package/build-module/private-apis.mjs +13 -0
- package/build-module/private-apis.mjs.map +7 -0
- package/build-module/store/actions.mjs +19 -0
- package/build-module/store/actions.mjs.map +7 -0
- package/build-module/store/constants.mjs +6 -0
- package/build-module/store/constants.mjs.map +7 -0
- package/build-module/store/index.mjs +16 -0
- package/build-module/store/index.mjs.map +7 -0
- package/build-module/store/reducer.mjs +26 -0
- package/build-module/store/reducer.mjs.map +7 -0
- package/build-module/store/selectors.mjs +16 -0
- package/build-module/store/selectors.mjs.map +7 -0
- package/build-style/style-rtl.css +315 -0
- package/build-style/style.css +315 -0
- package/build-types/components/media-editor-canvas/index.d.ts +22 -0
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -0
- package/build-types/components/media-editor-crop-panel/index.d.ts +45 -0
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/build-modifiers.d.ts +83 -0
- package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/index.d.ts +20 -0
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -0
- package/build-types/components/media-editor-toolbar/index.d.ts +21 -0
- package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -0
- package/build-types/image-editor/core/camera.d.ts +107 -0
- package/build-types/image-editor/core/camera.d.ts.map +1 -0
- package/build-types/image-editor/core/constants.d.ts +46 -0
- package/build-types/image-editor/core/constants.d.ts.map +1 -0
- package/build-types/image-editor/core/containment.d.ts +61 -0
- package/build-types/image-editor/core/containment.d.ts.map +1 -0
- package/build-types/image-editor/core/export/canvas-renderer.d.ts +106 -0
- package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +1 -0
- package/build-types/image-editor/core/index.d.ts +8 -0
- package/build-types/image-editor/core/index.d.ts.map +1 -0
- package/build-types/image-editor/core/interaction-controller.d.ts +178 -0
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -0
- package/build-types/image-editor/core/math/rotation.d.ts +18 -0
- package/build-types/image-editor/core/math/rotation.d.ts.map +1 -0
- package/build-types/image-editor/core/source-region.d.ts +75 -0
- package/build-types/image-editor/core/source-region.d.ts.map +1 -0
- package/build-types/image-editor/core/state.d.ts +42 -0
- package/build-types/image-editor/core/state.d.ts.map +1 -0
- package/build-types/image-editor/core/stencil-math.d.ts +79 -0
- package/build-types/image-editor/core/stencil-math.d.ts.map +1 -0
- package/build-types/image-editor/core/transform-style.d.ts +23 -0
- package/build-types/image-editor/core/transform-style.d.ts.map +1 -0
- package/build-types/image-editor/core/transforms/pipeline.d.ts +27 -0
- package/build-types/image-editor/core/transforms/pipeline.d.ts.map +1 -0
- package/build-types/image-editor/core/types.d.ts +211 -0
- package/build-types/image-editor/core/types.d.ts.map +1 -0
- package/build-types/image-editor/index.d.ts +16 -0
- package/build-types/image-editor/index.d.ts.map +1 -0
- package/build-types/image-editor/react/components/cropper-provider.d.ts +37 -0
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -0
- package/build-types/image-editor/react/components/cropper.d.ts +59 -0
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -0
- package/build-types/image-editor/react/components/index.d.ts +4 -0
- package/build-types/image-editor/react/components/index.d.ts.map +1 -0
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +30 -0
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -0
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +30 -0
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -0
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +32 -0
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/index.d.ts +3 -0
- package/build-types/image-editor/react/hooks/index.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +16 -0
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +68 -0
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts +60 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-transform-style.d.ts +16 -0
- package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +1 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts +16 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -0
- package/build-types/index.d.ts +1 -0
- package/build-types/index.d.ts.map +1 -1
- package/build-types/lock-unlock.d.ts +2 -0
- package/build-types/lock-unlock.d.ts.map +1 -0
- package/build-types/private-apis.d.ts +2 -0
- package/build-types/private-apis.d.ts.map +1 -0
- package/build-types/store/actions.d.ts +27 -0
- package/build-types/store/actions.d.ts.map +1 -0
- package/build-types/store/constants.d.ts +2 -0
- package/build-types/store/constants.d.ts.map +1 -0
- package/build-types/store/index.d.ts +4 -0
- package/build-types/store/index.d.ts.map +1 -0
- package/build-types/store/reducer.d.ts +23 -0
- package/build-types/store/reducer.d.ts.map +1 -0
- package/build-types/store/selectors.d.ts +8 -0
- package/build-types/store/selectors.d.ts.map +1 -0
- package/package.json +21 -7
- package/src/components/media-editor-canvas/index.tsx +56 -0
- package/src/components/media-editor-canvas/style.scss +9 -0
- package/src/components/media-editor-crop-panel/index.tsx +143 -0
- package/src/components/media-editor-modal/build-modifiers.ts +164 -0
- package/src/components/media-editor-modal/index.tsx +639 -0
- package/src/components/media-editor-modal/style.scss +139 -0
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +343 -0
- package/src/components/media-editor-toolbar/index.tsx +150 -0
- package/src/components/media-editor-toolbar/style.scss +21 -0
- package/src/image-editor/README.md +162 -0
- package/src/image-editor/core/camera.ts +352 -0
- package/src/image-editor/core/constants.ts +93 -0
- package/src/image-editor/core/containment.ts +435 -0
- package/src/image-editor/core/export/canvas-renderer.ts +253 -0
- package/src/image-editor/core/export/test/canvas-renderer.ts +813 -0
- package/src/image-editor/core/index.ts +35 -0
- package/src/image-editor/core/interaction-controller.ts +889 -0
- package/src/image-editor/core/math/rotation.ts +26 -0
- package/src/image-editor/core/source-region.ts +166 -0
- package/src/image-editor/core/state.ts +428 -0
- package/src/image-editor/core/stencil-math.ts +330 -0
- package/src/image-editor/core/test/camera.ts +593 -0
- package/src/image-editor/core/test/interaction-controller.ts +996 -0
- package/src/image-editor/core/test/preview-export-parity.ts +242 -0
- package/src/image-editor/core/test/render-camera-bridge.ts +348 -0
- package/src/image-editor/core/test/state.ts +762 -0
- package/src/image-editor/core/test/stencil-math.ts +321 -0
- package/src/image-editor/core/transform-style.ts +44 -0
- package/src/image-editor/core/transforms/pipeline.ts +43 -0
- package/src/image-editor/core/transforms/test/pipeline.ts +269 -0
- package/src/image-editor/core/types.ts +177 -0
- package/src/image-editor/docs/architecture.md +96 -0
- package/src/image-editor/docs/recipes.md +720 -0
- package/src/image-editor/docs/roadmap.md +66 -0
- package/src/image-editor/index.ts +43 -0
- package/src/image-editor/react/components/cropper-provider.tsx +73 -0
- package/src/image-editor/react/components/cropper.scss +195 -0
- package/src/image-editor/react/components/cropper.tsx +534 -0
- package/src/image-editor/react/components/index.ts +3 -0
- package/src/image-editor/react/components/overlays/dimming-overlay.tsx +57 -0
- package/src/image-editor/react/components/overlays/grid-overlay.tsx +80 -0
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +513 -0
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +265 -0
- package/src/image-editor/react/components/test/cropper.tsx +140 -0
- package/src/image-editor/react/hooks/index.ts +2 -0
- package/src/image-editor/react/hooks/test/use-cropper-state.ts +913 -0
- package/src/image-editor/react/hooks/test/use-interaction.ts +218 -0
- package/src/image-editor/react/hooks/test/use-transform-style.ts +146 -0
- package/src/image-editor/react/hooks/use-aria-announcer.ts +64 -0
- package/src/image-editor/react/hooks/use-cropper-state.ts +238 -0
- package/src/image-editor/react/hooks/use-interaction.ts +223 -0
- package/src/image-editor/react/hooks/use-transform-style.ts +30 -0
- package/src/image-editor/stories/rectangle-crop.story.tsx +1085 -0
- package/src/image-editor/stories/style.css +94 -0
- package/src/image-editor/style.scss +1 -0
- package/src/index.ts +3 -0
- package/src/lock-unlock.ts +10 -0
- package/src/private-apis.ts +12 -0
- package/src/store/actions.ts +33 -0
- package/src/store/constants.ts +1 -0
- package/src/store/index.ts +20 -0
- package/src/store/reducer.ts +48 -0
- package/src/store/selectors.ts +16 -0
- package/src/store/test/reducer.ts +57 -0
- package/src/style.scss +4 -0
- package/tsconfig.json +10 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { act, renderHook } from '@testing-library/react';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import { useInteraction } from '../use-interaction';
|
|
10
|
+
import type { CropperInteractionActions } from '../../../core/interaction-controller';
|
|
11
|
+
import type { CropperState, Size } from '../../../core/types';
|
|
12
|
+
import { DEFAULT_STATE } from '../../../core/constants';
|
|
13
|
+
|
|
14
|
+
const CONTAINER_SIZE: Size = { width: 500, height: 300 };
|
|
15
|
+
const IMAGE_SIZE: Size = { width: 500, height: 300 };
|
|
16
|
+
|
|
17
|
+
function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
18
|
+
return {
|
|
19
|
+
...DEFAULT_STATE,
|
|
20
|
+
image: {
|
|
21
|
+
src: 'test.jpg',
|
|
22
|
+
naturalWidth: 1000,
|
|
23
|
+
naturalHeight: 600,
|
|
24
|
+
},
|
|
25
|
+
zoom: 2,
|
|
26
|
+
...overrides,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function createActions(): jest.Mocked< CropperInteractionActions > {
|
|
31
|
+
return {
|
|
32
|
+
setPan: jest.fn(),
|
|
33
|
+
setZoom: jest.fn(),
|
|
34
|
+
setZoomAtPoint: jest.fn(),
|
|
35
|
+
snapRotate90: jest.fn(),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function createWheelEvent(
|
|
40
|
+
overrides: Partial< WheelEvent > & { currentTarget?: unknown } = {}
|
|
41
|
+
): WheelEvent {
|
|
42
|
+
return {
|
|
43
|
+
preventDefault: jest.fn(),
|
|
44
|
+
deltaY: -100,
|
|
45
|
+
clientX: 0,
|
|
46
|
+
clientY: 0,
|
|
47
|
+
currentTarget: null,
|
|
48
|
+
...overrides,
|
|
49
|
+
} as unknown as WheelEvent;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function createKeyboardEvent( key: string ): React.KeyboardEvent {
|
|
53
|
+
return {
|
|
54
|
+
key,
|
|
55
|
+
nativeEvent: {
|
|
56
|
+
key,
|
|
57
|
+
preventDefault: jest.fn(),
|
|
58
|
+
},
|
|
59
|
+
} as unknown as React.KeyboardEvent;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function createTouchEvent(
|
|
63
|
+
touches: Array< { clientX: number; clientY: number } >
|
|
64
|
+
): TouchEvent {
|
|
65
|
+
return {
|
|
66
|
+
preventDefault: jest.fn(),
|
|
67
|
+
touches,
|
|
68
|
+
} as unknown as TouchEvent;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function createTouchDocument(): Document & {
|
|
72
|
+
_fire: ( type: string, event: unknown ) => void;
|
|
73
|
+
} {
|
|
74
|
+
const listeners: Record< string, EventListener[] > = {};
|
|
75
|
+
return {
|
|
76
|
+
addEventListener: jest.fn( ( type: string, fn: EventListener ) => {
|
|
77
|
+
if ( ! listeners[ type ] ) {
|
|
78
|
+
listeners[ type ] = [];
|
|
79
|
+
}
|
|
80
|
+
listeners[ type ].push( fn );
|
|
81
|
+
} ),
|
|
82
|
+
removeEventListener: jest.fn( ( type: string, fn: EventListener ) => {
|
|
83
|
+
if ( listeners[ type ] ) {
|
|
84
|
+
listeners[ type ] = listeners[ type ].filter(
|
|
85
|
+
( listener ) => listener !== fn
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
} ),
|
|
89
|
+
_fire( type: string, event: unknown ) {
|
|
90
|
+
listeners[ type ]?.forEach( ( fn ) =>
|
|
91
|
+
fn( event as unknown as Event )
|
|
92
|
+
);
|
|
93
|
+
},
|
|
94
|
+
} as unknown as Document & {
|
|
95
|
+
_fire: ( type: string, event: unknown ) => void;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function createTouchTarget( ownerDocument: Document ) {
|
|
100
|
+
return {
|
|
101
|
+
getBoundingClientRect: () => ( {
|
|
102
|
+
left: 0,
|
|
103
|
+
top: 0,
|
|
104
|
+
width: 500,
|
|
105
|
+
height: 300,
|
|
106
|
+
} ),
|
|
107
|
+
ownerDocument,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
describe( 'useInteraction grid placement signal', () => {
|
|
112
|
+
afterEach( () => {
|
|
113
|
+
jest.useRealTimers();
|
|
114
|
+
} );
|
|
115
|
+
|
|
116
|
+
it( 'sets isPlacementActive during keyboard pan, then clears it after idle', () => {
|
|
117
|
+
jest.useFakeTimers();
|
|
118
|
+
const { result } = renderHook( () =>
|
|
119
|
+
useInteraction(
|
|
120
|
+
makeState(),
|
|
121
|
+
createActions(),
|
|
122
|
+
CONTAINER_SIZE,
|
|
123
|
+
IMAGE_SIZE
|
|
124
|
+
)
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
act( () => {
|
|
128
|
+
result.current.handlers.onKeyDown(
|
|
129
|
+
createKeyboardEvent( 'ArrowRight' )
|
|
130
|
+
);
|
|
131
|
+
} );
|
|
132
|
+
|
|
133
|
+
expect( result.current.isPlacementActive ).toBe( true );
|
|
134
|
+
|
|
135
|
+
act( () => {
|
|
136
|
+
jest.advanceTimersByTime( 300 );
|
|
137
|
+
} );
|
|
138
|
+
|
|
139
|
+
expect( result.current.isPlacementActive ).toBe( false );
|
|
140
|
+
} );
|
|
141
|
+
|
|
142
|
+
it.each( [ '+', '-', 'r' ] )(
|
|
143
|
+
'does not set isPlacementActive for non-placement key %s',
|
|
144
|
+
( key ) => {
|
|
145
|
+
const { result } = renderHook( () =>
|
|
146
|
+
useInteraction(
|
|
147
|
+
makeState(),
|
|
148
|
+
createActions(),
|
|
149
|
+
CONTAINER_SIZE,
|
|
150
|
+
IMAGE_SIZE
|
|
151
|
+
)
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
act( () => {
|
|
155
|
+
result.current.handlers.onKeyDown( createKeyboardEvent( key ) );
|
|
156
|
+
} );
|
|
157
|
+
|
|
158
|
+
expect( result.current.isPlacementActive ).toBe( false );
|
|
159
|
+
}
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
it( 'sets isPlacementActive during wheel zoom, then clears it after the wheel debounce', () => {
|
|
163
|
+
jest.useFakeTimers();
|
|
164
|
+
const { result } = renderHook( () =>
|
|
165
|
+
useInteraction(
|
|
166
|
+
makeState(),
|
|
167
|
+
createActions(),
|
|
168
|
+
CONTAINER_SIZE,
|
|
169
|
+
IMAGE_SIZE
|
|
170
|
+
)
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
act( () => {
|
|
174
|
+
result.current.onWheelNative(
|
|
175
|
+
createWheelEvent( { deltaY: -100, currentTarget: null } )
|
|
176
|
+
);
|
|
177
|
+
} );
|
|
178
|
+
|
|
179
|
+
expect( result.current.isPlacementActive ).toBe( true );
|
|
180
|
+
|
|
181
|
+
act( () => {
|
|
182
|
+
jest.advanceTimersByTime( 300 );
|
|
183
|
+
} );
|
|
184
|
+
|
|
185
|
+
expect( result.current.isPlacementActive ).toBe( false );
|
|
186
|
+
} );
|
|
187
|
+
|
|
188
|
+
it( 'sets isPlacementActive during pinch zoom, then clears it on touch end', () => {
|
|
189
|
+
const doc = createTouchDocument();
|
|
190
|
+
const target = createTouchTarget( doc );
|
|
191
|
+
const { result } = renderHook( () =>
|
|
192
|
+
useInteraction(
|
|
193
|
+
makeState(),
|
|
194
|
+
createActions(),
|
|
195
|
+
CONTAINER_SIZE,
|
|
196
|
+
IMAGE_SIZE
|
|
197
|
+
)
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
act( () => {
|
|
201
|
+
result.current.handlers.onTouchStart( {
|
|
202
|
+
currentTarget: target,
|
|
203
|
+
nativeEvent: createTouchEvent( [
|
|
204
|
+
{ clientX: 200, clientY: 150 },
|
|
205
|
+
{ clientX: 300, clientY: 150 },
|
|
206
|
+
] ),
|
|
207
|
+
} as unknown as React.TouchEvent );
|
|
208
|
+
} );
|
|
209
|
+
|
|
210
|
+
expect( result.current.isPlacementActive ).toBe( true );
|
|
211
|
+
|
|
212
|
+
act( () => {
|
|
213
|
+
doc._fire( 'touchend', createTouchEvent( [] ) );
|
|
214
|
+
} );
|
|
215
|
+
|
|
216
|
+
expect( result.current.isPlacementActive ).toBe( false );
|
|
217
|
+
} );
|
|
218
|
+
} );
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { renderHook } from '@testing-library/react';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import { useTransformStyle } from '../use-transform-style';
|
|
10
|
+
import type { CropperState, Size } from '../../../core/types';
|
|
11
|
+
import { DEFAULT_STATE } from '../../../core/constants';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Create a mock state with optional overrides.
|
|
15
|
+
* @param overrides
|
|
16
|
+
*/
|
|
17
|
+
function createState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
18
|
+
return { ...DEFAULT_STATE, ...overrides };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describe( 'useTransformStyle', () => {
|
|
22
|
+
const imageSize: Size = { width: 400, height: 300 };
|
|
23
|
+
|
|
24
|
+
it( 'should return identity-like matrix for default state', () => {
|
|
25
|
+
const state = createState();
|
|
26
|
+
const { result } = renderHook( () =>
|
|
27
|
+
useTransformStyle( state, imageSize )
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// Default: no rotation, no flip, zoom=1, crop=(0,0)
|
|
31
|
+
// a=cos(0)*1*1=1, b=sin(0)*1*1=0, c=-sin(0)*1*1=0, d=cos(0)*1*1=1, tx=0, ty=0
|
|
32
|
+
expect( result.current ).toBe( 'matrix(1, 0, 0, 1, 0, 0)' );
|
|
33
|
+
} );
|
|
34
|
+
|
|
35
|
+
it( 'should include translation from crop offset', () => {
|
|
36
|
+
const state = createState( { pan: { x: 0.5, y: 0.25 } } );
|
|
37
|
+
const { result } = renderHook( () =>
|
|
38
|
+
useTransformStyle( state, imageSize )
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
// 0.5 * 400 = 200, 0.25 * 300 = 75
|
|
42
|
+
// No rotation, no flip, zoom=1: matrix(1, 0, 0, 1, 200, 75)
|
|
43
|
+
expect( result.current ).toBe( 'matrix(1, 0, 0, 1, 200, 75)' );
|
|
44
|
+
} );
|
|
45
|
+
|
|
46
|
+
it( 'should apply rotation=90 producing correct sin/cos values', () => {
|
|
47
|
+
const state = createState( { rotation: 90 } );
|
|
48
|
+
const { result } = renderHook( () =>
|
|
49
|
+
useTransformStyle( state, imageSize )
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// cos(90°)≈0, sin(90°)≈1, no flip, zoom=1
|
|
53
|
+
// a=0*1*1≈0, b=1*1*1≈1, c=-1*1*1≈-1, d=0*1*1≈0
|
|
54
|
+
const transform = result.current;
|
|
55
|
+
// Parse matrix values
|
|
56
|
+
const match = transform.match(
|
|
57
|
+
/matrix\(([^,]+), ([^,]+), ([^,]+), ([^,]+), ([^,]+), ([^)]+)\)/
|
|
58
|
+
);
|
|
59
|
+
expect( match ).not.toBeNull();
|
|
60
|
+
const [ , a, b, c, d ] = match!.map( Number );
|
|
61
|
+
expect( a ).toBeCloseTo( 0 );
|
|
62
|
+
expect( b ).toBeCloseTo( 1 );
|
|
63
|
+
expect( c ).toBeCloseTo( -1 );
|
|
64
|
+
expect( d ).toBeCloseTo( 0 );
|
|
65
|
+
} );
|
|
66
|
+
|
|
67
|
+
it( 'should apply zoom=2 producing scale factor 2 in matrix', () => {
|
|
68
|
+
const state = createState( { zoom: 2 } );
|
|
69
|
+
const { result } = renderHook( () =>
|
|
70
|
+
useTransformStyle( state, imageSize )
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
// cos(0)=1, sin(0)=0, no flip, zoom=2
|
|
74
|
+
// a=1*1*2=2, b=0, c=0, d=1*1*2=2
|
|
75
|
+
expect( result.current ).toBe( 'matrix(2, 0, 0, 2, 0, 0)' );
|
|
76
|
+
} );
|
|
77
|
+
|
|
78
|
+
it( 'should apply horizontal flip producing negative a and b values', () => {
|
|
79
|
+
const state = createState( {
|
|
80
|
+
flip: { horizontal: true, vertical: false },
|
|
81
|
+
} );
|
|
82
|
+
const { result } = renderHook( () =>
|
|
83
|
+
useTransformStyle( state, imageSize )
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// cos(0)=1, sin(0)=0, sx=-1, sy=1, zoom=1
|
|
87
|
+
// a=1*(-1)*1=-1, b=0*(-1)*1=0, c=-0*1*1=0, d=1*1*1=1
|
|
88
|
+
expect( result.current ).toBe( 'matrix(-1, 0, 0, 1, 0, 0)' );
|
|
89
|
+
} );
|
|
90
|
+
|
|
91
|
+
it( 'should apply vertical flip producing negative c and d values', () => {
|
|
92
|
+
const state = createState( {
|
|
93
|
+
flip: { horizontal: false, vertical: true },
|
|
94
|
+
} );
|
|
95
|
+
const { result } = renderHook( () =>
|
|
96
|
+
useTransformStyle( state, imageSize )
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
// cos(0)=1, sin(0)=0, sx=1, sy=-1, zoom=1
|
|
100
|
+
// a=1*1*1=1, b=0, c=-0*(-1)*1=0, d=1*(-1)*1=-1
|
|
101
|
+
expect( result.current ).toBe( 'matrix(1, 0, 0, -1, 0, 0)' );
|
|
102
|
+
} );
|
|
103
|
+
|
|
104
|
+
it( 'should apply both flips producing negative a and d values', () => {
|
|
105
|
+
const state = createState( {
|
|
106
|
+
flip: { horizontal: true, vertical: true },
|
|
107
|
+
} );
|
|
108
|
+
const { result } = renderHook( () =>
|
|
109
|
+
useTransformStyle( state, imageSize )
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
// sx=-1, sy=-1, no rotation, zoom=1
|
|
113
|
+
// a=1*(-1)*1=-1, b=0, c=0, d=1*(-1)*1=-1
|
|
114
|
+
expect( result.current ).toBe( 'matrix(-1, 0, 0, -1, 0, 0)' );
|
|
115
|
+
} );
|
|
116
|
+
|
|
117
|
+
it( 'should combine all transforms correctly', () => {
|
|
118
|
+
const state = createState( {
|
|
119
|
+
pan: { x: 0.1, y: 0.2 },
|
|
120
|
+
rotation: 90,
|
|
121
|
+
flip: { horizontal: true, vertical: false },
|
|
122
|
+
zoom: 3,
|
|
123
|
+
} );
|
|
124
|
+
const { result } = renderHook( () =>
|
|
125
|
+
useTransformStyle( state, imageSize )
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
// tx = 0.1 * 400 = 40, ty = 0.2 * 300 = 60
|
|
129
|
+
// Matrix order is flip * rotate * zoom (viewport-relative flip).
|
|
130
|
+
// cos(90°)≈0, sin(90°)≈1, sx=-1, sy=1, z=3
|
|
131
|
+
// a=sx*cos*z=(-1)*0*3≈0, b=sy*sin*z=1*1*3≈3
|
|
132
|
+
// c=-sx*sin*z=-(-1)*1*3≈3, d=sy*cos*z=1*0*3≈0
|
|
133
|
+
const transform = result.current;
|
|
134
|
+
const match = transform.match(
|
|
135
|
+
/matrix\(([^,]+), ([^,]+), ([^,]+), ([^,]+), ([^,]+), ([^)]+)\)/
|
|
136
|
+
);
|
|
137
|
+
expect( match ).not.toBeNull();
|
|
138
|
+
const [ , a, b, c, d, tx, ty ] = match!.map( Number );
|
|
139
|
+
expect( a ).toBeCloseTo( 0 );
|
|
140
|
+
expect( b ).toBeCloseTo( 3 );
|
|
141
|
+
expect( c ).toBeCloseTo( 3 );
|
|
142
|
+
expect( d ).toBeCloseTo( 0 );
|
|
143
|
+
expect( tx ).toBeCloseTo( 40 );
|
|
144
|
+
expect( ty ).toBeCloseTo( 60 );
|
|
145
|
+
} );
|
|
146
|
+
} );
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { useState, useEffect, useRef } from '@wordpress/element';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import type { CropperState } from '../../core/types';
|
|
10
|
+
|
|
11
|
+
/** Debounce delay for ARIA live announcements (ms). */
|
|
12
|
+
const ARIA_DEBOUNCE_MS = 300;
|
|
13
|
+
|
|
14
|
+
// Build a human-readable announcement string from cropper state.
|
|
15
|
+
function buildAnnouncement( state: CropperState ): string {
|
|
16
|
+
const parts: string[] = [];
|
|
17
|
+
parts.push( `Zoom ${ Math.round( state.zoom * 100 ) }%` );
|
|
18
|
+
if ( state.rotation !== 0 ) {
|
|
19
|
+
parts.push( `Rotation ${ Math.round( state.rotation ) } degrees` );
|
|
20
|
+
}
|
|
21
|
+
const cropW = Math.round( state.cropRect.width * 100 );
|
|
22
|
+
const cropH = Math.round( state.cropRect.height * 100 );
|
|
23
|
+
parts.push( `Crop ${ cropW }% by ${ cropH }%` );
|
|
24
|
+
return parts.join( ', ' );
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Debounce and dedupe ARIA-live announcements for screen readers as the
|
|
29
|
+
* cropper state changes. Returns the current announcement message to render
|
|
30
|
+
* inside an `aria-live="polite"` region.
|
|
31
|
+
*
|
|
32
|
+
* Debouncing avoids flooding the live region during drag/pointermove bursts;
|
|
33
|
+
* deduping avoids re-announcing the same state.
|
|
34
|
+
*
|
|
35
|
+
* @param state The current cropper state to announce.
|
|
36
|
+
*/
|
|
37
|
+
export function useAriaAnnouncer( state: CropperState ): string {
|
|
38
|
+
const [ ariaMessage, setAriaMessage ] = useState( '' );
|
|
39
|
+
const timerRef = useRef< ReturnType< typeof setTimeout > >();
|
|
40
|
+
const prevMessageRef = useRef( '' );
|
|
41
|
+
|
|
42
|
+
useEffect( () => {
|
|
43
|
+
clearTimeout( timerRef.current );
|
|
44
|
+
timerRef.current = setTimeout( () => {
|
|
45
|
+
const msg = buildAnnouncement( state );
|
|
46
|
+
if ( msg !== prevMessageRef.current ) {
|
|
47
|
+
prevMessageRef.current = msg;
|
|
48
|
+
setAriaMessage( msg );
|
|
49
|
+
}
|
|
50
|
+
}, ARIA_DEBOUNCE_MS );
|
|
51
|
+
|
|
52
|
+
return () => {
|
|
53
|
+
clearTimeout( timerRef.current );
|
|
54
|
+
};
|
|
55
|
+
}, [
|
|
56
|
+
state.zoom,
|
|
57
|
+
state.rotation,
|
|
58
|
+
state.cropRect.width,
|
|
59
|
+
state.cropRect.height,
|
|
60
|
+
state,
|
|
61
|
+
] );
|
|
62
|
+
|
|
63
|
+
return ariaMessage;
|
|
64
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { useReducer, useCallback, useRef } from '@wordpress/element';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import type {
|
|
10
|
+
CropperState,
|
|
11
|
+
TransformOperation,
|
|
12
|
+
NormalizedPoint,
|
|
13
|
+
NormalizedRect,
|
|
14
|
+
Flip,
|
|
15
|
+
} from '../../core/types';
|
|
16
|
+
import { DEFAULT_STATE } from '../../core/constants';
|
|
17
|
+
import { exportCroppedImage } from '../../core/export/canvas-renderer';
|
|
18
|
+
import {
|
|
19
|
+
cropperReducer,
|
|
20
|
+
enforceContainment,
|
|
21
|
+
isStateDirty,
|
|
22
|
+
} from '../../core/state';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The return type of the useCropperState hook.
|
|
26
|
+
*
|
|
27
|
+
* The hook exposes its state through named setter methods rather
|
|
28
|
+
* than a raw dispatch function. Each setter has a specific
|
|
29
|
+
* behavioral contract (see individual JSDoc) that maps 1:1 to a
|
|
30
|
+
* reducer action internally. Keeping the reducer actions private
|
|
31
|
+
* lets the implementation evolve without breaking consumers.
|
|
32
|
+
*/
|
|
33
|
+
export interface UseCropperStateReturn {
|
|
34
|
+
/** The current cropper state (read-only). */
|
|
35
|
+
state: CropperState;
|
|
36
|
+
/** Set the loaded image (natural size and src). */
|
|
37
|
+
setImage: ( image: CropperState[ 'image' ] ) => void;
|
|
38
|
+
/**
|
|
39
|
+
* Set the image pan offset in normalized coordinates. Use
|
|
40
|
+
* `setCropRect` for the crop rectangle.
|
|
41
|
+
*/
|
|
42
|
+
setPan: ( pan: NormalizedPoint ) => void;
|
|
43
|
+
/** Set the zoom level. Clamped to [1, 10]. */
|
|
44
|
+
setZoom: ( zoom: number ) => void;
|
|
45
|
+
/** Set zoom and pan together so focal-point zoom remains atomic. */
|
|
46
|
+
setZoomAtPoint: ( zoom: number, pan: NormalizedPoint ) => void;
|
|
47
|
+
/** Set the rotation in degrees. Normalized to [0, 360). */
|
|
48
|
+
setRotation: ( rotation: number ) => void;
|
|
49
|
+
/** Set the flip state. */
|
|
50
|
+
setFlip: ( flip: Flip ) => void;
|
|
51
|
+
/** Snap rotate 90° preserving the image selection (Google Photos style). */
|
|
52
|
+
snapRotate90: ( direction: 1 | -1 ) => void;
|
|
53
|
+
/** Set the crop rectangle in normalized coordinates. */
|
|
54
|
+
setCropRect: ( rect: NormalizedRect ) => void;
|
|
55
|
+
/**
|
|
56
|
+
* Settle the crop rect after a resize drag: expand to fill the
|
|
57
|
+
* available visual area while preserving the framed image region.
|
|
58
|
+
* Typically called from a stencil's `onResizeEnd` callback.
|
|
59
|
+
*/
|
|
60
|
+
settleCrop: () => void;
|
|
61
|
+
/** Apply a transform operation through the pipeline. */
|
|
62
|
+
applyOperation: ( op: TransformOperation ) => void;
|
|
63
|
+
/** Reset the state. Optionally merge partial state overrides. */
|
|
64
|
+
reset: ( resetState?: Partial< CropperState > ) => void;
|
|
65
|
+
/** Whether the current state differs from the initial state. */
|
|
66
|
+
isDirty: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Export the cropped image as a Blob. Throws on failure — see
|
|
69
|
+
* `exportCroppedImage` in core for the error semantics (image
|
|
70
|
+
* load errors, CORS taint, missing canvas context). Wrap in
|
|
71
|
+
* try/catch if you need to recover.
|
|
72
|
+
*/
|
|
73
|
+
getCroppedImage: ( mimeType?: string, quality?: number ) => Promise< Blob >;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Reducer-based state management hook for the image editor.
|
|
78
|
+
*
|
|
79
|
+
* Provides the full cropper state and named setter methods for
|
|
80
|
+
* every supported transition. The reducer and containment logic
|
|
81
|
+
* live in core/state.ts (framework-agnostic); this hook is a thin
|
|
82
|
+
* React wrapper around that pure reducer, with memoized callbacks.
|
|
83
|
+
*
|
|
84
|
+
* @param initialState Optional partial state to merge with DEFAULT_STATE.
|
|
85
|
+
* @return The cropper state, setters, and utilities.
|
|
86
|
+
*/
|
|
87
|
+
export function useCropperState(
|
|
88
|
+
initialState?: Partial< CropperState >
|
|
89
|
+
): UseCropperStateReturn {
|
|
90
|
+
const [ state, dispatch ] = useReducer(
|
|
91
|
+
cropperReducer,
|
|
92
|
+
initialState,
|
|
93
|
+
( init ) => enforceContainment( { ...DEFAULT_STATE, ...init } )
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const initialRef = useRef< CropperState >(
|
|
97
|
+
enforceContainment( { ...DEFAULT_STATE, ...initialState } )
|
|
98
|
+
);
|
|
99
|
+
// Keep a ref to the latest state so callbacks with stable identity
|
|
100
|
+
// (reset, setImage) can read fresh state without re-creating themselves.
|
|
101
|
+
const stateRef = useRef( state );
|
|
102
|
+
stateRef.current = state;
|
|
103
|
+
|
|
104
|
+
const setImage = useCallback(
|
|
105
|
+
( image: CropperState[ 'image' ] ) => {
|
|
106
|
+
dispatch( { type: 'SET_IMAGE', payload: image } );
|
|
107
|
+
// Refresh the "clean" snapshot to match the post-load state
|
|
108
|
+
// produced by the reducer. Otherwise containment can nudge
|
|
109
|
+
// pan/zoom by tiny amounts on load and `isDirty` would
|
|
110
|
+
// report true from the start.
|
|
111
|
+
initialRef.current = enforceContainment( {
|
|
112
|
+
...initialRef.current,
|
|
113
|
+
image,
|
|
114
|
+
} );
|
|
115
|
+
},
|
|
116
|
+
[ dispatch ]
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
const setPan = useCallback(
|
|
120
|
+
( pan: NormalizedPoint ) => {
|
|
121
|
+
dispatch( { type: 'SET_PAN', payload: pan } );
|
|
122
|
+
},
|
|
123
|
+
[ dispatch ]
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const setZoom = useCallback(
|
|
127
|
+
( zoom: number ) => {
|
|
128
|
+
dispatch( { type: 'SET_ZOOM', payload: zoom } );
|
|
129
|
+
},
|
|
130
|
+
[ dispatch ]
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const setZoomAtPoint = useCallback(
|
|
134
|
+
( zoom: number, pan: NormalizedPoint ) => {
|
|
135
|
+
dispatch( {
|
|
136
|
+
type: 'SET_ZOOM_AT_POINT',
|
|
137
|
+
payload: { zoom, pan },
|
|
138
|
+
} );
|
|
139
|
+
},
|
|
140
|
+
[ dispatch ]
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const setRotation = useCallback(
|
|
144
|
+
( rotation: number ) => {
|
|
145
|
+
dispatch( { type: 'SET_ROTATION', payload: rotation } );
|
|
146
|
+
},
|
|
147
|
+
[ dispatch ]
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
const setFlip = useCallback(
|
|
151
|
+
( flip: Flip ) => {
|
|
152
|
+
dispatch( { type: 'SET_FLIP', payload: flip } );
|
|
153
|
+
},
|
|
154
|
+
[ dispatch ]
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
const snapRotate90 = useCallback(
|
|
158
|
+
( direction: 1 | -1 ) => {
|
|
159
|
+
dispatch( {
|
|
160
|
+
type: 'SNAP_ROTATE_90',
|
|
161
|
+
payload: { direction },
|
|
162
|
+
} );
|
|
163
|
+
},
|
|
164
|
+
[ dispatch ]
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const setCropRect = useCallback(
|
|
168
|
+
( rect: NormalizedRect ) => {
|
|
169
|
+
dispatch( { type: 'SET_CROP_RECT', payload: rect } );
|
|
170
|
+
},
|
|
171
|
+
[ dispatch ]
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
const settleCrop = useCallback( () => {
|
|
175
|
+
dispatch( { type: 'SETTLE_CROP' } );
|
|
176
|
+
}, [ dispatch ] );
|
|
177
|
+
|
|
178
|
+
const applyOperation = useCallback(
|
|
179
|
+
( op: TransformOperation ) => {
|
|
180
|
+
dispatch( { type: 'APPLY_OPERATION', payload: op } );
|
|
181
|
+
},
|
|
182
|
+
[ dispatch ]
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
const reset = useCallback(
|
|
186
|
+
( resetState?: Partial< CropperState > ) => {
|
|
187
|
+
dispatch( { type: 'RESET', payload: resetState } );
|
|
188
|
+
// Mirror the reducer's RESET exactly so isDirty stays in
|
|
189
|
+
// sync. RESET preserves the currently-loaded image; the
|
|
190
|
+
// containment step can tweak pan/zoom/cropRect by float ulp,
|
|
191
|
+
// which we must fold into the "initial" snapshot or isDirty
|
|
192
|
+
// would report true after a reset.
|
|
193
|
+
initialRef.current = enforceContainment( {
|
|
194
|
+
...DEFAULT_STATE,
|
|
195
|
+
image: stateRef.current.image,
|
|
196
|
+
...resetState,
|
|
197
|
+
} );
|
|
198
|
+
},
|
|
199
|
+
[ dispatch ]
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
const isDirty = isStateDirty( state, initialRef.current );
|
|
203
|
+
|
|
204
|
+
const getCroppedImage = useCallback(
|
|
205
|
+
( mimeType?: string, quality?: number ): Promise< Blob > => {
|
|
206
|
+
if ( ! state.image ) {
|
|
207
|
+
return Promise.reject(
|
|
208
|
+
new Error( 'No image loaded — call setImage first.' )
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
return exportCroppedImage(
|
|
212
|
+
state.image.src,
|
|
213
|
+
state,
|
|
214
|
+
mimeType,
|
|
215
|
+
quality
|
|
216
|
+
);
|
|
217
|
+
},
|
|
218
|
+
[ state ]
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const controller: UseCropperStateReturn = {
|
|
222
|
+
state,
|
|
223
|
+
setImage,
|
|
224
|
+
setPan,
|
|
225
|
+
setZoom,
|
|
226
|
+
setZoomAtPoint,
|
|
227
|
+
setRotation,
|
|
228
|
+
setFlip,
|
|
229
|
+
snapRotate90,
|
|
230
|
+
setCropRect,
|
|
231
|
+
settleCrop,
|
|
232
|
+
applyOperation,
|
|
233
|
+
reset,
|
|
234
|
+
isDirty,
|
|
235
|
+
getCroppedImage,
|
|
236
|
+
};
|
|
237
|
+
return controller;
|
|
238
|
+
}
|