-
Notifications
You must be signed in to change notification settings - Fork 19
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 #168 from CloudBytes-Academy/copy
FEAT: Copy to clipboard feature
- Loading branch information
Showing
3 changed files
with
92 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
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,52 @@ | ||
const copyToClipboardDefaultText = { | ||
innerText: "Copy", | ||
ariaLabel: "Copy to clipboard", | ||
}; | ||
const copyToClipboardSuccessText = { | ||
innerText: "Copied!", | ||
ariaLabel: "Copied to clipboard", | ||
}; | ||
|
||
// Get all pre. But ignore line numbers section | ||
document.querySelectorAll("div.highlight pre").forEach((snippet) => { | ||
// create div.codecopy | ||
const wrapper = document.createElement("div"); | ||
wrapper.classList.add("codecopy"); | ||
|
||
// Wrap code inside div.codecopy | ||
const parent = snippet.parentNode; | ||
parent.replaceChild(wrapper, snippet); | ||
wrapper.appendChild(snippet); | ||
|
||
// Create button | ||
const button = ` | ||
<button | ||
class="codecopy-btn" | ||
title=${copyToClipboardDefaultText.ariaLabel} | ||
aria-label=${copyToClipboardDefaultText.ariaLabel} | ||
>${copyToClipboardDefaultText.innerText} | ||
</button>`; | ||
|
||
// Add button to div.codecopy | ||
wrapper.insertAdjacentHTML("afterbegin", button); | ||
}); | ||
|
||
// Add copy to clipboard functionality | ||
const clipboard = new ClipboardJS(".codecopy-btn", { | ||
target: (trigger) => { | ||
return trigger.parentNode; | ||
}, | ||
}); | ||
|
||
// Show message on success | ||
clipboard.on("success", (e) => { | ||
e.trigger.innerText = copyToClipboardSuccessText.innerText; | ||
e.trigger.setAttribute("aria-label", copyToClipboardSuccessText.ariaLabel); | ||
e.clearSelection(); | ||
|
||
// Reset button text | ||
setTimeout(() => { | ||
e.trigger.innerText = copyToClipboardDefaultText.innerText; | ||
e.trigger.setAttribute("aria-label", copyToClipboardDefaultText.ariaLabel); | ||
}, 400); | ||
}); |
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
<link rel="dns-prefetch" href="https://cdn.jsdelivr.net" /> | ||
<link rel="dns-prefetch" href="https://www.googletagmanager.com" /> | ||
<link rel="prefetch" as="script" href="/{{ THEME_STATIC_DIR }}/js/app.js" /> | ||
<link rel="prefetch" as="script" href="/{{ THEME_STATIC_DIR }}/js/copy.js" /> | ||
|
||
<!-- Title --> | ||
<title>{% block title%}{% endblock %}</title> | ||
|
@@ -52,6 +53,7 @@ | |
|
||
<!-- App --> | ||
<script defer src="/{{ THEME_STATIC_DIR }}/js/app.js"></script> | ||
<script defer src="/{{ THEME_STATIC_DIR }}/js/copy.js"></script> | ||
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-0503340532690886" | ||
crossorigin="anonymous"></script> | ||
</head> | ||
|
@@ -133,6 +135,8 @@ | |
{% include "layouts/footer.html" %} | ||
</footer> | ||
</body> | ||
<!-- Code Copy JS--> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/clipboard.min.js"></script> | ||
<!-- Include AlgoliaSearch JS Client and autocomplete.js library --> | ||
<script src="https://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/autocomplete.js/0/autocomplete.min.js"></script> | ||
|