@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.
Files changed (196) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +2 -1
  3. package/build/components/media-editor/index.cjs +136 -63
  4. package/build/components/media-editor/index.cjs.map +3 -3
  5. package/build/components/media-editor/use-crop-options.cjs +2 -3
  6. package/build/components/media-editor/use-crop-options.cjs.map +2 -2
  7. package/build/components/media-editor/use-save-media-editor.cjs +36 -20
  8. package/build/components/media-editor/use-save-media-editor.cjs.map +2 -2
  9. package/build/components/media-editor-canvas/index.cjs +8 -8
  10. package/build/components/media-editor-canvas/index.cjs.map +3 -3
  11. package/build/components/media-editor-crop-panel/index.cjs +12 -8
  12. package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
  13. package/build/components/media-editor-fine-rotation/index.cjs +1 -1
  14. package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
  15. package/build/components/media-editor-image-controls/index.cjs +46 -29
  16. package/build/components/media-editor-image-controls/index.cjs.map +2 -2
  17. package/build/components/media-editor-modal/index.cjs +19 -9
  18. package/build/components/media-editor-modal/index.cjs.map +2 -2
  19. package/build/components/media-editor-provider/index.cjs +2 -1
  20. package/build/components/media-editor-provider/index.cjs.map +2 -2
  21. package/build/components/media-form/index.cjs +28 -28
  22. package/build/components/media-form/index.cjs.map +3 -3
  23. package/build/components/media-preview/index.cjs.map +3 -3
  24. package/build/hooks/use-crop-gesture-handlers.cjs +1 -1
  25. package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
  26. package/build/image-editor/core/interaction-controller.cjs +5 -0
  27. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  28. package/build/image-editor/react/components/cropper.cjs +4 -14
  29. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  30. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +1 -1
  31. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  32. package/build/image-editor/react/hooks/use-aria-announcer.cjs +3 -1
  33. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
  34. package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
  35. package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
  36. package/build/state/composite-reducer.cjs +35 -10
  37. package/build/state/composite-reducer.cjs.map +3 -3
  38. package/build/state/index.cjs.map +1 -1
  39. package/build/state/media-editor-state-provider.cjs +1 -6
  40. package/build/state/media-editor-state-provider.cjs.map +2 -2
  41. package/build/state/types.cjs.map +1 -1
  42. package/build/state/use-media-editor-state.cjs +57 -42
  43. package/build/state/use-media-editor-state.cjs.map +2 -2
  44. package/build/store/actions.cjs +2 -0
  45. package/build/store/actions.cjs.map +2 -2
  46. package/build/store/reducer.cjs +3 -1
  47. package/build/store/reducer.cjs.map +2 -2
  48. package/build/store/selectors.cjs +5 -0
  49. package/build/store/selectors.cjs.map +2 -2
  50. package/build-module/components/media-editor/index.mjs +151 -67
  51. package/build-module/components/media-editor/index.mjs.map +2 -2
  52. package/build-module/components/media-editor/use-crop-options.mjs +2 -3
  53. package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
  54. package/build-module/components/media-editor/use-save-media-editor.mjs +36 -20
  55. package/build-module/components/media-editor/use-save-media-editor.mjs.map +2 -2
  56. package/build-module/components/media-editor-canvas/index.mjs +10 -10
  57. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  58. package/build-module/components/media-editor-crop-panel/index.mjs +13 -9
  59. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  60. package/build-module/components/media-editor-fine-rotation/index.mjs +1 -1
  61. package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
  62. package/build-module/components/media-editor-image-controls/index.mjs +47 -36
  63. package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
  64. package/build-module/components/media-editor-modal/index.mjs +19 -9
  65. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  66. package/build-module/components/media-editor-provider/index.mjs +2 -1
  67. package/build-module/components/media-editor-provider/index.mjs.map +2 -2
  68. package/build-module/components/media-form/index.mjs +33 -30
  69. package/build-module/components/media-form/index.mjs.map +2 -2
  70. package/build-module/components/media-preview/index.mjs +2 -2
  71. package/build-module/components/media-preview/index.mjs.map +2 -2
  72. package/build-module/hooks/use-crop-gesture-handlers.mjs +1 -1
  73. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
  74. package/build-module/image-editor/core/interaction-controller.mjs +5 -0
  75. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  76. package/build-module/image-editor/react/components/cropper.mjs +4 -14
  77. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  78. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +1 -1
  79. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  80. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +3 -1
  81. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
  82. package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
  83. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
  84. package/build-module/state/composite-reducer.mjs +25 -10
  85. package/build-module/state/composite-reducer.mjs.map +2 -2
  86. package/build-module/state/index.mjs.map +1 -1
  87. package/build-module/state/media-editor-state-provider.mjs +1 -6
  88. package/build-module/state/media-editor-state-provider.mjs.map +2 -2
  89. package/build-module/state/types.mjs.map +1 -1
  90. package/build-module/state/use-media-editor-state.mjs +57 -42
  91. package/build-module/state/use-media-editor-state.mjs.map +2 -2
  92. package/build-module/store/actions.mjs +2 -0
  93. package/build-module/store/actions.mjs.map +2 -2
  94. package/build-module/store/reducer.mjs +3 -1
  95. package/build-module/store/reducer.mjs.map +2 -2
  96. package/build-module/store/selectors.mjs +4 -0
  97. package/build-module/store/selectors.mjs.map +2 -2
  98. package/build-style/style-rtl.css +5 -5
  99. package/build-style/style.css +5 -5
  100. package/build-types/components/media-editor/index.d.ts +4 -3
  101. package/build-types/components/media-editor/index.d.ts.map +1 -1
  102. package/build-types/components/media-editor/use-crop-options.d.ts +0 -1
  103. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
  104. package/build-types/components/media-editor/use-save-media-editor.d.ts +15 -3
  105. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
  106. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  107. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  108. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  109. package/build-types/components/media-editor-modal/index.d.ts +7 -2
  110. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  111. package/build-types/components/media-editor-provider/index.d.ts +7 -1
  112. package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
  113. package/build-types/components/media-form/index.d.ts +5 -2
  114. package/build-types/components/media-form/index.d.ts.map +1 -1
  115. package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
  116. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  117. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  118. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  119. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
  120. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
  121. package/build-types/state/composite-reducer.d.ts +5 -5
  122. package/build-types/state/composite-reducer.d.ts.map +1 -1
  123. package/build-types/state/index.d.ts +2 -2
  124. package/build-types/state/index.d.ts.map +1 -1
  125. package/build-types/state/media-editor-state-provider.d.ts +8 -16
  126. package/build-types/state/media-editor-state-provider.d.ts.map +1 -1
  127. package/build-types/state/types.d.ts +34 -5
  128. package/build-types/state/types.d.ts.map +1 -1
  129. package/build-types/state/use-media-editor-state.d.ts +53 -35
  130. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  131. package/build-types/store/actions.d.ts +9 -1
  132. package/build-types/store/actions.d.ts.map +1 -1
  133. package/build-types/store/reducer.d.ts +3 -0
  134. package/build-types/store/reducer.d.ts.map +1 -1
  135. package/build-types/store/selectors.d.ts +1 -0
  136. package/build-types/store/selectors.d.ts.map +1 -1
  137. package/package.json +41 -25
  138. package/src/components/media-editor/index.tsx +213 -79
  139. package/src/components/media-editor/use-crop-options.ts +1 -3
  140. package/src/components/media-editor/use-save-media-editor.ts +61 -20
  141. package/src/components/media-editor-canvas/index.tsx +9 -10
  142. package/src/components/media-editor-crop-panel/index.tsx +17 -9
  143. package/src/components/media-editor-fine-rotation/index.tsx +1 -1
  144. package/src/components/media-editor-image-controls/index.tsx +41 -41
  145. package/src/components/media-editor-image-controls/style.scss +1 -1
  146. package/src/components/media-editor-modal/index.tsx +26 -11
  147. package/src/components/media-editor-provider/index.tsx +8 -1
  148. package/src/components/media-form/index.tsx +48 -42
  149. package/src/components/media-preview/index.tsx +2 -2
  150. package/src/hooks/use-crop-gesture-handlers.ts +3 -1
  151. package/src/image-editor/core/interaction-controller.ts +8 -0
  152. package/src/image-editor/docs/architecture.md +10 -2
  153. package/src/image-editor/docs/recipes.md +8 -8
  154. package/src/image-editor/react/components/cropper.scss +5 -5
  155. package/src/image-editor/react/components/cropper.tsx +9 -18
  156. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +3 -1
  157. package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
  158. package/src/image-editor/react/hooks/use-interaction.ts +3 -2
  159. package/src/state/composite-reducer.ts +29 -17
  160. package/src/state/index.ts +3 -1
  161. package/src/state/media-editor-state-provider.tsx +9 -22
  162. package/src/state/types.ts +37 -5
  163. package/src/state/use-media-editor-state.ts +124 -78
  164. package/src/store/actions.ts +9 -0
  165. package/src/store/reducer.ts +6 -1
  166. package/src/store/selectors.ts +4 -0
  167. package/global.d.ts +0 -5
  168. package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -104
  169. package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -74
  170. package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -180
  171. package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -359
  172. package/src/components/media-editor-modal/test/index.jsdom.test.tsx +0 -165
  173. package/src/components/rotation-ruler/stories/index.story.tsx +0 -43
  174. package/src/components/rotation-ruler/test/index.jsdom.test.tsx +0 -140
  175. package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +0 -823
  176. package/src/image-editor/core/math/test/sanitize.ts +0 -242
  177. package/src/image-editor/core/test/camera.ts +0 -1218
  178. package/src/image-editor/core/test/interaction-controller.ts +0 -1254
  179. package/src/image-editor/core/test/preview-export-parity.ts +0 -249
  180. package/src/image-editor/core/test/render-camera-bridge.ts +0 -349
  181. package/src/image-editor/core/test/state.ts +0 -876
  182. package/src/image-editor/core/test/stencil-math.ts +0 -665
  183. package/src/image-editor/core/transforms/test/pipeline.ts +0 -267
  184. package/src/image-editor/react/components/stencils/test/rectangle-stencil.browser.test.tsx +0 -438
  185. package/src/image-editor/react/components/test/cropper.browser.test.tsx +0 -1460
  186. package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +0 -491
  187. package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +0 -1049
  188. package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +0 -213
  189. package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +0 -140
  190. package/src/image-editor/stories/rectangle-crop.story.tsx +0 -1073
  191. package/src/image-editor/stories/style.module.css +0 -65
  192. package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -324
  193. package/src/store/test/reducer.ts +0 -64
  194. package/tsconfig.build.json +0 -26
  195. package/tsconfig.build.tsbuildinfo +0 -1
  196. 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 composite controller exposed by `useMediaEditorState`.
