@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
@@ -8,7 +8,7 @@
8
8
  | React cropper | `src/image-editor/react/` | DOM measurement, interaction hooks, overlays, stencil rendering, and the pure `useCropperReducer` hook. |
9
9
  | Media editor state | `src/state/` | Composite state for the media editor: cropper state, crop options, undo/redo, and gesture boundaries. |
10
10
 
11
- The cropper layer does not own editor history. History belongs to the composite media editor controller because sidebar controls and cropper gestures need one shared undo stack.
11
+ The cropper layer does not own editor history. History belongs to the media editor session because sidebar controls and cropper gestures need one shared undo stack.
12
12
 
13
13
  ## Coordinate Spaces
14
14
 
@@ -37,6 +37,14 @@ The render path uses focused calculations for CSS transforms, stencil placement,
37
37
 
38
38
  The core reducer is framework-agnostic and serializable. React and media-editor layers add DOM measurement, gestures, undo/redo, save behavior, and UI state.
39
39
 
40
+ ### Session
41
+
42
+ `useMediaEditorState()` returns the media editor session. The session owns the source image, every editing slice, the shared undo/redo history, gesture boundaries, and whether the edits change the saved image (`hasOutputEdits`). The cropper is one participant: `session.cropper` is a geometry-only `CropperController` that a `<Cropper>` takes via its `controller` prop, and its `isDirty` covers geometry alone. Replacing the source image (`setSourceImage`, which the cropper's `setImage` also calls) starts a fresh session with default geometry and crop options, a clean baseline, and no history. Save code asks the session, not the cropper, whether image output needs saving.
43
+
44
+ The session also holds named edits other than crop: `session.edits`, set with `session.setEdit( name, value )` and removed by passing `undefined`. They share the crop history, so undo, redo and gestures treat them like any crop change, and a different source image clears them. Values are plain data (`EditValue`: strings, numbers, booleans, `null`, and arrays and objects of those), because the session compares them by content to tell a real change from a no-op. Edits do not yet count towards `hasOutputEdits`; nothing saves them.
45
+
46
+ `session.reset()` puts every slice back to its starting state in one undo entry: default geometry and crop options, no edits, same image. The Reset button calls it; it does not compose per-slice resets itself.
47
+
40
48
  ### Viewport
41
49
 
42
50
  Viewport pan/zoom lets the user inspect or follow the crop area without changing the crop output. It is not part of export and should not create undo entries.
@@ -52,4 +60,4 @@ These APIs let code inside `@wordpress/media-editor` compose the cropper. They a
52
60
  | Crop data for REST requests | `getSourceRegion()` and `getSourceRegionPercent()`. |
53
61
  | Canvas export | `exportCroppedImage()` or `applyToCanvas()`. |
54
62
  | Shared cropper state across internal components | `CropperProvider` / `useCropper()`. |
55
- | Media editor history | `useMediaEditorState()` composite controller. |
63
+ | Media editor history | `useMediaEditorState()` session. |
@@ -76,29 +76,29 @@ function CropperPanel( { src }: { src: string } ) {
76
76
  }
77
77
  ```
78
78
 
79
- ## Use the Media Editor Composite Controller
79
+ ## Use the Media Editor Session
80
80
 
81
- Use the media editor controller when cropper state must share history with controls such as aspect ratio.
81
+ Use the media editor session when cropper state must share history with controls such as aspect ratio. The session owns history and gesture boundaries; pass its `cropper` controller to `<Cropper>`.
82
82
 
83
83
  ```tsx
84
84
  import { resolveAspectRatio, useMediaEditor } from '../../state';
85
85
  import { Cropper } from '../image-editor';
86
86
 
87
87
  function MediaEditorCanvas( { src }: { src: string } ) {
88
- const controller = useMediaEditor();
88
+ const session = useMediaEditor();
89
89
  const aspectRatio = resolveAspectRatio(
90
- controller.cropOptions.aspectRatioValue,
91
- controller.state.image
90
+ session.cropOptions.aspectRatioValue,
91
+ session.cropper.state.image
92
92
  );
93
93
 
94
94
  return (
95
95
  <Cropper
96
96
  src={ src }
97
- controller={ controller }
97
+ controller={ session.cropper }
98
98
  aspectRatio={ aspectRatio }
99
99
  freeformCrop
100
- onGestureStart={ controller.beginGesture }
101
- onGestureEnd={ controller.endGesture }
100
+ onGestureStart={ session.beginGesture }
101
+ onGestureEnd={ session.endGesture }
102
102
  />
103
103
  );
104
104
  }
@@ -149,8 +149,8 @@ $handle-touch-target-size: 44px;
149
149
  // on the canvas ensures the outline appears only when the canvas
150
150
  // itself is focused, not when focus is on a child handle.
151
151
  &__canvas--focus-visible:focus &__stencil-rect {
152
- border-color: var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #007cba));
153
- box-shadow: 0 0 0 1px var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #007cba));
152
+ border-color: var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #3858e9));
153
+ box-shadow: 0 0 0 1px var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #3858e9));
154
154
  }
155
155
 
156
156
  &__handle {
@@ -169,7 +169,7 @@ $handle-touch-target-size: 44px;
169
169
  // Solid white fill with an accent-blue border so the handles read
170
170
  // clearly against the light grey wash.
171
171
  background: $white;
172
- border: 1px solid var(--wp-admin-theme-color, #007cba);
172
+ border: 1px solid var(--wp-admin-theme-color, #3858e9);
173
173
  border-radius: 50%;
174
174
  box-sizing: border-box;
175
175
  pointer-events: auto;
@@ -185,9 +185,9 @@ $handle-touch-target-size: 44px;
185
185
  // keyboard session — e.g., clicking a handle to start a drag —
186
186
  // don't leave the active ring stuck on the handle.
187
187
  .wp-media-editor-image-editor__canvas--focus-visible &:focus {
188
- background: var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #007cba));
188
+ background: var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #3858e9));
189
189
  border-color: transparent;
190
- box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #007cba));
190
+ box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #3858e9));
191
191
  }
192
192
 
193
193
  // Extend touch target to the minimum accessible hit area.
@@ -571,14 +571,17 @@ function CropperInner(
571
571
  return;
572
572
  }
573
573
 
574
- setCropRect( snappedCropRect );
574
+ // The display scale crossing the threshold is not an edit, so the
575
+ // snap must not record an undo entry. Recording it here would also
576
+ // clear the redo stack whenever an undo or redo crosses it.
577
+ adjustCropRectForViewport( snappedCropRect );
575
578
  }, [
579
+ adjustCropRectForViewport,
576
580
  aspectRatio,
577
581
  displayScale,
578
582
  freeformCrop,
579
583
  naturalWidth,
580
584
  naturalHeight,
581
- setCropRect,
582
585
  state,
583
586
  ] );
584
587
 
@@ -760,7 +763,9 @@ function CropperInner(
760
763
 
761
764
  // Settling animation: brief transition after resize end.
762
765
  const [ settling, setSettling ] = useState( false );
763
- const settleTimerRef = useRef< ReturnType< typeof setTimeout > >();
766
+ const settleTimerRef = useRef<
767
+ ReturnType< typeof setTimeout > | undefined
768
+ >( undefined );
764
769
  const finishSettling = useCallback( () => {
765
770
  clearTimeout( settleTimerRef.current );
766
771
  isSettlingRef.current = false;
@@ -962,23 +967,9 @@ function CropperInner(
962
967
  };
963
968
  }
964
969
 
965
- // Forward the root element to the consumer's ref.
966
- const setContainerRef = useCallback(
967
- ( element: HTMLDivElement | null ) => {
968
- if ( typeof ref === 'function' ) {
969
- ref( element );
970
- } else if ( ref ) {
971
- (
972
- ref as React.MutableRefObject< HTMLDivElement | null >
973
- ).current = element;
974
- }
975
- },
976
- [ ref ]
977
- );
978
-
979
970
  return (
980
971
  <div
981
- ref={ setContainerRef }
972
+ ref={ ref }
982
973
  className={ clsx(
983
974
  'wp-media-editor-image-editor',
984
975
  isDragging && 'wp-media-editor-image-editor--dragging',
@@ -134,7 +134,9 @@ export function RectangleStencil( {
134
134
  } ),
135
135
  [ boundsMinX, boundsMinY, boundsMaxX, boundsMaxY ]
136
136
  );
137
- const keyboardSettleTimerRef = useRef< ReturnType< typeof setTimeout > >();
137
+ const keyboardSettleTimerRef = useRef<
138
+ ReturnType< typeof setTimeout > | undefined
139
+ >( undefined );
138
140
  const keyboardResizeActiveRef = useRef( false );
139
141
  const resizeHandleDescriptionId = useId();
140
142
  const hasLockedRatio = !! ( aspectRatio && aspectRatio > 0 );
@@ -157,7 +157,9 @@ function buildAnnouncement(
157
157
  * @param state The current cropper state to announce.
158
158
  */
159
159
  export function useAriaAnnouncer( state: CropperState ): void {
160
- const timerRef = useRef< ReturnType< typeof setTimeout > >();
160
+ const timerRef = useRef< ReturnType< typeof setTimeout > | undefined >(
161
+ undefined
162
+ );
161
163
  const prevMessageRef = useRef( '' );
162
164
  const prevStateRef = useRef< CropperState | null >( null );
163
165
  const latestStateRef = useRef( state );
@@ -106,8 +106,9 @@ export function useInteraction(
106
106
  const [ isZooming, setIsZooming ] = useState( false );
107
107
  const [ isGestureActive, setIsGestureActive ] = useState( false );
108
108
  const [ isKeyboardPanning, setIsKeyboardPanning ] = useState( false );
109
- const keyboardInteractionTimerRef =
110
- useRef< ReturnType< typeof setTimeout > >();
109
+ const keyboardInteractionTimerRef = useRef<
110
+ ReturnType< typeof setTimeout > | undefined
111
+ >( undefined );
111
112
  // Tracks whether a keyboard gesture (pan or zoom) is currently active so
112
113
  // onGestureStart is only fired once per gesture, not on every key repeat.
113
114
  const isKeyboardGestureActiveRef = useRef( false );
@@ -1,3 +1,4 @@
1
+ import fastDeepEqual from 'fast-deep-equal/es6/index.js';
1
2
  import {
2
3
  cropperReducer,
3
4
  areCropperStatesEqual,
@@ -64,21 +65,31 @@ export function mediaEditorReducer(
64
65
  payload: newCropRect,
65
66
  } );
66
67
  return {
68
+ ...state,
67
69
  cropper: nextCropper,
68
70
  cropOptions: nextCropOptions,
69
71
  };
70
72
  }
71
- case 'RESET_CROP_OPTIONS': {
72
- if (
73
- state.cropOptions.aspectRatioValue ===
74
- DEFAULT_CROP_OPTIONS.aspectRatioValue
75
- ) {
73
+ case 'RESET': {
74
+ const next = buildInitialMediaEditorState(
75
+ cropperReducer( state.cropper, { type: 'RESET' } )
76
+ );
77
+ return areMediaEditorStatesEqual( state, next ) ? state : next;
78
+ }
79
+ case 'SET_EDIT': {
80
+ const { name, value } = action.payload;
81
+ if ( value === undefined ) {
82
+ if ( ! Object.hasOwn( state.edits, name ) ) {
83
+ return state;
84
+ }
85
+ const edits = { ...state.edits };
86
+ delete edits[ name ];
87
+ return { ...state, edits };
88
+ }
89
+ if ( fastDeepEqual( state.edits[ name ], value ) ) {
76
90
  return state;
77
91
  }
78
- return {
79
- ...state,
80
- cropOptions: { ...DEFAULT_CROP_OPTIONS },
81
- };
92
+ return { ...state, edits: { ...state.edits, [ name ]: value } };
82
93
  }
83
94
  case 'RESTORE_SNAPSHOT':
84
95
  return action.payload;
@@ -112,23 +123,24 @@ export function areMediaEditorStatesEqual(
112
123
  }
113
124
  return (
114
125
  areCropperStatesEqual( a.cropper, b.cropper ) &&
115
- a.cropOptions.aspectRatioValue === b.cropOptions.aspectRatioValue
126
+ a.cropOptions.aspectRatioValue === b.cropOptions.aspectRatioValue &&
127
+ fastDeepEqual( a.edits, b.edits )
116
128
  );
117
129
  }
118
130
 
119
131
  /**
120
- * Build the initial composite state from optional partial overrides.
132
+ * Build the composite state a session starts from: the given cropper
133
+ * slice, default crop options, and no edits.
121
134
  *
122
- * @param initialCropper Initial cropper-slice fields to merge.
123
- * @param initialOptions Initial cropOptions-slice fields to merge.
135
+ * @param cropper The cropper slice.
124
136
  * @return Composite initial state.
125
137
  */
126
138
  export function buildInitialMediaEditorState(
127
- initialCropper: CropperState,
128
- initialOptions?: Partial< CropOptionsSlice >
139
+ cropper: CropperState
129
140
  ): MediaEditorState {
130
141
  return {
131
- cropper: initialCropper,
132
- cropOptions: { ...DEFAULT_CROP_OPTIONS, ...initialOptions },
142
+ cropper,
143
+ cropOptions: { ...DEFAULT_CROP_OPTIONS },
144
+ edits: {},
133
145
  };
134
146
  }
@@ -2,7 +2,7 @@ export {
2
2
  useMediaEditorState,
3
3
  resolveAspectRatio,
4
4
  } from './use-media-editor-state';
5
- export type { MediaEditorController } from './use-media-editor-state';
5
+ export type { MediaEditorSession } from './use-media-editor-state';
6
6
  export {
7
7
  MediaEditorStateProvider,
8
8
  useMediaEditor,
@@ -15,6 +15,8 @@ export {
15
15
  export { DEFAULT_CROP_OPTIONS } from './types';
16
16
  export type {
17
17
  CropOptionsSlice,
18
+ EditsSlice,
19
+ EditValue,
18
20
  MediaEditorState,
19
21
  MediaEditorAction,
20
22
  } from './types';
@@ -1,43 +1,30 @@
1
1
  import { createContext, useContext } from '@wordpress/element';
2
- import type { CropperState } from '../image-editor';
3
2
  import {
4
3
  useMediaEditorState,
5
- type MediaEditorController,
4
+ type MediaEditorSession,
6
5
  } from './use-media-editor-state';
7
- import type { CropOptionsSlice } from './types';
8
6
 
9
- const MediaEditorStateContext = createContext< MediaEditorController | null >(
7
+ const MediaEditorStateContext = createContext< MediaEditorSession | null >(
10
8
  null
11
9
  );
12
10
 
13
11
  interface MediaEditorStateProviderProps {
14
- /** Optional cropper-slice initial state. */
15
- initialCropperState?: Partial< CropperState >;
16
- /** Optional cropOptions-slice initial state. */
17
- initialCropOptions?: Partial< CropOptionsSlice >;
18
12
  /** Child components. */
19
13
  children: React.ReactNode;
20
14
  }
21
15
 
22
16
  /**
23
- * Provider that vends the composite media-editor controller via
24
- * context. Wrap the media editor in this so every consumer
25
- * (canvas, toolbar, sidebar) reads from the same store.
17
+ * Provider that vends the media editor session via context. Wrap
18
+ * the media editor in this so every consumer (canvas, toolbar,
19
+ * sidebar) reads from the same store.
26
20
  *
27
21
  * @param props
28
- * @param props.initialCropperState
29
- * @param props.initialCropOptions
30
22
  * @param props.children
31
23
  */
32
24
  export function MediaEditorStateProvider( {
33
- initialCropperState,
34
- initialCropOptions,
35
25
  children,
36
26
  }: MediaEditorStateProviderProps ) {
37
- const controller = useMediaEditorState( {
38
- cropper: initialCropperState,
39
- cropOptions: initialCropOptions,
40
- } );
27
+ const controller = useMediaEditorState();
41
28
 
42
29
  return (
43
30
  <MediaEditorStateContext.Provider value={ controller }>
@@ -47,12 +34,12 @@ export function MediaEditorStateProvider( {
47
34
  }
48
35
 
49
36
  /**
50
- * Consume the composite media-editor controller. Throws if used
37
+ * Consume the media editor session. Throws if used
51
38
  * outside a `<MediaEditorStateProvider>`.
52
39
  *
53
- * @return The composite controller.
40
+ * @return The media editor session.
54
41
  */
55
- export function useMediaEditor(): MediaEditorController {
42
+ export function useMediaEditor(): MediaEditorSession {
56
43
  const context = useContext( MediaEditorStateContext );
57
44
  if ( ! context ) {
58
45
  throw new Error(
@@ -27,9 +27,31 @@ export const DEFAULT_CROP_OPTIONS: CropOptionsSlice = {
27
27
  };
28
28
 
29
29
  /**
30
- * The full composite state for the media editor — cropper geometry
31
- * plus the sidebar control slice. Future feature slices (text
32
- * overlays, filters, …) extend this object.
30
+ * A value an edit can hold: plain data only, because the session
31
+ * compares values by content to tell a real change from a no-op.
32
+ * Anything else, such as a `Blob`, would compare equal to any other
33
+ * and the change would be dropped — keep bytes behind a URL instead.
34
+ *
35
+ * Objects must satisfy the index signature, so type a value with a
36
+ * `type` alias rather than an `interface`.
37
+ */
38
+ export type EditValue =
39
+ | string
40
+ | number
41
+ | boolean
42
+ | null
43
+ | EditValue[]
44
+ | { [ key: string ]: EditValue };
45
+
46
+ /**
47
+ * Named edits other than crop, keyed by edit name (for example
48
+ * `my-plugin/vignette`). An edit that is not present means "no edit".
49
+ */
50
+ export type EditsSlice = Record< string, EditValue >;
51
+
52
+ /**
53
+ * The full composite state for the media editor — cropper geometry,
54
+ * the sidebar control slice, and named edits.
33
55
  *
34
56
  * Every undo/redo snapshot is a full `MediaEditorState`. Slices stay
35
57
  * referentially equal across actions that don't touch them.
@@ -37,6 +59,7 @@ export const DEFAULT_CROP_OPTIONS: CropOptionsSlice = {
37
59
  export interface MediaEditorState {
38
60
  cropper: CropperState;
39
61
  cropOptions: CropOptionsSlice;
62
+ edits: EditsSlice;
40
63
  }
41
64
 
42
65
  /**
@@ -79,8 +102,17 @@ export type MediaEditorAction =
79
102
  visualSize: Size;
80
103
  };
81
104
  }
82
- /** Reset cropOptions to defaults. */
83
- | { type: 'RESET_CROP_OPTIONS' }
105
+ /**
106
+ * Put every slice back to its starting state, keeping the image:
107
+ * default geometry, default crop options, no edits. One action,
108
+ * so one undo entry.
109
+ */
110
+ | { type: 'RESET' }
111
+ /** Set a named edit, or remove it when `value` is `undefined`. */
112
+ | {
113
+ type: 'SET_EDIT';
114
+ payload: { name: string; value: EditValue | undefined };
115
+ }
84
116
  /**
85
117
  * Replace the entire composite state with `payload`. Used by
86
118
  * undo/redo to restore a full snapshot atomically.