Skip to content

armgit / ente

Public Imported from GitHub
0

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

feat: floating apply crop button when cropping

httpjamesmf3a5451e0ff8
diff --git a/apps/photos/public/locales/en/translation.json b/apps/photos/public/locales/en/translation.json
index beb8722fe9..8d6a7516ab 100644
--- a/apps/photos/public/locales/en/translation.json
+++ b/apps/photos/public/locales/en/translation.json
@@ -626,6 +626,6 @@
"INDEXED_ITEMS": "Indexed items",
"CACHE_DIRECTORY": "Cache folder",
"FREEHAND": "Freehand",
- "APPLY": "Apply",
+ "APPLY_CROP": "Apply Crop",
"PHOTO_EDIT_REQUIRED_TO_SAVE": "At least one transformation or color adjustment must be performed before saving."
}
diff --git a/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/CropMenu.tsx b/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/CropMenu.tsx
index 625e807448..1d07dea1e8 100644
--- a/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/CropMenu.tsx
+++ b/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/CropMenu.tsx
@@ -15,6 +15,71 @@ interface IProps {
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,
@@ -25,46 +90,6 @@ const CropMenu = (props: IProps) => {
setCurrentTab,
} = useContext(ImageEditorOverlayContext);
- const cropRegionOfCanvas = (
- canvas: HTMLCanvasElement,
- topLeftX: number,
- topLeftY: number,
- bottomRightX: number,
- bottomRightY: number,
- scale: number = 1
- ) => {
- setTransformationPerformed(true);
- 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
- );
-
- props.resetCropBox();
- };
- };
-
return (
<>
<MenuSectionTitle title={t('FREEHAND')} />
@@ -79,32 +104,12 @@ const CropMenu = (props: IProps) => {
if (!props.cropBoxRef.current || !canvasRef.current)
return;
- // get the bounding rectangle of the crop box
- const cropBoxRect =
- props.cropBoxRef.current.getBoundingClientRect();
- // Get the bounding rectangle of the canvas
- const canvasRect =
- canvasRef.current.getBoundingClientRect();
-
- // calculate the scale of the canvas display relative to its actual dimensions
- const displayScale =
- canvasRef.current.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;
-
+ 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,
@@ -113,11 +118,12 @@ const CropMenu = (props: IProps) => {
x2 / props.previewScale,
y2 / props.previewScale
);
+ props.resetCropBox();
setCanvasLoading(false);
setCurrentTab('transform');
}}
- label={t('APPLY')}
+ label={t('APPLY_CROP')}
/>
</MenuItemGroup>
</>
diff --git a/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx b/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx
index e25745178a..8c053f4717 100644
--- a/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx
+++ b/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx
@@ -47,6 +47,10 @@ import { getFileType } from 'services/typeDetectionService';
import { downloadUsingAnchor } from '@ente/shared/utils';
import { CORNER_THRESHOLD, FILTER_DEFAULT_VALUES } from 'constants/photoEditor';
import FreehandCropRegion from './FreehandCropRegion';
+import EnteButton from '@ente/shared/components/EnteButton';
+import { CenteredFlex } from '@ente/shared/components/Container';
+import CropIcon from '@mui/icons-material/Crop';
+import { cropRegionOfCanvas, getCropRegionArgs } from './CropMenu';
interface IProps {
file: EnteFile;
@@ -591,6 +595,48 @@ const ImageEditorOverlay = (props: IProps) => {
/>
)}
</Box>
+ {currentTab === 'crop' && (
+ <CenteredFlex marginTop="1rem">
+ <EnteButton
+ color="accent"
+ startIcon={<CropIcon />}
+ onClick={() => {
+ if (
+ !cropBoxRef.current ||
+ !canvasRef.current
+ )
+ return;
+
+ const { x1, x2, y1, y2 } =
+ getCropRegionArgs(
+ cropBoxRef.current,
+ canvasRef.current
+ );
+ setCanvasLoading(true);
+ setTransformationPerformed(true);
+ cropRegionOfCanvas(
+ canvasRef.current,
+ x1,
+ y1,
+ x2,
+ y2
+ );
+ cropRegionOfCanvas(
+ originalSizeCanvasRef.current,
+ x1 / previewCanvasScale,
+ y1 / previewCanvasScale,
+ x2 / previewCanvasScale,
+ y2 / previewCanvasScale
+ );
+ resetCropBox();
+ setCanvasLoading(false);
+
+ setCurrentTab('transform');
+ }}>
+ {t('APPLY_CROP')}
+ </EnteButton>
+ </CenteredFlex>
+ )}
</Box>
</Box>
</Box>