refactor: Fix eslint warnings (#3698)
* Update dependencies and remove typescript version pinning (fixed upstream) * Fix esling warnings (disabled any and script lang checks) Rewrote clipboard copy (Fixes #3407) Run prettier --------- Co-authored-by: Oleg Lobanov <oleg@lobanov.me>
This commit is contained in:
@@ -1,39 +1,36 @@
|
||||
// Based on code by the following links:
|
||||
// https://stackoverflow.com/a/74528564
|
||||
// https://web.dev/articles/async-clipboard
|
||||
export function copy(text: string) {
|
||||
|
||||
interface ClipboardArgs {
|
||||
text?: string;
|
||||
data?: ClipboardItems;
|
||||
}
|
||||
|
||||
interface ClipboardOpts {
|
||||
permission?: boolean;
|
||||
}
|
||||
|
||||
export function copy(data: ClipboardArgs, opts?: ClipboardOpts) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
if (
|
||||
// Clipboard API requires secure context
|
||||
window.isSecureContext &&
|
||||
typeof navigator.clipboard !== "undefined" &&
|
||||
// @ts-ignore
|
||||
navigator.permissions !== "undefined"
|
||||
typeof navigator.clipboard !== "undefined"
|
||||
) {
|
||||
navigator.permissions
|
||||
// @ts-ignore
|
||||
.query({ name: "clipboard-write" })
|
||||
.then((permission) => {
|
||||
if (permission.state === "granted" || permission.state === "prompt") {
|
||||
// simple writeText should work for all modern browsers
|
||||
navigator.clipboard.writeText(text).then(resolve).catch(reject);
|
||||
} else {
|
||||
reject(new Error("Permission not granted!"));
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
// Firefox doesn't support clipboard-write permission
|
||||
if (navigator.userAgent.indexOf("Firefox") != -1) {
|
||||
navigator.clipboard.writeText(text).then(resolve).catch(reject);
|
||||
} else {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
if (opts?.permission) {
|
||||
getPermission("clipboard-write")
|
||||
.then(() => writeToClipboard(data).then(resolve).catch(reject))
|
||||
.catch(reject);
|
||||
} else {
|
||||
writeToClipboard(data).then(resolve).catch(reject);
|
||||
}
|
||||
} else if (
|
||||
document.queryCommandSupported &&
|
||||
document.queryCommandSupported("copy")
|
||||
document.queryCommandSupported("copy") &&
|
||||
data.text // old method only supports text
|
||||
) {
|
||||
const textarea = createTemporaryTextarea(text);
|
||||
const textarea = createTemporaryTextarea(data.text);
|
||||
const body = document.activeElement || document.body;
|
||||
try {
|
||||
body.appendChild(textarea);
|
||||
@@ -54,6 +51,35 @@ export function copy(text: string) {
|
||||
});
|
||||
}
|
||||
|
||||
function getPermission(name: string) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
typeof navigator.permissions !== "undefined" &&
|
||||
navigator.permissions
|
||||
// @ts-expect-error chrome specific api
|
||||
.query({ name })
|
||||
.then((permission) => {
|
||||
if (permission.state === "granted" || permission.state === "prompt") {
|
||||
resolve();
|
||||
} else {
|
||||
reject(new Error("Permission denied!"));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function writeToClipboard(data: ClipboardArgs) {
|
||||
if (data.text) {
|
||||
return navigator.clipboard.writeText(data.text);
|
||||
}
|
||||
if (data.data) {
|
||||
return navigator.clipboard.write(data.data);
|
||||
}
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
reject(new Error("No data was supplied!"));
|
||||
});
|
||||
}
|
||||
|
||||
const styles = {
|
||||
fontSize: "12pt",
|
||||
position: "fixed",
|
||||
@@ -69,10 +95,10 @@ const styles = {
|
||||
background: "transparent",
|
||||
};
|
||||
|
||||
const createTemporaryTextarea = (text: string) => {
|
||||
function createTemporaryTextarea(text: string) {
|
||||
const textarea = document.createElement("textarea");
|
||||
textarea.value = text;
|
||||
textarea.setAttribute("readonly", "");
|
||||
Object.assign(textarea.style, styles);
|
||||
return textarea;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user