@wordpress/media-editor 0.8.1-next.v.202605131032.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
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { CropperState, TransformOperation, NormalizedPoint, NormalizedRect, Flip } from '../../core/types';
|
|
5
|
-
/**
|
|
6
|
-
* The return type of the useCropperState hook.
|
|
7
|
-
*
|
|
8
|
-
* The hook exposes its state through named setter methods rather
|
|
9
|
-
* than a raw dispatch function. Each setter has a specific
|
|
10
|
-
* behavioral contract (see individual JSDoc) that maps 1:1 to a
|
|
11
|
-
* reducer action internally. Keeping the reducer actions private
|
|
12
|
-
* lets the implementation evolve without breaking consumers.
|
|
13
|
-
*/
|
|
14
|
-
export interface UseCropperStateReturn {
|
|
15
|
-
/** The current cropper state (read-only). */
|
|
16
|
-
state: CropperState;
|
|
17
|
-
/** Set the loaded image (natural size and src). */
|
|
18
|
-
setImage: (image: CropperState['image']) => void;
|
|
19
|
-
/**
|
|
20
|
-
* Set the image pan offset in normalized coordinates. Use
|
|
21
|
-
* `setCropRect` for the crop rectangle.
|
|
22
|
-
*/
|
|
23
|
-
setPan: (pan: NormalizedPoint) => void;
|
|
24
|
-
/** Set the zoom level. Clamped to [1, 10]. */
|
|
25
|
-
setZoom: (zoom: number) => void;
|
|
26
|
-
/** Set zoom and pan together so focal-point zoom remains atomic. */
|
|
27
|
-
setZoomAtPoint: (zoom: number, pan: NormalizedPoint) => void;
|
|
28
|
-
/** Set the rotation in degrees. Normalized to [0, 360). */
|
|
29
|
-
setRotation: (rotation: number) => void;
|
|
30
|
-
/** Set the flip state. */
|
|
31
|
-
setFlip: (flip: Flip) => void;
|
|
32
|
-
/** Toggle flip on the given axis. */
|
|
33
|
-
toggleFlip: (direction: 'horizontal' | 'vertical') => void;
|
|
34
|
-
/** Snap rotate 90° preserving the image selection (Google Photos style). */
|
|
35
|
-
snapRotate90: (direction: 1 | -1) => void;
|
|
36
|
-
/** Set the crop rectangle in normalized coordinates. */
|
|
37
|
-
setCropRect: (rect: NormalizedRect) => void;
|
|
38
|
-
/**
|
|
39
|
-
* Settle the crop rect after a resize drag: expand to fill the
|
|
40
|
-
* available visual area while preserving the framed image region.
|
|
41
|
-
* Typically called from a stencil's `onResizeEnd` callback.
|
|
42
|
-
*
|
|
43
|
-
* Flushes the pending resize history before dispatching, then suppresses
|
|
44
|
-
* the settle dispatch itself so resizing and settling undo as one step.
|
|
45
|
-
*/
|
|
46
|
-
settleCrop: () => void;
|
|
47
|
-
/** Apply a transform operation through the pipeline. */
|
|
48
|
-
applyOperation: (op: TransformOperation) => void;
|
|
49
|
-
/** Reset the state. Optionally merge partial state overrides. */
|
|
50
|
-
reset: (resetState?: Partial<CropperState>) => void;
|
|
51
|
-
/** Whether the current state differs from the initial state. */
|
|
52
|
-
isDirty: boolean;
|
|
53
|
-
/** Whether there is a cropper state to undo. */
|
|
54
|
-
hasUndo: boolean;
|
|
55
|
-
/** Whether there is a cropper state to redo. */
|
|
56
|
-
hasRedo: boolean;
|
|
57
|
-
/** Undo the last committed cropper operation. */
|
|
58
|
-
undo: () => void;
|
|
59
|
-
/** Redo the last undone cropper operation. */
|
|
60
|
-
redo: () => void;
|
|
61
|
-
/**
|
|
62
|
-
* Flush the pending history entry immediately, bypassing the debounce
|
|
63
|
-
* timer. Useful in two situations:
|
|
64
|
-
*
|
|
65
|
-
* - **Pointer-up / key-up**: commit as soon as the user releases so the
|
|
66
|
-
* undo button lights up without waiting for the debounce window.
|
|
67
|
-
* - **Gesture start**: pass to `onGestureStart` on the `Cropper` so any
|
|
68
|
-
* pending sidebar interaction is committed before a new canvas gesture
|
|
69
|
-
* begins, keeping the two interactions as separate undo steps.
|
|
70
|
-
*/
|
|
71
|
-
commitHistory: () => void;
|
|
72
|
-
/**
|
|
73
|
-
* Export the cropped image as a Blob. Throws on failure — see
|
|
74
|
-
* `exportCroppedImage` in core for the error semantics (image
|
|
75
|
-
* load errors, CORS taint, missing canvas context). Wrap in
|
|
76
|
-
* try/catch if you need to recover.
|
|
77
|
-
*/
|
|
78
|
-
getCroppedImage: (mimeType?: string, quality?: number) => Promise<Blob>;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Reducer-based state management hook for the image editor.
|
|
82
|
-
*
|
|
83
|
-
* Provides the full cropper state and named setter methods for
|
|
84
|
-
* every supported transition. The reducer and containment logic
|
|
85
|
-
* live in core/state.ts (framework-agnostic); this hook is a thin
|
|
86
|
-
* React wrapper around that pure reducer, with memoized callbacks.
|
|
87
|
-
*
|
|
88
|
-
* History is recorded via a state-change debounce: any state change
|
|
89
|
-
* starts a 300 ms timer; when it fires the pre-change state is pushed
|
|
90
|
-
* onto the undo stack. `commitHistory` flushes the timer immediately
|
|
91
|
-
* (useful on pointer-up / key-up for a crisp commit feel).
|
|
92
|
-
*
|
|
93
|
-
* @param initialState Optional partial state to merge with DEFAULT_STATE.
|
|
94
|
-
* @return The cropper state, setters, and utilities.
|
|
95
|
-
*/
|
|
96
|
-
export declare function useCropperState(initialState?: Partial<CropperState>): UseCropperStateReturn;
|
|
97
|
-
//# sourceMappingURL=use-cropper-state.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use-cropper-state.d.ts","sourceRoot":"","sources":["../../../../src/image-editor/react/hooks/use-cropper-state.ts"],"names":[],"mappings":"AAWA;;GAEG;AACH,OAAO,KAAK,EACX,YAAY,EACZ,kBAAkB,EAClB,eAAe,EACf,cAAc,EACd,IAAI,EACJ,MAAM,kBAAkB,CAAC;AAS1B;;;;;;;;GAQG;AACH,MAAM,WAAW,qBAAqB;IACrC,6CAA6C;IAC7C,KAAK,EAAE,YAAY,CAAC;IACpB,mDAAmD;IACnD,QAAQ,EAAE,CAAE,KAAK,EAAE,YAAY,CAAE,OAAO,CAAE,KAAM,IAAI,CAAC;IACrD;;;OAGG;IACH,MAAM,EAAE,CAAE,GAAG,EAAE,eAAe,KAAM,IAAI,CAAC;IACzC,8CAA8C;IAC9C,OAAO,EAAE,CAAE,IAAI,EAAE,MAAM,KAAM,IAAI,CAAC;IAClC,oEAAoE;IACpE,cAAc,EAAE,CAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,eAAe,KAAM,IAAI,CAAC;IAC/D,2DAA2D;IAC3D,WAAW,EAAE,CAAE,QAAQ,EAAE,MAAM,KAAM,IAAI,CAAC;IAC1C,0BAA0B;IAC1B,OAAO,EAAE,CAAE,IAAI,EAAE,IAAI,KAAM,IAAI,CAAC;IAChC,qCAAqC;IACrC,UAAU,EAAE,CAAE,SAAS,EAAE,YAAY,GAAG,UAAU,KAAM,IAAI,CAAC;IAC7D,4EAA4E;IAC5E,YAAY,EAAE,CAAE,SAAS,EAAE,CAAC,GAAG,CAAC,CAAC,KAAM,IAAI,CAAC;IAC5C,wDAAwD;IACxD,WAAW,EAAE,CAAE,IAAI,EAAE,cAAc,KAAM,IAAI,CAAC;IAC9C;;;;;;;OAOG;IACH,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,wDAAwD;IACxD,cAAc,EAAE,CAAE,EAAE,EAAE,kBAAkB,KAAM,IAAI,CAAC;IACnD,iEAAiE;IACjE,KAAK,EAAE,CAAE,UAAU,CAAC,EAAE,OAAO,CAAE,YAAY,CAAE,KAAM,IAAI,CAAC;IACxD,gEAAgE;IAChE,OAAO,EAAE,OAAO,CAAC;IACjB,gDAAgD;IAChD,OAAO,EAAE,OAAO,CAAC;IACjB,gDAAgD;IAChD,OAAO,EAAE,OAAO,CAAC;IACjB,iDAAiD;IACjD,IAAI,EAAE,MAAM,IAAI,CAAC;IACjB,8CAA8C;IAC9C,IAAI,EAAE,MAAM,IAAI,CAAC;IACjB;;;;;;;;;OASG;IACH,aAAa,EAAE,MAAM,IAAI,CAAC;IAC1B;;;;;OAKG;IACH,eAAe,EAAE,CAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,KAAM,OAAO,CAAE,IAAI,CAAE,CAAC;CAC5E;AAgCD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,eAAe,CAC9B,YAAY,CAAC,EAAE,OAAO,CAAE,YAAY,CAAE,GACpC,qBAAqB,CA+SvB"}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
# Roadmap
|
|
2
|
-
|
|
3
|
-
Follow-up work and future phases for the image editor. These are not blockers for the MVP — they're captured here so we don't lose track.
|
|
4
|
-
|
|
5
|
-
## Phase 1 — API refinements
|
|
6
|
-
|
|
7
|
-
### ~~Hide `dispatch` and `CropperAction`~~ — **done**
|
|
8
|
-
|
|
9
|
-
Reducer details are no longer part of the public API. `useCropperState()` returns a `controller` object with named setters (`setPan`, `setZoom`, `setZoomAtPoint`, `setRotation`, `setFlip`, `snapRotate90`, `setCropRect`, `settleCrop`, `applyOperation`, `reset`), plus `setImage` for loading and `getCroppedImage` for export. `<Cropper>` takes the controller as a single prop (`controller={ controller }`).
|
|
10
|
-
|
|
11
|
-
The controller object does not expose the reducer dispatch. Internal React code uses named interaction callbacks, and the reducer action type `CropperAction` is intentionally not exported from the public barrel.
|
|
12
|
-
|
|
13
|
-
## Phase 2 — Framework-agnostic consumers
|
|
14
|
-
|
|
15
|
-
### Promote core to public API if a real non-React consumer materializes
|
|
16
|
-
|
|
17
|
-
The `core/` layer (cropperReducer, InteractionController, computeTransformStyle, stencil math) is zero-dependency pure code. It's currently **not** exported publicly — tree-shaking means React-only consumers never bundle it, but non-React consumers can't use it either.
|
|
18
|
-
|
|
19
|
-
**Trigger to promote:** A concrete use case emerges (Vue integration, vanilla JS integration, server-side pipeline). At that point, decide whether to add these to the public barrel.
|
|
20
|
-
|
|
21
|
-
**Alternative:** Split into `@wordpress/image-editor-core` and `@wordpress/image-editor-react` as separate packages when there's real demand.
|
|
22
|
-
|
|
23
|
-
## Phase 3 — Media editor expansion
|
|
24
|
-
|
|
25
|
-
### Video editing
|
|
26
|
-
|
|
27
|
-
The package is named `@wordpress/media-editor` but currently only supports images. Adding video would require:
|
|
28
|
-
|
|
29
|
-
- Video frame extraction via `<VideoCropper>` wrapper (feed a frame through `applyToCanvas`).
|
|
30
|
-
- FFmpeg flag generator from `getSourceRegion()` for server-side transcoding.
|
|
31
|
-
- Eventually, client-side WebCodecs integration for in-browser video processing.
|
|
32
|
-
|
|
33
|
-
See `docs/planning/video-roadmap.md` in the git history for earlier thinking.
|
|
34
|
-
|
|
35
|
-
### AI integration
|
|
36
|
-
|
|
37
|
-
The pipeline API (`TransformOperation[]`) is intentionally JSON-serializable for AI agent control:
|
|
38
|
-
|
|
39
|
-
- Auto-crop suggestions from image subject detection.
|
|
40
|
-
- Auto-straighten from horizon detection.
|
|
41
|
-
- Smart resize that preserves important content when aspect ratio changes.
|
|
42
|
-
|
|
43
|
-
These are consumer-side features built on the existing pipeline API. No core changes needed.
|
|
44
|
-
|
|
45
|
-
## Phase 4 — Package distribution
|
|
46
|
-
|
|
47
|
-
### Decide the final package boundary
|
|
48
|
-
|
|
49
|
-
The cropper currently lives inside `@wordpress/media-editor` as an internal module. Options for broader distribution:
|
|
50
|
-
|
|
51
|
-
- **Subpath export:** `@wordpress/media-editor/image-editor` — explicit subpath for consumers who only want the cropper.
|
|
52
|
-
- **Separate package:** `@wordpress/image-editor-next` (or similar) — ships independently.
|
|
53
|
-
- **Keep internal:** The cropper stays internal; only the media editor's top-level components are public.
|
|
54
|
-
|
|
55
|
-
Defer this decision until the API is stable and we have real consumers.
|
|
56
|
-
|
|
57
|
-
## Known issues and follow-ups
|
|
58
|
-
|
|
59
|
-
### Minor nits
|
|
60
|
-
|
|
61
|
-
- Custom slider for ±45° rotation would avoid the native `<input type="range">` behavior of tracking pointer outside the rail (see story demos).
|
|
62
|
-
- `cropBounds` recomputation depends on full `state` because React Compiler requires it. Lightweight enough to not matter, but documented as intentional.
|
|
63
|
-
|
|
64
|
-
### Documented "escape hatches"
|
|
65
|
-
|
|
66
|
-
- Application code should use the controller's named setters or `applyOperation()`. Raw reducer actions are intentionally not part of the supported API.
|
|
@@ -1,457 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WordPress dependencies
|
|
3
|
-
*/
|
|
4
|
-
import {
|
|
5
|
-
useReducer,
|
|
6
|
-
useCallback,
|
|
7
|
-
useRef,
|
|
8
|
-
useState,
|
|
9
|
-
useEffect,
|
|
10
|
-
} from '@wordpress/element';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Internal dependencies
|
|
14
|
-
*/
|
|
15
|
-
import type {
|
|
16
|
-
CropperState,
|
|
17
|
-
TransformOperation,
|
|
18
|
-
NormalizedPoint,
|
|
19
|
-
NormalizedRect,
|
|
20
|
-
Flip,
|
|
21
|
-
} from '../../core/types';
|
|
22
|
-
import { DEFAULT_STATE } from '../../core/constants';
|
|
23
|
-
import { exportCroppedImage } from '../../core/export/canvas-renderer';
|
|
24
|
-
import {
|
|
25
|
-
cropperReducer,
|
|
26
|
-
enforceContainment,
|
|
27
|
-
isStateDirty,
|
|
28
|
-
} from '../../core/state';
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The return type of the useCropperState hook.
|
|
32
|
-
*
|
|
33
|
-
* The hook exposes its state through named setter methods rather
|
|
34
|
-
* than a raw dispatch function. Each setter has a specific
|
|
35
|
-
* behavioral contract (see individual JSDoc) that maps 1:1 to a
|
|
36
|
-
* reducer action internally. Keeping the reducer actions private
|
|
37
|
-
* lets the implementation evolve without breaking consumers.
|
|
38
|
-
*/
|
|
39
|
-
export interface UseCropperStateReturn {
|
|
40
|
-
/** The current cropper state (read-only). */
|
|
41
|
-
state: CropperState;
|
|
42
|
-
/** Set the loaded image (natural size and src). */
|
|
43
|
-
setImage: ( image: CropperState[ 'image' ] ) => void;
|
|
44
|
-
/**
|
|
45
|
-
* Set the image pan offset in normalized coordinates. Use
|
|
46
|
-
* `setCropRect` for the crop rectangle.
|
|
47
|
-
*/
|
|
48
|
-
setPan: ( pan: NormalizedPoint ) => void;
|
|
49
|
-
/** Set the zoom level. Clamped to [1, 10]. */
|
|
50
|
-
setZoom: ( zoom: number ) => void;
|
|
51
|
-
/** Set zoom and pan together so focal-point zoom remains atomic. */
|
|
52
|
-
setZoomAtPoint: ( zoom: number, pan: NormalizedPoint ) => void;
|
|
53
|
-
/** Set the rotation in degrees. Normalized to [0, 360). */
|
|
54
|
-
setRotation: ( rotation: number ) => void;
|
|
55
|
-
/** Set the flip state. */
|
|
56
|
-
setFlip: ( flip: Flip ) => void;
|
|
57
|
-
/** Toggle flip on the given axis. */
|
|
58
|
-
toggleFlip: ( direction: 'horizontal' | 'vertical' ) => void;
|
|
59
|
-
/** Snap rotate 90° preserving the image selection (Google Photos style). */
|
|
60
|
-
snapRotate90: ( direction: 1 | -1 ) => void;
|
|
61
|
-
/** Set the crop rectangle in normalized coordinates. */
|
|
62
|
-
setCropRect: ( rect: NormalizedRect ) => void;
|
|
63
|
-
/**
|
|
64
|
-
* Settle the crop rect after a resize drag: expand to fill the
|
|
65
|
-
* available visual area while preserving the framed image region.
|
|
66
|
-
* Typically called from a stencil's `onResizeEnd` callback.
|
|
67
|
-
*
|
|
68
|
-
* Flushes the pending resize history before dispatching, then suppresses
|
|
69
|
-
* the settle dispatch itself so resizing and settling undo as one step.
|
|
70
|
-
*/
|
|
71
|
-
settleCrop: () => void;
|
|
72
|
-
/** Apply a transform operation through the pipeline. */
|
|
73
|
-
applyOperation: ( op: TransformOperation ) => void;
|
|
74
|
-
/** Reset the state. Optionally merge partial state overrides. */
|
|
75
|
-
reset: ( resetState?: Partial< CropperState > ) => void;
|
|
76
|
-
/** Whether the current state differs from the initial state. */
|
|
77
|
-
isDirty: boolean;
|
|
78
|
-
/** Whether there is a cropper state to undo. */
|
|
79
|
-
hasUndo: boolean;
|
|
80
|
-
/** Whether there is a cropper state to redo. */
|
|
81
|
-
hasRedo: boolean;
|
|
82
|
-
/** Undo the last committed cropper operation. */
|
|
83
|
-
undo: () => void;
|
|
84
|
-
/** Redo the last undone cropper operation. */
|
|
85
|
-
redo: () => void;
|
|
86
|
-
/**
|
|
87
|
-
* Flush the pending history entry immediately, bypassing the debounce
|
|
88
|
-
* timer. Useful in two situations:
|
|
89
|
-
*
|
|
90
|
-
* - **Pointer-up / key-up**: commit as soon as the user releases so the
|
|
91
|
-
* undo button lights up without waiting for the debounce window.
|
|
92
|
-
* - **Gesture start**: pass to `onGestureStart` on the `Cropper` so any
|
|
93
|
-
* pending sidebar interaction is committed before a new canvas gesture
|
|
94
|
-
* begins, keeping the two interactions as separate undo steps.
|
|
95
|
-
*/
|
|
96
|
-
commitHistory: () => void;
|
|
97
|
-
/**
|
|
98
|
-
* Export the cropped image as a Blob. Throws on failure — see
|
|
99
|
-
* `exportCroppedImage` in core for the error semantics (image
|
|
100
|
-
* load errors, CORS taint, missing canvas context). Wrap in
|
|
101
|
-
* try/catch if you need to recover.
|
|
102
|
-
*/
|
|
103
|
-
getCroppedImage: ( mimeType?: string, quality?: number ) => Promise< Blob >;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/** Milliseconds of inactivity after which a continuous interaction is committed to history. */
|
|
107
|
-
const HISTORY_DEBOUNCE_MS = 300;
|
|
108
|
-
|
|
109
|
-
/** Small tolerance for cropper floating-point comparisons. */
|
|
110
|
-
const HISTORY_EPSILON = 1e-6;
|
|
111
|
-
|
|
112
|
-
function nearlyEqual( a: number, b: number ): boolean {
|
|
113
|
-
return Math.abs( a - b ) < HISTORY_EPSILON;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function areHistoryStatesEqual( a: CropperState, b: CropperState ): boolean {
|
|
117
|
-
const aImage = a.image;
|
|
118
|
-
const bImage = b.image;
|
|
119
|
-
return (
|
|
120
|
-
aImage?.src === bImage?.src &&
|
|
121
|
-
aImage?.naturalWidth === bImage?.naturalWidth &&
|
|
122
|
-
aImage?.naturalHeight === bImage?.naturalHeight &&
|
|
123
|
-
nearlyEqual( a.pan.x, b.pan.x ) &&
|
|
124
|
-
nearlyEqual( a.pan.y, b.pan.y ) &&
|
|
125
|
-
nearlyEqual( a.zoom, b.zoom ) &&
|
|
126
|
-
nearlyEqual( a.rotation, b.rotation ) &&
|
|
127
|
-
a.flip.horizontal === b.flip.horizontal &&
|
|
128
|
-
a.flip.vertical === b.flip.vertical &&
|
|
129
|
-
nearlyEqual( a.cropRect.x, b.cropRect.x ) &&
|
|
130
|
-
nearlyEqual( a.cropRect.y, b.cropRect.y ) &&
|
|
131
|
-
nearlyEqual( a.cropRect.width, b.cropRect.width ) &&
|
|
132
|
-
nearlyEqual( a.cropRect.height, b.cropRect.height )
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Reducer-based state management hook for the image editor.
|
|
138
|
-
*
|
|
139
|
-
* Provides the full cropper state and named setter methods for
|
|
140
|
-
* every supported transition. The reducer and containment logic
|
|
141
|
-
* live in core/state.ts (framework-agnostic); this hook is a thin
|
|
142
|
-
* React wrapper around that pure reducer, with memoized callbacks.
|
|
143
|
-
*
|
|
144
|
-
* History is recorded via a state-change debounce: any state change
|
|
145
|
-
* starts a 300 ms timer; when it fires the pre-change state is pushed
|
|
146
|
-
* onto the undo stack. `commitHistory` flushes the timer immediately
|
|
147
|
-
* (useful on pointer-up / key-up for a crisp commit feel).
|
|
148
|
-
*
|
|
149
|
-
* @param initialState Optional partial state to merge with DEFAULT_STATE.
|
|
150
|
-
* @return The cropper state, setters, and utilities.
|
|
151
|
-
*/
|
|
152
|
-
export function useCropperState(
|
|
153
|
-
initialState?: Partial< CropperState >
|
|
154
|
-
): UseCropperStateReturn {
|
|
155
|
-
const [ state, dispatch ] = useReducer(
|
|
156
|
-
cropperReducer,
|
|
157
|
-
initialState,
|
|
158
|
-
( init ) => enforceContainment( { ...DEFAULT_STATE, ...init } )
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
const initialRef = useRef< CropperState >(
|
|
162
|
-
enforceContainment( { ...DEFAULT_STATE, ...initialState } )
|
|
163
|
-
);
|
|
164
|
-
// Keep a ref to the latest state so callbacks with stable identity
|
|
165
|
-
// (reset, setImage) can read fresh state without re-creating themselves.
|
|
166
|
-
const stateRef = useRef( state );
|
|
167
|
-
stateRef.current = state;
|
|
168
|
-
|
|
169
|
-
// History stack for undo/redo. Using refs for the arrays avoids
|
|
170
|
-
// unnecessary re-renders on every push; a pair of boolean state values
|
|
171
|
-
// drives the enabled/disabled state of the undo/redo buttons.
|
|
172
|
-
const historyRef = useRef< CropperState[] >( [] );
|
|
173
|
-
const redoStackRef = useRef< CropperState[] >( [] );
|
|
174
|
-
const [ hasUndo, setHasUndo ] = useState( false );
|
|
175
|
-
const [ hasRedo, setHasRedo ] = useState( false );
|
|
176
|
-
|
|
177
|
-
// Debounce-based history: tracks the last committed state and a pending
|
|
178
|
-
// timer. Any state change resets the timer; when it expires the
|
|
179
|
-
// pre-change state is pushed to history.
|
|
180
|
-
const lastCommittedStateRef = useRef< CropperState | null >( state );
|
|
181
|
-
const debounceTimerRef = useRef< ReturnType< typeof setTimeout > >();
|
|
182
|
-
// Set to true before dispatching actions that must not produce a debounce
|
|
183
|
-
// history entry (undo, redo, reset, setImage, discrete actions).
|
|
184
|
-
const suppressDebounceRef = useRef( false );
|
|
185
|
-
|
|
186
|
-
// Pushes `entry` (defaults to current state) onto the undo stack and
|
|
187
|
-
// clears the redo stack. Skips if `entry` is already the last item,
|
|
188
|
-
// so rapid discrete actions on unchanged state don't create duplicates.
|
|
189
|
-
const pushToHistory = useCallback( ( entry?: CropperState ) => {
|
|
190
|
-
const target = entry ?? stateRef.current;
|
|
191
|
-
const previousEntry =
|
|
192
|
-
historyRef.current[ historyRef.current.length - 1 ];
|
|
193
|
-
if ( previousEntry && areHistoryStatesEqual( previousEntry, target ) ) {
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
historyRef.current = [ ...historyRef.current, target ];
|
|
197
|
-
redoStackRef.current = [];
|
|
198
|
-
setHasUndo( true );
|
|
199
|
-
setHasRedo( false );
|
|
200
|
-
}, [] );
|
|
201
|
-
|
|
202
|
-
// Watch state and debounce history commits. Any dispatch resets the
|
|
203
|
-
// timer; once the state has settled for HISTORY_DEBOUNCE_MS the
|
|
204
|
-
// pre-change snapshot is pushed to the undo stack.
|
|
205
|
-
useEffect( () => {
|
|
206
|
-
// Suppress flag: undo/redo/reset/setImage/discrete actions set this
|
|
207
|
-
// to opt out of the debounce for their own state changes.
|
|
208
|
-
if ( suppressDebounceRef.current ) {
|
|
209
|
-
suppressDebounceRef.current = false;
|
|
210
|
-
lastCommittedStateRef.current = stateRef.current;
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
if (
|
|
214
|
-
lastCommittedStateRef.current !== null &&
|
|
215
|
-
areHistoryStatesEqual(
|
|
216
|
-
lastCommittedStateRef.current,
|
|
217
|
-
stateRef.current
|
|
218
|
-
)
|
|
219
|
-
) {
|
|
220
|
-
lastCommittedStateRef.current = stateRef.current;
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
clearTimeout( debounceTimerRef.current );
|
|
224
|
-
debounceTimerRef.current = setTimeout( () => {
|
|
225
|
-
const snapshot = lastCommittedStateRef.current;
|
|
226
|
-
if (
|
|
227
|
-
snapshot !== null &&
|
|
228
|
-
! areHistoryStatesEqual( snapshot, stateRef.current )
|
|
229
|
-
) {
|
|
230
|
-
pushToHistory( snapshot );
|
|
231
|
-
}
|
|
232
|
-
lastCommittedStateRef.current = stateRef.current;
|
|
233
|
-
}, HISTORY_DEBOUNCE_MS );
|
|
234
|
-
return () => clearTimeout( debounceTimerRef.current );
|
|
235
|
-
}, [ state, pushToHistory ] );
|
|
236
|
-
|
|
237
|
-
// Flush the pending debounce immediately: cancel the timer and push the
|
|
238
|
-
// pre-change snapshot if the state has actually changed. Used by
|
|
239
|
-
// pointer-up / key-up handlers for an instant commit feel, and by
|
|
240
|
-
// undo/redo/discrete actions before recording their own history entry.
|
|
241
|
-
const commitHistory = useCallback( () => {
|
|
242
|
-
clearTimeout( debounceTimerRef.current );
|
|
243
|
-
const snapshot = lastCommittedStateRef.current;
|
|
244
|
-
if (
|
|
245
|
-
snapshot !== null &&
|
|
246
|
-
! areHistoryStatesEqual( snapshot, stateRef.current )
|
|
247
|
-
) {
|
|
248
|
-
pushToHistory( snapshot );
|
|
249
|
-
}
|
|
250
|
-
lastCommittedStateRef.current = stateRef.current;
|
|
251
|
-
}, [ pushToHistory ] );
|
|
252
|
-
|
|
253
|
-
const undo = useCallback( () => {
|
|
254
|
-
// Flush any pending gesture so it becomes a distinct undo step
|
|
255
|
-
// before we pop history. This ensures mid-gesture undo undoes the
|
|
256
|
-
// pending change rather than silently discarding it.
|
|
257
|
-
commitHistory();
|
|
258
|
-
const prev = historyRef.current[ historyRef.current.length - 1 ];
|
|
259
|
-
if ( ! prev ) {
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
redoStackRef.current = [ stateRef.current, ...redoStackRef.current ];
|
|
263
|
-
historyRef.current = historyRef.current.slice( 0, -1 );
|
|
264
|
-
suppressDebounceRef.current = true;
|
|
265
|
-
setHasUndo( historyRef.current.length > 0 );
|
|
266
|
-
setHasRedo( true );
|
|
267
|
-
dispatch( { type: 'RESET', payload: prev } );
|
|
268
|
-
}, [ dispatch, commitHistory ] );
|
|
269
|
-
|
|
270
|
-
const redo = useCallback( () => {
|
|
271
|
-
// Flush any pending gesture before redoing so the in-flight change
|
|
272
|
-
// is saved and the redo target lands on top of it.
|
|
273
|
-
commitHistory();
|
|
274
|
-
const next = redoStackRef.current[ 0 ];
|
|
275
|
-
if ( ! next ) {
|
|
276
|
-
return;
|
|
277
|
-
}
|
|
278
|
-
historyRef.current = [ ...historyRef.current, stateRef.current ];
|
|
279
|
-
redoStackRef.current = redoStackRef.current.slice( 1 );
|
|
280
|
-
suppressDebounceRef.current = true;
|
|
281
|
-
setHasUndo( true );
|
|
282
|
-
setHasRedo( redoStackRef.current.length > 0 );
|
|
283
|
-
dispatch( { type: 'RESET', payload: next } );
|
|
284
|
-
}, [ dispatch, commitHistory ] );
|
|
285
|
-
|
|
286
|
-
const setImage = useCallback(
|
|
287
|
-
( image: CropperState[ 'image' ] ) => {
|
|
288
|
-
clearTimeout( debounceTimerRef.current );
|
|
289
|
-
suppressDebounceRef.current = true;
|
|
290
|
-
dispatch( { type: 'SET_IMAGE', payload: image } );
|
|
291
|
-
// Refresh the "clean" snapshot to match the post-load state
|
|
292
|
-
// produced by the reducer. Otherwise containment can nudge
|
|
293
|
-
// pan/zoom by tiny amounts on load and `isDirty` would
|
|
294
|
-
// report true from the start.
|
|
295
|
-
initialRef.current = enforceContainment( {
|
|
296
|
-
...initialRef.current,
|
|
297
|
-
image,
|
|
298
|
-
} );
|
|
299
|
-
},
|
|
300
|
-
[ dispatch ]
|
|
301
|
-
);
|
|
302
|
-
|
|
303
|
-
const setPan = useCallback(
|
|
304
|
-
( pan: NormalizedPoint ) => {
|
|
305
|
-
dispatch( { type: 'SET_PAN', payload: pan } );
|
|
306
|
-
},
|
|
307
|
-
[ dispatch ]
|
|
308
|
-
);
|
|
309
|
-
|
|
310
|
-
const setZoom = useCallback(
|
|
311
|
-
( zoom: number ) => {
|
|
312
|
-
dispatch( { type: 'SET_ZOOM', payload: zoom } );
|
|
313
|
-
},
|
|
314
|
-
[ dispatch ]
|
|
315
|
-
);
|
|
316
|
-
|
|
317
|
-
const setZoomAtPoint = useCallback(
|
|
318
|
-
( zoom: number, pan: NormalizedPoint ) => {
|
|
319
|
-
dispatch( {
|
|
320
|
-
type: 'SET_ZOOM_AT_POINT',
|
|
321
|
-
payload: { zoom, pan },
|
|
322
|
-
} );
|
|
323
|
-
},
|
|
324
|
-
[ dispatch ]
|
|
325
|
-
);
|
|
326
|
-
|
|
327
|
-
const setRotation = useCallback(
|
|
328
|
-
( rotation: number ) => {
|
|
329
|
-
dispatch( { type: 'SET_ROTATION', payload: rotation } );
|
|
330
|
-
},
|
|
331
|
-
[ dispatch ]
|
|
332
|
-
);
|
|
333
|
-
|
|
334
|
-
const setFlip = useCallback(
|
|
335
|
-
( flip: Flip ) => {
|
|
336
|
-
commitHistory(); // flush any pending continuous gesture first
|
|
337
|
-
pushToHistory(); // record current state as the undo point
|
|
338
|
-
suppressDebounceRef.current = true;
|
|
339
|
-
dispatch( { type: 'SET_FLIP', payload: flip } );
|
|
340
|
-
},
|
|
341
|
-
[ dispatch, pushToHistory, commitHistory ]
|
|
342
|
-
);
|
|
343
|
-
|
|
344
|
-
const toggleFlip = useCallback(
|
|
345
|
-
( direction: 'horizontal' | 'vertical' ) => {
|
|
346
|
-
setFlip( {
|
|
347
|
-
...stateRef.current.flip,
|
|
348
|
-
[ direction ]: ! stateRef.current.flip[ direction ],
|
|
349
|
-
} );
|
|
350
|
-
},
|
|
351
|
-
[ setFlip ]
|
|
352
|
-
);
|
|
353
|
-
|
|
354
|
-
const snapRotate90 = useCallback(
|
|
355
|
-
( direction: 1 | -1 ) => {
|
|
356
|
-
commitHistory();
|
|
357
|
-
pushToHistory();
|
|
358
|
-
suppressDebounceRef.current = true;
|
|
359
|
-
dispatch( {
|
|
360
|
-
type: 'SNAP_ROTATE_90',
|
|
361
|
-
payload: { direction },
|
|
362
|
-
} );
|
|
363
|
-
},
|
|
364
|
-
[ dispatch, pushToHistory, commitHistory ]
|
|
365
|
-
);
|
|
366
|
-
|
|
367
|
-
const setCropRect = useCallback(
|
|
368
|
-
( rect: NormalizedRect ) => {
|
|
369
|
-
dispatch( { type: 'SET_CROP_RECT', payload: rect } );
|
|
370
|
-
},
|
|
371
|
-
[ dispatch ]
|
|
372
|
-
);
|
|
373
|
-
|
|
374
|
-
const settleCrop = useCallback( () => {
|
|
375
|
-
commitHistory();
|
|
376
|
-
suppressDebounceRef.current = true;
|
|
377
|
-
dispatch( { type: 'SETTLE_CROP' } );
|
|
378
|
-
}, [ dispatch, commitHistory ] );
|
|
379
|
-
|
|
380
|
-
const applyOperation = useCallback(
|
|
381
|
-
( op: TransformOperation ) => {
|
|
382
|
-
commitHistory();
|
|
383
|
-
pushToHistory();
|
|
384
|
-
suppressDebounceRef.current = true;
|
|
385
|
-
dispatch( { type: 'APPLY_OPERATION', payload: op } );
|
|
386
|
-
},
|
|
387
|
-
[ dispatch, pushToHistory, commitHistory ]
|
|
388
|
-
);
|
|
389
|
-
|
|
390
|
-
const reset = useCallback(
|
|
391
|
-
( resetState?: Partial< CropperState > ) => {
|
|
392
|
-
commitHistory();
|
|
393
|
-
const nextInitialState = enforceContainment( {
|
|
394
|
-
...DEFAULT_STATE,
|
|
395
|
-
image: stateRef.current.image,
|
|
396
|
-
...resetState,
|
|
397
|
-
} );
|
|
398
|
-
if (
|
|
399
|
-
! areHistoryStatesEqual( stateRef.current, nextInitialState )
|
|
400
|
-
) {
|
|
401
|
-
pushToHistory();
|
|
402
|
-
}
|
|
403
|
-
suppressDebounceRef.current = true;
|
|
404
|
-
dispatch( { type: 'RESET', payload: resetState } );
|
|
405
|
-
// Mirror the reducer's RESET exactly so isDirty stays in
|
|
406
|
-
// sync. RESET preserves the currently-loaded image; the
|
|
407
|
-
// containment step can tweak pan/zoom/cropRect by float ulp,
|
|
408
|
-
// which we must fold into the "initial" snapshot or isDirty
|
|
409
|
-
// would report true after a reset.
|
|
410
|
-
initialRef.current = nextInitialState;
|
|
411
|
-
},
|
|
412
|
-
[ dispatch, pushToHistory, commitHistory ]
|
|
413
|
-
);
|
|
414
|
-
|
|
415
|
-
const isDirty = isStateDirty( state, initialRef.current );
|
|
416
|
-
|
|
417
|
-
const getCroppedImage = useCallback(
|
|
418
|
-
( mimeType?: string, quality?: number ): Promise< Blob > => {
|
|
419
|
-
if ( ! state.image ) {
|
|
420
|
-
return Promise.reject(
|
|
421
|
-
new Error( 'No image loaded — call setImage first.' )
|
|
422
|
-
);
|
|
423
|
-
}
|
|
424
|
-
return exportCroppedImage(
|
|
425
|
-
state.image.src,
|
|
426
|
-
state,
|
|
427
|
-
mimeType,
|
|
428
|
-
quality
|
|
429
|
-
);
|
|
430
|
-
},
|
|
431
|
-
[ state ]
|
|
432
|
-
);
|
|
433
|
-
|
|
434
|
-
const controller: UseCropperStateReturn = {
|
|
435
|
-
state,
|
|
436
|
-
setImage,
|
|
437
|
-
setPan,
|
|
438
|
-
setZoom,
|
|
439
|
-
setZoomAtPoint,
|
|
440
|
-
setRotation,
|
|
441
|
-
setFlip,
|
|
442
|
-
toggleFlip,
|
|
443
|
-
snapRotate90,
|
|
444
|
-
setCropRect,
|
|
445
|
-
settleCrop,
|
|
446
|
-
applyOperation,
|
|
447
|
-
reset,
|
|
448
|
-
isDirty,
|
|
449
|
-
getCroppedImage,
|
|
450
|
-
hasUndo,
|
|
451
|
-
hasRedo,
|
|
452
|
-
undo,
|
|
453
|
-
redo,
|
|
454
|
-
commitHistory,
|
|
455
|
-
};
|
|
456
|
-
return controller;
|
|
457
|
-
}
|