@wordpress/media-editor 0.19.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 +34 -0
- package/README.md +2 -1
- package/build/components/media-editor/index.cjs +136 -63
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +2 -3
- package/build/components/media-editor/use-crop-options.cjs.map +2 -2
- package/build/components/media-editor/use-save-media-editor.cjs +36 -20
- package/build/components/media-editor/use-save-media-editor.cjs.map +2 -2
- package/build/components/media-editor-canvas/index.cjs +8 -8
- 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-fine-rotation/index.cjs +1 -1
- package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
- package/build/components/media-editor-image-controls/index.cjs +46 -29
- package/build/components/media-editor-image-controls/index.cjs.map +2 -2
- package/build/components/media-editor-modal/index.cjs +19 -9
- 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/hooks/use-crop-gesture-handlers.cjs +1 -1
- package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs +5 -0
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +4 -14
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +1 -1
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +3 -1
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/state/composite-reducer.cjs +35 -10
- package/build/state/composite-reducer.cjs.map +3 -3
- package/build/state/index.cjs.map +1 -1
- package/build/state/media-editor-state-provider.cjs +1 -6
- package/build/state/media-editor-state-provider.cjs.map +2 -2
- package/build/state/types.cjs.map +1 -1
- package/build/state/use-media-editor-state.cjs +57 -42
- package/build/state/use-media-editor-state.cjs.map +2 -2
- package/build/store/actions.cjs +2 -0
- package/build/store/actions.cjs.map +2 -2
- package/build/store/reducer.cjs +3 -1
- package/build/store/reducer.cjs.map +2 -2
- package/build/store/selectors.cjs +5 -0
- package/build/store/selectors.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +151 -67
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +2 -3
- package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
- package/build-module/components/media-editor/use-save-media-editor.mjs +36 -20
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +2 -2
- package/build-module/components/media-editor-canvas/index.mjs +10 -10
- 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-fine-rotation/index.mjs +1 -1
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
- package/build-module/components/media-editor-image-controls/index.mjs +47 -36
- package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
- package/build-module/components/media-editor-modal/index.mjs +19 -9
- 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/hooks/use-crop-gesture-handlers.mjs +1 -1
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs +5 -0
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +4 -14
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +1 -1
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +3 -1
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/state/composite-reducer.mjs +25 -10
- package/build-module/state/composite-reducer.mjs.map +2 -2
- package/build-module/state/index.mjs.map +1 -1
- package/build-module/state/media-editor-state-provider.mjs +1 -6
- package/build-module/state/media-editor-state-provider.mjs.map +2 -2
- package/build-module/state/types.mjs.map +1 -1
- package/build-module/state/use-media-editor-state.mjs +57 -42
- package/build-module/state/use-media-editor-state.mjs.map +2 -2
- package/build-module/store/actions.mjs +2 -0
- package/build-module/store/actions.mjs.map +2 -2
- package/build-module/store/reducer.mjs +3 -1
- package/build-module/store/reducer.mjs.map +2 -2
- package/build-module/store/selectors.mjs +4 -0
- package/build-module/store/selectors.mjs.map +2 -2
- package/build-style/style-rtl.css +5 -5
- package/build-style/style.css +5 -5
- package/build-types/components/media-editor/index.d.ts +4 -3
- 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/use-save-media-editor.d.ts +15 -3
- package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -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.map +1 -1
- package/build-types/components/media-editor-image-controls/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/hooks/use-crop-gesture-handlers.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.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-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts +5 -5
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/index.d.ts +2 -2
- package/build-types/state/index.d.ts.map +1 -1
- package/build-types/state/media-editor-state-provider.d.ts +8 -16
- package/build-types/state/media-editor-state-provider.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 +53 -35
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/build-types/store/actions.d.ts +9 -1
- package/build-types/store/actions.d.ts.map +1 -1
- package/build-types/store/reducer.d.ts +3 -0
- package/build-types/store/reducer.d.ts.map +1 -1
- package/build-types/store/selectors.d.ts +1 -0
- package/build-types/store/selectors.d.ts.map +1 -1
- package/package.json +41 -25
- package/src/components/media-editor/index.tsx +213 -79
- package/src/components/media-editor/use-crop-options.ts +1 -3
- package/src/components/media-editor/use-save-media-editor.ts +61 -20
- package/src/components/media-editor-canvas/index.tsx +9 -10
- package/src/components/media-editor-crop-panel/index.tsx +17 -9
- package/src/components/media-editor-fine-rotation/index.tsx +1 -1
- package/src/components/media-editor-image-controls/index.tsx +41 -41
- package/src/components/media-editor-image-controls/style.scss +1 -1
- package/src/components/media-editor-modal/index.tsx +26 -11
- 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/hooks/use-crop-gesture-handlers.ts +3 -1
- package/src/image-editor/core/interaction-controller.ts +8 -0
- package/src/image-editor/docs/architecture.md +10 -2
- package/src/image-editor/docs/recipes.md +8 -8
- package/src/image-editor/react/components/cropper.scss +5 -5
- package/src/image-editor/react/components/cropper.tsx +9 -18
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +3 -1
- package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
- package/src/image-editor/react/hooks/use-interaction.ts +3 -2
- package/src/state/composite-reducer.ts +29 -17
- package/src/state/index.ts +3 -1
- package/src/state/media-editor-state-provider.tsx +9 -22
- package/src/state/types.ts +37 -5
- package/src/state/use-media-editor-state.ts +124 -78
- package/src/store/actions.ts +9 -0
- package/src/store/reducer.ts +6 -1
- package/src/store/selectors.ts +4 -0
- package/global.d.ts +0 -5
- package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -104
- package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -74
- package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -180
- 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 -165
- 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 -1254
- 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 -1460
- 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.jsdom.test.ts +0 -324
- 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,1460 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi, type Mock } from 'vitest';
|
|
2
|
-
import { userEvent } from 'vitest/browser';
|
|
3
|
-
import { act, fireEvent, screen, waitFor } from '@testing-library/react';
|
|
4
|
-
import { render } from 'vitest-browser-react';
|
|
5
|
-
import { Cropper } from '../cropper';
|
|
6
|
-
import type { CropperController } from '../../hooks/use-cropper-reducer';
|
|
7
|
-
import { DEFAULT_STATE } from '../../../core/constants';
|
|
8
|
-
import { getSourceRegion } from '../../../core/source-region';
|
|
9
|
-
// Browser Mode needs the package's real styles for layout and transitions.
|
|
10
|
-
// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports
|
|
11
|
-
import '../../../style.scss';
|
|
12
|
-
|
|
13
|
-
const GRID_TEST_ID = 'cropper-grid';
|
|
14
|
-
const GRID_INTERACTIVE_CLASS =
|
|
15
|
-
'wp-media-editor-image-editor__canvas--grid-interactive';
|
|
16
|
-
const SHOW_GRID_CLASS = 'wp-media-editor-image-editor__canvas--show-grid';
|
|
17
|
-
|
|
18
|
-
function createController(): CropperController {
|
|
19
|
-
return {
|
|
20
|
-
state: {
|
|
21
|
-
...DEFAULT_STATE,
|
|
22
|
-
image: {
|
|
23
|
-
src: 'test.jpg',
|
|
24
|
-
naturalWidth: 600,
|
|
25
|
-
naturalHeight: 400,
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
setImage: vi.fn(),
|
|
29
|
-
setPan: vi.fn(),
|
|
30
|
-
setZoom: vi.fn(),
|
|
31
|
-
setZoomAtPoint: vi.fn(),
|
|
32
|
-
setRotation: vi.fn(),
|
|
33
|
-
setFlip: vi.fn(),
|
|
34
|
-
toggleFlip: vi.fn(),
|
|
35
|
-
snapRotate90: vi.fn(),
|
|
36
|
-
setCropRect: vi.fn(),
|
|
37
|
-
settleCrop: vi.fn(),
|
|
38
|
-
applyOperation: vi.fn(),
|
|
39
|
-
reset: vi.fn(),
|
|
40
|
-
isDirty: false,
|
|
41
|
-
getCroppedImage: vi.fn(),
|
|
42
|
-
setVisualSize: vi.fn(),
|
|
43
|
-
adjustCropRectForViewport: vi.fn(),
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function CropperFixture( { children }: { children: React.ReactNode } ) {
|
|
48
|
-
return <div style={ { width: 648, height: 448 } }>{ children }</div>;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async function renderCropper( cropper: React.ReactElement ) {
|
|
52
|
-
const view = await render( <CropperFixture>{ cropper }</CropperFixture> );
|
|
53
|
-
return {
|
|
54
|
-
...view,
|
|
55
|
-
rerender: async ( nextCropper: React.ReactElement ) =>
|
|
56
|
-
await view.rerender(
|
|
57
|
-
<CropperFixture>{ nextCropper }</CropperFixture>
|
|
58
|
-
),
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function hasSettleTransition( element: HTMLElement ) {
|
|
63
|
-
const style = getComputedStyle( element );
|
|
64
|
-
return (
|
|
65
|
-
style.transitionProperty === 'transform' &&
|
|
66
|
-
style.transitionDuration === '0.2s' &&
|
|
67
|
-
style.transitionTimingFunction === 'ease-out'
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
afterEach( () => {
|
|
72
|
-
vi.useRealTimers();
|
|
73
|
-
} );
|
|
74
|
-
|
|
75
|
-
describe( 'Cropper', () => {
|
|
76
|
-
it( 'does not render the grid when showGrid is false', async () => {
|
|
77
|
-
await renderCropper(
|
|
78
|
-
<Cropper
|
|
79
|
-
src="test.jpg"
|
|
80
|
-
controller={ createController() }
|
|
81
|
-
showGrid={ false }
|
|
82
|
-
showDimming={ false }
|
|
83
|
-
/>
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
expect( screen.queryByTestId( GRID_TEST_ID ) ).not.toBeInTheDocument();
|
|
87
|
-
} );
|
|
88
|
-
|
|
89
|
-
it( 'renders the grid always visible when showGrid is true', async () => {
|
|
90
|
-
await renderCropper(
|
|
91
|
-
<Cropper
|
|
92
|
-
src="test.jpg"
|
|
93
|
-
controller={ createController() }
|
|
94
|
-
showGrid
|
|
95
|
-
showDimming={ false }
|
|
96
|
-
/>
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
await screen.findByTestId( GRID_TEST_ID );
|
|
100
|
-
|
|
101
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
102
|
-
expect( canvas ).not.toHaveClass( GRID_INTERACTIVE_CLASS );
|
|
103
|
-
expect( canvas ).not.toHaveClass( SHOW_GRID_CLASS );
|
|
104
|
-
} );
|
|
105
|
-
|
|
106
|
-
it( 'describes and focuses the crop area when requested', async () => {
|
|
107
|
-
await renderCropper(
|
|
108
|
-
<Cropper
|
|
109
|
-
src="test.jpg"
|
|
110
|
-
controller={ createController() }
|
|
111
|
-
showDimming={ false }
|
|
112
|
-
focusOnMount
|
|
113
|
-
/>
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
117
|
-
expect( canvas ).toHaveAccessibleDescription(
|
|
118
|
-
'When this area is focused, use arrow keys to move the image and plus or minus to zoom. Tab to resize handles and controls.'
|
|
119
|
-
);
|
|
120
|
-
expect( canvas ).toHaveFocus();
|
|
121
|
-
} );
|
|
122
|
-
|
|
123
|
-
it( 'does not expose crop area keyboard hints while resize handles are focused', async () => {
|
|
124
|
-
await renderCropper(
|
|
125
|
-
<Cropper
|
|
126
|
-
src="test.jpg"
|
|
127
|
-
controller={ createController() }
|
|
128
|
-
showDimming={ false }
|
|
129
|
-
freeformCrop
|
|
130
|
-
focusOnMount
|
|
131
|
-
/>
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
135
|
-
const handle = await screen.findByRole( 'button', {
|
|
136
|
-
name: 'Resize from top-left corner',
|
|
137
|
-
} );
|
|
138
|
-
|
|
139
|
-
await userEvent.tab();
|
|
140
|
-
expect( handle ).toHaveFocus();
|
|
141
|
-
|
|
142
|
-
expect( canvas ).not.toHaveAccessibleDescription(
|
|
143
|
-
'When this area is focused, use arrow keys to move the image and plus or minus to zoom. Tab to resize handles and controls.'
|
|
144
|
-
);
|
|
145
|
-
expect( handle ).toHaveAccessibleDescription(
|
|
146
|
-
'Use arrow keys to resize the crop area. Hold Shift for larger steps.'
|
|
147
|
-
);
|
|
148
|
-
} );
|
|
149
|
-
|
|
150
|
-
it( 'clears the keyboard-active class when a pointer drag starts on a handle', async () => {
|
|
151
|
-
await renderCropper(
|
|
152
|
-
<Cropper
|
|
153
|
-
src="test.jpg"
|
|
154
|
-
controller={ createController() }
|
|
155
|
-
showDimming={ false }
|
|
156
|
-
freeformCrop
|
|
157
|
-
focusOnMount
|
|
158
|
-
/>
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
162
|
-
const handle = await screen.findByRole( 'button', {
|
|
163
|
-
name: 'Resize from top-left corner',
|
|
164
|
-
} );
|
|
165
|
-
const focusVisibleClass =
|
|
166
|
-
'wp-media-editor-image-editor__canvas--focus-visible';
|
|
167
|
-
|
|
168
|
-
// Simulate keyboard resize on a handle — the canvas should pick up
|
|
169
|
-
// the keyboard-active class via the capture-phase keydown listener.
|
|
170
|
-
act( () => {
|
|
171
|
-
handle.focus();
|
|
172
|
-
} );
|
|
173
|
-
await userEvent.keyboard( '{ArrowRight}' );
|
|
174
|
-
expect( canvas ).toHaveClass( focusVisibleClass );
|
|
175
|
-
|
|
176
|
-
// Switching to the mouse on the same handle must drop the
|
|
177
|
-
// keyboard-active state, even though the handle's pointerdown
|
|
178
|
-
// handler calls stopPropagation.
|
|
179
|
-
fireEvent.pointerDown( handle, {
|
|
180
|
-
button: 0,
|
|
181
|
-
clientX: 100,
|
|
182
|
-
clientY: 100,
|
|
183
|
-
pointerId: 1,
|
|
184
|
-
} );
|
|
185
|
-
expect( canvas ).not.toHaveClass( focusVisibleClass );
|
|
186
|
-
|
|
187
|
-
fireEvent.pointerUp( handle, { pointerId: 1 } );
|
|
188
|
-
} );
|
|
189
|
-
|
|
190
|
-
it( 'returns focus to the crop area on Escape from a resize handle', async () => {
|
|
191
|
-
await renderCropper(
|
|
192
|
-
<Cropper
|
|
193
|
-
src="test.jpg"
|
|
194
|
-
controller={ createController() }
|
|
195
|
-
showDimming={ false }
|
|
196
|
-
freeformCrop
|
|
197
|
-
focusOnMount
|
|
198
|
-
/>
|
|
199
|
-
);
|
|
200
|
-
|
|
201
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
202
|
-
const handle = await screen.findByRole( 'button', {
|
|
203
|
-
name: 'Resize from top-left corner',
|
|
204
|
-
} );
|
|
205
|
-
|
|
206
|
-
act( () => {
|
|
207
|
-
handle.focus();
|
|
208
|
-
} );
|
|
209
|
-
await userEvent.keyboard( '{Escape}' );
|
|
210
|
-
|
|
211
|
-
expect( canvas ).toHaveFocus();
|
|
212
|
-
} );
|
|
213
|
-
|
|
214
|
-
it( 'renders the grid hidden by default in interactive mode', async () => {
|
|
215
|
-
await renderCropper(
|
|
216
|
-
<Cropper
|
|
217
|
-
src="test.jpg"
|
|
218
|
-
controller={ createController() }
|
|
219
|
-
showGrid="interactive"
|
|
220
|
-
showDimming={ false }
|
|
221
|
-
/>
|
|
222
|
-
);
|
|
223
|
-
|
|
224
|
-
await screen.findByTestId( GRID_TEST_ID );
|
|
225
|
-
|
|
226
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
227
|
-
expect( canvas ).toHaveClass( GRID_INTERACTIVE_CLASS );
|
|
228
|
-
expect( canvas ).not.toHaveClass( SHOW_GRID_CLASS );
|
|
229
|
-
} );
|
|
230
|
-
|
|
231
|
-
it( 'shows the interactive grid when a placement control is active', async () => {
|
|
232
|
-
await renderCropper(
|
|
233
|
-
<Cropper
|
|
234
|
-
src="test.jpg"
|
|
235
|
-
controller={ createController() }
|
|
236
|
-
showGrid="interactive"
|
|
237
|
-
showDimming={ false }
|
|
238
|
-
isPlacementActive
|
|
239
|
-
/>
|
|
240
|
-
);
|
|
241
|
-
|
|
242
|
-
await screen.findByTestId( GRID_TEST_ID );
|
|
243
|
-
|
|
244
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
245
|
-
expect( canvas ).toHaveClass( GRID_INTERACTIVE_CLASS );
|
|
246
|
-
expect( canvas ).toHaveClass( SHOW_GRID_CLASS );
|
|
247
|
-
} );
|
|
248
|
-
|
|
249
|
-
it( 'preserves a free crop when freeform handles are toggled off', async () => {
|
|
250
|
-
const controller = createController();
|
|
251
|
-
controller.state = {
|
|
252
|
-
...controller.state,
|
|
253
|
-
cropRect: { x: 0.1, y: 0.2, width: 0.5, height: 0.4 },
|
|
254
|
-
};
|
|
255
|
-
const { rerender } = await renderCropper(
|
|
256
|
-
<Cropper
|
|
257
|
-
src="test.jpg"
|
|
258
|
-
controller={ controller }
|
|
259
|
-
showDimming={ false }
|
|
260
|
-
freeformCrop
|
|
261
|
-
/>
|
|
262
|
-
);
|
|
263
|
-
|
|
264
|
-
await screen.findByRole( 'button', {
|
|
265
|
-
name: 'Resize from top-left corner',
|
|
266
|
-
} );
|
|
267
|
-
( controller.setCropRect as Mock ).mockClear();
|
|
268
|
-
( controller.adjustCropRectForViewport as Mock ).mockClear();
|
|
269
|
-
|
|
270
|
-
await rerender(
|
|
271
|
-
<Cropper
|
|
272
|
-
src="test.jpg"
|
|
273
|
-
controller={ controller }
|
|
274
|
-
showDimming={ false }
|
|
275
|
-
freeformCrop={ false }
|
|
276
|
-
/>
|
|
277
|
-
);
|
|
278
|
-
|
|
279
|
-
expect(
|
|
280
|
-
screen.queryByRole( 'button', {
|
|
281
|
-
name: 'Resize from top-left corner',
|
|
282
|
-
} )
|
|
283
|
-
).not.toBeInTheDocument();
|
|
284
|
-
expect( controller.setCropRect ).not.toHaveBeenCalled();
|
|
285
|
-
expect( controller.adjustCropRectForViewport ).not.toHaveBeenCalled();
|
|
286
|
-
} );
|
|
287
|
-
|
|
288
|
-
it( 'centers a fixed-ratio crop when freeform handles are off', async () => {
|
|
289
|
-
const controller = createController();
|
|
290
|
-
controller.state = {
|
|
291
|
-
...controller.state,
|
|
292
|
-
cropRect: { x: 0.1, y: 0.2, width: 0.5, height: 0.4 },
|
|
293
|
-
};
|
|
294
|
-
await renderCropper(
|
|
295
|
-
<Cropper
|
|
296
|
-
src="test.jpg"
|
|
297
|
-
controller={ controller }
|
|
298
|
-
showDimming={ false }
|
|
299
|
-
freeformCrop={ false }
|
|
300
|
-
aspectRatio={ 1 }
|
|
301
|
-
/>
|
|
302
|
-
);
|
|
303
|
-
|
|
304
|
-
await waitFor( () =>
|
|
305
|
-
expect( controller.adjustCropRectForViewport ).toHaveBeenCalledWith(
|
|
306
|
-
{
|
|
307
|
-
x: expect.closeTo( 1 / 6, 5 ),
|
|
308
|
-
y: 0,
|
|
309
|
-
width: expect.closeTo( 2 / 3, 5 ),
|
|
310
|
-
height: 1,
|
|
311
|
-
}
|
|
312
|
-
)
|
|
313
|
-
);
|
|
314
|
-
} );
|
|
315
|
-
|
|
316
|
-
it( 'clears settling state when a new resize starts before the settle fallback fires', async () => {
|
|
317
|
-
await renderCropper(
|
|
318
|
-
<Cropper
|
|
319
|
-
src="test.jpg"
|
|
320
|
-
controller={ createController() }
|
|
321
|
-
showDimming={ false }
|
|
322
|
-
freeformCrop
|
|
323
|
-
/>
|
|
324
|
-
);
|
|
325
|
-
|
|
326
|
-
const handle = await screen.findByRole( 'button', {
|
|
327
|
-
name: 'Resize from top-left corner',
|
|
328
|
-
} );
|
|
329
|
-
vi.useFakeTimers();
|
|
330
|
-
// The settle transition and viewport pan live on the stage, not the
|
|
331
|
-
// canvas (which stays fixed so the root background is never exposed).
|
|
332
|
-
const stage = screen.getByTestId( 'cropper-stage' );
|
|
333
|
-
|
|
334
|
-
// Start and end a resize to trigger the settle animation.
|
|
335
|
-
fireEvent.pointerDown( handle, {
|
|
336
|
-
button: 0,
|
|
337
|
-
clientX: 100,
|
|
338
|
-
clientY: 100,
|
|
339
|
-
pointerId: 1,
|
|
340
|
-
} );
|
|
341
|
-
fireEvent.pointerUp( handle, { pointerId: 1 } );
|
|
342
|
-
|
|
343
|
-
// The settle transition should now be active on the stage.
|
|
344
|
-
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
345
|
-
|
|
346
|
-
// Start a new resize before the settle fallback fires.
|
|
347
|
-
fireEvent.pointerDown( handle, {
|
|
348
|
-
button: 0,
|
|
349
|
-
clientX: 100,
|
|
350
|
-
clientY: 100,
|
|
351
|
-
pointerId: 1,
|
|
352
|
-
} );
|
|
353
|
-
|
|
354
|
-
// Settling must be cleared — no transition on the drag.
|
|
355
|
-
expect( hasSettleTransition( stage ) ).toBe( false );
|
|
356
|
-
|
|
357
|
-
// Advance past the old settle fallback; it was cancelled so the stage
|
|
358
|
-
// should still have no transition.
|
|
359
|
-
act( () => vi.advanceTimersByTime( 300 ) );
|
|
360
|
-
expect( hasSettleTransition( stage ) ).toBe( false );
|
|
361
|
-
|
|
362
|
-
fireEvent.pointerUp( handle, { pointerId: 1 } );
|
|
363
|
-
|
|
364
|
-
vi.useRealTimers();
|
|
365
|
-
} );
|
|
366
|
-
|
|
367
|
-
it( 'keeps settling active until the settle transform transition ends', async () => {
|
|
368
|
-
await renderCropper(
|
|
369
|
-
<Cropper
|
|
370
|
-
src="test.jpg"
|
|
371
|
-
controller={ createController() }
|
|
372
|
-
showDimming={ false }
|
|
373
|
-
freeformCrop
|
|
374
|
-
/>
|
|
375
|
-
);
|
|
376
|
-
|
|
377
|
-
const handle = await screen.findByRole( 'button', {
|
|
378
|
-
name: 'Resize from top-left corner',
|
|
379
|
-
} );
|
|
380
|
-
vi.useFakeTimers();
|
|
381
|
-
const stage = screen.getByTestId( 'cropper-stage' );
|
|
382
|
-
|
|
383
|
-
fireEvent.pointerDown( handle, {
|
|
384
|
-
button: 0,
|
|
385
|
-
clientX: 100,
|
|
386
|
-
clientY: 100,
|
|
387
|
-
pointerId: 1,
|
|
388
|
-
} );
|
|
389
|
-
fireEvent.pointerUp( handle, { pointerId: 1 } );
|
|
390
|
-
|
|
391
|
-
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
392
|
-
|
|
393
|
-
act( () => vi.advanceTimersByTime( 200 ) );
|
|
394
|
-
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
395
|
-
|
|
396
|
-
// The transform transition runs on the image and bubbles up to the
|
|
397
|
-
// stage handler, so fire from the image to exercise the real path.
|
|
398
|
-
const image = screen.getByTestId( 'cropper-image' );
|
|
399
|
-
const transitionEndEvent = new Event( 'transitionend', {
|
|
400
|
-
bubbles: true,
|
|
401
|
-
} );
|
|
402
|
-
Object.defineProperty( transitionEndEvent, 'propertyName', {
|
|
403
|
-
value: 'transform',
|
|
404
|
-
} );
|
|
405
|
-
fireEvent( image, transitionEndEvent );
|
|
406
|
-
|
|
407
|
-
expect( hasSettleTransition( stage ) ).toBe( false );
|
|
408
|
-
|
|
409
|
-
vi.useRealTimers();
|
|
410
|
-
} );
|
|
411
|
-
|
|
412
|
-
it( 'clears settling via the fallback timer when no transitionend fires', async () => {
|
|
413
|
-
await renderCropper(
|
|
414
|
-
<Cropper
|
|
415
|
-
src="test.jpg"
|
|
416
|
-
controller={ createController() }
|
|
417
|
-
showDimming={ false }
|
|
418
|
-
freeformCrop
|
|
419
|
-
/>
|
|
420
|
-
);
|
|
421
|
-
|
|
422
|
-
const handle = await screen.findByRole( 'button', {
|
|
423
|
-
name: 'Resize from top-left corner',
|
|
424
|
-
} );
|
|
425
|
-
vi.useFakeTimers();
|
|
426
|
-
const stage = screen.getByTestId( 'cropper-stage' );
|
|
427
|
-
|
|
428
|
-
fireEvent.pointerDown( handle, {
|
|
429
|
-
button: 0,
|
|
430
|
-
clientX: 100,
|
|
431
|
-
clientY: 100,
|
|
432
|
-
pointerId: 1,
|
|
433
|
-
} );
|
|
434
|
-
fireEvent.pointerUp( handle, { pointerId: 1 } );
|
|
435
|
-
|
|
436
|
-
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
437
|
-
|
|
438
|
-
// Without a transitionend, settling persists past the CSS duration...
|
|
439
|
-
act( () => vi.advanceTimersByTime( 200 ) );
|
|
440
|
-
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
441
|
-
|
|
442
|
-
// ...and is cleared by the fallback timer (CSS duration + 100ms).
|
|
443
|
-
act( () => vi.advanceTimersByTime( 100 ) );
|
|
444
|
-
expect( hasSettleTransition( stage ) ).toBe( false );
|
|
445
|
-
|
|
446
|
-
vi.useRealTimers();
|
|
447
|
-
} );
|
|
448
|
-
|
|
449
|
-
it( 'ignores non-transform transitionend events while settling', async () => {
|
|
450
|
-
await renderCropper(
|
|
451
|
-
<Cropper
|
|
452
|
-
src="test.jpg"
|
|
453
|
-
controller={ createController() }
|
|
454
|
-
showDimming={ false }
|
|
455
|
-
freeformCrop
|
|
456
|
-
/>
|
|
457
|
-
);
|
|
458
|
-
|
|
459
|
-
const handle = await screen.findByRole( 'button', {
|
|
460
|
-
name: 'Resize from top-left corner',
|
|
461
|
-
} );
|
|
462
|
-
vi.useFakeTimers();
|
|
463
|
-
const stage = screen.getByTestId( 'cropper-stage' );
|
|
464
|
-
|
|
465
|
-
fireEvent.pointerDown( handle, {
|
|
466
|
-
button: 0,
|
|
467
|
-
clientX: 100,
|
|
468
|
-
clientY: 100,
|
|
469
|
-
pointerId: 1,
|
|
470
|
-
} );
|
|
471
|
-
fireEvent.pointerUp( handle, { pointerId: 1 } );
|
|
472
|
-
|
|
473
|
-
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
474
|
-
|
|
475
|
-
// A stencil left/top/width/height transitionend must not clear settling.
|
|
476
|
-
const stencilTransitionEnd = new Event( 'transitionend', {
|
|
477
|
-
bubbles: true,
|
|
478
|
-
} );
|
|
479
|
-
Object.defineProperty( stencilTransitionEnd, 'propertyName', {
|
|
480
|
-
value: 'left',
|
|
481
|
-
} );
|
|
482
|
-
fireEvent( stage, stencilTransitionEnd );
|
|
483
|
-
|
|
484
|
-
expect( hasSettleTransition( stage ) ).toBe( true );
|
|
485
|
-
|
|
486
|
-
vi.useRealTimers();
|
|
487
|
-
} );
|
|
488
|
-
|
|
489
|
-
it( 'pans the canvas when a keyboard resize extends the crop past the canvas edge', async () => {
|
|
490
|
-
// zoom: 2 so the image bounds extend past [0, 1] in normalized space,
|
|
491
|
-
// allowing the crop to be resized beyond the canvas edge.
|
|
492
|
-
const controller = createController();
|
|
493
|
-
controller.state = { ...controller.state, zoom: 2 };
|
|
494
|
-
|
|
495
|
-
await renderCropper(
|
|
496
|
-
<Cropper
|
|
497
|
-
src="test.jpg"
|
|
498
|
-
controller={ controller }
|
|
499
|
-
showDimming={ false }
|
|
500
|
-
freeformCrop
|
|
501
|
-
/>
|
|
502
|
-
);
|
|
503
|
-
|
|
504
|
-
// East handle (4th button clockwise: nw, n, ne, e).
|
|
505
|
-
const eHandle = await screen.findByRole( 'button', {
|
|
506
|
-
name: 'Resize from right edge',
|
|
507
|
-
} );
|
|
508
|
-
vi.useFakeTimers();
|
|
509
|
-
const stage = screen.getByTestId( 'cropper-stage' );
|
|
510
|
-
|
|
511
|
-
// cropRect starts at {x:0, y:0, width:1, height:1} (right edge = 1.0).
|
|
512
|
-
// Pixel snapping is active at zoom:2, so one ArrowRight step is one
|
|
513
|
-
// source pixel: zoom / sourceWidth = 2 / 600 normalized.
|
|
514
|
-
// With canvasSize=600×400 and visualSize=600×400:
|
|
515
|
-
// rightOverflow = (1 + 2/600) * 600 − 600 = 2 → pan.x = −2
|
|
516
|
-
fireEvent.keyDown( eHandle, { key: 'ArrowRight' } );
|
|
517
|
-
|
|
518
|
-
const match = stage.style.transform.match(
|
|
519
|
-
/^translate\((-?\d+(?:\.\d+)?)px, (-?\d+(?:\.\d+)?)px\)$/
|
|
520
|
-
);
|
|
521
|
-
expect( match ).not.toBeNull();
|
|
522
|
-
expect( Number( match?.[ 1 ] ) ).toBeCloseTo( -2, 4 );
|
|
523
|
-
expect( Number( match?.[ 2 ] ) ).toBeCloseTo( 0, 5 );
|
|
524
|
-
|
|
525
|
-
vi.useRealTimers();
|
|
526
|
-
} );
|
|
527
|
-
|
|
528
|
-
it( 'ignores wheel zoom while a crop resize is active', async () => {
|
|
529
|
-
const controller = createController();
|
|
530
|
-
await renderCropper(
|
|
531
|
-
<Cropper
|
|
532
|
-
src="test.jpg"
|
|
533
|
-
controller={ controller }
|
|
534
|
-
showDimming={ false }
|
|
535
|
-
freeformCrop
|
|
536
|
-
/>
|
|
537
|
-
);
|
|
538
|
-
|
|
539
|
-
const resizeHandle = await screen.findByRole( 'button', {
|
|
540
|
-
name: 'Resize from top-left corner',
|
|
541
|
-
} );
|
|
542
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
543
|
-
|
|
544
|
-
fireEvent.pointerDown( resizeHandle, {
|
|
545
|
-
button: 0,
|
|
546
|
-
clientX: 100,
|
|
547
|
-
clientY: 100,
|
|
548
|
-
pointerId: 1,
|
|
549
|
-
} );
|
|
550
|
-
|
|
551
|
-
const wheelEvent = new WheelEvent( 'wheel', {
|
|
552
|
-
bubbles: true,
|
|
553
|
-
cancelable: true,
|
|
554
|
-
clientX: 300,
|
|
555
|
-
clientY: 200,
|
|
556
|
-
deltaY: -100,
|
|
557
|
-
} );
|
|
558
|
-
fireEvent( canvas, wheelEvent );
|
|
559
|
-
|
|
560
|
-
expect( wheelEvent.defaultPrevented ).toBe( true );
|
|
561
|
-
expect( controller.setZoom ).not.toHaveBeenCalled();
|
|
562
|
-
expect( controller.setZoomAtPoint ).not.toHaveBeenCalled();
|
|
563
|
-
|
|
564
|
-
fireEvent.pointerUp( resizeHandle, { pointerId: 1 } );
|
|
565
|
-
} );
|
|
566
|
-
|
|
567
|
-
it( 'does not start canvas drag from touch pointer events', async () => {
|
|
568
|
-
const controller = createController();
|
|
569
|
-
await renderCropper(
|
|
570
|
-
<Cropper
|
|
571
|
-
src="test.jpg"
|
|
572
|
-
controller={ controller }
|
|
573
|
-
showDimming
|
|
574
|
-
showGrid="interactive"
|
|
575
|
-
freeformCrop
|
|
576
|
-
/>
|
|
577
|
-
);
|
|
578
|
-
|
|
579
|
-
await screen.findByRole( 'button', {
|
|
580
|
-
name: 'Resize from top-left corner',
|
|
581
|
-
} );
|
|
582
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
583
|
-
|
|
584
|
-
fireEvent.pointerDown( canvas, {
|
|
585
|
-
button: 0,
|
|
586
|
-
clientX: 100,
|
|
587
|
-
clientY: 100,
|
|
588
|
-
pointerId: 1,
|
|
589
|
-
pointerType: 'touch',
|
|
590
|
-
isPrimary: true,
|
|
591
|
-
} );
|
|
592
|
-
fireEvent.pointerMove( canvas, {
|
|
593
|
-
button: 0,
|
|
594
|
-
clientX: 150,
|
|
595
|
-
clientY: 120,
|
|
596
|
-
pointerId: 1,
|
|
597
|
-
pointerType: 'touch',
|
|
598
|
-
isPrimary: true,
|
|
599
|
-
} );
|
|
600
|
-
|
|
601
|
-
expect( canvas ).not.toHaveClass( SHOW_GRID_CLASS );
|
|
602
|
-
expect( controller.setPan ).not.toHaveBeenCalled();
|
|
603
|
-
} );
|
|
604
|
-
|
|
605
|
-
it( 'ignores pointer drags on the canvas while disabled', async () => {
|
|
606
|
-
const controller = createController();
|
|
607
|
-
await render(
|
|
608
|
-
<Cropper
|
|
609
|
-
src="test.jpg"
|
|
610
|
-
controller={ controller }
|
|
611
|
-
showDimming
|
|
612
|
-
showGrid="interactive"
|
|
613
|
-
freeformCrop
|
|
614
|
-
disabled
|
|
615
|
-
/>
|
|
616
|
-
);
|
|
617
|
-
|
|
618
|
-
await screen.findByRole( 'button', {
|
|
619
|
-
name: 'Resize from top-left corner',
|
|
620
|
-
} );
|
|
621
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
622
|
-
|
|
623
|
-
fireEvent.pointerDown( canvas, {
|
|
624
|
-
button: 0,
|
|
625
|
-
clientX: 100,
|
|
626
|
-
clientY: 100,
|
|
627
|
-
pointerId: 1,
|
|
628
|
-
isPrimary: true,
|
|
629
|
-
} );
|
|
630
|
-
fireEvent.pointerMove( canvas, {
|
|
631
|
-
button: 0,
|
|
632
|
-
clientX: 150,
|
|
633
|
-
clientY: 120,
|
|
634
|
-
pointerId: 1,
|
|
635
|
-
isPrimary: true,
|
|
636
|
-
} );
|
|
637
|
-
|
|
638
|
-
await act( async () => {
|
|
639
|
-
await new Promise( ( resolve ) =>
|
|
640
|
-
requestAnimationFrame( () => resolve( undefined ) )
|
|
641
|
-
);
|
|
642
|
-
} );
|
|
643
|
-
expect( controller.setPan ).not.toHaveBeenCalled();
|
|
644
|
-
} );
|
|
645
|
-
|
|
646
|
-
it( 'pans again after disabled is lifted, as when a save fails', async () => {
|
|
647
|
-
const controller = createController();
|
|
648
|
-
const props = {
|
|
649
|
-
src: 'test.jpg',
|
|
650
|
-
controller,
|
|
651
|
-
showDimming: true,
|
|
652
|
-
showGrid: 'interactive' as const,
|
|
653
|
-
freeformCrop: true,
|
|
654
|
-
};
|
|
655
|
-
const { rerender } = await render( <Cropper { ...props } disabled /> );
|
|
656
|
-
|
|
657
|
-
await screen.findByRole( 'button', {
|
|
658
|
-
name: 'Resize from top-left corner',
|
|
659
|
-
} );
|
|
660
|
-
|
|
661
|
-
await rerender( <Cropper { ...props } /> );
|
|
662
|
-
|
|
663
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
664
|
-
fireEvent.pointerDown( canvas, {
|
|
665
|
-
button: 0,
|
|
666
|
-
clientX: 100,
|
|
667
|
-
clientY: 100,
|
|
668
|
-
pointerId: 1,
|
|
669
|
-
isPrimary: true,
|
|
670
|
-
} );
|
|
671
|
-
fireEvent.pointerMove( canvas, {
|
|
672
|
-
button: 0,
|
|
673
|
-
clientX: 150,
|
|
674
|
-
clientY: 120,
|
|
675
|
-
pointerId: 1,
|
|
676
|
-
isPrimary: true,
|
|
677
|
-
} );
|
|
678
|
-
|
|
679
|
-
await waitFor( () => expect( controller.setPan ).toHaveBeenCalled() );
|
|
680
|
-
} );
|
|
681
|
-
|
|
682
|
-
it( 'ends the gesture when a drag is cancelled by disabling', async () => {
|
|
683
|
-
const controller = createController();
|
|
684
|
-
const onGestureStart = vi.fn();
|
|
685
|
-
const onGestureEnd = vi.fn();
|
|
686
|
-
const props = {
|
|
687
|
-
src: 'test.jpg',
|
|
688
|
-
controller,
|
|
689
|
-
showDimming: true,
|
|
690
|
-
showGrid: 'interactive' as const,
|
|
691
|
-
freeformCrop: true,
|
|
692
|
-
onGestureStart,
|
|
693
|
-
onGestureEnd,
|
|
694
|
-
};
|
|
695
|
-
const { rerender } = await render( <Cropper { ...props } /> );
|
|
696
|
-
|
|
697
|
-
await screen.findByRole( 'button', {
|
|
698
|
-
name: 'Resize from top-left corner',
|
|
699
|
-
} );
|
|
700
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
701
|
-
|
|
702
|
-
fireEvent.pointerDown( canvas, {
|
|
703
|
-
button: 0,
|
|
704
|
-
clientX: 100,
|
|
705
|
-
clientY: 100,
|
|
706
|
-
pointerId: 1,
|
|
707
|
-
isPrimary: true,
|
|
708
|
-
} );
|
|
709
|
-
fireEvent.pointerMove( canvas, {
|
|
710
|
-
button: 0,
|
|
711
|
-
clientX: 150,
|
|
712
|
-
clientY: 120,
|
|
713
|
-
pointerId: 1,
|
|
714
|
-
isPrimary: true,
|
|
715
|
-
} );
|
|
716
|
-
await waitFor( () => expect( onGestureStart ).toHaveBeenCalled() );
|
|
717
|
-
|
|
718
|
-
await rerender( <Cropper { ...props } disabled /> );
|
|
719
|
-
|
|
720
|
-
// Without this the caller is left believing a gesture is still
|
|
721
|
-
// running, which keeps undo/redo disabled after a failed save.
|
|
722
|
-
// Exactly once: a second call would close a gesture that is no
|
|
723
|
-
// longer open.
|
|
724
|
-
expect( onGestureEnd ).toHaveBeenCalledTimes( 1 );
|
|
725
|
-
} );
|
|
726
|
-
|
|
727
|
-
it( 'does not signal a gesture end when disabling with no gesture running', async () => {
|
|
728
|
-
const onGestureEnd = vi.fn();
|
|
729
|
-
const props = {
|
|
730
|
-
src: 'test.jpg',
|
|
731
|
-
controller: createController(),
|
|
732
|
-
showDimming: true,
|
|
733
|
-
showGrid: 'interactive' as const,
|
|
734
|
-
freeformCrop: true,
|
|
735
|
-
onGestureEnd,
|
|
736
|
-
};
|
|
737
|
-
const { rerender } = await render( <Cropper { ...props } /> );
|
|
738
|
-
|
|
739
|
-
await screen.findByRole( 'button', {
|
|
740
|
-
name: 'Resize from top-left corner',
|
|
741
|
-
} );
|
|
742
|
-
|
|
743
|
-
await rerender( <Cropper { ...props } disabled /> );
|
|
744
|
-
|
|
745
|
-
expect( onGestureEnd ).not.toHaveBeenCalled();
|
|
746
|
-
} );
|
|
747
|
-
|
|
748
|
-
it( 'signals a fresh gesture on wheel zoom after a save interrupted one', async () => {
|
|
749
|
-
const controller = createController();
|
|
750
|
-
const onGestureStart = vi.fn();
|
|
751
|
-
const props = {
|
|
752
|
-
src: 'test.jpg',
|
|
753
|
-
controller,
|
|
754
|
-
showDimming: true,
|
|
755
|
-
showGrid: 'interactive' as const,
|
|
756
|
-
freeformCrop: true,
|
|
757
|
-
onGestureStart,
|
|
758
|
-
};
|
|
759
|
-
const { rerender } = await renderCropper( <Cropper { ...props } /> );
|
|
760
|
-
|
|
761
|
-
await screen.findByRole( 'button', {
|
|
762
|
-
name: 'Resize from top-left corner',
|
|
763
|
-
} );
|
|
764
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
765
|
-
|
|
766
|
-
// A wheel zoom opens a debounced gesture.
|
|
767
|
-
await userEvent.wheel( canvas, { delta: { y: -120 } } );
|
|
768
|
-
await waitFor( () => expect( onGestureStart ).toHaveBeenCalled() );
|
|
769
|
-
|
|
770
|
-
// Saving cancels it, then the save fails and tools come back.
|
|
771
|
-
await rerender( <Cropper { ...props } disabled /> );
|
|
772
|
-
await rerender( <Cropper { ...props } /> );
|
|
773
|
-
onGestureStart.mockClear();
|
|
774
|
-
|
|
775
|
-
// The next wheel zoom must open a gesture of its own.
|
|
776
|
-
await userEvent.wheel( canvas, { delta: { y: -120 } } );
|
|
777
|
-
await waitFor( () => expect( onGestureStart ).toHaveBeenCalled() );
|
|
778
|
-
} );
|
|
779
|
-
|
|
780
|
-
it( 'ignores wheel zoom while disabled', async () => {
|
|
781
|
-
const controller = createController();
|
|
782
|
-
await renderCropper(
|
|
783
|
-
<Cropper
|
|
784
|
-
src="test.jpg"
|
|
785
|
-
controller={ controller }
|
|
786
|
-
showDimming
|
|
787
|
-
showGrid="interactive"
|
|
788
|
-
freeformCrop
|
|
789
|
-
disabled
|
|
790
|
-
/>
|
|
791
|
-
);
|
|
792
|
-
|
|
793
|
-
await screen.findByRole( 'button', {
|
|
794
|
-
name: 'Resize from top-left corner',
|
|
795
|
-
} );
|
|
796
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
797
|
-
|
|
798
|
-
const onWheel = vi.fn();
|
|
799
|
-
canvas.addEventListener( 'wheel', onWheel, { once: true } );
|
|
800
|
-
await userEvent.wheel( canvas, { delta: { y: -120 } } );
|
|
801
|
-
await waitFor( () => expect( onWheel ).toHaveBeenCalledOnce() );
|
|
802
|
-
|
|
803
|
-
await act( async () => {
|
|
804
|
-
await new Promise( ( resolve ) =>
|
|
805
|
-
requestAnimationFrame( () => resolve( undefined ) )
|
|
806
|
-
);
|
|
807
|
-
} );
|
|
808
|
-
expect( controller.setZoomAtPoint ).not.toHaveBeenCalled();
|
|
809
|
-
expect( controller.setZoom ).not.toHaveBeenCalled();
|
|
810
|
-
} );
|
|
811
|
-
|
|
812
|
-
it( 'ignores keyboard pan while disabled', async () => {
|
|
813
|
-
const controller = createController();
|
|
814
|
-
await renderCropper(
|
|
815
|
-
<Cropper
|
|
816
|
-
src="test.jpg"
|
|
817
|
-
controller={ controller }
|
|
818
|
-
showDimming
|
|
819
|
-
showGrid="interactive"
|
|
820
|
-
freeformCrop
|
|
821
|
-
disabled
|
|
822
|
-
/>
|
|
823
|
-
);
|
|
824
|
-
|
|
825
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
826
|
-
canvas.focus();
|
|
827
|
-
expect( canvas ).toHaveFocus();
|
|
828
|
-
await userEvent.keyboard( '{ArrowRight}' );
|
|
829
|
-
|
|
830
|
-
expect( controller.setPan ).not.toHaveBeenCalled();
|
|
831
|
-
} );
|
|
832
|
-
|
|
833
|
-
it( 'stops an in-flight canvas drag when disabled flips mid-gesture', async () => {
|
|
834
|
-
const controller = createController();
|
|
835
|
-
const props = {
|
|
836
|
-
src: 'test.jpg',
|
|
837
|
-
controller,
|
|
838
|
-
showDimming: true,
|
|
839
|
-
showGrid: 'interactive' as const,
|
|
840
|
-
freeformCrop: true,
|
|
841
|
-
};
|
|
842
|
-
const { rerender } = await render( <Cropper { ...props } /> );
|
|
843
|
-
|
|
844
|
-
await screen.findByRole( 'button', {
|
|
845
|
-
name: 'Resize from top-left corner',
|
|
846
|
-
} );
|
|
847
|
-
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
848
|
-
|
|
849
|
-
fireEvent.pointerDown( canvas, {
|
|
850
|
-
button: 0,
|
|
851
|
-
clientX: 100,
|
|
852
|
-
clientY: 100,
|
|
853
|
-
pointerId: 1,
|
|
854
|
-
isPrimary: true,
|
|
855
|
-
} );
|
|
856
|
-
|
|
857
|
-
await rerender( <Cropper { ...props } disabled /> );
|
|
858
|
-
vi.mocked( controller.setPan ).mockClear();
|
|
859
|
-
|
|
860
|
-
fireEvent.pointerMove( canvas, {
|
|
861
|
-
button: 0,
|
|
862
|
-
clientX: 180,
|
|
863
|
-
clientY: 140,
|
|
864
|
-
pointerId: 1,
|
|
865
|
-
isPrimary: true,
|
|
866
|
-
} );
|
|
867
|
-
|
|
868
|
-
await act( async () => {
|
|
869
|
-
await new Promise( ( resolve ) =>
|
|
870
|
-
requestAnimationFrame( () => resolve( undefined ) )
|
|
871
|
-
);
|
|
872
|
-
} );
|
|
873
|
-
expect( controller.setPan ).not.toHaveBeenCalled();
|
|
874
|
-
} );
|
|
875
|
-
|
|
876
|
-
it( 'does not resize the crop from a handle drag while disabled', async () => {
|
|
877
|
-
const controller = createController();
|
|
878
|
-
await render(
|
|
879
|
-
<Cropper
|
|
880
|
-
src="test.jpg"
|
|
881
|
-
controller={ controller }
|
|
882
|
-
showDimming
|
|
883
|
-
showGrid="interactive"
|
|
884
|
-
freeformCrop
|
|
885
|
-
disabled
|
|
886
|
-
/>
|
|
887
|
-
);
|
|
888
|
-
|
|
889
|
-
const handle = await screen.findByRole( 'button', {
|
|
890
|
-
name: 'Resize from top-left corner',
|
|
891
|
-
} );
|
|
892
|
-
|
|
893
|
-
fireEvent.pointerDown( handle, {
|
|
894
|
-
button: 0,
|
|
895
|
-
clientX: 100,
|
|
896
|
-
clientY: 100,
|
|
897
|
-
pointerId: 1,
|
|
898
|
-
} );
|
|
899
|
-
fireEvent.pointerMove( handle, {
|
|
900
|
-
button: 0,
|
|
901
|
-
clientX: 160,
|
|
902
|
-
clientY: 150,
|
|
903
|
-
pointerId: 1,
|
|
904
|
-
} );
|
|
905
|
-
|
|
906
|
-
await act( async () => {
|
|
907
|
-
await new Promise( ( resolve ) =>
|
|
908
|
-
requestAnimationFrame( () => resolve( undefined ) )
|
|
909
|
-
);
|
|
910
|
-
} );
|
|
911
|
-
expect( controller.setCropRect ).not.toHaveBeenCalled();
|
|
912
|
-
} );
|
|
913
|
-
|
|
914
|
-
it( 'cancels handle resize when a touch gesture becomes a pinch', async () => {
|
|
915
|
-
const originalRAF = globalThis.requestAnimationFrame;
|
|
916
|
-
const originalCAF = globalThis.cancelAnimationFrame;
|
|
917
|
-
globalThis.requestAnimationFrame = ( cb: FrameRequestCallback ) => {
|
|
918
|
-
cb( 0 );
|
|
919
|
-
return 0;
|
|
920
|
-
};
|
|
921
|
-
globalThis.cancelAnimationFrame = vi.fn();
|
|
922
|
-
|
|
923
|
-
try {
|
|
924
|
-
const controller = createController();
|
|
925
|
-
const onGestureEnd = vi.fn();
|
|
926
|
-
await renderCropper(
|
|
927
|
-
<Cropper
|
|
928
|
-
src="test.jpg"
|
|
929
|
-
controller={ controller }
|
|
930
|
-
showDimming={ false }
|
|
931
|
-
freeformCrop
|
|
932
|
-
onGestureEnd={ onGestureEnd }
|
|
933
|
-
/>
|
|
934
|
-
);
|
|
935
|
-
|
|
936
|
-
const resizeHandle = await screen.findByRole( 'button', {
|
|
937
|
-
name: 'Resize from top-left corner',
|
|
938
|
-
} );
|
|
939
|
-
const canvas = screen.getByRole( 'group', {
|
|
940
|
-
name: 'Crop area',
|
|
941
|
-
} );
|
|
942
|
-
|
|
943
|
-
fireEvent.pointerDown( resizeHandle, {
|
|
944
|
-
button: 0,
|
|
945
|
-
clientX: 100,
|
|
946
|
-
clientY: 100,
|
|
947
|
-
pointerId: 1,
|
|
948
|
-
pointerType: 'touch',
|
|
949
|
-
isPrimary: true,
|
|
950
|
-
} );
|
|
951
|
-
|
|
952
|
-
( controller.setPan as Mock ).mockClear();
|
|
953
|
-
fireEvent.pointerDown( canvas, {
|
|
954
|
-
button: 0,
|
|
955
|
-
clientX: 250,
|
|
956
|
-
clientY: 200,
|
|
957
|
-
pointerId: 2,
|
|
958
|
-
pointerType: 'touch',
|
|
959
|
-
isPrimary: false,
|
|
960
|
-
} );
|
|
961
|
-
fireEvent.pointerMove( canvas, {
|
|
962
|
-
button: 0,
|
|
963
|
-
clientX: 275,
|
|
964
|
-
clientY: 200,
|
|
965
|
-
pointerId: 2,
|
|
966
|
-
pointerType: 'touch',
|
|
967
|
-
isPrimary: false,
|
|
968
|
-
} );
|
|
969
|
-
|
|
970
|
-
expect( controller.setPan ).not.toHaveBeenCalled();
|
|
971
|
-
|
|
972
|
-
const touch = ( identifier: number, clientX: number ) =>
|
|
973
|
-
new Touch( {
|
|
974
|
-
identifier,
|
|
975
|
-
target: canvas,
|
|
976
|
-
clientX,
|
|
977
|
-
clientY: 200,
|
|
978
|
-
} );
|
|
979
|
-
fireEvent(
|
|
980
|
-
canvas,
|
|
981
|
-
new TouchEvent( 'touchstart', {
|
|
982
|
-
bubbles: true,
|
|
983
|
-
cancelable: true,
|
|
984
|
-
touches: [ touch( 1, 250 ), touch( 2, 350 ) ],
|
|
985
|
-
} )
|
|
986
|
-
);
|
|
987
|
-
|
|
988
|
-
await act( async () => {
|
|
989
|
-
await Promise.resolve();
|
|
990
|
-
} );
|
|
991
|
-
|
|
992
|
-
expect( controller.settleCrop ).not.toHaveBeenCalled();
|
|
993
|
-
expect( onGestureEnd ).not.toHaveBeenCalled();
|
|
994
|
-
|
|
995
|
-
( controller.setCropRect as Mock ).mockClear();
|
|
996
|
-
( controller.setZoomAtPoint as Mock ).mockClear();
|
|
997
|
-
|
|
998
|
-
fireEvent.pointerMove( resizeHandle, {
|
|
999
|
-
button: 0,
|
|
1000
|
-
clientX: 60,
|
|
1001
|
-
clientY: 60,
|
|
1002
|
-
pointerId: 1,
|
|
1003
|
-
pointerType: 'touch',
|
|
1004
|
-
isPrimary: true,
|
|
1005
|
-
} );
|
|
1006
|
-
fireEvent(
|
|
1007
|
-
document,
|
|
1008
|
-
new TouchEvent( 'touchmove', {
|
|
1009
|
-
bubbles: true,
|
|
1010
|
-
cancelable: true,
|
|
1011
|
-
touches: [ touch( 1, 225 ), touch( 2, 375 ) ],
|
|
1012
|
-
} )
|
|
1013
|
-
);
|
|
1014
|
-
|
|
1015
|
-
expect( controller.setCropRect ).not.toHaveBeenCalled();
|
|
1016
|
-
expect( controller.setZoomAtPoint ).toHaveBeenCalled();
|
|
1017
|
-
|
|
1018
|
-
fireEvent(
|
|
1019
|
-
canvas,
|
|
1020
|
-
new TouchEvent( 'touchend', {
|
|
1021
|
-
bubbles: true,
|
|
1022
|
-
cancelable: true,
|
|
1023
|
-
touches: [],
|
|
1024
|
-
} )
|
|
1025
|
-
);
|
|
1026
|
-
|
|
1027
|
-
expect( onGestureEnd ).toHaveBeenCalledTimes( 1 );
|
|
1028
|
-
} finally {
|
|
1029
|
-
globalThis.requestAnimationFrame = originalRAF;
|
|
1030
|
-
globalThis.cancelAnimationFrame = originalCAF;
|
|
1031
|
-
}
|
|
1032
|
-
} );
|
|
1033
|
-
|
|
1034
|
-
// View-scale: at rest, the scene magnifies so an under-filling crop fills
|
|
1035
|
-
// the canvas. The sized fixture gives the canvas a real 600x400 box. A tall 400x1200 image contain-
|
|
1036
|
-
// fits to a 133.33x400 footprint (fills the height, 133px of 600 wide). A
|
|
1037
|
-
// square crop is bound by the footprint's 133px width, so it magnifies.
|
|
1038
|
-
const TALL_IMAGE = {
|
|
1039
|
-
src: 'tall.jpg',
|
|
1040
|
-
naturalWidth: 400,
|
|
1041
|
-
naturalHeight: 1200,
|
|
1042
|
-
};
|
|
1043
|
-
|
|
1044
|
-
// The view-scale magnification is folded into the image's transform as a
|
|
1045
|
-
// leading `scale(...)`, always present (identity `scale(1)` at rest) so the
|
|
1046
|
-
// transform's function list stays structurally constant for transitions.
|
|
1047
|
-
function imageScale(): number {
|
|
1048
|
-
const img = screen.getByTestId< HTMLImageElement >( 'cropper-image' );
|
|
1049
|
-
const match = img.style.transform.match( /^scale\(\s*([\d.]+)/ );
|
|
1050
|
-
return match ? parseFloat( match[ 1 ] ) : 1;
|
|
1051
|
-
}
|
|
1052
|
-
|
|
1053
|
-
function imageTransformParts(): { viewScale: number; matrix: number[] } {
|
|
1054
|
-
const img = screen.getByTestId< HTMLImageElement >( 'cropper-image' );
|
|
1055
|
-
const match = img.style.transform.match(
|
|
1056
|
-
/^scale\(\s*([^)]+?)\s*\)\s+matrix\(([^)]+)\)/
|
|
1057
|
-
);
|
|
1058
|
-
expect( match ).not.toBeNull();
|
|
1059
|
-
const [ , viewScale, matrix ] = match as RegExpMatchArray;
|
|
1060
|
-
return {
|
|
1061
|
-
viewScale: parseFloat( viewScale ),
|
|
1062
|
-
matrix: matrix
|
|
1063
|
-
.split( ',' )
|
|
1064
|
-
.map( ( value ) => parseFloat( value.trim() ) ),
|
|
1065
|
-
};
|
|
1066
|
-
}
|
|
1067
|
-
|
|
1068
|
-
it( 'magnifies the scene so an under-filling crop fills the canvas at rest', async () => {
|
|
1069
|
-
const controller = createController();
|
|
1070
|
-
controller.state = {
|
|
1071
|
-
...controller.state,
|
|
1072
|
-
image: TALL_IMAGE,
|
|
1073
|
-
// Centered square crop: width fills the footprint (133px), height
|
|
1074
|
-
// 1/3 of 400 = 133px. On-screen 133x133, well below 0.8 * 400.
|
|
1075
|
-
cropRect: { x: 0, y: 1 / 3, width: 1, height: 1 / 3 },
|
|
1076
|
-
};
|
|
1077
|
-
await renderCropper(
|
|
1078
|
-
<Cropper src="tall.jpg" controller={ controller } />
|
|
1079
|
-
);
|
|
1080
|
-
|
|
1081
|
-
// Footprint is 133.33x400. The square crop's binding axis (height) fills
|
|
1082
|
-
// to 0.8 * 400, so the image magnifies by 0.8 * 400 / 133.33 = 2.4.
|
|
1083
|
-
await waitFor( () => expect( imageScale() ).toBeCloseTo( 2.4, 2 ) );
|
|
1084
|
-
} );
|
|
1085
|
-
|
|
1086
|
-
it( 'computes image pan from the unscaled footprint before applying view scale', async () => {
|
|
1087
|
-
const controller = createController();
|
|
1088
|
-
controller.state = {
|
|
1089
|
-
...controller.state,
|
|
1090
|
-
image: TALL_IMAGE,
|
|
1091
|
-
cropRect: { x: 0, y: 1 / 3, width: 1, height: 1 / 3 },
|
|
1092
|
-
pan: { x: 0.1, y: -0.05 },
|
|
1093
|
-
};
|
|
1094
|
-
await renderCropper(
|
|
1095
|
-
<Cropper src="tall.jpg" controller={ controller } />
|
|
1096
|
-
);
|
|
1097
|
-
|
|
1098
|
-
await waitFor( () => expect( imageScale() ).toBeCloseTo( 2.4, 2 ) );
|
|
1099
|
-
const { viewScale, matrix } = imageTransformParts();
|
|
1100
|
-
|
|
1101
|
-
expect( viewScale ).toBeCloseTo( 2.4, 2 );
|
|
1102
|
-
// Pan translations are produced by useTransformStyle from the unscaled
|
|
1103
|
-
// 133.33x400 footprint, then magnified by the leading scale(viewScale).
|
|
1104
|
-
// If useTransformStyle received scaledVisualSize, these would be 32 and
|
|
1105
|
-
// -48 instead.
|
|
1106
|
-
expect( matrix[ 4 ] ).toBeCloseTo( ( 400 / 3 ) * 0.1, 4 );
|
|
1107
|
-
expect( matrix[ 5 ] ).toBeCloseTo( 400 * -0.05, 5 );
|
|
1108
|
-
} );
|
|
1109
|
-
|
|
1110
|
-
it( 'positions crop overlays against the magnified footprint', async () => {
|
|
1111
|
-
const controller = createController();
|
|
1112
|
-
controller.state = {
|
|
1113
|
-
...controller.state,
|
|
1114
|
-
image: TALL_IMAGE,
|
|
1115
|
-
cropRect: { x: 0, y: 1 / 3, width: 1, height: 1 / 3 },
|
|
1116
|
-
};
|
|
1117
|
-
await renderCropper(
|
|
1118
|
-
<Cropper src="tall.jpg" controller={ controller } showGrid />
|
|
1119
|
-
);
|
|
1120
|
-
|
|
1121
|
-
await waitFor( () => expect( imageScale() ).toBeCloseTo( 2.4, 2 ) );
|
|
1122
|
-
|
|
1123
|
-
const expectedRect = {
|
|
1124
|
-
left: 140,
|
|
1125
|
-
top: 40,
|
|
1126
|
-
width: 320,
|
|
1127
|
-
height: 320,
|
|
1128
|
-
};
|
|
1129
|
-
const expectOverlayRect = ( element: HTMLElement | null ) => {
|
|
1130
|
-
expect( element ).not.toBeNull();
|
|
1131
|
-
const overlay = element as HTMLElement;
|
|
1132
|
-
expect( parseFloat( overlay.style.left ) ).toBeCloseTo(
|
|
1133
|
-
expectedRect.left,
|
|
1134
|
-
1
|
|
1135
|
-
);
|
|
1136
|
-
expect( parseFloat( overlay.style.top ) ).toBeCloseTo(
|
|
1137
|
-
expectedRect.top,
|
|
1138
|
-
1
|
|
1139
|
-
);
|
|
1140
|
-
expect( parseFloat( overlay.style.width ) ).toBeCloseTo(
|
|
1141
|
-
expectedRect.width,
|
|
1142
|
-
1
|
|
1143
|
-
);
|
|
1144
|
-
expect( parseFloat( overlay.style.height ) ).toBeCloseTo(
|
|
1145
|
-
expectedRect.height,
|
|
1146
|
-
1
|
|
1147
|
-
);
|
|
1148
|
-
};
|
|
1149
|
-
|
|
1150
|
-
expectOverlayRect( screen.getByTestId( 'cropper-stencil' ) );
|
|
1151
|
-
expectOverlayRect( screen.getByTestId( 'cropper-dimming' ) );
|
|
1152
|
-
expectOverlayRect( screen.getByTestId( GRID_TEST_ID ) );
|
|
1153
|
-
} );
|
|
1154
|
-
|
|
1155
|
-
it( 'does not magnify when the crop already fills the canvas', async () => {
|
|
1156
|
-
const controller = createController();
|
|
1157
|
-
controller.state = {
|
|
1158
|
-
...controller.state,
|
|
1159
|
-
image: TALL_IMAGE,
|
|
1160
|
-
// Full-frame crop already fills the canvas height (footprint == 400).
|
|
1161
|
-
cropRect: { x: 0, y: 0, width: 1, height: 1 },
|
|
1162
|
-
};
|
|
1163
|
-
await renderCropper(
|
|
1164
|
-
<Cropper src="tall.jpg" controller={ controller } />
|
|
1165
|
-
);
|
|
1166
|
-
|
|
1167
|
-
await waitFor( () =>
|
|
1168
|
-
expect( controller.setVisualSize ).toHaveBeenLastCalledWith( {
|
|
1169
|
-
width: expect.closeTo( 400 / 3, 2 ),
|
|
1170
|
-
height: 400,
|
|
1171
|
-
} )
|
|
1172
|
-
);
|
|
1173
|
-
// viewScale = 1, so the leading scale is identity (no magnification).
|
|
1174
|
-
expect( imageScale() ).toBe( 1 );
|
|
1175
|
-
} );
|
|
1176
|
-
|
|
1177
|
-
it( 'holds the magnified view while resizing instead of resetting the zoom', async () => {
|
|
1178
|
-
const controller = createController();
|
|
1179
|
-
controller.state = {
|
|
1180
|
-
...controller.state,
|
|
1181
|
-
image: TALL_IMAGE,
|
|
1182
|
-
cropRect: { x: 0, y: 1 / 3, width: 1, height: 1 / 3 },
|
|
1183
|
-
};
|
|
1184
|
-
await renderCropper(
|
|
1185
|
-
<Cropper src="tall.jpg" controller={ controller } freeformCrop />
|
|
1186
|
-
);
|
|
1187
|
-
|
|
1188
|
-
const handle = await screen.findByRole( 'button', {
|
|
1189
|
-
name: 'Resize from top-left corner',
|
|
1190
|
-
} );
|
|
1191
|
-
// At rest the square crop magnifies the image by 2.4.
|
|
1192
|
-
expect( imageScale() ).toBeCloseTo( 2.4, 2 );
|
|
1193
|
-
fireEvent.pointerDown( handle, {
|
|
1194
|
-
button: 0,
|
|
1195
|
-
clientX: 100,
|
|
1196
|
-
clientY: 100,
|
|
1197
|
-
pointerId: 1,
|
|
1198
|
-
} );
|
|
1199
|
-
|
|
1200
|
-
// Grabbing a handle must not snap the scene back to the footprint;
|
|
1201
|
-
// the magnification holds for the duration of the drag.
|
|
1202
|
-
expect( imageScale() ).toBeCloseTo( 2.4, 2 );
|
|
1203
|
-
|
|
1204
|
-
fireEvent.pointerUp( handle, { pointerId: 1 } );
|
|
1205
|
-
} );
|
|
1206
|
-
|
|
1207
|
-
it( 'snaps freeform resize output to source pixels when the image is shown at 1:1 or larger', async () => {
|
|
1208
|
-
const image = { src: 'tiny.png', naturalWidth: 50, naturalHeight: 50 };
|
|
1209
|
-
const cropRect = { x: 0.1, y: 0.12, width: 0.6, height: 0.44 };
|
|
1210
|
-
const controller = createController();
|
|
1211
|
-
controller.state = {
|
|
1212
|
-
...controller.state,
|
|
1213
|
-
image,
|
|
1214
|
-
cropRect,
|
|
1215
|
-
};
|
|
1216
|
-
const initialRegion = getSourceRegion(
|
|
1217
|
-
{ ...controller.state, cropRect },
|
|
1218
|
-
{ width: image.naturalWidth, height: image.naturalHeight }
|
|
1219
|
-
);
|
|
1220
|
-
await renderCropper(
|
|
1221
|
-
<Cropper src="tiny.png" controller={ controller } freeformCrop />
|
|
1222
|
-
);
|
|
1223
|
-
|
|
1224
|
-
const handle = await screen.findByRole( 'button', {
|
|
1225
|
-
name: 'Resize from right edge',
|
|
1226
|
-
} );
|
|
1227
|
-
handle.focus();
|
|
1228
|
-
await userEvent.keyboard( '{ArrowRight}' );
|
|
1229
|
-
|
|
1230
|
-
const rect = ( controller.setCropRect as Mock ).mock.calls[ 0 ][ 0 ];
|
|
1231
|
-
const region = getSourceRegion(
|
|
1232
|
-
{ ...controller.state, cropRect: rect },
|
|
1233
|
-
{ width: image.naturalWidth, height: image.naturalHeight }
|
|
1234
|
-
);
|
|
1235
|
-
expect( region.x ).toBeCloseTo( initialRegion.x, 3 );
|
|
1236
|
-
expect( region.x + region.width ).toBeCloseTo(
|
|
1237
|
-
Math.round( region.x + region.width ),
|
|
1238
|
-
3
|
|
1239
|
-
);
|
|
1240
|
-
expect( rect.x ).toBeCloseTo( cropRect.x, 5 );
|
|
1241
|
-
expect( rect.y ).toBeCloseTo( cropRect.y, 5 );
|
|
1242
|
-
expect( rect.height ).toBeCloseTo( cropRect.height, 5 );
|
|
1243
|
-
} );
|
|
1244
|
-
|
|
1245
|
-
it( 'uses source-pixel keyboard steps for horizontal freeform resize when snapping is active', async () => {
|
|
1246
|
-
const image = {
|
|
1247
|
-
src: 'wide.png',
|
|
1248
|
-
naturalWidth: 200,
|
|
1249
|
-
naturalHeight: 100,
|
|
1250
|
-
};
|
|
1251
|
-
const cropRect = { x: 0.1, y: 0.1, width: 0.6, height: 0.6 };
|
|
1252
|
-
const controller = createController();
|
|
1253
|
-
controller.state = {
|
|
1254
|
-
...controller.state,
|
|
1255
|
-
image,
|
|
1256
|
-
cropRect,
|
|
1257
|
-
};
|
|
1258
|
-
const initialRegion = getSourceRegion(
|
|
1259
|
-
{ ...controller.state, cropRect },
|
|
1260
|
-
{ width: image.naturalWidth, height: image.naturalHeight }
|
|
1261
|
-
);
|
|
1262
|
-
await renderCropper(
|
|
1263
|
-
<Cropper src="wide.png" controller={ controller } freeformCrop />
|
|
1264
|
-
);
|
|
1265
|
-
|
|
1266
|
-
const handle = await screen.findByRole( 'button', {
|
|
1267
|
-
name: 'Resize from right edge',
|
|
1268
|
-
} );
|
|
1269
|
-
( controller.setCropRect as Mock ).mockClear();
|
|
1270
|
-
handle.focus();
|
|
1271
|
-
await userEvent.keyboard( '{ArrowRight}' );
|
|
1272
|
-
|
|
1273
|
-
const rect = ( controller.setCropRect as Mock ).mock.calls[ 0 ][ 0 ];
|
|
1274
|
-
const region = getSourceRegion(
|
|
1275
|
-
{ ...controller.state, cropRect: rect },
|
|
1276
|
-
{ width: image.naturalWidth, height: image.naturalHeight }
|
|
1277
|
-
);
|
|
1278
|
-
expect( region.x + region.width ).toBeCloseTo(
|
|
1279
|
-
initialRegion.x + initialRegion.width + 1,
|
|
1280
|
-
3
|
|
1281
|
-
);
|
|
1282
|
-
} );
|
|
1283
|
-
|
|
1284
|
-
it( 'keeps freeform resize smooth below 1:1 display scale', async () => {
|
|
1285
|
-
const image = {
|
|
1286
|
-
src: 'large.png',
|
|
1287
|
-
naturalWidth: 3333,
|
|
1288
|
-
naturalHeight: 3333,
|
|
1289
|
-
};
|
|
1290
|
-
const cropRect = { x: 0.113, y: 0.127, width: 0.6, height: 0.456 };
|
|
1291
|
-
const controller = createController();
|
|
1292
|
-
controller.state = {
|
|
1293
|
-
...controller.state,
|
|
1294
|
-
image,
|
|
1295
|
-
cropRect,
|
|
1296
|
-
};
|
|
1297
|
-
await renderCropper(
|
|
1298
|
-
<Cropper src="large.png" controller={ controller } freeformCrop />
|
|
1299
|
-
);
|
|
1300
|
-
|
|
1301
|
-
const handle = await screen.findByRole( 'button', {
|
|
1302
|
-
name: 'Resize from right edge',
|
|
1303
|
-
} );
|
|
1304
|
-
handle.focus();
|
|
1305
|
-
await userEvent.keyboard( '{ArrowRight}' );
|
|
1306
|
-
|
|
1307
|
-
const rect = ( controller.setCropRect as Mock ).mock.calls[ 0 ][ 0 ];
|
|
1308
|
-
const region = getSourceRegion(
|
|
1309
|
-
{ ...controller.state, cropRect: rect },
|
|
1310
|
-
{ width: image.naturalWidth, height: image.naturalHeight }
|
|
1311
|
-
);
|
|
1312
|
-
expect( rect.width ).toBeCloseTo( 0.61, 5 );
|
|
1313
|
-
expect(
|
|
1314
|
-
Math.abs(
|
|
1315
|
-
region.x + region.width - Math.round( region.x + region.width )
|
|
1316
|
-
)
|
|
1317
|
-
).toBeGreaterThan( 0.01 );
|
|
1318
|
-
} );
|
|
1319
|
-
|
|
1320
|
-
it( 'snaps all crop edges once when display scale reaches source-pixel size', async () => {
|
|
1321
|
-
const image = {
|
|
1322
|
-
src: 'crossing.png',
|
|
1323
|
-
naturalWidth: 1000,
|
|
1324
|
-
naturalHeight: 1000,
|
|
1325
|
-
};
|
|
1326
|
-
const cropRect = {
|
|
1327
|
-
x: 0.113,
|
|
1328
|
-
y: 0.127,
|
|
1329
|
-
width: 0.733,
|
|
1330
|
-
height: 0.641,
|
|
1331
|
-
};
|
|
1332
|
-
const controller = createController();
|
|
1333
|
-
controller.state = {
|
|
1334
|
-
...controller.state,
|
|
1335
|
-
image,
|
|
1336
|
-
cropRect,
|
|
1337
|
-
zoom: 0.5,
|
|
1338
|
-
};
|
|
1339
|
-
const { rerender } = await renderCropper(
|
|
1340
|
-
<Cropper
|
|
1341
|
-
src="crossing.png"
|
|
1342
|
-
controller={ controller }
|
|
1343
|
-
freeformCrop
|
|
1344
|
-
/>
|
|
1345
|
-
);
|
|
1346
|
-
|
|
1347
|
-
await screen.findByTestId( 'cropper-image' );
|
|
1348
|
-
expect( controller.setCropRect ).not.toHaveBeenCalled();
|
|
1349
|
-
|
|
1350
|
-
controller.state = { ...controller.state, zoom: 3 };
|
|
1351
|
-
await rerender(
|
|
1352
|
-
<Cropper
|
|
1353
|
-
src="crossing.png"
|
|
1354
|
-
controller={ controller }
|
|
1355
|
-
freeformCrop
|
|
1356
|
-
/>
|
|
1357
|
-
);
|
|
1358
|
-
|
|
1359
|
-
await waitFor( () =>
|
|
1360
|
-
expect( controller.setCropRect ).toHaveBeenCalledTimes( 1 )
|
|
1361
|
-
);
|
|
1362
|
-
const rect = ( controller.setCropRect as Mock ).mock.calls[ 0 ][ 0 ];
|
|
1363
|
-
const region = getSourceRegion(
|
|
1364
|
-
{ ...controller.state, cropRect: rect },
|
|
1365
|
-
{ width: image.naturalWidth, height: image.naturalHeight }
|
|
1366
|
-
);
|
|
1367
|
-
for ( const edge of [
|
|
1368
|
-
region.x,
|
|
1369
|
-
region.y,
|
|
1370
|
-
region.x + region.width,
|
|
1371
|
-
region.y + region.height,
|
|
1372
|
-
] ) {
|
|
1373
|
-
expect( edge ).toBeCloseTo( Math.round( edge ), 3 );
|
|
1374
|
-
}
|
|
1375
|
-
} );
|
|
1376
|
-
|
|
1377
|
-
it( 'does not snap all crop edges for fixed-aspect freeform crops', async () => {
|
|
1378
|
-
const image = {
|
|
1379
|
-
src: 'locked-ratio.png',
|
|
1380
|
-
naturalWidth: 1000,
|
|
1381
|
-
naturalHeight: 1000,
|
|
1382
|
-
};
|
|
1383
|
-
const controller = createController();
|
|
1384
|
-
controller.state = {
|
|
1385
|
-
...controller.state,
|
|
1386
|
-
image,
|
|
1387
|
-
cropRect: {
|
|
1388
|
-
x: 0.113,
|
|
1389
|
-
y: 0.127,
|
|
1390
|
-
width: 0.733,
|
|
1391
|
-
height: 0.641,
|
|
1392
|
-
},
|
|
1393
|
-
zoom: 0.5,
|
|
1394
|
-
};
|
|
1395
|
-
const { rerender } = await renderCropper(
|
|
1396
|
-
<Cropper
|
|
1397
|
-
src="locked-ratio.png"
|
|
1398
|
-
controller={ controller }
|
|
1399
|
-
freeformCrop
|
|
1400
|
-
aspectRatio={ 1 }
|
|
1401
|
-
/>
|
|
1402
|
-
);
|
|
1403
|
-
|
|
1404
|
-
await screen.findByTestId( 'cropper-image' );
|
|
1405
|
-
controller.state = { ...controller.state, zoom: 3 };
|
|
1406
|
-
await rerender(
|
|
1407
|
-
<Cropper
|
|
1408
|
-
src="locked-ratio.png"
|
|
1409
|
-
controller={ controller }
|
|
1410
|
-
freeformCrop
|
|
1411
|
-
aspectRatio={ 1 }
|
|
1412
|
-
/>
|
|
1413
|
-
);
|
|
1414
|
-
|
|
1415
|
-
expect( controller.setCropRect ).not.toHaveBeenCalled();
|
|
1416
|
-
} );
|
|
1417
|
-
|
|
1418
|
-
function imageRendering(): string {
|
|
1419
|
-
return getComputedStyle(
|
|
1420
|
-
screen.getByTestId< HTMLImageElement >( 'cropper-image' )
|
|
1421
|
-
).imageRendering;
|
|
1422
|
-
}
|
|
1423
|
-
|
|
1424
|
-
it( 'renders upscaled (small) images pixelated so pixel boundaries stay crisp', async () => {
|
|
1425
|
-
const controller = createController();
|
|
1426
|
-
controller.state = {
|
|
1427
|
-
...controller.state,
|
|
1428
|
-
// A 50x50 image contain-fits to 400px in the 600x400 canvas — an 8x
|
|
1429
|
-
// upscale — so the display scale exceeds 1:1.
|
|
1430
|
-
image: { src: 'tiny.png', naturalWidth: 50, naturalHeight: 50 },
|
|
1431
|
-
};
|
|
1432
|
-
await renderCropper(
|
|
1433
|
-
<Cropper src="tiny.png" controller={ controller } />
|
|
1434
|
-
);
|
|
1435
|
-
|
|
1436
|
-
await waitFor( () => expect( imageRendering() ).toBe( 'pixelated' ) );
|
|
1437
|
-
} );
|
|
1438
|
-
|
|
1439
|
-
it( 'renders downscaled (large) images smoothly', async () => {
|
|
1440
|
-
const controller = createController();
|
|
1441
|
-
controller.state = {
|
|
1442
|
-
...controller.state,
|
|
1443
|
-
// Tall image is shown well below 1:1 (fit ~0.33), even when the crop
|
|
1444
|
-
// magnifies, so the image is downscaled and should stay smooth.
|
|
1445
|
-
image: TALL_IMAGE,
|
|
1446
|
-
cropRect: { x: 0, y: 1 / 3, width: 1, height: 1 / 3 },
|
|
1447
|
-
};
|
|
1448
|
-
await renderCropper(
|
|
1449
|
-
<Cropper src="tall.jpg" controller={ controller } />
|
|
1450
|
-
);
|
|
1451
|
-
|
|
1452
|
-
await waitFor( () =>
|
|
1453
|
-
expect( controller.setVisualSize ).toHaveBeenLastCalledWith( {
|
|
1454
|
-
width: expect.closeTo( 400 / 3, 2 ),
|
|
1455
|
-
height: 400,
|
|
1456
|
-
} )
|
|
1457
|
-
);
|
|
1458
|
-
expect( imageRendering() ).not.toBe( 'pixelated' );
|
|
1459
|
-
} );
|
|
1460
|
-
} );
|