189189.copy-buttons-container {
190190 position: absolute;
191191 right: 20px;
192- top: 20px ;
192+ top: 70px ;
193193 display: flex;
194194 flex-direction: column;
195195 gap: 10px;
305305
306306 .copy-buttons-container {
307307 right: 10px;
308- top: 10px ;
308+ top: 60px ;
309309 }
310310
311311 .copy-button {
331331 < a href ="{{ url_for('indexpage') }} " class ="header-link ">
332332 < div class ="glitch-text " data-text ="paste.py 🐍 "> < u > paste.py</ u > 🐍</ div >
333333 </ a >
334- < div style ="width: 50px; "> </ div >
334+ < div class ="copy-buttons-container ">
335+ < button id ="copyButton " class ="copy-button " onclick ="copyAllText() ">
336+ < i class ="fas fa-copy "> </ i > COPY CODE
337+ </ button >
338+ < button id ="copyLinkButton " class ="copy-button " onclick ="copyLink() ">
339+ < i class ="fas fa-link "> </ i > COPY LINK
340+ </ button >
341+ </ div >
335342 </ div >
336- < div class ="copy-buttons-container ">
337- < button id ="copyButton " class ="copy-button " onclick ="copyAllText() ">
338- < i class ="fas fa-copy "> </ i > COPY CODE
339- </ button >
340- < button id ="copyLinkButton " class ="copy-button " onclick ="copyLink() ">
341- < i class ="fas fa-link "> </ i > COPY LINK
342- </ button >
343- </ div >
344343 < div class ="code ">
345344 {{ highlighted_code | safe }}
346345 </ div >
402401 }, 300);
403402 });
404403
405- function copyAllText() {
406- const codeElement = document.querySelector('.code pre');
407- const range = document.createRange();
408- range.selectNode(codeElement);
409-
410- const selection = window.getSelection();
411- selection.removeAllRanges();
412- selection.addRange(range);
413-
414- document.execCommand('copy');
415- selection.removeAllRanges();
416-
417- const copyButton = document.getElementById('copyButton');
418- const originalText = copyButton.innerHTML;
419- copyButton.innerHTML = '< i class ="fas fa-check "> </ i > COPIED!';
420-
404+ function showCopyFeedback(button, success, successHTML, failureHTML) {
405+ const originalText = button.innerHTML;
406+ button.innerHTML = success ? successHTML : failureHTML;
421407 setTimeout(() => {
422- copyButton .innerHTML = originalText;
408+ button .innerHTML = originalText;
423409 }, 2000);
424410 }
425-
426- function copyLink() {
427- const currentUrl = window.location.href;
428-
429- if (navigator.clipboard) {
430- navigator.clipboard.writeText(currentUrl).then(() => {
431- const copyLinkButton = document.getElementById('copyLinkButton');
432- const originalText = copyLinkButton.innerHTML;
433- copyLinkButton.innerHTML = '< i class ="fas fa-check "> </ i > LINK COPIED!';
434-
435- setTimeout(() => {
436- copyLinkButton.innerHTML = originalText;
437- }, 2000);
438- }).catch(() => {
439- fallbackCopyLink(currentUrl);
440- });
441- } else {
442- fallbackCopyLink(currentUrl);
443- }
444- }
445-
446- function fallbackCopyLink(text) {
411+
412+ function fallbackCopy(textToCopy, button, successHTML, failureHTML) {
447413 const textArea = document.createElement("textarea");
448- textArea.value = text ;
414+ textArea.value = textToCopy ;
449415 textArea.style.position = "fixed";
450416 textArea.style.left = "-999999px";
451417 textArea.style.top = "-999999px";
452418 document.body.appendChild(textArea);
453419 textArea.focus();
454420 textArea.select();
455-
421+
422+ let success = false;
456423 try {
457- document.execCommand('copy');
458- const copyLinkButton = document.getElementById('copyLinkButton');
459- const originalText = copyLinkButton.innerHTML;
460- copyLinkButton.innerHTML = '< i class ="fas fa-check "> </ i > LINK COPIED!';
461-
462- setTimeout(() => {
463- copyLinkButton.innerHTML = originalText;
464- }, 2000);
424+ success = document.execCommand('copy');
465425 } catch (err) {
466- const copyLinkButton = document.getElementById('copyLinkButton');
467- const originalText = copyLinkButton.innerHTML;
468- copyLinkButton.innerHTML = '< i class ="fas fa-times "> </ i > COPY FAILED';
469-
470- setTimeout(() => {
471- copyLinkButton.innerHTML = originalText;
472- }, 2000);
426+ console.error('Fallback copy failed', err);
473427 }
474-
428+
475429 document.body.removeChild(textArea);
430+ showCopyFeedback(button, success, successHTML, failureHTML);
431+ }
432+
433+ function copyAllText() {
434+ const codeElement = document.querySelector('.code pre');
435+ if (!codeElement) return;
436+
437+ const clone = codeElement.cloneNode(true);
438+ const lineNumbers = clone.querySelectorAll('.linenos');
439+ lineNumbers.forEach(span => span.remove());
440+ const textToCopy = clone.textContent;
441+
442+ const copyButton = document.getElementById('copyButton');
443+ const successHTML = '< i class ="fas fa-check "> </ i > COPIED!';
444+ const failureHTML = '< i class ="fas fa-times "> </ i > FAILED!';
445+
446+ if (navigator.clipboard) {
447+ navigator.clipboard.writeText(textToCopy).then(() => {
448+ showCopyFeedback(copyButton, true, successHTML, failureHTML);
449+ }).catch(err => {
450+ console.error('Async copy failed, using fallback', err);
451+ fallbackCopy(textToCopy, copyButton, successHTML, failureHTML);
452+ });
453+ } else {
454+ fallbackCopy(textToCopy, copyButton, successHTML, failureHTML);
455+ }
456+ }
457+
458+ function copyLink() {
459+ const currentUrl = window.location.href;
460+ const copyLinkButton = document.getElementById('copyLinkButton');
461+ const successHTML = '< i class ="fas fa-check "> </ i > LINK COPIED!';
462+ const failureHTML = '< i class ="fas fa-times "> </ i > COPY FAILED';
463+
464+ if (navigator.clipboard) {
465+ navigator.clipboard.writeText(currentUrl).then(() => {
466+ showCopyFeedback(copyLinkButton, true, successHTML, failureHTML);
467+ }).catch(() => {
468+ fallbackCopy(currentUrl, copyLinkButton, successHTML, failureHTML);
469+ });
470+ } else {
471+ fallbackCopy(currentUrl, copyLinkButton, successHTML, failureHTML);
472+ }
476473 }
477474
478475{% endblock %}
0 commit comments