@wordpress/media-editor 0.9.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/components/media-editor/index.cjs +212 -170
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +63 -0
- package/build/components/media-editor/use-crop-options.cjs.map +7 -0
- package/build/components/media-editor/use-save-media-editor.cjs +185 -0
- package/build/components/media-editor/use-save-media-editor.cjs.map +7 -0
- package/build/components/media-editor-canvas/index.cjs +33 -12
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs +70 -84
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-fine-rotation/index.cjs +81 -0
- package/build/components/media-editor-fine-rotation/index.cjs.map +7 -0
- package/build/components/media-editor-modal/index.cjs +63 -16
- package/build/components/media-editor-modal/index.cjs.map +3 -3
- package/build/components/media-editor-transform-controls/index.cjs +84 -0
- package/build/components/media-editor-transform-controls/index.cjs.map +7 -0
- package/build/components/media-form/index.cjs +19 -1
- package/build/components/media-form/index.cjs.map +3 -3
- package/build/components/media-form/sidebar-datetime-view.cjs +53 -0
- package/build/components/media-form/sidebar-datetime-view.cjs.map +7 -0
- package/build/components/rotation-ruler/index.cjs +8 -2
- package/build/components/rotation-ruler/index.cjs.map +2 -2
- package/build/hooks/use-crop-gesture-handlers.cjs +41 -4
- package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
- package/build/image-editor/core/camera.cjs +29 -11
- package/build/image-editor/core/camera.cjs.map +2 -2
- package/build/image-editor/core/constants.cjs +6 -0
- package/build/image-editor/core/constants.cjs.map +2 -2
- package/build/image-editor/core/containment.cjs +71 -34
- package/build/image-editor/core/containment.cjs.map +2 -2
- package/build/image-editor/core/crop-rect.cjs +48 -0
- package/build/image-editor/core/crop-rect.cjs.map +7 -0
- package/build/image-editor/core/index.cjs +12 -0
- package/build/image-editor/core/index.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/core/math/sanitize.cjs +92 -0
- package/build/image-editor/core/math/sanitize.cjs.map +7 -0
- package/build/image-editor/core/setter-helpers.cjs +64 -0
- package/build/image-editor/core/setter-helpers.cjs.map +7 -0
- package/build/image-editor/core/source-region.cjs +16 -14
- package/build/image-editor/core/source-region.cjs.map +2 -2
- package/build/image-editor/core/state.cjs +20 -10
- package/build/image-editor/core/state.cjs.map +2 -2
- package/build/image-editor/core/stencil-math.cjs +31 -20
- package/build/image-editor/core/stencil-math.cjs.map +2 -2
- package/build/image-editor/core/transform-style.cjs +12 -6
- package/build/image-editor/core/transform-style.cjs.map +2 -2
- package/build/image-editor/core/types.cjs.map +1 -1
- package/build/image-editor/index.cjs +12 -2
- package/build/image-editor/index.cjs.map +2 -2
- package/build/image-editor/react/components/cropper-provider.cjs +2 -2
- package/build/image-editor/react/components/cropper-provider.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +120 -47
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs +172 -0
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +7 -0
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +19 -9
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/build-cropper-setters.cjs +71 -0
- package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +7 -0
- package/build/image-editor/react/hooks/index.cjs +6 -3
- package/build/image-editor/react/hooks/index.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs +113 -0
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +7 -0
- package/build/state/composite-reducer.cjs +103 -0
- package/build/state/composite-reducer.cjs.map +7 -0
- package/build/state/index.cjs +48 -0
- package/build/state/index.cjs.map +7 -0
- package/build/state/media-editor-state-provider.cjs +58 -0
- package/build/state/media-editor-state-provider.cjs.map +7 -0
- package/build/state/types.cjs +33 -0
- package/build/state/types.cjs.map +7 -0
- package/build/state/use-media-editor-state.cjs +287 -0
- package/build/state/use-media-editor-state.cjs.map +7 -0
- package/build/store/actions.cjs +4 -2
- package/build/store/actions.cjs.map +2 -2
- package/build/store/reducer.cjs +5 -3
- package/build/store/reducer.cjs.map +2 -2
- package/build/store/selectors.cjs +5 -0
- package/build/store/selectors.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +224 -177
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +37 -0
- package/build-module/components/media-editor/use-crop-options.mjs.map +7 -0
- package/build-module/components/media-editor/use-save-media-editor.mjs +151 -0
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +7 -0
- package/build-module/components/media-editor-canvas/index.mjs +34 -13
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +73 -89
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs +50 -0
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +7 -0
- package/build-module/components/media-editor-modal/index.mjs +64 -17
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-transform-controls/index.mjs +68 -0
- package/build-module/components/media-editor-transform-controls/index.mjs.map +7 -0
- package/build-module/components/media-form/index.mjs +9 -1
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/media-form/sidebar-datetime-view.mjs +32 -0
- package/build-module/components/media-form/sidebar-datetime-view.mjs.map +7 -0
- package/build-module/components/rotation-ruler/index.mjs +8 -2
- package/build-module/components/rotation-ruler/index.mjs.map +2 -2
- package/build-module/hooks/use-crop-gesture-handlers.mjs +41 -4
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
- package/build-module/image-editor/core/camera.mjs +34 -11
- package/build-module/image-editor/core/camera.mjs.map +2 -2
- package/build-module/image-editor/core/constants.mjs +4 -0
- package/build-module/image-editor/core/constants.mjs.map +2 -2
- package/build-module/image-editor/core/containment.mjs +75 -34
- package/build-module/image-editor/core/containment.mjs.map +2 -2
- package/build-module/image-editor/core/crop-rect.mjs +23 -0
- package/build-module/image-editor/core/crop-rect.mjs.map +7 -0
- package/build-module/image-editor/core/index.mjs +12 -0
- package/build-module/image-editor/core/index.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/core/math/sanitize.mjs +63 -0
- package/build-module/image-editor/core/math/sanitize.mjs.map +7 -0
- package/build-module/image-editor/core/setter-helpers.mjs +39 -0
- package/build-module/image-editor/core/setter-helpers.mjs.map +7 -0
- package/build-module/image-editor/core/source-region.mjs +16 -14
- package/build-module/image-editor/core/source-region.mjs.map +2 -2
- package/build-module/image-editor/core/state.mjs +25 -11
- package/build-module/image-editor/core/state.mjs.map +2 -2
- package/build-module/image-editor/core/stencil-math.mjs +30 -19
- package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
- package/build-module/image-editor/core/transform-style.mjs +12 -6
- package/build-module/image-editor/core/transform-style.mjs.map +2 -2
- package/build-module/image-editor/index.mjs +11 -2
- package/build-module/image-editor/index.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper-provider.mjs +3 -3
- package/build-module/image-editor/react/components/cropper-provider.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +121 -48
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs +147 -0
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +7 -0
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +19 -9
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs +46 -0
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +7 -0
- package/build-module/image-editor/react/hooks/index.mjs +4 -2
- package/build-module/image-editor/react/hooks/index.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +100 -0
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +7 -0
- package/build-module/state/composite-reducer.mjs +82 -0
- package/build-module/state/composite-reducer.mjs.map +7 -0
- package/build-module/state/index.mjs +26 -0
- package/build-module/state/index.mjs.map +7 -0
- package/build-module/state/media-editor-state-provider.mjs +34 -0
- package/build-module/state/media-editor-state-provider.mjs.map +7 -0
- package/build-module/state/types.mjs +8 -0
- package/build-module/state/types.mjs.map +7 -0
- package/build-module/state/use-media-editor-state.mjs +278 -0
- package/build-module/state/use-media-editor-state.mjs.map +7 -0
- package/build-module/store/actions.mjs +4 -2
- package/build-module/store/actions.mjs.map +2 -2
- package/build-module/store/reducer.mjs +5 -3
- package/build-module/store/reducer.mjs.map +2 -2
- package/build-module/store/selectors.mjs +4 -0
- package/build-module/store/selectors.mjs.map +2 -2
- package/build-style/style-rtl.css +97 -44
- package/build-style/style.css +97 -44
- package/build-types/components/media-editor/index.d.ts +14 -5
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +35 -0
- package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -0
- package/build-types/components/media-editor/use-save-media-editor.d.ts +29 -0
- package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -0
- package/build-types/components/media-editor-canvas/index.d.ts +5 -10
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts +6 -25
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-fine-rotation/index.d.ts +14 -0
- package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -0
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-transform-controls/index.d.ts +8 -0
- package/build-types/components/media-editor-transform-controls/index.d.ts.map +1 -0
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/components/media-form/sidebar-datetime-view.d.ts +10 -0
- package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +1 -0
- package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
- package/build-types/hooks/use-crop-gesture-handlers.d.ts +13 -14
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
- package/build-types/image-editor/core/camera.d.ts.map +1 -1
- package/build-types/image-editor/core/constants.d.ts +18 -0
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/containment.d.ts +10 -0
- package/build-types/image-editor/core/containment.d.ts.map +1 -1
- package/build-types/image-editor/core/crop-rect.d.ts +19 -0
- package/build-types/image-editor/core/crop-rect.d.ts.map +1 -0
- package/build-types/image-editor/core/index.d.ts +3 -1
- package/build-types/image-editor/core/index.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts +5 -1
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/core/math/sanitize.d.ts +70 -0
- package/build-types/image-editor/core/math/sanitize.d.ts.map +1 -0
- package/build-types/image-editor/core/setter-helpers.d.ts +24 -0
- package/build-types/image-editor/core/setter-helpers.d.ts.map +1 -0
- package/build-types/image-editor/core/source-region.d.ts.map +1 -1
- package/build-types/image-editor/core/state.d.ts +12 -0
- package/build-types/image-editor/core/state.d.ts.map +1 -1
- package/build-types/image-editor/core/stencil-math.d.ts +23 -18
- package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
- package/build-types/image-editor/core/transform-style.d.ts.map +1 -1
- package/build-types/image-editor/core/types.d.ts +23 -4
- package/build-types/image-editor/core/types.d.ts.map +1 -1
- package/build-types/image-editor/index.d.ts +4 -4
- package/build-types/image-editor/index.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts +11 -7
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +6 -4
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts +36 -0
- package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +1 -0
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +2 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +47 -0
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +1 -0
- package/build-types/image-editor/react/hooks/index.d.ts +4 -2
- package/build-types/image-editor/react/hooks/index.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts +58 -0
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +1 -0
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts +39 -0
- package/build-types/state/composite-reducer.d.ts.map +1 -0
- package/build-types/state/index.d.ts +7 -0
- package/build-types/state/index.d.ts.map +1 -0
- package/build-types/state/media-editor-state-provider.d.ts +34 -0
- package/build-types/state/media-editor-state-provider.d.ts.map +1 -0
- package/build-types/state/types.d.ts +96 -0
- package/build-types/state/types.d.ts.map +1 -0
- package/build-types/state/use-media-editor-state.d.ts +101 -0
- package/build-types/state/use-media-editor-state.d.ts.map +1 -0
- package/build-types/store/actions.d.ts +3 -1
- package/build-types/store/actions.d.ts.map +1 -1
- package/build-types/store/reducer.d.ts +3 -0
- package/build-types/store/reducer.d.ts.map +1 -1
- package/build-types/store/selectors.d.ts +1 -0
- package/build-types/store/selectors.d.ts.map +1 -1
- package/package.json +19 -16
- package/src/components/media-editor/index.tsx +262 -229
- package/src/components/media-editor/style.scss +0 -9
- package/src/components/media-editor/test/use-crop-options.tsx +110 -0
- package/src/components/media-editor/use-crop-options.ts +79 -0
- package/src/components/media-editor/use-save-media-editor.ts +214 -0
- package/src/components/media-editor-canvas/index.tsx +57 -25
- package/src/components/media-editor-crop-panel/index.tsx +53 -87
- package/src/components/media-editor-crop-panel/test/index.tsx +46 -53
- package/src/components/media-editor-fine-rotation/index.tsx +74 -0
- package/src/components/media-editor-modal/index.tsx +70 -16
- package/src/components/media-editor-modal/style.scss +80 -0
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +14 -0
- package/src/components/media-editor-modal/test/index.tsx +146 -0
- package/src/components/media-editor-transform-controls/index.tsx +70 -0
- package/src/components/media-editor-transform-controls/style.scss +8 -0
- package/src/components/media-form/index.tsx +15 -1
- package/src/components/media-form/sidebar-datetime-view.tsx +47 -0
- package/src/components/media-form/test/sidebar-datetime-view.test.tsx +96 -0
- package/src/components/rotation-ruler/README.md +11 -25
- package/src/components/rotation-ruler/index.tsx +17 -2
- package/src/components/rotation-ruler/style.scss +49 -13
- package/src/hooks/use-crop-gesture-handlers.ts +64 -17
- package/src/image-editor/README.md +28 -129
- package/src/image-editor/core/camera.ts +51 -26
- package/src/image-editor/core/constants.ts +19 -0
- package/src/image-editor/core/containment.ts +102 -49
- package/src/image-editor/core/crop-rect.ts +45 -0
- package/src/image-editor/core/index.ts +16 -4
- package/src/image-editor/core/interaction-controller.ts +5 -1
- package/src/image-editor/core/math/sanitize.ts +158 -0
- package/src/image-editor/core/math/test/sanitize.ts +244 -0
- package/src/image-editor/core/setter-helpers.ts +66 -0
- package/src/image-editor/core/source-region.ts +21 -14
- package/src/image-editor/core/state.ts +67 -15
- package/src/image-editor/core/stencil-math.ts +55 -35
- package/src/image-editor/core/test/camera.ts +367 -1
- package/src/image-editor/core/test/state.ts +101 -8
- package/src/image-editor/core/test/stencil-math.ts +193 -0
- package/src/image-editor/core/transform-style.ts +21 -6
- package/src/image-editor/core/types.ts +24 -4
- package/src/image-editor/docs/architecture.md +48 -89
- package/src/image-editor/docs/recipes.md +123 -659
- package/src/image-editor/index.ts +12 -3
- package/src/image-editor/react/components/cropper-provider.tsx +16 -12
- package/src/image-editor/react/components/cropper.scss +23 -4
- package/src/image-editor/react/components/cropper.tsx +194 -89
- package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +269 -0
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +19 -8
- package/src/image-editor/react/components/test/cropper.tsx +55 -13
- package/src/image-editor/react/hooks/build-cropper-setters.ts +105 -0
- package/src/image-editor/react/hooks/index.ts +4 -2
- package/src/image-editor/react/hooks/test/{use-cropper-state.ts → use-cropper-reducer.ts} +142 -286
- package/src/image-editor/react/hooks/use-cropper-reducer.ts +204 -0
- package/src/image-editor/stories/rectangle-crop.story.tsx +7 -17
- package/src/state/composite-reducer.ts +137 -0
- package/src/state/index.ts +20 -0
- package/src/state/media-editor-state-provider.tsx +70 -0
- package/src/state/test/use-media-editor-state.ts +330 -0
- package/src/state/types.ts +101 -0
- package/src/state/use-media-editor-state.ts +472 -0
- package/src/store/actions.ts +3 -0
- package/src/store/reducer.ts +6 -1
- package/src/store/selectors.ts +4 -0
- package/src/store/test/reducer.ts +9 -0
- package/src/style.scss +1 -1
- package/tsconfig.json +3 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/build/components/media-editor-toolbar/index.cjs +0 -214
- package/build/components/media-editor-toolbar/index.cjs.map +0 -7
- package/build/image-editor/react/hooks/use-cropper-state.cjs +0 -277
- package/build/image-editor/react/hooks/use-cropper-state.cjs.map +0 -7
- package/build-module/components/media-editor-toolbar/index.mjs +0 -190
- package/build-module/components/media-editor-toolbar/index.mjs.map +0 -7
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs +0 -262
- package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +0 -7
- package/build-types/components/media-editor-toolbar/index.d.ts +0 -24
- package/build-types/components/media-editor-toolbar/index.d.ts.map +0 -1
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -97
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
- package/src/components/media-editor-toolbar/index.tsx +0 -209
- package/src/components/media-editor-toolbar/style.scss +0 -49
- package/src/image-editor/docs/roadmap.md +0 -66
- package/src/image-editor/react/hooks/use-cropper-state.ts +0 -457
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// packages/media-editor/src/image-editor/react/components/overlays/dimensions-overlay.tsx
|
|
2
|
+
import { useLayoutEffect, useRef, useState } from "@wordpress/element";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
var HANDLE_GAP_PX = 12;
|
|
5
|
+
var TRANSLATE_PERCENT = {
|
|
6
|
+
before: "-100%",
|
|
7
|
+
center: "-50%",
|
|
8
|
+
after: "0"
|
|
9
|
+
};
|
|
10
|
+
var GAP_DIRECTION = {
|
|
11
|
+
before: -1,
|
|
12
|
+
center: 0,
|
|
13
|
+
after: 1
|
|
14
|
+
};
|
|
15
|
+
var OPPOSITE_SIDE = {
|
|
16
|
+
before: "after",
|
|
17
|
+
center: "center",
|
|
18
|
+
after: "before"
|
|
19
|
+
};
|
|
20
|
+
function preferredAnchor(handle) {
|
|
21
|
+
let x = "center";
|
|
22
|
+
if (handle.includes("w")) {
|
|
23
|
+
x = "before";
|
|
24
|
+
} else if (handle.includes("e")) {
|
|
25
|
+
x = "after";
|
|
26
|
+
}
|
|
27
|
+
let y = "center";
|
|
28
|
+
if (handle.includes("n")) {
|
|
29
|
+
y = "before";
|
|
30
|
+
} else if (handle.includes("s")) {
|
|
31
|
+
y = "after";
|
|
32
|
+
}
|
|
33
|
+
return { x, y };
|
|
34
|
+
}
|
|
35
|
+
function sideStart(side, handlePos, length) {
|
|
36
|
+
if (side === "before") {
|
|
37
|
+
return handlePos - HANDLE_GAP_PX - length;
|
|
38
|
+
}
|
|
39
|
+
if (side === "after") {
|
|
40
|
+
return handlePos + HANDLE_GAP_PX;
|
|
41
|
+
}
|
|
42
|
+
return handlePos - length / 2;
|
|
43
|
+
}
|
|
44
|
+
function fittingSide(preferred, handlePos, length, containerLength) {
|
|
45
|
+
const fits = (side) => {
|
|
46
|
+
const start = sideStart(side, handlePos, length);
|
|
47
|
+
return start >= 0 && start + length <= containerLength;
|
|
48
|
+
};
|
|
49
|
+
if (fits(preferred)) {
|
|
50
|
+
return preferred;
|
|
51
|
+
}
|
|
52
|
+
const opposite = OPPOSITE_SIDE[preferred];
|
|
53
|
+
if (opposite !== preferred && fits(opposite)) {
|
|
54
|
+
return opposite;
|
|
55
|
+
}
|
|
56
|
+
if (preferred !== "center" && fits("center")) {
|
|
57
|
+
return "center";
|
|
58
|
+
}
|
|
59
|
+
const overflow = (side) => {
|
|
60
|
+
const start = sideStart(side, handlePos, length);
|
|
61
|
+
return Math.max(0, -start) + Math.max(0, start + length - containerLength);
|
|
62
|
+
};
|
|
63
|
+
const candidates = [preferred, opposite, "center"];
|
|
64
|
+
return candidates.reduce(
|
|
65
|
+
(best, side) => overflow(side) < overflow(best) ? side : best
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
function DimensionsOverlay({
|
|
69
|
+
cropRect,
|
|
70
|
+
containerSize,
|
|
71
|
+
imageSize,
|
|
72
|
+
activeHandle,
|
|
73
|
+
outputWidth,
|
|
74
|
+
outputHeight
|
|
75
|
+
}) {
|
|
76
|
+
const tooltipRef = useRef(null);
|
|
77
|
+
const [tooltipSize, setTooltipSize] = useState(null);
|
|
78
|
+
useLayoutEffect(() => {
|
|
79
|
+
if (!tooltipRef.current) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const rect = tooltipRef.current.getBoundingClientRect();
|
|
83
|
+
setTooltipSize((prev) => {
|
|
84
|
+
if (prev && prev.width === rect.width && prev.height === rect.height) {
|
|
85
|
+
return prev;
|
|
86
|
+
}
|
|
87
|
+
return { width: rect.width, height: rect.height };
|
|
88
|
+
});
|
|
89
|
+
}, [outputWidth, outputHeight]);
|
|
90
|
+
if (!activeHandle || containerSize.width === 0 || containerSize.height === 0) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
const offsetX = (containerSize.width - imageSize.width) / 2;
|
|
94
|
+
const offsetY = (containerSize.height - imageSize.height) / 2;
|
|
95
|
+
const left = offsetX + cropRect.x * imageSize.width;
|
|
96
|
+
const top = offsetY + cropRect.y * imageSize.height;
|
|
97
|
+
const width = cropRect.width * imageSize.width;
|
|
98
|
+
const height = cropRect.height * imageSize.height;
|
|
99
|
+
let handleX = left + width / 2;
|
|
100
|
+
if (activeHandle.includes("w")) {
|
|
101
|
+
handleX = left;
|
|
102
|
+
} else if (activeHandle.includes("e")) {
|
|
103
|
+
handleX = left + width;
|
|
104
|
+
}
|
|
105
|
+
let handleY = top + height / 2;
|
|
106
|
+
if (activeHandle.includes("n")) {
|
|
107
|
+
handleY = top;
|
|
108
|
+
} else if (activeHandle.includes("s")) {
|
|
109
|
+
handleY = top + height;
|
|
110
|
+
}
|
|
111
|
+
const preferred = preferredAnchor(activeHandle);
|
|
112
|
+
const anchor = tooltipSize ? {
|
|
113
|
+
x: fittingSide(
|
|
114
|
+
preferred.x,
|
|
115
|
+
handleX,
|
|
116
|
+
tooltipSize.width,
|
|
117
|
+
containerSize.width
|
|
118
|
+
),
|
|
119
|
+
y: fittingSide(
|
|
120
|
+
preferred.y,
|
|
121
|
+
handleY,
|
|
122
|
+
tooltipSize.height,
|
|
123
|
+
containerSize.height
|
|
124
|
+
)
|
|
125
|
+
} : preferred;
|
|
126
|
+
return /* @__PURE__ */ jsx(
|
|
127
|
+
"div",
|
|
128
|
+
{
|
|
129
|
+
ref: tooltipRef,
|
|
130
|
+
className: "wp-media-editor-image-editor__dimensions-tooltip",
|
|
131
|
+
"data-testid": "cropper-dimensions-tooltip",
|
|
132
|
+
style: {
|
|
133
|
+
left: handleX + GAP_DIRECTION[anchor.x] * HANDLE_GAP_PX,
|
|
134
|
+
top: handleY + GAP_DIRECTION[anchor.y] * HANDLE_GAP_PX,
|
|
135
|
+
transform: `translate(${TRANSLATE_PERCENT[anchor.x]}, ${TRANSLATE_PERCENT[anchor.y]})`
|
|
136
|
+
},
|
|
137
|
+
"aria-hidden": "true",
|
|
138
|
+
children: `W: ${Math.round(outputWidth)}px H: ${Math.round(
|
|
139
|
+
outputHeight
|
|
140
|
+
)}px`
|
|
141
|
+
}
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
export {
|
|
145
|
+
DimensionsOverlay
|
|
146
|
+
};
|
|
147
|
+
//# sourceMappingURL=dimensions-overlay.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../../src/image-editor/react/components/overlays/dimensions-overlay.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useLayoutEffect, useRef, useState } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type { HandlePosition, NormalizedRect, Size } from '../../../core/types';\n\nconst HANDLE_GAP_PX = 12;\n\n/**\n * Which side of the handle a tooltip axis sits on. `before` places the\n * tooltip on the negative side of the handle (above for Y, left for X),\n * `after` on the positive side, `center` keeps it aligned with the handle.\n */\ntype AxisSide = 'before' | 'center' | 'after';\n\ninterface Anchor {\n\tx: AxisSide;\n\ty: AxisSide;\n}\n\nconst TRANSLATE_PERCENT: Record< AxisSide, string > = {\n\tbefore: '-100%',\n\tcenter: '-50%',\n\tafter: '0',\n};\n\nconst GAP_DIRECTION: Record< AxisSide, number > = {\n\tbefore: -1,\n\tcenter: 0,\n\tafter: 1,\n};\n\nconst OPPOSITE_SIDE: Record< AxisSide, AxisSide > = {\n\tbefore: 'after',\n\tcenter: 'center',\n\tafter: 'before',\n};\n\n/**\n * Preferred anchor for a handle, before edge-clipping is considered:\n * tooltip sits outward from the crop rectangle's center.\n *\n * @param handle\n */\nfunction preferredAnchor( handle: HandlePosition ): Anchor {\n\tlet x: AxisSide = 'center';\n\tif ( handle.includes( 'w' ) ) {\n\t\tx = 'before';\n\t} else if ( handle.includes( 'e' ) ) {\n\t\tx = 'after';\n\t}\n\n\tlet y: AxisSide = 'center';\n\tif ( handle.includes( 'n' ) ) {\n\t\ty = 'before';\n\t} else if ( handle.includes( 's' ) ) {\n\t\ty = 'after';\n\t}\n\n\treturn { x, y };\n}\n\n/**\n * Start coordinate of a tooltip placed on the given side of `handlePos`,\n * accounting for the per-side handle gap and the tooltip's own length.\n *\n * @param side\n * @param handlePos\n * @param length\n */\nfunction sideStart(\n\tside: AxisSide,\n\thandlePos: number,\n\tlength: number\n): number {\n\tif ( side === 'before' ) {\n\t\treturn handlePos - HANDLE_GAP_PX - length;\n\t}\n\tif ( side === 'after' ) {\n\t\treturn handlePos + HANDLE_GAP_PX;\n\t}\n\treturn handlePos - length / 2;\n}\n\n/**\n * Picks a side that keeps the tooltip inside the container along one\n * axis, preferring the supplied side and falling back to its opposite,\n * then center.\n *\n * @param preferred\n * @param handlePos\n * @param length\n * @param containerLength\n */\nfunction fittingSide(\n\tpreferred: AxisSide,\n\thandlePos: number,\n\tlength: number,\n\tcontainerLength: number\n): AxisSide {\n\tconst fits = ( side: AxisSide ) => {\n\t\tconst start = sideStart( side, handlePos, length );\n\t\treturn start >= 0 && start + length <= containerLength;\n\t};\n\n\tif ( fits( preferred ) ) {\n\t\treturn preferred;\n\t}\n\tconst opposite = OPPOSITE_SIDE[ preferred ];\n\tif ( opposite !== preferred && fits( opposite ) ) {\n\t\treturn opposite;\n\t}\n\tif ( preferred !== 'center' && fits( 'center' ) ) {\n\t\treturn 'center';\n\t}\n\t// None of the candidates fully fit \u2014 pick the one that pushes the\n\t// least past the container edge. Without this, the chain falls back\n\t// to `preferred`, which is the off-canvas side that triggered the\n\t// flip in the first place, so the tooltip ends up clipped.\n\tconst overflow = ( side: AxisSide ) => {\n\t\tconst start = sideStart( side, handlePos, length );\n\t\treturn (\n\t\t\tMath.max( 0, -start ) +\n\t\t\tMath.max( 0, start + length - containerLength )\n\t\t);\n\t};\n\tconst candidates: AxisSide[] = [ preferred, opposite, 'center' ];\n\treturn candidates.reduce( ( best, side ) =>\n\t\toverflow( side ) < overflow( best ) ? side : best\n\t);\n}\n\ninterface DimensionsOverlayProps {\n\t/** Crop rectangle in normalized coordinates. */\n\tcropRect: NormalizedRect;\n\t/** Container element dimensions in pixels. */\n\tcontainerSize: Size;\n\t/** Rendered image dimensions in pixels within the container. */\n\timageSize: Size;\n\t/** Currently-dragged handle, or null when no pointer drag is active. */\n\tactiveHandle: HandlePosition | null;\n\t/** Output crop width in source pixels. */\n\toutputWidth: number;\n\t/** Output crop height in source pixels. */\n\toutputHeight: number;\n}\n\n/**\n * Tooltip that follows the active resize handle during a pointer drag,\n * showing the current output dimensions of the crop in source pixels.\n *\n * Renders nothing outside of a pointer-driven resize, so keyboard\n * arrow-key adjustments and pan/zoom gestures stay quiet.\n *\n * @param props\n * @param props.cropRect\n * @param props.containerSize\n * @param props.imageSize\n * @param props.activeHandle\n * @param props.outputWidth\n * @param props.outputHeight\n */\nexport function DimensionsOverlay( {\n\tcropRect,\n\tcontainerSize,\n\timageSize,\n\tactiveHandle,\n\toutputWidth,\n\toutputHeight,\n}: DimensionsOverlayProps ) {\n\tconst tooltipRef = useRef< HTMLDivElement >( null );\n\tconst [ tooltipSize, setTooltipSize ] = useState< Size | null >( null );\n\n\t// Measure the rendered tooltip so we can flip its anchor when it\n\t// would otherwise be clipped by the cropper's `overflow: hidden`\n\t// root. Runs synchronously before paint, so the user never sees\n\t// the un-flipped position. Tracks `outputWidth`/`outputHeight`\n\t// because they drive the digit count, the only thing that changes\n\t// the tooltip's intrinsic size during a drag.\n\tuseLayoutEffect( () => {\n\t\tif ( ! tooltipRef.current ) {\n\t\t\treturn;\n\t\t}\n\t\tconst rect = tooltipRef.current.getBoundingClientRect();\n\t\tsetTooltipSize( ( prev ) => {\n\t\t\tif (\n\t\t\t\tprev &&\n\t\t\t\tprev.width === rect.width &&\n\t\t\t\tprev.height === rect.height\n\t\t\t) {\n\t\t\t\treturn prev;\n\t\t\t}\n\t\t\treturn { width: rect.width, height: rect.height };\n\t\t} );\n\t}, [ outputWidth, outputHeight ] );\n\n\tif (\n\t\t! activeHandle ||\n\t\tcontainerSize.width === 0 ||\n\t\tcontainerSize.height === 0\n\t) {\n\t\treturn null;\n\t}\n\n\tconst offsetX = ( containerSize.width - imageSize.width ) / 2;\n\tconst offsetY = ( containerSize.height - imageSize.height ) / 2;\n\tconst left = offsetX + cropRect.x * imageSize.width;\n\tconst top = offsetY + cropRect.y * imageSize.height;\n\tconst width = cropRect.width * imageSize.width;\n\tconst height = cropRect.height * imageSize.height;\n\n\t// Handle pixel position within the container. Same math as the\n\t// stencil's handle layout, just keyed by direction here so the\n\t// tooltip can anchor to any of the eight.\n\tlet handleX = left + width / 2;\n\tif ( activeHandle.includes( 'w' ) ) {\n\t\thandleX = left;\n\t} else if ( activeHandle.includes( 'e' ) ) {\n\t\thandleX = left + width;\n\t}\n\tlet handleY = top + height / 2;\n\tif ( activeHandle.includes( 'n' ) ) {\n\t\thandleY = top;\n\t} else if ( activeHandle.includes( 's' ) ) {\n\t\thandleY = top + height;\n\t}\n\n\tconst preferred = preferredAnchor( activeHandle );\n\tconst anchor: Anchor = tooltipSize\n\t\t? {\n\t\t\t\tx: fittingSide(\n\t\t\t\t\tpreferred.x,\n\t\t\t\t\thandleX,\n\t\t\t\t\ttooltipSize.width,\n\t\t\t\t\tcontainerSize.width\n\t\t\t\t),\n\t\t\t\ty: fittingSide(\n\t\t\t\t\tpreferred.y,\n\t\t\t\t\thandleY,\n\t\t\t\t\ttooltipSize.height,\n\t\t\t\t\tcontainerSize.height\n\t\t\t\t),\n\t\t }\n\t\t: preferred;\n\n\treturn (\n\t\t<div\n\t\t\tref={ tooltipRef }\n\t\t\tclassName=\"wp-media-editor-image-editor__dimensions-tooltip\"\n\t\t\tdata-testid=\"cropper-dimensions-tooltip\"\n\t\t\tstyle={ {\n\t\t\t\tleft: handleX + GAP_DIRECTION[ anchor.x ] * HANDLE_GAP_PX,\n\t\t\t\ttop: handleY + GAP_DIRECTION[ anchor.y ] * HANDLE_GAP_PX,\n\t\t\t\ttransform: `translate(${ TRANSLATE_PERCENT[ anchor.x ] }, ${\n\t\t\t\t\tTRANSLATE_PERCENT[ an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
|
65
|
+
minCropSize
|
|
65
66
|
}) {
|
|
66
67
|
const boundsMinX = cropBounds?.minX ?? 0;
|
|
67
68
|
const boundsMinY = cropBounds?.minY ?? 0;
|
|
@@ -170,15 +171,22 @@ function RectangleStencil({
|
|
|
170
171
|
el.addEventListener("pointermove", onMove);
|
|
171
172
|
el.addEventListener("pointerup", onEnd);
|
|
172
173
|
el.addEventListener("lostpointercapture", onEnd);
|
|
173
|
-
onResizeStart?.();
|
|
174
|
+
onResizeStart?.(handle);
|
|
174
175
|
clearTimeout(keyboardSettleTimerRef.current);
|
|
175
176
|
keyboardResizeActiveRef.current = false;
|
|
176
177
|
},
|
|
177
178
|
[cropRect, onResizeStart]
|
|
178
179
|
);
|
|
179
180
|
const computeFreeRect = useCallback(
|
|
180
|
-
(drag, clientX, clientY) => computeFreeResizeRect(
|
|
181
|
-
|
|
181
|
+
(drag, clientX, clientY) => computeFreeResizeRect(
|
|
182
|
+
drag,
|
|
183
|
+
clientX,
|
|
184
|
+
clientY,
|
|
185
|
+
imageSize,
|
|
186
|
+
bounds,
|
|
187
|
+
minCropSize
|
|
188
|
+
),
|
|
189
|
+
[imageSize, bounds, minCropSize]
|
|
182
190
|
);
|
|
183
191
|
const computeLockedRect = useCallback(
|
|
184
192
|
(drag, clientX, clientY) => computeLockedResizeRect(
|
|
@@ -187,9 +195,10 @@ function RectangleStencil({
|
|
|
187
195
|
clientY,
|
|
188
196
|
imageSize,
|
|
189
197
|
bounds,
|
|
190
|
-
normalizedRatio
|
|
198
|
+
normalizedRatio,
|
|
199
|
+
minCropSize
|
|
191
200
|
),
|
|
192
|
-
[imageSize, bounds, normalizedRatio]
|
|
201
|
+
[imageSize, bounds, normalizedRatio, minCropSize]
|
|
193
202
|
);
|
|
194
203
|
const computeShiftLockedRect = useCallback(
|
|
195
204
|
(drag, clientX, clientY) => computeShiftLockedResizeRect(
|
|
@@ -197,9 +206,10 @@ function RectangleStencil({
|
|
|
197
206
|
clientX,
|
|
198
207
|
clientY,
|
|
199
208
|
imageSize,
|
|
200
|
-
bounds
|
|
209
|
+
bounds,
|
|
210
|
+
minCropSize
|
|
201
211
|
),
|
|
202
|
-
[imageSize, bounds]
|
|
212
|
+
[imageSize, bounds, minCropSize]
|
|
203
213
|
);
|
|
204
214
|
latestHandlersRef.current = {
|
|
205
215
|
hasLockedRatio,
|
|
@@ -225,7 +235,7 @@ function RectangleStencil({
|
|
|
225
235
|
event.stopPropagation();
|
|
226
236
|
if (!keyboardResizeActiveRef.current) {
|
|
227
237
|
keyboardResizeActiveRef.current = true;
|
|
228
|
-
onResizeStart?.();
|
|
238
|
+
onResizeStart?.(handle);
|
|
229
239
|
}
|
|
230
240
|
const scheduleKeyboardResizeEnd = () => {
|
|
231
241
|
clearTimeout(keyboardSettleTimerRef.current);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../../src/image-editor/react/components/stencils/rectangle-stencil.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tuseCallback,\n\tuseEffect,\n\tuseId,\n\tuseMemo,\n\tuseRef,\n} from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport type { StencilProps, NormalizedRect } from '../../../core/types';\nimport {\n\tDEFAULT_KEYBOARD_STEP,\n\tKEYBOARD_SHIFT_STEP_MULTIPLIER,\n} from '../../../core/constants';\nimport {\n\tcomputeFreeResizeRect,\n\tcomputeLockedResizeRect,\n\tcomputeShiftLockedResizeRect,\n\ttype HandlePosition,\n\ttype CropBounds,\n\ttype ResizeDragState,\n} from '../../../core/stencil-math';\nimport { VISUALLY_HIDDEN_STYLE } from '../../visually-hidden-style';\n\n/**\n * Corner handle positions only \u2014 used when aspect ratio is locked.\n * Ordered clockwise from top-left for logical tab order.\n */\nconst CORNER_POSITIONS: HandlePosition[] = [ 'nw', 'ne', 'se', 'sw' ];\n\n/**\n * All handle positions rendered by the stencil.\n * Ordered clockwise from top-left for logical tab order.\n */\nconst ALL_POSITIONS: HandlePosition[] = [\n\t'nw',\n\t'n',\n\t'ne',\n\t'e',\n\t'se',\n\t's',\n\t'sw',\n\t'w',\n];\n\n/**\n * Get the translated aria-label for a resize handle position.\n *\n * @param pos The handle position identifier.\n * @return The translated label string.\n */\nfunction getHandleLabel( pos: HandlePosition ): string {\n\tswitch ( pos ) {\n\t\tcase 'n':\n\t\t\treturn __( 'Resize top edge' );\n\t\tcase 's':\n\t\t\treturn __( 'Resize bottom edge' );\n\t\tcase 'e':\n\t\t\treturn __( 'Resize right edge' );\n\t\tcase 'w':\n\t\t\treturn __( 'Resize left edge' );\n\t\tcase 'nw':\n\t\t\treturn __( 'Resize top-left corner' );\n\t\tcase 'ne':\n\t\t\treturn __( 'Resize top-right corner' );\n\t\tcase 'sw':\n\t\t\treturn __( 'Resize bottom-left corner' );\n\t\tcase 'se':\n\t\t\treturn __( 'Resize bottom-right corner' );\n\t}\n}\n\n/** Delay before keyboard resize triggers settle (ms). */\nconst KEYBOARD_SETTLE_DELAY = 500;\n\n/**\n * Props for the RectangleStencil component.\n */\ntype RectangleStencilProps = StencilProps;\n\n/**\n * A rectangular crop stencil with resize handles.\n *\n * In freeform mode, handles resize the crop area. Clicks inside the\n * crop pass through to the container for image panning. The crop\n * auto-centers after resize via SETTLE_CROP.\n *\n * @param props Component props implementing StencilProps.\n * @param props.cropRect The crop rectangle in normalized coordinates.\n * @param props.containerSize The container element dimensions in pixels.\n * @param props.imageSize The rendered image dimensions in pixels.\n * @param props.onCropChange Callback fired when the crop rect changes.\n * @param props.onResizeStart Callback fired when a resize drag starts.\n * @param props.onResizeEnd Callback fired when a resize drag ends (mouseup).\n * @param props.aspectRatio Optional fixed aspect ratio (width / height).\n * @param props.freeformCrop Whether resize handles are shown.\n * @param props.stencilTransition CSS transition string for settle animation.\n * @param props.cropBounds Maximum crop rect bounds from camera (zoom/rotation-aware).\n * @param props.onEscape Called when Escape is pressed on a resize handle.\n * @return The rectangle stencil element.\n */\nexport function RectangleStencil( {\n\tcropRect,\n\tcontainerSize,\n\timageSize,\n\tonCropChange,\n\tonResizeStart,\n\tonResizeEnd,\n\taspectRatio,\n\tfreeformCrop = false,\n\tstencilTransition,\n\tcropBounds,\n\tonEscape,\n}: RectangleStencilProps ) {\n\t// Use cropBounds from the camera if available, otherwise default to [0,1].\n\tconst boundsMinX = cropBounds?.minX ?? 0;\n\tconst boundsMinY = cropBounds?.minY ?? 0;\n\tconst boundsMaxX = cropBounds?.maxX ?? 1;\n\tconst boundsMaxY = cropBounds?.maxY ?? 1;\n\tconst bounds: CropBounds = useMemo(\n\t\t() => ( {\n\t\t\tminX: boundsMinX,\n\t\t\tminY: boundsMinY,\n\t\t\tmaxX: boundsMaxX,\n\t\t\tmaxY: boundsMaxY,\n\t\t} ),\n\t\t[ boundsMinX, boundsMinY, boundsMaxX, boundsMaxY ]\n\t);\n\tconst keyboardSettleTimerRef = useRef< ReturnType< typeof setTimeout > >();\n\tconst keyboardResizeActiveRef = useRef( false );\n\tconst resizeHandleDescriptionId = useId();\n\tconst hasLockedRatio = !! ( aspectRatio && aspectRatio > 0 );\n\n\t// Clear the pending keyboard settle timer on unmount so it can't\n\t// fire onResizeEnd / dispatch onto an unmounted parent.\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tclearTimeout( keyboardSettleTimerRef.current );\n\t\t\tkeyboardResizeActiveRef.current = false;\n\t\t};\n\t}, [] );\n\n\t// Latest callbacks for the drag listeners. The drag closure in\n\t// handlePointerDown reads from this ref so it always sees current\n\t// props without having to re-attach listeners mid-drag. Previously,\n\t// drag listeners lived in a useEffect that re-ran whenever `bounds`\n\t// changed (every SET_CROP_RECT dispatch), creating a window where\n\t// pointerup could be missed and the crop would never settle.\n\tconst latestHandlersRef = useRef< {\n\t\thasLockedRatio: boolean;\n\t\tcomputeLockedRect: (\n\t\t\tdrag: ResizeDragState,\n\t\t\tclientX: number,\n\t\t\tclientY: number\n\t\t) => NormalizedRect;\n\t\tcomputeFreeRect: (\n\t\t\tdrag: ResizeDragState,\n\t\t\tclientX: number,\n\t\t\tclientY: number\n\t\t) => NormalizedRect;\n\t\tcomputeShiftLockedRect: (\n\t\t\tdrag: ResizeDragState,\n\t\t\tclientX: number,\n\t\t\tclientY: number\n\t\t) => NormalizedRect;\n\t\tonCropChange: ( rect: NormalizedRect ) => void;\n\t\tonResizeEnd?: () => void;\n\t} | null >( null );\n\n\t// The normalized aspect ratio: the w/h ratio in normalized space that\n\t// produces the desired pixel aspect ratio.\n\t// pixelW = w * imageSize.width, pixelH = h * imageSize.height\n\t// pixelW / pixelH = aspectRatio => w / h = aspectRatio * imageSize.height / imageSize.width\n\tconst normalizedRatio = useMemo( () => {\n\t\tif ( ! hasLockedRatio || imageSize.width === 0 ) {\n\t\t\treturn 0;\n\t\t}\n\t\treturn ( aspectRatio * imageSize.height ) / imageSize.width;\n\t}, [ aspectRatio, hasLockedRatio, imageSize.width, imageSize.height ] );\n\n\t// Convert normalized crop rect to pixel bounds, accounting for the\n\t// image offset within the container.\n\tconst offsetX = ( containerSize.width - imageSize.width ) / 2;\n\tconst offsetY = ( containerSize.height - imageSize.height ) / 2;\n\tconst left = offsetX + cropRect.x * imageSize.width;\n\tconst top = offsetY + cropRect.y * imageSize.height;\n\tconst width = cropRect.width * imageSize.width;\n\tconst height = cropRect.height * imageSize.height;\n\n\t/**\n\t * Start a resize drag on a handle. Registers pointer listeners\n\t * synchronously so no events are missed between capture and the\n\t * next React commit.\n\t */\n\tconst handlePointerDown = useCallback(\n\t\t( handle: HandlePosition, event: React.PointerEvent ) => {\n\t\t\tif ( event.button !== 0 ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tevent.preventDefault();\n\t\t\tevent.stopPropagation();\n\t\t\t// Blur any previously focused handle.\n\t\t\tconst ownerDoc = event.currentTarget.ownerDocument;\n\t\t\tif ( ownerDoc.activeElement instanceof HTMLElement ) {\n\t\t\t\townerDoc.activeElement.blur();\n\t\t\t}\n\t\t\t// Capture pointer so drag works across iframe boundaries.\n\t\t\tconst el = event.currentTarget as HTMLButtonElement;\n\t\t\tel.setPointerCapture( event.pointerId );\n\n\t\t\tconst drag: ResizeDragState = {\n\t\t\t\thandle,\n\t\t\t\tstartX: event.clientX,\n\t\t\t\tstartY: event.clientY,\n\t\t\t\tstartRect: { ...cropRect },\n\t\t\t};\n\n\t\t\t// RAF-throttle: stash the latest pointer position and\n\t\t\t// process it once per animation frame. Avoids dispatching\n\t\t\t// on every pointermove (which fires at much higher than\n\t\t\t// screen refresh rate) and matches the pan-drag path in\n\t\t\t// InteractionController.\n\t\t\tlet rafId = 0;\n\t\t\tlet latestX = event.clientX;\n\t\t\tlet latestY = event.clientY;\n\t\t\tlet latestShift = event.shiftKey;\n\n\t\t\tconst onMove = ( e: Event ) => {\n\t\t\t\tconst pe = e as PointerEvent;\n\t\t\t\tlatestX = pe.clientX;\n\t\t\t\tlatestY = pe.clientY;\n\t\t\t\tlatestShift = pe.shiftKey;\n\t\t\t\tif ( rafId ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\trafId = requestAnimationFrame( () => {\n\t\t\t\t\trafId = 0;\n\t\t\t\t\tconst h = latestHandlersRef.current;\n\t\t\t\t\tif ( ! h ) {\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tlet newRect: NormalizedRect;\n\t\t\t\t\tif ( h.hasLockedRatio ) {\n\t\t\t\t\t\tnewRect = h.computeLockedRect( drag, latestX, latestY );\n\t\t\t\t\t} else if ( latestShift ) {\n\t\t\t\t\t\tnewRect = h.computeShiftLockedRect(\n\t\t\t\t\t\t\tdrag,\n\t\t\t\t\t\t\tlatestX,\n\t\t\t\t\t\t\tlatestY\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tnewRect = h.computeFreeRect( drag, latestX, latestY );\n\t\t\t\t\t}\n\t\t\t\t\th.onCropChange( newRect );\n\t\t\t\t} );\n\t\t\t};\n\n\t\t\t// Guard against duplicate firing: pointerup and\n\t\t\t// lostpointercapture both fire on normal release.\n\t\t\tlet ended = false;\n\t\t\tconst onEnd = () => {\n\t\t\t\tif ( ended ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tended = true;\n\t\t\t\tif ( rafId ) {\n\t\t\t\t\tcancelAnimationFrame( rafId );\n\t\t\t\t\trafId = 0;\n\t\t\t\t}\n\t\t\t\tel.removeEventListener( 'pointermove', onMove );\n\t\t\t\tel.removeEventListener( 'pointerup', onEnd );\n\t\t\t\tel.removeEventListener( 'lostpointercapture', onEnd );\n\t\t\t\tlatestHandlersRef.current?.onResizeEnd?.();\n\t\t\t\t// Restore focus to the handle so arrow keys work\n\t\t\t\t// immediately after a mouse drag. Browsers suppress\n\t\t\t\t// :focus-visible after pointer interactions, so the\n\t\t\t\t// focus ring stays hidden until the user presses a key.\n\t\t\t\tel.focus( { preventScroll: true } );\n\t\t\t};\n\n\t\t\tel.addEventListener( 'pointermove', onMove );\n\t\t\tel.addEventListener( 'pointerup', onEnd );\n\t\t\tel.addEventListener( 'lostpointercapture', onEnd );\n\n\t\t\tonResizeStart?.();\n\t\t\t// Cancel any pending keyboard settle so it can't fire onResizeEnd\n\t\t\t// mid-drag if the user switches from keyboard to pointer within\n\t\t\t// the settle window.\n\t\t\tclearTimeout( keyboardSettleTimerRef.current );\n\t\t\tkeyboardResizeActiveRef.current = false;\n\t\t},\n\t\t[ cropRect, onResizeStart ]\n\t);\n\n\t/**\n\t * Compute the new crop rect for a free (no aspect ratio) resize.\n\t * Delegates to the pure function in core/stencil-math.ts.\n\t */\n\tconst computeFreeRect = useCallback(\n\t\t(\n\t\t\tdrag: ResizeDragState,\n\t\t\tclientX: number,\n\t\t\tclientY: number\n\t\t): NormalizedRect =>\n\t\t\tcomputeFreeResizeRect( drag, clientX, clientY, imageSize, bounds ),\n\t\t[ imageSize, bounds ]\n\t);\n\n\t/**\n\t * Compute the new crop rect for a locked-aspect-ratio corner resize.\n\t * Delegates to the pure function in core/stencil-math.ts.\n\t */\n\tconst computeLockedRect = useCallback(\n\t\t(\n\t\t\tdrag: ResizeDragState,\n\t\t\tclientX: number,\n\t\t\tclientY: number\n\t\t): NormalizedRect =>\n\t\t\tcomputeLockedResizeRect(\n\t\t\t\tdrag,\n\t\t\t\tclientX,\n\t\t\t\tclientY,\n\t\t\t\timageSize,\n\t\t\t\tbounds,\n\t\t\t\tnormalizedRatio\n\t\t\t),\n\t\t[ imageSize, bounds, normalizedRatio ]\n\t);\n\n\t/**\n\t * Compute the new crop rect when Shift is held during a freeform\n\t * resize \u2014 preserves the start rect's aspect ratio.\n\t */\n\tconst computeShiftLockedRect = useCallback(\n\t\t(\n\t\t\tdrag: ResizeDragState,\n\t\t\tclientX: number,\n\t\t\tclientY: number\n\t\t): NormalizedRect =>\n\t\t\tcomputeShiftLockedResizeRect(\n\t\t\t\tdrag,\n\t\t\t\tclientX,\n\t\t\t\tclientY,\n\t\t\t\timageSize,\n\t\t\t\tbounds\n\t\t\t),\n\t\t[ imageSize, bounds ]\n\t);\n\n\tlatestHandlersRef.current = {\n\t\thasLockedRatio,\n\t\tcomputeLockedRect,\n\t\tcomputeFreeRect,\n\t\tcomputeShiftLockedRect,\n\t\tonCropChange,\n\t\tonResizeEnd,\n\t};\n\n\t/**\n\t * Handle keyboard events on a resize handle.\n\t * Arrow keys resize; Escape returns focus to the canvas.\n\t * Shift multiplies the step size by 10 for coarser movement.\n\t */\n\tconst handleKeyDown = useCallback(\n\t\t( handle: HandlePosition, event: React.KeyboardEvent ) => {\n\t\t\tconst key = event.key;\n\n\t\t\tif ( key === 'Escape' ) {\n\t\t\t\tevent.preventDefault();\n\t\t\t\tevent.stopPropagation();\n\t\t\t\tonEscape?.();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tkey !== 'ArrowUp' &&\n\t\t\t\tkey !== 'ArrowDown' &&\n\t\t\t\tkey !== 'ArrowLeft' &&\n\t\t\t\tkey !== 'ArrowRight'\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tevent.preventDefault();\n\t\t\tevent.stopPropagation();\n\n\t\t\tif ( ! keyboardResizeActiveRef.current ) {\n\t\t\t\tkeyboardResizeActiveRef.current = true;\n\t\t\t\tonResizeStart?.();\n\t\t\t}\n\n\t\t\tconst scheduleKeyboardResizeEnd = () => {\n\t\t\t\tclearTimeout( keyboardSettleTimerRef.current );\n\t\t\t\tkeyboardSettleTimerRef.current = setTimeout( () => {\n\t\t\t\t\tkeyboardResizeActiveRef.current = false;\n\t\t\t\t\tonResizeEnd?.();\n\t\t\t\t}, KEYBOARD_SETTLE_DELAY );\n\t\t\t};\n\n\t\t\tconst step = event.shiftKey\n\t\t\t\t? DEFAULT_KEYBOARD_STEP * KEYBOARD_SHIFT_STEP_MULTIPLIER\n\t\t\t\t: DEFAULT_KEYBOARD_STEP;\n\n\t\t\t// Determine the normalized delta from the arrow key.\n\t\t\tlet dx = 0;\n\t\t\tlet dy = 0;\n\t\t\tif ( key === 'ArrowLeft' ) {\n\t\t\t\tdx = -step;\n\t\t\t}\n\t\t\tif ( key === 'ArrowRight' ) {\n\t\t\t\tdx = step;\n\t\t\t}\n\t\t\tif ( key === 'ArrowUp' ) {\n\t\t\t\tdy = -step;\n\t\t\t}\n\t\t\tif ( key === 'ArrowDown' ) {\n\t\t\t\tdy = step;\n\t\t\t}\n\n\t\t\tif ( hasLockedRatio ) {\n\t\t\t\t// For locked aspect ratio, synthesize a drag from the\n\t\t\t\t// current rect and apply the delta via computeLockedRect.\n\t\t\t\tconst syntheticDrag: ResizeDragState = {\n\t\t\t\t\thandle,\n\t\t\t\t\tstartX: 0,\n\t\t\t\t\tstartY: 0,\n\t\t\t\t\tstartRect: { ...cropRect },\n\t\t\t\t};\n\t\t\t\tconst clientX = dx * imageSize.width;\n\t\t\t\tconst clientY = dy * imageSize.height;\n\t\t\t\tonCropChange(\n\t\t\t\t\tcomputeLockedRect( syntheticDrag, clientX, clientY )\n\t\t\t\t);\n\t\t\t\tscheduleKeyboardResizeEnd();\n\t\t\t} else {\n\t\t\t\t// For freeform resize, synthesize a drag via computeFreeRect.\n\t\t\t\tconst syntheticDrag: ResizeDragState = {\n\t\t\t\t\thandle,\n\t\t\t\t\tstartX: 0,\n\t\t\t\t\tstartY: 0,\n\t\t\t\t\tstartRect: { ...cropRect },\n\t\t\t\t};\n\t\t\t\tconst clientX = dx * imageSize.width;\n\t\t\t\tconst clientY = dy * imageSize.height;\n\t\t\t\tonCropChange(\n\t\t\t\t\tcomputeFreeRect( syntheticDrag, clientX, clientY )\n\t\t\t\t);\n\t\t\t\tscheduleKeyboardResizeEnd();\n\t\t\t}\n\t\t},\n\t\t[\n\t\t\tcropRect,\n\t\t\thasLockedRatio,\n\t\t\timageSize.width,\n\t\t\timageSize.height,\n\t\t\tcomputeLockedRect,\n\t\t\tcomputeFreeRect,\n\t\t\tonCropChange,\n\t\t\tonResizeStart,\n\t\t\tonResizeEnd,\n\t\t\tonEscape,\n\t\t]\n\t);\n\n\tif ( containerSize.width === 0 || containerSize.height === 0 ) {\n\t\treturn null;\n\t}\n\n\tconst handles = hasLockedRatio ? CORNER_POSITIONS : ALL_POSITIONS;\n\n\treturn (\n\t\t<div\n\t\t\tclassName=\"wp-media-editor-image-editor__stencil\"\n\t\t\tstyle={ {\n\t\t\t\tleft,\n\t\t\t\ttop,\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\ttransition: stencilTransition,\n\t\t\t} }\n\t\t>\n\t\t\t{ freeformCrop && (\n\t\t\t\t<div\n\t\t\t\t\tid={ resizeHandleDescriptionId }\n\t\t\t\t\tstyle={ VISUALLY_HIDDEN_STYLE }\n\t\t\t\t>\n\t\t\t\t\t{ __(\n\t\t\t\t\t\t'Use arrow keys to resize the crop area. Hold Shift for larger steps.'\n\t\t\t\t\t) }\n\t\t\t\t</div>\n\t\t\t) }\n\t\t\t{ /* The crop rectangle border. pointer-events: none is set in\n\t\t\t\t CSS so clicks pass through to the container for panning. */ }\n\t\t\t<div\n\t\t\t\tclassName=\"wp-media-editor-image-editor__stencil-rect\"\n\t\t\t\tstyle={ {\n\t\t\t\t\twidth: '100%',\n\t\t\t\t\theight: '100%',\n\t\t\t\t\ttop: 0,\n\t\t\t\t\tleft: 0,\n\t\t\t\t} }\n\t\t\t/>\n\t\t\t{ /* Resize handles \u2014 only in freeform mode.\n\t\t\t\t Semantics: role=\"button\" with aria-label describing\n\t\t\t\t which edge/corner. Arrow keys resize (onKeyDown).\n\t\t\t\t role=\"slider\" would be more precise but requires\n\t\t\t\t per-handle aria-valuemin/max/now which don't map\n\t\t\t\t cleanly to a 2D crop rect. */ }\n\t\t\t{ freeformCrop &&\n\t\t\t\thandles.map( ( pos ) => (\n\t\t\t\t\t<button\n\t\t\t\t\t\tkey={ pos }\n\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\tclassName={ `wp-media-editor-image-editor__handle wp-media-editor-image-editor__handle--${ pos }` }\n\t\t\t\t\t\tonPointerDown={ ( event ) =>\n\t\t\t\t\t\t\thandlePointerDown( pos, event )\n\t\t\t\t\t\t}\n\t\t\t\t\t\tonTouchStart={ ( event ) => event.stopPropagation() }\n\t\t\t\t\t\tonKeyDown={ ( event ) => handleKeyDown( pos, event ) }\n\t\t\t\t\t\taria-label={ getHandleLabel( pos ) }\n\t\t\t\t\t\taria-describedby={ resizeHandleDescriptionId }\n\t\t\t\t\t/>\n\t\t\t\t) ) }\n\t\t</div>\n\t);\n}\n"],
|
|
5
|
-
"mappings": ";AAGA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,UAAU;AAMnB;AAAA,EACC;AAAA,EACA;AAAA,OACM;AACP;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OAIM;AACP,SAAS,6BAA6B;
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tuseCallback,\n\tuseEffect,\n\tuseId,\n\tuseMemo,\n\tuseRef,\n} from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport type { StencilProps, NormalizedRect } from '../../../core/types';\nimport {\n\tDEFAULT_KEYBOARD_STEP,\n\tKEYBOARD_SHIFT_STEP_MULTIPLIER,\n} from '../../../core/constants';\nimport {\n\tcomputeFreeResizeRect,\n\tcomputeLockedResizeRect,\n\tcomputeShiftLockedResizeRect,\n\ttype HandlePosition,\n\ttype CropBounds,\n\ttype ResizeDragState,\n} from '../../../core/stencil-math';\nimport { VISUALLY_HIDDEN_STYLE } from '../../visually-hidden-style';\n\n/**\n * Corner handle positions only \u2014 used when aspect ratio is locked.\n * Ordered clockwise from top-left for logical tab order.\n */\nconst CORNER_POSITIONS: HandlePosition[] = [ 'nw', 'ne', 'se', 'sw' ];\n\n/**\n * All handle positions rendered by the stencil.\n * Ordered clockwise from top-left for logical tab order.\n */\nconst ALL_POSITIONS: HandlePosition[] = [\n\t'nw',\n\t'n',\n\t'ne',\n\t'e',\n\t'se',\n\t's',\n\t'sw',\n\t'w',\n];\n\n/**\n * Get the translated aria-label for a resize handle position.\n *\n * @param pos The handle position identifier.\n * @return The translated label string.\n */\nfunction getHandleLabel( pos: HandlePosition ): string {\n\tswitch ( pos ) {\n\t\tcase 'n':\n\t\t\treturn __( 'Resize top edge' );\n\t\tcase 's':\n\t\t\treturn __( 'Resize bottom edge' );\n\t\tcase 'e':\n\t\t\treturn __( 'Resize right edge' );\n\t\tcase 'w':\n\t\t\treturn __( 'Resize left edge' );\n\t\tcase 'nw':\n\t\t\treturn __( 'Resize top-left corner' );\n\t\tcase 'ne':\n\t\t\treturn __( 'Resize top-right corner' );\n\t\tcase 'sw':\n\t\t\treturn __( 'Resize bottom-left corner' );\n\t\tcase 'se':\n\t\t\treturn __( 'Resize bottom-right corner' );\n\t}\n}\n\n/** Delay before keyboard resize triggers settle (ms). */\nconst KEYBOARD_SETTLE_DELAY = 500;\n\n/**\n * Props for the RectangleStencil component.\n */\ntype RectangleStencilProps = StencilProps;\n\n/**\n * A rectangular crop stencil with resize handles.\n *\n * In freeform mode, handles resize the crop area. Clicks inside the\n * crop pass through to the container for image panning. The crop\n * auto-centers after resize via SETTLE_CROP.\n *\n * @param props Component props implementing StencilProps.\n * @param props.cropRect The crop rectangle in normalized coordinates.\n * @param props.containerSize The container element dimensions in pixels.\n * @param props.imageSize The rendered image dimensions in pixels.\n * @param props.onCropChange Callback fired when the crop rect changes.\n * @param props.onResizeStart Callback fired when a resize drag starts.\n * @param props.onResizeEnd Callback fired when a resize drag ends (mouseup).\n * @param props.aspectRatio Optional fixed aspect ratio (width / height).\n * @param props.freeformCrop Whether resize handles are shown.\n * @param props.stencilTransition CSS transition string for settle animation.\n * @param props.cropBounds Maximum crop rect bounds from camera (zoom/rotation-aware).\n * @param props.onEscape Called when Escape is pressed on a resize handle.\n * @param props.minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @return The rectangle stencil element.\n */\nexport function RectangleStencil( {\n\tcropRect,\n\tcontainerSize,\n\timageSize,\n\tonCropChange,\n\tonResizeStart,\n\tonResizeEnd,\n\taspectRatio,\n\tfreeformCrop = false,\n\tstencilTransition,\n\tcropBounds,\n\tonEscape,\n\tminCropSize,\n}: RectangleStencilProps ) {\n\t// Use cropBounds from the camera if available, otherwise default to [0,1].\n\tconst boundsMinX = cropBounds?.minX ?? 0;\n\tconst boundsMinY = cropBounds?.minY ?? 0;\n\tconst boundsMaxX = cropBounds?.maxX ?? 1;\n\tconst boundsMaxY = cropBounds?.maxY ?? 1;\n\tconst bounds: CropBounds = useMemo(\n\t\t() => ( {\n\t\t\tminX: boundsMinX,\n\t\t\tminY: boundsMinY,\n\t\t\tmaxX: boundsMaxX,\n\t\t\tmaxY: boundsMaxY,\n\t\t} ),\n\t\t[ boundsMinX, boundsMinY, boundsMaxX, boundsMaxY ]\n\t);\n\tconst keyboardSettleTimerRef = useRef< ReturnType< typeof setTimeout > >();\n\tconst keyboardResizeActiveRef = useRef( false );\n\tconst resizeHandleDescriptionId = useId();\n\tconst hasLockedRatio = !! ( aspectRatio && aspectRatio > 0 );\n\n\t// Clear the pending keyboard settle timer on unmount so it can't\n\t// fire onResizeEnd / dispatch onto an unmounted parent.\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tclearTimeout( keyboardSettleTimerRef.current );\n\t\t\tkeyboardResizeActiveRef.current = false;\n\t\t};\n\t}, [] );\n\n\t// Latest callbacks for the drag listeners. The drag closure in\n\t// handlePointerDown reads from this ref so it always sees current\n\t// props without having to re-attach listeners mid-drag. Previously,\n\t// drag listeners lived in a useEffect that re-ran whenever `bounds`\n\t// changed (every SET_CROP_RECT dispatch), creating a window where\n\t// pointerup could be missed and the crop would never settle.\n\tconst latestHandlersRef = useRef< {\n\t\thasLockedRatio: boolean;\n\t\tcomputeLockedRect: (\n\t\t\tdrag: ResizeDragState,\n\t\t\tclientX: number,\n\t\t\tclientY: number\n\t\t) => NormalizedRect;\n\t\tcomputeFreeRect: (\n\t\t\tdrag: ResizeDragState,\n\t\t\tclientX: number,\n\t\t\tclientY: number\n\t\t) => NormalizedRect;\n\t\tcomputeShiftLockedRect: (\n\t\t\tdrag: ResizeDragState,\n\t\t\tclientX: number,\n\t\t\tclientY: number\n\t\t) => NormalizedRect;\n\t\tonCropChange: ( rect: NormalizedRect ) => void;\n\t\tonResizeEnd?: () => void;\n\t} | null >( null );\n\n\t// The normalized aspect ratio: the w/h ratio in normalized space that\n\t// produces the desired pixel aspect ratio.\n\t// pixelW = w * imageSize.width, pixelH = h * imageSize.height\n\t// pixelW / pixelH = aspectRatio => w / h = aspectRatio * imageSize.height / imageSize.width\n\tconst normalizedRatio = useMemo( () => {\n\t\tif ( ! hasLockedRatio || imageSize.width === 0 ) {\n\t\t\treturn 0;\n\t\t}\n\t\treturn ( aspectRatio * imageSize.height ) / imageSize.width;\n\t}, [ aspectRatio, hasLockedRatio, imageSize.width, imageSize.height ] );\n\n\t// Convert normalized crop rect to pixel bounds, accounting for the\n\t// image offset within the container.\n\tconst offsetX = ( containerSize.width - imageSize.width ) / 2;\n\tconst offsetY = ( containerSize.height - imageSize.height ) / 2;\n\tconst left = offsetX + cropRect.x * imageSize.width;\n\tconst top = offsetY + cropRect.y * imageSize.height;\n\tconst width = cropRect.width * imageSize.width;\n\tconst height = cropRect.height * imageSize.height;\n\n\t/**\n\t * Start a resize drag on a handle. Registers pointer listeners\n\t * synchronously so no events are missed between capture and the\n\t * next React commit.\n\t */\n\tconst handlePointerDown = useCallback(\n\t\t( handle: HandlePosition, event: React.PointerEvent ) => {\n\t\t\tif ( event.button !== 0 ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tevent.preventDefault();\n\t\t\tevent.stopPropagation();\n\t\t\t// Blur any previously focused handle.\n\t\t\tconst ownerDoc = event.currentTarget.ownerDocument;\n\t\t\tif ( ownerDoc.activeElement instanceof HTMLElement ) {\n\t\t\t\townerDoc.activeElement.blur();\n\t\t\t}\n\t\t\t// Capture pointer so drag works across iframe boundaries.\n\t\t\tconst el = event.currentTarget as HTMLButtonElement;\n\t\t\tel.setPointerCapture( event.pointerId );\n\n\t\t\tconst drag: ResizeDragState = {\n\t\t\t\thandle,\n\t\t\t\tstartX: event.clientX,\n\t\t\t\tstartY: event.clientY,\n\t\t\t\tstartRect: { ...cropRect },\n\t\t\t};\n\n\t\t\t// RAF-throttle: stash the latest pointer position and\n\t\t\t// process it once per animation frame. Avoids dispatching\n\t\t\t// on every pointermove (which fires at much higher than\n\t\t\t// screen refresh rate) and matches the pan-drag path in\n\t\t\t// InteractionController.\n\t\t\tlet rafId = 0;\n\t\t\tlet latestX = event.clientX;\n\t\t\tlet latestY = event.clientY;\n\t\t\tlet latestShift = event.shiftKey;\n\n\t\t\tconst onMove = ( e: Event ) => {\n\t\t\t\tconst pe = e as PointerEvent;\n\t\t\t\tlatestX = pe.clientX;\n\t\t\t\tlatestY = pe.clientY;\n\t\t\t\tlatestShift = pe.shiftKey;\n\t\t\t\tif ( rafId ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\trafId = requestAnimationFrame( () => {\n\t\t\t\t\trafId = 0;\n\t\t\t\t\tconst h = latestHandlersRef.current;\n\t\t\t\t\tif ( ! h ) {\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tlet newRect: NormalizedRect;\n\t\t\t\t\tif ( h.hasLockedRatio ) {\n\t\t\t\t\t\tnewRect = h.computeLockedRect( drag, latestX, latestY );\n\t\t\t\t\t} else if ( latestShift ) {\n\t\t\t\t\t\tnewRect = h.computeShiftLockedRect(\n\t\t\t\t\t\t\tdrag,\n\t\t\t\t\t\t\tlatestX,\n\t\t\t\t\t\t\tlatestY\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tnewRect = h.computeFreeRect( drag, latestX, latestY );\n\t\t\t\t\t}\n\t\t\t\t\th.onCropChange( newRect );\n\t\t\t\t} );\n\t\t\t};\n\n\t\t\t// Guard against duplicate firing: pointerup and\n\t\t\t// lostpointercapture both fire on normal release.\n\t\t\tlet ended = false;\n\t\t\tconst onEnd = () => {\n\t\t\t\tif ( ended ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tended = true;\n\t\t\t\tif ( rafId ) {\n\t\t\t\t\tcancelAnimationFrame( rafId );\n\t\t\t\t\trafId = 0;\n\t\t\t\t}\n\t\t\t\tel.removeEventListener( 'pointermove', onMove );\n\t\t\t\tel.removeEventListener( 'pointerup', onEnd );\n\t\t\t\tel.removeEventListener( 'lostpointercapture', onEnd );\n\t\t\t\tlatestHandlersRef.current?.onResizeEnd?.();\n\t\t\t\t// Restore focus to the handle so arrow keys work\n\t\t\t\t// immediately after a mouse drag. Browsers suppress\n\t\t\t\t// :focus-visible after pointer interactions, so the\n\t\t\t\t// focus ring stays hidden until the user presses a key.\n\t\t\t\tel.focus( { preventScroll: true } );\n\t\t\t};\n\n\t\t\tel.addEventListener( 'pointermove', onMove );\n\t\t\tel.addEventListener( 'pointerup', onEnd );\n\t\t\tel.addEventListener( 'lostpointercapture', onEnd );\n\n\t\t\tonResizeStart?.( handle );\n\t\t\t// Cancel any pending keyboard settle so it can't fire onResizeEnd\n\t\t\t// mid-drag if the user switches from keyboard to pointer within\n\t\t\t// the settle window.\n\t\t\tclearTimeout( keyboardSettleTimerRef.current );\n\t\t\tkeyboardResizeActiveRef.current = false;\n\t\t},\n\t\t[ cropRect, onResizeStart ]\n\t);\n\n\t/**\n\t * Compute the new crop rect for a free (no aspect ratio) resize.\n\t * Delegates to the pure function in core/stencil-math.ts.\n\t */\n\tconst computeFreeRect = useCallback(\n\t\t(\n\t\t\tdrag: ResizeDragState,\n\t\t\tclientX: number,\n\t\t\tclientY: number\n\t\t): NormalizedRect =>\n\t\t\tcomputeFreeResizeRect(\n\t\t\t\tdrag,\n\t\t\t\tclientX,\n\t\t\t\tclientY,\n\t\t\t\timageSize,\n\t\t\t\tbounds,\n\t\t\t\tminCropSize\n\t\t\t),\n\t\t[ imageSize, bounds, minCropSize ]\n\t);\n\n\t/**\n\t * Compute the new crop rect for a locked-aspect-ratio corner resize.\n\t * Delegates to the pure function in core/stencil-math.ts.\n\t */\n\tconst computeLockedRect = useCallback(\n\t\t(\n\t\t\tdrag: ResizeDragState,\n\t\t\tclientX: number,\n\t\t\tclientY: number\n\t\t): NormalizedRect =>\n\t\t\tcomputeLockedResizeRect(\n\t\t\t\tdrag,\n\t\t\t\tclientX,\n\t\t\t\tclientY,\n\t\t\t\timageSize,\n\t\t\t\tbounds,\n\t\t\t\tnormalizedRatio,\n\t\t\t\tminCropSize\n\t\t\t),\n\t\t[ imageSize, bounds, normalizedRatio, minCropSize ]\n\t);\n\n\t/**\n\t * Compute the new crop rect when Shift is held during a freeform\n\t * resize \u2014 preserves the start rect's aspect ratio.\n\t */\n\tconst computeShiftLockedRect = useCallback(\n\t\t(\n\t\t\tdrag: ResizeDragState,\n\t\t\tclientX: number,\n\t\t\tclientY: number\n\t\t): NormalizedRect =>\n\t\t\tcomputeShiftLockedResizeRect(\n\t\t\t\tdrag,\n\t\t\t\tclientX,\n\t\t\t\tclientY,\n\t\t\t\timageSize,\n\t\t\t\tbounds,\n\t\t\t\tminCropSize\n\t\t\t),\n\t\t[ imageSize, bounds, minCropSize ]\n\t);\n\n\tlatestHandlersRef.current = {\n\t\thasLockedRatio,\n\t\tcomputeLockedRect,\n\t\tcomputeFreeRect,\n\t\tcomputeShiftLockedRect,\n\t\tonCropChange,\n\t\tonResizeEnd,\n\t};\n\n\t/**\n\t * Handle keyboard events on a resize handle.\n\t * Arrow keys resize; Escape returns focus to the canvas.\n\t * Shift multiplies the step size by 10 for coarser movement.\n\t */\n\tconst handleKeyDown = useCallback(\n\t\t( handle: HandlePosition, event: React.KeyboardEvent ) => {\n\t\t\tconst key = event.key;\n\n\t\t\tif ( key === 'Escape' ) {\n\t\t\t\tevent.preventDefault();\n\t\t\t\tevent.stopPropagation();\n\t\t\t\tonEscape?.();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tkey !== 'ArrowUp' &&\n\t\t\t\tkey !== 'ArrowDown' &&\n\t\t\t\tkey !== 'ArrowLeft' &&\n\t\t\t\tkey !== 'ArrowRight'\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tevent.preventDefault();\n\t\t\tevent.stopPropagation();\n\n\t\t\tif ( ! keyboardResizeActiveRef.current ) {\n\t\t\t\tkeyboardResizeActiveRef.current = true;\n\t\t\t\tonResizeStart?.( handle );\n\t\t\t}\n\n\t\t\tconst scheduleKeyboardResizeEnd = () => {\n\t\t\t\tclearTimeout( keyboardSettleTimerRef.current );\n\t\t\t\tkeyboardSettleTimerRef.current = setTimeout( () => {\n\t\t\t\t\tkeyboardResizeActiveRef.current = false;\n\t\t\t\t\tonResizeEnd?.();\n\t\t\t\t}, KEYBOARD_SETTLE_DELAY );\n\t\t\t};\n\n\t\t\tconst step = event.shiftKey\n\t\t\t\t? DEFAULT_KEYBOARD_STEP * KEYBOARD_SHIFT_STEP_MULTIPLIER\n\t\t\t\t: DEFAULT_KEYBOARD_STEP;\n\n\t\t\t// Determine the normalized delta from the arrow key.\n\t\t\tlet dx = 0;\n\t\t\tlet dy = 0;\n\t\t\tif ( key === 'ArrowLeft' ) {\n\t\t\t\tdx = -step;\n\t\t\t}\n\t\t\tif ( key === 'ArrowRight' ) {\n\t\t\t\tdx = step;\n\t\t\t}\n\t\t\tif ( key === 'ArrowUp' ) {\n\t\t\t\tdy = -step;\n\t\t\t}\n\t\t\tif ( key === 'ArrowDown' ) {\n\t\t\t\tdy = step;\n\t\t\t}\n\n\t\t\tif ( hasLockedRatio ) {\n\t\t\t\t// For locked aspect ratio, synthesize a drag from the\n\t\t\t\t// current rect and apply the delta via computeLockedRect.\n\t\t\t\tconst syntheticDrag: ResizeDragState = {\n\t\t\t\t\thandle,\n\t\t\t\t\tstartX: 0,\n\t\t\t\t\tstartY: 0,\n\t\t\t\t\tstartRect: { ...cropRect },\n\t\t\t\t};\n\t\t\t\tconst clientX = dx * imageSize.width;\n\t\t\t\tconst clientY = dy * imageSize.height;\n\t\t\t\tonCropChange(\n\t\t\t\t\tcomputeLockedRect( syntheticDrag, clientX, clientY )\n\t\t\t\t);\n\t\t\t\tscheduleKeyboardResizeEnd();\n\t\t\t} else {\n\t\t\t\t// For freeform resize, synthesize a drag via computeFreeRect.\n\t\t\t\tconst syntheticDrag: ResizeDragState = {\n\t\t\t\t\thandle,\n\t\t\t\t\tstartX: 0,\n\t\t\t\t\tstartY: 0,\n\t\t\t\t\tstartRect: { ...cropRect },\n\t\t\t\t};\n\t\t\t\tconst clientX = dx * imageSize.width;\n\t\t\t\tconst clientY = dy * imageSize.height;\n\t\t\t\tonCropChange(\n\t\t\t\t\tcomputeFreeRect( syntheticDrag, clientX, clientY )\n\t\t\t\t);\n\t\t\t\tscheduleKeyboardResizeEnd();\n\t\t\t}\n\t\t},\n\t\t[\n\t\t\tcropRect,\n\t\t\thasLockedRatio,\n\t\t\timageSize.width,\n\t\t\timageSize.height,\n\t\t\tcomputeLockedRect,\n\t\t\tcomputeFreeRect,\n\t\t\tonCropChange,\n\t\t\tonResizeStart,\n\t\t\tonResizeEnd,\n\t\t\tonEscape,\n\t\t]\n\t);\n\n\tif ( containerSize.width === 0 || containerSize.height === 0 ) {\n\t\treturn null;\n\t}\n\n\tconst handles = hasLockedRatio ? CORNER_POSITIONS : ALL_POSITIONS;\n\n\treturn (\n\t\t<div\n\t\t\tclassName=\"wp-media-editor-image-editor__stencil\"\n\t\t\tstyle={ {\n\t\t\t\tleft,\n\t\t\t\ttop,\n\t\t\t\twidth,\n\t\t\t\theight,\n\t\t\t\ttransition: stencilTransition,\n\t\t\t} }\n\t\t>\n\t\t\t{ freeformCrop && (\n\t\t\t\t<div\n\t\t\t\t\tid={ resizeHandleDescriptionId }\n\t\t\t\t\tstyle={ VISUALLY_HIDDEN_STYLE }\n\t\t\t\t>\n\t\t\t\t\t{ __(\n\t\t\t\t\t\t'Use arrow keys to resize the crop area. Hold Shift for larger steps.'\n\t\t\t\t\t) }\n\t\t\t\t</div>\n\t\t\t) }\n\t\t\t{ /* The crop rectangle border. pointer-events: none is set in\n\t\t\t\t CSS so clicks pass through to the container for panning. */ }\n\t\t\t<div\n\t\t\t\tclassName=\"wp-media-editor-image-editor__stencil-rect\"\n\t\t\t\tstyle={ {\n\t\t\t\t\twidth: '100%',\n\t\t\t\t\theight: '100%',\n\t\t\t\t\ttop: 0,\n\t\t\t\t\tleft: 0,\n\t\t\t\t} }\n\t\t\t/>\n\t\t\t{ /* Resize handles \u2014 only in freeform mode.\n\t\t\t\t Semantics: role=\"button\" with aria-label describing\n\t\t\t\t which edge/corner. Arrow keys resize (onKeyDown).\n\t\t\t\t role=\"slider\" would be more precise but requires\n\t\t\t\t per-handle aria-valuemin/max/now which don't map\n\t\t\t\t cleanly to a 2D crop rect. */ }\n\t\t\t{ freeformCrop &&\n\t\t\t\thandles.map( ( pos ) => (\n\t\t\t\t\t<button\n\t\t\t\t\t\tkey={ pos }\n\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\tclassName={ `wp-media-editor-image-editor__handle wp-media-editor-image-editor__handle--${ pos }` }\n\t\t\t\t\t\tonPointerDown={ ( event ) =>\n\t\t\t\t\t\t\thandlePointerDown( pos, event )\n\t\t\t\t\t\t}\n\t\t\t\t\t\tonTouchStart={ ( event ) => event.stopPropagation() }\n\t\t\t\t\t\tonKeyDown={ ( event ) => handleKeyDown( pos, event ) }\n\t\t\t\t\t\taria-label={ getHandleLabel( pos ) }\n\t\t\t\t\t\taria-describedby={ resizeHandleDescriptionId }\n\t\t\t\t\t/>\n\t\t\t\t) ) }\n\t\t</div>\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";AAGA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,UAAU;AAMnB;AAAA,EACC;AAAA,EACA;AAAA,OACM;AACP;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OAIM;AACP,SAAS,6BAA6B;AA4cpC,SAWE,KAXF;AAtcF,IAAM,mBAAqC,CAAE,MAAM,MAAM,MAAM,IAAK;AAMpE,IAAM,gBAAkC;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AAQA,SAAS,eAAgB,KAA8B;AACtD,UAAS,KAAM;AAAA,IACd,KAAK;AACJ,aAAO,GAAI,iBAAkB;AAAA,IAC9B,KAAK;AACJ,aAAO,GAAI,oBAAqB;AAAA,IACjC,KAAK;AACJ,aAAO,GAAI,mBAAoB;AAAA,IAChC,KAAK;AACJ,aAAO,GAAI,kBAAmB;AAAA,IAC/B,KAAK;AACJ,aAAO,GAAI,wBAAyB;AAAA,IACrC,KAAK;AACJ,aAAO,GAAI,yBAA0B;AAAA,IACtC,KAAK;AACJ,aAAO,GAAI,2BAA4B;AAAA,IACxC,KAAK;AACJ,aAAO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IACD;AAAA,IACA;AAAA,MACC;AAAA,MACA;AAAA,MACA,UAAU;AAAA,MACV,UAAU;AAAA,MACV;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAEA,MAAK,cAAc,UAAU,KAAK,cAAc,WAAW,GAAI;AAC9D,WAAO;AAAA,EACR;AAEA,QAAM,UAAU,iBAAiB,mBAAmB;AAEpD,SACC;AAAA,IAAC;AAAA;AAAA,MACA,WAAU;AAAA,MACV,OAAQ;AAAA,QACP;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,YAAY;AAAA,MACb;AAAA,MAEE;AAAA,wBACD;AAAA,UAAC;AAAA;AAAA,YACA,IAAK;AAAA,YACL,OAAQ;AAAA,YAEN;AAAA,cACD;AAAA,YACD;AAAA;AAAA,QACD;AAAA,QAID;AAAA,UAAC;AAAA;AAAA,YACA,WAAU;AAAA,YACV,OAAQ;AAAA,cACP,OAAO;AAAA,cACP,QAAQ;AAAA,cACR,KAAK;AAAA,cACL,MAAM;AAAA,YACP;AAAA;AAAA,QACD;AAAA,QAOE,gBACD,QAAQ,IAAK,CAAE,QACd;AAAA,UAAC;AAAA;AAAA,YAEA,MAAK;AAAA,YACL,WAAY,8EAA+E,GAAI;AAAA,YAC/F,eAAgB,CAAE,UACjB,kBAAmB,KAAK,KAAM;AAAA,YAE/B,cAAe,CAAE,UAAW,MAAM,gBAAgB;AAAA,YAClD,WAAY,CAAE,UAAW,cAAe,KAAK,KAAM;AAAA,YACnD,cAAa,eAAgB,GAAI;AAAA,YACjC,oBAAmB;AAAA;AAAA,UATb;AAAA,QAUP,CACC;AAAA;AAAA;AAAA,EACJ;AAEF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// packages/media-editor/src/image-editor/react/hooks/build-cropper-setters.ts
|
|
2
|
+
import { buildFocalPointZoomAction } from "../../core/setter-helpers.mjs";
|
|
3
|
+
function buildCropperSetters(dispatchCropperAction, getCropperState) {
|
|
4
|
+
return {
|
|
5
|
+
setPan: (pan) => dispatchCropperAction({ type: "SET_PAN", payload: pan }),
|
|
6
|
+
setZoom: (zoom) => {
|
|
7
|
+
const action = buildFocalPointZoomAction(getCropperState(), zoom);
|
|
8
|
+
if (action) {
|
|
9
|
+
dispatchCropperAction(action);
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
setZoomAtPoint: (zoom, pan) => dispatchCropperAction({
|
|
13
|
+
type: "SET_ZOOM_AT_POINT",
|
|
14
|
+
payload: { zoom, pan }
|
|
15
|
+
}),
|
|
16
|
+
setRotation: (rotation) => dispatchCropperAction({
|
|
17
|
+
type: "SET_ROTATION",
|
|
18
|
+
payload: rotation
|
|
19
|
+
}),
|
|
20
|
+
setFlip: (flip) => dispatchCropperAction({ type: "SET_FLIP", payload: flip }),
|
|
21
|
+
toggleFlip: (direction) => {
|
|
22
|
+
const { flip } = getCropperState();
|
|
23
|
+
dispatchCropperAction({
|
|
24
|
+
type: "SET_FLIP",
|
|
25
|
+
payload: { ...flip, [direction]: !flip[direction] }
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
snapRotate90: (direction) => dispatchCropperAction({
|
|
29
|
+
type: "SNAP_ROTATE_90",
|
|
30
|
+
payload: { direction }
|
|
31
|
+
}),
|
|
32
|
+
setCropRect: (rect) => dispatchCropperAction({
|
|
33
|
+
type: "SET_CROP_RECT",
|
|
34
|
+
payload: rect
|
|
35
|
+
}),
|
|
36
|
+
settleCrop: () => dispatchCropperAction({ type: "SETTLE_CROP" }),
|
|
37
|
+
applyOperation: (op) => dispatchCropperAction({
|
|
38
|
+
type: "APPLY_OPERATION",
|
|
39
|
+
payload: op
|
|
40
|
+
})
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export {
|
|
44
|
+
buildCropperSetters
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=build-cropper-setters.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/image-editor/react/hooks/build-cropper-setters.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport type {\n\tCropperAction,\n\tCropperState,\n\tFlip,\n\tNormalizedPoint,\n\tNormalizedRect,\n\tTransformOperation,\n} from '../../core/types';\nimport { buildFocalPointZoomAction } from '../../core/setter-helpers';\n\n/**\n * The named cropper setters shared by every hook that drives the\n * cropper reducer \u2014 the pure `useCropperReducer` and any composite\n * store that delegates cropper actions (e.g. `useMediaEditorState`).\n *\n * `setImage` and `reset` are intentionally NOT included: each hook\n * has additional bookkeeping for them (dirty-baseline, history clear)\n * that doesn't fit a shared factory.\n */\nexport interface CropperSetters {\n\tsetPan: ( pan: NormalizedPoint ) => void;\n\tsetZoom: ( zoom: number ) => void;\n\tsetZoomAtPoint: ( zoom: number, pan: NormalizedPoint ) => void;\n\tsetRotation: ( rotation: number ) => void;\n\tsetFlip: ( flip: Flip ) => void;\n\ttoggleFlip: ( direction: 'horizontal' | 'vertical' ) => void;\n\tsnapRotate90: ( direction: 1 | -1 ) => void;\n\tsetCropRect: ( rect: NormalizedRect ) => void;\n\tsettleCrop: () => void;\n\tapplyOperation: ( op: TransformOperation ) => void;\n}\n\n/**\n * Build the named cropper setters from a dispatch function and a\n * state getter. The two consuming hooks differ in how they dispatch:\n *\n * - `useCropperReducer` dispatches directly into its `useReducer`.\n * - `useMediaEditorState` wraps cropper actions in a composite\n * `CROPPER` envelope so the history machinery can record them.\n *\n * Both pass their own dispatch function and a getter that returns\n * the current `CropperState`. The factory shape keeps the two hooks\n * in lockstep \u2014 add a setter here once, both hooks get it.\n *\n * @param dispatchCropperAction Apply a `CropperAction` to the\n * underlying reducer.\n * @param getCropperState Return the current `CropperState`.\n * Used by setters that need to read\n * the latest state to compute their\n * payload (focal-point zoom, flip toggle).\n * @return The named cropper setters.\n */\nexport function buildCropperSetters(\n\tdispatchCropperAction: ( action: CropperAction ) => void,\n\tgetCropperState: () => CropperState\n): CropperSetters {\n\treturn {\n\t\tsetPan: ( pan ) =>\n\t\t\tdispatchCropperAction( { type: 'SET_PAN', payload: pan } ),\n\t\tsetZoom: ( zoom ) => {\n\t\t\tconst action = buildFocalPointZoomAction( getCropperState(), zoom );\n\t\t\tif ( action ) {\n\t\t\t\tdispatchCropperAction( action );\n\t\t\t}\n\t\t},\n\t\tsetZoomAtPoint: ( zoom, pan ) =>\n\t\t\tdispatchCropperAction( {\n\t\t\t\ttype: 'SET_ZOOM_AT_POINT',\n\t\t\t\tpayload: { zoom, pan },\n\t\t\t} ),\n\t\tsetRotation: ( rotation ) =>\n\t\t\tdispatchCropperAction( {\n\t\t\t\ttype: 'SET_ROTATION',\n\t\t\t\tpayload: rotation,\n\t\t\t} ),\n\t\tsetFlip: ( flip ) =>\n\t\t\tdispatchCropperAction( { type: 'SET_FLIP', payload: flip } ),\n\t\ttoggleFlip: ( direction ) => {\n\t\t\tconst { flip } = getCropperState();\n\t\t\tdispatchCropperAction( {\n\t\t\t\ttype: 'SET_FLIP',\n\t\t\t\tpayload: { ...flip, [ direction ]: ! flip[ direction ] },\n\t\t\t} );\n\t\t},\n\t\tsnapRotate90: ( direction ) =>\n\t\t\tdispatchCropperAction( {\n\t\t\t\ttype: 'SNAP_ROTATE_90',\n\t\t\t\tpayload: { direction },\n\t\t\t} ),\n\t\tsetCropRect: ( rect ) =>\n\t\t\tdispatchCropperAction( {\n\t\t\t\ttype: 'SET_CROP_RECT',\n\t\t\t\tpayload: rect,\n\t\t\t} ),\n\t\tsettleCrop: () => dispatchCropperAction( { type: 'SETTLE_CROP' } ),\n\t\tapplyOperation: ( op ) =>\n\t\t\tdispatchCropperAction( {\n\t\t\t\ttype: 'APPLY_OPERATION',\n\t\t\t\tpayload: op,\n\t\t\t} ),\n\t};\n}\n"],
|
|
5
|
+
"mappings": ";AAWA,SAAS,iCAAiC;AA4CnC,SAAS,oBACf,uBACA,iBACiB;AACjB,SAAO;AAAA,IACN,QAAQ,CAAE,QACT,sBAAuB,EAAE,MAAM,WAAW,SAAS,IAAI,CAAE;AAAA,IAC1D,SAAS,CAAE,SAAU;AACpB,YAAM,SAAS,0BAA2B,gBAAgB,GAAG,IAAK;AAClE,UAAK,QAAS;AACb,8BAAuB,MAAO;AAAA,MAC/B;AAAA,IACD;AAAA,IACA,gBAAgB,CAAE,MAAM,QACvB,sBAAuB;AAAA,MACtB,MAAM;AAAA,MACN,SAAS,EAAE,MAAM,IAAI;AAAA,IACtB,CAAE;AAAA,IACH,aAAa,CAAE,aACd,sBAAuB;AAAA,MACtB,MAAM;AAAA,MACN,SAAS;AAAA,IACV,CAAE;AAAA,IACH,SAAS,CAAE,SACV,sBAAuB,EAAE,MAAM,YAAY,SAAS,KAAK,CAAE;AAAA,IAC5D,YAAY,CAAE,cAAe;AAC5B,YAAM,EAAE,KAAK,IAAI,gBAAgB;AACjC,4BAAuB;AAAA,QACtB,MAAM;AAAA,QACN,SAAS,EAAE,GAAG,MAAM,CAAE,SAAU,GAAG,CAAE,KAAM,SAAU,EAAE;AAAA,MACxD,CAAE;AAAA,IACH;AAAA,IACA,cAAc,CAAE,cACf,sBAAuB;AAAA,MACtB,MAAM;AAAA,MACN,SAAS,EAAE,UAAU;AAAA,IACtB,CAAE;AAAA,IACH,aAAa,CAAE,SACd,sBAAuB;AAAA,MACtB,MAAM;AAAA,MACN,SAAS;AAAA,IACV,CAAE;AAAA,IACH,YAAY,MAAM,sBAAuB,EAAE,MAAM,cAAc,CAAE;AAAA,IACjE,gBAAgB,CAAE,OACjB,sBAAuB;AAAA,MACtB,MAAM;AAAA,MACN,SAAS;AAAA,IACV,CAAE;AAAA,EACJ;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// packages/media-editor/src/image-editor/react/hooks/index.ts
|
|
2
|
-
import {
|
|
2
|
+
import { useCropperReducer } from "./use-cropper-reducer.mjs";
|
|
3
|
+
import { buildCropperSetters } from "./build-cropper-setters.mjs";
|
|
3
4
|
export {
|
|
4
|
-
|
|
5
|
+
buildCropperSetters,
|
|
6
|
+
useCropperReducer
|
|
5
7
|
};
|
|
6
8
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/image-editor/react/hooks/index.ts"],
|
|
4
|
-
"sourcesContent": ["export {
|
|
5
|
-
"mappings": ";AAAA,SAAS,
|
|
4
|
+
"sourcesContent": ["export { useCropperReducer } from './use-cropper-reducer';\nexport type { CropperController } from './use-cropper-reducer';\nexport { buildCropperSetters } from './build-cropper-setters';\nexport type { CropperSetters } from './build-cropper-setters';\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,yBAAyB;AAElC,SAAS,2BAA2B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// packages/media-editor/src/image-editor/react/hooks/use-cropper-reducer.ts
|
|
2
|
+
import {
|
|
3
|
+
useReducer,
|
|
4
|
+
useCallback,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useState
|
|
8
|
+
} from "@wordpress/element";
|
|
9
|
+
import { DEFAULT_STATE } from "../../core/constants.mjs";
|
|
10
|
+
import { exportCroppedImage } from "../../core/export/canvas-renderer.mjs";
|
|
11
|
+
import {
|
|
12
|
+
cropperReducer,
|
|
13
|
+
enforceContainment,
|
|
14
|
+
isStateDirty
|
|
15
|
+
} from "../../core/state.mjs";
|
|
16
|
+
import {
|
|
17
|
+
buildCropperSetters
|
|
18
|
+
} from "./build-cropper-setters.mjs";
|
|
19
|
+
function areCropperImagesEqual(a, b) {
|
|
20
|
+
return a?.src === b?.src && a?.naturalWidth === b?.naturalWidth && a?.naturalHeight === b?.naturalHeight;
|
|
21
|
+
}
|
|
22
|
+
function useCropperReducer(initialState) {
|
|
23
|
+
const [state, dispatch] = useReducer(
|
|
24
|
+
cropperReducer,
|
|
25
|
+
initialState,
|
|
26
|
+
(init) => enforceContainment({ ...DEFAULT_STATE, ...init })
|
|
27
|
+
);
|
|
28
|
+
const [initialBaseline, setInitialBaseline] = useState(
|
|
29
|
+
() => enforceContainment({ ...DEFAULT_STATE, ...initialState })
|
|
30
|
+
);
|
|
31
|
+
const stateRef = useRef(state);
|
|
32
|
+
const dispatchAndSync = useCallback((action) => {
|
|
33
|
+
const next = cropperReducer(stateRef.current, action);
|
|
34
|
+
stateRef.current = next;
|
|
35
|
+
dispatch(action);
|
|
36
|
+
return next;
|
|
37
|
+
}, []);
|
|
38
|
+
const setImage = useCallback(
|
|
39
|
+
(image) => {
|
|
40
|
+
if (areCropperImagesEqual(stateRef.current.image, image)) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const next = dispatchAndSync({
|
|
44
|
+
type: "SET_IMAGE",
|
|
45
|
+
payload: image
|
|
46
|
+
});
|
|
47
|
+
setInitialBaseline(next);
|
|
48
|
+
},
|
|
49
|
+
[dispatchAndSync]
|
|
50
|
+
);
|
|
51
|
+
const reset = useCallback(
|
|
52
|
+
(resetState) => {
|
|
53
|
+
const nextInitialState = dispatchAndSync({
|
|
54
|
+
type: "RESET",
|
|
55
|
+
payload: resetState
|
|
56
|
+
});
|
|
57
|
+
setInitialBaseline(nextInitialState);
|
|
58
|
+
},
|
|
59
|
+
[dispatchAndSync]
|
|
60
|
+
);
|
|
61
|
+
const setters = useMemo(
|
|
62
|
+
() => buildCropperSetters(dispatchAndSync, () => stateRef.current),
|
|
63
|
+
[dispatchAndSync]
|
|
64
|
+
);
|
|
65
|
+
const isDirty = isStateDirty(state, initialBaseline);
|
|
66
|
+
const getCroppedImage = useCallback(
|
|
67
|
+
(mimeType, quality) => {
|
|
68
|
+
if (!state.image) {
|
|
69
|
+
return Promise.reject(
|
|
70
|
+
new Error("No image loaded \u2014 call setImage first.")
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return exportCroppedImage(
|
|
74
|
+
state.image.src,
|
|
75
|
+
state,
|
|
76
|
+
mimeType,
|
|
77
|
+
quality
|
|
78
|
+
);
|
|
79
|
+
},
|
|
80
|
+
[state]
|
|
81
|
+
);
|
|
82
|
+
const setVisualSize = useCallback(() => {
|
|
83
|
+
}, []);
|
|
84
|
+
const adjustCropRectForViewport = setters.setCropRect;
|
|
85
|
+
const controller = {
|
|
86
|
+
...setters,
|
|
87
|
+
state,
|
|
88
|
+
setImage,
|
|
89
|
+
reset,
|
|
90
|
+
isDirty,
|
|
91
|
+
getCroppedImage,
|
|
92
|
+
setVisualSize,
|
|
93
|
+
adjustCropRectForViewport
|
|
94
|
+
};
|
|
95
|
+
return controller;
|
|
96
|
+
}
|
|
97
|
+
export {
|
|
98
|
+
useCropperReducer
|
|
99
|
+
};
|
|
100
|
+
//# sourceMappingURL=use-cropper-reducer.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/image-editor/react/hooks/use-cropper-reducer.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tuseReducer,\n\tuseCallback,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n} from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\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 \u2014 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 \u2014 see\n\t * `exportCroppedImage` for error semantics.\n\t */\n\tgetCroppedImage: ( mimeType?: string, quality?: number ) => Promise< Blob >;\n\t/**\n\t * Report the current rendered image size to the controller. The\n\t * Cropper component calls this on visualSize changes; composite\n\t * controllers use it to compute aspect-ratio reshapes. The pure\n\t * cropper hook ignores it (no-op).\n\t */\n\tsetVisualSize: ( size: Size ) => void;\n\t/**\n\t * Adjust the cropRect in response to a viewport change (window\n\t * resize, canvas resize). Behaves like `setCropRect` but, in\n\t * composite controllers, does NOT record an undo entry \u2014 viewport\n\t * reshapes aren't editor actions. The pure cropper hook aliases\n\t * this to `setCropRect` (history is a composite concern).\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 * Pure reducer hook for the image cropper.\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 \u2014 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 \u2014 state, setters, and helpers.\n */\nexport function useCropperReducer(\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 \u2014 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 \u2014 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 \u2014 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"],
|
|
5
|
+
"mappings": ";AAGA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAWP,SAAS,qBAAqB;AAC9B,SAAS,0BAA0B;AACnC;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP;AAAA,EACC;AAAA,OAEM;AA0CP,SAAS,sBACR,GACA,GACU;AACV,SACC,GAAG,QAAQ,GAAG,OACd,GAAG,iBAAiB,GAAG,gBACvB,GAAG,k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