-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from zackAJ/feat-copy_clipboard
- Loading branch information
Showing
5 changed files
with
91 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,45 @@ | ||
const codeBlocks = document.querySelectorAll('pre:has(code)'); | ||
|
||
//add copy btn to every code block on the dom | ||
codeBlocks.forEach((code) => { | ||
//button icon | ||
const use = document.createElementNS('http://www.w3.org/2000/svg', 'use'); | ||
use.setAttribute('href', '/copy.svg#empty'); | ||
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); | ||
svg.classList.add('copy-svg'); | ||
svg.appendChild(use); | ||
|
||
//create button | ||
const btn = document.createElement('button'); | ||
btn.appendChild(svg); | ||
btn.classList.add('copy-btn'); | ||
btn.addEventListener('click', (e) => copyCode(e)); | ||
|
||
//container to fix copy button | ||
const container = document.createElement('div'); | ||
container.classList.add('copy-cnt'); | ||
container.appendChild(btn); | ||
|
||
//add to code block | ||
code.classList.add('relative'); | ||
code.appendChild(container); | ||
}); | ||
|
||
/** | ||
* @param {MouseEvent} event | ||
*/ | ||
function copyCode(event) { | ||
let codeBlock = getChildByTagName(event.currentTarget.parentElement.parentElement, 'CODE') | ||
navigator.clipboard.writeText(codeBlock.innerText) | ||
const use = getChildByTagName(getChildByTagName(event.currentTarget, 'svg'), 'use'); | ||
use.setAttribute('href', '/copy.svg#filled') | ||
setTimeout(() => { | ||
if (use) { | ||
use.setAttribute('href', '/copy.svg#empty') | ||
} | ||
}, 100); | ||
} | ||
|
||
function getChildByTagName(element, tagName) { | ||
return Array.from(element.children).find((child) => child.tagName === tagName); | ||
} |
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