Skip to content

Commit

Permalink
Merge pull request #677 from Lemoncode/dev
Browse files Browse the repository at this point in the history
fix drop multiple images
  • Loading branch information
brauliodiez authored Jan 31, 2025
2 parents 0be3e75 + 16c2ef3 commit 3d1f70f
Showing 1 changed file with 51 additions and 39 deletions.
90 changes: 51 additions & 39 deletions src/pods/canvas/use-drop-image-from-desktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,64 @@ export const useDropImageFromDesktop = (
e.preventDefault();
e.stopPropagation();

const file = e.dataTransfer.files[0];
const reader = new FileReader();
const files = e.dataTransfer.files;

const { clientX, clientY } = e;

const divCoords = {
x: clientX - e.currentTarget.offsetLeft,
y: clientY - e.currentTarget.offsetTop,
};

reader.onload = e => {
const img = new Image();
img.src = e.target?.result as string;
img.onload = () => {
invariant(stageRef.current);
const stage = stageRef.current;
const { scrollLeft, scrollTop } = getScrollFromDiv(dropRef);
let konvaCoord = calculateScaledCoordsFromCanvasDivCoordinates(
stage,
divCoords,
{ x: scrollLeft, y: scrollTop }
);

const positionX =
konvaCoord.x -
calculateShapeOffsetToXDropCoordinate(konvaCoord.x, 'image');
const positionY = konvaCoord.y;

const newImg = addNewShape('image', positionX, positionY, {
imageSrc: img.src,
});

// Preserves aspect ratio
const defaultWidth = getImageShapeSizeRestrictions().defaultWidth;
const defaultHeight = getImageShapeSizeRestrictions().defaultHeight;
updateShapeSizeAndPosition(
newImg,
{ x: positionX, y: positionY },
adjustSizeKeepingAspectRatio(
{ width: img.width, height: img.height },
{ width: defaultWidth, height: defaultHeight }
),
false
);
};
};
let positionIncrement = 0;

Array.from(files).forEach(file => {
const reader = new FileReader();

reader.onload = e => {
const img = new Image();
img.src = e.target?.result as string;

img.onload = () => {
invariant(stageRef.current);
const stage = stageRef.current;
const { scrollLeft, scrollTop } = getScrollFromDiv(dropRef);
let konvaCoord = calculateScaledCoordsFromCanvasDivCoordinates(
stage,
divCoords,
{ x: scrollLeft, y: scrollTop }
);

reader.readAsDataURL(file);
const positionX =
konvaCoord.x -
calculateShapeOffsetToXDropCoordinate(konvaCoord.x, 'image') +
positionIncrement;

const positionY = konvaCoord.y + positionIncrement;

const newImg = addNewShape('image', positionX, positionY, {
imageSrc: img.src,
});

// Preserves aspect ratio
const defaultWidth = getImageShapeSizeRestrictions().defaultWidth;
const defaultHeight = getImageShapeSizeRestrictions().defaultHeight;

updateShapeSizeAndPosition(
newImg,
{ x: positionX, y: positionY },
adjustSizeKeepingAspectRatio(
{ width: img.width, height: img.height },
{ width: defaultWidth, height: defaultHeight }
),
false
);

positionIncrement += 40;
};
};
reader.readAsDataURL(file);
});
}
};

Expand Down

0 comments on commit 3d1f70f

Please sign in to comment.