Skip to content

Commit

Permalink
fix: the copy method in clipboard.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
gdy666 committed Apr 29, 2024
1 parent 236ca63 commit efec1af
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions frontend/src/utils/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,18 @@ export function copy(text: string) {
document.queryCommandSupported &&
document.queryCommandSupported("copy")
) {
const textarea = document.createElement("textarea");
textarea.textContent = text;
textarea.setAttribute("readonly", "");
textarea.style.fontSize = "12pt";
textarea.style.position = "fixed";
textarea.style.width = "2em";
textarea.style.height = "2em";
textarea.style.padding = "0";
textarea.style.margin = "0";
textarea.style.border = "none";
textarea.style.outline = "none";
textarea.style.boxShadow = "none";
textarea.style.background = "transparent";
document.body.appendChild(textarea);
textarea.focus();
textarea.select();
const textarea = createTemporaryTextarea(text);
const body = document.activeElement || document.body;
try {
body.appendChild(textarea);
textarea.focus();
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
resolve();
} catch (e) {
document.body.removeChild(textarea);
reject(e);
} finally {
body.removeChild(textarea);
}
} else {
reject(
Expand All @@ -64,3 +53,26 @@ export function copy(text: string) {
}
});
}

const styles = {
fontSize: "12pt",
position: "fixed",
top: 0,
left: 0,
width: "2em",
height: "2em",
padding: 0,
margin: 0,
border: "none",
outline: "none",
boxShadow: "none",
background: "transparent"

Check warning on line 69 in frontend/src/utils/clipboard.ts

View workflow job for this annotation

GitHub Actions / lint-frontend

Insert `,`
};

const createTemporaryTextarea = (text:string) => {

Check warning on line 72 in frontend/src/utils/clipboard.ts

View workflow job for this annotation

GitHub Actions / lint-frontend

Insert `路`
const textarea = document.createElement("textarea");
textarea.value = text;
textarea.setAttribute("readonly", "");
Object.assign(textarea.style, styles);
return textarea;
};

Check warning on line 78 in frontend/src/utils/clipboard.ts

View workflow job for this annotation

GitHub Actions / lint-frontend

Insert `鈴巂

0 comments on commit efec1af

Please sign in to comment.