159159 margin-top : 1px ;
160160 opacity : .7 ;
161161 }
162+
163+ /* Snippet generator */
164+ .snippet-section {
165+ display : none;
166+ }
167+
168+ .snippet-input {
169+ font-family : var (--bs-font-monospace );
170+ font-size : .75rem ;
171+ color : var (--bs-secondary-color );
172+ background-color : var (--bs-tertiary-bg );
173+ cursor : default;
174+ }
162175 </ style >
163176</ head >
164177
@@ -219,17 +232,38 @@ <h1 class="h4 fw-semibold mb-0">Base64 Image Encoder</h1>
219232 </ div >
220233 </ div >
221234 < div class ="output-text-wrapper ">
222- < button id ="copy-button " type ="button " class ="btn btn-sm btn-light border " title ="Copy to clipboard ">
235+ < button id ="copy-button " type ="button " class ="btn btn-sm btn-primary " title ="Copy to clipboard ">
223236 < svg id ="clipboard-icon " xmlns ="http://www.w3.org/2000/svg " width ="14 " height ="14 "
224- fill ="currentColor " class ="bi bi-clipboard " viewBox ="0 0 16 16 ">
237+ fill ="currentColor " class ="bi bi-clipboard me-1 " viewBox ="0 0 16 16 ">
225238 < path
226239 d ="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z " />
227240 < path
228241 d ="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z " />
229- </ svg >
242+ </ svg > Copy
230243 </ button >
231244 < textarea id ="output-text " class ="form-control " rows ="20 " readonly > </ textarea >
232245 </ div >
246+
247+ < div id ="snippet-section " class ="snippet-section mt-3 ">
248+ < p class ="form-label fw-medium mb-2 "> Use as</ p >
249+ < div class ="d-flex flex-column gap-2 ">
250+ < div class ="input-group input-group-sm ">
251+ < span class ="input-group-text text-body-secondary " style ="width:5rem; "> HTML</ span >
252+ < input id ="snippet-html " type ="text " class ="form-control snippet-input " readonly >
253+ < button class ="btn btn-outline-secondary snippet-copy " data-target ="snippet-html " type ="button "> Copy</ button >
254+ </ div >
255+ < div class ="input-group input-group-sm ">
256+ < span class ="input-group-text text-body-secondary " style ="width:5rem; "> CSS</ span >
257+ < input id ="snippet-css " type ="text " class ="form-control snippet-input " readonly >
258+ < button class ="btn btn-outline-secondary snippet-copy " data-target ="snippet-css " type ="button "> Copy</ button >
259+ </ div >
260+ < div class ="input-group input-group-sm ">
261+ < span class ="input-group-text text-body-secondary " style ="width:5rem; "> Markdown</ span >
262+ < input id ="snippet-md " type ="text " class ="form-control snippet-input " readonly >
263+ < button class ="btn btn-outline-secondary snippet-copy " data-target ="snippet-md " type ="button "> Copy</ button >
264+ </ div >
265+ </ div >
266+ </ div >
233267 </ div >
234268 < div class ="col-lg-6 ">
235269 < p class ="form-label fw-medium mb-2 "> Image preview</ p >
@@ -278,6 +312,10 @@ <h1 class="h4 fw-semibold mb-0">Base64 Image Encoder</h1>
278312 const copyButton = document . getElementById ( 'copy-button' ) ;
279313 const clipboardIcon = document . getElementById ( 'clipboard-icon' ) ;
280314 const copyToast = new bootstrap . Toast ( document . getElementById ( 'copy-toast' ) , { delay : 2000 } ) ;
315+ const snippetSection = document . getElementById ( 'snippet-section' ) ;
316+ const snippetHtml = document . getElementById ( 'snippet-html' ) ;
317+ const snippetCss = document . getElementById ( 'snippet-css' ) ;
318+ const snippetMd = document . getElementById ( 'snippet-md' ) ;
281319
282320 let lastDataUrl = '' ;
283321
@@ -301,10 +339,19 @@ <h1 class="h4 fw-semibold mb-0">Base64 Image Encoder</h1>
301339 outputImage . src = lastDataUrl ;
302340 outputImage . style . display = 'block' ;
303341 previewPlaceholder . style . display = 'none' ;
342+ if ( file . size <= 100 * 1024 ) {
343+ snippetHtml . value = `<img src="${ lastDataUrl } " alt="">` ;
344+ snippetCss . value = `background-image: url("${ lastDataUrl } ");` ;
345+ snippetMd . value = `` ;
346+ snippetSection . style . display = 'block' ;
347+ } else {
348+ snippetSection . style . display = 'none' ;
349+ }
304350 } else {
305351 outputImage . src = '' ;
306352 outputImage . style . display = 'none' ;
307353 previewPlaceholder . style . display = 'block' ;
354+ snippetSection . style . display = 'none' ;
308355 }
309356 } ;
310357 reader . readAsDataURL ( file ) ;
@@ -368,9 +415,25 @@ <h1 class="h4 fw-semibold mb-0">Base64 Image Encoder</h1>
368415 outputText . setSelectionRange ( 0 , outputText . value . length ) ;
369416 } ) ;
370417
418+ document . querySelectorAll ( '.snippet-copy' ) . forEach ( btn => {
419+ btn . addEventListener ( 'click' , async ( ) => {
420+ const input = document . getElementById ( btn . dataset . target ) ;
421+ await navigator . clipboard . writeText ( input . value ) ;
422+ const original = btn . textContent ;
423+ btn . textContent = 'Copied!' ;
424+ setTimeout ( ( ) => btn . textContent = original , 2000 ) ;
425+ } ) ;
426+ } ) ;
427+
371428 copyButton . addEventListener ( 'click' , async ( ) => {
372429 await navigator . clipboard . writeText ( outputText . value ) ;
373430 copyToast . show ( ) ;
431+ clipboardIcon . style . display = 'none' ;
432+ copyButton . append ( ' Copied!' ) ;
433+ setTimeout ( ( ) => {
434+ copyButton . lastChild . remove ( ) ;
435+ clipboardIcon . style . display = 'inline-block' ;
436+ } , 2000 ) ;
374437 } ) ;
375438 } ) ( ) ;
376439 </ script >
0 commit comments