@wordpress/media-editor 0.8.0 → 0.8.1-next.v.202605131032.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/components/media-editor/index.cjs +560 -0
- package/build/components/media-editor/index.cjs.map +7 -0
- package/build/components/media-editor-canvas/index.cjs +14 -2
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +34 -27
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +162 -0
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +7 -0
- package/build/components/media-editor-modal/index.cjs +42 -430
- package/build/components/media-editor-modal/index.cjs.map +3 -3
- package/build/components/media-editor-toolbar/index.cjs +143 -75
- package/build/components/media-editor-toolbar/index.cjs.map +3 -3
- package/build/components/media-form/index.cjs +3 -0
- package/build/components/media-form/index.cjs.map +2 -2
- package/build/components/rotation-ruler/index.cjs +197 -0
- package/build/components/rotation-ruler/index.cjs.map +7 -0
- package/build/components/rotation-ruler/use-ruler-drag.cjs +154 -0
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +7 -0
- package/build/hooks/use-crop-gesture-handlers.cjs +43 -0
- package/build/hooks/use-crop-gesture-handlers.cjs.map +7 -0
- package/build/image-editor/core/constants.cjs +9 -0
- package/build/image-editor/core/constants.cjs.map +2 -2
- package/build/image-editor/core/containment.cjs +7 -13
- package/build/image-editor/core/containment.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs +43 -7
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/core/state.cjs +5 -1
- package/build/image-editor/core/state.cjs.map +2 -2
- package/build/image-editor/core/transform-style.cjs.map +1 -1
- package/build/image-editor/core/types.cjs.map +1 -1
- package/build/image-editor/core/viewport-state.cjs +50 -0
- package/build/image-editor/core/viewport-state.cjs.map +7 -0
- package/build/image-editor/react/components/cropper.cjs +192 -72
- package/build/image-editor/react/components/cropper.cjs.map +3 -3
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +5 -2
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +16 -4
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/components/viewport-provider.cjs +51 -0
- package/build/image-editor/react/components/viewport-provider.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +53 -6
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-cropper-state.cjs +127 -8
- package/build/image-editor/react/hooks/use-cropper-state.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs +26 -5
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-viewport-state.cjs +51 -0
- package/build/image-editor/react/hooks/use-viewport-state.cjs.map +7 -0
- package/build/image-editor/react/visually-hidden-style.cjs +41 -0
- package/build/image-editor/react/visually-hidden-style.cjs.map +7 -0
- package/build/private-apis.cjs +2 -0
- package/build/private-apis.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +545 -0
- package/build-module/components/media-editor/index.mjs.map +7 -0
- package/build-module/components/media-editor-canvas/index.mjs +14 -2
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +35 -28
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +144 -0
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +7 -0
- package/build-module/components/media-editor-modal/index.mjs +46 -455
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-toolbar/index.mjs +137 -77
- package/build-module/components/media-editor-toolbar/index.mjs.map +2 -2
- package/build-module/components/media-form/index.mjs +3 -0
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/index.mjs +166 -0
- package/build-module/components/rotation-ruler/index.mjs.map +7 -0
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs +126 -0
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +7 -0
- package/build-module/hooks/use-crop-gesture-handlers.mjs +17 -0
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +7 -0
- package/build-module/image-editor/core/constants.mjs +6 -0
- package/build-module/image-editor/core/constants.mjs.map +2 -2
- package/build-module/image-editor/core/containment.mjs +6 -12
- package/build-module/image-editor/core/containment.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs +50 -8
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/core/state.mjs +5 -1
- package/build-module/image-editor/core/state.mjs.map +2 -2
- package/build-module/image-editor/core/transform-style.mjs.map +1 -1
- package/build-module/image-editor/core/viewport-state.mjs +24 -0
- package/build-module/image-editor/core/viewport-state.mjs.map +7 -0
- package/build-module/image-editor/react/components/cropper.mjs +195 -73
- package/build-module/image-editor/react/components/cropper.mjs.map +3 -3
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +5 -2
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +26 -5
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/components/viewport-provider.mjs +27 -0
- package/build-module/image-editor/react/components/viewport-provider.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +59 -7
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs +134 -9
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs +26 -5
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs +29 -0
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +7 -0
- package/build-module/image-editor/react/visually-hidden-style.mjs +16 -0
- package/build-module/image-editor/react/visually-hidden-style.mjs.map +7 -0
- package/build-module/private-apis.mjs +2 -0
- package/build-module/private-apis.mjs.map +2 -2
- package/build-style/style-rtl.css +254 -29
- package/build-style/style.css +254 -29
- package/build-types/components/media-editor/index.d.ts +34 -0
- package/build-types/components/media-editor/index.d.ts.map +1 -0
- package/build-types/components/media-editor-canvas/index.d.ts +10 -1
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts +6 -0
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/index.d.ts +2 -2
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-provider/index.d.ts +1 -1
- package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
- package/build-types/components/media-editor-toolbar/index.d.ts +4 -1
- package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts +1 -1
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/index.d.ts +46 -0
- package/build-types/components/rotation-ruler/index.d.ts.map +1 -0
- package/build-types/components/rotation-ruler/stories/index.story.d.ts +14 -0
- package/build-types/components/rotation-ruler/stories/index.story.d.ts.map +1 -0
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts +61 -0
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -0
- package/build-types/hooks/use-crop-gesture-handlers.d.ts +42 -0
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -0
- package/build-types/image-editor/core/constants.d.ts +9 -0
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/containment.d.ts +11 -7
- package/build-types/image-editor/core/containment.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts +14 -2
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/core/state.d.ts.map +1 -1
- package/build-types/image-editor/core/transform-style.d.ts +1 -1
- package/build-types/image-editor/core/types.d.ts +25 -0
- package/build-types/image-editor/core/types.d.ts.map +1 -1
- package/build-types/image-editor/core/viewport-state.d.ts +7 -0
- package/build-types/image-editor/core/viewport-state.d.ts.map +1 -0
- package/build-types/image-editor/react/components/cropper-provider.d.ts +1 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +2 -1
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +3 -13
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +1 -1
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/components/viewport-provider.d.ts +9 -0
- package/build-types/image-editor/react/components/viewport-provider.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +29 -0
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts +5 -2
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-viewport-state.d.ts +19 -0
- package/build-types/image-editor/react/hooks/use-viewport-state.d.ts.map +1 -0
- package/build-types/image-editor/react/visually-hidden-style.d.ts +6 -0
- package/build-types/image-editor/react/visually-hidden-style.d.ts.map +1 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
- package/build-types/private-apis.d.ts.map +1 -1
- package/build-types/store/actions.d.ts +3 -3
- package/build-types/store/actions.d.ts.map +1 -1
- package/package.json +16 -15
- package/src/components/media-editor/index.tsx +709 -0
- package/src/components/media-editor/style.scss +89 -0
- package/src/components/media-editor-canvas/index.tsx +24 -0
- package/src/components/media-editor-crop-panel/index.tsx +39 -27
- package/src/components/media-editor-crop-panel/test/index.tsx +109 -0
- package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +177 -0
- package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +53 -0
- package/src/components/media-editor-modal/index.tsx +49 -589
- package/src/components/media-editor-modal/style.scss +13 -112
- package/src/components/media-editor-toolbar/index.tsx +122 -63
- package/src/components/media-editor-toolbar/style.scss +39 -11
- package/src/components/media-form/index.tsx +5 -0
- package/src/components/rotation-ruler/README.md +37 -0
- package/src/components/rotation-ruler/index.tsx +266 -0
- package/src/components/rotation-ruler/stories/index.story.tsx +54 -0
- package/src/components/rotation-ruler/style.scss +147 -0
- package/src/components/rotation-ruler/test/index.tsx +109 -0
- package/src/components/rotation-ruler/use-ruler-drag.ts +230 -0
- package/src/hooks/use-crop-gesture-handlers.ts +54 -0
- package/src/image-editor/core/constants.ts +12 -0
- package/src/image-editor/core/containment.ts +16 -28
- package/src/image-editor/core/interaction-controller.ts +66 -10
- package/src/image-editor/core/state.ts +15 -8
- package/src/image-editor/core/test/camera.ts +15 -26
- package/src/image-editor/core/test/interaction-controller.ts +158 -7
- package/src/image-editor/core/test/state.ts +21 -1
- package/src/image-editor/core/transform-style.ts +1 -1
- package/src/image-editor/core/types.ts +16 -0
- package/src/image-editor/core/viewport-state.ts +33 -0
- package/src/image-editor/docs/architecture.md +1 -1
- package/src/image-editor/docs/recipes.md +2 -2
- package/src/image-editor/react/components/cropper.scss +28 -0
- package/src/image-editor/react/components/cropper.tsx +268 -72
- package/src/image-editor/react/components/overlays/dimming-overlay.tsx +12 -12
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +27 -8
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +29 -3
- package/src/image-editor/react/components/test/cropper.tsx +294 -5
- package/src/image-editor/react/components/viewport-provider.tsx +39 -0
- package/src/image-editor/react/hooks/test/use-aria-announcer.ts +81 -0
- package/src/image-editor/react/hooks/test/use-cropper-state.ts +286 -0
- package/src/image-editor/react/hooks/test/use-interaction.ts +1 -0
- package/src/image-editor/react/hooks/use-aria-announcer.ts +68 -7
- package/src/image-editor/react/hooks/use-cropper-state.ts +230 -11
- package/src/image-editor/react/hooks/use-interaction.ts +36 -6
- package/src/image-editor/react/hooks/use-viewport-state.ts +48 -0
- package/src/image-editor/react/visually-hidden-style.ts +15 -0
- package/src/private-apis.ts +2 -0
- package/src/style.scss +3 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
useReducer,
|
|
6
|
+
useCallback,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
useEffect,
|
|
10
|
+
} from '@wordpress/element';
|
|
5
11
|
|
|
6
12
|
/**
|
|
7
13
|
* Internal dependencies
|
|
@@ -48,6 +54,8 @@ export interface UseCropperStateReturn {
|
|
|
48
54
|
setRotation: ( rotation: number ) => void;
|
|
49
55
|
/** Set the flip state. */
|
|
50
56
|
setFlip: ( flip: Flip ) => void;
|
|
57
|
+
/** Toggle flip on the given axis. */
|
|
58
|
+
toggleFlip: ( direction: 'horizontal' | 'vertical' ) => void;
|
|
51
59
|
/** Snap rotate 90° preserving the image selection (Google Photos style). */
|
|
52
60
|
snapRotate90: ( direction: 1 | -1 ) => void;
|
|
53
61
|
/** Set the crop rectangle in normalized coordinates. */
|
|
@@ -56,6 +64,9 @@ export interface UseCropperStateReturn {
|
|
|
56
64
|
* Settle the crop rect after a resize drag: expand to fill the
|
|
57
65
|
* available visual area while preserving the framed image region.
|
|
58
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.
|
|
59
70
|
*/
|
|
60
71
|
settleCrop: () => void;
|
|
61
72
|
/** Apply a transform operation through the pipeline. */
|
|
@@ -64,6 +75,25 @@ export interface UseCropperStateReturn {
|
|
|
64
75
|
reset: ( resetState?: Partial< CropperState > ) => void;
|
|
65
76
|
/** Whether the current state differs from the initial state. */
|
|
66
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;
|
|
67
97
|
/**
|
|
68
98
|
* Export the cropped image as a Blob. Throws on failure — see
|
|
69
99
|
* `exportCroppedImage` in core for the error semantics (image
|
|
@@ -73,6 +103,36 @@ export interface UseCropperStateReturn {
|
|
|
73
103
|
getCroppedImage: ( mimeType?: string, quality?: number ) => Promise< Blob >;
|
|
74
104
|
}
|
|
75
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
|
+
|
|
76
136
|
/**
|
|
77
137
|
* Reducer-based state management hook for the image editor.
|
|
78
138
|
*
|
|
@@ -81,6 +141,11 @@ export interface UseCropperStateReturn {
|
|
|
81
141
|
* live in core/state.ts (framework-agnostic); this hook is a thin
|
|
82
142
|
* React wrapper around that pure reducer, with memoized callbacks.
|
|
83
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
|
+
*
|
|
84
149
|
* @param initialState Optional partial state to merge with DEFAULT_STATE.
|
|
85
150
|
* @return The cropper state, setters, and utilities.
|
|
86
151
|
*/
|
|
@@ -101,8 +166,127 @@ export function useCropperState(
|
|
|
101
166
|
const stateRef = useRef( state );
|
|
102
167
|
stateRef.current = state;
|
|
103
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
|
+
|
|
104
286
|
const setImage = useCallback(
|
|
105
287
|
( image: CropperState[ 'image' ] ) => {
|
|
288
|
+
clearTimeout( debounceTimerRef.current );
|
|
289
|
+
suppressDebounceRef.current = true;
|
|
106
290
|
dispatch( { type: 'SET_IMAGE', payload: image } );
|
|
107
291
|
// Refresh the "clean" snapshot to match the post-load state
|
|
108
292
|
// produced by the reducer. Otherwise containment can nudge
|
|
@@ -149,19 +333,35 @@ export function useCropperState(
|
|
|
149
333
|
|
|
150
334
|
const setFlip = useCallback(
|
|
151
335
|
( flip: Flip ) => {
|
|
336
|
+
commitHistory(); // flush any pending continuous gesture first
|
|
337
|
+
pushToHistory(); // record current state as the undo point
|
|
338
|
+
suppressDebounceRef.current = true;
|
|
152
339
|
dispatch( { type: 'SET_FLIP', payload: flip } );
|
|
153
340
|
},
|
|
154
|
-
[ dispatch ]
|
|
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 ]
|
|
155
352
|
);
|
|
156
353
|
|
|
157
354
|
const snapRotate90 = useCallback(
|
|
158
355
|
( direction: 1 | -1 ) => {
|
|
356
|
+
commitHistory();
|
|
357
|
+
pushToHistory();
|
|
358
|
+
suppressDebounceRef.current = true;
|
|
159
359
|
dispatch( {
|
|
160
360
|
type: 'SNAP_ROTATE_90',
|
|
161
361
|
payload: { direction },
|
|
162
362
|
} );
|
|
163
363
|
},
|
|
164
|
-
[ dispatch ]
|
|
364
|
+
[ dispatch, pushToHistory, commitHistory ]
|
|
165
365
|
);
|
|
166
366
|
|
|
167
367
|
const setCropRect = useCallback(
|
|
@@ -172,31 +372,44 @@ export function useCropperState(
|
|
|
172
372
|
);
|
|
173
373
|
|
|
174
374
|
const settleCrop = useCallback( () => {
|
|
375
|
+
commitHistory();
|
|
376
|
+
suppressDebounceRef.current = true;
|
|
175
377
|
dispatch( { type: 'SETTLE_CROP' } );
|
|
176
|
-
}, [ dispatch ] );
|
|
378
|
+
}, [ dispatch, commitHistory ] );
|
|
177
379
|
|
|
178
380
|
const applyOperation = useCallback(
|
|
179
381
|
( op: TransformOperation ) => {
|
|
382
|
+
commitHistory();
|
|
383
|
+
pushToHistory();
|
|
384
|
+
suppressDebounceRef.current = true;
|
|
180
385
|
dispatch( { type: 'APPLY_OPERATION', payload: op } );
|
|
181
386
|
},
|
|
182
|
-
[ dispatch ]
|
|
387
|
+
[ dispatch, pushToHistory, commitHistory ]
|
|
183
388
|
);
|
|
184
389
|
|
|
185
390
|
const reset = useCallback(
|
|
186
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;
|
|
187
404
|
dispatch( { type: 'RESET', payload: resetState } );
|
|
188
405
|
// Mirror the reducer's RESET exactly so isDirty stays in
|
|
189
406
|
// sync. RESET preserves the currently-loaded image; the
|
|
190
407
|
// containment step can tweak pan/zoom/cropRect by float ulp,
|
|
191
408
|
// which we must fold into the "initial" snapshot or isDirty
|
|
192
409
|
// would report true after a reset.
|
|
193
|
-
initialRef.current =
|
|
194
|
-
...DEFAULT_STATE,
|
|
195
|
-
image: stateRef.current.image,
|
|
196
|
-
...resetState,
|
|
197
|
-
} );
|
|
410
|
+
initialRef.current = nextInitialState;
|
|
198
411
|
},
|
|
199
|
-
[ dispatch ]
|
|
412
|
+
[ dispatch, pushToHistory, commitHistory ]
|
|
200
413
|
);
|
|
201
414
|
|
|
202
415
|
const isDirty = isStateDirty( state, initialRef.current );
|
|
@@ -226,6 +439,7 @@ export function useCropperState(
|
|
|
226
439
|
setZoomAtPoint,
|
|
227
440
|
setRotation,
|
|
228
441
|
setFlip,
|
|
442
|
+
toggleFlip,
|
|
229
443
|
snapRotate90,
|
|
230
444
|
setCropRect,
|
|
231
445
|
settleCrop,
|
|
@@ -233,6 +447,11 @@ export function useCropperState(
|
|
|
233
447
|
reset,
|
|
234
448
|
isDirty,
|
|
235
449
|
getCroppedImage,
|
|
450
|
+
hasUndo,
|
|
451
|
+
hasRedo,
|
|
452
|
+
undo,
|
|
453
|
+
redo,
|
|
454
|
+
commitHistory,
|
|
236
455
|
};
|
|
237
456
|
return controller;
|
|
238
457
|
}
|
|
@@ -40,9 +40,12 @@ export interface UseInteractionOptions {
|
|
|
40
40
|
minZoom?: number;
|
|
41
41
|
/** Maximum zoom level. Defaults to MAX_ZOOM. */
|
|
42
42
|
maxZoom?: number;
|
|
43
|
-
/** Zoom speed multiplier for wheel events. Defaults to 0.
|
|
43
|
+
/** Zoom speed multiplier for wheel events. Defaults to 0.0025. */
|
|
44
44
|
zoomSpeed?: number;
|
|
45
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* Pan step size in normalized coords for keyboard events.
|
|
47
|
+
* Defaults to 0.01. Shift multiplies it by 10.
|
|
48
|
+
*/
|
|
46
49
|
keyboardStep?: number;
|
|
47
50
|
/** Zoom level for double-tap zoom. Defaults to 2. */
|
|
48
51
|
doubleTapZoom?: number;
|
|
@@ -67,6 +70,18 @@ function isHandledKeyboardPan( event: KeyboardEvent ): boolean {
|
|
|
67
70
|
}
|
|
68
71
|
}
|
|
69
72
|
|
|
73
|
+
function isHandledKeyboardZoom( event: KeyboardEvent ): boolean {
|
|
74
|
+
switch ( event.key ) {
|
|
75
|
+
case '+':
|
|
76
|
+
case '=':
|
|
77
|
+
case '-':
|
|
78
|
+
case '_':
|
|
79
|
+
return true;
|
|
80
|
+
default:
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
70
85
|
/**
|
|
71
86
|
* Mouse, touch, and keyboard event handling for pan, zoom,
|
|
72
87
|
* and crop manipulation.
|
|
@@ -95,6 +110,9 @@ export function useInteraction(
|
|
|
95
110
|
const [ isKeyboardPanning, setIsKeyboardPanning ] = useState( false );
|
|
96
111
|
const keyboardInteractionTimerRef =
|
|
97
112
|
useRef< ReturnType< typeof setTimeout > >();
|
|
113
|
+
// Tracks whether a keyboard gesture (pan or zoom) is currently active so
|
|
114
|
+
// onGestureStart is only fired once per gesture, not on every key repeat.
|
|
115
|
+
const isKeyboardGestureActiveRef = useRef( false );
|
|
98
116
|
|
|
99
117
|
// Keep mutable refs so the controller always reads fresh values
|
|
100
118
|
// without needing to be recreated.
|
|
@@ -116,11 +134,18 @@ export function useInteraction(
|
|
|
116
134
|
const stopPlacementGesture = useCallback( () => {
|
|
117
135
|
setIsGestureActive( false );
|
|
118
136
|
}, [] );
|
|
119
|
-
|
|
120
|
-
|
|
137
|
+
// Shared timer logic for any keyboard gesture (pan or zoom): fires
|
|
138
|
+
// onGestureStart once per burst and onGestureEnd after the idle window.
|
|
139
|
+
const signalKeyboardGesture = useCallback( () => {
|
|
140
|
+
if ( ! isKeyboardGestureActiveRef.current ) {
|
|
141
|
+
isKeyboardGestureActiveRef.current = true;
|
|
142
|
+
optionsRef.current?.onGestureStart?.();
|
|
143
|
+
}
|
|
121
144
|
clearTimeout( keyboardInteractionTimerRef.current );
|
|
122
145
|
keyboardInteractionTimerRef.current = setTimeout( () => {
|
|
146
|
+
isKeyboardGestureActiveRef.current = false;
|
|
123
147
|
setIsKeyboardPanning( false );
|
|
148
|
+
optionsRef.current?.onGestureEnd?.();
|
|
124
149
|
}, KEYBOARD_INTERACTION_IDLE_MS );
|
|
125
150
|
}, [] );
|
|
126
151
|
|
|
@@ -142,6 +167,8 @@ export function useInteraction(
|
|
|
142
167
|
actionsRef.current.setZoomAtPoint( zoom, pan ),
|
|
143
168
|
snapRotate90: ( direction ) =>
|
|
144
169
|
actionsRef.current.snapRotate90( direction ),
|
|
170
|
+
toggleFlip: ( direction ) =>
|
|
171
|
+
actionsRef.current.toggleFlip?.( direction ),
|
|
145
172
|
},
|
|
146
173
|
getContainerSize: () => containerSizeRef.current,
|
|
147
174
|
getImageSize: () => imageSizeRef.current,
|
|
@@ -198,11 +225,14 @@ export function useInteraction(
|
|
|
198
225
|
const onKeyDown = useCallback(
|
|
199
226
|
( e: React.KeyboardEvent ) => {
|
|
200
227
|
if ( isHandledKeyboardPan( e.nativeEvent ) ) {
|
|
201
|
-
|
|
228
|
+
setIsKeyboardPanning( true );
|
|
229
|
+
signalKeyboardGesture();
|
|
230
|
+
} else if ( isHandledKeyboardZoom( e.nativeEvent ) ) {
|
|
231
|
+
signalKeyboardGesture();
|
|
202
232
|
}
|
|
203
233
|
controllerRef.current?.handleKeyDown( e.nativeEvent );
|
|
204
234
|
},
|
|
205
|
-
[
|
|
235
|
+
[ signalKeyboardGesture ]
|
|
206
236
|
);
|
|
207
237
|
|
|
208
238
|
const onWheelNative = useCallback( ( e: WheelEvent ) => {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { useReducer, useCallback, useMemo } from '@wordpress/element';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import type { ViewportState } from '../../core/types';
|
|
10
|
+
import {
|
|
11
|
+
viewportReducer,
|
|
12
|
+
DEFAULT_VIEWPORT_STATE,
|
|
13
|
+
} from '../../core/viewport-state';
|
|
14
|
+
|
|
15
|
+
export interface UseViewportStateReturn {
|
|
16
|
+
viewport: ViewportState;
|
|
17
|
+
/**
|
|
18
|
+
* Unused in the current Cropper implementation — scaffolded for the
|
|
19
|
+
* upcoming navigator panel (viewport zoom slider).
|
|
20
|
+
*/
|
|
21
|
+
setViewportZoom: ( zoom: number ) => void;
|
|
22
|
+
setViewportPan: ( pan: { x: number; y: number } ) => void;
|
|
23
|
+
resetViewport: () => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function useViewportState(): UseViewportStateReturn {
|
|
27
|
+
const [ viewport, dispatch ] = useReducer(
|
|
28
|
+
viewportReducer,
|
|
29
|
+
DEFAULT_VIEWPORT_STATE
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const setViewportZoom = useCallback( ( zoom: number ) => {
|
|
33
|
+
dispatch( { type: 'SET_VIEWPORT_ZOOM', payload: zoom } );
|
|
34
|
+
}, [] );
|
|
35
|
+
|
|
36
|
+
const setViewportPan = useCallback( ( pan: { x: number; y: number } ) => {
|
|
37
|
+
dispatch( { type: 'SET_VIEWPORT_PAN', payload: pan } );
|
|
38
|
+
}, [] );
|
|
39
|
+
|
|
40
|
+
const resetViewport = useCallback( () => {
|
|
41
|
+
dispatch( { type: 'RESET_VIEWPORT' } );
|
|
42
|
+
}, [] );
|
|
43
|
+
|
|
44
|
+
return useMemo(
|
|
45
|
+
() => ( { viewport, setViewportZoom, setViewportPan, resetViewport } ),
|
|
46
|
+
[ viewport, setViewportZoom, setViewportPan, resetViewport ]
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSS style object that visually hides an element while keeping it accessible
|
|
3
|
+
* to screen readers.
|
|
4
|
+
*/
|
|
5
|
+
export const VISUALLY_HIDDEN_STYLE: React.CSSProperties = {
|
|
6
|
+
position: 'absolute',
|
|
7
|
+
width: 1,
|
|
8
|
+
height: 1,
|
|
9
|
+
padding: 0,
|
|
10
|
+
margin: -1,
|
|
11
|
+
overflow: 'hidden',
|
|
12
|
+
clip: 'rect(0, 0, 0, 0)',
|
|
13
|
+
whiteSpace: 'nowrap',
|
|
14
|
+
border: 0,
|
|
15
|
+
};
|
package/src/private-apis.ts
CHANGED
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
import { lock } from './lock-unlock';
|
|
5
5
|
import { store } from './store';
|
|
6
6
|
import { MediaEditorModal } from './components/media-editor-modal';
|
|
7
|
+
import { MediaEditor } from './components/media-editor';
|
|
7
8
|
|
|
8
9
|
export const privateApis = {};
|
|
9
10
|
lock( privateApis, {
|
|
10
11
|
store,
|
|
12
|
+
MediaEditor,
|
|
11
13
|
MediaEditorModal,
|
|
12
14
|
} );
|
package/src/style.scss
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// Import component styles
|
|
2
2
|
@use "./components/media-preview/style.scss" as *;
|
|
3
3
|
@use "./image-editor/style.scss" as *;
|
|
4
|
+
@use "./components/media-editor/style.scss" as *;
|
|
4
5
|
@use "./components/media-editor-modal/style.scss" as *;
|
|
5
6
|
@use "./components/media-editor-canvas/style.scss" as *;
|
|
6
7
|
@use "./components/media-editor-toolbar/style.scss" as *;
|
|
8
|
+
@use "./components/media-editor-keyboard-shortcuts-modal/style.scss" as *;
|
|
9
|
+
@use "./components/rotation-ruler/style.scss" as *;
|