@wordpress/media-editor 0.20.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 (116) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +2 -1
  3. package/build/components/media-editor/index.cjs +7 -16
  4. package/build/components/media-editor/index.cjs.map +3 -3
  5. package/build/components/media-editor/use-crop-options.cjs +1 -2
  6. package/build/components/media-editor/use-crop-options.cjs.map +2 -2
  7. package/build/components/media-editor-canvas/index.cjs.map +3 -3
  8. package/build/components/media-editor-crop-panel/index.cjs +12 -8
  9. package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
  10. package/build/components/media-editor-modal/index.cjs +2 -0
  11. package/build/components/media-editor-modal/index.cjs.map +2 -2
  12. package/build/components/media-editor-provider/index.cjs +2 -1
  13. package/build/components/media-editor-provider/index.cjs.map +2 -2
  14. package/build/components/media-form/index.cjs +28 -28
  15. package/build/components/media-form/index.cjs.map +3 -3
  16. package/build/components/media-preview/index.cjs.map +3 -3
  17. package/build/image-editor/react/components/cropper.cjs +1 -11
  18. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  19. package/build/state/composite-reducer.cjs +33 -8
  20. package/build/state/composite-reducer.cjs.map +3 -3
  21. package/build/state/index.cjs.map +1 -1
  22. package/build/state/types.cjs.map +1 -1
  23. package/build/state/use-media-editor-state.cjs +20 -7
  24. package/build/state/use-media-editor-state.cjs.map +2 -2
  25. package/build-module/components/media-editor/index.mjs +9 -18
  26. package/build-module/components/media-editor/index.mjs.map +2 -2
  27. package/build-module/components/media-editor/use-crop-options.mjs +1 -2
  28. package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
  29. package/build-module/components/media-editor-canvas/index.mjs +2 -2
  30. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  31. package/build-module/components/media-editor-crop-panel/index.mjs +13 -9
  32. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  33. package/build-module/components/media-editor-modal/index.mjs +2 -0
  34. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  35. package/build-module/components/media-editor-provider/index.mjs +2 -1
  36. package/build-module/components/media-editor-provider/index.mjs.map +2 -2
  37. package/build-module/components/media-form/index.mjs +33 -30
  38. package/build-module/components/media-form/index.mjs.map +2 -2
  39. package/build-module/components/media-preview/index.mjs +2 -2
  40. package/build-module/components/media-preview/index.mjs.map +2 -2
  41. package/build-module/image-editor/react/components/cropper.mjs +1 -11
  42. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  43. package/build-module/state/composite-reducer.mjs +23 -8
  44. package/build-module/state/composite-reducer.mjs.map +2 -2
  45. package/build-module/state/index.mjs.map +1 -1
  46. package/build-module/state/types.mjs.map +1 -1
  47. package/build-module/state/use-media-editor-state.mjs +20 -7
  48. package/build-module/state/use-media-editor-state.mjs.map +2 -2
  49. package/build-types/components/media-editor/index.d.ts +2 -1
  50. package/build-types/components/media-editor/index.d.ts.map +1 -1
  51. package/build-types/components/media-editor/use-crop-options.d.ts +0 -1
  52. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
  53. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  54. package/build-types/components/media-editor-modal/index.d.ts +7 -2
  55. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  56. package/build-types/components/media-editor-provider/index.d.ts +7 -1
  57. package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
  58. package/build-types/components/media-form/index.d.ts +5 -2
  59. package/build-types/components/media-form/index.d.ts.map +1 -1
  60. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  61. package/build-types/state/composite-reducer.d.ts +1 -1
  62. package/build-types/state/composite-reducer.d.ts.map +1 -1
  63. package/build-types/state/index.d.ts +1 -1
  64. package/build-types/state/index.d.ts.map +1 -1
  65. package/build-types/state/types.d.ts +34 -5
  66. package/build-types/state/types.d.ts.map +1 -1
  67. package/build-types/state/use-media-editor-state.d.ts +16 -5
  68. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  69. package/package.json +33 -22
  70. package/src/components/media-editor/index.tsx +12 -22
  71. package/src/components/media-editor/use-crop-options.ts +0 -2
  72. package/src/components/media-editor-canvas/index.tsx +2 -2
  73. package/src/components/media-editor-crop-panel/index.tsx +17 -9
  74. package/src/components/media-editor-image-controls/style.scss +1 -1
  75. package/src/components/media-editor-modal/index.tsx +8 -1
  76. package/src/components/media-editor-provider/index.tsx +8 -1
  77. package/src/components/media-form/index.tsx +48 -42
  78. package/src/components/media-preview/index.tsx +2 -2
  79. package/src/image-editor/docs/architecture.md +4 -0
  80. package/src/image-editor/react/components/cropper.tsx +1 -15
  81. package/src/state/composite-reducer.ts +24 -11
  82. package/src/state/index.ts +2 -0
  83. package/src/state/types.ts +37 -5
  84. package/src/state/use-media-editor-state.ts +46 -11
  85. package/global.d.ts +0 -5
  86. package/src/components/media-editor/test/restore-original.browser.test.tsx +0 -227
  87. package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -106
  88. package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -72
  89. package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -186
  90. package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -359
  91. package/src/components/media-editor-modal/test/index.jsdom.test.tsx +0 -182
  92. package/src/components/rotation-ruler/stories/index.story.tsx +0 -43
  93. package/src/components/rotation-ruler/test/index.jsdom.test.tsx +0 -140
  94. package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +0 -823
  95. package/src/image-editor/core/math/test/sanitize.ts +0 -242
  96. package/src/image-editor/core/test/camera.ts +0 -1218
  97. package/src/image-editor/core/test/interaction-controller.ts +0 -1299
  98. package/src/image-editor/core/test/preview-export-parity.ts +0 -249
  99. package/src/image-editor/core/test/render-camera-bridge.ts +0 -349
  100. package/src/image-editor/core/test/state.ts +0 -876
  101. package/src/image-editor/core/test/stencil-math.ts +0 -665
  102. package/src/image-editor/core/transforms/test/pipeline.ts +0 -267
  103. package/src/image-editor/react/components/stencils/test/rectangle-stencil.browser.test.tsx +0 -438
  104. package/src/image-editor/react/components/test/cropper.browser.test.tsx +0 -1462
  105. package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +0 -491
  106. package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +0 -1049
  107. package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +0 -213
  108. package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +0 -140
  109. package/src/image-editor/stories/rectangle-crop.story.tsx +0 -1073
  110. package/src/image-editor/stories/style.module.css +0 -65
  111. package/src/state/test/use-media-editor-state.browser.test.tsx +0 -79
  112. package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -386
  113. package/src/store/test/reducer.ts +0 -64
  114. package/tsconfig.build.json +0 -26
  115. package/tsconfig.build.tsbuildinfo +0 -1
  116. package/tsconfig.json +0 -37
