@wordpress/media-editor 0.7.1-next.v.202604201441.0 → 0.8.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-canvas/index.cjs +54 -0
- package/build/components/media-editor-canvas/index.cjs.map +7 -0
- package/build/components/media-editor-crop-panel/index.cjs +116 -0
- package/build/components/media-editor-crop-panel/index.cjs.map +7 -0
- package/build/components/media-editor-modal/build-modifiers.cjs +91 -0
- package/build/components/media-editor-modal/build-modifiers.cjs.map +7 -0
- package/build/components/media-editor-modal/index.cjs +497 -0
- package/build/components/media-editor-modal/index.cjs.map +7 -0
- package/build/components/media-editor-toolbar/index.cjs +146 -0
- package/build/components/media-editor-toolbar/index.cjs.map +7 -0
- package/build/image-editor/core/camera.cjs +194 -0
- package/build/image-editor/core/camera.cjs.map +7 -0
- package/build/image-editor/core/constants.cjs +81 -0
- package/build/image-editor/core/constants.cjs.map +7 -0
- package/build/image-editor/core/containment.cjs +245 -0
- package/build/image-editor/core/containment.cjs.map +7 -0
- package/build/image-editor/core/export/canvas-renderer.cjs +151 -0
- package/build/image-editor/core/export/canvas-renderer.cjs.map +7 -0
- package/build/image-editor/core/index.cjs +50 -0
- package/build/image-editor/core/index.cjs.map +7 -0
- package/build/image-editor/core/interaction-controller.cjs +608 -0
- package/build/image-editor/core/interaction-controller.cjs.map +7 -0
- package/build/image-editor/core/math/rotation.cjs +41 -0
- package/build/image-editor/core/math/rotation.cjs.map +7 -0
- package/build/image-editor/core/source-region.cjs +95 -0
- package/build/image-editor/core/source-region.cjs.map +7 -0
- package/build/image-editor/core/state.cjs +269 -0
- package/build/image-editor/core/state.cjs.map +7 -0
- package/build/image-editor/core/stencil-math.cjs +197 -0
- package/build/image-editor/core/stencil-math.cjs.map +7 -0
- package/build/image-editor/core/transform-style.cjs +46 -0
- package/build/image-editor/core/transform-style.cjs.map +7 -0
- package/build/image-editor/core/transforms/pipeline.cjs +43 -0
- package/build/image-editor/core/transforms/pipeline.cjs.map +7 -0
- package/build/image-editor/core/types.cjs +19 -0
- package/build/image-editor/core/types.cjs.map +7 -0
- package/build/image-editor/index.cjs +57 -0
- package/build/image-editor/index.cjs.map +7 -0
- package/build/image-editor/react/components/cropper-provider.cjs +50 -0
- package/build/image-editor/react/components/cropper-provider.cjs.map +7 -0
- package/build/image-editor/react/components/cropper.cjs +365 -0
- package/build/image-editor/react/components/cropper.cjs.map +7 -0
- package/build/image-editor/react/components/index.cjs +36 -0
- package/build/image-editor/react/components/index.cjs.map +7 -0
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +58 -0
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +7 -0
- package/build/image-editor/react/components/overlays/grid-overlay.cjs +91 -0
- package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +7 -0
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +351 -0
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +7 -0
- package/build/image-editor/react/hooks/index.cjs +31 -0
- package/build/image-editor/react/hooks/index.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +68 -0
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-cropper-state.cjs +158 -0
- package/build/image-editor/react/hooks/use-cropper-state.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-interaction.cjs +161 -0
- package/build/image-editor/react/hooks/use-interaction.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-transform-style.cjs +38 -0
- package/build/image-editor/react/hooks/use-transform-style.cjs.map +7 -0
- package/build/index.cjs +5 -2
- package/build/index.cjs.map +2 -2
- package/build/lock-unlock.cjs +37 -0
- package/build/lock-unlock.cjs.map +7 -0
- package/build/private-apis.cjs +38 -0
- package/build/private-apis.cjs.map +7 -0
- package/build/store/actions.cjs +45 -0
- package/build/store/actions.cjs.map +7 -0
- package/build/store/constants.cjs +31 -0
- package/build/store/constants.cjs.map +7 -0
- package/build/store/index.cjs +51 -0
- package/build/store/index.cjs.map +7 -0
- package/build/store/reducer.cjs +51 -0
- package/build/store/reducer.cjs.map +7 -0
- package/build/store/selectors.cjs +43 -0
- package/build/store/selectors.cjs.map +7 -0
- package/build-module/components/media-editor-canvas/index.mjs +33 -0
- package/build-module/components/media-editor-canvas/index.mjs.map +7 -0
- package/build-module/components/media-editor-crop-panel/index.mjs +100 -0
- package/build-module/components/media-editor-crop-panel/index.mjs.map +7 -0
- package/build-module/components/media-editor-modal/build-modifiers.mjs +66 -0
- package/build-module/components/media-editor-modal/build-modifiers.mjs.map +7 -0
- package/build-module/components/media-editor-modal/index.mjs +483 -0
- package/build-module/components/media-editor-modal/index.mjs.map +7 -0
- package/build-module/components/media-editor-toolbar/index.mjs +130 -0
- package/build-module/components/media-editor-toolbar/index.mjs.map +7 -0
- package/build-module/image-editor/core/camera.mjs +163 -0
- package/build-module/image-editor/core/camera.mjs.map +7 -0
- package/build-module/image-editor/core/constants.mjs +51 -0
- package/build-module/image-editor/core/constants.mjs.map +7 -0
- package/build-module/image-editor/core/containment.mjs +218 -0
- package/build-module/image-editor/core/containment.mjs.map +7 -0
- package/build-module/image-editor/core/export/canvas-renderer.mjs +120 -0
- package/build-module/image-editor/core/export/canvas-renderer.mjs.map +7 -0
- package/build-module/image-editor/core/index.mjs +24 -0
- package/build-module/image-editor/core/index.mjs.map +7 -0
- package/build-module/image-editor/core/interaction-controller.mjs +583 -0
- package/build-module/image-editor/core/interaction-controller.mjs.map +7 -0
- package/build-module/image-editor/core/math/rotation.mjs +15 -0
- package/build-module/image-editor/core/math/rotation.mjs.map +7 -0
- package/build-module/image-editor/core/source-region.mjs +69 -0
- package/build-module/image-editor/core/source-region.mjs.map +7 -0
- package/build-module/image-editor/core/state.mjs +242 -0
- package/build-module/image-editor/core/state.mjs.map +7 -0
- package/build-module/image-editor/core/stencil-math.mjs +169 -0
- package/build-module/image-editor/core/stencil-math.mjs.map +7 -0
- package/build-module/image-editor/core/transform-style.mjs +21 -0
- package/build-module/image-editor/core/transform-style.mjs.map +7 -0
- package/build-module/image-editor/core/transforms/pipeline.mjs +17 -0
- package/build-module/image-editor/core/transforms/pipeline.mjs.map +7 -0
- package/build-module/image-editor/core/types.mjs +1 -0
- package/build-module/image-editor/core/types.mjs.map +7 -0
- package/build-module/image-editor/index.mjs +30 -0
- package/build-module/image-editor/index.mjs.map +7 -0
- package/build-module/image-editor/react/components/cropper-provider.mjs +26 -0
- package/build-module/image-editor/react/components/cropper-provider.mjs.map +7 -0
- package/build-module/image-editor/react/components/cropper.mjs +337 -0
- package/build-module/image-editor/react/components/cropper.mjs.map +7 -0
- package/build-module/image-editor/react/components/index.mjs +9 -0
- package/build-module/image-editor/react/components/index.mjs.map +7 -0
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +33 -0
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +7 -0
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +66 -0
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +7 -0
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +330 -0
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/index.mjs +6 -0
- package/build-module/image-editor/react/hooks/index.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +43 -0
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs +137 -0
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs +138 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-transform-style.mjs +13 -0
- package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +7 -0
- package/build-module/index.mjs +3 -1
- package/build-module/index.mjs.map +2 -2
- package/build-module/lock-unlock.mjs +11 -0
- package/build-module/lock-unlock.mjs.map +7 -0
- package/build-module/private-apis.mjs +13 -0
- package/build-module/private-apis.mjs.map +7 -0
- package/build-module/store/actions.mjs +19 -0
- package/build-module/store/actions.mjs.map +7 -0
- package/build-module/store/constants.mjs +6 -0
- package/build-module/store/constants.mjs.map +7 -0
- package/build-module/store/index.mjs +16 -0
- package/build-module/store/index.mjs.map +7 -0
- package/build-module/store/reducer.mjs +26 -0
- package/build-module/store/reducer.mjs.map +7 -0
- package/build-module/store/selectors.mjs +16 -0
- package/build-module/store/selectors.mjs.map +7 -0
- package/build-style/style-rtl.css +315 -0
- package/build-style/style.css +315 -0
- package/build-types/components/media-editor-canvas/index.d.ts +22 -0
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -0
- package/build-types/components/media-editor-crop-panel/index.d.ts +45 -0
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/build-modifiers.d.ts +83 -0
- package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/index.d.ts +20 -0
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -0
- package/build-types/components/media-editor-toolbar/index.d.ts +21 -0
- package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -0
- package/build-types/image-editor/core/camera.d.ts +107 -0
- package/build-types/image-editor/core/camera.d.ts.map +1 -0
- package/build-types/image-editor/core/constants.d.ts +46 -0
- package/build-types/image-editor/core/constants.d.ts.map +1 -0
- package/build-types/image-editor/core/containment.d.ts +61 -0
- package/build-types/image-editor/core/containment.d.ts.map +1 -0
- package/build-types/image-editor/core/export/canvas-renderer.d.ts +106 -0
- package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +1 -0
- package/build-types/image-editor/core/index.d.ts +8 -0
- package/build-types/image-editor/core/index.d.ts.map +1 -0
- package/build-types/image-editor/core/interaction-controller.d.ts +178 -0
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -0
- package/build-types/image-editor/core/math/rotation.d.ts +18 -0
- package/build-types/image-editor/core/math/rotation.d.ts.map +1 -0
- package/build-types/image-editor/core/source-region.d.ts +75 -0
- package/build-types/image-editor/core/source-region.d.ts.map +1 -0
- package/build-types/image-editor/core/state.d.ts +42 -0
- package/build-types/image-editor/core/state.d.ts.map +1 -0
- package/build-types/image-editor/core/stencil-math.d.ts +79 -0
- package/build-types/image-editor/core/stencil-math.d.ts.map +1 -0
- package/build-types/image-editor/core/transform-style.d.ts +23 -0
- package/build-types/image-editor/core/transform-style.d.ts.map +1 -0
- package/build-types/image-editor/core/transforms/pipeline.d.ts +27 -0
- package/build-types/image-editor/core/transforms/pipeline.d.ts.map +1 -0
- package/build-types/image-editor/core/types.d.ts +211 -0
- package/build-types/image-editor/core/types.d.ts.map +1 -0
- package/build-types/image-editor/index.d.ts +16 -0
- package/build-types/image-editor/index.d.ts.map +1 -0
- package/build-types/image-editor/react/components/cropper-provider.d.ts +37 -0
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -0
- package/build-types/image-editor/react/components/cropper.d.ts +59 -0
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -0
- package/build-types/image-editor/react/components/index.d.ts +4 -0
- package/build-types/image-editor/react/components/index.d.ts.map +1 -0
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +30 -0
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -0
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +30 -0
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -0
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +32 -0
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/index.d.ts +3 -0
- package/build-types/image-editor/react/hooks/index.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +16 -0
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +68 -0
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts +60 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-transform-style.d.ts +16 -0
- package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +1 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts +16 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -0
- package/build-types/index.d.ts +1 -0
- package/build-types/index.d.ts.map +1 -1
- package/build-types/lock-unlock.d.ts +2 -0
- package/build-types/lock-unlock.d.ts.map +1 -0
- package/build-types/private-apis.d.ts +2 -0
- package/build-types/private-apis.d.ts.map +1 -0
- package/build-types/store/actions.d.ts +27 -0
- package/build-types/store/actions.d.ts.map +1 -0
- package/build-types/store/constants.d.ts +2 -0
- package/build-types/store/constants.d.ts.map +1 -0
- package/build-types/store/index.d.ts +4 -0
- package/build-types/store/index.d.ts.map +1 -0
- package/build-types/store/reducer.d.ts +23 -0
- package/build-types/store/reducer.d.ts.map +1 -0
- package/build-types/store/selectors.d.ts +8 -0
- package/build-types/store/selectors.d.ts.map +1 -0
- package/package.json +21 -7
- package/src/components/media-editor-canvas/index.tsx +56 -0
- package/src/components/media-editor-canvas/style.scss +9 -0
- package/src/components/media-editor-crop-panel/index.tsx +143 -0
- package/src/components/media-editor-modal/build-modifiers.ts +164 -0
- package/src/components/media-editor-modal/index.tsx +639 -0
- package/src/components/media-editor-modal/style.scss +139 -0
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +343 -0
- package/src/components/media-editor-toolbar/index.tsx +150 -0
- package/src/components/media-editor-toolbar/style.scss +21 -0
- package/src/image-editor/README.md +162 -0
- package/src/image-editor/core/camera.ts +352 -0
- package/src/image-editor/core/constants.ts +93 -0
- package/src/image-editor/core/containment.ts +435 -0
- package/src/image-editor/core/export/canvas-renderer.ts +253 -0
- package/src/image-editor/core/export/test/canvas-renderer.ts +813 -0
- package/src/image-editor/core/index.ts +35 -0
- package/src/image-editor/core/interaction-controller.ts +889 -0
- package/src/image-editor/core/math/rotation.ts +26 -0
- package/src/image-editor/core/source-region.ts +166 -0
- package/src/image-editor/core/state.ts +428 -0
- package/src/image-editor/core/stencil-math.ts +330 -0
- package/src/image-editor/core/test/camera.ts +593 -0
- package/src/image-editor/core/test/interaction-controller.ts +996 -0
- package/src/image-editor/core/test/preview-export-parity.ts +242 -0
- package/src/image-editor/core/test/render-camera-bridge.ts +348 -0
- package/src/image-editor/core/test/state.ts +762 -0
- package/src/image-editor/core/test/stencil-math.ts +321 -0
- package/src/image-editor/core/transform-style.ts +44 -0
- package/src/image-editor/core/transforms/pipeline.ts +43 -0
- package/src/image-editor/core/transforms/test/pipeline.ts +269 -0
- package/src/image-editor/core/types.ts +177 -0
- package/src/image-editor/docs/architecture.md +96 -0
- package/src/image-editor/docs/recipes.md +720 -0
- package/src/image-editor/docs/roadmap.md +66 -0
- package/src/image-editor/index.ts +43 -0
- package/src/image-editor/react/components/cropper-provider.tsx +73 -0
- package/src/image-editor/react/components/cropper.scss +195 -0
- package/src/image-editor/react/components/cropper.tsx +534 -0
- package/src/image-editor/react/components/index.ts +3 -0
- package/src/image-editor/react/components/overlays/dimming-overlay.tsx +57 -0
- package/src/image-editor/react/components/overlays/grid-overlay.tsx +80 -0
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +513 -0
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +265 -0
- package/src/image-editor/react/components/test/cropper.tsx +140 -0
- package/src/image-editor/react/hooks/index.ts +2 -0
- package/src/image-editor/react/hooks/test/use-cropper-state.ts +913 -0
- package/src/image-editor/react/hooks/test/use-interaction.ts +218 -0
- package/src/image-editor/react/hooks/test/use-transform-style.ts +146 -0
- package/src/image-editor/react/hooks/use-aria-announcer.ts +64 -0
- package/src/image-editor/react/hooks/use-cropper-state.ts +238 -0
- package/src/image-editor/react/hooks/use-interaction.ts +223 -0
- package/src/image-editor/react/hooks/use-transform-style.ts +30 -0
- package/src/image-editor/stories/rectangle-crop.story.tsx +1085 -0
- package/src/image-editor/stories/style.css +94 -0
- package/src/image-editor/style.scss +1 -0
- package/src/index.ts +3 -0
- package/src/lock-unlock.ts +10 -0
- package/src/private-apis.ts +12 -0
- package/src/store/actions.ts +33 -0
- package/src/store/constants.ts +1 -0
- package/src/store/index.ts +20 -0
- package/src/store/reducer.ts +48 -0
- package/src/store/selectors.ts +16 -0
- package/src/store/test/reducer.ts +57 -0
- package/src/style.scss +4 -0
- package/tsconfig.json +10 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import clsx from 'clsx';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* WordPress dependencies
|
|
9
|
+
*/
|
|
10
|
+
import {
|
|
11
|
+
useState,
|
|
12
|
+
useCallback,
|
|
13
|
+
useMemo,
|
|
14
|
+
useRef,
|
|
15
|
+
useEffect,
|
|
16
|
+
forwardRef,
|
|
17
|
+
} from '@wordpress/element';
|
|
18
|
+
import { __ } from '@wordpress/i18n';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Internal dependencies
|
|
22
|
+
*/
|
|
23
|
+
import type {
|
|
24
|
+
CropperState,
|
|
25
|
+
StencilProps,
|
|
26
|
+
Size,
|
|
27
|
+
NormalizedRect,
|
|
28
|
+
} from '../../core/types';
|
|
29
|
+
import type { UseCropperStateReturn } from '../hooks/use-cropper-state';
|
|
30
|
+
import { getImageFit } from '../../core/camera';
|
|
31
|
+
import { getCropBounds } from '../../core/containment';
|
|
32
|
+
import { useInteraction } from '../hooks/use-interaction';
|
|
33
|
+
import { useTransformStyle } from '../hooks/use-transform-style';
|
|
34
|
+
import { useAriaAnnouncer } from '../hooks/use-aria-announcer';
|
|
35
|
+
import { RectangleStencil } from './stencils/rectangle-stencil';
|
|
36
|
+
import { DimmingOverlay } from './overlays/dimming-overlay';
|
|
37
|
+
import { GridOverlay } from './overlays/grid-overlay';
|
|
38
|
+
import './cropper.scss';
|
|
39
|
+
|
|
40
|
+
/** Threshold for comparing normalized crop rect values. */
|
|
41
|
+
const CROP_RECT_EPSILON = 1e-6;
|
|
42
|
+
|
|
43
|
+
// Largest rect of the given pixel aspect ratio that fits inside the visual
|
|
44
|
+
// bounds, centered in [0,1] × [0,1] normalized space. Returns a full-frame
|
|
45
|
+
// rect (1×1) if `aspectRatio` is unset or non-positive.
|
|
46
|
+
function computeInscribedRect(
|
|
47
|
+
aspectRatio: number | undefined,
|
|
48
|
+
visualSize: Size
|
|
49
|
+
): NormalizedRect {
|
|
50
|
+
let w = 1;
|
|
51
|
+
let h = 1;
|
|
52
|
+
if ( aspectRatio && aspectRatio > 0 && visualSize.width > 0 ) {
|
|
53
|
+
// normalizedRatio = w/h in normalized space that produces the
|
|
54
|
+
// desired pixel aspect ratio.
|
|
55
|
+
// pixelW = w * visualW, pixelH = h * visualH
|
|
56
|
+
// pixelW / pixelH = aspectRatio
|
|
57
|
+
// => w / h = aspectRatio * visualH / visualW
|
|
58
|
+
const normalizedRatio =
|
|
59
|
+
( aspectRatio * visualSize.height ) / visualSize.width;
|
|
60
|
+
if ( normalizedRatio <= 1 ) {
|
|
61
|
+
w = normalizedRatio;
|
|
62
|
+
} else {
|
|
63
|
+
h = 1 / normalizedRatio;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
x: ( 1 - w ) / 2,
|
|
68
|
+
y: ( 1 - h ) / 2,
|
|
69
|
+
width: w,
|
|
70
|
+
height: h,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Props for the Cropper component.
|
|
76
|
+
*/
|
|
77
|
+
export interface CropperProps {
|
|
78
|
+
/** Image source URL. */
|
|
79
|
+
src: string;
|
|
80
|
+
/** The full state/setter object from `useCropperState`. */
|
|
81
|
+
controller: UseCropperStateReturn;
|
|
82
|
+
/** Stencil component for the crop area. Defaults to RectangleStencil. */
|
|
83
|
+
stencil?: React.ComponentType< StencilProps >;
|
|
84
|
+
/** Show the rule-of-thirds grid overlay, or only during interactions. */
|
|
85
|
+
showGrid?: boolean | 'interactive';
|
|
86
|
+
/** Whether external placement activity should keep the grid visible. */
|
|
87
|
+
isPlacementActive?: boolean;
|
|
88
|
+
/** Show the dimming overlay outside the crop area. */
|
|
89
|
+
showDimming?: boolean;
|
|
90
|
+
/** Minimum zoom level. */
|
|
91
|
+
minZoom?: number;
|
|
92
|
+
/** Maximum zoom level. */
|
|
93
|
+
maxZoom?: number;
|
|
94
|
+
/** Fixed aspect ratio (width / height) in pixel space for the crop area. */
|
|
95
|
+
aspectRatio?: number;
|
|
96
|
+
/**
|
|
97
|
+
* Enable freeform crop mode with resizable handles.
|
|
98
|
+
* When false (default), the crop area is fixed and centered.
|
|
99
|
+
* When true, the crop area has resize handles and can be freely repositioned.
|
|
100
|
+
*/
|
|
101
|
+
freeformCrop?: boolean;
|
|
102
|
+
/** Callback fired when the image is loaded. */
|
|
103
|
+
onImageLoaded?: ( size: Size ) => void;
|
|
104
|
+
/**
|
|
105
|
+
* Callback fired on every state change. Fires at pointermove rate
|
|
106
|
+
* during drags, so keep the handler light — no heavy work, no
|
|
107
|
+
* expensive parent re-renders. For commit-style events (drag end,
|
|
108
|
+
* settled crop), use `onGestureEnd` instead.
|
|
109
|
+
*
|
|
110
|
+
* Useful for lightweight syncing with external tools, analytics,
|
|
111
|
+
* or AI agents. Receives the full state so consumers can derive
|
|
112
|
+
* whatever they need.
|
|
113
|
+
*/
|
|
114
|
+
onStateChange?: ( state: CropperState ) => void;
|
|
115
|
+
/** Fires when a continuous gesture begins (pan drag, handle resize, pinch zoom). */
|
|
116
|
+
onGestureStart?: () => void;
|
|
117
|
+
/** Fires when a continuous gesture ends (pointerup, resize settle). */
|
|
118
|
+
onGestureEnd?: () => void;
|
|
119
|
+
/** Additional className for the container. */
|
|
120
|
+
className?: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The main image cropper component.
|
|
125
|
+
*
|
|
126
|
+
* Renders an image within a container with interactive crop overlays.
|
|
127
|
+
* Creates the camera once per render via getImageFit, then passes
|
|
128
|
+
* derived values to stencil, overlays, and interaction hooks.
|
|
129
|
+
*
|
|
130
|
+
* The component fills its parent container (100% width and height).
|
|
131
|
+
* Wrap it in a sized container to control its dimensions.
|
|
132
|
+
*
|
|
133
|
+
* @param root0 Component props implementing CropperProps.
|
|
134
|
+
* @param root0.src Image source URL.
|
|
135
|
+
* @param root0.controller The full state/setter object from `useCropperState`.
|
|
136
|
+
* @param root0.stencil Custom stencil component.
|
|
137
|
+
* @param root0.showGrid Grid overlay mode: false | true | 'interactive'.
|
|
138
|
+
* @param root0.isPlacementActive Keep grid visible during external placement activity.
|
|
139
|
+
* @param root0.showDimming Show dimming overlay outside crop.
|
|
140
|
+
* @param root0.minZoom Minimum zoom level.
|
|
141
|
+
* @param root0.maxZoom Maximum zoom level.
|
|
142
|
+
* @param root0.aspectRatio Fixed aspect ratio (width/height).
|
|
143
|
+
* @param root0.freeformCrop Enable resize handles.
|
|
144
|
+
* @param root0.onImageLoaded Image load callback.
|
|
145
|
+
* @param root0.onStateChange Every-frame state callback.
|
|
146
|
+
* @param root0.onGestureStart Gesture boundary start.
|
|
147
|
+
* @param root0.onGestureEnd Gesture boundary end.
|
|
148
|
+
* @param root0.className Additional CSS class.
|
|
149
|
+
* @param ref Forwarded ref for the container div.
|
|
150
|
+
*/
|
|
151
|
+
function CropperInner(
|
|
152
|
+
{
|
|
153
|
+
src,
|
|
154
|
+
controller,
|
|
155
|
+
stencil: StencilComponent = RectangleStencil,
|
|
156
|
+
showGrid = false,
|
|
157
|
+
isPlacementActive = false,
|
|
158
|
+
showDimming = true,
|
|
159
|
+
minZoom,
|
|
160
|
+
maxZoom,
|
|
161
|
+
aspectRatio,
|
|
162
|
+
freeformCrop = false,
|
|
163
|
+
onImageLoaded,
|
|
164
|
+
onStateChange,
|
|
165
|
+
onGestureStart,
|
|
166
|
+
onGestureEnd,
|
|
167
|
+
className,
|
|
168
|
+
}: CropperProps,
|
|
169
|
+
ref: React.ForwardedRef< HTMLDivElement >
|
|
170
|
+
) {
|
|
171
|
+
const { state, setImage, setCropRect, settleCrop } = controller;
|
|
172
|
+
// Canvas measurement via ResizeObserver. The canvas is the inner
|
|
173
|
+
// positioning context for image/stencil/handles — inset from the root
|
|
174
|
+
// by the handle gutter, so crop math operates on the reduced box.
|
|
175
|
+
const canvasRef = useRef< HTMLDivElement >( null );
|
|
176
|
+
const [ canvasSize, setCanvasSize ] = useState< Size >( {
|
|
177
|
+
width: 0,
|
|
178
|
+
height: 0,
|
|
179
|
+
} );
|
|
180
|
+
|
|
181
|
+
useEffect( () => {
|
|
182
|
+
const element = canvasRef.current;
|
|
183
|
+
if ( ! element ) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const observer = new ResizeObserver( ( entries ) => {
|
|
187
|
+
for ( const entry of entries ) {
|
|
188
|
+
const { width, height } = entry.contentRect;
|
|
189
|
+
setCanvasSize( ( prev ) => {
|
|
190
|
+
if ( prev.width === width && prev.height === height ) {
|
|
191
|
+
return prev;
|
|
192
|
+
}
|
|
193
|
+
return { width, height };
|
|
194
|
+
} );
|
|
195
|
+
}
|
|
196
|
+
} );
|
|
197
|
+
observer.observe( element );
|
|
198
|
+
return () => {
|
|
199
|
+
observer.disconnect();
|
|
200
|
+
};
|
|
201
|
+
}, [] );
|
|
202
|
+
|
|
203
|
+
// Notify consumer of state changes.
|
|
204
|
+
useEffect( () => {
|
|
205
|
+
onStateChange?.( state );
|
|
206
|
+
}, [ state, onStateChange ] );
|
|
207
|
+
|
|
208
|
+
// ARIA live region: announce significant state changes for screen readers.
|
|
209
|
+
const ariaMessage = useAriaAnnouncer( state );
|
|
210
|
+
|
|
211
|
+
// Compute fitted image dimensions and visual bounds from camera math.
|
|
212
|
+
const naturalWidth = state.image?.naturalWidth ?? 0;
|
|
213
|
+
const naturalHeight = state.image?.naturalHeight ?? 0;
|
|
214
|
+
const { elementSize, visualSize } = useMemo(
|
|
215
|
+
() =>
|
|
216
|
+
getImageFit(
|
|
217
|
+
canvasSize,
|
|
218
|
+
{ width: naturalWidth, height: naturalHeight },
|
|
219
|
+
state.rotation
|
|
220
|
+
),
|
|
221
|
+
[ canvasSize, naturalWidth, naturalHeight, state.rotation ]
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
// In fixed-crop mode, auto-size the crop rect to fill the visual area
|
|
225
|
+
// while respecting the aspect ratio. The crop is always centered.
|
|
226
|
+
useEffect( () => {
|
|
227
|
+
if (
|
|
228
|
+
freeformCrop ||
|
|
229
|
+
visualSize.width === 0 ||
|
|
230
|
+
visualSize.height === 0
|
|
231
|
+
) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
const rect = computeInscribedRect( aspectRatio, visualSize );
|
|
235
|
+
const current = state.cropRect;
|
|
236
|
+
if (
|
|
237
|
+
Math.abs( current.x - rect.x ) < CROP_RECT_EPSILON &&
|
|
238
|
+
Math.abs( current.y - rect.y ) < CROP_RECT_EPSILON &&
|
|
239
|
+
Math.abs( current.width - rect.width ) < CROP_RECT_EPSILON &&
|
|
240
|
+
Math.abs( current.height - rect.height ) < CROP_RECT_EPSILON
|
|
241
|
+
) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
setCropRect( rect );
|
|
245
|
+
}, [ freeformCrop, aspectRatio, visualSize, setCropRect, state.cropRect ] );
|
|
246
|
+
|
|
247
|
+
// In freeform mode, when aspectRatio changes, reshape the crop to the
|
|
248
|
+
// largest inscribed rect of the new ratio.
|
|
249
|
+
const prevAspectRatioRef = useRef( aspectRatio );
|
|
250
|
+
useEffect( () => {
|
|
251
|
+
if ( prevAspectRatioRef.current === aspectRatio ) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
prevAspectRatioRef.current = aspectRatio;
|
|
255
|
+
|
|
256
|
+
if (
|
|
257
|
+
! freeformCrop ||
|
|
258
|
+
visualSize.width === 0 ||
|
|
259
|
+
visualSize.height === 0 ||
|
|
260
|
+
! aspectRatio ||
|
|
261
|
+
aspectRatio <= 0
|
|
262
|
+
) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
setCropRect( computeInscribedRect( aspectRatio, visualSize ) );
|
|
266
|
+
}, [ aspectRatio, freeformCrop, visualSize, setCropRect ] );
|
|
267
|
+
|
|
268
|
+
// Compute the crop handle bounds from the actual image footprint.
|
|
269
|
+
// Depends on the full state object because getCropBounds reads
|
|
270
|
+
// crop, zoom, rotation, flip, and image. React Compiler requires
|
|
271
|
+
// the complete dependency; the computation is lightweight (a few
|
|
272
|
+
// trig ops + 4 corner transforms).
|
|
273
|
+
const cropBounds = useMemo( () => {
|
|
274
|
+
if ( ! state.image || elementSize.width === 0 ) {
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
return getCropBounds( state, elementSize, visualSize, canvasSize );
|
|
278
|
+
}, [ state, elementSize, visualSize, canvasSize ] );
|
|
279
|
+
|
|
280
|
+
// Use the interaction hook for mouse, touch, and keyboard events.
|
|
281
|
+
const {
|
|
282
|
+
handlers,
|
|
283
|
+
onWheelNative,
|
|
284
|
+
isDragging,
|
|
285
|
+
isZooming,
|
|
286
|
+
isPlacementActive: isInteractionPlacementActive,
|
|
287
|
+
} = useInteraction( state, controller, canvasSize, visualSize, {
|
|
288
|
+
minZoom,
|
|
289
|
+
maxZoom,
|
|
290
|
+
onGestureStart,
|
|
291
|
+
onGestureEnd,
|
|
292
|
+
} );
|
|
293
|
+
|
|
294
|
+
// Register wheel handler natively with { passive: false } so
|
|
295
|
+
// preventDefault works. React's onWheel registers as passive. Bound
|
|
296
|
+
// to the canvas (not the root) so pointer geometry inside the handler
|
|
297
|
+
// resolves against the canvas box.
|
|
298
|
+
useEffect( () => {
|
|
299
|
+
const el = canvasRef.current;
|
|
300
|
+
if ( ! el ) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
el.addEventListener( 'wheel', onWheelNative, {
|
|
304
|
+
passive: false,
|
|
305
|
+
} );
|
|
306
|
+
return () => {
|
|
307
|
+
el.removeEventListener( 'wheel', onWheelNative );
|
|
308
|
+
};
|
|
309
|
+
}, [ onWheelNative ] );
|
|
310
|
+
|
|
311
|
+
// Use the transform style hook for the image CSS transform.
|
|
312
|
+
const transformString = useTransformStyle( state, visualSize );
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Handle the image load event.
|
|
316
|
+
*/
|
|
317
|
+
const handleImageLoad = useCallback(
|
|
318
|
+
( event: React.SyntheticEvent< HTMLImageElement > ) => {
|
|
319
|
+
const img = event.currentTarget;
|
|
320
|
+
const size: Size = {
|
|
321
|
+
width: img.naturalWidth,
|
|
322
|
+
height: img.naturalHeight,
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
setImage( {
|
|
326
|
+
src,
|
|
327
|
+
naturalWidth: size.width,
|
|
328
|
+
naturalHeight: size.height,
|
|
329
|
+
} );
|
|
330
|
+
|
|
331
|
+
onImageLoaded?.( size );
|
|
332
|
+
},
|
|
333
|
+
[ src, setImage, onImageLoaded ]
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Handle crop rect changes from the stencil (during drag).
|
|
338
|
+
*/
|
|
339
|
+
const handleCropChange = useCallback(
|
|
340
|
+
( rect: NormalizedRect ) => {
|
|
341
|
+
setCropRect( rect );
|
|
342
|
+
},
|
|
343
|
+
[ setCropRect ]
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
// Settling animation: brief linear transition after resize end.
|
|
347
|
+
const [ settling, setSettling ] = useState( false );
|
|
348
|
+
const settleTimerRef = useRef< ReturnType< typeof setTimeout > >();
|
|
349
|
+
|
|
350
|
+
// Clear the pending settle timer on unmount so it can't fire a
|
|
351
|
+
// state update on an unmounted component.
|
|
352
|
+
useEffect( () => {
|
|
353
|
+
return () => {
|
|
354
|
+
clearTimeout( settleTimerRef.current );
|
|
355
|
+
};
|
|
356
|
+
}, [] );
|
|
357
|
+
|
|
358
|
+
const [ isResizing, setIsResizing ] = useState( false );
|
|
359
|
+
const isInteractiveGrid = showGrid === 'interactive';
|
|
360
|
+
const showInteractiveGrid =
|
|
361
|
+
isInteractiveGrid &&
|
|
362
|
+
( isInteractionPlacementActive || isResizing || isPlacementActive );
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Handle Escape on a resize handle — return focus to the canvas so
|
|
366
|
+
* arrow keys pan the image rather than resize.
|
|
367
|
+
*/
|
|
368
|
+
const handleEscape = useCallback( () => {
|
|
369
|
+
canvasRef.current?.focus( { preventScroll: true } );
|
|
370
|
+
}, [] );
|
|
371
|
+
|
|
372
|
+
const handleResizeStart = useCallback( () => {
|
|
373
|
+
setIsResizing( true );
|
|
374
|
+
onGestureStart?.();
|
|
375
|
+
}, [ onGestureStart ] );
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Handle resize end — settle the crop rect (re-center, fill height).
|
|
379
|
+
*/
|
|
380
|
+
const handleResizeEnd = useCallback( () => {
|
|
381
|
+
setIsResizing( false );
|
|
382
|
+
setSettling( true );
|
|
383
|
+
settleCrop();
|
|
384
|
+
onGestureEnd?.();
|
|
385
|
+
clearTimeout( settleTimerRef.current );
|
|
386
|
+
settleTimerRef.current = setTimeout( () => {
|
|
387
|
+
setSettling( false );
|
|
388
|
+
}, 200 );
|
|
389
|
+
}, [ settleCrop, onGestureEnd ] );
|
|
390
|
+
|
|
391
|
+
const imageTransition =
|
|
392
|
+
settling || isZooming ? 'transform 150ms linear' : undefined;
|
|
393
|
+
const settleStencilTransition = settling
|
|
394
|
+
? 'left 150ms linear, top 150ms linear, width 150ms linear, height 150ms linear'
|
|
395
|
+
: undefined;
|
|
396
|
+
|
|
397
|
+
// Compute the image's CSS style.
|
|
398
|
+
const imageStyle = useMemo( (): React.CSSProperties => {
|
|
399
|
+
if ( elementSize.width === 0 || elementSize.height === 0 ) {
|
|
400
|
+
return {};
|
|
401
|
+
}
|
|
402
|
+
const centerX = ( canvasSize.width - elementSize.width ) / 2;
|
|
403
|
+
const centerY = ( canvasSize.height - elementSize.height ) / 2;
|
|
404
|
+
return {
|
|
405
|
+
width: elementSize.width,
|
|
406
|
+
height: elementSize.height,
|
|
407
|
+
maxWidth: elementSize.width,
|
|
408
|
+
maxHeight: elementSize.height,
|
|
409
|
+
left: centerX,
|
|
410
|
+
top: centerY,
|
|
411
|
+
transform: transformString,
|
|
412
|
+
transition: imageTransition,
|
|
413
|
+
};
|
|
414
|
+
}, [ canvasSize, elementSize, transformString, imageTransition ] );
|
|
415
|
+
|
|
416
|
+
// Forward the root element to the consumer's ref.
|
|
417
|
+
const setContainerRef = useCallback(
|
|
418
|
+
( element: HTMLDivElement | null ) => {
|
|
419
|
+
if ( typeof ref === 'function' ) {
|
|
420
|
+
ref( element );
|
|
421
|
+
} else if ( ref ) {
|
|
422
|
+
(
|
|
423
|
+
ref as React.MutableRefObject< HTMLDivElement | null >
|
|
424
|
+
).current = element;
|
|
425
|
+
}
|
|
426
|
+
},
|
|
427
|
+
[ ref ]
|
|
428
|
+
);
|
|
429
|
+
|
|
430
|
+
return (
|
|
431
|
+
<div
|
|
432
|
+
ref={ setContainerRef }
|
|
433
|
+
className={ clsx(
|
|
434
|
+
'wp-media-editor-image-editor',
|
|
435
|
+
isDragging && 'wp-media-editor-image-editor--dragging',
|
|
436
|
+
className
|
|
437
|
+
) }
|
|
438
|
+
>
|
|
439
|
+
{ /*
|
|
440
|
+
* The canvas is the interactive, inset surface. Handles and
|
|
441
|
+
* the ARIA role/tabIndex live here so pointer geometry
|
|
442
|
+
* (getBoundingClientRect on e.currentTarget) resolves against
|
|
443
|
+
* the same box that crop math uses. The root stays as the
|
|
444
|
+
* clipping shell for the dimming overlay's box-shadow.
|
|
445
|
+
*
|
|
446
|
+
* Not role="application" — that disables the screen reader's
|
|
447
|
+
* normal keyboard interception, too heavy-handed for a single
|
|
448
|
+
* widget. Screen reader users get the ARIA live region below
|
|
449
|
+
* as the announcement channel.
|
|
450
|
+
*/ }
|
|
451
|
+
<div
|
|
452
|
+
ref={ canvasRef }
|
|
453
|
+
className={ clsx(
|
|
454
|
+
'wp-media-editor-image-editor__canvas',
|
|
455
|
+
isInteractiveGrid &&
|
|
456
|
+
'wp-media-editor-image-editor__canvas--grid-interactive',
|
|
457
|
+
showInteractiveGrid &&
|
|
458
|
+
'wp-media-editor-image-editor__canvas--show-grid'
|
|
459
|
+
) }
|
|
460
|
+
tabIndex={ 0 }
|
|
461
|
+
role="group"
|
|
462
|
+
aria-label={ __( 'Image editor' ) }
|
|
463
|
+
{ ...handlers }
|
|
464
|
+
>
|
|
465
|
+
{ /* The image layer */ }
|
|
466
|
+
<img
|
|
467
|
+
className="wp-media-editor-image-editor__image"
|
|
468
|
+
src={ src }
|
|
469
|
+
alt=""
|
|
470
|
+
onLoad={ handleImageLoad }
|
|
471
|
+
style={ imageStyle }
|
|
472
|
+
draggable={ false }
|
|
473
|
+
/>
|
|
474
|
+
|
|
475
|
+
{ /* Dimming overlay outside the crop area */ }
|
|
476
|
+
{ showDimming && (
|
|
477
|
+
<DimmingOverlay
|
|
478
|
+
cropRect={ state.cropRect }
|
|
479
|
+
containerSize={ canvasSize }
|
|
480
|
+
imageSize={ visualSize }
|
|
481
|
+
/>
|
|
482
|
+
) }
|
|
483
|
+
|
|
484
|
+
{ /* The stencil (crop area with handles) */ }
|
|
485
|
+
<StencilComponent
|
|
486
|
+
cropRect={ state.cropRect }
|
|
487
|
+
containerSize={ canvasSize }
|
|
488
|
+
imageSize={ visualSize }
|
|
489
|
+
onCropChange={ handleCropChange }
|
|
490
|
+
onResizeStart={ handleResizeStart }
|
|
491
|
+
onResizeEnd={ handleResizeEnd }
|
|
492
|
+
onEscape={ handleEscape }
|
|
493
|
+
aspectRatio={ aspectRatio }
|
|
494
|
+
freeformCrop={ freeformCrop }
|
|
495
|
+
stencilTransition={ settleStencilTransition }
|
|
496
|
+
cropBounds={ cropBounds }
|
|
497
|
+
/>
|
|
498
|
+
|
|
499
|
+
{ /* Rule-of-thirds grid */ }
|
|
500
|
+
{ ( showGrid === true || isInteractiveGrid ) && (
|
|
501
|
+
<GridOverlay
|
|
502
|
+
cropRect={ state.cropRect }
|
|
503
|
+
containerSize={ canvasSize }
|
|
504
|
+
imageSize={ visualSize }
|
|
505
|
+
/>
|
|
506
|
+
) }
|
|
507
|
+
|
|
508
|
+
{ /* ARIA live region for screen reader announcements */ }
|
|
509
|
+
<div
|
|
510
|
+
aria-live="polite"
|
|
511
|
+
aria-atomic="true"
|
|
512
|
+
className="wp-media-editor-image-editor__aria-live"
|
|
513
|
+
style={ {
|
|
514
|
+
position: 'absolute',
|
|
515
|
+
width: 1,
|
|
516
|
+
height: 1,
|
|
517
|
+
padding: 0,
|
|
518
|
+
margin: -1,
|
|
519
|
+
overflow: 'hidden',
|
|
520
|
+
clip: 'rect(0, 0, 0, 0)',
|
|
521
|
+
whiteSpace: 'nowrap',
|
|
522
|
+
border: 0,
|
|
523
|
+
} }
|
|
524
|
+
>
|
|
525
|
+
{ ariaMessage }
|
|
526
|
+
</div>
|
|
527
|
+
</div>
|
|
528
|
+
</div>
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
export const Cropper = forwardRef< HTMLDivElement, CropperProps >(
|
|
533
|
+
CropperInner
|
|
534
|
+
);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { NormalizedRect, Size } from '../../../core/types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Props for the DimmingOverlay component.
|
|
8
|
+
*/
|
|
9
|
+
interface DimmingOverlayProps {
|
|
10
|
+
/** The crop rectangle in normalized coordinates. */
|
|
11
|
+
cropRect: NormalizedRect;
|
|
12
|
+
/** The container element dimensions in pixels. */
|
|
13
|
+
containerSize: Size;
|
|
14
|
+
/** The rendered image dimensions in pixels within the container. */
|
|
15
|
+
imageSize: Size;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Renders a semi-transparent overlay outside the crop rectangle.
|
|
20
|
+
*
|
|
21
|
+
* Uses the box-shadow approach: a div matching the crop rect position
|
|
22
|
+
* with a large box-shadow that dims everything outside it.
|
|
23
|
+
*
|
|
24
|
+
* @param props Component props.
|
|
25
|
+
* @param props.cropRect The crop rectangle in normalized coordinates.
|
|
26
|
+
* @param props.containerSize The container element dimensions in pixels.
|
|
27
|
+
* @param props.imageSize The rendered image dimensions in pixels.
|
|
28
|
+
* @return The dimming overlay element.
|
|
29
|
+
*/
|
|
30
|
+
export function DimmingOverlay( {
|
|
31
|
+
cropRect,
|
|
32
|
+
containerSize,
|
|
33
|
+
imageSize,
|
|
34
|
+
}: DimmingOverlayProps ) {
|
|
35
|
+
if ( containerSize.width === 0 || containerSize.height === 0 ) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const offsetX = ( containerSize.width - imageSize.width ) / 2;
|
|
40
|
+
const offsetY = ( containerSize.height - imageSize.height ) / 2;
|
|
41
|
+
const left = offsetX + cropRect.x * imageSize.width;
|
|
42
|
+
const top = offsetY + cropRect.y * imageSize.height;
|
|
43
|
+
const width = cropRect.width * imageSize.width;
|
|
44
|
+
const height = cropRect.height * imageSize.height;
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<div
|
|
48
|
+
className="wp-media-editor-image-editor__dimming"
|
|
49
|
+
style={ {
|
|
50
|
+
left,
|
|
51
|
+
top,
|
|
52
|
+
width,
|
|
53
|
+
height,
|
|
54
|
+
} }
|
|
55
|
+
/>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { NormalizedRect, Size } from '../../../core/types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Props for the GridOverlay component.
|
|
8
|
+
*/
|
|
9
|
+
interface GridOverlayProps {
|
|
10
|
+
/** The crop rectangle in normalized coordinates. */
|
|
11
|
+
cropRect: NormalizedRect;
|
|
12
|
+
/** The container element dimensions in pixels. */
|
|
13
|
+
containerSize: Size;
|
|
14
|
+
/** The rendered image dimensions in pixels within the container. */
|
|
15
|
+
imageSize: Size;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Renders a rule-of-thirds grid inside the crop rectangle.
|
|
20
|
+
*
|
|
21
|
+
* Displays 2 horizontal and 2 vertical lines at 1/3 and 2/3
|
|
22
|
+
* positions within the crop area.
|
|
23
|
+
*
|
|
24
|
+
* @param props Component props.
|
|
25
|
+
* @param props.cropRect The crop rectangle in normalized coordinates.
|
|
26
|
+
* @param props.containerSize The container element dimensions in pixels.
|
|
27
|
+
* @param props.imageSize The rendered image dimensions in pixels.
|
|
28
|
+
* @return The grid overlay element.
|
|
29
|
+
*/
|
|
30
|
+
export function GridOverlay( {
|
|
31
|
+
cropRect,
|
|
32
|
+
containerSize,
|
|
33
|
+
imageSize,
|
|
34
|
+
}: GridOverlayProps ) {
|
|
35
|
+
if ( containerSize.width === 0 || containerSize.height === 0 ) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const offsetX = ( containerSize.width - imageSize.width ) / 2;
|
|
40
|
+
const offsetY = ( containerSize.height - imageSize.height ) / 2;
|
|
41
|
+
const left = offsetX + cropRect.x * imageSize.width;
|
|
42
|
+
const top = offsetY + cropRect.y * imageSize.height;
|
|
43
|
+
const width = cropRect.width * imageSize.width;
|
|
44
|
+
const height = cropRect.height * imageSize.height;
|
|
45
|
+
|
|
46
|
+
const thirdW = width / 3;
|
|
47
|
+
const thirdH = height / 3;
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<div
|
|
51
|
+
className="wp-media-editor-image-editor__grid"
|
|
52
|
+
data-testid="cropper-grid"
|
|
53
|
+
style={ {
|
|
54
|
+
left,
|
|
55
|
+
top,
|
|
56
|
+
width,
|
|
57
|
+
height,
|
|
58
|
+
} }
|
|
59
|
+
>
|
|
60
|
+
{ /* Horizontal lines at 1/3 and 2/3 */ }
|
|
61
|
+
<div
|
|
62
|
+
className="wp-media-editor-image-editor__grid-line wp-media-editor-image-editor__grid-line--horizontal"
|
|
63
|
+
style={ { top: thirdH } }
|
|
64
|
+
/>
|
|
65
|
+
<div
|
|
66
|
+
className="wp-media-editor-image-editor__grid-line wp-media-editor-image-editor__grid-line--horizontal"
|
|
67
|
+
style={ { top: thirdH * 2 } }
|
|
68
|
+
/>
|
|
69
|
+
{ /* Vertical lines at 1/3 and 2/3 */ }
|
|
70
|
+
<div
|
|
71
|
+
className="wp-media-editor-image-editor__grid-line wp-media-editor-image-editor__grid-line--vertical"
|
|
72
|
+
style={ { left: thirdW } }
|
|
73
|
+
/>
|
|
74
|
+
<div
|
|
75
|
+
className="wp-media-editor-image-editor__grid-line wp-media-editor-image-editor__grid-line--vertical"
|
|
76
|
+
style={ { left: thirdW * 2 } }
|
|
77
|
+
/>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|