Skip to content

Commit

Permalink
Rollup merge of rust-lang#84740 - r00ster91:patch-6, r=GuillaumeGomez
Browse files Browse the repository at this point in the history
Reset the docs' copy path button after 1 second

I like that this copy path button on the top next to the type/module's name changes to a check mark when you successfully clicked and copied the path but I find it really weird how the icon stays that check mark forever after the first time of clicking it. Imagine you leave that documentation tab open and come back after 2 hours and you still see that check mark in that box because you copied the path 2 hours ago. You will probably be confused and you might've forgotten what that button even does (even more so currently where this is a new feature, or when you simply don't use it often), so I really think at some point it should go back to the ⎘ icon which, at least to me, pretty clearly indicates copying, whereas the check mark (if it stays there for so long) could falsely look like a verification mark indicating "this module is verified" or something like that.
I believe after a longer period of time it's not logical to still tell the user "yes you've copied this successful".

In addition to this timeout, maybe it could be made so that you can't copy again until this cooldown of 1 second is over, but I'm not sure how useful or user-friendly that feature would be so maybe it's fine the way it is now.
Also the timeout is cleared every time you click again so if you constantly click it, it won't reset during that.
  • Loading branch information
JohnTitor authored May 1, 2021
2 parents 7c87bec + bea99a5 commit 4cb2f9f
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1490,27 +1490,42 @@ function hideThemeButtonState() {
searchState.setup();
}());

function copy_path(but) {
var parent = but.parentElement;
var path = [];
(function () {
var reset_button_timeout = null;

onEach(parent.childNodes, function(child) {
if (child.tagName === 'A') {
path.push(child.textContent);
}
});
window.copy_path = function(but) {
var parent = but.parentElement;
var path = [];

var el = document.createElement('textarea');
el.value = 'use ' + path.join('::') + ';';
el.setAttribute('readonly', '');
// To not make it appear on the screen.
el.style.position = 'absolute';
el.style.left = '-9999px';
onEach(parent.childNodes, function(child) {
if (child.tagName === 'A') {
path.push(child.textContent);
}
});

document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
var el = document.createElement('textarea');
el.value = 'use ' + path.join('::') + ';';
el.setAttribute('readonly', '');
// To not make it appear on the screen.
el.style.position = 'absolute';
el.style.left = '-9999px';

but.textContent = '✓';
}
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);

but.textContent = '✓';

if (reset_button_timeout !== null) {
window.clearTimeout(reset_button_timeout);
}

function reset_button() {
but.textContent = '⎘';
reset_button_timeout = null;
}

reset_button_timeout = window.setTimeout(reset_button, 1000);
};
}());

0 comments on commit 4cb2f9f

Please sign in to comment.