Merge pull request #188 from chen-jingjie/main

fix: http cannot be copied
This commit is contained in:
ryjiang
2023-05-12 14:54:16 +08:00
committed by GitHub

View File

@@ -13,12 +13,29 @@ const CopyButton: FC<CopyButtonProps> = props => {
const { t: commonTrans } = useTranslation();
const copyTrans = commonTrans('copy');
const [tooltipTitle, setTooltipTitle] = useState('Copy');
const unsecuredCopyToClipboard = (v: string) => {
const textArea = document.createElement("textarea");
textArea.style.position = 'fixed';
textArea.style.opacity = '0';
textArea.style.zIndex = '-1000';
textArea.value = v;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand('copy');
} catch (err) {
console.error('Unable to copy to clipboard', err);
}
document.body.removeChild(textArea);
}
const handleClick = (event: React.MouseEvent<HTMLElement>, v: string) => {
event.stopPropagation();
setTooltipTitle(copyTrans.copied);
navigator.clipboard.writeText(v);
(navigator.clipboard?.writeText ?? unsecuredCopyToClipboard)?.(v);
setTimeout(() => {
setTooltipTitle(copyTrans.copy);
}, 1000);