@wordpress/media-editor 0.8.0 → 0.8.1-next.v.202605131006.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/build/components/media-editor/index.cjs +560 -0
- package/build/components/media-editor/index.cjs.map +7 -0
- package/build/components/media-editor-canvas/index.cjs +14 -2
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +34 -27
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +162 -0
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +7 -0
- package/build/components/media-editor-modal/index.cjs +42 -430
- package/build/components/media-editor-modal/index.cjs.map +3 -3
- package/build/components/media-editor-toolbar/index.cjs +143 -75
- package/build/components/media-editor-toolbar/index.cjs.map +3 -3
- package/build/components/media-form/index.cjs +3 -0
- package/build/components/media-form/index.cjs.map +2 -2
- package/build/components/rotation-ruler/index.cjs +197 -0
- package/build/components/rotation-ruler/index.cjs.map +7 -0
- package/build/components/rotation-ruler/use-ruler-drag.cjs +154 -0
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +7 -0
- package/build/hooks/use-crop-gesture-handlers.cjs +43 -0
- package/build/hooks/use-crop-gesture-handlers.cjs.map +7 -0
- package/build/image-editor/core/constants.cjs +9 -0
- package/build/image-editor/core/constants.cjs.map +2 -2
- package/build/image-editor/core/containment.cjs +7 -13
- package/build/image-editor/core/containment.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs +43 -7
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/core/state.cjs +5 -1
- package/build/image-editor/core/state.cjs.map +2 -2
- package/build/image-editor/core/transform-style.cjs.map +1 -1
- package/build/image-editor/core/types.cjs.map +1 -1
- package/build/image-editor/core/viewport-state.cjs +50 -0
- package/build/image-editor/core/viewport-state.cjs.map +7 -0
- package/build/image-editor/react/components/cropper.cjs +192 -72
- package/build/image-editor/react/components/cropper.cjs.map +3 -3
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +5 -2
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +16 -4
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/components/viewport-provider.cjs +51 -0
- package/build/image-editor/react/components/viewport-provider.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +53 -6
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-cropper-state.cjs +127 -8
- package/build/image-editor/react/hooks/use-cropper-state.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs +26 -5
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-viewport-state.cjs +51 -0
- package/build/image-editor/react/hooks/use-viewport-state.cjs.map +7 -0
- package/build/image-editor/react/visually-hidden-style.cjs +41 -0
- package/build/image-editor/react/visually-hidden-style.cjs.map +7 -0
- package/build/private-apis.cjs +2 -0
- package/build/private-apis.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +545 -0
- package/build-module/components/media-editor/index.mjs.map +7 -0
- package/build-module/components/media-editor-canvas/index.mjs +14 -2
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +35 -28
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +144 -0
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +7 -0
- package/build-module/components/media-editor-modal/index.mjs +46 -455
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-toolbar/index.mjs +137 -77
- package/build-module/components/media-editor-toolbar/index.mjs.map +2 -2
- package/build-module/components/media-form/index.mjs +3 -0
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/index.mjs +166 -0
- package/build-module/components/rotation-ruler/index.mjs.map +7 -0
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs +126 -0
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +7 -0
- package/build-module/hooks/use-crop-gesture-handlers.mjs +17 -0
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +7 -0
- package/build-module/image-editor/core/constants.mjs +6 -0
- package/build-module/image-editor/core/constants.mjs.map +2 -2
- package/build-module/image-editor/core/containment.mjs +6 -12
- package/build-module/image-editor/core/containment.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs +50 -8
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/core/state.mjs +5 -1
- package/build-module/image-editor/core/state.mjs.map +2 -2
- package/build-module/image-editor/core/transform-style.mjs.map +1 -1
- package/build-module/image-editor/core/viewport-state.mjs +24 -0
- package/build-module/image-editor/core/viewport-state.mjs.map +7 -0
- package/build-module/image-editor/react/components/cropper.mjs +195 -73
- package/build-module/image-editor/react/components/cropper.mjs.map +3 -3
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +5 -2
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +26 -5
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/components/viewport-provider.mjs +27 -0
- package/build-module/image-editor/react/components/viewport-provider.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +59 -7
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs +134 -9
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs +26 -5
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs +29 -0
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +7 -0
- package/build-module/image-editor/react/visually-hidden-style.mjs +16 -0
- package/build-module/image-editor/react/visually-hidden-style.mjs.map +7 -0
- package/build-module/private-apis.mjs +2 -0
- package/build-module/private-apis.mjs.map +2 -2
- package/build-style/style-rtl.css +254 -29
- package/build-style/style.css +254 -29
- package/build-types/components/media-editor/index.d.ts +34 -0
- package/build-types/components/media-editor/index.d.ts.map +1 -0
- package/build-types/components/media-editor-canvas/index.d.ts +10 -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 +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts +6 -0
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/index.d.ts +2 -2
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-provider/index.d.ts +1 -1
- package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
- package/build-types/components/media-editor-toolbar/index.d.ts +4 -1
- package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts +1 -1
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/index.d.ts +46 -0
- package/build-types/components/rotation-ruler/index.d.ts.map +1 -0
- package/build-types/components/rotation-ruler/stories/index.story.d.ts +14 -0
- package/build-types/components/rotation-ruler/stories/index.story.d.ts.map +1 -0
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts +61 -0
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -0
- package/build-types/hooks/use-crop-gesture-handlers.d.ts +42 -0
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -0
- package/build-types/image-editor/core/constants.d.ts +9 -0
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/containment.d.ts +11 -7
- package/build-types/image-editor/core/containment.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts +14 -2
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/core/state.d.ts.map +1 -1
- package/build-types/image-editor/core/transform-style.d.ts +1 -1
- package/build-types/image-editor/core/types.d.ts +25 -0
- package/build-types/image-editor/core/types.d.ts.map +1 -1
- package/build-types/image-editor/core/viewport-state.d.ts +7 -0
- package/build-types/image-editor/core/viewport-state.d.ts.map +1 -0
- package/build-types/image-editor/react/components/cropper-provider.d.ts +1 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +2 -1
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +3 -13
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +1 -1
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/components/viewport-provider.d.ts +9 -0
- package/build-types/image-editor/react/components/viewport-provider.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +29 -0
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts +5 -2
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-viewport-state.d.ts +19 -0
- package/build-types/image-editor/react/hooks/use-viewport-state.d.ts.map +1 -0
- package/build-types/image-editor/react/visually-hidden-style.d.ts +6 -0
- package/build-types/image-editor/react/visually-hidden-style.d.ts.map +1 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
- package/build-types/private-apis.d.ts.map +1 -1
- package/build-types/store/actions.d.ts +3 -3
- package/build-types/store/actions.d.ts.map +1 -1
- package/package.json +16 -15
- package/src/components/media-editor/index.tsx +709 -0
- package/src/components/media-editor/style.scss +89 -0
- package/src/components/media-editor-canvas/index.tsx +24 -0
- package/src/components/media-editor-crop-panel/index.tsx +39 -27
- package/src/components/media-editor-crop-panel/test/index.tsx +109 -0
- package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +177 -0
- package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +53 -0
- package/src/components/media-editor-modal/index.tsx +49 -589
- package/src/components/media-editor-modal/style.scss +13 -112
- package/src/components/media-editor-toolbar/index.tsx +122 -63
- package/src/components/media-editor-toolbar/style.scss +39 -11
- package/src/components/media-form/index.tsx +5 -0
- package/src/components/rotation-ruler/README.md +37 -0
- package/src/components/rotation-ruler/index.tsx +266 -0
- package/src/components/rotation-ruler/stories/index.story.tsx +54 -0
- package/src/components/rotation-ruler/style.scss +147 -0
- package/src/components/rotation-ruler/test/index.tsx +109 -0
- package/src/components/rotation-ruler/use-ruler-drag.ts +230 -0
- package/src/hooks/use-crop-gesture-handlers.ts +54 -0
- package/src/image-editor/core/constants.ts +12 -0
- package/src/image-editor/core/containment.ts +16 -28
- package/src/image-editor/core/interaction-controller.ts +66 -10
- package/src/image-editor/core/state.ts +15 -8
- package/src/image-editor/core/test/camera.ts +15 -26
- package/src/image-editor/core/test/interaction-controller.ts +158 -7
- package/src/image-editor/core/test/state.ts +21 -1
- package/src/image-editor/core/transform-style.ts +1 -1
- package/src/image-editor/core/types.ts +16 -0
- package/src/image-editor/core/viewport-state.ts +33 -0
- package/src/image-editor/docs/architecture.md +1 -1
- package/src/image-editor/docs/recipes.md +2 -2
- package/src/image-editor/react/components/cropper.scss +28 -0
- package/src/image-editor/react/components/cropper.tsx +268 -72
- package/src/image-editor/react/components/overlays/dimming-overlay.tsx +12 -12
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +27 -8
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +29 -3
- package/src/image-editor/react/components/test/cropper.tsx +294 -5
- package/src/image-editor/react/components/viewport-provider.tsx +39 -0
- package/src/image-editor/react/hooks/test/use-aria-announcer.ts +81 -0
- package/src/image-editor/react/hooks/test/use-cropper-state.ts +286 -0
- package/src/image-editor/react/hooks/test/use-interaction.ts +1 -0
- package/src/image-editor/react/hooks/use-aria-announcer.ts +68 -7
- package/src/image-editor/react/hooks/use-cropper-state.ts +230 -11
- package/src/image-editor/react/hooks/use-interaction.ts +36 -6
- package/src/image-editor/react/hooks/use-viewport-state.ts +48 -0
- package/src/image-editor/react/visually-hidden-style.ts +15 -0
- package/src/private-apis.ts +2 -0
- package/src/style.scss +3 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { useCallback, useEffect, useRef } from '@wordpress/element';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Convert a horizontal pointer delta (px) into a value delta.
|
|
8
|
+
*
|
|
9
|
+
* Negative because dragging the ruler to the right should expose
|
|
10
|
+
* smaller values to the left of the fixed center pointer (and vice
|
|
11
|
+
* versa) — the ruler scrubs *under* the pointer.
|
|
12
|
+
*
|
|
13
|
+
* @param deltaPx Pointer movement in CSS pixels.
|
|
14
|
+
* @param pixelsPerStep CSS pixels of pointer travel per `step`.
|
|
15
|
+
* @param step Value units per step.
|
|
16
|
+
*/
|
|
17
|
+
export function pxToValueDelta(
|
|
18
|
+
deltaPx: number,
|
|
19
|
+
pixelsPerStep: number,
|
|
20
|
+
step: number
|
|
21
|
+
): number {
|
|
22
|
+
return ( -deltaPx / pixelsPerStep ) * step;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Clamp `value` to the inclusive `[min, max]` interval.
|
|
27
|
+
*
|
|
28
|
+
* @param value Value to clamp.
|
|
29
|
+
* @param min Lower bound (inclusive).
|
|
30
|
+
* @param max Upper bound (inclusive).
|
|
31
|
+
*/
|
|
32
|
+
export function clampValue( value: number, min: number, max: number ): number {
|
|
33
|
+
if ( value < min ) {
|
|
34
|
+
return min;
|
|
35
|
+
}
|
|
36
|
+
if ( value > max ) {
|
|
37
|
+
return max;
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Round `value` to the nearest multiple of `step`.
|
|
44
|
+
*
|
|
45
|
+
* @param value Value to quantize.
|
|
46
|
+
* @param step Step granularity (must be > 0).
|
|
47
|
+
*/
|
|
48
|
+
export function quantize( value: number, step: number ): number {
|
|
49
|
+
return Math.round( value / step ) * step;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface UseRulerDragOptions {
|
|
53
|
+
value: number;
|
|
54
|
+
onChange: ( next: number ) => void;
|
|
55
|
+
min: number;
|
|
56
|
+
max: number;
|
|
57
|
+
step: number;
|
|
58
|
+
pixelsPerStep: number;
|
|
59
|
+
disabled: boolean;
|
|
60
|
+
/** Called once on pointerdown so the caller can focus the input. */
|
|
61
|
+
onPointerDownStart?: () => void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface RulerDragHandlers {
|
|
65
|
+
onPointerDown: ( event: React.PointerEvent< HTMLElement > ) => void;
|
|
66
|
+
onPointerMove: ( event: React.PointerEvent< HTMLElement > ) => void;
|
|
67
|
+
onPointerUp: ( event: React.PointerEvent< HTMLElement > ) => void;
|
|
68
|
+
onPointerCancel: ( event: React.PointerEvent< HTMLElement > ) => void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Drag-the-ruler gesture for a horizontal slider. Translates pointer
|
|
73
|
+
* movement into value changes quantized to `step`; the caller renders
|
|
74
|
+
* the visuals and holds the value.
|
|
75
|
+
*
|
|
76
|
+
* The hook owns no state of its own — every value change is reported
|
|
77
|
+
* through the supplied `onChange`. The "current value" is read from a
|
|
78
|
+
* ref so closure staleness during a drag is impossible. Drag continues
|
|
79
|
+
* past the wrapper's bounds (standard slider behaviour); a window-level
|
|
80
|
+
* `pointerup` listener guarantees the drag ends and pointer capture
|
|
81
|
+
* releases even if the underlying captured event never fires on the
|
|
82
|
+
* wrapper.
|
|
83
|
+
*
|
|
84
|
+
* @param options Ruler-drag configuration. See `UseRulerDragOptions`.
|
|
85
|
+
*/
|
|
86
|
+
export function useRulerDrag(
|
|
87
|
+
options: UseRulerDragOptions
|
|
88
|
+
): RulerDragHandlers {
|
|
89
|
+
const {
|
|
90
|
+
value,
|
|
91
|
+
onChange,
|
|
92
|
+
min,
|
|
93
|
+
max,
|
|
94
|
+
step,
|
|
95
|
+
pixelsPerStep,
|
|
96
|
+
disabled,
|
|
97
|
+
onPointerDownStart,
|
|
98
|
+
} = options;
|
|
99
|
+
|
|
100
|
+
// Mutable mirror of the latest committed value plus per-drag state.
|
|
101
|
+
// Pointermove handlers fire far faster than React commits, so we
|
|
102
|
+
// cannot read `value` from closure. The `value` write happens in an
|
|
103
|
+
// effect (not at render time) to satisfy `react-hooks/refs`; the
|
|
104
|
+
// effect flushes after commit, so by the time any pointer handler
|
|
105
|
+
// runs, the ref reflects the latest committed `value`.
|
|
106
|
+
const latestRef = useRef< {
|
|
107
|
+
value: number;
|
|
108
|
+
startX: number;
|
|
109
|
+
startValue: number;
|
|
110
|
+
activeStep: number;
|
|
111
|
+
dragging: boolean;
|
|
112
|
+
captureElement: HTMLElement | null;
|
|
113
|
+
capturePointerId: number;
|
|
114
|
+
windowPointerUp: ( () => void ) | null;
|
|
115
|
+
} >( {
|
|
116
|
+
value,
|
|
117
|
+
startX: 0,
|
|
118
|
+
startValue: 0,
|
|
119
|
+
activeStep: step,
|
|
120
|
+
dragging: false,
|
|
121
|
+
captureElement: null,
|
|
122
|
+
capturePointerId: 0,
|
|
123
|
+
windowPointerUp: null,
|
|
124
|
+
} );
|
|
125
|
+
useEffect( () => {
|
|
126
|
+
latestRef.current.value = value;
|
|
127
|
+
}, [ value ] );
|
|
128
|
+
|
|
129
|
+
const endDrag = useCallback( () => {
|
|
130
|
+
const state = latestRef.current;
|
|
131
|
+
if ( ! state.dragging ) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
state.dragging = false;
|
|
135
|
+
if (
|
|
136
|
+
state.captureElement &&
|
|
137
|
+
state.captureElement.hasPointerCapture( state.capturePointerId )
|
|
138
|
+
) {
|
|
139
|
+
state.captureElement.releasePointerCapture(
|
|
140
|
+
state.capturePointerId
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
state.captureElement = null;
|
|
144
|
+
if ( state.windowPointerUp ) {
|
|
145
|
+
window.removeEventListener( 'pointerup', state.windowPointerUp );
|
|
146
|
+
window.removeEventListener(
|
|
147
|
+
'pointercancel',
|
|
148
|
+
state.windowPointerUp
|
|
149
|
+
);
|
|
150
|
+
state.windowPointerUp = null;
|
|
151
|
+
}
|
|
152
|
+
}, [] );
|
|
153
|
+
|
|
154
|
+
// Cleanup any in-flight drag on unmount so we never leak window
|
|
155
|
+
// listeners or stale pointer capture.
|
|
156
|
+
useEffect( () => endDrag, [ endDrag ] );
|
|
157
|
+
|
|
158
|
+
const onPointerDown = useCallback(
|
|
159
|
+
( event: React.PointerEvent< HTMLElement > ) => {
|
|
160
|
+
if ( disabled || event.button !== 0 ) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const state = latestRef.current;
|
|
164
|
+
event.currentTarget.setPointerCapture( event.pointerId );
|
|
165
|
+
state.captureElement = event.currentTarget;
|
|
166
|
+
state.capturePointerId = event.pointerId;
|
|
167
|
+
state.startX = event.clientX;
|
|
168
|
+
state.startValue = state.value;
|
|
169
|
+
state.activeStep = event.shiftKey ? step / 2 : step;
|
|
170
|
+
state.dragging = true;
|
|
171
|
+
onPointerDownStart?.();
|
|
172
|
+
|
|
173
|
+
// Window-level backstop. With `setPointerCapture`, the
|
|
174
|
+
// element should always receive `pointerup` even when the
|
|
175
|
+
// cursor is released outside its bounds — but in practice
|
|
176
|
+
// some browsers / window-blur sequences drop the event. The
|
|
177
|
+
// window listener guarantees the drag ends and capture
|
|
178
|
+
// releases.
|
|
179
|
+
const onWindowUp = () => endDrag();
|
|
180
|
+
state.windowPointerUp = onWindowUp;
|
|
181
|
+
window.addEventListener( 'pointerup', onWindowUp );
|
|
182
|
+
window.addEventListener( 'pointercancel', onWindowUp );
|
|
183
|
+
},
|
|
184
|
+
[ disabled, onPointerDownStart, endDrag, step ]
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
const onPointerMove = useCallback(
|
|
188
|
+
( event: React.PointerEvent< HTMLElement > ) => {
|
|
189
|
+
const state = latestRef.current;
|
|
190
|
+
if ( ! state.dragging ) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
// Shift toggles fine mode mid-drag. The drag math computes
|
|
194
|
+
// from `startX` / `startValue`, so naively swapping `step`
|
|
195
|
+
// would yank the value backward. Rebase to the current
|
|
196
|
+
// pointer + value so the granularity changes without a
|
|
197
|
+
// jump.
|
|
198
|
+
const requestedStep = event.shiftKey ? step / 2 : step;
|
|
199
|
+
if ( requestedStep !== state.activeStep ) {
|
|
200
|
+
state.startX = event.clientX;
|
|
201
|
+
state.startValue = state.value;
|
|
202
|
+
state.activeStep = requestedStep;
|
|
203
|
+
}
|
|
204
|
+
const deltaPx = event.clientX - state.startX;
|
|
205
|
+
const deltaValue = pxToValueDelta(
|
|
206
|
+
deltaPx,
|
|
207
|
+
pixelsPerStep,
|
|
208
|
+
state.activeStep
|
|
209
|
+
);
|
|
210
|
+
const raw = state.startValue + deltaValue;
|
|
211
|
+
const stepped = quantize( raw, state.activeStep );
|
|
212
|
+
const next = clampValue( stepped, min, max );
|
|
213
|
+
if ( next !== state.value ) {
|
|
214
|
+
// Update the ref synchronously so consecutive pointermove
|
|
215
|
+
// events that fire before React commits still see the
|
|
216
|
+
// last *emitted* value as `previous` for the next math.
|
|
217
|
+
state.value = next;
|
|
218
|
+
onChange( next );
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
[ onChange, min, max, step, pixelsPerStep ]
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
return {
|
|
225
|
+
onPointerDown,
|
|
226
|
+
onPointerMove,
|
|
227
|
+
onPointerUp: endDrag,
|
|
228
|
+
onPointerCancel: endDrag,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { useCropper } from '../image-editor';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Data attribute applied to crop control wrappers. The modal's keyboard
|
|
8
|
+
* shortcut handler uses this to distinguish crop controls (where custom
|
|
9
|
+
* undo/redo should fire) from metadata text fields (where the browser's
|
|
10
|
+
* native undo should be preserved).
|
|
11
|
+
*/
|
|
12
|
+
export const CROP_CONTROL_ATTR = 'data-crop-control';
|
|
13
|
+
|
|
14
|
+
export interface UseCropGestureHandlersOptions {
|
|
15
|
+
/**
|
|
16
|
+
* When `true` (default), key-up triggers an immediate history commit so
|
|
17
|
+
* each discrete keypress becomes its own undo step. Set to `false` for
|
|
18
|
+
* continuous-input controls (e.g. the rotation ruler) where rapid
|
|
19
|
+
* keypresses should coalesce into a single history entry via the
|
|
20
|
+
* state-change debounce.
|
|
21
|
+
*/
|
|
22
|
+
commitOnKeyUp?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Returns event handler props to spread onto a wrapper element around a
|
|
27
|
+
* crop control. Marks the wrapper as a crop control (via `data-crop-control`)
|
|
28
|
+
* so the modal's Cmd+Z handler can identify it, and wires up immediate-flush
|
|
29
|
+
* signals on pointer-up and (optionally) key-up so history is committed as
|
|
30
|
+
* soon as the user releases rather than waiting for the debounce window to
|
|
31
|
+
* expire.
|
|
32
|
+
*
|
|
33
|
+
* The history entry itself is recorded by the state-change debounce in
|
|
34
|
+
* `useCropperState` — no explicit gesture-start signal is needed.
|
|
35
|
+
*
|
|
36
|
+
* Usage:
|
|
37
|
+
* const gestureHandlers = useCropGestureHandlers();
|
|
38
|
+
* <div role="presentation" { ...gestureHandlers }>
|
|
39
|
+
* <RangeControl ... />
|
|
40
|
+
* </div>
|
|
41
|
+
*
|
|
42
|
+
* @param options Optional behavior flags.
|
|
43
|
+
*/
|
|
44
|
+
export function useCropGestureHandlers(
|
|
45
|
+
options: UseCropGestureHandlersOptions = {}
|
|
46
|
+
) {
|
|
47
|
+
const { commitOnKeyUp = true } = options;
|
|
48
|
+
const { commitHistory } = useCropper();
|
|
49
|
+
return {
|
|
50
|
+
[ CROP_CONTROL_ATTR ]: true,
|
|
51
|
+
onPointerUp: commitHistory,
|
|
52
|
+
...( commitOnKeyUp ? { onKeyUp: commitHistory } : {} ),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -11,6 +11,18 @@ import type { NormalizedRect, Flip, CropperState } from './types';
|
|
|
11
11
|
export const MIN_ZOOM = 1;
|
|
12
12
|
export const MAX_ZOOM = 10;
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Wheel zoom sensitivity. A deltaY of 100 changes zoom by 0.25.
|
|
16
|
+
* This could be made configurable as a prop to the Cropper component.
|
|
17
|
+
*/
|
|
18
|
+
export const DEFAULT_WHEEL_ZOOM_SPEED = 0.0025;
|
|
19
|
+
|
|
20
|
+
/** Fine step for keyboard-driven cropper movement, in normalized coordinates. */
|
|
21
|
+
export const DEFAULT_KEYBOARD_STEP = 0.01;
|
|
22
|
+
|
|
23
|
+
/** Coarse keyboard movement multiplier when Shift is held. */
|
|
24
|
+
export const KEYBOARD_SHIFT_STEP_MULTIPLIER = 10;
|
|
25
|
+
|
|
14
26
|
/**
|
|
15
27
|
* Maximum free-rotation offset in degrees from the nearest 90° step.
|
|
16
28
|
* The rotation slider allows ±45° around the current cardinal angle.
|
|
@@ -86,25 +86,28 @@ function getMinZoomForCover(
|
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
/**
|
|
89
|
-
* Compute the crop handle bounds in normalized visual space
|
|
89
|
+
* Compute the crop handle bounds in normalized visual space — the
|
|
90
90
|
* axis-aligned bounding box (AABB) of the actual image footprint at the
|
|
91
|
-
* current pan/zoom/rotation
|
|
91
|
+
* current pan/zoom/rotation.
|
|
92
|
+
*
|
|
93
|
+
* Returns the full image AABB without intersecting with the container
|
|
94
|
+
* viewport. The viewport pan mechanism in the Cropper component allows
|
|
95
|
+
* the canvas to scroll so handles can reach the image edge even when it
|
|
96
|
+
* lies outside the visible canvas area.
|
|
92
97
|
*
|
|
93
98
|
* Unlike a static centerline-based calculation, this accounts for the
|
|
94
99
|
* current pan position — if the user pans right, the left bound tightens
|
|
95
100
|
* because the image's left edge has moved right.
|
|
96
101
|
*
|
|
97
|
-
* @param state
|
|
98
|
-
* @param elementSize
|
|
99
|
-
* @param visualSize
|
|
100
|
-
* @param containerSize The container dimensions in pixels.
|
|
102
|
+
* @param state The current cropper state (crop, zoom, rotation, flip).
|
|
103
|
+
* @param elementSize The fitted (unrotated) image element dimensions in pixels.
|
|
104
|
+
* @param visualSize The visual (rotated) image bounding box in pixels.
|
|
101
105
|
* @return The min/max x and y that a crop rect edge can reach in normalized space.
|
|
102
106
|
*/
|
|
103
|
-
export function
|
|
107
|
+
export function getImageCropBounds(
|
|
104
108
|
state: CropperState,
|
|
105
109
|
elementSize: Size,
|
|
106
|
-
visualSize: Size
|
|
107
|
-
containerSize: Size
|
|
110
|
+
visualSize: Size
|
|
108
111
|
): { minX: number; minY: number; maxX: number; maxY: number } {
|
|
109
112
|
if (
|
|
110
113
|
elementSize.width === 0 ||
|
|
@@ -140,13 +143,6 @@ export function getCropBounds(
|
|
|
140
143
|
[ -hw, hh ],
|
|
141
144
|
];
|
|
142
145
|
|
|
143
|
-
// Transform corners through CSS matrix → screen offsets from element center.
|
|
144
|
-
// Screen position = element_center + transformed_offset.
|
|
145
|
-
// Element center is at (containerW/2, containerH/2) after CSS centering.
|
|
146
|
-
// But we want normalized coords, so work relative to the visual image origin.
|
|
147
|
-
const offsetX = ( containerSize.width - visualSize.width ) / 2;
|
|
148
|
-
const offsetY = ( containerSize.height - visualSize.height ) / 2;
|
|
149
|
-
|
|
150
146
|
let imgMinX = Infinity;
|
|
151
147
|
let imgMaxX = -Infinity;
|
|
152
148
|
let imgMinY = Infinity;
|
|
@@ -167,19 +163,11 @@ export function getCropBounds(
|
|
|
167
163
|
imgMaxY = Math.max( imgMaxY, ny );
|
|
168
164
|
}
|
|
169
165
|
|
|
170
|
-
// Container bounds in normalized space.
|
|
171
|
-
const containerMinX = -offsetX / visualSize.width;
|
|
172
|
-
const containerMaxX = ( containerSize.width - offsetX ) / visualSize.width;
|
|
173
|
-
const containerMinY = -offsetY / visualSize.height;
|
|
174
|
-
const containerMaxY =
|
|
175
|
-
( containerSize.height - offsetY ) / visualSize.height;
|
|
176
|
-
|
|
177
|
-
// Crop bounds = intersection of image AABB and container bounds.
|
|
178
166
|
return {
|
|
179
|
-
minX:
|
|
180
|
-
minY:
|
|
181
|
-
maxX:
|
|
182
|
-
maxY:
|
|
167
|
+
minX: imgMinX,
|
|
168
|
+
minY: imgMinY,
|
|
169
|
+
maxX: imgMaxX,
|
|
170
|
+
maxY: imgMaxY,
|
|
183
171
|
};
|
|
184
172
|
}
|
|
185
173
|
|
|
@@ -2,7 +2,13 @@
|
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
4
|
import type { CropperState, NormalizedPoint, Size } from './types';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
DEFAULT_KEYBOARD_STEP,
|
|
7
|
+
DEFAULT_WHEEL_ZOOM_SPEED,
|
|
8
|
+
KEYBOARD_SHIFT_STEP_MULTIPLIER,
|
|
9
|
+
MIN_ZOOM,
|
|
10
|
+
MAX_ZOOM,
|
|
11
|
+
} from './constants';
|
|
6
12
|
import { restrictPanZoom } from './containment';
|
|
7
13
|
|
|
8
14
|
/** Time window for detecting a double-tap gesture (ms). */
|
|
@@ -62,6 +68,8 @@ export interface CropperInteractionActions {
|
|
|
62
68
|
setZoomAtPoint: ( zoom: number, pan: NormalizedPoint ) => void;
|
|
63
69
|
/** Snap rotate by 90 degrees. */
|
|
64
70
|
snapRotate90: ( direction: 1 | -1 ) => void;
|
|
71
|
+
/** Toggle flip on the given axis. */
|
|
72
|
+
toggleFlip?: ( direction: 'horizontal' | 'vertical' ) => void;
|
|
65
73
|
}
|
|
66
74
|
|
|
67
75
|
/**
|
|
@@ -84,9 +92,12 @@ export interface InteractionControllerOptions {
|
|
|
84
92
|
minZoom?: number;
|
|
85
93
|
/** Maximum zoom level. Defaults to MAX_ZOOM. Read lazily. */
|
|
86
94
|
maxZoom?: number;
|
|
87
|
-
/** Zoom speed multiplier for wheel events. Defaults to 0.
|
|
95
|
+
/** Zoom speed multiplier for wheel events. Defaults to 0.0025. Read lazily. */
|
|
88
96
|
zoomSpeed?: number;
|
|
89
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* Pan step size in normalized coords for keyboard events.
|
|
99
|
+
* Defaults to 0.01. Read lazily. Shift multiplies it by 10.
|
|
100
|
+
*/
|
|
90
101
|
keyboardStep?: number;
|
|
91
102
|
/** Zoom level for double-tap zoom. Defaults to 2. Read lazily. */
|
|
92
103
|
doubleTapZoom?: number;
|
|
@@ -200,12 +211,24 @@ export class InteractionController {
|
|
|
200
211
|
|
|
201
212
|
/** Read zoomSpeed lazily so option changes take effect immediately. */
|
|
202
213
|
private get zoomSpeed(): number {
|
|
203
|
-
return this.options.zoomSpeed ??
|
|
214
|
+
return this.options.zoomSpeed ?? DEFAULT_WHEEL_ZOOM_SPEED;
|
|
204
215
|
}
|
|
205
216
|
|
|
206
217
|
/** Read keyboardStep lazily so option changes take effect immediately. */
|
|
207
218
|
private get keyboardStep(): number {
|
|
208
|
-
return this.options.keyboardStep ??
|
|
219
|
+
return this.options.keyboardStep ?? DEFAULT_KEYBOARD_STEP;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Get the keyboard pan step, including modifier-based coarse movement.
|
|
224
|
+
*
|
|
225
|
+
* @param event The native KeyboardEvent.
|
|
226
|
+
* @return The normalized pan step.
|
|
227
|
+
*/
|
|
228
|
+
private getKeyboardStep( event: KeyboardEvent ): number {
|
|
229
|
+
return event.shiftKey
|
|
230
|
+
? this.keyboardStep * KEYBOARD_SHIFT_STEP_MULTIPLIER
|
|
231
|
+
: this.keyboardStep;
|
|
209
232
|
}
|
|
210
233
|
|
|
211
234
|
/** Read doubleTapZoom lazily so option changes take effect immediately. */
|
|
@@ -361,6 +384,10 @@ export class InteractionController {
|
|
|
361
384
|
handleWheel( e: WheelEvent ): void {
|
|
362
385
|
e.preventDefault();
|
|
363
386
|
|
|
387
|
+
if ( this.drag ) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
|
|
364
391
|
// Debounced gesture boundaries for wheel zoom.
|
|
365
392
|
// Start on first wheel event, end after 300ms of no events.
|
|
366
393
|
if ( ! this.wheelGestureActive ) {
|
|
@@ -731,6 +758,11 @@ export class InteractionController {
|
|
|
731
758
|
this.setStatus( { isZooming: false } );
|
|
732
759
|
}, ZOOM_ANIMATION_DURATION );
|
|
733
760
|
|
|
761
|
+
// Double-tap bypasses the normal handleTouchStart gesture path so
|
|
762
|
+
// onGestureStart/End are never called — fire them here so undo
|
|
763
|
+
// receives a proper boundary around this discrete zoom action.
|
|
764
|
+
this.options.onGestureStart?.();
|
|
765
|
+
|
|
734
766
|
if ( visSize.width > 0 && visSize.height > 0 ) {
|
|
735
767
|
const fx = tapX - containerRect.left - containerSize.width / 2;
|
|
736
768
|
const fy = tapY - containerRect.top - containerSize.height / 2;
|
|
@@ -758,6 +790,8 @@ export class InteractionController {
|
|
|
758
790
|
} else {
|
|
759
791
|
this.options.actions.setZoom( targetZoom );
|
|
760
792
|
}
|
|
793
|
+
|
|
794
|
+
this.options.onGestureEnd?.();
|
|
761
795
|
return true;
|
|
762
796
|
}
|
|
763
797
|
|
|
@@ -774,12 +808,13 @@ export class InteractionController {
|
|
|
774
808
|
switch ( e.key ) {
|
|
775
809
|
case 'ArrowUp': {
|
|
776
810
|
e.preventDefault();
|
|
811
|
+
const keyboardStep = this.getKeyboardStep( e );
|
|
777
812
|
const { pan: newCrop } = restrictPanZoom(
|
|
778
813
|
{
|
|
779
814
|
...currentState,
|
|
780
815
|
pan: {
|
|
781
816
|
x: currentState.pan.x,
|
|
782
|
-
y: currentState.pan.y +
|
|
817
|
+
y: currentState.pan.y + keyboardStep,
|
|
783
818
|
},
|
|
784
819
|
},
|
|
785
820
|
getImageSizeFromState( currentState ),
|
|
@@ -790,12 +825,13 @@ export class InteractionController {
|
|
|
790
825
|
}
|
|
791
826
|
case 'ArrowDown': {
|
|
792
827
|
e.preventDefault();
|
|
828
|
+
const keyboardStep = this.getKeyboardStep( e );
|
|
793
829
|
const { pan: newCrop } = restrictPanZoom(
|
|
794
830
|
{
|
|
795
831
|
...currentState,
|
|
796
832
|
pan: {
|
|
797
833
|
x: currentState.pan.x,
|
|
798
|
-
y: currentState.pan.y -
|
|
834
|
+
y: currentState.pan.y - keyboardStep,
|
|
799
835
|
},
|
|
800
836
|
},
|
|
801
837
|
getImageSizeFromState( currentState ),
|
|
@@ -806,11 +842,12 @@ export class InteractionController {
|
|
|
806
842
|
}
|
|
807
843
|
case 'ArrowLeft': {
|
|
808
844
|
e.preventDefault();
|
|
845
|
+
const keyboardStep = this.getKeyboardStep( e );
|
|
809
846
|
const { pan: newCrop } = restrictPanZoom(
|
|
810
847
|
{
|
|
811
848
|
...currentState,
|
|
812
849
|
pan: {
|
|
813
|
-
x: currentState.pan.x +
|
|
850
|
+
x: currentState.pan.x + keyboardStep,
|
|
814
851
|
y: currentState.pan.y,
|
|
815
852
|
},
|
|
816
853
|
},
|
|
@@ -822,11 +859,12 @@ export class InteractionController {
|
|
|
822
859
|
}
|
|
823
860
|
case 'ArrowRight': {
|
|
824
861
|
e.preventDefault();
|
|
862
|
+
const keyboardStep = this.getKeyboardStep( e );
|
|
825
863
|
const { pan: newCrop } = restrictPanZoom(
|
|
826
864
|
{
|
|
827
865
|
...currentState,
|
|
828
866
|
pan: {
|
|
829
|
-
x: currentState.pan.x -
|
|
867
|
+
x: currentState.pan.x - keyboardStep,
|
|
830
868
|
y: currentState.pan.y,
|
|
831
869
|
},
|
|
832
870
|
},
|
|
@@ -858,11 +896,29 @@ export class InteractionController {
|
|
|
858
896
|
}
|
|
859
897
|
case 'r':
|
|
860
898
|
case 'R': {
|
|
899
|
+
if ( e.metaKey || e.ctrlKey || e.altKey ) {
|
|
900
|
+
break;
|
|
901
|
+
}
|
|
902
|
+
e.preventDefault();
|
|
903
|
+
this.options.actions.snapRotate90( e.shiftKey ? -1 : 1 );
|
|
904
|
+
break;
|
|
905
|
+
}
|
|
906
|
+
case 'h':
|
|
907
|
+
case 'H': {
|
|
908
|
+
if ( e.metaKey || e.ctrlKey || e.altKey || e.shiftKey ) {
|
|
909
|
+
break;
|
|
910
|
+
}
|
|
911
|
+
e.preventDefault();
|
|
912
|
+
this.options.actions.toggleFlip?.( 'horizontal' );
|
|
913
|
+
break;
|
|
914
|
+
}
|
|
915
|
+
case 'v':
|
|
916
|
+
case 'V': {
|
|
861
917
|
if ( e.metaKey || e.ctrlKey || e.altKey || e.shiftKey ) {
|
|
862
918
|
break;
|
|
863
919
|
}
|
|
864
920
|
e.preventDefault();
|
|
865
|
-
this.options.actions.
|
|
921
|
+
this.options.actions.toggleFlip?.( 'vertical' );
|
|
866
922
|
break;
|
|
867
923
|
}
|
|
868
924
|
}
|
|
@@ -6,6 +6,13 @@ import { DEFAULT_STATE, MAX_ZOOM } from './constants';
|
|
|
6
6
|
import { normalizeRotation, degreesToRadians } from './math/rotation';
|
|
7
7
|
import { restrictPanZoom, restrictCropRect } from './containment';
|
|
8
8
|
|
|
9
|
+
/** Small tolerance for cropper floating-point comparisons. */
|
|
10
|
+
const STATE_EPSILON = 1e-6;
|
|
11
|
+
|
|
12
|
+
function nearlyEqual( a: number, b: number ): boolean {
|
|
13
|
+
return Math.abs( a - b ) < STATE_EPSILON;
|
|
14
|
+
}
|
|
15
|
+
|
|
9
16
|
/**
|
|
10
17
|
* Translate a pipeline transform operation into the equivalent
|
|
11
18
|
* reducer action. Pipeline ops aren't 1:1 with reducer actions —
|
|
@@ -414,15 +421,15 @@ export function isStateDirty(
|
|
|
414
421
|
initial: CropperState
|
|
415
422
|
): boolean {
|
|
416
423
|
return (
|
|
417
|
-
current.pan.x
|
|
418
|
-
current.pan.y
|
|
419
|
-
current.zoom
|
|
420
|
-
current.rotation
|
|
424
|
+
! nearlyEqual( current.pan.x, initial.pan.x ) ||
|
|
425
|
+
! nearlyEqual( current.pan.y, initial.pan.y ) ||
|
|
426
|
+
! nearlyEqual( current.zoom, initial.zoom ) ||
|
|
427
|
+
! nearlyEqual( current.rotation, initial.rotation ) ||
|
|
421
428
|
current.flip.horizontal !== initial.flip.horizontal ||
|
|
422
429
|
current.flip.vertical !== initial.flip.vertical ||
|
|
423
|
-
current.cropRect.x
|
|
424
|
-
current.cropRect.y
|
|
425
|
-
current.cropRect.width
|
|
426
|
-
current.cropRect.height
|
|
430
|
+
! nearlyEqual( current.cropRect.x, initial.cropRect.x ) ||
|
|
431
|
+
! nearlyEqual( current.cropRect.y, initial.cropRect.y ) ||
|
|
432
|
+
! nearlyEqual( current.cropRect.width, initial.cropRect.width ) ||
|
|
433
|
+
! nearlyEqual( current.cropRect.height, initial.cropRect.height )
|
|
427
434
|
);
|
|
428
435
|
}
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
import {
|
|
10
10
|
restrictPanZoom,
|
|
11
11
|
restrictCropRect,
|
|
12
|
-
|
|
12
|
+
getImageCropBounds,
|
|
13
13
|
} from '../containment';
|
|
14
14
|
import { getSourceRegion, getSourceRegionPercent } from '../source-region';
|
|
15
15
|
import { DEFAULT_STATE } from '../constants';
|
|
@@ -547,18 +547,16 @@ describe( 'getSourceRegionPercent', () => {
|
|
|
547
547
|
} );
|
|
548
548
|
} );
|
|
549
549
|
|
|
550
|
-
describe( '
|
|
551
|
-
it( '
|
|
552
|
-
//
|
|
553
|
-
// At zoom=1
|
|
554
|
-
//
|
|
555
|
-
//
|
|
556
|
-
// which is at normalized x = -0.0625.
|
|
550
|
+
describe( 'getImageCropBounds', () => {
|
|
551
|
+
it( 'returns the image AABB — handles can reach the image edge regardless of container size', () => {
|
|
552
|
+
// 500×375 (4:3) in a 600×400 container (3:2).
|
|
553
|
+
// At zoom=1.75 the image overflows the container. Bounds should reflect
|
|
554
|
+
// the image footprint, not the container edge, so one drag can reach the
|
|
555
|
+
// image edge (viewport pan reveals content outside the canvas boundary).
|
|
557
556
|
const nat: Size = { width: 500, height: 375 };
|
|
558
557
|
const container: Size = { width: 600, height: 400 };
|
|
559
558
|
const { elementSize, visualSize } = getImageFit( container, nat, 0 );
|
|
560
559
|
|
|
561
|
-
// Verify the image doesn't fill the container at zoom=1.
|
|
562
560
|
expect( visualSize.width ).toBeLessThan( container.width );
|
|
563
561
|
|
|
564
562
|
const state = makeState( {
|
|
@@ -571,23 +569,14 @@ describe( 'getCropBounds', () => {
|
|
|
571
569
|
rotation: 0,
|
|
572
570
|
} );
|
|
573
571
|
|
|
574
|
-
const bounds =
|
|
575
|
-
state,
|
|
576
|
-
elementSize,
|
|
577
|
-
visualSize,
|
|
578
|
-
container
|
|
579
|
-
);
|
|
572
|
+
const bounds = getImageCropBounds( state, elementSize, visualSize );
|
|
580
573
|
|
|
581
|
-
//
|
|
582
|
-
|
|
583
|
-
//
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
expect(
|
|
587
|
-
|
|
588
|
-
// boundsMaxX should be > 1 (crop can extend right of visual image).
|
|
589
|
-
expect( bounds.maxX ).toBeGreaterThan( 1 );
|
|
590
|
-
const pixelRight = offsetX + bounds.maxX * visualSize.width;
|
|
591
|
-
expect( pixelRight ).toBeCloseTo( container.width, 0 );
|
|
574
|
+
// At zoom=1.75 the image extends well beyond the container on all sides.
|
|
575
|
+
// Bounds should be the image AABB corners (≈ ±0.375), not the container
|
|
576
|
+
// edges (which would be ≈ ±0.0625).
|
|
577
|
+
expect( bounds.minX ).toBeLessThan( -0.3 );
|
|
578
|
+
expect( bounds.maxX ).toBeGreaterThan( 1.3 );
|
|
579
|
+
expect( bounds.minY ).toBeLessThan( -0.3 );
|
|
580
|
+
expect( bounds.maxY ).toBeGreaterThan( 1.3 );
|
|
592
581
|
} );
|
|
593
582
|
} );
|