Skip to content

Commit

Permalink
docs(code): Introduce copy code feature
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisommore committed Aug 2, 2021
1 parent fe41b8e commit b96f4d8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
11 changes: 10 additions & 1 deletion docs/themes/porter/assets/sass/docs-content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
color: black;
border: none;
padding: 0.75em 1em;
margin: 0.25em 0 2.5em;
margin: 0 0 2.5em;
font-size: 1rem;

code {
Expand Down Expand Up @@ -280,3 +280,12 @@
min-height: 320px;
}
}

.copy-button-container {
display: flex;
justify-content: flex-end;
.copy-button {
margin-bottom: 0;
padding: 4px;
}
}
1 change: 1 addition & 0 deletions docs/themes/porter/layouts/partials/js.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
<script src="/js/main.min.js"></script>
<script src="/js/custom/app_init.js"></script>
<script src="/js/custom/prism.js"></script>
<script src="/js/custom/copy_code.js"></script>
29 changes: 29 additions & 0 deletions docs/themes/porter/static/js/custom/copy_code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
document.querySelectorAll("pre").forEach(ele => {
var codeElement = ele.querySelector("code")

if (codeElement != null) {
with (document) {
var copyButtonContainer = createElement('div')
copyButtonContainer.classList.add("copy-button-container")
var copyButton = createElement('button');
copyButtonContainer.append(copyButton)
}

with (copyButton) {
className = "copy-button"
innerText = 'copy';

addEventListener('click', () => {
navigator.clipboard.writeText(codeElement.innerText).then(e => {
innerText = "copied"
}).catch(e => {
innerText = "error"
})
setTimeout(() => innerText = "copy", 900)
});
}

ele.parentElement.insertBefore(copyButtonContainer, ele)
}
})

0 comments on commit b96f4d8

Please sign in to comment.