@wordpress/media-editor 0.21.0 → 0.23.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 +30 -0
- package/build/components/media-editor/index.cjs +586 -686
- package/build/components/media-editor/index.cjs.map +1 -7
- package/build/components/media-editor/use-crop-options.cjs +42 -57
- package/build/components/media-editor/use-crop-options.cjs.map +1 -7
- package/build/components/media-editor/use-save-media-editor.cjs +131 -185
- package/build/components/media-editor/use-save-media-editor.cjs.map +1 -7
- package/build/components/media-editor-canvas/index.cjs +117 -124
- package/build/components/media-editor-canvas/index.cjs.map +1 -7
- package/build/components/media-editor-crop-panel/index.cjs +81 -72
- package/build/components/media-editor-crop-panel/index.cjs.map +1 -7
- package/build/components/media-editor-fine-rotation/index.cjs +61 -72
- package/build/components/media-editor-fine-rotation/index.cjs.map +1 -7
- package/build/components/media-editor-image-controls/index.cjs +181 -257
- package/build/components/media-editor-image-controls/index.cjs.map +1 -7
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +120 -157
- package/build/components/media-editor-keyboard-shortcuts-modal/index.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 +82 -77
- package/build/components/media-form/index.cjs.map +1 -7
- package/build/components/media-preview/index.cjs +113 -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/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 +59 -0
- package/build/state/aspect-ratio-presets.cjs.map +1 -0
- package/build/state/composite-reducer.cjs +100 -113
- package/build/state/composite-reducer.cjs.map +1 -7
- package/build/state/index.cjs +22 -45
- 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 +11 -31
- package/build/state/types.cjs.map +1 -7
- package/build/state/use-media-editor-state.cjs +227 -296
- 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 +545 -675
- package/build-module/components/media-editor/index.mjs.map +1 -7
- package/build-module/components/media-editor/use-crop-options.mjs +37 -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 +109 -157
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +1 -7
- package/build-module/components/media-editor-canvas/index.mjs +95 -98
- package/build-module/components/media-editor-canvas/index.mjs.map +1 -7
- package/build-module/components/media-editor-crop-panel/index.mjs +59 -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 -46
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -7
- package/build-module/components/media-editor-image-controls/index.mjs +171 -238
- package/build-module/components/media-editor-image-controls/index.mjs.map +1 -7
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +113 -137
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.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 +74 -56
- package/build-module/components/media-form/index.mjs.map +1 -7
- package/build-module/components/media-preview/index.mjs +105 -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/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 +53 -0
- package/build-module/state/aspect-ratio-presets.mjs.map +1 -0
- package/build-module/state/composite-reducer.mjs +80 -91
- package/build-module/state/composite-reducer.mjs.map +1 -7
- package/build-module/state/index.mjs +3 -26
- 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 +7 -7
- package/build-module/state/types.mjs.map +1 -7
- package/build-module/state/use-media-editor-state.mjs +223 -288
- 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 +145 -61
- package/build-style/style.css +144 -61
- package/build-types/components/media-editor/index.d.ts +20 -2
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +2 -3
- 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 +3 -3
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/index.d.ts +1 -1
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- 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/state/aspect-ratio-presets.d.ts +24 -0
- package/build-types/state/aspect-ratio-presets.d.ts.map +1 -0
- package/build-types/state/composite-reducer.d.ts +17 -19
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/index.d.ts +2 -4
- package/build-types/state/index.d.ts.map +1 -1
- package/build-types/state/types.d.ts +20 -63
- package/build-types/state/types.d.ts.map +1 -1
- package/build-types/state/use-media-editor-state.d.ts +33 -52
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/package.json +22 -23
- package/src/components/media-editor/index.tsx +355 -167
- package/src/components/media-editor/style.scss +19 -0
- package/src/components/media-editor/use-crop-options.ts +9 -10
- package/src/components/media-editor/use-save-media-editor.ts +48 -10
- package/src/components/media-editor-canvas/index.tsx +30 -51
- package/src/components/media-editor-canvas/style.scss +6 -6
- package/src/components/media-editor-crop-panel/index.tsx +38 -2
- package/src/components/media-editor-crop-panel/style.scss +21 -0
- package/src/components/media-editor-fine-rotation/index.tsx +6 -6
- package/src/components/media-editor-image-controls/index.tsx +15 -11
- package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +1 -0
- package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +1 -1
- package/src/components/media-editor-modal/index.tsx +1 -1
- package/src/components/media-editor-modal/style.scss +1 -1
- package/src/components/media-form/index.tsx +7 -1
- package/src/components/media-preview/index.tsx +65 -23
- package/src/components/media-preview/style.scss +16 -2
- package/src/components/rotation-ruler/style.scss +9 -5
- package/src/components/rotation-ruler/use-ruler-drag.ts +6 -11
- package/src/state/README.md +32 -0
- package/src/state/aspect-ratio-presets.ts +37 -0
- package/src/state/composite-reducer.ts +42 -80
- package/src/state/index.ts +2 -17
- package/src/state/types.ts +18 -72
- package/src/state/use-media-editor-state.ts +155 -238
- package/src/style.scss +2 -1
- package/build/components/media-editor-modal/build-modifiers.cjs +0 -91
- package/build/components/media-editor-modal/build-modifiers.cjs.map +0 -7
- package/build/image-editor/core/camera.cjs +0 -225
- package/build/image-editor/core/camera.cjs.map +0 -7
- package/build/image-editor/core/constants.cjs +0 -108
- package/build/image-editor/core/constants.cjs.map +0 -7
- package/build/image-editor/core/containment.cjs +0 -276
- package/build/image-editor/core/containment.cjs.map +0 -7
- package/build/image-editor/core/crop-rect.cjs +0 -48
- package/build/image-editor/core/crop-rect.cjs.map +0 -7
- package/build/image-editor/core/export/canvas-renderer.cjs +0 -151
- package/build/image-editor/core/export/canvas-renderer.cjs.map +0 -7
- package/build/image-editor/core/index.cjs +0 -62
- package/build/image-editor/core/index.cjs.map +0 -7
- package/build/image-editor/core/interaction-controller.cjs +0 -654
- package/build/image-editor/core/interaction-controller.cjs.map +0 -7
- package/build/image-editor/core/math/rotation.cjs +0 -41
- package/build/image-editor/core/math/rotation.cjs.map +0 -7
- package/build/image-editor/core/math/sanitize.cjs +0 -92
- package/build/image-editor/core/math/sanitize.cjs.map +0 -7
- package/build/image-editor/core/setter-helpers.cjs +0 -64
- package/build/image-editor/core/setter-helpers.cjs.map +0 -7
- package/build/image-editor/core/source-region.cjs +0 -206
- package/build/image-editor/core/source-region.cjs.map +0 -7
- package/build/image-editor/core/state.cjs +0 -283
- package/build/image-editor/core/state.cjs.map +0 -7
- package/build/image-editor/core/stencil-math.cjs +0 -216
- package/build/image-editor/core/stencil-math.cjs.map +0 -7
- package/build/image-editor/core/transform-style.cjs +0 -52
- package/build/image-editor/core/transform-style.cjs.map +0 -7
- package/build/image-editor/core/transforms/pipeline.cjs +0 -43
- package/build/image-editor/core/transforms/pipeline.cjs.map +0 -7
- package/build/image-editor/core/types.cjs +0 -19
- package/build/image-editor/core/types.cjs.map +0 -7
- package/build/image-editor/core/viewport-state.cjs +0 -50
- package/build/image-editor/core/viewport-state.cjs.map +0 -7
- package/build/image-editor/index.cjs +0 -67
- package/build/image-editor/index.cjs.map +0 -7
- package/build/image-editor/react/components/cropper-provider.cjs +0 -50
- package/build/image-editor/react/components/cropper-provider.cjs.map +0 -7
- package/build/image-editor/react/components/cropper.cjs +0 -722
- package/build/image-editor/react/components/cropper.cjs.map +0 -7
- package/build/image-editor/react/components/index.cjs +0 -36
- package/build/image-editor/react/components/index.cjs.map +0 -7
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +0 -172
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +0 -7
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +0 -62
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +0 -7
- package/build/image-editor/react/components/overlays/grid-overlay.cjs +0 -91
- package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +0 -7
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +0 -426
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +0 -7
- package/build/image-editor/react/components/viewport-provider.cjs +0 -51
- package/build/image-editor/react/components/viewport-provider.cjs.map +0 -7
- package/build/image-editor/react/hooks/build-cropper-setters.cjs +0 -71
- package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +0 -7
- package/build/image-editor/react/hooks/index.cjs +0 -34
- package/build/image-editor/react/hooks/index.cjs.map +0 -7
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +0 -160
- package/build/image-editor/react/hooks/use-aria-announcer.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/image-editor/react/hooks/use-interaction.cjs +0 -213
- package/build/image-editor/react/hooks/use-interaction.cjs.map +0 -7
- package/build/image-editor/react/hooks/use-transform-style.cjs +0 -38
- package/build/image-editor/react/hooks/use-transform-style.cjs.map +0 -7
- package/build/image-editor/react/hooks/use-viewport-state.cjs +0 -51
- package/build/image-editor/react/hooks/use-viewport-state.cjs.map +0 -7
- package/build/image-editor/react/visually-hidden-style.cjs +0 -41
- package/build/image-editor/react/visually-hidden-style.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/components/media-editor-modal/build-modifiers.mjs +0 -66
- package/build-module/components/media-editor-modal/build-modifiers.mjs.map +0 -7
- package/build-module/image-editor/core/camera.mjs +0 -198
- package/build-module/image-editor/core/camera.mjs.map +0 -7
- package/build-module/image-editor/core/constants.mjs +0 -69
- package/build-module/image-editor/core/constants.mjs.map +0 -7
- package/build-module/image-editor/core/containment.mjs +0 -253
- package/build-module/image-editor/core/containment.mjs.map +0 -7
- package/build-module/image-editor/core/crop-rect.mjs +0 -23
- package/build-module/image-editor/core/crop-rect.mjs.map +0 -7
- package/build-module/image-editor/core/export/canvas-renderer.mjs +0 -120
- package/build-module/image-editor/core/export/canvas-renderer.mjs.map +0 -7
- package/build-module/image-editor/core/index.mjs +0 -36
- package/build-module/image-editor/core/index.mjs.map +0 -7
- package/build-module/image-editor/core/interaction-controller.mjs +0 -635
- package/build-module/image-editor/core/interaction-controller.mjs.map +0 -7
- package/build-module/image-editor/core/math/rotation.mjs +0 -15
- package/build-module/image-editor/core/math/rotation.mjs.map +0 -7
- package/build-module/image-editor/core/math/sanitize.mjs +0 -63
- package/build-module/image-editor/core/math/sanitize.mjs.map +0 -7
- package/build-module/image-editor/core/setter-helpers.mjs +0 -39
- package/build-module/image-editor/core/setter-helpers.mjs.map +0 -7
- package/build-module/image-editor/core/source-region.mjs +0 -178
- package/build-module/image-editor/core/source-region.mjs.map +0 -7
- package/build-module/image-editor/core/state.mjs +0 -260
- package/build-module/image-editor/core/state.mjs.map +0 -7
- package/build-module/image-editor/core/stencil-math.mjs +0 -187
- package/build-module/image-editor/core/stencil-math.mjs.map +0 -7
- package/build-module/image-editor/core/transform-style.mjs +0 -27
- package/build-module/image-editor/core/transform-style.mjs.map +0 -7
- package/build-module/image-editor/core/transforms/pipeline.mjs +0 -17
- package/build-module/image-editor/core/transforms/pipeline.mjs.map +0 -7
- package/build-module/image-editor/core/types.mjs +0 -1
- package/build-module/image-editor/core/types.mjs.map +0 -7
- package/build-module/image-editor/core/viewport-state.mjs +0 -24
- package/build-module/image-editor/core/viewport-state.mjs.map +0 -7
- package/build-module/image-editor/index.mjs +0 -39
- package/build-module/image-editor/index.mjs.map +0 -7
- package/build-module/image-editor/react/components/cropper-provider.mjs +0 -26
- package/build-module/image-editor/react/components/cropper-provider.mjs.map +0 -7
- package/build-module/image-editor/react/components/cropper.mjs +0 -704
- package/build-module/image-editor/react/components/cropper.mjs.map +0 -7
- package/build-module/image-editor/react/components/index.mjs +0 -9
- package/build-module/image-editor/react/components/index.mjs.map +0 -7
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +0 -147
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +0 -7
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +0 -37
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +0 -7
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +0 -66
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +0 -7
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +0 -414
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +0 -7
- package/build-module/image-editor/react/components/viewport-provider.mjs +0 -27
- package/build-module/image-editor/react/components/viewport-provider.mjs.map +0 -7
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +0 -46
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +0 -7
- package/build-module/image-editor/react/hooks/index.mjs +0 -8
- package/build-module/image-editor/react/hooks/index.mjs.map +0 -7
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +0 -135
- package/build-module/image-editor/react/hooks/use-aria-announcer.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/image-editor/react/hooks/use-interaction.mjs +0 -190
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +0 -7
- package/build-module/image-editor/react/hooks/use-transform-style.mjs +0 -13
- package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +0 -7
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs +0 -29
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +0 -7
- package/build-module/image-editor/react/visually-hidden-style.mjs +0 -16
- package/build-module/image-editor/react/visually-hidden-style.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/components/media-editor-modal/build-modifiers.d.ts +0 -80
- package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +0 -1
- package/build-types/image-editor/core/camera.d.ts +0 -128
- package/build-types/image-editor/core/camera.d.ts.map +0 -1
- package/build-types/image-editor/core/constants.d.ts +0 -99
- package/build-types/image-editor/core/constants.d.ts.map +0 -1
- package/build-types/image-editor/core/containment.d.ts +0 -72
- package/build-types/image-editor/core/containment.d.ts.map +0 -1
- package/build-types/image-editor/core/crop-rect.d.ts +0 -16
- package/build-types/image-editor/core/crop-rect.d.ts.map +0 -1
- package/build-types/image-editor/core/export/canvas-renderer.d.ts +0 -103
- package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +0 -1
- package/build-types/image-editor/core/index.d.ts +0 -10
- package/build-types/image-editor/core/index.d.ts.map +0 -1
- package/build-types/image-editor/core/interaction-controller.d.ts +0 -191
- package/build-types/image-editor/core/interaction-controller.d.ts.map +0 -1
- package/build-types/image-editor/core/math/rotation.d.ts +0 -18
- package/build-types/image-editor/core/math/rotation.d.ts.map +0 -1
- package/build-types/image-editor/core/math/sanitize.d.ts +0 -67
- package/build-types/image-editor/core/math/sanitize.d.ts.map +0 -1
- package/build-types/image-editor/core/setter-helpers.d.ts +0 -21
- package/build-types/image-editor/core/setter-helpers.d.ts.map +0 -1
- package/build-types/image-editor/core/source-region.d.ts +0 -94
- package/build-types/image-editor/core/source-region.d.ts.map +0 -1
- package/build-types/image-editor/core/state.d.ts +0 -51
- package/build-types/image-editor/core/state.d.ts.map +0 -1
- package/build-types/image-editor/core/stencil-math.d.ts +0 -102
- package/build-types/image-editor/core/stencil-math.d.ts.map +0 -1
- package/build-types/image-editor/core/transform-style.d.ts +0 -20
- package/build-types/image-editor/core/transform-style.d.ts.map +0 -1
- package/build-types/image-editor/core/transforms/pipeline.d.ts +0 -24
- package/build-types/image-editor/core/transforms/pipeline.d.ts.map +0 -1
- package/build-types/image-editor/core/types.d.ts +0 -269
- package/build-types/image-editor/core/types.d.ts.map +0 -1
- package/build-types/image-editor/core/viewport-state.d.ts +0 -4
- package/build-types/image-editor/core/viewport-state.d.ts.map +0 -1
- package/build-types/image-editor/index.d.ts +0 -16
- package/build-types/image-editor/index.d.ts.map +0 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts +0 -38
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +0 -1
- package/build-types/image-editor/react/components/cropper.d.ts +0 -63
- package/build-types/image-editor/react/components/cropper.d.ts.map +0 -1
- package/build-types/image-editor/react/components/index.d.ts +0 -4
- package/build-types/image-editor/react/components/index.d.ts.map +0 -1
- package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +0 -33
- package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +0 -1
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +0 -17
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +0 -1
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +0 -27
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +0 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +0 -33
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +0 -1
- package/build-types/image-editor/react/components/viewport-provider.d.ts +0 -6
- package/build-types/image-editor/react/components/viewport-provider.d.ts.map +0 -1
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +0 -44
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +0 -1
- package/build-types/image-editor/react/hooks/index.d.ts +0 -5
- package/build-types/image-editor/react/hooks/index.d.ts.map +0 -1
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +0 -14
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +0 -1
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts +0 -55
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +0 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts +0 -65
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +0 -1
- package/build-types/image-editor/react/hooks/use-transform-style.d.ts +0 -13
- package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +0 -1
- package/build-types/image-editor/react/hooks/use-viewport-state.d.ts +0 -16
- package/build-types/image-editor/react/hooks/use-viewport-state.d.ts.map +0 -1
- package/build-types/image-editor/react/visually-hidden-style.d.ts +0 -6
- package/build-types/image-editor/react/visually-hidden-style.d.ts.map +0 -1
- package/src/components/media-editor-modal/build-modifiers.ts +0 -161
- package/src/image-editor/README.md +0 -61
- package/src/image-editor/core/camera.ts +0 -429
- package/src/image-editor/core/constants.ts +0 -150
- package/src/image-editor/core/containment.ts +0 -469
- package/src/image-editor/core/crop-rect.ts +0 -42
- package/src/image-editor/core/export/canvas-renderer.ts +0 -250
- package/src/image-editor/core/index.ts +0 -47
- package/src/image-editor/core/interaction-controller.ts +0 -976
- package/src/image-editor/core/math/rotation.ts +0 -26
- package/src/image-editor/core/math/sanitize.ts +0 -155
- package/src/image-editor/core/setter-helpers.ts +0 -63
- package/src/image-editor/core/source-region.ts +0 -367
- package/src/image-editor/core/state.ts +0 -484
- package/src/image-editor/core/stencil-math.ts +0 -379
- package/src/image-editor/core/transform-style.ts +0 -56
- package/src/image-editor/core/transforms/pipeline.ts +0 -40
- package/src/image-editor/core/types.ts +0 -230
- package/src/image-editor/core/viewport-state.ts +0 -30
- package/src/image-editor/docs/architecture.md +0 -63
- package/src/image-editor/docs/recipes.md +0 -184
- package/src/image-editor/index.ts +0 -52
- package/src/image-editor/react/components/cropper-provider.tsx +0 -70
- package/src/image-editor/react/components/cropper.scss +0 -259
- package/src/image-editor/react/components/cropper.tsx +0 -1116
- package/src/image-editor/react/components/index.ts +0 -3
- package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +0 -262
- package/src/image-editor/react/components/overlays/dimming-overlay.tsx +0 -55
- package/src/image-editor/react/components/overlays/grid-overlay.tsx +0 -77
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +0 -623
- package/src/image-editor/react/components/viewport-provider.tsx +0 -32
- package/src/image-editor/react/hooks/build-cropper-setters.ts +0 -102
- package/src/image-editor/react/hooks/index.ts +0 -4
- package/src/image-editor/react/hooks/use-aria-announcer.ts +0 -193
- package/src/image-editor/react/hooks/use-cropper-reducer.ts +0 -197
- package/src/image-editor/react/hooks/use-interaction.ts +0 -294
- package/src/image-editor/react/hooks/use-transform-style.ts +0 -23
- package/src/image-editor/react/hooks/use-viewport-state.ts +0 -41
- package/src/image-editor/react/visually-hidden-style.ts +0 -15
- package/src/image-editor/style.scss +0 -1
|
@@ -1,1116 +0,0 @@
|
|
|
1
|
-
import clsx from 'clsx';
|
|
2
|
-
import {
|
|
3
|
-
useState,
|
|
4
|
-
useCallback,
|
|
5
|
-
useMemo,
|
|
6
|
-
useRef,
|
|
7
|
-
useEffect,
|
|
8
|
-
useLayoutEffect,
|
|
9
|
-
useId,
|
|
10
|
-
forwardRef,
|
|
11
|
-
} from '@wordpress/element';
|
|
12
|
-
import { __ } from '@wordpress/i18n';
|
|
13
|
-
import type {
|
|
14
|
-
CropperState,
|
|
15
|
-
HandlePosition,
|
|
16
|
-
StencilProps,
|
|
17
|
-
Size,
|
|
18
|
-
NormalizedRect,
|
|
19
|
-
} from '../../core/types';
|
|
20
|
-
import type { CropperController } from '../hooks/use-cropper-reducer';
|
|
21
|
-
import { getImageFit, getRotatedBBox, getViewScale } from '../../core/camera';
|
|
22
|
-
import { getImageCropBounds, getMinZoom } from '../../core/containment';
|
|
23
|
-
import {
|
|
24
|
-
MAX_VIEW_SCALE,
|
|
25
|
-
PIXEL_SNAP_DISPLAY_SCALE,
|
|
26
|
-
SETTLE_TARGET_CANVAS_FILL,
|
|
27
|
-
} from '../../core/constants';
|
|
28
|
-
import { computeInscribedRect } from '../../core/crop-rect';
|
|
29
|
-
import { getMinCropPixels } from '../../core/stencil-math';
|
|
30
|
-
import { useInteraction } from '../hooks/use-interaction';
|
|
31
|
-
import { useTransformStyle } from '../hooks/use-transform-style';
|
|
32
|
-
import { useAriaAnnouncer } from '../hooks/use-aria-announcer';
|
|
33
|
-
import { RectangleStencil } from './stencils/rectangle-stencil';
|
|
34
|
-
import { DimmingOverlay } from './overlays/dimming-overlay';
|
|
35
|
-
import { GridOverlay } from './overlays/grid-overlay';
|
|
36
|
-
import { DimensionsOverlay } from './overlays/dimensions-overlay';
|
|
37
|
-
import {
|
|
38
|
-
getSourceRegion,
|
|
39
|
-
snapCropRectToSourcePixelGrid,
|
|
40
|
-
snapCropRectToSourcePixels,
|
|
41
|
-
} from '../../core/source-region';
|
|
42
|
-
import { ViewportProvider, useViewport } from './viewport-provider';
|
|
43
|
-
import { VISUALLY_HIDDEN_STYLE } from '../visually-hidden-style';
|
|
44
|
-
|
|
45
|
-
/** Threshold for comparing normalized crop rect values. */
|
|
46
|
-
const CROP_RECT_EPSILON = 1e-6;
|
|
47
|
-
/** Threshold for deciding whether rotation is at a 90-degree stop. */
|
|
48
|
-
const CARDINAL_ROTATION_EPSILON = 1e-6;
|
|
49
|
-
/** Duration of the settle transform transition after resize end. */
|
|
50
|
-
const SETTLE_TRANSITION_DURATION_MS = 200;
|
|
51
|
-
// Fallback timer for when the computed transform is unchanged and no
|
|
52
|
-
// `transitionend` fires. Sits past the CSS duration so it only wins the race
|
|
53
|
-
// when the transition genuinely never runs.
|
|
54
|
-
const SETTLE_TRANSITION_FALLBACK_MS = SETTLE_TRANSITION_DURATION_MS + 100;
|
|
55
|
-
const SETTLE_TRANSFORM_TRANSITION = `transform ${ SETTLE_TRANSITION_DURATION_MS }ms ease-out`;
|
|
56
|
-
const SETTLE_STENCIL_TRANSITION = [
|
|
57
|
-
`left ${ SETTLE_TRANSITION_DURATION_MS }ms ease-out`,
|
|
58
|
-
`top ${ SETTLE_TRANSITION_DURATION_MS }ms ease-out`,
|
|
59
|
-
`width ${ SETTLE_TRANSITION_DURATION_MS }ms ease-out`,
|
|
60
|
-
`height ${ SETTLE_TRANSITION_DURATION_MS }ms ease-out`,
|
|
61
|
-
].join( ', ' );
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Props for the Cropper component.
|
|
65
|
-
*/
|
|
66
|
-
export interface CropperProps {
|
|
67
|
-
/** Image source URL. */
|
|
68
|
-
src: string;
|
|
69
|
-
/** The cropper controller from `useCropperReducer` or a composite store. */
|
|
70
|
-
controller: CropperController;
|
|
71
|
-
/** Stencil component for the crop area. Defaults to RectangleStencil. */
|
|
72
|
-
stencil?: React.ComponentType< StencilProps >;
|
|
73
|
-
/** Show the rule-of-thirds grid overlay, or only during interactions. */
|
|
74
|
-
showGrid?: boolean | 'interactive';
|
|
75
|
-
/** Whether external placement activity should keep the grid visible. */
|
|
76
|
-
isPlacementActive?: boolean;
|
|
77
|
-
/** Show the dimming overlay outside the crop area. */
|
|
78
|
-
showDimming?: boolean;
|
|
79
|
-
/** Show the live output dimensions tooltip during a resize. */
|
|
80
|
-
showDimensions?: boolean;
|
|
81
|
-
/** Minimum zoom level override. Defaults to the coverage-aware minimum. */
|
|
82
|
-
minZoom?: number;
|
|
83
|
-
/** Maximum zoom level. */
|
|
84
|
-
maxZoom?: number;
|
|
85
|
-
/** Fixed aspect ratio (width / height) in pixel space for the crop area. */
|
|
86
|
-
aspectRatio?: number;
|
|
87
|
-
/**
|
|
88
|
-
* Enable freeform crop mode with resizable handles.
|
|
89
|
-
* When false (default), the crop area is fixed and centered.
|
|
90
|
-
* When true, the crop area has resize handles and can be freely repositioned.
|
|
91
|
-
*/
|
|
92
|
-
freeformCrop?: boolean;
|
|
93
|
-
/** Focus the crop area when the cropper mounts. */
|
|
94
|
-
focusOnMount?: boolean;
|
|
95
|
-
/**
|
|
96
|
-
* Ignore all crop interaction: pointer drags, wheel zoom, keyboard
|
|
97
|
-
* pan/zoom and the stencil's resize handles. Any gesture already in
|
|
98
|
-
* flight is cancelled. Used while the edit is being saved, so the
|
|
99
|
-
* modifiers sent to the server cannot change after the request starts.
|
|
100
|
-
*/
|
|
101
|
-
disabled?: boolean;
|
|
102
|
-
/** Callback fired when the image is loaded. */
|
|
103
|
-
onImageLoaded?: ( size: Size ) => void;
|
|
104
|
-
/**
|
|
105
|
-
* Callback fired on every state change. Fires at pointermove rate
|
|
106
|
-
* during drags, so keep the handler light — no heavy work, no
|
|
107
|
-
* expensive parent re-renders. For commit-style events (drag end,
|
|
108
|
-
* settled crop), use `onGestureEnd` instead.
|
|
109
|
-
*
|
|
110
|
-
* Useful for lightweight syncing with external tools, analytics,
|
|
111
|
-
* or AI agents. Receives the full state so consumers can derive
|
|
112
|
-
* whatever they need.
|
|
113
|
-
*/
|
|
114
|
-
onStateChange?: ( state: CropperState ) => void;
|
|
115
|
-
/** Fires when a continuous gesture begins (pan drag, handle resize, pinch zoom). */
|
|
116
|
-
onGestureStart?: () => void;
|
|
117
|
-
/** Fires when a continuous gesture ends (pointerup, resize settle). */
|
|
118
|
-
onGestureEnd?: () => void;
|
|
119
|
-
/** Additional className for the container. */
|
|
120
|
-
className?: string;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* The main image cropper component.
|
|
125
|
-
*
|
|
126
|
-
* Renders an image within a container with interactive crop overlays.
|
|
127
|
-
* Creates the camera once per render via getImageFit, then passes
|
|
128
|
-
* derived values to stencil, overlays, and interaction hooks.
|
|
129
|
-
*
|
|
130
|
-
* The component fills its parent container (100% width and height).
|
|
131
|
-
* Wrap it in a sized container to control its dimensions.
|
|
132
|
-
*
|
|
133
|
-
* @param root0 Component props implementing CropperProps.
|
|
134
|
-
* @param root0.src Image source URL.
|
|
135
|
-
* @param root0.controller The cropper controller from `useCropperReducer` or a composite store.
|
|
136
|
-
* @param root0.stencil Custom stencil component.
|
|
137
|
-
* @param root0.showGrid Grid overlay mode: false | true | 'interactive'.
|
|
138
|
-
* @param root0.isPlacementActive Keep grid visible during external placement activity.
|
|
139
|
-
* @param root0.showDimming Show dimming overlay outside crop.
|
|
140
|
-
* @param root0.showDimensions Show live dimensions tooltip during resize.
|
|
141
|
-
* @param root0.minZoom Minimum zoom level override.
|
|
142
|
-
* @param root0.maxZoom Maximum zoom level.
|
|
143
|
-
* @param root0.aspectRatio Fixed aspect ratio (width/height).
|
|
144
|
-
* @param root0.freeformCrop Enable resize handles.
|
|
145
|
-
* @param root0.focusOnMount Focus the crop area on mount.
|
|
146
|
-
* @param root0.disabled Ignore all crop interaction.
|
|
147
|
-
* @param root0.onImageLoaded Image load callback.
|
|
148
|
-
* @param root0.onStateChange Every-frame state callback.
|
|
149
|
-
* @param root0.onGestureStart Gesture boundary start.
|
|
150
|
-
* @param root0.onGestureEnd Gesture boundary end.
|
|
151
|
-
* @param root0.className Additional CSS class.
|
|
152
|
-
* @param ref Forwarded ref for the container div.
|
|
153
|
-
*/
|
|
154
|
-
function CropperInner(
|
|
155
|
-
{
|
|
156
|
-
src,
|
|
157
|
-
controller,
|
|
158
|
-
stencil: StencilComponent = RectangleStencil,
|
|
159
|
-
showGrid = false,
|
|
160
|
-
isPlacementActive = false,
|
|
161
|
-
showDimming = true,
|
|
162
|
-
showDimensions = true,
|
|
163
|
-
minZoom,
|
|
164
|
-
maxZoom,
|
|
165
|
-
aspectRatio,
|
|
166
|
-
freeformCrop = false,
|
|
167
|
-
focusOnMount = false,
|
|
168
|
-
disabled = false,
|
|
169
|
-
onImageLoaded,
|
|
170
|
-
onStateChange,
|
|
171
|
-
onGestureStart,
|
|
172
|
-
onGestureEnd,
|
|
173
|
-
className,
|
|
174
|
-
}: CropperProps,
|
|
175
|
-
ref: React.ForwardedRef< HTMLDivElement >
|
|
176
|
-
) {
|
|
177
|
-
const {
|
|
178
|
-
state,
|
|
179
|
-
setImage,
|
|
180
|
-
setCropRect,
|
|
181
|
-
settleCrop,
|
|
182
|
-
setVisualSize,
|
|
183
|
-
adjustCropRectForViewport,
|
|
184
|
-
} = controller;
|
|
185
|
-
const {
|
|
186
|
-
viewport: viewportState,
|
|
187
|
-
setViewportPan,
|
|
188
|
-
resetViewport,
|
|
189
|
-
} = useViewport();
|
|
190
|
-
// Canvas measurement via ResizeObserver. The canvas is the inner
|
|
191
|
-
// positioning context for image/stencil/handles — inset from the root
|
|
192
|
-
// by the handle gutter, so crop math operates on the reduced box.
|
|
193
|
-
const canvasRef = useRef< HTMLDivElement >( null );
|
|
194
|
-
const cropAreaDescriptionId = useId();
|
|
195
|
-
const [ isCropAreaFocused, setIsCropAreaFocused ] =
|
|
196
|
-
useState( focusOnMount );
|
|
197
|
-
const [ isFocusVisible, setIsFocusVisible ] = useState( false );
|
|
198
|
-
const [ canvasSize, setCanvasSize ] = useState< Size >( {
|
|
199
|
-
width: 0,
|
|
200
|
-
height: 0,
|
|
201
|
-
} );
|
|
202
|
-
|
|
203
|
-
useLayoutEffect( () => {
|
|
204
|
-
if ( focusOnMount ) {
|
|
205
|
-
canvasRef.current?.focus( { preventScroll: true } );
|
|
206
|
-
}
|
|
207
|
-
}, [ focusOnMount ] );
|
|
208
|
-
|
|
209
|
-
const handleCropAreaFocus = useCallback(
|
|
210
|
-
( event: React.FocusEvent< HTMLDivElement > ) => {
|
|
211
|
-
const target = event.target as HTMLElement;
|
|
212
|
-
if ( target === event.currentTarget ) {
|
|
213
|
-
setIsCropAreaFocused( true );
|
|
214
|
-
}
|
|
215
|
-
// Show the outline only when focus arrived via keyboard
|
|
216
|
-
// navigation. relatedTarget is null for pointer-initiated and
|
|
217
|
-
// cross-window focus, so it reliably excludes those cases
|
|
218
|
-
// without needing a separate ref or flag. Applies to focus
|
|
219
|
-
// arriving on the canvas itself or on a descendant handle.
|
|
220
|
-
if (
|
|
221
|
-
event.relatedTarget !== null &&
|
|
222
|
-
target.matches( ':focus-visible' )
|
|
223
|
-
) {
|
|
224
|
-
setIsFocusVisible( true );
|
|
225
|
-
}
|
|
226
|
-
},
|
|
227
|
-
[]
|
|
228
|
-
);
|
|
229
|
-
|
|
230
|
-
const handleCropAreaBlur = useCallback(
|
|
231
|
-
( event: React.FocusEvent< HTMLDivElement > ) => {
|
|
232
|
-
if ( event.target === event.currentTarget ) {
|
|
233
|
-
setIsCropAreaFocused( false );
|
|
234
|
-
}
|
|
235
|
-
// Reset keyboard-active styling only when focus leaves the
|
|
236
|
-
// cropper entirely. Moves between the canvas and a handle (or
|
|
237
|
-
// between handles) keep the keyboard-active state intact.
|
|
238
|
-
if (
|
|
239
|
-
! event.currentTarget.contains(
|
|
240
|
-
event.relatedTarget as Node | null
|
|
241
|
-
)
|
|
242
|
-
) {
|
|
243
|
-
setIsFocusVisible( false );
|
|
244
|
-
}
|
|
245
|
-
},
|
|
246
|
-
[]
|
|
247
|
-
);
|
|
248
|
-
|
|
249
|
-
useEffect( () => {
|
|
250
|
-
const element = canvasRef.current;
|
|
251
|
-
if ( ! element ) {
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
const observer = new ResizeObserver( ( entries ) => {
|
|
255
|
-
for ( const entry of entries ) {
|
|
256
|
-
const { width, height } = entry.contentRect;
|
|
257
|
-
setCanvasSize( ( prev ) => {
|
|
258
|
-
if ( prev.width === width && prev.height === height ) {
|
|
259
|
-
return prev;
|
|
260
|
-
}
|
|
261
|
-
return { width, height };
|
|
262
|
-
} );
|
|
263
|
-
}
|
|
264
|
-
} );
|
|
265
|
-
observer.observe( element );
|
|
266
|
-
return () => {
|
|
267
|
-
observer.disconnect();
|
|
268
|
-
};
|
|
269
|
-
}, [] );
|
|
270
|
-
|
|
271
|
-
// Notify consumer of state changes.
|
|
272
|
-
useEffect( () => {
|
|
273
|
-
onStateChange?.( state );
|
|
274
|
-
}, [ state, onStateChange ] );
|
|
275
|
-
|
|
276
|
-
// ARIA announcements: announce significant state changes for screen readers
|
|
277
|
-
// via the centralized @wordpress/a11y speak() API called inside the hook.
|
|
278
|
-
useAriaAnnouncer( state );
|
|
279
|
-
|
|
280
|
-
// Compute fitted image dimensions and visual bounds from camera math.
|
|
281
|
-
const naturalWidth = state.image?.naturalWidth ?? 0;
|
|
282
|
-
const naturalHeight = state.image?.naturalHeight ?? 0;
|
|
283
|
-
const { elementSize, visualSize } = useMemo(
|
|
284
|
-
() =>
|
|
285
|
-
getImageFit(
|
|
286
|
-
canvasSize,
|
|
287
|
-
{ width: naturalWidth, height: naturalHeight },
|
|
288
|
-
state.rotation
|
|
289
|
-
),
|
|
290
|
-
[ canvasSize, naturalWidth, naturalHeight, state.rotation ]
|
|
291
|
-
);
|
|
292
|
-
|
|
293
|
-
// Report the rendered image size to the controller. Composite
|
|
294
|
-
// controllers need it to compute aspect-ratio reshapes from the
|
|
295
|
-
// reducer (the dropdown dispatches without DOM access); pure
|
|
296
|
-
// controllers ignore it.
|
|
297
|
-
useEffect( () => {
|
|
298
|
-
setVisualSize( visualSize );
|
|
299
|
-
}, [ visualSize, setVisualSize ] );
|
|
300
|
-
|
|
301
|
-
// In fixed-crop mode, auto-size the crop rect only when a fixed
|
|
302
|
-
// aspect ratio is selected. With "Free" selected, turning freeform
|
|
303
|
-
// handles off should preserve the user's current unconstrained
|
|
304
|
-
// crop. Uses `adjustCropRectForViewport` so window-resize-driven
|
|
305
|
-
// reshapes don't create undo entries (composite controller); the
|
|
306
|
-
// dedup at lines below avoids redundant dispatches when the
|
|
307
|
-
// reducer has already updated the cropRect for an aspect change.
|
|
308
|
-
useEffect( () => {
|
|
309
|
-
if (
|
|
310
|
-
freeformCrop ||
|
|
311
|
-
visualSize.width === 0 ||
|
|
312
|
-
visualSize.height === 0 ||
|
|
313
|
-
! aspectRatio ||
|
|
314
|
-
aspectRatio <= 0
|
|
315
|
-
) {
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
const rect = computeInscribedRect( aspectRatio, visualSize );
|
|
319
|
-
const current = state.cropRect;
|
|
320
|
-
if (
|
|
321
|
-
Math.abs( current.x - rect.x ) < CROP_RECT_EPSILON &&
|
|
322
|
-
Math.abs( current.y - rect.y ) < CROP_RECT_EPSILON &&
|
|
323
|
-
Math.abs( current.width - rect.width ) < CROP_RECT_EPSILON &&
|
|
324
|
-
Math.abs( current.height - rect.height ) < CROP_RECT_EPSILON
|
|
325
|
-
) {
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
328
|
-
adjustCropRectForViewport( rect );
|
|
329
|
-
}, [
|
|
330
|
-
freeformCrop,
|
|
331
|
-
aspectRatio,
|
|
332
|
-
visualSize,
|
|
333
|
-
adjustCropRectForViewport,
|
|
334
|
-
state.cropRect,
|
|
335
|
-
] );
|
|
336
|
-
|
|
337
|
-
// In freeform mode, when aspectRatio changes, reshape the crop to
|
|
338
|
-
// the largest inscribed rect of the new ratio. Bails out when the
|
|
339
|
-
// cropRect already matches the inscribed rect — composite stores
|
|
340
|
-
// reshape atomically inside the reducer, so this effect is a
|
|
341
|
-
// no-op there.
|
|
342
|
-
const prevAspectRatioRef = useRef( aspectRatio );
|
|
343
|
-
useEffect( () => {
|
|
344
|
-
if ( prevAspectRatioRef.current === aspectRatio ) {
|
|
345
|
-
return;
|
|
346
|
-
}
|
|
347
|
-
prevAspectRatioRef.current = aspectRatio;
|
|
348
|
-
|
|
349
|
-
if (
|
|
350
|
-
! freeformCrop ||
|
|
351
|
-
visualSize.width === 0 ||
|
|
352
|
-
visualSize.height === 0 ||
|
|
353
|
-
! aspectRatio ||
|
|
354
|
-
aspectRatio <= 0
|
|
355
|
-
) {
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
358
|
-
const rect = computeInscribedRect( aspectRatio, visualSize );
|
|
359
|
-
const current = state.cropRect;
|
|
360
|
-
if (
|
|
361
|
-
Math.abs( current.x - rect.x ) < CROP_RECT_EPSILON &&
|
|
362
|
-
Math.abs( current.y - rect.y ) < CROP_RECT_EPSILON &&
|
|
363
|
-
Math.abs( current.width - rect.width ) < CROP_RECT_EPSILON &&
|
|
364
|
-
Math.abs( current.height - rect.height ) < CROP_RECT_EPSILON
|
|
365
|
-
) {
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
adjustCropRectForViewport( rect );
|
|
369
|
-
}, [
|
|
370
|
-
aspectRatio,
|
|
371
|
-
freeformCrop,
|
|
372
|
-
visualSize,
|
|
373
|
-
adjustCropRectForViewport,
|
|
374
|
-
state.cropRect,
|
|
375
|
-
] );
|
|
376
|
-
|
|
377
|
-
// Compute the crop handle bounds from the actual image footprint.
|
|
378
|
-
// Depends on the full state object because getImageCropBounds reads
|
|
379
|
-
// crop, zoom, rotation, flip, and image. React Compiler requires
|
|
380
|
-
// the complete dependency; the computation is lightweight (a few
|
|
381
|
-
// trig ops + 4 corner transforms).
|
|
382
|
-
const cropBounds = useMemo( () => {
|
|
383
|
-
if ( ! state.image || elementSize.width === 0 ) {
|
|
384
|
-
return undefined;
|
|
385
|
-
}
|
|
386
|
-
return getImageCropBounds( state, elementSize, visualSize );
|
|
387
|
-
}, [ state, elementSize, visualSize ] );
|
|
388
|
-
const effectiveMinZoom =
|
|
389
|
-
minZoom !== undefined ? minZoom : getMinZoom( state );
|
|
390
|
-
const [ isResizing, setIsResizing ] = useState( false );
|
|
391
|
-
const isResizingRef = useRef( false );
|
|
392
|
-
const isSettlingRef = useRef( false );
|
|
393
|
-
const [ isTouchPinching, setIsTouchPinchingState ] = useState( false );
|
|
394
|
-
const isTouchPinchingRef = useRef( false );
|
|
395
|
-
const setTouchPinching = useCallback( ( isPinching: boolean ) => {
|
|
396
|
-
isTouchPinchingRef.current = isPinching;
|
|
397
|
-
setIsTouchPinchingState( isPinching );
|
|
398
|
-
}, [] );
|
|
399
|
-
// Direction of the handle the user is currently resizing — pointer or
|
|
400
|
-
// keyboard. `null` outside of an active resize. Drives the live
|
|
401
|
-
// dimensions tooltip overlay.
|
|
402
|
-
const [ activeHandle, setActiveHandle ] = useState< HandlePosition | null >(
|
|
403
|
-
null
|
|
404
|
-
);
|
|
405
|
-
|
|
406
|
-
// Presentational magnification so the at-rest crop fills the canvas. The
|
|
407
|
-
// crop overlay is laid out as `cropRect * visualSize` (the contain-fit
|
|
408
|
-
// footprint, no zoom), so a crop whose aspect differs from the image's can
|
|
409
|
-
// render far smaller than the canvas allows. Scaling `elementSize` and
|
|
410
|
-
// `visualSize` together magnifies the whole scene uniformly — image,
|
|
411
|
-
// overlays, and the pixel->normalized interaction math — and the root's
|
|
412
|
-
// `overflow: hidden` clips the image bleed. Magnifying around the footprint
|
|
413
|
-
// centre keeps the crop centred only because the at-rest crop is always
|
|
414
|
-
// centred (SETTLE_CROP, computeInscribedRect, and the initial full-frame
|
|
415
|
-
// crop all centre it).
|
|
416
|
-
const viewScaleRest = useMemo(
|
|
417
|
-
() =>
|
|
418
|
-
getViewScale(
|
|
419
|
-
state.cropRect,
|
|
420
|
-
canvasSize,
|
|
421
|
-
visualSize,
|
|
422
|
-
SETTLE_TARGET_CANVAS_FILL,
|
|
423
|
-
MAX_VIEW_SCALE
|
|
424
|
-
),
|
|
425
|
-
[ state.cropRect, canvasSize, visualSize ]
|
|
426
|
-
);
|
|
427
|
-
// During a resize the magnification is frozen at its pre-drag value rather
|
|
428
|
-
// than recomputed per pointer move: recomputing live would zoom the scene
|
|
429
|
-
// under the cursor, and snapping to 1 would reset the zoom the moment a
|
|
430
|
-
// handle is grabbed. `handleResizeStart` snapshots the current rest value
|
|
431
|
-
// into `frozenViewScale`; on release the settle recomputes from the new crop.
|
|
432
|
-
const [ frozenViewScale, setFrozenViewScale ] = useState( 1 );
|
|
433
|
-
const viewScale = isResizing ? frozenViewScale : viewScaleRest;
|
|
434
|
-
const scaledVisualSize = useMemo(
|
|
435
|
-
() => ( {
|
|
436
|
-
width: visualSize.width * viewScale,
|
|
437
|
-
height: visualSize.height * viewScale,
|
|
438
|
-
} ),
|
|
439
|
-
[ visualSize.width, visualSize.height, viewScale ]
|
|
440
|
-
);
|
|
441
|
-
// CSS pixels rendered per source pixel: the contain fit
|
|
442
|
-
// (elementSize / natural), times zoom and the view-scale magnification.
|
|
443
|
-
const displayScale =
|
|
444
|
-
naturalWidth > 0
|
|
445
|
-
? ( elementSize.width / naturalWidth ) * state.zoom * viewScale
|
|
446
|
-
: 0;
|
|
447
|
-
const keyboardResizeStep = useMemo( () => {
|
|
448
|
-
if (
|
|
449
|
-
displayScale < PIXEL_SNAP_DISPLAY_SCALE ||
|
|
450
|
-
( aspectRatio && aspectRatio > 0 ) ||
|
|
451
|
-
naturalWidth <= 0 ||
|
|
452
|
-
naturalHeight <= 0
|
|
453
|
-
) {
|
|
454
|
-
return undefined;
|
|
455
|
-
}
|
|
456
|
-
const snapRotation = Math.round( state.rotation / 90 ) * 90;
|
|
457
|
-
if (
|
|
458
|
-
Math.abs( state.rotation - snapRotation ) >=
|
|
459
|
-
CARDINAL_ROTATION_EPSILON
|
|
460
|
-
) {
|
|
461
|
-
return undefined;
|
|
462
|
-
}
|
|
463
|
-
const bbox = getRotatedBBox(
|
|
464
|
-
naturalWidth,
|
|
465
|
-
naturalHeight,
|
|
466
|
-
snapRotation
|
|
467
|
-
);
|
|
468
|
-
if ( bbox.width <= 0 || bbox.height <= 0 ) {
|
|
469
|
-
return undefined;
|
|
470
|
-
}
|
|
471
|
-
// Keyboard steps are normalized crop-space deltas, but snapping wants
|
|
472
|
-
// one source pixel per key press. The normalized delta for one source
|
|
473
|
-
// pixel is different on each axis when the snap-rotation bbox is not
|
|
474
|
-
// square, so this must be an object with separate width/height steps
|
|
475
|
-
// instead of a single scalar.
|
|
476
|
-
return {
|
|
477
|
-
width: state.zoom / bbox.width,
|
|
478
|
-
height: state.zoom / bbox.height,
|
|
479
|
-
};
|
|
480
|
-
}, [
|
|
481
|
-
displayScale,
|
|
482
|
-
aspectRatio,
|
|
483
|
-
naturalWidth,
|
|
484
|
-
naturalHeight,
|
|
485
|
-
state.rotation,
|
|
486
|
-
state.zoom,
|
|
487
|
-
] );
|
|
488
|
-
|
|
489
|
-
// Per-axis minimum crop size in normalized space, expressing a pixel floor
|
|
490
|
-
// on the captured source region. cropRect is normalized in the viewport's
|
|
491
|
-
// snap-rotation bbox; the captured source-pixel width is
|
|
492
|
-
// `cropRect.width * bbox.width / zoom`, so the normalized floor scales with
|
|
493
|
-
// `zoom` to keep the source-pixel floor constant. The floor itself adapts to
|
|
494
|
-
// the on-screen display scale (fit × zoom × view scale) via
|
|
495
|
-
// `getMinCropPixels`, so on a large image shown small the crop can't shrink
|
|
496
|
-
// until the handles collapse — it yields to the 24px source floor only once
|
|
497
|
-
// the image is shown large enough.
|
|
498
|
-
const minCropSize: Size | undefined = useMemo( () => {
|
|
499
|
-
if ( naturalWidth <= 0 || naturalHeight <= 0 ) {
|
|
500
|
-
return undefined;
|
|
501
|
-
}
|
|
502
|
-
const snapRotation = Math.round( state.rotation / 90 ) * 90;
|
|
503
|
-
const bbox = getRotatedBBox(
|
|
504
|
-
naturalWidth,
|
|
505
|
-
naturalHeight,
|
|
506
|
-
snapRotation
|
|
507
|
-
);
|
|
508
|
-
const minPixels = getMinCropPixels( displayScale );
|
|
509
|
-
return {
|
|
510
|
-
width: Math.min( 1, ( minPixels * state.zoom ) / bbox.width ),
|
|
511
|
-
height: Math.min( 1, ( minPixels * state.zoom ) / bbox.height ),
|
|
512
|
-
};
|
|
513
|
-
}, [
|
|
514
|
-
naturalWidth,
|
|
515
|
-
naturalHeight,
|
|
516
|
-
state.rotation,
|
|
517
|
-
state.zoom,
|
|
518
|
-
displayScale,
|
|
519
|
-
] );
|
|
520
|
-
|
|
521
|
-
const snapCropRect = useCallback(
|
|
522
|
-
( rect: NormalizedRect, handle: HandlePosition ): NormalizedRect => {
|
|
523
|
-
if (
|
|
524
|
-
displayScale < PIXEL_SNAP_DISPLAY_SCALE ||
|
|
525
|
-
naturalWidth <= 0 ||
|
|
526
|
-
naturalHeight <= 0
|
|
527
|
-
) {
|
|
528
|
-
return rect;
|
|
529
|
-
}
|
|
530
|
-
return snapCropRectToSourcePixels(
|
|
531
|
-
state,
|
|
532
|
-
{ width: naturalWidth, height: naturalHeight },
|
|
533
|
-
rect,
|
|
534
|
-
handle
|
|
535
|
-
);
|
|
536
|
-
},
|
|
537
|
-
[ displayScale, naturalWidth, naturalHeight, state ]
|
|
538
|
-
);
|
|
539
|
-
|
|
540
|
-
const wasPixelSnapEnabledRef = useRef( false );
|
|
541
|
-
useEffect( () => {
|
|
542
|
-
const isPixelSnapEnabled =
|
|
543
|
-
freeformCrop &&
|
|
544
|
-
( ! aspectRatio || aspectRatio <= 0 ) &&
|
|
545
|
-
displayScale >= PIXEL_SNAP_DISPLAY_SCALE &&
|
|
546
|
-
naturalWidth > 0 &&
|
|
547
|
-
naturalHeight > 0;
|
|
548
|
-
const wasPixelSnapEnabled = wasPixelSnapEnabledRef.current;
|
|
549
|
-
wasPixelSnapEnabledRef.current = isPixelSnapEnabled;
|
|
550
|
-
|
|
551
|
-
if ( ! isPixelSnapEnabled || wasPixelSnapEnabled ) {
|
|
552
|
-
return;
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
const snappedCropRect = snapCropRectToSourcePixelGrid(
|
|
556
|
-
state,
|
|
557
|
-
{ width: naturalWidth, height: naturalHeight },
|
|
558
|
-
state.cropRect
|
|
559
|
-
);
|
|
560
|
-
const currentCropRect = state.cropRect;
|
|
561
|
-
if (
|
|
562
|
-
Math.abs( currentCropRect.x - snappedCropRect.x ) <
|
|
563
|
-
CROP_RECT_EPSILON &&
|
|
564
|
-
Math.abs( currentCropRect.y - snappedCropRect.y ) <
|
|
565
|
-
CROP_RECT_EPSILON &&
|
|
566
|
-
Math.abs( currentCropRect.width - snappedCropRect.width ) <
|
|
567
|
-
CROP_RECT_EPSILON &&
|
|
568
|
-
Math.abs( currentCropRect.height - snappedCropRect.height ) <
|
|
569
|
-
CROP_RECT_EPSILON
|
|
570
|
-
) {
|
|
571
|
-
return;
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
// The display scale crossing the threshold is not an edit, so the
|
|
575
|
-
// snap must not record an undo entry. Recording it here would also
|
|
576
|
-
// clear the redo stack whenever an undo or redo crosses it.
|
|
577
|
-
adjustCropRectForViewport( snappedCropRect );
|
|
578
|
-
}, [
|
|
579
|
-
adjustCropRectForViewport,
|
|
580
|
-
aspectRatio,
|
|
581
|
-
displayScale,
|
|
582
|
-
freeformCrop,
|
|
583
|
-
naturalWidth,
|
|
584
|
-
naturalHeight,
|
|
585
|
-
state,
|
|
586
|
-
] );
|
|
587
|
-
|
|
588
|
-
// Use the interaction hook for mouse, touch, and keyboard events.
|
|
589
|
-
const {
|
|
590
|
-
handlers,
|
|
591
|
-
onWheelNative,
|
|
592
|
-
isDragging,
|
|
593
|
-
isZooming,
|
|
594
|
-
isPlacementActive: isInteractionPlacementActive,
|
|
595
|
-
} = useInteraction( state, controller, canvasSize, scaledVisualSize, {
|
|
596
|
-
minZoom: effectiveMinZoom,
|
|
597
|
-
maxZoom,
|
|
598
|
-
onGestureStart,
|
|
599
|
-
onGestureEnd,
|
|
600
|
-
disabled,
|
|
601
|
-
} );
|
|
602
|
-
|
|
603
|
-
// Compose focus-visibility tracking into the canvas event handlers.
|
|
604
|
-
// Kept as a spread rather than explicit props to avoid triggering
|
|
605
|
-
// jsx-a11y/no-noninteractive-element-interactions on role="group".
|
|
606
|
-
//
|
|
607
|
-
// The pointer/key tracking lives in the capture phase so it runs
|
|
608
|
-
// before any child handler — handles call stopPropagation in their
|
|
609
|
-
// own onPointerDown / onKeyDown, which would otherwise prevent the
|
|
610
|
-
// keyboard-active state from updating when the user interacts with
|
|
611
|
-
// a handle directly.
|
|
612
|
-
const canvasHandlers = {
|
|
613
|
-
...handlers,
|
|
614
|
-
onPointerDownCapture: () => {
|
|
615
|
-
setIsFocusVisible( false );
|
|
616
|
-
},
|
|
617
|
-
onTouchStartCapture: ( event: React.TouchEvent< HTMLDivElement > ) => {
|
|
618
|
-
if ( event.touches.length > 1 ) {
|
|
619
|
-
setTouchPinching( true );
|
|
620
|
-
}
|
|
621
|
-
},
|
|
622
|
-
onTouchMoveCapture: ( event: React.TouchEvent< HTMLDivElement > ) => {
|
|
623
|
-
if ( event.touches.length > 1 ) {
|
|
624
|
-
setTouchPinching( true );
|
|
625
|
-
}
|
|
626
|
-
},
|
|
627
|
-
onTouchEndCapture: ( event: React.TouchEvent< HTMLDivElement > ) => {
|
|
628
|
-
if ( event.touches.length === 0 ) {
|
|
629
|
-
setTouchPinching( false );
|
|
630
|
-
}
|
|
631
|
-
},
|
|
632
|
-
onTouchCancelCapture: ( event: React.TouchEvent< HTMLDivElement > ) => {
|
|
633
|
-
if ( event.touches.length === 0 ) {
|
|
634
|
-
setTouchPinching( false );
|
|
635
|
-
}
|
|
636
|
-
},
|
|
637
|
-
onKeyDownCapture: ( event: React.KeyboardEvent< HTMLDivElement > ) => {
|
|
638
|
-
// Modifier-only keypresses precede another key rather than
|
|
639
|
-
// indicating deliberate keyboard interaction on their own.
|
|
640
|
-
if (
|
|
641
|
-
! [ 'Shift', 'Control', 'Alt', 'Meta' ].includes( event.key )
|
|
642
|
-
) {
|
|
643
|
-
setIsFocusVisible( true );
|
|
644
|
-
}
|
|
645
|
-
},
|
|
646
|
-
onPointerDown: ( event: React.PointerEvent< HTMLDivElement > ) => {
|
|
647
|
-
if (
|
|
648
|
-
disabled ||
|
|
649
|
-
isResizingRef.current ||
|
|
650
|
-
isTouchPinchingRef.current ||
|
|
651
|
-
( event.pointerType === 'touch' && event.isPrimary === false )
|
|
652
|
-
) {
|
|
653
|
-
event.preventDefault();
|
|
654
|
-
return;
|
|
655
|
-
}
|
|
656
|
-
handlers.onPointerDown?.( event );
|
|
657
|
-
// Re-assert false after handlers run — el.focus() inside the
|
|
658
|
-
// handler fires onFocus, which may otherwise set it back to true.
|
|
659
|
-
setIsFocusVisible( false );
|
|
660
|
-
},
|
|
661
|
-
};
|
|
662
|
-
|
|
663
|
-
// Register wheel handler natively with { passive: false } so
|
|
664
|
-
// preventDefault works. React's onWheel registers as passive. Bound
|
|
665
|
-
// to the canvas (not the root) so pointer geometry inside the handler
|
|
666
|
-
// resolves against the canvas box.
|
|
667
|
-
useEffect( () => {
|
|
668
|
-
const el = canvasRef.current;
|
|
669
|
-
if ( ! el ) {
|
|
670
|
-
return;
|
|
671
|
-
}
|
|
672
|
-
const handleWheel = ( event: WheelEvent ) => {
|
|
673
|
-
// Block wheel zoom while resizing or settling — the stage CSS
|
|
674
|
-
// transform changes getBoundingClientRect() during this window,
|
|
675
|
-
// so focal-point math would resolve against the wrong center.
|
|
676
|
-
if ( isResizingRef.current || isSettlingRef.current ) {
|
|
677
|
-
event.preventDefault();
|
|
678
|
-
return;
|
|
679
|
-
}
|
|
680
|
-
onWheelNative( event );
|
|
681
|
-
};
|
|
682
|
-
el.addEventListener( 'wheel', handleWheel, {
|
|
683
|
-
passive: false,
|
|
684
|
-
} );
|
|
685
|
-
return () => {
|
|
686
|
-
el.removeEventListener( 'wheel', handleWheel );
|
|
687
|
-
};
|
|
688
|
-
}, [ onWheelNative ] );
|
|
689
|
-
|
|
690
|
-
// Use the unscaled contain-fit footprint here. `imageStyle` prepends
|
|
691
|
-
// `scale(viewScale)` to this matrix, so pan translations are magnified with
|
|
692
|
-
// the rest of the scene; using `scaledVisualSize` here would apply viewScale
|
|
693
|
-
// to pan twice.
|
|
694
|
-
const transformString = useTransformStyle( state, visualSize );
|
|
695
|
-
|
|
696
|
-
/**
|
|
697
|
-
* Handle the image load event.
|
|
698
|
-
*/
|
|
699
|
-
const handleImageLoad = useCallback(
|
|
700
|
-
( event: React.SyntheticEvent< HTMLImageElement > ) => {
|
|
701
|
-
const img = event.currentTarget;
|
|
702
|
-
const size: Size = {
|
|
703
|
-
width: img.naturalWidth,
|
|
704
|
-
height: img.naturalHeight,
|
|
705
|
-
};
|
|
706
|
-
|
|
707
|
-
setImage( {
|
|
708
|
-
src,
|
|
709
|
-
naturalWidth: size.width,
|
|
710
|
-
naturalHeight: size.height,
|
|
711
|
-
} );
|
|
712
|
-
|
|
713
|
-
onImageLoaded?.( size );
|
|
714
|
-
},
|
|
715
|
-
[ src, setImage, onImageLoaded ]
|
|
716
|
-
);
|
|
717
|
-
|
|
718
|
-
const handleCropChange = useCallback(
|
|
719
|
-
( rect: NormalizedRect ) => {
|
|
720
|
-
if ( isTouchPinchingRef.current ) {
|
|
721
|
-
return;
|
|
722
|
-
}
|
|
723
|
-
setCropRect( rect );
|
|
724
|
-
// During a resize drag, pan the viewport so the handle stays
|
|
725
|
-
// visible even when the crop extends beyond the canvas edge.
|
|
726
|
-
if (
|
|
727
|
-
isResizingRef.current &&
|
|
728
|
-
scaledVisualSize.width > 0 &&
|
|
729
|
-
scaledVisualSize.height > 0
|
|
730
|
-
) {
|
|
731
|
-
const offsetX =
|
|
732
|
-
( canvasSize.width - scaledVisualSize.width ) / 2;
|
|
733
|
-
const offsetY =
|
|
734
|
-
( canvasSize.height - scaledVisualSize.height ) / 2;
|
|
735
|
-
const rightOverflow = Math.max(
|
|
736
|
-
0,
|
|
737
|
-
offsetX +
|
|
738
|
-
( rect.x + rect.width ) * scaledVisualSize.width -
|
|
739
|
-
canvasSize.width
|
|
740
|
-
);
|
|
741
|
-
const leftOverflow = Math.max(
|
|
742
|
-
0,
|
|
743
|
-
-( offsetX + rect.x * scaledVisualSize.width )
|
|
744
|
-
);
|
|
745
|
-
const bottomOverflow = Math.max(
|
|
746
|
-
0,
|
|
747
|
-
offsetY +
|
|
748
|
-
( rect.y + rect.height ) * scaledVisualSize.height -
|
|
749
|
-
canvasSize.height
|
|
750
|
-
);
|
|
751
|
-
const topOverflow = Math.max(
|
|
752
|
-
0,
|
|
753
|
-
-( offsetY + rect.y * scaledVisualSize.height )
|
|
754
|
-
);
|
|
755
|
-
setViewportPan( {
|
|
756
|
-
x: -rightOverflow + leftOverflow,
|
|
757
|
-
y: -bottomOverflow + topOverflow,
|
|
758
|
-
} );
|
|
759
|
-
}
|
|
760
|
-
},
|
|
761
|
-
[ setCropRect, setViewportPan, canvasSize, scaledVisualSize ]
|
|
762
|
-
);
|
|
763
|
-
|
|
764
|
-
// Settling animation: brief transition after resize end.
|
|
765
|
-
const [ settling, setSettling ] = useState( false );
|
|
766
|
-
const settleTimerRef = useRef<
|
|
767
|
-
ReturnType< typeof setTimeout > | undefined
|
|
768
|
-
>( undefined );
|
|
769
|
-
const finishSettling = useCallback( () => {
|
|
770
|
-
clearTimeout( settleTimerRef.current );
|
|
771
|
-
isSettlingRef.current = false;
|
|
772
|
-
setSettling( false );
|
|
773
|
-
}, [] );
|
|
774
|
-
|
|
775
|
-
// Clear the pending settle timer on unmount so it can't fire a
|
|
776
|
-
// state update on an unmounted component.
|
|
777
|
-
useEffect( () => {
|
|
778
|
-
return () => {
|
|
779
|
-
clearTimeout( settleTimerRef.current );
|
|
780
|
-
};
|
|
781
|
-
}, [] );
|
|
782
|
-
|
|
783
|
-
const isInteractiveGrid = showGrid === 'interactive';
|
|
784
|
-
const showInteractiveGrid =
|
|
785
|
-
isInteractiveGrid &&
|
|
786
|
-
( isInteractionPlacementActive || isResizing || isPlacementActive );
|
|
787
|
-
|
|
788
|
-
// Output crop size in source pixels — drives the live tooltip
|
|
789
|
-
// overlay. Only computed during pointer drags, so the per-frame
|
|
790
|
-
// state churn during pan/zoom doesn't pay for it.
|
|
791
|
-
const outputSize = useMemo( () => {
|
|
792
|
-
if ( ! showDimensions || ! activeHandle || ! state.image ) {
|
|
793
|
-
return null;
|
|
794
|
-
}
|
|
795
|
-
const region = getSourceRegion( state, {
|
|
796
|
-
width: state.image.naturalWidth,
|
|
797
|
-
height: state.image.naturalHeight,
|
|
798
|
-
} );
|
|
799
|
-
return { width: region.width, height: region.height };
|
|
800
|
-
}, [ showDimensions, activeHandle, state ] );
|
|
801
|
-
|
|
802
|
-
/**
|
|
803
|
-
* Handle Escape on a resize handle — return focus to the canvas so
|
|
804
|
-
* arrow keys pan the image rather than resize.
|
|
805
|
-
*/
|
|
806
|
-
const handleEscape = useCallback( () => {
|
|
807
|
-
// Escape is always a keyboard action, so show the outline immediately
|
|
808
|
-
// rather than relying on the focus handler's :focus-visible check.
|
|
809
|
-
setIsFocusVisible( true );
|
|
810
|
-
canvasRef.current?.focus( { preventScroll: true } );
|
|
811
|
-
}, [] );
|
|
812
|
-
|
|
813
|
-
const handleResizeStart = useCallback(
|
|
814
|
-
( handle?: HandlePosition ) => {
|
|
815
|
-
if ( isTouchPinchingRef.current ) {
|
|
816
|
-
return;
|
|
817
|
-
}
|
|
818
|
-
// Freeze the magnification at its current value so grabbing a
|
|
819
|
-
// handle doesn't reset the zoom; it holds for the whole drag.
|
|
820
|
-
setFrozenViewScale( viewScaleRest );
|
|
821
|
-
isResizingRef.current = true;
|
|
822
|
-
setIsResizing( true );
|
|
823
|
-
setActiveHandle( handle ?? null );
|
|
824
|
-
// Clear any in-flight settle so transitions don't apply during the
|
|
825
|
-
// new drag (rapid successive resizes would otherwise inherit the
|
|
826
|
-
// previous settle animation).
|
|
827
|
-
finishSettling();
|
|
828
|
-
resetViewport();
|
|
829
|
-
onGestureStart?.();
|
|
830
|
-
},
|
|
831
|
-
[ finishSettling, onGestureStart, resetViewport, viewScaleRest ]
|
|
832
|
-
);
|
|
833
|
-
|
|
834
|
-
/**
|
|
835
|
-
* Handle resize end — settle the crop rect (re-center, fill height)
|
|
836
|
-
* and reset the viewport pan back to neutral.
|
|
837
|
-
*/
|
|
838
|
-
const handleResizeEnd = useCallback( () => {
|
|
839
|
-
const isCancellingForPinch = isTouchPinchingRef.current;
|
|
840
|
-
isResizingRef.current = false;
|
|
841
|
-
setIsResizing( false );
|
|
842
|
-
setActiveHandle( null );
|
|
843
|
-
clearTimeout( settleTimerRef.current );
|
|
844
|
-
|
|
845
|
-
if ( isCancellingForPinch ) {
|
|
846
|
-
// A pinch took over mid-resize. Tear down resize UI without
|
|
847
|
-
// settling or firing onGestureEnd: the pinch owns the gesture
|
|
848
|
-
// boundary and fires the end on its own touchend. The resize and
|
|
849
|
-
// the pinch therefore collapse into a single undo step, since
|
|
850
|
-
// `beginGesture` is idempotent and the snapshot captured at resize
|
|
851
|
-
// start is flushed by the pinch's `endGesture`.
|
|
852
|
-
finishSettling();
|
|
853
|
-
resetViewport();
|
|
854
|
-
return;
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
isSettlingRef.current = true;
|
|
858
|
-
setSettling( true );
|
|
859
|
-
// Reset viewport pan first so it transitions back to zero in sync
|
|
860
|
-
// with the settle animation on the image.
|
|
861
|
-
resetViewport();
|
|
862
|
-
settleCrop();
|
|
863
|
-
onGestureEnd?.();
|
|
864
|
-
// Normal completion is driven by transitionend so settling cannot clear
|
|
865
|
-
// before the browser has actually finished animating. Keep a fallback for
|
|
866
|
-
// cases where the computed transform is unchanged and no event fires.
|
|
867
|
-
settleTimerRef.current = setTimeout(
|
|
868
|
-
finishSettling,
|
|
869
|
-
SETTLE_TRANSITION_FALLBACK_MS
|
|
870
|
-
);
|
|
871
|
-
}, [ finishSettling, settleCrop, onGestureEnd, resetViewport ] );
|
|
872
|
-
|
|
873
|
-
/**
|
|
874
|
-
* Finish settling when the settle transform transition actually completes.
|
|
875
|
-
* The handler lives on the stage, but in the common (un-panned) case the
|
|
876
|
-
* transform transition runs on the image layer and its `transitionend`
|
|
877
|
-
* bubbles up to here. Other transitionend events (stencil left/top/width/
|
|
878
|
-
* height) also bubble up, so only the transform transition should clear the
|
|
879
|
-
* settle state.
|
|
880
|
-
*/
|
|
881
|
-
const handleSettleTransitionEnd = useCallback(
|
|
882
|
-
( event: React.TransitionEvent< HTMLDivElement > ) => {
|
|
883
|
-
if (
|
|
884
|
-
! isSettlingRef.current ||
|
|
885
|
-
event.propertyName !== 'transform'
|
|
886
|
-
) {
|
|
887
|
-
return;
|
|
888
|
-
}
|
|
889
|
-
finishSettling();
|
|
890
|
-
},
|
|
891
|
-
[ finishSettling ]
|
|
892
|
-
);
|
|
893
|
-
|
|
894
|
-
let imageTransition: string | undefined;
|
|
895
|
-
if ( settling ) {
|
|
896
|
-
imageTransition = SETTLE_TRANSFORM_TRANSITION;
|
|
897
|
-
} else if ( isZooming ) {
|
|
898
|
-
imageTransition = 'transform 150ms linear';
|
|
899
|
-
}
|
|
900
|
-
const settleStencilTransition = settling
|
|
901
|
-
? SETTLE_STENCIL_TRANSITION
|
|
902
|
-
: undefined;
|
|
903
|
-
|
|
904
|
-
// Compute the image's CSS style. The element keeps its contain-fit box; the
|
|
905
|
-
// view-scale magnification is folded into the transform as an outer
|
|
906
|
-
// `scale()` so the whole settle motion (zoom, pan, and magnification) is one
|
|
907
|
-
// coherent transform that interpolates as a unit — the image slides/scales
|
|
908
|
-
// to its position rather than animating its box separately. Magnifying
|
|
909
|
-
// around the centred box keeps the (centred) crop centred.
|
|
910
|
-
const imageStyle = useMemo( (): React.CSSProperties => {
|
|
911
|
-
if ( elementSize.width === 0 || elementSize.height === 0 ) {
|
|
912
|
-
return {};
|
|
913
|
-
}
|
|
914
|
-
const centerX = ( canvasSize.width - elementSize.width ) / 2;
|
|
915
|
-
const centerY = ( canvasSize.height - elementSize.height ) / 2;
|
|
916
|
-
// Above 1:1 the image is upscaled, so render nearest-neighbour to keep
|
|
917
|
-
// pixel boundaries crisp (e.g. small images the cropper magnifies);
|
|
918
|
-
// below 1:1 leave smoothing on for downscaled large images.
|
|
919
|
-
return {
|
|
920
|
-
width: elementSize.width,
|
|
921
|
-
height: elementSize.height,
|
|
922
|
-
maxWidth: elementSize.width,
|
|
923
|
-
maxHeight: elementSize.height,
|
|
924
|
-
left: centerX,
|
|
925
|
-
top: centerY,
|
|
926
|
-
// Always lead with `scale()` (identity at rest) so the transform's
|
|
927
|
-
// function list stays structurally constant across rest, resize, and
|
|
928
|
-
// settle. A conditional that dropped `scale()` at viewScale === 1
|
|
929
|
-
// would change the list shape exactly when the settle transition
|
|
930
|
-
// crosses 1:1, forcing the browser to fall back to matrix-decomposition
|
|
931
|
-
// interpolation instead of interpolating each function in turn.
|
|
932
|
-
transform: `scale(${ viewScale }) ${ transformString }`,
|
|
933
|
-
transition: imageTransition,
|
|
934
|
-
imageRendering: displayScale > 1 ? 'pixelated' : undefined,
|
|
935
|
-
};
|
|
936
|
-
}, [
|
|
937
|
-
canvasSize,
|
|
938
|
-
elementSize,
|
|
939
|
-
transformString,
|
|
940
|
-
imageTransition,
|
|
941
|
-
displayScale,
|
|
942
|
-
viewScale,
|
|
943
|
-
] );
|
|
944
|
-
|
|
945
|
-
// Viewport pan CSS transform for the stage div. Applied during resize
|
|
946
|
-
// drags to keep handles visible when the crop extends past the canvas edge.
|
|
947
|
-
// Transitions back to zero during the settle animation.
|
|
948
|
-
// will-change promotes the stage to its own compositor layer while the
|
|
949
|
-
// transform is active, keeping per-frame pan updates off the main thread.
|
|
950
|
-
const settleTransition = settling ? SETTLE_TRANSFORM_TRANSITION : undefined;
|
|
951
|
-
const willChange = isResizing || settling ? 'transform' : undefined;
|
|
952
|
-
let stageStyle: React.CSSProperties | undefined;
|
|
953
|
-
if ( viewportState.pan.x !== 0 || viewportState.pan.y !== 0 ) {
|
|
954
|
-
stageStyle = {
|
|
955
|
-
transform: `translate(${ viewportState.pan.x }px, ${ viewportState.pan.y }px)`,
|
|
956
|
-
transition: settleTransition,
|
|
957
|
-
willChange,
|
|
958
|
-
};
|
|
959
|
-
} else if ( settling ) {
|
|
960
|
-
// Animate the viewport pan back to an explicit identity translate rather
|
|
961
|
-
// than dropping the transform property, so the reset eases instead of
|
|
962
|
-
// snapping (transitions to a removed transform are unreliable).
|
|
963
|
-
stageStyle = {
|
|
964
|
-
transform: 'translate(0px, 0px)',
|
|
965
|
-
transition: settleTransition,
|
|
966
|
-
willChange,
|
|
967
|
-
};
|
|
968
|
-
}
|
|
969
|
-
|
|
970
|
-
return (
|
|
971
|
-
<div
|
|
972
|
-
ref={ ref }
|
|
973
|
-
className={ clsx(
|
|
974
|
-
'wp-media-editor-image-editor',
|
|
975
|
-
isDragging && 'wp-media-editor-image-editor--dragging',
|
|
976
|
-
className
|
|
977
|
-
) }
|
|
978
|
-
>
|
|
979
|
-
{ /*
|
|
980
|
-
* The canvas is the interactive, inset surface. Handles and
|
|
981
|
-
* the ARIA role/tabIndex live here so pointer geometry
|
|
982
|
-
* (getBoundingClientRect on e.currentTarget) resolves against
|
|
983
|
-
* the same box that crop math uses. The root stays as the
|
|
984
|
-
* clipping shell for the dimming overlay's box-shadow.
|
|
985
|
-
*
|
|
986
|
-
* Not role="application" — that disables the screen reader's
|
|
987
|
-
* normal keyboard interception, too heavy-handed for a single
|
|
988
|
-
* widget. Screen reader users get the ARIA live region below
|
|
989
|
-
* as the announcement channel.
|
|
990
|
-
*/ }
|
|
991
|
-
<div
|
|
992
|
-
ref={ canvasRef }
|
|
993
|
-
className={ clsx(
|
|
994
|
-
'wp-media-editor-image-editor__canvas',
|
|
995
|
-
isInteractiveGrid &&
|
|
996
|
-
'wp-media-editor-image-editor__canvas--grid-interactive',
|
|
997
|
-
showInteractiveGrid &&
|
|
998
|
-
'wp-media-editor-image-editor__canvas--show-grid',
|
|
999
|
-
settling &&
|
|
1000
|
-
'wp-media-editor-image-editor__canvas--settling',
|
|
1001
|
-
// Marks the cropper as in keyboard-interaction mode.
|
|
1002
|
-
// CSS uses :focus on the canvas to show the stencil
|
|
1003
|
-
// outline and :focus on a handle to show its ring,
|
|
1004
|
-
// so the class applies whenever any cropper element
|
|
1005
|
-
// has keyboard focus.
|
|
1006
|
-
isFocusVisible &&
|
|
1007
|
-
'wp-media-editor-image-editor__canvas--focus-visible'
|
|
1008
|
-
) }
|
|
1009
|
-
tabIndex={ 0 }
|
|
1010
|
-
role="group"
|
|
1011
|
-
aria-label={ __( 'Crop area' ) }
|
|
1012
|
-
aria-describedby={
|
|
1013
|
-
isCropAreaFocused ? cropAreaDescriptionId : undefined
|
|
1014
|
-
}
|
|
1015
|
-
onFocus={ handleCropAreaFocus }
|
|
1016
|
-
onBlur={ handleCropAreaBlur }
|
|
1017
|
-
{ ...canvasHandlers }
|
|
1018
|
-
>
|
|
1019
|
-
<div
|
|
1020
|
-
id={ cropAreaDescriptionId }
|
|
1021
|
-
style={ VISUALLY_HIDDEN_STYLE }
|
|
1022
|
-
>
|
|
1023
|
-
{ __(
|
|
1024
|
-
'When this area is focused, use arrow keys to move the image and plus or minus to zoom. Tab to resize handles and controls.'
|
|
1025
|
-
) }
|
|
1026
|
-
</div>
|
|
1027
|
-
{ /*
|
|
1028
|
-
* The stage is an inner full-size div that receives the
|
|
1029
|
-
* viewport pan CSS transform. Keeping the transform here
|
|
1030
|
-
* (rather than on the canvas) means the canvas always stays
|
|
1031
|
-
* at its natural position, so the root div's background is
|
|
1032
|
-
* never exposed when the stage is panned during a resize drag.
|
|
1033
|
-
*/ }
|
|
1034
|
-
<div
|
|
1035
|
-
className="wp-media-editor-image-editor__stage"
|
|
1036
|
-
data-testid="cropper-stage"
|
|
1037
|
-
style={ stageStyle }
|
|
1038
|
-
onTransitionEnd={ handleSettleTransitionEnd }
|
|
1039
|
-
>
|
|
1040
|
-
{ /* The image layer */ }
|
|
1041
|
-
<img
|
|
1042
|
-
className="wp-media-editor-image-editor__image"
|
|
1043
|
-
data-testid="cropper-image"
|
|
1044
|
-
src={ src }
|
|
1045
|
-
alt=""
|
|
1046
|
-
onLoad={ handleImageLoad }
|
|
1047
|
-
style={ imageStyle }
|
|
1048
|
-
draggable={ false }
|
|
1049
|
-
/>
|
|
1050
|
-
|
|
1051
|
-
{ /* Dimming overlay outside the crop area */ }
|
|
1052
|
-
{ showDimming && (
|
|
1053
|
-
<DimmingOverlay
|
|
1054
|
-
cropRect={ state.cropRect }
|
|
1055
|
-
containerSize={ canvasSize }
|
|
1056
|
-
imageSize={ scaledVisualSize }
|
|
1057
|
-
transition={ settleStencilTransition }
|
|
1058
|
-
/>
|
|
1059
|
-
) }
|
|
1060
|
-
|
|
1061
|
-
{ /* The stencil (crop area with handles) */ }
|
|
1062
|
-
<StencilComponent
|
|
1063
|
-
cropRect={ state.cropRect }
|
|
1064
|
-
containerSize={ canvasSize }
|
|
1065
|
-
imageSize={ scaledVisualSize }
|
|
1066
|
-
onCropChange={ handleCropChange }
|
|
1067
|
-
onResizeStart={ handleResizeStart }
|
|
1068
|
-
onResizeEnd={ handleResizeEnd }
|
|
1069
|
-
onEscape={ handleEscape }
|
|
1070
|
-
aspectRatio={ aspectRatio }
|
|
1071
|
-
freeformCrop={ freeformCrop }
|
|
1072
|
-
isResizeDisabled={ isTouchPinching || disabled }
|
|
1073
|
-
stencilTransition={ settleStencilTransition }
|
|
1074
|
-
cropBounds={ cropBounds }
|
|
1075
|
-
minCropSize={ minCropSize }
|
|
1076
|
-
snapCropRect={ snapCropRect }
|
|
1077
|
-
keyboardResizeStep={ keyboardResizeStep }
|
|
1078
|
-
/>
|
|
1079
|
-
|
|
1080
|
-
{ /* Rule-of-thirds grid */ }
|
|
1081
|
-
{ ( showGrid === true || isInteractiveGrid ) && (
|
|
1082
|
-
<GridOverlay
|
|
1083
|
-
cropRect={ state.cropRect }
|
|
1084
|
-
containerSize={ canvasSize }
|
|
1085
|
-
imageSize={ scaledVisualSize }
|
|
1086
|
-
/>
|
|
1087
|
-
) }
|
|
1088
|
-
|
|
1089
|
-
{ /* Live dimensions tooltip pinned to the dragged handle. */ }
|
|
1090
|
-
{ activeHandle && outputSize && (
|
|
1091
|
-
<DimensionsOverlay
|
|
1092
|
-
cropRect={ state.cropRect }
|
|
1093
|
-
containerSize={ canvasSize }
|
|
1094
|
-
imageSize={ scaledVisualSize }
|
|
1095
|
-
activeHandle={ activeHandle }
|
|
1096
|
-
outputWidth={ outputSize.width }
|
|
1097
|
-
outputHeight={ outputSize.height }
|
|
1098
|
-
/>
|
|
1099
|
-
) }
|
|
1100
|
-
</div>
|
|
1101
|
-
</div>
|
|
1102
|
-
</div>
|
|
1103
|
-
);
|
|
1104
|
-
}
|
|
1105
|
-
|
|
1106
|
-
const CropperInnerWithRef = forwardRef< HTMLDivElement, CropperProps >(
|
|
1107
|
-
CropperInner
|
|
1108
|
-
);
|
|
1109
|
-
|
|
1110
|
-
export const Cropper = forwardRef< HTMLDivElement, CropperProps >(
|
|
1111
|
-
( props, ref ) => (
|
|
1112
|
-
<ViewportProvider>
|
|
1113
|
-
<CropperInnerWithRef { ...props } ref={ ref } />
|
|
1114
|
-
</ViewportProvider>
|
|
1115
|
-
)
|
|
1116
|
-
);
|