@@ -1,65 +0,0 @@
1
- .container {
2
- width: 100%;
3
- max-width: 600px;
4
- aspect-ratio: 3 / 2;
5
- border: 1px solid #ccc;
6
- position: relative;
7
- }
8
-
9
- .resizable {
10
- width: 600px;
11
- height: 400px;
12
- max-width: 100%;
13
- min-width: 200px;
14
- min-height: 200px;
15
- border: 1px solid #ccc;
16
- resize: both;
17
- overflow: hidden;
18
- position: relative;
19
- }
20
-
21
- .toolbar {
22
- display: flex;
23
- flex-direction: column;
24
- gap: 12px;
25
- padding: 8px;
26
- margin-bottom: 12px;
27
- border: 1px solid #e0e0e0;
28
- border-radius: 4px;
29
- background: #fafafa;
30
- }
31
-
32
- .sliders {
33
- display: grid;
34
- grid-template-columns: 1fr 1fr;
35
- gap: 16px;
36
- padding: 0 4px;
37
- }
38
-
39
- .hidden-file {
40
- display: none;
41
- }
42
-
43
- .state {
44
- font-family: monospace;
45
- font-size: 11px;
46
- background: #f5f5f5;
47
- padding: 8px;
48
- border-radius: 4px;
49
- white-space: pre-wrap;
50
- max-height: 200px;
51
- overflow: auto;
52
- }
53
-
54
- .export-preview {
55
- margin-bottom: 16px;
56
- max-width: 200px;
57
- }
58
-
59
- .export-image {
60
- display: block;
61
- max-width: 100%;
62
- border: 1px solid #ccc;
63
- border-radius: 4px;
64
- background: repeating-conic-gradient(#e0e0e0 0% 25%, transparent 0% 50%) 50% / 16px 16px;
65
- }
@@ -1,79 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { userEvent } from 'vitest/browser';
3
- import { act, screen, waitFor } from '@testing-library/react';
4
- import { render } from 'vitest-browser-react';
5
- import { useEffect } from '@wordpress/element';
6
- import { Cropper } from '../../image-editor';
7
- import {
8
- useMediaEditorState,
9
- type MediaEditorSession,
10
- } from '../use-media-editor-state';
11
- // Browser Mode needs the package's real styles for layout and transitions.
12
- import '../../image-editor/style.scss';
13
-
14
- // Shown below 1:1 in the fixture, so pixel snapping stays off until a crop
15
- // magnifies the image past one screen pixel per source pixel.
16
- const IMAGE = {
17
- src: 'large.png',
18
- naturalWidth: 999,
19
- naturalHeight: 999,
20
- };
21
-
22
- function CropperWithHistory( {
23
- onController,
24
- }: {
25
- onController: ( controller: MediaEditorSession ) => void;
26
- } ) {
27
- const controller = useMediaEditorState();
28
- const { setSourceImage } = controller;
29
- useEffect( () => setSourceImage( IMAGE ), [ setSourceImage ] );
30
- useEffect( () => onController( controller ), [ controller, onController ] );
31
- return (
32
- <div style={ { width: 648, height: 448 } }>
33
- <Cropper
34
- src={ IMAGE.src }
35
- controller={ controller.cropper }
36
- freeformCrop
37
- onGestureStart={ controller.beginGesture }
38
- onGestureEnd={ controller.endGesture }
39
- />
40
- </div>
41
- );
42
- }
43
-
44
- describe( 'useMediaEditorState with a Cropper', () => {
45
- it( 'keeps redo steps after redoing a handle crop that turns on pixel snapping', async () => {
46
- let controller!: MediaEditorSession;
47
- const onController = ( next: MediaEditorSession ) => {
48
- controller = next;
49
- };
50
- await render( <CropperWithHistory onController={ onController } /> );
51
- const handle = await screen.findByRole( 'button', {
52
- name: 'Resize from bottom-right corner',
53
- } );
54
-
55
- // Shrink the crop to 30% in steps of 10% of a 999px image, so its
56
- // edges fall between source pixels, then wait for it to settle.
57
- handle.focus();
58
- await userEvent.keyboard(
59
- '{Shift>}' +
60
- '{ArrowLeft}'.repeat( 7 ) +
61
- '{ArrowUp}'.repeat( 7 ) +
62
- '{/Shift}'
63
- );
64
- await waitFor( () =>
65
- expect( controller.cropper.state.zoom ).toBeGreaterThan( 1 )
66
- );
67
-
68
- act( () =>
69
- controller.cropper.setFlip( { horizontal: true, vertical: false } )
70
- );
71
-
72
- for ( let i = 0; i < 10 && controller.hasUndo; i++ ) {
73
- act( () => controller.undo() );
74
- }
75
- act( () => controller.redo() );
76
-
77
- expect( controller.hasRedo ).toBe( true );
78
- } );
79
- } );
@@ -1,386 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { act, renderHook } from '@testing-library/react';
3
- import { useMediaEditorState } from '../use-media-editor-state';
4
-
5
- const IMAGE = {
6
- src: 'test.jpg',
7
- naturalWidth: 1200,
8
- naturalHeight: 600,
9
- };
10
-
11
- // Loading the image through the session, as the canvas does, so the
12
- // baseline and history start from the loaded image.
13
- function setupHook( visualSize = { width: 600, height: 400 } ) {
14
- const view = renderHook( () => useMediaEditorState() );
15
- act( () => {
16
- view.result.current.setSourceImage( IMAGE );
17
- view.result.current.cropper.setVisualSize( visualSize );
18
- } );
19
- return view;
20
- }
21
-
22
- describe( 'useMediaEditorState', () => {
23
- describe( 'initial state', () => {
24
- it( 'starts with default cropOptions', () => {
25
- const { result } = renderHook( () => useMediaEditorState() );
26
-
27
- expect( result.current.cropOptions.aspectRatioValue ).toBe( '0' );
28
- } );
29
-
30
- it( 'starts with isDirty=false and no undo / redo', () => {
31
- const { result } = setupHook();
32
-
33
- expect( result.current.isDirty ).toBe( false );
34
- expect( result.current.cropper.isDirty ).toBe( false );
35
- expect( result.current.hasUndo ).toBe( false );
36
- expect( result.current.hasRedo ).toBe( false );
37
- } );
38
- } );
39
-
40
- describe( 'session edits versus image output edits', () => {
41
- it( 'counts an aspect-ratio preset change as a session edit only', () => {
42
- // No visual size is reported, so picking a ratio changes the
43
- // preset without reshaping the crop.
44
- const { result } = renderHook( () => useMediaEditorState() );
45
- act( () => result.current.setSourceImage( IMAGE ) );
46
-
47
- act( () => result.current.setAspectRatioValue( '1' ) );
48
-
49
- expect( result.current.isDirty ).toBe( true );
50
- expect( result.current.cropper.isDirty ).toBe( false );
51
- expect( result.current.hasOutputEdits ).toBe( false );
52
- } );
53
-
54
- it( 'counts a geometry change as an image output edit', () => {
55
- const { result } = setupHook();
56
-
57
- act( () => result.current.cropper.setRotation( 45 ) );
58
-
59
- expect( result.current.isDirty ).toBe( true );
60
- expect( result.current.cropper.isDirty ).toBe( true );
61
- expect( result.current.hasOutputEdits ).toBe( true );
62
- } );
63
- } );
64
-
65
- describe( 'history — undo / redo across slices', () => {
66
- it( 'records the pre-state of each action and undoes it', () => {
67
- const { result } = setupHook();
68
-
69
- act( () => result.current.cropper.setRotation( 45 ) );
70
-
71
- expect( result.current.cropper.state.rotation ).toBe( 45 );
72
- expect( result.current.hasUndo ).toBe( true );
73
-
74
- act( () => result.current.undo() );
75
-
76
- expect( result.current.cropper.state.rotation ).toBe( 0 );
77
- expect( result.current.hasUndo ).toBe( false );
78
- expect( result.current.hasRedo ).toBe( true );
79
- } );
80
-
81
- it( 'redo re-applies an undone action', () => {
82
- const { result } = setupHook();
83
-
84
- act( () => result.current.cropper.setRotation( 45 ) );
85
- act( () => result.current.undo() );
86
- act( () => result.current.redo() );
87
-
88
- expect( result.current.cropper.state.rotation ).toBe( 45 );
89
- } );
90
-
91
- it( 'redo stack clears on a new action after undo', () => {
92
- const { result } = setupHook();
93
-
94
- act( () => result.current.cropper.setRotation( 45 ) );
95
- act( () => result.current.undo() );
96
- act( () => result.current.cropper.setRotation( 90 ) );
97
-
98
- expect( result.current.hasRedo ).toBe( false );
99
- } );
100
-
101
- it( 'no-op actions do not record history entries', () => {
102
- const { result } = setupHook();
103
-
104
- act( () => result.current.setAspectRatioValue( '0' ) );
105
-
106
- expect( result.current.hasUndo ).toBe( false );
107
- } );
108
-
109
- it( 'a single undo restores both cropOptions and cropRect after aspect-ratio change', () => {
110
- const { result } = setupHook();
111
- const initialCropRect = result.current.cropper.state.cropRect;
112
- const initialPreset = result.current.cropOptions.aspectRatioValue;
113
-
114
- act( () => result.current.setAspectRatioValue( '1' ) ); // Square
115
-
116
- // Both slices changed atomically: preset key and cropRect.
117
- expect( result.current.cropOptions.aspectRatioValue ).toBe( '1' );
118
- expect( result.current.cropper.state.cropRect ).not.toEqual(
119
- initialCropRect
120
- );
121
- expect( result.current.cropper.isDirty ).toBe( true );
122
- // Only ONE undo entry — atomic transaction.
123
- expect( result.current.hasUndo ).toBe( true );
124
-
125
- act( () => result.current.undo() );
126
-
127
- expect( result.current.cropOptions.aspectRatioValue ).toBe(
128
- initialPreset
129
- );
130
- expect( result.current.cropper.state.cropRect ).toEqual(
131
- initialCropRect
132
- );
133
- } );
134
- } );
135
-
136
- describe( 'gestures — coalesce many changes into one undo entry', () => {
137
- it( 'coalesces all changes between beginGesture and endGesture', () => {
138
- const { result } = setupHook();
139
-
140
- act( () => result.current.beginGesture() );
141
- act( () => result.current.cropper.setRotation( 10 ) );
142
- act( () => result.current.cropper.setRotation( 20 ) );
143
- act( () => result.current.cropper.setRotation( 30 ) );
144
- act( () => result.current.endGesture() );
145
-
146
- expect( result.current.cropper.state.rotation ).toBe( 30 );
147
- expect( result.current.hasUndo ).toBe( true );
148
-
149
- // One undo restores all the way to the pre-gesture state.
150
- act( () => result.current.undo() );
151
- expect( result.current.cropper.state.rotation ).toBe( 0 );
152
- } );
153
-
154
- it( 'an empty gesture (no changes) records no history', () => {
155
- const { result } = setupHook();
156
-
157
- act( () => result.current.beginGesture() );
158
- act( () => result.current.endGesture() );
159
-
160
- expect( result.current.hasUndo ).toBe( false );
161
- } );
162
-
163
- it( 'undo during an open gesture flushes the gesture first', () => {
164
- const { result } = setupHook();
165
-
166
- act( () => result.current.beginGesture() );
167
- act( () => result.current.cropper.setRotation( 45 ) );
168
- // Undo without endGesture should still flush the in-flight change.
169
- act( () => result.current.undo() );
170
-
171
- expect( result.current.cropper.state.rotation ).toBe( 0 );
172
- expect( result.current.hasRedo ).toBe( true );
173
- } );
174
-
175
- it( 'keeps the original gesture snapshot if beginGesture is called repeatedly', () => {
176
- const { result } = setupHook();
177
-
178
- act( () => {
179
- result.current.beginGesture();
180
- result.current.cropper.setRotation( 10 );
181
- result.current.beginGesture();
182
- result.current.cropper.setRotation( 20 );
183
- result.current.endGesture();
184
- } );
185
-
186
- expect( result.current.cropper.state.rotation ).toBe( 20 );
187
-
188
- act( () => result.current.undo() );
189
-
190
- expect( result.current.cropper.state.rotation ).toBe( 0 );
191
- } );
192
-
193
- it( 'does not record a gesture that returns to its starting state', () => {
194
- const { result } = setupHook();
195
-
196
- act( () => {
197
- result.current.beginGesture();
198
- result.current.cropper.setRotation( 10 );
199
- result.current.cropper.setRotation( 0 );
200
- result.current.endGesture();
201
- } );
202
-
203
- expect( result.current.cropper.state.rotation ).toBe( 0 );
204
- expect( result.current.hasUndo ).toBe( false );
205
- } );
206
- } );
207
-
208
- describe( 'SET_ASPECT_RATIO_VALUE atomicity', () => {
209
- it( 'reshapes cropRect when a fixed ratio is selected with visualSize > 0', () => {
210
- const { result } = setupHook( { width: 600, height: 400 } );
211
-
212
- act( () => result.current.setAspectRatioValue( '1' ) ); // Square
213
-
214
- // At visualSize 600x400, the inscribed square has w/h such that
215
- // pixel ratio = 1. normalizedRatio = 1 * 400 / 600 = 0.666...
216
- // So normalized w = 0.666..., h = 1.
217
- expect( result.current.cropper.state.cropRect.width ).toBeCloseTo(
218
- 2 / 3,
219
- 5
220
- );
221
- expect( result.current.cropper.state.cropRect.height ).toBeCloseTo(
222
- 1,
223
- 5
224
- );
225
- } );
226
-
227
- it( 'Free leaves the cropRect alone', () => {
228
- const { result } = setupHook();
229
- const original = result.current.cropper.state.cropRect;
230
-
231
- // User-picked custom crop first.
232
- act( () =>
233
- result.current.cropper.setCropRect( {
234
- x: 0.1,
235
- y: 0.1,
236
- width: 0.5,
237
- height: 0.5,
238
- } )
239
- );
240
- const beforeFree = result.current.cropper.state.cropRect;
241
-
242
- // Switch to Free — cropRect should NOT snap back to anything.
243
- act( () => result.current.setAspectRatioValue( '0' ) );
244
-
245
- expect( result.current.cropper.state.cropRect ).toEqual(
246
- beforeFree
247
- );
248
- expect( original ).not.toEqual( beforeFree );
249
- } );
250
-
251
- it( 'resolves Original from the loaded image', () => {
252
- const { result } = setupHook();
253
-
254
- act( () => result.current.setAspectRatioValue( '-1' ) ); // Original
255
-
256
- // Image is 1200x600 = ratio 2. inscribedRect at visualSize 600x400:
257
- // normalizedRatio = 2 * 400 / 600 = 1.333... > 1, so h = 1/1.333 = 0.75.
258
- expect( result.current.cropper.state.cropRect.width ).toBeCloseTo(
259
- 1,
260
- 5
261
- );
262
- expect( result.current.cropper.state.cropRect.height ).toBeCloseTo(
263
- 0.75,
264
- 5
265
- );
266
- } );
267
-
268
- it( 'groups cropper reset and cropOptions reset in one undo step', () => {
269
- const { result } = setupHook();
270
-
271
- act( () => result.current.setAspectRatioValue( '1' ) );
272
- act( () => result.current.cropper.setRotation( 45 ) );
273
-
274
- act( () => {
275
- result.current.beginGesture();
276
- result.current.cropper.reset();
277
- result.current.resetCropOptions();
278
- result.current.endGesture();
279
- } );
280
-
281
- expect( result.current.cropper.state.rotation ).toBe( 0 );
282
- expect( result.current.cropOptions ).toEqual( {
283
- aspectRatioValue: '0',
284
- } );
285
-
286
- act( () => result.current.undo() );
287
-
288
- expect( result.current.cropper.state.rotation ).toBe( 45 );
289
- expect( result.current.cropOptions ).toEqual( {
290
- aspectRatioValue: '1',
291
- } );
292
- } );
293
- } );
294
-
295
- describe( 'viewport reshape is silent', () => {
296
- it( 'adjustCropRectForViewport does NOT record an undo entry', () => {
297
- const { result } = setupHook();
298
-
299
- act( () =>
300
- result.current.cropper.adjustCropRectForViewport( {
301
- x: 0,
302
- y: 0,
303
- width: 0.5,
304
- height: 0.5,
305
- } )
306
- );
307
-
308
- expect( result.current.hasUndo ).toBe( false );
309
- } );
310
- } );
311
-
312
- describe( 'setSourceImage', () => {
313
- it( 'clears history and resets isDirty', () => {
314
- const { result } = setupHook();
315
-
316
- act( () => result.current.cropper.setRotation( 45 ) );
317
- expect( result.current.hasUndo ).toBe( true );
318
- expect( result.current.isDirty ).toBe( true );
319
-
320
- act( () =>
321
- result.current.setSourceImage( {
322
- src: 'other.jpg',
323
- naturalWidth: 800,
324
- naturalHeight: 800,
325
- } )
326
- );
327
-
328
- expect( result.current.hasUndo ).toBe( false );
329
- expect( result.current.hasRedo ).toBe( false );
330
- expect( result.current.isDirty ).toBe( false );
331
- } );
332
-
333
- it( 'resets geometry and crop options for a new image', () => {
334
- const { result } = setupHook();
335
-
336
- act( () => result.current.cropper.setRotation( 45 ) );
337
- act( () => result.current.setAspectRatioValue( '1' ) );
338
- expect( result.current.cropper.state.rotation ).toBe( 45 );
339
- expect( result.current.cropOptions.aspectRatioValue ).toBe( '1' );
340
-
341
- act( () =>
342
- result.current.setSourceImage( {
343
- src: 'other.jpg',
344
- naturalWidth: 800,
345
- naturalHeight: 800,
346
- } )
347
- );
348
-
349
- // Edits made against one image must not survive onto another.
350
- expect( result.current.cropper.state.rotation ).toBe( 0 );
351
- expect( result.current.cropOptions.aspectRatioValue ).toBe( '0' );
352
- } );
353
-
354
- it( 'starts a fresh session when the cropper reports a different image', () => {
355
- const { result } = setupHook();
356
-
357
- act( () => result.current.cropper.setRotation( 45 ) );
358
- act( () =>
359
- result.current.cropper.setImage( {
360
- src: 'other.jpg',
361
- naturalWidth: 800,
362
- naturalHeight: 800,
363
- } )
364
- );
365
-
366
- expect( result.current.cropper.state.image?.src ).toBe(
367
- 'other.jpg'
368
- );
369
- expect( result.current.cropper.state.rotation ).toBe( 0 );
370
- expect( result.current.hasUndo ).toBe( false );
371
- } );
372
-
373
- it( 'does not clear history when the same image is reported again', () => {
374
- const { result } = setupHook();
375
-
376
- act( () => result.current.cropper.setRotation( 45 ) );
377
- expect( result.current.hasUndo ).toBe( true );
378
- expect( result.current.cropper.isDirty ).toBe( true );
379
-
380
- act( () => result.current.setSourceImage( IMAGE ) );
381
-
382
- expect( result.current.hasUndo ).toBe( true );
383
- expect( result.current.cropper.isDirty ).toBe( true );
384
- } );
385
- } );
386
- } );
@@ -1,64 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest';
2
- import reducer, { DEFAULT_STATE } from '../reducer';
3
-
4
- describe( 'core/media-editor reducer', () => {
5
- it( 'returns the default state for an unknown action', () => {
6
- const state = reducer( undefined, { type: '@@INIT' } );
7
- expect( state ).toEqual( DEFAULT_STATE );
8
- } );
9
-
10
- it( 'opens the modal with id and onUpdate', () => {
11
- const onUpdate = vi.fn();
12
- const onClose = vi.fn();
13
- const state = reducer( undefined, {
14
- type: 'OPEN_MEDIA_EDITOR_MODAL',
15
- id: 42,
16
- onUpdate,
17
- onClose,
18
- } );
19
- expect( state ).toEqual( {
20
- isOpen: true,
21
- id: 42,
22
- onUpdate,
23
- onClose,
24
- } );
25
- } );
26
-
27
- it( 'replaces state on a subsequent open (new onUpdate)', () => {
28
- const firstOnUpdate = vi.fn();
29
- const firstOnClose = vi.fn();
30
- const secondOnUpdate = vi.fn();
31
- const secondOnClose = vi.fn();
32
- const first = reducer( undefined, {
33
- type: 'OPEN_MEDIA_EDITOR_MODAL',
34
- id: 42,
35
- onUpdate: firstOnUpdate,
36
- onClose: firstOnClose,
37
- } );
38
- const second = reducer( first, {
39
- type: 'OPEN_MEDIA_EDITOR_MODAL',
40
- id: 99,
41
- onUpdate: secondOnUpdate,
42
- onClose: secondOnClose,
43
- } );
44
- expect( second ).toEqual( {
45
- isOpen: true,
46
- id: 99,
47
- onUpdate: secondOnUpdate,
48
- onClose: secondOnClose,
49
- } );
50
- } );
51
-
52
- it( 'clears state on close', () => {
53
- const opened = reducer( undefined, {
54
- type: 'OPEN_MEDIA_EDITOR_MODAL',
55
- id: 42,
56
- onUpdate: vi.fn(),
57
- onClose: vi.fn(),
58
- } );
59
- const closed = reducer( opened, {
60
- type: 'CLOSE_MEDIA_EDITOR_MODAL',
61
- } );
62
- expect( closed ).toEqual( DEFAULT_STATE );
63
- } );
64
- } );
@@ -1,26 +0,0 @@
1
- {
2
- "$schema": "https://json.schemastore.org/tsconfig.json",
3
- "extends": "@wordpress/monorepo-tools/tsconfig/base.json",
4
- "compilerOptions": {
5
- "types": [ "gutenberg-env", "style-imports" ],
6
- "checkJs": false
7
- },
8
- "references": [
9
- { "path": "../a11y/tsconfig.build.json" },
10
- { "path": "../admin-ui/tsconfig.build.json" },
11
- { "path": "../api-fetch/tsconfig.build.json" },
12
- { "path": "../components/tsconfig.build.json" },
13
- { "path": "../compose/tsconfig.build.json" },
14
- { "path": "../core-data/tsconfig.build.json" },
15
- { "path": "../data" },
16
- { "path": "../dataviews/tsconfig.build.json" },
17
- { "path": "../element/tsconfig.build.json" },
18
- { "path": "../i18n/tsconfig.build.json" },
19
- { "path": "../icons/tsconfig.build.json" },
20
- { "path": "../keyboard-shortcuts" },
21
- { "path": "../keycodes/tsconfig.build.json" },
22
- { "path": "../notices/tsconfig.build.json" },
23
- { "path": "../private-apis" },
24
- { "path": "../ui/tsconfig.build.json" }
25
- ]
26
- }