@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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CropperState, Size } from '../types';
|
|
2
|
-
import { DEFAULT_STATE } from '../constants';
|
|
2
|
+
import { ABSOLUTE_MIN_ZOOM, DEFAULT_STATE, MAX_ZOOM } from '../constants';
|
|
3
3
|
import { cropperReducer, enforceContainment, isStateDirty } from '../state';
|
|
4
4
|
import {
|
|
5
5
|
createCamera,
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
} from '../camera';
|
|
10
10
|
|
|
11
11
|
const IMAGE: Size = { width: 1600, height: 900 };
|
|
12
|
+
const PORTRAIT_IMAGE: Size = { width: 900, height: 1600 };
|
|
12
13
|
const CONTAINER: Size = { width: 800, height: 600 };
|
|
13
14
|
|
|
14
15
|
function makeState( overrides: Partial< CropperState > = {} ): CropperState {
|
|
@@ -105,6 +106,19 @@ describe( 'enforceContainment', () => {
|
|
|
105
106
|
expect( result.cropRect ).toEqual( state.cropRect );
|
|
106
107
|
} );
|
|
107
108
|
|
|
109
|
+
it( 'keeps zoom at the absolute minimum for a degenerate crop rect', () => {
|
|
110
|
+
// A degenerate crop has coverage minimum 0; the floor must come
|
|
111
|
+
// from ABSOLUTE_MIN_ZOOM. Use a sub-floor (but >EPSILON) zoom so
|
|
112
|
+
// sanitization doesn't bump it to 1 before restrictPanZoom runs.
|
|
113
|
+
const state = makeState( {
|
|
114
|
+
cropRect: { x: 0.5, y: 0.5, width: 0, height: 0 },
|
|
115
|
+
zoom: ABSOLUTE_MIN_ZOOM / 2,
|
|
116
|
+
} );
|
|
117
|
+
const result = enforceContainment( state );
|
|
118
|
+
|
|
119
|
+
expect( result.zoom ).toBe( ABSOLUTE_MIN_ZOOM );
|
|
120
|
+
} );
|
|
121
|
+
|
|
108
122
|
it( 'bumps zoom before shrinking crop for small rotation', () => {
|
|
109
123
|
const state = makeState( {
|
|
110
124
|
rotation: 15,
|
|
@@ -268,6 +282,62 @@ describe( 'cropperReducer — SETTLE_CROP', () => {
|
|
|
268
282
|
expectSameVisibleRegion( state, settled );
|
|
269
283
|
} );
|
|
270
284
|
|
|
285
|
+
it( 'caps zoom at MAX_ZOOM for tight crops and keeps the visible region', () => {
|
|
286
|
+
// A 5% crop at zoom=1 would otherwise settle to zoom=20 (1/0.05),
|
|
287
|
+
// pushing state past the user-facing zoom cap and breaking wheel
|
|
288
|
+
// and slider interactions that clamp to MAX_ZOOM. With the cap,
|
|
289
|
+
// the post-settle crop is smaller than the viewport but the
|
|
290
|
+
// content the user framed is preserved.
|
|
291
|
+
const state = makeState( {
|
|
292
|
+
cropRect: { x: 0.4, y: 0.4, width: 0.05, height: 0.05 },
|
|
293
|
+
zoom: 1,
|
|
294
|
+
pan: { x: 0, y: 0 },
|
|
295
|
+
} );
|
|
296
|
+
const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
|
|
297
|
+
|
|
298
|
+
expect( settled.zoom ).toBeCloseTo( MAX_ZOOM, 5 );
|
|
299
|
+
// The scale was capped at MAX_ZOOM/zoom (=10), not at the uncapped
|
|
300
|
+
// fit scale of 20 — so the new crop is 5% × 10 = 50% of the viewport,
|
|
301
|
+
// not 100%.
|
|
302
|
+
expect( settled.cropRect.width ).toBeCloseTo( 0.5, 5 );
|
|
303
|
+
expect( settled.cropRect.height ).toBeCloseTo( 0.5, 5 );
|
|
304
|
+
expectSameVisibleRegion( state, settled );
|
|
305
|
+
} );
|
|
306
|
+
|
|
307
|
+
it( 'allows a fine-rotated tall portrait crop to settle below 1x zoom', () => {
|
|
308
|
+
const state = makeState( {
|
|
309
|
+
image: {
|
|
310
|
+
src: 'portrait.jpg',
|
|
311
|
+
naturalWidth: PORTRAIT_IMAGE.width,
|
|
312
|
+
naturalHeight: PORTRAIT_IMAGE.height,
|
|
313
|
+
},
|
|
314
|
+
cropRect: { x: 0.46, y: -0.02, width: 0.08, height: 1.04 },
|
|
315
|
+
rotation: 19,
|
|
316
|
+
zoom: 1,
|
|
317
|
+
pan: { x: 0, y: 0 },
|
|
318
|
+
} );
|
|
319
|
+
const settled = cropperReducer( state, { type: 'SETTLE_CROP' } );
|
|
320
|
+
|
|
321
|
+
expect( settled.cropRect.height ).toBeCloseTo( 1, 5 );
|
|
322
|
+
expect( settled.zoom ).toBeLessThan( 1 );
|
|
323
|
+
expect( settled.zoom ).toBeGreaterThan( 0.95 );
|
|
324
|
+
|
|
325
|
+
const regionBefore = getCropWorldRegion(
|
|
326
|
+
state,
|
|
327
|
+
PORTRAIT_IMAGE,
|
|
328
|
+
CONTAINER
|
|
329
|
+
);
|
|
330
|
+
const regionAfter = getCropWorldRegion(
|
|
331
|
+
settled,
|
|
332
|
+
PORTRAIT_IMAGE,
|
|
333
|
+
CONTAINER
|
|
334
|
+
);
|
|
335
|
+
expect( regionAfter.minX ).toBeCloseTo( regionBefore.minX, 1 );
|
|
336
|
+
expect( regionAfter.minY ).toBeCloseTo( regionBefore.minY, 1 );
|
|
337
|
+
expect( regionAfter.maxX ).toBeCloseTo( regionBefore.maxX, 1 );
|
|
338
|
+
expect( regionAfter.maxY ).toBeCloseTo( regionBefore.maxY, 1 );
|
|
339
|
+
} );
|
|
340
|
+
|
|
271
341
|
it( 'is a no-op when crop is already full-sized and centered', () => {
|
|
272
342
|
const state = makeState( {
|
|
273
343
|
cropRect: { x: 0, y: 0, width: 1, height: 1 },
|
|
@@ -285,14 +355,14 @@ describe( 'cropperReducer — SETTLE_CROP', () => {
|
|
|
285
355
|
} );
|
|
286
356
|
|
|
287
357
|
it( 'preserves image selection across 50 random crop/zoom/pan combos', () => {
|
|
288
|
-
let passCount = 0;
|
|
289
358
|
for ( let i = 0; i < 50; i++ ) {
|
|
290
|
-
// Generate deterministic-ish crop rects
|
|
291
|
-
|
|
292
|
-
const
|
|
359
|
+
// Generate deterministic-ish crop rects in the preservation
|
|
360
|
+
// regime. Capped zoom behavior is covered by a dedicated test.
|
|
361
|
+
const cropW = 0.4 + ( ( i * 7 ) % 6 ) / 10;
|
|
362
|
+
const cropH = 0.4 + ( ( i * 11 ) % 6 ) / 10;
|
|
293
363
|
const cropX = Math.min( ( ( i * 3 ) % 10 ) / 10, 1 - cropW );
|
|
294
364
|
const cropY = Math.min( ( ( i * 5 ) % 10 ) / 10, 1 - cropH );
|
|
295
|
-
const zoom = 1 + ( ( i * 13 ) %
|
|
365
|
+
const zoom = 1 + ( ( i * 13 ) % 12 ) / 10;
|
|
296
366
|
const panX = ( ( ( i * 17 ) % 20 ) - 10 ) / 100;
|
|
297
367
|
const panY = ( ( ( i * 19 ) % 20 ) - 10 ) / 100;
|
|
298
368
|
|
|
@@ -313,6 +383,11 @@ describe( 'cropperReducer — SETTLE_CROP', () => {
|
|
|
313
383
|
type: 'SETTLE_CROP',
|
|
314
384
|
} );
|
|
315
385
|
|
|
386
|
+
const expectedUncappedZoom =
|
|
387
|
+
state.zoom /
|
|
388
|
+
Math.max( state.cropRect.width, state.cropRect.height );
|
|
389
|
+
expect( expectedUncappedZoom ).toBeLessThanOrEqual( MAX_ZOOM );
|
|
390
|
+
|
|
316
391
|
const regionBefore = getCropWorldRegion( state, IMAGE, CONTAINER );
|
|
317
392
|
const regionAfter = getCropWorldRegion( settled, IMAGE, CONTAINER );
|
|
318
393
|
|
|
@@ -336,9 +411,7 @@ describe( 'cropperReducer — SETTLE_CROP', () => {
|
|
|
336
411
|
expect( dy ).toBeLessThanOrEqual( tol );
|
|
337
412
|
expect( dw ).toBeLessThanOrEqual( tol );
|
|
338
413
|
expect( dh ).toBeLessThanOrEqual( tol );
|
|
339
|
-
passCount++;
|
|
340
414
|
}
|
|
341
|
-
expect( passCount ).toBe( 50 );
|
|
342
415
|
} );
|
|
343
416
|
|
|
344
417
|
it( 'diagonal corner drag: small crop settles accurately', () => {
|
|
@@ -424,6 +497,26 @@ describe( 'cropperReducer — SETTLE_CROP', () => {
|
|
|
424
497
|
const wAfter = regionAfter.maxX - regionAfter.minX;
|
|
425
498
|
expect( wAfter ).toBeCloseTo( wBefore, 1 );
|
|
426
499
|
} );
|
|
500
|
+
|
|
501
|
+
it( 'caps settle zoom for crops smaller than the maximum zoom can preserve', () => {
|
|
502
|
+
const afterDrag = enforceContainment(
|
|
503
|
+
makeState( {
|
|
504
|
+
cropRect: { x: 0.475, y: 0.475, width: 0.05, height: 0.05 },
|
|
505
|
+
zoom: 1,
|
|
506
|
+
pan: { x: 0, y: 0 },
|
|
507
|
+
} )
|
|
508
|
+
);
|
|
509
|
+
|
|
510
|
+
const settled = cropperReducer( afterDrag, { type: 'SETTLE_CROP' } );
|
|
511
|
+
|
|
512
|
+
expect( settled.zoom ).toBe( MAX_ZOOM );
|
|
513
|
+
|
|
514
|
+
const regionAfter = getCropWorldRegion( settled, IMAGE, CONTAINER );
|
|
515
|
+
expect( regionAfter.minX ).toBeGreaterThanOrEqual( -0.001 );
|
|
516
|
+
expect( regionAfter.minY ).toBeGreaterThanOrEqual( -0.001 );
|
|
517
|
+
expect( regionAfter.maxX ).toBeLessThanOrEqual( 1.001 );
|
|
518
|
+
expect( regionAfter.maxY ).toBeLessThanOrEqual( 1.001 );
|
|
519
|
+
} );
|
|
427
520
|
} );
|
|
428
521
|
|
|
429
522
|
describe( 'cropperReducer — SET_ZOOM_AT_POINT', () => {
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
4
|
import {
|
|
5
|
+
computeFreeResizeRect,
|
|
5
6
|
computeLockedResizeRect,
|
|
6
7
|
computeShiftLockedResizeRect,
|
|
7
8
|
type CropBounds,
|
|
@@ -319,3 +320,195 @@ describe( 'computeShiftLockedResizeRect', () => {
|
|
|
319
320
|
} );
|
|
320
321
|
} );
|
|
321
322
|
} );
|
|
323
|
+
|
|
324
|
+
describe( 'per-axis minCropSize', () => {
|
|
325
|
+
// Cover the explicit per-axis floor introduced for the source-pixel
|
|
326
|
+
// minimum. The defaults (0.05, 0.05) are already covered by the
|
|
327
|
+
// suites above; these tests pass `minCropSize` explicitly to lock in
|
|
328
|
+
// the math the cropper relies on.
|
|
329
|
+
const IMAGE_NON_SQUARE: Size = { width: 1000, height: 500 };
|
|
330
|
+
|
|
331
|
+
describe( 'computeFreeResizeRect', () => {
|
|
332
|
+
it( 'clamps the dragged edge to the per-axis floor (width)', () => {
|
|
333
|
+
// SE handle from a wide start rect; drag left far enough that
|
|
334
|
+
// width would collapse without the floor.
|
|
335
|
+
const startRect = { x: 0.1, y: 0.1, width: 0.6, height: 0.6 };
|
|
336
|
+
const drag: ResizeDragState = {
|
|
337
|
+
handle: 'e',
|
|
338
|
+
startX: 700,
|
|
339
|
+
startY: 350,
|
|
340
|
+
startRect,
|
|
341
|
+
};
|
|
342
|
+
const minCropSize: Size = { width: 0.2, height: 0.05 };
|
|
343
|
+
const rect = computeFreeResizeRect(
|
|
344
|
+
drag,
|
|
345
|
+
0,
|
|
346
|
+
350,
|
|
347
|
+
IMAGE_NON_SQUARE,
|
|
348
|
+
FULL_BOUNDS,
|
|
349
|
+
minCropSize
|
|
350
|
+
);
|
|
351
|
+
expect( rect.width ).toBeCloseTo( minCropSize.width, 5 );
|
|
352
|
+
// Left edge anchored at start; right edge pulled in to the floor.
|
|
353
|
+
expect( rect.x ).toBeCloseTo( startRect.x, 5 );
|
|
354
|
+
} );
|
|
355
|
+
|
|
356
|
+
it( 'clamps the dragged edge to the per-axis floor (height)', () => {
|
|
357
|
+
const startRect = { x: 0.1, y: 0.1, width: 0.6, height: 0.6 };
|
|
358
|
+
const drag: ResizeDragState = {
|
|
359
|
+
handle: 's',
|
|
360
|
+
startX: 500,
|
|
361
|
+
startY: 350,
|
|
362
|
+
startRect,
|
|
363
|
+
};
|
|
364
|
+
const minCropSize: Size = { width: 0.05, height: 0.3 };
|
|
365
|
+
const rect = computeFreeResizeRect(
|
|
366
|
+
drag,
|
|
367
|
+
500,
|
|
368
|
+
0,
|
|
369
|
+
IMAGE_NON_SQUARE,
|
|
370
|
+
FULL_BOUNDS,
|
|
371
|
+
minCropSize
|
|
372
|
+
);
|
|
373
|
+
expect( rect.height ).toBeCloseTo( minCropSize.height, 5 );
|
|
374
|
+
expect( rect.y ).toBeCloseTo( startRect.y, 5 );
|
|
375
|
+
} );
|
|
376
|
+
|
|
377
|
+
it( 'uses width vs height independently — different floors per axis', () => {
|
|
378
|
+
// Drag NW corner inward far past both floors. The per-axis
|
|
379
|
+
// floors clamp x-collapse and y-collapse independently.
|
|
380
|
+
const startRect = { x: 0.1, y: 0.1, width: 0.8, height: 0.8 };
|
|
381
|
+
const drag: ResizeDragState = {
|
|
382
|
+
handle: 'nw',
|
|
383
|
+
startX: 100,
|
|
384
|
+
startY: 50,
|
|
385
|
+
startRect,
|
|
386
|
+
};
|
|
387
|
+
const minCropSize: Size = { width: 0.4, height: 0.1 };
|
|
388
|
+
const rect = computeFreeResizeRect(
|
|
389
|
+
drag,
|
|
390
|
+
1000,
|
|
391
|
+
500,
|
|
392
|
+
IMAGE_NON_SQUARE,
|
|
393
|
+
FULL_BOUNDS,
|
|
394
|
+
minCropSize
|
|
395
|
+
);
|
|
396
|
+
// Both floors hit at the same time on different axes.
|
|
397
|
+
expect( rect.width ).toBeCloseTo( minCropSize.width, 5 );
|
|
398
|
+
expect( rect.height ).toBeCloseTo( minCropSize.height, 5 );
|
|
399
|
+
} );
|
|
400
|
+
} );
|
|
401
|
+
|
|
402
|
+
describe( 'computeLockedResizeRect — per-axis floor projects through the ratio', () => {
|
|
403
|
+
// Wide image, square aspect ratio. The per-axis floor projection
|
|
404
|
+
// in `minDistW = max(minCropSize.width, minCropSize.height * normalizedRatio)`
|
|
405
|
+
// is the load-bearing piece for non-equal per-axis floors.
|
|
406
|
+
const imageSize: Size = { width: 2000, height: 1000 };
|
|
407
|
+
const aspectRatio = 1; // pixel-square
|
|
408
|
+
const normalizedRatio =
|
|
409
|
+
( aspectRatio * imageSize.height ) / imageSize.width; // 0.5
|
|
410
|
+
const startRect = { x: 0, y: 0, width: 0, height: 0 };
|
|
411
|
+
|
|
412
|
+
it( 'lifts width to satisfy the height floor when the height floor is the binding axis', () => {
|
|
413
|
+
// Drag SE inward to zero — both axes try to collapse. With
|
|
414
|
+
// minCropSize = { 0.01, 0.05 } and normalizedRatio 0.5,
|
|
415
|
+
// minDistW = max(0.01, 0.05 * 0.5) = 0.025, minDistH = 0.05.
|
|
416
|
+
// Driver: pixel motion equal → height drives → distW = distH/2.
|
|
417
|
+
const drag: ResizeDragState = {
|
|
418
|
+
handle: 'se',
|
|
419
|
+
startX: 0,
|
|
420
|
+
startY: 0,
|
|
421
|
+
startRect,
|
|
422
|
+
};
|
|
423
|
+
const minCropSize: Size = { width: 0.01, height: 0.05 };
|
|
424
|
+
const rect = computeLockedResizeRect(
|
|
425
|
+
drag,
|
|
426
|
+
1,
|
|
427
|
+
1,
|
|
428
|
+
imageSize,
|
|
429
|
+
FULL_BOUNDS,
|
|
430
|
+
normalizedRatio,
|
|
431
|
+
minCropSize
|
|
432
|
+
);
|
|
433
|
+
// Height floor binds: width is `height * normalizedRatio`.
|
|
434
|
+
expect( rect.height ).toBeCloseTo( minCropSize.height, 5 );
|
|
435
|
+
expect( rect.width ).toBeCloseTo(
|
|
436
|
+
minCropSize.height * normalizedRatio,
|
|
437
|
+
5
|
|
438
|
+
);
|
|
439
|
+
} );
|
|
440
|
+
|
|
441
|
+
it( 'lifts height to satisfy the width floor when the width floor binds', () => {
|
|
442
|
+
// minDistW = max(0.2, 0.05 * 0.5) = 0.2, minDistH = 0.4.
|
|
443
|
+
const drag: ResizeDragState = {
|
|
444
|
+
handle: 'se',
|
|
445
|
+
startX: 0,
|
|
446
|
+
startY: 0,
|
|
447
|
+
startRect,
|
|
448
|
+
};
|
|
449
|
+
const minCropSize: Size = { width: 0.2, height: 0.05 };
|
|
450
|
+
const rect = computeLockedResizeRect(
|
|
451
|
+
drag,
|
|
452
|
+
1,
|
|
453
|
+
1,
|
|
454
|
+
imageSize,
|
|
455
|
+
FULL_BOUNDS,
|
|
456
|
+
normalizedRatio,
|
|
457
|
+
minCropSize
|
|
458
|
+
);
|
|
459
|
+
expect( rect.width ).toBeCloseTo( 0.2, 5 );
|
|
460
|
+
expect( rect.height ).toBeCloseTo( 0.4, 5 );
|
|
461
|
+
} );
|
|
462
|
+
} );
|
|
463
|
+
|
|
464
|
+
describe( 'computeShiftLockedResizeRect — edge handles', () => {
|
|
465
|
+
// Shift-locked edge resize must enforce the floor on the driver
|
|
466
|
+
// axis and on the perpendicular axis after projecting through
|
|
467
|
+
// the start rect's normalized ratio.
|
|
468
|
+
const imageSize: Size = { width: 1000, height: 500 };
|
|
469
|
+
// Wide start rect — normalizedRatio = 0.4/0.4 = 1.
|
|
470
|
+
const startRect = { x: 0.3, y: 0.3, width: 0.4, height: 0.4 };
|
|
471
|
+
|
|
472
|
+
it( 'east edge: width-driver respects width floor and projects to height', () => {
|
|
473
|
+
const drag: ResizeDragState = {
|
|
474
|
+
handle: 'e',
|
|
475
|
+
startX: 700,
|
|
476
|
+
startY: 400,
|
|
477
|
+
startRect,
|
|
478
|
+
};
|
|
479
|
+
// Drag well past the floor toward the start anchor.
|
|
480
|
+
const minCropSize: Size = { width: 0.2, height: 0.05 };
|
|
481
|
+
const rect = computeShiftLockedResizeRect(
|
|
482
|
+
drag,
|
|
483
|
+
300,
|
|
484
|
+
400,
|
|
485
|
+
imageSize,
|
|
486
|
+
FULL_BOUNDS,
|
|
487
|
+
minCropSize
|
|
488
|
+
);
|
|
489
|
+
// Width clamped at the per-axis floor; height follows ratio (1:1).
|
|
490
|
+
expect( rect.width ).toBeCloseTo( minCropSize.width, 5 );
|
|
491
|
+
expect( rect.height ).toBeCloseTo( minCropSize.width, 5 );
|
|
492
|
+
} );
|
|
493
|
+
|
|
494
|
+
it( 'north edge: height-driver respects height floor and projects to width', () => {
|
|
495
|
+
const drag: ResizeDragState = {
|
|
496
|
+
handle: 'n',
|
|
497
|
+
startX: 500,
|
|
498
|
+
startY: 300,
|
|
499
|
+
startRect,
|
|
500
|
+
};
|
|
501
|
+
const minCropSize: Size = { width: 0.05, height: 0.2 };
|
|
502
|
+
const rect = computeShiftLockedResizeRect(
|
|
503
|
+
drag,
|
|
504
|
+
500,
|
|
505
|
+
700,
|
|
506
|
+
imageSize,
|
|
507
|
+
FULL_BOUNDS,
|
|
508
|
+
minCropSize
|
|
509
|
+
);
|
|
510
|
+
expect( rect.height ).toBeCloseTo( minCropSize.height, 5 );
|
|
511
|
+
expect( rect.width ).toBeCloseTo( minCropSize.height, 5 );
|
|
512
|
+
} );
|
|
513
|
+
} );
|
|
514
|
+
} );
|
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import type { CropperState, Size } from './types';
|
|
5
5
|
import { degreesToRadians } from './math/rotation';
|
|
6
|
+
import { isValidSize, sanitizeCropperState } from './math/sanitize';
|
|
7
|
+
|
|
8
|
+
/** Identity CSS transform — applied when inputs aren't safe to compose. */
|
|
9
|
+
const IDENTITY_MATRIX_STYLE = 'matrix(1, 0, 0, 1, 0, 0)';
|
|
6
10
|
|
|
7
11
|
/**
|
|
8
12
|
* Computes a CSS matrix() transform string from the cropper state.
|
|
@@ -25,14 +29,25 @@ export function computeTransformStyle(
|
|
|
25
29
|
state: CropperState,
|
|
26
30
|
imageSize: Size
|
|
27
31
|
): string {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
// Match the identity short-circuit used by `createCamera` and
|
|
33
|
+
// `getSourceRegion` so the preview path stays consistent with the math
|
|
34
|
+
// path when `imageSize` itself is hostile (state sanitization below
|
|
35
|
+
// only guards the state fields, not these size arguments).
|
|
36
|
+
if ( ! isValidSize( imageSize ) ) {
|
|
37
|
+
return IDENTITY_MATRIX_STYLE;
|
|
38
|
+
}
|
|
39
|
+
// Sanitize so hostile state can't produce `matrix(NaN, NaN, ...)` here
|
|
40
|
+
// while the parallel `createCamera` path returns a finite matrix. Both
|
|
41
|
+
// paths must agree under defense-in-depth conditions.
|
|
42
|
+
const safeState = sanitizeCropperState( state );
|
|
43
|
+
const translateX = safeState.pan.x * imageSize.width;
|
|
44
|
+
const translateY = safeState.pan.y * imageSize.height;
|
|
45
|
+
const rad = degreesToRadians( safeState.rotation );
|
|
31
46
|
const cos = Math.cos( rad );
|
|
32
47
|
const sin = Math.sin( rad );
|
|
33
|
-
const sx =
|
|
34
|
-
const sy =
|
|
35
|
-
const z =
|
|
48
|
+
const sx = safeState.flip.horizontal ? -1 : 1;
|
|
49
|
+
const sy = safeState.flip.vertical ? -1 : 1;
|
|
50
|
+
const z = safeState.zoom;
|
|
36
51
|
|
|
37
52
|
// Combined: translate(tx,ty) * scale(sx,sy) * rotate(r) * scale(z)
|
|
38
53
|
const a = sx * cos * z;
|
|
@@ -109,14 +109,15 @@ export interface CropperState {
|
|
|
109
109
|
*
|
|
110
110
|
* This type is intentionally not exported from the image-editor public barrel.
|
|
111
111
|
* Consumers should drive state through the controller returned by
|
|
112
|
-
* `
|
|
112
|
+
* `useCropperReducer()` (or a composite store) or through serializable
|
|
113
|
+
* `TransformOperation` values.
|
|
113
114
|
*/
|
|
114
115
|
export type CropperAction =
|
|
115
116
|
/** Sets the loaded image metadata (natural size, src). */
|
|
116
117
|
| { type: 'SET_IMAGE'; payload: CropperState[ 'image' ] }
|
|
117
118
|
/** Sets the image pan offset. (Crop rectangle is SET_CROP_RECT.) */
|
|
118
119
|
| { type: 'SET_PAN'; payload: NormalizedPoint }
|
|
119
|
-
/** Sets the zoom level, clamped to [
|
|
120
|
+
/** Sets the zoom level, clamped to [coverage-aware min, MAX_ZOOM]. */
|
|
120
121
|
| { type: 'SET_ZOOM'; payload: number }
|
|
121
122
|
/**
|
|
122
123
|
* Sets zoom and pan together atomically. Used by focal-point
|
|
@@ -158,6 +159,13 @@ export type ViewportAction =
|
|
|
158
159
|
| { type: 'SET_VIEWPORT_PAN'; payload: { x: number; y: number } }
|
|
159
160
|
| { type: 'RESET_VIEWPORT' };
|
|
160
161
|
|
|
162
|
+
/**
|
|
163
|
+
* Direction of a crop resize handle. Eight compass positions: four
|
|
164
|
+
* corners and four edge midpoints. Used by stencil callbacks and any
|
|
165
|
+
* overlay that needs to know which handle the user is dragging.
|
|
166
|
+
*/
|
|
167
|
+
export type HandlePosition = 'n' | 's' | 'e' | 'w' | 'nw' | 'ne' | 'sw' | 'se';
|
|
168
|
+
|
|
161
169
|
/**
|
|
162
170
|
* The contract for a pluggable stencil component.
|
|
163
171
|
* Stencils render the crop area overlay and handle resize interactions.
|
|
@@ -171,8 +179,14 @@ export interface StencilProps {
|
|
|
171
179
|
imageSize: Size;
|
|
172
180
|
/** Callback when the crop rectangle changes (during drag). */
|
|
173
181
|
onCropChange: ( rect: NormalizedRect ) => void;
|
|
174
|
-
/**
|
|
175
|
-
|
|
182
|
+
/**
|
|
183
|
+
* Callback when a resize interaction starts — pointerdown on a handle
|
|
184
|
+
* or the first arrow-key press on a focused handle. The handle direction
|
|
185
|
+
* is passed so downstream overlays can anchor to it. The argument is
|
|
186
|
+
* optional to keep custom stencils that don't track a direction
|
|
187
|
+
* backward-compatible.
|
|
188
|
+
*/
|
|
189
|
+
onResizeStart?: ( handle?: HandlePosition ) => void;
|
|
176
190
|
/** Callback when a resize drag ends (mouseup after handle drag). */
|
|
177
191
|
onResizeEnd?: () => void;
|
|
178
192
|
/** Optional fixed aspect ratio (width / height) in pixel space. */
|
|
@@ -190,4 +204,10 @@ export interface StencilProps {
|
|
|
190
204
|
};
|
|
191
205
|
/** Called when Escape is pressed on a resize handle. */
|
|
192
206
|
onEscape?: () => void;
|
|
207
|
+
/**
|
|
208
|
+
* Minimum crop rect dimension in normalized space, per axis. Derived
|
|
209
|
+
* by the host from a pixel-based floor (see `MIN_CROP_PIXELS`) and
|
|
210
|
+
* the source image dimensions. Omit to use the stencil's default.
|
|
211
|
+
*/
|
|
212
|
+
minCropSize?: Size;
|
|
193
213
|
}
|
|
@@ -1,96 +1,55 @@
|
|
|
1
|
-
# Image Editor
|
|
1
|
+
# Image Editor Architecture
|
|
2
|
+
|
|
3
|
+
## Layers
|
|
4
|
+
|
|
5
|
+
| Layer | Files | Responsibility |
|
|
6
|
+
| --- | --- | --- |
|
|
7
|
+
| Core cropper | `src/image-editor/core/` | Pure state, camera math, containment, source-region, transforms, and canvas export. |
|
|
8
|
+
| React cropper | `src/image-editor/react/` | DOM measurement, interaction hooks, overlays, stencil rendering, and the pure `useCropperReducer` hook. |
|
|
9
|
+
| Media editor state | `src/state/` | Composite state for the media editor: cropper state, crop options, undo/redo, and gesture boundaries. |
|
|
10
|
+
|
|
11
|
+
The cropper layer does not own editor history. History belongs to the composite media editor controller because sidebar controls and cropper gestures need one shared undo stack.
|
|
2
12
|
|
|
3
13
|
## Coordinate Spaces
|
|
4
14
|
|
|
5
15
|
| Space | What lives here | Coordinates |
|
|
6
|
-
|
|
7
|
-
|
|
|
8
|
-
|
|
|
9
|
-
|
|
|
16
|
+
| --- | --- | --- |
|
|
17
|
+
| World | Image and crop rectangle | Normalized 0-1, origin at the top-left of the image. |
|
|
18
|
+
| Camera | Pan, zoom, rotation, and flip transform | `mat2d` mapping world to screen. |
|
|
19
|
+
| Screen | DOM container, pointer events, CSS transforms | Pixels relative to the cropper canvas. |
|
|
20
|
+
| Viewport | Display-only pan/zoom of the cropper stage | CSS pixels; does not affect export. |
|
|
10
21
|
|
|
11
22
|
## Data Flow
|
|
12
23
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
subgraph Interaction["Interaction Path"]
|
|
46
|
-
useInteraction["use-interaction.ts\ndelta / visualSize → restrictPanZoom"]
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
subgraph Export["Export Path"]
|
|
50
|
-
canvasRenderer["canvas-renderer.ts"]
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
State --> restrictPanZoom
|
|
54
|
-
State --> restrictCropRect
|
|
55
|
-
restrictPanZoom --> |"corrected crop/zoom"| State
|
|
56
|
-
restrictCropRect --> |"corrected cropRect"| State
|
|
57
|
-
|
|
58
|
-
State --> getImageFit
|
|
59
|
-
getImageFit --> |"elementSize"| imgStyle["img element style"]
|
|
60
|
-
getImageFit --> |"visualSize"| stencil
|
|
61
|
-
getImageFit --> |"visualSize"| dimming
|
|
62
|
-
getImageFit --> |"visualSize"| grid
|
|
63
|
-
getImageFit --> |"visualSize"| useInteraction
|
|
64
|
-
getImageFit --> |"visualSize"| useTransformStyle
|
|
65
|
-
|
|
66
|
-
useInteraction --> |"named controller actions"| State
|
|
67
|
-
|
|
68
|
-
State --> createExportCamera --> canvasRenderer
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
### Design decisions
|
|
72
|
-
|
|
73
|
-
**Why restriction uses the camera but rendering doesn't:**
|
|
74
|
-
- `restrictPanZoom` builds a camera internally and projects crop corners through its inverse. This guarantees the restriction and rendering agree on geometry — the camera IS the screen transform.
|
|
75
|
-
- The render path (`use-transform-style`, stencils, overlays) uses simple manual math because the CSS transform operates in a different coordinate system (element-center origin vs container-center). Deriving CSS matrix from the camera mat2d would be more complex, not less.
|
|
76
|
-
|
|
77
|
-
**Why `getImageFit` exists:**
|
|
78
|
-
- `createCamera` needs to know the fitted image dimensions to build its matrix. `cropper.tsx` also needs them to size the `<img>` element and compute `visualSize` for overlays. `getImageFit` is the shared contain-fit calculation used by both — no duplication.
|
|
79
|
-
|
|
80
|
-
**UX invariant:**
|
|
81
|
-
> After `restrictPanZoom`, transforming the 4 crop corners through `screenToWorld(camera, corner)` must produce world points inside [0,1]×[0,1].
|
|
82
|
-
|
|
83
|
-
This is both the restriction algorithm AND the test. If the camera says it's covered, it's covered on screen — because the camera IS the screen transform.
|
|
84
|
-
|
|
85
|
-
## Extension points
|
|
86
|
-
|
|
87
|
-
See [recipes.md](recipes.md) for the full developer guide. Summary:
|
|
88
|
-
|
|
89
|
-
| Extension | Mechanism |
|
|
90
|
-
|-----------|-----------|
|
|
91
|
-
| Custom crop area UI | `stencil` prop — any component implementing `StencilProps` |
|
|
92
|
-
| AI agent control | `TransformOperation[]` pipeline — JSON-serializable, replayable |
|
|
93
|
-
| Custom export | `exportCroppedImage()` or `applyToCanvas()` for multi-step pipelines |
|
|
94
|
-
| Theming | BEM CSS classes (`.wp-media-editor-image-editor__*`) |
|
|
95
|
-
| State observation | `onStateChange` (every frame), `onGestureStart`/`onGestureEnd` (gesture boundaries) |
|
|
96
|
-
| Undo/redo | Snapshot state at gesture boundaries, `reset()` to restore — see recipes.md |
|
|
24
|
+
`MediaEditorState` delegates cropper actions to `cropperReducer()`. Containment keeps the image covering the crop rectangle. React components measure the DOM and render the resulting state.
|
|
25
|
+
|
|
26
|
+
## Design Decisions
|
|
27
|
+
|
|
28
|
+
### Containment
|
|
29
|
+
|
|
30
|
+
Restriction logic projects crop corners through the camera and checks that they stay inside the image.
|
|
31
|
+
|
|
32
|
+
### Rendering
|
|
33
|
+
|
|
34
|
+
The render path uses focused calculations for CSS transforms, stencil placement, dimming, and grid overlays.
|
|
35
|
+
|
|
36
|
+
### State
|
|
37
|
+
|
|
38
|
+
The core reducer is framework-agnostic and serializable. React and media-editor layers add DOM measurement, gestures, undo/redo, save behavior, and UI state.
|
|
39
|
+
|
|
40
|
+
### Viewport
|
|
41
|
+
|
|
42
|
+
Viewport pan/zoom lets the user inspect or follow the crop area without changing the crop output. It is not part of export and should not create undo entries.
|
|
43
|
+
|
|
44
|
+
## Internal Integration Points
|
|
45
|
+
|
|
46
|
+
These APIs let code inside `@wordpress/media-editor` compose the cropper. They are not supported extension points for themes or plugins.
|
|
47
|
+
|
|
48
|
+
| Task | API |
|
|
49
|
+
| --- | --- |
|
|
50
|
+
| Alternate crop UI inside media-editor | `stencil` prop implementing `StencilProps`. |
|
|
51
|
+
| Programmatic edits | `TransformOperation[]`, `applyOperation`, or `stateFromPipeline`. |
|
|
52
|
+
| Crop data for REST requests | `getSourceRegion()` and `getSourceRegionPercent()`. |
|
|
53
|
+
| Canvas export | `exportCroppedImage()` or `applyToCanvas()`. |
|
|
54
|
+
| Shared cropper state across internal components | `CropperProvider` / `useCropper()`. |
|
|
55
|
+
| Media editor history | `useMediaEditorState()` composite controller. |
|