fix: clipboard copy in safari

This commit is contained in:
Kloon ImKloon 2024-05-27 20:34:55 +02:00
parent 6ca3d5a573
commit 27d271a375
No known key found for this signature in database
GPG Key ID: CCF1C86A995C5B6A

View File

@ -1,9 +1,11 @@
// Based on code provided by Amir Fo
// Based on code by the following links:
// https://stackoverflow.com/a/74528564
// https://web.dev/articles/async-clipboard
export function copy(text: string) {
return new Promise<void>((resolve, reject) => {
if (
typeof navigator !== "undefined" &&
// Clipboard API requires secure context
window.isSecureContext &&
typeof navigator.clipboard !== "undefined" &&
// @ts-ignore
navigator.permissions !== "undefined"
@ -13,10 +15,8 @@ export function copy(text: string) {
.query({ name: "clipboard-write" })
.then((permission) => {
if (permission.state === "granted" || permission.state === "prompt") {
const type = "text/plain";
const blob = new Blob([text], { type });
const data = [new ClipboardItem({ [type]: blob })];
navigator.clipboard.write(data).then(resolve).catch(reject);
// simple writeText should work for all modern browsers
navigator.clipboard.writeText(text).then(resolve).catch(reject);
} else {
reject(new Error("Permission not granted!"));
}
@ -66,13 +66,13 @@ const styles = {
border: "none",
outline: "none",
boxShadow: "none",
background: "transparent"
background: "transparent",
};
const createTemporaryTextarea = (text:string) => {
const createTemporaryTextarea = (text: string) => {
const textarea = document.createElement("textarea");
textarea.value = text;
textarea.setAttribute("readonly", "");
Object.assign(textarea.style, styles);
return textarea;
};
};