@wordpress/media-editor 0.9.0 → 0.10.1
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 +212 -170
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +63 -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 +33 -12
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +70 -84
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-fine-rotation/index.cjs +81 -0
- package/build/components/media-editor-fine-rotation/index.cjs.map +7 -0
- package/build/components/media-editor-modal/index.cjs +63 -16
- package/build/components/media-editor-modal/index.cjs.map +3 -3
- package/build/components/media-editor-transform-controls/index.cjs +84 -0
- package/build/components/media-editor-transform-controls/index.cjs.map +7 -0
- 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 +120 -47
- 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 +19 -9
- 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-cropper-reducer.cjs +113 -0
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +7 -0
- package/build/state/composite-reducer.cjs +103 -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 +33 -0
- package/build/state/types.cjs.map +7 -0
- package/build/state/use-media-editor-state.cjs +287 -0
- package/build/state/use-media-editor-state.cjs.map +7 -0
- package/build/store/actions.cjs +4 -2
- package/build/store/actions.cjs.map +2 -2
- package/build/store/reducer.cjs +5 -3
- package/build/store/reducer.cjs.map +2 -2
- package/build/store/selectors.cjs +5 -0
- package/build/store/selectors.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +224 -177
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +37 -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 +34 -13
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +73 -89
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs +50 -0
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +7 -0
- package/build-module/components/media-editor-modal/index.mjs +64 -17
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-transform-controls/index.mjs +68 -0
- package/build-module/components/media-editor-transform-controls/index.mjs.map +7 -0
- 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 +121 -48
- 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 +19 -9
- 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-cropper-reducer.mjs +100 -0
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +7 -0
- package/build-module/state/composite-reducer.mjs +82 -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 +8 -0
- package/build-module/state/types.mjs.map +7 -0
- package/build-module/state/use-media-editor-state.mjs +278 -0
- package/build-module/state/use-media-editor-state.mjs.map +7 -0
- package/build-module/store/actions.mjs +4 -2
- package/build-module/store/actions.mjs.map +2 -2
- package/build-module/store/reducer.mjs +5 -3
- package/build-module/store/reducer.mjs.map +2 -2
- package/build-module/store/selectors.mjs +4 -0
- package/build-module/store/selectors.mjs.map +2 -2
- package/build-style/style-rtl.css +97 -44
- package/build-style/style.css +97 -44
- package/build-types/components/media-editor/index.d.ts +14 -5
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +35 -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 +6 -25
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-fine-rotation/index.d.ts +14 -0
- package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-transform-controls/index.d.ts +8 -0
- package/build-types/components/media-editor-transform-controls/index.d.ts.map +1 -0
- 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-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/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 +96 -0
- package/build-types/state/types.d.ts.map +1 -0
- package/build-types/state/use-media-editor-state.d.ts +101 -0
- package/build-types/state/use-media-editor-state.d.ts.map +1 -0
- package/build-types/store/actions.d.ts +3 -1
- package/build-types/store/actions.d.ts.map +1 -1
- package/build-types/store/reducer.d.ts +3 -0
- package/build-types/store/reducer.d.ts.map +1 -1
- package/build-types/store/selectors.d.ts +1 -0
- package/build-types/store/selectors.d.ts.map +1 -1
- package/package.json +19 -16
- package/src/components/media-editor/index.tsx +262 -229
- package/src/components/media-editor/style.scss +0 -9
- package/src/components/media-editor/test/use-crop-options.tsx +110 -0
- package/src/components/media-editor/use-crop-options.ts +79 -0
- package/src/components/media-editor/use-save-media-editor.ts +214 -0
- package/src/components/media-editor-canvas/index.tsx +57 -25
- package/src/components/media-editor-crop-panel/index.tsx +53 -87
- package/src/components/media-editor-crop-panel/test/index.tsx +46 -53
- package/src/components/media-editor-fine-rotation/index.tsx +74 -0
- package/src/components/media-editor-modal/index.tsx +70 -16
- package/src/components/media-editor-modal/style.scss +80 -0
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +14 -0
- package/src/components/media-editor-modal/test/index.tsx +146 -0
- package/src/components/media-editor-transform-controls/index.tsx +70 -0
- package/src/components/media-editor-transform-controls/style.scss +8 -0
- 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 +64 -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 +194 -89
- package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +269 -0
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +19 -8
- 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-cropper-reducer.ts +204 -0
- package/src/image-editor/stories/rectangle-crop.story.tsx +7 -17
- package/src/state/composite-reducer.ts +137 -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 +330 -0
- package/src/state/types.ts +101 -0
- package/src/state/use-media-editor-state.ts +472 -0
- package/src/store/actions.ts +3 -0
- package/src/store/reducer.ts +6 -1
- package/src/store/selectors.ts +4 -0
- package/src/store/test/reducer.ts +9 -0
- package/src/style.scss +1 -1
- package/tsconfig.json +3 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/build/components/media-editor-toolbar/index.cjs +0 -214
- package/build/components/media-editor-toolbar/index.cjs.map +0 -7
- 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/components/media-editor-toolbar/index.mjs +0 -190
- package/build-module/components/media-editor-toolbar/index.mjs.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/components/media-editor-toolbar/index.d.ts +0 -24
- package/build-types/components/media-editor-toolbar/index.d.ts.map +0 -1
- 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/components/media-editor-toolbar/index.tsx +0 -209
- package/src/components/media-editor-toolbar/style.scss +0 -49
- package/src/image-editor/docs/roadmap.md +0 -66
- package/src/image-editor/react/hooks/use-cropper-state.ts +0 -457
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
useCallback,
|
|
6
|
+
useMemo,
|
|
7
|
+
useReducer,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
} from '@wordpress/element';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Internal dependencies
|
|
14
|
+
*/
|
|
15
|
+
import {
|
|
16
|
+
DEFAULT_STATE,
|
|
17
|
+
ORIGINAL_ASPECT_RATIO,
|
|
18
|
+
areCropperStatesEqual,
|
|
19
|
+
buildCropperSetters,
|
|
20
|
+
enforceContainment,
|
|
21
|
+
exportCroppedImage,
|
|
22
|
+
type CropperAction,
|
|
23
|
+
type CropperController,
|
|
24
|
+
type CropperState,
|
|
25
|
+
type NormalizedRect,
|
|
26
|
+
type Size,
|
|
27
|
+
} from '../image-editor';
|
|
28
|
+
import {
|
|
29
|
+
mediaEditorReducer,
|
|
30
|
+
areMediaEditorStatesEqual,
|
|
31
|
+
buildInitialMediaEditorState,
|
|
32
|
+
} from './composite-reducer';
|
|
33
|
+
import type {
|
|
34
|
+
CropOptionsSlice,
|
|
35
|
+
MediaEditorAction,
|
|
36
|
+
MediaEditorState,
|
|
37
|
+
} from './types';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Resolve an aspect-ratio preset value (the string the dropdown
|
|
41
|
+
* round-trips) into a numeric width/height ratio for the cropper.
|
|
42
|
+
*
|
|
43
|
+
* - `'0'` → `undefined` (Free; no lock)
|
|
44
|
+
* - `'-1'` (Original) → image's natural width / height, or `undefined`
|
|
45
|
+
* if no image is loaded.
|
|
46
|
+
* - Any positive decimal → the parsed number.
|
|
47
|
+
*
|
|
48
|
+
* Centralizing here means the dropdown, the action dispatcher, and
|
|
49
|
+
* the test harness all resolve the same way.
|
|
50
|
+
*
|
|
51
|
+
* @param presetKey Preset value from the dropdown.
|
|
52
|
+
* @param cropperImage The cropper's loaded image, or `null`.
|
|
53
|
+
* @return The resolved ratio, or `undefined` for Free / unresolvable.
|
|
54
|
+
*/
|
|
55
|
+
export function resolveAspectRatio(
|
|
56
|
+
presetKey: string,
|
|
57
|
+
cropperImage: CropperState[ 'image' ] | null
|
|
58
|
+
): number | undefined {
|
|
59
|
+
const num = parseFloat( presetKey );
|
|
60
|
+
if ( num === 0 || Number.isNaN( num ) ) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
if ( num === ORIGINAL_ASPECT_RATIO ) {
|
|
64
|
+
if (
|
|
65
|
+
cropperImage &&
|
|
66
|
+
cropperImage.naturalWidth > 0 &&
|
|
67
|
+
cropperImage.naturalHeight > 0
|
|
68
|
+
) {
|
|
69
|
+
return cropperImage.naturalWidth / cropperImage.naturalHeight;
|
|
70
|
+
}
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
if ( num > 0 ) {
|
|
74
|
+
return num;
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function areCropperImagesEqual(
|
|
80
|
+
a: CropperState[ 'image' ],
|
|
81
|
+
b: CropperState[ 'image' ]
|
|
82
|
+
): boolean {
|
|
83
|
+
return (
|
|
84
|
+
a?.src === b?.src &&
|
|
85
|
+
a?.naturalWidth === b?.naturalWidth &&
|
|
86
|
+
a?.naturalHeight === b?.naturalHeight
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The composite controller exposed by `useMediaEditorState`.
|
|
92
|
+
*
|
|
93
|
+
* Satisfies `CropperController` (so a `<Cropper>` can take this
|
|
94
|
+
* controller directly via the `controller` prop) and adds slice
|
|
95
|
+
* setters, undo/redo, gesture boundaries, and viewport reporting.
|
|
96
|
+
*/
|
|
97
|
+
export interface MediaEditorController extends CropperController {
|
|
98
|
+
/** The cropOptions slice. */
|
|
99
|
+
cropOptions: CropOptionsSlice;
|
|
100
|
+
/** Set the aspect-ratio preset. Atomic with the cropRect reshape. */
|
|
101
|
+
setAspectRatioValue: ( presetKey: string ) => void;
|
|
102
|
+
/** Reset cropOptions to defaults. */
|
|
103
|
+
resetCropOptions: () => void;
|
|
104
|
+
/**
|
|
105
|
+
* Whether the cropper geometry differs from the initial baseline.
|
|
106
|
+
*/
|
|
107
|
+
isCropperDirty: boolean;
|
|
108
|
+
/** Whether there's an undoable change in history. */
|
|
109
|
+
hasUndo: boolean;
|
|
110
|
+
/** Whether there's a redoable change in history. */
|
|
111
|
+
hasRedo: boolean;
|
|
112
|
+
/** Undo the last committed action across all slices. */
|
|
113
|
+
undo: () => void;
|
|
114
|
+
/** Redo the last undone action across all slices. */
|
|
115
|
+
redo: () => void;
|
|
116
|
+
/**
|
|
117
|
+
* Open a gesture boundary. During a gesture all state-changing
|
|
118
|
+
* actions coalesce into one undo entry — the snapshot taken at
|
|
119
|
+
* `beginGesture`. Pair with `endGesture` on pointer-up / key-up.
|
|
120
|
+
*/
|
|
121
|
+
beginGesture: () => void;
|
|
122
|
+
/** Close a gesture boundary and commit the pre-gesture snapshot. */
|
|
123
|
+
endGesture: () => void;
|
|
124
|
+
/**
|
|
125
|
+
* Report the current rendered image size to the controller. The
|
|
126
|
+
* Cropper component calls this whenever its visualSize changes;
|
|
127
|
+
* the controller needs it to compute aspect-ratio reshapes.
|
|
128
|
+
*/
|
|
129
|
+
setVisualSize: ( size: Size ) => void;
|
|
130
|
+
/**
|
|
131
|
+
* Reshape the cropRect in response to a viewport change. Behaves
|
|
132
|
+
* like `setCropRect` but does NOT record an undo entry — window
|
|
133
|
+
* resizes aren't editor actions.
|
|
134
|
+
*/
|
|
135
|
+
adjustCropRectForViewport: ( rect: NormalizedRect ) => void;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
interface InitialMediaEditorState {
|
|
139
|
+
cropper?: Partial< CropperState >;
|
|
140
|
+
cropOptions?: Partial< CropOptionsSlice >;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Composite store for the media editor.
|
|
145
|
+
*
|
|
146
|
+
* Owns the only undo/redo history for the editor. Cropper geometry
|
|
147
|
+
* actions and sidebar cropOptions actions flow through the same
|
|
148
|
+
* reducer and the same history stack.
|
|
149
|
+
*
|
|
150
|
+
* History semantics:
|
|
151
|
+
* - Each action's pre-state is pushed to the undo stack before the
|
|
152
|
+
* action commits, unless a gesture is open or the action is a
|
|
153
|
+
* no-op (post-state value-equal to pre-state).
|
|
154
|
+
* - During a gesture (`beginGesture` → `endGesture`) all mutations
|
|
155
|
+
* coalesce: only the pre-gesture snapshot is pushed, at gesture
|
|
156
|
+
* end. Any pending gesture is flushed on undo/redo.
|
|
157
|
+
* - Viewport-driven cropRect reshapes (window resize) use a
|
|
158
|
+
* dedicated non-recording dispatch path so they never produce
|
|
159
|
+
* undo entries.
|
|
160
|
+
*
|
|
161
|
+
* `isDirty` tracks the full editor session, including undoable UI
|
|
162
|
+
* state. Use `isCropperDirty` when deciding whether the image output
|
|
163
|
+
* needs saving.
|
|
164
|
+
*
|
|
165
|
+
* @param initialState Optional seed for the cropper and cropOptions slices.
|
|
166
|
+
* @return The composite controller.
|
|
167
|
+
*/
|
|
168
|
+
export function useMediaEditorState(
|
|
169
|
+
initialState?: InitialMediaEditorState
|
|
170
|
+
): MediaEditorController {
|
|
171
|
+
const [ state, dispatch ] = useReducer( mediaEditorReducer, null, () =>
|
|
172
|
+
buildInitialMediaEditorState(
|
|
173
|
+
enforceContainment( {
|
|
174
|
+
...DEFAULT_STATE,
|
|
175
|
+
...initialState?.cropper,
|
|
176
|
+
} ),
|
|
177
|
+
initialState?.cropOptions
|
|
178
|
+
)
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
// The "clean" snapshot the current state is compared against for
|
|
182
|
+
// `isDirty`. Stored in state (not a ref) so the comparison reads
|
|
183
|
+
// a value, not a ref's `current`. `setImage` refreshes this when
|
|
184
|
+
// a new image establishes a fresh baseline.
|
|
185
|
+
const [ initialBaseline, setInitialBaseline ] =
|
|
186
|
+
useState< MediaEditorState >( () => state );
|
|
187
|
+
|
|
188
|
+
// Latest-state ref used by setters that need to read fresh state
|
|
189
|
+
// (focal-point zoom, dispatch wrapper). Every mutation path
|
|
190
|
+
// (`dispatchWithHistory`, `setImage`, `undo`, `redo`) writes this
|
|
191
|
+
// synchronously, so multiple actions in one event see the latest
|
|
192
|
+
// reducer output before React commits.
|
|
193
|
+
const stateRef = useRef( state );
|
|
194
|
+
const visualSizeRef = useRef< Size >( { width: 0, height: 0 } );
|
|
195
|
+
|
|
196
|
+
// History stacks: full composite snapshots. Refs avoid re-renders
|
|
197
|
+
// on every push; a boolean state pair drives the enabled/disabled
|
|
198
|
+
// indicators for undo/redo buttons.
|
|
199
|
+
const historyRef = useRef< MediaEditorState[] >( [] );
|
|
200
|
+
const redoStackRef = useRef< MediaEditorState[] >( [] );
|
|
201
|
+
const [ hasUndo, setHasUndo ] = useState( false );
|
|
202
|
+
const [ hasRedo, setHasRedo ] = useState( false );
|
|
203
|
+
|
|
204
|
+
// Gesture state. While open, the first non-no-op action captures
|
|
205
|
+
// the pre-gesture snapshot; subsequent actions don't push.
|
|
206
|
+
// `endGesture` pushes the captured snapshot (or nothing, if the
|
|
207
|
+
// gesture produced no changes).
|
|
208
|
+
const isGestureOpenRef = useRef( false );
|
|
209
|
+
const gestureSnapshotRef = useRef< MediaEditorState | null >( null );
|
|
210
|
+
|
|
211
|
+
const pushSnapshot = useCallback( ( snapshot: MediaEditorState ) => {
|
|
212
|
+
const last = historyRef.current[ historyRef.current.length - 1 ];
|
|
213
|
+
if ( last && areMediaEditorStatesEqual( last, snapshot ) ) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
historyRef.current = [ ...historyRef.current, snapshot ];
|
|
217
|
+
redoStackRef.current = [];
|
|
218
|
+
setHasUndo( true );
|
|
219
|
+
setHasRedo( false );
|
|
220
|
+
}, [] );
|
|
221
|
+
|
|
222
|
+
// Core dispatch wrapper. Synchronously computes the post-state via
|
|
223
|
+
// the pure reducer so we can skip history pushes for no-op actions
|
|
224
|
+
// and keep `stateRef` in lockstep with React's pending state.
|
|
225
|
+
const dispatchWithHistory = useCallback(
|
|
226
|
+
( action: MediaEditorAction, recordHistory = true ) => {
|
|
227
|
+
const preState = stateRef.current;
|
|
228
|
+
const postState = mediaEditorReducer( preState, action );
|
|
229
|
+
if ( recordHistory ) {
|
|
230
|
+
if ( ! areMediaEditorStatesEqual( preState, postState ) ) {
|
|
231
|
+
if ( isGestureOpenRef.current ) {
|
|
232
|
+
if ( gestureSnapshotRef.current === null ) {
|
|
233
|
+
gestureSnapshotRef.current = preState;
|
|
234
|
+
}
|
|
235
|
+
} else {
|
|
236
|
+
pushSnapshot( preState );
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
stateRef.current = postState;
|
|
241
|
+
dispatch( action );
|
|
242
|
+
},
|
|
243
|
+
[ pushSnapshot ]
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
const beginGesture = useCallback( () => {
|
|
247
|
+
if ( isGestureOpenRef.current ) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
isGestureOpenRef.current = true;
|
|
251
|
+
gestureSnapshotRef.current = null;
|
|
252
|
+
}, [] );
|
|
253
|
+
|
|
254
|
+
// Close a gesture (or flush a pending one before undo/redo).
|
|
255
|
+
// Pushes the pre-gesture snapshot only if it differs from current
|
|
256
|
+
// state. Safe to call when no gesture is open.
|
|
257
|
+
const endGesture = useCallback( () => {
|
|
258
|
+
const snapshot = gestureSnapshotRef.current;
|
|
259
|
+
if (
|
|
260
|
+
snapshot &&
|
|
261
|
+
! areMediaEditorStatesEqual( snapshot, stateRef.current )
|
|
262
|
+
) {
|
|
263
|
+
pushSnapshot( snapshot );
|
|
264
|
+
}
|
|
265
|
+
gestureSnapshotRef.current = null;
|
|
266
|
+
isGestureOpenRef.current = false;
|
|
267
|
+
}, [ pushSnapshot ] );
|
|
268
|
+
|
|
269
|
+
// =====================================================================
|
|
270
|
+
// Cropper-slice setters (built from the shared factory so the pure
|
|
271
|
+
// hook and this composite stay in lockstep — add a setter in
|
|
272
|
+
// `buildCropperSetters` once and both hooks get it).
|
|
273
|
+
// =====================================================================
|
|
274
|
+
|
|
275
|
+
const dispatchCropperAction = useCallback(
|
|
276
|
+
( action: CropperAction ) => {
|
|
277
|
+
dispatchWithHistory( { type: 'CROPPER', action } );
|
|
278
|
+
},
|
|
279
|
+
[ dispatchWithHistory ]
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
const cropperSetters = useMemo(
|
|
283
|
+
() =>
|
|
284
|
+
buildCropperSetters(
|
|
285
|
+
dispatchCropperAction,
|
|
286
|
+
() => stateRef.current.cropper
|
|
287
|
+
),
|
|
288
|
+
[ dispatchCropperAction ]
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
const setImage = useCallback( ( image: CropperState[ 'image' ] ) => {
|
|
292
|
+
if ( areCropperImagesEqual( stateRef.current.cropper.image, image ) ) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
// New image = fresh canvas: clear history, refresh the
|
|
296
|
+
// initial snapshot so isDirty starts at false.
|
|
297
|
+
const action = {
|
|
298
|
+
type: 'CROPPER' as const,
|
|
299
|
+
action: { type: 'SET_IMAGE' as const, payload: image },
|
|
300
|
+
};
|
|
301
|
+
const next = mediaEditorReducer( stateRef.current, action );
|
|
302
|
+
stateRef.current = next;
|
|
303
|
+
dispatch( action );
|
|
304
|
+
setInitialBaseline( next );
|
|
305
|
+
isGestureOpenRef.current = false;
|
|
306
|
+
gestureSnapshotRef.current = null;
|
|
307
|
+
historyRef.current = [];
|
|
308
|
+
redoStackRef.current = [];
|
|
309
|
+
setHasUndo( false );
|
|
310
|
+
setHasRedo( false );
|
|
311
|
+
}, [] );
|
|
312
|
+
|
|
313
|
+
const reset = useCallback(
|
|
314
|
+
( resetState?: Partial< CropperState > ) => {
|
|
315
|
+
dispatchWithHistory( {
|
|
316
|
+
type: 'CROPPER',
|
|
317
|
+
action: { type: 'RESET', payload: resetState },
|
|
318
|
+
} );
|
|
319
|
+
},
|
|
320
|
+
[ dispatchWithHistory ]
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
// =====================================================================
|
|
324
|
+
// CropOptions-slice setters
|
|
325
|
+
// =====================================================================
|
|
326
|
+
|
|
327
|
+
const setAspectRatioValue = useCallback(
|
|
328
|
+
( presetKey: string ) => {
|
|
329
|
+
const resolved = resolveAspectRatio(
|
|
330
|
+
presetKey,
|
|
331
|
+
stateRef.current.cropper.image
|
|
332
|
+
);
|
|
333
|
+
dispatchWithHistory( {
|
|
334
|
+
type: 'SET_ASPECT_RATIO_VALUE',
|
|
335
|
+
payload: {
|
|
336
|
+
presetKey,
|
|
337
|
+
resolved,
|
|
338
|
+
visualSize: visualSizeRef.current,
|
|
339
|
+
},
|
|
340
|
+
} );
|
|
341
|
+
},
|
|
342
|
+
[ dispatchWithHistory ]
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
const resetCropOptions = useCallback( () => {
|
|
346
|
+
dispatchWithHistory( { type: 'RESET_CROP_OPTIONS' } );
|
|
347
|
+
}, [ dispatchWithHistory ] );
|
|
348
|
+
|
|
349
|
+
// =====================================================================
|
|
350
|
+
// History
|
|
351
|
+
// =====================================================================
|
|
352
|
+
|
|
353
|
+
const undo = useCallback( () => {
|
|
354
|
+
endGesture();
|
|
355
|
+
const prev = historyRef.current[ historyRef.current.length - 1 ];
|
|
356
|
+
if ( ! prev ) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
redoStackRef.current = [ stateRef.current, ...redoStackRef.current ];
|
|
360
|
+
historyRef.current = historyRef.current.slice( 0, -1 );
|
|
361
|
+
setHasUndo( historyRef.current.length > 0 );
|
|
362
|
+
setHasRedo( true );
|
|
363
|
+
stateRef.current = prev;
|
|
364
|
+
dispatch( { type: 'RESTORE_SNAPSHOT', payload: prev } );
|
|
365
|
+
}, [ endGesture ] );
|
|
366
|
+
|
|
367
|
+
const redo = useCallback( () => {
|
|
368
|
+
endGesture();
|
|
369
|
+
const next = redoStackRef.current[ 0 ];
|
|
370
|
+
if ( ! next ) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
historyRef.current = [ ...historyRef.current, stateRef.current ];
|
|
374
|
+
redoStackRef.current = redoStackRef.current.slice( 1 );
|
|
375
|
+
setHasUndo( true );
|
|
376
|
+
setHasRedo( redoStackRef.current.length > 0 );
|
|
377
|
+
stateRef.current = next;
|
|
378
|
+
dispatch( { type: 'RESTORE_SNAPSHOT', payload: next } );
|
|
379
|
+
}, [ endGesture ] );
|
|
380
|
+
|
|
381
|
+
// =====================================================================
|
|
382
|
+
// Viewport
|
|
383
|
+
// =====================================================================
|
|
384
|
+
|
|
385
|
+
const setVisualSize = useCallback( ( size: Size ) => {
|
|
386
|
+
visualSizeRef.current = size;
|
|
387
|
+
}, [] );
|
|
388
|
+
|
|
389
|
+
const adjustCropRectForViewport = useCallback(
|
|
390
|
+
( rect: NormalizedRect ) => {
|
|
391
|
+
dispatchWithHistory(
|
|
392
|
+
{ type: 'VIEWPORT_ADJUST_CROP_RECT', payload: rect },
|
|
393
|
+
false
|
|
394
|
+
);
|
|
395
|
+
},
|
|
396
|
+
[ dispatchWithHistory ]
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
// =====================================================================
|
|
400
|
+
// Derived
|
|
401
|
+
// =====================================================================
|
|
402
|
+
|
|
403
|
+
const isDirty = ! areMediaEditorStatesEqual( state, initialBaseline );
|
|
404
|
+
const isCropperDirty = ! areCropperStatesEqual(
|
|
405
|
+
state.cropper,
|
|
406
|
+
initialBaseline.cropper
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
const getCroppedImage = useCallback(
|
|
410
|
+
( mimeType?: string, quality?: number ): Promise< Blob > => {
|
|
411
|
+
if ( ! state.cropper.image ) {
|
|
412
|
+
return Promise.reject(
|
|
413
|
+
new Error( 'No image loaded — call setImage first.' )
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
return exportCroppedImage(
|
|
417
|
+
state.cropper.image.src,
|
|
418
|
+
state.cropper,
|
|
419
|
+
mimeType,
|
|
420
|
+
quality
|
|
421
|
+
);
|
|
422
|
+
},
|
|
423
|
+
[ state.cropper ]
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
const controller = useMemo< MediaEditorController >(
|
|
427
|
+
() => ( {
|
|
428
|
+
// CropperController surface (state is the cropper slice so a
|
|
429
|
+
// <Cropper> takes this controller as-is).
|
|
430
|
+
...cropperSetters,
|
|
431
|
+
state: state.cropper,
|
|
432
|
+
setImage,
|
|
433
|
+
reset,
|
|
434
|
+
isDirty,
|
|
435
|
+
getCroppedImage,
|
|
436
|
+
// Composite extensions
|
|
437
|
+
cropOptions: state.cropOptions,
|
|
438
|
+
setAspectRatioValue,
|
|
439
|
+
resetCropOptions,
|
|
440
|
+
isCropperDirty,
|
|
441
|
+
hasUndo,
|
|
442
|
+
hasRedo,
|
|
443
|
+
undo,
|
|
444
|
+
redo,
|
|
445
|
+
beginGesture,
|
|
446
|
+
endGesture,
|
|
447
|
+
setVisualSize,
|
|
448
|
+
adjustCropRectForViewport,
|
|
449
|
+
} ),
|
|
450
|
+
[
|
|
451
|
+
cropperSetters,
|
|
452
|
+
state.cropper,
|
|
453
|
+
setImage,
|
|
454
|
+
reset,
|
|
455
|
+
isDirty,
|
|
456
|
+
getCroppedImage,
|
|
457
|
+
state.cropOptions,
|
|
458
|
+
setAspectRatioValue,
|
|
459
|
+
resetCropOptions,
|
|
460
|
+
isCropperDirty,
|
|
461
|
+
hasUndo,
|
|
462
|
+
hasRedo,
|
|
463
|
+
undo,
|
|
464
|
+
redo,
|
|
465
|
+
beginGesture,
|
|
466
|
+
endGesture,
|
|
467
|
+
setVisualSize,
|
|
468
|
+
adjustCropRectForViewport,
|
|
469
|
+
]
|
|
470
|
+
);
|
|
471
|
+
return controller;
|
|
472
|
+
}
|
package/src/store/actions.ts
CHANGED
|
@@ -15,16 +15,19 @@ export interface MediaEditorModalUpdate {
|
|
|
15
15
|
interface OpenMediaEditorModalArgs {
|
|
16
16
|
id: number;
|
|
17
17
|
onUpdate?: ( updated: MediaEditorModalUpdate ) => void;
|
|
18
|
+
onClose?: () => void;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
export function openMediaEditorModal( {
|
|
21
22
|
id,
|
|
22
23
|
onUpdate,
|
|
24
|
+
onClose,
|
|
23
25
|
}: OpenMediaEditorModalArgs ) {
|
|
24
26
|
return {
|
|
25
27
|
type: 'OPEN_MEDIA_EDITOR_MODAL' as const,
|
|
26
28
|
id,
|
|
27
29
|
onUpdate: onUpdate ?? null,
|
|
30
|
+
onClose: onClose ?? null,
|
|
28
31
|
};
|
|
29
32
|
}
|
|
30
33
|
|
package/src/store/reducer.ts
CHANGED
|
@@ -4,17 +4,20 @@
|
|
|
4
4
|
import type { MediaEditorModalUpdate } from './actions';
|
|
5
5
|
|
|
6
6
|
type OnUpdateCallback = ( updated: MediaEditorModalUpdate ) => void;
|
|
7
|
+
type OnCloseCallback = () => void;
|
|
7
8
|
|
|
8
9
|
export interface State {
|
|
9
10
|
isOpen: boolean;
|
|
10
11
|
id: number | null;
|
|
11
12
|
onUpdate: OnUpdateCallback | null;
|
|
13
|
+
onClose: OnCloseCallback | null;
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
export const DEFAULT_STATE: State = {
|
|
15
17
|
isOpen: false,
|
|
16
18
|
id: null,
|
|
17
19
|
onUpdate: null,
|
|
20
|
+
onClose: null,
|
|
18
21
|
};
|
|
19
22
|
|
|
20
23
|
type Action =
|
|
@@ -22,6 +25,7 @@ type Action =
|
|
|
22
25
|
type: 'OPEN_MEDIA_EDITOR_MODAL';
|
|
23
26
|
id: number;
|
|
24
27
|
onUpdate: OnUpdateCallback | null;
|
|
28
|
+
onClose: OnCloseCallback | null;
|
|
25
29
|
}
|
|
26
30
|
| { type: 'CLOSE_MEDIA_EDITOR_MODAL' };
|
|
27
31
|
|
|
@@ -31,7 +35,7 @@ export default function reducer(
|
|
|
31
35
|
): State {
|
|
32
36
|
switch ( action.type ) {
|
|
33
37
|
case 'OPEN_MEDIA_EDITOR_MODAL': {
|
|
34
|
-
const { id, onUpdate } = action as Extract<
|
|
38
|
+
const { id, onUpdate, onClose } = action as Extract<
|
|
35
39
|
Action,
|
|
36
40
|
{ type: 'OPEN_MEDIA_EDITOR_MODAL' }
|
|
37
41
|
>;
|
|
@@ -39,6 +43,7 @@ export default function reducer(
|
|
|
39
43
|
isOpen: true,
|
|
40
44
|
id,
|
|
41
45
|
onUpdate,
|
|
46
|
+
onClose,
|
|
42
47
|
};
|
|
43
48
|
}
|
|
44
49
|
case 'CLOSE_MEDIA_EDITOR_MODAL':
|
package/src/store/selectors.ts
CHANGED
|
@@ -11,35 +11,43 @@ describe( 'core/media-editor reducer', () => {
|
|
|
11
11
|
|
|
12
12
|
it( 'opens the modal with id and onUpdate', () => {
|
|
13
13
|
const onUpdate = jest.fn();
|
|
14
|
+
const onClose = jest.fn();
|
|
14
15
|
const state = reducer( undefined, {
|
|
15
16
|
type: 'OPEN_MEDIA_EDITOR_MODAL',
|
|
16
17
|
id: 42,
|
|
17
18
|
onUpdate,
|
|
19
|
+
onClose,
|
|
18
20
|
} );
|
|
19
21
|
expect( state ).toEqual( {
|
|
20
22
|
isOpen: true,
|
|
21
23
|
id: 42,
|
|
22
24
|
onUpdate,
|
|
25
|
+
onClose,
|
|
23
26
|
} );
|
|
24
27
|
} );
|
|
25
28
|
|
|
26
29
|
it( 'replaces state on a subsequent open (new onUpdate)', () => {
|
|
27
30
|
const firstOnUpdate = jest.fn();
|
|
31
|
+
const firstOnClose = jest.fn();
|
|
28
32
|
const secondOnUpdate = jest.fn();
|
|
33
|
+
const secondOnClose = jest.fn();
|
|
29
34
|
const first = reducer( undefined, {
|
|
30
35
|
type: 'OPEN_MEDIA_EDITOR_MODAL',
|
|
31
36
|
id: 42,
|
|
32
37
|
onUpdate: firstOnUpdate,
|
|
38
|
+
onClose: firstOnClose,
|
|
33
39
|
} );
|
|
34
40
|
const second = reducer( first, {
|
|
35
41
|
type: 'OPEN_MEDIA_EDITOR_MODAL',
|
|
36
42
|
id: 99,
|
|
37
43
|
onUpdate: secondOnUpdate,
|
|
44
|
+
onClose: secondOnClose,
|
|
38
45
|
} );
|
|
39
46
|
expect( second ).toEqual( {
|
|
40
47
|
isOpen: true,
|
|
41
48
|
id: 99,
|
|
42
49
|
onUpdate: secondOnUpdate,
|
|
50
|
+
onClose: secondOnClose,
|
|
43
51
|
} );
|
|
44
52
|
} );
|
|
45
53
|
|
|
@@ -48,6 +56,7 @@ describe( 'core/media-editor reducer', () => {
|
|
|
48
56
|
type: 'OPEN_MEDIA_EDITOR_MODAL',
|
|
49
57
|
id: 42,
|
|
50
58
|
onUpdate: jest.fn(),
|
|
59
|
+
onClose: jest.fn(),
|
|
51
60
|
} );
|
|
52
61
|
const closed = reducer( opened, {
|
|
53
62
|
type: 'CLOSE_MEDIA_EDITOR_MODAL',
|
package/src/style.scss
CHANGED
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
@use "./components/media-editor/style.scss" as *;
|
|
5
5
|
@use "./components/media-editor-modal/style.scss" as *;
|
|
6
6
|
@use "./components/media-editor-canvas/style.scss" as *;
|
|
7
|
-
@use "./components/media-editor-
|
|
7
|
+
@use "./components/media-editor-transform-controls/style.scss" as *;
|
|
8
8
|
@use "./components/media-editor-keyboard-shortcuts-modal/style.scss" as *;
|
|
9
9
|
@use "./components/rotation-ruler/style.scss" as *;
|
package/tsconfig.json
CHANGED
|
@@ -8,12 +8,15 @@
|
|
|
8
8
|
"references": [
|
|
9
9
|
{ "path": "../api-fetch" },
|
|
10
10
|
{ "path": "../components" },
|
|
11
|
+
{ "path": "../compose" },
|
|
11
12
|
{ "path": "../core-data" },
|
|
12
13
|
{ "path": "../data" },
|
|
13
14
|
{ "path": "../dataviews" },
|
|
15
|
+
{ "path": "../date" },
|
|
14
16
|
{ "path": "../element" },
|
|
15
17
|
{ "path": "../i18n" },
|
|
16
18
|
{ "path": "../icons" },
|
|
19
|
+
{ "path": "../keyboard-shortcuts" },
|
|
17
20
|
{ "path": "../keycodes" },
|
|
18
21
|
{ "path": "../notices" },
|
|
19
22
|
{ "path": "../private-apis" },
|