mirror of
https://github.com/pheralb/svgl.git
synced 2025-12-29 08:01:36 +08:00
27 lines
741 B
TypeScript
27 lines
741 B
TypeScript
export function copyToClipboard(value: string) {
|
|
try {
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
// @ts-ignore
|
|
if (window.copy) {
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
// @ts-ignore
|
|
window.copy(value);
|
|
} else {
|
|
const area = document.createElement("textarea");
|
|
document.body.appendChild(area);
|
|
area.value = value;
|
|
// area.focus();
|
|
area.select();
|
|
const result = document.execCommand("copy");
|
|
document.body.removeChild(area);
|
|
if (!result) {
|
|
throw new Error();
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error(`Unable to copy the value: ${value} - Error: ${error}`);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|