@wordpress/media-editor 0.8.0 → 0.8.1-next.v.202605131006.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 (217) hide show
  1. package/build/components/media-editor/index.cjs +560 -0
  2. package/build/components/media-editor/index.cjs.map +7 -0
  3. package/build/components/media-editor-canvas/index.cjs +14 -2
  4. package/build/components/media-editor-canvas/index.cjs.map +2 -2
  5. package/build/components/media-editor-crop-panel/index.cjs +34 -27
  6. package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
  7. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +162 -0
  8. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +7 -0
  9. package/build/components/media-editor-modal/index.cjs +42 -430
  10. package/build/components/media-editor-modal/index.cjs.map +3 -3
  11. package/build/components/media-editor-toolbar/index.cjs +143 -75
  12. package/build/components/media-editor-toolbar/index.cjs.map +3 -3
  13. package/build/components/media-form/index.cjs +3 -0
  14. package/build/components/media-form/index.cjs.map +2 -2
  15. package/build/components/rotation-ruler/index.cjs +197 -0
  16. package/build/components/rotation-ruler/index.cjs.map +7 -0
  17. package/build/components/rotation-ruler/use-ruler-drag.cjs +154 -0
  18. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +7 -0
  19. package/build/hooks/use-crop-gesture-handlers.cjs +43 -0
  20. package/build/hooks/use-crop-gesture-handlers.cjs.map +7 -0
  21. package/build/image-editor/core/constants.cjs +9 -0
  22. package/build/image-editor/core/constants.cjs.map +2 -2
  23. package/build/image-editor/core/containment.cjs +7 -13
  24. package/build/image-editor/core/containment.cjs.map +2 -2
  25. package/build/image-editor/core/interaction-controller.cjs +43 -7
  26. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  27. package/build/image-editor/core/state.cjs +5 -1
  28. package/build/image-editor/core/state.cjs.map +2 -2
  29. package/build/image-editor/core/transform-style.cjs.map +1 -1
  30. package/build/image-editor/core/types.cjs.map +1 -1
  31. package/build/image-editor/core/viewport-state.cjs +50 -0
  32. package/build/image-editor/core/viewport-state.cjs.map +7 -0
  33. package/build/image-editor/react/components/cropper.cjs +192 -72
  34. package/build/image-editor/react/components/cropper.cjs.map +3 -3
  35. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +5 -2
  36. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
  37. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +16 -4
  38. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  39. package/build/image-editor/react/components/viewport-provider.cjs +51 -0
  40. package/build/image-editor/react/components/viewport-provider.cjs.map +7 -0
  41. package/build/image-editor/react/hooks/use-aria-announcer.cjs +53 -6
  42. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
  43. package/build/image-editor/react/hooks/use-cropper-state.cjs +127 -8
  44. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +2 -2
  45. package/build/image-editor/react/hooks/use-interaction.cjs +26 -5
  46. package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
  47. package/build/image-editor/react/hooks/use-viewport-state.cjs +51 -0
  48. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +7 -0
  49. package/build/image-editor/react/visually-hidden-style.cjs +41 -0
  50. package/build/image-editor/react/visually-hidden-style.cjs.map +7 -0
  51. package/build/private-apis.cjs +2 -0
  52. package/build/private-apis.cjs.map +2 -2
  53. package/build-module/components/media-editor/index.mjs +545 -0
  54. package/build-module/components/media-editor/index.mjs.map +7 -0
  55. package/build-module/components/media-editor-canvas/index.mjs +14 -2
  56. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  57. package/build-module/components/media-editor-crop-panel/index.mjs +35 -28
  58. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  59. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +144 -0
  60. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +7 -0
  61. package/build-module/components/media-editor-modal/index.mjs +46 -455
  62. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  63. package/build-module/components/media-editor-toolbar/index.mjs +137 -77
  64. package/build-module/components/media-editor-toolbar/index.mjs.map +2 -2
  65. package/build-module/components/media-form/index.mjs +3 -0
  66. package/build-module/components/media-form/index.mjs.map +2 -2
  67. package/build-module/components/rotation-ruler/index.mjs +166 -0
  68. package/build-module/components/rotation-ruler/index.mjs.map +7 -0
  69. package/build-module/components/rotation-ruler/use-ruler-drag.mjs +126 -0
  70. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +7 -0
  71. package/build-module/hooks/use-crop-gesture-handlers.mjs +17 -0
  72. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +7 -0
  73. package/build-module/image-editor/core/constants.mjs +6 -0
  74. package/build-module/image-editor/core/constants.mjs.map +2 -2
  75. package/build-module/image-editor/core/containment.mjs +6 -12
  76. package/build-module/image-editor/core/containment.mjs.map +2 -2
  77. package/build-module/image-editor/core/interaction-controller.mjs +50 -8
  78. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  79. package/build-module/image-editor/core/state.mjs +5 -1
  80. package/build-module/image-editor/core/state.mjs.map +2 -2
  81. package/build-module/image-editor/core/transform-style.mjs.map +1 -1
  82. package/build-module/image-editor/core/viewport-state.mjs +24 -0
  83. package/build-module/image-editor/core/viewport-state.mjs.map +7 -0
  84. package/build-module/image-editor/react/components/cropper.mjs +195 -73
  85. package/build-module/image-editor/react/components/cropper.mjs.map +3 -3
  86. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +5 -2
  87. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
  88. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +26 -5
  89. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  90. package/build-module/image-editor/react/components/viewport-provider.mjs +27 -0
  91. package/build-module/image-editor/react/components/viewport-provider.mjs.map +7 -0
  92. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +59 -7
  93. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
  94. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +134 -9
  95. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +2 -2
  96. package/build-module/image-editor/react/hooks/use-interaction.mjs +26 -5
  97. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
  98. package/build-module/image-editor/react/hooks/use-viewport-state.mjs +29 -0
  99. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +7 -0
  100. package/build-module/image-editor/react/visually-hidden-style.mjs +16 -0
  101. package/build-module/image-editor/react/visually-hidden-style.mjs.map +7 -0
  102. package/build-module/private-apis.mjs +2 -0
  103. package/build-module/private-apis.mjs.map +2 -2
  104. package/build-style/style-rtl.css +254 -29
  105. package/build-style/style.css +254 -29
  106. package/build-types/components/media-editor/index.d.ts +34 -0
  107. package/build-types/components/media-editor/index.d.ts.map +1 -0
  108. package/build-types/components/media-editor-canvas/index.d.ts +10 -1
  109. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  110. package/build-types/components/media-editor-crop-panel/index.d.ts +1 -1
  111. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  112. package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts +6 -0
  113. package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -0
  114. package/build-types/components/media-editor-modal/index.d.ts +2 -2
  115. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  116. package/build-types/components/media-editor-provider/index.d.ts +1 -1
  117. package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
  118. package/build-types/components/media-editor-toolbar/index.d.ts +4 -1
  119. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -1
  120. package/build-types/components/media-form/index.d.ts +1 -1
  121. package/build-types/components/media-form/index.d.ts.map +1 -1
  122. package/build-types/components/rotation-ruler/index.d.ts +46 -0
  123. package/build-types/components/rotation-ruler/index.d.ts.map +1 -0
  124. package/build-types/components/rotation-ruler/stories/index.story.d.ts +14 -0
  125. package/build-types/components/rotation-ruler/stories/index.story.d.ts.map +1 -0
  126. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts +61 -0
  127. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -0
  128. package/build-types/hooks/use-crop-gesture-handlers.d.ts +42 -0
  129. package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -0
  130. package/build-types/image-editor/core/constants.d.ts +9 -0
  131. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  132. package/build-types/image-editor/core/containment.d.ts +11 -7
  133. package/build-types/image-editor/core/containment.d.ts.map +1 -1
  134. package/build-types/image-editor/core/interaction-controller.d.ts +14 -2
  135. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  136. package/build-types/image-editor/core/state.d.ts.map +1 -1
  137. package/build-types/image-editor/core/transform-style.d.ts +1 -1
  138. package/build-types/image-editor/core/types.d.ts +25 -0
  139. package/build-types/image-editor/core/types.d.ts.map +1 -1
  140. package/build-types/image-editor/core/viewport-state.d.ts +7 -0
  141. package/build-types/image-editor/core/viewport-state.d.ts.map +1 -0
  142. package/build-types/image-editor/react/components/cropper-provider.d.ts +1 -1
  143. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
  144. package/build-types/image-editor/react/components/cropper.d.ts +2 -1
  145. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  146. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +3 -13
  147. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
  148. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +1 -1
  149. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -1
  150. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +1 -1
  151. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  152. package/build-types/image-editor/react/components/viewport-provider.d.ts +9 -0
  153. package/build-types/image-editor/react/components/viewport-provider.d.ts.map +1 -0
  154. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
  155. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +29 -0
  156. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -1
  157. package/build-types/image-editor/react/hooks/use-interaction.d.ts +5 -2
  158. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
  159. package/build-types/image-editor/react/hooks/use-viewport-state.d.ts +19 -0
  160. package/build-types/image-editor/react/hooks/use-viewport-state.d.ts.map +1 -0
  161. package/build-types/image-editor/react/visually-hidden-style.d.ts +6 -0
  162. package/build-types/image-editor/react/visually-hidden-style.d.ts.map +1 -0
  163. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
  164. package/build-types/private-apis.d.ts.map +1 -1
  165. package/build-types/store/actions.d.ts +3 -3
  166. package/build-types/store/actions.d.ts.map +1 -1
  167. package/package.json +16 -15
  168. package/src/components/media-editor/index.tsx +709 -0
  169. package/src/components/media-editor/style.scss +89 -0
  170. package/src/components/media-editor-canvas/index.tsx +24 -0
  171. package/src/components/media-editor-crop-panel/index.tsx +39 -27
  172. package/src/components/media-editor-crop-panel/test/index.tsx +109 -0
  173. package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +177 -0
  174. package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +53 -0
  175. package/src/components/media-editor-modal/index.tsx +49 -589
  176. package/src/components/media-editor-modal/style.scss +13 -112
  177. package/src/components/media-editor-toolbar/index.tsx +122 -63
  178. package/src/components/media-editor-toolbar/style.scss +39 -11
  179. package/src/components/media-form/index.tsx +5 -0
  180. package/src/components/rotation-ruler/README.md +37 -0
  181. package/src/components/rotation-ruler/index.tsx +266 -0
  182. package/src/components/rotation-ruler/stories/index.story.tsx +54 -0
  183. package/src/components/rotation-ruler/style.scss +147 -0
  184. package/src/components/rotation-ruler/test/index.tsx +109 -0
  185. package/src/components/rotation-ruler/use-ruler-drag.ts +230 -0
  186. package/src/hooks/use-crop-gesture-handlers.ts +54 -0
  187. package/src/image-editor/core/constants.ts +12 -0
  188. package/src/image-editor/core/containment.ts +16 -28
  189. package/src/image-editor/core/interaction-controller.ts +66 -10
  190. package/src/image-editor/core/state.ts +15 -8
  191. package/src/image-editor/core/test/camera.ts +15 -26
  192. package/src/image-editor/core/test/interaction-controller.ts +158 -7
  193. package/src/image-editor/core/test/state.ts +21 -1
  194. package/src/image-editor/core/transform-style.ts +1 -1
  195. package/src/image-editor/core/types.ts +16 -0
  196. package/src/image-editor/core/viewport-state.ts +33 -0
  197. package/src/image-editor/docs/architecture.md +1 -1
  198. package/src/image-editor/docs/recipes.md +2 -2
  199. package/src/image-editor/react/components/cropper.scss +28 -0
  200. package/src/image-editor/react/components/cropper.tsx +268 -72
  201. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +12 -12
  202. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +27 -8
  203. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +29 -3
  204. package/src/image-editor/react/components/test/cropper.tsx +294 -5
  205. package/src/image-editor/react/components/viewport-provider.tsx +39 -0
  206. package/src/image-editor/react/hooks/test/use-aria-announcer.ts +81 -0
  207. package/src/image-editor/react/hooks/test/use-cropper-state.ts +286 -0
  208. package/src/image-editor/react/hooks/test/use-interaction.ts +1 -0
  209. package/src/image-editor/react/hooks/use-aria-announcer.ts +68 -7
  210. package/src/image-editor/react/hooks/use-cropper-state.ts +230 -11
  211. package/src/image-editor/react/hooks/use-interaction.ts +36 -6
  212. package/src/image-editor/react/hooks/use-viewport-state.ts +48 -0
  213. package/src/image-editor/react/visually-hidden-style.ts +15 -0
  214. package/src/private-apis.ts +2 -0
  215. package/src/style.scss +3 -0
  216. package/tsconfig.json +1 -0
  217. package/tsconfig.tsbuildinfo +1 -1
