25/assets/js/copy-to-clipboard.js
Anders Englöf Ytterström 89b93cf231
List shared links (#15)
* Exclude not published items in directus client

* Add link view to page controller

* Add page controller links list view to router

* Add links list view CSS

* Add permalink view for link

* Improve page titles

* Let user copy link permalink to clipboard

if alt, shift or ctrl are pressed, fallback to
default behavior.
2024-10-03 14:38:56 +02:00

30 lines
960 B
JavaScript

export const copyCodeToClipboard = () => {
const codeblocks = document.querySelectorAll("pre>code");
for (const b of codeblocks) {
const button = document.createElement("button");
button.innerHTML = "Kopiera";
button.addEventListener("click", function ({ target }) {
const text = target.previousSibling.innerHTML;
navigator.clipboard.writeText(text);
});
b.parentNode.appendChild(button);
}
};
export const copyUrlToClipboard = () => {
const permalinks = document.querySelectorAll(".permalink");
for (const pl of permalinks) {
pl.setAttribute(
"title",
pl.getAttribute("title") + ", klicka för att kopiera",
);
pl.addEventListener("click", function (evt) {
const { target, shiftKey, ctrlKey, altKey } = evt;
if (!shiftKey && !ctrlKey && !altKey) {
evt.preventDefault();
const text = target.href;
navigator.clipboard.writeText(text);
}
});
}
};