@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.
- package/CHANGELOG.md +16 -0
- package/README.md +2 -1
- package/build/components/media-editor/index.cjs +7 -16
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +1 -2
- package/build/components/media-editor/use-crop-options.cjs.map +2 -2
- package/build/components/media-editor-canvas/index.cjs.map +3 -3
- package/build/components/media-editor-crop-panel/index.cjs +12 -8
- package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
- package/build/components/media-editor-modal/index.cjs +2 -0
- package/build/components/media-editor-modal/index.cjs.map +2 -2
- package/build/components/media-editor-provider/index.cjs +2 -1
- package/build/components/media-editor-provider/index.cjs.map +2 -2
- package/build/components/media-form/index.cjs +28 -28
- package/build/components/media-form/index.cjs.map +3 -3
- package/build/components/media-preview/index.cjs.map +3 -3
- package/build/image-editor/react/components/cropper.cjs +1 -11
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/state/composite-reducer.cjs +33 -8
- package/build/state/composite-reducer.cjs.map +3 -3
- package/build/state/index.cjs.map +1 -1
- package/build/state/types.cjs.map +1 -1
- package/build/state/use-media-editor-state.cjs +20 -7
- package/build/state/use-media-editor-state.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +9 -18
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +1 -2
- package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
- package/build-module/components/media-editor-canvas/index.mjs +2 -2
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +13 -9
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-modal/index.mjs +2 -0
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-provider/index.mjs +2 -1
- package/build-module/components/media-editor-provider/index.mjs.map +2 -2
- package/build-module/components/media-form/index.mjs +33 -30
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/media-preview/index.mjs +2 -2
- package/build-module/components/media-preview/index.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +1 -11
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/state/composite-reducer.mjs +23 -8
- package/build-module/state/composite-reducer.mjs.map +2 -2
- package/build-module/state/index.mjs.map +1 -1
- package/build-module/state/types.mjs.map +1 -1
- package/build-module/state/use-media-editor-state.mjs +20 -7
- package/build-module/state/use-media-editor-state.mjs.map +2 -2
- package/build-types/components/media-editor/index.d.ts +2 -1
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +0 -1
- package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/index.d.ts +7 -2
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-provider/index.d.ts +7 -1
- package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts +5 -2
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts +1 -1
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/index.d.ts +1 -1
- package/build-types/state/index.d.ts.map +1 -1
- package/build-types/state/types.d.ts +34 -5
- package/build-types/state/types.d.ts.map +1 -1
- package/build-types/state/use-media-editor-state.d.ts +16 -5
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/package.json +33 -22
- package/src/components/media-editor/index.tsx +12 -22
- package/src/components/media-editor/use-crop-options.ts +0 -2
- package/src/components/media-editor-canvas/index.tsx +2 -2
- package/src/components/media-editor-crop-panel/index.tsx +17 -9
- package/src/components/media-editor-image-controls/style.scss +1 -1
- package/src/components/media-editor-modal/index.tsx +8 -1
- package/src/components/media-editor-provider/index.tsx +8 -1
- package/src/components/media-form/index.tsx +48 -42
- package/src/components/media-preview/index.tsx +2 -2
- package/src/image-editor/docs/architecture.md +4 -0
- package/src/image-editor/react/components/cropper.tsx +1 -15
- package/src/state/composite-reducer.ts +24 -11
- package/src/state/index.ts +2 -0
- package/src/state/types.ts +37 -5
- package/src/state/use-media-editor-state.ts +46 -11
- package/global.d.ts +0 -5
- package/src/components/media-editor/test/restore-original.browser.test.tsx +0 -227
- package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -106
- package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -72
- package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -186
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -359
- package/src/components/media-editor-modal/test/index.jsdom.test.tsx +0 -182
- package/src/components/rotation-ruler/stories/index.story.tsx +0 -43
- package/src/components/rotation-ruler/test/index.jsdom.test.tsx +0 -140
- package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +0 -823
- package/src/image-editor/core/math/test/sanitize.ts +0 -242
- package/src/image-editor/core/test/camera.ts +0 -1218
- package/src/image-editor/core/test/interaction-controller.ts +0 -1299
- package/src/image-editor/core/test/preview-export-parity.ts +0 -249
- package/src/image-editor/core/test/render-camera-bridge.ts +0 -349
- package/src/image-editor/core/test/state.ts +0 -876
- package/src/image-editor/core/test/stencil-math.ts +0 -665
- package/src/image-editor/core/transforms/test/pipeline.ts +0 -267
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.browser.test.tsx +0 -438
- package/src/image-editor/react/components/test/cropper.browser.test.tsx +0 -1462
- package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +0 -491
- package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +0 -1049
- package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +0 -213
- package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +0 -140
- package/src/image-editor/stories/rectangle-crop.story.tsx +0 -1073
- package/src/image-editor/stories/style.module.css +0 -65
- package/src/state/test/use-media-editor-state.browser.test.tsx +0 -79
- package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -386
- package/src/store/test/reducer.ts +0 -64
- package/tsconfig.build.json +0 -26
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -37
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { page, userEvent } from 'vitest/browser';
|
|
3
|
-
import { screen, waitFor } from '@testing-library/react';
|
|
4
|
-
import { cleanup, render } from 'vitest-browser-react';
|
|
5
|
-
import { createRegistry, RegistryProvider } from '@wordpress/data';
|
|
6
|
-
import { store as coreStore } from '@wordpress/core-data';
|
|
7
|
-
import { store as noticesStore } from '@wordpress/notices';
|
|
8
|
-
import '@wordpress/components/src/style.scss';
|
|
9
|
-
import MediaEditor, { type MediaEditorFrameProps } from '../index';
|
|
10
|
-
import type { Media } from '../../media-editor-provider';
|
|
11
|
-
import '../../../style.scss';
|
|
12
|
-
|
|
13
|
-
const original: Media = {
|
|
14
|
-
id: 10,
|
|
15
|
-
source_url:
|
|
16
|
-
'data:image/svg+xml,' +
|
|
17
|
-
encodeURIComponent(
|
|
18
|
-
'<svg xmlns="http://www.w3.org/2000/svg" width="600" height="400"><rect width="600" height="400" fill="black"/></svg>'
|
|
19
|
-
),
|
|
20
|
-
mime_type: 'image/svg+xml',
|
|
21
|
-
media_details: { width: 600, height: 400 },
|
|
22
|
-
alt_text: 'Original alternative text',
|
|
23
|
-
title: { raw: 'Original title', rendered: 'Original title' },
|
|
24
|
-
post: 5,
|
|
25
|
-
};
|
|
26
|
-
const cropped: Media = {
|
|
27
|
-
...original,
|
|
28
|
-
id: 11,
|
|
29
|
-
source_url: `${ original.source_url }#cropped`,
|
|
30
|
-
edit_root: 10,
|
|
31
|
-
alt_text: 'Cropped alternative text',
|
|
32
|
-
title: { raw: 'Cropped title', rendered: 'Cropped title' },
|
|
33
|
-
post: 6,
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
function Frame( { children }: MediaEditorFrameProps ) {
|
|
37
|
-
return (
|
|
38
|
-
<div
|
|
39
|
-
style={ {
|
|
40
|
-
display: 'flex',
|
|
41
|
-
flexDirection: 'column',
|
|
42
|
-
width: 900,
|
|
43
|
-
height: 650,
|
|
44
|
-
} }
|
|
45
|
-
>
|
|
46
|
-
<MediaEditor.HeaderActions />
|
|
47
|
-
{ children }
|
|
48
|
-
<MediaEditor.SaveActions />
|
|
49
|
-
</div>
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async function setup() {
|
|
54
|
-
await page.viewport( 1000, 800 );
|
|
55
|
-
const writes = vi.fn();
|
|
56
|
-
vi.spyOn( window, 'fetch' ).mockImplementation(
|
|
57
|
-
async ( input, options ) => {
|
|
58
|
-
const url = new URL( String( input ), window.location.href );
|
|
59
|
-
const record = url.pathname.startsWith( '/wp/v2/media/10' )
|
|
60
|
-
? original
|
|
61
|
-
: cropped;
|
|
62
|
-
if ( options?.method === 'POST' ) {
|
|
63
|
-
const data = JSON.parse( String( options.body ) );
|
|
64
|
-
writes( url.pathname, data );
|
|
65
|
-
return Response.json( {
|
|
66
|
-
...record,
|
|
67
|
-
...data,
|
|
68
|
-
...( url.pathname.endsWith( '/edit' ) ? { id: 12 } : {} ),
|
|
69
|
-
} );
|
|
70
|
-
}
|
|
71
|
-
return Response.json( record );
|
|
72
|
-
}
|
|
73
|
-
);
|
|
74
|
-
const registry = createRegistry();
|
|
75
|
-
registry.register( coreStore );
|
|
76
|
-
registry.register( noticesStore );
|
|
77
|
-
registry.dispatch( coreStore ).addEntities( [
|
|
78
|
-
{
|
|
79
|
-
kind: 'postType',
|
|
80
|
-
name: 'attachment',
|
|
81
|
-
baseURL: '/wp/v2/media',
|
|
82
|
-
baseURLParams: { context: 'edit' },
|
|
83
|
-
rawAttributes: [ 'title', 'caption', 'description' ],
|
|
84
|
-
},
|
|
85
|
-
] );
|
|
86
|
-
registry
|
|
87
|
-
.dispatch( coreStore )
|
|
88
|
-
.receiveEntityRecords( 'postType', 'attachment', [
|
|
89
|
-
original,
|
|
90
|
-
cropped,
|
|
91
|
-
] );
|
|
92
|
-
const onSaved = vi.fn();
|
|
93
|
-
await render(
|
|
94
|
-
<RegistryProvider value={ registry }>
|
|
95
|
-
<MediaEditor
|
|
96
|
-
id={ 11 }
|
|
97
|
-
onSaved={ onSaved }
|
|
98
|
-
renderFrame={ Frame }
|
|
99
|
-
fields={ [
|
|
100
|
-
{
|
|
101
|
-
id: 'alt_text',
|
|
102
|
-
label: 'Alternative text',
|
|
103
|
-
type: 'text',
|
|
104
|
-
},
|
|
105
|
-
{
|
|
106
|
-
id: 'title',
|
|
107
|
-
label: 'Title',
|
|
108
|
-
type: 'text',
|
|
109
|
-
getValue: ( { item } ) =>
|
|
110
|
-
typeof item.title === 'string'
|
|
111
|
-
? item.title
|
|
112
|
-
: item.title?.raw,
|
|
113
|
-
},
|
|
114
|
-
] }
|
|
115
|
-
/>
|
|
116
|
-
</RegistryProvider>
|
|
117
|
-
);
|
|
118
|
-
await userEvent.click( screen.getByRole( 'tab', { name: 'Details' } ) );
|
|
119
|
-
return { registry, writes, onSaved };
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async function restoreOriginal() {
|
|
123
|
-
await userEvent.click(
|
|
124
|
-
screen.getByRole( 'button', { name: 'More options' } )
|
|
125
|
-
);
|
|
126
|
-
await userEvent.click(
|
|
127
|
-
screen.getByRole( 'menuitem', { name: /^Restore original image/ } )
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
afterEach( async () => {
|
|
132
|
-
await cleanup();
|
|
133
|
-
vi.restoreAllMocks();
|
|
134
|
-
} );
|
|
135
|
-
|
|
136
|
-
describe( 'Restore original', () => {
|
|
137
|
-
it( 'loads editable original details and discards the cropped attachment edits', async () => {
|
|
138
|
-
const { registry } = await setup();
|
|
139
|
-
expect( screen.getByRole( 'textbox', { name: 'Title' } ) ).toHaveValue(
|
|
140
|
-
'Cropped title'
|
|
141
|
-
);
|
|
142
|
-
await userEvent.fill(
|
|
143
|
-
screen.getByRole( 'textbox', { name: 'Alternative text' } ),
|
|
144
|
-
'Unsaved cropped text'
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
await restoreOriginal();
|
|
148
|
-
|
|
149
|
-
expect( screen.getByRole( 'textbox', { name: 'Title' } ) ).toHaveValue(
|
|
150
|
-
'Original title'
|
|
151
|
-
);
|
|
152
|
-
const alt = screen.getByRole( 'textbox', { name: 'Alternative text' } );
|
|
153
|
-
expect( alt ).toBeEnabled();
|
|
154
|
-
expect( alt ).toHaveValue( 'Original alternative text' );
|
|
155
|
-
expect(
|
|
156
|
-
registry
|
|
157
|
-
.select( coreStore )
|
|
158
|
-
.hasEditsForEntityRecord( 'postType', 'attachment', 11 )
|
|
159
|
-
).toBe( false );
|
|
160
|
-
} );
|
|
161
|
-
|
|
162
|
-
it( 'saves an untouched restore without writing an attachment', async () => {
|
|
163
|
-
const { writes, onSaved } = await setup();
|
|
164
|
-
await restoreOriginal();
|
|
165
|
-
|
|
166
|
-
await userEvent.click( screen.getByRole( 'button', { name: 'Save' } ) );
|
|
167
|
-
|
|
168
|
-
expect( writes ).not.toHaveBeenCalled();
|
|
169
|
-
expect( onSaved ).toHaveBeenCalledWith( {
|
|
170
|
-
id: 10,
|
|
171
|
-
url: original.source_url,
|
|
172
|
-
media: original,
|
|
173
|
-
previous: { id: 11, url: cropped.source_url },
|
|
174
|
-
} );
|
|
175
|
-
} );
|
|
176
|
-
|
|
177
|
-
it.each( [
|
|
178
|
-
{ withTransform: false, target: 'the original attachment' },
|
|
179
|
-
{ withTransform: true, target: 'a new attachment after rotating' },
|
|
180
|
-
] )( 'saves restored details to $target', async ( { withTransform } ) => {
|
|
181
|
-
const { registry, writes, onSaved } = await setup();
|
|
182
|
-
await restoreOriginal();
|
|
183
|
-
await userEvent.fill(
|
|
184
|
-
screen.getByRole( 'textbox', { name: 'Alternative text' } ),
|
|
185
|
-
'Updated original text'
|
|
186
|
-
);
|
|
187
|
-
if ( withTransform ) {
|
|
188
|
-
await userEvent.click(
|
|
189
|
-
screen.getByRole( 'tab', { name: 'Crop' } )
|
|
190
|
-
);
|
|
191
|
-
await userEvent.click(
|
|
192
|
-
screen.getByRole( 'button', {
|
|
193
|
-
name: 'Rotate 90° clockwise',
|
|
194
|
-
} )
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
await userEvent.click( screen.getByRole( 'button', { name: 'Save' } ) );
|
|
199
|
-
|
|
200
|
-
await waitFor( () => expect( onSaved ).toHaveBeenCalledTimes( 1 ) );
|
|
201
|
-
expect( writes ).toHaveBeenCalledExactlyOnceWith(
|
|
202
|
-
withTransform ? '/wp/v2/media/10/edit' : '/wp/v2/media/10',
|
|
203
|
-
expect.objectContaining( { alt_text: 'Updated original text' } )
|
|
204
|
-
);
|
|
205
|
-
expect( onSaved ).toHaveBeenCalledWith(
|
|
206
|
-
expect.objectContaining( {
|
|
207
|
-
id: withTransform ? 12 : 10,
|
|
208
|
-
previous: { id: 11, url: cropped.source_url },
|
|
209
|
-
} )
|
|
210
|
-
);
|
|
211
|
-
expect(
|
|
212
|
-
registry
|
|
213
|
-
.select( coreStore )
|
|
214
|
-
.hasEditsForEntityRecord( 'postType', 'attachment', 10 )
|
|
215
|
-
).toBe( false );
|
|
216
|
-
// Only the transform case writes to `/edit`; expressed as one
|
|
217
|
-
// unconditional assertion so each case asserts its own expectation.
|
|
218
|
-
const editWrite = withTransform
|
|
219
|
-
? writes.mock.calls[ 0 ][ 1 ]
|
|
220
|
-
: undefined;
|
|
221
|
-
expect( { src: editWrite?.src, post: editWrite?.post } ).toEqual(
|
|
222
|
-
withTransform
|
|
223
|
-
? { src: original.source_url, post: original.post }
|
|
224
|
-
: { src: undefined, post: undefined }
|
|
225
|
-
);
|
|
226
|
-
} );
|
|
227
|
-
} );
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { fireEvent, render, screen } from '@testing-library/react';
|
|
3
|
-
import { useEffect } from '@wordpress/element';
|
|
4
|
-
import { useCropOptions } from '../use-crop-options';
|
|
5
|
-
import { ORIGINAL_ASPECT_RATIO } from '../../../image-editor/core/constants';
|
|
6
|
-
import { MediaEditorStateProvider, useMediaEditor } from '../../../state';
|
|
7
|
-
|
|
8
|
-
const IMAGE = {
|
|
9
|
-
src: 'test.jpg',
|
|
10
|
-
naturalWidth: 1200,
|
|
11
|
-
naturalHeight: 600,
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
function CropOptionsHarness() {
|
|
15
|
-
// Load the image through the session, as the canvas does.
|
|
16
|
-
const { setSourceImage } = useMediaEditor();
|
|
17
|
-
useEffect( () => setSourceImage( IMAGE ), [ setSourceImage ] );
|
|
18
|
-
const cropOptions = useCropOptions( {
|
|
19
|
-
aspectRatioPresets: [
|
|
20
|
-
{ label: 'Square', value: 1 },
|
|
21
|
-
{ label: 'Landscape', value: 4 / 3 },
|
|
22
|
-
],
|
|
23
|
-
} );
|
|
24
|
-
|
|
25
|
-
return (
|
|
26
|
-
<div>
|
|
27
|
-
<div data-testid="aspect-ratio-value">
|
|
28
|
-
{ cropOptions.aspectRatioValue }
|
|
29
|
-
</div>
|
|
30
|
-
<div data-testid="resolved-aspect-ratio">
|
|
31
|
-
{ cropOptions.resolvedAspectRatio ?? 'undefined' }
|
|
32
|
-
</div>
|
|
33
|
-
<div data-testid="aspect-ratio-options">
|
|
34
|
-
{ cropOptions.aspectRatioOptions
|
|
35
|
-
.map( ( option ) => option.value )
|
|
36
|
-
.join( ',' ) }
|
|
37
|
-
</div>
|
|
38
|
-
<button
|
|
39
|
-
onClick={ () =>
|
|
40
|
-
cropOptions.setAspectRatioValue(
|
|
41
|
-
ORIGINAL_ASPECT_RATIO.toString()
|
|
42
|
-
)
|
|
43
|
-
}
|
|
44
|
-
>
|
|
45
|
-
Original
|
|
46
|
-
</button>
|
|
47
|
-
<button onClick={ () => cropOptions.setAspectRatioValue( '1' ) }>
|
|
48
|
-
Square
|
|
49
|
-
</button>
|
|
50
|
-
<button onClick={ () => cropOptions.setAspectRatioValue( '0' ) }>
|
|
51
|
-
Free
|
|
52
|
-
</button>
|
|
53
|
-
<button onClick={ cropOptions.resetCropOptions }>Reset</button>
|
|
54
|
-
</div>
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function renderHarness() {
|
|
59
|
-
render(
|
|
60
|
-
<MediaEditorStateProvider>
|
|
61
|
-
<CropOptionsHarness />
|
|
62
|
-
</MediaEditorStateProvider>
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
describe( 'useCropOptions', () => {
|
|
67
|
-
it( 'builds explicit aspect-ratio options', () => {
|
|
68
|
-
renderHarness();
|
|
69
|
-
|
|
70
|
-
expect(
|
|
71
|
-
screen.getByTestId( 'aspect-ratio-options' )
|
|
72
|
-
).toHaveTextContent( '0,-1,1,1.3333333333333333' );
|
|
73
|
-
} );
|
|
74
|
-
|
|
75
|
-
it( 'resolves the Original aspect ratio from the cropper image dimensions', () => {
|
|
76
|
-
renderHarness();
|
|
77
|
-
|
|
78
|
-
fireEvent.click( screen.getByRole( 'button', { name: 'Original' } ) );
|
|
79
|
-
|
|
80
|
-
// 1200 / 600 = 2.
|
|
81
|
-
expect(
|
|
82
|
-
screen.getByTestId( 'resolved-aspect-ratio' )
|
|
83
|
-
).toHaveTextContent( '2' );
|
|
84
|
-
} );
|
|
85
|
-
|
|
86
|
-
it( 'reports Free as undefined resolved aspect ratio', () => {
|
|
87
|
-
renderHarness();
|
|
88
|
-
|
|
89
|
-
fireEvent.click( screen.getByRole( 'button', { name: 'Free' } ) );
|
|
90
|
-
|
|
91
|
-
expect(
|
|
92
|
-
screen.getByTestId( 'resolved-aspect-ratio' )
|
|
93
|
-
).toHaveTextContent( 'undefined' );
|
|
94
|
-
} );
|
|
95
|
-
|
|
96
|
-
it( 'reset returns cropOptions to defaults', () => {
|
|
97
|
-
renderHarness();
|
|
98
|
-
|
|
99
|
-
fireEvent.click( screen.getByRole( 'button', { name: 'Square' } ) );
|
|
100
|
-
fireEvent.click( screen.getByRole( 'button', { name: 'Reset' } ) );
|
|
101
|
-
|
|
102
|
-
expect( screen.getByTestId( 'aspect-ratio-value' ) ).toHaveTextContent(
|
|
103
|
-
'0'
|
|
104
|
-
);
|
|
105
|
-
} );
|
|
106
|
-
} );
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi, type Mock } from 'vitest';
|
|
2
|
-
import { fireEvent, render, screen } from '@testing-library/react';
|
|
3
|
-
import MediaEditorCropPanel from '..';
|
|
4
|
-
import type { MediaEditorCropPanelProps } from '..';
|
|
5
|
-
import { MediaEditorStateProvider } from '../../../state';
|
|
6
|
-
|
|
7
|
-
globalThis.wpVitest.mockMatchMedia();
|
|
8
|
-
|
|
9
|
-
function setupCropPanel(
|
|
10
|
-
overrides: Partial< MediaEditorCropPanelProps > = {}
|
|
11
|
-
) {
|
|
12
|
-
const props: MediaEditorCropPanelProps = {
|
|
13
|
-
aspectRatioValue: '1',
|
|
14
|
-
onAspectRatioChange: vi.fn(),
|
|
15
|
-
aspectRatioOptions: [
|
|
16
|
-
{ label: 'Free', value: 0 },
|
|
17
|
-
{ label: 'Original', value: -1 },
|
|
18
|
-
{ label: 'Square', value: 1 },
|
|
19
|
-
],
|
|
20
|
-
...overrides,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
render(
|
|
24
|
-
<MediaEditorStateProvider>
|
|
25
|
-
<MediaEditorCropPanel { ...props } />
|
|
26
|
-
</MediaEditorStateProvider>
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
return props;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
describe( 'MediaEditorCropPanel', () => {
|
|
33
|
-
it( 'passes selected aspect ratio changes to the caller', () => {
|
|
34
|
-
const controls = setupCropPanel( {
|
|
35
|
-
aspectRatioValue: '1',
|
|
36
|
-
} );
|
|
37
|
-
|
|
38
|
-
fireEvent.change( screen.getByLabelText( 'Aspect ratio' ), {
|
|
39
|
-
target: { value: '0' },
|
|
40
|
-
} );
|
|
41
|
-
|
|
42
|
-
expect( controls.onAspectRatioChange ).toHaveBeenCalled();
|
|
43
|
-
expect(
|
|
44
|
-
( controls.onAspectRatioChange as Mock ).mock.calls[ 0 ][ 0 ]
|
|
45
|
-
).toBe( '0' );
|
|
46
|
-
} );
|
|
47
|
-
|
|
48
|
-
it( 'renders rotate, flip and zoom controls', () => {
|
|
49
|
-
setupCropPanel();
|
|
50
|
-
|
|
51
|
-
expect( screen.getByText( 'Rotate' ) ).toBeInTheDocument();
|
|
52
|
-
expect( screen.getByText( 'Flip' ) ).toBeInTheDocument();
|
|
53
|
-
expect( screen.getByText( 'Zoom' ) ).toBeInTheDocument();
|
|
54
|
-
expect(
|
|
55
|
-
screen.getByRole( 'button', { name: 'Rotate 90° clockwise' } )
|
|
56
|
-
).toBeInTheDocument();
|
|
57
|
-
expect(
|
|
58
|
-
screen.getByRole( 'button', { name: 'Zoom in' } )
|
|
59
|
-
).toBeInTheDocument();
|
|
60
|
-
} );
|
|
61
|
-
|
|
62
|
-
it( 'renders the image controls above the aspect-ratio selector', () => {
|
|
63
|
-
setupCropPanel();
|
|
64
|
-
|
|
65
|
-
const rotate = screen.getByText( 'Rotate' );
|
|
66
|
-
const aspectRatio = screen.getByLabelText( 'Aspect ratio' );
|
|
67
|
-
|
|
68
|
-
expect( rotate.compareDocumentPosition( aspectRatio ) ).toBe(
|
|
69
|
-
Node.DOCUMENT_POSITION_FOLLOWING
|
|
70
|
-
);
|
|
71
|
-
} );
|
|
72
|
-
} );
|
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
3
|
-
import { useEffect, useState } from '@wordpress/element';
|
|
4
|
-
import MediaEditorImageControls from '..';
|
|
5
|
-
import type { MediaEditorImageControlsProps } from '..';
|
|
6
|
-
import { MediaEditorStateProvider, useMediaEditor } from '../../../state';
|
|
7
|
-
import { MAX_ZOOM } from '../../../image-editor/core/constants';
|
|
8
|
-
|
|
9
|
-
function setup( props: MediaEditorImageControlsProps = {}, zoom?: number ) {
|
|
10
|
-
render(
|
|
11
|
-
<MediaEditorStateProvider>
|
|
12
|
-
<MediaEditorImageControls { ...props } />
|
|
13
|
-
<CurrentState zoom={ zoom } />
|
|
14
|
-
</MediaEditorStateProvider>
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function CurrentState( { zoom }: { zoom?: number } ) {
|
|
19
|
-
const {
|
|
20
|
-
cropper: { state, setZoom },
|
|
21
|
-
cropOptions,
|
|
22
|
-
} = useMediaEditor();
|
|
23
|
-
// Start the cases that need it zoomed in. `setZoom` is a session edit,
|
|
24
|
-
// so these cases begin with one undo entry; none of them checks history.
|
|
25
|
-
useEffect( () => {
|
|
26
|
-
if ( zoom !== undefined ) {
|
|
27
|
-
setZoom( zoom );
|
|
28
|
-
}
|
|
29
|
-
}, [ setZoom, zoom ] );
|
|
30
|
-
|
|
31
|
-
return (
|
|
32
|
-
<>
|
|
33
|
-
<output data-testid="current-aspect-ratio">
|
|
34
|
-
{ cropOptions.aspectRatioValue }
|
|
35
|
-
</output>
|
|
36
|
-
<output data-testid="current-zoom">{ state.zoom }</output>
|
|
37
|
-
</>
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
describe( 'MediaEditorImageControls', () => {
|
|
42
|
-
it( 'renders a flat row of rotate, flip and zoom buttons by default', () => {
|
|
43
|
-
setup();
|
|
44
|
-
|
|
45
|
-
expect(
|
|
46
|
-
screen.getByRole( 'button', {
|
|
47
|
-
name: 'Rotate 90° counter-clockwise',
|
|
48
|
-
} )
|
|
49
|
-
).toBeInTheDocument();
|
|
50
|
-
expect(
|
|
51
|
-
screen.getByRole( 'button', { name: 'Flip horizontal' } )
|
|
52
|
-
).toBeInTheDocument();
|
|
53
|
-
expect(
|
|
54
|
-
screen.getByRole( 'button', { name: 'Zoom in' } )
|
|
55
|
-
).toBeInTheDocument();
|
|
56
|
-
expect(
|
|
57
|
-
screen.getByRole( 'button', { name: 'Zoom out' } )
|
|
58
|
-
).toBeInTheDocument();
|
|
59
|
-
|
|
60
|
-
// No visible group labels in the flat (footer) layout.
|
|
61
|
-
expect( screen.queryByText( 'Rotate' ) ).not.toBeInTheDocument();
|
|
62
|
-
expect( screen.queryByText( 'Flip' ) ).not.toBeInTheDocument();
|
|
63
|
-
expect( screen.queryByText( 'Zoom' ) ).not.toBeInTheDocument();
|
|
64
|
-
} );
|
|
65
|
-
|
|
66
|
-
it( 'renders labelled rotate, flip and zoom groups when withLabels is set', () => {
|
|
67
|
-
setup( { withLabels: true } );
|
|
68
|
-
|
|
69
|
-
expect( screen.getByText( 'Rotate' ) ).toBeInTheDocument();
|
|
70
|
-
expect( screen.getByText( 'Flip' ) ).toBeInTheDocument();
|
|
71
|
-
expect( screen.getByText( 'Zoom' ) ).toBeInTheDocument();
|
|
72
|
-
expect(
|
|
73
|
-
screen.getByRole( 'button', { name: 'Rotate 90° clockwise' } )
|
|
74
|
-
).toBeInTheDocument();
|
|
75
|
-
expect(
|
|
76
|
-
screen.getByRole( 'button', { name: 'Zoom in' } )
|
|
77
|
-
).toBeInTheDocument();
|
|
78
|
-
} );
|
|
79
|
-
|
|
80
|
-
it( 'zoom in multiplies the zoom and zoom out divides it', () => {
|
|
81
|
-
setup( {}, 2 );
|
|
82
|
-
|
|
83
|
-
fireEvent.click( screen.getByRole( 'button', { name: 'Zoom in' } ) );
|
|
84
|
-
const zoomedIn = Number(
|
|
85
|
-
screen.getByTestId( 'current-zoom' ).textContent
|
|
86
|
-
);
|
|
87
|
-
expect( zoomedIn ).toBeGreaterThan( 2 );
|
|
88
|
-
|
|
89
|
-
fireEvent.click( screen.getByRole( 'button', { name: 'Zoom out' } ) );
|
|
90
|
-
expect(
|
|
91
|
-
Number( screen.getByTestId( 'current-zoom' ).textContent )
|
|
92
|
-
).toBeLessThan( zoomedIn );
|
|
93
|
-
} );
|
|
94
|
-
|
|
95
|
-
it( 'honors a custom zoomFactor', () => {
|
|
96
|
-
setup( { zoomFactor: 2 }, 2 );
|
|
97
|
-
|
|
98
|
-
fireEvent.click( screen.getByRole( 'button', { name: 'Zoom in' } ) );
|
|
99
|
-
|
|
100
|
-
// A factor of 2 from zoom 2 lands at 4 (within [minZoom, MAX_ZOOM]).
|
|
101
|
-
expect(
|
|
102
|
-
Number( screen.getByTestId( 'current-zoom' ).textContent )
|
|
103
|
-
).toBe( 4 );
|
|
104
|
-
} );
|
|
105
|
-
|
|
106
|
-
it( 'disables Zoom in at the maximum zoom', () => {
|
|
107
|
-
setup( {}, MAX_ZOOM );
|
|
108
|
-
// Buttons use `accessibleWhenDisabled`, so the disabled state is
|
|
109
|
-
// expressed via aria-disabled, not the `disabled` attribute.
|
|
110
|
-
expect(
|
|
111
|
-
screen.getByRole( 'button', { name: 'Zoom in' } )
|
|
112
|
-
).toHaveAttribute( 'aria-disabled', 'true' );
|
|
113
|
-
} );
|
|
114
|
-
|
|
115
|
-
it( 'disables Zoom out at the minimum zoom', () => {
|
|
116
|
-
setup();
|
|
117
|
-
expect(
|
|
118
|
-
screen.getByRole( 'button', { name: 'Zoom out' } )
|
|
119
|
-
).toHaveAttribute( 'aria-disabled', 'true' );
|
|
120
|
-
} );
|
|
121
|
-
|
|
122
|
-
it( 'renders an aspect ratio dropdown in the flat toolbar when enabled', async () => {
|
|
123
|
-
setup( {
|
|
124
|
-
showAspectRatioControl: true,
|
|
125
|
-
} );
|
|
126
|
-
|
|
127
|
-
fireEvent.click(
|
|
128
|
-
screen.getByRole( 'button', { name: 'Aspect ratio' } )
|
|
129
|
-
);
|
|
130
|
-
fireEvent.click(
|
|
131
|
-
screen.getByRole( 'menuitemradio', { name: 'Square (1:1)' } )
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
await waitFor( () =>
|
|
135
|
-
expect(
|
|
136
|
-
screen.getByTestId( 'current-aspect-ratio' )
|
|
137
|
-
).toHaveTextContent( '1' )
|
|
138
|
-
);
|
|
139
|
-
} );
|
|
140
|
-
|
|
141
|
-
it( 'ignores an aspect ratio chosen from a menu left open when disabled', async () => {
|
|
142
|
-
function Harness() {
|
|
143
|
-
const [ locked, setLocked ] = useState( false );
|
|
144
|
-
return (
|
|
145
|
-
<MediaEditorStateProvider>
|
|
146
|
-
<MediaEditorImageControls
|
|
147
|
-
showAspectRatioControl
|
|
148
|
-
disabled={ locked }
|
|
149
|
-
/>
|
|
150
|
-
<button onClick={ () => setLocked( true ) }>lock</button>
|
|
151
|
-
<CurrentState />
|
|
152
|
-
</MediaEditorStateProvider>
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
render( <Harness /> );
|
|
156
|
-
|
|
157
|
-
fireEvent.click(
|
|
158
|
-
screen.getByRole( 'button', { name: 'Aspect ratio' } )
|
|
159
|
-
);
|
|
160
|
-
const before = screen.getByTestId( 'current-aspect-ratio' ).textContent;
|
|
161
|
-
|
|
162
|
-
// The popover stays mounted when the toggle becomes disabled, so
|
|
163
|
-
// the items have to refuse the change themselves.
|
|
164
|
-
fireEvent.click( screen.getByRole( 'button', { name: 'lock' } ) );
|
|
165
|
-
fireEvent.click(
|
|
166
|
-
screen.getByRole( 'menuitemradio', { name: 'Square (1:1)' } )
|
|
167
|
-
);
|
|
168
|
-
|
|
169
|
-
await waitFor( () =>
|
|
170
|
-
expect(
|
|
171
|
-
screen.getByTestId( 'current-aspect-ratio' )
|
|
172
|
-
).toHaveTextContent( before ?? '' )
|
|
173
|
-
);
|
|
174
|
-
} );
|
|
175
|
-
|
|
176
|
-
it( 'omits the aspect ratio dropdown from the labelled panel layout', () => {
|
|
177
|
-
setup( {
|
|
178
|
-
withLabels: true,
|
|
179
|
-
showAspectRatioControl: true,
|
|
180
|
-
} );
|
|
181
|
-
|
|
182
|
-
expect(
|
|
183
|
-
screen.queryByRole( 'button', { name: 'Aspect ratio' } )
|
|
184
|
-
).not.toBeInTheDocument();
|
|
185
|
-
} );
|
|
186
|
-
} );
|