-
Couldn't load subscription status.
- Fork 13.9k
Add copy code to clipboard button for rustdoc
#119929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ac21818
324c041
da9546f
e3d800d
f614f5d
65c591b
8aebe6e
c92dcd9
a323bf8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1734,6 +1734,37 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm | |||||
| resizer.addEventListener("pointerdown", initResize, false); | ||||||
| }()); | ||||||
|
|
||||||
| // This section handles the copy buttons that appears in top right corner of the code blocks | ||||||
| (function() { | ||||||
| let preElements = document.querySelectorAll("pre:has(code)"); | ||||||
| preElements.forEach(function(pre, index) { | ||||||
| let resetTimeout = null; | ||||||
| let copyBtn = pre.querySelector("button[class='copy-code']"); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| let icon = copyBtn.innerHTML; | ||||||
| pre.addEventListener("mouseenter", function() { | ||||||
| copyBtn.style.opacity = "1"; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to express this in pure CSS with: pre:hover > button.copy-code { opacity: 1; } |
||||||
| copyBtn.addEventListener("click", function() { | ||||||
| let code = pre.querySelector("code"); | ||||||
| let summary = code.querySelector("summary"); | ||||||
| let content = code.textContent; | ||||||
| content = content.replace(summary.textContent, ""); | ||||||
| navigator.clipboard.writeText(content).then(function() { | ||||||
| copyBtn.textContent = "✓"; | ||||||
| if (resetTimeout !== null) { | ||||||
| clearTimeout(resetTimeout); | ||||||
| } | ||||||
| resetTimeout = setTimeout(function() { | ||||||
| copyBtn.innerHTML = icon; | ||||||
| }, 1000); | ||||||
| }); | ||||||
| }); | ||||||
| }); | ||||||
| pre.addEventListener("mouseleave", function() { | ||||||
| copyBtn.style.opacity = "0"; | ||||||
| }); | ||||||
| }); | ||||||
| }()); | ||||||
|
|
||||||
| // This section handles the copy button that appears next to the path breadcrumbs | ||||||
| (function() { | ||||||
| let reset_button_timeout = null; | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:hasis not supported in Firefox Mobile: https://caniuse.com/css-has