-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add code copy button icons in js (#133)
- Loading branch information
Showing
3 changed files
with
70 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,63 @@ | ||
document.querySelectorAll('.code-copy-btn').forEach(function (button) { | ||
button.addEventListener('click', function (e) { | ||
e.preventDefault(); | ||
const targetId = button.getAttribute('data-clipboard-target'); | ||
const target = document.querySelector(targetId); | ||
let codeElement; | ||
if (target.tagName === 'CODE') { | ||
codeElement = target; | ||
} else { | ||
// Select the last code element in case line numbers are present | ||
const codeElements = target.querySelectorAll('code'); | ||
codeElement = codeElements[codeElements.length - 1]; | ||
} | ||
if (codeElement) { | ||
// Replace double newlines with single newlines in the innerText | ||
// as each line inside <span> has trailing newline '\n' | ||
const code = codeElement.innerText.replace(/\n\n/g, '\n'); | ||
navigator.clipboard.writeText(code).then(function () { | ||
button.classList.add('copied'); | ||
setTimeout(function () { | ||
button.classList.remove('copied'); | ||
}, 500); | ||
}).catch(function (err) { | ||
console.error('Failed to copy text: ', err); | ||
}); | ||
} else { | ||
console.error('Target element not found'); | ||
} | ||
// Copy button for code blocks | ||
|
||
document.addEventListener('DOMContentLoaded', function () { | ||
const getCopyIcon = () => { | ||
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); | ||
svg.innerHTML = ` | ||
<path stroke-linecap="round" stroke-linejoin="round" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" /> | ||
`; | ||
svg.setAttribute('fill', 'none'); | ||
svg.setAttribute('viewBox', '0 0 24 24'); | ||
svg.setAttribute('stroke', 'currentColor'); | ||
svg.setAttribute('stroke-width', '2'); | ||
return svg; | ||
} | ||
|
||
const getSuccessIcon = () => { | ||
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); | ||
svg.innerHTML = ` | ||
<path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" /> | ||
`; | ||
svg.setAttribute('fill', 'none'); | ||
svg.setAttribute('viewBox', '0 0 24 24'); | ||
svg.setAttribute('stroke', 'currentColor'); | ||
svg.setAttribute('stroke-width', '2'); | ||
return svg; | ||
} | ||
|
||
document.querySelectorAll('.code-copy-btn').forEach(function (button) { | ||
// Add copy and success icons | ||
button.querySelector('.copy-icon')?.appendChild(getCopyIcon()); | ||
button.querySelector('.success-icon')?.appendChild(getSuccessIcon()); | ||
|
||
// Add click event listener for copy button | ||
button.addEventListener('click', function (e) { | ||
e.preventDefault(); | ||
const targetId = button.getAttribute('data-clipboard-target'); | ||
const target = document.querySelector(targetId); | ||
let codeElement; | ||
if (target.tagName === 'CODE') { | ||
codeElement = target; | ||
} else { | ||
// Select the last code element in case line numbers are present | ||
const codeElements = target.querySelectorAll('code'); | ||
codeElement = codeElements[codeElements.length - 1]; | ||
} | ||
if (codeElement) { | ||
// Replace double newlines with single newlines in the innerText | ||
// as each line inside <span> has trailing newline '\n' | ||
const code = codeElement.innerText.replace(/\n\n/g, '\n'); | ||
navigator.clipboard.writeText(code).then(function () { | ||
button.classList.add('copied'); | ||
setTimeout(function () { | ||
button.classList.remove('copied'); | ||
}, 500); | ||
}).catch(function (err) { | ||
console.error('Failed to copy text: ', err); | ||
}); | ||
} else { | ||
console.error('Target element not found'); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters