forked from google/docsy
-
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.
Adds a click to copy button to code samples that don't use the Prism highlighter.
- Loading branch information
Showing
3 changed files
with
137 additions
and
2 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,86 @@ | ||
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"); | ||
copyButton.setAttribute('type', 'button'); | ||
copyButton.onclick = function() { copyCode(codeSample); }; | ||
copyButton.classList.add('fas', 'fa-copy'); | ||
|
||
const buttonTooltip = document.createElement('div'); | ||
buttonTooltip.classList.add('c2c-tooltip'); | ||
buttonTooltip.setAttribute('role', 'tooltip'); | ||
buttonTooltip.innerHTML += 'Copy to clipboard'; | ||
|
||
const buttonDiv = document.createElement('div'); | ||
buttonDiv.classList.add('click-to-copy'); | ||
|
||
// Use Popper to create and handle the tooltip behavior. | ||
|
||
const popperInstance = Popper.createPopper(copyButton, buttonTooltip, | ||
{ | ||
modifiers: | ||
[ | ||
{ | ||
name: 'offset', | ||
options: | ||
{ | ||
offset: [0, -48], | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
copyButton.addEventListener('click', () => | ||
{ | ||
buttonTooltip.innerHTML = 'Copied!'; | ||
}); | ||
|
||
copyButton.addEventListener('mouseenter', () => | ||
{ | ||
buttonTooltip.setAttribute('show-tooltip', ''); | ||
|
||
// Enable eventListeners when the code block is on the viewport | ||
|
||
popperInstance.setOptions((options) => ({ | ||
...options, | ||
modifiers: | ||
[ | ||
...options.modifiers, | ||
{ name: 'eventListeners', enabled: true }, | ||
], | ||
})); | ||
popperInstance.update(); | ||
}); | ||
|
||
copyButton.addEventListener('mouseleave', () => | ||
{ | ||
buttonTooltip.removeAttribute('show-tooltip'); | ||
|
||
// Reset the message in case the button was clicked | ||
buttonTooltip.innerHTML = 'Copy to clipboard'; | ||
|
||
// Disble eventListeners when the code block is NOT on the viewport | ||
|
||
popperInstance.setOptions((options) => ({ | ||
...options, | ||
modifiers: | ||
[ | ||
...options.modifiers, | ||
{ name: 'eventListeners', enabled: false }, | ||
], | ||
})); | ||
}); | ||
|
||
buttonDiv.append(copyButton); | ||
buttonDiv.append(buttonTooltip); | ||
codeListings[index].insertBefore(buttonDiv, codeSample); | ||
|
||
} | ||
|
||
function copyCode(codeSample) | ||
{ | ||
navigator.clipboard.writeText(codeSample.textContent.trim()); | ||
} | ||
|
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