@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,242 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import {
|
|
3
|
-
isValidSize,
|
|
4
|
-
safeBoundedNumber,
|
|
5
|
-
sanitizeCropperState,
|
|
6
|
-
sanitizeRect,
|
|
7
|
-
} from '../sanitize';
|
|
8
|
-
import { DEFAULT_STATE } from '../../constants';
|
|
9
|
-
import type { CropperState } from '../../types';
|
|
10
|
-
|
|
11
|
-
describe( 'safeBoundedNumber', () => {
|
|
12
|
-
it( 'returns the value when it is finite and in range', () => {
|
|
13
|
-
expect( safeBoundedNumber( 42, 0 ) ).toBe( 42 );
|
|
14
|
-
expect( safeBoundedNumber( -3.14, 0 ) ).toBe( -3.14 );
|
|
15
|
-
expect( safeBoundedNumber( 0, 1 ) ).toBe( 0 );
|
|
16
|
-
} );
|
|
17
|
-
|
|
18
|
-
it( 'returns the fallback for NaN', () => {
|
|
19
|
-
expect( safeBoundedNumber( Number.NaN, 7 ) ).toBe( 7 );
|
|
20
|
-
} );
|
|
21
|
-
|
|
22
|
-
it( 'returns the fallback for ±Infinity', () => {
|
|
23
|
-
expect( safeBoundedNumber( Number.POSITIVE_INFINITY, 7 ) ).toBe( 7 );
|
|
24
|
-
expect( safeBoundedNumber( Number.NEGATIVE_INFINITY, 7 ) ).toBe( 7 );
|
|
25
|
-
} );
|
|
26
|
-
|
|
27
|
-
it( 'returns the fallback for finite values beyond the safe magnitude', () => {
|
|
28
|
-
// MAX_VALUE is technically finite but multiplying it through trig
|
|
29
|
-
// and matrix code overflows to Infinity.
|
|
30
|
-
expect( safeBoundedNumber( Number.MAX_VALUE, 1 ) ).toBe( 1 );
|
|
31
|
-
expect( safeBoundedNumber( -Number.MAX_VALUE, 1 ) ).toBe( 1 );
|
|
32
|
-
expect( safeBoundedNumber( 1e10, 1 ) ).toBe( 1 );
|
|
33
|
-
} );
|
|
34
|
-
|
|
35
|
-
it( 'accepts values right at the safe magnitude boundary', () => {
|
|
36
|
-
expect( safeBoundedNumber( 1e6, 0 ) ).toBe( 1e6 );
|
|
37
|
-
expect( safeBoundedNumber( -1e6, 0 ) ).toBe( -1e6 );
|
|
38
|
-
} );
|
|
39
|
-
|
|
40
|
-
it( 'accepts sub-normal values (sub-normal protection lives elsewhere)', () => {
|
|
41
|
-
// Sub-normals don't overflow on their own; they only cause trouble
|
|
42
|
-
// when used as divisors. That guard lives in sanitizeCropperState's
|
|
43
|
-
// zoom check, not here.
|
|
44
|
-
expect( safeBoundedNumber( Number.MIN_VALUE, 0 ) ).toBe(
|
|
45
|
-
Number.MIN_VALUE
|
|
46
|
-
);
|
|
47
|
-
} );
|
|
48
|
-
} );
|
|
49
|
-
|
|
50
|
-
describe( 'isValidSize', () => {
|
|
51
|
-
it( 'returns true for finite, positive, in-range sizes', () => {
|
|
52
|
-
expect( isValidSize( { width: 100, height: 100 } ) ).toBe( true );
|
|
53
|
-
expect( isValidSize( { width: 1, height: 1 } ) ).toBe( true );
|
|
54
|
-
expect( isValidSize( { width: 1e6, height: 1e6 } ) ).toBe( true );
|
|
55
|
-
} );
|
|
56
|
-
|
|
57
|
-
it( 'returns false for zero or negative dimensions', () => {
|
|
58
|
-
expect( isValidSize( { width: 0, height: 100 } ) ).toBe( false );
|
|
59
|
-
expect( isValidSize( { width: 100, height: 0 } ) ).toBe( false );
|
|
60
|
-
expect( isValidSize( { width: -1, height: 100 } ) ).toBe( false );
|
|
61
|
-
} );
|
|
62
|
-
|
|
63
|
-
it( 'returns false for non-finite dimensions', () => {
|
|
64
|
-
expect( isValidSize( { width: Number.NaN, height: 100 } ) ).toBe(
|
|
65
|
-
false
|
|
66
|
-
);
|
|
67
|
-
expect(
|
|
68
|
-
isValidSize( {
|
|
69
|
-
width: Number.POSITIVE_INFINITY,
|
|
70
|
-
height: 100,
|
|
71
|
-
} )
|
|
72
|
-
).toBe( false );
|
|
73
|
-
} );
|
|
74
|
-
|
|
75
|
-
it( 'returns false for dimensions beyond the safe magnitude', () => {
|
|
76
|
-
expect( isValidSize( { width: Number.MAX_VALUE, height: 100 } ) ).toBe(
|
|
77
|
-
false
|
|
78
|
-
);
|
|
79
|
-
expect( isValidSize( { width: 1e10, height: 100 } ) ).toBe( false );
|
|
80
|
-
} );
|
|
81
|
-
|
|
82
|
-
it( 'returns false for sub-normal dimensions (overflow when divided into)', () => {
|
|
83
|
-
// Number.MIN_VALUE passes `> 0` but `container / MIN_VALUE`
|
|
84
|
-
// overflows to Infinity downstream in getImageFit / createCamera.
|
|
85
|
-
expect( isValidSize( { width: Number.MIN_VALUE, height: 100 } ) ).toBe(
|
|
86
|
-
false
|
|
87
|
-
);
|
|
88
|
-
expect( isValidSize( { width: 100, height: Number.MIN_VALUE } ) ).toBe(
|
|
89
|
-
false
|
|
90
|
-
);
|
|
91
|
-
} );
|
|
92
|
-
} );
|
|
93
|
-
|
|
94
|
-
describe( 'sanitizeRect', () => {
|
|
95
|
-
it( 'replaces non-finite fields with 0', () => {
|
|
96
|
-
expect(
|
|
97
|
-
sanitizeRect( {
|
|
98
|
-
x: Number.NaN,
|
|
99
|
-
y: 0.1,
|
|
100
|
-
width: Number.POSITIVE_INFINITY,
|
|
101
|
-
height: 0.5,
|
|
102
|
-
} )
|
|
103
|
-
).toEqual( { x: 0, y: 0.1, width: 0, height: 0.5 } );
|
|
104
|
-
} );
|
|
105
|
-
|
|
106
|
-
it( 'leaves a clean rect unchanged', () => {
|
|
107
|
-
expect(
|
|
108
|
-
sanitizeRect( { x: 0.1, y: 0.2, width: 0.5, height: 0.3 } )
|
|
109
|
-
).toEqual( { x: 0.1, y: 0.2, width: 0.5, height: 0.3 } );
|
|
110
|
-
} );
|
|
111
|
-
} );
|
|
112
|
-
|
|
113
|
-
describe( 'sanitizeCropperState', () => {
|
|
114
|
-
function makeBaseState(
|
|
115
|
-
overrides: Partial< CropperState > = {}
|
|
116
|
-
): CropperState {
|
|
117
|
-
return {
|
|
118
|
-
...DEFAULT_STATE,
|
|
119
|
-
...overrides,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
it( 'leaves a clean state unchanged', () => {
|
|
124
|
-
const state = makeBaseState( {
|
|
125
|
-
pan: { x: 0.1, y: -0.05 },
|
|
126
|
-
zoom: 1.5,
|
|
127
|
-
rotation: 30,
|
|
128
|
-
} );
|
|
129
|
-
const out = sanitizeCropperState( state );
|
|
130
|
-
expect( out.pan ).toEqual( { x: 0.1, y: -0.05 } );
|
|
131
|
-
expect( out.zoom ).toBe( 1.5 );
|
|
132
|
-
expect( out.rotation ).toBe( 30 );
|
|
133
|
-
} );
|
|
134
|
-
|
|
135
|
-
it( 'replaces NaN pan with zero', () => {
|
|
136
|
-
const state = makeBaseState( {
|
|
137
|
-
pan: { x: Number.NaN, y: Number.NaN },
|
|
138
|
-
} );
|
|
139
|
-
const out = sanitizeCropperState( state );
|
|
140
|
-
expect( out.pan ).toEqual( { x: 0, y: 0 } );
|
|
141
|
-
} );
|
|
142
|
-
|
|
143
|
-
it( 'replaces non-finite zoom with 1 (identity)', () => {
|
|
144
|
-
expect(
|
|
145
|
-
sanitizeCropperState( makeBaseState( { zoom: Number.NaN } ) ).zoom
|
|
146
|
-
).toBe( 1 );
|
|
147
|
-
expect(
|
|
148
|
-
sanitizeCropperState(
|
|
149
|
-
makeBaseState( { zoom: Number.POSITIVE_INFINITY } )
|
|
150
|
-
).zoom
|
|
151
|
-
).toBe( 1 );
|
|
152
|
-
expect(
|
|
153
|
-
sanitizeCropperState( makeBaseState( { zoom: -2 } ) ).zoom
|
|
154
|
-
).toBe( 1 );
|
|
155
|
-
} );
|
|
156
|
-
|
|
157
|
-
it( 'replaces sub-normal zoom with 1 (prevents division explosion)', () => {
|
|
158
|
-
expect(
|
|
159
|
-
sanitizeCropperState( makeBaseState( { zoom: Number.MIN_VALUE } ) )
|
|
160
|
-
.zoom
|
|
161
|
-
).toBe( 1 );
|
|
162
|
-
} );
|
|
163
|
-
|
|
164
|
-
it( 'replaces extreme rotation magnitudes with 0', () => {
|
|
165
|
-
expect(
|
|
166
|
-
sanitizeCropperState(
|
|
167
|
-
makeBaseState( { rotation: Number.MAX_VALUE } )
|
|
168
|
-
).rotation
|
|
169
|
-
).toBe( 0 );
|
|
170
|
-
expect(
|
|
171
|
-
sanitizeCropperState( makeBaseState( { rotation: Number.NaN } ) )
|
|
172
|
-
.rotation
|
|
173
|
-
).toBe( 0 );
|
|
174
|
-
} );
|
|
175
|
-
|
|
176
|
-
it( 'sanitizes cropRect fields independently', () => {
|
|
177
|
-
const state = makeBaseState( {
|
|
178
|
-
cropRect: {
|
|
179
|
-
x: Number.NaN,
|
|
180
|
-
y: 0.1,
|
|
181
|
-
width: Number.POSITIVE_INFINITY,
|
|
182
|
-
height: 0.5,
|
|
183
|
-
},
|
|
184
|
-
} );
|
|
185
|
-
const out = sanitizeCropperState( state );
|
|
186
|
-
expect( out.cropRect ).toEqual( {
|
|
187
|
-
x: 0,
|
|
188
|
-
y: 0.1,
|
|
189
|
-
width: 0,
|
|
190
|
-
height: 0.5,
|
|
191
|
-
} );
|
|
192
|
-
} );
|
|
193
|
-
|
|
194
|
-
it( 'sanitizes base pose fields (basePan, baseZoom, baseRotation)', () => {
|
|
195
|
-
const state = makeBaseState( {
|
|
196
|
-
basePan: { x: Number.NaN, y: 0 },
|
|
197
|
-
baseZoom: Number.NEGATIVE_INFINITY,
|
|
198
|
-
baseRotation: Number.MAX_VALUE,
|
|
199
|
-
} );
|
|
200
|
-
const out = sanitizeCropperState( state );
|
|
201
|
-
expect( out.basePan ).toEqual( { x: 0, y: 0 } );
|
|
202
|
-
expect( out.baseZoom ).toBe( 1 );
|
|
203
|
-
expect( out.baseRotation ).toBe( 0 );
|
|
204
|
-
} );
|
|
205
|
-
|
|
206
|
-
it( 'returns the same reference when no field needs substitution', () => {
|
|
207
|
-
// Called once per pointermove (via restrictPanZoom + createCamera),
|
|
208
|
-
// so the no-change fast path matters. Clean state → same reference,
|
|
209
|
-
// no per-frame GC churn.
|
|
210
|
-
const state = makeBaseState( {
|
|
211
|
-
pan: { x: 0.1, y: -0.05 },
|
|
212
|
-
zoom: 1.5,
|
|
213
|
-
rotation: 30,
|
|
214
|
-
} );
|
|
215
|
-
expect( sanitizeCropperState( state ) ).toBe( state );
|
|
216
|
-
} );
|
|
217
|
-
|
|
218
|
-
it( 'preserves sub-object references when only some fields change', () => {
|
|
219
|
-
// If pan is clean but zoom is corrupted, pan should keep its
|
|
220
|
-
// reference even though the parent state must be a new object.
|
|
221
|
-
const pan = { x: 0.1, y: -0.05 };
|
|
222
|
-
const state = makeBaseState( { pan, zoom: Number.NaN } );
|
|
223
|
-
const out = sanitizeCropperState( state );
|
|
224
|
-
expect( out ).not.toBe( state );
|
|
225
|
-
expect( out.pan ).toBe( pan );
|
|
226
|
-
expect( out.zoom ).toBe( 1 );
|
|
227
|
-
} );
|
|
228
|
-
|
|
229
|
-
it( 'leaves state.image untouched (caller passes imageSize separately)', () => {
|
|
230
|
-
// The math layer never reads state.image — image dimensions arrive as
|
|
231
|
-
// a separate `imageSize` argument. Cloning state.image on every call
|
|
232
|
-
// would break the reducer's reference-equality short-circuit.
|
|
233
|
-
const image = {
|
|
234
|
-
src: 'test.jpg',
|
|
235
|
-
naturalWidth: 1600,
|
|
236
|
-
naturalHeight: 900,
|
|
237
|
-
};
|
|
238
|
-
const state = makeBaseState( { image } );
|
|
239
|
-
const out = sanitizeCropperState( state );
|
|
240
|
-
expect( out.image ).toBe( image );
|
|
241
|
-
} );
|
|
242
|
-
} );
|