-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: removed "add line" behaviour from Docsy's click-to-copy
- Loading branch information
1 parent
4ac2878
commit 87803ba
Showing
1 changed file
with
46 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
let codeListings = document.querySelectorAll('.highlight > pre'); | ||
|
||
for (let index = 0; index < codeListings.length; index++) { | ||
const codeSample = codeListings[index].querySelector('code'); | ||
const copyButton = document.createElement('button'); | ||
const buttonAttributes = { | ||
type: 'button', | ||
title: 'Copy to clipboard', | ||
'data-bs-toggle': 'tooltip', | ||
'data-bs-placement': 'top', | ||
'data-bs-container': 'body', | ||
}; | ||
|
||
Object.keys(buttonAttributes).forEach((key) => { | ||
copyButton.setAttribute(key, buttonAttributes[key]); | ||
}); | ||
|
||
copyButton.classList.add( | ||
'fas', | ||
'fa-copy', | ||
'btn', | ||
'btn-sm', | ||
'td-click-to-copy' | ||
); | ||
const tooltip = new bootstrap.Tooltip(copyButton); | ||
|
||
copyButton.onclick = () => { | ||
copyCode(codeSample); | ||
copyButton.setAttribute('data-bs-original-title', 'Copied!'); | ||
tooltip.show(); | ||
}; | ||
|
||
copyButton.onmouseout = () => { | ||
copyButton.setAttribute('data-bs-original-title', 'Copy to clipboard'); | ||
tooltip.hide(); | ||
}; | ||
|
||
const buttonDiv = document.createElement('div'); | ||
buttonDiv.classList.add('click-to-copy'); | ||
buttonDiv.append(copyButton); | ||
codeListings[index].insertBefore(buttonDiv, codeSample); | ||
} | ||
|
||
const copyCode = (codeSample) => { | ||
navigator.clipboard.writeText(codeSample.textContent.trim()); | ||
}; |