@wordpress/media-editor 0.20.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +2 -1
- package/build/components/media-editor/index.cjs +7 -16
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +1 -2
- package/build/components/media-editor/use-crop-options.cjs.map +2 -2
- 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-modal/index.cjs +2 -0
- 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/image-editor/react/components/cropper.cjs +1 -11
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/state/composite-reducer.cjs +33 -8
- package/build/state/composite-reducer.cjs.map +3 -3
- package/build/state/index.cjs.map +1 -1
- package/build/state/types.cjs.map +1 -1
- package/build/state/use-media-editor-state.cjs +20 -7
- package/build/state/use-media-editor-state.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +9 -18
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +1 -2
- package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
- package/build-module/components/media-editor-canvas/index.mjs +2 -2
- 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-modal/index.mjs +2 -0
- 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/image-editor/react/components/cropper.mjs +1 -11
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/state/composite-reducer.mjs +23 -8
- package/build-module/state/composite-reducer.mjs.map +2 -2
- package/build-module/state/index.mjs.map +1 -1
- package/build-module/state/types.mjs.map +1 -1
- package/build-module/state/use-media-editor-state.mjs +20 -7
- package/build-module/state/use-media-editor-state.mjs.map +2 -2
- package/build-types/components/media-editor/index.d.ts +2 -1
- 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-crop-panel/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/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts +1 -1
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/index.d.ts +1 -1
- package/build-types/state/index.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 +16 -5
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/package.json +33 -22
- package/src/components/media-editor/index.tsx +12 -22
- package/src/components/media-editor/use-crop-options.ts +0 -2
- package/src/components/media-editor-canvas/index.tsx +2 -2
- package/src/components/media-editor-crop-panel/index.tsx +17 -9
- package/src/components/media-editor-image-controls/style.scss +1 -1
- package/src/components/media-editor-modal/index.tsx +8 -1
- 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/image-editor/docs/architecture.md +4 -0
- package/src/image-editor/react/components/cropper.tsx +1 -15
- package/src/state/composite-reducer.ts +24 -11
- package/src/state/index.ts +2 -0
- package/src/state/types.ts +37 -5
- package/src/state/use-media-editor-state.ts +46 -11
- package/global.d.ts +0 -5
- package/src/components/media-editor/test/restore-original.browser.test.tsx +0 -227
- package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -106
- package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -72
- package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -186
- 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 -182
- 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 -1299
- 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 -1462
- 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.browser.test.tsx +0 -79
- package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -386
- 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,1073 +0,0 @@
|
|
|
1
|
-
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
2
|
-
import { useState, useCallback, useEffect, useRef } from '@wordpress/element';
|
|
3
|
-
import {
|
|
4
|
-
Button,
|
|
5
|
-
SelectControl as WCSelectControl,
|
|
6
|
-
RangeControl,
|
|
7
|
-
ToggleControl,
|
|
8
|
-
Flex,
|
|
9
|
-
FlexItem,
|
|
10
|
-
} from '@wordpress/components';
|
|
11
|
-
import {
|
|
12
|
-
rotateLeft,
|
|
13
|
-
rotateRight,
|
|
14
|
-
flipHorizontal,
|
|
15
|
-
flipVertical,
|
|
16
|
-
reset as resetIcon,
|
|
17
|
-
cloudUpload,
|
|
18
|
-
download as downloadIcon,
|
|
19
|
-
} from '@wordpress/icons';
|
|
20
|
-
import { Cropper } from '../react/components/cropper';
|
|
21
|
-
import { useCropperReducer } from '../react/hooks/use-cropper-reducer';
|
|
22
|
-
import {
|
|
23
|
-
MAX_ZOOM,
|
|
24
|
-
MAX_ROTATION_OFFSET,
|
|
25
|
-
DEFAULT_ASPECT_RATIOS,
|
|
26
|
-
ORIGINAL_ASPECT_RATIO,
|
|
27
|
-
} from '../core/constants';
|
|
28
|
-
import { getMinZoom, restrictPanZoom } from '../core/containment';
|
|
29
|
-
import {
|
|
30
|
-
loadImage,
|
|
31
|
-
renderToCanvas,
|
|
32
|
-
canvasToDataURL,
|
|
33
|
-
downloadCroppedImage,
|
|
34
|
-
} from '../core/export/canvas-renderer';
|
|
35
|
-
import {
|
|
36
|
-
getRotatedBBox,
|
|
37
|
-
createCamera,
|
|
38
|
-
screenToWorld,
|
|
39
|
-
getImageFit,
|
|
40
|
-
getVisibleBounds,
|
|
41
|
-
} from '../core/camera';
|
|
42
|
-
import { getSourceRegion } from '../core/source-region';
|
|
43
|
-
import styles from './style.module.css';
|
|
44
|
-
|
|
45
|
-
const SAMPLE_IMAGE = 'image-editor-demo.jpeg';
|
|
46
|
-
|
|
47
|
-
const GRID_MODES = {
|
|
48
|
-
off: false,
|
|
49
|
-
on: true,
|
|
50
|
-
interactive: 'interactive',
|
|
51
|
-
} as const;
|
|
52
|
-
|
|
53
|
-
type GridMode = keyof typeof GRID_MODES;
|
|
54
|
-
|
|
55
|
-
const IMAGE_CREDIT = (
|
|
56
|
-
<p style={ { fontSize: 10, color: '#aaa', margin: '4px 0 0' } }>
|
|
57
|
-
{ '"A fashionable melange of English words (1887)" ' }
|
|
58
|
-
<a
|
|
59
|
-
href="http://publicdomainreview.org/2013/01/23/a-fashionable-melange-of-english-words-1887"
|
|
60
|
-
style={ { color: '#bbb' } }
|
|
61
|
-
>
|
|
62
|
-
publicdomainreview.org
|
|
63
|
-
</a>
|
|
64
|
-
{ ' by See-ming Lee (SML) is licensed under CC BY 2.0.' }
|
|
65
|
-
</p>
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
// Hook: manage a swappable image source. Defaults to the sample image.
|
|
69
|
-
// When a file is picked, reads it as a data URL so the <img> src updates
|
|
70
|
-
// without leaving the page. The consumer also gets a reset callback to
|
|
71
|
-
// return to the sample.
|
|
72
|
-
function useUploadableImage() {
|
|
73
|
-
const [ src, setSrc ] = useState< string >( SAMPLE_IMAGE );
|
|
74
|
-
const [ isCustom, setIsCustom ] = useState( false );
|
|
75
|
-
const handleFileChange = useCallback(
|
|
76
|
-
( event: React.ChangeEvent< HTMLInputElement > ) => {
|
|
77
|
-
const file = event.target.files?.[ 0 ];
|
|
78
|
-
if ( ! file ) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
const reader = new FileReader();
|
|
82
|
-
reader.onload = () => {
|
|
83
|
-
if ( typeof reader.result === 'string' ) {
|
|
84
|
-
setSrc( reader.result );
|
|
85
|
-
setIsCustom( true );
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
reader.readAsDataURL( file );
|
|
89
|
-
},
|
|
90
|
-
[]
|
|
91
|
-
);
|
|
92
|
-
const resetToSample = useCallback( () => {
|
|
93
|
-
setSrc( SAMPLE_IMAGE );
|
|
94
|
-
setIsCustom( false );
|
|
95
|
-
}, [] );
|
|
96
|
-
return { src, isCustom, handleFileChange, resetToSample };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Resolve an aspect ratio value from the select dropdown.
|
|
101
|
-
* 0 = free (no lock), ORIGINAL_ASPECT_RATIO (-1) = image's natural ratio.
|
|
102
|
-
*/
|
|
103
|
-
function resolveAspectRatio(
|
|
104
|
-
value: string,
|
|
105
|
-
imageState: { naturalWidth: number; naturalHeight: number } | null
|
|
106
|
-
): number | undefined {
|
|
107
|
-
const num = parseFloat( value );
|
|
108
|
-
if ( num === 0 ) {
|
|
109
|
-
return undefined; // Free — no lock.
|
|
110
|
-
}
|
|
111
|
-
if ( num === ORIGINAL_ASPECT_RATIO && imageState ) {
|
|
112
|
-
return imageState.naturalWidth / imageState.naturalHeight;
|
|
113
|
-
}
|
|
114
|
-
if ( num > 0 ) {
|
|
115
|
-
return num;
|
|
116
|
-
}
|
|
117
|
-
return undefined;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const meta: Meta< typeof Cropper > = {
|
|
121
|
-
id: 'mediaeditor-imageeditor',
|
|
122
|
-
title: 'Editor/Media Editor/ImageEditor',
|
|
123
|
-
component: Cropper,
|
|
124
|
-
tags: [ 'status-experimental' ],
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
export default meta;
|
|
128
|
-
|
|
129
|
-
type Story = StoryObj< typeof Cropper >;
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Default story. Basic cropper with a sample image, no controls.
|
|
133
|
-
*/
|
|
134
|
-
const DefaultComponent = () => {
|
|
135
|
-
const controller = useCropperReducer();
|
|
136
|
-
|
|
137
|
-
return (
|
|
138
|
-
<div>
|
|
139
|
-
<div className={ styles.container }>
|
|
140
|
-
<Cropper
|
|
141
|
-
src={ SAMPLE_IMAGE }
|
|
142
|
-
controller={ controller }
|
|
143
|
-
showDimming
|
|
144
|
-
/>
|
|
145
|
-
</div>
|
|
146
|
-
{ IMAGE_CREDIT }
|
|
147
|
-
</div>
|
|
148
|
-
);
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
export const Default: Story = {
|
|
152
|
-
parameters: {
|
|
153
|
-
// FIXME: Image credit text and its link fail color-contrast.
|
|
154
|
-
// See: https://github.com/WordPress/gutenberg/issues/81596
|
|
155
|
-
a11y: { test: 'todo' },
|
|
156
|
-
},
|
|
157
|
-
render: DefaultComponent,
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Full interactive demo with controls.
|
|
162
|
-
*/
|
|
163
|
-
const WithControlsComponent = () => {
|
|
164
|
-
const controller = useCropperReducer();
|
|
165
|
-
const {
|
|
166
|
-
state,
|
|
167
|
-
setRotation,
|
|
168
|
-
setFlip,
|
|
169
|
-
setZoom,
|
|
170
|
-
setCropRect,
|
|
171
|
-
snapRotate90,
|
|
172
|
-
reset,
|
|
173
|
-
isDirty,
|
|
174
|
-
} = controller;
|
|
175
|
-
|
|
176
|
-
const [ aspectRatioValue, setAspectRatioValue ] = useState( '0' );
|
|
177
|
-
const [ freeformCrop, setFreeformCrop ] = useState( false );
|
|
178
|
-
const [ gridMode, setGridMode ] = useState< GridMode >( 'interactive' );
|
|
179
|
-
const { src, isCustom, handleFileChange, resetToSample } =
|
|
180
|
-
useUploadableImage();
|
|
181
|
-
const fileInputRef = useRef< HTMLInputElement >( null );
|
|
182
|
-
const openFilePicker = useCallback( () => {
|
|
183
|
-
fileInputRef.current?.click();
|
|
184
|
-
}, [] );
|
|
185
|
-
|
|
186
|
-
// Reset crop/pan/zoom when the image changes so the old framing
|
|
187
|
-
// doesn't linger on a newly-shaped image.
|
|
188
|
-
const prevSrcRef = useRef( src );
|
|
189
|
-
useEffect( () => {
|
|
190
|
-
if ( prevSrcRef.current !== src ) {
|
|
191
|
-
prevSrcRef.current = src;
|
|
192
|
-
reset();
|
|
193
|
-
setAspectRatioValue( '0' );
|
|
194
|
-
}
|
|
195
|
-
}, [ src, reset ] );
|
|
196
|
-
|
|
197
|
-
// Compute the crop dimensions in source pixels.
|
|
198
|
-
const cropDimensions = state.image
|
|
199
|
-
? getSourceRegion( state, {
|
|
200
|
-
width: state.image.naturalWidth,
|
|
201
|
-
height: state.image.naturalHeight,
|
|
202
|
-
} )
|
|
203
|
-
: null;
|
|
204
|
-
|
|
205
|
-
// The base cardinal angle (nearest 90° step) and the fine offset.
|
|
206
|
-
// `setRotation` is a raw state setter (absolute-angle assignment), so
|
|
207
|
-
// sliders need to flip the offset sign when a single flip is active to
|
|
208
|
-
// stay visually consistent. `snapRotate90` and the pipeline `rotate`
|
|
209
|
-
// op already handle this internally.
|
|
210
|
-
const baseAngle = Math.round( state.rotation / 90 ) * 90;
|
|
211
|
-
const singleFlip = state.flip.horizontal !== state.flip.vertical;
|
|
212
|
-
const visualDir = singleFlip ? -1 : 1;
|
|
213
|
-
const fineOffset = ( state.rotation - baseAngle ) * visualDir;
|
|
214
|
-
|
|
215
|
-
const handleRotateLeft = useCallback( () => {
|
|
216
|
-
snapRotate90( -1 );
|
|
217
|
-
}, [ snapRotate90 ] );
|
|
218
|
-
|
|
219
|
-
const handleRotateRight = useCallback( () => {
|
|
220
|
-
snapRotate90( 1 );
|
|
221
|
-
}, [ snapRotate90 ] );
|
|
222
|
-
|
|
223
|
-
const handleRotationSlider = useCallback(
|
|
224
|
-
( value: number | undefined ) => {
|
|
225
|
-
if ( value === undefined ) {
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
// Clamp strictly inside [-MAX, MAX). Reaching exactly ±MAX
|
|
229
|
-
// lands state.rotation on a 90° midpoint, which flips the
|
|
230
|
-
// computed baseAngle on the next render; subsequent slider
|
|
231
|
-
// events then spiral because they're added to the NEW base.
|
|
232
|
-
const EPS = 0.01;
|
|
233
|
-
const clamped = Math.max(
|
|
234
|
-
-MAX_ROTATION_OFFSET + EPS,
|
|
235
|
-
Math.min( MAX_ROTATION_OFFSET - EPS, value )
|
|
236
|
-
);
|
|
237
|
-
setRotation( baseAngle + clamped * visualDir );
|
|
238
|
-
},
|
|
239
|
-
[ baseAngle, setRotation, visualDir ]
|
|
240
|
-
);
|
|
241
|
-
|
|
242
|
-
const handleFlipHorizontal = useCallback( () => {
|
|
243
|
-
setFlip( {
|
|
244
|
-
horizontal: ! state.flip.horizontal,
|
|
245
|
-
vertical: state.flip.vertical,
|
|
246
|
-
} );
|
|
247
|
-
}, [ state.flip, setFlip ] );
|
|
248
|
-
|
|
249
|
-
const handleFlipVertical = useCallback( () => {
|
|
250
|
-
setFlip( {
|
|
251
|
-
horizontal: state.flip.horizontal,
|
|
252
|
-
vertical: ! state.flip.vertical,
|
|
253
|
-
} );
|
|
254
|
-
}, [ state.flip, setFlip ] );
|
|
255
|
-
|
|
256
|
-
const handleZoomChange = useCallback(
|
|
257
|
-
( value: number | undefined ) => {
|
|
258
|
-
if ( value === undefined ) {
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
setZoom( value );
|
|
262
|
-
},
|
|
263
|
-
[ setZoom ]
|
|
264
|
-
);
|
|
265
|
-
|
|
266
|
-
const handleAspectRatioChange = useCallback(
|
|
267
|
-
( value: string ) => {
|
|
268
|
-
setAspectRatioValue( value );
|
|
269
|
-
|
|
270
|
-
// In fixed mode (freeformCrop=false), the cropper's useEffect
|
|
271
|
-
// auto-computes the crop rect from the aspect ratio. In freeform
|
|
272
|
-
// mode, we adjust the crop rect here to fit the new ratio.
|
|
273
|
-
const resolved = resolveAspectRatio( value, state.image );
|
|
274
|
-
if ( freeformCrop && resolved && resolved > 0 && state.image ) {
|
|
275
|
-
const ratio = resolved;
|
|
276
|
-
const natW = state.image.naturalWidth;
|
|
277
|
-
const natH = state.image.naturalHeight;
|
|
278
|
-
const visualBBox = getRotatedBBox( natW, natH, state.rotation );
|
|
279
|
-
// normalizedRatio = w/h in normalized space that produces
|
|
280
|
-
// the desired pixel aspect ratio.
|
|
281
|
-
const normalizedRatio =
|
|
282
|
-
( ratio * visualBBox.height ) / visualBBox.width;
|
|
283
|
-
|
|
284
|
-
let w = state.cropRect.width;
|
|
285
|
-
let h = w / normalizedRatio;
|
|
286
|
-
|
|
287
|
-
if ( h > 1 ) {
|
|
288
|
-
h = state.cropRect.height;
|
|
289
|
-
w = h * normalizedRatio;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
w = Math.min( w, 1 );
|
|
293
|
-
h = Math.min( h, 1 );
|
|
294
|
-
|
|
295
|
-
setCropRect( {
|
|
296
|
-
x: ( 1 - w ) / 2,
|
|
297
|
-
y: ( 1 - h ) / 2,
|
|
298
|
-
width: w,
|
|
299
|
-
height: h,
|
|
300
|
-
} );
|
|
301
|
-
}
|
|
302
|
-
},
|
|
303
|
-
[
|
|
304
|
-
freeformCrop,
|
|
305
|
-
state.image,
|
|
306
|
-
state.cropRect,
|
|
307
|
-
state.rotation,
|
|
308
|
-
setCropRect,
|
|
309
|
-
]
|
|
310
|
-
);
|
|
311
|
-
|
|
312
|
-
const handleReset = useCallback( () => {
|
|
313
|
-
reset();
|
|
314
|
-
setAspectRatioValue( '0' );
|
|
315
|
-
}, [ reset ] );
|
|
316
|
-
|
|
317
|
-
return (
|
|
318
|
-
<div>
|
|
319
|
-
<input
|
|
320
|
-
ref={ fileInputRef }
|
|
321
|
-
type="file"
|
|
322
|
-
accept="image/*"
|
|
323
|
-
onChange={ handleFileChange }
|
|
324
|
-
className={ styles[ 'hidden-file' ] }
|
|
325
|
-
/>
|
|
326
|
-
<div className={ styles.toolbar }>
|
|
327
|
-
<Flex align="center" gap={ 2 } wrap>
|
|
328
|
-
<FlexItem>
|
|
329
|
-
<Button
|
|
330
|
-
__next40pxDefaultSize
|
|
331
|
-
variant="primary"
|
|
332
|
-
icon={ cloudUpload }
|
|
333
|
-
onClick={ openFilePicker }
|
|
334
|
-
>
|
|
335
|
-
Upload
|
|
336
|
-
</Button>
|
|
337
|
-
</FlexItem>
|
|
338
|
-
{ isCustom && (
|
|
339
|
-
<FlexItem>
|
|
340
|
-
<Button
|
|
341
|
-
__next40pxDefaultSize
|
|
342
|
-
variant="tertiary"
|
|
343
|
-
onClick={ resetToSample }
|
|
344
|
-
>
|
|
345
|
-
Use sample
|
|
346
|
-
</Button>
|
|
347
|
-
</FlexItem>
|
|
348
|
-
) }
|
|
349
|
-
<FlexItem>
|
|
350
|
-
<Button
|
|
351
|
-
__next40pxDefaultSize
|
|
352
|
-
variant="secondary"
|
|
353
|
-
icon={ rotateLeft }
|
|
354
|
-
label="Rotate 90° counter-clockwise"
|
|
355
|
-
showTooltip
|
|
356
|
-
onClick={ handleRotateLeft }
|
|
357
|
-
/>
|
|
358
|
-
</FlexItem>
|
|
359
|
-
<FlexItem>
|
|
360
|
-
<Button
|
|
361
|
-
__next40pxDefaultSize
|
|
362
|
-
variant="secondary"
|
|
363
|
-
icon={ rotateRight }
|
|
364
|
-
label="Rotate 90° clockwise"
|
|
365
|
-
showTooltip
|
|
366
|
-
onClick={ handleRotateRight }
|
|
367
|
-
/>
|
|
368
|
-
</FlexItem>
|
|
369
|
-
<FlexItem>
|
|
370
|
-
<Button
|
|
371
|
-
__next40pxDefaultSize
|
|
372
|
-
variant="secondary"
|
|
373
|
-
icon={ flipHorizontal }
|
|
374
|
-
label="Flip horizontal"
|
|
375
|
-
showTooltip
|
|
376
|
-
isPressed={ state.flip.horizontal }
|
|
377
|
-
onClick={ handleFlipHorizontal }
|
|
378
|
-
/>
|
|
379
|
-
</FlexItem>
|
|
380
|
-
<FlexItem>
|
|
381
|
-
<Button
|
|
382
|
-
__next40pxDefaultSize
|
|
383
|
-
variant="secondary"
|
|
384
|
-
icon={ flipVertical }
|
|
385
|
-
label="Flip vertical"
|
|
386
|
-
showTooltip
|
|
387
|
-
isPressed={ state.flip.vertical }
|
|
388
|
-
onClick={ handleFlipVertical }
|
|
389
|
-
/>
|
|
390
|
-
</FlexItem>
|
|
391
|
-
<FlexItem>
|
|
392
|
-
<WCSelectControl
|
|
393
|
-
label="Aspect ratio"
|
|
394
|
-
hideLabelFromVision
|
|
395
|
-
value={ aspectRatioValue }
|
|
396
|
-
onChange={ handleAspectRatioChange }
|
|
397
|
-
options={ DEFAULT_ASPECT_RATIOS.map(
|
|
398
|
-
( preset ) => ( {
|
|
399
|
-
label: preset.label,
|
|
400
|
-
value: preset.value.toString(),
|
|
401
|
-
} )
|
|
402
|
-
) }
|
|
403
|
-
/>
|
|
404
|
-
</FlexItem>
|
|
405
|
-
<FlexItem>
|
|
406
|
-
<ToggleControl
|
|
407
|
-
label="Freeform"
|
|
408
|
-
checked={ freeformCrop }
|
|
409
|
-
onChange={ setFreeformCrop }
|
|
410
|
-
/>
|
|
411
|
-
</FlexItem>
|
|
412
|
-
<FlexItem>
|
|
413
|
-
<WCSelectControl
|
|
414
|
-
label="Grid"
|
|
415
|
-
hideLabelFromVision
|
|
416
|
-
value={ gridMode }
|
|
417
|
-
onChange={ ( value ) =>
|
|
418
|
-
setGridMode( value as GridMode )
|
|
419
|
-
}
|
|
420
|
-
options={ [
|
|
421
|
-
{ label: 'Grid: off', value: 'off' },
|
|
422
|
-
{ label: 'Grid: always on', value: 'on' },
|
|
423
|
-
{
|
|
424
|
-
label: 'Grid: interactive',
|
|
425
|
-
value: 'interactive',
|
|
426
|
-
},
|
|
427
|
-
] }
|
|
428
|
-
/>
|
|
429
|
-
</FlexItem>
|
|
430
|
-
<FlexItem isBlock />
|
|
431
|
-
<FlexItem>
|
|
432
|
-
<Button
|
|
433
|
-
__next40pxDefaultSize
|
|
434
|
-
variant="primary"
|
|
435
|
-
icon={ resetIcon }
|
|
436
|
-
disabled={ ! isDirty }
|
|
437
|
-
accessibleWhenDisabled
|
|
438
|
-
onClick={ handleReset }
|
|
439
|
-
>
|
|
440
|
-
Reset
|
|
441
|
-
</Button>
|
|
442
|
-
</FlexItem>
|
|
443
|
-
</Flex>
|
|
444
|
-
<div className={ styles.sliders }>
|
|
445
|
-
<RangeControl
|
|
446
|
-
label="Fine rotation"
|
|
447
|
-
min={ -MAX_ROTATION_OFFSET }
|
|
448
|
-
max={ MAX_ROTATION_OFFSET }
|
|
449
|
-
step={ 0.5 }
|
|
450
|
-
value={ fineOffset }
|
|
451
|
-
onChange={ handleRotationSlider }
|
|
452
|
-
/>
|
|
453
|
-
<RangeControl
|
|
454
|
-
label="Zoom"
|
|
455
|
-
min={ getMinZoom( state ) }
|
|
456
|
-
max={ MAX_ZOOM }
|
|
457
|
-
step={ 0.1 }
|
|
458
|
-
value={ state.zoom }
|
|
459
|
-
onChange={ handleZoomChange }
|
|
460
|
-
/>
|
|
461
|
-
</div>
|
|
462
|
-
</div>
|
|
463
|
-
|
|
464
|
-
<div className={ styles.resizable }>
|
|
465
|
-
<Cropper
|
|
466
|
-
src={ src }
|
|
467
|
-
controller={ controller }
|
|
468
|
-
showGrid={ GRID_MODES[ gridMode ] }
|
|
469
|
-
showDimming
|
|
470
|
-
freeformCrop={ freeformCrop }
|
|
471
|
-
aspectRatio={ resolveAspectRatio(
|
|
472
|
-
aspectRatioValue,
|
|
473
|
-
state.image
|
|
474
|
-
) }
|
|
475
|
-
/>
|
|
476
|
-
</div>
|
|
477
|
-
<Flex justify="space-between" align="center">
|
|
478
|
-
<FlexItem>{ ! isCustom && IMAGE_CREDIT }</FlexItem>
|
|
479
|
-
{ cropDimensions && (
|
|
480
|
-
<FlexItem
|
|
481
|
-
style={ {
|
|
482
|
-
fontSize: 11,
|
|
483
|
-
color: '#757575',
|
|
484
|
-
fontFamily: 'monospace',
|
|
485
|
-
} }
|
|
486
|
-
>
|
|
487
|
-
{ Math.round( cropDimensions.width ) } ×{ ' ' }
|
|
488
|
-
{ Math.round( cropDimensions.height ) } px
|
|
489
|
-
</FlexItem>
|
|
490
|
-
) }
|
|
491
|
-
</Flex>
|
|
492
|
-
|
|
493
|
-
<div style={ { marginTop: 16 } }>
|
|
494
|
-
<strong>Current State:</strong>
|
|
495
|
-
<pre className={ styles.state }>
|
|
496
|
-
{ JSON.stringify(
|
|
497
|
-
{
|
|
498
|
-
rotation: state.rotation,
|
|
499
|
-
zoom: state.zoom,
|
|
500
|
-
cropRect: state.cropRect,
|
|
501
|
-
pan: state.pan,
|
|
502
|
-
image: state.image
|
|
503
|
-
? {
|
|
504
|
-
naturalWidth: state.image.naturalWidth,
|
|
505
|
-
naturalHeight:
|
|
506
|
-
state.image.naturalHeight,
|
|
507
|
-
}
|
|
508
|
-
: null,
|
|
509
|
-
},
|
|
510
|
-
null,
|
|
511
|
-
2
|
|
512
|
-
) }
|
|
513
|
-
</pre>
|
|
514
|
-
</div>
|
|
515
|
-
</div>
|
|
516
|
-
);
|
|
517
|
-
};
|
|
518
|
-
|
|
519
|
-
export const WithControls: Story = {
|
|
520
|
-
parameters: {
|
|
521
|
-
// FIXME: The state dump scrolls once its content overflows, and is not keyboard-accessible (scrollable-region-focusable). Whether it overflows depends on the numbers rendered, so this appears intermittently.
|
|
522
|
-
// See: https://github.com/WordPress/gutenberg/issues/81596
|
|
523
|
-
a11y: { test: 'todo' },
|
|
524
|
-
},
|
|
525
|
-
render: WithControlsComponent,
|
|
526
|
-
};
|
|
527
|
-
|
|
528
|
-
/**
|
|
529
|
-
* Debug visualization showing camera internals, export preview, and
|
|
530
|
-
* image info alongside the cropper.
|
|
531
|
-
*
|
|
532
|
-
* Displays the camera matrix, crop corner world-space coordinates,
|
|
533
|
-
* restriction values, source region, and a live export preview --
|
|
534
|
-
* updated live as you interact. Use this to debug containment issues
|
|
535
|
-
* or verify the camera and render paths agree.
|
|
536
|
-
*/
|
|
537
|
-
const DebugComponent = () => {
|
|
538
|
-
const controller = useCropperReducer();
|
|
539
|
-
const {
|
|
540
|
-
state,
|
|
541
|
-
setRotation,
|
|
542
|
-
setZoom,
|
|
543
|
-
setFlip,
|
|
544
|
-
snapRotate90,
|
|
545
|
-
reset,
|
|
546
|
-
isDirty,
|
|
547
|
-
} = controller;
|
|
548
|
-
|
|
549
|
-
const [ freeformCrop, setFreeformCrop ] = useState( false );
|
|
550
|
-
const [ exportFormat, setExportFormat ] =
|
|
551
|
-
useState< string >( 'image/jpeg' );
|
|
552
|
-
const { src, isCustom, handleFileChange, resetToSample } =
|
|
553
|
-
useUploadableImage();
|
|
554
|
-
const fileInputRef = useRef< HTMLInputElement >( null );
|
|
555
|
-
const [ previewSrc, setPreviewSrc ] = useState< string | null >( null );
|
|
556
|
-
const imageRef = useRef< HTMLImageElement | null >( null );
|
|
557
|
-
|
|
558
|
-
// Reset crop/pan/zoom when the image changes.
|
|
559
|
-
const prevSrcRef = useRef( src );
|
|
560
|
-
useEffect( () => {
|
|
561
|
-
if ( prevSrcRef.current !== src ) {
|
|
562
|
-
prevSrcRef.current = src;
|
|
563
|
-
reset();
|
|
564
|
-
}
|
|
565
|
-
}, [ src, reset ] );
|
|
566
|
-
|
|
567
|
-
const [ containerSize, setContainerSize ] = useState( {
|
|
568
|
-
width: 0,
|
|
569
|
-
height: 0,
|
|
570
|
-
} );
|
|
571
|
-
const containerRef = useRef< HTMLDivElement >( null );
|
|
572
|
-
|
|
573
|
-
// Load the source image whenever the src changes (sample or uploaded).
|
|
574
|
-
useEffect( () => {
|
|
575
|
-
let cancelled = false;
|
|
576
|
-
loadImage( src ).then( ( img ) => {
|
|
577
|
-
if ( ! cancelled ) {
|
|
578
|
-
imageRef.current = img;
|
|
579
|
-
}
|
|
580
|
-
} );
|
|
581
|
-
return () => {
|
|
582
|
-
cancelled = true;
|
|
583
|
-
};
|
|
584
|
-
}, [ src ] );
|
|
585
|
-
|
|
586
|
-
// Re-render the preview whenever state changes.
|
|
587
|
-
useEffect( () => {
|
|
588
|
-
if ( ! imageRef.current || ! state.image ) {
|
|
589
|
-
return;
|
|
590
|
-
}
|
|
591
|
-
const canvas = renderToCanvas( imageRef.current, state );
|
|
592
|
-
setPreviewSrc( canvasToDataURL( canvas, 'image/jpeg', 0.85 ) );
|
|
593
|
-
}, [ state ] );
|
|
594
|
-
|
|
595
|
-
// Track container size for camera computations.
|
|
596
|
-
useEffect( () => {
|
|
597
|
-
const el = containerRef.current;
|
|
598
|
-
if ( ! el ) {
|
|
599
|
-
return;
|
|
600
|
-
}
|
|
601
|
-
const ro = new ResizeObserver( ( entries ) => {
|
|
602
|
-
const { width, height } = entries[ 0 ].contentRect;
|
|
603
|
-
setContainerSize( { width, height } );
|
|
604
|
-
} );
|
|
605
|
-
ro.observe( el );
|
|
606
|
-
return () => ro.disconnect();
|
|
607
|
-
}, [] );
|
|
608
|
-
|
|
609
|
-
const imageSize = state.image
|
|
610
|
-
? { width: state.image.naturalWidth, height: state.image.naturalHeight }
|
|
611
|
-
: { width: 0, height: 0 };
|
|
612
|
-
|
|
613
|
-
const hasImage = imageSize.width > 0 && containerSize.width > 0;
|
|
614
|
-
const { elementSize, visualSize } = hasImage
|
|
615
|
-
? getImageFit( containerSize, imageSize, state.rotation )
|
|
616
|
-
: {
|
|
617
|
-
elementSize: { width: 0, height: 0 },
|
|
618
|
-
visualSize: { width: 0, height: 0 },
|
|
619
|
-
};
|
|
620
|
-
|
|
621
|
-
// Camera and restriction.
|
|
622
|
-
const camera = hasImage
|
|
623
|
-
? createCamera( state, containerSize, imageSize )
|
|
624
|
-
: null;
|
|
625
|
-
const baseCamera = hasImage
|
|
626
|
-
? createCamera(
|
|
627
|
-
{ ...state, pan: { x: 0, y: 0 }, zoom: 1 },
|
|
628
|
-
containerSize,
|
|
629
|
-
imageSize
|
|
630
|
-
)
|
|
631
|
-
: null;
|
|
632
|
-
const vb = baseCamera ? getVisibleBounds( baseCamera ) : null;
|
|
633
|
-
|
|
634
|
-
// Crop corners in world space (should be inside [0,1] when contained).
|
|
635
|
-
const cropWorldCorners =
|
|
636
|
-
camera && vb
|
|
637
|
-
? [
|
|
638
|
-
screenToWorld( camera, {
|
|
639
|
-
x: vb.left + state.cropRect.x * vb.width,
|
|
640
|
-
y: vb.top + state.cropRect.y * vb.height,
|
|
641
|
-
} ),
|
|
642
|
-
screenToWorld( camera, {
|
|
643
|
-
x:
|
|
644
|
-
vb.left +
|
|
645
|
-
( state.cropRect.x + state.cropRect.width ) *
|
|
646
|
-
vb.width,
|
|
647
|
-
y:
|
|
648
|
-
vb.top +
|
|
649
|
-
( state.cropRect.y + state.cropRect.height ) *
|
|
650
|
-
vb.height,
|
|
651
|
-
} ),
|
|
652
|
-
]
|
|
653
|
-
: null;
|
|
654
|
-
|
|
655
|
-
// Restriction result.
|
|
656
|
-
const restrictionResult = hasImage
|
|
657
|
-
? restrictPanZoom( state, imageSize, state.cropRect )
|
|
658
|
-
: null;
|
|
659
|
-
|
|
660
|
-
// Source region.
|
|
661
|
-
const sourceRegion = hasImage ? getSourceRegion( state, imageSize ) : null;
|
|
662
|
-
|
|
663
|
-
// Is contained? All crop corners in [0,1].
|
|
664
|
-
const isContained =
|
|
665
|
-
cropWorldCorners &&
|
|
666
|
-
cropWorldCorners[ 0 ].x >= -0.001 &&
|
|
667
|
-
cropWorldCorners[ 0 ].y >= -0.001 &&
|
|
668
|
-
cropWorldCorners[ 1 ].x <= 1.001 &&
|
|
669
|
-
cropWorldCorners[ 1 ].y <= 1.001;
|
|
670
|
-
|
|
671
|
-
// `setRotation` is a raw state setter (absolute-angle assignment), so
|
|
672
|
-
// sliders need to flip the offset sign when a single flip is active
|
|
673
|
-
// to stay visually consistent. `snapRotate90` already handles this.
|
|
674
|
-
const baseAngle = Math.round( state.rotation / 90 ) * 90;
|
|
675
|
-
const singleFlip = state.flip.horizontal !== state.flip.vertical;
|
|
676
|
-
const visualDir = singleFlip ? -1 : 1;
|
|
677
|
-
const fineOffset = ( state.rotation - baseAngle ) * visualDir;
|
|
678
|
-
|
|
679
|
-
const handleRotateLeft = useCallback( () => {
|
|
680
|
-
snapRotate90( -1 );
|
|
681
|
-
}, [ snapRotate90 ] );
|
|
682
|
-
|
|
683
|
-
const handleRotateRight = useCallback( () => {
|
|
684
|
-
snapRotate90( 1 );
|
|
685
|
-
}, [ snapRotate90 ] );
|
|
686
|
-
|
|
687
|
-
const handleRotationSlider = useCallback(
|
|
688
|
-
( value: number | undefined ) => {
|
|
689
|
-
if ( value === undefined ) {
|
|
690
|
-
return;
|
|
691
|
-
}
|
|
692
|
-
// Clamp strictly inside [-MAX, MAX). Reaching exactly ±MAX
|
|
693
|
-
// lands state.rotation on a 90° midpoint, which flips the
|
|
694
|
-
// computed baseAngle on the next render; subsequent slider
|
|
695
|
-
// events then spiral because they're added to the NEW base.
|
|
696
|
-
const EPS = 0.01;
|
|
697
|
-
const clamped = Math.max(
|
|
698
|
-
-MAX_ROTATION_OFFSET + EPS,
|
|
699
|
-
Math.min( MAX_ROTATION_OFFSET - EPS, value )
|
|
700
|
-
);
|
|
701
|
-
setRotation( baseAngle + clamped * visualDir );
|
|
702
|
-
},
|
|
703
|
-
[ baseAngle, setRotation, visualDir ]
|
|
704
|
-
);
|
|
705
|
-
|
|
706
|
-
const handleZoomChange = useCallback(
|
|
707
|
-
( value: number | undefined ) => {
|
|
708
|
-
if ( value === undefined ) {
|
|
709
|
-
return;
|
|
710
|
-
}
|
|
711
|
-
setZoom( value );
|
|
712
|
-
},
|
|
713
|
-
[ setZoom ]
|
|
714
|
-
);
|
|
715
|
-
|
|
716
|
-
const handleFlipHorizontal = useCallback( () => {
|
|
717
|
-
setFlip( {
|
|
718
|
-
horizontal: ! state.flip.horizontal,
|
|
719
|
-
vertical: state.flip.vertical,
|
|
720
|
-
} );
|
|
721
|
-
}, [ state.flip, setFlip ] );
|
|
722
|
-
|
|
723
|
-
const handleFlipVertical = useCallback( () => {
|
|
724
|
-
setFlip( {
|
|
725
|
-
horizontal: state.flip.horizontal,
|
|
726
|
-
vertical: ! state.flip.vertical,
|
|
727
|
-
} );
|
|
728
|
-
}, [ state.flip, setFlip ] );
|
|
729
|
-
|
|
730
|
-
const openFilePicker = useCallback( () => {
|
|
731
|
-
fileInputRef.current?.click();
|
|
732
|
-
}, [] );
|
|
733
|
-
|
|
734
|
-
return (
|
|
735
|
-
<div>
|
|
736
|
-
<input
|
|
737
|
-
ref={ fileInputRef }
|
|
738
|
-
type="file"
|
|
739
|
-
accept="image/*"
|
|
740
|
-
onChange={ handleFileChange }
|
|
741
|
-
className={ styles[ 'hidden-file' ] }
|
|
742
|
-
/>
|
|
743
|
-
<div className={ styles.toolbar }>
|
|
744
|
-
<Flex align="center" gap={ 2 } wrap>
|
|
745
|
-
<FlexItem>
|
|
746
|
-
<Button
|
|
747
|
-
__next40pxDefaultSize
|
|
748
|
-
variant="primary"
|
|
749
|
-
icon={ cloudUpload }
|
|
750
|
-
onClick={ openFilePicker }
|
|
751
|
-
>
|
|
752
|
-
Upload
|
|
753
|
-
</Button>
|
|
754
|
-
</FlexItem>
|
|
755
|
-
{ isCustom && (
|
|
756
|
-
<FlexItem>
|
|
757
|
-
<Button
|
|
758
|
-
__next40pxDefaultSize
|
|
759
|
-
variant="tertiary"
|
|
760
|
-
onClick={ resetToSample }
|
|
761
|
-
>
|
|
762
|
-
Use sample
|
|
763
|
-
</Button>
|
|
764
|
-
</FlexItem>
|
|
765
|
-
) }
|
|
766
|
-
<FlexItem>
|
|
767
|
-
<Button
|
|
768
|
-
__next40pxDefaultSize
|
|
769
|
-
variant="secondary"
|
|
770
|
-
icon={ rotateLeft }
|
|
771
|
-
label="Rotate 90° counter-clockwise"
|
|
772
|
-
showTooltip
|
|
773
|
-
onClick={ handleRotateLeft }
|
|
774
|
-
/>
|
|
775
|
-
</FlexItem>
|
|
776
|
-
<FlexItem>
|
|
777
|
-
<Button
|
|
778
|
-
__next40pxDefaultSize
|
|
779
|
-
variant="secondary"
|
|
780
|
-
icon={ rotateRight }
|
|
781
|
-
label="Rotate 90° clockwise"
|
|
782
|
-
showTooltip
|
|
783
|
-
onClick={ handleRotateRight }
|
|
784
|
-
/>
|
|
785
|
-
</FlexItem>
|
|
786
|
-
<FlexItem>
|
|
787
|
-
<Button
|
|
788
|
-
__next40pxDefaultSize
|
|
789
|
-
variant="secondary"
|
|
790
|
-
icon={ flipHorizontal }
|
|
791
|
-
label="Flip horizontal"
|
|
792
|
-
showTooltip
|
|
793
|
-
isPressed={ state.flip.horizontal }
|
|
794
|
-
onClick={ handleFlipHorizontal }
|
|
795
|
-
/>
|
|
796
|
-
</FlexItem>
|
|
797
|
-
<FlexItem>
|
|
798
|
-
<Button
|
|
799
|
-
__next40pxDefaultSize
|
|
800
|
-
variant="secondary"
|
|
801
|
-
icon={ flipVertical }
|
|
802
|
-
label="Flip vertical"
|
|
803
|
-
showTooltip
|
|
804
|
-
isPressed={ state.flip.vertical }
|
|
805
|
-
onClick={ handleFlipVertical }
|
|
806
|
-
/>
|
|
807
|
-
</FlexItem>
|
|
808
|
-
<FlexItem>
|
|
809
|
-
<ToggleControl
|
|
810
|
-
label="Freeform"
|
|
811
|
-
checked={ freeformCrop }
|
|
812
|
-
onChange={ setFreeformCrop }
|
|
813
|
-
/>
|
|
814
|
-
</FlexItem>
|
|
815
|
-
<FlexItem isBlock />
|
|
816
|
-
<FlexItem>
|
|
817
|
-
<Button
|
|
818
|
-
__next40pxDefaultSize
|
|
819
|
-
variant="primary"
|
|
820
|
-
icon={ resetIcon }
|
|
821
|
-
disabled={ ! isDirty }
|
|
822
|
-
accessibleWhenDisabled
|
|
823
|
-
onClick={ () => reset() }
|
|
824
|
-
>
|
|
825
|
-
Reset
|
|
826
|
-
</Button>
|
|
827
|
-
</FlexItem>
|
|
828
|
-
<FlexItem>
|
|
829
|
-
<WCSelectControl
|
|
830
|
-
label="Format"
|
|
831
|
-
hideLabelFromVision
|
|
832
|
-
value={ exportFormat as 'image/jpeg' }
|
|
833
|
-
onChange={ ( value ) =>
|
|
834
|
-
setExportFormat( value as string )
|
|
835
|
-
}
|
|
836
|
-
options={ [
|
|
837
|
-
{ label: 'JPEG', value: 'image/jpeg' },
|
|
838
|
-
{ label: 'PNG', value: 'image/png' },
|
|
839
|
-
{ label: 'WebP', value: 'image/webp' },
|
|
840
|
-
] }
|
|
841
|
-
/>
|
|
842
|
-
</FlexItem>
|
|
843
|
-
<FlexItem>
|
|
844
|
-
<Button
|
|
845
|
-
__next40pxDefaultSize
|
|
846
|
-
variant="primary"
|
|
847
|
-
icon={ downloadIcon }
|
|
848
|
-
onClick={ () =>
|
|
849
|
-
downloadCroppedImage(
|
|
850
|
-
src,
|
|
851
|
-
state,
|
|
852
|
-
'cropped',
|
|
853
|
-
exportFormat,
|
|
854
|
-
0.9
|
|
855
|
-
)
|
|
856
|
-
}
|
|
857
|
-
>
|
|
858
|
-
Download
|
|
859
|
-
</Button>
|
|
860
|
-
</FlexItem>
|
|
861
|
-
</Flex>
|
|
862
|
-
<div className={ styles.sliders }>
|
|
863
|
-
<RangeControl
|
|
864
|
-
label="Fine rotation"
|
|
865
|
-
min={ -MAX_ROTATION_OFFSET }
|
|
866
|
-
max={ MAX_ROTATION_OFFSET }
|
|
867
|
-
step={ 0.5 }
|
|
868
|
-
value={ fineOffset }
|
|
869
|
-
onChange={ handleRotationSlider }
|
|
870
|
-
/>
|
|
871
|
-
<RangeControl
|
|
872
|
-
label="Zoom"
|
|
873
|
-
min={ getMinZoom( state ) }
|
|
874
|
-
max={ MAX_ZOOM }
|
|
875
|
-
step={ 0.1 }
|
|
876
|
-
value={ state.zoom }
|
|
877
|
-
onChange={ handleZoomChange }
|
|
878
|
-
/>
|
|
879
|
-
</div>
|
|
880
|
-
</div>
|
|
881
|
-
|
|
882
|
-
<div
|
|
883
|
-
style={ { display: 'flex', gap: 16, alignItems: 'flex-start' } }
|
|
884
|
-
>
|
|
885
|
-
<div
|
|
886
|
-
ref={ containerRef }
|
|
887
|
-
style={ { flex: '1 1 60%', minWidth: 0 } }
|
|
888
|
-
>
|
|
889
|
-
<div className={ styles.container }>
|
|
890
|
-
<Cropper
|
|
891
|
-
src={ src }
|
|
892
|
-
controller={ controller }
|
|
893
|
-
showGrid
|
|
894
|
-
showDimming
|
|
895
|
-
freeformCrop={ freeformCrop }
|
|
896
|
-
/>
|
|
897
|
-
</div>
|
|
898
|
-
{ ! isCustom && IMAGE_CREDIT }
|
|
899
|
-
</div>
|
|
900
|
-
|
|
901
|
-
<div
|
|
902
|
-
style={ {
|
|
903
|
-
flex: '0 0 320px',
|
|
904
|
-
fontSize: 11,
|
|
905
|
-
fontFamily: 'monospace',
|
|
906
|
-
lineHeight: 1.5,
|
|
907
|
-
overflow: 'auto',
|
|
908
|
-
maxHeight: 500,
|
|
909
|
-
} }
|
|
910
|
-
>
|
|
911
|
-
<div className={ styles[ 'export-preview' ] }>
|
|
912
|
-
{ previewSrc ? (
|
|
913
|
-
<img
|
|
914
|
-
className={ styles[ 'export-image' ] }
|
|
915
|
-
src={ previewSrc }
|
|
916
|
-
alt="Crop preview"
|
|
917
|
-
/>
|
|
918
|
-
) : (
|
|
919
|
-
<p>Loading...</p>
|
|
920
|
-
) }
|
|
921
|
-
</div>
|
|
922
|
-
<strong>Containment</strong>
|
|
923
|
-
<div
|
|
924
|
-
style={ {
|
|
925
|
-
padding: '2px 6px',
|
|
926
|
-
background: isContained ? '#d4edda' : '#f8d7da',
|
|
927
|
-
borderRadius: 3,
|
|
928
|
-
marginBottom: 8,
|
|
929
|
-
display: 'inline-block',
|
|
930
|
-
} }
|
|
931
|
-
>
|
|
932
|
-
{ isContained ? 'COVERED' : 'NOT COVERED' }
|
|
933
|
-
</div>
|
|
934
|
-
|
|
935
|
-
{ state.image && (
|
|
936
|
-
<div>
|
|
937
|
-
<strong>Image info</strong>
|
|
938
|
-
<pre style={ { margin: '4px 0' } }>
|
|
939
|
-
{ `original: ${ state.image.naturalWidth }×${
|
|
940
|
-
state.image.naturalHeight
|
|
941
|
-
}
|
|
942
|
-
aspect ratio: ${ (
|
|
943
|
-
state.image.naturalWidth /
|
|
944
|
-
state.image.naturalHeight
|
|
945
|
-
).toFixed( 2 ) }` }
|
|
946
|
-
</pre>
|
|
947
|
-
</div>
|
|
948
|
-
) }
|
|
949
|
-
|
|
950
|
-
<div>
|
|
951
|
-
<strong>State</strong>
|
|
952
|
-
<pre style={ { margin: '4px 0' } }>
|
|
953
|
-
{ `zoom: ${ state.zoom.toFixed( 3 ) }
|
|
954
|
-
rotation: ${ state.rotation.toFixed( 1 ) }°
|
|
955
|
-
pan: (${ state.pan.x.toFixed( 4 ) }, ${ state.pan.y.toFixed( 4 ) })
|
|
956
|
-
cropRect: (${ state.cropRect.x.toFixed( 3 ) }, ${ state.cropRect.y.toFixed(
|
|
957
|
-
3
|
|
958
|
-
) }) ${ state.cropRect.width.toFixed(
|
|
959
|
-
3
|
|
960
|
-
) }×${ state.cropRect.height.toFixed( 3 ) }
|
|
961
|
-
flip: h=${ state.flip.horizontal } v=${ state.flip.vertical }` }
|
|
962
|
-
</pre>
|
|
963
|
-
</div>
|
|
964
|
-
|
|
965
|
-
<div>
|
|
966
|
-
<strong>Sizes</strong>
|
|
967
|
-
<pre style={ { margin: '4px 0' } }>
|
|
968
|
-
{ `container: ${ containerSize.width }×${
|
|
969
|
-
containerSize.height
|
|
970
|
-
}
|
|
971
|
-
element: ${ elementSize.width.toFixed( 0 ) }×${ elementSize.height.toFixed(
|
|
972
|
-
0
|
|
973
|
-
) }
|
|
974
|
-
visual: ${ visualSize.width.toFixed( 0 ) }×${ visualSize.height.toFixed(
|
|
975
|
-
0
|
|
976
|
-
) }` }
|
|
977
|
-
</pre>
|
|
978
|
-
</div>
|
|
979
|
-
|
|
980
|
-
{ camera && (
|
|
981
|
-
<div>
|
|
982
|
-
<strong>Camera matrix</strong>
|
|
983
|
-
<pre style={ { margin: '4px 0' } }>
|
|
984
|
-
{ `[${ Array.from( camera )
|
|
985
|
-
.map( ( v ) => v.toFixed( 2 ) )
|
|
986
|
-
.join( ', ' ) }]` }
|
|
987
|
-
</pre>
|
|
988
|
-
</div>
|
|
989
|
-
) }
|
|
990
|
-
|
|
991
|
-
{ cropWorldCorners && (
|
|
992
|
-
<div>
|
|
993
|
-
<strong>Crop corners (world space)</strong>
|
|
994
|
-
<pre style={ { margin: '4px 0' } }>
|
|
995
|
-
{ `TL: (${ cropWorldCorners[ 0 ].x.toFixed(
|
|
996
|
-
4
|
|
997
|
-
) }, ${ cropWorldCorners[ 0 ].y.toFixed( 4 ) })
|
|
998
|
-
BR: (${ cropWorldCorners[ 1 ].x.toFixed(
|
|
999
|
-
4
|
|
1000
|
-
) }, ${ cropWorldCorners[ 1 ].y.toFixed(
|
|
1001
|
-
4
|
|
1002
|
-
) })` }
|
|
1003
|
-
</pre>
|
|
1004
|
-
<span style={ { fontSize: 10, color: '#666' } }>
|
|
1005
|
-
Should be within [0,1] when contained
|
|
1006
|
-
</span>
|
|
1007
|
-
</div>
|
|
1008
|
-
) }
|
|
1009
|
-
|
|
1010
|
-
{ restrictionResult && (
|
|
1011
|
-
<div>
|
|
1012
|
-
<strong>Restriction result</strong>
|
|
1013
|
-
<pre style={ { margin: '4px 0' } }>
|
|
1014
|
-
{ `zoom: ${ restrictionResult.zoom.toFixed(
|
|
1015
|
-
3
|
|
1016
|
-
) }
|
|
1017
|
-
pan: (${ restrictionResult.pan.x.toFixed(
|
|
1018
|
-
4
|
|
1019
|
-
) }, ${ restrictionResult.pan.y.toFixed(
|
|
1020
|
-
4
|
|
1021
|
-
) })` }
|
|
1022
|
-
</pre>
|
|
1023
|
-
</div>
|
|
1024
|
-
) }
|
|
1025
|
-
|
|
1026
|
-
{ sourceRegion && (
|
|
1027
|
-
<div>
|
|
1028
|
-
<strong>Source region (px)</strong>
|
|
1029
|
-
<pre style={ { margin: '4px 0' } }>
|
|
1030
|
-
{ `x: ${ sourceRegion.x.toFixed(
|
|
1031
|
-
0
|
|
1032
|
-
) } y: ${ sourceRegion.y.toFixed( 0 ) }
|
|
1033
|
-
w: ${ sourceRegion.width.toFixed( 0 ) } h: ${ sourceRegion.height.toFixed(
|
|
1034
|
-
0
|
|
1035
|
-
) }` }
|
|
1036
|
-
</pre>
|
|
1037
|
-
</div>
|
|
1038
|
-
) }
|
|
1039
|
-
|
|
1040
|
-
{ sourceRegion &&
|
|
1041
|
-
sourceRegion.width > 0 &&
|
|
1042
|
-
sourceRegion.height > 0 && (
|
|
1043
|
-
<div>
|
|
1044
|
-
<strong>Crop output</strong>
|
|
1045
|
-
<pre style={ { margin: '4px 0' } }>
|
|
1046
|
-
{ `dimensions: ${ Math.round(
|
|
1047
|
-
sourceRegion.width
|
|
1048
|
-
) }×${ Math.round( sourceRegion.height ) }
|
|
1049
|
-
aspect ratio: ${ ( sourceRegion.width / sourceRegion.height ).toFixed( 2 ) }
|
|
1050
|
-
% of original: ${ (
|
|
1051
|
-
( ( sourceRegion.width *
|
|
1052
|
-
sourceRegion.height ) /
|
|
1053
|
-
( imageSize.width *
|
|
1054
|
-
imageSize.height ) ) *
|
|
1055
|
-
100
|
|
1056
|
-
).toFixed( 1 ) }%` }
|
|
1057
|
-
</pre>
|
|
1058
|
-
</div>
|
|
1059
|
-
) }
|
|
1060
|
-
</div>
|
|
1061
|
-
</div>
|
|
1062
|
-
</div>
|
|
1063
|
-
);
|
|
1064
|
-
};
|
|
1065
|
-
|
|
1066
|
-
export const Debug: Story = {
|
|
1067
|
-
parameters: {
|
|
1068
|
-
// FIXME: Image credit text and its link fail color-contrast.
|
|
1069
|
-
// See: https://github.com/WordPress/gutenberg/issues/81596
|
|
1070
|
-
a11y: { test: 'todo' },
|
|
1071
|
-
},
|
|
1072
|
-
render: DebugComponent,
|
|
1073
|
-
};
|