Skip to content

Commit 3d1f70f

Browse files
authored
Merge pull request #677 from Lemoncode/dev
fix drop multiple images
2 parents 0be3e75 + 16c2ef3 commit 3d1f70f

1 file changed

Lines changed: 51 additions & 39 deletions

File tree

src/pods/canvas/use-drop-image-from-desktop.tsx

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,64 @@ export const useDropImageFromDesktop = (
2727
e.preventDefault();
2828
e.stopPropagation();
2929

30-
const file = e.dataTransfer.files[0];
31-
const reader = new FileReader();
30+
const files = e.dataTransfer.files;
31+
3232
const { clientX, clientY } = e;
33+
3334
const divCoords = {
3435
x: clientX - e.currentTarget.offsetLeft,
3536
y: clientY - e.currentTarget.offsetTop,
3637
};
3738

38-
reader.onload = e => {
39-
const img = new Image();
40-
img.src = e.target?.result as string;
41-
img.onload = () => {
42-
invariant(stageRef.current);
43-
const stage = stageRef.current;
44-
const { scrollLeft, scrollTop } = getScrollFromDiv(dropRef);
45-
let konvaCoord = calculateScaledCoordsFromCanvasDivCoordinates(
46-
stage,
47-
divCoords,
48-
{ x: scrollLeft, y: scrollTop }
49-
);
50-
51-
const positionX =
52-
konvaCoord.x -
53-
calculateShapeOffsetToXDropCoordinate(konvaCoord.x, 'image');
54-
const positionY = konvaCoord.y;
55-
56-
const newImg = addNewShape('image', positionX, positionY, {
57-
imageSrc: img.src,
58-
});
59-
60-
// Preserves aspect ratio
61-
const defaultWidth = getImageShapeSizeRestrictions().defaultWidth;
62-
const defaultHeight = getImageShapeSizeRestrictions().defaultHeight;
63-
updateShapeSizeAndPosition(
64-
newImg,
65-
{ x: positionX, y: positionY },
66-
adjustSizeKeepingAspectRatio(
67-
{ width: img.width, height: img.height },
68-
{ width: defaultWidth, height: defaultHeight }
69-
),
70-
false
71-
);
72-
};
73-
};
39+
let positionIncrement = 0;
40+
41+
Array.from(files).forEach(file => {
42+
const reader = new FileReader();
43+
44+
reader.onload = e => {
45+
const img = new Image();
46+
img.src = e.target?.result as string;
47+
48+
img.onload = () => {
49+
invariant(stageRef.current);
50+
const stage = stageRef.current;
51+
const { scrollLeft, scrollTop } = getScrollFromDiv(dropRef);
52+
let konvaCoord = calculateScaledCoordsFromCanvasDivCoordinates(
53+
stage,
54+
divCoords,
55+
{ x: scrollLeft, y: scrollTop }
56+
);
7457

75-
reader.readAsDataURL(file);
58+
const positionX =
59+
konvaCoord.x -
60+
calculateShapeOffsetToXDropCoordinate(konvaCoord.x, 'image') +
61+
positionIncrement;
62+
63+
const positionY = konvaCoord.y + positionIncrement;
64+
65+
const newImg = addNewShape('image', positionX, positionY, {
66+
imageSrc: img.src,
67+
});
68+
69+
// Preserves aspect ratio
70+
const defaultWidth = getImageShapeSizeRestrictions().defaultWidth;
71+
const defaultHeight = getImageShapeSizeRestrictions().defaultHeight;
72+
73+
updateShapeSizeAndPosition(
74+
newImg,
75+
{ x: positionX, y: positionY },
76+
adjustSizeKeepingAspectRatio(
77+
{ width: img.width, height: img.height },
78+
{ width: defaultWidth, height: defaultHeight }
79+
),
80+
false
81+
);
82+
83+
positionIncrement += 40;
84+
};
85+
};
86+
reader.readAsDataURL(file);
87+
});
7688
}
7789
};
7890

0 commit comments

Comments
 (0)