@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
@@ -1,267 +0,0 @@
1
- import { beforeEach, describe, expect, it } from 'vitest';
2
- import type { CropperState, TransformOperation } from '../../types';
3
- import { DEFAULT_STATE } from '../../constants';
4
- import { applyOperationToState, stateFromPipeline } from '../pipeline';
5
- import { cropperReducer } from '../../state';
6
-
7
- describe( 'applyOperationToState', () => {
8
- let baseState: CropperState;
9
-
10
- beforeEach( () => {
11
- baseState = {
12
- ...DEFAULT_STATE,
13
- flip: { ...DEFAULT_STATE.flip },
14
- pan: { ...DEFAULT_STATE.pan },
15
- cropRect: { ...DEFAULT_STATE.cropRect },
16
- };
17
- } );
18
-
19
- it( 'should handle crop: set cropRect', () => {
20
- const result = applyOperationToState( baseState, {
21
- type: 'crop',
22
- rect: { x: 0.1, y: 0.2, width: 0.5, height: 0.6 },
23
- } );
24
-
25
- expect( result.cropRect ).toEqual( {
26
- x: 0.1,
27
- y: 0.2,
28
- width: 0.5,
29
- height: 0.6,
30
- } );
31
- } );
32
-
33
- it( 'should handle rotate: add degrees and normalize', () => {
34
- const result = applyOperationToState( baseState, {
35
- type: 'rotate',
36
- degrees: 90,
37
- } );
38
-
39
- expect( result.rotation ).toBe( 90 );
40
- } );
41
-
42
- it( 'should handle rotate: accumulate and normalize past 360', () => {
43
- let state = applyOperationToState( baseState, {
44
- type: 'rotate',
45
- degrees: 270,
46
- } );
47
- state = applyOperationToState( state, {
48
- type: 'rotate',
49
- degrees: 180,
50
- } );
51
-
52
- // 270 + 180 = 450 -> 90 (normalized)
53
- expect( state.rotation ).toBe( 90 );
54
- } );
55
-
56
- it( 'should handle rotate: negative degrees normalize correctly', () => {
57
- const result = applyOperationToState( baseState, {
58
- type: 'rotate',
59
- degrees: -90,
60
- } );
61
-
62
- expect( result.rotation ).toBe( 270 );
63
- } );
64
-
65
- it( 'should handle flip horizontal: toggle on', () => {
66
- const result = applyOperationToState( baseState, {
67
- type: 'flip',
68
- direction: 'horizontal',
69
- } );
70
-
71
- expect( result.flip.horizontal ).toBe( true );
72
- expect( result.flip.vertical ).toBe( false );
73
- } );
74
-
75
- it( 'should handle flip horizontal: toggle off', () => {
76
- const flippedState: CropperState = {
77
- ...baseState,
78
- flip: { horizontal: true, vertical: false },
79
- };
80
-
81
- const result = applyOperationToState( flippedState, {
82
- type: 'flip',
83
- direction: 'horizontal',
84
- } );
85
-
86
- expect( result.flip.horizontal ).toBe( false );
87
- expect( result.flip.vertical ).toBe( false );
88
- } );
89
-
90
- it( 'should handle flip vertical: toggle on', () => {
91
- const result = applyOperationToState( baseState, {
92
- type: 'flip',
93
- direction: 'vertical',
94
- } );
95
-
96
- expect( result.flip.horizontal ).toBe( false );
97
- expect( result.flip.vertical ).toBe( true );
98
- } );
99
-
100
- it( 'should handle zoom: set zoom factor', () => {
101
- const result = applyOperationToState( baseState, {
102
- type: 'zoom',
103
- factor: 3.5,
104
- } );
105
-
106
- expect( result.zoom ).toBe( 3.5 );
107
- } );
108
-
109
- it( 'should not mutate the original state', () => {
110
- const original = { ...baseState };
111
- applyOperationToState( baseState, {
112
- type: 'rotate',
113
- degrees: 90,
114
- } );
115
-
116
- expect( baseState.rotation ).toBe( original.rotation );
117
- } );
118
- } );
119
-
120
- describe( 'stateFromPipeline', () => {
121
- it( 'should return default state for an empty pipeline', () => {
122
- const result = stateFromPipeline( [] );
123
- expect( result.rotation ).toBe( 0 );
124
- expect( result.zoom ).toBe( 1 );
125
- expect( result.flip ).toEqual( { horizontal: false, vertical: false } );
126
- } );
127
-
128
- it( 'should apply a sequence of operations correctly', () => {
129
- const pipeline: TransformOperation[] = [
130
- { type: 'rotate', degrees: 45 },
131
- { type: 'flip', direction: 'horizontal' },
132
- { type: 'zoom', factor: 2 },
133
- {
134
- type: 'crop',
135
- rect: { x: 0.25, y: 0.25, width: 0.5, height: 0.5 },
136
- },
137
- ];
138
-
139
- const result = stateFromPipeline( pipeline );
140
-
141
- expect( result.rotation ).toBe( 45 );
142
- expect( result.flip.horizontal ).toBe( true );
143
- expect( result.flip.vertical ).toBe( false );
144
- expect( result.zoom ).toBe( 2 );
145
- expect( result.cropRect ).toEqual( {
146
- x: 0.25,
147
- y: 0.25,
148
- width: 0.5,
149
- height: 0.5,
150
- } );
151
- } );
152
-
153
- it( 'should use custom initial state when provided', () => {
154
- // The custom initial represents a user who committed to this
155
- // pose via UI actions. Sync base fields so the pipeline replay
156
- // (which routes through the reducer) doesn't relax them.
157
- const customInitial: CropperState = {
158
- ...DEFAULT_STATE,
159
- rotation: 90,
160
- baseRotation: 90,
161
- zoom: 2,
162
- baseZoom: 2,
163
- flip: { horizontal: true, vertical: false },
164
- pan: { ...DEFAULT_STATE.pan },
165
- basePan: { ...DEFAULT_STATE.pan },
166
- cropRect: { ...DEFAULT_STATE.cropRect },
167
- };
168
-
169
- const pipeline: TransformOperation[] = [
170
- { type: 'rotate', degrees: 90 },
171
- ];
172
-
173
- const result = stateFromPipeline( pipeline, customInitial );
174
-
175
- // Single-axis flip is active, so `degrees: 90` (visual CW) is
176
- // applied as -90° to the rotation field: 90 − 90 = 0.
177
- expect( result.rotation ).toBe( 0 );
178
- // Other state should carry forward.
179
- expect( result.zoom ).toBe( 2 );
180
- expect( result.flip.horizontal ).toBe( true );
181
- } );
182
- } );
183
-
184
- describe( 'pipeline / reducer parity', () => {
185
- // These tests pin the API parity contract: applying a pipeline op
186
- // via applyOperationToState and dispatching the equivalent reducer
187
- // action should produce the same bounded state.
188
- const stateWithImage = {
189
- ...DEFAULT_STATE,
190
- image: {
191
- src: 'test.jpg',
192
- naturalWidth: 1600,
193
- naturalHeight: 900,
194
- },
195
- };
196
-
197
- it( 'rotate op matches SET_ROTATION (absolute angle)', () => {
198
- const pipelineResult = applyOperationToState( stateWithImage, {
199
- type: 'rotate',
200
- degrees: 30,
201
- } );
202
- // Pipeline op is additive, reducer action is absolute, so use
203
- // the resolved absolute angle in the reducer call.
204
- const reducerResult = cropperReducer( stateWithImage, {
205
- type: 'SET_ROTATION',
206
- payload: 30,
207
- } );
208
- expect( pipelineResult.rotation ).toBe( reducerResult.rotation );
209
- expect( pipelineResult.zoom ).toBe( reducerResult.zoom );
210
- expect( pipelineResult.pan.x ).toBeCloseTo( reducerResult.pan.x, 6 );
211
- expect( pipelineResult.pan.y ).toBeCloseTo( reducerResult.pan.y, 6 );
212
- } );
213
-
214
- it( 'zoom op matches SET_ZOOM', () => {
215
- const pipelineResult = applyOperationToState( stateWithImage, {
216
- type: 'zoom',
217
- factor: 3,
218
- } );
219
- const reducerResult = cropperReducer( stateWithImage, {
220
- type: 'SET_ZOOM',
221
- payload: 3,
222
- } );
223
- expect( pipelineResult.zoom ).toBe( reducerResult.zoom );
224
- expect( pipelineResult.baseZoom ).toBe( reducerResult.baseZoom );
225
- } );
226
-
227
- it( 'flip op matches SET_FLIP', () => {
228
- const pipelineResult = applyOperationToState( stateWithImage, {
229
- type: 'flip',
230
- direction: 'horizontal',
231
- } );
232
- const reducerResult = cropperReducer( stateWithImage, {
233
- type: 'SET_FLIP',
234
- payload: { horizontal: true, vertical: false },
235
- } );
236
- expect( pipelineResult.flip ).toEqual( reducerResult.flip );
237
- } );
238
-
239
- it( 'crop op matches SET_CROP_RECT', () => {
240
- const rect = { x: 0.1, y: 0.1, width: 0.4, height: 0.4 };
241
- const pipelineResult = applyOperationToState( stateWithImage, {
242
- type: 'crop',
243
- rect,
244
- } );
245
- const reducerResult = cropperReducer( stateWithImage, {
246
- type: 'SET_CROP_RECT',
247
- payload: rect,
248
- } );
249
- expect( pipelineResult.cropRect ).toEqual( reducerResult.cropRect );
250
- expect( pipelineResult.zoom ).toBe( reducerResult.zoom );
251
- } );
252
-
253
- it( 'rotate op: degrees means visual CW, negated on single-axis flip', () => {
254
- // `{ type: 'rotate', degrees: 90 }` means "90° clockwise visually".
255
- // On a single-flipped image the rotation field must go the other
256
- // way to achieve that visual outcome — matching snapRotate90.
257
- const flippedState = {
258
- ...stateWithImage,
259
- flip: { horizontal: true, vertical: false },
260
- };
261
- const result = applyOperationToState( flippedState, {
262
- type: 'rotate',
263
- degrees: 90,
264
- } );
265
- expect( result.rotation ).toBe( 270 );
266
- } );
267
- } );
@@ -1,438 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest';
2
- import { userEvent } from 'vitest/browser';
3
- import { screen, fireEvent, act } from '@testing-library/react';
4
- import { render } from 'vitest-browser-react';
5
- import { RectangleStencil } from '../rectangle-stencil';
6
- import type {
7
- HandlePosition,
8
- NormalizedRect,
9
- Size,
10
- } from '../../../../core/types';
11
- import { DEFAULT_KEYBOARD_STEP } from '../../../../core/constants';
12
-
13
- const DEFAULT_CROP_RECT: NormalizedRect = {
14
- x: 0.1,
15
- y: 0.1,
16
- width: 0.8,
17
- height: 0.8,
18
- };
19
- const CONTAINER_SIZE: Size = { width: 600, height: 400 };
20
- const IMAGE_SIZE: Size = { width: 500, height: 300 };
21
- const CROP_BOUNDS = { minX: 0, minY: 0, maxX: 1, maxY: 1 };
22
-
23
- /**
24
- * Render a RectangleStencil in freeform mode with sensible defaults.
25
- * Returns the mock callbacks; use `screen` for DOM queries.
26
- *
27
- * @param overrides Props to override on the rendered stencil.
28
- */
29
- async function renderStencil(
30
- overrides: Partial< React.ComponentProps< typeof RectangleStencil > > = {}
31
- ) {
32
- const onCropChange = vi.fn();
33
- const onResizeStart = vi.fn();
34
- const onResizeEnd = vi.fn();
35
- const onEscape = vi.fn();
36
- const props = {
37
- cropRect: DEFAULT_CROP_RECT,
38
- containerSize: CONTAINER_SIZE,
39
- imageSize: IMAGE_SIZE,
40
- onCropChange,
41
- onResizeStart,
42
- onResizeEnd,
43
- onEscape,
44
- freeformCrop: true,
45
- cropBounds: CROP_BOUNDS,
46
- ...overrides,
47
- };
48
-
49
- const utils = await render( <RectangleStencil { ...props } /> );
50
-
51
- return {
52
- ...utils,
53
- props,
54
- onCropChange,
55
- onResizeStart,
56
- onResizeEnd,
57
- onEscape,
58
- };
59
- }
60
-
61
- describe( 'RectangleStencil', () => {
62
- describe( 'tab order', () => {
63
- it( 'renders handles clockwise from top-left in freeform mode', async () => {
64
- await renderStencil();
65
- const labels = screen
66
- .getAllByRole( 'button' )
67
- .map( ( b ) => b.getAttribute( 'aria-label' ) );
68
-
69
- expect( labels ).toEqual( [
70
- 'Resize from top-left corner',
71
- 'Resize from top edge',
72
- 'Resize from top-right corner',
73
- 'Resize from right edge',
74
- 'Resize from bottom-right corner',
75
- 'Resize from bottom edge',
76
- 'Resize from bottom-left corner',
77
- 'Resize from left edge',
78
- ] );
79
- } );
80
-
81
- it( 'describes keyboard resizing on resize handles', async () => {
82
- await renderStencil();
83
- expect(
84
- screen.getByRole( 'button', {
85
- name: 'Resize from top-left corner',
86
- } )
87
- ).toHaveAccessibleDescription(
88
- 'Use arrow keys to resize the crop area. Hold Shift for larger steps.'
89
- );
90
- } );
91
-
92
- it( 'renders corner handles clockwise from top-left when aspect ratio is locked', async () => {
93
- await renderStencil( { aspectRatio: 16 / 9 } );
94
- const labels = screen
95
- .getAllByRole( 'button' )
96
- .map( ( b ) => b.getAttribute( 'aria-label' ) );
97
-
98
- expect( labels ).toEqual( [
99
- 'Resize from top-left corner',
100
- 'Resize from top-right corner',
101
- 'Resize from bottom-right corner',
102
- 'Resize from bottom-left corner',
103
- ] );
104
- } );
105
- } );
106
-
107
- describe( 'keyboard — Escape', () => {
108
- it( 'handles Escape on a handle without bubbling', async () => {
109
- const onKeyDown = vi.fn();
110
- const onEscape = vi.fn();
111
- await render(
112
- // eslint-disable-next-line jsx-a11y/no-static-element-interactions
113
- <div onKeyDown={ onKeyDown }>
114
- <RectangleStencil
115
- cropRect={ DEFAULT_CROP_RECT }
116
- containerSize={ CONTAINER_SIZE }
117
- imageSize={ IMAGE_SIZE }
118
- onCropChange={ vi.fn() }
119
- onEscape={ onEscape }
120
- freeformCrop
121
- cropBounds={ CROP_BOUNDS }
122
- />
123
- </div>
124
- );
125
- const [ firstHandle ] = screen.getAllByRole( 'button' );
126
-
127
- firstHandle.focus();
128
- await userEvent.keyboard( '{Escape}' );
129
-
130
- expect( onEscape ).toHaveBeenCalledTimes( 1 );
131
- expect( onKeyDown ).not.toHaveBeenCalled();
132
- } );
133
-
134
- it( 'does not call onCropChange when Escape is pressed', async () => {
135
- const { onCropChange, onEscape } = await renderStencil();
136
- const [ firstHandle ] = screen.getAllByRole( 'button' );
137
-
138
- firstHandle.focus();
139
- await userEvent.keyboard( '{Escape}' );
140
-
141
- expect( onEscape ).toHaveBeenCalledTimes( 1 );
142
- expect( onCropChange ).not.toHaveBeenCalled();
143
- } );
144
- } );
145
-
146
- describe( 'keyboard — arrow keys (fine step)', () => {
147
- it( 'calls onResizeStart once and onResizeEnd after keyboard resize settles', async () => {
148
- vi.useFakeTimers();
149
- const { onResizeStart, onResizeEnd } = await renderStencil();
150
- const eHandle = screen.getAllByRole( 'button' )[ 3 ];
151
-
152
- fireEvent.keyDown( eHandle, { key: 'ArrowRight' } );
153
- fireEvent.keyDown( eHandle, { key: 'ArrowRight' } );
154
-
155
- expect( onResizeStart ).toHaveBeenCalledTimes( 1 );
156
- expect( onResizeEnd ).not.toHaveBeenCalled();
157
-
158
- act( () => {
159
- vi.advanceTimersByTime( 500 );
160
- } );
161
-
162
- expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
163
- vi.useRealTimers();
164
- } );
165
-
166
- it( 'closes a pending keyboard resize when resizing becomes disabled', async () => {
167
- vi.useFakeTimers();
168
- const { props, rerender, onResizeStart, onResizeEnd } =
169
- await renderStencil();
170
- const eHandle = screen.getAllByRole( 'button' )[ 3 ];
171
-
172
- fireEvent.keyDown( eHandle, { key: 'ArrowRight' } );
173
- expect( onResizeStart ).toHaveBeenCalledTimes( 1 );
174
- expect( onResizeEnd ).not.toHaveBeenCalled();
175
-
176
- await rerender(
177
- <RectangleStencil { ...props } isResizeDisabled />
178
- );
179
-
180
- // Closed straight away, not left to the settle timer.
181
- expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
182
-
183
- // And the cancelled timer must not fire a second one.
184
- act( () => {
185
- vi.advanceTimersByTime( 500 );
186
- } );
187
- expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
188
- vi.useRealTimers();
189
- } );
190
-
191
- it( 'moves the right edge right by KEYBOARD_STEP on ArrowRight (no Shift)', async () => {
192
- const { onCropChange } = await renderStencil();
193
- // 'e' handle is the 4th button in clockwise order (nw, n, ne, e).
194
- const eHandle = screen.getAllByRole( 'button' )[ 3 ];
195
-
196
- eHandle.focus();
197
- await userEvent.keyboard( '{ArrowRight}' );
198
-
199
- expect( onCropChange ).toHaveBeenCalledTimes( 1 );
200
- const newRect: NormalizedRect = onCropChange.mock.calls[ 0 ][ 0 ];
201
- // dx = 0.01, edgeRight = 0.1 + 0.8 + 0.01 = 0.91, width = 0.81.
202
- expect( newRect.width ).toBeCloseTo( 0.81, 5 );
203
- expect( newRect.x ).toBeCloseTo( 0.1, 5 );
204
- } );
205
-
206
- it( 'moves the bottom edge down by KEYBOARD_STEP on ArrowDown (no Shift)', async () => {
207
- const { onCropChange } = await renderStencil();
208
- // 's' handle is the 6th button (nw, n, ne, e, se, s).
209
- const sHandle = screen.getAllByRole( 'button' )[ 5 ];
210
-
211
- sHandle.focus();
212
- await userEvent.keyboard( '{ArrowDown}' );
213
-
214
- expect( onCropChange ).toHaveBeenCalledTimes( 1 );
215
- const newRect: NormalizedRect = onCropChange.mock.calls[ 0 ][ 0 ];
216
- // dy = 0.01, edgeBottom = 0.1 + 0.8 + 0.01 = 0.91, height = 0.81.
217
- expect( newRect.height ).toBeCloseTo( 0.81, 5 );
218
- expect( newRect.y ).toBeCloseTo( 0.1, 5 );
219
- } );
220
-
221
- it( 'applies snapCropRect to freeform resize output', async () => {
222
- const snapCropRect = vi.fn<
223
- (
224
- rect: NormalizedRect,
225
- handle: HandlePosition
226
- ) => NormalizedRect
227
- >( ( rect ) => ( {
228
- ...rect,
229
- width: 0.82,
230
- } ) );
231
- const { onCropChange } = await renderStencil( { snapCropRect } );
232
- const eHandle = screen.getAllByRole( 'button' )[ 3 ];
233
-
234
- eHandle.focus();
235
- await userEvent.keyboard( '{ArrowRight}' );
236
-
237
- expect( snapCropRect ).toHaveBeenCalledTimes( 1 );
238
- expect( snapCropRect.mock.calls[ 0 ][ 0 ].width ).toBeCloseTo(
239
- 0.81,
240
- 5
241
- );
242
- expect( snapCropRect.mock.calls[ 0 ][ 1 ] ).toBe( 'e' );
243
- expect( onCropChange.mock.calls[ 0 ][ 0 ].width ).toBeCloseTo(
244
- 0.82,
245
- 5
246
- );
247
- } );
248
-
249
- it( 'shrinks a locked-ratio crop from a corner handle', async () => {
250
- const { onCropChange } = await renderStencil( {
251
- aspectRatio: 1,
252
- containerSize: { width: 500, height: 500 },
253
- imageSize: { width: 500, height: 500 },
254
- } );
255
- const nwHandle = screen.getByRole( 'button', {
256
- name: 'Resize from top-left corner',
257
- } );
258
-
259
- nwHandle.focus();
260
- await userEvent.keyboard( '{ArrowRight}' );
261
-
262
- expect( onCropChange ).toHaveBeenCalledTimes( 1 );
263
- const newRect: NormalizedRect = onCropChange.mock.calls[ 0 ][ 0 ];
264
- expect( newRect.x ).toBeCloseTo(
265
- DEFAULT_CROP_RECT.x + DEFAULT_KEYBOARD_STEP,
266
- 5
267
- );
268
- expect( newRect.y ).toBeCloseTo(
269
- DEFAULT_CROP_RECT.y + DEFAULT_KEYBOARD_STEP,
270
- 5
271
- );
272
- expect( newRect.width ).toBeCloseTo(
273
- DEFAULT_CROP_RECT.width - DEFAULT_KEYBOARD_STEP,
274
- 5
275
- );
276
- expect( newRect.height ).toBeCloseTo(
277
- DEFAULT_CROP_RECT.height - DEFAULT_KEYBOARD_STEP,
278
- 5
279
- );
280
- } );
281
- } );
282
-
283
- describe( 'keyboard — arrow keys (coarse step with Shift)', () => {
284
- it( 'moves the right edge right by KEYBOARD_STEP_SHIFT on Shift+ArrowRight', async () => {
285
- const { onCropChange } = await renderStencil();
286
- const eHandle = screen.getAllByRole( 'button' )[ 3 ];
287
-
288
- eHandle.focus();
289
- await userEvent.keyboard( '{Shift>}{ArrowRight}{/Shift}' );
290
-
291
- expect( onCropChange ).toHaveBeenCalledTimes( 1 );
292
- const newRect: NormalizedRect = onCropChange.mock.calls[ 0 ][ 0 ];
293
- // dx = 0.1, edgeRight = 0.1 + 0.8 + 0.1 = 1.0 (clamped to boundsMaxX=1), width = 0.9.
294
- expect( newRect.width ).toBeCloseTo( 0.9, 5 );
295
- } );
296
-
297
- it( 'step is 10x larger with Shift than without', async () => {
298
- const { onCropChange } = await renderStencil();
299
- const eHandle = screen.getAllByRole( 'button' )[ 3 ];
300
-
301
- eHandle.focus();
302
- await userEvent.keyboard( '{ArrowRight}' );
303
- const fineRect: NormalizedRect = onCropChange.mock.calls[ 0 ][ 0 ];
304
- const fineDelta = fineRect.width - DEFAULT_CROP_RECT.width;
305
-
306
- onCropChange.mockClear();
307
-
308
- await userEvent.keyboard( '{Shift>}{ArrowRight}{/Shift}' );
309
- const coarseRect: NormalizedRect =
310
- onCropChange.mock.calls[ 0 ][ 0 ];
311
- const coarseDelta = coarseRect.width - DEFAULT_CROP_RECT.width;
312
-
313
- expect( coarseDelta / fineDelta ).toBeCloseTo( 10, 0 );
314
- } );
315
-
316
- it( 'does not apply snapCropRect while resizing a locked aspect ratio', async () => {
317
- const snapCropRect = vi.fn( ( rect: NormalizedRect ) => rect );
318
- await renderStencil( { aspectRatio: 1, snapCropRect } );
319
- const nwHandle = screen.getByRole( 'button', {
320
- name: 'Resize from top-left corner',
321
- } );
322
-
323
- nwHandle.focus();
324
- await userEvent.keyboard( '{ArrowRight}' );
325
-
326
- expect( snapCropRect ).not.toHaveBeenCalled();
327
- } );
328
- } );
329
-
330
- describe( 'pointer drag — focus after release', () => {
331
- it( 'calls onResizeStart on pointerdown and onResizeEnd on pointerup', async () => {
332
- const { onResizeStart, onResizeEnd } = await renderStencil();
333
- const [ firstHandle ] = screen.getAllByRole( 'button' );
334
-
335
- fireEvent.pointerDown( firstHandle, {
336
- button: 0,
337
- clientX: 100,
338
- clientY: 100,
339
- pointerId: 1,
340
- } );
341
-
342
- expect( onResizeStart ).toHaveBeenCalledTimes( 1 );
343
- expect( onResizeEnd ).not.toHaveBeenCalled();
344
-
345
- fireEvent.pointerUp( firstHandle, { pointerId: 1 } );
346
-
347
- expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
348
- } );
349
-
350
- it( 'focuses the handle button after a pointer drag ends', async () => {
351
- await renderStencil();
352
- const [ firstHandle ] = screen.getAllByRole( 'button' );
353
-
354
- vi.spyOn( firstHandle, 'focus' );
355
-
356
- await userEvent.dragAndDrop( firstHandle, firstHandle );
357
-
358
- expect( firstHandle.focus ).toHaveBeenCalled();
359
- expect( firstHandle ).toHaveFocus();
360
- } );
361
-
362
- it( 'does not start a pointer resize while resizing is disabled', async () => {
363
- const { onResizeStart } = await renderStencil( {
364
- isResizeDisabled: true,
365
- } );
366
- const [ firstHandle ] = screen.getAllByRole( 'button' );
367
-
368
- fireEvent.pointerDown( firstHandle, {
369
- button: 0,
370
- clientX: 100,
371
- clientY: 100,
372
- pointerId: 1,
373
- } );
374
-
375
- expect( onResizeStart ).not.toHaveBeenCalled();
376
- } );
377
-
378
- it( 'cancels an active pointer resize when resizing becomes disabled', async () => {
379
- const { props, rerender, onResizeStart, onResizeEnd } =
380
- await renderStencil();
381
- const [ firstHandle ] = screen.getAllByRole( 'button' );
382
-
383
- fireEvent.pointerDown( firstHandle, {
384
- button: 0,
385
- clientX: 100,
386
- clientY: 100,
387
- pointerId: 1,
388
- } );
389
-
390
- expect( onResizeStart ).toHaveBeenCalledTimes( 1 );
391
- expect( onResizeEnd ).not.toHaveBeenCalled();
392
-
393
- await rerender(
394
- <RectangleStencil { ...props } isResizeDisabled />
395
- );
396
-
397
- expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
398
-
399
- fireEvent.pointerUp( firstHandle, { pointerId: 1 } );
400
-
401
- expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
402
- } );
403
-
404
- it( 'only stops touchstart propagation for single-touch handle gestures', async () => {
405
- const onTouchStart = vi.fn();
406
- await render(
407
- <div onTouchStart={ onTouchStart }>
408
- <RectangleStencil
409
- cropRect={ DEFAULT_CROP_RECT }
410
- containerSize={ CONTAINER_SIZE }
411
- imageSize={ IMAGE_SIZE }
412
- onCropChange={ vi.fn() }
413
- freeformCrop
414
- cropBounds={ CROP_BOUNDS }
415
- />
416
- </div>
417
- );
418
- const [ firstHandle ] = screen.getAllByRole( 'button' );
419
- const createTouch = ( identifier: number, clientX: number ) =>
420
- new Touch( {
421
- identifier,
422
- target: firstHandle,
423
- clientX,
424
- clientY: 100,
425
- } );
426
-
427
- fireEvent.touchStart( firstHandle, {
428
- touches: [ createTouch( 1, 100 ) ],
429
- } );
430
- expect( onTouchStart ).not.toHaveBeenCalled();
431
-
432
- fireEvent.touchStart( firstHandle, {
433
- touches: [ createTouch( 1, 100 ), createTouch( 2, 160 ) ],
434
- } );
435
- expect( onTouchStart ).toHaveBeenCalledTimes( 1 );
436
- } );
437
- } );
438
- } );