86
+ * The media editor session exposed by `useMediaEditorState`.
85
87
  *
86
- * Satisfies `CropperController` (so a `<Cropper>` can take this
87
- * controller directly via the `controller` prop) and adds slice
88
- * setters, undo/redo, gesture boundaries, and viewport reporting.
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 MediaEditorController extends CropperController {
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
- * Whether the cropper geometry differs from the initial baseline.
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
- isCropperDirty: boolean;
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
- * Composite store for the media editor.
156
+ * The media editor session.
138
157
  *
139
158
  * Owns the only undo/redo history for the editor. Cropper geometry
140
- * actions and sidebar cropOptions actions flow through the same
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 `isCropperDirty` when deciding whether the image output
174
+ * state. Use `hasOutputEdits` when deciding whether the image output
156
175
  * needs saving.
157
176
  *
158
- * @param initialState Optional seed for the cropper and cropOptions slices.
159
- * @return The composite controller.
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`. `setImage` refreshes this when
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`, `setImage`, `undo`, `redo`) writes this
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 setImage = useCallback( ( image: CropperState[ 'image' ] ) => {
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 canvas: clear history, refresh the
289
- // initial snapshot so isDirty starts at false.
290
- const action = {
291
- type: 'CROPPER' as const,
292
- action: { type: 'SET_IMAGE' as const, payload: image },
293
- };
294
- const next = mediaEditorReducer( stateRef.current, action );
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( action );
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 reset = useCallback(
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
- const resetCropOptions = useCallback( () => {
339
- dispatchWithHistory( { type: 'RESET_CROP_OPTIONS' } );
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 setImage first.' )
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 controller = useMemo< MediaEditorController >(
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
- // Composite extensions
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
- resetCropOptions,
433
- isCropperDirty,
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
- cropperSetters,
445
- state.cropper,
446
- setImage,
447
- reset,
448
- isDirty,
449
- getCroppedImage,
493
+ cropper,
450
494
  state.cropOptions,
451
495
  setAspectRatioValue,
452
- resetCropOptions,
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 controller;
510
+ return session;
465
511
  }
@@ -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
  }
@@ -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
  }
@@ -12,6 +12,10 @@ export function getOnUpdate( state: State ) {
12
12
  return state.onUpdate;
13
13
  }
14
14
 
15
+ export function getOnUndo( state: State ) {
16
+ return state.onUndo;
17
+ }
18
+
15
19
  export function getOnClose( state: State ) {
16
20
  return state.onClose;
17
21
  }
package/global.d.ts DELETED
@@ -1,5 +0,0 @@
1
- /*
2
- * With typeRoots set in tsconfig, global types outside those directories,
3
- * like the jest-dom matchers, must be referenced explicitly.
4
- */
5
- import '@testing-library/jest-dom/vitest';
@@ -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
- } );