@wordpress/media-editor 0.20.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 +28 -0
- package/README.md +2 -1
- package/build/components/media-editor/index.cjs +533 -695
- package/build/components/media-editor/index.cjs.map +1 -7
- package/build/components/media-editor/use-crop-options.cjs +43 -58
- 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 -68
- 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 -153
- package/build/components/media-editor-modal/index.cjs.map +1 -7
- package/build/components/media-editor-provider/index.cjs +32 -54
- package/build/components/media-editor-provider/index.cjs.map +1 -7
- package/build/components/media-form/index.cjs +77 -78
- 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 -720
- 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 +136 -89
- 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 -281
- 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 -682
- package/build-module/components/media-editor/index.mjs.map +1 -7
- package/build-module/components/media-editor/use-crop-options.mjs +38 -33
- 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 +97 -95
- package/build-module/components/media-editor-canvas/index.mjs.map +1 -7
- package/build-module/components/media-editor-crop-panel/index.mjs +44 -43
- 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 -122
- package/build-module/components/media-editor-modal/index.mjs.map +1 -7
- package/build-module/components/media-editor-provider/index.mjs +26 -28
- package/build-module/components/media-editor-provider/index.mjs.map +1 -7
- package/build-module/components/media-form/index.mjs +69 -54
- package/build-module/components/media-form/index.mjs.map +1 -7
- package/build-module/components/media-preview/index.mjs +105 -89
- 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 -696
- 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 -75
- 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 -273
- 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 +6 -1
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +0 -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-crop-panel/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-editor-modal/index.d.ts +7 -2
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-provider/index.d.ts +7 -1
- package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts +5 -2
- package/build-types/components/media-form/index.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 +2 -2
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/index.d.ts +2 -1
- package/build-types/state/index.d.ts.map +1 -1
- package/build-types/state/types.d.ts +36 -6
- package/build-types/state/types.d.ts.map +1 -1
- package/build-types/state/use-media-editor-state.d.ts +16 -5
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/package.json +35 -24
- package/src/components/media-editor/index.tsx +226 -183
- package/src/components/media-editor/style.scss +19 -0
- package/src/components/media-editor/use-crop-options.ts +5 -4
- package/src/components/media-editor/use-save-media-editor.ts +2 -6
- package/src/components/media-editor-canvas/index.tsx +18 -28
- package/src/components/media-editor-crop-panel/index.tsx +17 -9
- package/src/components/media-editor-image-controls/index.tsx +4 -2
- package/src/components/media-editor-image-controls/style.scss +1 -1
- package/src/components/media-editor-modal/build-modifiers.ts +13 -7
- package/src/components/media-editor-modal/index.tsx +8 -1
- package/src/components/media-editor-modal/style.scss +1 -1
- package/src/components/media-editor-provider/index.tsx +8 -1
- package/src/components/media-form/index.tsx +48 -42
- package/src/components/media-preview/index.tsx +68 -25
- 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 +5 -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 +20 -42
- 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 +26 -13
- package/src/state/index.ts +3 -0
- package/src/state/types.ts +39 -11
- package/src/state/use-media-editor-state.ts +53 -16
- 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
- package/global.d.ts +0 -5
- package/src/components/media-editor/test/restore-original.browser.test.tsx +0 -227
- package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -106
- package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -72
- package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -186
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -359
- package/src/components/media-editor-modal/test/index.jsdom.test.tsx +0 -182
- package/src/components/rotation-ruler/stories/index.story.tsx +0 -43
- package/src/components/rotation-ruler/test/index.jsdom.test.tsx +0 -140
- package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +0 -823
- package/src/image-editor/core/math/test/sanitize.ts +0 -242
- package/src/image-editor/core/test/camera.ts +0 -1218
- package/src/image-editor/core/test/interaction-controller.ts +0 -1299
- package/src/image-editor/core/test/preview-export-parity.ts +0 -249
- package/src/image-editor/core/test/render-camera-bridge.ts +0 -349
- package/src/image-editor/core/test/state.ts +0 -876
- package/src/image-editor/core/test/stencil-math.ts +0 -665
- package/src/image-editor/core/transforms/test/pipeline.ts +0 -267
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.browser.test.tsx +0 -438
- package/src/image-editor/react/components/test/cropper.browser.test.tsx +0 -1462
- package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +0 -491
- package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +0 -1049
- package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +0 -213
- package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +0 -140
- package/src/image-editor/stories/rectangle-crop.story.tsx +0 -1073
- package/src/image-editor/stories/style.module.css +0 -65
- package/src/state/test/use-media-editor-state.browser.test.tsx +0 -79
- package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -386
- package/src/store/test/reducer.ts +0 -64
- package/tsconfig.build.json +0 -26
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -37
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { useCallback, useMemo, useReducer, useRef, useState } from "@wordpress/element";
|
|
2
|
+
import { DEFAULT_STATE } from "../../core/constants.mjs";
|
|
3
|
+
import { exportCroppedImage } from "../../core/export/canvas-renderer.mjs";
|
|
4
|
+
import { cropperReducer, enforceContainment, isStateDirty } from "../../core/state.mjs";
|
|
5
|
+
import { buildCropperSetters } from "./build-cropper-setters.mjs";
|
|
6
|
+
// packages/media-editor/src/image-editor/react/hooks/use-cropper-controller.ts
|
|
7
|
+
function areCropperImagesEqual(a, b) {
|
|
8
|
+
return a?.src === b?.src && a?.naturalWidth === b?.naturalWidth && a?.naturalHeight === b?.naturalHeight;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Controller hook for the image cropper: the state, the named setters and
|
|
12
|
+
* the export helpers, without history.
|
|
13
|
+
*
|
|
14
|
+
* Wraps the framework-agnostic `cropperReducer` and exposes named
|
|
15
|
+
* setters (built from the shared `buildCropperSetters` factory).
|
|
16
|
+
* Does NOT manage undo/redo history — that responsibility belongs to
|
|
17
|
+
* the surrounding application (see `useMediaEditorState` for the
|
|
18
|
+
* composite store used by the media editor).
|
|
19
|
+
*
|
|
20
|
+
* @param initialState Optional partial state to merge with DEFAULT_STATE.
|
|
21
|
+
* @return The cropper controller — state, setters, and helpers.
|
|
22
|
+
*/
|
|
23
|
+
function useCropperController(initialState) {
|
|
24
|
+
const [state, dispatch] = useReducer(cropperReducer, initialState, (init) => enforceContainment({
|
|
25
|
+
...DEFAULT_STATE,
|
|
26
|
+
...init
|
|
27
|
+
}));
|
|
28
|
+
const [initialBaseline, setInitialBaseline] = useState(() => enforceContainment({
|
|
29
|
+
...DEFAULT_STATE,
|
|
30
|
+
...initialState
|
|
31
|
+
}));
|
|
32
|
+
const stateRef = useRef(state);
|
|
33
|
+
const dispatchAndSync = useCallback((action) => {
|
|
34
|
+
const next = cropperReducer(stateRef.current, action);
|
|
35
|
+
stateRef.current = next;
|
|
36
|
+
dispatch(action);
|
|
37
|
+
return next;
|
|
38
|
+
}, []);
|
|
39
|
+
const setImage = useCallback((image) => {
|
|
40
|
+
if (areCropperImagesEqual(stateRef.current.image, image)) return;
|
|
41
|
+
const next = dispatchAndSync({
|
|
42
|
+
type: "SET_IMAGE",
|
|
43
|
+
payload: image
|
|
44
|
+
});
|
|
45
|
+
setInitialBaseline(next);
|
|
46
|
+
}, [dispatchAndSync]);
|
|
47
|
+
const reset = useCallback((resetState) => {
|
|
48
|
+
const nextInitialState = dispatchAndSync({
|
|
49
|
+
type: "RESET",
|
|
50
|
+
payload: resetState
|
|
51
|
+
});
|
|
52
|
+
setInitialBaseline(nextInitialState);
|
|
53
|
+
}, [dispatchAndSync]);
|
|
54
|
+
const setters = useMemo(() => buildCropperSetters(dispatchAndSync, () => stateRef.current), [dispatchAndSync]);
|
|
55
|
+
const isDirty = isStateDirty(state, initialBaseline);
|
|
56
|
+
const getCroppedImage = useCallback((mimeType, quality) => {
|
|
57
|
+
if (!state.image) return Promise.reject(/* @__PURE__ */ new Error("No image loaded — call setImage first."));
|
|
58
|
+
return exportCroppedImage(state.image.src, state, mimeType, quality);
|
|
59
|
+
}, [state]);
|
|
60
|
+
const setVisualSize = useCallback(() => {}, []);
|
|
61
|
+
const adjustCropRectForViewport = setters.setCropRect;
|
|
62
|
+
return {
|
|
63
|
+
...setters,
|
|
64
|
+
state,
|
|
65
|
+
setImage,
|
|
66
|
+
reset,
|
|
67
|
+
isDirty,
|
|
68
|
+
getCroppedImage,
|
|
69
|
+
setVisualSize,
|
|
70
|
+
adjustCropRectForViewport
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export { useCropperController };
|
|
74
|
+
|
|
75
|
+
//# sourceMappingURL=use-cropper-controller.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-cropper-controller.mjs","names":[],"sources":["../../../../src/image-editor/react/hooks/use-cropper-controller.ts"],"sourcesContent":["import {\n\tuseReducer,\n\tuseCallback,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n} from '@wordpress/element';\nimport type {\n\tCropperState,\n\tCropperAction,\n\tNormalizedRect,\n\tSize,\n} from '../../core/types';\nimport { DEFAULT_STATE } from '../../core/constants';\nimport { exportCroppedImage } from '../../core/export/canvas-renderer';\nimport {\n\tcropperReducer,\n\tenforceContainment,\n\tisStateDirty,\n} from '../../core/state';\nimport {\n\tbuildCropperSetters,\n\ttype CropperSetters,\n} from './build-cropper-setters';\n\n/**\n * The cropper controller surface — the contract the Cropper component\n * and its interaction hooks consume.\n *\n * History (undo / redo / commitHistory) is intentionally NOT part of\n * this interface. The cropper is a pure state machine; history is a\n * higher-layer concern owned by a composite store\n * (see media-editor/src/state).\n */\nexport interface CropperController extends CropperSetters {\n\t/** The current cropper state (read-only). */\n\tstate: CropperState;\n\t/** Set the loaded image (natural size and src). */\n\tsetImage: ( image: CropperState[ 'image' ] ) => void;\n\t/** Reset the state. Optionally merge partial state overrides. */\n\treset: ( resetState?: Partial< CropperState > ) => void;\n\t/** Whether the current state differs from the initial state. */\n\tisDirty: boolean;\n\t/**\n\t * Export the cropped image as a Blob. Throws on failure — see\n\t * `exportCroppedImage` for error semantics.\n\t */\n\tgetCroppedImage: ( mimeType?: string, quality?: number ) => Promise< Blob >;\n\t/**\n\t * Called by `Cropper` whenever its rendered image size changes; not for\n\t * consumers to call. A composite controller uses the size to reshape the\n\t * crop for an aspect ratio. The pure cropper hook ignores it.\n\t */\n\tsetVisualSize: ( size: Size ) => void;\n\t/**\n\t * Called by `Cropper` when the viewport changes (window or canvas\n\t * resize); not for consumers to call. Like `setCropRect`, except that a\n\t * composite controller records no undo entry for it, since a viewport\n\t * reshape is not an editor action. The pure cropper hook aliases it to\n\t * `setCropRect`.\n\t */\n\tadjustCropRectForViewport: ( rect: NormalizedRect ) => void;\n}\n\nfunction areCropperImagesEqual(\n\ta: CropperState[ 'image' ],\n\tb: CropperState[ 'image' ]\n): boolean {\n\treturn (\n\t\ta?.src === b?.src &&\n\t\ta?.naturalWidth === b?.naturalWidth &&\n\t\ta?.naturalHeight === b?.naturalHeight\n\t);\n}\n\n/**\n * Controller hook for the image cropper: the state, the named setters and\n * the export helpers, without history.\n *\n * Wraps the framework-agnostic `cropperReducer` and exposes named\n * setters (built from the shared `buildCropperSetters` factory).\n * Does NOT manage undo/redo history — that responsibility belongs to\n * the surrounding application (see `useMediaEditorState` for the\n * composite store used by the media editor).\n *\n * @param initialState Optional partial state to merge with DEFAULT_STATE.\n * @return The cropper controller — state, setters, and helpers.\n */\nexport function useCropperController(\n\tinitialState?: Partial< CropperState >\n): CropperController {\n\tconst [ state, dispatch ] = useReducer(\n\t\tcropperReducer,\n\t\tinitialState,\n\t\t( init ) => enforceContainment( { ...DEFAULT_STATE, ...init } )\n\t);\n\n\t// The \"clean\" snapshot the current state is compared against for\n\t// `isDirty`. Stored in state (not a ref) so the comparison reads\n\t// a value, not a ref's `current`. `setImage` and `reset` refresh\n\t// this when the consumer establishes a new baseline.\n\tconst [ initialBaseline, setInitialBaseline ] = useState< CropperState >(\n\t\t() => enforceContainment( { ...DEFAULT_STATE, ...initialState } )\n\t);\n\t// Latest-state ref used by setters that need to read fresh state\n\t// (focal-point zoom, toggleFlip, reset, setImage). Updated\n\t// synchronously inside `dispatchAndSync` so multiple actions in\n\t// one event see each other's output before React commits.\n\tconst stateRef = useRef( state );\n\n\tconst dispatchAndSync = useCallback( ( action: CropperAction ) => {\n\t\tconst next = cropperReducer( stateRef.current, action );\n\t\tstateRef.current = next;\n\t\tdispatch( action );\n\t\treturn next;\n\t}, [] );\n\n\tconst setImage = useCallback(\n\t\t( image: CropperState[ 'image' ] ) => {\n\t\t\tif ( areCropperImagesEqual( stateRef.current.image, image ) ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst next = dispatchAndSync( {\n\t\t\t\ttype: 'SET_IMAGE',\n\t\t\t\tpayload: image,\n\t\t\t} );\n\t\t\t// Refresh the \"clean\" snapshot to match the post-load state\n\t\t\t// produced by the reducer. Otherwise containment can nudge\n\t\t\t// pan/zoom by tiny amounts on load and `isDirty` would\n\t\t\t// report true from the start.\n\t\t\tsetInitialBaseline( next );\n\t\t},\n\t\t[ dispatchAndSync ]\n\t);\n\n\tconst reset = useCallback(\n\t\t( resetState?: Partial< CropperState > ) => {\n\t\t\tconst nextInitialState = dispatchAndSync( {\n\t\t\t\ttype: 'RESET',\n\t\t\t\tpayload: resetState,\n\t\t\t} );\n\t\t\t// Mirror the reducer's RESET exactly so isDirty stays in\n\t\t\t// sync. The containment step can tweak pan/zoom/cropRect by\n\t\t\t// float ulp; fold those into the \"initial\" snapshot or\n\t\t\t// isDirty would report true after a reset.\n\t\t\tsetInitialBaseline( nextInitialState );\n\t\t},\n\t\t[ dispatchAndSync ]\n\t);\n\n\tconst setters = useMemo(\n\t\t() => buildCropperSetters( dispatchAndSync, () => stateRef.current ),\n\t\t[ dispatchAndSync ]\n\t);\n\n\tconst isDirty = isStateDirty( state, initialBaseline );\n\n\tconst getCroppedImage = useCallback(\n\t\t( mimeType?: string, quality?: number ): Promise< Blob > => {\n\t\t\tif ( ! state.image ) {\n\t\t\t\treturn Promise.reject(\n\t\t\t\t\tnew Error( 'No image loaded — call setImage first.' )\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn exportCroppedImage(\n\t\t\t\tstate.image.src,\n\t\t\t\tstate,\n\t\t\t\tmimeType,\n\t\t\t\tquality\n\t\t\t);\n\t\t},\n\t\t[ state ]\n\t);\n\n\t// No-op for the pure hook — visualSize matters only when a\n\t// composite store needs to compute aspect-ratio reshapes\n\t// (see useMediaEditorState). Typed to accept a Size so it\n\t// satisfies the controller surface; the value is intentionally\n\t// ignored.\n\tconst setVisualSize = useCallback( () => {}, [] );\n\n\t// In the pure hook, history isn't a concern — viewport-driven\n\t// reshape is identical to setCropRect.\n\tconst adjustCropRectForViewport = setters.setCropRect;\n\n\tconst controller: CropperController = {\n\t\t...setters,\n\t\tstate,\n\t\tsetImage,\n\t\treset,\n\t\tisDirty,\n\t\tgetCroppedImage,\n\t\tsetVisualSize,\n\t\tadjustCropRectForViewport,\n\t};\n\treturn controller;\n}\n"],"mappings":";;;;;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AC,CAAE;CAIhD,MAAM,4BAA4B,QAAQ;CAY1C,OAAO;EATN,GAAG;EACH;EACA;EACA;EACA;EACA;EACA;EACA;CAEe;AACjB"}
|
|
@@ -1,190 +1,182 @@
|
|
|
1
|
-
// packages/media-editor/src/image-editor/react/hooks/use-interaction.ts
|
|
2
1
|
import { useCallback, useEffect, useRef, useState } from "@wordpress/element";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
import { InteractionController } from "../../core/interaction-controller.mjs";
|
|
3
|
+
// packages/media-editor/src/image-editor/react/hooks/use-interaction.ts
|
|
4
|
+
/** How long keyboard placement stays active after the latest handled key. */
|
|
5
|
+
const KEYBOARD_INTERACTION_IDLE_MS = 300;
|
|
7
6
|
function isHandledKeyboardPan(event) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
7
|
+
switch (event.key) {
|
|
8
|
+
case "ArrowUp":
|
|
9
|
+
case "ArrowDown":
|
|
10
|
+
case "ArrowLeft":
|
|
11
|
+
case "ArrowRight": return true;
|
|
12
|
+
default: return false;
|
|
13
|
+
}
|
|
17
14
|
}
|
|
18
15
|
function isHandledKeyboardZoom(event) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
16
|
+
switch (event.key) {
|
|
17
|
+
case "+":
|
|
18
|
+
case "=":
|
|
19
|
+
case "-":
|
|
20
|
+
case "_": return true;
|
|
21
|
+
default: return false;
|
|
22
|
+
}
|
|
28
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Mouse, touch, and keyboard event handling for pan, zoom,
|
|
26
|
+
* and crop manipulation.
|
|
27
|
+
*
|
|
28
|
+
* Returns event handler props to spread on the container element.
|
|
29
|
+
* Uses requestAnimationFrame for drag/pinch updates to avoid
|
|
30
|
+
* layout thrashing.
|
|
31
|
+
*
|
|
32
|
+
* @param state The current cropper state.
|
|
33
|
+
* @param actions Named state updates for cropper interactions.
|
|
34
|
+
* @param containerSize The container dimensions in pixels.
|
|
35
|
+
* @param imageSize The rendered image dimensions in pixels.
|
|
36
|
+
* @param options Optional configuration for zoom and keyboard behavior.
|
|
37
|
+
* @return Event handler props for the container element.
|
|
38
|
+
*/
|
|
29
39
|
function useInteraction(state, actions, containerSize, imageSize, options) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
const onWheelNative = useCallback((e) => {
|
|
170
|
-
if (optionsRef.current?.disabled) {
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
controllerRef.current?.handleWheel(e);
|
|
174
|
-
}, []);
|
|
175
|
-
return {
|
|
176
|
-
handlers: {
|
|
177
|
-
onPointerDown,
|
|
178
|
-
onTouchStart,
|
|
179
|
-
onKeyDown
|
|
180
|
-
},
|
|
181
|
-
onWheelNative,
|
|
182
|
-
isDragging,
|
|
183
|
-
isZooming,
|
|
184
|
-
isPlacementActive: isGestureActive || isKeyboardPanning || isZooming
|
|
185
|
-
};
|
|
40
|
+
const [isDragging, setIsDragging] = useState(false);
|
|
41
|
+
const [isZooming, setIsZooming] = useState(false);
|
|
42
|
+
const [isGestureActive, setIsGestureActive] = useState(false);
|
|
43
|
+
const [isKeyboardPanning, setIsKeyboardPanning] = useState(false);
|
|
44
|
+
const keyboardInteractionTimerRef = useRef(void 0);
|
|
45
|
+
const isKeyboardGestureActiveRef = useRef(false);
|
|
46
|
+
const stateRef = useRef(state);
|
|
47
|
+
stateRef.current = state;
|
|
48
|
+
const containerSizeRef = useRef(containerSize);
|
|
49
|
+
containerSizeRef.current = containerSize;
|
|
50
|
+
const imageSizeRef = useRef(imageSize);
|
|
51
|
+
imageSizeRef.current = imageSize;
|
|
52
|
+
const optionsRef = useRef(options);
|
|
53
|
+
optionsRef.current = options;
|
|
54
|
+
const actionsRef = useRef(actions);
|
|
55
|
+
actionsRef.current = actions;
|
|
56
|
+
const controllerRef = useRef(null);
|
|
57
|
+
const startPlacementGesture = useCallback(() => {
|
|
58
|
+
setIsGestureActive(true);
|
|
59
|
+
}, []);
|
|
60
|
+
const stopPlacementGesture = useCallback(() => {
|
|
61
|
+
setIsGestureActive(false);
|
|
62
|
+
}, []);
|
|
63
|
+
const signalKeyboardGesture = useCallback(() => {
|
|
64
|
+
if (!isKeyboardGestureActiveRef.current) {
|
|
65
|
+
isKeyboardGestureActiveRef.current = true;
|
|
66
|
+
optionsRef.current?.onGestureStart?.();
|
|
67
|
+
}
|
|
68
|
+
clearTimeout(keyboardInteractionTimerRef.current);
|
|
69
|
+
keyboardInteractionTimerRef.current = setTimeout(() => {
|
|
70
|
+
isKeyboardGestureActiveRef.current = false;
|
|
71
|
+
setIsKeyboardPanning(false);
|
|
72
|
+
optionsRef.current?.onGestureEnd?.();
|
|
73
|
+
}, KEYBOARD_INTERACTION_IDLE_MS);
|
|
74
|
+
}, []);
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
return () => {
|
|
77
|
+
clearTimeout(keyboardInteractionTimerRef.current);
|
|
78
|
+
};
|
|
79
|
+
}, []);
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
const controller = new InteractionController({
|
|
82
|
+
getState: () => stateRef.current,
|
|
83
|
+
actions: {
|
|
84
|
+
setPan: (pan) => actionsRef.current.setPan(pan),
|
|
85
|
+
setZoom: (zoom) => actionsRef.current.setZoom(zoom),
|
|
86
|
+
setZoomAtPoint: (zoom, pan) => actionsRef.current.setZoomAtPoint(zoom, pan),
|
|
87
|
+
snapRotate90: (direction) => actionsRef.current.snapRotate90(direction),
|
|
88
|
+
toggleFlip: (direction) => actionsRef.current.toggleFlip?.(direction)
|
|
89
|
+
},
|
|
90
|
+
getContainerSize: () => containerSizeRef.current,
|
|
91
|
+
getImageSize: () => imageSizeRef.current,
|
|
92
|
+
get minZoom() {
|
|
93
|
+
return optionsRef.current?.minZoom;
|
|
94
|
+
},
|
|
95
|
+
get maxZoom() {
|
|
96
|
+
return optionsRef.current?.maxZoom;
|
|
97
|
+
},
|
|
98
|
+
get zoomSpeed() {
|
|
99
|
+
return optionsRef.current?.zoomSpeed;
|
|
100
|
+
},
|
|
101
|
+
get keyboardStep() {
|
|
102
|
+
return optionsRef.current?.keyboardStep;
|
|
103
|
+
},
|
|
104
|
+
get doubleTapZoom() {
|
|
105
|
+
return optionsRef.current?.doubleTapZoom;
|
|
106
|
+
},
|
|
107
|
+
onGestureStart: () => {
|
|
108
|
+
startPlacementGesture();
|
|
109
|
+
optionsRef.current?.onGestureStart?.();
|
|
110
|
+
},
|
|
111
|
+
onGestureEnd: () => {
|
|
112
|
+
stopPlacementGesture();
|
|
113
|
+
optionsRef.current?.onGestureEnd?.();
|
|
114
|
+
},
|
|
115
|
+
onStatusChange: (status) => {
|
|
116
|
+
setIsDragging(status.isDragging);
|
|
117
|
+
setIsZooming(status.isZooming);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
controllerRef.current = controller;
|
|
121
|
+
return () => {
|
|
122
|
+
controller.destroy();
|
|
123
|
+
controllerRef.current = null;
|
|
124
|
+
};
|
|
125
|
+
}, [startPlacementGesture, stopPlacementGesture]);
|
|
126
|
+
const isDisabled = options?.disabled ?? false;
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
if (!isDisabled) return;
|
|
129
|
+
controllerRef.current?.destroy();
|
|
130
|
+
setIsDragging(false);
|
|
131
|
+
setIsZooming(false);
|
|
132
|
+
setIsKeyboardPanning(false);
|
|
133
|
+
const wasGestureOpen = isGestureActive || isKeyboardGestureActiveRef.current;
|
|
134
|
+
if (isKeyboardGestureActiveRef.current) {
|
|
135
|
+
isKeyboardGestureActiveRef.current = false;
|
|
136
|
+
clearTimeout(keyboardInteractionTimerRef.current);
|
|
137
|
+
}
|
|
138
|
+
stopPlacementGesture();
|
|
139
|
+
if (wasGestureOpen) optionsRef.current?.onGestureEnd?.();
|
|
140
|
+
}, [
|
|
141
|
+
isDisabled,
|
|
142
|
+
isGestureActive,
|
|
143
|
+
stopPlacementGesture
|
|
144
|
+
]);
|
|
145
|
+
const onPointerDown = useCallback((e) => {
|
|
146
|
+
if (optionsRef.current?.disabled) return;
|
|
147
|
+
const el = e.currentTarget;
|
|
148
|
+
controllerRef.current?.handlePointerDown(e.nativeEvent, el);
|
|
149
|
+
}, []);
|
|
150
|
+
const onTouchStart = useCallback((e) => {
|
|
151
|
+
if (optionsRef.current?.disabled) return;
|
|
152
|
+
const el = e.currentTarget;
|
|
153
|
+
const rect = el.getBoundingClientRect();
|
|
154
|
+
controllerRef.current?.handleTouchStart(e.nativeEvent, rect, el.ownerDocument);
|
|
155
|
+
}, []);
|
|
156
|
+
const onKeyDown = useCallback((e) => {
|
|
157
|
+
if (optionsRef.current?.disabled) return;
|
|
158
|
+
if (isHandledKeyboardPan(e.nativeEvent)) {
|
|
159
|
+
setIsKeyboardPanning(true);
|
|
160
|
+
signalKeyboardGesture();
|
|
161
|
+
} else if (isHandledKeyboardZoom(e.nativeEvent)) signalKeyboardGesture();
|
|
162
|
+
controllerRef.current?.handleKeyDown(e.nativeEvent);
|
|
163
|
+
}, [signalKeyboardGesture]);
|
|
164
|
+
const onWheelNative = useCallback((e) => {
|
|
165
|
+
if (optionsRef.current?.disabled) return;
|
|
166
|
+
controllerRef.current?.handleWheel(e);
|
|
167
|
+
}, []);
|
|
168
|
+
return {
|
|
169
|
+
handlers: {
|
|
170
|
+
onPointerDown,
|
|
171
|
+
onTouchStart,
|
|
172
|
+
onKeyDown
|
|
173
|
+
},
|
|
174
|
+
onWheelNative,
|
|
175
|
+
isDragging,
|
|
176
|
+
isZooming,
|
|
177
|
+
isPlacementActive: isGestureActive || isKeyboardPanning || isZooming
|
|
178
|
+
};
|
|
186
179
|
}
|
|
187
|
-
export {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
//# sourceMappingURL=use-interaction.mjs.map
|
|
180
|
+
export { useInteraction };
|
|
181
|
+
|
|
182
|
+
//# sourceMappingURL=use-interaction.mjs.map
|
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../src/image-editor/react/hooks/use-interaction.ts"],
|
|
4
|
-
"sourcesContent": ["import { useCallback, useEffect, useRef, useState } from '@wordpress/element';\nimport type { CropperState, Size } from '../../core/types';\nimport {\n\tInteractionController,\n\ttype CropperInteractionActions,\n} from '../../core/interaction-controller';\n\n/**\n * The return type of the useInteraction hook.\n */\nexport interface UseInteractionReturn {\n\t/** Event handler props to spread on the container element. */\n\thandlers: {\n\t\tonPointerDown: ( e: React.PointerEvent ) => void;\n\t\tonTouchStart: ( e: React.TouchEvent ) => void;\n\t\tonKeyDown: ( e: React.KeyboardEvent ) => void;\n\t};\n\t/** Native wheel handler — must be registered with { passive: false }. */\n\tonWheelNative: ( e: WheelEvent ) => void;\n\t/** Whether a drag (pan) interaction is in progress. */\n\tisDragging: boolean;\n\t/** Whether a double-tap zoom animation is in progress. */\n\tisZooming: boolean;\n\t/** Whether the user is currently performing a placement interaction. */\n\tisPlacementActive: boolean;\n}\n\n/**\n * Options for the useInteraction hook.\n */\nexport interface UseInteractionOptions {\n\t/** Minimum zoom level. Defaults to MIN_ZOOM. */\n\tminZoom?: number;\n\t/** Maximum zoom level. Defaults to MAX_ZOOM. */\n\tmaxZoom?: number;\n\t/** Zoom speed multiplier for wheel events. Defaults to 0.0025. */\n\tzoomSpeed?: number;\n\t/**\n\t * Pan step size in normalized coords for keyboard events.\n\t * Defaults to 0.01. Shift multiplies it by 10.\n\t */\n\tkeyboardStep?: number;\n\t/** Zoom level for double-tap zoom. Defaults to 2. */\n\tdoubleTapZoom?: number;\n\t/** Fires when a continuous gesture begins (pan drag, pinch zoom). */\n\tonGestureStart?: () => void;\n\t/** Fires when a continuous gesture ends (pointer release). */\n\tonGestureEnd?: () => void;\n\t/**\n\t * Ignore all interaction. Any gesture already in flight is torn down,\n\t * so a drag started before this flipped cannot keep moving the image.\n\t */\n\tdisabled?: boolean;\n}\n\n/** How long keyboard placement stays active after the latest handled key. */\nconst KEYBOARD_INTERACTION_IDLE_MS = 300;\n\nfunction isHandledKeyboardPan( event: KeyboardEvent ): boolean {\n\tswitch ( event.key ) {\n\t\tcase 'ArrowUp':\n\t\tcase 'ArrowDown':\n\t\tcase 'ArrowLeft':\n\t\tcase 'ArrowRight':\n\t\t\treturn true;\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nfunction isHandledKeyboardZoom( event: KeyboardEvent ): boolean {\n\tswitch ( event.key ) {\n\t\tcase '+':\n\t\tcase '=':\n\t\tcase '-':\n\t\tcase '_':\n\t\t\treturn true;\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\n/**\n * Mouse, touch, and keyboard event handling for pan, zoom,\n * and crop manipulation.\n *\n * Returns event handler props to spread on the container element.\n * Uses requestAnimationFrame for drag/pinch updates to avoid\n * layout thrashing.\n *\n * @param state The current cropper state.\n * @param actions Named state updates for cropper interactions.\n * @param containerSize The container dimensions in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param options Optional configuration for zoom and keyboard behavior.\n * @return Event handler props for the container element.\n */\nexport function useInteraction(\n\tstate: CropperState,\n\tactions: CropperInteractionActions,\n\tcontainerSize: Size,\n\timageSize?: Size,\n\toptions?: UseInteractionOptions\n): UseInteractionReturn {\n\tconst [ isDragging, setIsDragging ] = useState( false );\n\tconst [ isZooming, setIsZooming ] = useState( false );\n\tconst [ isGestureActive, setIsGestureActive ] = useState( false );\n\tconst [ isKeyboardPanning, setIsKeyboardPanning ] = useState( false );\n\tconst keyboardInteractionTimerRef = useRef<\n\t\tReturnType< typeof setTimeout > | undefined\n\t>( undefined );\n\t// Tracks whether a keyboard gesture (pan or zoom) is currently active so\n\t// onGestureStart is only fired once per gesture, not on every key repeat.\n\tconst isKeyboardGestureActiveRef = useRef( false );\n\n\t// Keep mutable refs so the controller always reads fresh values\n\t// without needing to be recreated.\n\tconst stateRef = useRef( state );\n\tstateRef.current = state;\n\tconst containerSizeRef = useRef( containerSize );\n\tcontainerSizeRef.current = containerSize;\n\tconst imageSizeRef = useRef( imageSize );\n\timageSizeRef.current = imageSize;\n\tconst optionsRef = useRef( options );\n\toptionsRef.current = options;\n\tconst actionsRef = useRef( actions );\n\tactionsRef.current = actions;\n\n\tconst controllerRef = useRef< InteractionController | null >( null );\n\tconst startPlacementGesture = useCallback( () => {\n\t\tsetIsGestureActive( true );\n\t}, [] );\n\tconst stopPlacementGesture = useCallback( () => {\n\t\tsetIsGestureActive( false );\n\t}, [] );\n\t// Shared timer logic for any keyboard gesture (pan or zoom): fires\n\t// onGestureStart once per burst and onGestureEnd after the idle window.\n\tconst signalKeyboardGesture = useCallback( () => {\n\t\tif ( ! isKeyboardGestureActiveRef.current ) {\n\t\t\tisKeyboardGestureActiveRef.current = true;\n\t\t\toptionsRef.current?.onGestureStart?.();\n\t\t}\n\t\tclearTimeout( keyboardInteractionTimerRef.current );\n\t\tkeyboardInteractionTimerRef.current = setTimeout( () => {\n\t\t\tisKeyboardGestureActiveRef.current = false;\n\t\t\tsetIsKeyboardPanning( false );\n\t\t\toptionsRef.current?.onGestureEnd?.();\n\t\t}, KEYBOARD_INTERACTION_IDLE_MS );\n\t}, [] );\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tclearTimeout( keyboardInteractionTimerRef.current );\n\t\t};\n\t}, [] );\n\n\t// Create / destroy the controller. The controller reads all volatile\n\t// values through refs, so it can stay mounted across render updates.\n\tuseEffect( () => {\n\t\tconst controller = new InteractionController( {\n\t\t\tgetState: () => stateRef.current,\n\t\t\tactions: {\n\t\t\t\tsetPan: ( pan ) => actionsRef.current.setPan( pan ),\n\t\t\t\tsetZoom: ( zoom ) => actionsRef.current.setZoom( zoom ),\n\t\t\t\tsetZoomAtPoint: ( zoom, pan ) =>\n\t\t\t\t\tactionsRef.current.setZoomAtPoint( zoom, pan ),\n\t\t\t\tsnapRotate90: ( direction ) =>\n\t\t\t\t\tactionsRef.current.snapRotate90( direction ),\n\t\t\t\ttoggleFlip: ( direction ) =>\n\t\t\t\t\tactionsRef.current.toggleFlip?.( direction ),\n\t\t\t},\n\t\t\tgetContainerSize: () => containerSizeRef.current,\n\t\t\tgetImageSize: () => imageSizeRef.current,\n\t\t\tget minZoom() {\n\t\t\t\treturn optionsRef.current?.minZoom;\n\t\t\t},\n\t\t\tget maxZoom() {\n\t\t\t\treturn optionsRef.current?.maxZoom;\n\t\t\t},\n\t\t\tget zoomSpeed() {\n\t\t\t\treturn optionsRef.current?.zoomSpeed;\n\t\t\t},\n\t\t\tget keyboardStep() {\n\t\t\t\treturn optionsRef.current?.keyboardStep;\n\t\t\t},\n\t\t\tget doubleTapZoom() {\n\t\t\t\treturn optionsRef.current?.doubleTapZoom;\n\t\t\t},\n\t\t\tonGestureStart: () => {\n\t\t\t\tstartPlacementGesture();\n\t\t\t\toptionsRef.current?.onGestureStart?.();\n\t\t\t},\n\t\t\tonGestureEnd: () => {\n\t\t\t\tstopPlacementGesture();\n\t\t\t\toptionsRef.current?.onGestureEnd?.();\n\t\t\t},\n\t\t\tonStatusChange: ( status ) => {\n\t\t\t\tsetIsDragging( status.isDragging );\n\t\t\t\tsetIsZooming( status.isZooming );\n\t\t\t},\n\t\t} );\n\t\tcontrollerRef.current = controller;\n\t\treturn () => {\n\t\t\tcontroller.destroy();\n\t\t\tcontrollerRef.current = null;\n\t\t};\n\t}, [ startPlacementGesture, stopPlacementGesture ] );\n\n\t// Cancel any gesture already in flight when interaction is switched off,\n\t// so a drag that started before the flip cannot keep moving the image.\n\t// `destroy()` releases the pointer/touch listeners and the pending rAF;\n\t// the controller keeps working for later gestures once re-enabled.\n\t//\n\t// It does not signal the end of the gesture it just cut short, so that\n\t// is done here: a caller told a gesture began is otherwise left waiting\n\t// for an end that never comes, which keeps undo/redo disabled and leaves\n\t// the cropper's history batch open after a failed save.\n\tconst isDisabled = options?.disabled ?? false;\n\tuseEffect( () => {\n\t\tif ( ! isDisabled ) {\n\t\t\treturn;\n\t\t}\n\t\tcontrollerRef.current?.destroy();\n\t\tsetIsDragging( false );\n\t\tsetIsZooming( false );\n\t\tsetIsKeyboardPanning( false );\n\t\tconst wasGestureOpen =\n\t\t\tisGestureActive || isKeyboardGestureActiveRef.current;\n\t\tif ( isKeyboardGestureActiveRef.current ) {\n\t\t\tisKeyboardGestureActiveRef.current = false;\n\t\t\tclearTimeout( keyboardInteractionTimerRef.current );\n\t\t}\n\t\tstopPlacementGesture();\n\t\tif ( wasGestureOpen ) {\n\t\t\toptionsRef.current?.onGestureEnd?.();\n\t\t}\n\t}, [ isDisabled, isGestureActive, stopPlacementGesture ] );\n\n\tconst onPointerDown = useCallback( ( e: React.PointerEvent ) => {\n\t\tif ( optionsRef.current?.disabled ) {\n\t\t\treturn;\n\t\t}\n\t\tconst el = e.currentTarget as HTMLElement;\n\t\tcontrollerRef.current?.handlePointerDown( e.nativeEvent, el );\n\t}, [] );\n\n\tconst onTouchStart = useCallback( ( e: React.TouchEvent ) => {\n\t\tif ( optionsRef.current?.disabled ) {\n\t\t\treturn;\n\t\t}\n\t\tconst el = e.currentTarget as HTMLElement;\n\t\tconst rect = el.getBoundingClientRect();\n\t\tcontrollerRef.current?.handleTouchStart(\n\t\t\te.nativeEvent,\n\t\t\trect,\n\t\t\tel.ownerDocument\n\t\t);\n\t}, [] );\n\n\tconst onKeyDown = useCallback(\n\t\t( e: React.KeyboardEvent ) => {\n\t\t\tif ( optionsRef.current?.disabled ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( isHandledKeyboardPan( e.nativeEvent ) ) {\n\t\t\t\tsetIsKeyboardPanning( true );\n\t\t\t\tsignalKeyboardGesture();\n\t\t\t} else if ( isHandledKeyboardZoom( e.nativeEvent ) ) {\n\t\t\t\tsignalKeyboardGesture();\n\t\t\t}\n\t\t\tcontrollerRef.current?.handleKeyDown( e.nativeEvent );\n\t\t},\n\t\t[ signalKeyboardGesture ]\n\t);\n\n\tconst onWheelNative = useCallback( ( e: WheelEvent ) => {\n\t\tif ( optionsRef.current?.disabled ) {\n\t\t\treturn;\n\t\t}\n\t\tcontrollerRef.current?.handleWheel( e );\n\t}, [] );\n\n\treturn {\n\t\thandlers: {\n\t\t\tonPointerDown,\n\t\t\tonTouchStart,\n\t\t\tonKeyDown,\n\t\t},\n\t\tonWheelNative,\n\t\tisDragging,\n\t\tisZooming,\n\t\tisPlacementActive: isGestureActive || isKeyboardPanning || isZooming,\n\t};\n}\n"],
|
|
5
|
-
"mappings": ";AAAA,SAAS,aAAa,WAAW,QAAQ,gBAAgB;AAEzD;AAAA,EACC;AAAA,OAEM;AAmDP,IAAM,+BAA+B;AAErC,SAAS,qBAAsB,OAAgC;AAC9D,UAAS,MAAM,KAAM;AAAA,IACpB,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACJ,aAAO;AAAA,IACR;AACC,aAAO;AAAA,EACT;AACD;AAEA,SAAS,sBAAuB,OAAgC;AAC/D,UAAS,MAAM,KAAM;AAAA,IACpB,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACJ,aAAO;AAAA,IACR;AACC,aAAO;AAAA,EACT;AACD;AAiBO,SAAS,eACf,OACA,SACA,eACA,WACA,SACu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AAAA,MACA;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,mBAAmB,mBAAmB,qBAAqB;AAAA,EAC5D;AACD;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
1
|
+
{"version":3,"file":"use-interaction.mjs","names":[],"sources":["../../../../src/image-editor/react/hooks/use-interaction.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from '@wordpress/element';\nimport type { CropperState, Size } from '../../core/types';\nimport {\n\tInteractionController,\n\ttype CropperInteractionActions,\n} from '../../core/interaction-controller';\n\n/**\n * The return type of the useInteraction hook.\n */\nexport interface UseInteractionReturn {\n\t/** Event handler props to spread on the container element. */\n\thandlers: {\n\t\tonPointerDown: ( e: React.PointerEvent ) => void;\n\t\tonTouchStart: ( e: React.TouchEvent ) => void;\n\t\tonKeyDown: ( e: React.KeyboardEvent ) => void;\n\t};\n\t/** Native wheel handler — must be registered with { passive: false }. */\n\tonWheelNative: ( e: WheelEvent ) => void;\n\t/** Whether a drag (pan) interaction is in progress. */\n\tisDragging: boolean;\n\t/** Whether a double-tap zoom animation is in progress. */\n\tisZooming: boolean;\n\t/** Whether the user is currently performing a placement interaction. */\n\tisPlacementActive: boolean;\n}\n\n/**\n * Options for the useInteraction hook.\n */\nexport interface UseInteractionOptions {\n\t/** Minimum zoom level. Defaults to MIN_ZOOM. */\n\tminZoom?: number;\n\t/** Maximum zoom level. Defaults to MAX_ZOOM. */\n\tmaxZoom?: number;\n\t/** Zoom speed multiplier for wheel events. Defaults to 0.0025. */\n\tzoomSpeed?: number;\n\t/**\n\t * Pan step size in normalized coords for keyboard events.\n\t * Defaults to 0.01. Shift multiplies it by 10.\n\t */\n\tkeyboardStep?: number;\n\t/** Zoom level for double-tap zoom. Defaults to 2. */\n\tdoubleTapZoom?: number;\n\t/** Fires when a continuous gesture begins (pan drag, pinch zoom). */\n\tonGestureStart?: () => void;\n\t/** Fires when a continuous gesture ends (pointer release). */\n\tonGestureEnd?: () => void;\n\t/**\n\t * Ignore all interaction. Any gesture already in flight is torn down,\n\t * so a drag started before this flipped cannot keep moving the image.\n\t */\n\tdisabled?: boolean;\n}\n\n/** How long keyboard placement stays active after the latest handled key. */\nconst KEYBOARD_INTERACTION_IDLE_MS = 300;\n\nfunction isHandledKeyboardPan( event: KeyboardEvent ): boolean {\n\tswitch ( event.key ) {\n\t\tcase 'ArrowUp':\n\t\tcase 'ArrowDown':\n\t\tcase 'ArrowLeft':\n\t\tcase 'ArrowRight':\n\t\t\treturn true;\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nfunction isHandledKeyboardZoom( event: KeyboardEvent ): boolean {\n\tswitch ( event.key ) {\n\t\tcase '+':\n\t\tcase '=':\n\t\tcase '-':\n\t\tcase '_':\n\t\t\treturn true;\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\n/**\n * Mouse, touch, and keyboard event handling for pan, zoom,\n * and crop manipulation.\n *\n * Returns event handler props to spread on the container element.\n * Uses requestAnimationFrame for drag/pinch updates to avoid\n * layout thrashing.\n *\n * @param state The current cropper state.\n * @param actions Named state updates for cropper interactions.\n * @param containerSize The container dimensions in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param options Optional configuration for zoom and keyboard behavior.\n * @return Event handler props for the container element.\n */\nexport function useInteraction(\n\tstate: CropperState,\n\tactions: CropperInteractionActions,\n\tcontainerSize: Size,\n\timageSize?: Size,\n\toptions?: UseInteractionOptions\n): UseInteractionReturn {\n\tconst [ isDragging, setIsDragging ] = useState( false );\n\tconst [ isZooming, setIsZooming ] = useState( false );\n\tconst [ isGestureActive, setIsGestureActive ] = useState( false );\n\tconst [ isKeyboardPanning, setIsKeyboardPanning ] = useState( false );\n\tconst keyboardInteractionTimerRef = useRef<\n\t\tReturnType< typeof setTimeout > | undefined\n\t>( undefined );\n\t// Tracks whether a keyboard gesture (pan or zoom) is currently active so\n\t// onGestureStart is only fired once per gesture, not on every key repeat.\n\tconst isKeyboardGestureActiveRef = useRef( false );\n\n\t// Keep mutable refs so the controller always reads fresh values\n\t// without needing to be recreated.\n\tconst stateRef = useRef( state );\n\tstateRef.current = state;\n\tconst containerSizeRef = useRef( containerSize );\n\tcontainerSizeRef.current = containerSize;\n\tconst imageSizeRef = useRef( imageSize );\n\timageSizeRef.current = imageSize;\n\tconst optionsRef = useRef( options );\n\toptionsRef.current = options;\n\tconst actionsRef = useRef( actions );\n\tactionsRef.current = actions;\n\n\tconst controllerRef = useRef< InteractionController | null >( null );\n\tconst startPlacementGesture = useCallback( () => {\n\t\tsetIsGestureActive( true );\n\t}, [] );\n\tconst stopPlacementGesture = useCallback( () => {\n\t\tsetIsGestureActive( false );\n\t}, [] );\n\t// Shared timer logic for any keyboard gesture (pan or zoom): fires\n\t// onGestureStart once per burst and onGestureEnd after the idle window.\n\tconst signalKeyboardGesture = useCallback( () => {\n\t\tif ( ! isKeyboardGestureActiveRef.current ) {\n\t\t\tisKeyboardGestureActiveRef.current = true;\n\t\t\toptionsRef.current?.onGestureStart?.();\n\t\t}\n\t\tclearTimeout( keyboardInteractionTimerRef.current );\n\t\tkeyboardInteractionTimerRef.current = setTimeout( () => {\n\t\t\tisKeyboardGestureActiveRef.current = false;\n\t\t\tsetIsKeyboardPanning( false );\n\t\t\toptionsRef.current?.onGestureEnd?.();\n\t\t}, KEYBOARD_INTERACTION_IDLE_MS );\n\t}, [] );\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tclearTimeout( keyboardInteractionTimerRef.current );\n\t\t};\n\t}, [] );\n\n\t// Create / destroy the controller. The controller reads all volatile\n\t// values through refs, so it can stay mounted across render updates.\n\tuseEffect( () => {\n\t\tconst controller = new InteractionController( {\n\t\t\tgetState: () => stateRef.current,\n\t\t\tactions: {\n\t\t\t\tsetPan: ( pan ) => actionsRef.current.setPan( pan ),\n\t\t\t\tsetZoom: ( zoom ) => actionsRef.current.setZoom( zoom ),\n\t\t\t\tsetZoomAtPoint: ( zoom, pan ) =>\n\t\t\t\t\tactionsRef.current.setZoomAtPoint( zoom, pan ),\n\t\t\t\tsnapRotate90: ( direction ) =>\n\t\t\t\t\tactionsRef.current.snapRotate90( direction ),\n\t\t\t\ttoggleFlip: ( direction ) =>\n\t\t\t\t\tactionsRef.current.toggleFlip?.( direction ),\n\t\t\t},\n\t\t\tgetContainerSize: () => containerSizeRef.current,\n\t\t\tgetImageSize: () => imageSizeRef.current,\n\t\t\tget minZoom() {\n\t\t\t\treturn optionsRef.current?.minZoom;\n\t\t\t},\n\t\t\tget maxZoom() {\n\t\t\t\treturn optionsRef.current?.maxZoom;\n\t\t\t},\n\t\t\tget zoomSpeed() {\n\t\t\t\treturn optionsRef.current?.zoomSpeed;\n\t\t\t},\n\t\t\tget keyboardStep() {\n\t\t\t\treturn optionsRef.current?.keyboardStep;\n\t\t\t},\n\t\t\tget doubleTapZoom() {\n\t\t\t\treturn optionsRef.current?.doubleTapZoom;\n\t\t\t},\n\t\t\tonGestureStart: () => {\n\t\t\t\tstartPlacementGesture();\n\t\t\t\toptionsRef.current?.onGestureStart?.();\n\t\t\t},\n\t\t\tonGestureEnd: () => {\n\t\t\t\tstopPlacementGesture();\n\t\t\t\toptionsRef.current?.onGestureEnd?.();\n\t\t\t},\n\t\t\tonStatusChange: ( status ) => {\n\t\t\t\tsetIsDragging( status.isDragging );\n\t\t\t\tsetIsZooming( status.isZooming );\n\t\t\t},\n\t\t} );\n\t\tcontrollerRef.current = controller;\n\t\treturn () => {\n\t\t\tcontroller.destroy();\n\t\t\tcontrollerRef.current = null;\n\t\t};\n\t}, [ startPlacementGesture, stopPlacementGesture ] );\n\n\t// Cancel any gesture already in flight when interaction is switched off,\n\t// so a drag that started before the flip cannot keep moving the image.\n\t// `destroy()` releases the pointer/touch listeners and the pending rAF;\n\t// the controller keeps working for later gestures once re-enabled.\n\t//\n\t// It does not signal the end of the gesture it just cut short, so that\n\t// is done here: a caller told a gesture began is otherwise left waiting\n\t// for an end that never comes, which keeps undo/redo disabled and leaves\n\t// the cropper's history batch open after a failed save.\n\tconst isDisabled = options?.disabled ?? false;\n\tuseEffect( () => {\n\t\tif ( ! isDisabled ) {\n\t\t\treturn;\n\t\t}\n\t\tcontrollerRef.current?.destroy();\n\t\tsetIsDragging( false );\n\t\tsetIsZooming( false );\n\t\tsetIsKeyboardPanning( false );\n\t\tconst wasGestureOpen =\n\t\t\tisGestureActive || isKeyboardGestureActiveRef.current;\n\t\tif ( isKeyboardGestureActiveRef.current ) {\n\t\t\tisKeyboardGestureActiveRef.current = false;\n\t\t\tclearTimeout( keyboardInteractionTimerRef.current );\n\t\t}\n\t\tstopPlacementGesture();\n\t\tif ( wasGestureOpen ) {\n\t\t\toptionsRef.current?.onGestureEnd?.();\n\t\t}\n\t}, [ isDisabled, isGestureActive, stopPlacementGesture ] );\n\n\tconst onPointerDown = useCallback( ( e: React.PointerEvent ) => {\n\t\tif ( optionsRef.current?.disabled ) {\n\t\t\treturn;\n\t\t}\n\t\tconst el = e.currentTarget as HTMLElement;\n\t\tcontrollerRef.current?.handlePointerDown( e.nativeEvent, el );\n\t}, [] );\n\n\tconst onTouchStart = useCallback( ( e: React.TouchEvent ) => {\n\t\tif ( optionsRef.current?.disabled ) {\n\t\t\treturn;\n\t\t}\n\t\tconst el = e.currentTarget as HTMLElement;\n\t\tconst rect = el.getBoundingClientRect();\n\t\tcontrollerRef.current?.handleTouchStart(\n\t\t\te.nativeEvent,\n\t\t\trect,\n\t\t\tel.ownerDocument\n\t\t);\n\t}, [] );\n\n\tconst onKeyDown = useCallback(\n\t\t( e: React.KeyboardEvent ) => {\n\t\t\tif ( optionsRef.current?.disabled ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( isHandledKeyboardPan( e.nativeEvent ) ) {\n\t\t\t\tsetIsKeyboardPanning( true );\n\t\t\t\tsignalKeyboardGesture();\n\t\t\t} else if ( isHandledKeyboardZoom( e.nativeEvent ) ) {\n\t\t\t\tsignalKeyboardGesture();\n\t\t\t}\n\t\t\tcontrollerRef.current?.handleKeyDown( e.nativeEvent );\n\t\t},\n\t\t[ signalKeyboardGesture ]\n\t);\n\n\tconst onWheelNative = useCallback( ( e: WheelEvent ) => {\n\t\tif ( optionsRef.current?.disabled ) {\n\t\t\treturn;\n\t\t}\n\t\tcontrollerRef.current?.handleWheel( e );\n\t}, [] );\n\n\treturn {\n\t\thandlers: {\n\t\t\tonPointerDown,\n\t\t\tonTouchStart,\n\t\t\tonKeyDown,\n\t\t},\n\t\tonWheelNative,\n\t\tisDragging,\n\t\tisZooming,\n\t\tisPlacementActive: isGestureActive || isKeyboardPanning || isZooming,\n\t};\n}\n"],"mappings":";;;;AAwDA,MAAM,+BAA+B;AAErC,SAAS,qBAAsB,OAAgC;CAC9D,QAAS,MAAM,KAAf;EACC,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,cACJ,OAAO;EACR,SACC,OAAO;CACT;AACD;AAEA,SAAS,sBAAuB,OAAgC;CAC/D,QAAS,MAAM,KAAf;EACC,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,KACJ,OAAO;EACR,SACC,OAAO;CACT;AACD;;;;;;;;;;;;;;;;AAiBA,SAAgB,eACf,OACA,SACA,eACA,WACA,SACuB;CACvB,MAAM,CAAE,YAAY,iBAAkB,SAAU,KAAM;CACtD,MAAM,CAAE,WAAW,gBAAiB,SAAU,KAAM;CACpD,MAAM,CAAE,iBAAiB,sBAAuB,SAAU,KAAM;CAChE,MAAM,CAAE,mBAAmB,wBAAyB,SAAU,KAAM;CACpE,MAAM,8BAA8B,OAEjC,KAAA,CAAU;CAGb,MAAM,6BAA6B,OAAQ,KAAM;CAIjD,MAAM,WAAW,OAAQ,KAAM;CAC/B,SAAS,UAAU;CACnB,MAAM,mBAAmB,OAAQ,aAAc;CAC/C,iBAAiB,UAAU;CAC3B,MAAM,eAAe,OAAQ,SAAU;CACvC,aAAa,UAAU;CACvB,MAAM,aAAa,OAAQ,OAAQ;CACnC,WAAW,UAAU;CACrB,MAAM,aAAa,OAAQ,OAAQ;CACnC,WAA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B,aAAe,MAAmB;EACvD,IAAK,WAAW,SAAS,UACxB;EAED,cAAc,SAAS,YAAa,CAAE;CACvC,GAAG,CAAC,CAAE;CAEN,OAAO;EACN,UAAU;GACT;GACA;GACA;EACD;EACA;EACA;EACA;EACA,mBAAmB,mBAAmB,qBAAqB;CAC5D;AACD"}
|
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
// packages/media-editor/src/image-editor/react/hooks/use-transform-style.ts
|
|
2
1
|
import { useMemo } from "@wordpress/element";
|
|
3
2
|
import { computeTransformStyle } from "../../core/transform-style.mjs";
|
|
3
|
+
// packages/media-editor/src/image-editor/react/hooks/use-transform-style.ts
|
|
4
|
+
/**
|
|
5
|
+
* React hook that computes a memoized CSS matrix() transform string
|
|
6
|
+
* from the cropper state.
|
|
7
|
+
*
|
|
8
|
+
* Delegates to the pure `computeTransformStyle` function in core.
|
|
9
|
+
*
|
|
10
|
+
* @param state The current cropper state.
|
|
11
|
+
* @param imageSize The rendered image dimensions in pixels.
|
|
12
|
+
* @return A CSS transform string.
|
|
13
|
+
*/
|
|
4
14
|
function useTransformStyle(state, imageSize) {
|
|
5
|
-
|
|
6
|
-
() => computeTransformStyle(state, imageSize),
|
|
7
|
-
[state, imageSize]
|
|
8
|
-
);
|
|
15
|
+
return useMemo(() => computeTransformStyle(state, imageSize), [state, imageSize]);
|
|
9
16
|
}
|
|
10
|
-
export {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
//# sourceMappingURL=use-transform-style.mjs.map
|
|
17
|
+
export { useTransformStyle };
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=use-transform-style.mjs.map
|
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../src/image-editor/react/hooks/use-transform-style.ts"],
|
|
4
|
-
"sourcesContent": ["import { useMemo } from '@wordpress/element';\nimport type { CropperState, Size } from '../../core/types';\nimport { computeTransformStyle } from '../../core/transform-style';\n\n/**\n * React hook that computes a memoized CSS matrix() transform string\n * from the cropper state.\n *\n * Delegates to the pure `computeTransformStyle` function in core.\n *\n * @param state The current cropper state.\n * @param imageSize The rendered image dimensions in pixels.\n * @return A CSS transform string.\n */\nexport function useTransformStyle(\n\tstate: CropperState,\n\timageSize: Size\n): string {\n\treturn useMemo(\n\t\t() => computeTransformStyle( state, imageSize ),\n\t\t[ state, imageSize ]\n\t);\n}\n"],
|
|
5
|
-
"mappings": ";AAAA,SAAS,eAAe;AAExB,SAAS,6BAA6B;AAY/B,SAAS,kBACf,OACA,WACS;AACT,SAAO;AAAA,IACN,MAAM,sBAAuB,OAAO,SAAU;AAAA,IAC9C,CAAE,OAAO,SAAU;AAAA,EACpB;AACD;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
1
|
+
{"version":3,"file":"use-transform-style.mjs","names":[],"sources":["../../../../src/image-editor/react/hooks/use-transform-style.ts"],"sourcesContent":["import { useMemo } from '@wordpress/element';\nimport type { CropperState, Size } from '../../core/types';\nimport { computeTransformStyle } from '../../core/transform-style';\n\n/**\n * React hook that computes a memoized CSS matrix() transform string\n * from the cropper state.\n *\n * Delegates to the pure `computeTransformStyle` function in core.\n *\n * @param state The current cropper state.\n * @param imageSize The rendered image dimensions in pixels.\n * @return A CSS transform string.\n */\nexport function useTransformStyle(\n\tstate: CropperState,\n\timageSize: Size\n): string {\n\treturn useMemo(\n\t\t() => computeTransformStyle( state, imageSize ),\n\t\t[ state, imageSize ]\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;AAcA,SAAgB,kBACf,OACA,WACS;CACT,OAAO,cACA,sBAAuB,OAAO,SAAU,GAC9C,CAAE,OAAO,SAAU,CACpB;AACD"}
|
|
@@ -1,29 +1,35 @@
|
|
|
1
|
+
import { useCallback, useMemo, useReducer } from "@wordpress/element";
|
|
2
|
+
import { DEFAULT_VIEWPORT_STATE, viewportReducer } from "../../core/viewport-state.mjs";
|
|
1
3
|
// packages/media-editor/src/image-editor/react/hooks/use-viewport-state.ts
|
|
2
|
-
import { useReducer, useCallback, useMemo } from "@wordpress/element";
|
|
3
|
-
import {
|
|
4
|
-
viewportReducer,
|
|
5
|
-
DEFAULT_VIEWPORT_STATE
|
|
6
|
-
} from "../../core/viewport-state.mjs";
|
|
7
4
|
function useViewportState() {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
5
|
+
const [viewport, dispatch] = useReducer(viewportReducer, DEFAULT_VIEWPORT_STATE);
|
|
6
|
+
const setViewportZoom = useCallback((zoom) => {
|
|
7
|
+
dispatch({
|
|
8
|
+
type: "SET_VIEWPORT_ZOOM",
|
|
9
|
+
payload: zoom
|
|
10
|
+
});
|
|
11
|
+
}, []);
|
|
12
|
+
const setViewportPan = useCallback((pan) => {
|
|
13
|
+
dispatch({
|
|
14
|
+
type: "SET_VIEWPORT_PAN",
|
|
15
|
+
payload: pan
|
|
16
|
+
});
|
|
17
|
+
}, []);
|
|
18
|
+
const resetViewport = useCallback(() => {
|
|
19
|
+
dispatch({ type: "RESET_VIEWPORT" });
|
|
20
|
+
}, []);
|
|
21
|
+
return useMemo(() => ({
|
|
22
|
+
viewport,
|
|
23
|
+
setViewportZoom,
|
|
24
|
+
setViewportPan,
|
|
25
|
+
resetViewport
|
|
26
|
+
}), [
|
|
27
|
+
viewport,
|
|
28
|
+
setViewportZoom,
|
|
29
|
+
setViewportPan,
|
|
30
|
+
resetViewport
|
|
31
|
+
]);
|
|
25
32
|
}
|
|
26
|
-
export {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
//# sourceMappingURL=use-viewport-state.mjs.map
|
|
33
|
+
export { useViewportState };
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=use-viewport-state.mjs.map
|