Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Inline

Manav Rathi4e0d49b68987
diff --git a/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/CropMenu.tsx b/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/CropMenu.tsx
deleted file mode 100644
index 286b26a522..0000000000
--- a/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/CropMenu.tsx
+++ /dev/null
@@ -1,132 +0,0 @@
-import { MenuItemGroup, MenuSectionTitle } from "@/base/components/Menu";
-import { EnteMenuItem } from "@ente/shared/components/Menu/EnteMenuItem";
-import CropIcon from "@mui/icons-material/Crop";
-import { t } from "i18next";
-import type { MutableRefObject } from "react";
-import { useContext } from "react";
-import { CropBoxProps, ImageEditorOverlayContext } from "./";
-
-interface IProps {
- previewScale: number;
- cropBoxProps: CropBoxProps;
- cropBoxRef: MutableRefObject<HTMLDivElement>;
- resetCropBox: () => void;
-}
-
-export const cropRegionOfCanvas = (
- canvas: HTMLCanvasElement,
- topLeftX: number,
- topLeftY: number,
- bottomRightX: number,
- bottomRightY: number,
- scale: number = 1,
-) => {
- const context = canvas.getContext("2d");
- if (!context || !canvas) return;
- context.imageSmoothingEnabled = false;
-
- const width = (bottomRightX - topLeftX) * scale;
- const height = (bottomRightY - topLeftY) * scale;
-
- const img = new Image();
- img.src = canvas.toDataURL();
- img.onload = () => {
- context.clearRect(0, 0, canvas.width, canvas.height);
-
- canvas.width = width;
- canvas.height = height;
-
- context.drawImage(
- img,
- topLeftX,
- topLeftY,
- width,
- height,
- 0,
- 0,
- width,
- height,
- );
- };
-};
-
-export const getCropRegionArgs = (
- cropBoxEle: HTMLDivElement,
- canvasEle: HTMLCanvasElement,
-) => {
- // get the bounding rectangle of the crop box
- const cropBoxRect = cropBoxEle.getBoundingClientRect();
- // Get the bounding rectangle of the canvas
- const canvasRect = canvasEle.getBoundingClientRect();
-
- // calculate the scale of the canvas display relative to its actual dimensions
- const displayScale = canvasEle.width / canvasRect.width;
-
- // calculate the coordinates of the crop box relative to the canvas and adjust for any scrolling by adding scroll offsets
- const x1 =
- (cropBoxRect.left - canvasRect.left + window.scrollX) * displayScale;
- const y1 =
- (cropBoxRect.top - canvasRect.top + window.scrollY) * displayScale;
- const x2 = x1 + cropBoxRect.width * displayScale;
- const y2 = y1 + cropBoxRect.height * displayScale;
-
- return {
- x1,
- x2,
- y1,
- y2,
- };
-};
-
-const CropMenu = (props: IProps) => {
- const {
- canvasRef,
- originalSizeCanvasRef,
- canvasLoading,
- setCanvasLoading,
- setTransformationPerformed,
- setCurrentTab,
- } = useContext(ImageEditorOverlayContext);
-
- return (
- <>
- <MenuSectionTitle title={t("FREEHAND")} />
- <MenuItemGroup
- style={{
- marginBottom: "0.5rem",
- }}
- >
- <EnteMenuItem
- disabled={canvasLoading}
- startIcon={<CropIcon />}
- onClick={() => {
- if (!props.cropBoxRef.current || !canvasRef.current)
- return;
-
- const { x1, x2, y1, y2 } = getCropRegionArgs(
- props.cropBoxRef.current,
- canvasRef.current,
- );
- setCanvasLoading(true);
- setTransformationPerformed(true);
- cropRegionOfCanvas(canvasRef.current, x1, y1, x2, y2);
- cropRegionOfCanvas(
- originalSizeCanvasRef.current,
- x1 / props.previewScale,
- y1 / props.previewScale,
- x2 / props.previewScale,
- y2 / props.previewScale,
- );
- props.resetCropBox();
- setCanvasLoading(false);
-
- setCurrentTab("transform");
- }}
- label={t("APPLY_CROP")}
- />
- </MenuItemGroup>
- </>
- );
-};
-
-export default CropMenu;
diff --git a/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/FreehandCropRegion.tsx b/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/FreehandCropRegion.tsx
deleted file mode 100644
index e69de29bb2..0000000000
diff --git a/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx b/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx
index c80c7bf8c9..7efa5fa75f 100644
--- a/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx
+++ b/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx
@@ -47,7 +47,6 @@ import {
import { getLocalCollections } from "services/collectionService";
import uploadManager from "services/upload/uploadManager";
import ColoursMenu from "./ColoursMenu";
-import CropMenu, { cropRegionOfCanvas, getCropRegionArgs } from "./CropMenu";
import TransformMenu from "./TransformMenu";
interface IProps {
@@ -730,6 +729,129 @@ const ImageEditorOverlay = (props: IProps) => {
export default ImageEditorOverlay;
+interface CropMenuProps {
+ previewScale: number;
+ cropBoxProps: CropBoxProps;
+ cropBoxRef: MutableRefObject<HTMLDivElement>;
+ resetCropBox: () => void;
+}
+
+const CropMenu: React.FC<CropMenuProps> = (props) => {
+ const {
+ canvasRef,
+ originalSizeCanvasRef,
+ canvasLoading,
+ setCanvasLoading,
+ setTransformationPerformed,
+ setCurrentTab,
+ } = useContext(ImageEditorOverlayContext);
+
+ return (
+ <>
+ <MenuSectionTitle title={t("FREEHAND")} />
+ <MenuItemGroup
+ style={{
+ marginBottom: "0.5rem",
+ }}
+ >
+ <EnteMenuItem
+ disabled={canvasLoading}
+ startIcon={<CropIcon />}
+ onClick={() => {
+ if (!props.cropBoxRef.current || !canvasRef.current)
+ return;
+
+ const { x1, x2, y1, y2 } = getCropRegionArgs(
+ props.cropBoxRef.current,
+ canvasRef.current,
+ );
+ setCanvasLoading(true);
+ setTransformationPerformed(true);
+ cropRegionOfCanvas(canvasRef.current, x1, y1, x2, y2);
+ cropRegionOfCanvas(
+ originalSizeCanvasRef.current,
+ x1 / props.previewScale,
+ y1 / props.previewScale,
+ x2 / props.previewScale,
+ y2 / props.previewScale,
+ );
+ props.resetCropBox();
+ setCanvasLoading(false);
+
+ setCurrentTab("transform");
+ }}
+ label={t("APPLY_CROP")}
+ />
+ </MenuItemGroup>
+ </>
+ );
+};
+
+const cropRegionOfCanvas = (
+ canvas: HTMLCanvasElement,
+ topLeftX: number,
+ topLeftY: number,
+ bottomRightX: number,
+ bottomRightY: number,
+ scale: number = 1,
+) => {
+ const context = canvas.getContext("2d");
+ if (!context || !canvas) return;
+ context.imageSmoothingEnabled = false;
+
+ const width = (bottomRightX - topLeftX) * scale;
+ const height = (bottomRightY - topLeftY) * scale;
+
+ const img = new Image();
+ img.src = canvas.toDataURL();
+ img.onload = () => {
+ context.clearRect(0, 0, canvas.width, canvas.height);
+
+ canvas.width = width;
+ canvas.height = height;
+
+ context.drawImage(
+ img,
+ topLeftX,
+ topLeftY,
+ width,
+ height,
+ 0,
+ 0,
+ width,
+ height,
+ );
+ };
+};
+
+const getCropRegionArgs = (
+ cropBoxEle: HTMLDivElement,
+ canvasEle: HTMLCanvasElement,
+) => {
+ // get the bounding rectangle of the crop box
+ const cropBoxRect = cropBoxEle.getBoundingClientRect();
+ // Get the bounding rectangle of the canvas
+ const canvasRect = canvasEle.getBoundingClientRect();
+
+ // calculate the scale of the canvas display relative to its actual dimensions
+ const displayScale = canvasEle.width / canvasRect.width;
+
+ // calculate the coordinates of the crop box relative to the canvas and adjust for any scrolling by adding scroll offsets
+ const x1 =
+ (cropBoxRect.left - canvasRect.left + window.scrollX) * displayScale;
+ const y1 =
+ (cropBoxRect.top - canvasRect.top + window.scrollY) * displayScale;
+ const x2 = x1 + cropBoxRect.width * displayScale;
+ const y2 = y1 + cropBoxRect.height * displayScale;
+
+ return {
+ x1,
+ x2,
+ y1,
+ y2,
+ };
+};
+
interface FreehandCropRegionProps {
cropBox: CropBoxProps;
setIsDragging: Dispatch<SetStateAction<boolean>>;