@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,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
- } );