flamenco/web/app/src/clipboard.js
Sybren A. Stüvel 3306c7fc8d Web: add support for worker clusters
The support is still fairly minimal. Clusters cannot be managed via the
webapp yet, so the API has to be used directly for that. Workers can be
assigned to clusters via the webapp though.
2023-04-04 12:19:14 +02:00

55 lines
1.6 KiB
JavaScript

/**
* The duration in milliseconds of the "flash" effect, when an element has been
* copied.
*
* Also check `base.css`, `.copied` rule, which defines transition durations.
*/
const flashAfterCopyDuration = 150;
/**
* Copy the inner text of an element to the clipboard.
*
* @param {Event } clickEvent the click event that triggered this function call.
*/
export function copyElementText(clickEvent) {
const sourceElement = clickEvent.target;
copyElementValue(sourceElement, sourceElement.innerText);
}
/**
* Copy the inner text of an element to the clipboard.
*
* @param {Event } clickEvent the click event that triggered this function call.
*/
export function copyElementData(clickEvent) {
const sourceElement = clickEvent.target;
window.sourceElement = sourceElement;
copyElementValue(sourceElement, sourceElement.dataset.clipboard);
}
function copyElementValue(sourceElement, value) {
const inputElement = document.createElement("input");
document.body.appendChild(inputElement);
inputElement.setAttribute("value", value);
inputElement.select();
// Note that the `navigator.clipboard` interface is only available when using
// a secure (HTTPS) connection, which Flamenco Manager will likely not have.
// This is why this code falls back to the deprecated `document.execCommand()`
// call.
// Source: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard
document.execCommand("copy");
document.body.removeChild(inputElement);
flashElement(sourceElement);
}
function flashElement(element) {
element.classList.add("copied");
window.setTimeout(() => {
element.classList.remove("copied");
}, 150);
}