@wordpress/media-editor 0.17.2-next.v.202609031004.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-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 +22 -22
- package/src/components/media-editor/index.tsx +42 -5
- package/src/components/media-editor/test/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.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.jsdom.test.tsx +37 -0
- package/src/components/media-editor-modal/test/index.jsdom.test.tsx +65 -35
- package/src/components/rotation-ruler/index.tsx +1 -0
- package/src/components/rotation-ruler/test/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.jsdom.test.ts +76 -63
- package/src/image-editor/core/interaction-controller.ts +8 -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.jsdom.test.tsx +54 -36
- package/src/image-editor/react/components/test/cropper.jsdom.test.tsx +304 -0
- package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +57 -44
- package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +1 -0
- package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +17 -16
- package/src/image-editor/react/hooks/test/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.jsdom.test.ts +1 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.json +6 -1
|
@@ -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
|
}
|
|
@@ -92,6 +92,13 @@ export interface CropperProps {
|
|
|
92
92
|
freeformCrop?: boolean;
|
|
93
93
|
/** Focus the crop area when the cropper mounts. */
|
|
94
94
|
focusOnMount?: boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Ignore all crop interaction: pointer drags, wheel zoom, keyboard
|
|
97
|
+
* pan/zoom and the stencil's resize handles. Any gesture already in
|
|
98
|
+
* flight is cancelled. Used while the edit is being saved, so the
|
|
99
|
+
* modifiers sent to the server cannot change after the request starts.
|
|
100
|
+
*/
|
|
101
|
+
disabled?: boolean;
|
|
95
102
|
/** Callback fired when the image is loaded. */
|
|
96
103
|
onImageLoaded?: ( size: Size ) => void;
|
|
97
104
|
/**
|
|
@@ -136,6 +143,7 @@ export interface CropperProps {
|
|
|
136
143
|
* @param root0.aspectRatio Fixed aspect ratio (width/height).
|
|
137
144
|
* @param root0.freeformCrop Enable resize handles.
|
|
138
145
|
* @param root0.focusOnMount Focus the crop area on mount.
|
|
146
|
+
* @param root0.disabled Ignore all crop interaction.
|
|
139
147
|
* @param root0.onImageLoaded Image load callback.
|
|
140
148
|
* @param root0.onStateChange Every-frame state callback.
|
|
141
149
|
* @param root0.onGestureStart Gesture boundary start.
|
|
@@ -157,6 +165,7 @@ function CropperInner(
|
|
|
157
165
|
aspectRatio,
|
|
158
166
|
freeformCrop = false,
|
|
159
167
|
focusOnMount = false,
|
|
168
|
+
disabled = false,
|
|
160
169
|
onImageLoaded,
|
|
161
170
|
onStateChange,
|
|
162
171
|
onGestureStart,
|
|
@@ -585,6 +594,7 @@ function CropperInner(
|
|
|
585
594
|
maxZoom,
|
|
586
595
|
onGestureStart,
|
|
587
596
|
onGestureEnd,
|
|
597
|
+
disabled,
|
|
588
598
|
} );
|
|
589
599
|
|
|
590
600
|
// Compose focus-visibility tracking into the canvas event handlers.
|
|
@@ -632,6 +642,7 @@ function CropperInner(
|
|
|
632
642
|
},
|
|
633
643
|
onPointerDown: ( event: React.PointerEvent< HTMLDivElement > ) => {
|
|
634
644
|
if (
|
|
645
|
+
disabled ||
|
|
635
646
|
isResizingRef.current ||
|
|
636
647
|
isTouchPinchingRef.current ||
|
|
637
648
|
( event.pointerType === 'touch' && event.isPrimary === false )
|
|
@@ -1067,7 +1078,7 @@ function CropperInner(
|
|
|
1067
1078
|
onEscape={ handleEscape }
|
|
1068
1079
|
aspectRatio={ aspectRatio }
|
|
1069
1080
|
freeformCrop={ freeformCrop }
|
|
1070
|
-
isResizeDisabled={ isTouchPinching }
|
|
1081
|
+
isResizeDisabled={ isTouchPinching || disabled }
|
|
1071
1082
|
stencilTransition={ settleStencilTransition }
|
|
1072
1083
|
cropBounds={ cropBounds }
|
|
1073
1084
|
minCropSize={ minCropSize }
|
|
@@ -152,12 +152,6 @@ export function RectangleStencil( {
|
|
|
152
152
|
};
|
|
153
153
|
}, [] );
|
|
154
154
|
|
|
155
|
-
useEffect( () => {
|
|
156
|
-
if ( isResizeDisabled ) {
|
|
157
|
-
activePointerResizeRef.current?.cancel();
|
|
158
|
-
}
|
|
159
|
-
}, [ isResizeDisabled ] );
|
|
160
|
-
|
|
161
155
|
// Latest callbacks for the drag listeners. The drag closure in
|
|
162
156
|
// handlePointerDown reads from this ref so it always sees current
|
|
163
157
|
// props without having to re-attach listeners mid-drag. Previously,
|
|
@@ -413,6 +407,24 @@ export function RectangleStencil( {
|
|
|
413
407
|
snapCropRect,
|
|
414
408
|
};
|
|
415
409
|
|
|
410
|
+
// Cancel an in-flight resize when the handles are disabled. Reads
|
|
411
|
+
// `onResizeEnd` from the ref above so a new callback identity does
|
|
412
|
+
// not re-run this and close the gesture twice.
|
|
413
|
+
useEffect( () => {
|
|
414
|
+
if ( ! isResizeDisabled ) {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
activePointerResizeRef.current?.cancel();
|
|
418
|
+
// A keyboard resize settles on a timer rather than a pointer
|
|
419
|
+
// release, so close it here too: otherwise the pending timer
|
|
420
|
+
// fires `onResizeEnd` after the resize was already cancelled.
|
|
421
|
+
if ( keyboardResizeActiveRef.current ) {
|
|
422
|
+
clearTimeout( keyboardSettleTimerRef.current );
|
|
423
|
+
keyboardResizeActiveRef.current = false;
|
|
424
|
+
latestHandlersRef.current?.onResizeEnd?.();
|
|
425
|
+
}
|
|
426
|
+
}, [ isResizeDisabled ] );
|
|
427
|
+
|
|
416
428
|
/**
|
|
417
429
|
* Handle keyboard events on a resize handle.
|
|
418
430
|
* Arrow keys resize; Escape returns focus to the canvas.
|
|
@@ -596,6 +608,10 @@ export function RectangleStencil( {
|
|
|
596
608
|
}
|
|
597
609
|
} }
|
|
598
610
|
onKeyDown={ ( event ) => handleKeyDown( pos, event ) }
|
|
611
|
+
// The handlers already refuse input, but the
|
|
612
|
+
// button must say so too, or assistive technology
|
|
613
|
+
// announces a control that does nothing.
|
|
614
|
+
disabled={ isResizeDisabled }
|
|
599
615
|
aria-label={ getHandleLabel( pos ) }
|
|
600
616
|
aria-describedby={ resizeHandleDescriptionId }
|
|
601
617
|
/>
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
import { beforeAll, describe, expect, it, vi } from 'vitest';
|
|
1
2
|
import { render, screen, fireEvent, act } from '@testing-library/react';
|
|
2
3
|
import { RectangleStencil } from '../rectangle-stencil';
|
|
3
|
-
import type {
|
|
4
|
+
import type {
|
|
5
|
+
HandlePosition,
|
|
6
|
+
NormalizedRect,
|
|
7
|
+
Size,
|
|
8
|
+
} from '../../../../core/types';
|
|
4
9
|
import { DEFAULT_KEYBOARD_STEP } from '../../../../core/constants';
|
|
5
10
|
|
|
6
11
|
const DEFAULT_CROP_RECT: NormalizedRect = {
|
|
@@ -13,6 +18,8 @@ const CONTAINER_SIZE: Size = { width: 600, height: 400 };
|
|
|
13
18
|
const IMAGE_SIZE: Size = { width: 500, height: 300 };
|
|
14
19
|
const CROP_BOUNDS = { minX: 0, minY: 0, maxX: 1, maxY: 1 };
|
|
15
20
|
|
|
21
|
+
globalThis.wpVitest.mockPointerEvent();
|
|
22
|
+
|
|
16
23
|
/**
|
|
17
24
|
* Render a RectangleStencil in freeform mode with sensible defaults.
|
|
18
25
|
* Returns the mock callbacks; use `screen` for DOM queries.
|
|
@@ -22,10 +29,10 @@ const CROP_BOUNDS = { minX: 0, minY: 0, maxX: 1, maxY: 1 };
|
|
|
22
29
|
function renderStencil(
|
|
23
30
|
overrides: Partial< React.ComponentProps< typeof RectangleStencil > > = {}
|
|
24
31
|
) {
|
|
25
|
-
const onCropChange =
|
|
26
|
-
const onResizeStart =
|
|
27
|
-
const onResizeEnd =
|
|
28
|
-
const onEscape =
|
|
32
|
+
const onCropChange = vi.fn();
|
|
33
|
+
const onResizeStart = vi.fn();
|
|
34
|
+
const onResizeEnd = vi.fn();
|
|
35
|
+
const onEscape = vi.fn();
|
|
29
36
|
const props = {
|
|
30
37
|
cropRect: DEFAULT_CROP_RECT,
|
|
31
38
|
containerSize: CONTAINER_SIZE,
|
|
@@ -52,30 +59,13 @@ function renderStencil(
|
|
|
52
59
|
}
|
|
53
60
|
|
|
54
61
|
describe( 'RectangleStencil', () => {
|
|
55
|
-
// jsdom does not implement
|
|
56
|
-
// handle drag tests work. Same pattern as core/test/interaction-controller.ts.
|
|
62
|
+
// jsdom does not implement pointer capture, so stub it for handle drag tests.
|
|
57
63
|
beforeAll( () => {
|
|
58
64
|
if ( ! HTMLElement.prototype.setPointerCapture ) {
|
|
59
|
-
HTMLElement.prototype.setPointerCapture =
|
|
65
|
+
HTMLElement.prototype.setPointerCapture = vi.fn();
|
|
60
66
|
}
|
|
61
67
|
if ( ! HTMLElement.prototype.releasePointerCapture ) {
|
|
62
|
-
HTMLElement.prototype.releasePointerCapture =
|
|
63
|
-
}
|
|
64
|
-
// Without a PointerEvent constructor, fireEvent.pointerDown falls back to
|
|
65
|
-
// the base Event class which has no `button` property. The stencil's
|
|
66
|
-
// handler guards on `event.button !== 0` and returns early, so no native
|
|
67
|
-
// listeners are ever registered. Providing a minimal stub (extending
|
|
68
|
-
// MouseEvent so `button` comes from MouseEventInit) fixes this.
|
|
69
|
-
if ( typeof ( globalThis as any ).PointerEvent === 'undefined' ) {
|
|
70
|
-
( globalThis as any ).PointerEvent = class PointerEvent extends (
|
|
71
|
-
MouseEvent
|
|
72
|
-
) {
|
|
73
|
-
pointerId: number;
|
|
74
|
-
constructor( type: string, init: PointerEventInit = {} ) {
|
|
75
|
-
super( type, init );
|
|
76
|
-
this.pointerId = init.pointerId ?? 0;
|
|
77
|
-
}
|
|
78
|
-
};
|
|
68
|
+
HTMLElement.prototype.releasePointerCapture = vi.fn();
|
|
79
69
|
}
|
|
80
70
|
} );
|
|
81
71
|
|
|
@@ -126,8 +116,8 @@ describe( 'RectangleStencil', () => {
|
|
|
126
116
|
|
|
127
117
|
describe( 'keyboard — Escape', () => {
|
|
128
118
|
it( 'handles Escape on a handle without bubbling', () => {
|
|
129
|
-
const onKeyDown =
|
|
130
|
-
const onEscape =
|
|
119
|
+
const onKeyDown = vi.fn();
|
|
120
|
+
const onEscape = vi.fn();
|
|
131
121
|
render(
|
|
132
122
|
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
|
|
133
123
|
<div onKeyDown={ onKeyDown }>
|
|
@@ -135,7 +125,7 @@ describe( 'RectangleStencil', () => {
|
|
|
135
125
|
cropRect={ DEFAULT_CROP_RECT }
|
|
136
126
|
containerSize={ CONTAINER_SIZE }
|
|
137
127
|
imageSize={ IMAGE_SIZE }
|
|
138
|
-
onCropChange={
|
|
128
|
+
onCropChange={ vi.fn() }
|
|
139
129
|
onEscape={ onEscape }
|
|
140
130
|
freeformCrop
|
|
141
131
|
cropBounds={ CROP_BOUNDS }
|
|
@@ -163,7 +153,7 @@ describe( 'RectangleStencil', () => {
|
|
|
163
153
|
|
|
164
154
|
describe( 'keyboard — arrow keys (fine step)', () => {
|
|
165
155
|
it( 'calls onResizeStart once and onResizeEnd after keyboard resize settles', () => {
|
|
166
|
-
|
|
156
|
+
vi.useFakeTimers();
|
|
167
157
|
const { onResizeStart, onResizeEnd } = renderStencil();
|
|
168
158
|
const eHandle = screen.getAllByRole( 'button' )[ 3 ];
|
|
169
159
|
|
|
@@ -174,11 +164,34 @@ describe( 'RectangleStencil', () => {
|
|
|
174
164
|
expect( onResizeEnd ).not.toHaveBeenCalled();
|
|
175
165
|
|
|
176
166
|
act( () => {
|
|
177
|
-
|
|
167
|
+
vi.advanceTimersByTime( 500 );
|
|
178
168
|
} );
|
|
179
169
|
|
|
180
170
|
expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
|
|
181
|
-
|
|
171
|
+
vi.useRealTimers();
|
|
172
|
+
} );
|
|
173
|
+
|
|
174
|
+
it( 'closes a pending keyboard resize when resizing becomes disabled', () => {
|
|
175
|
+
vi.useFakeTimers();
|
|
176
|
+
const { props, rerender, onResizeStart, onResizeEnd } =
|
|
177
|
+
renderStencil();
|
|
178
|
+
const eHandle = screen.getAllByRole( 'button' )[ 3 ];
|
|
179
|
+
|
|
180
|
+
fireEvent.keyDown( eHandle, { key: 'ArrowRight' } );
|
|
181
|
+
expect( onResizeStart ).toHaveBeenCalledTimes( 1 );
|
|
182
|
+
expect( onResizeEnd ).not.toHaveBeenCalled();
|
|
183
|
+
|
|
184
|
+
rerender( <RectangleStencil { ...props } isResizeDisabled /> );
|
|
185
|
+
|
|
186
|
+
// Closed straight away, not left to the settle timer.
|
|
187
|
+
expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
|
|
188
|
+
|
|
189
|
+
// And the cancelled timer must not fire a second one.
|
|
190
|
+
act( () => {
|
|
191
|
+
vi.advanceTimersByTime( 500 );
|
|
192
|
+
} );
|
|
193
|
+
expect( onResizeEnd ).toHaveBeenCalledTimes( 1 );
|
|
194
|
+
vi.useRealTimers();
|
|
182
195
|
} );
|
|
183
196
|
|
|
184
197
|
it( 'moves the right edge right by KEYBOARD_STEP on ArrowRight (no Shift)', () => {
|
|
@@ -216,7 +229,12 @@ describe( 'RectangleStencil', () => {
|
|
|
216
229
|
} );
|
|
217
230
|
|
|
218
231
|
it( 'applies snapCropRect to freeform resize output', () => {
|
|
219
|
-
const snapCropRect =
|
|
232
|
+
const snapCropRect = vi.fn<
|
|
233
|
+
(
|
|
234
|
+
rect: NormalizedRect,
|
|
235
|
+
handle: HandlePosition
|
|
236
|
+
) => NormalizedRect
|
|
237
|
+
>( ( rect ) => ( {
|
|
220
238
|
...rect,
|
|
221
239
|
width: 0.82,
|
|
222
240
|
} ) );
|
|
@@ -311,7 +329,7 @@ describe( 'RectangleStencil', () => {
|
|
|
311
329
|
} );
|
|
312
330
|
|
|
313
331
|
it( 'does not apply snapCropRect while resizing a locked aspect ratio', () => {
|
|
314
|
-
const snapCropRect =
|
|
332
|
+
const snapCropRect = vi.fn( ( rect: NormalizedRect ) => rect );
|
|
315
333
|
renderStencil( { aspectRatio: 1, snapCropRect } );
|
|
316
334
|
const nwHandle = screen.getByRole( 'button', {
|
|
317
335
|
name: 'Resize from top-left corner',
|
|
@@ -350,7 +368,7 @@ describe( 'RectangleStencil', () => {
|
|
|
350
368
|
renderStencil();
|
|
351
369
|
const [ firstHandle ] = screen.getAllByRole( 'button' );
|
|
352
370
|
|
|
353
|
-
|
|
371
|
+
vi.spyOn( firstHandle, 'focus' );
|
|
354
372
|
|
|
355
373
|
fireEvent.pointerDown( firstHandle, {
|
|
356
374
|
button: 0,
|
|
@@ -406,14 +424,14 @@ describe( 'RectangleStencil', () => {
|
|
|
406
424
|
} );
|
|
407
425
|
|
|
408
426
|
it( 'only stops touchstart propagation for single-touch handle gestures', () => {
|
|
409
|
-
const onTouchStart =
|
|
427
|
+
const onTouchStart = vi.fn();
|
|
410
428
|
render(
|
|
411
429
|
<div onTouchStart={ onTouchStart }>
|
|
412
430
|
<RectangleStencil
|
|
413
431
|
cropRect={ DEFAULT_CROP_RECT }
|
|
414
432
|
containerSize={ CONTAINER_SIZE }
|
|
415
433
|
imageSize={ IMAGE_SIZE }
|
|
416
|
-
onCropChange={
|
|
434
|
+
onCropChange={ vi.fn() }
|
|
417
435
|
freeformCrop
|
|
418
436
|
cropBounds={ CROP_BOUNDS }
|
|
419
437
|
/>
|
|
@@ -640,6 +640,310 @@ describe( 'Cropper', () => {
|
|
|
640
640
|
expect( controller.setPan ).not.toHaveBeenCalled();
|
|
641
641
|
} );
|
|
642
642
|
|
|
643
|
+
it( 'ignores pointer drags on the canvas while disabled', async () => {
|
|
644
|
+
const controller = createController();
|
|
645
|
+
render(
|
|
646
|
+
<Cropper
|
|
647
|
+
src="test.jpg"
|
|
648
|
+
controller={ controller }
|
|
649
|
+
showDimming
|
|
650
|
+
showGrid="interactive"
|
|
651
|
+
freeformCrop
|
|
652
|
+
disabled
|
|
653
|
+
/>
|
|
654
|
+
);
|
|
655
|
+
|
|
656
|
+
await screen.findByRole( 'button', {
|
|
657
|
+
name: 'Resize from top-left corner',
|
|
658
|
+
} );
|
|
659
|
+
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
660
|
+
|
|
661
|
+
fireEvent.pointerDown( canvas, {
|
|
662
|
+
button: 0,
|
|
663
|
+
clientX: 100,
|
|
664
|
+
clientY: 100,
|
|
665
|
+
pointerId: 1,
|
|
666
|
+
isPrimary: true,
|
|
667
|
+
} );
|
|
668
|
+
fireEvent.pointerMove( canvas, {
|
|
669
|
+
button: 0,
|
|
670
|
+
clientX: 150,
|
|
671
|
+
clientY: 120,
|
|
672
|
+
pointerId: 1,
|
|
673
|
+
isPrimary: true,
|
|
674
|
+
} );
|
|
675
|
+
|
|
676
|
+
await act( async () => {
|
|
677
|
+
await new Promise( ( resolve ) =>
|
|
678
|
+
requestAnimationFrame( () => resolve( undefined ) )
|
|
679
|
+
);
|
|
680
|
+
} );
|
|
681
|
+
expect( controller.setPan ).not.toHaveBeenCalled();
|
|
682
|
+
} );
|
|
683
|
+
|
|
684
|
+
it( 'pans again after disabled is lifted, as when a save fails', async () => {
|
|
685
|
+
const controller = createController();
|
|
686
|
+
const props = {
|
|
687
|
+
src: 'test.jpg',
|
|
688
|
+
controller,
|
|
689
|
+
showDimming: true,
|
|
690
|
+
showGrid: 'interactive' as const,
|
|
691
|
+
freeformCrop: true,
|
|
692
|
+
};
|
|
693
|
+
const { rerender } = render( <Cropper { ...props } disabled /> );
|
|
694
|
+
|
|
695
|
+
await screen.findByRole( 'button', {
|
|
696
|
+
name: 'Resize from top-left corner',
|
|
697
|
+
} );
|
|
698
|
+
|
|
699
|
+
rerender( <Cropper { ...props } /> );
|
|
700
|
+
|
|
701
|
+
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
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
|
+
|
|
717
|
+
await waitFor( () => expect( controller.setPan ).toHaveBeenCalled() );
|
|
718
|
+
} );
|
|
719
|
+
|
|
720
|
+
it( 'ends the gesture when a drag is cancelled by disabling', async () => {
|
|
721
|
+
const controller = createController();
|
|
722
|
+
const onGestureStart = jest.fn();
|
|
723
|
+
const onGestureEnd = jest.fn();
|
|
724
|
+
const props = {
|
|
725
|
+
src: 'test.jpg',
|
|
726
|
+
controller,
|
|
727
|
+
showDimming: true,
|
|
728
|
+
showGrid: 'interactive' as const,
|
|
729
|
+
freeformCrop: true,
|
|
730
|
+
onGestureStart,
|
|
731
|
+
onGestureEnd,
|
|
732
|
+
};
|
|
733
|
+
const { rerender } = render( <Cropper { ...props } /> );
|
|
734
|
+
|
|
735
|
+
await screen.findByRole( 'button', {
|
|
736
|
+
name: 'Resize from top-left corner',
|
|
737
|
+
} );
|
|
738
|
+
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
739
|
+
|
|
740
|
+
fireEvent.pointerDown( canvas, {
|
|
741
|
+
button: 0,
|
|
742
|
+
clientX: 100,
|
|
743
|
+
clientY: 100,
|
|
744
|
+
pointerId: 1,
|
|
745
|
+
isPrimary: true,
|
|
746
|
+
} );
|
|
747
|
+
fireEvent.pointerMove( canvas, {
|
|
748
|
+
button: 0,
|
|
749
|
+
clientX: 150,
|
|
750
|
+
clientY: 120,
|
|
751
|
+
pointerId: 1,
|
|
752
|
+
isPrimary: true,
|
|
753
|
+
} );
|
|
754
|
+
await waitFor( () => expect( onGestureStart ).toHaveBeenCalled() );
|
|
755
|
+
|
|
756
|
+
rerender( <Cropper { ...props } disabled /> );
|
|
757
|
+
|
|
758
|
+
// Without this the caller is left believing a gesture is still
|
|
759
|
+
// running, which keeps undo/redo disabled after a failed save.
|
|
760
|
+
// Exactly once: a second call would close a gesture that is no
|
|
761
|
+
// longer open.
|
|
762
|
+
expect( onGestureEnd ).toHaveBeenCalledTimes( 1 );
|
|
763
|
+
} );
|
|
764
|
+
|
|
765
|
+
it( 'does not signal a gesture end when disabling with no gesture running', async () => {
|
|
766
|
+
const onGestureEnd = jest.fn();
|
|
767
|
+
const props = {
|
|
768
|
+
src: 'test.jpg',
|
|
769
|
+
controller: createController(),
|
|
770
|
+
showDimming: true,
|
|
771
|
+
showGrid: 'interactive' as const,
|
|
772
|
+
freeformCrop: true,
|
|
773
|
+
onGestureEnd,
|
|
774
|
+
};
|
|
775
|
+
const { rerender } = render( <Cropper { ...props } /> );
|
|
776
|
+
|
|
777
|
+
await screen.findByRole( 'button', {
|
|
778
|
+
name: 'Resize from top-left corner',
|
|
779
|
+
} );
|
|
780
|
+
|
|
781
|
+
rerender( <Cropper { ...props } disabled /> );
|
|
782
|
+
|
|
783
|
+
expect( onGestureEnd ).not.toHaveBeenCalled();
|
|
784
|
+
} );
|
|
785
|
+
|
|
786
|
+
it( 'signals a fresh gesture on wheel zoom after a save interrupted one', async () => {
|
|
787
|
+
const controller = createController();
|
|
788
|
+
const onGestureStart = jest.fn();
|
|
789
|
+
const props = {
|
|
790
|
+
src: 'test.jpg',
|
|
791
|
+
controller,
|
|
792
|
+
showDimming: true,
|
|
793
|
+
showGrid: 'interactive' as const,
|
|
794
|
+
freeformCrop: true,
|
|
795
|
+
onGestureStart,
|
|
796
|
+
};
|
|
797
|
+
const { rerender } = render( <Cropper { ...props } /> );
|
|
798
|
+
|
|
799
|
+
await screen.findByRole( 'button', {
|
|
800
|
+
name: 'Resize from top-left corner',
|
|
801
|
+
} );
|
|
802
|
+
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
803
|
+
|
|
804
|
+
// A wheel zoom opens a debounced gesture.
|
|
805
|
+
fireEvent.wheel( canvas, { deltaY: -120, clientX: 100, clientY: 100 } );
|
|
806
|
+
await waitFor( () => expect( onGestureStart ).toHaveBeenCalled() );
|
|
807
|
+
|
|
808
|
+
// Saving cancels it, then the save fails and tools come back.
|
|
809
|
+
rerender( <Cropper { ...props } disabled /> );
|
|
810
|
+
rerender( <Cropper { ...props } /> );
|
|
811
|
+
onGestureStart.mockClear();
|
|
812
|
+
|
|
813
|
+
// The next wheel zoom must open a gesture of its own.
|
|
814
|
+
fireEvent.wheel( canvas, { deltaY: -120, clientX: 100, clientY: 100 } );
|
|
815
|
+
await waitFor( () => expect( onGestureStart ).toHaveBeenCalled() );
|
|
816
|
+
} );
|
|
817
|
+
|
|
818
|
+
it( 'ignores wheel zoom while disabled', async () => {
|
|
819
|
+
const controller = createController();
|
|
820
|
+
render(
|
|
821
|
+
<Cropper
|
|
822
|
+
src="test.jpg"
|
|
823
|
+
controller={ controller }
|
|
824
|
+
showDimming
|
|
825
|
+
showGrid="interactive"
|
|
826
|
+
freeformCrop
|
|
827
|
+
disabled
|
|
828
|
+
/>
|
|
829
|
+
);
|
|
830
|
+
|
|
831
|
+
await screen.findByRole( 'button', {
|
|
832
|
+
name: 'Resize from top-left corner',
|
|
833
|
+
} );
|
|
834
|
+
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
835
|
+
|
|
836
|
+
fireEvent.wheel( canvas, { deltaY: -120, clientX: 100, clientY: 100 } );
|
|
837
|
+
|
|
838
|
+
await act( async () => {
|
|
839
|
+
await new Promise( ( resolve ) =>
|
|
840
|
+
requestAnimationFrame( () => resolve( undefined ) )
|
|
841
|
+
);
|
|
842
|
+
} );
|
|
843
|
+
expect( controller.setZoomAtPoint ).not.toHaveBeenCalled();
|
|
844
|
+
expect( controller.setZoom ).not.toHaveBeenCalled();
|
|
845
|
+
} );
|
|
846
|
+
|
|
847
|
+
it( 'ignores keyboard pan while disabled', async () => {
|
|
848
|
+
const controller = createController();
|
|
849
|
+
render(
|
|
850
|
+
<Cropper
|
|
851
|
+
src="test.jpg"
|
|
852
|
+
controller={ controller }
|
|
853
|
+
showDimming
|
|
854
|
+
showGrid="interactive"
|
|
855
|
+
freeformCrop
|
|
856
|
+
disabled
|
|
857
|
+
/>
|
|
858
|
+
);
|
|
859
|
+
|
|
860
|
+
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
861
|
+
fireEvent.keyDown( canvas, { key: 'ArrowRight' } );
|
|
862
|
+
|
|
863
|
+
expect( controller.setPan ).not.toHaveBeenCalled();
|
|
864
|
+
} );
|
|
865
|
+
|
|
866
|
+
it( 'stops an in-flight canvas drag when disabled flips mid-gesture', async () => {
|
|
867
|
+
const controller = createController();
|
|
868
|
+
const props = {
|
|
869
|
+
src: 'test.jpg',
|
|
870
|
+
controller,
|
|
871
|
+
showDimming: true,
|
|
872
|
+
showGrid: 'interactive' as const,
|
|
873
|
+
freeformCrop: true,
|
|
874
|
+
};
|
|
875
|
+
const { rerender } = render( <Cropper { ...props } /> );
|
|
876
|
+
|
|
877
|
+
await screen.findByRole( 'button', {
|
|
878
|
+
name: 'Resize from top-left corner',
|
|
879
|
+
} );
|
|
880
|
+
const canvas = screen.getByRole( 'group', { name: 'Crop area' } );
|
|
881
|
+
|
|
882
|
+
fireEvent.pointerDown( canvas, {
|
|
883
|
+
button: 0,
|
|
884
|
+
clientX: 100,
|
|
885
|
+
clientY: 100,
|
|
886
|
+
pointerId: 1,
|
|
887
|
+
isPrimary: true,
|
|
888
|
+
} );
|
|
889
|
+
|
|
890
|
+
rerender( <Cropper { ...props } disabled /> );
|
|
891
|
+
( controller.setPan as jest.Mock ).mockClear();
|
|
892
|
+
|
|
893
|
+
fireEvent.pointerMove( canvas, {
|
|
894
|
+
button: 0,
|
|
895
|
+
clientX: 180,
|
|
896
|
+
clientY: 140,
|
|
897
|
+
pointerId: 1,
|
|
898
|
+
isPrimary: true,
|
|
899
|
+
} );
|
|
900
|
+
|
|
901
|
+
await act( async () => {
|
|
902
|
+
await new Promise( ( resolve ) =>
|
|
903
|
+
requestAnimationFrame( () => resolve( undefined ) )
|
|
904
|
+
);
|
|
905
|
+
} );
|
|
906
|
+
expect( controller.setPan ).not.toHaveBeenCalled();
|
|
907
|
+
} );
|
|
908
|
+
|
|
909
|
+
it( 'does not resize the crop from a handle drag while disabled', async () => {
|
|
910
|
+
const controller = createController();
|
|
911
|
+
render(
|
|
912
|
+
<Cropper
|
|
913
|
+
src="test.jpg"
|
|
914
|
+
controller={ controller }
|
|
915
|
+
showDimming
|
|
916
|
+
showGrid="interactive"
|
|
917
|
+
freeformCrop
|
|
918
|
+
disabled
|
|
919
|
+
/>
|
|
920
|
+
);
|
|
921
|
+
|
|
922
|
+
const handle = await screen.findByRole( 'button', {
|
|
923
|
+
name: 'Resize from top-left corner',
|
|
924
|
+
} );
|
|
925
|
+
|
|
926
|
+
fireEvent.pointerDown( handle, {
|
|
927
|
+
button: 0,
|
|
928
|
+
clientX: 100,
|
|
929
|
+
clientY: 100,
|
|
930
|
+
pointerId: 1,
|
|
931
|
+
} );
|
|
932
|
+
fireEvent.pointerMove( handle, {
|
|
933
|
+
button: 0,
|
|
934
|
+
clientX: 160,
|
|
935
|
+
clientY: 150,
|
|
936
|
+
pointerId: 1,
|
|
937
|
+
} );
|
|
938
|
+
|
|
939
|
+
await act( async () => {
|
|
940
|
+
await new Promise( ( resolve ) =>
|
|
941
|
+
requestAnimationFrame( () => resolve( undefined ) )
|
|
942
|
+
);
|
|
943
|
+
} );
|
|
944
|
+
expect( controller.setCropRect ).not.toHaveBeenCalled();
|
|
945
|
+
} );
|
|
946
|
+
|
|
643
947
|
it( 'cancels handle resize when a touch gesture becomes a pinch', async () => {
|
|
644
948
|
const originalRAF = globalThis.requestAnimationFrame;
|
|
645
949
|
const originalCAF = globalThis.cancelAnimationFrame;
|