@wordpress/media-editor 0.17.2-next.v.202608281122.0 → 0.18.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 +1 -1
- package/build/components/media-editor/index.cjs +43 -11
- package/build/components/media-editor/index.cjs.map +2 -2
- package/build/components/media-editor-canvas/index.cjs +4 -2
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +4 -2
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-fine-rotation/index.cjs +4 -2
- package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
- package/build/components/media-editor-image-controls/index.cjs +17 -4
- package/build/components/media-editor-image-controls/index.cjs.map +2 -2
- package/build/components/rotation-ruler/index.cjs +1 -0
- package/build/components/rotation-ruler/index.cjs.map +2 -2
- package/build/components/rotation-ruler/use-ruler-drag.cjs +7 -2
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs +3 -0
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +5 -3
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +12 -5
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs +31 -0
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +43 -11
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor-canvas/index.mjs +4 -2
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +4 -2
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs +4 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
- package/build-module/components/media-editor-image-controls/index.mjs +17 -4
- package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/index.mjs +1 -0
- package/build-module/components/rotation-ruler/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs +7 -2
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs +3 -0
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +5 -3
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +12 -5
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs +31 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-style/style-rtl.css +5 -12
- package/build-style/style.css +5 -12
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor-canvas/index.d.ts +7 -1
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +4 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-fine-rotation/index.d.ts +7 -1
- package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts +7 -1
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +7 -0
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts +5 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/global.d.ts +1 -0
- package/package.json +23 -22
- package/src/components/media-editor/index.tsx +42 -5
- package/src/components/media-editor/test/{use-crop-options.tsx → use-crop-options.jsdom.test.tsx} +1 -0
- package/src/components/media-editor-canvas/index.tsx +8 -0
- package/src/components/media-editor-crop-panel/index.tsx +6 -1
- package/src/components/media-editor-crop-panel/test/{index.tsx → index.jsdom.test.tsx} +5 -2
- package/src/components/media-editor-fine-rotation/index.tsx +8 -0
- package/src/components/media-editor-image-controls/index.tsx +22 -3
- package/src/components/media-editor-image-controls/test/{index.tsx → index.jsdom.test.tsx} +37 -0
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +24 -14
- package/src/components/media-editor-modal/test/{index.tsx → index.jsdom.test.tsx} +65 -35
- package/src/components/rotation-ruler/index.tsx +1 -0
- package/src/components/rotation-ruler/style.scss +1 -1
- package/src/components/rotation-ruler/test/{index.tsx → index.jsdom.test.tsx} +42 -4
- package/src/components/rotation-ruler/use-ruler-drag.ts +10 -2
- package/src/image-editor/core/export/test/{canvas-renderer.ts → canvas-renderer.jsdom.test.ts} +76 -63
- package/src/image-editor/core/interaction-controller.ts +8 -0
- package/src/image-editor/core/math/test/sanitize.ts +1 -0
- package/src/image-editor/core/test/camera.ts +2 -3
- package/src/image-editor/core/test/interaction-controller.ts +50 -40
- package/src/image-editor/core/test/preview-export-parity.ts +31 -23
- package/src/image-editor/core/test/render-camera-bridge.ts +1 -0
- package/src/image-editor/core/test/state.ts +1 -0
- package/src/image-editor/core/test/stencil-math.ts +1 -0
- package/src/image-editor/core/transforms/test/pipeline.ts +1 -0
- package/src/image-editor/react/components/cropper.tsx +12 -1
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +22 -6
- package/src/image-editor/react/components/stencils/test/{rectangle-stencil.tsx → rectangle-stencil.jsdom.test.tsx} +54 -36
- package/src/image-editor/react/components/test/{cropper.tsx → cropper.jsdom.test.tsx} +304 -0
- package/src/image-editor/react/hooks/test/{use-aria-announcer.ts → use-aria-announcer.jsdom.test.ts} +57 -44
- package/src/image-editor/react/hooks/test/{use-cropper-reducer.ts → use-cropper-reducer.jsdom.test.ts} +1 -0
- package/src/image-editor/react/hooks/test/{use-interaction.ts → use-interaction.jsdom.test.ts} +17 -16
- package/src/image-editor/react/hooks/test/{use-transform-style.ts → use-transform-style.jsdom.test.ts} +1 -0
- package/src/image-editor/react/hooks/use-interaction.ts +47 -0
- package/src/state/test/{use-media-editor-state.ts → use-media-editor-state.jsdom.test.ts} +1 -0
- package/src/store/test/reducer.ts +9 -8
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.json +8 -3
package/src/image-editor/core/export/test/{canvas-renderer.ts → canvas-renderer.jsdom.test.ts}
RENAMED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
afterEach,
|
|
3
|
+
beforeEach,
|
|
4
|
+
describe,
|
|
5
|
+
expect,
|
|
6
|
+
it,
|
|
7
|
+
vi,
|
|
8
|
+
type Mock,
|
|
9
|
+
} from 'vitest';
|
|
1
10
|
import type { CropperState } from '../../types';
|
|
2
11
|
import { DEFAULT_STATE } from '../../constants';
|
|
3
12
|
import {
|
|
@@ -14,18 +23,18 @@ import {
|
|
|
14
23
|
*/
|
|
15
24
|
function createMockContext() {
|
|
16
25
|
return {
|
|
17
|
-
setTransform:
|
|
18
|
-
translate:
|
|
19
|
-
rotate:
|
|
20
|
-
scale:
|
|
21
|
-
drawImage:
|
|
22
|
-
getImageData:
|
|
23
|
-
putImageData:
|
|
24
|
-
beginPath:
|
|
25
|
-
closePath:
|
|
26
|
-
moveTo:
|
|
27
|
-
lineTo:
|
|
28
|
-
clip:
|
|
26
|
+
setTransform: vi.fn(),
|
|
27
|
+
translate: vi.fn(),
|
|
28
|
+
rotate: vi.fn(),
|
|
29
|
+
scale: vi.fn(),
|
|
30
|
+
drawImage: vi.fn(),
|
|
31
|
+
getImageData: vi.fn(),
|
|
32
|
+
putImageData: vi.fn(),
|
|
33
|
+
beginPath: vi.fn(),
|
|
34
|
+
closePath: vi.fn(),
|
|
35
|
+
moveTo: vi.fn(),
|
|
36
|
+
lineTo: vi.fn(),
|
|
37
|
+
clip: vi.fn(),
|
|
29
38
|
};
|
|
30
39
|
}
|
|
31
40
|
|
|
@@ -38,9 +47,9 @@ function createMockCanvas( ctx: ReturnType< typeof createMockContext > ) {
|
|
|
38
47
|
const canvas = {
|
|
39
48
|
width: 0,
|
|
40
49
|
height: 0,
|
|
41
|
-
getContext:
|
|
42
|
-
toBlob:
|
|
43
|
-
toDataURL:
|
|
50
|
+
getContext: vi.fn().mockReturnValue( ctx ),
|
|
51
|
+
toBlob: vi.fn(),
|
|
52
|
+
toDataURL: vi.fn().mockReturnValue( 'data:image/png;base64,abc' ),
|
|
44
53
|
};
|
|
45
54
|
return canvas as unknown as HTMLCanvasElement;
|
|
46
55
|
}
|
|
@@ -68,7 +77,7 @@ function createTestState( overrides?: Partial< CropperState > ): CropperState {
|
|
|
68
77
|
describe( 'loadImage', () => {
|
|
69
78
|
it( 'should create an image element with crossOrigin set', async () => {
|
|
70
79
|
const mockImage = {
|
|
71
|
-
addEventListener:
|
|
80
|
+
addEventListener: vi.fn(),
|
|
72
81
|
set crossOrigin( value: string ) {
|
|
73
82
|
this._crossOrigin = value;
|
|
74
83
|
},
|
|
@@ -94,7 +103,9 @@ describe( 'loadImage', () => {
|
|
|
94
103
|
|
|
95
104
|
// Replace global Image constructor.
|
|
96
105
|
const originalImage = global.Image;
|
|
97
|
-
global.Image =
|
|
106
|
+
global.Image = vi.fn( function Image() {
|
|
107
|
+
return mockImage;
|
|
108
|
+
} ) as unknown as typeof Image;
|
|
98
109
|
|
|
99
110
|
try {
|
|
100
111
|
const result = await loadImage( 'https://example.com/test.jpg' );
|
|
@@ -117,7 +128,7 @@ describe( 'loadImage', () => {
|
|
|
117
128
|
it( 'should reject when the image fails to load', async () => {
|
|
118
129
|
const loadError = new Error( 'Network error' );
|
|
119
130
|
const mockImage = {
|
|
120
|
-
addEventListener:
|
|
131
|
+
addEventListener: vi.fn(),
|
|
121
132
|
set crossOrigin( _value: string ) {},
|
|
122
133
|
set src( _value: string ) {
|
|
123
134
|
// Simulate error on next tick.
|
|
@@ -131,7 +142,9 @@ describe( 'loadImage', () => {
|
|
|
131
142
|
};
|
|
132
143
|
|
|
133
144
|
const originalImage = global.Image;
|
|
134
|
-
global.Image =
|
|
145
|
+
global.Image = vi.fn( function Image() {
|
|
146
|
+
return mockImage;
|
|
147
|
+
} ) as unknown as typeof Image;
|
|
135
148
|
|
|
136
149
|
try {
|
|
137
150
|
await expect(
|
|
@@ -152,7 +165,7 @@ describe( 'renderToCanvas', () => {
|
|
|
152
165
|
canvasCount = 0;
|
|
153
166
|
|
|
154
167
|
// Mock document.createElement to return our mock canvases.
|
|
155
|
-
|
|
168
|
+
vi.spyOn( document, 'createElement' ).mockImplementation(
|
|
156
169
|
( tag: string ) => {
|
|
157
170
|
if ( tag === 'canvas' ) {
|
|
158
171
|
canvasCount++;
|
|
@@ -166,7 +179,7 @@ describe( 'renderToCanvas', () => {
|
|
|
166
179
|
} );
|
|
167
180
|
|
|
168
181
|
afterEach( () => {
|
|
169
|
-
|
|
182
|
+
vi.restoreAllMocks();
|
|
170
183
|
} );
|
|
171
184
|
|
|
172
185
|
it( 'should call setTransform and drawImage', () => {
|
|
@@ -211,8 +224,8 @@ describe( 'renderToCanvas', () => {
|
|
|
211
224
|
} );
|
|
212
225
|
const mockCtxNoFlip = createMockContext();
|
|
213
226
|
const canvasCountRef = { value: 0 };
|
|
214
|
-
|
|
215
|
-
|
|
227
|
+
vi.restoreAllMocks();
|
|
228
|
+
vi.spyOn( document, 'createElement' ).mockImplementation(
|
|
216
229
|
( tag: string ) => {
|
|
217
230
|
if ( tag === 'canvas' ) {
|
|
218
231
|
canvasCountRef.value++;
|
|
@@ -252,8 +265,8 @@ describe( 'renderToCanvas', () => {
|
|
|
252
265
|
const mockCtxRotated = createMockContext();
|
|
253
266
|
const mockCtxNormal = createMockContext();
|
|
254
267
|
const canvasCountRef = { value: 0 };
|
|
255
|
-
|
|
256
|
-
|
|
268
|
+
vi.restoreAllMocks();
|
|
269
|
+
vi.spyOn( document, 'createElement' ).mockImplementation(
|
|
257
270
|
( tag: string ) => {
|
|
258
271
|
if ( tag === 'canvas' ) {
|
|
259
272
|
canvasCountRef.value++;
|
|
@@ -287,7 +300,7 @@ describe( 'canvasToBlob', () => {
|
|
|
287
300
|
it( 'should call canvas.toBlob with the correct arguments', async () => {
|
|
288
301
|
const mockBlob = new Blob( [ 'test' ], { type: 'image/png' } );
|
|
289
302
|
const mockCanvas = {
|
|
290
|
-
toBlob:
|
|
303
|
+
toBlob: vi.fn( ( callback: BlobCallback ) => {
|
|
291
304
|
callback( mockBlob );
|
|
292
305
|
} ),
|
|
293
306
|
} as unknown as HTMLCanvasElement;
|
|
@@ -305,7 +318,7 @@ describe( 'canvasToBlob', () => {
|
|
|
305
318
|
it( 'should use default mime type and quality when not specified', async () => {
|
|
306
319
|
const mockBlob = new Blob( [ 'test' ], { type: 'image/png' } );
|
|
307
320
|
const mockCanvas = {
|
|
308
|
-
toBlob:
|
|
321
|
+
toBlob: vi.fn( ( callback: BlobCallback ) => {
|
|
309
322
|
callback( mockBlob );
|
|
310
323
|
} ),
|
|
311
324
|
} as unknown as HTMLCanvasElement;
|
|
@@ -321,7 +334,7 @@ describe( 'canvasToBlob', () => {
|
|
|
321
334
|
|
|
322
335
|
it( 'should reject when toBlob returns null', async () => {
|
|
323
336
|
const mockCanvas = {
|
|
324
|
-
toBlob:
|
|
337
|
+
toBlob: vi.fn( ( callback: BlobCallback ) => {
|
|
325
338
|
callback( null );
|
|
326
339
|
} ),
|
|
327
340
|
} as unknown as HTMLCanvasElement;
|
|
@@ -335,9 +348,7 @@ describe( 'canvasToBlob', () => {
|
|
|
335
348
|
describe( 'canvasToDataURL', () => {
|
|
336
349
|
it( 'should call canvas.toDataURL with correct arguments', () => {
|
|
337
350
|
const mockCanvas = {
|
|
338
|
-
toDataURL:
|
|
339
|
-
.fn()
|
|
340
|
-
.mockReturnValue( 'data:image/jpeg;base64,abc' ),
|
|
351
|
+
toDataURL: vi.fn().mockReturnValue( 'data:image/jpeg;base64,abc' ),
|
|
341
352
|
} as unknown as HTMLCanvasElement;
|
|
342
353
|
|
|
343
354
|
const result = canvasToDataURL( mockCanvas, 'image/jpeg', 0.85 );
|
|
@@ -351,7 +362,7 @@ describe( 'canvasToDataURL', () => {
|
|
|
351
362
|
|
|
352
363
|
it( 'should use default mime type and quality when not specified', () => {
|
|
353
364
|
const mockCanvas = {
|
|
354
|
-
toDataURL:
|
|
365
|
+
toDataURL: vi.fn().mockReturnValue( 'data:image/png;base64,xyz' ),
|
|
355
366
|
} as unknown as HTMLCanvasElement;
|
|
356
367
|
|
|
357
368
|
canvasToDataURL( mockCanvas );
|
|
@@ -373,7 +384,7 @@ describe( 'exportCroppedImage', () => {
|
|
|
373
384
|
const mockImage = {
|
|
374
385
|
naturalWidth: 800,
|
|
375
386
|
naturalHeight: 600,
|
|
376
|
-
addEventListener:
|
|
387
|
+
addEventListener: vi.fn(),
|
|
377
388
|
_crossOrigin: '',
|
|
378
389
|
set crossOrigin( value: string ) {
|
|
379
390
|
this._crossOrigin = value;
|
|
@@ -395,16 +406,18 @@ describe( 'exportCroppedImage', () => {
|
|
|
395
406
|
return this._src;
|
|
396
407
|
},
|
|
397
408
|
};
|
|
398
|
-
global.Image =
|
|
409
|
+
global.Image = vi.fn( function Image() {
|
|
410
|
+
return mockImage;
|
|
411
|
+
} ) as unknown as typeof Image;
|
|
399
412
|
|
|
400
413
|
// Mock document.createElement for renderToCanvas.
|
|
401
414
|
const mockBlob = new Blob( [ 'test' ], { type: 'image/png' } );
|
|
402
415
|
const mockCtx = createMockContext();
|
|
403
|
-
|
|
416
|
+
vi.spyOn( document, 'createElement' ).mockImplementation(
|
|
404
417
|
( tag: string ) => {
|
|
405
418
|
if ( tag === 'canvas' ) {
|
|
406
419
|
const canvas = createMockCanvas( mockCtx );
|
|
407
|
-
( canvas as any ).toBlob =
|
|
420
|
+
( canvas as any ).toBlob = vi.fn(
|
|
408
421
|
( callback: BlobCallback ) => {
|
|
409
422
|
callback( mockBlob );
|
|
410
423
|
}
|
|
@@ -418,7 +431,7 @@ describe( 'exportCroppedImage', () => {
|
|
|
418
431
|
|
|
419
432
|
afterEach( () => {
|
|
420
433
|
global.Image = originalImage;
|
|
421
|
-
|
|
434
|
+
vi.restoreAllMocks();
|
|
422
435
|
} );
|
|
423
436
|
|
|
424
437
|
it( 'should chain loadImage, renderToCanvas, and canvasToBlob', async () => {
|
|
@@ -436,7 +449,7 @@ describe( 'exportCroppedImage', () => {
|
|
|
436
449
|
it( 'throws when image fails to load', async () => {
|
|
437
450
|
// Override Image to simulate a load error.
|
|
438
451
|
const errorImage = {
|
|
439
|
-
addEventListener:
|
|
452
|
+
addEventListener: vi.fn(),
|
|
440
453
|
set crossOrigin( _value: string ) {},
|
|
441
454
|
set src( _value: string ) {
|
|
442
455
|
const errorHandler = this.addEventListener.mock.calls.find(
|
|
@@ -447,7 +460,9 @@ describe( 'exportCroppedImage', () => {
|
|
|
447
460
|
}
|
|
448
461
|
},
|
|
449
462
|
};
|
|
450
|
-
global.Image =
|
|
463
|
+
global.Image = vi.fn( function Image() {
|
|
464
|
+
return errorImage;
|
|
465
|
+
} ) as unknown as typeof Image;
|
|
451
466
|
|
|
452
467
|
const state = createTestState();
|
|
453
468
|
await expect(
|
|
@@ -459,22 +474,20 @@ describe( 'exportCroppedImage', () => {
|
|
|
459
474
|
// Mock Image to resolve (simulating a successful load even from
|
|
460
475
|
// a tainted source — the browser doesn't flag the load itself,
|
|
461
476
|
// only the later toBlob call).
|
|
462
|
-
global.Image =
|
|
477
|
+
global.Image = vi.fn( function Image() {
|
|
463
478
|
const img: {
|
|
464
|
-
addEventListener:
|
|
479
|
+
addEventListener: Mock;
|
|
465
480
|
crossOrigin: string;
|
|
466
481
|
src: string;
|
|
467
482
|
naturalWidth: number;
|
|
468
483
|
naturalHeight: number;
|
|
469
484
|
} = {
|
|
470
|
-
addEventListener:
|
|
471
|
-
( event
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
queueMicrotask( fn );
|
|
475
|
-
}
|
|
485
|
+
addEventListener: vi.fn( ( event: string, fn: () => void ) => {
|
|
486
|
+
if ( event === 'load' ) {
|
|
487
|
+
// Fire load synchronously after src is set.
|
|
488
|
+
queueMicrotask( fn );
|
|
476
489
|
}
|
|
477
|
-
),
|
|
490
|
+
} ),
|
|
478
491
|
crossOrigin: '',
|
|
479
492
|
src: '',
|
|
480
493
|
naturalWidth: 800,
|
|
@@ -487,18 +500,18 @@ describe( 'exportCroppedImage', () => {
|
|
|
487
500
|
const corsCanvas = {
|
|
488
501
|
width: 0,
|
|
489
502
|
height: 0,
|
|
490
|
-
getContext:
|
|
491
|
-
setTransform:
|
|
492
|
-
drawImage:
|
|
503
|
+
getContext: vi.fn( () => ( {
|
|
504
|
+
setTransform: vi.fn(),
|
|
505
|
+
drawImage: vi.fn(),
|
|
493
506
|
} ) ),
|
|
494
|
-
toBlob:
|
|
507
|
+
toBlob: vi.fn( () => {
|
|
495
508
|
throw new DOMException(
|
|
496
509
|
'Tainted canvases may not be exported',
|
|
497
510
|
'SecurityError'
|
|
498
511
|
);
|
|
499
512
|
} ),
|
|
500
513
|
};
|
|
501
|
-
|
|
514
|
+
vi.spyOn( document, 'createElement' ).mockImplementation( ( (
|
|
502
515
|
tag: string
|
|
503
516
|
) => {
|
|
504
517
|
if ( tag === 'canvas' ) {
|
|
@@ -515,7 +528,7 @@ describe( 'exportCroppedImage', () => {
|
|
|
515
528
|
)
|
|
516
529
|
).rejects.toMatchObject( { name: 'SecurityError' } );
|
|
517
530
|
|
|
518
|
-
|
|
531
|
+
vi.restoreAllMocks();
|
|
519
532
|
} );
|
|
520
533
|
|
|
521
534
|
it( 'throws descriptive error when canvas context is unavailable', () => {
|
|
@@ -523,9 +536,9 @@ describe( 'exportCroppedImage', () => {
|
|
|
523
536
|
const noCtxCanvas = {
|
|
524
537
|
width: 0,
|
|
525
538
|
height: 0,
|
|
526
|
-
getContext:
|
|
539
|
+
getContext: vi.fn( () => null ),
|
|
527
540
|
};
|
|
528
|
-
|
|
541
|
+
vi.spyOn( document, 'createElement' ).mockImplementation( ( (
|
|
529
542
|
tag: string
|
|
530
543
|
) => {
|
|
531
544
|
if ( tag === 'canvas' ) {
|
|
@@ -544,7 +557,7 @@ describe( 'exportCroppedImage', () => {
|
|
|
544
557
|
/2D context/i
|
|
545
558
|
);
|
|
546
559
|
|
|
547
|
-
|
|
560
|
+
vi.restoreAllMocks();
|
|
548
561
|
} );
|
|
549
562
|
} );
|
|
550
563
|
|
|
@@ -553,7 +566,7 @@ describe( 'renderToCanvas — export matrix verification', () => {
|
|
|
553
566
|
|
|
554
567
|
function setupMockCanvas() {
|
|
555
568
|
mockCtx = createMockContext();
|
|
556
|
-
|
|
569
|
+
vi.spyOn( document, 'createElement' ).mockImplementation(
|
|
557
570
|
( tag: string ) => {
|
|
558
571
|
if ( tag === 'canvas' ) {
|
|
559
572
|
return createMockCanvas(
|
|
@@ -566,7 +579,7 @@ describe( 'renderToCanvas — export matrix verification', () => {
|
|
|
566
579
|
}
|
|
567
580
|
|
|
568
581
|
afterEach( () => {
|
|
569
|
-
|
|
582
|
+
vi.restoreAllMocks();
|
|
570
583
|
} );
|
|
571
584
|
|
|
572
585
|
it( 'identity state (no rotation, no zoom, full crop) should produce a 1:1 mapping', () => {
|
|
@@ -652,7 +665,7 @@ describe( 'renderToCanvas — export matrix verification', () => {
|
|
|
652
665
|
const [ a1, , , d1 ] = mockCtx.setTransform.mock.calls[ 0 ];
|
|
653
666
|
|
|
654
667
|
// Reset for zoom=2.
|
|
655
|
-
|
|
668
|
+
vi.restoreAllMocks();
|
|
656
669
|
setupMockCanvas();
|
|
657
670
|
const stateZ2 = createTestState( {
|
|
658
671
|
rotation: 0,
|
|
@@ -688,7 +701,7 @@ describe( 'renderToCanvas — export matrix verification', () => {
|
|
|
688
701
|
const [ aNoFlip ] = mockCtx.setTransform.mock.calls[ 0 ];
|
|
689
702
|
|
|
690
703
|
// Reset for horizontal flip.
|
|
691
|
-
|
|
704
|
+
vi.restoreAllMocks();
|
|
692
705
|
setupMockCanvas();
|
|
693
706
|
const stateFlip = createTestState( {
|
|
694
707
|
rotation: 0,
|
|
@@ -716,7 +729,7 @@ describe( 'applyToCanvas — export matrix verification', () => {
|
|
|
716
729
|
mockCtx = createMockContext();
|
|
717
730
|
canvasWidths = [];
|
|
718
731
|
canvasHeights = [];
|
|
719
|
-
|
|
732
|
+
vi.spyOn( document, 'createElement' ).mockImplementation(
|
|
720
733
|
( tag: string ) => {
|
|
721
734
|
if ( tag === 'canvas' ) {
|
|
722
735
|
const canvas = createMockCanvas( mockCtx );
|
|
@@ -747,7 +760,7 @@ describe( 'applyToCanvas — export matrix verification', () => {
|
|
|
747
760
|
}
|
|
748
761
|
|
|
749
762
|
afterEach( () => {
|
|
750
|
-
|
|
763
|
+
vi.restoreAllMocks();
|
|
751
764
|
} );
|
|
752
765
|
|
|
753
766
|
it( 'should create a canvas with correct dimensions and call setTransform', () => {
|
|
@@ -956,5 +956,13 @@ export class InteractionController {
|
|
|
956
956
|
this.drag = null;
|
|
957
957
|
this.touch = null;
|
|
958
958
|
this.lastTap = null;
|
|
959
|
+
// Reset the gesture bookkeeping too. The timers above are cancelled
|
|
960
|
+
// rather than run, so these flags would otherwise stay set: a stale
|
|
961
|
+
// `wheelGestureActive` suppresses the next wheel gesture's start,
|
|
962
|
+
// and `setStatus` dedupes against `isDragging` / `isZooming`, so a
|
|
963
|
+
// stale value swallows the next real change.
|
|
964
|
+
this.wheelGestureActive = false;
|
|
965
|
+
this.isDragging = false;
|
|
966
|
+
this.isZooming = false;
|
|
959
967
|
}
|
|
960
968
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { mat2d as m2d, vec2 } from 'gl-matrix';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
1
3
|
import {
|
|
2
4
|
createCamera,
|
|
3
5
|
worldToScreen,
|
|
@@ -302,7 +304,6 @@ describe( 'createExportCamera', () => {
|
|
|
302
304
|
const state = makeState();
|
|
303
305
|
const outputSize = { width: 400, height: 225 };
|
|
304
306
|
const camera = createExportCamera( state, IMAGE, outputSize );
|
|
305
|
-
const { vec2 } = require( 'gl-matrix' );
|
|
306
307
|
const out = vec2.create();
|
|
307
308
|
vec2.transformMat2d(
|
|
308
309
|
out,
|
|
@@ -317,7 +318,6 @@ describe( 'createExportCamera', () => {
|
|
|
317
318
|
const state = makeState();
|
|
318
319
|
const outputSize = { width: IMAGE.width, height: IMAGE.height };
|
|
319
320
|
const camera = createExportCamera( state, IMAGE, outputSize );
|
|
320
|
-
const { vec2 } = require( 'gl-matrix' );
|
|
321
321
|
const topLeft = vec2.create();
|
|
322
322
|
vec2.transformMat2d( topLeft, [ 0, 0 ], camera );
|
|
323
323
|
expect( topLeft[ 0 ] ).toBeCloseTo( 0, 0 );
|
|
@@ -381,7 +381,6 @@ describe( 'createExportCamera', () => {
|
|
|
381
381
|
// output canvas.
|
|
382
382
|
const outputSize = { width: 400, height: 300 };
|
|
383
383
|
const exportCamera = createExportCamera( state, IMAGE, outputSize );
|
|
384
|
-
const { mat2d: m2d, vec2 } = require( 'gl-matrix' );
|
|
385
384
|
const inv = m2d.create();
|
|
386
385
|
m2d.invert( inv, exportCamera );
|
|
387
386
|
const exportPxVec = vec2.create();
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import {
|
|
2
|
+
afterAll,
|
|
3
|
+
beforeAll,
|
|
4
|
+
beforeEach,
|
|
5
|
+
describe,
|
|
6
|
+
expect,
|
|
7
|
+
it,
|
|
8
|
+
vi,
|
|
9
|
+
type Mocked,
|
|
10
|
+
} from 'vitest';
|
|
1
11
|
import {
|
|
2
12
|
InteractionController,
|
|
3
13
|
type CropperInteractionActions,
|
|
@@ -52,7 +62,7 @@ function createPointerEvent(
|
|
|
52
62
|
clientX: 0,
|
|
53
63
|
clientY: 0,
|
|
54
64
|
pointerId: 1,
|
|
55
|
-
preventDefault:
|
|
65
|
+
preventDefault: vi.fn(),
|
|
56
66
|
...overrides,
|
|
57
67
|
} as unknown as PointerEvent;
|
|
58
68
|
}
|
|
@@ -67,16 +77,16 @@ function createMockElement(): HTMLElement & {
|
|
|
67
77
|
} {
|
|
68
78
|
const listeners: Record< string, EventListener[] > = {};
|
|
69
79
|
return {
|
|
70
|
-
focus:
|
|
71
|
-
setPointerCapture:
|
|
72
|
-
releasePointerCapture:
|
|
73
|
-
addEventListener:
|
|
80
|
+
focus: vi.fn(),
|
|
81
|
+
setPointerCapture: vi.fn(),
|
|
82
|
+
releasePointerCapture: vi.fn(),
|
|
83
|
+
addEventListener: vi.fn( ( type: string, fn: EventListener ) => {
|
|
74
84
|
if ( ! listeners[ type ] ) {
|
|
75
85
|
listeners[ type ] = [];
|
|
76
86
|
}
|
|
77
87
|
listeners[ type ].push( fn );
|
|
78
88
|
} ),
|
|
79
|
-
removeEventListener:
|
|
89
|
+
removeEventListener: vi.fn( ( type: string, fn: EventListener ) => {
|
|
80
90
|
if ( listeners[ type ] ) {
|
|
81
91
|
listeners[ type ] = listeners[ type ].filter(
|
|
82
92
|
( l ) => l !== fn
|
|
@@ -106,7 +116,7 @@ function createWheelEvent(
|
|
|
106
116
|
overrides: Partial< WheelEvent > & { currentTarget?: unknown } = {}
|
|
107
117
|
): WheelEvent {
|
|
108
118
|
return {
|
|
109
|
-
preventDefault:
|
|
119
|
+
preventDefault: vi.fn(),
|
|
110
120
|
deltaY: 0,
|
|
111
121
|
clientX: 0,
|
|
112
122
|
clientY: 0,
|
|
@@ -127,7 +137,7 @@ function createKeyboardEvent(
|
|
|
127
137
|
): KeyboardEvent {
|
|
128
138
|
return {
|
|
129
139
|
key,
|
|
130
|
-
preventDefault:
|
|
140
|
+
preventDefault: vi.fn(),
|
|
131
141
|
...overrides,
|
|
132
142
|
} as unknown as KeyboardEvent;
|
|
133
143
|
}
|
|
@@ -144,7 +154,7 @@ function createTouchEvent(
|
|
|
144
154
|
overrides: Partial< TouchEvent > = {}
|
|
145
155
|
): TouchEvent {
|
|
146
156
|
return {
|
|
147
|
-
preventDefault:
|
|
157
|
+
preventDefault: vi.fn(),
|
|
148
158
|
touches,
|
|
149
159
|
...overrides,
|
|
150
160
|
} as unknown as TouchEvent;
|
|
@@ -166,7 +176,7 @@ function createContainerRect( overrides: Partial< DOMRect > = {} ): DOMRect {
|
|
|
166
176
|
bottom: 300,
|
|
167
177
|
x: 0,
|
|
168
178
|
y: 0,
|
|
169
|
-
toJSON:
|
|
179
|
+
toJSON: vi.fn(),
|
|
170
180
|
...overrides,
|
|
171
181
|
} as DOMRect;
|
|
172
182
|
}
|
|
@@ -182,13 +192,13 @@ function createMockDocument(): Document & {
|
|
|
182
192
|
} {
|
|
183
193
|
const listeners: Record< string, EventListener[] > = {};
|
|
184
194
|
return {
|
|
185
|
-
addEventListener:
|
|
195
|
+
addEventListener: vi.fn( ( type: string, fn: EventListener ) => {
|
|
186
196
|
if ( ! listeners[ type ] ) {
|
|
187
197
|
listeners[ type ] = [];
|
|
188
198
|
}
|
|
189
199
|
listeners[ type ].push( fn );
|
|
190
200
|
} ),
|
|
191
|
-
removeEventListener:
|
|
201
|
+
removeEventListener: vi.fn( ( type: string, fn: EventListener ) => {
|
|
192
202
|
if ( listeners[ type ] ) {
|
|
193
203
|
listeners[ type ] = listeners[ type ].filter(
|
|
194
204
|
( l ) => l !== fn
|
|
@@ -208,7 +218,7 @@ function createMockDocument(): Document & {
|
|
|
208
218
|
describe( 'InteractionController', () => {
|
|
209
219
|
const containerSize: Size = { width: 500, height: 300 };
|
|
210
220
|
const imageSize: Size = { width: 500, height: 300 };
|
|
211
|
-
let actionMocks:
|
|
221
|
+
let actionMocks: Mocked< CropperInteractionActions >;
|
|
212
222
|
|
|
213
223
|
// Store original requestAnimationFrame so we can restore it.
|
|
214
224
|
const originalRAF = globalThis.requestAnimationFrame;
|
|
@@ -220,7 +230,7 @@ describe( 'InteractionController', () => {
|
|
|
220
230
|
cb( 0 );
|
|
221
231
|
return 0;
|
|
222
232
|
};
|
|
223
|
-
globalThis.cancelAnimationFrame =
|
|
233
|
+
globalThis.cancelAnimationFrame = vi.fn();
|
|
224
234
|
} );
|
|
225
235
|
|
|
226
236
|
afterAll( () => {
|
|
@@ -230,11 +240,11 @@ describe( 'InteractionController', () => {
|
|
|
230
240
|
|
|
231
241
|
beforeEach( () => {
|
|
232
242
|
actionMocks = {
|
|
233
|
-
setPan:
|
|
234
|
-
setZoom:
|
|
235
|
-
setZoomAtPoint:
|
|
236
|
-
snapRotate90:
|
|
237
|
-
toggleFlip:
|
|
243
|
+
setPan: vi.fn(),
|
|
244
|
+
setZoom: vi.fn(),
|
|
245
|
+
setZoomAtPoint: vi.fn(),
|
|
246
|
+
snapRotate90: vi.fn(),
|
|
247
|
+
toggleFlip: vi.fn(),
|
|
238
248
|
};
|
|
239
249
|
} );
|
|
240
250
|
|
|
@@ -290,8 +300,8 @@ describe( 'InteractionController', () => {
|
|
|
290
300
|
|
|
291
301
|
it( 'ignores touch pointerdown so touch gestures own touch input', () => {
|
|
292
302
|
const state = makeState( { zoom: 2 } );
|
|
293
|
-
const onGestureStart =
|
|
294
|
-
const onStatusChange =
|
|
303
|
+
const onGestureStart = vi.fn();
|
|
304
|
+
const onStatusChange = vi.fn();
|
|
295
305
|
const { controller } = createController( state, {
|
|
296
306
|
onGestureStart,
|
|
297
307
|
onStatusChange,
|
|
@@ -333,7 +343,7 @@ describe( 'InteractionController', () => {
|
|
|
333
343
|
// Simulate pointerup.
|
|
334
344
|
el._fire( 'pointerup', createPointerEvent() );
|
|
335
345
|
|
|
336
|
-
|
|
346
|
+
vi.clearAllMocks();
|
|
337
347
|
|
|
338
348
|
// Another pointermove should not dispatch because the listener
|
|
339
349
|
// was removed after pointerup.
|
|
@@ -350,8 +360,8 @@ describe( 'InteractionController', () => {
|
|
|
350
360
|
|
|
351
361
|
it( 'calls onGestureStart on pointerdown and onGestureEnd on pointerup', () => {
|
|
352
362
|
const state = makeState( { zoom: 2 } );
|
|
353
|
-
const onGestureStart =
|
|
354
|
-
const onGestureEnd =
|
|
363
|
+
const onGestureStart = vi.fn();
|
|
364
|
+
const onGestureEnd = vi.fn();
|
|
355
365
|
const { controller } = createController( state, {
|
|
356
366
|
onGestureStart,
|
|
357
367
|
onGestureEnd,
|
|
@@ -380,7 +390,7 @@ describe( 'InteractionController', () => {
|
|
|
380
390
|
|
|
381
391
|
it( 'reports isDragging via onStatusChange', () => {
|
|
382
392
|
const state = makeState( { zoom: 2 } );
|
|
383
|
-
const onStatusChange =
|
|
393
|
+
const onStatusChange = vi.fn();
|
|
384
394
|
const { controller } = createController( state, {
|
|
385
395
|
onStatusChange,
|
|
386
396
|
} );
|
|
@@ -505,7 +515,7 @@ describe( 'InteractionController', () => {
|
|
|
505
515
|
el
|
|
506
516
|
);
|
|
507
517
|
|
|
508
|
-
|
|
518
|
+
vi.clearAllMocks();
|
|
509
519
|
|
|
510
520
|
const wheelEvent = createWheelEvent( {
|
|
511
521
|
deltaY: -100,
|
|
@@ -521,12 +531,12 @@ describe( 'InteractionController', () => {
|
|
|
521
531
|
} );
|
|
522
532
|
|
|
523
533
|
it( 'calls onGestureStart on first wheel, onGestureEnd after debounce', () => {
|
|
524
|
-
|
|
525
|
-
|
|
534
|
+
vi.useFakeTimers( {
|
|
535
|
+
toFake: [ 'setTimeout', 'clearTimeout' ],
|
|
526
536
|
} );
|
|
527
537
|
const state = makeState( { zoom: 2 } );
|
|
528
|
-
const onGestureStart =
|
|
529
|
-
const onGestureEnd =
|
|
538
|
+
const onGestureStart = vi.fn();
|
|
539
|
+
const onGestureEnd = vi.fn();
|
|
530
540
|
const { controller } = createController( state, {
|
|
531
541
|
onGestureStart,
|
|
532
542
|
onGestureEnd,
|
|
@@ -546,11 +556,11 @@ describe( 'InteractionController', () => {
|
|
|
546
556
|
expect( onGestureStart ).toHaveBeenCalledTimes( 1 );
|
|
547
557
|
|
|
548
558
|
// Advance past the 300ms debounce.
|
|
549
|
-
|
|
559
|
+
vi.advanceTimersByTime( 350 );
|
|
550
560
|
|
|
551
561
|
expect( onGestureEnd ).toHaveBeenCalledTimes( 1 );
|
|
552
562
|
|
|
553
|
-
|
|
563
|
+
vi.useRealTimers();
|
|
554
564
|
} );
|
|
555
565
|
} );
|
|
556
566
|
|
|
@@ -848,8 +858,8 @@ describe( 'InteractionController', () => {
|
|
|
848
858
|
|
|
849
859
|
it( 'calls onGestureStart/onGestureEnd for single-finger pan', () => {
|
|
850
860
|
const state = makeState( { zoom: 2 } );
|
|
851
|
-
const onGestureStart =
|
|
852
|
-
const onGestureEnd =
|
|
861
|
+
const onGestureStart = vi.fn();
|
|
862
|
+
const onGestureEnd = vi.fn();
|
|
853
863
|
const { controller } = createController( state, {
|
|
854
864
|
onGestureStart,
|
|
855
865
|
onGestureEnd,
|
|
@@ -873,7 +883,7 @@ describe( 'InteractionController', () => {
|
|
|
873
883
|
|
|
874
884
|
it( 'reports isDragging on first single-finger move', () => {
|
|
875
885
|
const state = makeState( { zoom: 2 } );
|
|
876
|
-
const onStatusChange =
|
|
886
|
+
const onStatusChange = vi.fn();
|
|
877
887
|
const { controller } = createController( state, {
|
|
878
888
|
onStatusChange,
|
|
879
889
|
} );
|
|
@@ -987,8 +997,8 @@ describe( 'InteractionController', () => {
|
|
|
987
997
|
|
|
988
998
|
it( 'calls onGestureStart for pinch, onGestureEnd on touchend', () => {
|
|
989
999
|
const state = makeState( { zoom: 1 } );
|
|
990
|
-
const onGestureStart =
|
|
991
|
-
const onGestureEnd =
|
|
1000
|
+
const onGestureStart = vi.fn();
|
|
1001
|
+
const onGestureEnd = vi.fn();
|
|
992
1002
|
const { controller } = createController( state, {
|
|
993
1003
|
onGestureStart,
|
|
994
1004
|
onGestureEnd,
|
|
@@ -1031,7 +1041,7 @@ describe( 'InteractionController', () => {
|
|
|
1031
1041
|
createTouchEvent( [ { clientX: 210, clientY: 155 } ] )
|
|
1032
1042
|
);
|
|
1033
1043
|
expect( actionMocks.setPan ).toHaveBeenCalled();
|
|
1034
|
-
|
|
1044
|
+
vi.clearAllMocks();
|
|
1035
1045
|
|
|
1036
1046
|
// Second finger arrives via touchstart.
|
|
1037
1047
|
controller.handleTouchStart(
|
|
@@ -1122,7 +1132,7 @@ describe( 'InteractionController', () => {
|
|
|
1122
1132
|
] )
|
|
1123
1133
|
);
|
|
1124
1134
|
|
|
1125
|
-
|
|
1135
|
+
vi.clearAllMocks();
|
|
1126
1136
|
|
|
1127
1137
|
// One finger lifts — move with 1 touch should NOT pan
|
|
1128
1138
|
// because didPinch is true.
|
|
@@ -1225,7 +1235,7 @@ describe( 'InteractionController', () => {
|
|
|
1225
1235
|
createPointerEvent( { clientX: 100, clientY: 100 } ),
|
|
1226
1236
|
el
|
|
1227
1237
|
);
|
|
1228
|
-
|
|
1238
|
+
vi.clearAllMocks();
|
|
1229
1239
|
|
|
1230
1240
|
controller.destroy();
|
|
1231
1241
|
|