@wordpress/media-editor 0.8.0 → 0.8.1-next.v.202605131032.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -0,0 +1,230 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useCallback, useEffect, useRef } from '@wordpress/element';
5
+
6
+ /**
7
+ * Convert a horizontal pointer delta (px) into a value delta.
8
+ *
9
+ * Negative because dragging the ruler to the right should expose
10
+ * smaller values to the left of the fixed center pointer (and vice
11
+ * versa) — the ruler scrubs *under* the pointer.
12
+ *
13
+ * @param deltaPx Pointer movement in CSS pixels.
14
+ * @param pixelsPerStep CSS pixels of pointer travel per `step`.
15
+ * @param step Value units per step.
16
+ */
17
+ export function pxToValueDelta(
18
+ deltaPx: number,
19
+ pixelsPerStep: number,
20
+ step: number
21
+ ): number {
22
+ return ( -deltaPx / pixelsPerStep ) * step;
23
+ }
24
+
25
+ /**
26
+ * Clamp `value` to the inclusive `[min, max]` interval.
27
+ *
28
+ * @param value Value to clamp.
29
+ * @param min Lower bound (inclusive).
30
+ * @param max Upper bound (inclusive).
31
+ */
32
+ export function clampValue( value: number, min: number, max: number ): number {
33
+ if ( value < min ) {
34
+ return min;
35
+ }
36
+ if ( value > max ) {
37
+ return max;
38
+ }
39
+ return value;
40
+ }
41
+
42
+ /**
43
+ * Round `value` to the nearest multiple of `step`.
44
+ *
45
+ * @param value Value to quantize.
46
+ * @param step Step granularity (must be > 0).
47
+ */
48
+ export function quantize( value: number, step: number ): number {
49
+ return Math.round( value / step ) * step;
50
+ }
51
+
52
+ export interface UseRulerDragOptions {
53
+ value: number;
54
+ onChange: ( next: number ) => void;
55
+ min: number;
56
+ max: number;
57
+ step: number;
58
+ pixelsPerStep: number;
59
+ disabled: boolean;
60
+ /** Called once on pointerdown so the caller can focus the input. */
61
+ onPointerDownStart?: () => void;
62
+ }
63
+
64
+ export interface RulerDragHandlers {
65
+ onPointerDown: ( event: React.PointerEvent< HTMLElement > ) => void;
66
+ onPointerMove: ( event: React.PointerEvent< HTMLElement > ) => void;
67
+ onPointerUp: ( event: React.PointerEvent< HTMLElement > ) => void;
68
+ onPointerCancel: ( event: React.PointerEvent< HTMLElement > ) => void;
69
+ }
70
+
71
+ /**
72
+ * Drag-the-ruler gesture for a horizontal slider. Translates pointer
73
+ * movement into value changes quantized to `step`; the caller renders
74
+ * the visuals and holds the value.
75
+ *
76
+ * The hook owns no state of its own — every value change is reported
77
+ * through the supplied `onChange`. The "current value" is read from a
78
+ * ref so closure staleness during a drag is impossible. Drag continues
79
+ * past the wrapper's bounds (standard slider behaviour); a window-level
80
+ * `pointerup` listener guarantees the drag ends and pointer capture
81
+ * releases even if the underlying captured event never fires on the
82
+ * wrapper.
83
+ *
84
+ * @param options Ruler-drag configuration. See `UseRulerDragOptions`.
85
+ */
86
+ export function useRulerDrag(
87
+ options: UseRulerDragOptions
88
+ ): RulerDragHandlers {
89
+ const {
90
+ value,
91
+ onChange,
92
+ min,
93
+ max,
94
+ step,
95
+ pixelsPerStep,
96
+ disabled,
97
+ onPointerDownStart,
98
+ } = options;
99
+
100
+ // Mutable mirror of the latest committed value plus per-drag state.
101
+ // Pointermove handlers fire far faster than React commits, so we
102
+ // cannot read `value` from closure. The `value` write happens in an
103
+ // effect (not at render time) to satisfy `react-hooks/refs`; the
104
+ // effect flushes after commit, so by the time any pointer handler
105
+ // runs, the ref reflects the latest committed `value`.
106
+ const latestRef = useRef< {
107
+ value: number;
108
+ startX: number;
109
+ startValue: number;
110
+ activeStep: number;
111
+ dragging: boolean;
112
+ captureElement: HTMLElement | null;
113
+ capturePointerId: number;
114
+ windowPointerUp: ( () => void ) | null;
115
+ } >( {
116
+ value,
117
+ startX: 0,
118
+ startValue: 0,
119
+ activeStep: step,
120
+ dragging: false,
121
+ captureElement: null,
122
+ capturePointerId: 0,
123
+ windowPointerUp: null,
124
+ } );
125
+ useEffect( () => {
126
+ latestRef.current.value = value;
127
+ }, [ value ] );
128
+
129
+ const endDrag = useCallback( () => {
130
+ const state = latestRef.current;
131
+ if ( ! state.dragging ) {
132
+ return;
133
+ }
134
+ state.dragging = false;
135
+ if (
136
+ state.captureElement &&
137
+ state.captureElement.hasPointerCapture( state.capturePointerId )
138
+ ) {
139
+ state.captureElement.releasePointerCapture(
140
+ state.capturePointerId
141
+ );
142
+ }
143
+ state.captureElement = null;
144
+ if ( state.windowPointerUp ) {
145
+ window.removeEventListener( 'pointerup', state.windowPointerUp );
146
+ window.removeEventListener(
147
+ 'pointercancel',
148
+ state.windowPointerUp
149
+ );
150
+ state.windowPointerUp = null;
151
+ }
152
+ }, [] );
153
+
154
+ // Cleanup any in-flight drag on unmount so we never leak window
155
+ // listeners or stale pointer capture.
156
+ useEffect( () => endDrag, [ endDrag ] );
157
+
158
+ const onPointerDown = useCallback(
159
+ ( event: React.PointerEvent< HTMLElement > ) => {
160
+ if ( disabled || event.button !== 0 ) {
161
+ return;
162
+ }
163
+ const state = latestRef.current;
164
+ event.currentTarget.setPointerCapture( event.pointerId );
165
+ state.captureElement = event.currentTarget;
166
+ state.capturePointerId = event.pointerId;
167
+ state.startX = event.clientX;
168
+ state.startValue = state.value;
169
+ state.activeStep = event.shiftKey ? step / 2 : step;
170
+ state.dragging = true;
171
+ onPointerDownStart?.();
172
+
173
+ // Window-level backstop. With `setPointerCapture`, the
174
+ // element should always receive `pointerup` even when the
175
+ // cursor is released outside its bounds — but in practice
176
+ // some browsers / window-blur sequences drop the event. The
177
+ // window listener guarantees the drag ends and capture
178
+ // releases.
179
+ const onWindowUp = () => endDrag();
180
+ state.windowPointerUp = onWindowUp;
181
+ window.addEventListener( 'pointerup', onWindowUp );
182
+ window.addEventListener( 'pointercancel', onWindowUp );
183
+ },
184
+ [ disabled, onPointerDownStart, endDrag, step ]
185
+ );
186
+
187
+ const onPointerMove = useCallback(
188
+ ( event: React.PointerEvent< HTMLElement > ) => {
189
+ const state = latestRef.current;
190
+ if ( ! state.dragging ) {
191
+ return;
192
+ }
193
+ // Shift toggles fine mode mid-drag. The drag math computes
194
+ // from `startX` / `startValue`, so naively swapping `step`
195
+ // would yank the value backward. Rebase to the current
196
+ // pointer + value so the granularity changes without a
197
+ // jump.
198
+ const requestedStep = event.shiftKey ? step / 2 : step;
199
+ if ( requestedStep !== state.activeStep ) {
200
+ state.startX = event.clientX;
201
+ state.startValue = state.value;
202
+ state.activeStep = requestedStep;
203
+ }
204
+ const deltaPx = event.clientX - state.startX;
205
+ const deltaValue = pxToValueDelta(
206
+ deltaPx,
207
+ pixelsPerStep,
208
+ state.activeStep
209
+ );
210
+ const raw = state.startValue + deltaValue;
211
+ const stepped = quantize( raw, state.activeStep );
212
+ const next = clampValue( stepped, min, max );
213
+ if ( next !== state.value ) {
214
+ // Update the ref synchronously so consecutive pointermove
215
+ // events that fire before React commits still see the
216
+ // last *emitted* value as `previous` for the next math.
217
+ state.value = next;
218
+ onChange( next );
219
+ }
220
+ },
221
+ [ onChange, min, max, step, pixelsPerStep ]
222
+ );
223
+
224
+ return {
225
+ onPointerDown,
226
+ onPointerMove,
227
+ onPointerUp: endDrag,
228
+ onPointerCancel: endDrag,
229
+ };
230
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import { useCropper } from '../image-editor';
5
+
6
+ /**
7
+ * Data attribute applied to crop control wrappers. The modal's keyboard
8
+ * shortcut handler uses this to distinguish crop controls (where custom
9
+ * undo/redo should fire) from metadata text fields (where the browser's
10
+ * native undo should be preserved).
11
+ */
12
+ export const CROP_CONTROL_ATTR = 'data-crop-control';
13
+
14
+ export interface UseCropGestureHandlersOptions {
15
+ /**
16
+ * When `true` (default), key-up triggers an immediate history commit so
17
+ * each discrete keypress becomes its own undo step. Set to `false` for
18
+ * continuous-input controls (e.g. the rotation ruler) where rapid
19
+ * keypresses should coalesce into a single history entry via the
20
+ * state-change debounce.
21
+ */
22
+ commitOnKeyUp?: boolean;
23
+ }
24
+
25
+ /**
26
+ * Returns event handler props to spread onto a wrapper element around a
27
+ * crop control. Marks the wrapper as a crop control (via `data-crop-control`)
28
+ * so the modal's Cmd+Z handler can identify it, and wires up immediate-flush
29
+ * signals on pointer-up and (optionally) key-up so history is committed as
30
+ * soon as the user releases rather than waiting for the debounce window to
31
+ * expire.
32
+ *
33
+ * The history entry itself is recorded by the state-change debounce in
34
+ * `useCropperState` — no explicit gesture-start signal is needed.
35
+ *
36
+ * Usage:
37
+ * const gestureHandlers = useCropGestureHandlers();
38
+ * <div role="presentation" { ...gestureHandlers }>
39
+ * <RangeControl ... />
40
+ * </div>
41
+ *
42
+ * @param options Optional behavior flags.
43
+ */
44
+ export function useCropGestureHandlers(
45
+ options: UseCropGestureHandlersOptions = {}
46
+ ) {
47
+ const { commitOnKeyUp = true } = options;
48
+ const { commitHistory } = useCropper();
49
+ return {
50
+ [ CROP_CONTROL_ATTR ]: true,
51
+ onPointerUp: commitHistory,
52
+ ...( commitOnKeyUp ? { onKeyUp: commitHistory } : {} ),
53
+ };
54
+ }
@@ -11,6 +11,18 @@ import type { NormalizedRect, Flip, CropperState } from './types';
11
11
  export const MIN_ZOOM = 1;
12
12
  export const MAX_ZOOM = 10;
13
13
 
14
+ /**
15
+ * Wheel zoom sensitivity. A deltaY of 100 changes zoom by 0.25.
16
+ * This could be made configurable as a prop to the Cropper component.
17
+ */
18
+ export const DEFAULT_WHEEL_ZOOM_SPEED = 0.0025;
19
+
20
+ /** Fine step for keyboard-driven cropper movement, in normalized coordinates. */
21
+ export const DEFAULT_KEYBOARD_STEP = 0.01;
22
+
23
+ /** Coarse keyboard movement multiplier when Shift is held. */
24
+ export const KEYBOARD_SHIFT_STEP_MULTIPLIER = 10;
25
+
14
26
  /**
15
27
  * Maximum free-rotation offset in degrees from the nearest 90° step.
16
28
  * The rotation slider allows ±45° around the current cardinal angle.
@@ -86,25 +86,28 @@ function getMinZoomForCover(
86
86
  }
87
87
 
88
88
  /**
89
- * Compute the crop handle bounds in normalized visual space by finding the
89
+ * Compute the crop handle bounds in normalized visual space — the
90
90
  * axis-aligned bounding box (AABB) of the actual image footprint at the
91
- * current pan/zoom/rotation, then intersecting with the container viewport.
91
+ * current pan/zoom/rotation.
92
+ *
93
+ * Returns the full image AABB without intersecting with the container
94
+ * viewport. The viewport pan mechanism in the Cropper component allows
95
+ * the canvas to scroll so handles can reach the image edge even when it
96
+ * lies outside the visible canvas area.
92
97
  *
93
98
  * Unlike a static centerline-based calculation, this accounts for the
94
99
  * current pan position — if the user pans right, the left bound tightens
95
100
  * because the image's left edge has moved right.
96
101
  *
97
- * @param state The current cropper state (crop, zoom, rotation, flip).
98
- * @param elementSize The fitted (unrotated) image element dimensions in pixels.
99
- * @param visualSize The visual (rotated) image bounding box in pixels.
100
- * @param containerSize The container dimensions in pixels.
102
+ * @param state The current cropper state (crop, zoom, rotation, flip).
103
+ * @param elementSize The fitted (unrotated) image element dimensions in pixels.
104
+ * @param visualSize The visual (rotated) image bounding box in pixels.
101
105
  * @return The min/max x and y that a crop rect edge can reach in normalized space.
102
106
  */
103
- export function getCropBounds(
107
+ export function getImageCropBounds(
104
108
  state: CropperState,
105
109
  elementSize: Size,
106
- visualSize: Size,
107
- containerSize: Size
110
+ visualSize: Size
108
111
  ): { minX: number; minY: number; maxX: number; maxY: number } {
109
112
  if (
110
113
  elementSize.width === 0 ||
@@ -140,13 +143,6 @@ export function getCropBounds(
140
143
  [ -hw, hh ],
141
144
  ];
142
145
 
143
- // Transform corners through CSS matrix → screen offsets from element center.
144
- // Screen position = element_center + transformed_offset.
145
- // Element center is at (containerW/2, containerH/2) after CSS centering.
146
- // But we want normalized coords, so work relative to the visual image origin.
147
- const offsetX = ( containerSize.width - visualSize.width ) / 2;
148
- const offsetY = ( containerSize.height - visualSize.height ) / 2;
149
-
150
146
  let imgMinX = Infinity;
151
147
  let imgMaxX = -Infinity;
152
148
  let imgMinY = Infinity;
@@ -167,19 +163,11 @@ export function getCropBounds(
167
163
  imgMaxY = Math.max( imgMaxY, ny );
168
164
  }
169
165
 
170
- // Container bounds in normalized space.
171
- const containerMinX = -offsetX / visualSize.width;
172
- const containerMaxX = ( containerSize.width - offsetX ) / visualSize.width;
173
- const containerMinY = -offsetY / visualSize.height;
174
- const containerMaxY =
175
- ( containerSize.height - offsetY ) / visualSize.height;
176
-
177
- // Crop bounds = intersection of image AABB and container bounds.
178
166
  return {
179
- minX: Math.max( imgMinX, containerMinX ),
180
- minY: Math.max( imgMinY, containerMinY ),
181
- maxX: Math.min( imgMaxX, containerMaxX ),
182
- maxY: Math.min( imgMaxY, containerMaxY ),
167
+ minX: imgMinX,
168
+ minY: imgMinY,
169
+ maxX: imgMaxX,
170
+ maxY: imgMaxY,
183
171
  };
184
172
  }
185
173
 
@@ -2,7 +2,13 @@
2
2
  * Internal dependencies
3
3
  */
4
4
  import type { CropperState, NormalizedPoint, Size } from './types';
5
- import { MIN_ZOOM, MAX_ZOOM } from './constants';
5
+ import {
6
+ DEFAULT_KEYBOARD_STEP,
7
+ DEFAULT_WHEEL_ZOOM_SPEED,
8
+ KEYBOARD_SHIFT_STEP_MULTIPLIER,
9
+ MIN_ZOOM,
10
+ MAX_ZOOM,
11
+ } from './constants';
6
12
  import { restrictPanZoom } from './containment';
7
13
 
8
14
  /** Time window for detecting a double-tap gesture (ms). */
@@ -62,6 +68,8 @@ export interface CropperInteractionActions {
62
68
  setZoomAtPoint: ( zoom: number, pan: NormalizedPoint ) => void;
63
69
  /** Snap rotate by 90 degrees. */
64
70
  snapRotate90: ( direction: 1 | -1 ) => void;
71
+ /** Toggle flip on the given axis. */
72
+ toggleFlip?: ( direction: 'horizontal' | 'vertical' ) => void;
65
73
  }
66
74
 
67
75
  /**
@@ -84,9 +92,12 @@ export interface InteractionControllerOptions {
84
92
  minZoom?: number;
85
93
  /** Maximum zoom level. Defaults to MAX_ZOOM. Read lazily. */
86
94
  maxZoom?: number;
87
- /** Zoom speed multiplier for wheel events. Defaults to 0.01. Read lazily. */
95
+ /** Zoom speed multiplier for wheel events. Defaults to 0.0025. Read lazily. */
88
96
  zoomSpeed?: number;
89
- /** Pan step size in normalized coords for keyboard events. Defaults to 0.05. Read lazily. */
97
+ /**
98
+ * Pan step size in normalized coords for keyboard events.
99
+ * Defaults to 0.01. Read lazily. Shift multiplies it by 10.
100
+ */
90
101
  keyboardStep?: number;
91
102
  /** Zoom level for double-tap zoom. Defaults to 2. Read lazily. */
92
103
  doubleTapZoom?: number;
@@ -200,12 +211,24 @@ export class InteractionController {
200
211
 
201
212
  /** Read zoomSpeed lazily so option changes take effect immediately. */
202
213
  private get zoomSpeed(): number {
203
- return this.options.zoomSpeed ?? 0.01;
214
+ return this.options.zoomSpeed ?? DEFAULT_WHEEL_ZOOM_SPEED;
204
215
  }
205
216
 
206
217
  /** Read keyboardStep lazily so option changes take effect immediately. */
207
218
  private get keyboardStep(): number {
208
- return this.options.keyboardStep ?? 0.05;
219
+ return this.options.keyboardStep ?? DEFAULT_KEYBOARD_STEP;
220
+ }
221
+
222
+ /**
223
+ * Get the keyboard pan step, including modifier-based coarse movement.
224
+ *
225
+ * @param event The native KeyboardEvent.
226
+ * @return The normalized pan step.
227
+ */
228
+ private getKeyboardStep( event: KeyboardEvent ): number {
229
+ return event.shiftKey
230
+ ? this.keyboardStep * KEYBOARD_SHIFT_STEP_MULTIPLIER
231
+ : this.keyboardStep;
209
232
  }
210
233
 
211
234
  /** Read doubleTapZoom lazily so option changes take effect immediately. */
@@ -361,6 +384,10 @@ export class InteractionController {
361
384
  handleWheel( e: WheelEvent ): void {
362
385
  e.preventDefault();
363
386
 
387
+ if ( this.drag ) {
388
+ return;
389
+ }
390
+
364
391
  // Debounced gesture boundaries for wheel zoom.
365
392
  // Start on first wheel event, end after 300ms of no events.
366
393
  if ( ! this.wheelGestureActive ) {
@@ -731,6 +758,11 @@ export class InteractionController {
731
758
  this.setStatus( { isZooming: false } );
732
759
  }, ZOOM_ANIMATION_DURATION );
733
760
 
761
+ // Double-tap bypasses the normal handleTouchStart gesture path so
762
+ // onGestureStart/End are never called — fire them here so undo
763
+ // receives a proper boundary around this discrete zoom action.
764
+ this.options.onGestureStart?.();
765
+
734
766
  if ( visSize.width > 0 && visSize.height > 0 ) {
735
767
  const fx = tapX - containerRect.left - containerSize.width / 2;
736
768
  const fy = tapY - containerRect.top - containerSize.height / 2;
@@ -758,6 +790,8 @@ export class InteractionController {
758
790
  } else {
759
791
  this.options.actions.setZoom( targetZoom );
760
792
  }
793
+
794
+ this.options.onGestureEnd?.();
761
795
  return true;
762
796
  }
763
797
 
@@ -774,12 +808,13 @@ export class InteractionController {
774
808
  switch ( e.key ) {
775
809
  case 'ArrowUp': {
776
810
  e.preventDefault();
811
+ const keyboardStep = this.getKeyboardStep( e );
777
812
  const { pan: newCrop } = restrictPanZoom(
778
813
  {
779
814
  ...currentState,
780
815
  pan: {
781
816
  x: currentState.pan.x,
782
- y: currentState.pan.y + this.keyboardStep,
817
+ y: currentState.pan.y + keyboardStep,
783
818
  },
784
819
  },
785
820
  getImageSizeFromState( currentState ),
@@ -790,12 +825,13 @@ export class InteractionController {
790
825
  }
791
826
  case 'ArrowDown': {
792
827
  e.preventDefault();
828
+ const keyboardStep = this.getKeyboardStep( e );
793
829
  const { pan: newCrop } = restrictPanZoom(
794
830
  {
795
831
  ...currentState,
796
832
  pan: {
797
833
  x: currentState.pan.x,
798
- y: currentState.pan.y - this.keyboardStep,
834
+ y: currentState.pan.y - keyboardStep,
799
835
  },
800
836
  },
801
837
  getImageSizeFromState( currentState ),
@@ -806,11 +842,12 @@ export class InteractionController {
806
842
  }
807
843
  case 'ArrowLeft': {
808
844
  e.preventDefault();
845
+ const keyboardStep = this.getKeyboardStep( e );
809
846
  const { pan: newCrop } = restrictPanZoom(
810
847
  {
811
848
  ...currentState,
812
849
  pan: {
813
- x: currentState.pan.x + this.keyboardStep,
850
+ x: currentState.pan.x + keyboardStep,
814
851
  y: currentState.pan.y,
815
852
  },
816
853
  },
@@ -822,11 +859,12 @@ export class InteractionController {
822
859
  }
823
860
  case 'ArrowRight': {
824
861
  e.preventDefault();
862
+ const keyboardStep = this.getKeyboardStep( e );
825
863
  const { pan: newCrop } = restrictPanZoom(
826
864
  {
827
865
  ...currentState,
828
866
  pan: {
829
- x: currentState.pan.x - this.keyboardStep,
867
+ x: currentState.pan.x - keyboardStep,
830
868
  y: currentState.pan.y,
831
869
  },
832
870
  },
@@ -858,11 +896,29 @@ export class InteractionController {
858
896
  }
859
897
  case 'r':
860
898
  case 'R': {
899
+ if ( e.metaKey || e.ctrlKey || e.altKey ) {
900
+ break;
901
+ }
902
+ e.preventDefault();
903
+ this.options.actions.snapRotate90( e.shiftKey ? -1 : 1 );
904
+ break;
905
+ }
906
+ case 'h':
907
+ case 'H': {
908
+ if ( e.metaKey || e.ctrlKey || e.altKey || e.shiftKey ) {
909
+ break;
910
+ }
911
+ e.preventDefault();
912
+ this.options.actions.toggleFlip?.( 'horizontal' );
913
+ break;
914
+ }
915
+ case 'v':
916
+ case 'V': {
861
917
  if ( e.metaKey || e.ctrlKey || e.altKey || e.shiftKey ) {
862
918
  break;
863
919
  }
864
920
  e.preventDefault();
865
- this.options.actions.snapRotate90( 1 );
921
+ this.options.actions.toggleFlip?.( 'vertical' );
866
922
  break;
867
923
  }
868
924
  }
@@ -6,6 +6,13 @@ import { DEFAULT_STATE, MAX_ZOOM } from './constants';
6
6
  import { normalizeRotation, degreesToRadians } from './math/rotation';
7
7
  import { restrictPanZoom, restrictCropRect } from './containment';
8
8
 
9
+ /** Small tolerance for cropper floating-point comparisons. */
10
+ const STATE_EPSILON = 1e-6;
11
+
12
+ function nearlyEqual( a: number, b: number ): boolean {
13
+ return Math.abs( a - b ) < STATE_EPSILON;
14
+ }
15
+
9
16
  /**
10
17
  * Translate a pipeline transform operation into the equivalent
11
18
  * reducer action. Pipeline ops aren't 1:1 with reducer actions —
@@ -414,15 +421,15 @@ export function isStateDirty(
414
421
  initial: CropperState
415
422
  ): boolean {
416
423
  return (
417
- current.pan.x !== initial.pan.x ||
418
- current.pan.y !== initial.pan.y ||
419
- current.zoom !== initial.zoom ||
420
- current.rotation !== initial.rotation ||
424
+ ! nearlyEqual( current.pan.x, initial.pan.x ) ||
425
+ ! nearlyEqual( current.pan.y, initial.pan.y ) ||
426
+ ! nearlyEqual( current.zoom, initial.zoom ) ||
427
+ ! nearlyEqual( current.rotation, initial.rotation ) ||
421
428
  current.flip.horizontal !== initial.flip.horizontal ||
422
429
  current.flip.vertical !== initial.flip.vertical ||
423
- current.cropRect.x !== initial.cropRect.x ||
424
- current.cropRect.y !== initial.cropRect.y ||
425
- current.cropRect.width !== initial.cropRect.width ||
426
- current.cropRect.height !== initial.cropRect.height
430
+ ! nearlyEqual( current.cropRect.x, initial.cropRect.x ) ||
431
+ ! nearlyEqual( current.cropRect.y, initial.cropRect.y ) ||
432
+ ! nearlyEqual( current.cropRect.width, initial.cropRect.width ) ||
433
+ ! nearlyEqual( current.cropRect.height, initial.cropRect.height )
427
434
  );
428
435
  }
@@ -9,7 +9,7 @@ import {
9
9
  import {
10
10
  restrictPanZoom,
11
11
  restrictCropRect,
12
- getCropBounds,
12
+ getImageCropBounds,
13
13
  } from '../containment';
14
14
  import { getSourceRegion, getSourceRegionPercent } from '../source-region';
15
15
  import { DEFAULT_STATE } from '../constants';
@@ -547,18 +547,16 @@ describe( 'getSourceRegionPercent', () => {
547
547
  } );
548
548
  } );
549
549
 
550
- describe( 'getCropBounds', () => {
551
- it( 'allows crop handle to reach container edge when 4:3 image is zoomed in 3:2 container', () => {
552
- // MtBlanc1.jpg is 500×375 (4:3) in a 600×400 container (3:2).
553
- // At zoom=1, the image is 533×400 — 33px padding on each side.
554
- // At zoom=1.75, the image fills the container. The crop handle
555
- // should be able to reach the container left edge (pixel 0),
556
- // which is at normalized x = -0.0625.
550
+ describe( 'getImageCropBounds', () => {
551
+ it( 'returns the image AABB — handles can reach the image edge regardless of container size', () => {
552
+ // 500×375 (4:3) in a 600×400 container (3:2).
553
+ // At zoom=1.75 the image overflows the container. Bounds should reflect
554
+ // the image footprint, not the container edge, so one drag can reach the
555
+ // image edge (viewport pan reveals content outside the canvas boundary).
557
556
  const nat: Size = { width: 500, height: 375 };
558
557
  const container: Size = { width: 600, height: 400 };
559
558
  const { elementSize, visualSize } = getImageFit( container, nat, 0 );
560
559
 
561
- // Verify the image doesn't fill the container at zoom=1.
562
560
  expect( visualSize.width ).toBeLessThan( container.width );
563
561
 
564
562
  const state = makeState( {
@@ -571,23 +569,14 @@ describe( 'getCropBounds', () => {
571
569
  rotation: 0,
572
570
  } );
573
571
 
574
- const bounds = getCropBounds(
575
- state,
576
- elementSize,
577
- visualSize,
578
- container
579
- );
572
+ const bounds = getImageCropBounds( state, elementSize, visualSize );
580
573
 
581
- // boundsMinX should be negative (crop can extend left of visual image origin).
582
- expect( bounds.minX ).toBeLessThan( 0 );
583
- // The pixel position of boundsMinX should be the container left edge (pixel 0).
584
- const offsetX = ( container.width - visualSize.width ) / 2;
585
- const pixelLeft = offsetX + bounds.minX * visualSize.width;
586
- expect( pixelLeft ).toBeCloseTo( 0, 0 );
587
-
588
- // boundsMaxX should be > 1 (crop can extend right of visual image).
589
- expect( bounds.maxX ).toBeGreaterThan( 1 );
590
- const pixelRight = offsetX + bounds.maxX * visualSize.width;
591
- expect( pixelRight ).toBeCloseTo( container.width, 0 );
574
+ // At zoom=1.75 the image extends well beyond the container on all sides.
575
+ // Bounds should be the image AABB corners (≈ ±0.375), not the container
576
+ // edges (which would be ≈ ±0.0625).
577
+ expect( bounds.minX ).toBeLessThan( -0.3 );
578
+ expect( bounds.maxX ).toBeGreaterThan( 1.3 );
579
+ expect( bounds.minY ).toBeLessThan( -0.3 );
580
+ expect( bounds.maxY ).toBeGreaterThan( 1.3 );
592
581
  } );
593
582
  } );