Skip to content

Commit 2d0267c

Browse files
committed
Fix image upload and add clipboard functionality
1 parent 35b997c commit 2d0267c

File tree

2 files changed

+94
-33
lines changed

2 files changed

+94
-33
lines changed

lazyweb/components/snippets/micro/Container.tsx

+68-28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as htmlToImage from 'html-to-image';
55
import generatePDF from 'react-to-pdf'
66
import {uniqueNamesGenerator,adjectives, colors, names} from 'unique-names-generator'
77
import { useRouter } from 'next/router';
8+
import { toast } from 'react-toastify';
89

910
type Props = {
1011
children?: React.JSX.Element | React.JSX.Element[]
@@ -37,43 +38,53 @@ const Container = ({children}: Props) => {
3738
}
3839
};
3940

40-
const handleSaveToImgur = () => {
41+
const handleSaveToIMBB = () => {
4142
if (divRef.current) {
4243
htmlToImage.toPng(divRef.current)
4344
.then((dataUrl) => {
4445
// Convert image to Blob
4546
fetch(dataUrl)
4647
.then(res => res.blob())
4748
.then(blob => {
49+
const name = uniqueNamesGenerator({
50+
dictionaries: [adjectives, colors, names],
51+
length: 3,
52+
separator: '-',
53+
style: 'capital'
54+
});
55+
const imbbAPIKey = process.env.NEXT_PUBLIC_IMBB_API_KEY;
4856
const formData = new FormData();
4957
formData.append('image', blob);
50-
51-
// Imgur API endpoint
52-
const imgurAPI = 'https://api.imgur.com/3/upload';
53-
54-
// Your Imgur client ID
55-
const clientId = '16450111350eabb';
56-
57-
// Post the image to Imgur
58-
fetch(imgurAPI, {
58+
formData.append('key', imbbAPIKey!);
59+
formData.append('name', name);
60+
formData.append('expiration', '15552000');
61+
formData.append('title', name);
62+
63+
64+
// Upload to IMGBB
65+
fetch('https://api.imgbb.com/1/upload', {
5966
method: 'POST',
60-
headers: {
61-
Authorization: `Client-ID ${clientId}`,
62-
},
63-
body: formData,
67+
body: formData
6468
})
65-
.then(response => response.json())
66-
.then(result => {
67-
if (result.success) {
68-
//open image in new tab
69-
window.open(result.data.link, '_blank');
70-
} else {
71-
console.error('Imgur upload failed:', result);
72-
}
73-
})
74-
.catch(error => {
75-
console.error('Error uploading image to Imgur:', error);
76-
});
69+
.then(response => response.json())
70+
.then(data => {
71+
const link = document.createElement('a');
72+
const fileName = uniqueNamesGenerator({
73+
dictionaries: [adjectives, colors, names],
74+
length: 3,
75+
separator: '-',
76+
style: 'capital'
77+
});
78+
//target="_blank"
79+
link.target = '_blank';
80+
link.download = `${fileName}.png`;
81+
link.href = data.data.url;
82+
link.click();
83+
})
84+
.catch(error => {
85+
console.error('Error uploading image to IMGBB:', error);
86+
});
87+
7788
})
7889
.catch(error => {
7990
console.error('Error converting image to Blob:', error);
@@ -84,6 +95,27 @@ const Container = ({children}: Props) => {
8495
});
8596
}
8697
};
98+
99+
const handleSaveClipboard = () => {
100+
if (divRef.current) {
101+
htmlToImage.toBlob(divRef.current)
102+
.then((blob) => {
103+
navigator.clipboard.write([
104+
new ClipboardItem({ 'image/png': blob! })
105+
])
106+
.then(() => {
107+
toast.success('Image copied to clipboard')
108+
})
109+
.catch((error) => {
110+
toast.error('Something went wrong when copying to clipboard')
111+
});
112+
})
113+
.catch((error) => {
114+
toast.error('Something went wrong when converting to image')
115+
});
116+
}
117+
};
118+
87119

88120

89121
const handleSaveSVG = () => {
@@ -117,7 +149,15 @@ const Container = ({children}: Props) => {
117149
});
118150
const pdf = generatePDF(divRef, {
119151
filename: `${filename}.pdf`,
120-
152+
page:{
153+
orientation: 'landscape',
154+
format: 'a4',
155+
156+
},
157+
canvas:{
158+
qualityRatio: 1,
159+
},
160+
resolution: 10
121161
})
122162

123163
}
@@ -135,7 +175,7 @@ const Container = ({children}: Props) => {
135175
}} className=' transition-all duration-300 shadow-[0_8px_30px_rgb(0,0,0,0.12)]'>
136176
{children}
137177
</div>
138-
<Selector uploadImage={handleSaveToImgur} savePDF={handleSavePDF} saveSVG={handleSaveSVG} save={handleSave} />
178+
<Selector saveClip={handleSaveClipboard} uploadImage={handleSaveToIMBB} savePDF={handleSavePDF} saveSVG={handleSaveSVG} save={handleSave} />
139179
</>
140180
)
141181
}

lazyweb/components/snippets/micro/Selector.tsx

+26-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { TbFileTypeSvg } from "react-icons/tb";
1313
import { useRouter } from 'next/router';
1414
import ReactTooltip from 'react-tooltip';
1515
import { BiSolidColor } from "react-icons/bi";
16+
import { MdOutlineFileUpload } from "react-icons/md";
17+
import { FaRegClipboard } from "react-icons/fa6";
1618
/*
1719
1820
*/
@@ -23,9 +25,10 @@ type Props = {
2325
saveSVG: () => void,
2426
savePDF: () => void,
2527
uploadImage: () => void,
28+
saveClip: () => void,
2629
}
2730

28-
const Selector = ({save, saveSVG, savePDF, uploadImage}: Props) => {
31+
const Selector = ({save, saveSVG, savePDF, uploadImage,saveClip}: Props) => {
2932
const [isEditOpen, setIsEditOpen] = useState(false);
3033
const {paddingX, setPaddingX,width, setWidth, containerBorderRadius, setContainerBorderRadius, paddingY,setPaddingY,setGradient,borderRadius, setBorderRadius} = useUIStore()
3134
const router = useRouter()
@@ -295,7 +298,7 @@ const Selector = ({save, saveSVG, savePDF, uploadImage}: Props) => {
295298
</p>
296299
</button>
297300
</div>
298-
{/* <div className="my-2 flex flex-col gap-2 w-full">
301+
<div className="my-2 flex flex-col gap-2 w-full">
299302
<button onClick={() => {
300303
event('upload image', {
301304
category: 'upload-image-snippet',
@@ -306,13 +309,31 @@ const Selector = ({save, saveSVG, savePDF, uploadImage}: Props) => {
306309
uploadImage()
307310
setIsEditOpen(false)
308311
}} className="flex items-center gap-[1rem]">
309-
<FaRegFilePdf className={` text-[18px] hover:scale-[1.1] cursor-pointer transition-all`} />
312+
<MdOutlineFileUpload className={` text-[18px] hover:scale-[1.1] cursor-pointer transition-all`} />
313+
314+
<p className="text-[14px]">
315+
Upload to ImgBB
316+
</p>
317+
</button>
318+
</div>
319+
<div className="my-2 flex flex-col gap-2 w-full">
320+
<button onClick={() => {
321+
event('upload image', {
322+
category: 'upload-image-snippet',
323+
title: 'upload-image-snippet',
324+
url: 'upload-image-snippet',
325+
})
326+
327+
saveClip()
328+
setIsEditOpen(false)
329+
}} className="flex items-center gap-[1rem]">
330+
<FaRegClipboard className={` text-[18px] hover:scale-[1.1] cursor-pointer transition-all`} />
310331

311332
<p className="text-[14px]">
312-
Save to Imgur
333+
Copy to Clipboard
313334
</p>
314335
</button>
315-
</div> */}
336+
</div>
316337
</PopoverContent>
317338
</Popover>
318339

0 commit comments

Comments
 (0)