@@ -1,7 +1,13 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { useReducer, useCallback, useRef } from '@wordpress/element';
4
+ import {
5
+ useReducer,
6
+ useCallback,
7
+ useRef,
8
+ useState,
9
+ useEffect,
10
+ } from '@wordpress/element';
5
11
 
6
12
  /**
7
13
  * Internal dependencies
@@ -48,6 +54,8 @@ export interface UseCropperStateReturn {
48
54
  setRotation: ( rotation: number ) => void;
49
55
  /** Set the flip state. */
50
56
  setFlip: ( flip: Flip ) => void;
57
+ /** Toggle flip on the given axis. */
58
+ toggleFlip: ( direction: 'horizontal' | 'vertical' ) => void;
51
59
  /** Snap rotate 90° preserving the image selection (Google Photos style). */
52
60
  snapRotate90: ( direction: 1 | -1 ) => void;
53
61
  /** Set the crop rectangle in normalized coordinates. */
@@ -56,6 +64,9 @@ export interface UseCropperStateReturn {
56
64
  * Settle the crop rect after a resize drag: expand to fill the
57
65
  * available visual area while preserving the framed image region.
58
66
  * Typically called from a stencil's `onResizeEnd` callback.
67
+ *
68
+ * Flushes the pending resize history before dispatching, then suppresses
69
+ * the settle dispatch itself so resizing and settling undo as one step.
59
70
  */
60
71
  settleCrop: () => void;
61
72
  /** Apply a transform operation through the pipeline. */
@@ -64,6 +75,25 @@ export interface UseCropperStateReturn {
64
75
  reset: ( resetState?: Partial< CropperState > ) => void;
65
76
  /** Whether the current state differs from the initial state. */
66
77
  isDirty: boolean;
78
+ /** Whether there is a cropper state to undo. */
79
+ hasUndo: boolean;
80
+ /** Whether there is a cropper state to redo. */
81
+ hasRedo: boolean;
82
+ /** Undo the last committed cropper operation. */
83
+ undo: () => void;
84
+ /** Redo the last undone cropper operation. */
85
+ redo: () => void;
86
+ /**
87
+ * Flush the pending history entry immediately, bypassing the debounce
88
+ * timer. Useful in two situations:
89
+ *
90
+ * - **Pointer-up / key-up**: commit as soon as the user releases so the
91
+ * undo button lights up without waiting for the debounce window.
92
+ * - **Gesture start**: pass to `onGestureStart` on the `Cropper` so any
93
+ * pending sidebar interaction is committed before a new canvas gesture
94
+ * begins, keeping the two interactions as separate undo steps.
95
+ */
96
+ commitHistory: () => void;
67
97
  /**
68
98
  * Export the cropped image as a Blob. Throws on failure — see
69
99
  * `exportCroppedImage` in core for the error semantics (image
@@ -73,6 +103,36 @@ export interface UseCropperStateReturn {
73
103
  getCroppedImage: ( mimeType?: string, quality?: number ) => Promise< Blob >;
74
104
  }
75
105
 
106
+ /** Milliseconds of inactivity after which a continuous interaction is committed to history. */
107
+ const HISTORY_DEBOUNCE_MS = 300;
108
+
109
+ /** Small tolerance for cropper floating-point comparisons. */
110
+ const HISTORY_EPSILON = 1e-6;
111
+
112
+ function nearlyEqual( a: number, b: number ): boolean {
113
+ return Math.abs( a - b ) < HISTORY_EPSILON;
114
+ }
115
+
116
+ function areHistoryStatesEqual( a: CropperState, b: CropperState ): boolean {
117
+ const aImage = a.image;
118
+ const bImage = b.image;
119
+ return (
120
+ aImage?.src === bImage?.src &&
121
+ aImage?.naturalWidth === bImage?.naturalWidth &&
122
+ aImage?.naturalHeight === bImage?.naturalHeight &&
123
+ nearlyEqual( a.pan.x, b.pan.x ) &&
124
+ nearlyEqual( a.pan.y, b.pan.y ) &&
125
+ nearlyEqual( a.zoom, b.zoom ) &&
126
+ nearlyEqual( a.rotation, b.rotation ) &&
127
+ a.flip.horizontal === b.flip.horizontal &&
128
+ a.flip.vertical === b.flip.vertical &&
129
+ nearlyEqual( a.cropRect.x, b.cropRect.x ) &&
130
+ nearlyEqual( a.cropRect.y, b.cropRect.y ) &&
131
+ nearlyEqual( a.cropRect.width, b.cropRect.width ) &&
132
+ nearlyEqual( a.cropRect.height, b.cropRect.height )
133
+ );
134
+ }
135
+
76
136
  /**
77
137
  * Reducer-based state management hook for the image editor.
78
138
  *
@@ -81,6 +141,11 @@ export interface UseCropperStateReturn {
81
141
  * live in core/state.ts (framework-agnostic); this hook is a thin
82
142
  * React wrapper around that pure reducer, with memoized callbacks.
83
143
  *
144
+ * History is recorded via a state-change debounce: any state change
145
+ * starts a 300 ms timer; when it fires the pre-change state is pushed
146
+ * onto the undo stack. `commitHistory` flushes the timer immediately
147
+ * (useful on pointer-up / key-up for a crisp commit feel).
148
+ *
84
149
  * @param initialState Optional partial state to merge with DEFAULT_STATE.
85
150
  * @return The cropper state, setters, and utilities.
86
151
  */
@@ -101,8 +166,127 @@ export function useCropperState(
101
166
  const stateRef = useRef( state );
102
167
  stateRef.current = state;
103
168
 
169
+ // History stack for undo/redo. Using refs for the arrays avoids
170
+ // unnecessary re-renders on every push; a pair of boolean state values
171
+ // drives the enabled/disabled state of the undo/redo buttons.
172
+ const historyRef = useRef< CropperState[] >( [] );
173
+ const redoStackRef = useRef< CropperState[] >( [] );
174
+ const [ hasUndo, setHasUndo ] = useState( false );
175
+ const [ hasRedo, setHasRedo ] = useState( false );
176
+
177
+ // Debounce-based history: tracks the last committed state and a pending
178
+ // timer. Any state change resets the timer; when it expires the
179
+ // pre-change state is pushed to history.
180
+ const lastCommittedStateRef = useRef< CropperState | null >( state );
181
+ const debounceTimerRef = useRef< ReturnType< typeof setTimeout > >();
182
+ // Set to true before dispatching actions that must not produce a debounce
183
+ // history entry (undo, redo, reset, setImage, discrete actions).
184
+ const suppressDebounceRef = useRef( false );
185
+
186
+ // Pushes `entry` (defaults to current state) onto the undo stack and
187
+ // clears the redo stack. Skips if `entry` is already the last item,
188
+ // so rapid discrete actions on unchanged state don't create duplicates.
189
+ const pushToHistory = useCallback( ( entry?: CropperState ) => {
190
+ const target = entry ?? stateRef.current;
191
+ const previousEntry =
192
+ historyRef.current[ historyRef.current.length - 1 ];
193
+ if ( previousEntry && areHistoryStatesEqual( previousEntry, target ) ) {
194
+ return;
195
+ }
196
+ historyRef.current = [ ...historyRef.current, target ];
197
+ redoStackRef.current = [];
198
+ setHasUndo( true );
199
+ setHasRedo( false );
200
+ }, [] );
201
+
202
+ // Watch state and debounce history commits. Any dispatch resets the
203
+ // timer; once the state has settled for HISTORY_DEBOUNCE_MS the
204
+ // pre-change snapshot is pushed to the undo stack.
205
+ useEffect( () => {
206
+ // Suppress flag: undo/redo/reset/setImage/discrete actions set this
207
+ // to opt out of the debounce for their own state changes.
208
+ if ( suppressDebounceRef.current ) {
209
+ suppressDebounceRef.current = false;
210
+ lastCommittedStateRef.current = stateRef.current;
211
+ return;
212
+ }
213
+ if (
214
+ lastCommittedStateRef.current !== null &&
215
+ areHistoryStatesEqual(
216
+ lastCommittedStateRef.current,
217
+ stateRef.current
218
+ )
219
+ ) {
220
+ lastCommittedStateRef.current = stateRef.current;
221
+ return;
222
+ }
223
+ clearTimeout( debounceTimerRef.current );
224
+ debounceTimerRef.current = setTimeout( () => {
225
+ const snapshot = lastCommittedStateRef.current;
226
+ if (
227
+ snapshot !== null &&
228
+ ! areHistoryStatesEqual( snapshot, stateRef.current )
229
+ ) {
230
+ pushToHistory( snapshot );
231
+ }
232
+ lastCommittedStateRef.current = stateRef.current;
233
+ }, HISTORY_DEBOUNCE_MS );
234
+ return () => clearTimeout( debounceTimerRef.current );
235
+ }, [ state, pushToHistory ] );
236
+
237
+ // Flush the pending debounce immediately: cancel the timer and push the
238
+ // pre-change snapshot if the state has actually changed. Used by
239
+ // pointer-up / key-up handlers for an instant commit feel, and by
240
+ // undo/redo/discrete actions before recording their own history entry.
241
+ const commitHistory = useCallback( () => {
242
+ clearTimeout( debounceTimerRef.current );
243
+ const snapshot = lastCommittedStateRef.current;
244
+ if (
245
+ snapshot !== null &&
246
+ ! areHistoryStatesEqual( snapshot, stateRef.current )
247
+ ) {
248
+ pushToHistory( snapshot );
249
+ }
250
+ lastCommittedStateRef.current = stateRef.current;
251
+ }, [ pushToHistory ] );
252
+
253
+ const undo = useCallback( () => {
254
+ // Flush any pending gesture so it becomes a distinct undo step
255
+ // before we pop history. This ensures mid-gesture undo undoes the
256
+ // pending change rather than silently discarding it.
257
+ commitHistory();
258
+ const prev = historyRef.current[ historyRef.current.length - 1 ];
259
+ if ( ! prev ) {
260
+ return;
261
+ }
262
+ redoStackRef.current = [ stateRef.current, ...redoStackRef.current ];
263
+ historyRef.current = historyRef.current.slice( 0, -1 );
264
+ suppressDebounceRef.current = true;
265
+ setHasUndo( historyRef.current.length > 0 );
266
+ setHasRedo( true );
267
+ dispatch( { type: 'RESET', payload: prev } );
268
+ }, [ dispatch, commitHistory ] );
269
+
270
+ const redo = useCallback( () => {
271
+ // Flush any pending gesture before redoing so the in-flight change
272
+ // is saved and the redo target lands on top of it.
273
+ commitHistory();
274
+ const next = redoStackRef.current[ 0 ];
275
+ if ( ! next ) {
276
+ return;
277
+ }
278
+ historyRef.current = [ ...historyRef.current, stateRef.current ];
279
+ redoStackRef.current = redoStackRef.current.slice( 1 );
280
+ suppressDebounceRef.current = true;
281
+ setHasUndo( true );
282
+ setHasRedo( redoStackRef.current.length > 0 );
283
+ dispatch( { type: 'RESET', payload: next } );
284
+ }, [ dispatch, commitHistory ] );
285
+
104
286
  const setImage = useCallback(
105
287
  ( image: CropperState[ 'image' ] ) => {
288
+ clearTimeout( debounceTimerRef.current );
289
+ suppressDebounceRef.current = true;
106
290
  dispatch( { type: 'SET_IMAGE', payload: image } );
107
291
  // Refresh the "clean" snapshot to match the post-load state
108
292
  // produced by the reducer. Otherwise containment can nudge
@@ -149,19 +333,35 @@ export function useCropperState(
149
333
 
150
334
  const setFlip = useCallback(
151
335
  ( flip: Flip ) => {
336
+ commitHistory(); // flush any pending continuous gesture first
337
+ pushToHistory(); // record current state as the undo point
338
+ suppressDebounceRef.current = true;
152
339
  dispatch( { type: 'SET_FLIP', payload: flip } );
153
340
  },
154
- [ dispatch ]
341
+ [ dispatch, pushToHistory, commitHistory ]
342
+ );
343
+
344
+ const toggleFlip = useCallback(
345
+ ( direction: 'horizontal' | 'vertical' ) => {
346
+ setFlip( {
347
+ ...stateRef.current.flip,
348
+ [ direction ]: ! stateRef.current.flip[ direction ],
349
+ } );
350
+ },
351
+ [ setFlip ]
155
352
  );
156
353
 
157
354
  const snapRotate90 = useCallback(
158
355
  ( direction: 1 | -1 ) => {
356
+ commitHistory();
357
+ pushToHistory();
358
+ suppressDebounceRef.current = true;
159
359
  dispatch( {
160
360
  type: 'SNAP_ROTATE_90',
161
361
  payload: { direction },
162
362
  } );
163
363
  },
164
- [ dispatch ]
364
+ [ dispatch, pushToHistory, commitHistory ]
165
365
  );
166
366
 
167
367
  const setCropRect = useCallback(
@@ -172,31 +372,44 @@ export function useCropperState(
172
372
  );
173
373
 
174
374
  const settleCrop = useCallback( () => {
375
+ commitHistory();
376
+ suppressDebounceRef.current = true;
175
377
  dispatch( { type: 'SETTLE_CROP' } );
176
- }, [ dispatch ] );
378
+ }, [ dispatch, commitHistory ] );
177
379
 
178
380
  const applyOperation = useCallback(
179
381
  ( op: TransformOperation ) => {
382
+ commitHistory();
383
+ pushToHistory();
384
+ suppressDebounceRef.current = true;
180
385
  dispatch( { type: 'APPLY_OPERATION', payload: op } );
181
386
  },
182
- [ dispatch ]
387
+ [ dispatch, pushToHistory, commitHistory ]
183
388
  );
184
389
 
185
390
  const reset = useCallback(
186
391
  ( resetState?: Partial< CropperState > ) => {
392
+ commitHistory();
393
+ const nextInitialState = enforceContainment( {
394
+ ...DEFAULT_STATE,
395
+ image: stateRef.current.image,
396
+ ...resetState,
397
+ } );
398
+ if (
399
+ ! areHistoryStatesEqual( stateRef.current, nextInitialState )
400
+ ) {
401
+ pushToHistory();
402
+ }
403
+ suppressDebounceRef.current = true;
187
404
  dispatch( { type: 'RESET', payload: resetState } );
188
405
  // Mirror the reducer's RESET exactly so isDirty stays in
189
406
  // sync. RESET preserves the currently-loaded image; the
190
407
  // containment step can tweak pan/zoom/cropRect by float ulp,
191
408
  // which we must fold into the "initial" snapshot or isDirty
192
409
  // would report true after a reset.
193
- initialRef.current = enforceContainment( {
194
- ...DEFAULT_STATE,
195
- image: stateRef.current.image,
196
- ...resetState,
197
- } );
410
+ initialRef.current = nextInitialState;
198
411
  },
199
- [ dispatch ]
412
+ [ dispatch, pushToHistory, commitHistory ]
200
413
  );
201
414
 
202
415
  const isDirty = isStateDirty( state, initialRef.current );
@@ -226,6 +439,7 @@ export function useCropperState(
226
439
  setZoomAtPoint,
227
440
  setRotation,
228
441
  setFlip,
442
+ toggleFlip,
229
443
  snapRotate90,
230
444
  setCropRect,
231
445
  settleCrop,
@@ -233,6 +447,11 @@ export function useCropperState(
233
447
  reset,
234
448
  isDirty,
235
449
  getCroppedImage,
450
+ hasUndo,
451
+ hasRedo,
452
+ undo,
453
+ redo,
454
+ commitHistory,
236
455
  };
237
456
  return controller;
238
457
  }
@@ -40,9 +40,12 @@ export interface UseInteractionOptions {
40
40
  minZoom?: number;
41
41
  /** Maximum zoom level. Defaults to MAX_ZOOM. */
42
42
  maxZoom?: number;
43
- /** Zoom speed multiplier for wheel events. Defaults to 0.01. */
43
+ /** Zoom speed multiplier for wheel events. Defaults to 0.0025. */
44
44
  zoomSpeed?: number;
45
- /** Pan step size in normalized coords for keyboard events. Defaults to 0.05. */
45
+ /**
46
+ * Pan step size in normalized coords for keyboard events.
47
+ * Defaults to 0.01. Shift multiplies it by 10.
48
+ */
46
49
  keyboardStep?: number;
47
50
  /** Zoom level for double-tap zoom. Defaults to 2. */
48
51
  doubleTapZoom?: number;
@@ -67,6 +70,18 @@ function isHandledKeyboardPan( event: KeyboardEvent ): boolean {
67
70
  }
68
71
  }
69
72
 
73
+ function isHandledKeyboardZoom( event: KeyboardEvent ): boolean {
74
+ switch ( event.key ) {
75
+ case '+':
76
+ case '=':
77
+ case '-':
78
+ case '_':
79
+ return true;
80
+ default:
81
+ return false;
82
+ }
83
+ }
84
+
70
85
  /**
71
86
  * Mouse, touch, and keyboard event handling for pan, zoom,
72
87
  * and crop manipulation.
@@ -95,6 +110,9 @@ export function useInteraction(
95
110
  const [ isKeyboardPanning, setIsKeyboardPanning ] = useState( false );
96
111
  const keyboardInteractionTimerRef =
97
112
  useRef< ReturnType< typeof setTimeout > >();
113
+ // Tracks whether a keyboard gesture (pan or zoom) is currently active so
114
+ // onGestureStart is only fired once per gesture, not on every key repeat.
115
+ const isKeyboardGestureActiveRef = useRef( false );
98
116
 
99
117
  // Keep mutable refs so the controller always reads fresh values
100
118
  // without needing to be recreated.
@@ -116,11 +134,18 @@ export function useInteraction(
116
134
  const stopPlacementGesture = useCallback( () => {
117
135
  setIsGestureActive( false );
118
136
  }, [] );
119
- const signalKeyboardPlacement = useCallback( () => {
120
- setIsKeyboardPanning( true );
137
+ // Shared timer logic for any keyboard gesture (pan or zoom): fires
138
+ // onGestureStart once per burst and onGestureEnd after the idle window.
139
+ const signalKeyboardGesture = useCallback( () => {
140
+ if ( ! isKeyboardGestureActiveRef.current ) {
141
+ isKeyboardGestureActiveRef.current = true;
142
+ optionsRef.current?.onGestureStart?.();
143
+ }
121
144
  clearTimeout( keyboardInteractionTimerRef.current );
122
145
  keyboardInteractionTimerRef.current = setTimeout( () => {
146
+ isKeyboardGestureActiveRef.current = false;
123
147
  setIsKeyboardPanning( false );
148
+ optionsRef.current?.onGestureEnd?.();
124
149
  }, KEYBOARD_INTERACTION_IDLE_MS );
125
150
  }, [] );
126
151
 
@@ -142,6 +167,8 @@ export function useInteraction(
142
167
  actionsRef.current.setZoomAtPoint( zoom, pan ),
143
168
  snapRotate90: ( direction ) =>
144
169
  actionsRef.current.snapRotate90( direction ),
170
+ toggleFlip: ( direction ) =>
171
+ actionsRef.current.toggleFlip?.( direction ),
145
172
  },
146
173
  getContainerSize: () => containerSizeRef.current,
147
174
  getImageSize: () => imageSizeRef.current,
@@ -198,11 +225,14 @@ export function useInteraction(
198
225
  const onKeyDown = useCallback(
199
226
  ( e: React.KeyboardEvent ) => {
200
227
  if ( isHandledKeyboardPan( e.nativeEvent ) ) {
201
- signalKeyboardPlacement();
228
+ setIsKeyboardPanning( true );
229
+ signalKeyboardGesture();
230
+ } else if ( isHandledKeyboardZoom( e.nativeEvent ) ) {
231
+ signalKeyboardGesture();
202
232
  }
203
233
  controllerRef.current?.handleKeyDown( e.nativeEvent );
204
234
  },
205
- [ signalKeyboardPlacement ]
235
+ [ signalKeyboardGesture ]
206
236
  );
207
237
 
208
238
  const onWheelNative = useCallback( ( e: WheelEvent ) => {
@@ -0,0 +1,48 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useReducer, useCallback, useMemo } from '@wordpress/element';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import type { ViewportState } from '../../core/types';
10
+ import {
11
+ viewportReducer,
12
+ DEFAULT_VIEWPORT_STATE,
13
+ } from '../../core/viewport-state';
14
+
15
+ export interface UseViewportStateReturn {
16
+ viewport: ViewportState;
17
+ /**
18
+ * Unused in the current Cropper implementation — scaffolded for the
19
+ * upcoming navigator panel (viewport zoom slider).
20
+ */
21
+ setViewportZoom: ( zoom: number ) => void;
22
+ setViewportPan: ( pan: { x: number; y: number } ) => void;
23
+ resetViewport: () => void;
24
+ }
25
+
26
+ export function useViewportState(): UseViewportStateReturn {
27
+ const [ viewport, dispatch ] = useReducer(
28
+ viewportReducer,
29
+ DEFAULT_VIEWPORT_STATE
30
+ );
31
+
32
+ const setViewportZoom = useCallback( ( zoom: number ) => {
33
+ dispatch( { type: 'SET_VIEWPORT_ZOOM', payload: zoom } );
34
+ }, [] );
35
+
36
+ const setViewportPan = useCallback( ( pan: { x: number; y: number } ) => {
37
+ dispatch( { type: 'SET_VIEWPORT_PAN', payload: pan } );
38
+ }, [] );
39
+
40
+ const resetViewport = useCallback( () => {
41
+ dispatch( { type: 'RESET_VIEWPORT' } );
42
+ }, [] );
43
+
44
+ return useMemo(
45
+ () => ( { viewport, setViewportZoom, setViewportPan, resetViewport } ),
46
+ [ viewport, setViewportZoom, setViewportPan, resetViewport ]
47
+ );
48
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * CSS style object that visually hides an element while keeping it accessible
3
+ * to screen readers.
4
+ */
5
+ export const VISUALLY_HIDDEN_STYLE: React.CSSProperties = {
6
+ position: 'absolute',
7
+ width: 1,
8
+ height: 1,
9
+ padding: 0,
10
+ margin: -1,
11
+ overflow: 'hidden',
12
+ clip: 'rect(0, 0, 0, 0)',
13
+ whiteSpace: 'nowrap',
14
+ border: 0,
15
+ };
@@ -4,9 +4,11 @@
4
4
  import { lock } from './lock-unlock';
5
5
  import { store } from './store';
6
6
  import { MediaEditorModal } from './components/media-editor-modal';
7
+ import { MediaEditor } from './components/media-editor';
7
8
 
8
9
  export const privateApis = {};
9
10
  lock( privateApis, {
10
11
  store,
12
+ MediaEditor,
11
13
  MediaEditorModal,
12
14
  } );
package/src/style.scss CHANGED
@@ -1,6 +1,9 @@
1
1
  // Import component styles
2
2
  @use "./components/media-preview/style.scss" as *;
3
3
  @use "./image-editor/style.scss" as *;
4
+ @use "./components/media-editor/style.scss" as *;
4
5
  @use "./components/media-editor-modal/style.scss" as *;
5
6
  @use "./components/media-editor-canvas/style.scss" as *;
6
7
  @use "./components/media-editor-toolbar/style.scss" as *;
8
+ @use "./components/media-editor-keyboard-shortcuts-modal/style.scss" as *;
9
+ @use "./components/rotation-ruler/style.scss" as *;
package/tsconfig.json CHANGED
@@ -14,6 +14,7 @@
14
14
  { "path": "../element" },
15
15
  { "path": "../i18n" },
16
16
  { "path": "../icons" },
17
+ { "path": "../keycodes" },
17
18
  { "path": "../notices" },
18
19
  { "path": "../private-apis" },
19
20
  { "path": "../ui" }