@wordpress/media-editor 0.8.1-next.v.202605131032.0 → 0.10.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 +34 -135
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +87 -0
- package/build/components/media-editor/use-crop-options.cjs.map +7 -0
- package/build/components/media-editor/use-save-media-editor.cjs +185 -0
- package/build/components/media-editor/use-save-media-editor.cjs.map +7 -0
- package/build/components/media-editor-canvas/index.cjs +32 -11
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +66 -83
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-modal/index.cjs +40 -11
- package/build/components/media-editor-modal/index.cjs.map +3 -3
- package/build/components/media-editor-toolbar/index.cjs +7 -3
- package/build/components/media-editor-toolbar/index.cjs.map +2 -2
- package/build/components/media-form/index.cjs +19 -1
- package/build/components/media-form/index.cjs.map +3 -3
- package/build/components/media-form/sidebar-datetime-view.cjs +53 -0
- package/build/components/media-form/sidebar-datetime-view.cjs.map +7 -0
- package/build/components/rotation-ruler/index.cjs +8 -2
- package/build/components/rotation-ruler/index.cjs.map +2 -2
- package/build/hooks/use-crop-gesture-handlers.cjs +41 -4
- package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
- package/build/image-editor/core/camera.cjs +29 -11
- package/build/image-editor/core/camera.cjs.map +2 -2
- package/build/image-editor/core/constants.cjs +6 -0
- package/build/image-editor/core/constants.cjs.map +2 -2
- package/build/image-editor/core/containment.cjs +71 -34
- package/build/image-editor/core/containment.cjs.map +2 -2
- package/build/image-editor/core/crop-rect.cjs +48 -0
- package/build/image-editor/core/crop-rect.cjs.map +7 -0
- package/build/image-editor/core/index.cjs +12 -0
- package/build/image-editor/core/index.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/core/math/sanitize.cjs +92 -0
- package/build/image-editor/core/math/sanitize.cjs.map +7 -0
- package/build/image-editor/core/setter-helpers.cjs +64 -0
- package/build/image-editor/core/setter-helpers.cjs.map +7 -0
- package/build/image-editor/core/source-region.cjs +16 -14
- package/build/image-editor/core/source-region.cjs.map +2 -2
- package/build/image-editor/core/state.cjs +20 -10
- package/build/image-editor/core/state.cjs.map +2 -2
- package/build/image-editor/core/stencil-math.cjs +31 -20
- package/build/image-editor/core/stencil-math.cjs.map +2 -2
- package/build/image-editor/core/transform-style.cjs +12 -6
- package/build/image-editor/core/transform-style.cjs.map +2 -2
- package/build/image-editor/core/types.cjs.map +1 -1
- package/build/image-editor/index.cjs +12 -2
- package/build/image-editor/index.cjs.map +2 -2
- package/build/image-editor/react/components/cropper-provider.cjs +2 -2
- package/build/image-editor/react/components/cropper-provider.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +121 -48
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +172 -0
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +7 -0
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +20 -10
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/build-cropper-setters.cjs +71 -0
- package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +7 -0
- package/build/image-editor/react/hooks/index.cjs +6 -3
- package/build/image-editor/react/hooks/index.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +3 -1
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs +113 -0
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +7 -0
- package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/state/composite-reducer.cjs +115 -0
- package/build/state/composite-reducer.cjs.map +7 -0
- package/build/state/index.cjs +48 -0
- package/build/state/index.cjs.map +7 -0
- package/build/state/media-editor-state-provider.cjs +58 -0
- package/build/state/media-editor-state-provider.cjs.map +7 -0
- package/build/state/types.cjs +34 -0
- package/build/state/types.cjs.map +7 -0
- package/build/state/use-media-editor-state.cjs +298 -0
- package/build/state/use-media-editor-state.cjs.map +7 -0
- package/build-module/components/media-editor/index.mjs +40 -140
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +61 -0
- package/build-module/components/media-editor/use-crop-options.mjs.map +7 -0
- package/build-module/components/media-editor/use-save-media-editor.mjs +151 -0
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +7 -0
- package/build-module/components/media-editor-canvas/index.mjs +33 -12
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +68 -83
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-modal/index.mjs +40 -11
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-toolbar/index.mjs +7 -3
- package/build-module/components/media-editor-toolbar/index.mjs.map +2 -2
- package/build-module/components/media-form/index.mjs +9 -1
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/media-form/sidebar-datetime-view.mjs +32 -0
- package/build-module/components/media-form/sidebar-datetime-view.mjs.map +7 -0
- package/build-module/components/rotation-ruler/index.mjs +8 -2
- package/build-module/components/rotation-ruler/index.mjs.map +2 -2
- package/build-module/hooks/use-crop-gesture-handlers.mjs +41 -4
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
- package/build-module/image-editor/core/camera.mjs +34 -11
- package/build-module/image-editor/core/camera.mjs.map +2 -2
- package/build-module/image-editor/core/constants.mjs +4 -0
- package/build-module/image-editor/core/constants.mjs.map +2 -2
- package/build-module/image-editor/core/containment.mjs +75 -34
- package/build-module/image-editor/core/containment.mjs.map +2 -2
- package/build-module/image-editor/core/crop-rect.mjs +23 -0
- package/build-module/image-editor/core/crop-rect.mjs.map +7 -0
- package/build-module/image-editor/core/index.mjs +12 -0
- package/build-module/image-editor/core/index.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/core/math/sanitize.mjs +63 -0
- package/build-module/image-editor/core/math/sanitize.mjs.map +7 -0
- package/build-module/image-editor/core/setter-helpers.mjs +39 -0
- package/build-module/image-editor/core/setter-helpers.mjs.map +7 -0
- package/build-module/image-editor/core/source-region.mjs +16 -14
- package/build-module/image-editor/core/source-region.mjs.map +2 -2
- package/build-module/image-editor/core/state.mjs +25 -11
- package/build-module/image-editor/core/state.mjs.map +2 -2
- package/build-module/image-editor/core/stencil-math.mjs +30 -19
- package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
- package/build-module/image-editor/core/transform-style.mjs +12 -6
- package/build-module/image-editor/core/transform-style.mjs.map +2 -2
- package/build-module/image-editor/index.mjs +11 -2
- package/build-module/image-editor/index.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper-provider.mjs +3 -3
- package/build-module/image-editor/react/components/cropper-provider.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +122 -49
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +147 -0
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +7 -0
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +20 -10
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +46 -0
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/index.mjs +4 -2
- package/build-module/image-editor/react/hooks/index.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +3 -1
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +100 -0
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/state/composite-reducer.mjs +94 -0
- package/build-module/state/composite-reducer.mjs.map +7 -0
- package/build-module/state/index.mjs +26 -0
- package/build-module/state/index.mjs.map +7 -0
- package/build-module/state/media-editor-state-provider.mjs +34 -0
- package/build-module/state/media-editor-state-provider.mjs.map +7 -0
- package/build-module/state/types.mjs +9 -0
- package/build-module/state/types.mjs.map +7 -0
- package/build-module/state/use-media-editor-state.mjs +289 -0
- package/build-module/state/use-media-editor-state.mjs.map +7 -0
- package/build-style/style-rtl.css +46 -9
- package/build-style/style.css +46 -9
- package/build-types/components/media-editor/index.d.ts +4 -7
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +37 -0
- package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -0
- package/build-types/components/media-editor/use-save-media-editor.d.ts +29 -0
- package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -0
- package/build-types/components/media-editor-canvas/index.d.ts +5 -10
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +4 -16
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/components/media-form/sidebar-datetime-view.d.ts +10 -0
- package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +1 -0
- package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
- package/build-types/hooks/use-crop-gesture-handlers.d.ts +13 -14
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
- package/build-types/image-editor/core/camera.d.ts.map +1 -1
- package/build-types/image-editor/core/constants.d.ts +18 -0
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/containment.d.ts +10 -0
- package/build-types/image-editor/core/containment.d.ts.map +1 -1
- package/build-types/image-editor/core/crop-rect.d.ts +19 -0
- package/build-types/image-editor/core/crop-rect.d.ts.map +1 -0
- package/build-types/image-editor/core/index.d.ts +3 -1
- package/build-types/image-editor/core/index.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts +5 -1
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/core/math/sanitize.d.ts +70 -0
- package/build-types/image-editor/core/math/sanitize.d.ts.map +1 -0
- package/build-types/image-editor/core/setter-helpers.d.ts +24 -0
- package/build-types/image-editor/core/setter-helpers.d.ts.map +1 -0
- package/build-types/image-editor/core/source-region.d.ts.map +1 -1
- package/build-types/image-editor/core/state.d.ts +12 -0
- package/build-types/image-editor/core/state.d.ts.map +1 -1
- package/build-types/image-editor/core/stencil-math.d.ts +23 -18
- package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
- package/build-types/image-editor/core/transform-style.d.ts.map +1 -1
- package/build-types/image-editor/core/types.d.ts +23 -4
- package/build-types/image-editor/core/types.d.ts.map +1 -1
- package/build-types/image-editor/index.d.ts +4 -4
- package/build-types/image-editor/index.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts +11 -7
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +6 -4
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +36 -0
- package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +1 -0
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +2 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +47 -0
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/index.d.ts +4 -2
- package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts +58 -0
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts +39 -0
- package/build-types/state/composite-reducer.d.ts.map +1 -0
- package/build-types/state/index.d.ts +7 -0
- package/build-types/state/index.d.ts.map +1 -0
- package/build-types/state/media-editor-state-provider.d.ts +34 -0
- package/build-types/state/media-editor-state-provider.d.ts.map +1 -0
- package/build-types/state/types.d.ts +104 -0
- package/build-types/state/types.d.ts.map +1 -0
- package/build-types/state/use-media-editor-state.d.ts +104 -0
- package/build-types/state/use-media-editor-state.d.ts.map +1 -0
- package/package.json +19 -17
- package/src/components/media-editor/index.tsx +52 -188
- package/src/components/media-editor/test/use-crop-options.tsx +164 -0
- package/src/components/media-editor/use-crop-options.ts +115 -0
- package/src/components/media-editor/use-save-media-editor.ts +214 -0
- package/src/components/media-editor-canvas/index.tsx +56 -24
- package/src/components/media-editor-crop-panel/index.tsx +25 -61
- package/src/components/media-editor-crop-panel/test/index.tsx +17 -43
- package/src/components/media-editor-modal/index.tsx +50 -12
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +14 -0
- package/src/components/media-editor-modal/test/index.tsx +143 -0
- package/src/components/media-editor-toolbar/index.tsx +9 -5
- package/src/components/media-form/index.tsx +15 -1
- package/src/components/media-form/sidebar-datetime-view.tsx +47 -0
- package/src/components/media-form/test/sidebar-datetime-view.test.tsx +96 -0
- package/src/components/rotation-ruler/README.md +11 -25
- package/src/components/rotation-ruler/index.tsx +17 -2
- package/src/components/rotation-ruler/style.scss +49 -13
- package/src/hooks/use-crop-gesture-handlers.ts +66 -17
- package/src/image-editor/README.md +28 -129
- package/src/image-editor/core/camera.ts +51 -26
- package/src/image-editor/core/constants.ts +19 -0
- package/src/image-editor/core/containment.ts +102 -49
- package/src/image-editor/core/crop-rect.ts +45 -0
- package/src/image-editor/core/index.ts +16 -4
- package/src/image-editor/core/interaction-controller.ts +5 -1
- package/src/image-editor/core/math/sanitize.ts +158 -0
- package/src/image-editor/core/math/test/sanitize.ts +244 -0
- package/src/image-editor/core/setter-helpers.ts +66 -0
- package/src/image-editor/core/source-region.ts +21 -14
- package/src/image-editor/core/state.ts +67 -15
- package/src/image-editor/core/stencil-math.ts +55 -35
- package/src/image-editor/core/test/camera.ts +367 -1
- package/src/image-editor/core/test/state.ts +101 -8
- package/src/image-editor/core/test/stencil-math.ts +193 -0
- package/src/image-editor/core/transform-style.ts +21 -6
- package/src/image-editor/core/types.ts +24 -4
- package/src/image-editor/docs/architecture.md +48 -89
- package/src/image-editor/docs/recipes.md +123 -659
- package/src/image-editor/index.ts +12 -3
- package/src/image-editor/react/components/cropper-provider.tsx +16 -12
- package/src/image-editor/react/components/cropper.scss +23 -4
- package/src/image-editor/react/components/cropper.tsx +197 -90
- package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +269 -0
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +22 -9
- package/src/image-editor/react/components/test/cropper.tsx +55 -13
- package/src/image-editor/react/hooks/build-cropper-setters.ts +105 -0
- package/src/image-editor/react/hooks/index.ts +4 -2
- package/src/image-editor/react/hooks/test/{use-cropper-state.ts → use-cropper-reducer.ts} +142 -286
- package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
- package/src/image-editor/react/hooks/use-cropper-reducer.ts +204 -0
- package/src/image-editor/react/hooks/use-interaction.ts +3 -2
- package/src/image-editor/stories/rectangle-crop.story.tsx +7 -17
- package/src/state/composite-reducer.ts +152 -0
- package/src/state/index.ts +20 -0
- package/src/state/media-editor-state-provider.tsx +70 -0
- package/src/state/test/use-media-editor-state.ts +402 -0
- package/src/state/types.ts +107 -0
- package/src/state/use-media-editor-state.ts +488 -0
- package/tsconfig.json +2 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/build/image-editor/react/hooks/use-cropper-state.cjs +0 -277
- package/build/image-editor/react/hooks/use-cropper-state.cjs.map +0 -7
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs +0 -262
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +0 -7
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -97
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
- package/src/image-editor/docs/roadmap.md +0 -66
- package/src/image-editor/react/hooks/use-cropper-state.ts +0 -457
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
-
import apiFetch from '@wordpress/api-fetch';
|
|
5
4
|
import {
|
|
6
5
|
Button,
|
|
7
6
|
Flex,
|
|
@@ -10,7 +9,7 @@ import {
|
|
|
10
9
|
privateApis as componentsPrivateApis,
|
|
11
10
|
} from '@wordpress/components';
|
|
12
11
|
import { Stack } from '@wordpress/ui';
|
|
13
|
-
import { useDispatch,
|
|
12
|
+
import { useDispatch, useSelect } from '@wordpress/data';
|
|
14
13
|
import { store as coreStore } from '@wordpress/core-data';
|
|
15
14
|
import {
|
|
16
15
|
createPortal,
|
|
@@ -21,7 +20,7 @@ import {
|
|
|
21
20
|
useRef,
|
|
22
21
|
useState,
|
|
23
22
|
} from '@wordpress/element';
|
|
24
|
-
import { __
|
|
23
|
+
import { __ } from '@wordpress/i18n';
|
|
25
24
|
import { close, drawerRight, keyboard } from '@wordpress/icons';
|
|
26
25
|
import { isAppleOS, isKeyboardEvent } from '@wordpress/keycodes';
|
|
27
26
|
import { SnackbarNotices, store as noticesStore } from '@wordpress/notices';
|
|
@@ -33,7 +32,11 @@ import {
|
|
|
33
32
|
// No type declarations available for @wordpress/interface.
|
|
34
33
|
// @ts-expect-error
|
|
35
34
|
} from '@wordpress/interface';
|
|
36
|
-
import type {
|
|
35
|
+
import type {
|
|
36
|
+
JSX,
|
|
37
|
+
KeyboardEvent as ReactKeyboardEvent,
|
|
38
|
+
ReactNode,
|
|
39
|
+
} from 'react';
|
|
37
40
|
|
|
38
41
|
/**
|
|
39
42
|
* Internal dependencies
|
|
@@ -43,35 +46,28 @@ import type { Media } from '../media-editor-provider';
|
|
|
43
46
|
import MediaPreview from '../media-preview';
|
|
44
47
|
import MediaEditorCanvas from '../media-editor-canvas';
|
|
45
48
|
import MediaEditorToolbar from '../media-editor-toolbar';
|
|
46
|
-
import MediaEditorCropPanel
|
|
47
|
-
resolveAspectRatio,
|
|
48
|
-
} from '../media-editor-crop-panel';
|
|
49
|
+
import MediaEditorCropPanel from '../media-editor-crop-panel';
|
|
49
50
|
import MediaForm from '../media-form';
|
|
50
51
|
import { unlock } from '../../lock-unlock';
|
|
51
52
|
import { getMediaTypeFromMimeType } from '../../utils';
|
|
52
|
-
import {
|
|
53
|
+
import { MediaEditorStateProvider, useMediaEditor } from '../../state';
|
|
53
54
|
import type { AspectRatioPreset } from '../../image-editor/core/constants';
|
|
54
55
|
import { CROP_CONTROL_ATTR } from '../../hooks/use-crop-gesture-handlers';
|
|
55
|
-
import { buildModifiers } from '../media-editor-modal/build-modifiers';
|
|
56
56
|
import MediaEditorKeyboardShortcutsModal from '../media-editor-keyboard-shortcuts-modal';
|
|
57
|
+
import {
|
|
58
|
+
MEDIA_EDITOR_NOTICES_CONTEXT,
|
|
59
|
+
useSaveMediaEditor,
|
|
60
|
+
type MediaEditorSaveResult,
|
|
61
|
+
} from './use-save-media-editor';
|
|
62
|
+
import { useCropOptions } from './use-crop-options';
|
|
57
63
|
|
|
58
|
-
|
|
59
|
-
// endpoint only accepts this whitelist.
|
|
60
|
-
const METADATA_EDIT_KEYS = [
|
|
61
|
-
'title',
|
|
62
|
-
'caption',
|
|
63
|
-
'description',
|
|
64
|
-
'alt_text',
|
|
65
|
-
'post',
|
|
66
|
-
] as const;
|
|
64
|
+
export type { MediaEditorSaveResult } from './use-save-media-editor';
|
|
67
65
|
|
|
68
66
|
// Embed query for the attachment's author and parent post. Shared between
|
|
69
67
|
// the `getEntityRecord` read and the matching `invalidateResolution` so the
|
|
70
68
|
// two stay in lockstep.
|
|
71
69
|
const ATTACHMENT_EMBED_QUERY = { _embed: 'author,wp:attached-to' } as const;
|
|
72
70
|
|
|
73
|
-
// Scope save-failure snackbars so they don't leak into the host editor/page.
|
|
74
|
-
const NOTICES_CONTEXT = 'media-editor';
|
|
75
71
|
const PLACEMENT_CONTROL_IDLE_MS = 300;
|
|
76
72
|
|
|
77
73
|
const { Tabs } = unlock( componentsPrivateApis );
|
|
@@ -82,12 +78,6 @@ interface EditorTab {
|
|
|
82
78
|
panel: JSX.Element;
|
|
83
79
|
}
|
|
84
80
|
|
|
85
|
-
export interface MediaEditorSaveResult {
|
|
86
|
-
id: number;
|
|
87
|
-
url?: string;
|
|
88
|
-
media: Media;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
81
|
export interface MediaEditorFrameProps {
|
|
92
82
|
children: ReactNode;
|
|
93
83
|
headerActions: ReactNode;
|
|
@@ -226,8 +216,6 @@ function HeaderActions( {
|
|
|
226
216
|
);
|
|
227
217
|
}
|
|
228
218
|
|
|
229
|
-
type PendingMetadataEdits = Record< string, unknown > | undefined;
|
|
230
|
-
|
|
231
219
|
function MediaEditorContent( {
|
|
232
220
|
fields = [],
|
|
233
221
|
id,
|
|
@@ -240,7 +228,7 @@ function MediaEditorContent( {
|
|
|
240
228
|
showCloseButton = false,
|
|
241
229
|
shouldCloseOnEsc = false,
|
|
242
230
|
}: MediaEditorProps ) {
|
|
243
|
-
const cropper =
|
|
231
|
+
const cropper = useMediaEditor();
|
|
244
232
|
|
|
245
233
|
const { media, hasEdits } = useSelect(
|
|
246
234
|
( select ) => {
|
|
@@ -275,28 +263,19 @@ function MediaEditorContent( {
|
|
|
275
263
|
[ id ]
|
|
276
264
|
);
|
|
277
265
|
|
|
278
|
-
const hasChanges = cropper.
|
|
266
|
+
const hasChanges = cropper.isCropperDirty || hasEdits;
|
|
267
|
+
|
|
268
|
+
const { clearEntityRecordEdits, editEntityRecord, invalidateResolution } =
|
|
269
|
+
useDispatch( coreStore );
|
|
270
|
+
const { removeAllNotices } = useDispatch( noticesStore );
|
|
279
271
|
|
|
280
|
-
const registry = useRegistry();
|
|
281
|
-
const {
|
|
282
|
-
clearEntityRecordEdits,
|
|
283
|
-
editEntityRecord,
|
|
284
|
-
invalidateResolution,
|
|
285
|
-
receiveEntityRecords,
|
|
286
|
-
saveEditedEntityRecord,
|
|
287
|
-
} = useDispatch( coreStore );
|
|
288
|
-
const { createErrorNotice, removeAllNotices } = useDispatch( noticesStore );
|
|
289
|
-
|
|
290
|
-
const [ isSaving, setIsSaving ] = useState( false );
|
|
291
272
|
const [ isDiscardDialogOpen, setIsDiscardDialogOpen ] = useState( false );
|
|
292
273
|
const [ isPlacementActive, setIsPlacementActive ] = useState( false );
|
|
293
274
|
const [ isCanvasGestureActive, setIsCanvasGestureActive ] =
|
|
294
275
|
useState( false );
|
|
295
|
-
const placementControlTimerRef =
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const [ aspectRatioValue, setAspectRatioValue ] = useState( '0' );
|
|
299
|
-
const [ freeformCrop, setFreeformCrop ] = useState( true );
|
|
276
|
+
const placementControlTimerRef = useRef<
|
|
277
|
+
ReturnType< typeof setTimeout > | undefined
|
|
278
|
+
>( undefined );
|
|
300
279
|
|
|
301
280
|
const signalPlacementControlInteraction = useCallback( () => {
|
|
302
281
|
setIsPlacementActive( true );
|
|
@@ -320,8 +299,6 @@ function MediaEditorContent( {
|
|
|
320
299
|
}, [] );
|
|
321
300
|
|
|
322
301
|
useEffect( () => {
|
|
323
|
-
setAspectRatioValue( '0' );
|
|
324
|
-
setFreeformCrop( true );
|
|
325
302
|
setIsPlacementActive( false );
|
|
326
303
|
setIsCanvasGestureActive( false );
|
|
327
304
|
}, [ id ] );
|
|
@@ -340,22 +317,23 @@ function MediaEditorContent( {
|
|
|
340
317
|
|
|
341
318
|
const mediaType = getMediaTypeFromMimeType( media?.mime_type ).type;
|
|
342
319
|
const isImage = !! media && mediaType === 'image';
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
320
|
+
const {
|
|
321
|
+
aspectRatioValue,
|
|
322
|
+
setAspectRatioValue,
|
|
323
|
+
aspectRatioOptions,
|
|
324
|
+
freeformCrop,
|
|
325
|
+
setFreeformCrop,
|
|
326
|
+
resetCropOptions,
|
|
327
|
+
} = useCropOptions( {
|
|
328
|
+
aspectRatioPresets,
|
|
329
|
+
} );
|
|
330
|
+
const { isSaving, save: saveMediaEditor } = useSaveMediaEditor( {
|
|
331
|
+
cropper,
|
|
332
|
+
id,
|
|
333
|
+
isImage,
|
|
334
|
+
media,
|
|
335
|
+
onSaved,
|
|
336
|
+
} );
|
|
359
337
|
|
|
360
338
|
const tabs = useMemo< EditorTab[] >( () => {
|
|
361
339
|
const detailsTab: EditorTab = {
|
|
@@ -392,7 +370,7 @@ function MediaEditorContent( {
|
|
|
392
370
|
onPlacementControlInteraction={
|
|
393
371
|
signalPlacementControlInteraction
|
|
394
372
|
}
|
|
395
|
-
|
|
373
|
+
aspectRatioOptions={ aspectRatioOptions }
|
|
396
374
|
/>
|
|
397
375
|
</Stack>
|
|
398
376
|
),
|
|
@@ -402,8 +380,10 @@ function MediaEditorContent( {
|
|
|
402
380
|
}, [
|
|
403
381
|
isImage,
|
|
404
382
|
aspectRatioValue,
|
|
383
|
+
setAspectRatioValue,
|
|
405
384
|
freeformCrop,
|
|
406
|
-
|
|
385
|
+
setFreeformCrop,
|
|
386
|
+
aspectRatioOptions,
|
|
407
387
|
signalPlacementControlInteraction,
|
|
408
388
|
] );
|
|
409
389
|
|
|
@@ -412,7 +392,7 @@ function MediaEditorContent( {
|
|
|
412
392
|
};
|
|
413
393
|
|
|
414
394
|
const discardAndClose = () => {
|
|
415
|
-
removeAllNotices( 'snackbar',
|
|
395
|
+
removeAllNotices( 'snackbar', MEDIA_EDITOR_NOTICES_CONTEXT );
|
|
416
396
|
clearEntityRecordEdits( 'postType', 'attachment', id );
|
|
417
397
|
onClose?.();
|
|
418
398
|
};
|
|
@@ -428,114 +408,6 @@ function MediaEditorContent( {
|
|
|
428
408
|
discardAndClose();
|
|
429
409
|
};
|
|
430
410
|
|
|
431
|
-
const handleSave = async () => {
|
|
432
|
-
removeAllNotices( 'snackbar', NOTICES_CONTEXT );
|
|
433
|
-
setIsSaving( true );
|
|
434
|
-
try {
|
|
435
|
-
let saved: Media | null | undefined;
|
|
436
|
-
|
|
437
|
-
const modifiers =
|
|
438
|
-
cropper.isDirty && cropper.state.image
|
|
439
|
-
? buildModifiers( cropper.state, {
|
|
440
|
-
width: cropper.state.image.naturalWidth,
|
|
441
|
-
height: cropper.state.image.naturalHeight,
|
|
442
|
-
} )
|
|
443
|
-
: [];
|
|
444
|
-
|
|
445
|
-
if ( modifiers.length > 0 ) {
|
|
446
|
-
const pendingEdits = registry
|
|
447
|
-
.select( coreStore )
|
|
448
|
-
.getEntityRecordNonTransientEdits(
|
|
449
|
-
'postType',
|
|
450
|
-
'attachment',
|
|
451
|
-
id
|
|
452
|
-
) as PendingMetadataEdits;
|
|
453
|
-
const metadataEdits: Record< string, unknown > = {};
|
|
454
|
-
for ( const key of METADATA_EDIT_KEYS ) {
|
|
455
|
-
if ( pendingEdits && key in pendingEdits ) {
|
|
456
|
-
metadataEdits[ key ] = pendingEdits[ key ];
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
// The `/edit` endpoint creates a new attachment for the crop
|
|
460
|
-
// and doesn't inherit `post_parent` from the source (unlike
|
|
461
|
-
// title/caption/etc.), so carry the existing value across when
|
|
462
|
-
// the user hasn't explicitly edited it. Use a defined-check so
|
|
463
|
-
// an explicit `0` (unattached) is also preserved.
|
|
464
|
-
if (
|
|
465
|
-
! ( 'post' in metadataEdits ) &&
|
|
466
|
-
media?.post !== undefined
|
|
467
|
-
) {
|
|
468
|
-
metadataEdits.post = media.post;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
saved = ( await apiFetch( {
|
|
472
|
-
path: `/wp/v2/media/${ id }/edit`,
|
|
473
|
-
method: 'POST',
|
|
474
|
-
data: {
|
|
475
|
-
src: media?.source_url,
|
|
476
|
-
modifiers,
|
|
477
|
-
...metadataEdits,
|
|
478
|
-
},
|
|
479
|
-
} ) ) as Media;
|
|
480
|
-
|
|
481
|
-
if ( saved ) {
|
|
482
|
-
receiveEntityRecords(
|
|
483
|
-
'postType',
|
|
484
|
-
'attachment',
|
|
485
|
-
saved,
|
|
486
|
-
undefined,
|
|
487
|
-
true
|
|
488
|
-
);
|
|
489
|
-
}
|
|
490
|
-
} else {
|
|
491
|
-
saved = ( await saveEditedEntityRecord(
|
|
492
|
-
'postType',
|
|
493
|
-
'attachment',
|
|
494
|
-
id
|
|
495
|
-
) ) as Media | undefined;
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
const next = ( saved ?? media ) as Media | null;
|
|
499
|
-
|
|
500
|
-
if ( next && next.id !== id ) {
|
|
501
|
-
clearEntityRecordEdits( 'postType', 'attachment', id );
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
if ( next && next.id ) {
|
|
505
|
-
if ( next.id === id ) {
|
|
506
|
-
cropper.reset();
|
|
507
|
-
}
|
|
508
|
-
onSaved?.( {
|
|
509
|
-
id: next.id,
|
|
510
|
-
url: next.source_url,
|
|
511
|
-
media: next,
|
|
512
|
-
} );
|
|
513
|
-
}
|
|
514
|
-
} catch ( error ) {
|
|
515
|
-
const message =
|
|
516
|
-
error instanceof Error
|
|
517
|
-
? error.message
|
|
518
|
-
: ( error as { message?: string } )?.message ??
|
|
519
|
-
__( 'An unknown error occurred.' );
|
|
520
|
-
createErrorNotice(
|
|
521
|
-
isImage
|
|
522
|
-
? sprintf(
|
|
523
|
-
/* translators: %s: Error message. */
|
|
524
|
-
__( 'Could not save image. %s' ),
|
|
525
|
-
message
|
|
526
|
-
)
|
|
527
|
-
: sprintf(
|
|
528
|
-
/* translators: %s: Error message. */
|
|
529
|
-
__( 'Could not save media. %s' ),
|
|
530
|
-
message
|
|
531
|
-
),
|
|
532
|
-
{ type: 'snackbar', context: NOTICES_CONTEXT }
|
|
533
|
-
);
|
|
534
|
-
} finally {
|
|
535
|
-
setIsSaving( false );
|
|
536
|
-
}
|
|
537
|
-
};
|
|
538
|
-
|
|
539
411
|
const handleKeyDown = ( event: ReactKeyboardEvent< HTMLElement > ) => {
|
|
540
412
|
const isUndoShortcut = isKeyboardEvent.primary( event, 'z' );
|
|
541
413
|
const isRedoShortcut =
|
|
@@ -579,7 +451,7 @@ function MediaEditorContent( {
|
|
|
579
451
|
const snackbar = (
|
|
580
452
|
<SnackbarNotices
|
|
581
453
|
className={ noticesClassName }
|
|
582
|
-
context={
|
|
454
|
+
context={ MEDIA_EDITOR_NOTICES_CONTEXT }
|
|
583
455
|
/>
|
|
584
456
|
);
|
|
585
457
|
|
|
@@ -612,11 +484,6 @@ function MediaEditorContent( {
|
|
|
612
484
|
<div className="media-editor__canvas">
|
|
613
485
|
{ isImage ? (
|
|
614
486
|
<MediaEditorCanvas
|
|
615
|
-
aspectRatio={ resolveAspectRatio(
|
|
616
|
-
aspectRatioValue,
|
|
617
|
-
imageAspectRatio
|
|
618
|
-
) }
|
|
619
|
-
freeformCrop={ freeformCrop }
|
|
620
487
|
focusOnMount
|
|
621
488
|
isPlacementActive={
|
|
622
489
|
isPlacementActive
|
|
@@ -636,10 +503,7 @@ function MediaEditorContent( {
|
|
|
636
503
|
footer={
|
|
637
504
|
isImage ? (
|
|
638
505
|
<MediaEditorToolbar
|
|
639
|
-
onReset={
|
|
640
|
-
setAspectRatioValue( '0' );
|
|
641
|
-
setFreeformCrop( true );
|
|
642
|
-
} }
|
|
506
|
+
onReset={ resetCropOptions }
|
|
643
507
|
onPlacementControlInteraction={
|
|
644
508
|
signalPlacementControlInteraction
|
|
645
509
|
}
|
|
@@ -686,7 +550,7 @@ function MediaEditorContent( {
|
|
|
686
550
|
isImage={ isImage }
|
|
687
551
|
showCloseButton={ showCloseButton }
|
|
688
552
|
onCancel={ handleRequestClose }
|
|
689
|
-
onSave={
|
|
553
|
+
onSave={ saveMediaEditor }
|
|
690
554
|
/>
|
|
691
555
|
),
|
|
692
556
|
onRequestClose: handleRequestClose,
|
|
@@ -700,9 +564,9 @@ function MediaEditorContent( {
|
|
|
700
564
|
|
|
701
565
|
export function MediaEditor( props: MediaEditorProps ) {
|
|
702
566
|
return (
|
|
703
|
-
<
|
|
567
|
+
<MediaEditorStateProvider key={ props.id }>
|
|
704
568
|
<MediaEditorContent { ...props } />
|
|
705
|
-
</
|
|
569
|
+
</MediaEditorStateProvider>
|
|
706
570
|
);
|
|
707
571
|
}
|
|
708
572
|
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { fireEvent, render, screen } from '@testing-library/react';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import { useCropOptions } from '../use-crop-options';
|
|
10
|
+
import { ORIGINAL_ASPECT_RATIO } from '../../../image-editor/core/constants';
|
|
11
|
+
import { MediaEditorStateProvider } from '../../../state';
|
|
12
|
+
|
|
13
|
+
function CropOptionsHarness() {
|
|
14
|
+
const cropOptions = useCropOptions( {
|
|
15
|
+
aspectRatioPresets: [
|
|
16
|
+
{ label: 'Square', value: 1 },
|
|
17
|
+
{ label: 'Landscape', value: 4 / 3 },
|
|
18
|
+
],
|
|
19
|
+
} );
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<div>
|
|
23
|
+
<div data-testid="aspect-ratio-value">
|
|
24
|
+
{ cropOptions.aspectRatioValue }
|
|
25
|
+
</div>
|
|
26
|
+
<div data-testid="freeform-crop">
|
|
27
|
+
{ cropOptions.freeformCrop ? 'true' : 'false' }
|
|
28
|
+
</div>
|
|
29
|
+
<div data-testid="resolved-aspect-ratio">
|
|
30
|
+
{ cropOptions.resolvedAspectRatio ?? 'undefined' }
|
|
31
|
+
</div>
|
|
32
|
+
<div data-testid="aspect-ratio-options">
|
|
33
|
+
{ cropOptions.aspectRatioOptions
|
|
34
|
+
.map( ( option ) => option.value )
|
|
35
|
+
.join( ',' ) }
|
|
36
|
+
</div>
|
|
37
|
+
<button
|
|
38
|
+
onClick={ () =>
|
|
39
|
+
cropOptions.setAspectRatioValue(
|
|
40
|
+
ORIGINAL_ASPECT_RATIO.toString()
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
>
|
|
44
|
+
Original
|
|
45
|
+
</button>
|
|
46
|
+
<button onClick={ () => cropOptions.setAspectRatioValue( '1' ) }>
|
|
47
|
+
Square
|
|
48
|
+
</button>
|
|
49
|
+
<button onClick={ () => cropOptions.setAspectRatioValue( '0' ) }>
|
|
50
|
+
Free
|
|
51
|
+
</button>
|
|
52
|
+
<button onClick={ () => cropOptions.setFreeformCrop( false ) }>
|
|
53
|
+
Disable handles
|
|
54
|
+
</button>
|
|
55
|
+
<button onClick={ cropOptions.resetCropOptions }>Reset</button>
|
|
56
|
+
</div>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function renderHarness() {
|
|
61
|
+
render(
|
|
62
|
+
<MediaEditorStateProvider
|
|
63
|
+
initialCropperState={ {
|
|
64
|
+
image: {
|
|
65
|
+
src: 'test.jpg',
|
|
66
|
+
naturalWidth: 1200,
|
|
67
|
+
naturalHeight: 600,
|
|
68
|
+
},
|
|
69
|
+
} }
|
|
70
|
+
>
|
|
71
|
+
<CropOptionsHarness />
|
|
72
|
+
</MediaEditorStateProvider>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
describe( 'useCropOptions', () => {
|
|
77
|
+
it( 'builds explicit aspect-ratio options', () => {
|
|
78
|
+
renderHarness();
|
|
79
|
+
|
|
80
|
+
expect(
|
|
81
|
+
screen.getByTestId( 'aspect-ratio-options' )
|
|
82
|
+
).toHaveTextContent( '0,-1,1,1.3333333333333333' );
|
|
83
|
+
} );
|
|
84
|
+
|
|
85
|
+
it( 'resolves the Original aspect ratio from the cropper image dimensions', () => {
|
|
86
|
+
renderHarness();
|
|
87
|
+
|
|
88
|
+
fireEvent.click( screen.getByRole( 'button', { name: 'Original' } ) );
|
|
89
|
+
|
|
90
|
+
// 1200 / 600 = 2.
|
|
91
|
+
expect(
|
|
92
|
+
screen.getByTestId( 'resolved-aspect-ratio' )
|
|
93
|
+
).toHaveTextContent( '2' );
|
|
94
|
+
} );
|
|
95
|
+
|
|
96
|
+
it( 'reports Free as undefined resolved aspect ratio', () => {
|
|
97
|
+
renderHarness();
|
|
98
|
+
|
|
99
|
+
fireEvent.click( screen.getByRole( 'button', { name: 'Free' } ) );
|
|
100
|
+
|
|
101
|
+
expect(
|
|
102
|
+
screen.getByTestId( 'resolved-aspect-ratio' )
|
|
103
|
+
).toHaveTextContent( 'undefined' );
|
|
104
|
+
} );
|
|
105
|
+
|
|
106
|
+
it( 'picking Free auto-enables freeform when it was off', () => {
|
|
107
|
+
renderHarness();
|
|
108
|
+
|
|
109
|
+
// Pick a non-Free preset and turn freeform off.
|
|
110
|
+
fireEvent.click( screen.getByRole( 'button', { name: 'Square' } ) );
|
|
111
|
+
fireEvent.click(
|
|
112
|
+
screen.getByRole( 'button', { name: 'Disable handles' } )
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
expect( screen.getByTestId( 'freeform-crop' ) ).toHaveTextContent(
|
|
116
|
+
'false'
|
|
117
|
+
);
|
|
118
|
+
expect( screen.getByTestId( 'aspect-ratio-value' ) ).toHaveTextContent(
|
|
119
|
+
'1'
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
// Picking Free re-enables freeform — picking Free implies the
|
|
123
|
+
// user wants to freeform-edit and there'd otherwise be no
|
|
124
|
+
// visible affordance.
|
|
125
|
+
fireEvent.click( screen.getByRole( 'button', { name: 'Free' } ) );
|
|
126
|
+
|
|
127
|
+
expect( screen.getByTestId( 'aspect-ratio-value' ) ).toHaveTextContent(
|
|
128
|
+
'0'
|
|
129
|
+
);
|
|
130
|
+
expect( screen.getByTestId( 'freeform-crop' ) ).toHaveTextContent(
|
|
131
|
+
'true'
|
|
132
|
+
);
|
|
133
|
+
} );
|
|
134
|
+
|
|
135
|
+
it( 'picking Free leaves freeform alone if it was already on', () => {
|
|
136
|
+
renderHarness();
|
|
137
|
+
|
|
138
|
+
// Start clean: freeform is true by default.
|
|
139
|
+
fireEvent.click( screen.getByRole( 'button', { name: 'Square' } ) );
|
|
140
|
+
// Don't turn freeform off — pick Free directly.
|
|
141
|
+
fireEvent.click( screen.getByRole( 'button', { name: 'Free' } ) );
|
|
142
|
+
|
|
143
|
+
expect( screen.getByTestId( 'freeform-crop' ) ).toHaveTextContent(
|
|
144
|
+
'true'
|
|
145
|
+
);
|
|
146
|
+
} );
|
|
147
|
+
|
|
148
|
+
it( 'reset returns cropOptions to defaults (Free + freeform on)', () => {
|
|
149
|
+
renderHarness();
|
|
150
|
+
|
|
151
|
+
fireEvent.click( screen.getByRole( 'button', { name: 'Square' } ) );
|
|
152
|
+
fireEvent.click(
|
|
153
|
+
screen.getByRole( 'button', { name: 'Disable handles' } )
|
|
154
|
+
);
|
|
155
|
+
fireEvent.click( screen.getByRole( 'button', { name: 'Reset' } ) );
|
|
156
|
+
|
|
157
|
+
expect( screen.getByTestId( 'aspect-ratio-value' ) ).toHaveTextContent(
|
|
158
|
+
'0'
|
|
159
|
+
);
|
|
160
|
+
expect( screen.getByTestId( 'freeform-crop' ) ).toHaveTextContent(
|
|
161
|
+
'true'
|
|
162
|
+
);
|
|
163
|
+
} );
|
|
164
|
+
} );
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { useCallback, useMemo } from '@wordpress/element';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import { DEFAULT_ASPECT_RATIOS } from '../../image-editor/core/constants';
|
|
10
|
+
import type { AspectRatioPreset } from '../../image-editor/core/constants';
|
|
11
|
+
import { useMediaEditor, resolveAspectRatio } from '../../state';
|
|
12
|
+
|
|
13
|
+
/** Preset key for "Free" — no aspect lock. Round-trips through SelectControl. */
|
|
14
|
+
const FREE_ASPECT_RATIO_VALUE = '0';
|
|
15
|
+
|
|
16
|
+
interface UseCropOptionsArgs {
|
|
17
|
+
aspectRatioPresets?: AspectRatioPreset[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface UseCropOptionsReturn {
|
|
21
|
+
aspectRatioValue: string;
|
|
22
|
+
setAspectRatioValue: ( value: string ) => void;
|
|
23
|
+
aspectRatioOptions: AspectRatioPreset[];
|
|
24
|
+
freeformCrop: boolean;
|
|
25
|
+
setFreeformCrop: ( value: boolean ) => void;
|
|
26
|
+
resolvedAspectRatio: number | undefined;
|
|
27
|
+
resetCropOptions: () => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Build the preset list shown in the dropdown — always include the
|
|
32
|
+
* non-numeric presets (Free, Original) and append either the
|
|
33
|
+
* caller-supplied set or the defaults.
|
|
34
|
+
*
|
|
35
|
+
* @param aspectRatioPresets Optional caller-supplied presets.
|
|
36
|
+
* @return The full preset list to render.
|
|
37
|
+
*/
|
|
38
|
+
export function getAspectRatioOptions(
|
|
39
|
+
aspectRatioPresets?: AspectRatioPreset[]
|
|
40
|
+
): AspectRatioPreset[] {
|
|
41
|
+
return [
|
|
42
|
+
...DEFAULT_ASPECT_RATIOS.filter( ( preset ) => preset.value <= 0 ),
|
|
43
|
+
...( aspectRatioPresets ??
|
|
44
|
+
DEFAULT_ASPECT_RATIOS.filter( ( preset ) => preset.value > 0 ) ),
|
|
45
|
+
];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Thin selector over the composite media-editor store for the Crop
|
|
50
|
+
* sidebar tab. Reads the cropOptions slice (preset key, freeform) and
|
|
51
|
+
* exposes the corresponding setters plus a render-time
|
|
52
|
+
* `resolvedAspectRatio` derivation.
|
|
53
|
+
*
|
|
54
|
+
* No local React state, no refs, no synchronization effects — the
|
|
55
|
+
* composite store is the single source of truth.
|
|
56
|
+
*
|
|
57
|
+
* @param args
|
|
58
|
+
* @param args.aspectRatioPresets Optional caller-supplied aspect-ratio presets.
|
|
59
|
+
*/
|
|
60
|
+
export function useCropOptions( {
|
|
61
|
+
aspectRatioPresets,
|
|
62
|
+
}: UseCropOptionsArgs = {} ): UseCropOptionsReturn {
|
|
63
|
+
const controller = useMediaEditor();
|
|
64
|
+
const { aspectRatioValue, freeformCrop } = controller.cropOptions;
|
|
65
|
+
const cropperImage = controller.state.image;
|
|
66
|
+
|
|
67
|
+
const aspectRatioOptions = useMemo(
|
|
68
|
+
() => getAspectRatioOptions( aspectRatioPresets ),
|
|
69
|
+
[ aspectRatioPresets ]
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const resolvedAspectRatio = useMemo(
|
|
73
|
+
() => resolveAspectRatio( aspectRatioValue, cropperImage ),
|
|
74
|
+
[ aspectRatioValue, cropperImage ]
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
// Sidebar UX rule: picking Free auto-enables Resize-crop (freeform)
|
|
78
|
+
// when it's currently off — picking Free implies the user wants to
|
|
79
|
+
// freeform-edit, and there'd otherwise be no visible affordance for
|
|
80
|
+
// it. Wrapped in a gesture so the two dispatches collapse into a
|
|
81
|
+
// single undo step.
|
|
82
|
+
const { beginGesture, endGesture, setAspectRatioValue, setFreeformCrop } =
|
|
83
|
+
controller;
|
|
84
|
+
const setAspectRatioValueWithFreeformSync = useCallback(
|
|
85
|
+
( value: string ) => {
|
|
86
|
+
const shouldReenableFreeform =
|
|
87
|
+
value === FREE_ASPECT_RATIO_VALUE && ! freeformCrop;
|
|
88
|
+
if ( ! shouldReenableFreeform ) {
|
|
89
|
+
setAspectRatioValue( value );
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
beginGesture();
|
|
93
|
+
setAspectRatioValue( value );
|
|
94
|
+
setFreeformCrop( true );
|
|
95
|
+
endGesture();
|
|
96
|
+
},
|
|
97
|
+
[
|
|
98
|
+
freeformCrop,
|
|
99
|
+
beginGesture,
|
|
100
|
+
endGesture,
|
|
101
|
+
setAspectRatioValue,
|
|
102
|
+
setFreeformCrop,
|
|
103
|
+
]
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
aspectRatioValue,
|
|
108
|
+
setAspectRatioValue: setAspectRatioValueWithFreeformSync,
|
|
109
|
+
aspectRatioOptions,
|
|
110
|
+
freeformCrop,
|
|
111
|
+
setFreeformCrop: controller.setFreeformCrop,
|
|
112
|
+
resolvedAspectRatio,
|
|
113
|
+
resetCropOptions: controller.resetCropOptions,
|
|
114
|
+
};
|
|
115
|
+
}
|