add(ltp) .function anchor copy link to clipboard

This commit is contained in:
Aire-One 2022-01-02 17:12:38 +01:00 committed by Emmanuel Lepage Vallee
parent fb8f7cfadd
commit e50b41b10d
2 changed files with 58 additions and 1 deletions

View File

@ -649,3 +649,17 @@ pre .url { color: #272fc2; text-decoration: underline; }
color: #00000044;
margin-top: 15px;
}
.copy-link {
font-size: 9px;
padding: 2px;
border-radius: 9px;
vertical-align: middle;
text-decoration: none;
}
.copy-link--success {
background-color: green;
}
.copy-link--failure {
background-color: red;
}

View File

@ -339,7 +339,7 @@
# end
# for item in iter(k.items) do if not item.tags.hidden then
<dt>
<a name = "$(item.name)"></a>
<a class="copy-link js-copy-link" name="$(item.name)" href="#$(item.name)">&#128279;</a>
<strong>$(display_name(item))</strong>
# if item.display_inheritance then
<span class="inheritance">
@ -559,6 +559,49 @@
target.classList.remove("open");
}
});
const copyResultClasses = {
success : "copy-link--success",
failure: "copy-link--failure"
};
const removeCopyResultClasses = ($target) =>
Object.values(copyResultClasses).forEach(c => $target.classList.remove(c));
document.querySelectorAll(".js-copy-link").forEach(copyLink => {
copyLink.addEventListener("click", function(e) {
e.preventDefault();
const $target = e.target;
removeCopyResultClasses($target);
let link = $target.href;
if (!link) {
return;
}
if (link.startsWith("#")) {
const curr = window.location.pathname;
link = curr.substring(0, curr.indexOf("#")) + link;
}
// We need to create a fake element to copy the text from
const fakeElement = document.createElement("textarea");
fakeElement.value = link;
document.body.appendChild(fakeElement);
fakeElement.select();
let success = false;
try {
success = document.execCommand("copy");
} catch(err) {
success = false;
}
fakeElement.remove();
$target.classList.add(success ? copyResultClasses.success : copyResultClasses.failure);
setInterval(() => removeCopyResultClasses($target), 1500);
});
});
</script>
</body>