@wordpress/media-editor 0.15.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -0
- package/build/components/media-editor/index.cjs +54 -44
- package/build/components/media-editor/index.cjs.map +2 -2
- package/build/components/media-editor/use-crop-options.cjs.map +2 -2
- package/build/components/media-editor/use-save-media-editor.cjs.map +2 -2
- package/build/components/media-editor-canvas/index.cjs.map +2 -2
- package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
- package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
- package/build/components/media-editor-image-controls/index.cjs.map +2 -2
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +2 -2
- package/build/components/media-editor-modal/build-modifiers.cjs.map +2 -2
- package/build/components/media-editor-modal/index.cjs.map +2 -2
- package/build/components/media-editor-provider/index.cjs.map +2 -2
- package/build/components/media-form/index.cjs.map +2 -2
- package/build/components/media-preview/index.cjs.map +2 -2
- package/build/components/rotation-ruler/index.cjs.map +2 -2
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +2 -2
- package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
- package/build/image-editor/core/camera.cjs.map +2 -2
- package/build/image-editor/core/constants.cjs.map +2 -2
- package/build/image-editor/core/containment.cjs.map +2 -2
- package/build/image-editor/core/crop-rect.cjs.map +2 -2
- package/build/image-editor/core/export/canvas-renderer.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/core/math/sanitize.cjs.map +2 -2
- package/build/image-editor/core/setter-helpers.cjs.map +2 -2
- package/build/image-editor/core/source-region.cjs.map +2 -2
- package/build/image-editor/core/state.cjs.map +2 -2
- package/build/image-editor/core/stencil-math.cjs.map +2 -2
- package/build/image-editor/core/transform-style.cjs.map +2 -2
- package/build/image-editor/core/transforms/pipeline.cjs.map +2 -2
- package/build/image-editor/core/viewport-state.cjs.map +2 -2
- package/build/image-editor/react/components/cropper-provider.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +2 -2
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
- package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/components/viewport-provider.cjs.map +2 -2
- package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-transform-style.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-viewport-state.cjs.map +2 -2
- package/build/lock-unlock.cjs.map +2 -2
- package/build/private-apis.cjs.map +2 -2
- package/build/state/composite-reducer.cjs.map +2 -2
- package/build/state/media-editor-state-provider.cjs.map +2 -2
- package/build/state/types.cjs.map +2 -2
- package/build/state/use-media-editor-state.cjs.map +2 -2
- package/build/store/index.cjs.map +2 -2
- package/build/store/reducer.cjs.map +2 -2
- package/build/store/selectors.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +54 -45
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +2 -2
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
- package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-modal/build-modifiers.mjs.map +2 -2
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-provider/index.mjs.map +2 -2
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/media-preview/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +2 -2
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
- package/build-module/image-editor/core/camera.mjs.map +2 -2
- package/build-module/image-editor/core/constants.mjs.map +2 -2
- package/build-module/image-editor/core/containment.mjs.map +2 -2
- package/build-module/image-editor/core/crop-rect.mjs.map +2 -2
- package/build-module/image-editor/core/export/canvas-renderer.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.map +2 -2
- package/build-module/image-editor/core/setter-helpers.mjs.map +2 -2
- package/build-module/image-editor/core/source-region.mjs.map +2 -2
- package/build-module/image-editor/core/state.mjs.map +2 -2
- package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
- package/build-module/image-editor/core/transform-style.mjs.map +2 -2
- package/build-module/image-editor/core/transforms/pipeline.mjs.map +2 -2
- package/build-module/image-editor/core/viewport-state.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper-provider.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +2 -2
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
- package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/components/viewport-provider.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +2 -2
- package/build-module/lock-unlock.mjs.map +2 -2
- package/build-module/private-apis.mjs.map +2 -2
- package/build-module/state/composite-reducer.mjs.map +2 -2
- package/build-module/state/media-editor-state-provider.mjs.map +2 -2
- package/build-module/state/types.mjs.map +2 -2
- package/build-module/state/use-media-editor-state.mjs.map +2 -2
- package/build-module/store/index.mjs.map +2 -2
- package/build-module/store/reducer.mjs.map +2 -2
- package/build-module/store/selectors.mjs.map +2 -2
- package/build-style/style-rtl.css +2 -2
- package/build-style/style.css +2 -2
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
- package/build-types/components/media-editor/use-save-media-editor.d.ts +0 -3
- package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
- package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/build-modifiers.d.ts +0 -3
- package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/components/media-preview/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/index.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/stories/index.story.d.ts +0 -6
- package/build-types/components/rotation-ruler/stories/index.story.d.ts.map +1 -1
- package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -1
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
- package/build-types/image-editor/core/camera.d.ts +0 -3
- package/build-types/image-editor/core/camera.d.ts.map +1 -1
- package/build-types/image-editor/core/constants.d.ts +0 -3
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/containment.d.ts +0 -3
- package/build-types/image-editor/core/containment.d.ts.map +1 -1
- package/build-types/image-editor/core/crop-rect.d.ts +0 -3
- package/build-types/image-editor/core/crop-rect.d.ts.map +1 -1
- package/build-types/image-editor/core/export/canvas-renderer.d.ts +0 -3
- package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts +0 -3
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/core/math/sanitize.d.ts +0 -3
- package/build-types/image-editor/core/math/sanitize.d.ts.map +1 -1
- package/build-types/image-editor/core/setter-helpers.d.ts +0 -3
- package/build-types/image-editor/core/setter-helpers.d.ts.map +1 -1
- package/build-types/image-editor/core/source-region.d.ts +0 -3
- package/build-types/image-editor/core/source-region.d.ts.map +1 -1
- package/build-types/image-editor/core/state.d.ts +0 -3
- package/build-types/image-editor/core/state.d.ts.map +1 -1
- package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
- package/build-types/image-editor/core/transform-style.d.ts +0 -3
- package/build-types/image-editor/core/transform-style.d.ts.map +1 -1
- package/build-types/image-editor/core/transforms/pipeline.d.ts +0 -3
- package/build-types/image-editor/core/transforms/pipeline.d.ts.map +1 -1
- package/build-types/image-editor/core/viewport-state.d.ts +0 -3
- package/build-types/image-editor/core/viewport-state.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper-provider.d.ts +0 -3
- package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts +0 -6
- 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 +0 -3
- package/build-types/image-editor/react/components/overlays/dimensions-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +0 -3
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +0 -3
- package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +0 -3
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/components/viewport-provider.d.ts +0 -3
- package/build-types/image-editor/react/components/viewport-provider.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts +0 -3
- package/build-types/image-editor/react/hooks/build-cropper-setters.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +0 -3
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts +0 -3
- package/build-types/image-editor/react/hooks/use-cropper-reducer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts +0 -3
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-transform-style.d.ts +0 -3
- package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-viewport-state.d.ts +0 -3
- package/build-types/image-editor/react/hooks/use-viewport-state.d.ts.map +1 -1
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts +0 -6
- package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
- package/build-types/lock-unlock.d.ts.map +1 -1
- package/build-types/private-apis.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts +0 -3
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/media-editor-state-provider.d.ts +0 -3
- package/build-types/state/media-editor-state-provider.d.ts.map +1 -1
- package/build-types/state/types.d.ts +0 -3
- package/build-types/state/types.d.ts.map +1 -1
- package/build-types/state/use-media-editor-state.d.ts +0 -3
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/build-types/store/index.d.ts.map +1 -1
- package/build-types/store/reducer.d.ts +0 -3
- package/build-types/store/reducer.d.ts.map +1 -1
- package/build-types/store/selectors.d.ts +0 -3
- package/build-types/store/selectors.d.ts.map +1 -1
- package/package.json +20 -20
- package/src/components/media-editor/index.tsx +83 -78
- package/src/components/media-editor/test/use-crop-options.tsx +0 -7
- package/src/components/media-editor/use-crop-options.ts +0 -7
- package/src/components/media-editor/use-save-media-editor.ts +0 -7
- package/src/components/media-editor-canvas/index.tsx +0 -11
- package/src/components/media-editor-crop-panel/index.tsx +0 -7
- package/src/components/media-editor-crop-panel/test/index.tsx +0 -7
- package/src/components/media-editor-fine-rotation/index.tsx +0 -7
- package/src/components/media-editor-image-controls/index.tsx +0 -7
- package/src/components/media-editor-image-controls/test/index.tsx +0 -7
- package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +0 -3
- package/src/components/media-editor-modal/build-modifiers.ts +0 -3
- package/src/components/media-editor-modal/index.tsx +0 -7
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -8
- package/src/components/media-editor-modal/test/index.tsx +0 -11
- package/src/components/media-editor-provider/index.tsx +0 -3
- package/src/components/media-form/index.tsx +0 -7
- package/src/components/media-preview/index.tsx +0 -7
- package/src/components/rotation-ruler/index.tsx +0 -11
- package/src/components/rotation-ruler/stories/index.story.tsx +0 -11
- package/src/components/rotation-ruler/test/index.tsx +0 -7
- package/src/components/rotation-ruler/use-ruler-drag.ts +0 -3
- package/src/hooks/use-crop-gesture-handlers.ts +0 -7
- package/src/image-editor/core/camera.ts +0 -7
- package/src/image-editor/core/constants.ts +0 -7
- package/src/image-editor/core/containment.ts +0 -7
- package/src/image-editor/core/crop-rect.ts +0 -3
- package/src/image-editor/core/export/canvas-renderer.ts +0 -3
- package/src/image-editor/core/export/test/canvas-renderer.ts +0 -3
- package/src/image-editor/core/interaction-controller.ts +0 -3
- package/src/image-editor/core/math/sanitize.ts +0 -3
- package/src/image-editor/core/math/test/sanitize.ts +0 -3
- package/src/image-editor/core/setter-helpers.ts +0 -3
- package/src/image-editor/core/source-region.ts +0 -7
- package/src/image-editor/core/state.ts +0 -3
- package/src/image-editor/core/stencil-math.ts +0 -3
- package/src/image-editor/core/test/interaction-controller.ts +0 -3
- package/src/image-editor/core/test/preview-export-parity.ts +0 -1
- package/src/image-editor/core/test/stencil-math.ts +0 -3
- package/src/image-editor/core/transform-style.ts +0 -3
- package/src/image-editor/core/transforms/pipeline.ts +0 -3
- package/src/image-editor/core/transforms/test/pipeline.ts +0 -3
- package/src/image-editor/core/viewport-state.ts +0 -3
- package/src/image-editor/react/components/cropper-provider.tsx +0 -7
- package/src/image-editor/react/components/cropper.tsx +0 -12
- package/src/image-editor/react/components/overlays/dimensions-overlay.tsx +0 -7
- package/src/image-editor/react/components/overlays/dimming-overlay.tsx +0 -3
- package/src/image-editor/react/components/overlays/grid-overlay.tsx +0 -3
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +0 -7
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +0 -7
- package/src/image-editor/react/components/test/cropper.tsx +0 -7
- package/src/image-editor/react/components/viewport-provider.tsx +0 -7
- package/src/image-editor/react/hooks/build-cropper-setters.ts +0 -3
- package/src/image-editor/react/hooks/test/use-aria-announcer.ts +0 -11
- package/src/image-editor/react/hooks/test/use-cropper-reducer.ts +0 -7
- package/src/image-editor/react/hooks/test/use-interaction.ts +0 -7
- package/src/image-editor/react/hooks/test/use-transform-style.ts +0 -7
- package/src/image-editor/react/hooks/use-aria-announcer.ts +0 -7
- package/src/image-editor/react/hooks/use-cropper-reducer.ts +0 -7
- package/src/image-editor/react/hooks/use-interaction.ts +0 -7
- package/src/image-editor/react/hooks/use-transform-style.ts +0 -7
- package/src/image-editor/react/hooks/use-viewport-state.ts +0 -7
- package/src/image-editor/stories/rectangle-crop.story.tsx +0 -11
- package/src/lock-unlock.ts +0 -3
- package/src/private-apis.ts +0 -3
- package/src/state/composite-reducer.ts +0 -3
- package/src/state/media-editor-state-provider.tsx +0 -7
- package/src/state/test/use-media-editor-state.ts +0 -7
- package/src/state/types.ts +0 -3
- package/src/state/use-media-editor-state.ts +0 -7
- package/src/store/index.ts +0 -7
- package/src/store/reducer.ts +0 -3
- package/src/store/selectors.ts +0 -3
- package/src/store/test/reducer.ts +0 -3
- package/tsconfig.tsbuildinfo +1 -1
- 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/components/media-editor-transform-controls/index.d.ts +0 -21
- package/build-types/components/media-editor-transform-controls/index.d.ts.map +0 -1
- package/build-types/components/media-form/sidebar-datetime-view.d.ts +0 -10
- package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +0 -1
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -108
- package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/image-editor/core/math/sanitize.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["import type { CropperState, NormalizedRect, Size } from '../types';\n\n/**\n * Magnitude beyond which any cropper-math input is treated as corrupt.\n * Picks a value well above any realistic image/rotation/zoom/pan but far\n * below `Number.MAX_VALUE`, so multiplications inside trig and matrix code\n * can't overflow to `Infinity`.\n */\nconst MAX_SAFE_MAGNITUDE = 1e6;\n\n/**\n * Returns `value` if it's finite and within `[-MAX_SAFE_MAGNITUDE,\n * MAX_SAFE_MAGNITUDE]`, otherwise `fallback`. Stricter than `Number.isFinite`\n * because extreme finite magnitudes (e.g. `Number.MAX_VALUE`) overflow\n * downstream trig/matrix math even though the spec reports them as finite.\n *\n * Note: sub-normal denormals (e.g. `Number.MIN_VALUE`) are accepted by this\n * function — they don't overflow on their own. Protection against divisions\n * by sub-normal values lives in `sanitizeCropperState`'s zoom guard.\n *\n * @param value The candidate value.\n * @param fallback The replacement to return when `value` is unsafe.\n * @return Either `value` or `fallback`.\n */\nexport function safeBoundedNumber( value: number, fallback: number ): number {\n\tif ( ! Number.isFinite( value ) ) {\n\t\treturn fallback;\n\t}\n\tif ( Math.abs( value ) > MAX_SAFE_MAGNITUDE ) {\n\t\treturn fallback;\n\t}\n\treturn value;\n}\n\n/**\n * Returns `true` when `value` is finite and within `[-MAX_SAFE_MAGNITUDE,\n * MAX_SAFE_MAGNITUDE]`. The predicate companion to {@link safeBoundedNumber}\n * for callers that need to short-circuit rather than substitute a fallback.\n *\n * @param value The candidate value.\n * @return Whether `value` is safe to use in math.\n */\nexport function isSafeNumber( value: number ): boolean {\n\treturn Number.isFinite( value ) && Math.abs( value ) <= MAX_SAFE_MAGNITUDE;\n}\n\n/**\n * Returns `true` when both dimensions are finite and within the safe\n * range `[Number.EPSILON, MAX_SAFE_MAGNITUDE]`. The lower bound matters\n * because sub-normal dimensions (e.g. `Number.MIN_VALUE`) pass `> 0` but\n * make `container / dim` overflow to `Infinity` downstream in\n * `getImageFit` / `createCamera`. Use this to short-circuit math that\n * would otherwise produce non-finite output.\n *\n * @param size The size to check.\n * @return Whether `size` is safe to use in math.\n */\nexport function isValidSize( size: Size ): boolean {\n\treturn (\n\t\tNumber.isFinite( size.width ) &&\n\t\tNumber.isFinite( size.height ) &&\n\t\tsize.width >= Number.EPSILON &&\n\t\tsize.height >= Number.EPSILON &&\n\t\tsize.width <= MAX_SAFE_MAGNITUDE &&\n\t\tsize.height <= MAX_SAFE_MAGNITUDE\n\t);\n}\n\n/**\n * Returns a copy of `rect` with every numeric field replaced by a safe\n * value via {@link safeBoundedNumber}. Returns the same reference when no\n * field needed substitution so callers can keep their reference-equality\n * short-circuits intact for clean inputs.\n *\n * @param rect The rect to sanitize.\n * @return A rect with all numeric fields finite.\n */\nexport function sanitizeRect( rect: NormalizedRect ): NormalizedRect {\n\tconst x = safeBoundedNumber( rect.x, 0 );\n\tconst y = safeBoundedNumber( rect.y, 0 );\n\tconst width = safeBoundedNumber( rect.width, 0 );\n\tconst height = safeBoundedNumber( rect.height, 0 );\n\tif (\n\t\tx === rect.x &&\n\t\ty === rect.y &&\n\t\twidth === rect.width &&\n\t\theight === rect.height\n\t) {\n\t\treturn rect;\n\t}\n\treturn { x, y, width, height };\n}\n\n/**\n * Returns a copy of `state` with the cropper-math numeric fields replaced\n * by safe defaults. Covers `pan`, `zoom`, `rotation`, the matching base\n * pose, and `cropRect`. The `state.image` numeric fields (naturalWidth /\n * naturalHeight) are intentionally NOT sanitized — image dimensions reach\n * the math layer as a separate `imageSize` argument, and cloning the\n * image object here would break the reducer's reference-equality\n * short-circuit.\n *\n * Returns the same `state` reference when no field needed substitution\n * so callers in hot paths (every pointermove via `restrictPanZoom`) can\n * keep their reference-equality optimizations. Sub-object references\n * (`pan`, `basePan`, `cropRect`) are also preserved when their fields\n * are individually clean.\n *\n * @param state The cropper state to sanitize.\n * @return A cropper state with the math-relevant numeric fields finite.\n */\nexport function sanitizeCropperState( state: CropperState ): CropperState {\n\tconst panX = safeBoundedNumber( state.pan.x, 0 );\n\tconst panY = safeBoundedNumber( state.pan.y, 0 );\n\tconst basePanX = safeBoundedNumber( state.basePan.x, 0 );\n\tconst basePanY = safeBoundedNumber( state.basePan.y, 0 );\n\tconst rotation = safeBoundedNumber( state.rotation, 0 );\n\tconst baseRotation = safeBoundedNumber( state.baseRotation, 0 );\n\tconst rawZoom = safeBoundedNumber( state.zoom, 1 );\n\t// Zoom must be strictly positive AND large enough that 1/zoom doesn't\n\t// overflow. Sub-normals (e.g. Number.MIN_VALUE) pass `> 0` but make\n\t// division explode, so guard against them with Number.EPSILON.\n\tconst zoom = rawZoom >= Number.EPSILON ? rawZoom : 1;\n\tconst rawBaseZoom = safeBoundedNumber( state.baseZoom, 1 );\n\tconst baseZoom = rawBaseZoom >= Number.EPSILON ? rawBaseZoom : 1;\n\tconst cropRect = sanitizeRect( state.cropRect );\n\n\tconst panUnchanged = panX === state.pan.x && panY === state.pan.y;\n\tconst basePanUnchanged =\n\t\tbasePanX === state.basePan.x && basePanY === state.basePan.y;\n\n\tif (\n\t\tpanUnchanged &&\n\t\tbasePanUnchanged &&\n\t\tzoom === state.zoom &&\n\t\tbaseZoom === state.baseZoom &&\n\t\trotation === state.rotation &&\n\t\tbaseRotation === state.baseRotation &&\n\t\tcropRect === state.cropRect\n\t) {\n\t\treturn state;\n\t}\n\treturn {\n\t\t...state,\n\t\tpan: panUnchanged ? state.pan : { x: panX, y: panY },\n\t\tzoom,\n\t\trotation,\n\t\tbasePan: basePanUnchanged\n\t\t\t? state.basePan\n\t\t\t: { x: basePanX, y: basePanY },\n\t\tbaseZoom,\n\t\tbaseRotation,\n\t\tcropRect,\n\t};\n}\n"],
|
|
5
|
+
"mappings": ";AAQA,IAAM,qBAAqB;AAgBpB,SAAS,kBAAmB,OAAe,UAA2B;AAC5E,MAAK,CAAE,OAAO,SAAU,KAAM,GAAI;AACjC,WAAO;AAAA,EACR;AACA,MAAK,KAAK,IAAK,KAAM,IAAI,oBAAqB;AAC7C,WAAO;AAAA,EACR;AACA,SAAO;AACR;AAUO,SAAS,aAAc,OAAyB;AACtD,SAAO,OAAO,SAAU,KAAM,KAAK,KAAK,IAAK,KAAM,KAAK;AACzD;AAaO,SAAS,YAAa,MAAsB;AAClD,SACC,OAAO,SAAU,KAAK,KAAM,KAC5B,OAAO,SAAU,KAAK,MAAO,KAC7B,KAAK,SAAS,OAAO,WACrB,KAAK,UAAU,OAAO,WACtB,KAAK,SAAS,sBACd,KAAK,UAAU;AAEjB;AAWO,SAAS,aAAc,MAAuC;AACpE,QAAM,IAAI,kBAAmB,KAAK,GAAG,CAAE;AACvC,QAAM,IAAI,kBAAmB,KAAK,GAAG,CAAE;AACvC,QAAM,QAAQ,kBAAmB,KAAK,OAAO,CAAE;AAC/C,QAAM,SAAS,kBAAmB,KAAK,QAAQ,CAAE;AACjD,MACC,MAA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1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/image-editor/core/setter-helpers.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["import type { CropperState, NormalizedPoint, CropperAction } from './types';\nimport { MAX_ZOOM } from './constants';\nimport { getMinZoom, restrictPanZoom } from './containment';\n\n/**\n * Build the reducer action that produces a \"cursorless\" zoom — zoom\n * anchored at the current crop-rect center, with pan clamped to keep\n * the image covering the crop.\n *\n * The slider, the `+`/`-` keys, and the toolbar buttons have no\n * natural focal point (unlike wheel/pinch zoom), so they default to\n * the crop center. Returns `null` when the clamped zoom matches the\n * current zoom (no-op).\n *\n * Extracted so multiple hooks (the pure `useCropperReducer` and the\n * composite `useMediaEditorState`) can share the math without\n * duplicating it.\n *\n * @param state The current cropper state.\n * @param requestedZoom The zoom value the caller asked for.\n * @return A `SET_ZOOM_AT_POINT` action, or `null` if no change is required.\n */\nexport function buildFocalPointZoomAction(\n\tstate: CropperState,\n\trequestedZoom: number\n): CropperAction | null {\n\tconst clampedZoom = Math.min(\n\t\tMAX_ZOOM,\n\t\tMath.max( getMinZoom( state ), requestedZoom )\n\t);\n\tif ( clampedZoom === state.zoom ) {\n\t\treturn null;\n\t}\n\t// Crop center expressed in the same coord system as `state.pan`:\n\t// container-center-relative, normalized by image rendered size.\n\t// `cropRect` is image-top-left-origin (0–1), so subtract 0.5\n\t// to recenter.\n\tconst { cropRect } = state;\n\tconst focalNormX = cropRect.x + cropRect.width / 2 - 0.5;\n\tconst focalNormY = cropRect.y + cropRect.height / 2 - 0.5;\n\tconst zoomRatio = 1 - clampedZoom / state.zoom;\n\tconst newPanX = state.pan.x + ( focalNormX - state.pan.x ) * zoomRatio;\n\tconst newPanY = state.pan.y + ( focalNormY - state.pan.y ) * zoomRatio;\n\tconst imageSize: { width: number; height: number } = state.image\n\t\t? {\n\t\t\t\twidth: state.image.naturalWidth,\n\t\t\t\theight: state.image.naturalHeight,\n\t\t }\n\t\t: { width: 1, height: 1 };\n\tconst { pan: clampedPan }: { pan: NormalizedPoint } = restrictPanZoom(\n\t\t{\n\t\t\t...state,\n\t\t\tzoom: clampedZoom,\n\t\t\tpan: { x: newPanX, y: newPanY },\n\t\t},\n\t\timageSize,\n\t\tstate.cropRect\n\t);\n\treturn {\n\t\ttype: 'SET_ZOOM_AT_POINT',\n\t\tpayload: { zoom: clampedZoom, pan: clampedPan },\n\t};\n}\n"],
|
|
5
|
+
"mappings": ";AACA,SAAS,gBAAgB;AACzB,SAAS,YAAY,uBAAuB;AAoBrC,SAAS,0BACf,OACA,eACuB;AACvB,QAAM,cAAc,KAAK;AAAA,IACxB;AAAA,IACA,KAAK,IAAK,WAAY,KAAM,GAAG,aAAc;AAAA,EAC9C;AACA,MAAK,gBAAgB,MAAM,MAAO;AACjC,WAAO;AAAA,EACR;AAKA,QAAM,EAAE,SAAS,IAAI;AACrB,QAAM,aAAa,SAAS,IAAI,SAAS,QAAQ,IAAI;AACrD,QAAM,aAAa,SAAS,IAAI,SAAS,SAAS,IAAI;AACtD,QAAM,YAAY,IAAI,cAAc,MAAM;AAC1C,QAAM,UAAU,MAAM,IAAI,KAAM,aAAa,MAAM,IAAI,KAAM;AAC7D,QAAM,UAAU,MAAM,IAAI,KAAM,aAAa,MAAM,IAAI,KAAM;AAC7D,QAAM,YAA+C,MAAM,QACxD;AAAA,IACA,OAAO,MAAM,MAAM;AAAA,IACnB,QAAQ,MAAM,MAAM;AAAA,EACpB,IACA,EAAE,OAAO,GAAG,QAAQ,EAAE;AACzB,QAAM,EAAE,KAAK,WAAW,IAA8B;AAAA,IACrD;AAAA,MACC,GAAG;AAAA,MACH,MAAM;AAAA,MACN,KAAK,EAAE,GAAG,SAAS,GAAG,QAAQ;AAAA,IAC/B;AAAA,IACA;AAAA,IACA,MAAM;AAAA,EACP;AACA,SAAO;AAAA,IACN,MAAM;AAAA,IACN,SAAS,EAAE,MAAM,aAAa,KAAK,WAAW;AAAA,EAC/C;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/image-editor/core/source-region.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport { mat2d, vec2 } from 'gl-matrix';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tCropperState,\n\tHandlePosition,\n\tNormalizedRect,\n\tSize,\n} from './types';\nimport { createCamera, getRotatedBBox, getVisibleBounds } from './camera';\nimport { isValidSize, sanitizeCropperState } from './math/sanitize';\nimport { normalizeRotation } from './math/rotation';\n\n/**\n * The selected image region in source-pixel coordinates.\n */\nexport interface SourceRegion {\n\t/** X offset in source pixels. */\n\tx: number;\n\t/** Y offset in source pixels. */\n\ty: number;\n\t/** Width in source pixels. */\n\twidth: number;\n\t/** Height in source pixels. */\n\theight: number;\n\t/** Rotation in degrees (0-360). */\n\trotation: number;\n\t/** Flip state. */\n\tflip: { horizontal: boolean; vertical: boolean };\n\t/** Zoom factor applied. */\n\tzoom: number;\n}\n\n/**\n * Get the selected image region in source-pixel coordinates.\n *\n * Converts the current crop state (normalized visual space) to the actual\n * region of the original image that's selected. This is the bridge between\n * the cropper's internal coordinate system and external tools (image\n * processing libraries, AI APIs, server-side processing) that work in\n * source-pixel coordinates.\n *\n * The returned rectangle accounts for pan, zoom, and the crop rect position,\n * but expresses the crop in the unrotated image's coordinate space. Rotation\n * and flip are included as separate fields since they represent transforms,\n * not a region.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @return The selected region in source pixels plus rotation/flip metadata.\n */\nexport function getSourceRegion(\n\tstate: CropperState,\n\timageSize: Size\n): SourceRegion {\n\t// Sanitize before the zero-size short-circuit so the early return\n\t// doesn't leak raw non-finite state.rotation or state.zoom out\n\t// through the metadata fields. (state.flip is boolean and isn't\n\t// at risk of NaN propagation.)\n\tconst safeState = sanitizeCropperState( state );\n\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn {\n\t\t\tx: 0,\n\t\t\ty: 0,\n\t\t\twidth: 0,\n\t\t\theight: 0,\n\t\t\trotation: safeState.rotation,\n\t\t\tflip: { ...safeState.flip },\n\t\t\tzoom: safeState.zoom,\n\t\t};\n\t}\n\n\t// Use a synthetic 1:1 container so the camera maps normalized coords\n\t// to a known pixel space. The container size cancels out.\n\tconst syntheticContainer: Size = { width: 1000, height: 1000 };\n\tconst camera = createCamera( safeState, syntheticContainer, imageSize );\n\n\t// Inverse camera maps screen pixels back to normalized [0,1] world coords.\n\tconst inv = mat2d.create();\n\tmat2d.invert( inv, camera );\n\n\t// The crop rect center in screen space. We need the base camera\n\t// (zoom=1, no pan) to locate the visual bounds, then place the\n\t// crop rect within them.\n\tconst baseCamera = createCamera(\n\t\t{ ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },\n\t\tsyntheticContainer,\n\t\timageSize\n\t);\n\tconst visibleBounds = getVisibleBounds( baseCamera );\n\n\tconst cropRect = safeState.cropRect;\n\tconst cropCenterScreenX =\n\t\tvisibleBounds.left +\n\t\t( cropRect.x + cropRect.width / 2 ) * visibleBounds.width;\n\tconst cropCenterScreenY =\n\t\tvisibleBounds.top +\n\t\t( cropRect.y + cropRect.height / 2 ) * visibleBounds.height;\n\n\t// Transform crop center through inverse camera to get source position.\n\tconst srcCenter = vec2.create();\n\tvec2.transformMat2d(\n\t\tsrcCenter,\n\t\t[ cropCenterScreenX, cropCenterScreenY ],\n\t\tinv\n\t);\n\n\t// Crop rect size in the snap-rotation visual space, divided by zoom\n\t// for source-pixel dimensions. Matches the stencil's reference frame.\n\tconst snapRotation = Math.round( safeState.rotation / 90 ) * 90;\n\tconst { width: rotW, height: rotH } = getRotatedBBox(\n\t\timageSize.width,\n\t\timageSize.height,\n\t\tsnapRotation\n\t);\n\tconst sourceW = ( cropRect.width * rotW ) / safeState.zoom;\n\tconst sourceH = ( cropRect.height * rotH ) / safeState.zoom;\n\n\treturn {\n\t\tx: srcCenter[ 0 ] * imageSize.width - sourceW / 2,\n\t\ty: srcCenter[ 1 ] * imageSize.height - sourceH / 2,\n\t\twidth: sourceW,\n\t\theight: sourceH,\n\t\trotation: safeState.rotation,\n\t\tflip: { ...safeState.flip },\n\t\tzoom: safeState.zoom,\n\t};\n}\n\n/**\n * Convert a source-pixel region back to the cropper's normalized crop rect.\n *\n * The math intentionally mirrors `getSourceRegion`: map the requested source\n * center through the current camera, then express that screen point inside the\n * same base visible bounds used to position crop overlays.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @param region The source-pixel region to represent as a crop rect.\n * @return The normalized crop rect, or null when inputs cannot be mapped.\n */\nfunction getCropRectFromSourceRegion(\n\tstate: CropperState,\n\timageSize: Size,\n\tregion: SourceRegion\n): NormalizedRect | null {\n\tif (\n\t\t! isValidSize( imageSize ) ||\n\t\tregion.width <= 0 ||\n\t\tregion.height <= 0\n\t) {\n\t\treturn null;\n\t}\n\n\tconst safeState = sanitizeCropperState( state );\n\tconst syntheticContainer: Size = { width: 1000, height: 1000 };\n\tconst baseCamera = createCamera(\n\t\t{ ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },\n\t\tsyntheticContainer,\n\t\timageSize\n\t);\n\tconst visibleBounds = getVisibleBounds( baseCamera );\n\tif ( visibleBounds.width <= 0 || visibleBounds.height <= 0 ) {\n\t\treturn null;\n\t}\n\n\tconst sourceCenter = vec2.fromValues(\n\t\t( region.x + region.width / 2 ) / imageSize.width,\n\t\t( region.y + region.height / 2 ) / imageSize.height\n\t);\n\tconst screenCenter = vec2.create();\n\tconst camera = createCamera( safeState, syntheticContainer, imageSize );\n\tvec2.transformMat2d( screenCenter, sourceCenter, camera );\n\n\tconst snapRotation = Math.round( safeState.rotation / 90 ) * 90;\n\tconst { width: rotW, height: rotH } = getRotatedBBox(\n\t\timageSize.width,\n\t\timageSize.height,\n\t\tsnapRotation\n\t);\n\tif ( rotW <= 0 || rotH <= 0 ) {\n\t\treturn null;\n\t}\n\n\tconst width = ( region.width * safeState.zoom ) / rotW;\n\tconst height = ( region.height * safeState.zoom ) / rotH;\n\tconst centerX =\n\t\t( screenCenter[ 0 ] - visibleBounds.left ) / visibleBounds.width;\n\tconst centerY =\n\t\t( screenCenter[ 1 ] - visibleBounds.top ) / visibleBounds.height;\n\n\treturn {\n\t\tx: centerX - width / 2,\n\t\ty: centerY - height / 2,\n\t\twidth,\n\t\theight,\n\t};\n}\n\ninterface SourcePixelSnapEdges {\n\tleft: boolean;\n\ttop: boolean;\n\tright: boolean;\n\tbottom: boolean;\n}\n\nconst CARDINAL_ROTATION_EPSILON = 1e-6;\n\n/**\n * Check whether a rotation is effectively at a 90-degree stop.\n *\n * @param rotation The rotation angle in degrees.\n * @return Whether the rotation is close enough to a cardinal angle.\n */\nfunction isCardinalRotation( rotation: number ): boolean {\n\tconst normalizedRotation = normalizeRotation( rotation );\n\tconst nearestCardinal = Math.round( normalizedRotation / 90 ) * 90;\n\treturn (\n\t\tMath.abs( normalizedRotation - nearestCardinal ) <\n\t\tCARDINAL_ROTATION_EPSILON\n\t);\n}\n\n/**\n * Snap a crop rect so the selected source-region edges land on whole pixels.\n *\n * This is a display-time affordance for visible pixel grids; it does not alter\n * pan/zoom/rotation. Callers decide when snapping is appropriate.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @param cropRect The candidate crop rect from resize math.\n * @param handle The active resize handle. Only edges moved by the handle snap.\n * @return A crop rect whose selected source-region edges are whole pixels.\n */\nexport function snapCropRectToSourcePixels(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect,\n\thandle: HandlePosition\n): NormalizedRect {\n\tconst safeState = sanitizeCropperState( state );\n\tif ( ! isCardinalRotation( safeState.rotation ) ) {\n\t\treturn cropRect;\n\t}\n\n\treturn snapCropRectEdgesToSourcePixels( safeState, imageSize, cropRect, {\n\t\tleft: handle.includes( 'w' ),\n\t\ttop: handle.includes( 'n' ),\n\t\tright: handle.includes( 'e' ),\n\t\tbottom: handle.includes( 's' ),\n\t} );\n}\n\n/**\n * Snap all source-region edges for a crop rect to the nearest whole pixels.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @param cropRect The crop rect to align to the source-pixel grid.\n * @return A crop rect whose source-region edges are whole pixels.\n */\nexport function snapCropRectToSourcePixelGrid(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect\n): NormalizedRect {\n\treturn snapCropRectEdgesToSourcePixels( state, imageSize, cropRect, {\n\t\tleft: true,\n\t\ttop: true,\n\t\tright: true,\n\t\tbottom: true,\n\t} );\n}\n\nfunction snapCropRectEdgesToSourcePixels(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect,\n\tedges: SourcePixelSnapEdges\n): NormalizedRect {\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn cropRect;\n\t}\n\n\tconst stateWithCrop = sanitizeCropperState( { ...state, cropRect } );\n\tconst region = getSourceRegion( stateWithCrop, imageSize );\n\tconst shouldSnapX = edges.left || edges.right;\n\tconst shouldSnapY = edges.top || edges.bottom;\n\tconst left = edges.left ? Math.round( region.x ) : region.x;\n\tconst top = edges.top ? Math.round( region.y ) : region.y;\n\tconst right = edges.right\n\t\t? Math.round( region.x + region.width )\n\t\t: region.x + region.width;\n\tconst bottom = edges.bottom\n\t\t? Math.round( region.y + region.height )\n\t\t: region.y + region.height;\n\tif ( right <= left || bottom <= top ) {\n\t\treturn cropRect;\n\t}\n\n\tconst snappedCropRect = getCropRectFromSourceRegion(\n\t\tstateWithCrop,\n\t\timageSize,\n\t\t{\n\t\t\t...region,\n\t\t\tx: left,\n\t\t\ty: top,\n\t\t\twidth: right - left,\n\t\t\theight: bottom - top,\n\t\t}\n\t);\n\tif ( ! snappedCropRect ) {\n\t\treturn cropRect;\n\t}\n\n\treturn {\n\t\tx: shouldSnapX ? snappedCropRect.x : cropRect.x,\n\t\ty: shouldSnapY ? snappedCropRect.y : cropRect.y,\n\t\twidth: shouldSnapX ? snappedCropRect.width : cropRect.width,\n\t\theight: shouldSnapY ? snappedCropRect.height : cropRect.height,\n\t};\n}\n\n/**\n * The selected crop region expressed as percentages of the source image.\n */\nexport interface SourceRegionPercent {\n\t/** X offset as a percentage (0–100) of the source image width. */\n\tx: number;\n\t/** Y offset as a percentage (0–100) of the source image height. */\n\ty: number;\n\t/** Width as a percentage (0–100) of the source image width. */\n\twidth: number;\n\t/** Height as a percentage (0–100) of the source image height. */\n\theight: number;\n}\n\n/**\n * Get the selected image region as percentages of the source image dimensions.\n *\n * Returns `{ x, y, width, height }` where each value is a percentage (0–100)\n * of the source image's natural width or height. This format is compatible\n * with the WordPress REST API attachments `/edit` endpoint and CSS-based\n * crop workflows.\n *\n * Internally delegates to `getSourceRegion` and divides by the image\n * dimensions, so accuracy is identical.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @return The crop region as percentages (0–100).\n */\nexport function getSourceRegionPercent(\n\tstate: CropperState,\n\timageSize: Size\n): SourceRegionPercent {\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn { x: 0, y: 0, width: 0, height: 0 };\n\t}\n\tconst region = getSourceRegion( state, imageSize );\n\treturn {\n\t\tx: ( region.x / imageSize.width ) * 100,\n\t\ty: ( region.y / imageSize.height ) * 100,\n\t\twidth: ( region.width / imageSize.width ) * 100,\n\t\theight: ( region.height / imageSize.height ) * 100,\n\t};\n}\n"],
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["import { mat2d, vec2 } from 'gl-matrix';\nimport type {\n\tCropperState,\n\tHandlePosition,\n\tNormalizedRect,\n\tSize,\n} from './types';\nimport { createCamera, getRotatedBBox, getVisibleBounds } from './camera';\nimport { isValidSize, sanitizeCropperState } from './math/sanitize';\nimport { normalizeRotation } from './math/rotation';\n\n/**\n * The selected image region in source-pixel coordinates.\n */\nexport interface SourceRegion {\n\t/** X offset in source pixels. */\n\tx: number;\n\t/** Y offset in source pixels. */\n\ty: number;\n\t/** Width in source pixels. */\n\twidth: number;\n\t/** Height in source pixels. */\n\theight: number;\n\t/** Rotation in degrees (0-360). */\n\trotation: number;\n\t/** Flip state. */\n\tflip: { horizontal: boolean; vertical: boolean };\n\t/** Zoom factor applied. */\n\tzoom: number;\n}\n\n/**\n * Get the selected image region in source-pixel coordinates.\n *\n * Converts the current crop state (normalized visual space) to the actual\n * region of the original image that's selected. This is the bridge between\n * the cropper's internal coordinate system and external tools (image\n * processing libraries, AI APIs, server-side processing) that work in\n * source-pixel coordinates.\n *\n * The returned rectangle accounts for pan, zoom, and the crop rect position,\n * but expresses the crop in the unrotated image's coordinate space. Rotation\n * and flip are included as separate fields since they represent transforms,\n * not a region.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @return The selected region in source pixels plus rotation/flip metadata.\n */\nexport function getSourceRegion(\n\tstate: CropperState,\n\timageSize: Size\n): SourceRegion {\n\t// Sanitize before the zero-size short-circuit so the early return\n\t// doesn't leak raw non-finite state.rotation or state.zoom out\n\t// through the metadata fields. (state.flip is boolean and isn't\n\t// at risk of NaN propagation.)\n\tconst safeState = sanitizeCropperState( state );\n\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn {\n\t\t\tx: 0,\n\t\t\ty: 0,\n\t\t\twidth: 0,\n\t\t\theight: 0,\n\t\t\trotation: safeState.rotation,\n\t\t\tflip: { ...safeState.flip },\n\t\t\tzoom: safeState.zoom,\n\t\t};\n\t}\n\n\t// Use a synthetic 1:1 container so the camera maps normalized coords\n\t// to a known pixel space. The container size cancels out.\n\tconst syntheticContainer: Size = { width: 1000, height: 1000 };\n\tconst camera = createCamera( safeState, syntheticContainer, imageSize );\n\n\t// Inverse camera maps screen pixels back to normalized [0,1] world coords.\n\tconst inv = mat2d.create();\n\tmat2d.invert( inv, camera );\n\n\t// The crop rect center in screen space. We need the base camera\n\t// (zoom=1, no pan) to locate the visual bounds, then place the\n\t// crop rect within them.\n\tconst baseCamera = createCamera(\n\t\t{ ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },\n\t\tsyntheticContainer,\n\t\timageSize\n\t);\n\tconst visibleBounds = getVisibleBounds( baseCamera );\n\n\tconst cropRect = safeState.cropRect;\n\tconst cropCenterScreenX =\n\t\tvisibleBounds.left +\n\t\t( cropRect.x + cropRect.width / 2 ) * visibleBounds.width;\n\tconst cropCenterScreenY =\n\t\tvisibleBounds.top +\n\t\t( cropRect.y + cropRect.height / 2 ) * visibleBounds.height;\n\n\t// Transform crop center through inverse camera to get source position.\n\tconst srcCenter = vec2.create();\n\tvec2.transformMat2d(\n\t\tsrcCenter,\n\t\t[ cropCenterScreenX, cropCenterScreenY ],\n\t\tinv\n\t);\n\n\t// Crop rect size in the snap-rotation visual space, divided by zoom\n\t// for source-pixel dimensions. Matches the stencil's reference frame.\n\tconst snapRotation = Math.round( safeState.rotation / 90 ) * 90;\n\tconst { width: rotW, height: rotH } = getRotatedBBox(\n\t\timageSize.width,\n\t\timageSize.height,\n\t\tsnapRotation\n\t);\n\tconst sourceW = ( cropRect.width * rotW ) / safeState.zoom;\n\tconst sourceH = ( cropRect.height * rotH ) / safeState.zoom;\n\n\treturn {\n\t\tx: srcCenter[ 0 ] * imageSize.width - sourceW / 2,\n\t\ty: srcCenter[ 1 ] * imageSize.height - sourceH / 2,\n\t\twidth: sourceW,\n\t\theight: sourceH,\n\t\trotation: safeState.rotation,\n\t\tflip: { ...safeState.flip },\n\t\tzoom: safeState.zoom,\n\t};\n}\n\n/**\n * Convert a source-pixel region back to the cropper's normalized crop rect.\n *\n * The math intentionally mirrors `getSourceRegion`: map the requested source\n * center through the current camera, then express that screen point inside the\n * same base visible bounds used to position crop overlays.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @param region The source-pixel region to represent as a crop rect.\n * @return The normalized crop rect, or null when inputs cannot be mapped.\n */\nfunction getCropRectFromSourceRegion(\n\tstate: CropperState,\n\timageSize: Size,\n\tregion: SourceRegion\n): NormalizedRect | null {\n\tif (\n\t\t! isValidSize( imageSize ) ||\n\t\tregion.width <= 0 ||\n\t\tregion.height <= 0\n\t) {\n\t\treturn null;\n\t}\n\n\tconst safeState = sanitizeCropperState( state );\n\tconst syntheticContainer: Size = { width: 1000, height: 1000 };\n\tconst baseCamera = createCamera(\n\t\t{ ...safeState, pan: { x: 0, y: 0 }, zoom: 1 },\n\t\tsyntheticContainer,\n\t\timageSize\n\t);\n\tconst visibleBounds = getVisibleBounds( baseCamera );\n\tif ( visibleBounds.width <= 0 || visibleBounds.height <= 0 ) {\n\t\treturn null;\n\t}\n\n\tconst sourceCenter = vec2.fromValues(\n\t\t( region.x + region.width / 2 ) / imageSize.width,\n\t\t( region.y + region.height / 2 ) / imageSize.height\n\t);\n\tconst screenCenter = vec2.create();\n\tconst camera = createCamera( safeState, syntheticContainer, imageSize );\n\tvec2.transformMat2d( screenCenter, sourceCenter, camera );\n\n\tconst snapRotation = Math.round( safeState.rotation / 90 ) * 90;\n\tconst { width: rotW, height: rotH } = getRotatedBBox(\n\t\timageSize.width,\n\t\timageSize.height,\n\t\tsnapRotation\n\t);\n\tif ( rotW <= 0 || rotH <= 0 ) {\n\t\treturn null;\n\t}\n\n\tconst width = ( region.width * safeState.zoom ) / rotW;\n\tconst height = ( region.height * safeState.zoom ) / rotH;\n\tconst centerX =\n\t\t( screenCenter[ 0 ] - visibleBounds.left ) / visibleBounds.width;\n\tconst centerY =\n\t\t( screenCenter[ 1 ] - visibleBounds.top ) / visibleBounds.height;\n\n\treturn {\n\t\tx: centerX - width / 2,\n\t\ty: centerY - height / 2,\n\t\twidth,\n\t\theight,\n\t};\n}\n\ninterface SourcePixelSnapEdges {\n\tleft: boolean;\n\ttop: boolean;\n\tright: boolean;\n\tbottom: boolean;\n}\n\nconst CARDINAL_ROTATION_EPSILON = 1e-6;\n\n/**\n * Check whether a rotation is effectively at a 90-degree stop.\n *\n * @param rotation The rotation angle in degrees.\n * @return Whether the rotation is close enough to a cardinal angle.\n */\nfunction isCardinalRotation( rotation: number ): boolean {\n\tconst normalizedRotation = normalizeRotation( rotation );\n\tconst nearestCardinal = Math.round( normalizedRotation / 90 ) * 90;\n\treturn (\n\t\tMath.abs( normalizedRotation - nearestCardinal ) <\n\t\tCARDINAL_ROTATION_EPSILON\n\t);\n}\n\n/**\n * Snap a crop rect so the selected source-region edges land on whole pixels.\n *\n * This is a display-time affordance for visible pixel grids; it does not alter\n * pan/zoom/rotation. Callers decide when snapping is appropriate.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @param cropRect The candidate crop rect from resize math.\n * @param handle The active resize handle. Only edges moved by the handle snap.\n * @return A crop rect whose selected source-region edges are whole pixels.\n */\nexport function snapCropRectToSourcePixels(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect,\n\thandle: HandlePosition\n): NormalizedRect {\n\tconst safeState = sanitizeCropperState( state );\n\tif ( ! isCardinalRotation( safeState.rotation ) ) {\n\t\treturn cropRect;\n\t}\n\n\treturn snapCropRectEdgesToSourcePixels( safeState, imageSize, cropRect, {\n\t\tleft: handle.includes( 'w' ),\n\t\ttop: handle.includes( 'n' ),\n\t\tright: handle.includes( 'e' ),\n\t\tbottom: handle.includes( 's' ),\n\t} );\n}\n\n/**\n * Snap all source-region edges for a crop rect to the nearest whole pixels.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @param cropRect The crop rect to align to the source-pixel grid.\n * @return A crop rect whose source-region edges are whole pixels.\n */\nexport function snapCropRectToSourcePixelGrid(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect\n): NormalizedRect {\n\treturn snapCropRectEdgesToSourcePixels( state, imageSize, cropRect, {\n\t\tleft: true,\n\t\ttop: true,\n\t\tright: true,\n\t\tbottom: true,\n\t} );\n}\n\nfunction snapCropRectEdgesToSourcePixels(\n\tstate: CropperState,\n\timageSize: Size,\n\tcropRect: NormalizedRect,\n\tedges: SourcePixelSnapEdges\n): NormalizedRect {\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn cropRect;\n\t}\n\n\tconst stateWithCrop = sanitizeCropperState( { ...state, cropRect } );\n\tconst region = getSourceRegion( stateWithCrop, imageSize );\n\tconst shouldSnapX = edges.left || edges.right;\n\tconst shouldSnapY = edges.top || edges.bottom;\n\tconst left = edges.left ? Math.round( region.x ) : region.x;\n\tconst top = edges.top ? Math.round( region.y ) : region.y;\n\tconst right = edges.right\n\t\t? Math.round( region.x + region.width )\n\t\t: region.x + region.width;\n\tconst bottom = edges.bottom\n\t\t? Math.round( region.y + region.height )\n\t\t: region.y + region.height;\n\tif ( right <= left || bottom <= top ) {\n\t\treturn cropRect;\n\t}\n\n\tconst snappedCropRect = getCropRectFromSourceRegion(\n\t\tstateWithCrop,\n\t\timageSize,\n\t\t{\n\t\t\t...region,\n\t\t\tx: left,\n\t\t\ty: top,\n\t\t\twidth: right - left,\n\t\t\theight: bottom - top,\n\t\t}\n\t);\n\tif ( ! snappedCropRect ) {\n\t\treturn cropRect;\n\t}\n\n\treturn {\n\t\tx: shouldSnapX ? snappedCropRect.x : cropRect.x,\n\t\ty: shouldSnapY ? snappedCropRect.y : cropRect.y,\n\t\twidth: shouldSnapX ? snappedCropRect.width : cropRect.width,\n\t\theight: shouldSnapY ? snappedCropRect.height : cropRect.height,\n\t};\n}\n\n/**\n * The selected crop region expressed as percentages of the source image.\n */\nexport interface SourceRegionPercent {\n\t/** X offset as a percentage (0–100) of the source image width. */\n\tx: number;\n\t/** Y offset as a percentage (0–100) of the source image height. */\n\ty: number;\n\t/** Width as a percentage (0–100) of the source image width. */\n\twidth: number;\n\t/** Height as a percentage (0–100) of the source image height. */\n\theight: number;\n}\n\n/**\n * Get the selected image region as percentages of the source image dimensions.\n *\n * Returns `{ x, y, width, height }` where each value is a percentage (0–100)\n * of the source image's natural width or height. This format is compatible\n * with the WordPress REST API attachments `/edit` endpoint and CSS-based\n * crop workflows.\n *\n * Internally delegates to `getSourceRegion` and divides by the image\n * dimensions, so accuracy is identical.\n *\n * @param state The current cropper state.\n * @param imageSize The natural dimensions of the source image.\n * @return The crop region as percentages (0–100).\n */\nexport function getSourceRegionPercent(\n\tstate: CropperState,\n\timageSize: Size\n): SourceRegionPercent {\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn { x: 0, y: 0, width: 0, height: 0 };\n\t}\n\tconst region = getSourceRegion( state, imageSize );\n\treturn {\n\t\tx: ( region.x / imageSize.width ) * 100,\n\t\ty: ( region.y / imageSize.height ) * 100,\n\t\twidth: ( region.width / imageSize.width ) * 100,\n\t\theight: ( region.height / imageSize.height ) * 100,\n\t};\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,OAAO,YAAY;AAO5B,SAAS,cAAc,gBAAgB,wBAAwB;AAC/D,SAAS,aAAa,4BAA4B;AAClD,SAAS,yBAAyB;AAwC3B,SAAS,gBACf,OACA,WACe;AAKf,QAAM,YAAY,qBAAsB,KAAM;AAE9C,MAAK,CAAE,YAAa,SAAU,GAAI;AACjC,WAAO;AAAA,MACN,GAAG;AAAA,MACH,GAAG;AAAA,MACH,OAAO;AAAA,MACP,QAAQ;AAAA,MACR,UAAU,UAAU;AAAA,MACpB,MAAM,EAAE,GAAG,UAAU,KAAK;AAAA,MAC1B,MAAM,UAAU;AAAA,IACjB;AAAA,EACD;AAIA,QAAM,qBAA2B,EAAE,OAAO,KAAM,QAAQ,IAAK;AAC7D,QAAM,SAAS,aAAc,WAAW,oBAAoB,SAAU;AAGtE,QAAM,MAAM,MAAM,OAAO;AACzB,QAAM,OAAQ,KAAK,MAAO;AAK1B,QAAM,aAAa;AAAA,IAClB,EAAE,GAAG,WAAW,KAAK,EAAE,GAAG,GAAG,GAAG,EAAE,GAAG,MAAM,EAAE;AAAA,IAC7C;AAAA,IACA;AAAA,EACD;AACA,QAAM,gBAAgB,iBAAkB,UAAW;AAEnD,QAAM,WAAW,UAAU;AAC3B,QAAM,oBACL,cAAc,QACZ,SAAS,IAAI,SAAS,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,SAAS;AAAA,EACzD;AACD;AA+BO,SAAS,uBACf,OACA,WACsB;AACtB,MAAK,CAAE,YAAa,SAAU,GAAI;AACjC,WAAO,EAAE,GAAG,GAAG,GAAG,GAAG,OAAO,GAAG,QAAQ,EAAE;AAAA,EAC1C;AACA,QAAM,SAAS,gBAAiB,OAAO,SAAU;AACjD,SAAO;AAAA,IACN,GAAK,OAAO,IAAI,UAAU,QAAU;AAAA,IACpC,GAAK,OAAO,IAAI,UAAU,SAAW;AAAA,IACrC,OAAS,OAAO,QAAQ,UAAU,QAAU;AAAA,IAC5C,QAAU,OAAO,SAAS,UAAU,SAAW;AAAA,EAChD;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/image-editor/core/state.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport type { CropperAction, CropperState, TransformOperation } from './types';\nimport {\n\tABSOLUTE_MIN_ZOOM,\n\tDEFAULT_STATE,\n\tMAX_ZOOM,\n\tMIN_ZOOM,\n} from './constants';\nimport { normalizeRotation, degreesToRadians } from './math/rotation';\nimport { restrictPanZoom, restrictCropRect } from './containment';\n\n/** Small tolerance for cropper floating-point comparisons. */\nconst STATE_EPSILON = 1e-6;\n\nfunction nearlyEqual( a: number, b: number ): boolean {\n\treturn Math.abs( a - b ) < STATE_EPSILON;\n}\n\n/**\n * Value-equality check for two cropper states.\n *\n * Used by history machinery to dedup snapshots and detect no-op\n * transitions. Compares every field that participates in the rendered\n * output; ignores reference identity.\n *\n * @param a First cropper state.\n * @param b Second cropper state.\n * @return Whether the two states are value-equal across all tracked fields.\n */\nexport function areCropperStatesEqual(\n\ta: CropperState,\n\tb: CropperState\n): boolean {\n\tconst aImage = a.image;\n\tconst bImage = b.image;\n\treturn (\n\t\taImage?.src === bImage?.src &&\n\t\taImage?.naturalWidth === bImage?.naturalWidth &&\n\t\taImage?.naturalHeight === bImage?.naturalHeight &&\n\t\tnearlyEqual( a.pan.x, b.pan.x ) &&\n\t\tnearlyEqual( a.pan.y, b.pan.y ) &&\n\t\tnearlyEqual( a.zoom, b.zoom ) &&\n\t\tnearlyEqual( a.rotation, b.rotation ) &&\n\t\ta.flip.horizontal === b.flip.horizontal &&\n\t\ta.flip.vertical === b.flip.vertical &&\n\t\tnearlyEqual( a.cropRect.x, b.cropRect.x ) &&\n\t\tnearlyEqual( a.cropRect.y, b.cropRect.y ) &&\n\t\tnearlyEqual( a.cropRect.width, b.cropRect.width ) &&\n\t\tnearlyEqual( a.cropRect.height, b.cropRect.height )\n\t);\n}\n\nfunction clampRequestedZoom( state: CropperState, zoom: number ): number {\n\t// With an image, enforceContainment raises zoom to the coverage-aware\n\t// floor below. Without an image, no enforcement runs, so MIN_ZOOM stays\n\t// the conservative resting default.\n\tif ( state.image ) {\n\t\treturn Math.min( MAX_ZOOM, Math.max( ABSOLUTE_MIN_ZOOM, zoom ) );\n\t}\n\treturn Math.min( MAX_ZOOM, Math.max( MIN_ZOOM, zoom ) );\n}\n\n/**\n * Translate a pipeline transform operation into the equivalent\n * reducer action. Pipeline ops aren't 1:1 with reducer actions —\n * `rotate` is a delta here, whereas SET_ROTATION takes an absolute\n * angle — so deltas are resolved against the current state.\n *\n * Used by APPLY_OPERATION (and by the pipeline module's\n * `applyOperationToState`, which is now a thin wrapper around the\n * reducer) so that pipeline replay produces identical bounded\n * state to UI interaction or direct setters.\n *\n * @param state The current cropper state.\n * @param op The pipeline operation.\n * @return The equivalent reducer action.\n */\nfunction operationToAction(\n\tstate: CropperState,\n\top: TransformOperation\n): CropperAction {\n\tswitch ( op.type ) {\n\t\tcase 'crop':\n\t\t\treturn { type: 'SET_CROP_RECT', payload: { ...op.rect } };\n\t\tcase 'rotate': {\n\t\t\t// `op.degrees` is the visual rotation the caller wants (positive\n\t\t\t// = CW on screen). A single-axis flip inverts on-screen rotation\n\t\t\t// relative to the rotation field, so negate the field delta in\n\t\t\t// that case — this keeps `{ type: 'rotate', degrees: 90 }` a\n\t\t\t// reliable \"90° clockwise visually\" instruction across flip\n\t\t\t// states, matching the SNAP_ROTATE_90 convention.\n\t\t\tconst singleFlip = state.flip.horizontal !== state.flip.vertical;\n\t\t\tconst fieldDelta = singleFlip ? -op.degrees : op.degrees;\n\t\t\treturn {\n\t\t\t\ttype: 'SET_ROTATION',\n\t\t\t\tpayload: normalizeRotation( state.rotation + fieldDelta ),\n\t\t\t};\n\t\t}\n\t\tcase 'flip':\n\t\t\treturn {\n\t\t\t\ttype: 'SET_FLIP',\n\t\t\t\tpayload: {\n\t\t\t\t\t...state.flip,\n\t\t\t\t\t[ op.direction ]: ! state.flip[ op.direction ],\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'zoom':\n\t\t\treturn { type: 'SET_ZOOM', payload: op.factor };\n\t}\n}\n\n/**\n * Enforces containment: restricts the crop rect to fit within the\n * rotated image, computes minimum zoom, clamps zoom, and restricts\n * the pan position so the image always covers the crop area.\n * Called after every relevant state transition.\n *\n * @param state The state to enforce containment on.\n * @return The state with cropRect, zoom, and position restricted.\n */\nexport function enforceContainment( state: CropperState ): CropperState {\n\tif ( ! state.image ) {\n\t\treturn state;\n\t}\n\tconst imageSize = {\n\t\twidth: state.image.naturalWidth,\n\t\theight: state.image.naturalHeight,\n\t};\n\tconst imageAspectRatio = imageSize.width / imageSize.height;\n\n\t// 1. First bump zoom so the image can cover the crop rect as-is.\n\t// This ensures that explicit crop rect changes (e.g., fixed-crop\n\t// mode during rotation) get zoom accommodation, not crop shrinkage.\n\tconst { pan: panAfterZoom, zoom } = restrictPanZoom(\n\t\tstate,\n\t\timageSize,\n\t\tstate.cropRect\n\t);\n\n\t// 2. Now restrict the crop rect at the (possibly bumped) zoom.\n\t// This handles cases where the crop rect is still too large\n\t// (e.g., if zoom hit MAX_ZOOM).\n\tconst cropRect = restrictCropRect(\n\t\tstate.cropRect,\n\t\tzoom,\n\t\tstate.rotation,\n\t\timageAspectRatio\n\t);\n\n\t// 3. If the crop rect was shrunk, re-restrict pan for the new rect.\n\tlet pan = panAfterZoom;\n\tif ( cropRect !== state.cropRect ) {\n\t\t( { pan } = restrictPanZoom(\n\t\t\t{ ...state, zoom, cropRect },\n\t\t\timageSize,\n\t\t\tcropRect\n\t\t) );\n\t}\n\n\tif (\n\t\tpan.x === state.pan.x &&\n\t\tpan.y === state.pan.y &&\n\t\tzoom === state.zoom &&\n\t\tcropRect === state.cropRect\n\t) {\n\t\treturn state;\n\t}\n\treturn { ...state, pan, zoom, cropRect };\n}\n\n/**\n * Snapshot the current `crop`, `zoom`, and `rotation` into the\n * corresponding `basePan`, `baseZoom`, `baseRotation` fields. Called\n * by every committing action so that the next SET_ROTATION derives\n * its transient pan from a fresh base (not from a drifted one).\n *\n * SET_ROTATION is the only action that does NOT commit base — it\n * reads the base, computes a transient pan, and leaves the base\n * alone. This is what eliminates rotation drift near edges.\n *\n * @param next The post-containment state to commit.\n * @return The state with base fields synced to current.\n */\nfunction commitBase( next: CropperState ): CropperState {\n\tif (\n\t\tnext.basePan.x === next.pan.x &&\n\t\tnext.basePan.y === next.pan.y &&\n\t\tnext.baseZoom === next.zoom &&\n\t\tnext.baseRotation === next.rotation\n\t) {\n\t\treturn next;\n\t}\n\treturn {\n\t\t...next,\n\t\tbasePan: { x: next.pan.x, y: next.pan.y },\n\t\tbaseZoom: next.zoom,\n\t\tbaseRotation: next.rotation,\n\t};\n}\n\n/**\n * Reducer function for cropper state management.\n *\n * Every state transition that could invalidate the containment invariant\n * (crop, zoom, rotation, cropRect, flip) is followed by enforceContainment\n * to ensure the image always covers the crop area.\n *\n * Committing actions (everything except SET_ROTATION) also call\n * `commitBase` to snapshot the post-containment state as the new\n * base pose. SET_ROTATION derives from that base without mutating\n * it, which prevents accumulated-clamp drift during continuous fine\n * rotation.\n *\n * @param state The current cropper state.\n * @param action The action to process.\n * @return The new cropper state.\n */\nexport function cropperReducer(\n\tstate: CropperState,\n\taction: CropperAction\n): CropperState {\n\t// Every action runs through enforceContainment to maintain the invariant:\n\t// the image always fully covers the crop area.\n\tswitch ( action.type ) {\n\t\tcase 'SET_IMAGE':\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\timage: action.payload,\n\t\t\t\t} )\n\t\t\t);\n\n\t\tcase 'SET_PAN':\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\tpan: action.payload,\n\t\t\t\t} )\n\t\t\t);\n\n\t\tcase 'SET_ZOOM': {\n\t\t\tconst z = clampRequestedZoom( state, action.payload );\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\tzoom: z,\n\t\t\t\t} )\n\t\t\t);\n\t\t}\n\n\t\tcase 'SET_ZOOM_AT_POINT': {\n\t\t\tconst z = clampRequestedZoom( state, action.payload.zoom );\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\tzoom: z,\n\t\t\t\t\tpan: action.payload.pan,\n\t\t\t\t} )\n\t\t\t);\n\t\t}\n\n\t\tcase 'SET_ROTATION': {\n\t\t\t// Rotate the base pan around the crop-rect center by the\n\t\t\t// delta from the base rotation. Deriving each tick from\n\t\t\t// the fixed base — rather than compounding on the previous\n\t\t\t// tick — prevents accumulated-clamp drift. Rotating\n\t\t\t// 0°→30°→0° now returns to the exact base pan.\n\t\t\t//\n\t\t\t// Zoom starts from baseZoom; enforceContainment may bump it\n\t\t\t// up to cover the rotated crop, but never raises the base.\n\t\t\t//\n\t\t\t// SET_ROTATION is the only action that does NOT call\n\t\t\t// commitBase — the base pose stays pinned at the user's\n\t\t\t// last committed pan/zoom/rotation.\n\t\t\tconst newRotation = normalizeRotation( action.payload );\n\t\t\t// With viewport-relative flip, pan rotation is conjugated by\n\t\t\t// the flip (S * R * S) — which reverses direction when exactly\n\t\t\t// one axis is flipped. Negate sin in that case so the crop\n\t\t\t// stays framed on the same image content through rotation.\n\t\t\tconst singleFlip = state.flip.horizontal !== state.flip.vertical;\n\t\t\tconst deltaRad = degreesToRadians(\n\t\t\t\tnewRotation - state.baseRotation\n\t\t\t);\n\t\t\tconst cos = Math.cos( deltaRad );\n\t\t\tconst sin = Math.sin( deltaRad ) * ( singleFlip ? -1 : 1 );\n\t\t\tconst cropCx = state.cropRect.x + state.cropRect.width / 2;\n\t\t\tconst cropCy = state.cropRect.y + state.cropRect.height / 2;\n\t\t\tconst ox = cropCx - 0.5;\n\t\t\tconst oy = cropCy - 0.5;\n\t\t\tconst dx = state.basePan.x - ox;\n\t\t\tconst dy = state.basePan.y - oy;\n\t\t\treturn enforceContainment( {\n\t\t\t\t...state,\n\t\t\t\trotation: newRotation,\n\t\t\t\tzoom: state.baseZoom,\n\t\t\t\tpan: {\n\t\t\t\t\tx: ox + cos * dx - sin * dy,\n\t\t\t\t\ty: oy + sin * dx + cos * dy,\n\t\t\t\t},\n\t\t\t} );\n\t\t}\n\n\t\tcase 'SNAP_ROTATE_90': {\n\t\t\t// 90° snap: the crop rect rotates 90° around its own center\n\t\t\t// (width/height swap), and the pan rotates 90° around the\n\t\t\t// pan-space origin. The combination keeps the same image\n\t\t\t// slice framed — just viewed through a rotated window.\n\t\t\t//\n\t\t\t// `direction` is the visual rotation the caller wants: +1\n\t\t\t// means \"rotate the image CW on screen\", −1 means \"CCW\".\n\t\t\t// With a single-axis flip active, rotation on screen appears\n\t\t\t// reversed relative to the rotation field (S·R·S conjugation),\n\t\t\t// so internally we negate the direction applied to the\n\t\t\t// `rotation` field. Pan rotates in the caller's direction —\n\t\t\t// that way the same image content stays framed after the\n\t\t\t// snap regardless of flip state.\n\t\t\tconst rawDir = action.payload.direction;\n\t\t\tconst singleFlip = state.flip.horizontal !== state.flip.vertical;\n\t\t\tconst fieldDir = singleFlip ? -rawDir : rawDir;\n\t\t\tconst rot90 = normalizeRotation( state.rotation + fieldDir * 90 );\n\t\t\tconst rect = state.cropRect;\n\t\t\tconst cx = rect.x + rect.width / 2;\n\t\t\tconst cy = rect.y + rect.height / 2;\n\n\t\t\t// Rotate pan vector 90° around origin.\n\t\t\t// CW (rawDir=+1): (px, py) → (-py, px)\n\t\t\t// CCW (rawDir=-1): (px, py) → (py, -px)\n\t\t\tconst newPanX = rawDir === 1 ? -state.pan.y : state.pan.y;\n\t\t\tconst newPanY = rawDir === 1 ? state.pan.x : -state.pan.x;\n\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\trotation: rot90,\n\t\t\t\t\tzoom: state.baseZoom,\n\t\t\t\t\tpan: { x: newPanX, y: newPanY },\n\t\t\t\t\tcropRect: {\n\t\t\t\t\t\tx: cx - rect.height / 2,\n\t\t\t\t\t\ty: cy - rect.width / 2,\n\t\t\t\t\t\twidth: rect.height,\n\t\t\t\t\t\theight: rect.width,\n\t\t\t\t\t},\n\t\t\t\t} )\n\t\t\t);\n\t\t}\n\n\t\tcase 'SET_FLIP': {\n\t\t\t// Mirror the crop rect and pan so the same image content stays\n\t\t\t// framed after a flip. Flip in the camera matrix is composed\n\t\t\t// outside rotation (viewport-relative), so both `pan` and\n\t\t\t// `cropRect` live in viewport-aligned space: negating the flipped\n\t\t\t// axis on each is enough — no rotation math needed.\n\t\t\tconst oldFlip = state.flip;\n\t\t\tconst newFlip = action.payload;\n\t\t\tconst flippedH = oldFlip.horizontal !== newFlip.horizontal;\n\t\t\tconst flippedV = oldFlip.vertical !== newFlip.vertical;\n\t\t\tconst rect = state.cropRect;\n\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\tflip: newFlip,\n\t\t\t\t\tpan: {\n\t\t\t\t\t\tx: flippedH ? -state.pan.x : state.pan.x,\n\t\t\t\t\t\ty: flippedV ? -state.pan.y : state.pan.y,\n\t\t\t\t\t},\n\t\t\t\t\tcropRect: {\n\t\t\t\t\t\tx: flippedH ? 1 - rect.x - rect.width : rect.x,\n\t\t\t\t\t\ty: flippedV ? 1 - rect.y - rect.height : rect.y,\n\t\t\t\t\t\twidth: rect.width,\n\t\t\t\t\t\theight: rect.height,\n\t\t\t\t\t},\n\t\t\t\t} )\n\t\t\t);\n\t\t}\n\n\t\tcase 'SET_CROP_RECT':\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\tcropRect: action.payload,\n\t\t\t\t} )\n\t\t\t);\n\n\t\tcase 'SETTLE_CROP': {\n\t\t\t// After a resize drag ends: expand the crop to fill the\n\t\t\t// available height (maintaining its aspect ratio), center it,\n\t\t\t// and adjust zoom/pan so the exact same image content that\n\t\t\t// was visible inside the old crop is visible in the new one.\n\t\t\tconst rect = state.cropRect;\n\t\t\tif ( rect.width === 0 || rect.height === 0 || ! state.image ) {\n\t\t\t\treturn state;\n\t\t\t}\n\n\t\t\t// Scale factor: how much the crop grows. fitScale fills the\n\t\t\t// larger axis to 1 (viewport-filling settle). Cap so the\n\t\t\t// resulting zoom stays at or below MAX_ZOOM — tight crops\n\t\t\t// would otherwise push state.zoom past the user-facing cap\n\t\t\t// and break wheel/slider interactions, which clamp to\n\t\t\t// MAX_ZOOM and snap state.zoom down on the first input.\n\t\t\tconst fitScale = 1 / Math.max( rect.width, rect.height );\n\t\t\tconst zoomCap = state.zoom > 0 ? MAX_ZOOM / state.zoom : fitScale;\n\t\t\tconst s = Math.min( fitScale, zoomCap );\n\n\t\t\tconst newW = rect.width * s;\n\t\t\tconst newH = rect.height * s;\n\n\t\t\t// The old crop center in normalized visual space.\n\t\t\tconst oldCx = rect.x + rect.width / 2;\n\t\t\tconst oldCy = rect.y + rect.height / 2;\n\n\t\t\t// Zoom scales by s so the same image region fills the new\n\t\t\t// crop at the same relative size. When s is capped, the new\n\t\t\t// crop is smaller than the viewport but the visible content\n\t\t\t// inside it remains the source region the user framed.\n\t\t\t// Pan: the visible content center was at\n\t\t\t// (cropCx - crop.x, cropCy - crop.y)\n\t\t\t// in visual-normalized space. After centering the crop to\n\t\t\t// (0.5, 0.5), the pan must place that same content at\n\t\t\t// the new center. Both pan and zoom scale by s because\n\t\t\t// the CSS translate is independent of zoom.\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\tzoom: state.zoom * s,\n\t\t\t\t\tpan: {\n\t\t\t\t\t\tx: ( state.pan.x - oldCx + 0.5 ) * s,\n\t\t\t\t\t\ty: ( state.pan.y - oldCy + 0.5 ) * s,\n\t\t\t\t\t},\n\t\t\t\t\tcropRect: {\n\t\t\t\t\t\tx: ( 1 - newW ) / 2,\n\t\t\t\t\t\ty: ( 1 - newH ) / 2,\n\t\t\t\t\t\twidth: newW,\n\t\t\t\t\t\theight: newH,\n\t\t\t\t\t},\n\t\t\t\t} )\n\t\t\t);\n\t\t}\n\n\t\tcase 'APPLY_OPERATION':\n\t\t\t// Route through the action equivalent so a pipeline replay\n\t\t\t// yields the same bounded state as the matching UI/setter\n\t\t\t// path (containment, baseZoom relaxation, pan rotation\n\t\t\t// around crop center, etc.).\n\t\t\treturn cropperReducer(\n\t\t\t\tstate,\n\t\t\t\toperationToAction( state, action.payload )\n\t\t\t);\n\n\t\tcase 'RESET':\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...DEFAULT_STATE,\n\t\t\t\t\timage: state.image,\n\t\t\t\t\t...action.payload,\n\t\t\t\t} )\n\t\t\t);\n\t}\n}\n\n/**\n * Shallow comparison of key cropper state fields to determine if\n * the state has been modified from an initial snapshot.\n *\n * @param current The current cropper state.\n * @param initial The initial cropper state snapshot.\n * @return True if any tracked field differs.\n */\nexport function isStateDirty(\n\tcurrent: CropperState,\n\tinitial: CropperState\n): boolean {\n\treturn (\n\t\t! nearlyEqual( current.pan.x, initial.pan.x ) ||\n\t\t! nearlyEqual( current.pan.y, initial.pan.y ) ||\n\t\t! nearlyEqual( current.zoom, initial.zoom ) ||\n\t\t! nearlyEqual( current.rotation, initial.rotation ) ||\n\t\tcurrent.flip.horizontal !== initial.flip.horizontal ||\n\t\tcurrent.flip.vertical !== initial.flip.vertical ||\n\t\t! nearlyEqual( current.cropRect.x, initial.cropRect.x ) ||\n\t\t! nearlyEqual( current.cropRect.y, initial.cropRect.y ) ||\n\t\t! nearlyEqual( current.cropRect.width, initial.cropRect.width ) ||\n\t\t! nearlyEqual( current.cropRect.height, initial.cropRect.height )\n\t);\n}\n"],
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["import type { CropperAction, CropperState, TransformOperation } from './types';\nimport {\n\tABSOLUTE_MIN_ZOOM,\n\tDEFAULT_STATE,\n\tMAX_ZOOM,\n\tMIN_ZOOM,\n} from './constants';\nimport { normalizeRotation, degreesToRadians } from './math/rotation';\nimport { restrictPanZoom, restrictCropRect } from './containment';\n\n/** Small tolerance for cropper floating-point comparisons. */\nconst STATE_EPSILON = 1e-6;\n\nfunction nearlyEqual( a: number, b: number ): boolean {\n\treturn Math.abs( a - b ) < STATE_EPSILON;\n}\n\n/**\n * Value-equality check for two cropper states.\n *\n * Used by history machinery to dedup snapshots and detect no-op\n * transitions. Compares every field that participates in the rendered\n * output; ignores reference identity.\n *\n * @param a First cropper state.\n * @param b Second cropper state.\n * @return Whether the two states are value-equal across all tracked fields.\n */\nexport function areCropperStatesEqual(\n\ta: CropperState,\n\tb: CropperState\n): boolean {\n\tconst aImage = a.image;\n\tconst bImage = b.image;\n\treturn (\n\t\taImage?.src === bImage?.src &&\n\t\taImage?.naturalWidth === bImage?.naturalWidth &&\n\t\taImage?.naturalHeight === bImage?.naturalHeight &&\n\t\tnearlyEqual( a.pan.x, b.pan.x ) &&\n\t\tnearlyEqual( a.pan.y, b.pan.y ) &&\n\t\tnearlyEqual( a.zoom, b.zoom ) &&\n\t\tnearlyEqual( a.rotation, b.rotation ) &&\n\t\ta.flip.horizontal === b.flip.horizontal &&\n\t\ta.flip.vertical === b.flip.vertical &&\n\t\tnearlyEqual( a.cropRect.x, b.cropRect.x ) &&\n\t\tnearlyEqual( a.cropRect.y, b.cropRect.y ) &&\n\t\tnearlyEqual( a.cropRect.width, b.cropRect.width ) &&\n\t\tnearlyEqual( a.cropRect.height, b.cropRect.height )\n\t);\n}\n\nfunction clampRequestedZoom( state: CropperState, zoom: number ): number {\n\t// With an image, enforceContainment raises zoom to the coverage-aware\n\t// floor below. Without an image, no enforcement runs, so MIN_ZOOM stays\n\t// the conservative resting default.\n\tif ( state.image ) {\n\t\treturn Math.min( MAX_ZOOM, Math.max( ABSOLUTE_MIN_ZOOM, zoom ) );\n\t}\n\treturn Math.min( MAX_ZOOM, Math.max( MIN_ZOOM, zoom ) );\n}\n\n/**\n * Translate a pipeline transform operation into the equivalent\n * reducer action. Pipeline ops aren't 1:1 with reducer actions —\n * `rotate` is a delta here, whereas SET_ROTATION takes an absolute\n * angle — so deltas are resolved against the current state.\n *\n * Used by APPLY_OPERATION (and by the pipeline module's\n * `applyOperationToState`, which is now a thin wrapper around the\n * reducer) so that pipeline replay produces identical bounded\n * state to UI interaction or direct setters.\n *\n * @param state The current cropper state.\n * @param op The pipeline operation.\n * @return The equivalent reducer action.\n */\nfunction operationToAction(\n\tstate: CropperState,\n\top: TransformOperation\n): CropperAction {\n\tswitch ( op.type ) {\n\t\tcase 'crop':\n\t\t\treturn { type: 'SET_CROP_RECT', payload: { ...op.rect } };\n\t\tcase 'rotate': {\n\t\t\t// `op.degrees` is the visual rotation the caller wants (positive\n\t\t\t// = CW on screen). A single-axis flip inverts on-screen rotation\n\t\t\t// relative to the rotation field, so negate the field delta in\n\t\t\t// that case — this keeps `{ type: 'rotate', degrees: 90 }` a\n\t\t\t// reliable \"90° clockwise visually\" instruction across flip\n\t\t\t// states, matching the SNAP_ROTATE_90 convention.\n\t\t\tconst singleFlip = state.flip.horizontal !== state.flip.vertical;\n\t\t\tconst fieldDelta = singleFlip ? -op.degrees : op.degrees;\n\t\t\treturn {\n\t\t\t\ttype: 'SET_ROTATION',\n\t\t\t\tpayload: normalizeRotation( state.rotation + fieldDelta ),\n\t\t\t};\n\t\t}\n\t\tcase 'flip':\n\t\t\treturn {\n\t\t\t\ttype: 'SET_FLIP',\n\t\t\t\tpayload: {\n\t\t\t\t\t...state.flip,\n\t\t\t\t\t[ op.direction ]: ! state.flip[ op.direction ],\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'zoom':\n\t\t\treturn { type: 'SET_ZOOM', payload: op.factor };\n\t}\n}\n\n/**\n * Enforces containment: restricts the crop rect to fit within the\n * rotated image, computes minimum zoom, clamps zoom, and restricts\n * the pan position so the image always covers the crop area.\n * Called after every relevant state transition.\n *\n * @param state The state to enforce containment on.\n * @return The state with cropRect, zoom, and position restricted.\n */\nexport function enforceContainment( state: CropperState ): CropperState {\n\tif ( ! state.image ) {\n\t\treturn state;\n\t}\n\tconst imageSize = {\n\t\twidth: state.image.naturalWidth,\n\t\theight: state.image.naturalHeight,\n\t};\n\tconst imageAspectRatio = imageSize.width / imageSize.height;\n\n\t// 1. First bump zoom so the image can cover the crop rect as-is.\n\t// This ensures that explicit crop rect changes (e.g., fixed-crop\n\t// mode during rotation) get zoom accommodation, not crop shrinkage.\n\tconst { pan: panAfterZoom, zoom } = restrictPanZoom(\n\t\tstate,\n\t\timageSize,\n\t\tstate.cropRect\n\t);\n\n\t// 2. Now restrict the crop rect at the (possibly bumped) zoom.\n\t// This handles cases where the crop rect is still too large\n\t// (e.g., if zoom hit MAX_ZOOM).\n\tconst cropRect = restrictCropRect(\n\t\tstate.cropRect,\n\t\tzoom,\n\t\tstate.rotation,\n\t\timageAspectRatio\n\t);\n\n\t// 3. If the crop rect was shrunk, re-restrict pan for the new rect.\n\tlet pan = panAfterZoom;\n\tif ( cropRect !== state.cropRect ) {\n\t\t( { pan } = restrictPanZoom(\n\t\t\t{ ...state, zoom, cropRect },\n\t\t\timageSize,\n\t\t\tcropRect\n\t\t) );\n\t}\n\n\tif (\n\t\tpan.x === state.pan.x &&\n\t\tpan.y === state.pan.y &&\n\t\tzoom === state.zoom &&\n\t\tcropRect === state.cropRect\n\t) {\n\t\treturn state;\n\t}\n\treturn { ...state, pan, zoom, cropRect };\n}\n\n/**\n * Snapshot the current `crop`, `zoom`, and `rotation` into the\n * corresponding `basePan`, `baseZoom`, `baseRotation` fields. Called\n * by every committing action so that the next SET_ROTATION derives\n * its transient pan from a fresh base (not from a drifted one).\n *\n * SET_ROTATION is the only action that does NOT commit base — it\n * reads the base, computes a transient pan, and leaves the base\n * alone. This is what eliminates rotation drift near edges.\n *\n * @param next The post-containment state to commit.\n * @return The state with base fields synced to current.\n */\nfunction commitBase( next: CropperState ): CropperState {\n\tif (\n\t\tnext.basePan.x === next.pan.x &&\n\t\tnext.basePan.y === next.pan.y &&\n\t\tnext.baseZoom === next.zoom &&\n\t\tnext.baseRotation === next.rotation\n\t) {\n\t\treturn next;\n\t}\n\treturn {\n\t\t...next,\n\t\tbasePan: { x: next.pan.x, y: next.pan.y },\n\t\tbaseZoom: next.zoom,\n\t\tbaseRotation: next.rotation,\n\t};\n}\n\n/**\n * Reducer function for cropper state management.\n *\n * Every state transition that could invalidate the containment invariant\n * (crop, zoom, rotation, cropRect, flip) is followed by enforceContainment\n * to ensure the image always covers the crop area.\n *\n * Committing actions (everything except SET_ROTATION) also call\n * `commitBase` to snapshot the post-containment state as the new\n * base pose. SET_ROTATION derives from that base without mutating\n * it, which prevents accumulated-clamp drift during continuous fine\n * rotation.\n *\n * @param state The current cropper state.\n * @param action The action to process.\n * @return The new cropper state.\n */\nexport function cropperReducer(\n\tstate: CropperState,\n\taction: CropperAction\n): CropperState {\n\t// Every action runs through enforceContainment to maintain the invariant:\n\t// the image always fully covers the crop area.\n\tswitch ( action.type ) {\n\t\tcase 'SET_IMAGE':\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\timage: action.payload,\n\t\t\t\t} )\n\t\t\t);\n\n\t\tcase 'SET_PAN':\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\tpan: action.payload,\n\t\t\t\t} )\n\t\t\t);\n\n\t\tcase 'SET_ZOOM': {\n\t\t\tconst z = clampRequestedZoom( state, action.payload );\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\tzoom: z,\n\t\t\t\t} )\n\t\t\t);\n\t\t}\n\n\t\tcase 'SET_ZOOM_AT_POINT': {\n\t\t\tconst z = clampRequestedZoom( state, action.payload.zoom );\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\tzoom: z,\n\t\t\t\t\tpan: action.payload.pan,\n\t\t\t\t} )\n\t\t\t);\n\t\t}\n\n\t\tcase 'SET_ROTATION': {\n\t\t\t// Rotate the base pan around the crop-rect center by the\n\t\t\t// delta from the base rotation. Deriving each tick from\n\t\t\t// the fixed base — rather than compounding on the previous\n\t\t\t// tick — prevents accumulated-clamp drift. Rotating\n\t\t\t// 0°→30°→0° now returns to the exact base pan.\n\t\t\t//\n\t\t\t// Zoom starts from baseZoom; enforceContainment may bump it\n\t\t\t// up to cover the rotated crop, but never raises the base.\n\t\t\t//\n\t\t\t// SET_ROTATION is the only action that does NOT call\n\t\t\t// commitBase — the base pose stays pinned at the user's\n\t\t\t// last committed pan/zoom/rotation.\n\t\t\tconst newRotation = normalizeRotation( action.payload );\n\t\t\t// With viewport-relative flip, pan rotation is conjugated by\n\t\t\t// the flip (S * R * S) — which reverses direction when exactly\n\t\t\t// one axis is flipped. Negate sin in that case so the crop\n\t\t\t// stays framed on the same image content through rotation.\n\t\t\tconst singleFlip = state.flip.horizontal !== state.flip.vertical;\n\t\t\tconst deltaRad = degreesToRadians(\n\t\t\t\tnewRotation - state.baseRotation\n\t\t\t);\n\t\t\tconst cos = Math.cos( deltaRad );\n\t\t\tconst sin = Math.sin( deltaRad ) * ( singleFlip ? -1 : 1 );\n\t\t\tconst cropCx = state.cropRect.x + state.cropRect.width / 2;\n\t\t\tconst cropCy = state.cropRect.y + state.cropRect.height / 2;\n\t\t\tconst ox = cropCx - 0.5;\n\t\t\tconst oy = cropCy - 0.5;\n\t\t\tconst dx = state.basePan.x - ox;\n\t\t\tconst dy = state.basePan.y - oy;\n\t\t\treturn enforceContainment( {\n\t\t\t\t...state,\n\t\t\t\trotation: newRotation,\n\t\t\t\tzoom: state.baseZoom,\n\t\t\t\tpan: {\n\t\t\t\t\tx: ox + cos * dx - sin * dy,\n\t\t\t\t\ty: oy + sin * dx + cos * dy,\n\t\t\t\t},\n\t\t\t} );\n\t\t}\n\n\t\tcase 'SNAP_ROTATE_90': {\n\t\t\t// 90° snap: the crop rect rotates 90° around its own center\n\t\t\t// (width/height swap), and the pan rotates 90° around the\n\t\t\t// pan-space origin. The combination keeps the same image\n\t\t\t// slice framed — just viewed through a rotated window.\n\t\t\t//\n\t\t\t// `direction` is the visual rotation the caller wants: +1\n\t\t\t// means \"rotate the image CW on screen\", −1 means \"CCW\".\n\t\t\t// With a single-axis flip active, rotation on screen appears\n\t\t\t// reversed relative to the rotation field (S·R·S conjugation),\n\t\t\t// so internally we negate the direction applied to the\n\t\t\t// `rotation` field. Pan rotates in the caller's direction —\n\t\t\t// that way the same image content stays framed after the\n\t\t\t// snap regardless of flip state.\n\t\t\tconst rawDir = action.payload.direction;\n\t\t\tconst singleFlip = state.flip.horizontal !== state.flip.vertical;\n\t\t\tconst fieldDir = singleFlip ? -rawDir : rawDir;\n\t\t\tconst rot90 = normalizeRotation( state.rotation + fieldDir * 90 );\n\t\t\tconst rect = state.cropRect;\n\t\t\tconst cx = rect.x + rect.width / 2;\n\t\t\tconst cy = rect.y + rect.height / 2;\n\n\t\t\t// Rotate pan vector 90° around origin.\n\t\t\t// CW (rawDir=+1): (px, py) → (-py, px)\n\t\t\t// CCW (rawDir=-1): (px, py) → (py, -px)\n\t\t\tconst newPanX = rawDir === 1 ? -state.pan.y : state.pan.y;\n\t\t\tconst newPanY = rawDir === 1 ? state.pan.x : -state.pan.x;\n\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\trotation: rot90,\n\t\t\t\t\tzoom: state.baseZoom,\n\t\t\t\t\tpan: { x: newPanX, y: newPanY },\n\t\t\t\t\tcropRect: {\n\t\t\t\t\t\tx: cx - rect.height / 2,\n\t\t\t\t\t\ty: cy - rect.width / 2,\n\t\t\t\t\t\twidth: rect.height,\n\t\t\t\t\t\theight: rect.width,\n\t\t\t\t\t},\n\t\t\t\t} )\n\t\t\t);\n\t\t}\n\n\t\tcase 'SET_FLIP': {\n\t\t\t// Mirror the crop rect and pan so the same image content stays\n\t\t\t// framed after a flip. Flip in the camera matrix is composed\n\t\t\t// outside rotation (viewport-relative), so both `pan` and\n\t\t\t// `cropRect` live in viewport-aligned space: negating the flipped\n\t\t\t// axis on each is enough — no rotation math needed.\n\t\t\tconst oldFlip = state.flip;\n\t\t\tconst newFlip = action.payload;\n\t\t\tconst flippedH = oldFlip.horizontal !== newFlip.horizontal;\n\t\t\tconst flippedV = oldFlip.vertical !== newFlip.vertical;\n\t\t\tconst rect = state.cropRect;\n\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\tflip: newFlip,\n\t\t\t\t\tpan: {\n\t\t\t\t\t\tx: flippedH ? -state.pan.x : state.pan.x,\n\t\t\t\t\t\ty: flippedV ? -state.pan.y : state.pan.y,\n\t\t\t\t\t},\n\t\t\t\t\tcropRect: {\n\t\t\t\t\t\tx: flippedH ? 1 - rect.x - rect.width : rect.x,\n\t\t\t\t\t\ty: flippedV ? 1 - rect.y - rect.height : rect.y,\n\t\t\t\t\t\twidth: rect.width,\n\t\t\t\t\t\theight: rect.height,\n\t\t\t\t\t},\n\t\t\t\t} )\n\t\t\t);\n\t\t}\n\n\t\tcase 'SET_CROP_RECT':\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\tcropRect: action.payload,\n\t\t\t\t} )\n\t\t\t);\n\n\t\tcase 'SETTLE_CROP': {\n\t\t\t// After a resize drag ends: expand the crop to fill the\n\t\t\t// available height (maintaining its aspect ratio), center it,\n\t\t\t// and adjust zoom/pan so the exact same image content that\n\t\t\t// was visible inside the old crop is visible in the new one.\n\t\t\tconst rect = state.cropRect;\n\t\t\tif ( rect.width === 0 || rect.height === 0 || ! state.image ) {\n\t\t\t\treturn state;\n\t\t\t}\n\n\t\t\t// Scale factor: how much the crop grows. fitScale fills the\n\t\t\t// larger axis to 1 (viewport-filling settle). Cap so the\n\t\t\t// resulting zoom stays at or below MAX_ZOOM — tight crops\n\t\t\t// would otherwise push state.zoom past the user-facing cap\n\t\t\t// and break wheel/slider interactions, which clamp to\n\t\t\t// MAX_ZOOM and snap state.zoom down on the first input.\n\t\t\tconst fitScale = 1 / Math.max( rect.width, rect.height );\n\t\t\tconst zoomCap = state.zoom > 0 ? MAX_ZOOM / state.zoom : fitScale;\n\t\t\tconst s = Math.min( fitScale, zoomCap );\n\n\t\t\tconst newW = rect.width * s;\n\t\t\tconst newH = rect.height * s;\n\n\t\t\t// The old crop center in normalized visual space.\n\t\t\tconst oldCx = rect.x + rect.width / 2;\n\t\t\tconst oldCy = rect.y + rect.height / 2;\n\n\t\t\t// Zoom scales by s so the same image region fills the new\n\t\t\t// crop at the same relative size. When s is capped, the new\n\t\t\t// crop is smaller than the viewport but the visible content\n\t\t\t// inside it remains the source region the user framed.\n\t\t\t// Pan: the visible content center was at\n\t\t\t// (cropCx - crop.x, cropCy - crop.y)\n\t\t\t// in visual-normalized space. After centering the crop to\n\t\t\t// (0.5, 0.5), the pan must place that same content at\n\t\t\t// the new center. Both pan and zoom scale by s because\n\t\t\t// the CSS translate is independent of zoom.\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...state,\n\t\t\t\t\tzoom: state.zoom * s,\n\t\t\t\t\tpan: {\n\t\t\t\t\t\tx: ( state.pan.x - oldCx + 0.5 ) * s,\n\t\t\t\t\t\ty: ( state.pan.y - oldCy + 0.5 ) * s,\n\t\t\t\t\t},\n\t\t\t\t\tcropRect: {\n\t\t\t\t\t\tx: ( 1 - newW ) / 2,\n\t\t\t\t\t\ty: ( 1 - newH ) / 2,\n\t\t\t\t\t\twidth: newW,\n\t\t\t\t\t\theight: newH,\n\t\t\t\t\t},\n\t\t\t\t} )\n\t\t\t);\n\t\t}\n\n\t\tcase 'APPLY_OPERATION':\n\t\t\t// Route through the action equivalent so a pipeline replay\n\t\t\t// yields the same bounded state as the matching UI/setter\n\t\t\t// path (containment, baseZoom relaxation, pan rotation\n\t\t\t// around crop center, etc.).\n\t\t\treturn cropperReducer(\n\t\t\t\tstate,\n\t\t\t\toperationToAction( state, action.payload )\n\t\t\t);\n\n\t\tcase 'RESET':\n\t\t\treturn commitBase(\n\t\t\t\tenforceContainment( {\n\t\t\t\t\t...DEFAULT_STATE,\n\t\t\t\t\timage: state.image,\n\t\t\t\t\t...action.payload,\n\t\t\t\t} )\n\t\t\t);\n\t}\n}\n\n/**\n * Shallow comparison of key cropper state fields to determine if\n * the state has been modified from an initial snapshot.\n *\n * @param current The current cropper state.\n * @param initial The initial cropper state snapshot.\n * @return True if any tracked field differs.\n */\nexport function isStateDirty(\n\tcurrent: CropperState,\n\tinitial: CropperState\n): boolean {\n\treturn (\n\t\t! nearlyEqual( current.pan.x, initial.pan.x ) ||\n\t\t! nearlyEqual( current.pan.y, initial.pan.y ) ||\n\t\t! nearlyEqual( current.zoom, initial.zoom ) ||\n\t\t! nearlyEqual( current.rotation, initial.rotation ) ||\n\t\tcurrent.flip.horizontal !== initial.flip.horizontal ||\n\t\tcurrent.flip.vertical !== initial.flip.vertical ||\n\t\t! nearlyEqual( current.cropRect.x, initial.cropRect.x ) ||\n\t\t! nearlyEqual( current.cropRect.y, initial.cropRect.y ) ||\n\t\t! nearlyEqual( current.cropRect.width, initial.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 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/image-editor/core/stencil-math.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport { MIN_CROP_PIXELS, MIN_CROP_SCREEN_PX } from './constants';\nimport type { HandlePosition, NormalizedRect, Size } from './types';\n\nexport type { HandlePosition };\n\n/** Axis whose resize distance controls the locked-ratio size projection. */\nexport type ResizeDriverAxis = 'width' | 'height';\n\n/**\n * Resolve the minimum crop dimension, in source-image pixels, for the current\n * display scale.\n *\n * Two floors compete: a fixed source-pixel floor (`MIN_CROP_PIXELS`) that\n * avoids sub-pixel crops, and an on-screen floor (`MIN_CROP_SCREEN_PX`) that\n * keeps the crop large enough to grab. The on-screen floor converted to source\n * pixels is `MIN_CROP_SCREEN_PX / displayScale`; the binding constraint is the\n * larger of the two. When the image is shown small (small `displayScale`) the\n * on-screen floor dominates; once it is shown large enough the source-pixel\n * floor takes over.\n *\n * @param displayScale CSS pixels rendered per source pixel\n * (fit scale × zoom × view scale).\n * @return Minimum crop dimension in source-image pixels.\n */\nexport function getMinCropPixels( displayScale: number ): number {\n\tif ( displayScale <= 0 ) {\n\t\treturn MIN_CROP_PIXELS;\n\t}\n\treturn Math.max( MIN_CROP_PIXELS, MIN_CROP_SCREEN_PX / displayScale );\n}\n\n/**\n * Default minimum crop rect dimension in normalized space, used when no\n * explicit per-axis minimum is supplied (e.g. before the source image\n * has loaded). Real usage derives a pixel-based min from the source\n * image dimensions and `MIN_CROP_PIXELS` in `constants.ts`.\n */\nexport const DEFAULT_MIN_CROP_SIZE: Size = { width: 0.05, height: 0.05 };\n\n/**\n * Bounds within which the crop rect must stay.\n */\nexport interface CropBounds {\n\tminX: number;\n\tminY: number;\n\tmaxX: number;\n\tmaxY: number;\n}\n\n/**\n * Drag state for tracking a resize interaction.\n */\nexport interface ResizeDragState {\n\t/** Which handle is being dragged. */\n\thandle: HandlePosition;\n\t/** The mouse position (pixels) when the drag started. */\n\tstartX: number;\n\tstartY: number;\n\t/** The crop rect (normalized) when the drag started. */\n\tstartRect: NormalizedRect;\n}\n\n/**\n * Compute the new crop rect for a free (no aspect ratio) resize.\n *\n * Each edge moves independently based on the handle being dragged.\n * Edges are clamped to the provided bounds and maintain a minimum size.\n *\n * @param drag The current drag state.\n * @param clientX Current mouse/touch X position in pixels.\n * @param clientY Current mouse/touch Y position in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param bounds The allowed crop area bounds.\n * @param minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @return The new crop rect in normalized coordinates.\n */\nexport function computeFreeResizeRect(\n\tdrag: ResizeDragState,\n\tclientX: number,\n\tclientY: number,\n\timageSize: Size,\n\tbounds: CropBounds,\n\tminCropSize: Size = DEFAULT_MIN_CROP_SIZE\n): NormalizedRect {\n\tconst dx =\n\t\timageSize.width > 0 ? ( clientX - drag.startX ) / imageSize.width : 0;\n\tconst dy =\n\t\timageSize.height > 0 ? ( clientY - drag.startY ) / imageSize.height : 0;\n\n\tconst s = drag.startRect;\n\tconst handle = drag.handle;\n\n\tlet edgeTop = s.y;\n\tlet edgeBottom = s.y + s.height;\n\tlet edgeLeft = s.x;\n\tlet edgeRight = s.x + s.width;\n\n\tif ( handle === 'n' || handle === 'nw' || handle === 'ne' ) {\n\t\tedgeTop = Math.max(\n\t\t\tbounds.minY,\n\t\t\tMath.min( s.y + dy, edgeBottom - minCropSize.height )\n\t\t);\n\t}\n\tif ( handle === 's' || handle === 'sw' || handle === 'se' ) {\n\t\tedgeBottom = Math.max(\n\t\t\tedgeTop + minCropSize.height,\n\t\t\tMath.min( s.y + s.height + dy, bounds.maxY )\n\t\t);\n\t}\n\tif ( handle === 'w' || handle === 'nw' || handle === 'sw' ) {\n\t\tedgeLeft = Math.max(\n\t\t\tbounds.minX,\n\t\t\tMath.min( s.x + dx, edgeRight - minCropSize.width )\n\t\t);\n\t}\n\tif ( handle === 'e' || handle === 'ne' || handle === 'se' ) {\n\t\tedgeRight = Math.max(\n\t\t\tedgeLeft + minCropSize.width,\n\t\t\tMath.min( s.x + s.width + dx, bounds.maxX )\n\t\t);\n\t}\n\n\treturn {\n\t\tx: edgeLeft,\n\t\ty: edgeTop,\n\t\twidth: edgeRight - edgeLeft,\n\t\theight: edgeBottom - edgeTop,\n\t};\n}\n\n/**\n * Compute the new crop rect for a locked-aspect-ratio corner resize.\n *\n * The opposite corner is the anchor. The dragged corner moves freely\n * but the result is clamped to maintain the aspect ratio and stay\n * within bounds.\n *\n * @param drag The current drag state.\n * @param clientX Current mouse/touch X position in pixels.\n * @param clientY Current mouse/touch Y position in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param bounds The allowed crop area bounds.\n * @param normalizedRatio The locked aspect ratio (width / height in normalized space).\n * @param minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @param driverAxis Optional explicit driver axis. Used for keyboard\n * resize, where one arrow axis should determine the step.\n * @return The new crop rect in normalized coordinates.\n */\nexport function computeLockedResizeRect(\n\tdrag: ResizeDragState,\n\tclientX: number,\n\tclientY: number,\n\timageSize: Size,\n\tbounds: CropBounds,\n\tnormalizedRatio: number,\n\tminCropSize: Size = DEFAULT_MIN_CROP_SIZE,\n\tdriverAxis?: ResizeDriverAxis\n): NormalizedRect {\n\t// The math below divides by `normalizedRatio` and `imageSize`, so\n\t// bail out with the start rect when any of them is zero. This can\n\t// happen if a resize is triggered (e.g. via keyboard arrows on a\n\t// focused handle) before the image has loaded.\n\tif (\n\t\tnormalizedRatio <= 0 ||\n\t\timageSize.width <= 0 ||\n\t\timageSize.height <= 0\n\t) {\n\t\treturn { ...drag.startRect };\n\t}\n\n\tconst dx = ( clientX - drag.startX ) / imageSize.width;\n\tconst dy = ( clientY - drag.startY ) / imageSize.height;\n\n\tconst s = drag.startRect;\n\tconst handle = drag.handle;\n\n\t// Determine the anchor corner (opposite to the dragged corner).\n\tconst anchorX = handle === 'nw' || handle === 'sw' ? s.x + s.width : s.x;\n\tconst anchorY = handle === 'nw' || handle === 'ne' ? s.y + s.height : s.y;\n\n\t// Direction the crop grows from the anchor (+1 = right/down, -1 = left/up).\n\tconst dirX = handle === 'nw' || handle === 'sw' ? -1 : 1;\n\tconst dirY = handle === 'nw' || handle === 'ne' ? -1 : 1;\n\n\t// Desired new position of the dragged corner.\n\tconst draggedX =\n\t\t( handle === 'nw' || handle === 'sw' ? s.x : s.x + s.width ) + dx;\n\tconst draggedY =\n\t\t( handle === 'nw' || handle === 'ne' ? s.y : s.y + s.height ) + dy;\n\n\t// Raw distances from anchor to dragged corner.\n\tlet distW = ( draggedX - anchorX ) * dirX;\n\tlet distH = ( draggedY - anchorY ) * dirY;\n\n\t// Enforce minimum size on both axes, projecting through the ratio so\n\t// neither axis falls below its per-axis floor.\n\tconst minDistW = Math.max(\n\t\tminCropSize.width,\n\t\tminCropSize.height * normalizedRatio\n\t);\n\tconst minDistH = minDistW / normalizedRatio;\n\tdistW = Math.max( distW, minDistW );\n\tdistH = Math.max( distH, minDistH );\n\n\t// Determine which axis \"drives\". Pointer drags use the dragged-corner\n\t// geometry so the projected crop changes continuously as the pointer moves.\n\t// Keyboard resize passes an explicit axis because a single-axis arrow step\n\t// should drive that axis instead of being cancelled by the unchanged axis.\n\tconst pixelRatio = ( normalizedRatio * imageSize.width ) / imageSize.height;\n\tconst isWidthDriver =\n\t\tdriverAxis === 'width' ||\n\t\t( ! driverAxis &&\n\t\t\t( distW * imageSize.width ) / ( distH * imageSize.height ) >\n\t\t\t\tpixelRatio );\n\n\tif ( isWidthDriver ) {\n\t\t// Width is the driver — compute height from ratio.\n\t\tdistH = distW / normalizedRatio;\n\t} else {\n\t\t// Height is the driver — compute width from ratio.\n\t\tdistW = distH * normalizedRatio;\n\t}\n\n\t// Clamp to image coverage bounds. If the rect would exceed,\n\t// shrink it (maintaining ratio) to fit.\n\tconst maxW = dirX > 0 ? bounds.maxX - anchorX : anchorX - bounds.minX;\n\tconst maxH = dirY > 0 ? bounds.maxY - anchorY : anchorY - bounds.minY;\n\n\tif ( distW > maxW ) {\n\t\tdistW = maxW;\n\t\tdistH = distW / normalizedRatio;\n\t}\n\tif ( distH > maxH ) {\n\t\tdistH = maxH;\n\t\tdistW = distH * normalizedRatio;\n\t}\n\n\t// Enforce minimum after clamping.\n\tdistW = Math.max( distW, minDistW );\n\tdistH = Math.max( distH, minDistH );\n\n\t// Compute the final rect position from the anchor.\n\tconst newX = dirX > 0 ? anchorX : anchorX - distW;\n\tconst newY = dirY > 0 ? anchorY : anchorY - distH;\n\n\treturn { x: newX, y: newY, width: distW, height: distH };\n}\n\n/**\n * Compute the new crop rect for a resize that temporarily locks the\n * aspect ratio to `drag.startRect`'s ratio (e.g. while Shift is held).\n *\n * Corner handles delegate to {@link computeLockedResizeRect}. Edge\n * handles size the dragged axis freely, then expand the perpendicular\n * axis symmetrically around the rect's center to preserve the ratio,\n * clamping symmetrically so the rect stays within bounds.\n *\n * @param drag The current drag state.\n * @param clientX Current mouse/touch X position in pixels.\n * @param clientY Current mouse/touch Y position in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param bounds The allowed crop area bounds.\n * @param minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @return The new crop rect in normalized coordinates.\n */\nexport function computeShiftLockedResizeRect(\n\tdrag: ResizeDragState,\n\tclientX: number,\n\tclientY: number,\n\timageSize: Size,\n\tbounds: CropBounds,\n\tminCropSize: Size = DEFAULT_MIN_CROP_SIZE\n): NormalizedRect {\n\tconst s = drag.startRect;\n\tconst pixelW = s.width * imageSize.width;\n\tconst pixelH = s.height * imageSize.height;\n\tif ( pixelH <= 0 || pixelW <= 0 ) {\n\t\treturn computeFreeResizeRect(\n\t\t\tdrag,\n\t\t\tclientX,\n\t\t\tclientY,\n\t\t\timageSize,\n\t\t\tbounds,\n\t\t\tminCropSize\n\t\t);\n\t}\n\tconst normalizedRatio = s.width / s.height;\n\n\tconst handle = drag.handle;\n\tif (\n\t\thandle === 'nw' ||\n\t\thandle === 'ne' ||\n\t\thandle === 'sw' ||\n\t\thandle === 'se'\n\t) {\n\t\treturn computeLockedResizeRect(\n\t\t\tdrag,\n\t\t\tclientX,\n\t\t\tclientY,\n\t\t\timageSize,\n\t\t\tbounds,\n\t\t\tnormalizedRatio,\n\t\t\tminCropSize\n\t\t);\n\t}\n\n\t// Edge handle: size the dragged axis with free-resize logic, then\n\t// expand the perpendicular axis symmetrically around the rect's center.\n\tconst free = computeFreeResizeRect(\n\t\tdrag,\n\t\tclientX,\n\t\tclientY,\n\t\timageSize,\n\t\tbounds,\n\t\tminCropSize\n\t);\n\n\tif ( handle === 'n' || handle === 's' ) {\n\t\t// Height is the driver; derive width from ratio.\n\t\tlet newHeight = free.height;\n\t\tlet newWidth = newHeight * normalizedRatio;\n\t\tconst centerX = s.x + s.width / 2;\n\t\t// Symmetric clamp: limit by the smaller of the two side gaps so\n\t\t// the rect stays centered around centerX.\n\t\tconst maxWidth =\n\t\t\tMath.min( centerX - bounds.minX, bounds.maxX - centerX ) * 2;\n\t\tif ( newWidth > maxWidth ) {\n\t\t\tnewWidth = maxWidth;\n\t\t\tnewHeight = newWidth / normalizedRatio;\n\t\t}\n\t\t// Enforce the minimum on the driving axis, then derive the\n\t\t// other axis from the ratio so it stays consistent. The\n\t\t// effective minimum height has to satisfy the per-axis floor\n\t\t// on both axes simultaneously.\n\t\tconst minHeight = Math.max(\n\t\t\tminCropSize.height,\n\t\t\tminCropSize.width / normalizedRatio\n\t\t);\n\t\tif ( newHeight < minHeight ) {\n\t\t\tnewHeight = minHeight;\n\t\t\tnewWidth = newHeight * normalizedRatio;\n\t\t}\n\t\t// Re-anchor the dragged axis to the opposite edge so the\n\t\t// height adjustment after clamping keeps that edge fixed.\n\t\tconst newY = handle === 'n' ? s.y + s.height - newHeight : s.y;\n\t\treturn {\n\t\t\tx: centerX - newWidth / 2,\n\t\t\ty: newY,\n\t\t\twidth: newWidth,\n\t\t\theight: newHeight,\n\t\t};\n\t}\n\n\t// 'e' or 'w': width is the driver; derive height from ratio.\n\tlet newWidth = free.width;\n\tlet newHeight = newWidth / normalizedRatio;\n\tconst centerY = s.y + s.height / 2;\n\tconst maxHeight =\n\t\tMath.min( centerY - bounds.minY, bounds.maxY - centerY ) * 2;\n\tif ( newHeight > maxHeight ) {\n\t\tnewHeight = maxHeight;\n\t\tnewWidth = newHeight * normalizedRatio;\n\t}\n\tconst minWidth = Math.max(\n\t\tminCropSize.width,\n\t\tminCropSize.height * normalizedRatio\n\t);\n\tif ( newWidth < minWidth ) {\n\t\tnewWidth = minWidth;\n\t\tnewHeight = newWidth / normalizedRatio;\n\t}\n\tconst newX = handle === 'w' ? s.x + s.width - newWidth : s.x;\n\treturn {\n\t\tx: newX,\n\t\ty: centerY - newHeight / 2,\n\t\twidth: newWidth,\n\t\theight: newHeight,\n\t};\n}\n"],
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["import { MIN_CROP_PIXELS, MIN_CROP_SCREEN_PX } from './constants';\nimport type { HandlePosition, NormalizedRect, Size } from './types';\n\nexport type { HandlePosition };\n\n/** Axis whose resize distance controls the locked-ratio size projection. */\nexport type ResizeDriverAxis = 'width' | 'height';\n\n/**\n * Resolve the minimum crop dimension, in source-image pixels, for the current\n * display scale.\n *\n * Two floors compete: a fixed source-pixel floor (`MIN_CROP_PIXELS`) that\n * avoids sub-pixel crops, and an on-screen floor (`MIN_CROP_SCREEN_PX`) that\n * keeps the crop large enough to grab. The on-screen floor converted to source\n * pixels is `MIN_CROP_SCREEN_PX / displayScale`; the binding constraint is the\n * larger of the two. When the image is shown small (small `displayScale`) the\n * on-screen floor dominates; once it is shown large enough the source-pixel\n * floor takes over.\n *\n * @param displayScale CSS pixels rendered per source pixel\n * (fit scale × zoom × view scale).\n * @return Minimum crop dimension in source-image pixels.\n */\nexport function getMinCropPixels( displayScale: number ): number {\n\tif ( displayScale <= 0 ) {\n\t\treturn MIN_CROP_PIXELS;\n\t}\n\treturn Math.max( MIN_CROP_PIXELS, MIN_CROP_SCREEN_PX / displayScale );\n}\n\n/**\n * Default minimum crop rect dimension in normalized space, used when no\n * explicit per-axis minimum is supplied (e.g. before the source image\n * has loaded). Real usage derives a pixel-based min from the source\n * image dimensions and `MIN_CROP_PIXELS` in `constants.ts`.\n */\nexport const DEFAULT_MIN_CROP_SIZE: Size = { width: 0.05, height: 0.05 };\n\n/**\n * Bounds within which the crop rect must stay.\n */\nexport interface CropBounds {\n\tminX: number;\n\tminY: number;\n\tmaxX: number;\n\tmaxY: number;\n}\n\n/**\n * Drag state for tracking a resize interaction.\n */\nexport interface ResizeDragState {\n\t/** Which handle is being dragged. */\n\thandle: HandlePosition;\n\t/** The mouse position (pixels) when the drag started. */\n\tstartX: number;\n\tstartY: number;\n\t/** The crop rect (normalized) when the drag started. */\n\tstartRect: NormalizedRect;\n}\n\n/**\n * Compute the new crop rect for a free (no aspect ratio) resize.\n *\n * Each edge moves independently based on the handle being dragged.\n * Edges are clamped to the provided bounds and maintain a minimum size.\n *\n * @param drag The current drag state.\n * @param clientX Current mouse/touch X position in pixels.\n * @param clientY Current mouse/touch Y position in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param bounds The allowed crop area bounds.\n * @param minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @return The new crop rect in normalized coordinates.\n */\nexport function computeFreeResizeRect(\n\tdrag: ResizeDragState,\n\tclientX: number,\n\tclientY: number,\n\timageSize: Size,\n\tbounds: CropBounds,\n\tminCropSize: Size = DEFAULT_MIN_CROP_SIZE\n): NormalizedRect {\n\tconst dx =\n\t\timageSize.width > 0 ? ( clientX - drag.startX ) / imageSize.width : 0;\n\tconst dy =\n\t\timageSize.height > 0 ? ( clientY - drag.startY ) / imageSize.height : 0;\n\n\tconst s = drag.startRect;\n\tconst handle = drag.handle;\n\n\tlet edgeTop = s.y;\n\tlet edgeBottom = s.y + s.height;\n\tlet edgeLeft = s.x;\n\tlet edgeRight = s.x + s.width;\n\n\tif ( handle === 'n' || handle === 'nw' || handle === 'ne' ) {\n\t\tedgeTop = Math.max(\n\t\t\tbounds.minY,\n\t\t\tMath.min( s.y + dy, edgeBottom - minCropSize.height )\n\t\t);\n\t}\n\tif ( handle === 's' || handle === 'sw' || handle === 'se' ) {\n\t\tedgeBottom = Math.max(\n\t\t\tedgeTop + minCropSize.height,\n\t\t\tMath.min( s.y + s.height + dy, bounds.maxY )\n\t\t);\n\t}\n\tif ( handle === 'w' || handle === 'nw' || handle === 'sw' ) {\n\t\tedgeLeft = Math.max(\n\t\t\tbounds.minX,\n\t\t\tMath.min( s.x + dx, edgeRight - minCropSize.width )\n\t\t);\n\t}\n\tif ( handle === 'e' || handle === 'ne' || handle === 'se' ) {\n\t\tedgeRight = Math.max(\n\t\t\tedgeLeft + minCropSize.width,\n\t\t\tMath.min( s.x + s.width + dx, bounds.maxX )\n\t\t);\n\t}\n\n\treturn {\n\t\tx: edgeLeft,\n\t\ty: edgeTop,\n\t\twidth: edgeRight - edgeLeft,\n\t\theight: edgeBottom - edgeTop,\n\t};\n}\n\n/**\n * Compute the new crop rect for a locked-aspect-ratio corner resize.\n *\n * The opposite corner is the anchor. The dragged corner moves freely\n * but the result is clamped to maintain the aspect ratio and stay\n * within bounds.\n *\n * @param drag The current drag state.\n * @param clientX Current mouse/touch X position in pixels.\n * @param clientY Current mouse/touch Y position in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param bounds The allowed crop area bounds.\n * @param normalizedRatio The locked aspect ratio (width / height in normalized space).\n * @param minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @param driverAxis Optional explicit driver axis. Used for keyboard\n * resize, where one arrow axis should determine the step.\n * @return The new crop rect in normalized coordinates.\n */\nexport function computeLockedResizeRect(\n\tdrag: ResizeDragState,\n\tclientX: number,\n\tclientY: number,\n\timageSize: Size,\n\tbounds: CropBounds,\n\tnormalizedRatio: number,\n\tminCropSize: Size = DEFAULT_MIN_CROP_SIZE,\n\tdriverAxis?: ResizeDriverAxis\n): NormalizedRect {\n\t// The math below divides by `normalizedRatio` and `imageSize`, so\n\t// bail out with the start rect when any of them is zero. This can\n\t// happen if a resize is triggered (e.g. via keyboard arrows on a\n\t// focused handle) before the image has loaded.\n\tif (\n\t\tnormalizedRatio <= 0 ||\n\t\timageSize.width <= 0 ||\n\t\timageSize.height <= 0\n\t) {\n\t\treturn { ...drag.startRect };\n\t}\n\n\tconst dx = ( clientX - drag.startX ) / imageSize.width;\n\tconst dy = ( clientY - drag.startY ) / imageSize.height;\n\n\tconst s = drag.startRect;\n\tconst handle = drag.handle;\n\n\t// Determine the anchor corner (opposite to the dragged corner).\n\tconst anchorX = handle === 'nw' || handle === 'sw' ? s.x + s.width : s.x;\n\tconst anchorY = handle === 'nw' || handle === 'ne' ? s.y + s.height : s.y;\n\n\t// Direction the crop grows from the anchor (+1 = right/down, -1 = left/up).\n\tconst dirX = handle === 'nw' || handle === 'sw' ? -1 : 1;\n\tconst dirY = handle === 'nw' || handle === 'ne' ? -1 : 1;\n\n\t// Desired new position of the dragged corner.\n\tconst draggedX =\n\t\t( handle === 'nw' || handle === 'sw' ? s.x : s.x + s.width ) + dx;\n\tconst draggedY =\n\t\t( handle === 'nw' || handle === 'ne' ? s.y : s.y + s.height ) + dy;\n\n\t// Raw distances from anchor to dragged corner.\n\tlet distW = ( draggedX - anchorX ) * dirX;\n\tlet distH = ( draggedY - anchorY ) * dirY;\n\n\t// Enforce minimum size on both axes, projecting through the ratio so\n\t// neither axis falls below its per-axis floor.\n\tconst minDistW = Math.max(\n\t\tminCropSize.width,\n\t\tminCropSize.height * normalizedRatio\n\t);\n\tconst minDistH = minDistW / normalizedRatio;\n\tdistW = Math.max( distW, minDistW );\n\tdistH = Math.max( distH, minDistH );\n\n\t// Determine which axis \"drives\". Pointer drags use the dragged-corner\n\t// geometry so the projected crop changes continuously as the pointer moves.\n\t// Keyboard resize passes an explicit axis because a single-axis arrow step\n\t// should drive that axis instead of being cancelled by the unchanged axis.\n\tconst pixelRatio = ( normalizedRatio * imageSize.width ) / imageSize.height;\n\tconst isWidthDriver =\n\t\tdriverAxis === 'width' ||\n\t\t( ! driverAxis &&\n\t\t\t( distW * imageSize.width ) / ( distH * imageSize.height ) >\n\t\t\t\tpixelRatio );\n\n\tif ( isWidthDriver ) {\n\t\t// Width is the driver — compute height from ratio.\n\t\tdistH = distW / normalizedRatio;\n\t} else {\n\t\t// Height is the driver — compute width from ratio.\n\t\tdistW = distH * normalizedRatio;\n\t}\n\n\t// Clamp to image coverage bounds. If the rect would exceed,\n\t// shrink it (maintaining ratio) to fit.\n\tconst maxW = dirX > 0 ? bounds.maxX - anchorX : anchorX - bounds.minX;\n\tconst maxH = dirY > 0 ? bounds.maxY - anchorY : anchorY - bounds.minY;\n\n\tif ( distW > maxW ) {\n\t\tdistW = maxW;\n\t\tdistH = distW / normalizedRatio;\n\t}\n\tif ( distH > maxH ) {\n\t\tdistH = maxH;\n\t\tdistW = distH * normalizedRatio;\n\t}\n\n\t// Enforce minimum after clamping.\n\tdistW = Math.max( distW, minDistW );\n\tdistH = Math.max( distH, minDistH );\n\n\t// Compute the final rect position from the anchor.\n\tconst newX = dirX > 0 ? anchorX : anchorX - distW;\n\tconst newY = dirY > 0 ? anchorY : anchorY - distH;\n\n\treturn { x: newX, y: newY, width: distW, height: distH };\n}\n\n/**\n * Compute the new crop rect for a resize that temporarily locks the\n * aspect ratio to `drag.startRect`'s ratio (e.g. while Shift is held).\n *\n * Corner handles delegate to {@link computeLockedResizeRect}. Edge\n * handles size the dragged axis freely, then expand the perpendicular\n * axis symmetrically around the rect's center to preserve the ratio,\n * clamping symmetrically so the rect stays within bounds.\n *\n * @param drag The current drag state.\n * @param clientX Current mouse/touch X position in pixels.\n * @param clientY Current mouse/touch Y position in pixels.\n * @param imageSize The rendered image dimensions in pixels.\n * @param bounds The allowed crop area bounds.\n * @param minCropSize Minimum crop rect dimension in normalized space, per axis.\n * @return The new crop rect in normalized coordinates.\n */\nexport function computeShiftLockedResizeRect(\n\tdrag: ResizeDragState,\n\tclientX: number,\n\tclientY: number,\n\timageSize: Size,\n\tbounds: CropBounds,\n\tminCropSize: Size = DEFAULT_MIN_CROP_SIZE\n): NormalizedRect {\n\tconst s = drag.startRect;\n\tconst pixelW = s.width * imageSize.width;\n\tconst pixelH = s.height * imageSize.height;\n\tif ( pixelH <= 0 || pixelW <= 0 ) {\n\t\treturn computeFreeResizeRect(\n\t\t\tdrag,\n\t\t\tclientX,\n\t\t\tclientY,\n\t\t\timageSize,\n\t\t\tbounds,\n\t\t\tminCropSize\n\t\t);\n\t}\n\tconst normalizedRatio = s.width / s.height;\n\n\tconst handle = drag.handle;\n\tif (\n\t\thandle === 'nw' ||\n\t\thandle === 'ne' ||\n\t\thandle === 'sw' ||\n\t\thandle === 'se'\n\t) {\n\t\treturn computeLockedResizeRect(\n\t\t\tdrag,\n\t\t\tclientX,\n\t\t\tclientY,\n\t\t\timageSize,\n\t\t\tbounds,\n\t\t\tnormalizedRatio,\n\t\t\tminCropSize\n\t\t);\n\t}\n\n\t// Edge handle: size the dragged axis with free-resize logic, then\n\t// expand the perpendicular axis symmetrically around the rect's center.\n\tconst free = computeFreeResizeRect(\n\t\tdrag,\n\t\tclientX,\n\t\tclientY,\n\t\timageSize,\n\t\tbounds,\n\t\tminCropSize\n\t);\n\n\tif ( handle === 'n' || handle === 's' ) {\n\t\t// Height is the driver; derive width from ratio.\n\t\tlet newHeight = free.height;\n\t\tlet newWidth = newHeight * normalizedRatio;\n\t\tconst centerX = s.x + s.width / 2;\n\t\t// Symmetric clamp: limit by the smaller of the two side gaps so\n\t\t// the rect stays centered around centerX.\n\t\tconst maxWidth =\n\t\t\tMath.min( centerX - bounds.minX, bounds.maxX - centerX ) * 2;\n\t\tif ( newWidth > maxWidth ) {\n\t\t\tnewWidth = maxWidth;\n\t\t\tnewHeight = newWidth / normalizedRatio;\n\t\t}\n\t\t// Enforce the minimum on the driving axis, then derive the\n\t\t// other axis from the ratio so it stays consistent. The\n\t\t// effective minimum height has to satisfy the per-axis floor\n\t\t// on both axes simultaneously.\n\t\tconst minHeight = Math.max(\n\t\t\tminCropSize.height,\n\t\t\tminCropSize.width / normalizedRatio\n\t\t);\n\t\tif ( newHeight < minHeight ) {\n\t\t\tnewHeight = minHeight;\n\t\t\tnewWidth = newHeight * normalizedRatio;\n\t\t}\n\t\t// Re-anchor the dragged axis to the opposite edge so the\n\t\t// height adjustment after clamping keeps that edge fixed.\n\t\tconst newY = handle === 'n' ? s.y + s.height - newHeight : s.y;\n\t\treturn {\n\t\t\tx: centerX - newWidth / 2,\n\t\t\ty: newY,\n\t\t\twidth: newWidth,\n\t\t\theight: newHeight,\n\t\t};\n\t}\n\n\t// 'e' or 'w': width is the driver; derive height from ratio.\n\tlet newWidth = free.width;\n\tlet newHeight = newWidth / normalizedRatio;\n\tconst centerY = s.y + s.height / 2;\n\tconst maxHeight =\n\t\tMath.min( centerY - bounds.minY, bounds.maxY - centerY ) * 2;\n\tif ( newHeight > maxHeight ) {\n\t\tnewHeight = maxHeight;\n\t\tnewWidth = newHeight * normalizedRatio;\n\t}\n\tconst minWidth = Math.max(\n\t\tminCropSize.width,\n\t\tminCropSize.height * normalizedRatio\n\t);\n\tif ( newWidth < minWidth ) {\n\t\tnewWidth = minWidth;\n\t\tnewHeight = newWidth / normalizedRatio;\n\t}\n\tconst newX = handle === 'w' ? s.x + s.width - newWidth : s.x;\n\treturn {\n\t\tx: newX,\n\t\ty: centerY - newHeight / 2,\n\t\twidth: newWidth,\n\t\theight: newHeight,\n\t};\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,iBAAiB,0BAA0B;AAwB7C,SAAS,iBAAkB,cAA+B;AAChE,MAAK,gBAAgB,GAAI;AACxB,WAAO;AAAA,EACR;AACA,SAAO,KAAK,IAAK,iBAAiB,qBAAqB,YAAa;AACrE;AAQO,IAAM,wBAA8B,EAAE,OAAO,MAAM,QAAQ,KAAK;AAuChE,SAAS,sBACf,MACA,SACA,SACA,WACA,QACA,cAAoB,uBACH;AACjB,QAAM,KACL,UAAU,QAAQ,KAAM,UAA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,YAAY,QAAQ;AAAA,IACrB;AACA,QAAKD,aAAY,WAAY;AAC5B,MAAAA,aAAY;AACZ,MAAAC,YAAWD,aAAY;AAAA,IACxB;AAGA,UAAM,OAAO,WAAW,MAAM,EAAE,IAAI,EAAE,SAASA,aAAY,EAAE;AAC7D,WAAO;AAAA,MACN,GAAG,UAAUC,YAAW;AAAA,MACxB,GAAG;AAAA,MACH,OAAOA;AAAA,MACP,QAAQD;AAAA,IACT;AAAA,EACD;AAGA,MAAI,WAAW,KAAK;AACpB,MAAI,YAAY,WAAW;AAC3B,QAAM,UAAU,EAAE,IAAI,EAAE,SAAS;AACjC,QAAM,YACL,KAAK,IAAK,UAAU,OAAO,MAAM,OAAO,OAAO,OAAQ,IAAI;AAC5D,MAAK,YAAY,WAAY;AAC5B,gBAAY;AACZ,eAAW,YAAY;AAAA,EACxB;AACA,QAAM,WAAW,KAAK;AAAA,IACrB,YAAY;AAAA,IACZ,YAAY,SAAS;AAAA,EACtB;AACA,MAAK,WAAW,UAAW;AAC1B,eAAW;AACX,gBAAY,WAAW;AAAA,EACxB;AACA,QAAM,OAAO,WAAW,MAAM,EAAE,IAAI,EAAE,QAAQ,WAAW,EAAE;AAC3D,SAAO;AAAA,IACN,GAAG;AAAA,IACH,GAAG,UAAU,YAAY;AAAA,IACzB,OAAO;AAAA,IACP,QAAQ;AAAA,EACT;AACD;",
|
|
6
6
|
"names": ["newHeight", "newWidth"]
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/image-editor/core/transform-style.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["import type { CropperState, Size } from './types';\nimport { degreesToRadians } from './math/rotation';\nimport { isValidSize, sanitizeCropperState } from './math/sanitize';\n\n/** Identity CSS transform — applied when inputs aren't safe to compose. */\nconst IDENTITY_MATRIX_STYLE = 'matrix(1, 0, 0, 1, 0, 0)';\n\n/**\n * Computes a CSS matrix() transform string from the cropper state.\n *\n * The combined transform is: translate(tx, ty) * scale(sx, sy) * rotate(r) * scale(z)\n * expressed as a 2D CSS matrix(a, b, c, d, tx, ty).\n *\n * Flip is composed outside rotation so it is viewport-relative — horizontal\n * flip mirrors across the viewport's vertical axis regardless of rotation.\n * Must match the matrix order in `createCamera` / `getImageCropBounds`.\n *\n * This is a pure function with no framework dependencies. The React hook\n * `useTransformStyle` wraps this in `useMemo` for memoization.\n *\n * @param state The current cropper state.\n * @param imageSize The rendered image dimensions in pixels.\n * @return A CSS transform string.\n */\nexport function computeTransformStyle(\n\tstate: CropperState,\n\timageSize: Size\n): string {\n\t// Match the identity short-circuit used by `createCamera` and\n\t// `getSourceRegion` so the preview path stays consistent with the math\n\t// path when `imageSize` itself is hostile (state sanitization below\n\t// only guards the state fields, not these size arguments).\n\tif ( ! isValidSize( imageSize ) ) {\n\t\treturn IDENTITY_MATRIX_STYLE;\n\t}\n\t// Sanitize so hostile state can't produce `matrix(NaN, NaN, ...)` here\n\t// while the parallel `createCamera` path returns a finite matrix. Both\n\t// paths must agree under defense-in-depth conditions.\n\tconst safeState = sanitizeCropperState( state );\n\tconst translateX = safeState.pan.x * imageSize.width;\n\tconst translateY = safeState.pan.y * imageSize.height;\n\tconst rad = degreesToRadians( safeState.rotation );\n\tconst cos = Math.cos( rad );\n\tconst sin = Math.sin( rad );\n\tconst sx = safeState.flip.horizontal ? -1 : 1;\n\tconst sy = safeState.flip.vertical ? -1 : 1;\n\tconst z = safeState.zoom;\n\n\t// Combined: translate(tx,ty) * scale(sx,sy) * rotate(r) * scale(z)\n\tconst a = sx * cos * z;\n\tconst b = sy * sin * z;\n\tconst c = -sx * sin * z;\n\tconst d = sy * cos * z;\n\n\treturn `matrix(${ a }, ${ b }, ${ c }, ${ d }, ${ translateX }, ${ translateY })`;\n}\n"],
|
|
5
|
+
"mappings": ";AACA,SAAS,wBAAwB;AACjC,SAAS,aAAa,4BAA4B;AAGlD,IAAM,wBAAwB;AAmBvB,SAAS,sBACf,OACA,WACS;AAKT,MAAK,CAAE,YAAa,SAAU,GAAI;AACjC,WAAO;AAAA,EACR;AAIA,QAAM,YAAY,qBAAsB,KAAM;AAC9C,QAAM,aAAa,UAAU,IAAI,IAAI,UAAU;AAC/C,QAAM,aAAa,UAAU,IAAI,IAAI,UAAU;AAC/C,QAAM,MAAM,iBAAkB,UAAU,QAAS;AACjD,QAAM,MAAM,KAAK,IAAK,GAAI;AAC1B,QAAM,MAAM,KAAK,IAAK,GAAI;AAC1B,QAAM,KAAK,UAAU,KAAK,aAAa,KAAK;AAC5C,QAAM,KAAK,UAAU,KAAK,WAAW,KAAK;AAC1C,QAAM,IAAI,UAAU;AAGpB,QAAM,IAAI,KAAK,MAAM;AACrB,QAAM,IAAI,KAAK,MAAM;AACrB,QAAM,IAAI,CAAC,KAAK,MAAM;AACtB,QAAM,IAAI,KAAK,MAAM;AAErB,SAAO,UAAW,CAAE,KAAM,CAAE,KAAM,CAAE,KAAM,CAAE,KAAM,UAAW,KAAM,UAAW;AAC/E;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/image-editor/core/transforms/pipeline.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["import type { TransformOperation, CropperState } from '../types';\nimport { DEFAULT_STATE } from '../constants';\nimport { cropperReducer } from '../state';\n\n/**\n * Apply a single transform operation to a cropper state, returning\n * a new state with the containment invariant enforced.\n *\n * Delegates to the reducer via APPLY_OPERATION so pipeline replay\n * produces identical bounded state to UI interaction or setter\n * calls. The same logical operation yields the same result\n * regardless of entrypoint.\n *\n * @param state The current cropper state.\n * @param op The operation to apply.\n * @return A new cropper state with the operation applied.\n */\nexport function applyOperationToState(\n\tstate: CropperState,\n\top: TransformOperation\n): CropperState {\n\treturn cropperReducer( state, { type: 'APPLY_OPERATION', payload: op } );\n}\n\n/**\n * Replay all operations from an initial state to produce the final state.\n *\n * @param pipeline The array of transform operations to replay.\n * @param initialState The starting state. Defaults to DEFAULT_STATE.\n * @return The resulting cropper state after all operations are applied.\n */\nexport function stateFromPipeline(\n\tpipeline: TransformOperation[],\n\tinitialState: CropperState = { ...DEFAULT_STATE }\n): CropperState {\n\treturn pipeline.reduce(\n\t\t( state, op ) => applyOperationToState( state, op ),\n\t\tinitialState\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";AACA,SAAS,qBAAqB;AAC9B,SAAS,sBAAsB;AAexB,SAAS,sBACf,OACA,IACe;AACf,SAAO,eAAgB,OAAO,EAAE,MAAM,mBAAmB,SAAS,GAAG,CAAE;AACxE;AASO,SAAS,kBACf,UACA,eAA6B,EAAE,GAAG,cAAc,GACjC;AACf,SAAO,SAAS;AAAA,IACf,CAAE,OAAO,OAAQ,sBAAuB,OAAO,EAAG;AAAA,IAClD;AAAA,EACD;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/image-editor/core/viewport-state.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["import type { ViewportState, ViewportAction } from './types';\n\nexport const DEFAULT_VIEWPORT_STATE: ViewportState = {\n\tzoom: 1,\n\tpan: { x: 0, y: 0 },\n};\n\nexport function viewportReducer(\n\tstate: ViewportState,\n\taction: ViewportAction\n): ViewportState {\n\tswitch ( action.type ) {\n\t\tcase 'SET_VIEWPORT_ZOOM': {\n\t\t\tconst zoom = Math.min( 4, Math.max( 0.1, action.payload ) );\n\t\t\treturn zoom === state.zoom ? state : { ...state, zoom };\n\t\t}\n\t\tcase 'SET_VIEWPORT_PAN': {\n\t\t\tconst { x, y } = action.payload;\n\t\t\treturn x === state.pan.x && y === state.pan.y\n\t\t\t\t? state\n\t\t\t\t: { ...state, pan: action.payload };\n\t\t}\n\t\tcase 'RESET_VIEWPORT':\n\t\t\treturn state.zoom === DEFAULT_VIEWPORT_STATE.zoom &&\n\t\t\t\tstate.pan.x === DEFAULT_VIEWPORT_STATE.pan.x &&\n\t\t\t\tstate.pan.y === DEFAULT_VIEWPORT_STATE.pan.y\n\t\t\t\t? state\n\t\t\t\t: DEFAULT_VIEWPORT_STATE;\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";AAEO,IAAM,yBAAwC;AAAA,EACpD,MAAM;AAAA,EACN,KAAK,EAAE,GAAG,GAAG,GAAG,EAAE;AACnB;AAEO,SAAS,gBACf,OACA,QACgB;AAChB,UAAS,OAAO,MAAO;AAAA,IACtB,KAAK,qBAAqB;AACzB,YAAM,OAAO,KAAK,IAAK,GAAG,KAAK,IAAK,KAAK,OAAO,OAAQ,CAAE;AAC1D,aAAO,SAAS,MAAM,OAAO,QAAQ,EAAE,GAAG,OAAO,KAAK;AAAA,IACvD;AAAA,IACA,KAAK,oBAAoB;AACxB,YAAM,EAAE,GAAG,EAAE,IAAI,OAAO;AACxB,aAAO,MAAM,MAAM,IAAI,KAAK,MAAM,MAAM,IAAI,IACzC,QACA,EAAE,GAAG,OAAO,KAAK,OAAO,QAAQ;AAAA,IACpC;AAAA,IACA,KAAK;AACJ,aAAO,MAAM,SAAS,uBAAuB,QAC5C,MAAM,IAAI,MAAM,uBAAuB,IAAI,KAC3C,MAAM,IAAI,MAAM,uBAAuB,IAAI,IACzC,QACA;AAAA,EACL;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/image-editor/react/components/cropper-provider.tsx"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["import { createContext, useContext } from '@wordpress/element';\nimport {\n\tuseCropperReducer,\n\ttype CropperController,\n} from '../hooks/use-cropper-reducer';\nimport type { CropperState } from '../../core/types';\n\n/**\n * The context value type for the CropperProvider.\n * Contains the cropper controller from useCropperReducer.\n */\ntype CropperContextValue = CropperController | null;\n\nconst CropperContext = createContext< CropperContextValue >( null );\n\n/**\n * Props for the CropperProvider component.\n */\ninterface CropperProviderProps {\n\t/** Optional partial initial state to merge with defaults. */\n\tinitialState?: Partial< CropperState >;\n\t/** Child components. */\n\tchildren: React.ReactNode;\n}\n\n/**\n * Convenience context provider that wraps `useCropperReducer`.\n *\n * Provides the pure cropper controller (state + setters, NO undo\n * history) to all descendant components via React context.\n *\n * History is a higher-layer concern owned by composite stores\n * (see `useMediaEditorState` in media-editor/state for the media\n * editor's composite controller with undo/redo).\n *\n * @param props\n * @param props.initialState\n * @param props.children\n * @return The provider element wrapping children.\n */\nexport function CropperProvider( {\n\tinitialState,\n\tchildren,\n}: CropperProviderProps ) {\n\tconst cropperReturn = useCropperReducer( initialState );\n\n\treturn (\n\t\t<CropperContext.Provider value={ cropperReturn }>\n\t\t\t{ children }\n\t\t</CropperContext.Provider>\n\t);\n}\n\n/**\n * Hook to consume the CropperProvider context.\n *\n * Must be used within a CropperProvider. Throws if used outside\n * of the provider tree.\n *\n * @return The cropper controller.\n */\nexport function useCropper(): CropperController {\n\tconst context = useContext( CropperContext );\n\n\tif ( ! context ) {\n\t\tthrow new Error( 'useCropper must be used within a CropperProvider.' );\n\t}\n\n\treturn context;\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,eAAe,kBAAkB;AAC1C;AAAA,EACC;AAAA,OAEM;AA2CL;AAlCF,IAAM,iBAAiB,cAAsC,IAAK;AA2B3D,SAAS,gBAAiB;AAAA,EAChC;AAAA,EACA;AACD,GAA0B;AACzB,QAAM,gBAAgB,kBAAmB,YAAa;AAEtD,SACC,oBAAC,eAAe,UAAf,EAAwB,OAAQ,eAC9B,UACH;AAEF;AAUO,SAAS,aAAgC;AAC/C,QAAM,UAAU,WAAY,cAAe;AAE3C,MAAK,CAAE,SAAU;AAChB,UAAM,IAAI,MAAO,mDAAoD;AAAA,EACtE;AAEA,SAAO;AACR;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|