Remove complex deep link copy fallback; fine but overkill

This commit is contained in:
Evan Wondrasek 2025-02-28 11:29:22 -08:00
parent 12b273c010
commit f921be0ad4

View File

@ -64,24 +64,17 @@ function initParallax() {
`) `)
const anchor = heading.querySelector('.anchor-link'); const anchor = heading.querySelector('.anchor-link');
anchor.addEventListener('click', (e) => { anchor.addEventListener('click', async (e) => {
e.preventDefault(); e.preventDefault();
const url = new URL(window.location.href); const url = new URL(window.location.href);
url.hash = heading.id; url.hash = heading.id;
// Try modern clipboard API first try {
if (navigator.clipboard && navigator.clipboard.writeText) { await navigator.clipboard.writeText(url.toString());
navigator.clipboard.writeText(url.toString())
.then(() => {
showCopiedFeedback(anchor); showCopiedFeedback(anchor);
}) } catch (err) {
.catch(() => { console.error('Failed to copy:', err);
// Fallback for clipboard API failure anchor.setAttribute('aria-label', 'Failed to copy link');
fallbackCopy(url.toString(), anchor);
});
} else {
// Fallback for browsers without clipboard API
fallbackCopy(url.toString(), anchor);
} }
}); });
}); });
@ -89,7 +82,6 @@ function initParallax() {
function showCopiedFeedback(element) { function showCopiedFeedback(element) {
element.classList.add('copied'); element.classList.add('copied');
// For screen readers
element.setAttribute('aria-label', 'Link copied to clipboard'); element.setAttribute('aria-label', 'Link copied to clipboard');
setTimeout(() => { setTimeout(() => {
@ -98,36 +90,5 @@ function initParallax() {
}, 2000); }, 2000);
} }
function fallbackCopy(text, element) {
// Create temporary input
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed'; // Avoid scrolling to bottom
document.body.appendChild(textArea);
// Handle iOS devices
if (navigator.userAgent.match(/ipad|iphone/i)) {
textArea.contentEditable = true;
textArea.readOnly = true;
const range = document.createRange();
range.selectNodeContents(textArea);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
textArea.setSelectionRange(0, 999999);
} else {
textArea.select();
}
try {
document.execCommand('copy');
showCopiedFeedback(element);
} catch (err) {
console.error('Fallback copy failed:', err);
}
document.body.removeChild(textArea);
}
document.addEventListener('DOMContentLoaded', addAnchors) document.addEventListener('DOMContentLoaded', addAnchors)
})(window, document); })(window, document);