@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,298 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/media-editor/src/state/use-media-editor-state.ts
|
|
21
|
+
var use_media_editor_state_exports = {};
|
|
22
|
+
__export(use_media_editor_state_exports, {
|
|
23
|
+
resolveAspectRatio: () => resolveAspectRatio,
|
|
24
|
+
useMediaEditorState: () => useMediaEditorState
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(use_media_editor_state_exports);
|
|
27
|
+
var import_element = require("@wordpress/element");
|
|
28
|
+
var import_image_editor = require("../image-editor/index.cjs");
|
|
29
|
+
var import_composite_reducer = require("./composite-reducer.cjs");
|
|
30
|
+
function resolveAspectRatio(presetKey, cropperImage) {
|
|
31
|
+
const num = parseFloat(presetKey);
|
|
32
|
+
if (num === 0 || Number.isNaN(num)) {
|
|
33
|
+
return void 0;
|
|
34
|
+
}
|
|
35
|
+
if (num === import_image_editor.ORIGINAL_ASPECT_RATIO) {
|
|
36
|
+
if (cropperImage && cropperImage.naturalWidth > 0 && cropperImage.naturalHeight > 0) {
|
|
37
|
+
return cropperImage.naturalWidth / cropperImage.naturalHeight;
|
|
38
|
+
}
|
|
39
|
+
return void 0;
|
|
40
|
+
}
|
|
41
|
+
if (num > 0) {
|
|
42
|
+
return num;
|
|
43
|
+
}
|
|
44
|
+
return void 0;
|
|
45
|
+
}
|
|
46
|
+
function areCropperImagesEqual(a, b) {
|
|
47
|
+
return a?.src === b?.src && a?.naturalWidth === b?.naturalWidth && a?.naturalHeight === b?.naturalHeight;
|
|
48
|
+
}
|
|
49
|
+
function useMediaEditorState(initialState) {
|
|
50
|
+
const [state, dispatch] = (0, import_element.useReducer)(
|
|
51
|
+
import_composite_reducer.mediaEditorReducer,
|
|
52
|
+
null,
|
|
53
|
+
() => (0, import_composite_reducer.buildInitialMediaEditorState)(
|
|
54
|
+
(0, import_image_editor.enforceContainment)({
|
|
55
|
+
...import_image_editor.DEFAULT_STATE,
|
|
56
|
+
...initialState?.cropper
|
|
57
|
+
}),
|
|
58
|
+
initialState?.cropOptions
|
|
59
|
+
)
|
|
60
|
+
);
|
|
61
|
+
const [initialBaseline, setInitialBaseline] = (0, import_element.useState)(() => state);
|
|
62
|
+
const stateRef = (0, import_element.useRef)(state);
|
|
63
|
+
const visualSizeRef = (0, import_element.useRef)({ width: 0, height: 0 });
|
|
64
|
+
const historyRef = (0, import_element.useRef)([]);
|
|
65
|
+
const redoStackRef = (0, import_element.useRef)([]);
|
|
66
|
+
const [hasUndo, setHasUndo] = (0, import_element.useState)(false);
|
|
67
|
+
const [hasRedo, setHasRedo] = (0, import_element.useState)(false);
|
|
68
|
+
const isGestureOpenRef = (0, import_element.useRef)(false);
|
|
69
|
+
const gestureSnapshotRef = (0, import_element.useRef)(null);
|
|
70
|
+
const pushSnapshot = (0, import_element.useCallback)((snapshot) => {
|
|
71
|
+
const last = historyRef.current[historyRef.current.length - 1];
|
|
72
|
+
if (last && (0, import_composite_reducer.areMediaEditorStatesEqual)(last, snapshot)) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
historyRef.current = [...historyRef.current, snapshot];
|
|
76
|
+
redoStackRef.current = [];
|
|
77
|
+
setHasUndo(true);
|
|
78
|
+
setHasRedo(false);
|
|
79
|
+
}, []);
|
|
80
|
+
const dispatchWithHistory = (0, import_element.useCallback)(
|
|
81
|
+
(action, recordHistory = true) => {
|
|
82
|
+
const preState = stateRef.current;
|
|
83
|
+
const postState = (0, import_composite_reducer.mediaEditorReducer)(preState, action);
|
|
84
|
+
if (recordHistory) {
|
|
85
|
+
if (!(0, import_composite_reducer.areMediaEditorStatesEqual)(preState, postState)) {
|
|
86
|
+
if (isGestureOpenRef.current) {
|
|
87
|
+
if (gestureSnapshotRef.current === null) {
|
|
88
|
+
gestureSnapshotRef.current = preState;
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
pushSnapshot(preState);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
stateRef.current = postState;
|
|
96
|
+
dispatch(action);
|
|
97
|
+
},
|
|
98
|
+
[pushSnapshot]
|
|
99
|
+
);
|
|
100
|
+
const beginGesture = (0, import_element.useCallback)(() => {
|
|
101
|
+
if (isGestureOpenRef.current) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
isGestureOpenRef.current = true;
|
|
105
|
+
gestureSnapshotRef.current = null;
|
|
106
|
+
}, []);
|
|
107
|
+
const endGesture = (0, import_element.useCallback)(() => {
|
|
108
|
+
const snapshot = gestureSnapshotRef.current;
|
|
109
|
+
if (snapshot && !(0, import_composite_reducer.areMediaEditorStatesEqual)(snapshot, stateRef.current)) {
|
|
110
|
+
pushSnapshot(snapshot);
|
|
111
|
+
}
|
|
112
|
+
gestureSnapshotRef.current = null;
|
|
113
|
+
isGestureOpenRef.current = false;
|
|
114
|
+
}, [pushSnapshot]);
|
|
115
|
+
const dispatchCropperAction = (0, import_element.useCallback)(
|
|
116
|
+
(action) => {
|
|
117
|
+
dispatchWithHistory({ type: "CROPPER", action });
|
|
118
|
+
},
|
|
119
|
+
[dispatchWithHistory]
|
|
120
|
+
);
|
|
121
|
+
const cropperSetters = (0, import_element.useMemo)(
|
|
122
|
+
() => (0, import_image_editor.buildCropperSetters)(
|
|
123
|
+
dispatchCropperAction,
|
|
124
|
+
() => stateRef.current.cropper
|
|
125
|
+
),
|
|
126
|
+
[dispatchCropperAction]
|
|
127
|
+
);
|
|
128
|
+
const setImage = (0, import_element.useCallback)((image) => {
|
|
129
|
+
if (areCropperImagesEqual(stateRef.current.cropper.image, image)) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const action = {
|
|
133
|
+
type: "CROPPER",
|
|
134
|
+
action: { type: "SET_IMAGE", payload: image }
|
|
135
|
+
};
|
|
136
|
+
const next = (0, import_composite_reducer.mediaEditorReducer)(stateRef.current, action);
|
|
137
|
+
stateRef.current = next;
|
|
138
|
+
dispatch(action);
|
|
139
|
+
setInitialBaseline(next);
|
|
140
|
+
isGestureOpenRef.current = false;
|
|
141
|
+
gestureSnapshotRef.current = null;
|
|
142
|
+
historyRef.current = [];
|
|
143
|
+
redoStackRef.current = [];
|
|
144
|
+
setHasUndo(false);
|
|
145
|
+
setHasRedo(false);
|
|
146
|
+
}, []);
|
|
147
|
+
const reset = (0, import_element.useCallback)(
|
|
148
|
+
(resetState) => {
|
|
149
|
+
dispatchWithHistory({
|
|
150
|
+
type: "CROPPER",
|
|
151
|
+
action: { type: "RESET", payload: resetState }
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
[dispatchWithHistory]
|
|
155
|
+
);
|
|
156
|
+
const setAspectRatioValue = (0, import_element.useCallback)(
|
|
157
|
+
(presetKey) => {
|
|
158
|
+
const resolved = resolveAspectRatio(
|
|
159
|
+
presetKey,
|
|
160
|
+
stateRef.current.cropper.image
|
|
161
|
+
);
|
|
162
|
+
dispatchWithHistory({
|
|
163
|
+
type: "SET_ASPECT_RATIO_VALUE",
|
|
164
|
+
payload: {
|
|
165
|
+
presetKey,
|
|
166
|
+
resolved,
|
|
167
|
+
visualSize: visualSizeRef.current
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
[dispatchWithHistory]
|
|
172
|
+
);
|
|
173
|
+
const setFreeformCrop = (0, import_element.useCallback)(
|
|
174
|
+
(value) => {
|
|
175
|
+
dispatchWithHistory({
|
|
176
|
+
type: "SET_FREEFORM_CROP",
|
|
177
|
+
payload: value
|
|
178
|
+
});
|
|
179
|
+
},
|
|
180
|
+
[dispatchWithHistory]
|
|
181
|
+
);
|
|
182
|
+
const resetCropOptions = (0, import_element.useCallback)(() => {
|
|
183
|
+
dispatchWithHistory({ type: "RESET_CROP_OPTIONS" });
|
|
184
|
+
}, [dispatchWithHistory]);
|
|
185
|
+
const undo = (0, import_element.useCallback)(() => {
|
|
186
|
+
endGesture();
|
|
187
|
+
const prev = historyRef.current[historyRef.current.length - 1];
|
|
188
|
+
if (!prev) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
redoStackRef.current = [stateRef.current, ...redoStackRef.current];
|
|
192
|
+
historyRef.current = historyRef.current.slice(0, -1);
|
|
193
|
+
setHasUndo(historyRef.current.length > 0);
|
|
194
|
+
setHasRedo(true);
|
|
195
|
+
stateRef.current = prev;
|
|
196
|
+
dispatch({ type: "RESTORE_SNAPSHOT", payload: prev });
|
|
197
|
+
}, [endGesture]);
|
|
198
|
+
const redo = (0, import_element.useCallback)(() => {
|
|
199
|
+
endGesture();
|
|
200
|
+
const next = redoStackRef.current[0];
|
|
201
|
+
if (!next) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
historyRef.current = [...historyRef.current, stateRef.current];
|
|
205
|
+
redoStackRef.current = redoStackRef.current.slice(1);
|
|
206
|
+
setHasUndo(true);
|
|
207
|
+
setHasRedo(redoStackRef.current.length > 0);
|
|
208
|
+
stateRef.current = next;
|
|
209
|
+
dispatch({ type: "RESTORE_SNAPSHOT", payload: next });
|
|
210
|
+
}, [endGesture]);
|
|
211
|
+
const setVisualSize = (0, import_element.useCallback)((size) => {
|
|
212
|
+
visualSizeRef.current = size;
|
|
213
|
+
}, []);
|
|
214
|
+
const adjustCropRectForViewport = (0, import_element.useCallback)(
|
|
215
|
+
(rect) => {
|
|
216
|
+
dispatchWithHistory(
|
|
217
|
+
{ type: "VIEWPORT_ADJUST_CROP_RECT", payload: rect },
|
|
218
|
+
false
|
|
219
|
+
);
|
|
220
|
+
},
|
|
221
|
+
[dispatchWithHistory]
|
|
222
|
+
);
|
|
223
|
+
const isDirty = !(0, import_composite_reducer.areMediaEditorStatesEqual)(state, initialBaseline);
|
|
224
|
+
const isCropperDirty = !(0, import_image_editor.areCropperStatesEqual)(
|
|
225
|
+
state.cropper,
|
|
226
|
+
initialBaseline.cropper
|
|
227
|
+
);
|
|
228
|
+
const getCroppedImage = (0, import_element.useCallback)(
|
|
229
|
+
(mimeType, quality) => {
|
|
230
|
+
if (!state.cropper.image) {
|
|
231
|
+
return Promise.reject(
|
|
232
|
+
new Error("No image loaded \u2014 call setImage first.")
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
return (0, import_image_editor.exportCroppedImage)(
|
|
236
|
+
state.cropper.image.src,
|
|
237
|
+
state.cropper,
|
|
238
|
+
mimeType,
|
|
239
|
+
quality
|
|
240
|
+
);
|
|
241
|
+
},
|
|
242
|
+
[state.cropper]
|
|
243
|
+
);
|
|
244
|
+
const controller = (0, import_element.useMemo)(
|
|
245
|
+
() => ({
|
|
246
|
+
// CropperController surface (state is the cropper slice so a
|
|
247
|
+
// <Cropper> takes this controller as-is).
|
|
248
|
+
...cropperSetters,
|
|
249
|
+
state: state.cropper,
|
|
250
|
+
setImage,
|
|
251
|
+
reset,
|
|
252
|
+
isDirty,
|
|
253
|
+
getCroppedImage,
|
|
254
|
+
// Composite extensions
|
|
255
|
+
cropOptions: state.cropOptions,
|
|
256
|
+
setAspectRatioValue,
|
|
257
|
+
setFreeformCrop,
|
|
258
|
+
resetCropOptions,
|
|
259
|
+
isCropperDirty,
|
|
260
|
+
hasUndo,
|
|
261
|
+
hasRedo,
|
|
262
|
+
undo,
|
|
263
|
+
redo,
|
|
264
|
+
beginGesture,
|
|
265
|
+
endGesture,
|
|
266
|
+
setVisualSize,
|
|
267
|
+
adjustCropRectForViewport
|
|
268
|
+
}),
|
|
269
|
+
[
|
|
270
|
+
cropperSetters,
|
|
271
|
+
state.cropper,
|
|
272
|
+
setImage,
|
|
273
|
+
reset,
|
|
274
|
+
isDirty,
|
|
275
|
+
getCroppedImage,
|
|
276
|
+
state.cropOptions,
|
|
277
|
+
setAspectRatioValue,
|
|
278
|
+
setFreeformCrop,
|
|
279
|
+
resetCropOptions,
|
|
280
|
+
isCropperDirty,
|
|
281
|
+
hasUndo,
|
|
282
|
+
hasRedo,
|
|
283
|
+
undo,
|
|
284
|
+
redo,
|
|
285
|
+
beginGesture,
|
|
286
|
+
endGesture,
|
|
287
|
+
setVisualSize,
|
|
288
|
+
adjustCropRectForViewport
|
|
289
|
+
]
|
|
290
|
+
);
|
|
291
|
+
return controller;
|
|
292
|
+
}
|
|
293
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
294
|
+
0 && (module.exports = {
|
|
295
|
+
resolveAspectRatio,
|
|
296
|
+
useMediaEditorState
|
|
297
|
+
});
|
|
298
|
+
//# sourceMappingURL=use-media-editor-state.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/state/use-media-editor-state.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tuseCallback,\n\tuseMemo,\n\tuseReducer,\n\tuseRef,\n\tuseState,\n} from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport {\n\tDEFAULT_STATE,\n\tORIGINAL_ASPECT_RATIO,\n\tareCropperStatesEqual,\n\tbuildCropperSetters,\n\tenforceContainment,\n\texportCroppedImage,\n\ttype CropperAction,\n\ttype CropperController,\n\ttype CropperState,\n\ttype NormalizedRect,\n\ttype Size,\n} from '../image-editor';\nimport {\n\tmediaEditorReducer,\n\tareMediaEditorStatesEqual,\n\tbuildInitialMediaEditorState,\n} from './composite-reducer';\nimport type {\n\tCropOptionsSlice,\n\tMediaEditorAction,\n\tMediaEditorState,\n} from './types';\n\n/**\n * Resolve an aspect-ratio preset value (the string the dropdown\n * round-trips) into a numeric width/height ratio for the cropper.\n *\n * - `'0'` \u2192 `undefined` (Free; no lock)\n * - `'-1'` (Original) \u2192 image's natural width / height, or `undefined`\n * if no image is loaded.\n * - Any positive decimal \u2192 the parsed number.\n *\n * Centralizing here means the dropdown, the action dispatcher, and\n * the test harness all resolve the same way.\n *\n * @param presetKey Preset value from the dropdown.\n * @param cropperImage The cropper's loaded image, or `null`.\n * @return The resolved ratio, or `undefined` for Free / unresolvable.\n */\nexport function resolveAspectRatio(\n\tpresetKey: string,\n\tcropperImage: CropperState[ 'image' ] | null\n): number | undefined {\n\tconst num = parseFloat( presetKey );\n\tif ( num === 0 || Number.isNaN( num ) ) {\n\t\treturn undefined;\n\t}\n\tif ( num === ORIGINAL_ASPECT_RATIO ) {\n\t\tif (\n\t\t\tcropperImage &&\n\t\t\tcropperImage.naturalWidth > 0 &&\n\t\t\tcropperImage.naturalHeight > 0\n\t\t) {\n\t\t\treturn cropperImage.naturalWidth / cropperImage.naturalHeight;\n\t\t}\n\t\treturn undefined;\n\t}\n\tif ( num > 0 ) {\n\t\treturn num;\n\t}\n\treturn undefined;\n}\n\nfunction areCropperImagesEqual(\n\ta: CropperState[ 'image' ],\n\tb: CropperState[ 'image' ]\n): boolean {\n\treturn (\n\t\ta?.src === b?.src &&\n\t\ta?.naturalWidth === b?.naturalWidth &&\n\t\ta?.naturalHeight === b?.naturalHeight\n\t);\n}\n\n/**\n * The composite controller exposed by `useMediaEditorState`.\n *\n * Satisfies `CropperController` (so a `<Cropper>` can take this\n * controller directly via the `controller` prop) and adds slice\n * setters, undo/redo, gesture boundaries, and viewport reporting.\n */\nexport interface MediaEditorController extends CropperController {\n\t/** The cropOptions slice (preset key + freeform toggle). */\n\tcropOptions: CropOptionsSlice;\n\t/** Set the aspect-ratio preset. Atomic with the cropRect reshape. */\n\tsetAspectRatioValue: ( presetKey: string ) => void;\n\t/** Toggle the resize-handle (freeform) mode. */\n\tsetFreeformCrop: ( value: boolean ) => void;\n\t/** Reset cropOptions to defaults (Free + freeform on). */\n\tresetCropOptions: () => void;\n\t/**\n\t * Whether the cropper geometry differs from the initial baseline.\n\t * This excludes UI-only cropOptions such as the resize-handle toggle.\n\t */\n\tisCropperDirty: boolean;\n\t/** Whether there's an undoable change in history. */\n\thasUndo: boolean;\n\t/** Whether there's a redoable change in history. */\n\thasRedo: boolean;\n\t/** Undo the last committed action across all slices. */\n\tundo: () => void;\n\t/** Redo the last undone action across all slices. */\n\tredo: () => void;\n\t/**\n\t * Open a gesture boundary. During a gesture all state-changing\n\t * actions coalesce into one undo entry \u2014 the snapshot taken at\n\t * `beginGesture`. Pair with `endGesture` on pointer-up / key-up.\n\t */\n\tbeginGesture: () => void;\n\t/** Close a gesture boundary and commit the pre-gesture snapshot. */\n\tendGesture: () => void;\n\t/**\n\t * Report the current rendered image size to the controller. The\n\t * Cropper component calls this whenever its visualSize changes;\n\t * the controller needs it to compute aspect-ratio reshapes.\n\t */\n\tsetVisualSize: ( size: Size ) => void;\n\t/**\n\t * Reshape the cropRect in response to a viewport change. Behaves\n\t * like `setCropRect` but does NOT record an undo entry \u2014 window\n\t * resizes aren't editor actions.\n\t */\n\tadjustCropRectForViewport: ( rect: NormalizedRect ) => void;\n}\n\ninterface InitialMediaEditorState {\n\tcropper?: Partial< CropperState >;\n\tcropOptions?: Partial< CropOptionsSlice >;\n}\n\n/**\n * Composite store for the media editor.\n *\n * Owns the only undo/redo history for the editor. Cropper geometry\n * actions and sidebar cropOptions actions flow through the same\n * reducer and the same history stack.\n *\n * History semantics:\n * - Each action's pre-state is pushed to the undo stack before the\n * action commits, unless a gesture is open or the action is a\n * no-op (post-state value-equal to pre-state).\n * - During a gesture (`beginGesture` \u2192 `endGesture`) all mutations\n * coalesce: only the pre-gesture snapshot is pushed, at gesture\n * end. Any pending gesture is flushed on undo/redo.\n * - Viewport-driven cropRect reshapes (window resize) use a\n * dedicated non-recording dispatch path so they never produce\n * undo entries.\n *\n * `isDirty` tracks the full editor session, including undoable UI\n * state. Use `isCropperDirty` when deciding whether the image output\n * needs saving.\n *\n * @param initialState Optional seed for the cropper and cropOptions slices.\n * @return The composite controller.\n */\nexport function useMediaEditorState(\n\tinitialState?: InitialMediaEditorState\n): MediaEditorController {\n\tconst [ state, dispatch ] = useReducer( mediaEditorReducer, null, () =>\n\t\tbuildInitialMediaEditorState(\n\t\t\tenforceContainment( {\n\t\t\t\t...DEFAULT_STATE,\n\t\t\t\t...initialState?.cropper,\n\t\t\t} ),\n\t\t\tinitialState?.cropOptions\n\t\t)\n\t);\n\n\t// The \"clean\" snapshot the current state is compared against for\n\t// `isDirty`. Stored in state (not a ref) so the comparison reads\n\t// a value, not a ref's `current`. `setImage` refreshes this when\n\t// a new image establishes a fresh baseline.\n\tconst [ initialBaseline, setInitialBaseline ] =\n\t\tuseState< MediaEditorState >( () => state );\n\n\t// Latest-state ref used by setters that need to read fresh state\n\t// (focal-point zoom, dispatch wrapper). Every mutation path\n\t// (`dispatchWithHistory`, `setImage`, `undo`, `redo`) writes this\n\t// synchronously, so multiple actions in one event see the latest\n\t// reducer output before React commits.\n\tconst stateRef = useRef( state );\n\tconst visualSizeRef = useRef< Size >( { width: 0, height: 0 } );\n\n\t// History stacks: full composite snapshots. Refs avoid re-renders\n\t// on every push; a boolean state pair drives the enabled/disabled\n\t// indicators for undo/redo buttons.\n\tconst historyRef = useRef< MediaEditorState[] >( [] );\n\tconst redoStackRef = useRef< MediaEditorState[] >( [] );\n\tconst [ hasUndo, setHasUndo ] = useState( false );\n\tconst [ hasRedo, setHasRedo ] = useState( false );\n\n\t// Gesture state. While open, the first non-no-op action captures\n\t// the pre-gesture snapshot; subsequent actions don't push.\n\t// `endGesture` pushes the captured snapshot (or nothing, if the\n\t// gesture produced no changes).\n\tconst isGestureOpenRef = useRef( false );\n\tconst gestureSnapshotRef = useRef< MediaEditorState | null >( null );\n\n\tconst pushSnapshot = useCallback( ( snapshot: MediaEditorState ) => {\n\t\tconst last = historyRef.current[ historyRef.current.length - 1 ];\n\t\tif ( last && areMediaEditorStatesEqual( last, snapshot ) ) {\n\t\t\treturn;\n\t\t}\n\t\thistoryRef.current = [ ...historyRef.current, snapshot ];\n\t\tredoStackRef.current = [];\n\t\tsetHasUndo( true );\n\t\tsetHasRedo( false );\n\t}, [] );\n\n\t// Core dispatch wrapper. Synchronously computes the post-state via\n\t// the pure reducer so we can skip history pushes for no-op actions\n\t// (e.g., setting a freeform toggle to its current value) and keep\n\t// `stateRef` in lockstep with React's pending state.\n\tconst dispatchWithHistory = useCallback(\n\t\t( action: MediaEditorAction, recordHistory = true ) => {\n\t\t\tconst preState = stateRef.current;\n\t\t\tconst postState = mediaEditorReducer( preState, action );\n\t\t\tif ( recordHistory ) {\n\t\t\t\tif ( ! areMediaEditorStatesEqual( preState, postState ) ) {\n\t\t\t\t\tif ( isGestureOpenRef.current ) {\n\t\t\t\t\t\tif ( gestureSnapshotRef.current === null ) {\n\t\t\t\t\t\t\tgestureSnapshotRef.current = preState;\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tpushSnapshot( preState );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tstateRef.current = postState;\n\t\t\tdispatch( action );\n\t\t},\n\t\t[ pushSnapshot ]\n\t);\n\n\tconst beginGesture = useCallback( () => {\n\t\tif ( isGestureOpenRef.current ) {\n\t\t\treturn;\n\t\t}\n\t\tisGestureOpenRef.current = true;\n\t\tgestureSnapshotRef.current = null;\n\t}, [] );\n\n\t// Close a gesture (or flush a pending one before undo/redo).\n\t// Pushes the pre-gesture snapshot only if it differs from current\n\t// state. Safe to call when no gesture is open.\n\tconst endGesture = useCallback( () => {\n\t\tconst snapshot = gestureSnapshotRef.current;\n\t\tif (\n\t\t\tsnapshot &&\n\t\t\t! areMediaEditorStatesEqual( snapshot, stateRef.current )\n\t\t) {\n\t\t\tpushSnapshot( snapshot );\n\t\t}\n\t\tgestureSnapshotRef.current = null;\n\t\tisGestureOpenRef.current = false;\n\t}, [ pushSnapshot ] );\n\n\t// =====================================================================\n\t// Cropper-slice setters (built from the shared factory so the pure\n\t// hook and this composite stay in lockstep \u2014 add a setter in\n\t// `buildCropperSetters` once and both hooks get it).\n\t// =====================================================================\n\n\tconst dispatchCropperAction = useCallback(\n\t\t( action: CropperAction ) => {\n\t\t\tdispatchWithHistory( { type: 'CROPPER', action } );\n\t\t},\n\t\t[ dispatchWithHistory ]\n\t);\n\n\tconst cropperSetters = useMemo(\n\t\t() =>\n\t\t\tbuildCropperSetters(\n\t\t\t\tdispatchCropperAction,\n\t\t\t\t() => stateRef.current.cropper\n\t\t\t),\n\t\t[ dispatchCropperAction ]\n\t);\n\n\tconst setImage = useCallback( ( image: CropperState[ 'image' ] ) => {\n\t\tif ( areCropperImagesEqual( stateRef.current.cropper.image, image ) ) {\n\t\t\treturn;\n\t\t}\n\t\t// New image = fresh canvas: clear history, refresh the\n\t\t// initial snapshot so isDirty starts at false.\n\t\tconst action = {\n\t\t\ttype: 'CROPPER' as const,\n\t\t\taction: { type: 'SET_IMAGE' as const, payload: image },\n\t\t};\n\t\tconst next = mediaEditorReducer( stateRef.current, action );\n\t\tstateRef.current = next;\n\t\tdispatch( action );\n\t\tsetInitialBaseline( next );\n\t\tisGestureOpenRef.current = false;\n\t\tgestureSnapshotRef.current = null;\n\t\thistoryRef.current = [];\n\t\tredoStackRef.current = [];\n\t\tsetHasUndo( false );\n\t\tsetHasRedo( false );\n\t}, [] );\n\n\tconst reset = useCallback(\n\t\t( resetState?: Partial< CropperState > ) => {\n\t\t\tdispatchWithHistory( {\n\t\t\t\ttype: 'CROPPER',\n\t\t\t\taction: { type: 'RESET', payload: resetState },\n\t\t\t} );\n\t\t},\n\t\t[ dispatchWithHistory ]\n\t);\n\n\t// =====================================================================\n\t// CropOptions-slice setters\n\t// =====================================================================\n\n\tconst setAspectRatioValue = useCallback(\n\t\t( presetKey: string ) => {\n\t\t\tconst resolved = resolveAspectRatio(\n\t\t\t\tpresetKey,\n\t\t\t\tstateRef.current.cropper.image\n\t\t\t);\n\t\t\tdispatchWithHistory( {\n\t\t\t\ttype: 'SET_ASPECT_RATIO_VALUE',\n\t\t\t\tpayload: {\n\t\t\t\t\tpresetKey,\n\t\t\t\t\tresolved,\n\t\t\t\t\tvisualSize: visualSizeRef.current,\n\t\t\t\t},\n\t\t\t} );\n\t\t},\n\t\t[ dispatchWithHistory ]\n\t);\n\n\tconst setFreeformCrop = useCallback(\n\t\t( value: boolean ) => {\n\t\t\tdispatchWithHistory( {\n\t\t\t\ttype: 'SET_FREEFORM_CROP',\n\t\t\t\tpayload: value,\n\t\t\t} );\n\t\t},\n\t\t[ dispatchWithHistory ]\n\t);\n\n\tconst resetCropOptions = useCallback( () => {\n\t\tdispatchWithHistory( { type: 'RESET_CROP_OPTIONS' } );\n\t}, [ dispatchWithHistory ] );\n\n\t// =====================================================================\n\t// History\n\t// =====================================================================\n\n\tconst undo = useCallback( () => {\n\t\tendGesture();\n\t\tconst prev = historyRef.current[ historyRef.current.length - 1 ];\n\t\tif ( ! prev ) {\n\t\t\treturn;\n\t\t}\n\t\tredoStackRef.current = [ stateRef.current, ...redoStackRef.current ];\n\t\thistoryRef.current = historyRef.current.slice( 0, -1 );\n\t\tsetHasUndo( historyRef.current.length > 0 );\n\t\tsetHasRedo( true );\n\t\tstateRef.current = prev;\n\t\tdispatch( { type: 'RESTORE_SNAPSHOT', payload: prev } );\n\t}, [ endGesture ] );\n\n\tconst redo = useCallback( () => {\n\t\tendGesture();\n\t\tconst next = redoStackRef.current[ 0 ];\n\t\tif ( ! next ) {\n\t\t\treturn;\n\t\t}\n\t\thistoryRef.current = [ ...historyRef.current, stateRef.current ];\n\t\tredoStackRef.current = redoStackRef.current.slice( 1 );\n\t\tsetHasUndo( true );\n\t\tsetHasRedo( redoStackRef.current.length > 0 );\n\t\tstateRef.current = next;\n\t\tdispatch( { type: 'RESTORE_SNAPSHOT', payload: next } );\n\t}, [ endGesture ] );\n\n\t// =====================================================================\n\t// Viewport\n\t// =====================================================================\n\n\tconst setVisualSize = useCallback( ( size: Size ) => {\n\t\tvisualSizeRef.current = size;\n\t}, [] );\n\n\tconst adjustCropRectForViewport = useCallback(\n\t\t( rect: NormalizedRect ) => {\n\t\t\tdispatchWithHistory(\n\t\t\t\t{ type: 'VIEWPORT_ADJUST_CROP_RECT', payload: rect },\n\t\t\t\tfalse\n\t\t\t);\n\t\t},\n\t\t[ dispatchWithHistory ]\n\t);\n\n\t// =====================================================================\n\t// Derived\n\t// =====================================================================\n\n\tconst isDirty = ! areMediaEditorStatesEqual( state, initialBaseline );\n\tconst isCropperDirty = ! areCropperStatesEqual(\n\t\tstate.cropper,\n\t\tinitialBaseline.cropper\n\t);\n\n\tconst getCroppedImage = useCallback(\n\t\t( mimeType?: string, quality?: number ): Promise< Blob > => {\n\t\t\tif ( ! state.cropper.image ) {\n\t\t\t\treturn Promise.reject(\n\t\t\t\t\tnew Error( 'No image loaded \u2014 call setImage first.' )\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn exportCroppedImage(\n\t\t\t\tstate.cropper.image.src,\n\t\t\t\tstate.cropper,\n\t\t\t\tmimeType,\n\t\t\t\tquality\n\t\t\t);\n\t\t},\n\t\t[ state.cropper ]\n\t);\n\n\tconst controller = useMemo< MediaEditorController >(\n\t\t() => ( {\n\t\t\t// CropperController surface (state is the cropper slice so a\n\t\t\t// <Cropper> takes this controller as-is).\n\t\t\t...cropperSetters,\n\t\t\tstate: state.cropper,\n\t\t\tsetImage,\n\t\t\treset,\n\t\t\tisDirty,\n\t\t\tgetCroppedImage,\n\t\t\t// Composite extensions\n\t\t\tcropOptions: state.cropOptions,\n\t\t\tsetAspectRatioValue,\n\t\t\tsetFreeformCrop,\n\t\t\tresetCropOptions,\n\t\t\tisCropperDirty,\n\t\t\thasUndo,\n\t\t\thasRedo,\n\t\t\tundo,\n\t\t\tredo,\n\t\t\tbeginGesture,\n\t\t\tendGesture,\n\t\t\tsetVisualSize,\n\t\t\tadjustCropRectForViewport,\n\t\t} ),\n\t\t[\n\t\t\tcropperSetters,\n\t\t\tstate.cropper,\n\t\t\tsetImage,\n\t\t\treset,\n\t\t\tisDirty,\n\t\t\tgetCroppedImage,\n\t\t\tstate.cropOptions,\n\t\t\tsetAspectRatioValue,\n\t\t\tsetFreeformCrop,\n\t\t\tresetCropOptions,\n\t\t\tisCropperDirty,\n\t\t\thasUndo,\n\t\t\thasRedo,\n\t\t\tundo,\n\t\t\tredo,\n\t\t\tbeginGesture,\n\t\t\tendGesture,\n\t\t\tsetVisualSize,\n\t\t\tadjustCropRectForViewport,\n\t\t]\n\t);\n\treturn controller;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAMO;AAKP,0BAYO;A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C;AAC3D,UAAK,CAAE,MAAM,QAAQ,OAAQ;AAC5B,eAAO,QAAQ;AAAA,UACd,IAAI,MAAO,6CAAyC;AAAA,QACrD;AAAA,MACD;AACA,iBAAO;AAAA,QACN,MAAM,QAAQ,MAAM;AAAA,QACpB,MAAM;AAAA,QACN;AAAA,QACA;AAAA,MACD;AAAA,IACD;AAAA,IACA,CAAE,MAAM,OAAQ;AAAA,EACjB;AAEA,QAAM,iBAAa;AAAA,IAClB,OAAQ;AAAA;AAAA;AAAA,MAGP,GAAG;AAAA,MACH,OAAO,MAAM;AAAA,MACb;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA;AAAA,MAEA,aAAa,MAAM;AAAA,MACnB;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,IACA;AAAA,MACC;AAAA,MACA,MAAM;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,MAAM;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AACA,SAAO;AACR;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// packages/media-editor/src/components/media-editor/index.tsx
|
|
2
|
-
import apiFetch from "@wordpress/api-fetch";
|
|
3
2
|
import {
|
|
4
3
|
Button,
|
|
5
4
|
Flex,
|
|
@@ -8,7 +7,7 @@ import {
|
|
|
8
7
|
privateApis as componentsPrivateApis
|
|
9
8
|
} from "@wordpress/components";
|
|
10
9
|
import { Stack } from "@wordpress/ui";
|
|
11
|
-
import { useDispatch,
|
|
10
|
+
import { useDispatch, useSelect } from "@wordpress/data";
|
|
12
11
|
import { store as coreStore } from "@wordpress/core-data";
|
|
13
12
|
import {
|
|
14
13
|
createPortal,
|
|
@@ -19,7 +18,7 @@ import {
|
|
|
19
18
|
useRef,
|
|
20
19
|
useState
|
|
21
20
|
} from "@wordpress/element";
|
|
22
|
-
import { __
|
|
21
|
+
import { __ } from "@wordpress/i18n";
|
|
23
22
|
import { close, drawerRight, keyboard } from "@wordpress/icons";
|
|
24
23
|
import { isAppleOS, isKeyboardEvent } from "@wordpress/keycodes";
|
|
25
24
|
import { SnackbarNotices, store as noticesStore } from "@wordpress/notices";
|
|
@@ -32,26 +31,20 @@ import { MediaEditorProvider } from "../media-editor-provider/index.mjs";
|
|
|
32
31
|
import MediaPreview from "../media-preview/index.mjs";
|
|
33
32
|
import MediaEditorCanvas from "../media-editor-canvas/index.mjs";
|
|
34
33
|
import MediaEditorToolbar from "../media-editor-toolbar/index.mjs";
|
|
35
|
-
import MediaEditorCropPanel
|
|
36
|
-
resolveAspectRatio
|
|
37
|
-
} from "../media-editor-crop-panel/index.mjs";
|
|
34
|
+
import MediaEditorCropPanel from "../media-editor-crop-panel/index.mjs";
|
|
38
35
|
import MediaForm from "../media-form/index.mjs";
|
|
39
36
|
import { unlock } from "../../lock-unlock.mjs";
|
|
40
37
|
import { getMediaTypeFromMimeType } from "../../utils/index.mjs";
|
|
41
|
-
import {
|
|
38
|
+
import { MediaEditorStateProvider, useMediaEditor } from "../../state/index.mjs";
|
|
42
39
|
import { CROP_CONTROL_ATTR } from "../../hooks/use-crop-gesture-handlers.mjs";
|
|
43
|
-
import { buildModifiers } from "../media-editor-modal/build-modifiers.mjs";
|
|
44
40
|
import MediaEditorKeyboardShortcutsModal from "../media-editor-keyboard-shortcuts-modal/index.mjs";
|
|
41
|
+
import {
|
|
42
|
+
MEDIA_EDITOR_NOTICES_CONTEXT,
|
|
43
|
+
useSaveMediaEditor
|
|
44
|
+
} from "./use-save-media-editor.mjs";
|
|
45
|
+
import { useCropOptions } from "./use-crop-options.mjs";
|
|
45
46
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
46
|
-
var METADATA_EDIT_KEYS = [
|
|
47
|
-
"title",
|
|
48
|
-
"caption",
|
|
49
|
-
"description",
|
|
50
|
-
"alt_text",
|
|
51
|
-
"post"
|
|
52
|
-
];
|
|
53
47
|
var ATTACHMENT_EMBED_QUERY = { _embed: "author,wp:attached-to" };
|
|
54
|
-
var NOTICES_CONTEXT = "media-editor";
|
|
55
48
|
var PLACEMENT_CONTROL_IDLE_MS = 300;
|
|
56
49
|
var { Tabs } = unlock(componentsPrivateApis);
|
|
57
50
|
function MediaEditorSidebar({ tabs }) {
|
|
@@ -165,7 +158,7 @@ function MediaEditorContent({
|
|
|
165
158
|
showCloseButton = false,
|
|
166
159
|
shouldCloseOnEsc = false
|
|
167
160
|
}) {
|
|
168
|
-
const cropper =
|
|
161
|
+
const cropper = useMediaEditor();
|
|
169
162
|
const { media, hasEdits } = useSelect(
|
|
170
163
|
(select) => {
|
|
171
164
|
const {
|
|
@@ -194,23 +187,13 @@ function MediaEditorContent({
|
|
|
194
187
|
},
|
|
195
188
|
[id]
|
|
196
189
|
);
|
|
197
|
-
const hasChanges = cropper.
|
|
198
|
-
const
|
|
199
|
-
const {
|
|
200
|
-
clearEntityRecordEdits,
|
|
201
|
-
editEntityRecord,
|
|
202
|
-
invalidateResolution,
|
|
203
|
-
receiveEntityRecords,
|
|
204
|
-
saveEditedEntityRecord
|
|
205
|
-
} = useDispatch(coreStore);
|
|
206
|
-
const { createErrorNotice, removeAllNotices } = useDispatch(noticesStore);
|
|
207
|
-
const [isSaving, setIsSaving] = useState(false);
|
|
190
|
+
const hasChanges = cropper.isCropperDirty || hasEdits;
|
|
191
|
+
const { clearEntityRecordEdits, editEntityRecord, invalidateResolution } = useDispatch(coreStore);
|
|
192
|
+
const { removeAllNotices } = useDispatch(noticesStore);
|
|
208
193
|
const [isDiscardDialogOpen, setIsDiscardDialogOpen] = useState(false);
|
|
209
194
|
const [isPlacementActive, setIsPlacementActive] = useState(false);
|
|
210
195
|
const [isCanvasGestureActive, setIsCanvasGestureActive] = useState(false);
|
|
211
|
-
const placementControlTimerRef = useRef();
|
|
212
|
-
const [aspectRatioValue, setAspectRatioValue] = useState("0");
|
|
213
|
-
const [freeformCrop, setFreeformCrop] = useState(true);
|
|
196
|
+
const placementControlTimerRef = useRef(void 0);
|
|
214
197
|
const signalPlacementControlInteraction = useCallback(() => {
|
|
215
198
|
setIsPlacementActive(true);
|
|
216
199
|
clearTimeout(placementControlTimerRef.current);
|
|
@@ -231,8 +214,6 @@ function MediaEditorContent({
|
|
|
231
214
|
};
|
|
232
215
|
}, []);
|
|
233
216
|
useEffect(() => {
|
|
234
|
-
setAspectRatioValue("0");
|
|
235
|
-
setFreeformCrop(true);
|
|
236
217
|
setIsPlacementActive(false);
|
|
237
218
|
setIsCanvasGestureActive(false);
|
|
238
219
|
}, [id]);
|
|
@@ -246,17 +227,23 @@ function MediaEditorContent({
|
|
|
246
227
|
}, [id, invalidateResolution]);
|
|
247
228
|
const mediaType = getMediaTypeFromMimeType(media?.mime_type).type;
|
|
248
229
|
const isImage = !!media && mediaType === "image";
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
230
|
+
const {
|
|
231
|
+
aspectRatioValue,
|
|
232
|
+
setAspectRatioValue,
|
|
233
|
+
aspectRatioOptions,
|
|
234
|
+
freeformCrop,
|
|
235
|
+
setFreeformCrop,
|
|
236
|
+
resetCropOptions
|
|
237
|
+
} = useCropOptions({
|
|
238
|
+
aspectRatioPresets
|
|
239
|
+
});
|
|
240
|
+
const { isSaving, save: saveMediaEditor } = useSaveMediaEditor({
|
|
241
|
+
cropper,
|
|
242
|
+
id,
|
|
243
|
+
isImage,
|
|
244
|
+
media,
|
|
245
|
+
onSaved
|
|
246
|
+
});
|
|
260
247
|
const tabs = useMemo(() => {
|
|
261
248
|
const detailsTab = {
|
|
262
249
|
id: "details",
|
|
@@ -292,7 +279,7 @@ function MediaEditorContent({
|
|
|
292
279
|
freeformCrop,
|
|
293
280
|
onFreeformChange: setFreeformCrop,
|
|
294
281
|
onPlacementControlInteraction: signalPlacementControlInteraction,
|
|
295
|
-
|
|
282
|
+
aspectRatioOptions
|
|
296
283
|
}
|
|
297
284
|
)
|
|
298
285
|
}
|
|
@@ -303,15 +290,17 @@ function MediaEditorContent({
|
|
|
303
290
|
}, [
|
|
304
291
|
isImage,
|
|
305
292
|
aspectRatioValue,
|
|
293
|
+
setAspectRatioValue,
|
|
306
294
|
freeformCrop,
|
|
307
|
-
|
|
295
|
+
setFreeformCrop,
|
|
296
|
+
aspectRatioOptions,
|
|
308
297
|
signalPlacementControlInteraction
|
|
309
298
|
]);
|
|
310
299
|
const handleChange = (updates) => {
|
|
311
300
|
editEntityRecord("postType", "attachment", id, updates);
|
|
312
301
|
};
|
|
313
302
|
const discardAndClose = () => {
|
|
314
|
-
removeAllNotices("snackbar",
|
|
303
|
+
removeAllNotices("snackbar", MEDIA_EDITOR_NOTICES_CONTEXT);
|
|
315
304
|
clearEntityRecordEdits("postType", "attachment", id);
|
|
316
305
|
onClose?.();
|
|
317
306
|
};
|
|
@@ -325,87 +314,6 @@ function MediaEditorContent({
|
|
|
325
314
|
}
|
|
326
315
|
discardAndClose();
|
|
327
316
|
};
|
|
328
|
-
const handleSave = async () => {
|
|
329
|
-
removeAllNotices("snackbar", NOTICES_CONTEXT);
|
|
330
|
-
setIsSaving(true);
|
|
331
|
-
try {
|
|
332
|
-
let saved;
|
|
333
|
-
const modifiers = cropper.isDirty && cropper.state.image ? buildModifiers(cropper.state, {
|
|
334
|
-
width: cropper.state.image.naturalWidth,
|
|
335
|
-
height: cropper.state.image.naturalHeight
|
|
336
|
-
}) : [];
|
|
337
|
-
if (modifiers.length > 0) {
|
|
338
|
-
const pendingEdits = registry.select(coreStore).getEntityRecordNonTransientEdits(
|
|
339
|
-
"postType",
|
|
340
|
-
"attachment",
|
|
341
|
-
id
|
|
342
|
-
);
|
|
343
|
-
const metadataEdits = {};
|
|
344
|
-
for (const key of METADATA_EDIT_KEYS) {
|
|
345
|
-
if (pendingEdits && key in pendingEdits) {
|
|
346
|
-
metadataEdits[key] = pendingEdits[key];
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
if (!("post" in metadataEdits) && media?.post !== void 0) {
|
|
350
|
-
metadataEdits.post = media.post;
|
|
351
|
-
}
|
|
352
|
-
saved = await apiFetch({
|
|
353
|
-
path: `/wp/v2/media/${id}/edit`,
|
|
354
|
-
method: "POST",
|
|
355
|
-
data: {
|
|
356
|
-
src: media?.source_url,
|
|
357
|
-
modifiers,
|
|
358
|
-
...metadataEdits
|
|
359
|
-
}
|
|
360
|
-
});
|
|
361
|
-
if (saved) {
|
|
362
|
-
receiveEntityRecords(
|
|
363
|
-
"postType",
|
|
364
|
-
"attachment",
|
|
365
|
-
saved,
|
|
366
|
-
void 0,
|
|
367
|
-
true
|
|
368
|
-
);
|
|
369
|
-
}
|
|
370
|
-
} else {
|
|
371
|
-
saved = await saveEditedEntityRecord(
|
|
372
|
-
"postType",
|
|
373
|
-
"attachment",
|
|
374
|
-
id
|
|
375
|
-
);
|
|
376
|
-
}
|
|
377
|
-
const next = saved ?? media;
|
|
378
|
-
if (next && next.id !== id) {
|
|
379
|
-
clearEntityRecordEdits("postType", "attachment", id);
|
|
380
|
-
}
|
|
381
|
-
if (next && next.id) {
|
|
382
|
-
if (next.id === id) {
|
|
383
|
-
cropper.reset();
|
|
384
|
-
}
|
|
385
|
-
onSaved?.({
|
|
386
|
-
id: next.id,
|
|
387
|
-
url: next.source_url,
|
|
388
|
-
media: next
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
} catch (error) {
|
|
392
|
-
const message = error instanceof Error ? error.message : error?.message ?? __("An unknown error occurred.");
|
|
393
|
-
createErrorNotice(
|
|
394
|
-
isImage ? sprintf(
|
|
395
|
-
/* translators: %s: Error message. */
|
|
396
|
-
__("Could not save image. %s"),
|
|
397
|
-
message
|
|
398
|
-
) : sprintf(
|
|
399
|
-
/* translators: %s: Error message. */
|
|
400
|
-
__("Could not save media. %s"),
|
|
401
|
-
message
|
|
402
|
-
),
|
|
403
|
-
{ type: "snackbar", context: NOTICES_CONTEXT }
|
|
404
|
-
);
|
|
405
|
-
} finally {
|
|
406
|
-
setIsSaving(false);
|
|
407
|
-
}
|
|
408
|
-
};
|
|
409
317
|
const handleKeyDown = (event) => {
|
|
410
318
|
const isUndoShortcut = isKeyboardEvent.primary(event, "z");
|
|
411
319
|
const isRedoShortcut = isKeyboardEvent.primaryShift(event, "z") || !isAppleOS() && isKeyboardEvent.primary(event, "y");
|
|
@@ -442,7 +350,7 @@ function MediaEditorContent({
|
|
|
442
350
|
SnackbarNotices,
|
|
443
351
|
{
|
|
444
352
|
className: noticesClassName,
|
|
445
|
-
context:
|
|
353
|
+
context: MEDIA_EDITOR_NOTICES_CONTEXT
|
|
446
354
|
}
|
|
447
355
|
);
|
|
448
356
|
const children = /* @__PURE__ */ jsxs(
|
|
@@ -466,11 +374,6 @@ function MediaEditorContent({
|
|
|
466
374
|
content: /* @__PURE__ */ jsx("div", { className: "media-editor__canvas", children: isImage ? /* @__PURE__ */ jsx(
|
|
467
375
|
MediaEditorCanvas,
|
|
468
376
|
{
|
|
469
|
-
aspectRatio: resolveAspectRatio(
|
|
470
|
-
aspectRatioValue,
|
|
471
|
-
imageAspectRatio
|
|
472
|
-
),
|
|
473
|
-
freeformCrop,
|
|
474
377
|
focusOnMount: true,
|
|
475
378
|
isPlacementActive,
|
|
476
379
|
onGestureStart: handleCanvasGestureStart,
|
|
@@ -480,10 +383,7 @@ function MediaEditorContent({
|
|
|
480
383
|
footer: isImage ? /* @__PURE__ */ jsx(
|
|
481
384
|
MediaEditorToolbar,
|
|
482
385
|
{
|
|
483
|
-
onReset:
|
|
484
|
-
setAspectRatioValue("0");
|
|
485
|
-
setFreeformCrop(true);
|
|
486
|
-
},
|
|
386
|
+
onReset: resetCropOptions,
|
|
487
387
|
onPlacementControlInteraction: signalPlacementControlInteraction,
|
|
488
388
|
isUndoRedoDisabled: isCropInteractionActive
|
|
489
389
|
}
|
|
@@ -523,7 +423,7 @@ function MediaEditorContent({
|
|
|
523
423
|
isImage,
|
|
524
424
|
showCloseButton,
|
|
525
425
|
onCancel: handleRequestClose,
|
|
526
|
-
onSave:
|
|
426
|
+
onSave: saveMediaEditor
|
|
527
427
|
}
|
|
528
428
|
),
|
|
529
429
|
onRequestClose: handleRequestClose,
|
|
@@ -535,7 +435,7 @@ function MediaEditorContent({
|
|
|
535
435
|
});
|
|
536
436
|
}
|
|
537
437
|
function MediaEditor(props) {
|
|
538
|
-
return /* @__PURE__ */ jsx(
|
|
438
|
+
return /* @__PURE__ */ jsx(MediaEditorStateProvider, { children: /* @__PURE__ */ jsx(MediaEditorContent, { ...props }) }, props.id);
|
|
539
439
|
}
|
|
540
440
|
var media_editor_default = MediaEditor;
|
|
541
441
|
export {
|