@@ -5,6 +5,7 @@ import * as htmlToImage from 'html-to-image';
5
5
import generatePDF from 'react-to-pdf'
6
6
import { uniqueNamesGenerator , adjectives , colors , names } from 'unique-names-generator'
7
7
import { useRouter } from 'next/router' ;
8
+ import { toast } from 'react-toastify' ;
8
9
9
10
type Props = {
10
11
children ?: React . JSX . Element | React . JSX . Element [ ]
@@ -37,43 +38,53 @@ const Container = ({children}: Props) => {
37
38
}
38
39
} ;
39
40
40
- const handleSaveToImgur = ( ) => {
41
+ const handleSaveToIMBB = ( ) => {
41
42
if ( divRef . current ) {
42
43
htmlToImage . toPng ( divRef . current )
43
44
. then ( ( dataUrl ) => {
44
45
// Convert image to Blob
45
46
fetch ( dataUrl )
46
47
. then ( res => res . blob ( ) )
47
48
. 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 ;
48
56
const formData = new FormData ( ) ;
49
57
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' , {
59
66
method : 'POST' ,
60
- headers : {
61
- Authorization : `Client-ID ${ clientId } ` ,
62
- } ,
63
- body : formData ,
67
+ body : formData
64
68
} )
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
+
77
88
} )
78
89
. catch ( error => {
79
90
console . error ( 'Error converting image to Blob:' , error ) ;
@@ -84,6 +95,27 @@ const Container = ({children}: Props) => {
84
95
} ) ;
85
96
}
86
97
} ;
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
+
87
119
88
120
89
121
const handleSaveSVG = ( ) => {
@@ -117,7 +149,15 @@ const Container = ({children}: Props) => {
117
149
} ) ;
118
150
const pdf = generatePDF ( divRef , {
119
151
filename : `${ filename } .pdf` ,
120
-
152
+ page :{
153
+ orientation : 'landscape' ,
154
+ format : 'a4' ,
155
+
156
+ } ,
157
+ canvas :{
158
+ qualityRatio : 1 ,
159
+ } ,
160
+ resolution : 10
121
161
} )
122
162
123
163
}
@@ -135,7 +175,7 @@ const Container = ({children}: Props) => {
135
175
} } className = ' transition-all duration-300 shadow-[0_8px_30px_rgb(0,0,0,0.12)]' >
136
176
{ children }
137
177
</ div >
138
- < Selector uploadImage = { handleSaveToImgur } savePDF = { handleSavePDF } saveSVG = { handleSaveSVG } save = { handleSave } />
178
+ < Selector saveClip = { handleSaveClipboard } uploadImage = { handleSaveToIMBB } savePDF = { handleSavePDF } saveSVG = { handleSaveSVG } save = { handleSave } />
139
179
</ >
140
180
)
141
181
}
0 commit comments