Skip to content

Commit 49c14da

Browse files
committed
feat: Add copy button in examples
1 parent 107c3af commit 49c14da

File tree

1 file changed

+128
-5
lines changed

1 file changed

+128
-5
lines changed

src/paste/templates/index.html

Lines changed: 128 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{% block style %}
77
@import url('https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/fira_code.min.css');
88
@import url('https://fonts.cdnfonts.com/css/vt323');
9+
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css');
910

1011
:root {
1112
--terminal-green: #00ff00;
@@ -274,6 +275,61 @@
274275
font-size: 20px;
275276
}
276277
}
278+
279+
.code-container {
280+
position: relative;
281+
display: flex;
282+
align-items: center;
283+
gap: 10px;
284+
}
285+
286+
pre {
287+
font-family: var(--terminal-font);
288+
background-color: rgba(0, 255, 0, 0.1);
289+
padding: 20px;
290+
border: 1px solid var(--terminal-green);
291+
margin: 15px 0;
292+
overflow-x: auto;
293+
position: relative;
294+
font-size: 20px;
295+
flex-grow: 1;
296+
}
297+
298+
.copy-button {
299+
background: transparent;
300+
border: none;
301+
color: var(--terminal-green);
302+
cursor: pointer;
303+
padding: 5px;
304+
transition: all 0.3s ease;
305+
font-size: 20px;
306+
display: flex;
307+
align-items: center;
308+
justify-content: center;
309+
opacity: 0.7;
310+
}
311+
312+
.copy-button:hover {
313+
opacity: 1;
314+
transform: scale(1.1);
315+
}
316+
317+
.copy-button.copied {
318+
color: var(--terminal-highlight);
319+
}
320+
321+
pre::before {
322+
content: "$";
323+
color: var(--terminal-highlight);
324+
position: absolute;
325+
left: 8px;
326+
}
327+
328+
pre code {
329+
margin-left: 20px;
330+
display: block;
331+
}
332+
277333
{% endblock %}
278334

279335
{% block content %}
@@ -322,25 +378,40 @@ <h3>API USAGE</h3>
322378

323379
<h3>EXAMPLES</h3>
324380
<p><strong>> cURL:</strong> Paste a file named 'file.txt'</p>
325-
<pre><code>curl -X POST -F "[email protected]" https://paste.fosscu.org/file</code></pre>
381+
<div class="code-container">
382+
<pre><code>curl -X POST -F "[email protected]" https://paste.fosscu.org/file</code></pre>
383+
<button class="copy-button" title="Copy to clipboard"><i class="fas fa-clipboard"></i></button>
384+
</div>
326385

327386
<p><strong>> cURL:</strong> Paste from stdin</p>
328-
<pre><code>echo "Hello, world." | curl -X POST -F "file=@-" https://paste.fosscu.org/file</code></pre>
387+
<div class="code-container">
388+
<pre><code>echo "Hello, world." | curl -X POST -F "file=@-" https://paste.fosscu.org/file</code></pre>
389+
<button class="copy-button" title="Copy to clipboard"><i class="fas fa-clipboard"></i></button>
390+
</div>
329391

330392
<p><strong>> cURL:</strong> Delete an existing paste</p>
331-
<pre><code>curl -X DELETE https://paste.fosscu.org/paste/&lt;id&gt;</code></pre>
393+
<div class="code-container">
394+
<pre><code>curl -X DELETE https://paste.fosscu.org/paste/<id></code></pre>
395+
<button class="copy-button" title="Copy to clipboard"><i class="fas fa-clipboard"></i></button>
396+
</div>
332397

333398
<p><strong>> Shell function:</strong></p>
334-
<pre><code>function paste() {
399+
<div class="code-container">
400+
<pre><code>function paste() {
335401
local file=${1:-/dev/stdin}
336402
curl -X POST -F "file=@${file}" https://paste.fosscu.org/file
337403
}</code></pre>
404+
<button class="copy-button" title="Copy to clipboard"><i class="fas fa-clipboard"></i></button>
405+
</div>
338406

339407
<p>A shell function that can be added to <strong>.bashrc</strong> or <strong>.bash_profile</strong> or <strong>.zshrc</strong> for
340408
quick pasting from the command line. The command takes a filename or reads
341409
from stdin if none was supplied and outputs the URL of the paste to stdout:</p>
342410

343-
<pre><code>paste file.txt</code></pre>
411+
<div class="code-container">
412+
<pre><code>paste file.txt</code></pre>
413+
<button class="copy-button" title="Copy to clipboard"><i class="fas fa-clipboard"></i></button>
414+
</div>
344415
</div>
345416
</div>
346417
{% endblock %}
@@ -389,6 +460,58 @@ <h3>EXAMPLES</h3>
389460

390461
setInterval(draw, 35);
391462

463+
// Add copy button functionality
464+
document.querySelectorAll('.copy-button').forEach(button => {
465+
button.addEventListener('click', async () => {
466+
// Get the associated pre element and extract its text content
467+
const codeContainer = button.closest('.code-container');
468+
const codeElement = codeContainer.querySelector('code');
469+
const textToCopy = codeElement.textContent.trim();
470+
471+
try {
472+
// Create a temporary textarea element to handle the copy
473+
const textarea = document.createElement('textarea');
474+
textarea.value = textToCopy;
475+
textarea.style.position = 'fixed'; // Ensure it's always on screen
476+
textarea.style.opacity = '0'; // Make it invisible
477+
document.body.appendChild(textarea);
478+
textarea.select();
479+
480+
// Try both modern and legacy copy methods
481+
if (navigator.clipboard && window.isSecureContext) {
482+
await navigator.clipboard.writeText(textToCopy);
483+
} else {
484+
document.execCommand('copy');
485+
}
486+
487+
document.body.removeChild(textarea);
488+
489+
// Visual feedback
490+
button.classList.add('copied');
491+
const icon = button.querySelector('i');
492+
icon.classList.remove('fa-clipboard');
493+
icon.classList.add('fa-check');
494+
495+
setTimeout(() => {
496+
button.classList.remove('copied');
497+
icon.classList.remove('fa-check');
498+
icon.classList.add('fa-clipboard');
499+
}, 2000);
500+
} catch (err) {
501+
console.error('Failed to copy:', err);
502+
const icon = button.querySelector('i');
503+
icon.classList.remove('fa-clipboard');
504+
icon.classList.add('fa-times');
505+
506+
setTimeout(() => {
507+
icon.classList.remove('fa-times');
508+
icon.classList.add('fa-clipboard');
509+
}, 2000);
510+
}
511+
});
512+
});
513+
514+
392515
// Terminal boot sequence effect
393516
const container = document.querySelector('.terminal-container');
394517
container.style.opacity = '0';

0 commit comments

Comments
 (0)