Skip to content

Commit 1f966ee

Browse files
committed
Added copy button to crate version
1 parent 5fe0e40 commit 1f966ee

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/librustdoc/html/static/css/rustdoc.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,29 @@ ul.block, .block li, .block ul {
618618
padding-left: var(--sidebar-elems-left-padding);
619619
}
620620

621+
#copy-cargo-snippet {
622+
color: var(--main-color);
623+
background: var(--sidebar-background-color);
624+
margin-left: 10px;
625+
padding: 0;
626+
padding-left: 2px;
627+
border: 0;
628+
font-size: 0;
629+
630+
scale: 80%;
631+
justify-content: center;
632+
}
633+
#copy-cargo-snippet::before {
634+
filter: var(--copy-path-img-filter);
635+
content: var(--clipboard-image);
636+
}
637+
#copy-cargo-snippet:hover::before {
638+
filter: var(--copy-path-img-hover-filter);
639+
}
640+
#copy-cargo-snippet.clicked::before {
641+
content: var(--checkmark-image);
642+
}
643+
621644
.sidebar a {
622645
color: var(--sidebar-link-color);
623646
}
@@ -740,6 +763,9 @@ ul.block, .block li, .block ul {
740763
font-weight: normal;
741764
font-size: 1rem;
742765
overflow-wrap: break-word;
766+
767+
display: flex;
768+
align-items: center;
743769
}
744770

745771
.sidebar-crate + .version {

src/librustdoc/html/static/js/main.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,19 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
18231823
copyButtonAnimation(but);
18241824
};
18251825

1826+
// Copy button that appears next to the crate version.
1827+
const crate_version_copy_button = document.getElementById("copy-cargo-snippet");
1828+
if (!crate_version_copy_button) {
1829+
return;
1830+
}
1831+
crate_version_copy_button.onclick = () => {
1832+
let crate_version = crate_version_copy_button.parentElement.textContent;
1833+
let snippet = `${window.currentCrate} = "${crate_version}"`;
1834+
1835+
copyContentToClipboard(snippet);
1836+
copyButtonAnimation(crate_version_copy_button);
1837+
};
1838+
18261839
// Copy buttons on code examples.
18271840
function copyCode(codeElem) {
18281841
if (!codeElem) {

src/librustdoc/html/templates/page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
<h2> {# #}
101101
<a href="{{page.root_path|safe}}{{display_krate_with_trailing_slash|safe}}index.html">{{display_krate|wrapped|safe}}</a> {# #}
102102
{% if !display_krate_version_number.is_empty() %}
103-
<span class="version">{{+ display_krate_version_number}}</span>
103+
<span class="version">{{+ display_krate_version_number}}<button id="copy-cargo-snippet" title="Copy Cargo.toml snippet to clipboard">{# #}</button></span>
104104
{% endif %}
105105
</h2> {# #}
106106
</div> {# #}

0 commit comments

Comments
 (0)