@wordpress/media-editor 0.11.0 → 0.11.2-next.v.202606191442.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 +16 -0
- package/build/components/media-editor/index.cjs +41 -51
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs.map +1 -1
- package/build/components/media-editor-canvas/index.cjs +63 -15
- package/build/components/media-editor-canvas/index.cjs.map +3 -3
- package/build/components/media-editor-crop-panel/index.cjs +15 -45
- package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
- package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
- package/build/components/media-editor-image-controls/index.cjs +230 -0
- package/build/components/media-editor-image-controls/index.cjs.map +7 -0
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +4 -4
- package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +2 -2
- package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -1
- package/build/components/media-form/index.cjs +12 -23
- package/build/components/media-form/index.cjs.map +3 -3
- package/build/components/rotation-ruler/index.cjs +1 -1
- package/build/components/rotation-ruler/index.cjs.map +1 -1
- package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -1
- package/build/image-editor/core/camera.cjs +13 -0
- package/build/image-editor/core/camera.cjs.map +2 -2
- package/build/image-editor/core/constants.cjs +15 -3
- package/build/image-editor/core/constants.cjs.map +2 -2
- package/build/image-editor/core/containment.cjs.map +1 -1
- package/build/image-editor/core/crop-rect.cjs.map +1 -1
- package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -1
- package/build/image-editor/core/interaction-controller.cjs +9 -7
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/core/math/sanitize.cjs.map +1 -1
- package/build/image-editor/core/setter-helpers.cjs.map +1 -1
- package/build/image-editor/core/source-region.cjs +111 -2
- package/build/image-editor/core/source-region.cjs.map +2 -2
- package/build/image-editor/core/state.cjs.map +1 -1
- package/build/image-editor/core/stencil-math.cjs +14 -6
- package/build/image-editor/core/stencil-math.cjs.map +2 -2
- package/build/image-editor/core/transform-style.cjs.map +1 -1
- package/build/image-editor/core/types.cjs.map +1 -1
- package/build/image-editor/react/components/cropper.cjs +231 -49
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -1
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs +1 -0
- package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +65 -19
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -1
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs +1 -1
- package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -1
- package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -1
- package/build/index.cjs.map +1 -1
- package/build/state/types.cjs.map +1 -1
- package/build/state/use-media-editor-state.cjs +1 -1
- package/build/state/use-media-editor-state.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +41 -51
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs.map +1 -1
- package/build-module/components/media-editor-canvas/index.mjs +55 -17
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +8 -51
- 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 +221 -0
- package/build-module/components/media-editor-image-controls/index.mjs.map +7 -0
- package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +4 -4
- 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 +1 -1
- package/build-module/components/media-form/index.mjs +12 -13
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/rotation-ruler/index.mjs +1 -1
- package/build-module/components/rotation-ruler/index.mjs.map +1 -1
- package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -1
- package/build-module/image-editor/core/camera.mjs +12 -0
- package/build-module/image-editor/core/camera.mjs.map +2 -2
- package/build-module/image-editor/core/constants.mjs +10 -2
- package/build-module/image-editor/core/constants.mjs.map +2 -2
- package/build-module/image-editor/core/containment.mjs.map +1 -1
- package/build-module/image-editor/core/crop-rect.mjs.map +1 -1
- package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -1
- package/build-module/image-editor/core/interaction-controller.mjs +9 -7
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/core/math/sanitize.mjs.map +1 -1
- package/build-module/image-editor/core/setter-helpers.mjs.map +1 -1
- package/build-module/image-editor/core/source-region.mjs +108 -1
- package/build-module/image-editor/core/source-region.mjs.map +2 -2
- package/build-module/image-editor/core/state.mjs.map +1 -1
- package/build-module/image-editor/core/stencil-math.mjs +12 -5
- package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
- package/build-module/image-editor/core/transform-style.mjs.map +1 -1
- package/build-module/image-editor/react/components/cropper.mjs +242 -52
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -1
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +1 -0
- package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +65 -19
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -1
- package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +1 -1
- 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 +1 -1
- package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -1
- package/build-module/index.mjs.map +1 -1
- package/build-module/state/types.mjs.map +1 -1
- package/build-module/state/use-media-editor-state.mjs +1 -1
- package/build-module/state/use-media-editor-state.mjs.map +2 -2
- package/build-style/style-rtl.css +179 -47
- package/build-style/style.css +179 -46
- package/build-types/components/media-editor/index.d.ts +6 -7
- package/build-types/components/media-editor/index.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 +11 -6
- 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 +4 -3
- 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 +46 -0
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -0
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/image-editor/core/camera.d.ts +25 -1
- package/build-types/image-editor/core/camera.d.ts.map +1 -1
- package/build-types/image-editor/core/constants.d.ts +30 -1
- package/build-types/image-editor/core/constants.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/core/source-region.d.ts +23 -1
- package/build-types/image-editor/core/source-region.d.ts.map +1 -1
- package/build-types/image-editor/core/stencil-math.d.ts +22 -4
- package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
- package/build-types/image-editor/core/types.d.ts +14 -0
- package/build-types/image-editor/core/types.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +17 -14
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/package.json +30 -20
- package/src/components/media-editor/index.tsx +69 -77
- package/src/components/media-editor/style.scss +136 -4
- package/src/components/media-editor-canvas/index.tsx +82 -13
- package/src/components/media-editor-canvas/style.scss +41 -0
- package/src/components/media-editor-crop-panel/index.tsx +16 -62
- package/src/components/media-editor-crop-panel/test/index.tsx +25 -41
- package/src/components/media-editor-fine-rotation/index.tsx +4 -3
- package/src/components/media-editor-image-controls/index.tsx +262 -0
- package/src/components/media-editor-image-controls/style.scss +48 -0
- package/src/components/media-editor-image-controls/test/index.tsx +150 -0
- package/src/components/media-editor-modal/style.scss +32 -42
- package/src/components/media-form/index.tsx +17 -21
- package/src/components/media-form/style.scss +6 -0
- package/src/components/rotation-ruler/style.scss +10 -3
- package/src/image-editor/core/camera.ts +60 -1
- package/src/image-editor/core/constants.ts +34 -1
- package/src/image-editor/core/interaction-controller.ts +25 -11
- package/src/image-editor/core/source-region.ts +202 -1
- package/src/image-editor/core/stencil-math.ts +42 -10
- package/src/image-editor/core/test/camera.ts +272 -1
- package/src/image-editor/core/test/interaction-controller.ts +100 -0
- package/src/image-editor/core/test/stencil-math.ts +153 -0
- package/src/image-editor/core/types.ts +17 -0
- package/src/image-editor/react/components/cropper.scss +28 -17
- package/src/image-editor/react/components/cropper.tsx +362 -62
- package/src/image-editor/react/components/overlays/dimming-overlay.tsx +1 -0
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +101 -32
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +169 -17
- package/src/image-editor/react/components/test/cropper.tsx +689 -6
- package/src/style.scss +2 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/build/components/media-editor-transform-controls/index.cjs +0 -84
- package/build/components/media-editor-transform-controls/index.cjs.map +0 -7
- package/build/components/media-form/sidebar-datetime-view.cjs +0 -53
- package/build/components/media-form/sidebar-datetime-view.cjs.map +0 -7
- package/build-module/components/media-editor-transform-controls/index.mjs +0 -68
- package/build-module/components/media-editor-transform-controls/index.mjs.map +0 -7
- package/build-module/components/media-form/sidebar-datetime-view.mjs +0 -32
- package/build-module/components/media-form/sidebar-datetime-view.mjs.map +0 -7
- package/build-types/components/media-editor-transform-controls/index.d.ts +0 -8
- 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/src/components/media-editor-transform-controls/index.tsx +0 -70
- package/src/components/media-editor-transform-controls/style.scss +0 -8
- package/src/components/media-form/sidebar-datetime-view.tsx +0 -47
- package/src/components/media-form/test/sidebar-datetime-view.test.tsx +0 -96
|
@@ -23,9 +23,17 @@ __export(stencil_math_exports, {
|
|
|
23
23
|
DEFAULT_MIN_CROP_SIZE: () => DEFAULT_MIN_CROP_SIZE,
|
|
24
24
|
computeFreeResizeRect: () => computeFreeResizeRect,
|
|
25
25
|
computeLockedResizeRect: () => computeLockedResizeRect,
|
|
26
|
-
computeShiftLockedResizeRect: () => computeShiftLockedResizeRect
|
|
26
|
+
computeShiftLockedResizeRect: () => computeShiftLockedResizeRect,
|
|
27
|
+
getMinCropPixels: () => getMinCropPixels
|
|
27
28
|
});
|
|
28
29
|
module.exports = __toCommonJS(stencil_math_exports);
|
|
30
|
+
var import_constants = require("./constants.cjs");
|
|
31
|
+
function getMinCropPixels(displayScale) {
|
|
32
|
+
if (displayScale <= 0) {
|
|
33
|
+
return import_constants.MIN_CROP_PIXELS;
|
|
34
|
+
}
|
|
35
|
+
return Math.max(import_constants.MIN_CROP_PIXELS, import_constants.MIN_CROP_SCREEN_PX / displayScale);
|
|
36
|
+
}
|
|
29
37
|
var DEFAULT_MIN_CROP_SIZE = { width: 0.05, height: 0.05 };
|
|
30
38
|
function computeFreeResizeRect(drag, clientX, clientY, imageSize, bounds, minCropSize = DEFAULT_MIN_CROP_SIZE) {
|
|
31
39
|
const dx = imageSize.width > 0 ? (clientX - drag.startX) / imageSize.width : 0;
|
|
@@ -67,7 +75,7 @@ function computeFreeResizeRect(drag, clientX, clientY, imageSize, bounds, minCro
|
|
|
67
75
|
height: edgeBottom - edgeTop
|
|
68
76
|
};
|
|
69
77
|
}
|
|
70
|
-
function computeLockedResizeRect(drag, clientX, clientY, imageSize, bounds, normalizedRatio, minCropSize = DEFAULT_MIN_CROP_SIZE) {
|
|
78
|
+
function computeLockedResizeRect(drag, clientX, clientY, imageSize, bounds, normalizedRatio, minCropSize = DEFAULT_MIN_CROP_SIZE, driverAxis) {
|
|
71
79
|
if (normalizedRatio <= 0 || imageSize.width <= 0 || imageSize.height <= 0) {
|
|
72
80
|
return { ...drag.startRect };
|
|
73
81
|
}
|
|
@@ -90,10 +98,9 @@ function computeLockedResizeRect(drag, clientX, clientY, imageSize, bounds, norm
|
|
|
90
98
|
const minDistH = minDistW / normalizedRatio;
|
|
91
99
|
distW = Math.max(distW, minDistW);
|
|
92
100
|
distH = Math.max(distH, minDistH);
|
|
93
|
-
const pixelDistW = distW * imageSize.width;
|
|
94
|
-
const pixelDistH = distH * imageSize.height;
|
|
95
101
|
const pixelRatio = normalizedRatio * imageSize.width / imageSize.height;
|
|
96
|
-
|
|
102
|
+
const isWidthDriver = driverAxis === "width" || !driverAxis && distW * imageSize.width / (distH * imageSize.height) > pixelRatio;
|
|
103
|
+
if (isWidthDriver) {
|
|
97
104
|
distH = distW / normalizedRatio;
|
|
98
105
|
} else {
|
|
99
106
|
distW = distH * normalizedRatio;
|
|
@@ -203,6 +210,7 @@ function computeShiftLockedResizeRect(drag, clientX, clientY, imageSize, bounds,
|
|
|
203
210
|
DEFAULT_MIN_CROP_SIZE,
|
|
204
211
|
computeFreeResizeRect,
|
|
205
212
|
computeLockedResizeRect,
|
|
206
|
-
computeShiftLockedResizeRect
|
|
213
|
+
computeShiftLockedResizeRect,
|
|
214
|
+
getMinCropPixels
|
|
207
215
|
});
|
|
208
216
|
//# sourceMappingURL=stencil-math.cjs.map
|
|
@@ -1,7 +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 type { HandlePosition, NormalizedRect, Size } from './types';\n\nexport type { HandlePosition };\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 * @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): 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\" \u2014 whichever the user moved more\n\t// (in pixel space) determines the size, the other follows. The\n\t// `normalizedRatio` is w/h in normalized space; the equivalent\n\t// pixel-space ratio is `normalizedRatio * imageW / imageH`. We\n\t// compare the pixel motion ratio against that pixel-space ratio\n\t// so the units line up (was a unit mismatch on non-square images).\n\tconst pixelDistW = distW * imageSize.width;\n\tconst pixelDistH = distH * imageSize.height;\n\tconst pixelRatio = ( normalizedRatio * imageSize.width ) / imageSize.height;\n\tif ( pixelDistW / pixelDistH > pixelRatio ) {\n\t\t// Width is the driver \u2014 compute height from ratio.\n\t\tdistH = distW / normalizedRatio;\n\t} else {\n\t\t// Height is the driver \u2014 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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
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": "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",
|
|
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": ["/**\n * Internal dependencies\n */\nimport type { CropperState, Size } from './types';\nimport { degreesToRadians } from './math/rotation';\nimport { isValidSize, sanitizeCropperState } from './math/sanitize';\n\n/** Identity CSS transform
|
|
4
|
+
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport 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
5
|
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,sBAAiC;AACjC,sBAAkD;AAGlD,IAAM,wBAAwB;AAmBvB,SAAS,sBACf,OACA,WACS;AAKT,MAAK,KAAE,6BAAa,SAAU,GAAI;AACjC,WAAO;AAAA,EACR;AAIA,QAAM,gBAAY,sCAAsB,KAAM;AAC9C,QAAM,aAAa,UAAU,IAAI,IAAI,UAAU;AAC/C,QAAM,aAAa,UAAU,IAAI,IAAI,UAAU;AAC/C,QAAM,UAAM,kCAAkB,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/types.ts"],
|
|
4
|
-
"sourcesContent": ["import type { mat2d } from 'gl-matrix';\n\n/**\n * A 2D camera represented as a 2
|
|
4
|
+
"sourcesContent": ["import type { mat2d } from 'gl-matrix';\n\n/**\n * A 2D camera represented as a 2×3 affine transformation matrix (`mat2d`).\n *\n * This is not a camera object with properties — it's a raw 6-element matrix\n * `[a, b, c, d, tx, ty]` that maps normalized world coordinates [0,1] to\n * screen pixels. It composes pan, rotation, flip, zoom, and contain-fit\n * into a single transform.\n *\n * Use `createCamera()` to build one from `CropperState`, then pass it to\n * `worldToScreen()` / `screenToWorld()` for coordinate conversion.\n *\n * A 3D camera would use `mat4`; this is strictly 2D (no perspective).\n */\nexport type Camera = mat2d;\n\n/**\n * A point with normalized coordinates (0-1 range relative to image dimensions).\n */\nexport interface NormalizedPoint {\n\tx: number;\n\ty: number;\n}\n\n/**\n * A rectangle with normalized coordinates (0-1 range).\n * Origin is the top-left corner of the image.\n */\nexport interface NormalizedRect {\n\tx: number;\n\ty: number;\n\twidth: number;\n\theight: number;\n}\n\n/**\n * Width and height dimensions.\n */\nexport interface Size {\n\twidth: number;\n\theight: number;\n}\n\n/**\n * Flip state for horizontal and vertical axes.\n */\nexport interface Flip {\n\thorizontal: boolean;\n\tvertical: boolean;\n}\n\n/**\n * A JSON-serializable transform operation for the non-destructive pipeline.\n */\nexport type TransformOperation =\n\t| { type: 'crop'; rect: NormalizedRect }\n\t| { type: 'rotate'; degrees: number }\n\t| { type: 'flip'; direction: 'horizontal' | 'vertical' }\n\t| { type: 'zoom'; factor: number };\n\n/**\n * Full cropper state.\n */\nexport interface CropperState {\n\t/** The source image information. Null until an image is loaded. */\n\timage: {\n\t\tsrc: string;\n\t\tnaturalWidth: number;\n\t\tnaturalHeight: number;\n\t} | null;\n\t/**\n\t * Image pan offset in normalized coordinates. Not the crop\n\t * rectangle — that's `cropRect` below.\n\t */\n\tpan: NormalizedPoint;\n\t/** Zoom level. 1 = no zoom. */\n\tzoom: number;\n\t/** Rotation in degrees, normalized to 0-360. */\n\trotation: number;\n\t/**\n\t * Base pan/zoom/rotation — the pose the user last explicitly set.\n\t *\n\t * During continuous fine rotation (SET_ROTATION), every tick\n\t * re-derives the transient `pan`/`zoom` from these base values\n\t * rather than compounding on the previous tick. This prevents\n\t * drift from accumulated containment clamping near image edges:\n\t * rotating 0°→30°→0° returns to the exact starting pan, not a\n\t * slightly-nudged version of it.\n\t *\n\t * These are updated by every action except SET_ROTATION itself —\n\t * explicit zoom, pan, crop-rect, flip, and 90° snap all commit\n\t * the post-containment state into base. SET_ROTATION reads from\n\t * `basePan` and `baseRotation` to compute the new pan, and reads\n\t * `baseZoom` as the zoom floor (enforceContainment may bump it up\n\t * to cover the rotated crop, but never leaves it above baseZoom).\n\t */\n\tbasePan: NormalizedPoint;\n\tbaseZoom: number;\n\tbaseRotation: number;\n\t/** Flip state. */\n\tflip: Flip;\n\t/** The crop rectangle in normalized coordinates. */\n\tcropRect: NormalizedRect;\n}\n\n/**\n * Internal reducer action union.\n *\n * This type is intentionally not exported from the image-editor public barrel.\n * Consumers should drive state through the controller returned by\n * `useCropperReducer()` (or a composite store) or through serializable\n * `TransformOperation` values.\n */\nexport type CropperAction =\n\t/** Sets the loaded image metadata (natural size, src). */\n\t| { type: 'SET_IMAGE'; payload: CropperState[ 'image' ] }\n\t/** Sets the image pan offset. (Crop rectangle is SET_CROP_RECT.) */\n\t| { type: 'SET_PAN'; payload: NormalizedPoint }\n\t/** Sets the zoom level, clamped to [coverage-aware min, MAX_ZOOM]. */\n\t| { type: 'SET_ZOOM'; payload: number }\n\t/**\n\t * Sets zoom and pan together atomically. Used by focal-point\n\t * zoom (wheel, pinch) to keep a target point stationary while\n\t * zoom changes.\n\t */\n\t| {\n\t\t\ttype: 'SET_ZOOM_AT_POINT';\n\t\t\tpayload: { zoom: number; pan: NormalizedPoint };\n\t }\n\t/** Sets the absolute rotation angle in degrees. */\n\t| { type: 'SET_ROTATION'; payload: number }\n\t/** Rotates by +/-90 degrees (snap). */\n\t| { type: 'SNAP_ROTATE_90'; payload: { direction: 1 | -1 } }\n\t/** Sets the flip state. */\n\t| { type: 'SET_FLIP'; payload: Flip }\n\t/** Sets the crop rectangle. */\n\t| { type: 'SET_CROP_RECT'; payload: NormalizedRect }\n\t/** Settle animation after resize drag, recentering the crop rect. */\n\t| { type: 'SETTLE_CROP' }\n\t/** Applies a single pipeline transform via the reducer. */\n\t| { type: 'APPLY_OPERATION'; payload: TransformOperation }\n\t/** Resets to DEFAULT_STATE, optionally merging a partial override. */\n\t| { type: 'RESET'; payload?: Partial< CropperState > };\n\n/**\n * Viewport camera state — display-only, does not affect export or undo.\n *\n * Applied as a CSS transform on the inner stage div so the user can scroll\n * the view independently of the crop. zoom > 1 magnifies; pan shifts in CSS px.\n */\nexport interface ViewportState {\n\tzoom: number;\n\tpan: { x: number; y: number };\n}\n\nexport type ViewportAction =\n\t| { type: 'SET_VIEWPORT_ZOOM'; payload: number }\n\t| { type: 'SET_VIEWPORT_PAN'; payload: { x: number; y: number } }\n\t| { type: 'RESET_VIEWPORT' };\n\n/**\n * Direction of a crop resize handle. Eight compass positions: four\n * corners and four edge midpoints. Used by stencil callbacks and any\n * overlay that needs to know which handle the user is dragging.\n */\nexport type HandlePosition = 'n' | 's' | 'e' | 'w' | 'nw' | 'ne' | 'sw' | 'se';\n\n/**\n * The contract for a pluggable stencil component.\n * Stencils render the crop area overlay and handle resize interactions.\n */\nexport interface StencilProps {\n\t/** The current crop rectangle in normalized coordinates. */\n\tcropRect: NormalizedRect;\n\t/** The container element dimensions in pixels. */\n\tcontainerSize: Size;\n\t/** The rendered image dimensions in pixels within the container. */\n\timageSize: Size;\n\t/** Callback when the crop rectangle changes (during drag). */\n\tonCropChange: ( rect: NormalizedRect ) => void;\n\t/**\n\t * Callback when a resize interaction starts — pointerdown on a handle\n\t * or the first arrow-key press on a focused handle. The handle direction\n\t * is passed so downstream overlays can anchor to it. The argument is\n\t * optional to keep custom stencils that don't track a direction\n\t * backward-compatible.\n\t */\n\tonResizeStart?: ( handle?: HandlePosition ) => void;\n\t/** Callback when a resize drag ends (mouseup after handle drag). */\n\tonResizeEnd?: () => void;\n\t/** Optional fixed aspect ratio (width / height) in pixel space. */\n\taspectRatio?: number;\n\t/** Whether the crop handles are shown for freeform resizing. */\n\tfreeformCrop?: boolean;\n\t/** Whether resize handles should ignore pointer and keyboard input. */\n\tisResizeDisabled?: boolean;\n\t/** CSS transition string for settle animation. */\n\tstencilTransition?: string;\n\t/** Maximum crop rect bounds based on current zoom/rotation. */\n\tcropBounds?: {\n\t\tminX: number;\n\t\tminY: number;\n\t\tmaxX: number;\n\t\tmaxY: number;\n\t};\n\t/** Called when Escape is pressed on a resize handle. */\n\tonEscape?: () => void;\n\t/**\n\t * Minimum crop rect dimension in normalized space, per axis. Derived\n\t * by the host from a pixel-based floor (see `MIN_CROP_PIXELS`) and\n\t * the source image dimensions. Omit to use the stencil's default.\n\t */\n\tminCropSize?: Size;\n\t/**\n\t * Optional post-processor for freeform resize output. Hosts use this for\n\t * display-gated behavior such as snapping visible pixel grids to source\n\t * pixel boundaries.\n\t */\n\tsnapCropRect?: (\n\t\trect: NormalizedRect,\n\t\thandle: HandlePosition\n\t) => NormalizedRect;\n\t/**\n\t * Optional keyboard resize step in normalized space, per axis. Hosts use\n\t * this when keyboard resize should follow the rendered image scale instead\n\t * of the default percentage-based step.\n\t */\n\tkeyboardResizeStep?: Size;\n}\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -40,6 +40,7 @@ var import_camera = require("../../core/camera.cjs");
|
|
|
40
40
|
var import_containment = require("../../core/containment.cjs");
|
|
41
41
|
var import_constants = require("../../core/constants.cjs");
|
|
42
42
|
var import_crop_rect = require("../../core/crop-rect.cjs");
|
|
43
|
+
var import_stencil_math = require("../../core/stencil-math.cjs");
|
|
43
44
|
var import_use_interaction = require("../hooks/use-interaction.cjs");
|
|
44
45
|
var import_use_transform_style = require("../hooks/use-transform-style.cjs");
|
|
45
46
|
var import_use_aria_announcer = require("../hooks/use-aria-announcer.cjs");
|
|
@@ -52,6 +53,16 @@ var import_viewport_provider = require("./viewport-provider.cjs");
|
|
|
52
53
|
var import_visually_hidden_style = require("../visually-hidden-style.cjs");
|
|
53
54
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
54
55
|
var CROP_RECT_EPSILON = 1e-6;
|
|
56
|
+
var CARDINAL_ROTATION_EPSILON = 1e-6;
|
|
57
|
+
var SETTLE_TRANSITION_DURATION_MS = 200;
|
|
58
|
+
var SETTLE_TRANSITION_FALLBACK_MS = SETTLE_TRANSITION_DURATION_MS + 100;
|
|
59
|
+
var SETTLE_TRANSFORM_TRANSITION = `transform ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`;
|
|
60
|
+
var SETTLE_STENCIL_TRANSITION = [
|
|
61
|
+
`left ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`,
|
|
62
|
+
`top ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`,
|
|
63
|
+
`width ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`,
|
|
64
|
+
`height ${SETTLE_TRANSITION_DURATION_MS}ms ease-out`
|
|
65
|
+
].join(", ");
|
|
55
66
|
function CropperInner({
|
|
56
67
|
src,
|
|
57
68
|
controller,
|
|
@@ -157,24 +168,6 @@ function CropperInner({
|
|
|
157
168
|
),
|
|
158
169
|
[canvasSize, naturalWidth, naturalHeight, state.rotation]
|
|
159
170
|
);
|
|
160
|
-
const minCropSize = (0, import_element.useMemo)(() => {
|
|
161
|
-
if (naturalWidth <= 0 || naturalHeight <= 0) {
|
|
162
|
-
return void 0;
|
|
163
|
-
}
|
|
164
|
-
const snapRotation = Math.round(state.rotation / 90) * 90;
|
|
165
|
-
const bbox = (0, import_camera.getRotatedBBox)(
|
|
166
|
-
naturalWidth,
|
|
167
|
-
naturalHeight,
|
|
168
|
-
snapRotation
|
|
169
|
-
);
|
|
170
|
-
return {
|
|
171
|
-
width: Math.min(1, import_constants.MIN_CROP_PIXELS * state.zoom / bbox.width),
|
|
172
|
-
height: Math.min(
|
|
173
|
-
1,
|
|
174
|
-
import_constants.MIN_CROP_PIXELS * state.zoom / bbox.height
|
|
175
|
-
)
|
|
176
|
-
};
|
|
177
|
-
}, [naturalWidth, naturalHeight, state.rotation, state.zoom]);
|
|
178
171
|
(0, import_element.useEffect)(() => {
|
|
179
172
|
setVisualSize(visualSize);
|
|
180
173
|
}, [visualSize, setVisualSize]);
|
|
@@ -227,16 +220,133 @@ function CropperInner({
|
|
|
227
220
|
const [isResizing, setIsResizing] = (0, import_element.useState)(false);
|
|
228
221
|
const isResizingRef = (0, import_element.useRef)(false);
|
|
229
222
|
const isSettlingRef = (0, import_element.useRef)(false);
|
|
223
|
+
const [isTouchPinching, setIsTouchPinchingState] = (0, import_element.useState)(false);
|
|
224
|
+
const isTouchPinchingRef = (0, import_element.useRef)(false);
|
|
225
|
+
const setTouchPinching = (0, import_element.useCallback)((isPinching) => {
|
|
226
|
+
isTouchPinchingRef.current = isPinching;
|
|
227
|
+
setIsTouchPinchingState(isPinching);
|
|
228
|
+
}, []);
|
|
230
229
|
const [activeHandle, setActiveHandle] = (0, import_element.useState)(
|
|
231
230
|
null
|
|
232
231
|
);
|
|
232
|
+
const viewScaleRest = (0, import_element.useMemo)(
|
|
233
|
+
() => (0, import_camera.getViewScale)(
|
|
234
|
+
state.cropRect,
|
|
235
|
+
canvasSize,
|
|
236
|
+
visualSize,
|
|
237
|
+
import_constants.SETTLE_TARGET_CANVAS_FILL,
|
|
238
|
+
import_constants.MAX_VIEW_SCALE
|
|
239
|
+
),
|
|
240
|
+
[state.cropRect, canvasSize, visualSize]
|
|
241
|
+
);
|
|
242
|
+
const [frozenViewScale, setFrozenViewScale] = (0, import_element.useState)(1);
|
|
243
|
+
const viewScale = isResizing ? frozenViewScale : viewScaleRest;
|
|
244
|
+
const scaledVisualSize = (0, import_element.useMemo)(
|
|
245
|
+
() => ({
|
|
246
|
+
width: visualSize.width * viewScale,
|
|
247
|
+
height: visualSize.height * viewScale
|
|
248
|
+
}),
|
|
249
|
+
[visualSize.width, visualSize.height, viewScale]
|
|
250
|
+
);
|
|
251
|
+
const displayScale = naturalWidth > 0 ? elementSize.width / naturalWidth * state.zoom * viewScale : 0;
|
|
252
|
+
const keyboardResizeStep = (0, import_element.useMemo)(() => {
|
|
253
|
+
if (displayScale < import_constants.PIXEL_SNAP_DISPLAY_SCALE || aspectRatio && aspectRatio > 0 || naturalWidth <= 0 || naturalHeight <= 0) {
|
|
254
|
+
return void 0;
|
|
255
|
+
}
|
|
256
|
+
const snapRotation = Math.round(state.rotation / 90) * 90;
|
|
257
|
+
if (Math.abs(state.rotation - snapRotation) >= CARDINAL_ROTATION_EPSILON) {
|
|
258
|
+
return void 0;
|
|
259
|
+
}
|
|
260
|
+
const bbox = (0, import_camera.getRotatedBBox)(
|
|
261
|
+
naturalWidth,
|
|
262
|
+
naturalHeight,
|
|
263
|
+
snapRotation
|
|
264
|
+
);
|
|
265
|
+
if (bbox.width <= 0 || bbox.height <= 0) {
|
|
266
|
+
return void 0;
|
|
267
|
+
}
|
|
268
|
+
return {
|
|
269
|
+
width: state.zoom / bbox.width,
|
|
270
|
+
height: state.zoom / bbox.height
|
|
271
|
+
};
|
|
272
|
+
}, [
|
|
273
|
+
displayScale,
|
|
274
|
+
aspectRatio,
|
|
275
|
+
naturalWidth,
|
|
276
|
+
naturalHeight,
|
|
277
|
+
state.rotation,
|
|
278
|
+
state.zoom
|
|
279
|
+
]);
|
|
280
|
+
const minCropSize = (0, import_element.useMemo)(() => {
|
|
281
|
+
if (naturalWidth <= 0 || naturalHeight <= 0) {
|
|
282
|
+
return void 0;
|
|
283
|
+
}
|
|
284
|
+
const snapRotation = Math.round(state.rotation / 90) * 90;
|
|
285
|
+
const bbox = (0, import_camera.getRotatedBBox)(
|
|
286
|
+
naturalWidth,
|
|
287
|
+
naturalHeight,
|
|
288
|
+
snapRotation
|
|
289
|
+
);
|
|
290
|
+
const minPixels = (0, import_stencil_math.getMinCropPixels)(displayScale);
|
|
291
|
+
return {
|
|
292
|
+
width: Math.min(1, minPixels * state.zoom / bbox.width),
|
|
293
|
+
height: Math.min(1, minPixels * state.zoom / bbox.height)
|
|
294
|
+
};
|
|
295
|
+
}, [
|
|
296
|
+
naturalWidth,
|
|
297
|
+
naturalHeight,
|
|
298
|
+
state.rotation,
|
|
299
|
+
state.zoom,
|
|
300
|
+
displayScale
|
|
301
|
+
]);
|
|
302
|
+
const snapCropRect = (0, import_element.useCallback)(
|
|
303
|
+
(rect, handle) => {
|
|
304
|
+
if (displayScale < import_constants.PIXEL_SNAP_DISPLAY_SCALE || naturalWidth <= 0 || naturalHeight <= 0) {
|
|
305
|
+
return rect;
|
|
306
|
+
}
|
|
307
|
+
return (0, import_source_region.snapCropRectToSourcePixels)(
|
|
308
|
+
state,
|
|
309
|
+
{ width: naturalWidth, height: naturalHeight },
|
|
310
|
+
rect,
|
|
311
|
+
handle
|
|
312
|
+
);
|
|
313
|
+
},
|
|
314
|
+
[displayScale, naturalWidth, naturalHeight, state]
|
|
315
|
+
);
|
|
316
|
+
const wasPixelSnapEnabledRef = (0, import_element.useRef)(false);
|
|
317
|
+
(0, import_element.useEffect)(() => {
|
|
318
|
+
const isPixelSnapEnabled = freeformCrop && (!aspectRatio || aspectRatio <= 0) && displayScale >= import_constants.PIXEL_SNAP_DISPLAY_SCALE && naturalWidth > 0 && naturalHeight > 0;
|
|
319
|
+
const wasPixelSnapEnabled = wasPixelSnapEnabledRef.current;
|
|
320
|
+
wasPixelSnapEnabledRef.current = isPixelSnapEnabled;
|
|
321
|
+
if (!isPixelSnapEnabled || wasPixelSnapEnabled) {
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
const snappedCropRect = (0, import_source_region.snapCropRectToSourcePixelGrid)(
|
|
325
|
+
state,
|
|
326
|
+
{ width: naturalWidth, height: naturalHeight },
|
|
327
|
+
state.cropRect
|
|
328
|
+
);
|
|
329
|
+
const currentCropRect = state.cropRect;
|
|
330
|
+
if (Math.abs(currentCropRect.x - snappedCropRect.x) < CROP_RECT_EPSILON && Math.abs(currentCropRect.y - snappedCropRect.y) < CROP_RECT_EPSILON && Math.abs(currentCropRect.width - snappedCropRect.width) < CROP_RECT_EPSILON && Math.abs(currentCropRect.height - snappedCropRect.height) < CROP_RECT_EPSILON) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
setCropRect(snappedCropRect);
|
|
334
|
+
}, [
|
|
335
|
+
aspectRatio,
|
|
336
|
+
displayScale,
|
|
337
|
+
freeformCrop,
|
|
338
|
+
naturalWidth,
|
|
339
|
+
naturalHeight,
|
|
340
|
+
setCropRect,
|
|
341
|
+
state
|
|
342
|
+
]);
|
|
233
343
|
const {
|
|
234
344
|
handlers,
|
|
235
345
|
onWheelNative,
|
|
236
346
|
isDragging,
|
|
237
347
|
isZooming,
|
|
238
348
|
isPlacementActive: isInteractionPlacementActive
|
|
239
|
-
} = (0, import_use_interaction.useInteraction)(state, controller, canvasSize,
|
|
349
|
+
} = (0, import_use_interaction.useInteraction)(state, controller, canvasSize, scaledVisualSize, {
|
|
240
350
|
minZoom: effectiveMinZoom,
|
|
241
351
|
maxZoom,
|
|
242
352
|
onGestureStart,
|
|
@@ -247,12 +357,36 @@ function CropperInner({
|
|
|
247
357
|
onPointerDownCapture: () => {
|
|
248
358
|
setIsFocusVisible(false);
|
|
249
359
|
},
|
|
360
|
+
onTouchStartCapture: (event) => {
|
|
361
|
+
if (event.touches.length > 1) {
|
|
362
|
+
setTouchPinching(true);
|
|
363
|
+
}
|
|
364
|
+
},
|
|
365
|
+
onTouchMoveCapture: (event) => {
|
|
366
|
+
if (event.touches.length > 1) {
|
|
367
|
+
setTouchPinching(true);
|
|
368
|
+
}
|
|
369
|
+
},
|
|
370
|
+
onTouchEndCapture: (event) => {
|
|
371
|
+
if (event.touches.length === 0) {
|
|
372
|
+
setTouchPinching(false);
|
|
373
|
+
}
|
|
374
|
+
},
|
|
375
|
+
onTouchCancelCapture: (event) => {
|
|
376
|
+
if (event.touches.length === 0) {
|
|
377
|
+
setTouchPinching(false);
|
|
378
|
+
}
|
|
379
|
+
},
|
|
250
380
|
onKeyDownCapture: (event) => {
|
|
251
381
|
if (!["Shift", "Control", "Alt", "Meta"].includes(event.key)) {
|
|
252
382
|
setIsFocusVisible(true);
|
|
253
383
|
}
|
|
254
384
|
},
|
|
255
385
|
onPointerDown: (event) => {
|
|
386
|
+
if (isResizingRef.current || isTouchPinchingRef.current || event.pointerType === "touch" && event.isPrimary === false) {
|
|
387
|
+
event.preventDefault();
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
256
390
|
handlers.onPointerDown?.(event);
|
|
257
391
|
setIsFocusVisible(false);
|
|
258
392
|
}
|
|
@@ -295,25 +429,28 @@ function CropperInner({
|
|
|
295
429
|
);
|
|
296
430
|
const handleCropChange = (0, import_element.useCallback)(
|
|
297
431
|
(rect) => {
|
|
432
|
+
if (isTouchPinchingRef.current) {
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
298
435
|
setCropRect(rect);
|
|
299
|
-
if (isResizingRef.current &&
|
|
300
|
-
const offsetX = (canvasSize.width -
|
|
301
|
-
const offsetY = (canvasSize.height -
|
|
436
|
+
if (isResizingRef.current && scaledVisualSize.width > 0 && scaledVisualSize.height > 0) {
|
|
437
|
+
const offsetX = (canvasSize.width - scaledVisualSize.width) / 2;
|
|
438
|
+
const offsetY = (canvasSize.height - scaledVisualSize.height) / 2;
|
|
302
439
|
const rightOverflow = Math.max(
|
|
303
440
|
0,
|
|
304
|
-
offsetX + (rect.x + rect.width) *
|
|
441
|
+
offsetX + (rect.x + rect.width) * scaledVisualSize.width - canvasSize.width
|
|
305
442
|
);
|
|
306
443
|
const leftOverflow = Math.max(
|
|
307
444
|
0,
|
|
308
|
-
-(offsetX + rect.x *
|
|
445
|
+
-(offsetX + rect.x * scaledVisualSize.width)
|
|
309
446
|
);
|
|
310
447
|
const bottomOverflow = Math.max(
|
|
311
448
|
0,
|
|
312
|
-
offsetY + (rect.y + rect.height) *
|
|
449
|
+
offsetY + (rect.y + rect.height) * scaledVisualSize.height - canvasSize.height
|
|
313
450
|
);
|
|
314
451
|
const topOverflow = Math.max(
|
|
315
452
|
0,
|
|
316
|
-
-(offsetY + rect.y *
|
|
453
|
+
-(offsetY + rect.y * scaledVisualSize.height)
|
|
317
454
|
);
|
|
318
455
|
setViewportPan({
|
|
319
456
|
x: -rightOverflow + leftOverflow,
|
|
@@ -321,10 +458,15 @@ function CropperInner({
|
|
|
321
458
|
});
|
|
322
459
|
}
|
|
323
460
|
},
|
|
324
|
-
[setCropRect, setViewportPan, canvasSize,
|
|
461
|
+
[setCropRect, setViewportPan, canvasSize, scaledVisualSize]
|
|
325
462
|
);
|
|
326
463
|
const [settling, setSettling] = (0, import_element.useState)(false);
|
|
327
464
|
const settleTimerRef = (0, import_element.useRef)();
|
|
465
|
+
const finishSettling = (0, import_element.useCallback)(() => {
|
|
466
|
+
clearTimeout(settleTimerRef.current);
|
|
467
|
+
isSettlingRef.current = false;
|
|
468
|
+
setSettling(false);
|
|
469
|
+
}, []);
|
|
328
470
|
(0, import_element.useEffect)(() => {
|
|
329
471
|
return () => {
|
|
330
472
|
clearTimeout(settleTimerRef.current);
|
|
@@ -348,39 +490,56 @@ function CropperInner({
|
|
|
348
490
|
}, []);
|
|
349
491
|
const handleResizeStart = (0, import_element.useCallback)(
|
|
350
492
|
(handle) => {
|
|
493
|
+
if (isTouchPinchingRef.current) {
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
setFrozenViewScale(viewScaleRest);
|
|
351
497
|
isResizingRef.current = true;
|
|
352
498
|
setIsResizing(true);
|
|
353
499
|
setActiveHandle(handle ?? null);
|
|
354
|
-
|
|
355
|
-
isSettlingRef.current = false;
|
|
356
|
-
setSettling(false);
|
|
500
|
+
finishSettling();
|
|
357
501
|
resetViewport();
|
|
358
502
|
onGestureStart?.();
|
|
359
503
|
},
|
|
360
|
-
[onGestureStart, resetViewport]
|
|
504
|
+
[finishSettling, onGestureStart, resetViewport, viewScaleRest]
|
|
361
505
|
);
|
|
362
506
|
const handleResizeEnd = (0, import_element.useCallback)(() => {
|
|
507
|
+
const isCancellingForPinch = isTouchPinchingRef.current;
|
|
363
508
|
isResizingRef.current = false;
|
|
364
509
|
setIsResizing(false);
|
|
365
510
|
setActiveHandle(null);
|
|
511
|
+
clearTimeout(settleTimerRef.current);
|
|
512
|
+
if (isCancellingForPinch) {
|
|
513
|
+
finishSettling();
|
|
514
|
+
resetViewport();
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
366
517
|
isSettlingRef.current = true;
|
|
367
518
|
setSettling(true);
|
|
368
519
|
resetViewport();
|
|
369
520
|
settleCrop();
|
|
370
521
|
onGestureEnd?.();
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
522
|
+
settleTimerRef.current = setTimeout(
|
|
523
|
+
finishSettling,
|
|
524
|
+
SETTLE_TRANSITION_FALLBACK_MS
|
|
525
|
+
);
|
|
526
|
+
}, [finishSettling, settleCrop, onGestureEnd, resetViewport]);
|
|
527
|
+
const handleSettleTransitionEnd = (0, import_element.useCallback)(
|
|
528
|
+
(event) => {
|
|
529
|
+
if (!isSettlingRef.current || event.propertyName !== "transform") {
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
finishSettling();
|
|
533
|
+
},
|
|
534
|
+
[finishSettling]
|
|
535
|
+
);
|
|
377
536
|
let imageTransition;
|
|
378
537
|
if (settling) {
|
|
379
|
-
imageTransition =
|
|
538
|
+
imageTransition = SETTLE_TRANSFORM_TRANSITION;
|
|
380
539
|
} else if (isZooming) {
|
|
381
540
|
imageTransition = "transform 150ms linear";
|
|
382
541
|
}
|
|
383
|
-
const settleStencilTransition = settling ?
|
|
542
|
+
const settleStencilTransition = settling ? SETTLE_STENCIL_TRANSITION : void 0;
|
|
384
543
|
const imageStyle = (0, import_element.useMemo)(() => {
|
|
385
544
|
if (elementSize.width === 0 || elementSize.height === 0) {
|
|
386
545
|
return {};
|
|
@@ -394,11 +553,25 @@ function CropperInner({
|
|
|
394
553
|
maxHeight: elementSize.height,
|
|
395
554
|
left: centerX,
|
|
396
555
|
top: centerY,
|
|
397
|
-
transform
|
|
398
|
-
|
|
556
|
+
// Always lead with `scale()` (identity at rest) so the transform's
|
|
557
|
+
// function list stays structurally constant across rest, resize, and
|
|
558
|
+
// settle. A conditional that dropped `scale()` at viewScale === 1
|
|
559
|
+
// would change the list shape exactly when the settle transition
|
|
560
|
+
// crosses 1:1, forcing the browser to fall back to matrix-decomposition
|
|
561
|
+
// interpolation instead of interpolating each function in turn.
|
|
562
|
+
transform: `scale(${viewScale}) ${transformString}`,
|
|
563
|
+
transition: imageTransition,
|
|
564
|
+
imageRendering: displayScale > 1 ? "pixelated" : void 0
|
|
399
565
|
};
|
|
400
|
-
}, [
|
|
401
|
-
|
|
566
|
+
}, [
|
|
567
|
+
canvasSize,
|
|
568
|
+
elementSize,
|
|
569
|
+
transformString,
|
|
570
|
+
imageTransition,
|
|
571
|
+
displayScale,
|
|
572
|
+
viewScale
|
|
573
|
+
]);
|
|
574
|
+
const settleTransition = settling ? SETTLE_TRANSFORM_TRANSITION : void 0;
|
|
402
575
|
const willChange = isResizing || settling ? "transform" : void 0;
|
|
403
576
|
let stageStyle;
|
|
404
577
|
if (viewportState.pan.x !== 0 || viewportState.pan.y !== 0) {
|
|
@@ -408,7 +581,11 @@ function CropperInner({
|
|
|
408
581
|
willChange
|
|
409
582
|
};
|
|
410
583
|
} else if (settling) {
|
|
411
|
-
stageStyle = {
|
|
584
|
+
stageStyle = {
|
|
585
|
+
transform: "translate(0px, 0px)",
|
|
586
|
+
transition: settleTransition,
|
|
587
|
+
willChange
|
|
588
|
+
};
|
|
412
589
|
}
|
|
413
590
|
const setContainerRef = (0, import_element.useCallback)(
|
|
414
591
|
(element) => {
|
|
@@ -469,11 +646,13 @@ function CropperInner({
|
|
|
469
646
|
className: "wp-media-editor-image-editor__stage",
|
|
470
647
|
"data-testid": "cropper-stage",
|
|
471
648
|
style: stageStyle,
|
|
649
|
+
onTransitionEnd: handleSettleTransitionEnd,
|
|
472
650
|
children: [
|
|
473
651
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
474
652
|
"img",
|
|
475
653
|
{
|
|
476
654
|
className: "wp-media-editor-image-editor__image",
|
|
655
|
+
"data-testid": "cropper-image",
|
|
477
656
|
src,
|
|
478
657
|
alt: "",
|
|
479
658
|
onLoad: handleImageLoad,
|
|
@@ -486,7 +665,7 @@ function CropperInner({
|
|
|
486
665
|
{
|
|
487
666
|
cropRect: state.cropRect,
|
|
488
667
|
containerSize: canvasSize,
|
|
489
|
-
imageSize:
|
|
668
|
+
imageSize: scaledVisualSize,
|
|
490
669
|
transition: settleStencilTransition
|
|
491
670
|
}
|
|
492
671
|
),
|
|
@@ -495,16 +674,19 @@ function CropperInner({
|
|
|
495
674
|
{
|
|
496
675
|
cropRect: state.cropRect,
|
|
497
676
|
containerSize: canvasSize,
|
|
498
|
-
imageSize:
|
|
677
|
+
imageSize: scaledVisualSize,
|
|
499
678
|
onCropChange: handleCropChange,
|
|
500
679
|
onResizeStart: handleResizeStart,
|
|
501
680
|
onResizeEnd: handleResizeEnd,
|
|
502
681
|
onEscape: handleEscape,
|
|
503
682
|
aspectRatio,
|
|
504
683
|
freeformCrop,
|
|
684
|
+
isResizeDisabled: isTouchPinching,
|
|
505
685
|
stencilTransition: settleStencilTransition,
|
|
506
686
|
cropBounds,
|
|
507
|
-
minCropSize
|
|
687
|
+
minCropSize,
|
|
688
|
+
snapCropRect,
|
|
689
|
+
keyboardResizeStep
|
|
508
690
|
}
|
|
509
691
|
),
|
|
510
692
|
(showGrid === true || isInteractiveGrid) && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
@@ -512,7 +694,7 @@ function CropperInner({
|
|
|
512
694
|
{
|
|
513
695
|
cropRect: state.cropRect,
|
|
514
696
|
containerSize: canvasSize,
|
|
515
|
-
imageSize:
|
|
697
|
+
imageSize: scaledVisualSize
|
|
516
698
|
}
|
|
517
699
|
),
|
|
518
700
|
activeHandle && outputSize && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
@@ -520,7 +702,7 @@ function CropperInner({
|
|
|
520
702
|
{
|
|
521
703
|
cropRect: state.cropRect,
|
|
522
704
|
containerSize: canvasSize,
|
|
523
|
-
imageSize:
|
|
705
|
+
imageSize: scaledVisualSize,
|
|
524
706
|
activeHandle,
|
|
525
707
|
outputWidth: outputSize.width,
|
|
526
708
|
outputHeight: outputSize.height
|