28 lines
929 B
JavaScript
28 lines
929 B
JavaScript
// Copies text to the system clipboard, mirroring the old app's behavior:
|
|
// navigator.clipboard when available, with a hidden-textarea fallback for
|
|
// webviews that lack the async clipboard API.
|
|
export const copyTextToClipboard = async (text) => {
|
|
if (typeof navigator !== "undefined" && navigator.clipboard && typeof navigator.clipboard.writeText === "function") {
|
|
await navigator.clipboard.writeText(text);
|
|
return;
|
|
}
|
|
|
|
if (typeof document === "undefined" || typeof document.createElement !== "function") {
|
|
return;
|
|
}
|
|
|
|
const textArea = document.createElement("textarea");
|
|
textArea.value = text;
|
|
textArea.setAttribute("readonly", "");
|
|
textArea.style.position = "absolute";
|
|
textArea.style.left = "-9999px";
|
|
document.body.appendChild(textArea);
|
|
textArea.select();
|
|
|
|
if (typeof document.execCommand === "function") {
|
|
document.execCommand("copy");
|
|
}
|
|
|
|
document.body.removeChild(textArea);
|
|
};
|