@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,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
useReducer,
|
|
6
|
+
useCallback,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
} from '@wordpress/element';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Internal dependencies
|
|
14
|
+
*/
|
|
15
|
+
import type {
|
|
16
|
+
CropperState,
|
|
17
|
+
CropperAction,
|
|
18
|
+
NormalizedRect,
|
|
19
|
+
Size,
|
|
20
|
+
} from '../../core/types';
|
|
21
|
+
import { DEFAULT_STATE } from '../../core/constants';
|
|
22
|
+
import { exportCroppedImage } from '../../core/export/canvas-renderer';
|
|
23
|
+
import {
|
|
24
|
+
cropperReducer,
|
|
25
|
+
enforceContainment,
|
|
26
|
+
isStateDirty,
|
|
27
|
+
} from '../../core/state';
|
|
28
|
+
import {
|
|
29
|
+
buildCropperSetters,
|
|
30
|
+
type CropperSetters,
|
|
31
|
+
} from './build-cropper-setters';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The cropper controller surface — the contract the Cropper component
|
|
35
|
+
* and its interaction hooks consume.
|
|
36
|
+
*
|
|
37
|
+
* History (undo / redo / commitHistory) is intentionally NOT part of
|
|
38
|
+
* this interface. The cropper is a pure state machine; history is a
|
|
39
|
+
* higher-layer concern owned by a composite store
|
|
40
|
+
* (see media-editor/src/state).
|
|
41
|
+
*/
|
|
42
|
+
export interface CropperController extends CropperSetters {
|
|
43
|
+
/** The current cropper state (read-only). */
|
|
44
|
+
state: CropperState;
|
|
45
|
+
/** Set the loaded image (natural size and src). */
|
|
46
|
+
setImage: ( image: CropperState[ 'image' ] ) => void;
|
|
47
|
+
/** Reset the state. Optionally merge partial state overrides. */
|
|
48
|
+
reset: ( resetState?: Partial< CropperState > ) => void;
|
|
49
|
+
/** Whether the current state differs from the initial state. */
|
|
50
|
+
isDirty: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Export the cropped image as a Blob. Throws on failure — see
|
|
53
|
+
* `exportCroppedImage` for error semantics.
|
|
54
|
+
*/
|
|
55
|
+
getCroppedImage: ( mimeType?: string, quality?: number ) => Promise< Blob >;
|
|
56
|
+
/**
|
|
57
|
+
* Report the current rendered image size to the controller. The
|
|
58
|
+
* Cropper component calls this on visualSize changes; composite
|
|
59
|
+
* controllers use it to compute aspect-ratio reshapes. The pure
|
|
60
|
+
* cropper hook ignores it (no-op).
|
|
61
|
+
*/
|
|
62
|
+
setVisualSize: ( size: Size ) => void;
|
|
63
|
+
/**
|
|
64
|
+
* Adjust the cropRect in response to a viewport change (window
|
|
65
|
+
* resize, canvas resize). Behaves like `setCropRect` but, in
|
|
66
|
+
* composite controllers, does NOT record an undo entry — viewport
|
|
67
|
+
* reshapes aren't editor actions. The pure cropper hook aliases
|
|
68
|
+
* this to `setCropRect` (history is a composite concern).
|
|
69
|
+
*/
|
|
70
|
+
adjustCropRectForViewport: ( rect: NormalizedRect ) => void;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function areCropperImagesEqual(
|
|
74
|
+
a: CropperState[ 'image' ],
|
|
75
|
+
b: CropperState[ 'image' ]
|
|
76
|
+
): boolean {
|
|
77
|
+
return (
|
|
78
|
+
a?.src === b?.src &&
|
|
79
|
+
a?.naturalWidth === b?.naturalWidth &&
|
|
80
|
+
a?.naturalHeight === b?.naturalHeight
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Pure reducer hook for the image cropper.
|
|
86
|
+
*
|
|
87
|
+
* Wraps the framework-agnostic `cropperReducer` and exposes named
|
|
88
|
+
* setters (built from the shared `buildCropperSetters` factory).
|
|
89
|
+
* Does NOT manage undo/redo history — that responsibility belongs to
|
|
90
|
+
* the surrounding application (see `useMediaEditorState` for the
|
|
91
|
+
* composite store used by the media editor).
|
|
92
|
+
*
|
|
93
|
+
* @param initialState Optional partial state to merge with DEFAULT_STATE.
|
|
94
|
+
* @return The cropper controller — state, setters, and helpers.
|
|
95
|
+
*/
|
|
96
|
+
export function useCropperReducer(
|
|
97
|
+
initialState?: Partial< CropperState >
|
|
98
|
+
): CropperController {
|
|
99
|
+
const [ state, dispatch ] = useReducer(
|
|
100
|
+
cropperReducer,
|
|
101
|
+
initialState,
|
|
102
|
+
( init ) => enforceContainment( { ...DEFAULT_STATE, ...init } )
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
// The "clean" snapshot the current state is compared against for
|
|
106
|
+
// `isDirty`. Stored in state (not a ref) so the comparison reads
|
|
107
|
+
// a value, not a ref's `current`. `setImage` and `reset` refresh
|
|
108
|
+
// this when the consumer establishes a new baseline.
|
|
109
|
+
const [ initialBaseline, setInitialBaseline ] = useState< CropperState >(
|
|
110
|
+
() => enforceContainment( { ...DEFAULT_STATE, ...initialState } )
|
|
111
|
+
);
|
|
112
|
+
// Latest-state ref used by setters that need to read fresh state
|
|
113
|
+
// (focal-point zoom, toggleFlip, reset, setImage). Updated
|
|
114
|
+
// synchronously inside `dispatchAndSync` so multiple actions in
|
|
115
|
+
// one event see each other's output before React commits.
|
|
116
|
+
const stateRef = useRef( state );
|
|
117
|
+
|
|
118
|
+
const dispatchAndSync = useCallback( ( action: CropperAction ) => {
|
|
119
|
+
const next = cropperReducer( stateRef.current, action );
|
|
120
|
+
stateRef.current = next;
|
|
121
|
+
dispatch( action );
|
|
122
|
+
return next;
|
|
123
|
+
}, [] );
|
|
124
|
+
|
|
125
|
+
const setImage = useCallback(
|
|
126
|
+
( image: CropperState[ 'image' ] ) => {
|
|
127
|
+
if ( areCropperImagesEqual( stateRef.current.image, image ) ) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const next = dispatchAndSync( {
|
|
131
|
+
type: 'SET_IMAGE',
|
|
132
|
+
payload: image,
|
|
133
|
+
} );
|
|
134
|
+
// Refresh the "clean" snapshot to match the post-load state
|
|
135
|
+
// produced by the reducer. Otherwise containment can nudge
|
|
136
|
+
// pan/zoom by tiny amounts on load and `isDirty` would
|
|
137
|
+
// report true from the start.
|
|
138
|
+
setInitialBaseline( next );
|
|
139
|
+
},
|
|
140
|
+
[ dispatchAndSync ]
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const reset = useCallback(
|
|
144
|
+
( resetState?: Partial< CropperState > ) => {
|
|
145
|
+
const nextInitialState = dispatchAndSync( {
|
|
146
|
+
type: 'RESET',
|
|
147
|
+
payload: resetState,
|
|
148
|
+
} );
|
|
149
|
+
// Mirror the reducer's RESET exactly so isDirty stays in
|
|
150
|
+
// sync. The containment step can tweak pan/zoom/cropRect by
|
|
151
|
+
// float ulp; fold those into the "initial" snapshot or
|
|
152
|
+
// isDirty would report true after a reset.
|
|
153
|
+
setInitialBaseline( nextInitialState );
|
|
154
|
+
},
|
|
155
|
+
[ dispatchAndSync ]
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
const setters = useMemo(
|
|
159
|
+
() => buildCropperSetters( dispatchAndSync, () => stateRef.current ),
|
|
160
|
+
[ dispatchAndSync ]
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
const isDirty = isStateDirty( state, initialBaseline );
|
|
164
|
+
|
|
165
|
+
const getCroppedImage = useCallback(
|
|
166
|
+
( mimeType?: string, quality?: number ): Promise< Blob > => {
|
|
167
|
+
if ( ! state.image ) {
|
|
168
|
+
return Promise.reject(
|
|
169
|
+
new Error( 'No image loaded — call setImage first.' )
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
return exportCroppedImage(
|
|
173
|
+
state.image.src,
|
|
174
|
+
state,
|
|
175
|
+
mimeType,
|
|
176
|
+
quality
|
|
177
|
+
);
|
|
178
|
+
},
|
|
179
|
+
[ state ]
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
// No-op for the pure hook — visualSize matters only when a
|
|
183
|
+
// composite store needs to compute aspect-ratio reshapes
|
|
184
|
+
// (see useMediaEditorState). Typed to accept a Size so it
|
|
185
|
+
// satisfies the controller surface; the value is intentionally
|
|
186
|
+
// ignored.
|
|
187
|
+
const setVisualSize = useCallback( () => {}, [] );
|
|
188
|
+
|
|
189
|
+
// In the pure hook, history isn't a concern — viewport-driven
|
|
190
|
+
// reshape is identical to setCropRect.
|
|
191
|
+
const adjustCropRectForViewport = setters.setCropRect;
|
|
192
|
+
|
|
193
|
+
const controller: CropperController = {
|
|
194
|
+
...setters,
|
|
195
|
+
state,
|
|
196
|
+
setImage,
|
|
197
|
+
reset,
|
|
198
|
+
isDirty,
|
|
199
|
+
getCroppedImage,
|
|
200
|
+
setVisualSize,
|
|
201
|
+
adjustCropRectForViewport,
|
|
202
|
+
};
|
|
203
|
+
return controller;
|
|
204
|
+
}
|
|
@@ -29,14 +29,14 @@ import {
|
|
|
29
29
|
* Internal dependencies
|
|
30
30
|
*/
|
|
31
31
|
import { Cropper } from '../react/components/cropper';
|
|
32
|
-
import {
|
|
32
|
+
import { useCropperReducer } from '../react/hooks/use-cropper-reducer';
|
|
33
33
|
import {
|
|
34
|
-
MIN_ZOOM,
|
|
35
34
|
MAX_ZOOM,
|
|
36
35
|
MAX_ROTATION_OFFSET,
|
|
37
36
|
DEFAULT_ASPECT_RATIOS,
|
|
38
37
|
ORIGINAL_ASPECT_RATIO,
|
|
39
38
|
} from '../core/constants';
|
|
39
|
+
import { getMinZoom, restrictPanZoom } from '../core/containment';
|
|
40
40
|
import {
|
|
41
41
|
loadImage,
|
|
42
42
|
renderToCanvas,
|
|
@@ -50,7 +50,6 @@ import {
|
|
|
50
50
|
getImageFit,
|
|
51
51
|
getVisibleBounds,
|
|
52
52
|
} from '../core/camera';
|
|
53
|
-
import { restrictPanZoom } from '../core/containment';
|
|
54
53
|
import { getSourceRegion } from '../core/source-region';
|
|
55
54
|
import './style.css';
|
|
56
55
|
|
|
@@ -143,7 +142,7 @@ type Story = StoryObj< typeof Cropper >;
|
|
|
143
142
|
* Default story. Basic cropper with a sample image, no controls.
|
|
144
143
|
*/
|
|
145
144
|
const DefaultComponent = () => {
|
|
146
|
-
const controller =
|
|
145
|
+
const controller = useCropperReducer();
|
|
147
146
|
|
|
148
147
|
return (
|
|
149
148
|
<div>
|
|
@@ -167,7 +166,7 @@ export const Default: Story = {
|
|
|
167
166
|
* Full interactive demo with controls.
|
|
168
167
|
*/
|
|
169
168
|
const WithControlsComponent = () => {
|
|
170
|
-
const controller =
|
|
169
|
+
const controller = useCropperReducer();
|
|
171
170
|
const {
|
|
172
171
|
state,
|
|
173
172
|
setRotation,
|
|
@@ -397,7 +396,6 @@ const WithControlsComponent = () => {
|
|
|
397
396
|
<FlexItem>
|
|
398
397
|
<SelectControl
|
|
399
398
|
__next40pxDefaultSize
|
|
400
|
-
__nextHasNoMarginBottom
|
|
401
399
|
label="Aspect ratio"
|
|
402
400
|
hideLabelFromVision
|
|
403
401
|
value={ aspectRatioValue }
|
|
@@ -412,7 +410,6 @@ const WithControlsComponent = () => {
|
|
|
412
410
|
</FlexItem>
|
|
413
411
|
<FlexItem>
|
|
414
412
|
<ToggleControl
|
|
415
|
-
__nextHasNoMarginBottom
|
|
416
413
|
label="Freeform"
|
|
417
414
|
checked={ freeformCrop }
|
|
418
415
|
onChange={ setFreeformCrop }
|
|
@@ -421,7 +418,6 @@ const WithControlsComponent = () => {
|
|
|
421
418
|
<FlexItem>
|
|
422
419
|
<SelectControl
|
|
423
420
|
__next40pxDefaultSize
|
|
424
|
-
__nextHasNoMarginBottom
|
|
425
421
|
label="Grid"
|
|
426
422
|
hideLabelFromVision
|
|
427
423
|
value={ gridMode }
|
|
@@ -455,7 +451,6 @@ const WithControlsComponent = () => {
|
|
|
455
451
|
<div className="image-editor-story__sliders">
|
|
456
452
|
<RangeControl
|
|
457
453
|
__next40pxDefaultSize
|
|
458
|
-
__nextHasNoMarginBottom
|
|
459
454
|
label="Fine rotation"
|
|
460
455
|
min={ -MAX_ROTATION_OFFSET }
|
|
461
456
|
max={ MAX_ROTATION_OFFSET }
|
|
@@ -465,9 +460,8 @@ const WithControlsComponent = () => {
|
|
|
465
460
|
/>
|
|
466
461
|
<RangeControl
|
|
467
462
|
__next40pxDefaultSize
|
|
468
|
-
__nextHasNoMarginBottom
|
|
469
463
|
label="Zoom"
|
|
470
|
-
min={
|
|
464
|
+
min={ getMinZoom( state ) }
|
|
471
465
|
max={ MAX_ZOOM }
|
|
472
466
|
step={ 0.1 }
|
|
473
467
|
value={ state.zoom }
|
|
@@ -545,7 +539,7 @@ export const WithControls: Story = {
|
|
|
545
539
|
* or verify the camera and render paths agree.
|
|
546
540
|
*/
|
|
547
541
|
const DebugComponent = () => {
|
|
548
|
-
const controller =
|
|
542
|
+
const controller = useCropperReducer();
|
|
549
543
|
const {
|
|
550
544
|
state,
|
|
551
545
|
setRotation,
|
|
@@ -817,7 +811,6 @@ const DebugComponent = () => {
|
|
|
817
811
|
</FlexItem>
|
|
818
812
|
<FlexItem>
|
|
819
813
|
<ToggleControl
|
|
820
|
-
__nextHasNoMarginBottom
|
|
821
814
|
label="Freeform"
|
|
822
815
|
checked={ freeformCrop }
|
|
823
816
|
onChange={ setFreeformCrop }
|
|
@@ -839,7 +832,6 @@ const DebugComponent = () => {
|
|
|
839
832
|
<FlexItem>
|
|
840
833
|
<SelectControl
|
|
841
834
|
__next40pxDefaultSize
|
|
842
|
-
__nextHasNoMarginBottom
|
|
843
835
|
label="Format"
|
|
844
836
|
hideLabelFromVision
|
|
845
837
|
value={ exportFormat as 'image/jpeg' }
|
|
@@ -875,7 +867,6 @@ const DebugComponent = () => {
|
|
|
875
867
|
<div className="image-editor-story__sliders">
|
|
876
868
|
<RangeControl
|
|
877
869
|
__next40pxDefaultSize
|
|
878
|
-
__nextHasNoMarginBottom
|
|
879
870
|
label="Fine rotation"
|
|
880
871
|
min={ -MAX_ROTATION_OFFSET }
|
|
881
872
|
max={ MAX_ROTATION_OFFSET }
|
|
@@ -885,9 +876,8 @@ const DebugComponent = () => {
|
|
|
885
876
|
/>
|
|
886
877
|
<RangeControl
|
|
887
878
|
__next40pxDefaultSize
|
|
888
|
-
__nextHasNoMarginBottom
|
|
889
879
|
label="Zoom"
|
|
890
|
-
min={
|
|
880
|
+
min={ getMinZoom( state ) }
|
|
891
881
|
max={ MAX_ZOOM }
|
|
892
882
|
step={ 0.1 }
|
|
893
883
|
value={ state.zoom }
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
cropperReducer,
|
|
6
|
+
areCropperStatesEqual,
|
|
7
|
+
computeInscribedRect,
|
|
8
|
+
type CropperState,
|
|
9
|
+
} from '../image-editor';
|
|
10
|
+
import {
|
|
11
|
+
DEFAULT_CROP_OPTIONS,
|
|
12
|
+
type CropOptionsSlice,
|
|
13
|
+
type MediaEditorAction,
|
|
14
|
+
type MediaEditorState,
|
|
15
|
+
} from './types';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Pure composite reducer for the media editor.
|
|
19
|
+
*
|
|
20
|
+
* Delegates `CROPPER`-tagged actions to the framework-agnostic
|
|
21
|
+
* `cropperReducer` and owns first-class transitions for the sidebar
|
|
22
|
+
* `cropOptions` slice. `RESTORE_SNAPSHOT` replaces the whole composite
|
|
23
|
+
* in one transaction (used by undo/redo).
|
|
24
|
+
*
|
|
25
|
+
* Note: this reducer carries NO history of its own. Snapshots and
|
|
26
|
+
* stacks live in the `useMediaEditorState` hook that wraps it.
|
|
27
|
+
*
|
|
28
|
+
* @param state Current composite state.
|
|
29
|
+
* @param action Action to apply.
|
|
30
|
+
* @return Next composite state.
|
|
31
|
+
*/
|
|
32
|
+
export function mediaEditorReducer(
|
|
33
|
+
state: MediaEditorState,
|
|
34
|
+
action: MediaEditorAction
|
|
35
|
+
): MediaEditorState {
|
|
36
|
+
switch ( action.type ) {
|
|
37
|
+
case 'CROPPER': {
|
|
38
|
+
const nextCropper = cropperReducer( state.cropper, action.action );
|
|
39
|
+
if ( nextCropper === state.cropper ) {
|
|
40
|
+
return state;
|
|
41
|
+
}
|
|
42
|
+
return { ...state, cropper: nextCropper };
|
|
43
|
+
}
|
|
44
|
+
case 'SET_ASPECT_RATIO_VALUE': {
|
|
45
|
+
const { presetKey, resolved, visualSize } = action.payload;
|
|
46
|
+
// Atomically: update the preset key AND reshape the
|
|
47
|
+
// cropRect (when the ratio is non-Free and we have a
|
|
48
|
+
// visualSize). One transaction = one undo entry.
|
|
49
|
+
const shouldReshape =
|
|
50
|
+
resolved !== undefined &&
|
|
51
|
+
resolved > 0 &&
|
|
52
|
+
visualSize.width > 0 &&
|
|
53
|
+
visualSize.height > 0;
|
|
54
|
+
const nextCropOptions: CropOptionsSlice = {
|
|
55
|
+
...state.cropOptions,
|
|
56
|
+
aspectRatioValue: presetKey,
|
|
57
|
+
};
|
|
58
|
+
if ( ! shouldReshape ) {
|
|
59
|
+
if ( state.cropOptions.aspectRatioValue === presetKey ) {
|
|
60
|
+
return state;
|
|
61
|
+
}
|
|
62
|
+
return { ...state, cropOptions: nextCropOptions };
|
|
63
|
+
}
|
|
64
|
+
const newCropRect = computeInscribedRect( resolved, visualSize );
|
|
65
|
+
const nextCropper = cropperReducer( state.cropper, {
|
|
66
|
+
type: 'SET_CROP_RECT',
|
|
67
|
+
payload: newCropRect,
|
|
68
|
+
} );
|
|
69
|
+
return {
|
|
70
|
+
cropper: nextCropper,
|
|
71
|
+
cropOptions: nextCropOptions,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
case 'RESET_CROP_OPTIONS': {
|
|
75
|
+
if (
|
|
76
|
+
state.cropOptions.aspectRatioValue ===
|
|
77
|
+
DEFAULT_CROP_OPTIONS.aspectRatioValue
|
|
78
|
+
) {
|
|
79
|
+
return state;
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
...state,
|
|
83
|
+
cropOptions: { ...DEFAULT_CROP_OPTIONS },
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
case 'RESTORE_SNAPSHOT':
|
|
87
|
+
return action.payload;
|
|
88
|
+
case 'VIEWPORT_ADJUST_CROP_RECT': {
|
|
89
|
+
const nextCropper = cropperReducer( state.cropper, {
|
|
90
|
+
type: 'SET_CROP_RECT',
|
|
91
|
+
payload: action.payload,
|
|
92
|
+
} );
|
|
93
|
+
if ( nextCropper === state.cropper ) {
|
|
94
|
+
return state;
|
|
95
|
+
}
|
|
96
|
+
return { ...state, cropper: nextCropper };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Value-equality check for two composite states. Used by the history
|
|
103
|
+
* machinery to dedup snapshots when an action turned out to be a no-op.
|
|
104
|
+
*
|
|
105
|
+
* @param a First composite state.
|
|
106
|
+
* @param b Second composite state.
|
|
107
|
+
* @return Whether `a` and `b` are value-equal across all slices.
|
|
108
|
+
*/
|
|
109
|
+
export function areMediaEditorStatesEqual(
|
|
110
|
+
a: MediaEditorState,
|
|
111
|
+
b: MediaEditorState
|
|
112
|
+
): boolean {
|
|
113
|
+
if ( a === b ) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
return (
|
|
117
|
+
areCropperStatesEqual( a.cropper, b.cropper ) &&
|
|
118
|
+
a.cropOptions.aspectRatioValue === b.cropOptions.aspectRatioValue
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Build the initial composite state from optional partial overrides.
|
|
124
|
+
*
|
|
125
|
+
* @param initialCropper Initial cropper-slice fields to merge.
|
|
126
|
+
* @param initialOptions Initial cropOptions-slice fields to merge.
|
|
127
|
+
* @return Composite initial state.
|
|
128
|
+
*/
|
|
129
|
+
export function buildInitialMediaEditorState(
|
|
130
|
+
initialCropper: CropperState,
|
|
131
|
+
initialOptions?: Partial< CropOptionsSlice >
|
|
132
|
+
): MediaEditorState {
|
|
133
|
+
return {
|
|
134
|
+
cropper: initialCropper,
|
|
135
|
+
cropOptions: { ...DEFAULT_CROP_OPTIONS, ...initialOptions },
|
|
136
|
+
};
|
|
137
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export {
|
|
2
|
+
useMediaEditorState,
|
|
3
|
+
resolveAspectRatio,
|
|
4
|
+
} from './use-media-editor-state';
|
|
5
|
+
export type { MediaEditorController } from './use-media-editor-state';
|
|
6
|
+
export {
|
|
7
|
+
MediaEditorStateProvider,
|
|
8
|
+
useMediaEditor,
|
|
9
|
+
} from './media-editor-state-provider';
|
|
10
|
+
export {
|
|
11
|
+
mediaEditorReducer,
|
|
12
|
+
areMediaEditorStatesEqual,
|
|
13
|
+
buildInitialMediaEditorState,
|
|
14
|
+
} from './composite-reducer';
|
|
15
|
+
export { DEFAULT_CROP_OPTIONS } from './types';
|
|
16
|
+
export type {
|
|
17
|
+
CropOptionsSlice,
|
|
18
|
+
MediaEditorState,
|
|
19
|
+
MediaEditorAction,
|
|
20
|
+
} from './types';
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { createContext, useContext } from '@wordpress/element';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import type { CropperState } from '../image-editor';
|
|
10
|
+
import {
|
|
11
|
+
useMediaEditorState,
|
|
12
|
+
type MediaEditorController,
|
|
13
|
+
} from './use-media-editor-state';
|
|
14
|
+
import type { CropOptionsSlice } from './types';
|
|
15
|
+
|
|
16
|
+
const MediaEditorStateContext = createContext< MediaEditorController | null >(
|
|
17
|
+
null
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
interface MediaEditorStateProviderProps {
|
|
21
|
+
/** Optional cropper-slice initial state. */
|
|
22
|
+
initialCropperState?: Partial< CropperState >;
|
|
23
|
+
/** Optional cropOptions-slice initial state. */
|
|
24
|
+
initialCropOptions?: Partial< CropOptionsSlice >;
|
|
25
|
+
/** Child components. */
|
|
26
|
+
children: React.ReactNode;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Provider that vends the composite media-editor controller via
|
|
31
|
+
* context. Wrap the media editor in this so every consumer
|
|
32
|
+
* (canvas, toolbar, sidebar) reads from the same store.
|
|
33
|
+
*
|
|
34
|
+
* @param props
|
|
35
|
+
* @param props.initialCropperState
|
|
36
|
+
* @param props.initialCropOptions
|
|
37
|
+
* @param props.children
|
|
38
|
+
*/
|
|
39
|
+
export function MediaEditorStateProvider( {
|
|
40
|
+
initialCropperState,
|
|
41
|
+
initialCropOptions,
|
|
42
|
+
children,
|
|
43
|
+
}: MediaEditorStateProviderProps ) {
|
|
44
|
+
const controller = useMediaEditorState( {
|
|
45
|
+
cropper: initialCropperState,
|
|
46
|
+
cropOptions: initialCropOptions,
|
|
47
|
+
} );
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<MediaEditorStateContext.Provider value={ controller }>
|
|
51
|
+
{ children }
|
|
52
|
+
</MediaEditorStateContext.Provider>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Consume the composite media-editor controller. Throws if used
|
|
58
|
+
* outside a `<MediaEditorStateProvider>`.
|
|
59
|
+
*
|
|
60
|
+
* @return The composite controller.
|
|
61
|
+
*/
|
|
62
|
+
export function useMediaEditor(): MediaEditorController {
|
|
63
|
+
const context = useContext( MediaEditorStateContext );
|
|
64
|
+
if ( ! context ) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
'useMediaEditor must be used within a MediaEditorStateProvider.'
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return context;
|
|
70
|
+
}
|