Skip to content

armgit / ente

Public Imported from GitHub
0

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

feat: auto correct cropbox pos on load

httpjamesm219102c31756
diff --git a/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx b/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx
index b9dc6aa4d4..069a6b809b 100644
--- a/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx
+++ b/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx
@@ -177,6 +177,22 @@ const ImageEditorOverlay = (props: IProps) => {
const cropBoxRef = useRef<HTMLDivElement>(null);
+ const getCanvasBoundsOffsets = () => {
+ const canvasBounds = canvasRef.current.getBoundingClientRect();
+ const parentBounds = parentRef.current.getBoundingClientRect();
+
+ // calculate the offset created by centering the canvas in its parent
+ const offsetX = (parentBounds.width - canvasBounds.width) / 2;
+ const offsetY = (parentBounds.height - canvasBounds.height) / 2;
+
+ return {
+ offsetY,
+ offsetX,
+ canvasBounds,
+ parentBounds,
+ };
+ };
+
const handleDragStart = (e) => {
if (currentTab !== 'crop') return;
@@ -214,12 +230,8 @@ const ImageEditorOverlay = (props: IProps) => {
const dx = e.pageX - startX;
const dy = e.pageY - startY;
- const canvasBounds = canvasRef.current.getBoundingClientRect();
- const parentBounds = parentRef.current.getBoundingClientRect();
-
- // calculate the offset created by centering the canvas in its parent
- const offsetX = (parentBounds.width - canvasBounds.width) / 2;
- const offsetY = (parentBounds.height - canvasBounds.height) / 2;
+ const { offsetX, offsetY, canvasBounds, parentBounds } =
+ getCanvasBoundsOffsets();
if (isGrowing) {
setCropBox((prev) => {
@@ -272,6 +284,24 @@ const ImageEditorOverlay = (props: IProps) => {
setIsDragging(false);
};
+ const moveCropBoxToTopLeft = () => {
+ const { offsetX, offsetY, canvasBounds, parentBounds } =
+ getCanvasBoundsOffsets();
+
+ // Set the crop box position to the top-left corner of the canvas, accounting for the offset
+ setCropBox((prev) => ({
+ ...prev,
+ x: offsetX,
+ y: offsetY,
+ }));
+ };
+
+ useEffect(() => {
+ if (!canvasRef.current || !parentRef.current || !cropBoxRef.current)
+ return;
+ moveCropBoxToTopLeft();
+ }, [canvasRef.current, parentRef.current, cropBoxRef.current]);
+
useEffect(() => {
if (!canvasRef.current) {
return;