@wordpress/media-editor 0.19.0 → 0.21.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/CHANGELOG.md +34 -0
- package/README.md +2 -1
- package/build/components/media-editor/index.cjs +136 -63
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +2 -3
- package/build/components/media-editor/use-crop-options.cjs.map +2 -2
- package/build/components/media-editor/use-save-media-editor.cjs +36 -20
- package/build/components/media-editor/use-save-media-editor.cjs.map +2 -2
- package/build/components/media-editor-canvas/index.cjs +8 -8
- package/build/components/media-editor-canvas/index.cjs.map +3 -3
- package/build/components/media-editor-crop-panel/index.cjs +12 -8
- package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
- package/build/components/media-editor-fine-rotation/index.cjs +1 -1
- package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
- package/build/components/media-editor-image-controls/index.cjs +46 -29
- package/build/components/media-editor-image-controls/index.cjs.map +2 -2
- package/build/components/media-editor-modal/index.cjs +19 -9
- package/build/components/media-editor-modal/index.cjs.map +2 -2
- package/build/components/media-editor-provider/index.cjs +2 -1
- package/build/components/media-editor-provider/index.cjs.map +2 -2
- package/build/components/media-form/index.cjs +28 -28
- package/build/components/media-form/index.cjs.map +3 -3
- package/build/components/media-preview/index.cjs.map +3 -3
- package/build/hooks/use-crop-gesture-handlers.cjs +1 -1
- package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs +5 -0
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +4 -14
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +1 -1
- package/build/image-editor/react/components/stencils/rectangle-stencil.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-interaction.cjs +1 -1
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/state/composite-reducer.cjs +35 -10
- package/build/state/composite-reducer.cjs.map +3 -3
- package/build/state/index.cjs.map +1 -1
- package/build/state/media-editor-state-provider.cjs +1 -6
- package/build/state/media-editor-state-provider.cjs.map +2 -2
- package/build/state/types.cjs.map +1 -1
- package/build/state/use-media-editor-state.cjs +57 -42
- package/build/state/use-media-editor-state.cjs.map +2 -2
- package/build/store/actions.cjs +2 -0
- package/build/store/actions.cjs.map +2 -2
- package/build/store/reducer.cjs +3 -1
- package/build/store/reducer.cjs.map +2 -2
- package/build/store/selectors.cjs +5 -0
- package/build/store/selectors.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +151 -67
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +2 -3
- package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
- package/build-module/components/media-editor/use-save-media-editor.mjs +36 -20
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +2 -2
- package/build-module/components/media-editor-canvas/index.mjs +10 -10
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +13 -9
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs +1 -1
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
- package/build-module/components/media-editor-image-controls/index.mjs +47 -36
- package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
- package/build-module/components/media-editor-modal/index.mjs +19 -9
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-provider/index.mjs +2 -1
- package/build-module/components/media-editor-provider/index.mjs.map +2 -2
- package/build-module/components/media-form/index.mjs +33 -30
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/media-preview/index.mjs +2 -2
- package/build-module/components/media-preview/index.mjs.map +2 -2
- package/build-module/hooks/use-crop-gesture-handlers.mjs +1 -1
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs +5 -0
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +4 -14
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +1 -1
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.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-interaction.mjs +1 -1
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/state/composite-reducer.mjs +25 -10
- package/build-module/state/composite-reducer.mjs.map +2 -2
- package/build-module/state/index.mjs.map +1 -1
- package/build-module/state/media-editor-state-provider.mjs +1 -6
- package/build-module/state/media-editor-state-provider.mjs.map +2 -2
- package/build-module/state/types.mjs.map +1 -1
- package/build-module/state/use-media-editor-state.mjs +57 -42
- package/build-module/state/use-media-editor-state.mjs.map +2 -2
- package/build-module/store/actions.mjs +2 -0
- package/build-module/store/actions.mjs.map +2 -2
- package/build-module/store/reducer.mjs +3 -1
- package/build-module/store/reducer.mjs.map +2 -2
- package/build-module/store/selectors.mjs +4 -0
- package/build-module/store/selectors.mjs.map +2 -2
- package/build-style/style-rtl.css +5 -5
- package/build-style/style.css +5 -5
- package/build-types/components/media-editor/index.d.ts +4 -3
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +0 -1
- package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
- package/build-types/components/media-editor/use-save-media-editor.d.ts +15 -3
- package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -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.map +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/index.d.ts +7 -2
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-provider/index.d.ts +7 -1
- package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts +5 -2
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.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-interaction.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts +5 -5
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/index.d.ts +2 -2
- package/build-types/state/index.d.ts.map +1 -1
- package/build-types/state/media-editor-state-provider.d.ts +8 -16
- package/build-types/state/media-editor-state-provider.d.ts.map +1 -1
- package/build-types/state/types.d.ts +34 -5
- package/build-types/state/types.d.ts.map +1 -1
- package/build-types/state/use-media-editor-state.d.ts +53 -35
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/build-types/store/actions.d.ts +9 -1
- package/build-types/store/actions.d.ts.map +1 -1
- package/build-types/store/reducer.d.ts +3 -0
- package/build-types/store/reducer.d.ts.map +1 -1
- package/build-types/store/selectors.d.ts +1 -0
- package/build-types/store/selectors.d.ts.map +1 -1
- package/package.json +41 -25
- package/src/components/media-editor/index.tsx +213 -79
- package/src/components/media-editor/use-crop-options.ts +1 -3
- package/src/components/media-editor/use-save-media-editor.ts +61 -20
- package/src/components/media-editor-canvas/index.tsx +9 -10
- package/src/components/media-editor-crop-panel/index.tsx +17 -9
- package/src/components/media-editor-fine-rotation/index.tsx +1 -1
- package/src/components/media-editor-image-controls/index.tsx +41 -41
- package/src/components/media-editor-image-controls/style.scss +1 -1
- package/src/components/media-editor-modal/index.tsx +26 -11
- package/src/components/media-editor-provider/index.tsx +8 -1
- package/src/components/media-form/index.tsx +48 -42
- package/src/components/media-preview/index.tsx +2 -2
- package/src/hooks/use-crop-gesture-handlers.ts +3 -1
- package/src/image-editor/core/interaction-controller.ts +8 -0
- package/src/image-editor/docs/architecture.md +10 -2
- package/src/image-editor/docs/recipes.md +8 -8
- package/src/image-editor/react/components/cropper.scss +5 -5
- package/src/image-editor/react/components/cropper.tsx +9 -18
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +3 -1
- package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
- package/src/image-editor/react/hooks/use-interaction.ts +3 -2
- package/src/state/composite-reducer.ts +29 -17
- package/src/state/index.ts +3 -1
- package/src/state/media-editor-state-provider.tsx +9 -22
- package/src/state/types.ts +37 -5
- package/src/state/use-media-editor-state.ts +124 -78
- package/src/store/actions.ts +9 -0
- package/src/store/reducer.ts +6 -1
- package/src/store/selectors.ts +4 -0
- package/global.d.ts +0 -5
- package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -104
- package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -74
- package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -180
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -359
- package/src/components/media-editor-modal/test/index.jsdom.test.tsx +0 -165
- package/src/components/rotation-ruler/stories/index.story.tsx +0 -43
- package/src/components/rotation-ruler/test/index.jsdom.test.tsx +0 -140
- package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +0 -823
- package/src/image-editor/core/math/test/sanitize.ts +0 -242
- package/src/image-editor/core/test/camera.ts +0 -1218
- package/src/image-editor/core/test/interaction-controller.ts +0 -1254
- package/src/image-editor/core/test/preview-export-parity.ts +0 -249
- package/src/image-editor/core/test/render-camera-bridge.ts +0 -349
- package/src/image-editor/core/test/state.ts +0 -876
- package/src/image-editor/core/test/stencil-math.ts +0 -665
- package/src/image-editor/core/transforms/test/pipeline.ts +0 -267
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.browser.test.tsx +0 -438
- package/src/image-editor/react/components/test/cropper.browser.test.tsx +0 -1460
- package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +0 -491
- package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +0 -1049
- package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +0 -213
- package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +0 -140
- package/src/image-editor/stories/rectangle-crop.story.tsx +0 -1073
- package/src/image-editor/stories/style.module.css +0 -65
- package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -324
- package/src/store/test/reducer.ts +0 -64
- package/tsconfig.build.json +0 -26
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -37
|
@@ -25,6 +25,8 @@ import {
|
|
|
25
25
|
} from './composite-reducer';
|
|
26
26
|
import type {
|
|
27
27
|
CropOptionsSlice,
|
|
28
|
+
EditsSlice,
|
|
29
|
+
EditValue,
|
|
28
30
|
MediaEditorAction,
|
|
29
31
|
MediaEditorState,
|
|
30
32
|
} from './types';
|
|
@@ -81,23 +83,57 @@ function areCropperImagesEqual(
|
|
|
81
83
|
}
|
|
82
84
|
|
|
83
85
|
/**
|
|
84
|
-
* The
|
|
86
|
+
* The media editor session exposed by `useMediaEditorState`.
|
|
85
87
|
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
88
|
+
* The session owns what is being edited and how: the source image,
|
|
89
|
+
* every editing slice, the shared undo/redo history, gesture
|
|
90
|
+
* boundaries, and whether the edits change the saved image. The
|
|
91
|
+
* cropper is one participant in the session — `cropper` is a
|
|
92
|
+
* geometry-only controller that a `<Cropper>` takes via its
|
|
93
|
+
* `controller` prop — rather than the session itself.
|
|
89
94
|
*/
|
|
90
|
-
export interface
|
|
95
|
+
export interface MediaEditorSession {
|
|
96
|
+
/**
|
|
97
|
+
* Geometry controller for the cropper slice. Its setters record
|
|
98
|
+
* history in the session; its `isDirty` covers geometry only.
|
|
99
|
+
* Its `setImage` is `setSourceImage`, so the `<Cropper>` reporting
|
|
100
|
+
* a newly loaded image starts a fresh session.
|
|
101
|
+
*/
|
|
102
|
+
cropper: CropperController;
|
|
91
103
|
/** The cropOptions slice. */
|
|
92
104
|
cropOptions: CropOptionsSlice;
|
|
93
105
|
/** Set the aspect-ratio preset. Atomic with the cropRect reshape. */
|
|
94
106
|
setAspectRatioValue: ( presetKey: string ) => void;
|
|
95
|
-
/** Reset cropOptions to defaults. */
|
|
96
|
-
resetCropOptions: () => void;
|
|
97
107
|
/**
|
|
98
|
-
*
|
|
108
|
+
* Put every slice back to its starting state in one undo entry:
|
|
109
|
+
* default geometry and crop options, no edits. Keeps the image.
|
|
110
|
+
*/
|
|
111
|
+
reset: () => void;
|
|
112
|
+
/** The named edits other than crop, keyed by edit name. */
|
|
113
|
+
edits: EditsSlice;
|
|
114
|
+
/**
|
|
115
|
+
* Set a named edit, or remove it by passing `undefined`. Recorded
|
|
116
|
+
* in the session history like any crop change. Setting a value
|
|
117
|
+
* equal in content to the current one is a no-op.
|
|
118
|
+
*/
|
|
119
|
+
setEdit: ( name: string, value: EditValue | undefined ) => void;
|
|
120
|
+
/**
|
|
121
|
+
* Replace the image being edited. A different image starts a
|
|
122
|
+
* fresh session: default geometry and crop options, a clean
|
|
123
|
+
* baseline, and no history. Reporting the same image again is a
|
|
124
|
+
* no-op.
|
|
125
|
+
*/
|
|
126
|
+
setSourceImage: ( image: CropperState[ 'image' ] ) => void;
|
|
127
|
+
/**
|
|
128
|
+
* Whether anything in the session differs from its baseline,
|
|
129
|
+
* including editor-only state such as the aspect-ratio preset.
|
|
99
130
|
*/
|
|
100
|
-
|
|
131
|
+
isDirty: boolean;
|
|
132
|
+
/**
|
|
133
|
+
* Whether the edits change the saved image, so saving has to
|
|
134
|
+
* produce new image output rather than only metadata.
|
|
135
|
+
*/
|
|
136
|
+
hasOutputEdits: boolean;
|
|
101
137
|
/** Whether there's an undoable change in history. */
|
|
102
138
|
hasUndo: boolean;
|
|
103
139
|
/** Whether there's a redoable change in history. */
|
|
@@ -114,31 +150,14 @@ export interface MediaEditorController extends CropperController {
|
|
|
114
150
|
beginGesture: () => void;
|
|
115
151
|
/** Close a gesture boundary and commit the pre-gesture snapshot. */
|
|
116
152
|
endGesture: () => void;
|
|
117
|
-
/**
|
|
118
|
-
* Report the current rendered image size to the controller. The
|
|
119
|
-
* Cropper component calls this whenever its visualSize changes;
|
|
120
|
-
* the controller needs it to compute aspect-ratio reshapes.
|
|
121
|
-
*/
|
|
122
|
-
setVisualSize: ( size: Size ) => void;
|
|
123
|
-
/**
|
|
124
|
-
* Reshape the cropRect in response to a viewport change. Behaves
|
|
125
|
-
* like `setCropRect` but does NOT record an undo entry — window
|
|
126
|
-
* resizes aren't editor actions.
|
|
127
|
-
*/
|
|
128
|
-
adjustCropRectForViewport: ( rect: NormalizedRect ) => void;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
interface InitialMediaEditorState {
|
|
132
|
-
cropper?: Partial< CropperState >;
|
|
133
|
-
cropOptions?: Partial< CropOptionsSlice >;
|
|
134
153
|
}
|
|
135
154
|
|
|
136
155
|
/**
|
|
137
|
-
*
|
|
156
|
+
* The media editor session.
|
|
138
157
|
*
|
|
139
158
|
* Owns the only undo/redo history for the editor. Cropper geometry
|
|
140
|
-
* actions
|
|
141
|
-
* reducer and the same history stack.
|
|
159
|
+
* actions, sidebar cropOptions actions and named edits flow through
|
|
160
|
+
* the same reducer and the same history stack.
|
|
142
161
|
*
|
|
143
162
|
* History semantics:
|
|
144
163
|
* - Each action's pre-state is pushed to the undo stack before the
|
|
@@ -152,35 +171,28 @@ interface InitialMediaEditorState {
|
|
|
152
171
|
* undo entries.
|
|
153
172
|
*
|
|
154
173
|
* `isDirty` tracks the full editor session, including undoable UI
|
|
155
|
-
* state. Use `
|
|
174
|
+
* state. Use `hasOutputEdits` when deciding whether the image output
|
|
156
175
|
* needs saving.
|
|
157
176
|
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
177
|
+
* The session starts empty. Call `setSourceImage` to load an image.
|
|
178
|
+
*
|
|
179
|
+
* @return The media editor session.
|
|
160
180
|
*/
|
|
161
|
-
export function useMediaEditorState(
|
|
162
|
-
initialState?: InitialMediaEditorState
|
|
163
|
-
): MediaEditorController {
|
|
181
|
+
export function useMediaEditorState(): MediaEditorSession {
|
|
164
182
|
const [ state, dispatch ] = useReducer( mediaEditorReducer, null, () =>
|
|
165
|
-
buildInitialMediaEditorState(
|
|
166
|
-
enforceContainment( {
|
|
167
|
-
...DEFAULT_STATE,
|
|
168
|
-
...initialState?.cropper,
|
|
169
|
-
} ),
|
|
170
|
-
initialState?.cropOptions
|
|
171
|
-
)
|
|
183
|
+
buildInitialMediaEditorState( { ...DEFAULT_STATE } )
|
|
172
184
|
);
|
|
173
185
|
|
|
174
186
|
// The "clean" snapshot the current state is compared against for
|
|
175
187
|
// `isDirty`. Stored in state (not a ref) so the comparison reads
|
|
176
|
-
// a value, not a ref's `current`. `
|
|
188
|
+
// a value, not a ref's `current`. `setSourceImage` refreshes this when
|
|
177
189
|
// a new image establishes a fresh baseline.
|
|
178
190
|
const [ initialBaseline, setInitialBaseline ] =
|
|
179
191
|
useState< MediaEditorState >( () => state );
|
|
180
192
|
|
|
181
193
|
// Latest-state ref used by setters that need to read fresh state
|
|
182
194
|
// (focal-point zoom, dispatch wrapper). Every mutation path
|
|
183
|
-
// (`dispatchWithHistory`, `
|
|
195
|
+
// (`dispatchWithHistory`, `setSourceImage`, `undo`, `redo`) writes this
|
|
184
196
|
// synchronously, so multiple actions in one event see the latest
|
|
185
197
|
// reducer output before React commits.
|
|
186
198
|
const stateRef = useRef( state );
|
|
@@ -281,19 +293,20 @@ export function useMediaEditorState(
|
|
|
281
293
|
[ dispatchCropperAction ]
|
|
282
294
|
);
|
|
283
295
|
|
|
284
|
-
const
|
|
296
|
+
const setSourceImage = useCallback( ( image: CropperState[ 'image' ] ) => {
|
|
285
297
|
if ( areCropperImagesEqual( stateRef.current.cropper.image, image ) ) {
|
|
286
298
|
return;
|
|
287
299
|
}
|
|
288
|
-
// New image = fresh
|
|
289
|
-
//
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
300
|
+
// New image = fresh session: default geometry and crop options, a
|
|
301
|
+
// clean baseline, and no history. Built from defaults rather than
|
|
302
|
+
// dispatching SET_IMAGE, which keeps the current pan / zoom /
|
|
303
|
+
// rotation / flip / cropRect — edits made against one image must
|
|
304
|
+
// not survive onto another.
|
|
305
|
+
const next = buildInitialMediaEditorState(
|
|
306
|
+
enforceContainment( { ...DEFAULT_STATE, image } )
|
|
307
|
+
);
|
|
295
308
|
stateRef.current = next;
|
|
296
|
-
dispatch(
|
|
309
|
+
dispatch( { type: 'RESTORE_SNAPSHOT', payload: next } );
|
|
297
310
|
setInitialBaseline( next );
|
|
298
311
|
isGestureOpenRef.current = false;
|
|
299
312
|
gestureSnapshotRef.current = null;
|
|
@@ -303,7 +316,7 @@ export function useMediaEditorState(
|
|
|
303
316
|
setHasRedo( false );
|
|
304
317
|
}, [] );
|
|
305
318
|
|
|
306
|
-
const
|
|
319
|
+
const resetCropper = useCallback(
|
|
307
320
|
( resetState?: Partial< CropperState > ) => {
|
|
308
321
|
dispatchWithHistory( {
|
|
309
322
|
type: 'CROPPER',
|
|
@@ -335,10 +348,28 @@ export function useMediaEditorState(
|
|
|
335
348
|
[ dispatchWithHistory ]
|
|
336
349
|
);
|
|
337
350
|
|
|
338
|
-
|
|
339
|
-
|
|
351
|
+
// =====================================================================
|
|
352
|
+
// Session
|
|
353
|
+
// =====================================================================
|
|
354
|
+
|
|
355
|
+
const reset = useCallback( () => {
|
|
356
|
+
dispatchWithHistory( { type: 'RESET' } );
|
|
340
357
|
}, [ dispatchWithHistory ] );
|
|
341
358
|
|
|
359
|
+
// =====================================================================
|
|
360
|
+
// Edits-slice setters
|
|
361
|
+
// =====================================================================
|
|
362
|
+
|
|
363
|
+
const setEdit = useCallback(
|
|
364
|
+
( name: string, value: EditValue | undefined ) => {
|
|
365
|
+
dispatchWithHistory( {
|
|
366
|
+
type: 'SET_EDIT',
|
|
367
|
+
payload: { name, value },
|
|
368
|
+
} );
|
|
369
|
+
},
|
|
370
|
+
[ dispatchWithHistory ]
|
|
371
|
+
);
|
|
372
|
+
|
|
342
373
|
// =====================================================================
|
|
343
374
|
// History
|
|
344
375
|
// =====================================================================
|
|
@@ -403,7 +434,7 @@ export function useMediaEditorState(
|
|
|
403
434
|
( mimeType?: string, quality?: number ): Promise< Blob > => {
|
|
404
435
|
if ( ! state.cropper.image ) {
|
|
405
436
|
return Promise.reject(
|
|
406
|
-
new Error( 'No image loaded — call
|
|
437
|
+
new Error( 'No image loaded — call setSourceImage first.' )
|
|
407
438
|
);
|
|
408
439
|
}
|
|
409
440
|
return exportCroppedImage(
|
|
@@ -416,40 +447,57 @@ export function useMediaEditorState(
|
|
|
416
447
|
[ state.cropper ]
|
|
417
448
|
);
|
|
418
449
|
|
|
419
|
-
const
|
|
450
|
+
const cropper = useMemo< CropperController >(
|
|
420
451
|
() => ( {
|
|
421
|
-
// CropperController surface (state is the cropper slice so a
|
|
422
|
-
// <Cropper> takes this controller as-is).
|
|
423
452
|
...cropperSetters,
|
|
424
453
|
state: state.cropper,
|
|
425
|
-
setImage,
|
|
426
|
-
reset,
|
|
427
|
-
isDirty,
|
|
454
|
+
setImage: setSourceImage,
|
|
455
|
+
reset: resetCropper,
|
|
456
|
+
isDirty: isCropperDirty,
|
|
428
457
|
getCroppedImage,
|
|
429
|
-
|
|
458
|
+
setVisualSize,
|
|
459
|
+
adjustCropRectForViewport,
|
|
460
|
+
} ),
|
|
461
|
+
[
|
|
462
|
+
cropperSetters,
|
|
463
|
+
state.cropper,
|
|
464
|
+
setSourceImage,
|
|
465
|
+
resetCropper,
|
|
466
|
+
isCropperDirty,
|
|
467
|
+
getCroppedImage,
|
|
468
|
+
setVisualSize,
|
|
469
|
+
adjustCropRectForViewport,
|
|
470
|
+
]
|
|
471
|
+
);
|
|
472
|
+
|
|
473
|
+
const session = useMemo< MediaEditorSession >(
|
|
474
|
+
() => ( {
|
|
475
|
+
cropper,
|
|
430
476
|
cropOptions: state.cropOptions,
|
|
431
477
|
setAspectRatioValue,
|
|
432
|
-
|
|
433
|
-
|
|
478
|
+
edits: state.edits,
|
|
479
|
+
setEdit,
|
|
480
|
+
setSourceImage,
|
|
481
|
+
reset,
|
|
482
|
+
isDirty,
|
|
483
|
+
// Only geometry changes the saved image so far.
|
|
484
|
+
hasOutputEdits: isCropperDirty,
|
|
434
485
|
hasUndo,
|
|
435
486
|
hasRedo,
|
|
436
487
|
undo,
|
|
437
488
|
redo,
|
|
438
489
|
beginGesture,
|
|
439
490
|
endGesture,
|
|
440
|
-
setVisualSize,
|
|
441
|
-
adjustCropRectForViewport,
|
|
442
491
|
} ),
|
|
443
492
|
[
|
|
444
|
-
|
|
445
|
-
state.cropper,
|
|
446
|
-
setImage,
|
|
447
|
-
reset,
|
|
448
|
-
isDirty,
|
|
449
|
-
getCroppedImage,
|
|
493
|
+
cropper,
|
|
450
494
|
state.cropOptions,
|
|
451
495
|
setAspectRatioValue,
|
|
452
|
-
|
|
496
|
+
state.edits,
|
|
497
|
+
setEdit,
|
|
498
|
+
setSourceImage,
|
|
499
|
+
reset,
|
|
500
|
+
isDirty,
|
|
453
501
|
isCropperDirty,
|
|
454
502
|
hasUndo,
|
|
455
503
|
hasRedo,
|
|
@@ -457,9 +505,7 @@ export function useMediaEditorState(
|
|
|
457
505
|
redo,
|
|
458
506
|
beginGesture,
|
|
459
507
|
endGesture,
|
|
460
|
-
setVisualSize,
|
|
461
|
-
adjustCropRectForViewport,
|
|
462
508
|
]
|
|
463
509
|
);
|
|
464
|
-
return
|
|
510
|
+
return session;
|
|
465
511
|
}
|
package/src/store/actions.ts
CHANGED
|
@@ -15,18 +15,27 @@ export interface MediaEditorModalUpdate {
|
|
|
15
15
|
interface OpenMediaEditorModalArgs {
|
|
16
16
|
id: number;
|
|
17
17
|
onUpdate?: ( updated: MediaEditorModalUpdate ) => void;
|
|
18
|
+
/**
|
|
19
|
+
* Restore the caller's state after a saved attachment change. May run
|
|
20
|
+
* before an asynchronous `onUpdate` finishes; the caller should cancel
|
|
21
|
+
* that update. Without this callback, Undo calls `onUpdate` with the
|
|
22
|
+
* previous attachment's id and URL.
|
|
23
|
+
*/
|
|
24
|
+
onUndo?: () => void;
|
|
18
25
|
onClose?: () => void;
|
|
19
26
|
}
|
|
20
27
|
|
|
21
28
|
export function openMediaEditorModal( {
|
|
22
29
|
id,
|
|
23
30
|
onUpdate,
|
|
31
|
+
onUndo,
|
|
24
32
|
onClose,
|
|
25
33
|
}: OpenMediaEditorModalArgs ) {
|
|
26
34
|
return {
|
|
27
35
|
type: 'OPEN_MEDIA_EDITOR_MODAL' as const,
|
|
28
36
|
id,
|
|
29
37
|
onUpdate: onUpdate ?? null,
|
|
38
|
+
onUndo: onUndo ?? null,
|
|
30
39
|
onClose: onClose ?? null,
|
|
31
40
|
};
|
|
32
41
|
}
|
package/src/store/reducer.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import type { MediaEditorModalUpdate } from './actions';
|
|
2
2
|
|
|
3
3
|
type OnUpdateCallback = ( updated: MediaEditorModalUpdate ) => void;
|
|
4
|
+
type OnUndoCallback = () => void;
|
|
4
5
|
type OnCloseCallback = () => void;
|
|
5
6
|
|
|
6
7
|
export interface State {
|
|
7
8
|
isOpen: boolean;
|
|
8
9
|
id: number | null;
|
|
9
10
|
onUpdate: OnUpdateCallback | null;
|
|
11
|
+
onUndo: OnUndoCallback | null;
|
|
10
12
|
onClose: OnCloseCallback | null;
|
|
11
13
|
}
|
|
12
14
|
|
|
@@ -14,6 +16,7 @@ export const DEFAULT_STATE: State = {
|
|
|
14
16
|
isOpen: false,
|
|
15
17
|
id: null,
|
|
16
18
|
onUpdate: null,
|
|
19
|
+
onUndo: null,
|
|
17
20
|
onClose: null,
|
|
18
21
|
};
|
|
19
22
|
|
|
@@ -22,6 +25,7 @@ type Action =
|
|
|
22
25
|
type: 'OPEN_MEDIA_EDITOR_MODAL';
|
|
23
26
|
id: number;
|
|
24
27
|
onUpdate: OnUpdateCallback | null;
|
|
28
|
+
onUndo: OnUndoCallback | null;
|
|
25
29
|
onClose: OnCloseCallback | null;
|
|
26
30
|
}
|
|
27
31
|
| { type: 'CLOSE_MEDIA_EDITOR_MODAL' };
|
|
@@ -32,7 +36,7 @@ export default function reducer(
|
|
|
32
36
|
): State {
|
|
33
37
|
switch ( action.type ) {
|
|
34
38
|
case 'OPEN_MEDIA_EDITOR_MODAL': {
|
|
35
|
-
const { id, onUpdate, onClose } = action as Extract<
|
|
39
|
+
const { id, onUpdate, onUndo, onClose } = action as Extract<
|
|
36
40
|
Action,
|
|
37
41
|
{ type: 'OPEN_MEDIA_EDITOR_MODAL' }
|
|
38
42
|
>;
|
|
@@ -40,6 +44,7 @@ export default function reducer(
|
|
|
40
44
|
isOpen: true,
|
|
41
45
|
id,
|
|
42
46
|
onUpdate,
|
|
47
|
+
onUndo,
|
|
43
48
|
onClose,
|
|
44
49
|
};
|
|
45
50
|
}
|
package/src/store/selectors.ts
CHANGED
package/global.d.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { fireEvent, render, screen } from '@testing-library/react';
|
|
3
|
-
import { useCropOptions } from '../use-crop-options';
|
|
4
|
-
import { ORIGINAL_ASPECT_RATIO } from '../../../image-editor/core/constants';
|
|
5
|
-
import { MediaEditorStateProvider } from '../../../state';
|
|
6
|
-
|
|
7
|
-
function CropOptionsHarness() {
|
|
8
|
-
const cropOptions = useCropOptions( {
|
|
9
|
-
aspectRatioPresets: [
|
|
10
|
-
{ label: 'Square', value: 1 },
|
|
11
|
-
{ label: 'Landscape', value: 4 / 3 },
|
|
12
|
-
],
|
|
13
|
-
} );
|
|
14
|
-
|
|
15
|
-
return (
|
|
16
|
-
<div>
|
|
17
|
-
<div data-testid="aspect-ratio-value">
|
|
18
|
-
{ cropOptions.aspectRatioValue }
|
|
19
|
-
</div>
|
|
20
|
-
<div data-testid="resolved-aspect-ratio">
|
|
21
|
-
{ cropOptions.resolvedAspectRatio ?? 'undefined' }
|
|
22
|
-
</div>
|
|
23
|
-
<div data-testid="aspect-ratio-options">
|
|
24
|
-
{ cropOptions.aspectRatioOptions
|
|
25
|
-
.map( ( option ) => option.value )
|
|
26
|
-
.join( ',' ) }
|
|
27
|
-
</div>
|
|
28
|
-
<button
|
|
29
|
-
onClick={ () =>
|
|
30
|
-
cropOptions.setAspectRatioValue(
|
|
31
|
-
ORIGINAL_ASPECT_RATIO.toString()
|
|
32
|
-
)
|
|
33
|
-
}
|
|
34
|
-
>
|
|
35
|
-
Original
|
|
36
|
-
</button>
|
|
37
|
-
<button onClick={ () => cropOptions.setAspectRatioValue( '1' ) }>
|
|
38
|
-
Square
|
|
39
|
-
</button>
|
|
40
|
-
<button onClick={ () => cropOptions.setAspectRatioValue( '0' ) }>
|
|
41
|
-
Free
|
|
42
|
-
</button>
|
|
43
|
-
<button onClick={ cropOptions.resetCropOptions }>Reset</button>
|
|
44
|
-
</div>
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function renderHarness() {
|
|
49
|
-
render(
|
|
50
|
-
<MediaEditorStateProvider
|
|
51
|
-
initialCropperState={ {
|
|
52
|
-
image: {
|
|
53
|
-
src: 'test.jpg',
|
|
54
|
-
naturalWidth: 1200,
|
|
55
|
-
naturalHeight: 600,
|
|
56
|
-
},
|
|
57
|
-
} }
|
|
58
|
-
>
|
|
59
|
-
<CropOptionsHarness />
|
|
60
|
-
</MediaEditorStateProvider>
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
describe( 'useCropOptions', () => {
|
|
65
|
-
it( 'builds explicit aspect-ratio options', () => {
|
|
66
|
-
renderHarness();
|
|
67
|
-
|
|
68
|
-
expect(
|
|
69
|
-
screen.getByTestId( 'aspect-ratio-options' )
|
|
70
|
-
).toHaveTextContent( '0,-1,1,1.3333333333333333' );
|
|
71
|
-
} );
|
|
72
|
-
|
|
73
|
-
it( 'resolves the Original aspect ratio from the cropper image dimensions', () => {
|
|
74
|
-
renderHarness();
|
|
75
|
-
|
|
76
|
-
fireEvent.click( screen.getByRole( 'button', { name: 'Original' } ) );
|
|
77
|
-
|
|
78
|
-
// 1200 / 600 = 2.
|
|
79
|
-
expect(
|
|
80
|
-
screen.getByTestId( 'resolved-aspect-ratio' )
|
|
81
|
-
).toHaveTextContent( '2' );
|
|
82
|
-
} );
|
|
83
|
-
|
|
84
|
-
it( 'reports Free as undefined resolved aspect ratio', () => {
|
|
85
|
-
renderHarness();
|
|
86
|
-
|
|
87
|
-
fireEvent.click( screen.getByRole( 'button', { name: 'Free' } ) );
|
|
88
|
-
|
|
89
|
-
expect(
|
|
90
|
-
screen.getByTestId( 'resolved-aspect-ratio' )
|
|
91
|
-
).toHaveTextContent( 'undefined' );
|
|
92
|
-
} );
|
|
93
|
-
|
|
94
|
-
it( 'reset returns cropOptions to defaults', () => {
|
|
95
|
-
renderHarness();
|
|
96
|
-
|
|
97
|
-
fireEvent.click( screen.getByRole( 'button', { name: 'Square' } ) );
|
|
98
|
-
fireEvent.click( screen.getByRole( 'button', { name: 'Reset' } ) );
|
|
99
|
-
|
|
100
|
-
expect( screen.getByTestId( 'aspect-ratio-value' ) ).toHaveTextContent(
|
|
101
|
-
'0'
|
|
102
|
-
);
|
|
103
|
-
} );
|
|
104
|
-
} );
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi, type Mock } from 'vitest';
|
|
2
|
-
import { fireEvent, render, screen } from '@testing-library/react';
|
|
3
|
-
import MediaEditorCropPanel from '..';
|
|
4
|
-
import type { MediaEditorCropPanelProps } from '..';
|
|
5
|
-
import { MediaEditorStateProvider } from '../../../state';
|
|
6
|
-
import type { CropperState } from '../../../image-editor';
|
|
7
|
-
|
|
8
|
-
globalThis.wpVitest.mockMatchMedia();
|
|
9
|
-
|
|
10
|
-
function setupCropPanel(
|
|
11
|
-
overrides: Partial< MediaEditorCropPanelProps > = {},
|
|
12
|
-
initialCropperState?: Partial< CropperState >
|
|
13
|
-
) {
|
|
14
|
-
const props: MediaEditorCropPanelProps = {
|
|
15
|
-
aspectRatioValue: '1',
|
|
16
|
-
onAspectRatioChange: vi.fn(),
|
|
17
|
-
aspectRatioOptions: [
|
|
18
|
-
{ label: 'Free', value: 0 },
|
|
19
|
-
{ label: 'Original', value: -1 },
|
|
20
|
-
{ label: 'Square', value: 1 },
|
|
21
|
-
],
|
|
22
|
-
...overrides,
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
render(
|
|
26
|
-
<MediaEditorStateProvider initialCropperState={ initialCropperState }>
|
|
27
|
-
<MediaEditorCropPanel { ...props } />
|
|
28
|
-
</MediaEditorStateProvider>
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
return props;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
describe( 'MediaEditorCropPanel', () => {
|
|
35
|
-
it( 'passes selected aspect ratio changes to the caller', () => {
|
|
36
|
-
const controls = setupCropPanel( {
|
|
37
|
-
aspectRatioValue: '1',
|
|
38
|
-
} );
|
|
39
|
-
|
|
40
|
-
fireEvent.change( screen.getByLabelText( 'Aspect ratio' ), {
|
|
41
|
-
target: { value: '0' },
|
|
42
|
-
} );
|
|
43
|
-
|
|
44
|
-
expect( controls.onAspectRatioChange ).toHaveBeenCalled();
|
|
45
|
-
expect(
|
|
46
|
-
( controls.onAspectRatioChange as Mock ).mock.calls[ 0 ][ 0 ]
|
|
47
|
-
).toBe( '0' );
|
|
48
|
-
} );
|
|
49
|
-
|
|
50
|
-
it( 'renders rotate, flip and zoom controls', () => {
|
|
51
|
-
setupCropPanel();
|
|
52
|
-
|
|
53
|
-
expect( screen.getByText( 'Rotate' ) ).toBeInTheDocument();
|
|
54
|
-
expect( screen.getByText( 'Flip' ) ).toBeInTheDocument();
|
|
55
|
-
expect( screen.getByText( 'Zoom' ) ).toBeInTheDocument();
|
|
56
|
-
expect(
|
|
57
|
-
screen.getByRole( 'button', { name: 'Rotate 90° clockwise' } )
|
|
58
|
-
).toBeInTheDocument();
|
|
59
|
-
expect(
|
|
60
|
-
screen.getByRole( 'button', { name: 'Zoom in' } )
|
|
61
|
-
).toBeInTheDocument();
|
|
62
|
-
} );
|
|
63
|
-
|
|
64
|
-
it( 'renders the image controls above the aspect-ratio selector', () => {
|
|
65
|
-
setupCropPanel();
|
|
66
|
-
|
|
67
|
-
const rotate = screen.getByText( 'Rotate' );
|
|
68
|
-
const aspectRatio = screen.getByLabelText( 'Aspect ratio' );
|
|
69
|
-
|
|
70
|
-
expect( rotate.compareDocumentPosition( aspectRatio ) ).toBe(
|
|
71
|
-
Node.DOCUMENT_POSITION_FOLLOWING
|
|
72
|
-
);
|
|
73
|
-
} );
|
|
74
|
-
} );
|