feat: lots updates

This commit is contained in:
zerosoul
2022-03-02 17:21:16 +08:00
parent 834c379e7c
commit b6b0a0c5ef
38 changed files with 1971 additions and 501 deletions
+42
View File
@@ -0,0 +1,42 @@
import { useState } from "react";
const useCopy = () => {
const copyToClipboard = (str) => {
const el = document.createElement("textarea");
el.value = str;
el.setAttribute("readonly", "");
el.style.position = "absolute";
el.style.left = "-9999px";
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0
? document.getSelection().getRangeAt(0)
: false;
el.select();
const success = document.execCommand("copy");
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
return success;
};
const [copied, setCopied] = useState(false);
const copy = (text) => {
let inter = 0;
if (!copied) {
setCopied(copyToClipboard(text));
inter = setTimeout(() => {
setCopied(false);
}, 500);
}
return () => {
clearTimeout(inter);
};
};
return [copied, copy];
};
export default useCopy;