@wordpress/media-editor 0.9.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
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import apiFetch from '@wordpress/api-fetch';
|
|
5
|
+
import { useDispatch, useRegistry } from '@wordpress/data';
|
|
6
|
+
import { store as coreStore } from '@wordpress/core-data';
|
|
7
|
+
import { useCallback, useState } from '@wordpress/element';
|
|
8
|
+
import { __, sprintf } from '@wordpress/i18n';
|
|
9
|
+
import { store as noticesStore } from '@wordpress/notices';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Internal dependencies
|
|
13
|
+
*/
|
|
14
|
+
import type { Media } from '../media-editor-provider';
|
|
15
|
+
import type { MediaEditorController } from '../../state';
|
|
16
|
+
import {
|
|
17
|
+
buildModifiers,
|
|
18
|
+
type Modifier,
|
|
19
|
+
} from '../media-editor-modal/build-modifiers';
|
|
20
|
+
|
|
21
|
+
// Details-tab edits are bundled into transformed `/edit` requests. Core's
|
|
22
|
+
// endpoint only accepts this whitelist.
|
|
23
|
+
const METADATA_EDIT_KEYS = [
|
|
24
|
+
'title',
|
|
25
|
+
'caption',
|
|
26
|
+
'description',
|
|
27
|
+
'alt_text',
|
|
28
|
+
'post',
|
|
29
|
+
] as const;
|
|
30
|
+
|
|
31
|
+
// Scope media editor snackbars so they don't leak into the host editor/page.
|
|
32
|
+
export const MEDIA_EDITOR_NOTICES_CONTEXT = 'media-editor';
|
|
33
|
+
|
|
34
|
+
type PendingMetadataEdits = Record< string, unknown > | undefined;
|
|
35
|
+
|
|
36
|
+
export interface MediaEditorSaveResult {
|
|
37
|
+
id: number;
|
|
38
|
+
url?: string;
|
|
39
|
+
media: Media;
|
|
40
|
+
previous?: {
|
|
41
|
+
id: number;
|
|
42
|
+
url?: string;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface UseSaveMediaEditorArgs {
|
|
47
|
+
cropper: MediaEditorController;
|
|
48
|
+
id: number;
|
|
49
|
+
isImage: boolean;
|
|
50
|
+
media?: Media | null;
|
|
51
|
+
onSaved?: ( result: MediaEditorSaveResult ) => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface UseSaveMediaEditorReturn {
|
|
55
|
+
isSaving: boolean;
|
|
56
|
+
save: () => Promise< void >;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getCropModifiers( cropper: MediaEditorController ): Modifier[] {
|
|
60
|
+
if ( ! cropper.isCropperDirty || ! cropper.state.image ) {
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
return buildModifiers( cropper.state, {
|
|
64
|
+
width: cropper.state.image.naturalWidth,
|
|
65
|
+
height: cropper.state.image.naturalHeight,
|
|
66
|
+
} );
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function getMetadataEdits(
|
|
70
|
+
pendingEdits: PendingMetadataEdits,
|
|
71
|
+
media?: Media | null
|
|
72
|
+
): Record< string, unknown > {
|
|
73
|
+
const metadataEdits: Record< string, unknown > = {};
|
|
74
|
+
for ( const key of METADATA_EDIT_KEYS ) {
|
|
75
|
+
if ( pendingEdits && key in pendingEdits ) {
|
|
76
|
+
metadataEdits[ key ] = pendingEdits[ key ];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// The `/edit` endpoint creates a new attachment for the crop and doesn't
|
|
80
|
+
// inherit `post_parent` from the source (unlike title/caption/etc.), so
|
|
81
|
+
// carry the existing value across when the user hasn't explicitly edited
|
|
82
|
+
// it. Use a defined-check so an explicit `0` (unattached) is preserved.
|
|
83
|
+
if ( ! ( 'post' in metadataEdits ) && media?.post !== undefined ) {
|
|
84
|
+
metadataEdits.post = media.post;
|
|
85
|
+
}
|
|
86
|
+
return metadataEdits;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function useSaveMediaEditor( {
|
|
90
|
+
cropper,
|
|
91
|
+
id,
|
|
92
|
+
isImage,
|
|
93
|
+
media,
|
|
94
|
+
onSaved,
|
|
95
|
+
}: UseSaveMediaEditorArgs ): UseSaveMediaEditorReturn {
|
|
96
|
+
const registry = useRegistry();
|
|
97
|
+
const {
|
|
98
|
+
clearEntityRecordEdits,
|
|
99
|
+
receiveEntityRecords,
|
|
100
|
+
saveEditedEntityRecord,
|
|
101
|
+
} = useDispatch( coreStore );
|
|
102
|
+
const { createErrorNotice, removeAllNotices } = useDispatch( noticesStore );
|
|
103
|
+
const [ isSaving, setIsSaving ] = useState( false );
|
|
104
|
+
|
|
105
|
+
const save = useCallback( async () => {
|
|
106
|
+
removeAllNotices( 'snackbar', MEDIA_EDITOR_NOTICES_CONTEXT );
|
|
107
|
+
setIsSaving( true );
|
|
108
|
+
try {
|
|
109
|
+
let saved: Media | null | undefined;
|
|
110
|
+
const modifiers = getCropModifiers( cropper );
|
|
111
|
+
const previous =
|
|
112
|
+
modifiers.length > 0 && media
|
|
113
|
+
? {
|
|
114
|
+
id,
|
|
115
|
+
url: media.source_url,
|
|
116
|
+
}
|
|
117
|
+
: undefined;
|
|
118
|
+
|
|
119
|
+
if ( modifiers.length > 0 ) {
|
|
120
|
+
const pendingEdits = registry
|
|
121
|
+
.select( coreStore )
|
|
122
|
+
.getEntityRecordNonTransientEdits(
|
|
123
|
+
'postType',
|
|
124
|
+
'attachment',
|
|
125
|
+
id
|
|
126
|
+
) as PendingMetadataEdits;
|
|
127
|
+
const metadataEdits = getMetadataEdits( pendingEdits, media );
|
|
128
|
+
|
|
129
|
+
saved = ( await apiFetch( {
|
|
130
|
+
path: `/wp/v2/media/${ id }/edit`,
|
|
131
|
+
method: 'POST',
|
|
132
|
+
data: {
|
|
133
|
+
src: media?.source_url,
|
|
134
|
+
modifiers,
|
|
135
|
+
...metadataEdits,
|
|
136
|
+
},
|
|
137
|
+
} ) ) as Media;
|
|
138
|
+
|
|
139
|
+
if ( saved ) {
|
|
140
|
+
receiveEntityRecords(
|
|
141
|
+
'postType',
|
|
142
|
+
'attachment',
|
|
143
|
+
saved,
|
|
144
|
+
undefined,
|
|
145
|
+
true
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
saved = ( await saveEditedEntityRecord(
|
|
150
|
+
'postType',
|
|
151
|
+
'attachment',
|
|
152
|
+
id
|
|
153
|
+
) ) as Media | undefined;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const next = ( saved ?? media ) as Media | null;
|
|
157
|
+
|
|
158
|
+
if ( next && next.id !== id ) {
|
|
159
|
+
clearEntityRecordEdits( 'postType', 'attachment', id );
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if ( next && next.id ) {
|
|
163
|
+
if ( next.id === id ) {
|
|
164
|
+
cropper.reset();
|
|
165
|
+
}
|
|
166
|
+
onSaved?.( {
|
|
167
|
+
id: next.id,
|
|
168
|
+
url: next.source_url,
|
|
169
|
+
media: next,
|
|
170
|
+
previous,
|
|
171
|
+
} );
|
|
172
|
+
}
|
|
173
|
+
} catch ( error ) {
|
|
174
|
+
const message =
|
|
175
|
+
error instanceof Error
|
|
176
|
+
? error.message
|
|
177
|
+
: ( error as { message?: string } )?.message ??
|
|
178
|
+
__( 'An unknown error occurred.' );
|
|
179
|
+
createErrorNotice(
|
|
180
|
+
isImage
|
|
181
|
+
? sprintf(
|
|
182
|
+
/* translators: %s: Error message. */
|
|
183
|
+
__( 'Could not save image. %s' ),
|
|
184
|
+
message
|
|
185
|
+
)
|
|
186
|
+
: sprintf(
|
|
187
|
+
/* translators: %s: Error message. */
|
|
188
|
+
__( 'Could not save media. %s' ),
|
|
189
|
+
message
|
|
190
|
+
),
|
|
191
|
+
{
|
|
192
|
+
type: 'snackbar',
|
|
193
|
+
context: MEDIA_EDITOR_NOTICES_CONTEXT,
|
|
194
|
+
}
|
|
195
|
+
);
|
|
196
|
+
} finally {
|
|
197
|
+
setIsSaving( false );
|
|
198
|
+
}
|
|
199
|
+
}, [
|
|
200
|
+
clearEntityRecordEdits,
|
|
201
|
+
createErrorNotice,
|
|
202
|
+
cropper,
|
|
203
|
+
id,
|
|
204
|
+
isImage,
|
|
205
|
+
media,
|
|
206
|
+
onSaved,
|
|
207
|
+
receiveEntityRecords,
|
|
208
|
+
registry,
|
|
209
|
+
removeAllNotices,
|
|
210
|
+
saveEditedEntityRecord,
|
|
211
|
+
] );
|
|
212
|
+
|
|
213
|
+
return { isSaving, save };
|
|
214
|
+
}
|
|
@@ -1,15 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { useCallback, useEffect, useMemo } from '@wordpress/element';
|
|
5
|
+
|
|
1
6
|
/**
|
|
2
7
|
* Internal dependencies
|
|
3
8
|
*/
|
|
4
9
|
import { useMediaEditorContext } from '../media-editor-provider';
|
|
5
10
|
import { getMediaTypeFromMimeType } from '../../utils';
|
|
6
|
-
import { Cropper
|
|
11
|
+
import { Cropper } from '../../image-editor';
|
|
12
|
+
import { useMediaEditor, resolveAspectRatio } from '../../state';
|
|
7
13
|
|
|
8
14
|
export interface MediaEditorCanvasProps {
|
|
9
|
-
/** Fixed aspect ratio (width / height). `undefined` means free. */
|
|
10
|
-
aspectRatio?: number;
|
|
11
|
-
/** Enable freeform crop mode (resize handles). */
|
|
12
|
-
freeformCrop?: boolean;
|
|
13
15
|
/** Focus the crop area when the canvas mounts. */
|
|
14
16
|
focusOnMount?: boolean;
|
|
15
17
|
/** Whether external placement activity should reveal the grid. */
|
|
@@ -21,33 +23,72 @@ export interface MediaEditorCanvasProps {
|
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
/**
|
|
24
|
-
* Editing surface for image media in the media editor modal. Pulls
|
|
25
|
-
* cropper controller from the surrounding `<
|
|
26
|
-
* bottom bar and Crop sidebar tab share the same state.
|
|
26
|
+
* Editing surface for image media in the media editor modal. Pulls
|
|
27
|
+
* its cropper controller from the surrounding `<MediaEditorStateProvider>`
|
|
28
|
+
* so the bottom bar and Crop sidebar tab share the same state.
|
|
27
29
|
*
|
|
28
30
|
* Returns `null` for missing or non-image media so the modal's outer
|
|
29
31
|
* guards can render a spinner or fall through to `<MediaPreview>`.
|
|
32
|
+
*
|
|
30
33
|
* @param props
|
|
31
|
-
* @param props.aspectRatio
|
|
32
|
-
* @param props.freeformCrop
|
|
33
34
|
* @param props.focusOnMount
|
|
34
35
|
* @param props.isPlacementActive
|
|
35
36
|
* @param props.onGestureStart
|
|
36
37
|
* @param props.onGestureEnd
|
|
37
38
|
*/
|
|
38
39
|
export default function MediaEditorCanvas( {
|
|
39
|
-
aspectRatio,
|
|
40
|
-
freeformCrop,
|
|
41
40
|
focusOnMount,
|
|
42
41
|
isPlacementActive = false,
|
|
43
42
|
onGestureStart,
|
|
44
43
|
onGestureEnd,
|
|
45
44
|
}: MediaEditorCanvasProps ) {
|
|
46
45
|
const { media } = useMediaEditorContext();
|
|
47
|
-
const controller =
|
|
46
|
+
const controller = useMediaEditor();
|
|
47
|
+
const { aspectRatioValue, freeformCrop } = controller.cropOptions;
|
|
48
|
+
const cropperImage = controller.state.image;
|
|
49
|
+
const { beginGesture, endGesture, setImage } = controller;
|
|
50
|
+
|
|
51
|
+
// Resolved aspect ratio is derived from the preset key + the
|
|
52
|
+
// loaded image (for the "Original" preset). The reducer doesn't
|
|
53
|
+
// store this number — only the preset key — so it's a render-time
|
|
54
|
+
// derivation here.
|
|
55
|
+
const aspectRatio = useMemo(
|
|
56
|
+
() => resolveAspectRatio( aspectRatioValue, cropperImage ),
|
|
57
|
+
[ aspectRatioValue, cropperImage ]
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const handleGestureStart = useCallback( () => {
|
|
61
|
+
beginGesture();
|
|
62
|
+
onGestureStart?.();
|
|
63
|
+
}, [ beginGesture, onGestureStart ] );
|
|
64
|
+
|
|
65
|
+
const handleGestureEnd = useCallback( () => {
|
|
66
|
+
endGesture();
|
|
67
|
+
onGestureEnd?.();
|
|
68
|
+
}, [ endGesture, onGestureEnd ] );
|
|
48
69
|
|
|
49
70
|
const mediaUrl = media?.source_url;
|
|
50
71
|
const mediaType = getMediaTypeFromMimeType( media?.mime_type );
|
|
72
|
+
const mediaWidth = Number( media?.media_details?.width );
|
|
73
|
+
const mediaHeight = Number( media?.media_details?.height );
|
|
74
|
+
|
|
75
|
+
useEffect( () => {
|
|
76
|
+
if (
|
|
77
|
+
cropperImage ||
|
|
78
|
+
! mediaUrl ||
|
|
79
|
+
! Number.isFinite( mediaWidth ) ||
|
|
80
|
+
! Number.isFinite( mediaHeight ) ||
|
|
81
|
+
mediaWidth <= 0 ||
|
|
82
|
+
mediaHeight <= 0
|
|
83
|
+
) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
setImage( {
|
|
87
|
+
src: mediaUrl,
|
|
88
|
+
naturalWidth: mediaWidth,
|
|
89
|
+
naturalHeight: mediaHeight,
|
|
90
|
+
} );
|
|
91
|
+
}, [ cropperImage, mediaUrl, mediaWidth, mediaHeight, setImage ] );
|
|
51
92
|
|
|
52
93
|
if ( ! mediaUrl || mediaType.type !== 'image' ) {
|
|
53
94
|
return null;
|
|
@@ -63,17 +104,8 @@ export default function MediaEditorCanvas( {
|
|
|
63
104
|
focusOnMount={ focusOnMount }
|
|
64
105
|
showGrid="interactive"
|
|
65
106
|
isPlacementActive={ isPlacementActive }
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
// step before the canvas gesture begins.
|
|
69
|
-
onGestureStart={ () => {
|
|
70
|
-
onGestureStart?.();
|
|
71
|
-
controller.commitHistory();
|
|
72
|
-
} }
|
|
73
|
-
onGestureEnd={ () => {
|
|
74
|
-
controller.commitHistory();
|
|
75
|
-
onGestureEnd?.();
|
|
76
|
-
} }
|
|
107
|
+
onGestureStart={ handleGestureStart }
|
|
108
|
+
onGestureEnd={ handleGestureEnd }
|
|
77
109
|
/>
|
|
78
110
|
</div>
|
|
79
111
|
);
|
|
@@ -12,14 +12,13 @@ import { __, sprintf } from '@wordpress/i18n';
|
|
|
12
12
|
/**
|
|
13
13
|
* Internal dependencies
|
|
14
14
|
*/
|
|
15
|
-
import {
|
|
16
|
-
import { useCropGestureHandlers } from '../../hooks/use-crop-gesture-handlers';
|
|
15
|
+
import { useMediaEditor } from '../../state';
|
|
17
16
|
import {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
} from '../../image-editor/core/
|
|
17
|
+
useCropGestureHandlers,
|
|
18
|
+
CROP_CONTROL_ATTR,
|
|
19
|
+
} from '../../hooks/use-crop-gesture-handlers';
|
|
20
|
+
import { MAX_ZOOM } from '../../image-editor/core/constants';
|
|
21
|
+
import { getMinZoom } from '../../image-editor/core/containment';
|
|
23
22
|
import type { AspectRatioPreset } from '../../image-editor/core/constants';
|
|
24
23
|
|
|
25
24
|
export interface MediaEditorCropPanelProps {
|
|
@@ -37,36 +36,8 @@ export interface MediaEditorCropPanelProps {
|
|
|
37
36
|
onFreeformChange: ( value: boolean ) => void;
|
|
38
37
|
/** Signal that a placement-oriented control is being adjusted. */
|
|
39
38
|
onPlacementControlInteraction?: () => void;
|
|
40
|
-
/**
|
|
41
|
-
|
|
42
|
-
* omitted, the media editor's default fixed-ratio presets are used.
|
|
43
|
-
*/
|
|
44
|
-
aspectRatioPresets?: AspectRatioPreset[];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Resolve an aspect-ratio preset value into a number suitable for
|
|
49
|
-
* `<Cropper aspectRatio=...>`. Returns `undefined` for Free (no lock).
|
|
50
|
-
*
|
|
51
|
-
* @param value Preset value as a string.
|
|
52
|
-
* @param imageAspectRatio Image's natural width / height — used for
|
|
53
|
-
* the Original preset.
|
|
54
|
-
*/
|
|
55
|
-
export function resolveAspectRatio(
|
|
56
|
-
value: string,
|
|
57
|
-
imageAspectRatio: number | null
|
|
58
|
-
): number | undefined {
|
|
59
|
-
const num = parseFloat( value );
|
|
60
|
-
if ( num === 0 ) {
|
|
61
|
-
return undefined;
|
|
62
|
-
}
|
|
63
|
-
if ( num === ORIGINAL_ASPECT_RATIO && imageAspectRatio ) {
|
|
64
|
-
return imageAspectRatio;
|
|
65
|
-
}
|
|
66
|
-
if ( num > 0 ) {
|
|
67
|
-
return num;
|
|
68
|
-
}
|
|
69
|
-
return undefined;
|
|
39
|
+
/** Aspect-ratio presets to display in the selector. */
|
|
40
|
+
aspectRatioOptions: AspectRatioPreset[];
|
|
70
41
|
}
|
|
71
42
|
|
|
72
43
|
/**
|
|
@@ -79,7 +50,7 @@ export function resolveAspectRatio(
|
|
|
79
50
|
* @param props.freeformCrop
|
|
80
51
|
* @param props.onFreeformChange
|
|
81
52
|
* @param props.onPlacementControlInteraction
|
|
82
|
-
* @param props.
|
|
53
|
+
* @param props.aspectRatioOptions
|
|
83
54
|
*/
|
|
84
55
|
export default function MediaEditorCropPanel( {
|
|
85
56
|
aspectRatioValue,
|
|
@@ -87,61 +58,54 @@ export default function MediaEditorCropPanel( {
|
|
|
87
58
|
freeformCrop,
|
|
88
59
|
onFreeformChange,
|
|
89
60
|
onPlacementControlInteraction,
|
|
90
|
-
|
|
61
|
+
aspectRatioOptions,
|
|
91
62
|
}: MediaEditorCropPanelProps ) {
|
|
92
|
-
const { state, setZoom } =
|
|
63
|
+
const { state, setZoom } = useMediaEditor();
|
|
93
64
|
const zoomGestureHandlers = useCropGestureHandlers();
|
|
94
|
-
const
|
|
95
|
-
...DEFAULT_ASPECT_RATIOS.filter( ( preset ) => preset.value <= 0 ),
|
|
96
|
-
...( aspectRatioPresets ??
|
|
97
|
-
DEFAULT_ASPECT_RATIOS.filter( ( preset ) => preset.value > 0 ) ),
|
|
98
|
-
];
|
|
99
|
-
const handleAspectRatioChange = ( value: string ) => {
|
|
100
|
-
onAspectRatioChange( value );
|
|
101
|
-
if ( value === '0' && ! freeformCrop ) {
|
|
102
|
-
onFreeformChange( true );
|
|
103
|
-
}
|
|
104
|
-
};
|
|
65
|
+
const minZoom = getMinZoom( state );
|
|
105
66
|
|
|
106
67
|
return (
|
|
107
|
-
|
|
68
|
+
// Tag the whole panel as a crop-control region so the modal's
|
|
69
|
+
// Cmd+Z handler doesn't mistake the SelectControl / ToggleControl
|
|
70
|
+
// inputs for metadata fields (which would suppress undo).
|
|
71
|
+
<Stack
|
|
72
|
+
direction="column"
|
|
73
|
+
gap="md"
|
|
74
|
+
{ ...{ [ CROP_CONTROL_ATTR ]: true } }
|
|
75
|
+
>
|
|
108
76
|
<VisuallyHidden render={ <h2 /> }>
|
|
109
77
|
{ __( 'Crop options' ) }
|
|
110
78
|
</VisuallyHidden>
|
|
111
79
|
<SelectControl
|
|
112
80
|
__next40pxDefaultSize
|
|
113
|
-
__nextHasNoMarginBottom
|
|
114
81
|
label={ __( 'Aspect ratio' ) }
|
|
115
82
|
value={ aspectRatioValue }
|
|
116
|
-
onChange={
|
|
83
|
+
onChange={ onAspectRatioChange }
|
|
117
84
|
options={ aspectRatioOptions.map( ( preset ) => ( {
|
|
118
85
|
label: preset.label,
|
|
119
86
|
value: preset.value.toString(),
|
|
120
87
|
} ) ) }
|
|
121
88
|
/>
|
|
122
89
|
<ToggleControl
|
|
123
|
-
|
|
124
|
-
label={ __( 'Resize crop area' ) }
|
|
125
|
-
help={ __( 'Show handles to adjust the crop box.' ) }
|
|
90
|
+
label={ __( 'Show resize handles' ) }
|
|
126
91
|
checked={ freeformCrop }
|
|
127
92
|
onChange={ onFreeformChange }
|
|
128
93
|
/>
|
|
129
94
|
<div role="presentation" { ...zoomGestureHandlers }>
|
|
130
95
|
<RangeControl
|
|
131
96
|
__next40pxDefaultSize
|
|
132
|
-
__nextHasNoMarginBottom
|
|
133
97
|
label={ __( 'Zoom' ) }
|
|
134
|
-
min={
|
|
98
|
+
min={ minZoom }
|
|
135
99
|
max={ MAX_ZOOM }
|
|
136
100
|
step={ 0.1 }
|
|
137
101
|
value={ state.zoom }
|
|
138
102
|
onChange={ ( value ) => {
|
|
139
103
|
onPlacementControlInteraction?.();
|
|
140
|
-
setZoom( typeof value === 'number' ? value :
|
|
104
|
+
setZoom( typeof value === 'number' ? value : minZoom );
|
|
141
105
|
} }
|
|
142
106
|
renderTooltipContent={ ( value ) => {
|
|
143
107
|
const zoom =
|
|
144
|
-
typeof value === 'number' ? value :
|
|
108
|
+
typeof value === 'number' ? value : minZoom;
|
|
145
109
|
return sprintf(
|
|
146
110
|
/* translators: %d: zoom level as a percentage. */
|
|
147
111
|
__( '%d%%' ),
|
|
@@ -8,7 +8,7 @@ import { fireEvent, render, screen } from '@testing-library/react';
|
|
|
8
8
|
*/
|
|
9
9
|
import MediaEditorCropPanel from '..';
|
|
10
10
|
import type { MediaEditorCropPanelProps } from '..';
|
|
11
|
-
import {
|
|
11
|
+
import { MediaEditorStateProvider } from '../../../state';
|
|
12
12
|
|
|
13
13
|
function setupCropPanel(
|
|
14
14
|
overrides: Partial< MediaEditorCropPanelProps > = {}
|
|
@@ -18,13 +18,18 @@ function setupCropPanel(
|
|
|
18
18
|
onAspectRatioChange: jest.fn(),
|
|
19
19
|
freeformCrop: false,
|
|
20
20
|
onFreeformChange: jest.fn(),
|
|
21
|
+
aspectRatioOptions: [
|
|
22
|
+
{ label: 'Free', value: 0 },
|
|
23
|
+
{ label: 'Original', value: -1 },
|
|
24
|
+
{ label: 'Square', value: 1 },
|
|
25
|
+
],
|
|
21
26
|
...overrides,
|
|
22
27
|
};
|
|
23
28
|
|
|
24
29
|
render(
|
|
25
|
-
<
|
|
30
|
+
<MediaEditorStateProvider>
|
|
26
31
|
<MediaEditorCropPanel { ...props } />
|
|
27
|
-
</
|
|
32
|
+
</MediaEditorStateProvider>
|
|
28
33
|
);
|
|
29
34
|
|
|
30
35
|
return props;
|
|
@@ -41,17 +46,14 @@ describe( 'MediaEditorCropPanel', () => {
|
|
|
41
46
|
setupCropPanel();
|
|
42
47
|
|
|
43
48
|
const aspectRatio = screen.getByLabelText( 'Aspect ratio' );
|
|
44
|
-
const resizeCropArea = screen.getByLabelText( '
|
|
49
|
+
const resizeCropArea = screen.getByLabelText( 'Show resize handles' );
|
|
45
50
|
const zoom = screen.getByRole( 'slider', { name: 'Zoom' } );
|
|
46
51
|
|
|
47
|
-
expect(
|
|
48
|
-
screen.getByText( 'Show handles to adjust the crop box.' )
|
|
49
|
-
).toBeInTheDocument();
|
|
50
52
|
expectElementBefore( aspectRatio, resizeCropArea );
|
|
51
53
|
expectElementBefore( resizeCropArea, zoom );
|
|
52
54
|
} );
|
|
53
55
|
|
|
54
|
-
it( '
|
|
56
|
+
it( 'passes selected aspect ratio changes to the caller', () => {
|
|
55
57
|
const controls = setupCropPanel( {
|
|
56
58
|
aspectRatioValue: '1',
|
|
57
59
|
freeformCrop: false,
|
|
@@ -61,49 +63,21 @@ describe( 'MediaEditorCropPanel', () => {
|
|
|
61
63
|
target: { value: '0' },
|
|
62
64
|
} );
|
|
63
65
|
|
|
64
|
-
expect( controls.onAspectRatioChange ).
|
|
65
|
-
expect(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
it( 'does not change freeform crop when a fixed ratio is selected', () => {
|
|
69
|
-
const controls = setupCropPanel( {
|
|
70
|
-
aspectRatioValue: '0',
|
|
71
|
-
freeformCrop: false,
|
|
72
|
-
} );
|
|
73
|
-
|
|
74
|
-
fireEvent.change( screen.getByLabelText( 'Aspect ratio' ), {
|
|
75
|
-
target: { value: '1' },
|
|
76
|
-
} );
|
|
77
|
-
|
|
78
|
-
expect( controls.onAspectRatioChange ).toHaveBeenCalledWith( '1' );
|
|
79
|
-
expect( controls.onFreeformChange ).not.toHaveBeenCalled();
|
|
80
|
-
} );
|
|
81
|
-
|
|
82
|
-
it( 'does not call onFreeformChange when Free is selected and handles are already on', () => {
|
|
83
|
-
const controls = setupCropPanel( {
|
|
84
|
-
aspectRatioValue: '1',
|
|
85
|
-
freeformCrop: true,
|
|
86
|
-
} );
|
|
87
|
-
|
|
88
|
-
fireEvent.change( screen.getByLabelText( 'Aspect ratio' ), {
|
|
89
|
-
target: { value: '0' },
|
|
90
|
-
} );
|
|
91
|
-
|
|
92
|
-
expect( controls.onAspectRatioChange ).toHaveBeenCalledWith( '0' );
|
|
66
|
+
expect( controls.onAspectRatioChange ).toHaveBeenCalled();
|
|
67
|
+
expect(
|
|
68
|
+
( controls.onAspectRatioChange as jest.Mock ).mock.calls[ 0 ][ 0 ]
|
|
69
|
+
).toBe( '0' );
|
|
93
70
|
expect( controls.onFreeformChange ).not.toHaveBeenCalled();
|
|
94
71
|
} );
|
|
95
72
|
|
|
96
|
-
it( '
|
|
73
|
+
it( 'passes resize-handle changes to the caller', () => {
|
|
97
74
|
const controls = setupCropPanel( {
|
|
98
75
|
aspectRatioValue: '0',
|
|
99
76
|
freeformCrop: true,
|
|
100
77
|
} );
|
|
101
78
|
|
|
102
|
-
fireEvent.
|
|
103
|
-
target: { value: '1' },
|
|
104
|
-
} );
|
|
79
|
+
fireEvent.click( screen.getByLabelText( 'Show resize handles' ) );
|
|
105
80
|
|
|
106
|
-
expect( controls.
|
|
107
|
-
expect( controls.onFreeformChange ).not.toHaveBeenCalled();
|
|
81
|
+
expect( controls.onFreeformChange ).toHaveBeenCalledWith( false );
|
|
108
82
|
} );
|
|
109
83
|
} );
|
|
@@ -4,7 +4,10 @@
|
|
|
4
4
|
import { Modal } from '@wordpress/components';
|
|
5
5
|
import { useDispatch, useSelect } from '@wordpress/data';
|
|
6
6
|
import { __ } from '@wordpress/i18n';
|
|
7
|
+
import { ShortcutProvider } from '@wordpress/keyboard-shortcuts';
|
|
8
|
+
import { store as noticesStore } from '@wordpress/notices';
|
|
7
9
|
import type { Field } from '@wordpress/dataviews';
|
|
10
|
+
import type { KeyboardEvent as ReactKeyboardEvent } from 'react';
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
13
|
* Internal dependencies
|
|
@@ -44,6 +47,7 @@ export function MediaEditorModal( {
|
|
|
44
47
|
}, [] );
|
|
45
48
|
|
|
46
49
|
const { closeMediaEditorModal } = useDispatch( mediaEditorStore );
|
|
50
|
+
const { createSuccessNotice } = useDispatch( noticesStore );
|
|
47
51
|
|
|
48
52
|
if ( ! isModalOpen || ! id ) {
|
|
49
53
|
return null;
|
|
@@ -52,6 +56,16 @@ export function MediaEditorModal( {
|
|
|
52
56
|
const portalElement =
|
|
53
57
|
typeof document === 'undefined' ? null : document.body;
|
|
54
58
|
|
|
59
|
+
// React synthetic events bubble through the React tree, not the DOM tree,
|
|
60
|
+
// so a host `ShortcutProvider` higher up still receives keydown events
|
|
61
|
+
// from inside this portaled modal. Stop propagation at the modal boundary
|
|
62
|
+
// so host shortcuts (undo/redo, save, etc.) don't fire from within.
|
|
63
|
+
const stopKeyDownPropagation = (
|
|
64
|
+
event: ReactKeyboardEvent< HTMLDivElement >
|
|
65
|
+
) => {
|
|
66
|
+
event.stopPropagation();
|
|
67
|
+
};
|
|
68
|
+
|
|
55
69
|
return (
|
|
56
70
|
<MediaEditor
|
|
57
71
|
id={ id }
|
|
@@ -62,7 +76,7 @@ export function MediaEditorModal( {
|
|
|
62
76
|
noticesClassName="media-editor-modal__snackbar"
|
|
63
77
|
noticesPortalElement={ portalElement }
|
|
64
78
|
onClose={ closeMediaEditorModal }
|
|
65
|
-
onSaved={ ( { id: savedId, url } ) => {
|
|
79
|
+
onSaved={ ( { id: savedId, url, previous } ) => {
|
|
66
80
|
if ( savedId && onUpdate ) {
|
|
67
81
|
const update: MediaEditorModalUpdate = {
|
|
68
82
|
id: savedId,
|
|
@@ -71,6 +85,25 @@ export function MediaEditorModal( {
|
|
|
71
85
|
onUpdate( update );
|
|
72
86
|
}
|
|
73
87
|
closeMediaEditorModal();
|
|
88
|
+
if ( previous && savedId !== previous.id && onUpdate ) {
|
|
89
|
+
// Intentionally unscoped: the modal is closing, so the
|
|
90
|
+
// snackbar surfaces in the host editor (not the media
|
|
91
|
+
// editor's `MEDIA_EDITOR_NOTICES_CONTEXT` region).
|
|
92
|
+
createSuccessNotice( __( 'Image edited.' ), {
|
|
93
|
+
type: 'snackbar',
|
|
94
|
+
actions: [
|
|
95
|
+
{
|
|
96
|
+
label: __( 'Undo' ),
|
|
97
|
+
onClick: () => {
|
|
98
|
+
onUpdate( {
|
|
99
|
+
id: previous.id,
|
|
100
|
+
url: previous.url,
|
|
101
|
+
} );
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
} );
|
|
106
|
+
}
|
|
74
107
|
} }
|
|
75
108
|
renderFrame={ ( {
|
|
76
109
|
children,
|
|
@@ -79,18 +112,23 @@ export function MediaEditorModal( {
|
|
|
79
112
|
onKeyDown,
|
|
80
113
|
shouldCloseOnClickOutside,
|
|
81
114
|
} ) => (
|
|
82
|
-
<
|
|
83
|
-
className="media-editor-
|
|
84
|
-
|
|
85
|
-
size="fill"
|
|
86
|
-
isDismissible={ false }
|
|
87
|
-
shouldCloseOnClickOutside={ shouldCloseOnClickOutside }
|
|
88
|
-
onKeyDown={ onKeyDown }
|
|
89
|
-
onRequestClose={ onRequestClose }
|
|
90
|
-
headerActions={ headerActions }
|
|
115
|
+
<ShortcutProvider
|
|
116
|
+
className="media-editor-modal__shortcut-scope"
|
|
117
|
+
onKeyDown={ stopKeyDownPropagation }
|
|
91
118
|
>
|
|
92
|
-
|
|
93
|
-
|
|
119
|
+
<Modal
|
|
120
|
+
className="media-editor-modal"
|
|
121
|
+
title={ __( 'Edit media' ) }
|
|
122
|
+
size="fill"
|
|
123
|
+
isDismissible={ false }
|
|
124
|
+
shouldCloseOnClickOutside={ shouldCloseOnClickOutside }
|
|
125
|
+
onKeyDown={ onKeyDown }
|
|
126
|
+
onRequestClose={ onRequestClose }
|
|
127
|
+
headerActions={ headerActions }
|
|
128
|
+
>
|
|
129
|
+
{ children }
|
|
130
|
+
</Modal>
|
|
131
|
+
</ShortcutProvider>
|
|
94
132
|
) }
|
|
95
133
|
/>
|
|
96
134
|
);
|