@wordpress/media-editor 0.21.0 → 0.22.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/CHANGELOG.md +12 -0
- package/build/components/media-editor/index.cjs +533 -686
- package/build/components/media-editor/index.cjs.map +1 -7
- package/build/components/media-editor/use-crop-options.cjs +43 -57
- package/build/components/media-editor/use-crop-options.cjs.map +1 -7
- package/build/components/media-editor/use-save-media-editor.cjs +111 -185
- package/build/components/media-editor/use-save-media-editor.cjs.map +1 -7
- package/build/components/media-editor-canvas/index.cjs +122 -124
- package/build/components/media-editor-canvas/index.cjs.map +1 -7
- package/build/components/media-editor-crop-panel/index.cjs +65 -72
- package/build/components/media-editor-crop-panel/index.cjs.map +1 -7
- package/build/components/media-editor-fine-rotation/index.cjs +62 -72
- package/build/components/media-editor-fine-rotation/index.cjs.map +1 -7
- package/build/components/media-editor-image-controls/index.cjs +183 -257
- package/build/components/media-editor-image-controls/index.cjs.map +1 -7
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +119 -157
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +1 -7
- package/build/components/media-editor-modal/build-modifiers.cjs +107 -88
- package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -7
- package/build/components/media-editor-modal/index.cjs +112 -155
- package/build/components/media-editor-modal/index.cjs.map +1 -7
- package/build/components/media-editor-provider/index.cjs +32 -55
- package/build/components/media-editor-provider/index.cjs.map +1 -7
- package/build/components/media-form/index.cjs +76 -77
- package/build/components/media-form/index.cjs.map +1 -7
- package/build/components/media-preview/index.cjs +114 -114
- package/build/components/media-preview/index.cjs.map +1 -7
- package/build/components/rotation-ruler/index.cjs +181 -188
- package/build/components/rotation-ruler/index.cjs.map +1 -7
- package/build/components/rotation-ruler/use-ruler-drag.cjs +132 -149
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -7
- package/build/hooks/use-crop-gesture-handlers.cjs +82 -76
- package/build/hooks/use-crop-gesture-handlers.cjs.map +1 -7
- package/build/image-editor/core/camera.cjs +257 -205
- package/build/image-editor/core/camera.cjs.map +1 -7
- package/build/image-editor/core/constants.cjs +127 -102
- package/build/image-editor/core/constants.cjs.map +1 -7
- package/build/image-editor/core/containment.cjs +275 -262
- package/build/image-editor/core/containment.cjs.map +1 -7
- package/build/image-editor/core/crop-rect.cjs +33 -44
- package/build/image-editor/core/crop-rect.cjs.map +1 -7
- package/build/image-editor/core/export/canvas-renderer.cjs +165 -135
- package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -7
- package/build/image-editor/core/index.cjs +88 -61
- package/build/image-editor/core/interaction-controller.cjs +583 -646
- package/build/image-editor/core/interaction-controller.cjs.map +1 -7
- package/build/image-editor/core/math/rotation.cjs +26 -35
- package/build/image-editor/core/math/rotation.cjs.map +1 -7
- package/build/image-editor/core/math/sanitize.cjs +124 -80
- package/build/image-editor/core/math/sanitize.cjs.map +1 -7
- package/build/image-editor/core/setter-helpers.cjs +57 -60
- package/build/image-editor/core/setter-helpers.cjs.map +1 -7
- package/build/image-editor/core/source-region.cjs +220 -190
- package/build/image-editor/core/source-region.cjs.map +1 -7
- package/build/image-editor/core/state.cjs +278 -265
- package/build/image-editor/core/state.cjs.map +1 -7
- package/build/image-editor/core/stencil-math.cjs +193 -206
- package/build/image-editor/core/stencil-math.cjs.map +1 -7
- package/build/image-editor/core/transform-style.cjs +39 -48
- package/build/image-editor/core/transform-style.cjs.map +1 -7
- package/build/image-editor/core/transforms/pipeline.cjs +36 -38
- package/build/image-editor/core/transforms/pipeline.cjs.map +1 -7
- package/build/image-editor/core/types.cjs +4 -18
- package/build/image-editor/core/viewport-state.cjs +31 -45
- package/build/image-editor/core/viewport-state.cjs.map +1 -7
- package/build/image-editor/index.cjs +65 -64
- package/build/image-editor/react/components/cropper-provider.cjs +44 -45
- package/build/image-editor/react/components/cropper-provider.cjs.map +1 -7
- package/build/image-editor/react/components/cropper.cjs +583 -710
- package/build/image-editor/react/components/cropper.cjs.map +1 -7
- package/build/image-editor/react/components/index.cjs +22 -33
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +135 -160
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -7
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +28 -59
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +1 -7
- package/build/image-editor/react/components/overlays/grid-overlay.cjs +58 -88
- package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +1 -7
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +361 -420
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +1 -7
- package/build/image-editor/react/components/viewport-provider.cjs +21 -46
- package/build/image-editor/react/components/viewport-provider.cjs.map +1 -7
- package/build/image-editor/react/hooks/build-cropper-setters.cjs +76 -67
- package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -7
- package/build/image-editor/react/hooks/index.cjs +16 -31
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +90 -146
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +1 -7
- package/build/image-editor/react/hooks/use-cropper-controller.cjs +80 -0
- package/build/image-editor/react/hooks/use-cropper-controller.cjs.map +1 -0
- package/build/image-editor/react/hooks/use-interaction.cjs +179 -205
- package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -7
- package/build/image-editor/react/hooks/use-transform-style.cjs +20 -34
- package/build/image-editor/react/hooks/use-transform-style.cjs.map +1 -7
- package/build/image-editor/react/hooks/use-viewport-state.cjs +36 -47
- package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -7
- package/build/image-editor/react/visually-hidden-style.cjs +21 -38
- package/build/image-editor/react/visually-hidden-style.cjs.map +1 -7
- package/build/index.cjs +45 -39
- package/build/lock-unlock.cjs +9 -35
- package/build/lock-unlock.cjs.map +1 -7
- package/build/private-apis.cjs +16 -37
- package/build/private-apis.cjs.map +1 -7
- package/build/state/aspect-ratio-presets.cjs +53 -0
- package/build/state/aspect-ratio-presets.cjs.map +1 -0
- package/build/state/composite-reducer.cjs +134 -112
- package/build/state/composite-reducer.cjs.map +1 -7
- package/build/state/index.cjs +63 -47
- package/build/state/media-editor-state-provider.cjs +35 -48
- package/build/state/media-editor-state-provider.cjs.map +1 -7
- package/build/state/types.cjs +12 -31
- package/build/state/types.cjs.map +1 -7
- package/build/state/use-media-editor-state.cjs +261 -294
- package/build/state/use-media-editor-state.cjs.map +1 -7
- package/build/store/actions.cjs +17 -44
- package/build/store/actions.cjs.map +1 -7
- package/build/store/constants.cjs +8 -29
- package/build/store/constants.cjs.map +1 -7
- package/build/store/index.cjs +33 -40
- package/build/store/index.cjs.map +1 -7
- package/build/store/reducer.cjs +28 -50
- package/build/store/reducer.cjs.map +1 -7
- package/build/store/selectors.cjs +16 -41
- package/build/store/selectors.cjs.map +1 -7
- package/build/utils/get-media-type.cjs +18 -41
- package/build/utils/get-media-type.cjs.map +1 -7
- package/build/utils/index.cjs +9 -28
- package/build-module/components/media-editor/index.mjs +490 -673
- package/build-module/components/media-editor/index.mjs.map +1 -7
- package/build-module/components/media-editor/use-crop-options.mjs +38 -32
- package/build-module/components/media-editor/use-crop-options.mjs.map +1 -7
- package/build-module/components/media-editor/use-save-media-editor.mjs +89 -157
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +1 -7
- package/build-module/components/media-editor-canvas/index.mjs +98 -96
- package/build-module/components/media-editor-canvas/index.mjs.map +1 -7
- package/build-module/components/media-editor-crop-panel/index.mjs +43 -46
- package/build-module/components/media-editor-crop-panel/index.mjs.map +1 -7
- package/build-module/components/media-editor-fine-rotation/index.mjs +39 -45
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -7
- package/build-module/components/media-editor-image-controls/index.mjs +172 -237
- package/build-module/components/media-editor-image-controls/index.mjs.map +1 -7
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +112 -137
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +1 -7
- package/build-module/components/media-editor-modal/build-modifiers.mjs +103 -64
- package/build-module/components/media-editor-modal/build-modifiers.mjs.map +1 -7
- package/build-module/components/media-editor-modal/index.mjs +88 -124
- package/build-module/components/media-editor-modal/index.mjs.map +1 -7
- package/build-module/components/media-editor-provider/index.mjs +26 -29
- package/build-module/components/media-editor-provider/index.mjs.map +1 -7
- package/build-module/components/media-form/index.mjs +68 -56
- package/build-module/components/media-form/index.mjs.map +1 -7
- package/build-module/components/media-preview/index.mjs +106 -90
- package/build-module/components/media-preview/index.mjs.map +1 -7
- package/build-module/components/rotation-ruler/index.mjs +161 -164
- package/build-module/components/rotation-ruler/index.mjs.map +1 -7
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs +125 -122
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -7
- package/build-module/hooks/use-crop-gesture-handlers.mjs +76 -50
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +1 -7
- package/build-module/image-editor/core/camera.mjs +245 -178
- package/build-module/image-editor/core/camera.mjs.map +1 -7
- package/build-module/image-editor/core/constants.mjs +110 -64
- package/build-module/image-editor/core/constants.mjs.map +1 -7
- package/build-module/image-editor/core/containment.mjs +265 -237
- package/build-module/image-editor/core/containment.mjs.map +1 -7
- package/build-module/image-editor/core/crop-rect.mjs +29 -20
- package/build-module/image-editor/core/crop-rect.mjs.map +1 -7
- package/build-module/image-editor/core/export/canvas-renderer.mjs +155 -105
- package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -7
- package/build-module/image-editor/core/index.mjs +5 -34
- package/build-module/image-editor/core/interaction-controller.mjs +579 -628
- package/build-module/image-editor/core/interaction-controller.mjs.map +1 -7
- package/build-module/image-editor/core/math/rotation.mjs +21 -10
- package/build-module/image-editor/core/math/rotation.mjs.map +1 -7
- package/build-module/image-editor/core/math/sanitize.mjs +116 -52
- package/build-module/image-editor/core/math/sanitize.mjs.map +1 -7
- package/build-module/image-editor/core/setter-helpers.mjs +52 -35
- package/build-module/image-editor/core/setter-helpers.mjs.map +1 -7
- package/build-module/image-editor/core/source-region.mjs +210 -160
- package/build-module/image-editor/core/source-region.mjs.map +1 -7
- package/build-module/image-editor/core/state.mjs +271 -243
- package/build-module/image-editor/core/state.mjs.map +1 -7
- package/build-module/image-editor/core/stencil-math.mjs +185 -178
- package/build-module/image-editor/core/stencil-math.mjs.map +1 -7
- package/build-module/image-editor/core/transform-style.mjs +34 -23
- package/build-module/image-editor/core/transform-style.mjs.map +1 -7
- package/build-module/image-editor/core/transforms/pipeline.mjs +29 -11
- package/build-module/image-editor/core/transforms/pipeline.mjs.map +1 -7
- package/build-module/image-editor/core/types.mjs +0 -1
- package/build-module/image-editor/core/viewport-state.mjs +26 -20
- package/build-module/image-editor/core/viewport-state.mjs.map +1 -7
- package/build-module/image-editor/index.mjs +3 -38
- package/build-module/image-editor/react/components/cropper-provider.mjs +38 -21
- package/build-module/image-editor/react/components/cropper-provider.mjs.map +1 -7
- package/build-module/image-editor/react/components/cropper.mjs +550 -686
- package/build-module/image-editor/react/components/cropper.mjs.map +1 -7
- package/build-module/image-editor/react/components/index.mjs +1 -7
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +130 -135
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -7
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +24 -35
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +1 -7
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +54 -64
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +1 -7
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +355 -407
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +1 -7
- package/build-module/image-editor/react/components/viewport-provider.mjs +15 -22
- package/build-module/image-editor/react/components/viewport-provider.mjs.map +1 -7
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +72 -43
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -7
- package/build-module/image-editor/react/hooks/index.mjs +2 -7
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +83 -119
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +1 -7
- package/build-module/image-editor/react/hooks/use-cropper-controller.mjs +75 -0
- package/build-module/image-editor/react/hooks/use-cropper-controller.mjs.map +1 -0
- package/build-module/image-editor/react/hooks/use-interaction.mjs +175 -183
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +1 -7
- package/build-module/image-editor/react/hooks/use-transform-style.mjs +15 -9
- package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +1 -7
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs +32 -26
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -7
- package/build-module/image-editor/react/visually-hidden-style.mjs +17 -14
- package/build-module/image-editor/react/visually-hidden-style.mjs.map +1 -7
- package/build-module/index.mjs +3 -10
- package/build-module/lock-unlock.mjs +5 -10
- package/build-module/lock-unlock.mjs.map +1 -7
- package/build-module/private-apis.mjs +8 -9
- package/build-module/private-apis.mjs.map +1 -7
- package/build-module/state/aspect-ratio-presets.mjs +48 -0
- package/build-module/state/aspect-ratio-presets.mjs.map +1 -0
- package/build-module/state/composite-reducer.mjs +114 -90
- package/build-module/state/composite-reducer.mjs.map +1 -7
- package/build-module/state/index.mjs +5 -25
- package/build-module/state/media-editor-state-provider.mjs +29 -24
- package/build-module/state/media-editor-state-provider.mjs.map +1 -7
- package/build-module/state/types.mjs +8 -7
- package/build-module/state/types.mjs.map +1 -7
- package/build-module/state/use-media-editor-state.mjs +256 -286
- package/build-module/state/use-media-editor-state.mjs.map +1 -7
- package/build-module/store/actions.mjs +12 -19
- package/build-module/store/actions.mjs.map +1 -7
- package/build-module/store/constants.mjs +4 -5
- package/build-module/store/constants.mjs.map +1 -7
- package/build-module/store/index.mjs +8 -9
- package/build-module/store/index.mjs.map +1 -7
- package/build-module/store/reducer.mjs +23 -26
- package/build-module/store/reducer.mjs.map +1 -7
- package/build-module/store/selectors.mjs +8 -13
- package/build-module/store/selectors.mjs.map +1 -7
- package/build-module/utils/get-media-type.mjs +14 -17
- package/build-module/utils/get-media-type.mjs.map +1 -7
- package/build-module/utils/index.mjs +1 -6
- package/build-style/style-rtl.css +41 -4
- package/build-style/style.css +41 -4
- package/build-types/components/media-editor/index.d.ts +4 -0
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
- package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/build-modifiers.d.ts +6 -5
- package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -1
- package/build-types/components/media-preview/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
- package/build-types/image-editor/core/constants.d.ts +0 -1
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/index.d.ts +1 -1
- package/build-types/image-editor/core/index.d.ts.map +1 -1
- package/build-types/image-editor/core/setter-helpers.d.ts +1 -1
- package/build-types/image-editor/core/types.d.ts +1 -1
- package/build-types/image-editor/index.d.ts +7 -6
- package/build-types/image-editor/index.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts +2 -2
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +5 -14
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +2 -2
- package/build-types/image-editor/react/hooks/index.d.ts +2 -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 → use-cropper-controller.d.ts} +12 -12
- package/build-types/image-editor/react/hooks/use-cropper-controller.d.ts.map +1 -0
- package/build-types/state/aspect-ratio-presets.d.ts +7 -0
- package/build-types/state/aspect-ratio-presets.d.ts.map +1 -0
- package/build-types/state/composite-reducer.d.ts +1 -1
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/index.d.ts +1 -0
- package/build-types/state/index.d.ts.map +1 -1
- package/build-types/state/types.d.ts +2 -1
- package/build-types/state/types.d.ts.map +1 -1
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/package.json +21 -21
- package/src/components/media-editor/index.tsx +215 -162
- package/src/components/media-editor/style.scss +19 -0
- package/src/components/media-editor/use-crop-options.ts +5 -2
- package/src/components/media-editor/use-save-media-editor.ts +2 -6
- package/src/components/media-editor-canvas/index.tsx +16 -26
- package/src/components/media-editor-image-controls/index.tsx +4 -2
- package/src/components/media-editor-modal/build-modifiers.ts +13 -7
- package/src/components/media-editor-modal/style.scss +1 -1
- package/src/components/media-preview/index.tsx +66 -23
- package/src/components/media-preview/style.scss +16 -2
- package/src/components/rotation-ruler/style.scss +6 -2
- package/src/components/rotation-ruler/use-ruler-drag.ts +6 -11
- package/src/image-editor/README.md +8 -8
- package/src/image-editor/core/constants.ts +0 -13
- package/src/image-editor/core/index.ts +1 -5
- package/src/image-editor/core/setter-helpers.ts +1 -1
- package/src/image-editor/core/types.ts +1 -1
- package/src/image-editor/docs/architecture.md +1 -1
- package/src/image-editor/docs/recipes.md +3 -3
- package/src/image-editor/index.ts +7 -16
- package/src/image-editor/react/components/cropper-provider.tsx +5 -5
- package/src/image-editor/react/components/cropper.tsx +19 -27
- package/src/image-editor/react/hooks/build-cropper-setters.ts +2 -2
- package/src/image-editor/react/hooks/index.ts +2 -2
- package/src/image-editor/react/hooks/{use-cropper-reducer.ts → use-cropper-controller.ts} +11 -11
- package/src/state/aspect-ratio-presets.ts +18 -0
- package/src/state/composite-reducer.ts +2 -2
- package/src/state/index.ts +1 -0
- package/src/state/types.ts +2 -6
- package/src/state/use-media-editor-state.ts +7 -5
- package/build/image-editor/core/index.cjs.map +0 -7
- package/build/image-editor/core/types.cjs.map +0 -7
- package/build/image-editor/index.cjs.map +0 -7
- package/build/image-editor/react/components/index.cjs.map +0 -7
- package/build/image-editor/react/hooks/index.cjs.map +0 -7
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs +0 -113
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +0 -7
- package/build/index.cjs.map +0 -7
- package/build/state/index.cjs.map +0 -7
- package/build/utils/index.cjs.map +0 -7
- package/build-module/image-editor/core/index.mjs.map +0 -7
- package/build-module/image-editor/core/types.mjs.map +0 -7
- package/build-module/image-editor/index.mjs.map +0 -7
- package/build-module/image-editor/react/components/index.mjs.map +0 -7
- package/build-module/image-editor/react/hooks/index.mjs.map +0 -7
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +0 -100
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +0 -7
- package/build-module/index.mjs.map +0 -7
- package/build-module/state/index.mjs.map +0 -7
- package/build-module/utils/index.mjs.map +0 -7
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +0 -1
|
@@ -1,293 +1,263 @@
|
|
|
1
|
+
import { useCallback, useMemo, useReducer, useRef, useState } from "@wordpress/element";
|
|
2
|
+
import { ORIGINAL_ASPECT_RATIO, exportCroppedImage } from "../image-editor/index.mjs";
|
|
3
|
+
import { DEFAULT_STATE, areCropperStatesEqual, enforceContainment } from "../image-editor/core/index.mjs";
|
|
4
|
+
import { buildCropperSetters } from "../image-editor/react/hooks/index.mjs";
|
|
5
|
+
import { areMediaEditorStatesEqual, buildInitialMediaEditorState, mediaEditorReducer } from "./composite-reducer.mjs";
|
|
1
6
|
// packages/media-editor/src/state/use-media-editor-state.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
mediaEditorReducer,
|
|
19
|
-
areMediaEditorStatesEqual,
|
|
20
|
-
buildInitialMediaEditorState
|
|
21
|
-
} from "./composite-reducer.mjs";
|
|
7
|
+
/**
|
|
8
|
+
* Resolve an aspect-ratio preset value (the string the dropdown
|
|
9
|
+
* round-trips) into a numeric width/height ratio for the cropper.
|
|
10
|
+
*
|
|
11
|
+
* - `'0'` → `undefined` (Free; no lock)
|
|
12
|
+
* - `'-1'` (Original) → image's natural width / height, or `undefined`
|
|
13
|
+
* if no image is loaded.
|
|
14
|
+
* - Any positive decimal → the parsed number.
|
|
15
|
+
*
|
|
16
|
+
* Centralizing here means the dropdown, the action dispatcher, and
|
|
17
|
+
* the test harness all resolve the same way.
|
|
18
|
+
*
|
|
19
|
+
* @param presetKey Preset value from the dropdown.
|
|
20
|
+
* @param cropperImage The cropper's loaded image, or `null`.
|
|
21
|
+
* @return The resolved ratio, or `undefined` for Free / unresolvable.
|
|
22
|
+
*/
|
|
22
23
|
function resolveAspectRatio(presetKey, cropperImage) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
return void 0;
|
|
32
|
-
}
|
|
33
|
-
if (num > 0) {
|
|
34
|
-
return num;
|
|
35
|
-
}
|
|
36
|
-
return void 0;
|
|
24
|
+
const num = parseFloat(presetKey);
|
|
25
|
+
if (num === 0 || Number.isNaN(num)) return;
|
|
26
|
+
if (num === ORIGINAL_ASPECT_RATIO) {
|
|
27
|
+
if (cropperImage && cropperImage.naturalWidth > 0 && cropperImage.naturalHeight > 0) return cropperImage.naturalWidth / cropperImage.naturalHeight;
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (num > 0) return num;
|
|
37
31
|
}
|
|
38
32
|
function areCropperImagesEqual(a, b) {
|
|
39
|
-
|
|
33
|
+
return a?.src === b?.src && a?.naturalWidth === b?.naturalWidth && a?.naturalHeight === b?.naturalHeight;
|
|
40
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* The media editor session.
|
|
37
|
+
*
|
|
38
|
+
* Owns the only undo/redo history for the editor. Cropper geometry
|
|
39
|
+
* actions, sidebar cropOptions actions and named edits flow through
|
|
40
|
+
* the same reducer and the same history stack.
|
|
41
|
+
*
|
|
42
|
+
* History semantics:
|
|
43
|
+
* - Each action's pre-state is pushed to the undo stack before the
|
|
44
|
+
* action commits, unless a gesture is open or the action is a
|
|
45
|
+
* no-op (post-state value-equal to pre-state).
|
|
46
|
+
* - During a gesture (`beginGesture` → `endGesture`) all mutations
|
|
47
|
+
* coalesce: only the pre-gesture snapshot is pushed, at gesture
|
|
48
|
+
* end. Any pending gesture is flushed on undo/redo.
|
|
49
|
+
* - Viewport-driven cropRect reshapes (window resize) use a
|
|
50
|
+
* dedicated non-recording dispatch path so they never produce
|
|
51
|
+
* undo entries.
|
|
52
|
+
*
|
|
53
|
+
* `isDirty` tracks the full editor session, including undoable UI
|
|
54
|
+
* state. Use `hasOutputEdits` when deciding whether the image output
|
|
55
|
+
* needs saving.
|
|
56
|
+
*
|
|
57
|
+
* The session starts empty. Call `setSourceImage` to load an image.
|
|
58
|
+
*
|
|
59
|
+
* @return The media editor session.
|
|
60
|
+
*/
|
|
41
61
|
function useMediaEditorState() {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
cropperSetters,
|
|
241
|
-
state.cropper,
|
|
242
|
-
setSourceImage,
|
|
243
|
-
resetCropper,
|
|
244
|
-
isCropperDirty,
|
|
245
|
-
getCroppedImage,
|
|
246
|
-
setVisualSize,
|
|
247
|
-
adjustCropRectForViewport
|
|
248
|
-
]
|
|
249
|
-
);
|
|
250
|
-
const session = useMemo(
|
|
251
|
-
() => ({
|
|
252
|
-
cropper,
|
|
253
|
-
cropOptions: state.cropOptions,
|
|
254
|
-
setAspectRatioValue,
|
|
255
|
-
edits: state.edits,
|
|
256
|
-
setEdit,
|
|
257
|
-
setSourceImage,
|
|
258
|
-
reset,
|
|
259
|
-
isDirty,
|
|
260
|
-
// Only geometry changes the saved image so far.
|
|
261
|
-
hasOutputEdits: isCropperDirty,
|
|
262
|
-
hasUndo,
|
|
263
|
-
hasRedo,
|
|
264
|
-
undo,
|
|
265
|
-
redo,
|
|
266
|
-
beginGesture,
|
|
267
|
-
endGesture
|
|
268
|
-
}),
|
|
269
|
-
[
|
|
270
|
-
cropper,
|
|
271
|
-
state.cropOptions,
|
|
272
|
-
setAspectRatioValue,
|
|
273
|
-
state.edits,
|
|
274
|
-
setEdit,
|
|
275
|
-
setSourceImage,
|
|
276
|
-
reset,
|
|
277
|
-
isDirty,
|
|
278
|
-
isCropperDirty,
|
|
279
|
-
hasUndo,
|
|
280
|
-
hasRedo,
|
|
281
|
-
undo,
|
|
282
|
-
redo,
|
|
283
|
-
beginGesture,
|
|
284
|
-
endGesture
|
|
285
|
-
]
|
|
286
|
-
);
|
|
287
|
-
return session;
|
|
62
|
+
const [state, dispatch] = useReducer(mediaEditorReducer, null, () => buildInitialMediaEditorState({ ...DEFAULT_STATE }));
|
|
63
|
+
const [initialBaseline, setInitialBaseline] = useState(() => state);
|
|
64
|
+
const stateRef = useRef(state);
|
|
65
|
+
const visualSizeRef = useRef({
|
|
66
|
+
width: 0,
|
|
67
|
+
height: 0
|
|
68
|
+
});
|
|
69
|
+
const historyRef = useRef([]);
|
|
70
|
+
const redoStackRef = useRef([]);
|
|
71
|
+
const [hasUndo, setHasUndo] = useState(false);
|
|
72
|
+
const [hasRedo, setHasRedo] = useState(false);
|
|
73
|
+
const isGestureOpenRef = useRef(false);
|
|
74
|
+
const gestureSnapshotRef = useRef(null);
|
|
75
|
+
const pushSnapshot = useCallback((snapshot) => {
|
|
76
|
+
const last = historyRef.current[historyRef.current.length - 1];
|
|
77
|
+
if (last && areMediaEditorStatesEqual(last, snapshot)) return;
|
|
78
|
+
historyRef.current = [...historyRef.current, snapshot];
|
|
79
|
+
redoStackRef.current = [];
|
|
80
|
+
setHasUndo(true);
|
|
81
|
+
setHasRedo(false);
|
|
82
|
+
}, []);
|
|
83
|
+
const dispatchWithHistory = useCallback((action, recordHistory = true) => {
|
|
84
|
+
const preState = stateRef.current;
|
|
85
|
+
const postState = mediaEditorReducer(preState, action);
|
|
86
|
+
if (recordHistory) {
|
|
87
|
+
if (!areMediaEditorStatesEqual(preState, postState)) {
|
|
88
|
+
if (isGestureOpenRef.current) {
|
|
89
|
+
if (gestureSnapshotRef.current === null) gestureSnapshotRef.current = preState;
|
|
90
|
+
} else pushSnapshot(preState);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
stateRef.current = postState;
|
|
94
|
+
dispatch(action);
|
|
95
|
+
}, [pushSnapshot]);
|
|
96
|
+
const beginGesture = useCallback(() => {
|
|
97
|
+
if (isGestureOpenRef.current) return;
|
|
98
|
+
isGestureOpenRef.current = true;
|
|
99
|
+
gestureSnapshotRef.current = null;
|
|
100
|
+
}, []);
|
|
101
|
+
const endGesture = useCallback(() => {
|
|
102
|
+
const snapshot = gestureSnapshotRef.current;
|
|
103
|
+
if (snapshot && !areMediaEditorStatesEqual(snapshot, stateRef.current)) pushSnapshot(snapshot);
|
|
104
|
+
gestureSnapshotRef.current = null;
|
|
105
|
+
isGestureOpenRef.current = false;
|
|
106
|
+
}, [pushSnapshot]);
|
|
107
|
+
const dispatchCropperAction = useCallback((action) => {
|
|
108
|
+
dispatchWithHistory({
|
|
109
|
+
type: "CROPPER",
|
|
110
|
+
action
|
|
111
|
+
});
|
|
112
|
+
}, [dispatchWithHistory]);
|
|
113
|
+
const cropperSetters = useMemo(() => buildCropperSetters(dispatchCropperAction, () => stateRef.current.cropper), [dispatchCropperAction]);
|
|
114
|
+
const setSourceImage = useCallback((image) => {
|
|
115
|
+
if (areCropperImagesEqual(stateRef.current.cropper.image, image)) return;
|
|
116
|
+
const next = buildInitialMediaEditorState(enforceContainment({
|
|
117
|
+
...DEFAULT_STATE,
|
|
118
|
+
image
|
|
119
|
+
}));
|
|
120
|
+
stateRef.current = next;
|
|
121
|
+
dispatch({
|
|
122
|
+
type: "RESTORE_SNAPSHOT",
|
|
123
|
+
payload: next
|
|
124
|
+
});
|
|
125
|
+
setInitialBaseline(next);
|
|
126
|
+
isGestureOpenRef.current = false;
|
|
127
|
+
gestureSnapshotRef.current = null;
|
|
128
|
+
historyRef.current = [];
|
|
129
|
+
redoStackRef.current = [];
|
|
130
|
+
setHasUndo(false);
|
|
131
|
+
setHasRedo(false);
|
|
132
|
+
}, []);
|
|
133
|
+
const resetCropper = useCallback((resetState) => {
|
|
134
|
+
dispatchWithHistory({
|
|
135
|
+
type: "CROPPER",
|
|
136
|
+
action: {
|
|
137
|
+
type: "RESET",
|
|
138
|
+
payload: resetState
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}, [dispatchWithHistory]);
|
|
142
|
+
const setAspectRatioValue = useCallback((presetKey) => {
|
|
143
|
+
const resolved = resolveAspectRatio(presetKey, stateRef.current.cropper.image);
|
|
144
|
+
dispatchWithHistory({
|
|
145
|
+
type: "SET_ASPECT_RATIO_VALUE",
|
|
146
|
+
payload: {
|
|
147
|
+
presetKey,
|
|
148
|
+
resolved,
|
|
149
|
+
visualSize: visualSizeRef.current
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}, [dispatchWithHistory]);
|
|
153
|
+
const reset = useCallback(() => {
|
|
154
|
+
dispatchWithHistory({ type: "RESET" });
|
|
155
|
+
}, [dispatchWithHistory]);
|
|
156
|
+
const setEdit = useCallback((name, value) => {
|
|
157
|
+
dispatchWithHistory({
|
|
158
|
+
type: "SET_EDIT",
|
|
159
|
+
payload: {
|
|
160
|
+
name,
|
|
161
|
+
value
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}, [dispatchWithHistory]);
|
|
165
|
+
const undo = useCallback(() => {
|
|
166
|
+
endGesture();
|
|
167
|
+
const prev = historyRef.current[historyRef.current.length - 1];
|
|
168
|
+
if (!prev) return;
|
|
169
|
+
redoStackRef.current = [stateRef.current, ...redoStackRef.current];
|
|
170
|
+
historyRef.current = historyRef.current.slice(0, -1);
|
|
171
|
+
setHasUndo(historyRef.current.length > 0);
|
|
172
|
+
setHasRedo(true);
|
|
173
|
+
stateRef.current = prev;
|
|
174
|
+
dispatch({
|
|
175
|
+
type: "RESTORE_SNAPSHOT",
|
|
176
|
+
payload: prev
|
|
177
|
+
});
|
|
178
|
+
}, [endGesture]);
|
|
179
|
+
const redo = useCallback(() => {
|
|
180
|
+
endGesture();
|
|
181
|
+
const next = redoStackRef.current[0];
|
|
182
|
+
if (!next) return;
|
|
183
|
+
historyRef.current = [...historyRef.current, stateRef.current];
|
|
184
|
+
redoStackRef.current = redoStackRef.current.slice(1);
|
|
185
|
+
setHasUndo(true);
|
|
186
|
+
setHasRedo(redoStackRef.current.length > 0);
|
|
187
|
+
stateRef.current = next;
|
|
188
|
+
dispatch({
|
|
189
|
+
type: "RESTORE_SNAPSHOT",
|
|
190
|
+
payload: next
|
|
191
|
+
});
|
|
192
|
+
}, [endGesture]);
|
|
193
|
+
const setVisualSize = useCallback((size) => {
|
|
194
|
+
visualSizeRef.current = size;
|
|
195
|
+
}, []);
|
|
196
|
+
const adjustCropRectForViewport = useCallback((rect) => {
|
|
197
|
+
dispatchWithHistory({
|
|
198
|
+
type: "VIEWPORT_ADJUST_CROP_RECT",
|
|
199
|
+
payload: rect
|
|
200
|
+
}, false);
|
|
201
|
+
}, [dispatchWithHistory]);
|
|
202
|
+
const isDirty = !areMediaEditorStatesEqual(state, initialBaseline);
|
|
203
|
+
const isCropperDirty = !areCropperStatesEqual(state.cropper, initialBaseline.cropper);
|
|
204
|
+
const getCroppedImage = useCallback((mimeType, quality) => {
|
|
205
|
+
if (!state.cropper.image) return Promise.reject(/* @__PURE__ */ new Error("No image loaded — call setSourceImage first."));
|
|
206
|
+
return exportCroppedImage(state.cropper.image.src, state.cropper, mimeType, quality);
|
|
207
|
+
}, [state.cropper]);
|
|
208
|
+
const cropper = useMemo(() => ({
|
|
209
|
+
...cropperSetters,
|
|
210
|
+
state: state.cropper,
|
|
211
|
+
setImage: setSourceImage,
|
|
212
|
+
reset: resetCropper,
|
|
213
|
+
isDirty: isCropperDirty,
|
|
214
|
+
getCroppedImage,
|
|
215
|
+
setVisualSize,
|
|
216
|
+
adjustCropRectForViewport
|
|
217
|
+
}), [
|
|
218
|
+
cropperSetters,
|
|
219
|
+
state.cropper,
|
|
220
|
+
setSourceImage,
|
|
221
|
+
resetCropper,
|
|
222
|
+
isCropperDirty,
|
|
223
|
+
getCroppedImage,
|
|
224
|
+
setVisualSize,
|
|
225
|
+
adjustCropRectForViewport
|
|
226
|
+
]);
|
|
227
|
+
return useMemo(() => ({
|
|
228
|
+
cropper,
|
|
229
|
+
cropOptions: state.cropOptions,
|
|
230
|
+
setAspectRatioValue,
|
|
231
|
+
edits: state.edits,
|
|
232
|
+
setEdit,
|
|
233
|
+
setSourceImage,
|
|
234
|
+
reset,
|
|
235
|
+
isDirty,
|
|
236
|
+
hasOutputEdits: isCropperDirty,
|
|
237
|
+
hasUndo,
|
|
238
|
+
hasRedo,
|
|
239
|
+
undo,
|
|
240
|
+
redo,
|
|
241
|
+
beginGesture,
|
|
242
|
+
endGesture
|
|
243
|
+
}), [
|
|
244
|
+
cropper,
|
|
245
|
+
state.cropOptions,
|
|
246
|
+
setAspectRatioValue,
|
|
247
|
+
state.edits,
|
|
248
|
+
setEdit,
|
|
249
|
+
setSourceImage,
|
|
250
|
+
reset,
|
|
251
|
+
isDirty,
|
|
252
|
+
isCropperDirty,
|
|
253
|
+
hasUndo,
|
|
254
|
+
hasRedo,
|
|
255
|
+
undo,
|
|
256
|
+
redo,
|
|
257
|
+
beginGesture,
|
|
258
|
+
endGesture
|
|
259
|
+
]);
|
|
288
260
|
}
|
|
289
|
-
export {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
};
|
|
293
|
-
//# sourceMappingURL=use-media-editor-state.mjs.map
|
|
261
|
+
export { resolveAspectRatio, useMediaEditorState };
|
|
262
|
+
|
|
263
|
+
//# sourceMappingURL=use-media-editor-state.mjs.map
|