Skip to content

Commit

Permalink
Copy to clupboard (parse-community#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
dblythy authored Nov 17, 2020
1 parent 055a1fe commit 9efae05
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,38 @@
gtag('js', new Date());

gtag('config', 'UA-102564547-3');

window.onload = function () {
function copyToClipboard() {
const textId = this.id.replace('btn','code')
const copyText = document.getElementById(textId);
const text = copyText.textContent || copyText.innerText;

const input = document.createElement('textarea');
input.innerHTML = text;
document.body.appendChild(input);
input.select();
input.setSelectionRange(0, 99999);
document.execCommand('copy');
document.body.removeChild(input);

this.innerHTML = '✔️';
setTimeout(() => {
this.innerHTML = '📋';
},2000)
}
let codes = document.querySelectorAll('.highlight > pre > code');
let count = 0;
codes.forEach((code) => {
code.setAttribute("id", `code-${count}`);
const btn = document.createElement('button');
btn.innerHTML = "📋";
btn.className = "btn-copy";
btn.setAttribute("id", `btn-${count}`);
btn.addEventListener("click",copyToClipboard);
code.before(btn);
count++;
});
}
</script>
</html>
10 changes: 10 additions & 0 deletions css/lib/docs/views/_detail.scss
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@
}
}

.highlight {
position: relative;
}
.btn-copy {
position: absolute;
right: 10px;
background: none;
border: none;
}

.toggles{
text-align: right;
padding-top: 15px;
Expand Down

0 comments on commit 9efae05

Please sign in to comment.